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