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