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