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