Fix #21077: multiple python headers in an area header not working correct,
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_userdef_types.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_rand.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_screen.h"
43 #include "BKE_utildefines.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47 #include "wm_subwindow.h"
48
49 #include "ED_screen.h"
50 #include "ED_screen_types.h"
51 #include "ED_types.h"
52 #include "ED_fileselect.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 #include "screen_intern.h"
63
64 /* general area and region code */
65
66 static void region_draw_emboss(ARegion *ar, rcti *scirct)
67 {
68         rcti rect;
69         
70         /* translate scissor rect to region space */
71         rect.xmin= scirct->xmin - ar->winrct.xmin;
72         rect.ymin= scirct->ymin - ar->winrct.ymin;
73         rect.xmax= scirct->xmax - ar->winrct.xmin;
74         rect.ymax= scirct->ymax - ar->winrct.ymin;
75         
76         /* set transp line */
77         glEnable( GL_BLEND );
78         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
79         
80         /* right  */
81         glColor4ub(0,0,0, 50);
82         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
83         
84         /* bottom  */
85         glColor4ub(0,0,0, 80);
86         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
87         
88         /* top  */
89         glColor4ub(255,255,255, 60);
90         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
91
92         /* left  */
93         glColor4ub(255,255,255, 50);
94         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
95         
96         glDisable( GL_BLEND );
97 }
98
99 void ED_region_pixelspace(ARegion *ar)
100 {
101         int width= ar->winrct.xmax-ar->winrct.xmin+1;
102         int height= ar->winrct.ymax-ar->winrct.ymin+1;
103         
104         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
105         glLoadIdentity();
106 }
107
108 /* only exported for WM */
109 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
110 {
111         /* generic notes first */
112         switch(note->category) {
113                 case NC_WM:
114                         if(note->data==ND_FILEREAD)
115                                 ED_region_tag_redraw(ar);
116                         break;
117                 case NC_WINDOW:
118                         ED_region_tag_redraw(ar);
119                         break;
120 #ifndef WM_FAST_DRAW
121                 case NC_SCREEN:
122                         if(note->action==NA_EDITED)
123                                 ED_region_tag_redraw(ar);
124                         break;
125 #endif
126         }
127
128         if(ar->type && ar->type->listener)
129                 ar->type->listener(ar, note);
130 }
131
132 /* only exported for WM */
133 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
134 {
135         /* no generic notes? */
136         if(sa->type && sa->type->listener) {
137                 sa->type->listener(sa, note);
138         }
139 }
140
141 /* only exported for WM */
142 void ED_area_do_refresh(bContext *C, ScrArea *sa)
143 {
144         /* no generic notes? */
145         if(sa->type && sa->type->refresh) {
146                 sa->type->refresh(C, sa);
147         }
148         sa->do_refresh= 0;
149 }
150
151 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
152 /* only exported for WM */
153 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
154 {
155         AZone *az;
156         
157         for(az= sa->actionzones.first; az; az= az->next) {
158                 int xs, ys;
159                 
160                 xs= (az->x1+az->x2)/2;
161                 ys= (az->y1+az->y2)/2;
162
163                 /* test if inside */
164                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
165                         az->do_draw= 1;
166                 }
167         }
168 }
169
170 static void area_draw_azone(short x1, short y1, short x2, short y2)
171 {
172         float xmin = x1;
173         float xmax = x2-2;
174         float ymin = y1-1;
175         float ymax = y2-3;
176         
177         float dx= 0.3f*(xmax-xmin);
178         float dy= 0.3f*(ymax-ymin);
179         
180         glColor4ub(255, 255, 255, 180);
181         fdrawline(xmin, ymax, xmax, ymin);
182         glColor4ub(255, 255, 255, 130);
183         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
184         glColor4ub(255, 255, 255, 80);
185         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
186         
187         glColor4ub(0, 0, 0, 210);
188         fdrawline(xmin, ymax+1, xmax+1, ymin);
189         glColor4ub(0, 0, 0, 180);
190         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
191         glColor4ub(0, 0, 0, 150);
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                 UI_ThemeClearColor(TH_HEADER);
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= RGN_DRAW;
375                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
376         }
377 }
378
379 void ED_region_tag_redraw_overlay(ARegion *ar)
380 {
381         if(ar)
382                 ar->do_draw_overlay= RGN_DRAW;
383 }
384
385 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
386 {
387         if(ar) {
388                 if(!ar->do_draw) {
389                         /* no redraw set yet, set partial region */
390                         ar->do_draw= RGN_DRAW_PARTIAL;
391                         ar->drawrct= *rct;
392                 }
393                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
394                         /* partial redraw already set, expand region */
395                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
396                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
397                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
398                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
399                 }
400         }
401 }
402
403 void ED_area_tag_redraw(ScrArea *sa)
404 {
405         ARegion *ar;
406         
407         if(sa)
408                 for(ar= sa->regionbase.first; ar; ar= ar->next)
409                         ED_region_tag_redraw(ar);
410 }
411
412 void ED_area_tag_refresh(ScrArea *sa)
413 {
414         if(sa)
415                 sa->do_refresh= 1;
416 }
417
418 /* *************************************************************** */
419
420 /* use NULL to disable it */
421 void ED_area_headerprint(ScrArea *sa, const char *str)
422 {
423         ARegion *ar;
424         
425         for(ar= sa->regionbase.first; ar; ar= ar->next) {
426                 if(ar->regiontype==RGN_TYPE_HEADER) {
427                         if(str) {
428                                 if(ar->headerstr==NULL)
429                                         ar->headerstr= MEM_mallocN(256, "headerprint");
430                                 BLI_strncpy(ar->headerstr, str, 256);
431                         }
432                         else if(ar->headerstr) {
433                                 MEM_freeN(ar->headerstr);
434                                 ar->headerstr= NULL;
435                         }
436                         ED_region_tag_redraw(ar);
437                 }
438         }
439 }
440
441 /* ************************************************************ */
442
443
444 #define AZONESPOT               12
445 static void area_azone_initialize(ScrArea *sa) 
446 {
447         AZone *az;
448         
449         /* reinitalize entirely, regions add azones too */
450         BLI_freelistN(&sa->actionzones);
451         
452         /* set area action zones */
453         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
454         BLI_addtail(&(sa->actionzones), az);
455         az->type= AZONE_AREA;
456         az->x1= sa->totrct.xmin;
457         az->y1= sa->totrct.ymin;
458         az->x2= sa->totrct.xmin + AZONESPOT-1;
459         az->y2= sa->totrct.ymin + AZONESPOT-1;
460         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
461         
462         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
463         BLI_addtail(&(sa->actionzones), az);
464         az->type= AZONE_AREA;
465         az->x1= sa->totrct.xmax+1;
466         az->y1= sa->totrct.ymax+1;
467         az->x2= sa->totrct.xmax-AZONESPOT+1;
468         az->y2= sa->totrct.ymax-AZONESPOT+1;
469         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
470 }
471
472 #define AZONEPAD_EDGE   4
473 #define AZONEPAD_ICON   8
474 static void region_azone_edge(AZone *az, ARegion *ar)
475 {
476         if(az->edge=='t') {
477                 az->x1= ar->winrct.xmin;
478                 az->y1= ar->winrct.ymax - AZONEPAD_EDGE;
479                 az->x2= ar->winrct.xmax;
480                 az->y2= ar->winrct.ymax;
481         }
482         else if(az->edge=='b') {
483                 az->x1= ar->winrct.xmin;
484                 az->y1= ar->winrct.ymin + AZONEPAD_EDGE;
485                 az->x2= ar->winrct.xmax;
486                 az->y2= ar->winrct.ymin;
487         }
488         else if(az->edge=='l') {
489                 az->x1= ar->winrct.xmin;
490                 az->y1= ar->winrct.ymin;
491                 az->x2= ar->winrct.xmin + AZONEPAD_EDGE;
492                 az->y2= ar->winrct.ymax;
493         }
494         else { // if(az->edge=='r') {
495                 az->x1= ar->winrct.xmax;
496                 az->y1= ar->winrct.ymin;
497                 az->x2= ar->winrct.xmax - AZONEPAD_EDGE;
498                 az->y2= ar->winrct.ymax;
499         }
500
501         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
502 }
503
504 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
505 {
506         AZone *azt;
507         int tot=0;
508         
509         for(azt= sa->actionzones.first; azt; azt= azt->next) {
510                 if(azt->edge == az->edge) tot++;
511         }
512         
513         if(az->edge=='t') {
514                 az->x1= ar->winrct.xmax - tot*2*AZONEPAD_ICON;
515                 az->y1= ar->winrct.ymax + AZONEPAD_ICON;
516                 az->x2= ar->winrct.xmax - tot*AZONEPAD_ICON;
517                 az->y2= ar->winrct.ymax + 2*AZONEPAD_ICON;
518         }
519         else if(az->edge=='b') {
520                 az->x1= ar->winrct.xmin + AZONEPAD_ICON;
521                 az->y1= ar->winrct.ymin - 2*AZONEPAD_ICON;
522                 az->x2= ar->winrct.xmin + 2*AZONEPAD_ICON;
523                 az->y2= ar->winrct.ymin - AZONEPAD_ICON;
524         }
525         else if(az->edge=='l') {
526                 az->x1= ar->winrct.xmin - 2*AZONEPAD_ICON;
527                 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
528                 az->x2= ar->winrct.xmin - AZONEPAD_ICON;
529                 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
530         }
531         else { // if(az->edge=='r') {
532                 az->x1= ar->winrct.xmax + AZONEPAD_ICON;
533                 az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
534                 az->x2= ar->winrct.xmax + 2*AZONEPAD_ICON;
535                 az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
536         }
537
538         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
539         
540         /* if more azones on 1 spot, set offset */
541         for(azt= sa->actionzones.first; azt; azt= azt->next) {
542                 if(az!=azt) {
543                         if( ABS(az->x1-azt->x1) < 2 && ABS(az->y1-azt->y1) < 2) {
544                                 if(az->edge=='t' || az->edge=='b') {
545                                         az->x1+= AZONESPOT;
546                                         az->x2+= AZONESPOT;
547                                         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
548                                 }
549                                 else {
550                                         az->y1-= AZONESPOT;
551                                         az->y2-= AZONESPOT;
552                                         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
553                                 }
554                         }
555                 }
556         }
557 }
558
559 static void region_azone_initialize(ScrArea *sa, ARegion *ar, char edge) 
560 {
561         AZone *az;
562         
563         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
564         BLI_addtail(&(sa->actionzones), az);
565         az->type= AZONE_REGION;
566         az->ar= ar;
567         az->edge= edge;
568         
569         if (ar->flag & RGN_FLAG_HIDDEN) {
570                 region_azone_icon(sa, az, ar);
571         } else {
572                 region_azone_edge(az, ar);
573         }
574         
575 }
576
577
578 /* *************************************************************** */
579
580 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
581 {
582          /* edge code (t b l r) is where azone will be drawn */
583         
584         if(alignment==RGN_ALIGN_TOP)
585                 region_azone_initialize(sa, ar, 'b');
586         else if(alignment==RGN_ALIGN_BOTTOM)
587                 region_azone_initialize(sa, ar, 't');
588         else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
589                 region_azone_initialize(sa, ar, 'l');
590         else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
591                 region_azone_initialize(sa, ar, 'r');
592                                                                 
593 }
594
595 /* dir is direction to check, not the splitting edge direction! */
596 static int rct_fits(rcti *rect, char dir, int size)
597 {
598         if(dir=='h') {
599                 return rect->xmax-rect->xmin - size;
600         }
601         else { // 'v'
602                 return rect->ymax-rect->ymin - size;
603         }
604 }
605
606 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
607 {
608         rcti *remainder_prev= remainder;
609         int prefsizex, prefsizey;
610         int alignment;
611         
612         if(ar==NULL)
613                 return;
614         
615         /* no returns in function, winrct gets set in the end again */
616         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
617         
618         /* for test; allow split of previously defined region */
619         if(ar->alignment & RGN_SPLIT_PREV)
620                 if(ar->prev)
621                         remainder= &ar->prev->winrct;
622         
623         alignment = ar->alignment & ~RGN_SPLIT_PREV;
624         
625         /* clear state flags first */
626         ar->flag &= ~RGN_FLAG_TOO_SMALL;
627         /* user errors */
628         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
629                 alignment= RGN_ALIGN_NONE;
630         
631         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
632         prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
633         
634         /* hidden is user flag */
635         if(ar->flag & RGN_FLAG_HIDDEN);
636         /* XXX floating area region, not handled yet here */
637         else if(alignment == RGN_ALIGN_FLOAT);
638         /* remainder is too small for any usage */
639         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
640                 ar->flag |= RGN_FLAG_TOO_SMALL;
641         }
642         else if(alignment==RGN_ALIGN_NONE) {
643                 /* typically last region */
644                 ar->winrct= *remainder;
645                 BLI_init_rcti(remainder, 0, 0, 0, 0);
646         }
647         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
648                 
649                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
650                         ar->flag |= RGN_FLAG_TOO_SMALL;
651                 }
652                 else {
653                         int fac= rct_fits(remainder, 'v', prefsizey);
654
655                         if(fac < 0 )
656                                 prefsizey += fac;
657                         
658                         ar->winrct= *remainder;
659                         
660                         if(alignment==RGN_ALIGN_TOP) {
661                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
662                                 remainder->ymax= ar->winrct.ymin - 1;
663                         }
664                         else {
665                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
666                                 remainder->ymin= ar->winrct.ymax + 1;
667                         }
668                 }
669         }
670         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
671                 
672                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
673                         ar->flag |= RGN_FLAG_TOO_SMALL;
674                 }
675                 else {
676                         int fac= rct_fits(remainder, 'h', prefsizex);
677                         
678                         if(fac < 0 )
679                                 prefsizex += fac;
680                         
681                         ar->winrct= *remainder;
682                         
683                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
684                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
685                                 if(alignment==RGN_ALIGN_RIGHT)
686                                         remainder->xmax= ar->winrct.xmin - 1;
687                         }
688                         else {
689                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
690                                 if(alignment==RGN_ALIGN_LEFT)
691                                         remainder->xmin= ar->winrct.xmax + 1;
692                         }
693                 }
694         }
695         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
696                 /* percentage subdiv*/
697                 ar->winrct= *remainder;
698                 
699                 if(alignment==RGN_ALIGN_HSPLIT) {
700                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
701                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
702                                 remainder->xmin= ar->winrct.xmax+1;
703                         }
704                         else {
705                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
706                         }
707                 }
708                 else {
709                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
710                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
711                                 remainder->ymin= ar->winrct.ymax+1;
712                         }
713                         else {
714                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
715                         }
716                 }
717         }
718         else if(alignment==RGN_ALIGN_QSPLIT) {
719                 ar->winrct= *remainder;
720                 
721                 /* test if there's still 4 regions left */
722                 if(quad==0) {
723                         ARegion *artest= ar->next;
724                         int count= 1;
725                         
726                         while(artest) {
727                                 artest->alignment= RGN_ALIGN_QSPLIT;
728                                 artest= artest->next;
729                                 count++;
730                         }
731                         
732                         if(count!=4) {
733                                 /* let's stop adding regions */
734                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
735                                 if (G.f & G_DEBUG)
736                                         printf("region quadsplit failed\n");
737                         }
738                         else quad= 1;
739                 }
740                 if(quad) {
741                         if(quad==1) { /* left bottom */
742                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
743                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
744                         }
745                         else if(quad==2) { /* left top */
746                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
747                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
748                         }
749                         else if(quad==3) { /* right bottom */
750                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
751                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
752                         }
753                         else {  /* right top */
754                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
755                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
756                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
757                         }
758
759                         quad++;
760                 }
761         }
762         
763         /* for speedup */
764         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
765         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
766         
767         /* restore test exception */
768         if(ar->alignment & RGN_SPLIT_PREV) {
769                 if(ar->prev) {
770                         remainder= remainder_prev;
771                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
772                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
773                 }
774         }
775
776         /* set winrect for azones */
777         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
778                 ar->winrct= *remainder;
779                 
780                 if(alignment==RGN_ALIGN_TOP)
781                         ar->winrct.ymin= ar->winrct.ymax;
782                 else if(alignment==RGN_ALIGN_BOTTOM)
783                         ar->winrct.ymax= ar->winrct.ymin;
784                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
785                         ar->winrct.xmin= ar->winrct.xmax;
786                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
787                         ar->winrct.xmax= ar->winrct.xmin;
788                 else /* prevent winrct to be valid */
789                         ar->winrct.xmax= ar->winrct.xmin;
790         }
791         /* in end, add azones, where appropriate */
792         region_azone_add(sa, ar, alignment);
793
794
795         region_rect_recursive(sa, ar->next, remainder, quad);
796 }
797
798 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
799 {
800         short rt= CLAMPIS(G.rt, 0, 16);
801
802         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
803         else sa->totrct.xmin= sa->v1->vec.x;
804         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
805         else sa->totrct.xmax= sa->v4->vec.x;
806         
807         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
808         else sa->totrct.ymin= sa->v1->vec.y;
809         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
810         else sa->totrct.ymax= sa->v2->vec.y;
811         
812         /* for speedup */
813         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
814         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
815 }
816
817
818 /* used for area initialize below */
819 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
820 {
821         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
822                 if(ar->swinid)
823                         wm_subwindow_close(win, ar->swinid);
824                 ar->swinid= 0;
825         }
826         else if(ar->swinid==0)
827                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
828         else 
829                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
830 }
831
832 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
833 {
834         /* note, add-handler checks if it already exists */
835         
836         // XXX it would be good to have boundbox checks for some of these...
837         if(flag & ED_KEYMAP_UI) {
838                 /* user interface widgets */
839                 UI_add_region_handlers(handlers);
840         }
841         if(flag & ED_KEYMAP_VIEW2D) {
842                 /* 2d-viewport handling+manipulation */
843                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
844                 WM_event_add_keymap_handler(handlers, keymap);
845         }
846         if(flag & ED_KEYMAP_MARKERS) {
847                 /* time-markers */
848                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
849                 WM_event_add_keymap_handler(handlers, keymap);
850                 // XXX need boundbox check urgently!!!
851         }
852         if(flag & ED_KEYMAP_ANIMATION) {
853                 /* frame changing and timeline operators (for time spaces) */
854                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
855                 WM_event_add_keymap_handler(handlers, keymap);
856         }
857         if(flag & ED_KEYMAP_FRAMES) {
858                 /* frame changing/jumping (for all spaces) */
859                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
860                 WM_event_add_keymap_handler(handlers, keymap);
861         }
862         if(flag & ED_KEYMAP_GPENCIL) {
863                 /* grease pencil */
864                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
865                 WM_event_add_keymap_handler(handlers, keymap);
866         }
867         if(flag & ED_KEYMAP_HEADER) {
868                 /* standard keymap for headers regions */
869                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
870                 WM_event_add_keymap_handler(handlers, keymap);
871         }
872 }
873
874
875 /* called in screen_refresh, or screens_init, also area size changes */
876 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
877 {
878         ARegion *ar;
879         rcti rect;
880         
881         /* set typedefinitions */
882         sa->type= BKE_spacetype_from_id(sa->spacetype);
883         
884         if(sa->type==NULL) {
885                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
886                 sa->type= BKE_spacetype_from_id(sa->spacetype);
887         }
888         
889         for(ar= sa->regionbase.first; ar; ar= ar->next)
890                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
891         
892         /* area sizes */
893         area_calc_totrct(sa, win->sizex, win->sizey);
894         
895         /* clear all azones, add the area triange widgets */
896         area_azone_initialize(sa);
897
898         /* region rect sizes */
899         rect= sa->totrct;
900         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
901         
902         /* default area handlers */
903         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
904         /* checks spacedata, adds own handlers */
905         if(sa->type->init)
906                 sa->type->init(wm, sa);
907         
908         /* region windows, default and own handlers */
909         for(ar= sa->regionbase.first; ar; ar= ar->next) {
910                 region_subwindow(wm, win, ar);
911                 
912                 if(ar->swinid) {
913                         /* default region handlers */
914                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
915                         /* own handlers */
916                         if(ar->type->init)
917                                 ar->type->init(wm, ar);
918                 }
919                 else {
920                         /* prevent uiblocks to run */
921                         uiFreeBlocks(NULL, &ar->uiblocks);      
922                 }
923                 
924         }
925 }
926
927 /* externally called for floating regions like menus */
928 void ED_region_init(bContext *C, ARegion *ar)
929 {
930 //      ARegionType *at= ar->type;
931         
932         /* refresh can be called before window opened */
933         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
934         
935         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
936         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
937         
938         /* UI convention */
939         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
940         glLoadIdentity();
941 }
942
943 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
944 {
945         ScrArea *sa= CTX_wm_area(C);
946
947         ar->flag ^= RGN_FLAG_HIDDEN;
948         ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
949
950         if(ar->flag & RGN_FLAG_HIDDEN)
951                 WM_event_remove_handlers(C, &ar->handlers);
952
953         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
954         ED_area_tag_redraw(sa);
955 }
956
957 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
958 /* area vertices were set */
959 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
960 {
961         SpaceType *st;
962         ARegion *ar;
963         int spacetype= sa1->spacetype;
964         
965         sa1->headertype= sa2->headertype;
966         sa1->spacetype= sa2->spacetype;
967         sa1->butspacetype= sa2->butspacetype;
968         
969         if(swap_space == 1) {
970                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
971                 /* exception: ensure preview is reset */
972 //              if(sa1->spacetype==SPACE_VIEW3D)
973 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
974         }
975         else if (swap_space == 2) {
976                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
977         }
978         else {
979                 BKE_spacedata_freelist(&sa1->spacedata);
980                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
981         }
982         
983         /* Note; SPACE_EMPTY is possible on new screens */
984         
985         /* regions */
986         if(swap_space == 1) {
987                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
988         }
989         else {
990                 if(swap_space<2) {
991                         st= BKE_spacetype_from_id(spacetype);
992                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
993                                 BKE_area_region_free(st, ar);
994                         BLI_freelistN(&sa1->regionbase);
995                 }
996                 
997                 st= BKE_spacetype_from_id(sa2->spacetype);
998                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
999                         ARegion *newar= BKE_area_region_copy(st, ar);
1000                         BLI_addtail(&sa1->regionbase, newar);
1001                 }
1002         }
1003 }
1004
1005 /* *********** Space switching code *********** */
1006
1007 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1008 {
1009         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
1010
1011         ED_area_exit(C, sa1);
1012         ED_area_exit(C, sa2);
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         /* Special handling of filebrowser to stop background thread for
1103            thumbnail creation - don't want to waste cpu resources if not showing
1104            the filebrowser */
1105         if (sl->spacetype == SPACE_FILE) {
1106                 ED_fileselect_exit(C, (SpaceFile*)sl);
1107         }
1108
1109         if(sl->next) {
1110                 /* workaround for case of double prevspace, render window
1111                    with a file browser on top of it */
1112                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1113                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1114                 else
1115                         ED_area_newspace(C, sa, sl->next->spacetype);
1116         }
1117         else {
1118                 ED_area_newspace(C, sa, SPACE_INFO);
1119         }
1120         ED_area_tag_redraw(sa);
1121
1122         /*send space change notifyer*/
1123         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1124 }
1125
1126 static char *editortype_pup(void)
1127 {
1128         return(
1129                    "Editor type:%t"
1130                    "|3D View %x1"
1131
1132                    "|%l"
1133                    
1134                    "|Timeline %x15"
1135                    "|Graph Editor %x2"
1136                    "|DopeSheet %x12"
1137                    "|NLA Editor %x13"
1138                    
1139                    "|%l"
1140                    
1141                    "|UV/Image Editor %x6"
1142                    
1143                    "|Video Sequence Editor %x8"
1144                    "|Text Editor %x9" 
1145                    "|Node Editor %x16"
1146                    "|Logic Editor %x17"
1147                    
1148                    "|%l"
1149                    
1150                    "|Properties %x4"
1151                    "|Outliner %x3"
1152                    "|User Preferences %x19"
1153                    "|Info%x7"
1154                                    
1155                    "|%l"
1156                    
1157                    "|File Browser %x5"
1158                    
1159                    "|%l"
1160                    
1161                    "|Console %x18"
1162                    );
1163 }
1164
1165 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
1166 {
1167         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1168         ED_area_tag_redraw(CTX_wm_area(C));
1169
1170         /*send space change notifyer*/
1171         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1172 }
1173
1174 /* returns offset for next button in header */
1175 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1176 {
1177         ScrArea *sa= CTX_wm_area(C);
1178         uiBut *but;
1179         int xco= 8;
1180         
1181         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1182                                                    editortype_pup(), xco, yco, XIC+10, YIC, 
1183                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1184                                                    "Displays current editor type. "
1185                                                    "Click for menu of available types");
1186         uiButSetFunc(but, spacefunc, NULL, NULL);
1187         
1188         return xco + XIC + 14;
1189 }
1190
1191 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1192 {
1193         ScrArea *sa= CTX_wm_area(C);
1194         int xco= 8;
1195         
1196         if (!sa->full)
1197                 xco= ED_area_header_switchbutton(C, block, yco);
1198
1199         uiBlockSetEmboss(block, UI_EMBOSSN);
1200
1201         if (sa->flag & HEADER_NO_PULLDOWN) {
1202                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1203                                                  ICON_DISCLOSURE_TRI_RIGHT,
1204                                                  xco,yco,XIC,YIC-2,
1205                                                  &(sa->flag), 0, 0, 0, 0, 
1206                                                  "Show pulldown menus");
1207         }
1208         else {
1209                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1210                                                  ICON_DISCLOSURE_TRI_DOWN,
1211                                                  xco,yco,XIC,YIC-2,
1212                                                  &(sa->flag), 0, 0, 0, 0, 
1213                                                  "Hide pulldown menus");
1214         }
1215
1216         uiBlockSetEmboss(block, UI_EMBOSS);
1217         
1218         return xco + XIC;
1219 }
1220
1221 /************************ standard UI regions ************************/
1222
1223 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context, int contextnr)
1224 {
1225         ScrArea *sa= CTX_wm_area(C);
1226         uiStyle *style= U.uistyles.first;
1227         uiBlock *block;
1228         PanelType *pt;
1229         Panel *panel;
1230         View2D *v2d= &ar->v2d;
1231         View2DScrollers *scrollers;
1232         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1233
1234         if(contextnr >= 0)
1235                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1236
1237         if(vertical) {
1238                 w= v2d->cur.xmax - v2d->cur.xmin;
1239                 em= (ar->type->prefsizex)? 10: 20;
1240         }
1241         else {
1242                 w= UI_PANEL_WIDTH;
1243                 em= (ar->type->prefsizex)? 10: 20;
1244         }
1245
1246         x= 0;
1247         y= -style->panelouter;
1248
1249         /* create panels */
1250         uiBeginPanels(C, ar);
1251
1252         /* set view2d view matrix for scrolling (without scrollers) */
1253         UI_view2d_view_ortho(C, v2d);
1254
1255         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1256                 /* verify context */
1257                 if(context)
1258                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1259                                 continue;
1260
1261                 /* draw panel */
1262                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1263                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1264                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1265
1266                         /* bad fixed values */
1267                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1268                         triangle= 22;
1269
1270                         if(vertical)
1271                                 y -= header;
1272
1273                         if(pt->draw_header && header && (open || vertical)) {
1274                                 /* for enabled buttons */
1275                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1276                                         triangle, header+style->panelspace, header, 1, style);
1277
1278                                 pt->draw_header(C, panel);
1279
1280                                 uiBlockLayoutResolve(block, &xco, &yco);
1281                                 panel->labelofs= xco - triangle;
1282                                 panel->layout= NULL;
1283                         }
1284
1285                         if(open) {
1286                                 short panelContext;
1287                                 
1288                                 /* panel context can either be toolbar region or normal panels region */
1289                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1290                                         panelContext= UI_LAYOUT_TOOLBAR;
1291                                 else
1292                                         panelContext= UI_LAYOUT_PANEL;
1293                                 
1294                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1295                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1296
1297                                 pt->draw(C, panel);
1298
1299                                 uiBlockLayoutResolve(block, &xco, &yco);
1300                                 panel->layout= NULL;
1301
1302                                 yco -= 2*style->panelspace;
1303                                 uiEndPanel(block, w, -yco);
1304                         }
1305                         else {
1306                                 yco= 0;
1307                                 uiEndPanel(block, w, 0);
1308                         }
1309
1310                         uiEndBlock(C, block);
1311
1312                         if(vertical) {
1313                                 if(pt->flag & PNL_NO_HEADER)
1314                                         y += yco;
1315                                 else
1316                                         y += yco-style->panelouter;
1317                         }
1318                         else {
1319                                 x += w;
1320                                 miny= MIN2(y, yco-style->panelouter-header);
1321                         }
1322                 }
1323         }
1324
1325         if(vertical)
1326                 x += w;
1327         else
1328                 y= miny;
1329         
1330         /* in case there are no panels */
1331         if(x == 0 || y == 0) {
1332                 x= UI_PANEL_WIDTH;
1333                 y= UI_PANEL_WIDTH;
1334         }
1335
1336         /* clear */
1337         UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1338         glClear(GL_COLOR_BUFFER_BIT);
1339         
1340         /* before setting the view */
1341         if(vertical) {
1342                 /* only allow scrolling in vertical direction */
1343                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1344                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1345                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1346                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1347                 
1348                 // don't jump back when panels close or hide
1349                 if(!newcontext)
1350                         y= MAX2(-y, -v2d->cur.ymin);
1351                 else
1352                         y= -y;
1353         }
1354         else {
1355                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1356                  * they often don't fit in horizontal layout)
1357                  */
1358                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1359                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1360                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1361                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1362                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1363                 
1364                 // don't jump back when panels close or hide
1365                 if(!newcontext)
1366                         x= MAX2(x, v2d->cur.xmax);
1367                 y= -y;
1368         }
1369
1370         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1371         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1372
1373         /* set the view */
1374         UI_view2d_view_ortho(C, v2d);
1375
1376         /* this does the actual drawing! */
1377         uiEndPanels(C, ar);
1378         
1379         /* restore view matrix */
1380         UI_view2d_view_restore(C);
1381         
1382         /* scrollers */
1383         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1384         UI_view2d_scrollers_draw(C, v2d, scrollers);
1385         UI_view2d_scrollers_free(scrollers);
1386 }
1387
1388 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1389 {
1390         wmKeyMap *keymap;
1391         
1392         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1393                 // scrollbars for button regions
1394         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1395         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1396         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1397         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1398
1399                 // correctly initialised User-Prefs?
1400         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1401                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1402         
1403         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1404
1405         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1406         WM_event_add_keymap_handler(&ar->handlers, keymap);
1407 }
1408
1409 void ED_region_header(const bContext *C, ARegion *ar)
1410 {
1411         uiStyle *style= U.uistyles.first;
1412         uiBlock *block;
1413         uiLayout *layout;
1414         HeaderType *ht;
1415         Header header = {0};
1416         int maxco, xco, yco;
1417
1418         /* clear */     
1419         UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1420         glClear(GL_COLOR_BUFFER_BIT);
1421         
1422         /* set view2d view matrix for scrolling (without scrollers) */
1423         UI_view2d_view_ortho(C, &ar->v2d);
1424
1425         xco= maxco= 8;
1426         yco= HEADERY-3;
1427
1428         /* draw all headers types */
1429         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1430                 block= uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1431                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1432
1433                 if(ht->draw) {
1434                         header.type= ht;
1435                         header.layout= layout;
1436                         ht->draw(C, &header);
1437                         
1438                         /* for view2d */
1439                         xco= uiLayoutGetWidth(layout);
1440                         if(xco > maxco)
1441                                 maxco= xco;
1442                 }
1443
1444                 uiBlockLayoutResolve(block, &xco, &yco);
1445                 
1446                 /* for view2d */
1447                 if(xco > maxco)
1448                         maxco= xco;
1449                 
1450                 uiEndBlock(C, block);
1451                 uiDrawBlock(C, block);
1452         }
1453
1454         /* always as last  */
1455         UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1456
1457         /* restore view matrix? */
1458         UI_view2d_view_restore(C);
1459 }
1460
1461 void ED_region_header_init(ARegion *ar)
1462 {
1463         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1464 }
1465