migrated NDOF code from soc-2010-merwin, SpaceNavigator now works on Mac blender
[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         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         /* prefsize, for header we stick to exception */
654         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
655         if(ar->regiontype==RGN_TYPE_HEADER)
656                 prefsizey= ar->type->prefsizey;
657         else
658                 prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
659         
660         /* hidden is user flag */
661         if(ar->flag & RGN_FLAG_HIDDEN);
662         /* XXX floating area region, not handled yet here */
663         else if(alignment == RGN_ALIGN_FLOAT);
664         /* remainder is too small for any usage */
665         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
666                 ar->flag |= RGN_FLAG_TOO_SMALL;
667         }
668         else if(alignment==RGN_ALIGN_NONE) {
669                 /* typically last region */
670                 ar->winrct= *remainder;
671                 BLI_init_rcti(remainder, 0, 0, 0, 0);
672         }
673         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
674                 
675                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
676                         ar->flag |= RGN_FLAG_TOO_SMALL;
677                 }
678                 else {
679                         int fac= rct_fits(remainder, 'v', prefsizey);
680
681                         if(fac < 0 )
682                                 prefsizey += fac;
683                         
684                         ar->winrct= *remainder;
685                         
686                         if(alignment==RGN_ALIGN_TOP) {
687                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
688                                 remainder->ymax= ar->winrct.ymin - 1;
689                         }
690                         else {
691                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
692                                 remainder->ymin= ar->winrct.ymax + 1;
693                         }
694                 }
695         }
696         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
697                 
698                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
699                         ar->flag |= RGN_FLAG_TOO_SMALL;
700                 }
701                 else {
702                         int fac= rct_fits(remainder, 'h', prefsizex);
703                         
704                         if(fac < 0 )
705                                 prefsizex += fac;
706                         
707                         ar->winrct= *remainder;
708                         
709                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
710                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
711                                 if(alignment==RGN_ALIGN_RIGHT)
712                                         remainder->xmax= ar->winrct.xmin - 1;
713                         }
714                         else {
715                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
716                                 if(alignment==RGN_ALIGN_LEFT)
717                                         remainder->xmin= ar->winrct.xmax + 1;
718                         }
719                 }
720         }
721         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
722                 /* percentage subdiv*/
723                 ar->winrct= *remainder;
724                 
725                 if(alignment==RGN_ALIGN_HSPLIT) {
726                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
727                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
728                                 remainder->xmin= ar->winrct.xmax+1;
729                         }
730                         else {
731                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
732                         }
733                 }
734                 else {
735                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
736                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
737                                 remainder->ymin= ar->winrct.ymax+1;
738                         }
739                         else {
740                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
741                         }
742                 }
743         }
744         else if(alignment==RGN_ALIGN_QSPLIT) {
745                 ar->winrct= *remainder;
746                 
747                 /* test if there's still 4 regions left */
748                 if(quad==0) {
749                         ARegion *artest= ar->next;
750                         int count= 1;
751                         
752                         while(artest) {
753                                 artest->alignment= RGN_ALIGN_QSPLIT;
754                                 artest= artest->next;
755                                 count++;
756                         }
757                         
758                         if(count!=4) {
759                                 /* let's stop adding regions */
760                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
761                                 if (G.f & G_DEBUG)
762                                         printf("region quadsplit failed\n");
763                         }
764                         else quad= 1;
765                 }
766                 if(quad) {
767                         if(quad==1) { /* left bottom */
768                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
769                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
770                         }
771                         else if(quad==2) { /* left top */
772                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
773                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
774                         }
775                         else if(quad==3) { /* right bottom */
776                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
777                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
778                         }
779                         else {  /* right top */
780                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
781                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
782                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
783                         }
784
785                         quad++;
786                 }
787         }
788         
789         /* for speedup */
790         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
791         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
792         
793         /* restore test exception */
794         if(ar->alignment & RGN_SPLIT_PREV) {
795                 if(ar->prev) {
796                         remainder= remainder_prev;
797                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
798                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
799                 }
800         }
801
802         /* set winrect for azones */
803         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
804                 ar->winrct= *remainder;
805                 
806                 if(alignment==RGN_ALIGN_TOP)
807                         ar->winrct.ymin= ar->winrct.ymax;
808                 else if(alignment==RGN_ALIGN_BOTTOM)
809                         ar->winrct.ymax= ar->winrct.ymin;
810                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
811                         ar->winrct.xmin= ar->winrct.xmax;
812                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
813                         ar->winrct.xmax= ar->winrct.xmin;
814                 else /* prevent winrct to be valid */
815                         ar->winrct.xmax= ar->winrct.xmin;
816         }
817         /* in end, add azones, where appropriate */
818         if(ar->regiontype == RGN_TYPE_HEADER && ar->winy + 6 > sa->winy) {
819                 /* The logic for this is: when the header takes up the full area,
820                  * disallow hiding it to view the main window.
821                  *
822                  * Without this, uou can drag down the file selectors header and hide it
823                  * by accident very easily (highly annoying!), the value 6 is arbitrary
824                  * but accounts for small common rounding problems when scaling the UI,
825                  * must be minimum '4' */
826         }
827         else {
828                 region_azone_add(sa, ar, alignment);
829         }
830
831         region_rect_recursive(sa, ar->next, remainder, quad);
832 }
833
834 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
835 {
836         short rt= CLAMPIS(G.rt, 0, 16);
837
838         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
839         else sa->totrct.xmin= sa->v1->vec.x;
840         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
841         else sa->totrct.xmax= sa->v4->vec.x;
842         
843         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
844         else sa->totrct.ymin= sa->v1->vec.y;
845         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
846         else sa->totrct.ymax= sa->v2->vec.y;
847         
848         /* for speedup */
849         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
850         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
851 }
852
853
854 /* used for area initialize below */
855 static void region_subwindow(wmWindow *win, ARegion *ar)
856 {
857         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
858                 if(ar->swinid)
859                         wm_subwindow_close(win, ar->swinid);
860                 ar->swinid= 0;
861         }
862         else if(ar->swinid==0)
863                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
864         else 
865                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
866 }
867
868 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
869 {
870         /* note, add-handler checks if it already exists */
871         
872         // XXX it would be good to have boundbox checks for some of these...
873         if(flag & ED_KEYMAP_UI) {
874                 /* user interface widgets */
875                 UI_add_region_handlers(handlers);
876         }
877         if(flag & ED_KEYMAP_VIEW2D) {
878                 /* 2d-viewport handling+manipulation */
879                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
880                 WM_event_add_keymap_handler(handlers, keymap);
881         }
882         if(flag & ED_KEYMAP_MARKERS) {
883                 /* time-markers */
884                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
885                 
886                 /* time space only has this keymap, the others get a boundbox restricted map */
887                 if(sa->spacetype!=SPACE_TIME) {
888                         ARegion *ar;
889                         static rcti rect= {0, 10000, 0, 30};    /* same local check for all areas */
890                         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
891                         if(ar) {
892                                 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
893                         }
894                 }
895                 else
896                         WM_event_add_keymap_handler(handlers, keymap);
897         }
898         if(flag & ED_KEYMAP_ANIMATION) {
899                 /* frame changing and timeline operators (for time spaces) */
900                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
901                 WM_event_add_keymap_handler(handlers, keymap);
902         }
903         if(flag & ED_KEYMAP_FRAMES) {
904                 /* frame changing/jumping (for all spaces) */
905                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
906                 WM_event_add_keymap_handler(handlers, keymap);
907         }
908         if(flag & ED_KEYMAP_GPENCIL) {
909                 /* grease pencil */
910                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
911                 WM_event_add_keymap_handler(handlers, keymap);
912         }
913         if(flag & ED_KEYMAP_HEADER) {
914                 /* standard keymap for headers regions */
915                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
916                 WM_event_add_keymap_handler(handlers, keymap);
917         }
918 }
919
920
921 /* called in screen_refresh, or screens_init, also area size changes */
922 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
923 {
924         ARegion *ar;
925         rcti rect;
926         
927         /* set typedefinitions */
928         sa->type= BKE_spacetype_from_id(sa->spacetype);
929         
930         if(sa->type==NULL) {
931                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
932                 sa->type= BKE_spacetype_from_id(sa->spacetype);
933         }
934         
935         for(ar= sa->regionbase.first; ar; ar= ar->next)
936                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
937         
938         /* area sizes */
939         area_calc_totrct(sa, win->sizex, win->sizey);
940         
941         /* clear all azones, add the area triange widgets */
942         area_azone_initialize(sa);
943
944         /* region rect sizes */
945         rect= sa->totrct;
946         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
947         
948         /* default area handlers */
949         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
950         /* checks spacedata, adds own handlers */
951         if(sa->type->init)
952                 sa->type->init(wm, sa);
953         
954         /* region windows, default and own handlers */
955         for(ar= sa->regionbase.first; ar; ar= ar->next) {
956                 region_subwindow(win, ar);
957                 
958                 if(ar->swinid) {
959                         /* default region handlers */
960                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
961                         /* own handlers */
962                         if(ar->type->init)
963                                 ar->type->init(wm, ar);
964                 }
965                 else {
966                         /* prevent uiblocks to run */
967                         uiFreeBlocks(NULL, &ar->uiblocks);      
968                 }
969                 
970                 /* rechecks 2d matrix for header on dpi changing, do not do for other regions, it resets view && blocks view2d operator polls (ton) */
971                 if(ar->regiontype==RGN_TYPE_HEADER)
972                         ar->v2d.flag &= ~V2D_IS_INITIALISED;
973         }
974 }
975
976 /* externally called for floating regions like menus */
977 void ED_region_init(bContext *C, ARegion *ar)
978 {
979 //      ARegionType *at= ar->type;
980         
981         /* refresh can be called before window opened */
982         region_subwindow(CTX_wm_window(C), ar);
983         
984         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
985         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
986         
987         /* UI convention */
988         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
989         glLoadIdentity();
990 }
991
992 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
993 {
994         ScrArea *sa= CTX_wm_area(C);
995
996         ar->flag ^= RGN_FLAG_HIDDEN;
997
998         if(ar->flag & RGN_FLAG_HIDDEN)
999                 WM_event_remove_handlers(C, &ar->handlers);
1000
1001         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1002         ED_area_tag_redraw(sa);
1003 }
1004
1005 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
1006 /* area vertices were set */
1007 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
1008 {
1009         SpaceType *st;
1010         ARegion *ar;
1011         int spacetype= sa1->spacetype;
1012         
1013         sa1->headertype= sa2->headertype;
1014         sa1->spacetype= sa2->spacetype;
1015         sa1->butspacetype= sa2->butspacetype;
1016         
1017         if(swap_space == 1) {
1018                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
1019                 /* exception: ensure preview is reset */
1020 //              if(sa1->spacetype==SPACE_VIEW3D)
1021 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
1022         }
1023         else if (swap_space == 2) {
1024                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1025         }
1026         else {
1027                 BKE_spacedata_freelist(&sa1->spacedata);
1028                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1029         }
1030         
1031         /* Note; SPACE_EMPTY is possible on new screens */
1032         
1033         /* regions */
1034         if(swap_space == 1) {
1035                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
1036         }
1037         else {
1038                 if(swap_space<2) {
1039                         st= BKE_spacetype_from_id(spacetype);
1040                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
1041                                 BKE_area_region_free(st, ar);
1042                         BLI_freelistN(&sa1->regionbase);
1043                 }
1044                 
1045                 st= BKE_spacetype_from_id(sa2->spacetype);
1046                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
1047                         ARegion *newar= BKE_area_region_copy(st, ar);
1048                         BLI_addtail(&sa1->regionbase, newar);
1049                 }
1050         }
1051 }
1052
1053 /* *********** Space switching code *********** */
1054
1055 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1056 {
1057         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
1058
1059         ED_area_exit(C, sa1);
1060         ED_area_exit(C, sa2);
1061
1062         area_copy_data(tmp, sa1, 2);
1063         area_copy_data(sa1, sa2, 0);
1064         area_copy_data(sa2, tmp, 0);
1065         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1066         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1067
1068         BKE_screen_area_free(tmp);
1069         MEM_freeN(tmp);
1070
1071         /* tell WM to refresh, cursor types etc */
1072         WM_event_add_mousemove(C);
1073         
1074         ED_area_tag_redraw(sa1);
1075         ED_area_tag_refresh(sa1);
1076         ED_area_tag_redraw(sa2);
1077         ED_area_tag_refresh(sa2);
1078 }
1079
1080 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1081 {
1082         if(sa->spacetype != type) {
1083                 SpaceType *st;
1084                 SpaceLink *slold;
1085                 SpaceLink *sl;
1086
1087                 ED_area_exit(C, sa);
1088
1089                 st= BKE_spacetype_from_id(type);
1090                 slold= sa->spacedata.first;
1091
1092                 sa->spacetype= type;
1093                 sa->butspacetype= type;
1094                 sa->type= st;
1095                 
1096                 /* check previously stored space */
1097                 for (sl= sa->spacedata.first; sl; sl= sl->next)
1098                         if(sl->spacetype==type)
1099                                 break;
1100                 
1101                 /* old spacedata... happened during work on 2.50, remove */
1102                 if(sl && sl->regionbase.first==NULL) {
1103                         st->free(sl);
1104                         BLI_freelinkN(&sa->spacedata, sl);
1105                         if(slold == sl) {
1106                                 slold= NULL;
1107                         }
1108                         sl= NULL;
1109                 }
1110                 
1111                 if (sl) {
1112                         
1113                         /* swap regions */
1114                         slold->regionbase= sa->regionbase;
1115                         sa->regionbase= sl->regionbase;
1116                         sl->regionbase.first= sl->regionbase.last= NULL;
1117                         
1118                         /* put in front of list */
1119                         BLI_remlink(&sa->spacedata, sl);
1120                         BLI_addhead(&sa->spacedata, sl);
1121                 } 
1122                 else {
1123                         /* new space */
1124                         if(st) {
1125                                 sl= st->new(C);
1126                                 BLI_addhead(&sa->spacedata, sl);
1127                                 
1128                                 /* swap regions */
1129                                 if(slold)
1130                                         slold->regionbase= sa->regionbase;
1131                                 sa->regionbase= sl->regionbase;
1132                                 sl->regionbase.first= sl->regionbase.last= NULL;
1133                         }
1134                 }
1135                 
1136                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1137                 
1138                 /* tell WM to refresh, cursor types etc */
1139                 WM_event_add_mousemove(C);
1140                                 
1141                 /*send space change notifier*/
1142                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1143                 
1144                 ED_area_tag_refresh(sa);
1145         }
1146         
1147         /* also redraw when re-used */
1148         ED_area_tag_redraw(sa);
1149 }
1150
1151 void ED_area_prevspace(bContext *C, ScrArea *sa)
1152 {
1153         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1154
1155         if(sl->next) {
1156                 /* workaround for case of double prevspace, render window
1157                    with a file browser on top of it */
1158                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1159                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1160                 else
1161                         ED_area_newspace(C, sa, sl->next->spacetype);
1162         }
1163         else {
1164                 ED_area_newspace(C, sa, SPACE_INFO);
1165         }
1166         ED_area_tag_redraw(sa);
1167
1168         /*send space change notifier*/
1169         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1170 }
1171
1172 static const char *editortype_pup(void)
1173 {
1174         return(
1175                    "Editor type:%t"
1176                    "|3D View %x1"
1177
1178                    "|%l"
1179                    
1180                    "|Timeline %x15"
1181                    "|Graph Editor %x2"
1182                    "|DopeSheet %x12"
1183                    "|NLA Editor %x13"
1184                    
1185                    "|%l"
1186                    
1187                    "|UV/Image Editor %x6"
1188                    
1189                    "|Video Sequence Editor %x8"
1190                    "|Text Editor %x9" 
1191                    "|Node Editor %x16"
1192                    "|Logic Editor %x17"
1193                    
1194                    "|%l"
1195                    
1196                    "|Properties %x4"
1197                    "|Outliner %x3"
1198                    "|User Preferences %x19"
1199                    "|Info%x7"
1200                                    
1201                    "|%l"
1202                    
1203                    "|File Browser %x5"
1204                    
1205                    "|%l"
1206                    
1207                    "|Python Console %x18"
1208                    );
1209 }
1210
1211 static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1212 {
1213         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1214         ED_area_tag_redraw(CTX_wm_area(C));
1215
1216         /*send space change notifier*/
1217         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1218 }
1219
1220 /* returns offset for next button in header */
1221 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1222 {
1223         ScrArea *sa= CTX_wm_area(C);
1224         uiBut *but;
1225         int xco= 8;
1226         
1227         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1228                                                    editortype_pup(), xco, yco, UI_UNIT_X+10, UI_UNIT_Y, 
1229                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1230                                                    "Displays current editor type. "
1231                                                    "Click for menu of available types");
1232         uiButSetFunc(but, spacefunc, NULL, NULL);
1233         
1234         return xco + UI_UNIT_X + 14;
1235 }
1236
1237 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1238 {
1239         ScrArea *sa= CTX_wm_area(C);
1240         int xco= 8;
1241         
1242         if (!sa->full)
1243                 xco= ED_area_header_switchbutton(C, block, yco);
1244
1245         uiBlockSetEmboss(block, UI_EMBOSSN);
1246
1247         if (sa->flag & HEADER_NO_PULLDOWN) {
1248                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1249                                                  ICON_DISCLOSURE_TRI_RIGHT,
1250                                                  xco,yco,UI_UNIT_X,UI_UNIT_Y-2,
1251                                                  &(sa->flag), 0, 0, 0, 0, 
1252                                                  "Show pulldown menus");
1253         }
1254         else {
1255                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1256                                                  ICON_DISCLOSURE_TRI_DOWN,
1257                                                  xco,yco,UI_UNIT_X,UI_UNIT_Y-2,
1258                                                  &(sa->flag), 0, 0, 0, 0, 
1259                                                  "Hide pulldown menus");
1260         }
1261
1262         uiBlockSetEmboss(block, UI_EMBOSS);
1263         
1264         return xco + UI_UNIT_X;
1265 }
1266
1267 /************************ standard UI regions ************************/
1268
1269 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1270 {
1271         ScrArea *sa= CTX_wm_area(C);
1272         uiStyle *style= U.uistyles.first;
1273         uiBlock *block;
1274         PanelType *pt;
1275         Panel *panel;
1276         View2D *v2d= &ar->v2d;
1277         View2DScrollers *scrollers;
1278         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1279
1280         if(contextnr >= 0)
1281                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1282
1283         if(vertical) {
1284                 w= v2d->cur.xmax - v2d->cur.xmin;
1285                 em= (ar->type->prefsizex)? UI_UNIT_Y/2: UI_UNIT_Y;
1286         }
1287         else {
1288                 w= UI_PANEL_WIDTH;
1289                 em= (ar->type->prefsizex)? UI_UNIT_Y/2: UI_UNIT_Y;
1290         }
1291
1292         x= 0;
1293         y= -style->panelouter;
1294
1295         /* create panels */
1296         uiBeginPanels(C, ar);
1297
1298         /* set view2d view matrix for scrolling (without scrollers) */
1299         UI_view2d_view_ortho(v2d);
1300
1301         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1302                 /* verify context */
1303                 if(context)
1304                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1305                                 continue;
1306
1307                 /* draw panel */
1308                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1309                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1310                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1311
1312                         /* bad fixed values */
1313                         header= (pt->flag & PNL_NO_HEADER)? 0: UI_UNIT_Y;
1314                         triangle= (int)(UI_UNIT_Y * 1.1f);
1315
1316                         if(vertical)
1317                                 y -= header;
1318
1319                         if(pt->draw_header && header && (open || vertical)) {
1320                                 /* for enabled buttons */
1321                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1322                                         triangle, header+style->panelspace, header, 1, style);
1323
1324                                 pt->draw_header(C, panel);
1325
1326                                 uiBlockLayoutResolve(block, &xco, &yco);
1327                                 panel->labelofs= xco - triangle;
1328                                 panel->layout= NULL;
1329                         }
1330                         else {
1331                                 panel->labelofs= 0;
1332                         }
1333
1334                         if(open) {
1335                                 short panelContext;
1336                                 
1337                                 /* panel context can either be toolbar region or normal panels region */
1338                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1339                                         panelContext= UI_LAYOUT_TOOLBAR;
1340                                 else
1341                                         panelContext= UI_LAYOUT_PANEL;
1342                                 
1343                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1344                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1345
1346                                 pt->draw(C, panel);
1347
1348                                 uiBlockLayoutResolve(block, &xco, &yco);
1349                                 panel->layout= NULL;
1350
1351                                 yco -= 2*style->panelspace;
1352                                 uiEndPanel(block, w, -yco);
1353                         }
1354                         else {
1355                                 yco= 0;
1356                                 uiEndPanel(block, w, 0);
1357                         }
1358
1359                         uiEndBlock(C, block);
1360
1361                         if(vertical) {
1362                                 if(pt->flag & PNL_NO_HEADER)
1363                                         y += yco;
1364                                 else
1365                                         y += yco-style->panelouter;
1366                         }
1367                         else {
1368                                 x += w;
1369                                 miny= MIN2(y, yco-style->panelouter-header);
1370                         }
1371                 }
1372         }
1373
1374         if(vertical)
1375                 x += w;
1376         else
1377                 y= miny;
1378         
1379         /* in case there are no panels */
1380         if(x == 0 || y == 0) {
1381                 x= UI_PANEL_WIDTH;
1382                 y= UI_PANEL_WIDTH;
1383         }
1384
1385         /* clear */
1386         UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1387         glClear(GL_COLOR_BUFFER_BIT);
1388         
1389         /* before setting the view */
1390         if(vertical) {
1391                 /* only allow scrolling in vertical direction */
1392                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1393                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1394                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1395                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1396                 
1397                 // don't jump back when panels close or hide
1398                 if(!newcontext)
1399                         y= MAX2(-y, -v2d->cur.ymin);
1400                 else
1401                         y= -y;
1402         }
1403         else {
1404                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1405                  * they often don't fit in horizontal layout)
1406                  */
1407                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1408                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1409                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1410                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1411                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1412                 
1413                 // don't jump back when panels close or hide
1414                 if(!newcontext)
1415                         x= MAX2(x, v2d->cur.xmax);
1416                 y= -y;
1417         }
1418
1419         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1420         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1421
1422         /* set the view */
1423         UI_view2d_view_ortho(v2d);
1424
1425         /* this does the actual drawing! */
1426         uiEndPanels(C, ar);
1427         
1428         /* restore view matrix */
1429         UI_view2d_view_restore(C);
1430         
1431         /* scrollers */
1432         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1433         UI_view2d_scrollers_draw(C, v2d, scrollers);
1434         UI_view2d_scrollers_free(scrollers);
1435 }
1436
1437 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1438 {
1439         wmKeyMap *keymap;
1440         
1441         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1442                 // scrollbars for button regions
1443         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1444         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1445         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1446         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1447
1448                 // correctly initialised User-Prefs?
1449         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1450                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1451         
1452         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1453
1454         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1455         WM_event_add_keymap_handler(&ar->handlers, keymap);
1456 }
1457
1458 void ED_region_header(const bContext *C, ARegion *ar)
1459 {
1460         uiStyle *style= U.uistyles.first;
1461         uiBlock *block;
1462         uiLayout *layout;
1463         HeaderType *ht;
1464         Header header = {NULL};
1465         int maxco, xco, yco;
1466         int headery= ED_area_headersize();
1467
1468         /* clear */     
1469         UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1470         glClear(GL_COLOR_BUFFER_BIT);
1471         
1472         /* set view2d view matrix for scrolling (without scrollers) */
1473         UI_view2d_view_ortho(&ar->v2d);
1474
1475         xco= maxco= 8;
1476         yco= headery-4;
1477
1478         /* draw all headers types */
1479         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1480                 block= uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1481                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, UI_UNIT_Y, 1, style);
1482
1483                 if(ht->draw) {
1484                         header.type= ht;
1485                         header.layout= layout;
1486                         ht->draw(C, &header);
1487                         
1488                         /* for view2d */
1489                         xco= uiLayoutGetWidth(layout);
1490                         if(xco > maxco)
1491                                 maxco= xco;
1492                 }
1493
1494                 uiBlockLayoutResolve(block, &xco, &yco);
1495                 
1496                 /* for view2d */
1497                 if(xco > maxco)
1498                         maxco= xco;
1499                 
1500                 uiEndBlock(C, block);
1501                 uiDrawBlock(C, block);
1502         }
1503
1504         /* always as last  */
1505         UI_view2d_totRect_set(&ar->v2d, maxco+UI_UNIT_X+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1506
1507         /* restore view matrix? */
1508         UI_view2d_view_restore(C);
1509 }
1510
1511 void ED_region_header_init(ARegion *ar)
1512 {
1513         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1514 }
1515
1516 /* UI_UNIT_Y is defined as U variable now, depending dpi */
1517 int ED_area_headersize(void)
1518 {
1519         return UI_UNIT_Y+6;
1520 }