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