Todo items:
[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
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_screen.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46 #include "wm_subwindow.h"
47
48 #include "ED_screen.h"
49 #include "ED_screen_types.h"
50 #include "ED_space_api.h"
51 #include "ED_types.h"
52 #include "ED_fileselect.h" 
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56 #include "BLF_api.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "screen_intern.h"
63
64 /* general area and region code */
65
66 static void region_draw_emboss(ARegion *ar, rcti *scirct)
67 {
68         rcti rect;
69         
70         /* translate scissor rect to region space */
71         rect.xmin= scirct->xmin - ar->winrct.xmin;
72         rect.ymin= scirct->ymin - ar->winrct.ymin;
73         rect.xmax= scirct->xmax - ar->winrct.xmin;
74         rect.ymax= scirct->ymax - ar->winrct.ymin;
75         
76         /* set transp line */
77         glEnable( GL_BLEND );
78         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
79         
80         /* right  */
81         glColor4ub(0,0,0, 50);
82         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
83         
84         /* bottom  */
85         glColor4ub(0,0,0, 80);
86         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
87         
88         /* top  */
89         glColor4ub(255,255,255, 60);
90         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
91
92         /* left  */
93         glColor4ub(255,255,255, 50);
94         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
95         
96         glDisable( GL_BLEND );
97 }
98
99 void ED_region_pixelspace(ARegion *ar)
100 {
101         int width= ar->winrct.xmax-ar->winrct.xmin+1;
102         int height= ar->winrct.ymax-ar->winrct.ymin+1;
103         
104         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
105         glLoadIdentity();
106 }
107
108 /* only exported for WM */
109 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
110 {
111         /* generic notes first */
112         switch(note->category) {
113                 case NC_WM:
114                         if(note->data==ND_FILEREAD)
115                                 ED_region_tag_redraw(ar);
116                         break;
117                 case NC_WINDOW:
118                         ED_region_tag_redraw(ar);
119                         break;
120         }
121
122         if(ar->type && ar->type->listener)
123                 ar->type->listener(ar, note);
124 }
125
126 /* only exported for WM */
127 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
128 {
129         /* no generic notes? */
130         if(sa->type && sa->type->listener) {
131                 sa->type->listener(sa, note);
132         }
133 }
134
135 /* only exported for WM */
136 void ED_area_do_refresh(bContext *C, ScrArea *sa)
137 {
138         /* no generic notes? */
139         if(sa->type && sa->type->refresh) {
140                 sa->type->refresh(C, sa);
141         }
142         sa->do_refresh= 0;
143 }
144
145 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
146 /* only exported for WM */
147 void ED_area_overdraw_flush(ScrArea *sa, ARegion *ar)
148 {
149         AZone *az;
150         
151         for(az= sa->actionzones.first; az; az= az->next) {
152                 int xs, ys;
153                 
154                 xs= (az->x1+az->x2)/2;
155                 ys= (az->y1+az->y2)/2;
156
157                 /* test if inside */
158                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
159                         az->do_draw= 1;
160                 }
161         }
162 }
163
164 static void area_draw_azone(short x1, short y1, short x2, short y2)
165 {
166         float xmin = x1;
167         float xmax = x2-2;
168         float ymin = y1-1;
169         float ymax = y2-3;
170         
171         float dx= 0.3f*(xmax-xmin);
172         float dy= 0.3f*(ymax-ymin);
173         
174         glColor4ub(255, 255, 255, 180);
175         fdrawline(xmin, ymax, xmax, ymin);
176         glColor4ub(255, 255, 255, 130);
177         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
178         glColor4ub(255, 255, 255, 80);
179         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
180         
181         glColor4ub(0, 0, 0, 210);
182         fdrawline(xmin, ymax+1, xmax+1, ymin);
183         glColor4ub(0, 0, 0, 180);
184         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
185         glColor4ub(0, 0, 0, 150);
186         fdrawline(xmin, ymax-2*dy+1, xmax-2*dx+1, ymin);
187 }
188
189
190 static void region_draw_azone(AZone *az)
191 {
192         GLUquadricObj *qobj = NULL; 
193         short midx = az->x1 + (az->x2 - az->x1)/2;
194         short midy = az->y1 + (az->y2 - az->y1)/2;
195         
196         if(az->ar==NULL) return;
197         
198         /* only display action zone icons when the region is hidden */
199         if (!(az->ar->flag & RGN_FLAG_HIDDEN)) return;
200         
201         qobj = gluNewQuadric();
202         
203         glPushMatrix();         
204         glTranslatef(midx, midy, 0.); 
205         
206         /* outlined circle */
207         glEnable(GL_LINE_SMOOTH);
208
209         glColor4f(1.f, 1.f, 1.f, 0.8f);
210
211         gluQuadricDrawStyle(qobj, GLU_FILL); 
212         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
213
214         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
215         
216         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
217         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
218         
219         glDisable(GL_LINE_SMOOTH);
220         
221         glPopMatrix();
222         gluDeleteQuadric(qobj);
223         
224         /* + */
225         sdrawline(midx, midy-2, midx, midy+3);
226         sdrawline(midx-2, midy, midx+3, midy);
227 }
228
229
230 /* only exported for WM */
231 void ED_area_overdraw(bContext *C)
232 {
233         wmWindow *win= CTX_wm_window(C);
234         bScreen *screen= CTX_wm_screen(C);
235         ScrArea *sa;
236         
237         /* Draw AZones, in screenspace */
238         wmSubWindowSet(win, screen->mainwin);
239
240         glEnable( GL_BLEND );
241         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
242         
243         for(sa= screen->areabase.first; sa; sa= sa->next) {
244                 AZone *az;
245                 for(az= sa->actionzones.first; az; az= az->next) {
246                         if(az->do_draw) {
247                                 if(az->type==AZONE_AREA) {
248                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
249                                 } else if(az->type==AZONE_REGION) {
250                                         region_draw_azone(az);
251                                 }
252                                 
253                                 az->do_draw= 0;
254                         }
255                 }
256         }       
257         glDisable( GL_BLEND );
258         
259 }
260
261 /* get scissor rect, checking overlapping regions */
262 void region_scissor_winrct(ARegion *ar, rcti *winrct)
263 {
264         *winrct= ar->winrct;
265         
266         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
267                 return;
268
269         while(ar->prev) {
270                 ar= ar->prev;
271                 
272                 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
273                         if(ar->flag & RGN_FLAG_HIDDEN);
274                         else if(ar->alignment & RGN_SPLIT_PREV);
275                         else if(ar->alignment==RGN_OVERLAP_LEFT) {
276                                 winrct->xmin= ar->winrct.xmax + 1;
277                         }
278                         else if(ar->alignment==RGN_OVERLAP_RIGHT) {
279                                 winrct->xmax= ar->winrct.xmin - 1;
280                         }
281                         else break;
282                 }
283         }
284 }
285
286 /* only exported for WM */
287 /* makes region ready for drawing, sets pixelspace */
288 void ED_region_set(const bContext *C, ARegion *ar)
289 {
290         wmWindow *win= CTX_wm_window(C);
291         ScrArea *sa= CTX_wm_area(C);
292         rcti winrct;
293         
294         /* checks other overlapping regions */
295         region_scissor_winrct(ar, &winrct);
296         
297         ar->drawrct= winrct;
298         
299         /* note; this sets state, so we can use wmOrtho and friends */
300         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
301         
302         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
303         
304         ED_region_pixelspace(ar);
305 }
306
307
308 /* only exported for WM */
309 void ED_region_do_draw(bContext *C, ARegion *ar)
310 {
311         wmWindow *win= CTX_wm_window(C);
312         ScrArea *sa= CTX_wm_area(C);
313         ARegionType *at= ar->type;
314         rcti winrct;
315         
316         /* checks other overlapping regions */
317         region_scissor_winrct(ar, &winrct);
318         
319         /* if no partial draw rect set, full rect */
320         if(ar->drawrct.xmin == ar->drawrct.xmax)
321                 ar->drawrct= winrct;
322         else {
323                 /* extra clip for safety */
324                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
325                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
326                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
327                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
328         }
329         
330         /* note; this sets state, so we can use wmOrtho and friends */
331         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
332         
333         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
334         
335         /* optional header info instead? */
336         if(ar->headerstr) {
337                 UI_ThemeClearColor(TH_HEADER);
338                 glClear(GL_COLOR_BUFFER_BIT);
339                 
340                 UI_ThemeColor(TH_TEXT);
341                 BLF_draw_default(20, 8, 0.0f, ar->headerstr, 65535); /* XXX, use real length */
342         }
343         else if(at->draw) {
344                 at->draw(C, ar);
345         }
346
347         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
348         
349         uiFreeInactiveBlocks(C, &ar->uiblocks);
350         
351         if(sa)
352                 region_draw_emboss(ar, &winrct);
353         
354         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
355         ED_region_pixelspace(ar);
356         
357         ar->do_draw= 0;
358         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
359 }
360
361 /* **********************************
362    maybe silly, but let's try for now
363    to keep these tags protected
364    ********************************** */
365
366 void ED_region_tag_redraw(ARegion *ar)
367 {
368         if(ar) {
369                 /* zero region means full region redraw */
370                 ar->do_draw= RGN_DRAW;
371                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
372         }
373 }
374
375 void ED_region_tag_redraw_overlay(ARegion *ar)
376 {
377         if(ar)
378                 ar->do_draw_overlay= RGN_DRAW;
379 }
380
381 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
382 {
383         if(ar) {
384                 if(!ar->do_draw) {
385                         /* no redraw set yet, set partial region */
386                         ar->do_draw= RGN_DRAW_PARTIAL;
387                         ar->drawrct= *rct;
388                 }
389                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
390                         /* partial redraw already set, expand region */
391                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
392                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
393                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
394                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
395                 }
396         }
397 }
398
399 void ED_area_tag_redraw(ScrArea *sa)
400 {
401         ARegion *ar;
402         
403         if(sa)
404                 for(ar= sa->regionbase.first; ar; ar= ar->next)
405                         ED_region_tag_redraw(ar);
406 }
407
408 void ED_area_tag_redraw_regiontype(ScrArea *sa, int regiontype)
409 {
410         ARegion *ar;
411         
412         if(sa) {
413                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
414                         if(ar->regiontype == regiontype) {
415                                 ED_region_tag_redraw(ar);
416                         }
417                 }
418         }
419 }
420
421 void ED_area_tag_refresh(ScrArea *sa)
422 {
423         if(sa)
424                 sa->do_refresh= 1;
425 }
426
427 /* *************************************************************** */
428
429 /* use NULL to disable it */
430 void ED_area_headerprint(ScrArea *sa, const char *str)
431 {
432         ARegion *ar;
433         
434         for(ar= sa->regionbase.first; ar; ar= ar->next) {
435                 if(ar->regiontype==RGN_TYPE_HEADER) {
436                         if(str) {
437                                 if(ar->headerstr==NULL)
438                                         ar->headerstr= MEM_mallocN(256, "headerprint");
439                                 BLI_strncpy(ar->headerstr, str, 256);
440                         }
441                         else if(ar->headerstr) {
442                                 MEM_freeN(ar->headerstr);
443                                 ar->headerstr= NULL;
444                         }
445                         ED_region_tag_redraw(ar);
446                 }
447         }
448 }
449
450 /* ************************************************************ */
451
452
453 #define AZONESPOT               12
454 static void area_azone_initialize(ScrArea *sa) 
455 {
456         AZone *az;
457         
458         /* reinitalize entirely, regions add azones too */
459         BLI_freelistN(&sa->actionzones);
460         
461         /* set area action zones */
462         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
463         BLI_addtail(&(sa->actionzones), az);
464         az->type= AZONE_AREA;
465         az->x1= sa->totrct.xmin;
466         az->y1= sa->totrct.ymin;
467         az->x2= sa->totrct.xmin + AZONESPOT-1;
468         az->y2= sa->totrct.ymin + AZONESPOT-1;
469         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
470         
471         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
472         BLI_addtail(&(sa->actionzones), az);
473         az->type= AZONE_AREA;
474         az->x1= sa->totrct.xmax+1;
475         az->y1= sa->totrct.ymax+1;
476         az->x2= sa->totrct.xmax-AZONESPOT+1;
477         az->y2= sa->totrct.ymax-AZONESPOT+1;
478         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
479 }
480
481 #define AZONEPAD_EDGE   4
482 #define AZONEPAD_ICON   8
483 static void region_azone_edge(AZone *az, ARegion *ar)
484 {
485         switch(az->edge) {
486                 case AE_TOP_TO_BOTTOMRIGHT:
487                         az->x1= ar->winrct.xmin;
488                         az->y1= ar->winrct.ymax - AZONEPAD_EDGE;
489                         az->x2= ar->winrct.xmax;
490                         az->y2= ar->winrct.ymax;
491                         break;
492                 case AE_BOTTOM_TO_TOPLEFT:
493                         az->x1= ar->winrct.xmin;
494                         az->y1= ar->winrct.ymin + AZONEPAD_EDGE;
495                         az->x2= ar->winrct.xmax;
496                         az->y2= ar->winrct.ymin;
497                         break;
498                 case AE_LEFT_TO_TOPRIGHT:
499                         az->x1= ar->winrct.xmin;
500                         az->y1= ar->winrct.ymin;
501                         az->x2= ar->winrct.xmin + AZONEPAD_EDGE;
502                         az->y2= ar->winrct.ymax;
503                         break;
504                 case AE_RIGHT_TO_TOPLEFT:
505                         az->x1= ar->winrct.xmax;
506                         az->y1= ar->winrct.ymin;
507                         az->x2= ar->winrct.xmax - AZONEPAD_EDGE;
508                         az->y2= ar->winrct.ymax;
509                         break;
510         }
511
512         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
513 }
514
515 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
516 {
517         AZone *azt;
518         int tot=0;
519         
520         /* count how many actionzones with along same edge are available.
521            This allows for adding more action zones in the future without
522            having to worry about correct offset */
523         for(azt= sa->actionzones.first; azt; azt= azt->next) {
524                 if(azt->edge == az->edge) tot++;
525         }
526         
527         switch(az->edge) {
528                 case AE_TOP_TO_BOTTOMRIGHT:
529                         az->x1= ar->winrct.xmax - tot*2*AZONEPAD_ICON;
530                         az->y1= ar->winrct.ymax + AZONEPAD_ICON;
531                         az->x2= ar->winrct.xmax - tot*AZONEPAD_ICON;
532                         az->y2= ar->winrct.ymax + 2*AZONEPAD_ICON;
533                         break;
534                 case AE_BOTTOM_TO_TOPLEFT:
535                         az->x1= ar->winrct.xmin + AZONEPAD_ICON;
536                         az->y1= ar->winrct.ymin - 2*AZONEPAD_ICON;
537                         az->x2= ar->winrct.xmin + 2*AZONEPAD_ICON;
538                         az->y2= ar->winrct.ymin - AZONEPAD_ICON;
539                         break;
540                 case AE_LEFT_TO_TOPRIGHT:
541                         az->x1= ar->winrct.xmin - 2*AZONEPAD_ICON;
542                         az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
543                         az->x2= ar->winrct.xmin - AZONEPAD_ICON;
544                         az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
545                         break;
546                 case AE_RIGHT_TO_TOPLEFT:
547                         az->x1= ar->winrct.xmax + AZONEPAD_ICON;
548                         az->y1= ar->winrct.ymax - tot*2*AZONEPAD_ICON;
549                         az->x2= ar->winrct.xmax + 2*AZONEPAD_ICON;
550                         az->y2= ar->winrct.ymax - tot*AZONEPAD_ICON;
551                         break;
552         }
553
554         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
555         
556         /* if more azones on 1 spot, set offset */
557         for(azt= sa->actionzones.first; azt; azt= azt->next) {
558                 if(az!=azt) {
559                         if( ABS(az->x1-azt->x1) < 2 && ABS(az->y1-azt->y1) < 2) {
560                                 if(az->edge==AE_TOP_TO_BOTTOMRIGHT || az->edge==AE_BOTTOM_TO_TOPLEFT) {
561                                         az->x1+= AZONESPOT;
562                                         az->x2+= AZONESPOT;
563                                 }
564                                 else{
565                                         az->y1-= AZONESPOT;
566                                         az->y2-= AZONESPOT;
567                                 }
568                                 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
569                         }
570                 }
571         }
572 }
573
574 static void region_azone_initialize(ScrArea *sa, ARegion *ar, AZEdge edge) 
575 {
576         AZone *az;
577         
578         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
579         BLI_addtail(&(sa->actionzones), az);
580         az->type= AZONE_REGION;
581         az->ar= ar;
582         az->edge= edge;
583         
584         if (ar->flag & RGN_FLAG_HIDDEN) {
585                 region_azone_icon(sa, az, ar);
586         } else {
587                 region_azone_edge(az, ar);
588         }
589         
590 }
591
592
593 /* *************************************************************** */
594
595 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
596 {
597          /* edge code (t b l r) is along which area edge azone will be drawn */
598         
599         if(alignment==RGN_ALIGN_TOP)
600                 region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT);
601         else if(alignment==RGN_ALIGN_BOTTOM)
602                 region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT);
603         else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
604                 region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT);
605         else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
606                 region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT);
607 }
608
609 /* dir is direction to check, not the splitting edge direction! */
610 static int rct_fits(rcti *rect, char dir, int size)
611 {
612         if(dir=='h') {
613                 return rect->xmax-rect->xmin - size;
614         }
615         else { // 'v'
616                 return rect->ymax-rect->ymin - size;
617         }
618 }
619
620 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
621 {
622         rcti *remainder_prev= remainder;
623         int prefsizex, prefsizey;
624         int alignment;
625         
626         if(ar==NULL)
627                 return;
628         
629         /* no returns in function, winrct gets set in the end again */
630         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
631         
632         /* for test; allow split of previously defined region */
633         if(ar->alignment & RGN_SPLIT_PREV)
634                 if(ar->prev)
635                         remainder= &ar->prev->winrct;
636         
637         alignment = ar->alignment & ~RGN_SPLIT_PREV;
638         
639         /* clear state flags first */
640         ar->flag &= ~RGN_FLAG_TOO_SMALL;
641         /* user errors */
642         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
643                 alignment= RGN_ALIGN_NONE;
644         
645         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
646         prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
647         
648         /* hidden is user flag */
649         if(ar->flag & RGN_FLAG_HIDDEN);
650         /* XXX floating area region, not handled yet here */
651         else if(alignment == RGN_ALIGN_FLOAT);
652         /* remainder is too small for any usage */
653         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
654                 ar->flag |= RGN_FLAG_TOO_SMALL;
655         }
656         else if(alignment==RGN_ALIGN_NONE) {
657                 /* typically last region */
658                 ar->winrct= *remainder;
659                 BLI_init_rcti(remainder, 0, 0, 0, 0);
660         }
661         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
662                 
663                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
664                         ar->flag |= RGN_FLAG_TOO_SMALL;
665                 }
666                 else {
667                         int fac= rct_fits(remainder, 'v', prefsizey);
668
669                         if(fac < 0 )
670                                 prefsizey += fac;
671                         
672                         ar->winrct= *remainder;
673                         
674                         if(alignment==RGN_ALIGN_TOP) {
675                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
676                                 remainder->ymax= ar->winrct.ymin - 1;
677                         }
678                         else {
679                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
680                                 remainder->ymin= ar->winrct.ymax + 1;
681                         }
682                 }
683         }
684         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
685                 
686                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
687                         ar->flag |= RGN_FLAG_TOO_SMALL;
688                 }
689                 else {
690                         int fac= rct_fits(remainder, 'h', prefsizex);
691                         
692                         if(fac < 0 )
693                                 prefsizex += fac;
694                         
695                         ar->winrct= *remainder;
696                         
697                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
698                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
699                                 if(alignment==RGN_ALIGN_RIGHT)
700                                         remainder->xmax= ar->winrct.xmin - 1;
701                         }
702                         else {
703                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
704                                 if(alignment==RGN_ALIGN_LEFT)
705                                         remainder->xmin= ar->winrct.xmax + 1;
706                         }
707                 }
708         }
709         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
710                 /* percentage subdiv*/
711                 ar->winrct= *remainder;
712                 
713                 if(alignment==RGN_ALIGN_HSPLIT) {
714                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
715                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
716                                 remainder->xmin= ar->winrct.xmax+1;
717                         }
718                         else {
719                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
720                         }
721                 }
722                 else {
723                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
724                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
725                                 remainder->ymin= ar->winrct.ymax+1;
726                         }
727                         else {
728                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
729                         }
730                 }
731         }
732         else if(alignment==RGN_ALIGN_QSPLIT) {
733                 ar->winrct= *remainder;
734                 
735                 /* test if there's still 4 regions left */
736                 if(quad==0) {
737                         ARegion *artest= ar->next;
738                         int count= 1;
739                         
740                         while(artest) {
741                                 artest->alignment= RGN_ALIGN_QSPLIT;
742                                 artest= artest->next;
743                                 count++;
744                         }
745                         
746                         if(count!=4) {
747                                 /* let's stop adding regions */
748                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
749                                 if (G.f & G_DEBUG)
750                                         printf("region quadsplit failed\n");
751                         }
752                         else quad= 1;
753                 }
754                 if(quad) {
755                         if(quad==1) { /* left bottom */
756                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
757                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
758                         }
759                         else if(quad==2) { /* left top */
760                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
761                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
762                         }
763                         else if(quad==3) { /* right bottom */
764                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
765                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
766                         }
767                         else {  /* right top */
768                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
769                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
770                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
771                         }
772
773                         quad++;
774                 }
775         }
776         
777         /* for speedup */
778         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
779         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
780         
781         /* restore test exception */
782         if(ar->alignment & RGN_SPLIT_PREV) {
783                 if(ar->prev) {
784                         remainder= remainder_prev;
785                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
786                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
787                 }
788         }
789
790         /* set winrect for azones */
791         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
792                 ar->winrct= *remainder;
793                 
794                 if(alignment==RGN_ALIGN_TOP)
795                         ar->winrct.ymin= ar->winrct.ymax;
796                 else if(alignment==RGN_ALIGN_BOTTOM)
797                         ar->winrct.ymax= ar->winrct.ymin;
798                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
799                         ar->winrct.xmin= ar->winrct.xmax;
800                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
801                         ar->winrct.xmax= ar->winrct.xmin;
802                 else /* prevent winrct to be valid */
803                         ar->winrct.xmax= ar->winrct.xmin;
804         }
805         /* in end, add azones, where appropriate */
806         region_azone_add(sa, ar, alignment);
807
808
809         region_rect_recursive(sa, ar->next, remainder, quad);
810 }
811
812 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
813 {
814         short rt= CLAMPIS(G.rt, 0, 16);
815
816         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
817         else sa->totrct.xmin= sa->v1->vec.x;
818         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
819         else sa->totrct.xmax= sa->v4->vec.x;
820         
821         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
822         else sa->totrct.ymin= sa->v1->vec.y;
823         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
824         else sa->totrct.ymax= sa->v2->vec.y;
825         
826         /* for speedup */
827         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
828         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
829 }
830
831
832 /* used for area initialize below */
833 static void region_subwindow(wmWindow *win, ARegion *ar)
834 {
835         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
836                 if(ar->swinid)
837                         wm_subwindow_close(win, ar->swinid);
838                 ar->swinid= 0;
839         }
840         else if(ar->swinid==0)
841                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
842         else 
843                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
844 }
845
846 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
847 {
848         /* note, add-handler checks if it already exists */
849         
850         // XXX it would be good to have boundbox checks for some of these...
851         if(flag & ED_KEYMAP_UI) {
852                 /* user interface widgets */
853                 UI_add_region_handlers(handlers);
854         }
855         if(flag & ED_KEYMAP_VIEW2D) {
856                 /* 2d-viewport handling+manipulation */
857                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
858                 WM_event_add_keymap_handler(handlers, keymap);
859         }
860         if(flag & ED_KEYMAP_MARKERS) {
861                 /* time-markers */
862                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
863                 WM_event_add_keymap_handler(handlers, keymap);
864                 // XXX need boundbox check urgently!!!
865         }
866         if(flag & ED_KEYMAP_ANIMATION) {
867                 /* frame changing and timeline operators (for time spaces) */
868                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
869                 WM_event_add_keymap_handler(handlers, keymap);
870         }
871         if(flag & ED_KEYMAP_FRAMES) {
872                 /* frame changing/jumping (for all spaces) */
873                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
874                 WM_event_add_keymap_handler(handlers, keymap);
875         }
876         if(flag & ED_KEYMAP_GPENCIL) {
877                 /* grease pencil */
878                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
879                 WM_event_add_keymap_handler(handlers, keymap);
880         }
881         if(flag & ED_KEYMAP_HEADER) {
882                 /* standard keymap for headers regions */
883                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
884                 WM_event_add_keymap_handler(handlers, keymap);
885         }
886 }
887
888
889 /* called in screen_refresh, or screens_init, also area size changes */
890 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
891 {
892         ARegion *ar;
893         rcti rect;
894         
895         /* set typedefinitions */
896         sa->type= BKE_spacetype_from_id(sa->spacetype);
897         
898         if(sa->type==NULL) {
899                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
900                 sa->type= BKE_spacetype_from_id(sa->spacetype);
901         }
902         
903         for(ar= sa->regionbase.first; ar; ar= ar->next)
904                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
905         
906         /* area sizes */
907         area_calc_totrct(sa, win->sizex, win->sizey);
908         
909         /* clear all azones, add the area triange widgets */
910         area_azone_initialize(sa);
911
912         /* region rect sizes */
913         rect= sa->totrct;
914         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
915         
916         /* default area handlers */
917         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
918         /* checks spacedata, adds own handlers */
919         if(sa->type->init)
920                 sa->type->init(wm, sa);
921         
922         /* region windows, default and own handlers */
923         for(ar= sa->regionbase.first; ar; ar= ar->next) {
924                 region_subwindow(win, ar);
925                 
926                 if(ar->swinid) {
927                         /* default region handlers */
928                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
929                         /* own handlers */
930                         if(ar->type->init)
931                                 ar->type->init(wm, ar);
932                 }
933                 else {
934                         /* prevent uiblocks to run */
935                         uiFreeBlocks(NULL, &ar->uiblocks);      
936                 }
937                 
938         }
939 }
940
941 /* externally called for floating regions like menus */
942 void ED_region_init(bContext *C, ARegion *ar)
943 {
944 //      ARegionType *at= ar->type;
945         
946         /* refresh can be called before window opened */
947         region_subwindow(CTX_wm_window(C), ar);
948         
949         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
950         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
951         
952         /* UI convention */
953         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
954         glLoadIdentity();
955 }
956
957 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
958 {
959         ScrArea *sa= CTX_wm_area(C);
960
961         ar->flag ^= RGN_FLAG_HIDDEN;
962         ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
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