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