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