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