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