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