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