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