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