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