Merge from trunk -r 24758:25003.
[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_math.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                 /* user interface widgets */
832                 UI_add_region_handlers(handlers);
833         }
834         if(flag & ED_KEYMAP_VIEW2D) {
835                 /* 2d-viewport handling+manipulation */
836                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
837                 WM_event_add_keymap_handler(handlers, keymap);
838         }
839         if(flag & ED_KEYMAP_MARKERS) {
840                 /* time-markers */
841                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
842                 WM_event_add_keymap_handler(handlers, keymap);
843                 // XXX need boundbox check urgently!!!
844         }
845         if(flag & ED_KEYMAP_ANIMATION) {
846                 /* frame changing and timeline operators (for time spaces) */
847                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
848                 WM_event_add_keymap_handler(handlers, keymap);
849         }
850         if(flag & ED_KEYMAP_FRAMES) {
851                 /* frame changing/jumping (for all spaces) */
852                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
853                 WM_event_add_keymap_handler(handlers, keymap);
854         }
855         if(flag & ED_KEYMAP_GPENCIL) {
856                 /* grease pencil */
857                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
858                 WM_event_add_keymap_handler(handlers, keymap);
859         }
860         if(flag & ED_KEYMAP_HEADER) {
861                 /* standard keymap for headers regions */
862                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
863                 WM_event_add_keymap_handler(handlers, keymap);
864         }
865 }
866
867
868 /* called in screen_refresh, or screens_init, also area size changes */
869 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
870 {
871         ARegion *ar;
872         rcti rect;
873         
874         /* set typedefinitions */
875         sa->type= BKE_spacetype_from_id(sa->spacetype);
876         
877         if(sa->type==NULL) {
878                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
879                 sa->type= BKE_spacetype_from_id(sa->spacetype);
880         }
881         
882         for(ar= sa->regionbase.first; ar; ar= ar->next)
883                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
884         
885         /* area sizes */
886         area_calc_totrct(sa, win->sizex, win->sizey);
887         
888         /* clear all azones, add the area triange widgets */
889         area_azone_initialize(sa);
890
891         /* region rect sizes */
892         rect= sa->totrct;
893         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
894         
895         /* default area handlers */
896         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
897         /* checks spacedata, adds own handlers */
898         if(sa->type->init)
899                 sa->type->init(wm, sa);
900         
901         /* region windows, default and own handlers */
902         for(ar= sa->regionbase.first; ar; ar= ar->next) {
903                 region_subwindow(wm, win, ar);
904                 
905                 if(ar->swinid) {
906                         /* default region handlers */
907                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
908
909                         if(ar->type->init)
910                                 ar->type->init(wm, ar);
911                 }
912                 else {
913                         /* prevent uiblocks to run */
914                         uiFreeBlocks(NULL, &ar->uiblocks);      
915                 }
916                 
917         }
918 }
919
920 /* externally called for floating regions like menus */
921 void ED_region_init(bContext *C, ARegion *ar)
922 {
923 //      ARegionType *at= ar->type;
924         
925         /* refresh can be called before window opened */
926         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
927         
928         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
929         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
930         
931         /* UI convention */
932         wmLoadIdentity();
933         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
934         
935 }
936
937 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
938 {
939         ScrArea *sa= CTX_wm_area(C);
940
941         ar->flag ^= RGN_FLAG_HIDDEN;
942         ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
943
944         if(ar->flag & RGN_FLAG_HIDDEN)
945                 WM_event_remove_handlers(C, &ar->handlers);
946
947         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
948         ED_area_tag_redraw(sa);
949 }
950
951 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
952 /* area vertices were set */
953 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
954 {
955         SpaceType *st;
956         ARegion *ar;
957         
958         sa1->headertype= sa2->headertype;
959         sa1->spacetype= sa2->spacetype;
960         sa1->butspacetype= sa2->butspacetype;
961         
962         if(swap_space == 1) {
963                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
964                 /* exception: ensure preview is reset */
965 //              if(sa1->spacetype==SPACE_VIEW3D)
966 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
967         }
968         else if (swap_space == 2) {
969                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
970         }
971         else {
972                 BKE_spacedata_freelist(&sa1->spacedata);
973                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
974         }
975         
976         /* Note; SPACE_EMPTY is possible on new screens */
977         
978         /* regions */
979         if(swap_space == 1) {
980                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
981         }
982         else {
983                 if(swap_space<2) {
984                         st= BKE_spacetype_from_id(sa1->spacetype);
985                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
986                                 BKE_area_region_free(st, ar);
987                         BLI_freelistN(&sa1->regionbase);
988                 }
989                 
990                 st= BKE_spacetype_from_id(sa2->spacetype);
991                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
992                         ARegion *newar= BKE_area_region_copy(st, ar);
993                         BLI_addtail(&sa1->regionbase, newar);
994                 }
995         }
996 }
997
998 /* *********** Space switching code *********** */
999
1000 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1001 {
1002         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
1003
1004         ED_area_exit(C, sa1);
1005         ED_area_exit(C, sa2);
1006
1007         tmp->spacetype= sa1->spacetype;
1008         tmp->butspacetype= sa1->butspacetype;
1009         BKE_spacedata_copyfirst(&tmp->spacedata, &sa1->spacedata);
1010
1011         area_copy_data(tmp, sa1, 2);
1012         area_copy_data(sa1, sa2, 0);
1013         area_copy_data(sa2, tmp, 0);
1014         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1015         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1016
1017         BKE_screen_area_free(tmp);
1018         MEM_freeN(tmp);
1019
1020         /* tell WM to refresh, cursor types etc */
1021         WM_event_add_mousemove(C);
1022         
1023         ED_area_tag_redraw(sa1);
1024         ED_area_tag_refresh(sa1);
1025         ED_area_tag_redraw(sa2);
1026         ED_area_tag_refresh(sa2);
1027 }
1028
1029 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1030 {
1031         if(sa->spacetype != type) {
1032                 SpaceType *st;
1033                 SpaceLink *slold;
1034                 SpaceLink *sl;
1035
1036                 ED_area_exit(C, sa);
1037
1038                 st= BKE_spacetype_from_id(type);
1039                 slold= sa->spacedata.first;
1040
1041                 sa->spacetype= type;
1042                 sa->butspacetype= type;
1043                 sa->type= st;
1044                 
1045                 /* check previously stored space */
1046                 for (sl= sa->spacedata.first; sl; sl= sl->next)
1047                         if(sl->spacetype==type)
1048                                 break;
1049                 
1050                 /* old spacedata... happened during work on 2.50, remove */
1051                 if(sl && sl->regionbase.first==NULL) {
1052                         st->free(sl);
1053                         BLI_freelinkN(&sa->spacedata, sl);
1054                         sl= NULL;
1055                 }
1056                 
1057                 if (sl) {
1058                         
1059                         /* swap regions */
1060                         slold->regionbase= sa->regionbase;
1061                         sa->regionbase= sl->regionbase;
1062                         sl->regionbase.first= sl->regionbase.last= NULL;
1063                         
1064                         /* put in front of list */
1065                         BLI_remlink(&sa->spacedata, sl);
1066                         BLI_addhead(&sa->spacedata, sl);
1067                 } 
1068                 else {
1069                         /* new space */
1070                         if(st) {
1071                                 sl= st->new(C);
1072                                 BLI_addhead(&sa->spacedata, sl);
1073                                 
1074                                 /* swap regions */
1075                                 if(slold)
1076                                         slold->regionbase= sa->regionbase;
1077                                 sa->regionbase= sl->regionbase;
1078                                 sl->regionbase.first= sl->regionbase.last= NULL;
1079                         }
1080                 }
1081                 
1082                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1083                 
1084                 /* tell WM to refresh, cursor types etc */
1085                 WM_event_add_mousemove(C);
1086                 
1087                 ED_area_tag_redraw(sa);
1088                 ED_area_tag_refresh(sa);
1089         }
1090 }
1091
1092 void ED_area_prevspace(bContext *C)
1093 {
1094         SpaceLink *sl= CTX_wm_space_data(C);
1095         ScrArea *sa= CTX_wm_area(C);
1096
1097         /* cleanup */
1098 #if 0 // XXX needs to be space type specific
1099         if(sfile->spacetype==SPACE_FILE) {
1100                 if(sfile->pupmenu) {
1101                         MEM_freeN(sfile->pupmenu);
1102                         sfile->pupmenu= NULL;
1103                 }
1104         }
1105 #endif
1106
1107         if(sl->next) {
1108
1109 #if 0 // XXX check whether this is still needed
1110                 if (sfile->spacetype == SPACE_SCRIPT) {
1111                         SpaceScript *sc = (SpaceScript *)sfile;
1112                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
1113                 }
1114 #endif
1115
1116                 /* workaround for case of double prevspace, render window
1117                    with a file browser on top of it */
1118                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1119                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1120                 else
1121                         ED_area_newspace(C, sa, sl->next->spacetype);
1122         }
1123         else {
1124                 ED_area_newspace(C, sa, SPACE_INFO);
1125         }
1126         ED_area_tag_redraw(sa);
1127 }
1128
1129 static char *windowtype_pup(void)
1130 {
1131         return(
1132                    "Window type:%t"
1133                    "|3D View %x1"
1134
1135                    "|%l"
1136                    
1137                    "|Timeline %x15"
1138                    "|Graph Editor %x2"
1139                    "|DopeSheet %x12"
1140                    "|NLA Editor %x13"
1141                    
1142                    "|%l"
1143                    
1144                    "|UV/Image Editor %x6"
1145                    
1146                    "|Video Sequence Editor %x8"
1147                    "|Text Editor %x9" 
1148                    "|Node Editor %x16"
1149                    "|Logic Editor %x17"
1150                    
1151                    "|%l"
1152                    
1153                    "|Properties %x4"
1154                    "|Outliner %x3"
1155                    "|User Preferences %x19"
1156                    "|Info%x7"
1157                                    
1158                    "|%l"
1159                    
1160                    "|File Browser %x5"
1161                    
1162                    "|%l"
1163                    
1164                    "|Console %x18"
1165                    );
1166 }
1167
1168 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
1169 {
1170         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1171         ED_area_tag_redraw(CTX_wm_area(C));
1172 }
1173
1174 /* returns offset for next button in header */
1175 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1176 {
1177         ScrArea *sa= CTX_wm_area(C);
1178         uiBut *but;
1179         int xco= 8;
1180         
1181         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1182                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
1183                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1184                                                    "Displays Current Window Type. "
1185                                                    "Click for menu of available types.");
1186         uiButSetFunc(but, spacefunc, NULL, NULL);
1187         
1188         return xco + XIC + 14;
1189 }
1190
1191 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1192 {
1193         ScrArea *sa= CTX_wm_area(C);
1194         int xco= 8;
1195         
1196         xco= ED_area_header_switchbutton(C, block, yco);
1197
1198         uiBlockSetEmboss(block, UI_EMBOSSN);
1199
1200         if (sa->flag & HEADER_NO_PULLDOWN) {
1201                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1202                                                  ICON_DISCLOSURE_TRI_RIGHT,
1203                                                  xco,yco,XIC,YIC-2,
1204                                                  &(sa->flag), 0, 0, 0, 0, 
1205                                                  "Show pulldown menus");
1206         }
1207         else {
1208                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1209                                                  ICON_DISCLOSURE_TRI_DOWN,
1210                                                  xco,yco,XIC,YIC-2,
1211                                                  &(sa->flag), 0, 0, 0, 0, 
1212                                                  "Hide pulldown menus");
1213         }
1214
1215         uiBlockSetEmboss(block, UI_EMBOSS);
1216         
1217         return xco + XIC;
1218 }
1219
1220 /************************ standard UI regions ************************/
1221
1222 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context, int contextnr)
1223 {
1224         ScrArea *sa= CTX_wm_area(C);
1225         uiStyle *style= U.uistyles.first;
1226         uiBlock *block;
1227         PanelType *pt;
1228         Panel *panel;
1229         View2D *v2d= &ar->v2d;
1230         View2DScrollers *scrollers;
1231         float col[3];
1232         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1233
1234         if(contextnr >= 0)
1235                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1236
1237         if(vertical) {
1238                 w= v2d->cur.xmax - v2d->cur.xmin;
1239                 em= (ar->type->minsizex)? 10: 20;
1240         }
1241         else {
1242                 w= UI_PANEL_WIDTH;
1243                 em= (ar->type->minsizex)? 10: 20;
1244         }
1245
1246         x= 0;
1247         y= -style->panelouter;
1248
1249         /* create panels */
1250         uiBeginPanels(C, ar);
1251
1252         /* set view2d view matrix for scrolling (without scrollers) */
1253         UI_view2d_view_ortho(C, v2d);
1254
1255         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1256                 /* verify context */
1257                 if(context)
1258                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1259                                 continue;
1260
1261                 /* draw panel */
1262                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1263                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1264                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1265
1266                         /* bad fixed values */
1267                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1268                         triangle= 22;
1269
1270                         if(vertical)
1271                                 y -= header;
1272
1273                         if(pt->draw_header && header && (open || vertical)) {
1274                                 /* for enabled buttons */
1275                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1276                                         triangle, header+style->panelspace, header, 1, style);
1277
1278                                 pt->draw_header(C, panel);
1279
1280                                 uiBlockLayoutResolve(block, &xco, &yco);
1281                                 panel->labelofs= xco - triangle;
1282                                 panel->layout= NULL;
1283                         }
1284
1285                         if(open) {
1286                                 short panelContext;
1287                                 
1288                                 /* panel context can either be toolbar region or normal panels region */
1289                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1290                                         panelContext= UI_LAYOUT_TOOLBAR;
1291                                 else
1292                                         panelContext= UI_LAYOUT_PANEL;
1293                                 
1294                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1295                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1296
1297                                 pt->draw(C, panel);
1298
1299                                 uiBlockLayoutResolve(block, &xco, &yco);
1300                                 panel->layout= NULL;
1301
1302                                 yco -= 2*style->panelspace;
1303                                 uiEndPanel(block, w, -yco);
1304                         }
1305                         else {
1306                                 yco= 0;
1307                                 uiEndPanel(block, w, 0);
1308                         }
1309
1310                         uiEndBlock(C, block);
1311
1312                         if(vertical) {
1313                                 if(pt->flag & PNL_NO_HEADER)
1314                                         y += yco;
1315                                 else
1316                                         y += yco-style->panelouter;
1317                         }
1318                         else {
1319                                 x += w;
1320                                 miny= MIN2(y, yco-style->panelouter-header);
1321                         }
1322                 }
1323         }
1324
1325         if(vertical)
1326                 x += w;
1327         else
1328                 y= miny;
1329         
1330         /* in case there are no panels */
1331         if(x == 0 || y == 0) {
1332                 x= UI_PANEL_WIDTH;
1333                 y= UI_PANEL_WIDTH;
1334         }
1335
1336         /* clear */
1337         UI_GetThemeColor3fv(TH_BACK, col);
1338         glClearColor(col[0], col[1], col[2], 0.0);
1339         glClear(GL_COLOR_BUFFER_BIT);
1340
1341         /* before setting the view */
1342         if(vertical) {
1343                 /* only allow scrolling in vertical direction */
1344                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1345                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1346                 
1347                 // don't jump back when panels close or hide
1348                 if(!newcontext)
1349                         y= MAX2(-y, -v2d->cur.ymin);
1350                 else
1351                         y= -y;
1352         }
1353         else {
1354                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1355                  * they often don't fit in horizontal layout)
1356                  */
1357                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1358                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1359                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1360                 
1361                 // don't jump back when panels close or hide
1362                 if(!newcontext)
1363                         x= MAX2(x, v2d->cur.xmax);
1364                 y= -y;
1365         }
1366
1367         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1368         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1369
1370         /* set the view */
1371         UI_view2d_view_ortho(C, v2d);
1372
1373         /* this does the actual drawing! */
1374         uiEndPanels(C, ar);
1375         
1376         /* restore view matrix */
1377         UI_view2d_view_restore(C);
1378         
1379         /* scrollers */
1380         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1381         UI_view2d_scrollers_draw(C, v2d, scrollers);
1382         UI_view2d_scrollers_free(scrollers);
1383 }
1384
1385 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1386 {
1387         wmKeyMap *keymap;
1388         
1389         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1390                 // scrollbars for button regions
1391         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1392         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1393
1394                 // correctly initialised User-Prefs?
1395         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1396                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1397         
1398         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1399
1400         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1401         WM_event_add_keymap_handler(&ar->handlers, keymap);
1402 }
1403
1404 void ED_region_header(const bContext *C, ARegion *ar)
1405 {
1406         uiStyle *style= U.uistyles.first;
1407         uiBlock *block;
1408         uiLayout *layout;
1409         HeaderType *ht;
1410         Header header = {0};
1411         float col[3];
1412         int maxco, xco, yco;
1413
1414         /* clear */
1415         if(ED_screen_area_active(C))
1416                 UI_GetThemeColor3fv(TH_HEADER, col);
1417         else
1418                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
1419         
1420         glClearColor(col[0], col[1], col[2], 0.0);
1421         glClear(GL_COLOR_BUFFER_BIT);
1422         
1423         /* set view2d view matrix for scrolling (without scrollers) */
1424         UI_view2d_view_ortho(C, &ar->v2d);
1425
1426         xco= maxco= 8;
1427         yco= HEADERY-3;
1428
1429         /* draw all headers types */
1430         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1431                 block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
1432                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1433
1434                 if(ht->draw) {
1435                         header.type= ht;
1436                         header.layout= layout;
1437                         ht->draw(C, &header);
1438                         
1439                         /* for view2d */
1440                         xco= uiLayoutGetWidth(layout);
1441                         if(xco > maxco)
1442                                 maxco= xco;
1443                 }
1444
1445                 uiBlockLayoutResolve(block, &xco, &yco);
1446                 
1447                 /* for view2d */
1448                 if(xco > maxco)
1449                         maxco= xco;
1450                 
1451                 uiEndBlock(C, block);
1452                 uiDrawBlock(C, block);
1453         }
1454
1455         /* always as last  */
1456         UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1457
1458         /* restore view matrix? */
1459         UI_view2d_view_restore(C);
1460 }
1461
1462 void ED_region_header_init(ARegion *ar)
1463 {
1464         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1465 }
1466