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