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