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