Fix [#21516] UI artifacts in array modifier
[blender.git] / source / blender / editors / screen / area.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_userdef_types.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_rand.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_screen.h"
43 #include "BKE_utildefines.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47 #include "wm_subwindow.h"
48
49 #include "ED_screen.h"
50 #include "ED_screen_types.h"
51 #include "ED_types.h"
52 #include "ED_fileselect.h" 
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56 #include "BLF_api.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #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         glLoadIdentity();
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 #ifndef WM_FAST_DRAW
125                 case NC_SCREEN:
126                         if(note->action==NA_EDITED)
127                                 ED_region_tag_redraw(ar);
128                         /* pass on */
129 #endif
130                 default:
131                         if(ar->type && ar->type->listener)
132                                 ar->type->listener(ar, note);
133         }
134 }
135
136 /* only exported for WM */
137 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
138 {
139         /* no generic notes? */
140         if(sa->type && sa->type->listener) {
141                 sa->type->listener(sa, note);
142         }
143 }
144
145 /* only exported for WM */
146 void ED_area_do_refresh(bContext *C, ScrArea *sa)
147 {
148         /* no generic notes? */
149         if(sa->type && sa->type->refresh) {
150                 sa->type->refresh(C, sa);
151         }
152         sa->do_refresh= 0;
153 }
154
155 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
156 /* only exported for WM */
157 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
158 {
159         AZone *az;
160         
161         for(az= sa->actionzones.first; az; az= az->next) {
162                 int xs, ys;
163                 
164                 xs= (az->x1+az->x2)/2;
165                 ys= (az->y1+az->y2)/2;
166
167                 /* test if inside */
168                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
169                         az->do_draw= 1;
170                 }
171         }
172 }
173
174 static void area_draw_azone(short x1, short y1, short x2, short y2)
175 {
176         float xmin = x1;
177         float xmax = x2-2;
178         float ymin = y1-1;
179         float ymax = y2-3;
180         
181         float dx= 0.3f*(xmax-xmin);
182         float dy= 0.3f*(ymax-ymin);
183         
184         glColor4ub(255, 255, 255, 80);
185         fdrawline(xmin, ymax, xmax, ymin);
186         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
187         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
188         
189         glColor4ub(0, 0, 0, 150);
190         fdrawline(xmin, ymax+1, xmax+1, ymin);
191         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
192         fdrawline(xmin, ymax-2*dy+1, xmax-2*dx+1, ymin);
193 }
194
195
196 static void region_draw_azone(ScrArea *sa, AZone *az)
197 {
198         GLUquadricObj *qobj = NULL; 
199         short midx = az->x1 + (az->x2 - az->x1)/2;
200         short midy = az->y1 + (az->y2 - az->y1)/2;
201         
202         if(az->ar==NULL) return;
203         
204         /* only display action zone icons when the region is hidden */
205         if (!(az->ar->flag & RGN_FLAG_HIDDEN)) return;
206         
207         qobj = gluNewQuadric();
208         
209         glPushMatrix();         
210         glTranslatef(midx, midy, 0.); 
211         
212         /* outlined circle */
213         glEnable(GL_LINE_SMOOTH);
214
215         glColor4f(1.f, 1.f, 1.f, 0.8f);
216
217         gluQuadricDrawStyle(qobj, GLU_FILL); 
218         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
219
220         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
221         
222         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
223         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
224         
225         glDisable(GL_LINE_SMOOTH);
226         
227         glPopMatrix();
228         gluDeleteQuadric(qobj);
229         
230         /* + */
231         sdrawline(midx, midy-2, midx, midy+3);
232         sdrawline(midx-2, midy, midx+3, midy);
233 }
234
235
236 /* only exported for WM */
237 void ED_area_overdraw(bContext *C)
238 {
239         wmWindow *win= CTX_wm_window(C);
240         bScreen *screen= CTX_wm_screen(C);
241         ScrArea *sa;
242         
243         /* Draw AZones, in screenspace */
244         wmSubWindowSet(win, screen->mainwin);
245
246         glEnable( GL_BLEND );
247         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
248         
249         for(sa= screen->areabase.first; sa; sa= sa->next) {
250                 AZone *az;
251                 for(az= sa->actionzones.first; az; az= az->next) {
252                         if(az->do_draw) {
253                                 if(az->type==AZONE_AREA) {
254                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
255                                 } else if(az->type==AZONE_REGION) {
256                                         region_draw_azone(sa, az);
257                                 }
258                                 
259                                 az->do_draw= 0;
260                         }
261                 }
262         }       
263         glDisable( GL_BLEND );
264         
265 }
266
267 /* get scissor rect, checking overlapping regions */
268 void region_scissor_winrct(ARegion *ar, rcti *winrct)
269 {
270         *winrct= ar->winrct;
271         
272         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
273                 return;
274
275         while(ar->prev) {
276                 ar= ar->prev;
277                 
278                 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
279                         if(ar->flag & RGN_FLAG_HIDDEN);
280                         else if(ar->alignment & RGN_SPLIT_PREV);
281                         else if(ar->alignment==RGN_OVERLAP_LEFT) {
282                                 winrct->xmin= ar->winrct.xmax + 1;
283                         }
284                         else if(ar->alignment==RGN_OVERLAP_RIGHT) {
285                                 winrct->xmax= ar->winrct.xmin - 1;
286                         }
287                         else break;
288                 }
289         }
290 }
291
292 /* only exported for WM */
293 /* makes region ready for drawing, sets pixelspace */
294 void ED_region_set(const bContext *C, ARegion *ar)
295 {
296         wmWindow *win= CTX_wm_window(C);
297         ScrArea *sa= CTX_wm_area(C);
298         rcti winrct;
299         
300         /* checks other overlapping regions */
301         region_scissor_winrct(ar, &winrct);
302         
303         ar->drawrct= winrct;
304         
305         /* note; this sets state, so we can use wmOrtho and friends */
306         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
307         
308         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
309         
310         ED_region_pixelspace(ar);
311 }
312
313
314 /* only exported for WM */
315 void ED_region_do_draw(bContext *C, ARegion *ar)
316 {
317         wmWindow *win= CTX_wm_window(C);
318         ScrArea *sa= CTX_wm_area(C);
319         ARegionType *at= ar->type;
320         rcti winrct;
321         
322         /* checks other overlapping regions */
323         region_scissor_winrct(ar, &winrct);
324         
325         /* if no partial draw rect set, full rect */
326         if(ar->drawrct.xmin == ar->drawrct.xmax)
327                 ar->drawrct= winrct;
328         else {
329                 /* extra clip for safety */
330                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
331                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
332                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
333                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
334         }
335         
336         /* note; this sets state, so we can use wmOrtho and friends */
337         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
338         
339         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
340         
341         /* optional header info instead? */
342         if(ar->headerstr) {
343                 UI_ThemeClearColor(TH_HEADER);
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= RGN_DRAW;
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= RGN_DRAW_PARTIAL;
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 - 2*AZONEPAD_ICON;
516                 az->x2= ar->winrct.xmin + 2*AZONEPAD_ICON;
517                 az->y2= ar->winrct.ymin - 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->sizex?ar->sizex:ar->type->prefsizex;
626         prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
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                                 if (G.f & G_DEBUG)
730                                         printf("region quadsplit failed\n");
731                         }
732                         else quad= 1;
733                 }
734                 if(quad) {
735                         if(quad==1) { /* left bottom */
736                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
737                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
738                         }
739                         else if(quad==2) { /* left top */
740                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
741                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
742                         }
743                         else if(quad==3) { /* right bottom */
744                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
745                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
746                         }
747                         else {  /* right top */
748                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
749                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
750                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
751                         }
752
753                         quad++;
754                 }
755         }
756         
757         /* for speedup */
758         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
759         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
760         
761         /* restore test exception */
762         if(ar->alignment & RGN_SPLIT_PREV) {
763                 if(ar->prev) {
764                         remainder= remainder_prev;
765                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
766                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
767                 }
768         }
769
770         /* set winrect for azones */
771         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
772                 ar->winrct= *remainder;
773                 
774                 if(alignment==RGN_ALIGN_TOP)
775                         ar->winrct.ymin= ar->winrct.ymax;
776                 else if(alignment==RGN_ALIGN_BOTTOM)
777                         ar->winrct.ymax= ar->winrct.ymin;
778                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
779                         ar->winrct.xmin= ar->winrct.xmax;
780                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
781                         ar->winrct.xmax= ar->winrct.xmin;
782                 else /* prevent winrct to be valid */
783                         ar->winrct.xmax= ar->winrct.xmin;
784         }
785         /* in end, add azones, where appropriate */
786         region_azone_add(sa, ar, alignment);
787
788
789         region_rect_recursive(sa, ar->next, remainder, quad);
790 }
791
792 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
793 {
794         short rt= CLAMPIS(G.rt, 0, 16);
795
796         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
797         else sa->totrct.xmin= sa->v1->vec.x;
798         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
799         else sa->totrct.xmax= sa->v4->vec.x;
800         
801         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
802         else sa->totrct.ymin= sa->v1->vec.y;
803         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
804         else sa->totrct.ymax= sa->v2->vec.y;
805         
806         /* for speedup */
807         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
808         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
809 }
810
811
812 /* used for area initialize below */
813 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
814 {
815         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
816                 if(ar->swinid)
817                         wm_subwindow_close(win, ar->swinid);
818                 ar->swinid= 0;
819         }
820         else if(ar->swinid==0)
821                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
822         else 
823                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
824 }
825
826 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
827 {
828         /* note, add-handler checks if it already exists */
829         
830         // XXX it would be good to have boundbox checks for some of these...
831         if(flag & ED_KEYMAP_UI) {
832                 /* user interface widgets */
833                 UI_add_region_handlers(handlers);
834         }
835         if(flag & ED_KEYMAP_VIEW2D) {
836                 /* 2d-viewport handling+manipulation */
837                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
838                 WM_event_add_keymap_handler(handlers, keymap);
839         }
840         if(flag & ED_KEYMAP_MARKERS) {
841                 /* time-markers */
842                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
843                 WM_event_add_keymap_handler(handlers, keymap);
844                 // XXX need boundbox check urgently!!!
845         }
846         if(flag & ED_KEYMAP_ANIMATION) {
847                 /* frame changing and timeline operators (for time spaces) */
848                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
849                 WM_event_add_keymap_handler(handlers, keymap);
850         }
851         if(flag & ED_KEYMAP_FRAMES) {
852                 /* frame changing/jumping (for all spaces) */
853                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
854                 WM_event_add_keymap_handler(handlers, keymap);
855         }
856         if(flag & ED_KEYMAP_GPENCIL) {
857                 /* grease pencil */
858                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
859                 WM_event_add_keymap_handler(handlers, keymap);
860         }
861         if(flag & ED_KEYMAP_HEADER) {
862                 /* standard keymap for headers regions */
863                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
864                 WM_event_add_keymap_handler(handlers, keymap);
865         }
866 }
867
868
869 /* called in screen_refresh, or screens_init, also area size changes */
870 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
871 {
872         ARegion *ar;
873         rcti rect;
874         
875         /* set typedefinitions */
876         sa->type= BKE_spacetype_from_id(sa->spacetype);
877         
878         if(sa->type==NULL) {
879                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
880                 sa->type= BKE_spacetype_from_id(sa->spacetype);
881         }
882         
883         for(ar= sa->regionbase.first; ar; ar= ar->next)
884                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
885         
886         /* area sizes */
887         area_calc_totrct(sa, win->sizex, win->sizey);
888         
889         /* clear all azones, add the area triange widgets */
890         area_azone_initialize(sa);
891
892         /* region rect sizes */
893         rect= sa->totrct;
894         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
895         
896         /* default area handlers */
897         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
898         /* checks spacedata, adds own handlers */
899         if(sa->type->init)
900                 sa->type->init(wm, sa);
901         
902         /* region windows, default and own handlers */
903         for(ar= sa->regionbase.first; ar; ar= ar->next) {
904                 region_subwindow(wm, win, ar);
905                 
906                 if(ar->swinid) {
907                         /* default region handlers */
908                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
909
910                         if(ar->type->init)
911                                 ar->type->init(wm, ar);
912                 }
913                 else {
914                         /* prevent uiblocks to run */
915                         uiFreeBlocks(NULL, &ar->uiblocks);      
916                 }
917                 
918         }
919 }
920
921 /* externally called for floating regions like menus */
922 void ED_region_init(bContext *C, ARegion *ar)
923 {
924 //      ARegionType *at= ar->type;
925         
926         /* refresh can be called before window opened */
927         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
928         
929         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
930         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
931         
932         /* UI convention */
933         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
934         glLoadIdentity();
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                 /*send space change notifyer*/
1088                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1089
1090                 ED_area_tag_redraw(sa);
1091                 ED_area_tag_refresh(sa);
1092         }
1093 }
1094
1095 void ED_area_prevspace(bContext *C, ScrArea *sa)
1096 {
1097         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1098
1099         /* Special handling of filebrowser to stop background thread for
1100            thumbnail creation - don't want to waste cpu resources if not showing
1101            the filebrowser */
1102         if (sl->spacetype == SPACE_FILE) {
1103                 ED_fileselect_exit(C, (SpaceFile*)sl);
1104         }
1105
1106         if(sl->next) {
1107                 /* workaround for case of double prevspace, render window
1108                    with a file browser on top of it */
1109                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1110                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1111                 else
1112                         ED_area_newspace(C, sa, sl->next->spacetype);
1113         }
1114         else {
1115                 ED_area_newspace(C, sa, SPACE_INFO);
1116         }
1117         ED_area_tag_redraw(sa);
1118
1119         /*send space change notifyer*/
1120         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1121 }
1122
1123 static char *editortype_pup(void)
1124 {
1125         return(
1126                    "Editor type:%t"
1127                    "|3D View %x1"
1128
1129                    "|%l"
1130                    
1131                    "|Timeline %x15"
1132                    "|Graph Editor %x2"
1133                    "|DopeSheet %x12"
1134                    "|NLA Editor %x13"
1135                    
1136                    "|%l"
1137                    
1138                    "|UV/Image Editor %x6"
1139                    
1140                    "|Video Sequence Editor %x8"
1141                    "|Text Editor %x9" 
1142                    "|Node Editor %x16"
1143                    "|Logic Editor %x17"
1144                    
1145                    "|%l"
1146                    
1147                    "|Properties %x4"
1148                    "|Outliner %x3"
1149                    "|User Preferences %x19"
1150                    "|Info%x7"
1151                                    
1152                    "|%l"
1153                    
1154                    "|File Browser %x5"
1155                    
1156                    "|%l"
1157                    
1158                    "|Console %x18"
1159                    );
1160 }
1161
1162 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
1163 {
1164         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1165         ED_area_tag_redraw(CTX_wm_area(C));
1166
1167         /*send space change notifyer*/
1168         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1169 }
1170
1171 /* returns offset for next button in header */
1172 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1173 {
1174         ScrArea *sa= CTX_wm_area(C);
1175         uiBut *but;
1176         int xco= 8;
1177         
1178         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1179                                                    editortype_pup(), xco, yco, XIC+10, YIC, 
1180                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1181                                                    "Displays current editor type. "
1182                                                    "Click for menu of available types");
1183         uiButSetFunc(but, spacefunc, NULL, NULL);
1184         
1185         return xco + XIC + 14;
1186 }
1187
1188 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1189 {
1190         ScrArea *sa= CTX_wm_area(C);
1191         int xco= 8;
1192         
1193         if (!sa->full)
1194                 xco= ED_area_header_switchbutton(C, block, yco);
1195
1196         uiBlockSetEmboss(block, UI_EMBOSSN);
1197
1198         if (sa->flag & HEADER_NO_PULLDOWN) {
1199                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1200                                                  ICON_DISCLOSURE_TRI_RIGHT,
1201                                                  xco,yco,XIC,YIC-2,
1202                                                  &(sa->flag), 0, 0, 0, 0, 
1203                                                  "Show pulldown menus");
1204         }
1205         else {
1206                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1207                                                  ICON_DISCLOSURE_TRI_DOWN,
1208                                                  xco,yco,XIC,YIC-2,
1209                                                  &(sa->flag), 0, 0, 0, 0, 
1210                                                  "Hide pulldown menus");
1211         }
1212
1213         uiBlockSetEmboss(block, UI_EMBOSS);
1214         
1215         return xco + XIC;
1216 }
1217
1218 /************************ standard UI regions ************************/
1219
1220 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context, int contextnr)
1221 {
1222         ScrArea *sa= CTX_wm_area(C);
1223         uiStyle *style= U.uistyles.first;
1224         uiBlock *block;
1225         PanelType *pt;
1226         Panel *panel;
1227         View2D *v2d= &ar->v2d;
1228         View2DScrollers *scrollers;
1229         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1230
1231         if(contextnr >= 0)
1232                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1233
1234         if(vertical) {
1235                 w= v2d->cur.xmax - v2d->cur.xmin;
1236                 em= (ar->type->prefsizex)? 10: 20;
1237         }
1238         else {
1239                 w= UI_PANEL_WIDTH;
1240                 em= (ar->type->prefsizex)? 10: 20;
1241         }
1242
1243         x= 0;
1244         y= -style->panelouter;
1245
1246         /* create panels */
1247         uiBeginPanels(C, ar);
1248
1249         /* set view2d view matrix for scrolling (without scrollers) */
1250         UI_view2d_view_ortho(C, v2d);
1251
1252         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1253                 /* verify context */
1254                 if(context)
1255                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1256                                 continue;
1257
1258                 /* draw panel */
1259                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1260                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1261                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1262
1263                         /* bad fixed values */
1264                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1265                         triangle= 22;
1266
1267                         if(vertical)
1268                                 y -= header;
1269
1270                         if(pt->draw_header && header && (open || vertical)) {
1271                                 /* for enabled buttons */
1272                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1273                                         triangle, header+style->panelspace, header, 1, style);
1274
1275                                 pt->draw_header(C, panel);
1276
1277                                 uiBlockLayoutResolve(block, &xco, &yco);
1278                                 panel->labelofs= xco - triangle;
1279                                 panel->layout= NULL;
1280                         }
1281
1282                         if(open) {
1283                                 short panelContext;
1284                                 
1285                                 /* panel context can either be toolbar region or normal panels region */
1286                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1287                                         panelContext= UI_LAYOUT_TOOLBAR;
1288                                 else
1289                                         panelContext= UI_LAYOUT_PANEL;
1290                                 
1291                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1292                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1293
1294                                 pt->draw(C, panel);
1295
1296                                 uiBlockLayoutResolve(block, &xco, &yco);
1297                                 panel->layout= NULL;
1298
1299                                 yco -= 2*style->panelspace;
1300                                 uiEndPanel(block, w, -yco);
1301                         }
1302                         else {
1303                                 yco= 0;
1304                                 uiEndPanel(block, w, 0);
1305                         }
1306
1307                         uiEndBlock(C, block);
1308
1309                         if(vertical) {
1310                                 if(pt->flag & PNL_NO_HEADER)
1311                                         y += yco;
1312                                 else
1313                                         y += yco-style->panelouter;
1314                         }
1315                         else {
1316                                 x += w;
1317                                 miny= MIN2(y, yco-style->panelouter-header);
1318                         }
1319                 }
1320         }
1321
1322         if(vertical)
1323                 x += w;
1324         else
1325                 y= miny;
1326         
1327         /* in case there are no panels */
1328         if(x == 0 || y == 0) {
1329                 x= UI_PANEL_WIDTH;
1330                 y= UI_PANEL_WIDTH;
1331         }
1332
1333         /* clear */
1334         UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1335         glClear(GL_COLOR_BUFFER_BIT);
1336         
1337         /* before setting the view */
1338         if(vertical) {
1339                 /* only allow scrolling in vertical direction */
1340                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1341                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1342                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1343                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1344                 
1345                 // don't jump back when panels close or hide
1346                 if(!newcontext)
1347                         y= MAX2(-y, -v2d->cur.ymin);
1348                 else
1349                         y= -y;
1350         }
1351         else {
1352                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1353                  * they often don't fit in horizontal layout)
1354                  */
1355                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1356                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1357                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1358                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1359                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1360                 
1361                 // don't jump back when panels close or hide
1362                 if(!newcontext)
1363                         x= MAX2(x, v2d->cur.xmax);
1364                 y= -y;
1365         }
1366
1367         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1368         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1369
1370         /* set the view */
1371         UI_view2d_view_ortho(C, v2d);
1372
1373         /* this does the actual drawing! */
1374         uiEndPanels(C, ar);
1375         
1376         /* restore view matrix */
1377         UI_view2d_view_restore(C);
1378         
1379         /* scrollers */
1380         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1381         UI_view2d_scrollers_draw(C, v2d, scrollers);
1382         UI_view2d_scrollers_free(scrollers);
1383 }
1384
1385 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1386 {
1387         wmKeyMap *keymap;
1388         
1389         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1390                 // scrollbars for button regions
1391         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1392         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1393         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1394         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1395
1396                 // correctly initialised User-Prefs?
1397         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1398                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1399         
1400         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1401
1402         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1403         WM_event_add_keymap_handler(&ar->handlers, keymap);
1404 }
1405
1406 void ED_region_header(const bContext *C, ARegion *ar)
1407 {
1408         uiStyle *style= U.uistyles.first;
1409         uiBlock *block;
1410         uiLayout *layout;
1411         HeaderType *ht;
1412         Header header = {0};
1413         int maxco, xco, yco;
1414
1415         /* clear */     
1416         UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1417         glClear(GL_COLOR_BUFFER_BIT);
1418         
1419         /* set view2d view matrix for scrolling (without scrollers) */
1420         UI_view2d_view_ortho(C, &ar->v2d);
1421
1422         xco= maxco= 8;
1423         yco= HEADERY-3;
1424
1425         /* draw all headers types */
1426         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1427                 block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
1428                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1429
1430                 if(ht->draw) {
1431                         header.type= ht;
1432                         header.layout= layout;
1433                         ht->draw(C, &header);
1434                         
1435                         /* for view2d */
1436                         xco= uiLayoutGetWidth(layout);
1437                         if(xco > maxco)
1438                                 maxco= xco;
1439                 }
1440
1441                 uiBlockLayoutResolve(block, &xco, &yco);
1442                 
1443                 /* for view2d */
1444                 if(xco > maxco)
1445                         maxco= xco;
1446                 
1447                 uiEndBlock(C, block);
1448                 uiDrawBlock(C, block);
1449         }
1450
1451         /* always as last  */
1452         UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1453
1454         /* restore view matrix? */
1455         UI_view2d_view_restore(C);
1456 }
1457
1458 void ED_region_header_init(ARegion *ar)
1459 {
1460         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1461 }
1462