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