Drag and drop 2.5 integration! Finally, slashdot regulars can use
[blender.git] / source / blender / editors / screen / area.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_screen.h"
44 #include "BKE_utildefines.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48 #include "wm_subwindow.h"
49
50 #include "ED_screen.h"
51 #include "ED_screen_types.h"
52 #include "ED_types.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56 #include "BLF_api.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #ifndef DISABLE_PYTHON
63 #include "BPY_extern.h"
64 #endif
65
66 #include "screen_intern.h"
67
68 /* general area and region code */
69
70 static void region_draw_emboss(ARegion *ar, rcti *scirct)
71 {
72         rcti rect;
73         
74         /* translate scissor rect to region space */
75         rect.xmin= scirct->xmin - ar->winrct.xmin;
76         rect.ymin= scirct->ymin - ar->winrct.ymin;
77         rect.xmax= scirct->xmax - ar->winrct.xmin;
78         rect.ymax= scirct->ymax - ar->winrct.ymin;
79         
80         /* set transp line */
81         glEnable( GL_BLEND );
82         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
83         
84         /* right  */
85         glColor4ub(0,0,0, 50);
86         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
87         
88         /* bottom  */
89         glColor4ub(0,0,0, 80);
90         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
91         
92         /* top  */
93         glColor4ub(255,255,255, 60);
94         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
95
96         /* left  */
97         glColor4ub(255,255,255, 50);
98         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
99         
100         glDisable( GL_BLEND );
101 }
102
103 void ED_region_pixelspace(ARegion *ar)
104 {
105         int width= ar->winrct.xmax-ar->winrct.xmin+1;
106         int height= ar->winrct.ymax-ar->winrct.ymin+1;
107         
108         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
109         wmLoadIdentity();
110 }
111
112 /* only exported for WM */
113 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
114 {
115         /* generic notes first */
116         switch(note->category) {
117                 case NC_WM:
118                         if(note->data==ND_FILEREAD)
119                                 ED_region_tag_redraw(ar);
120                         break;
121                 case NC_WINDOW:
122                         ED_region_tag_redraw(ar);
123                         break;
124 #ifndef WM_FAST_DRAW
125                 case NC_SCREEN:
126                         if(note->action==NA_EDITED)
127                                 ED_region_tag_redraw(ar);
128                         /* pass on */
129 #endif
130                 default:
131                         if(ar->type && ar->type->listener)
132                                 ar->type->listener(ar, note);
133         }
134 }
135
136 /* only exported for WM */
137 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
138 {
139         /* no generic notes? */
140         if(sa->type && sa->type->listener) {
141                 sa->type->listener(sa, note);
142         }
143 }
144
145 /* only exported for WM */
146 void ED_area_do_refresh(bContext *C, ScrArea *sa)
147 {
148         /* no generic notes? */
149         if(sa->type && sa->type->refresh) {
150                 sa->type->refresh(C, sa);
151         }
152         sa->do_refresh= 0;
153 }
154
155 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
156 /* only exported for WM */
157 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
158 {
159         AZone *az;
160         
161         for(az= sa->actionzones.first; az; az= az->next) {
162                 int xs, ys;
163                 
164                 xs= (az->x1+az->x2)/2;
165                 ys= (az->y1+az->y2)/2;
166
167                 /* test if inside */
168                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
169                         az->do_draw= 1;
170                 }
171         }
172 }
173
174 static void area_draw_azone(short x1, short y1, short x2, short y2)
175 {
176         float xmin = x1;
177         float xmax = x2-2;
178         float ymin = y1-1;
179         float ymax = y2-3;
180         
181         float dx= 0.3f*(xmax-xmin);
182         float dy= 0.3f*(ymax-ymin);
183         
184         glColor4ub(255, 255, 255, 80);
185         fdrawline(xmin, ymax, xmax, ymin);
186         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
187         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
188         
189         glColor4ub(0, 0, 0, 150);
190         fdrawline(xmin, ymax+1, xmax+1, ymin);
191         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
192         fdrawline(xmin, ymax-2*dy+1, xmax-2*dx+1, ymin);
193 }
194
195
196 static void region_draw_azone(ScrArea *sa, AZone *az)
197 {
198         GLUquadricObj *qobj = NULL; 
199         short midx = az->x1 + (az->x2 - az->x1)/2;
200         short midy = az->y1 + (az->y2 - az->y1)/2;
201         
202         if(az->ar==NULL) return;
203         
204         /* only display action zone icons when the region is hidden */
205         if (!(az->ar->flag & RGN_FLAG_HIDDEN)) return;
206         
207         qobj = gluNewQuadric();
208         
209         glPushMatrix();         
210         glTranslatef(midx, midy, 0.); 
211         
212         /* outlined circle */
213         glEnable(GL_LINE_SMOOTH);
214
215         glColor4f(1.f, 1.f, 1.f, 0.8f);
216
217         gluQuadricDrawStyle(qobj, GLU_FILL); 
218         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
219
220         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
221         
222         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
223         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
224         
225         glDisable(GL_LINE_SMOOTH);
226         
227         glPopMatrix();
228         gluDeleteQuadric(qobj);
229         
230         /* + */
231         sdrawline(midx, midy-2, midx, midy+3);
232         sdrawline(midx-2, midy, midx+3, midy);
233 }
234
235
236 /* only exported for WM */
237 void ED_area_overdraw(bContext *C)
238 {
239         wmWindow *win= CTX_wm_window(C);
240         bScreen *screen= CTX_wm_screen(C);
241         ScrArea *sa;
242         
243         /* Draw AZones, in screenspace */
244         wmSubWindowSet(win, screen->mainwin);
245
246         glEnable( GL_BLEND );
247         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
248         
249         for(sa= screen->areabase.first; sa; sa= sa->next) {
250                 AZone *az;
251                 for(az= sa->actionzones.first; az; az= az->next) {
252                         if(az->do_draw) {
253                                 if(az->type==AZONE_AREA) {
254                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
255                                 } else if(az->type==AZONE_REGION) {
256                                         region_draw_azone(sa, az);
257                                 }
258                                 
259                                 az->do_draw= 0;
260                         }
261                 }
262         }       
263         glDisable( GL_BLEND );
264         
265 }
266
267 /* get scissor rect, checking overlapping regions */
268 void region_scissor_winrct(ARegion *ar, rcti *winrct)
269 {
270         *winrct= ar->winrct;
271         
272         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
273                 return;
274
275         while(ar->prev) {
276                 ar= ar->prev;
277                 
278                 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
279                         if(ar->flag & RGN_FLAG_HIDDEN);
280                         else if(ar->alignment & RGN_SPLIT_PREV);
281                         else if(ar->alignment==RGN_OVERLAP_LEFT) {
282                                 winrct->xmin= ar->winrct.xmax + 1;
283                         }
284                         else if(ar->alignment==RGN_OVERLAP_RIGHT) {
285                                 winrct->xmax= ar->winrct.xmin - 1;
286                         }
287                         else break;
288                 }
289         }
290 }
291
292 /* only exported for WM */
293 /* makes region ready for drawing, sets pixelspace */
294 void ED_region_set(const bContext *C, ARegion *ar)
295 {
296         wmWindow *win= CTX_wm_window(C);
297         ScrArea *sa= CTX_wm_area(C);
298         rcti winrct;
299         
300         /* checks other overlapping regions */
301         region_scissor_winrct(ar, &winrct);
302         
303         ar->drawrct= winrct;
304         
305         /* note; this sets state, so we can use wmOrtho and friends */
306         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
307         
308         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
309         
310         ED_region_pixelspace(ar);
311 }
312
313
314 /* only exported for WM */
315 void ED_region_do_draw(bContext *C, ARegion *ar)
316 {
317         wmWindow *win= CTX_wm_window(C);
318         ScrArea *sa= CTX_wm_area(C);
319         ARegionType *at= ar->type;
320         rcti winrct;
321         
322         /* checks other overlapping regions */
323         region_scissor_winrct(ar, &winrct);
324         
325         /* if no partial draw rect set, full rect */
326         if(ar->drawrct.xmin == ar->drawrct.xmax)
327                 ar->drawrct= winrct;
328         else {
329                 /* extra clip for safety */
330                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
331                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
332                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
333                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
334         }
335         
336         /* note; this sets state, so we can use wmOrtho and friends */
337         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
338         
339         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
340         
341         /* optional header info instead? */
342         if(ar->headerstr) {
343                 float col[3];
344                 UI_GetThemeColor3fv(TH_HEADER, col);
345                 glClearColor(col[0], col[1], col[2], 0.0);
346                 glClear(GL_COLOR_BUFFER_BIT);
347                 
348                 UI_ThemeColor(TH_TEXT);
349                 BLF_draw_default(20, 8, 0.0f, ar->headerstr);
350         }
351         else if(at->draw) {
352                 at->draw(C, ar);
353         }
354         
355         uiFreeInactiveBlocks(C, &ar->uiblocks);
356         
357         if(sa)
358                 region_draw_emboss(ar, &winrct);
359         
360         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
361         ED_region_pixelspace(ar);
362         
363         ar->do_draw= 0;
364         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
365 }
366
367 /* **********************************
368    maybe silly, but let's try for now
369    to keep these tags protected
370    ********************************** */
371
372 void ED_region_tag_redraw(ARegion *ar)
373 {
374         if(ar) {
375                 /* zero region means full region redraw */
376                 ar->do_draw= 1;
377                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
378         }
379 }
380
381 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
382 {
383         if(ar) {
384                 if(!ar->do_draw) {
385                         /* no redraw set yet, set partial region */
386                         ar->do_draw= 1;
387                         ar->drawrct= *rct;
388                 }
389                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
390                         /* partial redraw already set, expand region */
391                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
392                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
393                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
394                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
395                 }
396         }
397 }
398
399 void ED_area_tag_redraw(ScrArea *sa)
400 {
401         ARegion *ar;
402         
403         if(sa)
404                 for(ar= sa->regionbase.first; ar; ar= ar->next)
405                         ED_region_tag_redraw(ar);
406 }
407
408 void ED_area_tag_refresh(ScrArea *sa)
409 {
410         if(sa)
411                 sa->do_refresh= 1;
412 }
413
414 /* *************************************************************** */
415
416 /* use NULL to disable it */
417 void ED_area_headerprint(ScrArea *sa, const char *str)
418 {
419         ARegion *ar;
420         
421         for(ar= sa->regionbase.first; ar; ar= ar->next) {
422                 if(ar->regiontype==RGN_TYPE_HEADER) {
423                         if(str) {
424                                 if(ar->headerstr==NULL)
425                                         ar->headerstr= MEM_mallocN(256, "headerprint");
426                                 BLI_strncpy(ar->headerstr, str, 256);
427                         }
428                         else if(ar->headerstr) {
429                                 MEM_freeN(ar->headerstr);
430                                 ar->headerstr= NULL;
431                         }
432                         ED_region_tag_redraw(ar);
433                 }
434         }
435 }
436
437 /* ************************************************************ */
438
439
440 #define AZONESPOT               12
441 static void area_azone_initialize(ScrArea *sa) 
442 {
443         AZone *az;
444         
445         /* reinitalize entirely, regions add azones too */
446         BLI_freelistN(&sa->actionzones);
447         
448         /* set area action zones */
449         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
450         BLI_addtail(&(sa->actionzones), az);
451         az->type= AZONE_AREA;
452         az->x1= sa->totrct.xmin;
453         az->y1= sa->totrct.ymin;
454         az->x2= sa->totrct.xmin + AZONESPOT-1;
455         az->y2= sa->totrct.ymin + AZONESPOT-1;
456         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
457         
458         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
459         BLI_addtail(&(sa->actionzones), az);
460         az->type= AZONE_AREA;
461         az->x1= sa->totrct.xmax+1;
462         az->y1= sa->totrct.ymax+1;
463         az->x2= sa->totrct.xmax-AZONESPOT+1;
464         az->y2= sa->totrct.ymax-AZONESPOT+1;
465         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
466 }
467
468 #define AZONEPAD_EDGE   4
469 #define AZONEPAD_ICON   8
470 static void region_azone_edge(AZone *az, ARegion *ar)
471 {
472         if(az->edge=='t') {
473                 az->x1= ar->winrct.xmin;
474                 az->y1= ar->winrct.ymax - AZONEPAD_EDGE;
475                 az->x2= ar->winrct.xmax;
476                 az->y2= ar->winrct.ymax;
477         }
478         else if(az->edge=='b') {
479                 az->x1= ar->winrct.xmin;
480                 az->y1= ar->winrct.ymin + AZONEPAD_EDGE;
481                 az->x2= ar->winrct.xmax;
482                 az->y2= ar->winrct.ymin;
483         }
484         else if(az->edge=='l') {
485                 az->x1= ar->winrct.xmin;
486                 az->y1= ar->winrct.ymin;
487                 az->x2= ar->winrct.xmin + AZONEPAD_EDGE;
488                 az->y2= ar->winrct.ymax;
489         }
490         else { // if(az->edge=='r') {
491                 az->x1= ar->winrct.xmax;
492                 az->y1= ar->winrct.ymin;
493                 az->x2= ar->winrct.xmax - AZONEPAD_EDGE;
494                 az->y2= ar->winrct.ymax;
495         }
496
497         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
498 }
499
500 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
501 {
502         AZone *azt;
503         int tot=0;
504         
505         for(azt= sa->actionzones.first; azt; azt= azt->next) {
506                 if(azt->edge == az->edge) tot++;
507         }
508         
509         if(az->edge=='t') {
510                 az->x1= ar->winrct.xmax - tot*2*AZONEPAD_ICON;
511                 az->y1= ar->winrct.ymax + AZONEPAD_ICON;
512                 az->x2= ar->winrct.xmax - tot*AZONEPAD_ICON;
513                 az->y2= ar->winrct.ymax + 2*AZONEPAD_ICON;
514         }
515         else if(az->edge=='b') {
516                 az->x1= ar->winrct.xmin + AZONEPAD_ICON;
517                 az->y1= ar->winrct.ymin - 2*AZONEPAD_ICON;
518                 az->x2= ar->winrct.xmin + 2*AZONEPAD_ICON;
519                 az->y2= ar->winrct.ymin - AZONEPAD_ICON;
520         }
521         else if(az->edge=='l') {
522                 az->x1= ar->winrct.xmin - 2*AZONEPAD_ICON;
523                 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
524                 az->x2= ar->winrct.xmin - AZONEPAD_ICON;
525                 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
526         }
527         else { // if(az->edge=='r') {
528                 az->x1= ar->winrct.xmax + AZONEPAD_ICON;
529                 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
530                 az->x2= ar->winrct.xmax + 2*AZONEPAD_ICON;
531                 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
532         }
533
534         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
535         
536         /* if more azones on 1 spot, set offset */
537         for(azt= sa->actionzones.first; azt; azt= azt->next) {
538                 if(az!=azt) {
539                         if( ABS(az->x1-azt->x1) < 2 && ABS(az->y1-azt->y1) < 2) {
540                                 if(az->edge=='t' || az->edge=='b') {
541                                         az->x1+= AZONESPOT;
542                                         az->x2+= AZONESPOT;
543                                         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
544                                 }
545                                 else {
546                                         az->y1-= AZONESPOT;
547                                         az->y2-= AZONESPOT;
548                                         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
549                                 }
550                         }
551                 }
552         }
553 }
554
555 static void region_azone_initialize(ScrArea *sa, ARegion *ar, char edge) 
556 {
557         AZone *az;
558         
559         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
560         BLI_addtail(&(sa->actionzones), az);
561         az->type= AZONE_REGION;
562         az->ar= ar;
563         az->edge= edge;
564         
565         if (ar->flag & RGN_FLAG_HIDDEN) {
566                 region_azone_icon(sa, az, ar);
567         } else {
568                 region_azone_edge(az, ar);
569         }
570         
571 }
572
573
574 /* *************************************************************** */
575
576 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
577 {
578          /* edge code (t b l r) is where azone will be drawn */
579         
580         if(alignment==RGN_ALIGN_TOP)
581                 region_azone_initialize(sa, ar, 'b');
582         else if(alignment==RGN_ALIGN_BOTTOM)
583                 region_azone_initialize(sa, ar, 't');
584         else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
585                 region_azone_initialize(sa, ar, 'l');
586         else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
587                 region_azone_initialize(sa, ar, 'r');
588                                                                 
589 }
590
591 /* dir is direction to check, not the splitting edge direction! */
592 static int rct_fits(rcti *rect, char dir, int size)
593 {
594         if(dir=='h') {
595                 return rect->xmax-rect->xmin - size;
596         }
597         else { // 'v'
598                 return rect->ymax-rect->ymin - size;
599         }
600 }
601
602 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
603 {
604         rcti *remainder_prev= remainder;
605         int prefsizex, prefsizey;
606         int alignment;
607         
608         if(ar==NULL)
609                 return;
610         
611         /* no returns in function, winrct gets set in the end again */
612         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
613         
614         /* for test; allow split of previously defined region */
615         if(ar->alignment & RGN_SPLIT_PREV)
616                 if(ar->prev)
617                         remainder= &ar->prev->winrct;
618         
619         alignment = ar->alignment & ~RGN_SPLIT_PREV;
620         
621         /* clear state flags first */
622         ar->flag &= ~RGN_FLAG_TOO_SMALL;
623         /* user errors */
624         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
625                 alignment= RGN_ALIGN_NONE;
626         
627         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
628         prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
629         
630         /* hidden is user flag */
631         if(ar->flag & RGN_FLAG_HIDDEN);
632         /* XXX floating area region, not handled yet here */
633         else if(alignment == RGN_ALIGN_FLOAT);
634         /* remainder is too small for any usage */
635         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
636                 ar->flag |= RGN_FLAG_TOO_SMALL;
637         }
638         else if(alignment==RGN_ALIGN_NONE) {
639                 /* typically last region */
640                 ar->winrct= *remainder;
641                 BLI_init_rcti(remainder, 0, 0, 0, 0);
642         }
643         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
644                 
645                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
646                         ar->flag |= RGN_FLAG_TOO_SMALL;
647                 }
648                 else {
649                         int fac= rct_fits(remainder, 'v', prefsizey);
650
651                         if(fac < 0 )
652                                 prefsizey += fac;
653                         
654                         ar->winrct= *remainder;
655                         
656                         if(alignment==RGN_ALIGN_TOP) {
657                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
658                                 remainder->ymax= ar->winrct.ymin - 1;
659                         }
660                         else {
661                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
662                                 remainder->ymin= ar->winrct.ymax + 1;
663                         }
664                 }
665         }
666         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
667                 
668                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
669                         ar->flag |= RGN_FLAG_TOO_SMALL;
670                 }
671                 else {
672                         int fac= rct_fits(remainder, 'h', prefsizex);
673                         
674                         if(fac < 0 )
675                                 prefsizex += fac;
676                         
677                         ar->winrct= *remainder;
678                         
679                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
680                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
681                                 if(alignment==RGN_ALIGN_RIGHT)
682                                         remainder->xmax= ar->winrct.xmin - 1;
683                         }
684                         else {
685                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
686                                 if(alignment==RGN_ALIGN_LEFT)
687                                         remainder->xmin= ar->winrct.xmax + 1;
688                         }
689                 }
690         }
691         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
692                 /* percentage subdiv*/
693                 ar->winrct= *remainder;
694                 
695                 if(alignment==RGN_ALIGN_HSPLIT) {
696                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
697                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
698                                 remainder->xmin= ar->winrct.xmax+1;
699                         }
700                         else {
701                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
702                         }
703                 }
704                 else {
705                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
706                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
707                                 remainder->ymin= ar->winrct.ymax+1;
708                         }
709                         else {
710                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
711                         }
712                 }
713         }
714         else if(alignment==RGN_ALIGN_QSPLIT) {
715                 ar->winrct= *remainder;
716                 
717                 /* test if there's still 4 regions left */
718                 if(quad==0) {
719                         ARegion *artest= ar->next;
720                         int count= 1;
721                         
722                         while(artest) {
723                                 artest->alignment= RGN_ALIGN_QSPLIT;
724                                 artest= artest->next;
725                                 count++;
726                         }
727                         
728                         if(count!=4) {
729                                 /* let's stop adding regions */
730                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
731                                 if (G.f & G_DEBUG)
732                                         printf("region quadsplit failed\n");
733                         }
734                         else quad= 1;
735                 }
736                 if(quad) {
737                         if(quad==1) { /* left bottom */
738                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
739                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
740                         }
741                         else if(quad==2) { /* left top */
742                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
743                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
744                         }
745                         else if(quad==3) { /* right bottom */
746                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
747                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
748                         }
749                         else {  /* right top */
750                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
751                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
752                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
753                         }
754
755                         quad++;
756                 }
757         }
758         
759         /* for speedup */
760         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
761         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
762         
763         /* restore test exception */
764         if(ar->alignment & RGN_SPLIT_PREV) {
765                 if(ar->prev) {
766                         remainder= remainder_prev;
767                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
768                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
769                 }
770         }
771
772         /* set winrect for azones */
773         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
774                 ar->winrct= *remainder;
775                 
776                 if(alignment==RGN_ALIGN_TOP)
777                         ar->winrct.ymin= ar->winrct.ymax;
778                 else if(alignment==RGN_ALIGN_BOTTOM)
779                         ar->winrct.ymax= ar->winrct.ymin;
780                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
781                         ar->winrct.xmin= ar->winrct.xmax;
782                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
783                         ar->winrct.xmax= ar->winrct.xmin;
784                 else /* prevent winrct to be valid */
785                         ar->winrct.xmax= ar->winrct.xmin;
786         }
787         /* in end, add azones, where appropriate */
788         region_azone_add(sa, ar, alignment);
789
790
791         region_rect_recursive(sa, ar->next, remainder, quad);
792 }
793
794 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
795 {
796         short rt= CLAMPIS(G.rt, 0, 16);
797
798         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
799         else sa->totrct.xmin= sa->v1->vec.x;
800         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
801         else sa->totrct.xmax= sa->v4->vec.x;
802         
803         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
804         else sa->totrct.ymin= sa->v1->vec.y;
805         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
806         else sa->totrct.ymax= sa->v2->vec.y;
807         
808         /* for speedup */
809         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
810         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
811 }
812
813
814 /* used for area initialize below */
815 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
816 {
817         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
818                 if(ar->swinid)
819                         wm_subwindow_close(win, ar->swinid);
820                 ar->swinid= 0;
821         }
822         else if(ar->swinid==0)
823                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
824         else 
825                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
826 }
827
828 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
829 {
830         /* note, add-handler checks if it already exists */
831         
832         // XXX it would be good to have boundbox checks for some of these...
833         if(flag & ED_KEYMAP_UI) {
834                 /* user interface widgets */
835                 UI_add_region_handlers(handlers);
836         }
837         if(flag & ED_KEYMAP_VIEW2D) {
838                 /* 2d-viewport handling+manipulation */
839                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
840                 WM_event_add_keymap_handler(handlers, keymap);
841         }
842         if(flag & ED_KEYMAP_MARKERS) {
843                 /* time-markers */
844                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
845                 WM_event_add_keymap_handler(handlers, keymap);
846                 // XXX need boundbox check urgently!!!
847         }
848         if(flag & ED_KEYMAP_ANIMATION) {
849                 /* frame changing and timeline operators (for time spaces) */
850                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
851                 WM_event_add_keymap_handler(handlers, keymap);
852         }
853         if(flag & ED_KEYMAP_FRAMES) {
854                 /* frame changing/jumping (for all spaces) */
855                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
856                 WM_event_add_keymap_handler(handlers, keymap);
857         }
858         if(flag & ED_KEYMAP_GPENCIL) {
859                 /* grease pencil */
860                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
861                 WM_event_add_keymap_handler(handlers, keymap);
862         }
863         if(flag & ED_KEYMAP_HEADER) {
864                 /* standard keymap for headers regions */
865                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
866                 WM_event_add_keymap_handler(handlers, keymap);
867         }
868 }
869
870
871 /* called in screen_refresh, or screens_init, also area size changes */
872 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
873 {
874         ARegion *ar;
875         rcti rect;
876         
877         /* set typedefinitions */
878         sa->type= BKE_spacetype_from_id(sa->spacetype);
879         
880         if(sa->type==NULL) {
881                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
882                 sa->type= BKE_spacetype_from_id(sa->spacetype);
883         }
884         
885         for(ar= sa->regionbase.first; ar; ar= ar->next)
886                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
887         
888         /* area sizes */
889         area_calc_totrct(sa, win->sizex, win->sizey);
890         
891         /* clear all azones, add the area triange widgets */
892         area_azone_initialize(sa);
893
894         /* region rect sizes */
895         rect= sa->totrct;
896         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
897         
898         /* default area handlers */
899         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
900         /* checks spacedata, adds own handlers */
901         if(sa->type->init)
902                 sa->type->init(wm, sa);
903         
904         /* region windows, default and own handlers */
905         for(ar= sa->regionbase.first; ar; ar= ar->next) {
906                 region_subwindow(wm, win, ar);
907                 
908                 if(ar->swinid) {
909                         /* default region handlers */
910                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
911
912                         if(ar->type->init)
913                                 ar->type->init(wm, ar);
914                 }
915                 else {
916                         /* prevent uiblocks to run */
917                         uiFreeBlocks(NULL, &ar->uiblocks);      
918                 }
919                 
920         }
921 }
922
923 /* externally called for floating regions like menus */
924 void ED_region_init(bContext *C, ARegion *ar)
925 {
926 //      ARegionType *at= ar->type;
927         
928         /* refresh can be called before window opened */
929         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
930         
931         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
932         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
933         
934         /* UI convention */
935         wmLoadIdentity();
936         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
937         
938 }
939
940 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
941 {
942         ScrArea *sa= CTX_wm_area(C);
943
944         ar->flag ^= RGN_FLAG_HIDDEN;
945         ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
946
947         if(ar->flag & RGN_FLAG_HIDDEN)
948                 WM_event_remove_handlers(C, &ar->handlers);
949
950         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
951         ED_area_tag_redraw(sa);
952 }
953
954 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
955 /* area vertices were set */
956 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
957 {
958         SpaceType *st;
959         ARegion *ar;
960         
961         sa1->headertype= sa2->headertype;
962         sa1->spacetype= sa2->spacetype;
963         sa1->butspacetype= sa2->butspacetype;
964         
965         if(swap_space == 1) {
966                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
967                 /* exception: ensure preview is reset */
968 //              if(sa1->spacetype==SPACE_VIEW3D)
969 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
970         }
971         else if (swap_space == 2) {
972                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
973         }
974         else {
975                 BKE_spacedata_freelist(&sa1->spacedata);
976                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
977         }
978         
979         /* Note; SPACE_EMPTY is possible on new screens */
980         
981         /* regions */
982         if(swap_space == 1) {
983                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
984         }
985         else {
986                 if(swap_space<2) {
987                         st= BKE_spacetype_from_id(sa1->spacetype);
988                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
989                                 BKE_area_region_free(st, ar);
990                         BLI_freelistN(&sa1->regionbase);
991                 }
992                 
993                 st= BKE_spacetype_from_id(sa2->spacetype);
994                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
995                         ARegion *newar= BKE_area_region_copy(st, ar);
996                         BLI_addtail(&sa1->regionbase, newar);
997                 }
998         }
999 }
1000
1001 /* *********** Space switching code *********** */
1002
1003 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1004 {
1005         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
1006
1007         ED_area_exit(C, sa1);
1008         ED_area_exit(C, sa2);
1009
1010         tmp->spacetype= sa1->spacetype;
1011         tmp->butspacetype= sa1->butspacetype;
1012         BKE_spacedata_copyfirst(&tmp->spacedata, &sa1->spacedata);
1013
1014         area_copy_data(tmp, sa1, 2);
1015         area_copy_data(sa1, sa2, 0);
1016         area_copy_data(sa2, tmp, 0);
1017         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1018         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1019
1020         BKE_screen_area_free(tmp);
1021         MEM_freeN(tmp);
1022
1023         /* tell WM to refresh, cursor types etc */
1024         WM_event_add_mousemove(C);
1025         
1026         ED_area_tag_redraw(sa1);
1027         ED_area_tag_refresh(sa1);
1028         ED_area_tag_redraw(sa2);
1029         ED_area_tag_refresh(sa2);
1030 }
1031
1032 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1033 {
1034         if(sa->spacetype != type) {
1035                 SpaceType *st;
1036                 SpaceLink *slold;
1037                 SpaceLink *sl;
1038
1039                 ED_area_exit(C, sa);
1040
1041                 st= BKE_spacetype_from_id(type);
1042                 slold= sa->spacedata.first;
1043
1044                 sa->spacetype= type;
1045                 sa->butspacetype= type;
1046                 sa->type= st;
1047                 
1048                 /* check previously stored space */
1049                 for (sl= sa->spacedata.first; sl; sl= sl->next)
1050                         if(sl->spacetype==type)
1051                                 break;
1052                 
1053                 /* old spacedata... happened during work on 2.50, remove */
1054                 if(sl && sl->regionbase.first==NULL) {
1055                         st->free(sl);
1056                         BLI_freelinkN(&sa->spacedata, sl);
1057                         sl= NULL;
1058                 }
1059                 
1060                 if (sl) {
1061                         
1062                         /* swap regions */
1063                         slold->regionbase= sa->regionbase;
1064                         sa->regionbase= sl->regionbase;
1065                         sl->regionbase.first= sl->regionbase.last= NULL;
1066                         
1067                         /* put in front of list */
1068                         BLI_remlink(&sa->spacedata, sl);
1069                         BLI_addhead(&sa->spacedata, sl);
1070                 } 
1071                 else {
1072                         /* new space */
1073                         if(st) {
1074                                 sl= st->new(C);
1075                                 BLI_addhead(&sa->spacedata, sl);
1076                                 
1077                                 /* swap regions */
1078                                 if(slold)
1079                                         slold->regionbase= sa->regionbase;
1080                                 sa->regionbase= sl->regionbase;
1081                                 sl->regionbase.first= sl->regionbase.last= NULL;
1082                         }
1083                 }
1084                 
1085                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1086                 
1087                 /* tell WM to refresh, cursor types etc */
1088                 WM_event_add_mousemove(C);
1089                                 
1090                 /*send space change notifyer*/
1091                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1092
1093                 ED_area_tag_redraw(sa);
1094                 ED_area_tag_refresh(sa);
1095         }
1096 }
1097
1098 void ED_area_prevspace(bContext *C, ScrArea *sa)
1099 {
1100         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1101
1102         if(sl->next) {
1103                 /* workaround for case of double prevspace, render window
1104                    with a file browser on top of it */
1105                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1106                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1107                 else
1108                         ED_area_newspace(C, sa, sl->next->spacetype);
1109         }
1110         else {
1111                 ED_area_newspace(C, sa, SPACE_INFO);
1112         }
1113         ED_area_tag_redraw(sa);
1114
1115         /*send space change notifyer*/
1116         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1117 }
1118
1119 static char *editortype_pup(void)
1120 {
1121         return(
1122                    "Editor type:%t"
1123                    "|3D View %x1"
1124
1125                    "|%l"
1126                    
1127                    "|Timeline %x15"
1128                    "|Graph Editor %x2"
1129                    "|DopeSheet %x12"
1130                    "|NLA Editor %x13"
1131                    
1132                    "|%l"
1133                    
1134                    "|UV/Image Editor %x6"
1135                    
1136                    "|Video Sequence Editor %x8"
1137                    "|Text Editor %x9" 
1138                    "|Node Editor %x16"
1139                    "|Logic Editor %x17"
1140                    
1141                    "|%l"
1142                    
1143                    "|Properties %x4"
1144                    "|Outliner %x3"
1145                    "|User Preferences %x19"
1146                    "|Info%x7"
1147                                    
1148                    "|%l"
1149                    
1150                    "|File Browser %x5"
1151                    
1152                    "|%l"
1153                    
1154                    "|Console %x18"
1155                    );
1156 }
1157
1158 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
1159 {
1160         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1161         ED_area_tag_redraw(CTX_wm_area(C));
1162
1163         /*send space change notifyer*/
1164         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1165 }
1166
1167 /* returns offset for next button in header */
1168 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1169 {
1170         ScrArea *sa= CTX_wm_area(C);
1171         uiBut *but;
1172         int xco= 8;
1173         
1174         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1175                                                    editortype_pup(), xco, yco, XIC+10, YIC, 
1176                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1177                                                    "Displays Current Editor Type. "
1178                                                    "Click for menu of available types.");
1179         uiButSetFunc(but, spacefunc, NULL, NULL);
1180         
1181         return xco + XIC + 14;
1182 }
1183
1184 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1185 {
1186         ScrArea *sa= CTX_wm_area(C);
1187         int xco= 8;
1188         
1189         if (!sa->full)
1190                 xco= ED_area_header_switchbutton(C, block, yco);
1191
1192         uiBlockSetEmboss(block, UI_EMBOSSN);
1193
1194         if (sa->flag & HEADER_NO_PULLDOWN) {
1195                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1196                                                  ICON_DISCLOSURE_TRI_RIGHT,
1197                                                  xco,yco,XIC,YIC-2,
1198                                                  &(sa->flag), 0, 0, 0, 0, 
1199                                                  "Show pulldown menus");
1200         }
1201         else {
1202                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1203                                                  ICON_DISCLOSURE_TRI_DOWN,
1204                                                  xco,yco,XIC,YIC-2,
1205                                                  &(sa->flag), 0, 0, 0, 0, 
1206                                                  "Hide pulldown menus");
1207         }
1208
1209         uiBlockSetEmboss(block, UI_EMBOSS);
1210         
1211         return xco + XIC;
1212 }
1213
1214 /************************ standard UI regions ************************/
1215
1216 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context, int contextnr)
1217 {
1218         ScrArea *sa= CTX_wm_area(C);
1219         uiStyle *style= U.uistyles.first;
1220         uiBlock *block;
1221         PanelType *pt;
1222         Panel *panel;
1223         View2D *v2d= &ar->v2d;
1224         View2DScrollers *scrollers;
1225         float col[3];
1226         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1227
1228         if(contextnr >= 0)
1229                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1230
1231         if(vertical) {
1232                 w= v2d->cur.xmax - v2d->cur.xmin;
1233                 em= (ar->type->prefsizex)? 10: 20;
1234         }
1235         else {
1236                 w= UI_PANEL_WIDTH;
1237                 em= (ar->type->prefsizex)? 10: 20;
1238         }
1239
1240         x= 0;
1241         y= -style->panelouter;
1242
1243         /* create panels */
1244         uiBeginPanels(C, ar);
1245
1246         /* set view2d view matrix for scrolling (without scrollers) */
1247         UI_view2d_view_ortho(C, v2d);
1248
1249         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1250                 /* verify context */
1251                 if(context)
1252                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1253                                 continue;
1254
1255                 /* draw panel */
1256                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1257                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1258                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1259
1260                         /* bad fixed values */
1261                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1262                         triangle= 22;
1263
1264                         if(vertical)
1265                                 y -= header;
1266
1267                         if(pt->draw_header && header && (open || vertical)) {
1268                                 /* for enabled buttons */
1269                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1270                                         triangle, header+style->panelspace, header, 1, style);
1271
1272                                 pt->draw_header(C, panel);
1273
1274                                 uiBlockLayoutResolve(block, &xco, &yco);
1275                                 panel->labelofs= xco - triangle;
1276                                 panel->layout= NULL;
1277                         }
1278
1279                         if(open) {
1280                                 short panelContext;
1281                                 
1282                                 /* panel context can either be toolbar region or normal panels region */
1283                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1284                                         panelContext= UI_LAYOUT_TOOLBAR;
1285                                 else
1286                                         panelContext= UI_LAYOUT_PANEL;
1287                                 
1288                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1289                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1290
1291                                 pt->draw(C, panel);
1292
1293                                 uiBlockLayoutResolve(block, &xco, &yco);
1294                                 panel->layout= NULL;
1295
1296                                 yco -= 2*style->panelspace;
1297                                 uiEndPanel(block, w, -yco);
1298                         }
1299                         else {
1300                                 yco= 0;
1301                                 uiEndPanel(block, w, 0);
1302                         }
1303
1304                         uiEndBlock(C, block);
1305
1306                         if(vertical) {
1307                                 if(pt->flag & PNL_NO_HEADER)
1308                                         y += yco;
1309                                 else
1310                                         y += yco-style->panelouter;
1311                         }
1312                         else {
1313                                 x += w;
1314                                 miny= MIN2(y, yco-style->panelouter-header);
1315                         }
1316                 }
1317         }
1318
1319         if(vertical)
1320                 x += w;
1321         else
1322                 y= miny;
1323         
1324         /* in case there are no panels */
1325         if(x == 0 || y == 0) {
1326                 x= UI_PANEL_WIDTH;
1327                 y= UI_PANEL_WIDTH;
1328         }
1329
1330         /* clear */
1331         if (ar->type->regionid == RGN_TYPE_PREVIEW)
1332                 UI_GetThemeColor3fv(TH_PREVIEW_BACK, col);
1333         else
1334                 UI_GetThemeColor3fv(TH_BACK, col);
1335         
1336         glClearColor(col[0], col[1], col[2], 0.0);
1337         glClear(GL_COLOR_BUFFER_BIT);
1338
1339         /* before setting the view */
1340         if(vertical) {
1341                 /* only allow scrolling in vertical direction */
1342                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1343                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1344                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1345                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1346                 
1347                 // don't jump back when panels close or hide
1348                 if(!newcontext)
1349                         y= MAX2(-y, -v2d->cur.ymin);
1350                 else
1351                         y= -y;
1352         }
1353         else {
1354                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1355                  * they often don't fit in horizontal layout)
1356                  */
1357                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1358                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1359                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1360                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1361                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1362                 
1363                 // don't jump back when panels close or hide
1364                 if(!newcontext)
1365                         x= MAX2(x, v2d->cur.xmax);
1366                 y= -y;
1367         }
1368
1369         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1370         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1371
1372         /* set the view */
1373         UI_view2d_view_ortho(C, v2d);
1374
1375         /* this does the actual drawing! */
1376         uiEndPanels(C, ar);
1377         
1378         /* restore view matrix */
1379         UI_view2d_view_restore(C);
1380         
1381         /* scrollers */
1382         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1383         UI_view2d_scrollers_draw(C, v2d, scrollers);
1384         UI_view2d_scrollers_free(scrollers);
1385 }
1386
1387 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1388 {
1389         wmKeyMap *keymap;
1390         
1391         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1392                 // scrollbars for button regions
1393         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1394         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1395
1396                 // correctly initialised User-Prefs?
1397         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1398                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1399         
1400         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1401
1402         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1403         WM_event_add_keymap_handler(&ar->handlers, keymap);
1404 }
1405
1406 void ED_region_header(const bContext *C, ARegion *ar)
1407 {
1408         uiStyle *style= U.uistyles.first;
1409         uiBlock *block;
1410         uiLayout *layout;
1411         HeaderType *ht;
1412         Header header = {0};
1413         float col[3];
1414         int maxco, xco, yco;
1415
1416         /* clear */
1417         if(ED_screen_area_active(C))
1418                 UI_GetThemeColor3fv(TH_HEADER, col);
1419         else
1420                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
1421         
1422         glClearColor(col[0], col[1], col[2], 0.0);
1423         glClear(GL_COLOR_BUFFER_BIT);
1424         
1425         /* set view2d view matrix for scrolling (without scrollers) */
1426         UI_view2d_view_ortho(C, &ar->v2d);
1427
1428         xco= maxco= 8;
1429         yco= HEADERY-3;
1430
1431         /* draw all headers types */
1432         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1433                 block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
1434                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1435
1436                 if(ht->draw) {
1437                         header.type= ht;
1438                         header.layout= layout;
1439                         ht->draw(C, &header);
1440                         
1441                         /* for view2d */
1442                         xco= uiLayoutGetWidth(layout);
1443                         if(xco > maxco)
1444                                 maxco= xco;
1445                 }
1446
1447                 uiBlockLayoutResolve(block, &xco, &yco);
1448                 
1449                 /* for view2d */
1450                 if(xco > maxco)
1451                         maxco= xco;
1452                 
1453                 uiEndBlock(C, block);
1454                 uiDrawBlock(C, block);
1455         }
1456
1457         /* always as last  */
1458         UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1459
1460         /* restore view matrix? */
1461         UI_view2d_view_restore(C);
1462 }
1463
1464 void ED_region_header_init(ARegion *ar)
1465 {
1466         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1467 }
1468