UI DPI scaling:
[blender.git] / source / blender / editors / screen / area.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/screen/area.c
28  *  \ingroup edscr
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_userdef_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_rand.h"
42 #include "BLI_utildefines.h"
43
44 #include "BLF_translation.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, 30);
88         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
89         
90         /* bottom  */
91         glColor4ub(0, 0, 0, 30);
92         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
93         
94         /* top  */
95         glColor4ub(255, 255, 255, 30);
96         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
97
98         /* left  */
99         glColor4ub(255, 255, 255, 30);
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  = BLI_rcti_size_x(&ar->winrct) + 1;
108         int height = BLI_rcti_size_y(&ar->winrct) + 1;
109         
110         wmOrtho2(-GLA_PIXEL_OFS, (float)width - GLA_PIXEL_OFS, -GLA_PIXEL_OFS, (float)height - GLA_PIXEL_OFS);
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 = FALSE;
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_rcti_isect_pt(&ar->winrct, xs, ys)) {
165                         az->do_draw = TRUE;
166                 }
167         }
168 }
169
170 /**
171  * \brief Corner widgets use for dragging and splitting the view.
172  */
173 static void area_draw_azone(short x1, short y1, short x2, short y2)
174 {
175         int dx = x2 - x1;
176         int dy = y2 - y1;
177
178         dx = copysign(ceilf(0.3f * fabsf(dx)), dx);
179         dy = copysign(ceilf(0.3f * fabsf(dy)), dy);
180
181         glEnable(GL_BLEND);
182         glEnable(GL_LINE_SMOOTH);
183         
184         glColor4ub(255, 255, 255, 180);
185         fdrawline(x1, y2, x2, y1);
186         glColor4ub(255, 255, 255, 130);
187         fdrawline(x1, y2 - dy, x2 - dx, y1);
188         glColor4ub(255, 255, 255, 80);
189         fdrawline(x1, y2 - 2 * dy, x2 - 2 * dx, y1);
190         
191         glColor4ub(0, 0, 0, 210);
192         fdrawline(x1, y2 + 1, x2 + 1, y1);
193         glColor4ub(0, 0, 0, 180);
194         fdrawline(x1, y2 - dy + 1, x2 - dx + 1, y1);
195         glColor4ub(0, 0, 0, 150);
196         fdrawline(x1, y2 - 2 * dy + 1, x2 - 2 * dx + 1, y1);
197
198         glDisable(GL_LINE_SMOOTH);
199         glDisable(GL_BLEND);
200 }
201
202 static void region_draw_azone_icon(AZone *az)
203 {
204         GLUquadricObj *qobj = NULL; 
205         short midx = az->x1 + (az->x2 - az->x1) / 2;
206         short midy = az->y1 + (az->y2 - az->y1) / 2;
207                 
208         qobj = gluNewQuadric();
209         
210         glPushMatrix();
211         glTranslatef(midx, midy, 0.0);
212         
213         /* outlined circle */
214         glEnable(GL_LINE_SMOOTH);
215
216         glColor4f(1.f, 1.f, 1.f, 0.8f);
217
218         gluQuadricDrawStyle(qobj, GLU_FILL); 
219         gluDisk(qobj, 0.0,  4.25f, 16, 1);
220
221         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
222         
223         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
224         gluDisk(qobj, 0.0,  4.25f, 16, 1);
225         
226         glDisable(GL_LINE_SMOOTH);
227         
228         glPopMatrix();
229         gluDeleteQuadric(qobj);
230         
231         /* + */
232         sdrawline(midx, midy - 2, midx, midy + 3);
233         sdrawline(midx - 2, midy, midx + 3, midy);
234 }
235
236 static void draw_azone_plus(float x1, float y1, float x2, float y2)
237 {
238         float width = 0.1f * U.widget_unit;
239         float pad = 0.2f * U.widget_unit;
240         
241         glRectf((x1 + x2 - width) * 0.5f, y1 + pad, (x1 + x2 + width) * 0.5f, y2 - pad);
242         glRectf(x1 + pad, (y1 + y2 - width) * 0.5f, (x1 + x2 - width) * 0.5f, (y1 + y2 + width) * 0.5f);
243         glRectf((x1 + x2 + width) * 0.5f, (y1 + y2 - width) * 0.5f, x2 - pad, (y1 + y2 + width) * 0.5f);
244 }
245
246 static void region_draw_azone_tab_plus(AZone *az)
247 {
248         extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
249         
250         glEnable(GL_BLEND);
251         
252         /* add code to draw region hidden as 'too small' */
253         switch (az->edge) {
254                 case AE_TOP_TO_BOTTOMRIGHT:
255                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
256                         break;
257                 case AE_BOTTOM_TO_TOPLEFT:
258                         uiSetRoundBox(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT);
259                         break;
260                 case AE_LEFT_TO_TOPRIGHT:
261                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT);
262                         break;
263                 case AE_RIGHT_TO_TOPLEFT:
264                         uiSetRoundBox(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT);
265                         break;
266         }
267
268         glColor4f(0.05f, 0.05f, 0.05f, 0.4f);
269         uiRoundBox((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
270
271         glEnable(GL_BLEND);
272
273         glColor4f(0.8f, 0.8f, 0.8f, 0.4f);
274         draw_azone_plus((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2);
275
276         glDisable(GL_BLEND);
277 }
278
279 static void region_draw_azone_tab(AZone *az)
280 {
281         float col[3];
282         
283         glEnable(GL_BLEND);
284         UI_GetThemeColor3fv(TH_HEADER, col);
285         glColor4f(col[0], col[1], col[2], 0.5f);
286         
287         /* add code to draw region hidden as 'too small' */
288         switch (az->edge) {
289                 case AE_TOP_TO_BOTTOMRIGHT:
290                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT | UI_RB_ALPHA);
291                         
292                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
293                         glColor4ub(0, 0, 0, 255);
294                         uiRoundRect((float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f);
295                         break;
296                 case AE_BOTTOM_TO_TOPLEFT:
297                         uiSetRoundBox(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT | UI_RB_ALPHA);
298                         
299                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
300                         glColor4ub(0, 0, 0, 255);
301                         uiRoundRect((float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f);
302                         break;
303                 case AE_LEFT_TO_TOPRIGHT:
304                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT | UI_RB_ALPHA);
305                         
306                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
307                         glColor4ub(0, 0, 0, 255);
308                         uiRoundRect((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
309                         break;
310                 case AE_RIGHT_TO_TOPLEFT:
311                         uiSetRoundBox(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT | UI_RB_ALPHA);
312                         
313                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
314                         glColor4ub(0, 0, 0, 255);
315                         uiRoundRect((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
316                         break;
317         }
318         
319         glDisable(GL_BLEND);
320 }
321
322 static void region_draw_azone_tria(AZone *az)
323 {
324         extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
325         
326         glEnable(GL_BLEND);
327         //UI_GetThemeColor3fv(TH_HEADER, col);
328         glColor4f(0.0f, 0.0f, 0.0f, 0.35f);
329         
330         /* add code to draw region hidden as 'too small' */
331         switch (az->edge) {
332                 case AE_TOP_TO_BOTTOMRIGHT:
333                         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);
334                         break;
335                         
336                 case AE_BOTTOM_TO_TOPLEFT:
337                         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);
338                         break;
339
340                 case AE_LEFT_TO_TOPRIGHT:
341                         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);
342                         break;
343                         
344                 case AE_RIGHT_TO_TOPLEFT:
345                         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);
346                         break;
347                         
348         }
349         
350         glDisable(GL_BLEND);
351 }
352
353 /* only exported for WM */
354 void ED_area_overdraw(bContext *C)
355 {
356         wmWindow *win = CTX_wm_window(C);
357         bScreen *screen = CTX_wm_screen(C);
358         ScrArea *sa;
359         
360         /* Draw AZones, in screenspace */
361         wmSubWindowSet(win, screen->mainwin);
362
363         glEnable(GL_BLEND);
364         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
365         
366         for (sa = screen->areabase.first; sa; sa = sa->next) {
367                 AZone *az;
368                 for (az = sa->actionzones.first; az; az = az->next) {
369                         if (az->do_draw) {
370                                 if (az->type == AZONE_AREA) {
371                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
372                                 }
373                                 else if (az->type == AZONE_REGION) {
374                                         
375                                         if (az->ar) {
376                                                 /* only display tab or icons when the region is hidden */
377                                                 if (az->ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
378                                                         if (G.debug_value == 3)
379                                                                 region_draw_azone_icon(az);
380                                                         else if (G.debug_value == 2)
381                                                                 region_draw_azone_tria(az);
382                                                         else if (G.debug_value == 1)
383                                                                 region_draw_azone_tab(az);
384                                                         else
385                                                                 region_draw_azone_tab_plus(az);
386                                                 }
387                                         }
388                                 }
389                                 
390                                 az->do_draw = FALSE;
391                         }
392                 }
393         }
394         glDisable(GL_BLEND);
395         
396 }
397
398 /* only exported for WM */
399 /* makes region ready for drawing, sets pixelspace */
400 void ED_region_set(const bContext *C, ARegion *ar)
401 {
402         wmWindow *win = CTX_wm_window(C);
403         ScrArea *sa = CTX_wm_area(C);
404         
405         ar->drawrct = ar->winrct;
406         
407         /* note; this sets state, so we can use wmOrtho and friends */
408         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
409         
410         UI_SetTheme(sa ? sa->spacetype : 0, ar->type ? ar->type->regionid : 0);
411         
412         ED_region_pixelspace(ar);
413 }
414
415
416 /* only exported for WM */
417 void ED_region_do_draw(bContext *C, ARegion *ar)
418 {
419         wmWindow *win = CTX_wm_window(C);
420         ScrArea *sa = CTX_wm_area(C);
421         ARegionType *at = ar->type;
422         
423         /* see BKE_spacedata_draw_locks() */
424         if (at->do_lock)
425                 return;
426         
427         /* if no partial draw rect set, full rect */
428         if (ar->drawrct.xmin == ar->drawrct.xmax)
429                 ar->drawrct = ar->winrct;
430         else {
431                 /* extra clip for safety (intersect the rects, could use API func) */
432                 ar->drawrct.xmin = max_ii(ar->winrct.xmin, ar->drawrct.xmin);
433                 ar->drawrct.ymin = max_ii(ar->winrct.ymin, ar->drawrct.ymin);
434                 ar->drawrct.xmax = min_ii(ar->winrct.xmax, ar->drawrct.xmax);
435                 ar->drawrct.ymax = min_ii(ar->winrct.ymax, ar->drawrct.ymax);
436         }
437         
438         /* note; this sets state, so we can use wmOrtho and friends */
439         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
440         
441         UI_SetTheme(sa ? sa->spacetype : 0, ar->type ? ar->type->regionid : 0);
442         
443         /* optional header info instead? */
444         if (ar->headerstr) {
445                 UI_ThemeClearColor(TH_HEADER);
446                 glClear(GL_COLOR_BUFFER_BIT);
447                 
448                 UI_ThemeColor(TH_TEXT);
449                 BLF_draw_default(20, 8, 0.0f, ar->headerstr, BLF_DRAW_STR_DUMMY_MAX);
450         }
451         else if (at->draw) {
452                 at->draw(C, ar);
453         }
454
455         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
456         ED_region_pixelspace(ar);
457
458         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
459
460         ar->do_draw = FALSE;
461         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
462         
463         uiFreeInactiveBlocks(C, &ar->uiblocks);
464
465         if (sa)
466                 region_draw_emboss(ar, &ar->winrct);
467 }
468
469 /* **********************************
470  * maybe silly, but let's try for now
471  * to keep these tags protected
472  * ********************************** */
473
474 void ED_region_tag_redraw(ARegion *ar)
475 {
476         if (ar) {
477                 /* zero region means full region redraw */
478                 ar->do_draw = RGN_DRAW;
479                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
480         }
481 }
482
483 void ED_region_tag_redraw_overlay(ARegion *ar)
484 {
485         if (ar)
486                 ar->do_draw_overlay = RGN_DRAW;
487 }
488
489 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
490 {
491         if (ar) {
492                 if (!ar->do_draw) {
493                         /* no redraw set yet, set partial region */
494                         ar->do_draw = RGN_DRAW_PARTIAL;
495                         ar->drawrct = *rct;
496                 }
497                 else if (ar->drawrct.xmin != ar->drawrct.xmax) {
498                         /* partial redraw already set, expand region */
499                         BLI_rcti_union(&ar->drawrct, rct);
500                 }
501         }
502 }
503
504 void ED_area_tag_redraw(ScrArea *sa)
505 {
506         ARegion *ar;
507         
508         if (sa)
509                 for (ar = sa->regionbase.first; ar; ar = ar->next)
510                         ED_region_tag_redraw(ar);
511 }
512
513 void ED_area_tag_redraw_regiontype(ScrArea *sa, int regiontype)
514 {
515         ARegion *ar;
516         
517         if (sa) {
518                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
519                         if (ar->regiontype == regiontype) {
520                                 ED_region_tag_redraw(ar);
521                         }
522                 }
523         }
524 }
525
526 void ED_area_tag_refresh(ScrArea *sa)
527 {
528         if (sa)
529                 sa->do_refresh = TRUE;
530 }
531
532 /* *************************************************************** */
533
534 /* use NULL to disable it */
535 void ED_area_headerprint(ScrArea *sa, const char *str)
536 {
537         ARegion *ar;
538
539         /* happens when running transform operators in backround mode */
540         if (sa == NULL)
541                 return;
542
543         for (ar = sa->regionbase.first; ar; ar = ar->next) {
544                 if (ar->regiontype == RGN_TYPE_HEADER) {
545                         if (str) {
546                                 if (ar->headerstr == NULL)
547                                         ar->headerstr = MEM_mallocN(256, "headerprint");
548                                 BLI_strncpy(ar->headerstr, str, 256);
549                         }
550                         else if (ar->headerstr) {
551                                 MEM_freeN(ar->headerstr);
552                                 ar->headerstr = NULL;
553                         }
554                         ED_region_tag_redraw(ar);
555                 }
556         }
557 }
558
559 /* ************************************************************ */
560
561
562 static void area_azone_initialize(bScreen *screen, ScrArea *sa)
563 {
564         AZone *az;
565         
566         /* reinitalize entirely, regions add azones too */
567         BLI_freelistN(&sa->actionzones);
568
569         if (screen->full != SCREENNORMAL) {
570                 return;
571         }
572
573         /* set area action zones */
574         az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
575         BLI_addtail(&(sa->actionzones), az);
576         az->type = AZONE_AREA;
577         az->x1 = sa->totrct.xmin - 1;
578         az->y1 = sa->totrct.ymin - 1;
579         az->x2 = sa->totrct.xmin + (AZONESPOT - 1);
580         az->y2 = sa->totrct.ymin + (AZONESPOT - 1);
581         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
582         
583         az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
584         BLI_addtail(&(sa->actionzones), az);
585         az->type = AZONE_AREA;
586         az->x1 = sa->totrct.xmax + 1;
587         az->y1 = sa->totrct.ymax + 1;
588         az->x2 = sa->totrct.xmax - (AZONESPOT - 1);
589         az->y2 = sa->totrct.ymax - (AZONESPOT - 1);
590         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
591 }
592
593 #define AZONEPAD_EDGE   (0.2f * U.widget_unit)
594 #define AZONEPAD_ICON   (0.45f * U.widget_unit)
595 static void region_azone_edge(AZone *az, ARegion *ar)
596 {
597         switch (az->edge) {
598                 case AE_TOP_TO_BOTTOMRIGHT:
599                         az->x1 = ar->winrct.xmin;
600                         az->y1 = ar->winrct.ymax - AZONEPAD_EDGE;
601                         az->x2 = ar->winrct.xmax;
602                         az->y2 = ar->winrct.ymax;
603                         break;
604                 case AE_BOTTOM_TO_TOPLEFT:
605                         az->x1 = ar->winrct.xmin;
606                         az->y1 = ar->winrct.ymin + AZONEPAD_EDGE;
607                         az->x2 = ar->winrct.xmax;
608                         az->y2 = ar->winrct.ymin;
609                         break;
610                 case AE_LEFT_TO_TOPRIGHT:
611                         az->x1 = ar->winrct.xmin;
612                         az->y1 = ar->winrct.ymin;
613                         az->x2 = ar->winrct.xmin + AZONEPAD_EDGE;
614                         az->y2 = ar->winrct.ymax;
615                         break;
616                 case AE_RIGHT_TO_TOPLEFT:
617                         az->x1 = ar->winrct.xmax;
618                         az->y1 = ar->winrct.ymin;
619                         az->x2 = ar->winrct.xmax - AZONEPAD_EDGE;
620                         az->y2 = ar->winrct.ymax;
621                         break;
622         }
623
624         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
625 }
626
627 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
628 {
629         AZone *azt;
630         int tot = 0;
631         
632         /* count how many actionzones with along same edge are available.
633          * This allows for adding more action zones in the future without
634          * having to worry about correct offset */
635         for (azt = sa->actionzones.first; azt; azt = azt->next) {
636                 if (azt->edge == az->edge) tot++;
637         }
638         
639         switch (az->edge) {
640                 case AE_TOP_TO_BOTTOMRIGHT:
641                         az->x1 = ar->winrct.xmax - tot * 2 * AZONEPAD_ICON;
642                         az->y1 = ar->winrct.ymax + AZONEPAD_ICON;
643                         az->x2 = ar->winrct.xmax - tot * AZONEPAD_ICON;
644                         az->y2 = ar->winrct.ymax + 2 * AZONEPAD_ICON;
645                         break;
646                 case AE_BOTTOM_TO_TOPLEFT:
647                         az->x1 = ar->winrct.xmin + AZONEPAD_ICON;
648                         az->y1 = ar->winrct.ymin - 2 * AZONEPAD_ICON;
649                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_ICON;
650                         az->y2 = ar->winrct.ymin - AZONEPAD_ICON;
651                         break;
652                 case AE_LEFT_TO_TOPRIGHT:
653                         az->x1 = ar->winrct.xmin - 2 * AZONEPAD_ICON;
654                         az->y1 = ar->winrct.ymax - tot * 2 * AZONEPAD_ICON;
655                         az->x2 = ar->winrct.xmin - AZONEPAD_ICON;
656                         az->y2 = ar->winrct.ymax - tot * AZONEPAD_ICON;
657                         break;
658                 case AE_RIGHT_TO_TOPLEFT:
659                         az->x1 = ar->winrct.xmax + AZONEPAD_ICON;
660                         az->y1 = ar->winrct.ymax - tot * 2 * AZONEPAD_ICON;
661                         az->x2 = ar->winrct.xmax + 2 * AZONEPAD_ICON;
662                         az->y2 = ar->winrct.ymax - tot * AZONEPAD_ICON;
663                         break;
664         }
665
666         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
667         
668         /* if more azones on 1 spot, set offset */
669         for (azt = sa->actionzones.first; azt; azt = azt->next) {
670                 if (az != azt) {
671                         if (ABS(az->x1 - azt->x1) < 2 && ABS(az->y1 - azt->y1) < 2) {
672                                 if (az->edge == AE_TOP_TO_BOTTOMRIGHT || az->edge == AE_BOTTOM_TO_TOPLEFT) {
673                                         az->x1 += AZONESPOT;
674                                         az->x2 += AZONESPOT;
675                                 }
676                                 else {
677                                         az->y1 -= AZONESPOT;
678                                         az->y2 -= AZONESPOT;
679                                 }
680                                 BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
681                         }
682                 }
683         }
684 }
685
686 #define AZONEPAD_TAB_PLUSW  (0.7f * U.widget_unit)
687 #define AZONEPAD_TAB_PLUSH  (0.7f * U.widget_unit)
688
689 /* region already made zero sized, in shape of edge */
690 static void region_azone_tab_plus(ScrArea *sa, AZone *az, ARegion *ar)
691 {
692         AZone *azt;
693         int tot = 0, add;
694         
695         for (azt = sa->actionzones.first; azt; azt = azt->next) {
696                 if (azt->edge == az->edge) tot++;
697         }
698         
699         switch (az->edge) {
700                 case AE_TOP_TO_BOTTOMRIGHT:
701                         if (ar->winrct.ymax == sa->totrct.ymin) add = 1; else add = 0;
702                         az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
703                         az->y1 = ar->winrct.ymax - add;
704                         az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
705                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TAB_PLUSH;
706                         break;
707                 case AE_BOTTOM_TO_TOPLEFT:
708                         az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
709                         az->y1 = ar->winrct.ymin - AZONEPAD_TAB_PLUSH;
710                         az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
711                         az->y2 = ar->winrct.ymin;
712                         break;
713                 case AE_LEFT_TO_TOPRIGHT:
714                         az->x1 = ar->winrct.xmin - AZONEPAD_TAB_PLUSH;
715                         az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
716                         az->x2 = ar->winrct.xmin;
717                         az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
718                         break;
719                 case AE_RIGHT_TO_TOPLEFT:
720                         az->x1 = ar->winrct.xmax - 1;
721                         az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
722                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TAB_PLUSH;
723                         az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
724                         break;
725         }
726         /* rect needed for mouse pointer test */
727         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
728 }       
729
730
731 #define AZONEPAD_TABW   (0.9f * U.widget_unit)
732 #define AZONEPAD_TABH   (0.35f * U.widget_unit)
733
734 /* region already made zero sized, in shape of edge */
735 static void region_azone_tab(ScrArea *sa, AZone *az, ARegion *ar)
736 {
737         AZone *azt;
738         int tot = 0, add;
739         
740         for (azt = sa->actionzones.first; azt; azt = azt->next) {
741                 if (azt->edge == az->edge) tot++;
742         }
743         
744         switch (az->edge) {
745                 case AE_TOP_TO_BOTTOMRIGHT:
746                         if (ar->winrct.ymax == sa->totrct.ymin) add = 1; else add = 0;
747                         az->x1 = ar->winrct.xmax - 2 * AZONEPAD_TABW;
748                         az->y1 = ar->winrct.ymax - add;
749                         az->x2 = ar->winrct.xmax - AZONEPAD_TABW;
750                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TABH;
751                         break;
752                 case AE_BOTTOM_TO_TOPLEFT:
753                         az->x1 = ar->winrct.xmin + AZONEPAD_TABW;
754                         az->y1 = ar->winrct.ymin - AZONEPAD_TABH;
755                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_TABW;
756                         az->y2 = ar->winrct.ymin;
757                         break;
758                 case AE_LEFT_TO_TOPRIGHT:
759                         az->x1 = ar->winrct.xmin + 1 - AZONEPAD_TABH;
760                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TABW;
761                         az->x2 = ar->winrct.xmin + 1;
762                         az->y2 = ar->winrct.ymax - AZONEPAD_TABW;
763                         break;
764                 case AE_RIGHT_TO_TOPLEFT:
765                         az->x1 = ar->winrct.xmax - 1;
766                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TABW;
767                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TABH;
768                         az->y2 = ar->winrct.ymax - AZONEPAD_TABW;
769                         break;
770         }
771         /* rect needed for mouse pointer test */
772         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
773 }       
774
775 #define AZONEPAD_TRIAW  (0.8f * U.widget_unit)
776 #define AZONEPAD_TRIAH  (0.45f * U.widget_unit)
777
778
779 /* region already made zero sized, in shape of edge */
780 static void region_azone_tria(ScrArea *sa, AZone *az, ARegion *ar)
781 {
782         AZone *azt;
783         int tot = 0, add;
784         
785         for (azt = sa->actionzones.first; azt; azt = azt->next) {
786                 if (azt->edge == az->edge) tot++;
787         }
788         
789         switch (az->edge) {
790                 case AE_TOP_TO_BOTTOMRIGHT:
791                         if (ar->winrct.ymax == sa->totrct.ymin) add = 1; else add = 0;
792                         az->x1 = ar->winrct.xmax - 2 * AZONEPAD_TRIAW;
793                         az->y1 = ar->winrct.ymax - add;
794                         az->x2 = ar->winrct.xmax - AZONEPAD_TRIAW;
795                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TRIAH;
796                         break;
797                 case AE_BOTTOM_TO_TOPLEFT:
798                         az->x1 = ar->winrct.xmin + AZONEPAD_TRIAW;
799                         az->y1 = ar->winrct.ymin - AZONEPAD_TRIAH;
800                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_TRIAW;
801                         az->y2 = ar->winrct.ymin;
802                         break;
803                 case AE_LEFT_TO_TOPRIGHT:
804                         az->x1 = ar->winrct.xmin + 1 - AZONEPAD_TRIAH;
805                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TRIAW;
806                         az->x2 = ar->winrct.xmin + 1;
807                         az->y2 = ar->winrct.ymax - AZONEPAD_TRIAW;
808                         break;
809                 case AE_RIGHT_TO_TOPLEFT:
810                         az->x1 = ar->winrct.xmax - 1;
811                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TRIAW;
812                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TRIAH;
813                         az->y2 = ar->winrct.ymax - AZONEPAD_TRIAW;
814                         break;
815         }
816         /* rect needed for mouse pointer test */
817         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
818 }       
819
820
821 static void region_azone_initialize(ScrArea *sa, ARegion *ar, AZEdge edge) 
822 {
823         AZone *az;
824         
825         az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
826         BLI_addtail(&(sa->actionzones), az);
827         az->type = AZONE_REGION;
828         az->ar = ar;
829         az->edge = edge;
830         
831         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
832                 if (G.debug_value == 3)
833                         region_azone_icon(sa, az, ar);
834                 else if (G.debug_value == 2)
835                         region_azone_tria(sa, az, ar);
836                 else if (G.debug_value == 1)
837                         region_azone_tab(sa, az, ar);
838                 else
839                         region_azone_tab_plus(sa, az, ar);
840         }
841         else {
842                 region_azone_edge(az, ar);
843         }
844         
845 }
846
847
848 /* *************************************************************** */
849
850 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
851 {
852         /* edge code (t b l r) is along which area edge azone will be drawn */
853         
854         if (alignment == RGN_ALIGN_TOP)
855                 region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT);
856         else if (alignment == RGN_ALIGN_BOTTOM)
857                 region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT);
858         else if (alignment == RGN_ALIGN_RIGHT)
859                 region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT);
860         else if (alignment == RGN_ALIGN_LEFT)
861                 region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT);
862 }
863
864 /* dir is direction to check, not the splitting edge direction! */
865 static int rct_fits(rcti *rect, char dir, int size)
866 {
867         if (dir == 'h') {
868                 return BLI_rcti_size_x(rect) - size;
869         }
870         else {  /* 'v' */
871                 return BLI_rcti_size_y(rect) - size;
872         }
873 }
874
875 /* *************************************************************** */
876
877 /* ar should be overlapping */
878 /* function checks if some overlapping region was defined before - on same place */
879 static void region_overlap_fix(ARegion *ar)
880 {
881         ARegion *ar1 = ar->prev;
882         
883         /* find overlapping previous region on same place */
884         while (ar1) {
885                 if (ar1->overlap) {
886                         if ((ar1->alignment & RGN_SPLIT_PREV) == 0)
887                                 if (BLI_rcti_isect(&ar1->winrct, &ar->winrct, NULL))
888                                         break;
889                 }
890                 ar1 = ar1->prev;
891         }
892         
893         /* translate */
894         if (ar1) {
895                 int align1 = ar1->alignment & ~RGN_SPLIT_PREV;
896
897                 if (align1 == RGN_ALIGN_LEFT) {
898                         BLI_rcti_translate(&ar->winrct, ar1->winx, 0);
899                 }
900                 else if (align1 == RGN_ALIGN_RIGHT) {
901                         BLI_rcti_translate(&ar->winrct, -ar1->winx, 0);
902                 }
903         }
904
905 }
906
907 /* overlapping regions only in the following restricted cases */
908 static int region_is_overlap(wmWindow *win, ScrArea *sa, ARegion *ar)
909 {
910         if (U.uiflag2 & USER_REGION_OVERLAP)
911                 if (WM_is_draw_triple(win))
912                         if (ELEM5(sa->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_SEQ, SPACE_CLIP, SPACE_NODE))
913                                 if (ELEM3(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS))
914                                         return 1;
915         return 0;
916 }
917
918 static void region_rect_recursive(wmWindow *win, ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
919 {
920         rcti *remainder_prev = remainder;
921         int prefsizex, prefsizey;
922         int alignment;
923         
924         if (ar == NULL)
925                 return;
926         
927         /* no returns in function, winrct gets set in the end again */
928         BLI_rcti_init(&ar->winrct, 0, 0, 0, 0);
929         
930         /* for test; allow split of previously defined region */
931         if (ar->alignment & RGN_SPLIT_PREV)
932                 if (ar->prev)
933                         remainder = &ar->prev->winrct;
934         
935         alignment = ar->alignment & ~RGN_SPLIT_PREV;
936         
937         /* set here, assuming userpref switching forces to call this again */
938         ar->overlap = region_is_overlap(win, sa, ar);
939
940         /* clear state flags first */
941         ar->flag &= ~RGN_FLAG_TOO_SMALL;
942         /* user errors */
943         if (ar->next == NULL && alignment != RGN_ALIGN_QSPLIT)
944                 alignment = RGN_ALIGN_NONE;
945         
946         /* prefsize, for header we stick to exception (prevent dpi rounding error) */
947         prefsizex = UI_DPI_FAC * (ar->sizex > 1 ? ar->sizex + 0.5f : ar->type->prefsizex);
948         
949         if (ar->regiontype == RGN_TYPE_HEADER) {
950                 prefsizey = ED_area_headersize();
951         }
952         else if (ar->regiontype == RGN_TYPE_UI && sa->spacetype == SPACE_FILE) {
953                 prefsizey = UI_UNIT_Y * 2 + (UI_UNIT_Y / 2);
954         }
955         else {
956                 prefsizey = UI_DPI_FAC * (ar->sizey > 1 ? ar->sizey + 0.5f : ar->type->prefsizey);
957         }
958
959
960         if (ar->flag & RGN_FLAG_HIDDEN) {
961                 /* hidden is user flag */
962         }
963         else if (alignment == RGN_ALIGN_FLOAT) {
964                 /* XXX floating area region, not handled yet here */
965         }
966         else if (rct_fits(remainder, 'v', 1) < 0 || rct_fits(remainder, 'h', 1) < 0) {
967                 /* remainder is too small for any usage */
968                 ar->flag |= RGN_FLAG_TOO_SMALL;
969         }
970         else if (alignment == RGN_ALIGN_NONE) {
971                 /* typically last region */
972                 ar->winrct = *remainder;
973                 BLI_rcti_init(remainder, 0, 0, 0, 0);
974         }
975         else if (alignment == RGN_ALIGN_TOP || alignment == RGN_ALIGN_BOTTOM) {
976                 
977                 if (rct_fits(remainder, 'v', prefsizey) < 0) {
978                         ar->flag |= RGN_FLAG_TOO_SMALL;
979                 }
980                 else {
981                         int fac = rct_fits(remainder, 'v', prefsizey);
982
983                         if (fac < 0)
984                                 prefsizey += fac;
985                         
986                         ar->winrct = *remainder;
987                         
988                         if (alignment == RGN_ALIGN_TOP) {
989                                 ar->winrct.ymin = ar->winrct.ymax - prefsizey + 1;
990                                 remainder->ymax = ar->winrct.ymin - 1;
991                         }
992                         else {
993                                 ar->winrct.ymax = ar->winrct.ymin + prefsizey - 1;
994                                 remainder->ymin = ar->winrct.ymax + 1;
995                         }
996                 }
997         }
998         else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
999                 
1000                 if (rct_fits(remainder, 'h', prefsizex) < 0) {
1001                         ar->flag |= RGN_FLAG_TOO_SMALL;
1002                 }
1003                 else {
1004                         int fac = rct_fits(remainder, 'h', prefsizex);
1005                         
1006                         if (fac < 0)
1007                                 prefsizex += fac;
1008                         
1009                         ar->winrct = *remainder;
1010                         
1011                         if (alignment == RGN_ALIGN_RIGHT) {
1012                                 ar->winrct.xmin = ar->winrct.xmax - prefsizex + 1;
1013                                 if (ar->overlap == 0)
1014                                         remainder->xmax = ar->winrct.xmin - 1;
1015                         }
1016                         else {
1017                                 ar->winrct.xmax = ar->winrct.xmin + prefsizex - 1;
1018                                 if (ar->overlap == 0)
1019                                         remainder->xmin = ar->winrct.xmax + 1;
1020                         }
1021                 }
1022         }
1023         else if (alignment == RGN_ALIGN_VSPLIT || alignment == RGN_ALIGN_HSPLIT) {
1024                 /* percentage subdiv*/
1025                 ar->winrct = *remainder;
1026                 
1027                 if (alignment == RGN_ALIGN_HSPLIT) {
1028                         if (rct_fits(remainder, 'h', prefsizex) > 4) {
1029                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1030                                 remainder->xmin = ar->winrct.xmax + 1;
1031                         }
1032                         else {
1033                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1034                         }
1035                 }
1036                 else {
1037                         if (rct_fits(remainder, 'v', prefsizey) > 4) {
1038                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1039                                 remainder->ymin = ar->winrct.ymax + 1;
1040                         }
1041                         else {
1042                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1043                         }
1044                 }
1045         }
1046         else if (alignment == RGN_ALIGN_QSPLIT) {
1047                 ar->winrct = *remainder;
1048                 
1049                 /* test if there's still 4 regions left */
1050                 if (quad == 0) {
1051                         ARegion *artest = ar->next;
1052                         int count = 1;
1053                         
1054                         while (artest) {
1055                                 artest->alignment = RGN_ALIGN_QSPLIT;
1056                                 artest = artest->next;
1057                                 count++;
1058                         }
1059                         
1060                         if (count != 4) {
1061                                 /* let's stop adding regions */
1062                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1063                                 if (G.debug & G_DEBUG)
1064                                         printf("region quadsplit failed\n");
1065                         }
1066                         else quad = 1;
1067                 }
1068                 if (quad) {
1069                         if (quad == 1) { /* left bottom */
1070                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1071                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1072                         }
1073                         else if (quad == 2) { /* left top */
1074                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1075                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1076                         }
1077                         else if (quad == 3) { /* right bottom */
1078                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1079                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1080                         }
1081                         else {  /* right top */
1082                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1083                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1084                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1085                         }
1086
1087                         quad++;
1088                 }
1089         }
1090         
1091         /* for speedup */
1092         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1093         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1094         
1095         /* if region opened normally, we store this for hide/reveal usage */
1096         /* prevent rounding errors for UI_DPI_FAC mult and divide */
1097         if (ar->winx > 1) ar->sizex = (ar->winx + 0.5f) /  UI_DPI_FAC;
1098         if (ar->winy > 1) ar->sizey = (ar->winy + 0.5f) /  UI_DPI_FAC;
1099                 
1100         /* exception for multiple aligned overlapping regions on same spot */
1101         if (ar->overlap)
1102                 region_overlap_fix(ar);
1103
1104         /* set winrect for azones */
1105         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
1106                 ar->winrct = *remainder;
1107                 
1108                 if (alignment == RGN_ALIGN_TOP)
1109                         ar->winrct.ymin = ar->winrct.ymax;
1110                 else if (alignment == RGN_ALIGN_BOTTOM)
1111                         ar->winrct.ymax = ar->winrct.ymin;
1112                 else if (alignment == RGN_ALIGN_RIGHT)
1113                         ar->winrct.xmin = ar->winrct.xmax;
1114                 else if (alignment == RGN_ALIGN_LEFT)
1115                         ar->winrct.xmax = ar->winrct.xmin;
1116                 else /* prevent winrct to be valid */
1117                         ar->winrct.xmax = ar->winrct.xmin;
1118         }
1119
1120         /* restore prev-split exception */
1121         if (ar->alignment & RGN_SPLIT_PREV) {
1122                 if (ar->prev) {
1123                         remainder = remainder_prev;
1124                         ar->prev->winx = BLI_rcti_size_x(&ar->prev->winrct) + 1;
1125                         ar->prev->winy = BLI_rcti_size_y(&ar->prev->winrct) + 1;
1126                 }
1127         }
1128         
1129         /* in end, add azones, where appropriate */
1130         if (ar->regiontype == RGN_TYPE_HEADER && ar->winy + 6 > sa->winy) {
1131                 /* The logic for this is: when the header takes up the full area,
1132                  * disallow hiding it to view the main window.
1133                  *
1134                  * Without this, you can drag down the file selectors header and hide it
1135                  * by accident very easily (highly annoying!), the value 6 is arbitrary
1136                  * but accounts for small common rounding problems when scaling the UI,
1137                  * must be minimum '4' */
1138         }
1139         else {
1140                 region_azone_add(sa, ar, alignment);
1141         }
1142
1143         region_rect_recursive(win, sa, ar->next, remainder, quad);
1144 }
1145
1146 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
1147 {
1148         short rt = U.pixelsize > 1.0f ? 1 : 0;
1149
1150         if (sa->v1->vec.x > 0) sa->totrct.xmin = sa->v1->vec.x + 1 + rt;
1151         else sa->totrct.xmin = sa->v1->vec.x;
1152         if (sa->v4->vec.x < sizex - 1) sa->totrct.xmax = sa->v4->vec.x - 1 - rt;
1153         else sa->totrct.xmax = sa->v4->vec.x;
1154         
1155         if (sa->v1->vec.y > 0) sa->totrct.ymin = sa->v1->vec.y + 1 + rt;
1156         else sa->totrct.ymin = sa->v1->vec.y;
1157         if (sa->v2->vec.y < sizey - 1) sa->totrct.ymax = sa->v2->vec.y - 1 - rt;
1158         else sa->totrct.ymax = sa->v2->vec.y;
1159         
1160         /* for speedup */
1161         sa->winx = BLI_rcti_size_x(&sa->totrct) + 1;
1162         sa->winy = BLI_rcti_size_y(&sa->totrct) + 1;
1163 }
1164
1165
1166 /* used for area initialize below */
1167 static void region_subwindow(wmWindow *win, ARegion *ar)
1168 {
1169         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
1170                 if (ar->swinid)
1171                         wm_subwindow_close(win, ar->swinid);
1172                 ar->swinid = 0;
1173         }
1174         else if (ar->swinid == 0)
1175                 ar->swinid = wm_subwindow_open(win, &ar->winrct);
1176         else 
1177                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
1178 }
1179
1180 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
1181 {
1182         /* note, add-handler checks if it already exists */
1183         
1184         /* XXX it would be good to have boundbox checks for some of these... */
1185         if (flag & ED_KEYMAP_UI) {
1186                 /* user interface widgets */
1187                 UI_add_region_handlers(handlers);
1188         }
1189         if (flag & ED_KEYMAP_VIEW2D) {
1190                 /* 2d-viewport handling+manipulation */
1191                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
1192                 WM_event_add_keymap_handler(handlers, keymap);
1193         }
1194         if (flag & ED_KEYMAP_MARKERS) {
1195                 /* time-markers */
1196                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
1197                 
1198                 /* time space only has this keymap, the others get a boundbox restricted map */
1199                 if (sa->spacetype != SPACE_TIME) {
1200                         ARegion *ar;
1201                         static rcti rect = {0, 10000, 0, 30};    /* same local check for all areas */
1202                         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
1203                         if (ar) {
1204                                 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
1205                         }
1206                 }
1207                 else
1208                         WM_event_add_keymap_handler(handlers, keymap);
1209         }
1210         if (flag & ED_KEYMAP_ANIMATION) {
1211                 /* frame changing and timeline operators (for time spaces) */
1212                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
1213                 WM_event_add_keymap_handler(handlers, keymap);
1214         }
1215         if (flag & ED_KEYMAP_FRAMES) {
1216                 /* frame changing/jumping (for all spaces) */
1217                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
1218                 WM_event_add_keymap_handler(handlers, keymap);
1219         }
1220         if (flag & ED_KEYMAP_GPENCIL) {
1221                 /* grease pencil */
1222                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
1223                 WM_event_add_keymap_handler(handlers, keymap);
1224         }
1225         if (flag & ED_KEYMAP_HEADER) {
1226                 /* standard keymap for headers regions */
1227                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Header", 0, 0);
1228                 WM_event_add_keymap_handler(handlers, keymap);
1229         }
1230 }
1231
1232
1233 /* called in screen_refresh, or screens_init, also area size changes */
1234 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
1235 {
1236         ARegion *ar;
1237         rcti rect;
1238         
1239         /* set typedefinitions */
1240         sa->type = BKE_spacetype_from_id(sa->spacetype);
1241         
1242         if (sa->type == NULL) {
1243                 sa->butspacetype = sa->spacetype = SPACE_VIEW3D;
1244                 sa->type = BKE_spacetype_from_id(sa->spacetype);
1245         }
1246         
1247         for (ar = sa->regionbase.first; ar; ar = ar->next)
1248                 ar->type = BKE_regiontype_from_id(sa->type, ar->regiontype);
1249         
1250         /* area sizes */
1251         area_calc_totrct(sa, WM_window_pixels_x(win), WM_window_pixels_y(win));
1252         
1253         /* clear all azones, add the area triange widgets */
1254         area_azone_initialize(win->screen, sa);
1255
1256         /* region rect sizes */
1257         rect = sa->totrct;
1258         region_rect_recursive(win, sa, sa->regionbase.first, &rect, 0);
1259         
1260         /* default area handlers */
1261         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
1262         /* checks spacedata, adds own handlers */
1263         if (sa->type->init)
1264                 sa->type->init(wm, sa);
1265         
1266         /* region windows, default and own handlers */
1267         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1268                 region_subwindow(win, ar);
1269                 
1270                 if (ar->swinid) {
1271                         /* default region handlers */
1272                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
1273                         /* own handlers */
1274                         if (ar->type->init)
1275                                 ar->type->init(wm, ar);
1276                 }
1277                 else {
1278                         /* prevent uiblocks to run */
1279                         uiFreeBlocks(NULL, &ar->uiblocks);
1280                 }
1281         }
1282 }
1283
1284 /* externally called for floating regions like menus */
1285 void ED_region_init(bContext *C, ARegion *ar)
1286 {
1287 //      ARegionType *at = ar->type;
1288         
1289         /* refresh can be called before window opened */
1290         region_subwindow(CTX_wm_window(C), ar);
1291         
1292         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1293         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1294         
1295         /* UI convention */
1296         wmOrtho2(-0.01f, ar->winx - 0.01f, -0.01f, ar->winy - 0.01f);
1297         glLoadIdentity();
1298 }
1299
1300 /* for quick toggle, can skip fades */
1301 void region_toggle_hidden(bContext *C, ARegion *ar, int do_fade)
1302 {
1303         ScrArea *sa = CTX_wm_area(C);
1304         
1305         ar->flag ^= RGN_FLAG_HIDDEN;
1306         
1307         if (do_fade && ar->overlap) {
1308                 /* starts a timer, and in end calls the stuff below itself (region_sblend_invoke()) */
1309                 region_blend_start(C, sa, ar);
1310         }
1311         else {
1312                 if (ar->flag & RGN_FLAG_HIDDEN)
1313                         WM_event_remove_handlers(C, &ar->handlers);
1314                 
1315                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1316                 ED_area_tag_redraw(sa);
1317         }
1318 }
1319
1320 /* exported to all editors, uses fading default */
1321 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
1322 {
1323         region_toggle_hidden(C, ar, 1);
1324 }
1325
1326 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
1327 /* area vertices were set */
1328 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
1329 {
1330         SpaceType *st;
1331         ARegion *ar;
1332         int spacetype = sa1->spacetype;
1333         
1334         sa1->headertype = sa2->headertype;
1335         sa1->spacetype = sa2->spacetype;
1336         sa1->butspacetype = sa2->butspacetype;
1337         
1338         if (swap_space == 1) {
1339                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
1340                 /* exception: ensure preview is reset */
1341 //              if (sa1->spacetype == SPACE_VIEW3D)
1342 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
1343         }
1344         else if (swap_space == 2) {
1345                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1346         }
1347         else {
1348                 BKE_spacedata_freelist(&sa1->spacedata);
1349                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1350         }
1351         
1352         /* Note; SPACE_EMPTY is possible on new screens */
1353         
1354         /* regions */
1355         if (swap_space == 1) {
1356                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
1357         }
1358         else {
1359                 if (swap_space < 2) {
1360                         st = BKE_spacetype_from_id(spacetype);
1361                         for (ar = sa1->regionbase.first; ar; ar = ar->next)
1362                                 BKE_area_region_free(st, ar);
1363                         BLI_freelistN(&sa1->regionbase);
1364                 }
1365                 
1366                 st = BKE_spacetype_from_id(sa2->spacetype);
1367                 for (ar = sa2->regionbase.first; ar; ar = ar->next) {
1368                         ARegion *newar = BKE_area_region_copy(st, ar);
1369                         BLI_addtail(&sa1->regionbase, newar);
1370                 }
1371         }
1372 }
1373
1374 /* *********** Space switching code *********** */
1375
1376 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1377 {
1378         ScrArea *tmp = MEM_callocN(sizeof(ScrArea), "addscrarea");
1379
1380         ED_area_exit(C, sa1);
1381         ED_area_exit(C, sa2);
1382
1383         area_copy_data(tmp, sa1, 2);
1384         area_copy_data(sa1, sa2, 0);
1385         area_copy_data(sa2, tmp, 0);
1386         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1387         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1388
1389         BKE_screen_area_free(tmp);
1390         MEM_freeN(tmp);
1391
1392         /* tell WM to refresh, cursor types etc */
1393         WM_event_add_mousemove(C);
1394         
1395         ED_area_tag_redraw(sa1);
1396         ED_area_tag_refresh(sa1);
1397         ED_area_tag_redraw(sa2);
1398         ED_area_tag_refresh(sa2);
1399 }
1400
1401 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1402 {
1403         if (sa->spacetype != type) {
1404                 SpaceType *st;
1405                 SpaceLink *slold;
1406                 SpaceLink *sl;
1407
1408                 ED_area_exit(C, sa);
1409
1410                 st = BKE_spacetype_from_id(type);
1411                 slold = sa->spacedata.first;
1412
1413                 sa->spacetype = type;
1414                 sa->butspacetype = type;
1415                 sa->type = st;
1416                 
1417                 /* check previously stored space */
1418                 for (sl = sa->spacedata.first; sl; sl = sl->next)
1419                         if (sl->spacetype == type)
1420                                 break;
1421                 
1422                 /* old spacedata... happened during work on 2.50, remove */
1423                 if (sl && sl->regionbase.first == NULL) {
1424                         st->free(sl);
1425                         BLI_freelinkN(&sa->spacedata, sl);
1426                         if (slold == sl) {
1427                                 slold = NULL;
1428                         }
1429                         sl = NULL;
1430                 }
1431                 
1432                 if (sl) {
1433                         
1434                         /* swap regions */
1435                         slold->regionbase = sa->regionbase;
1436                         sa->regionbase = sl->regionbase;
1437                         sl->regionbase.first = sl->regionbase.last = NULL;
1438                         
1439                         /* put in front of list */
1440                         BLI_remlink(&sa->spacedata, sl);
1441                         BLI_addhead(&sa->spacedata, sl);
1442                 }
1443                 else {
1444                         /* new space */
1445                         if (st) {
1446                                 sl = st->new(C);
1447                                 BLI_addhead(&sa->spacedata, sl);
1448                                 
1449                                 /* swap regions */
1450                                 if (slold)
1451                                         slold->regionbase = sa->regionbase;
1452                                 sa->regionbase = sl->regionbase;
1453                                 sl->regionbase.first = sl->regionbase.last = NULL;
1454                         }
1455                 }
1456                 
1457                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1458                 
1459                 /* tell WM to refresh, cursor types etc */
1460                 WM_event_add_mousemove(C);
1461                                 
1462                 /* send space change notifier */
1463                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1464                 
1465                 ED_area_tag_refresh(sa);
1466         }
1467         
1468         /* also redraw when re-used */
1469         ED_area_tag_redraw(sa);
1470 }
1471
1472 void ED_area_prevspace(bContext *C, ScrArea *sa)
1473 {
1474         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1475
1476         if (sl && sl->next) {
1477                 /* workaround for case of double prevspace, render window
1478                  * with a file browser on top of it */
1479                 if (sl->next->spacetype == SPACE_FILE && sl->next->next)
1480                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1481                 else
1482                         ED_area_newspace(C, sa, sl->next->spacetype);
1483         }
1484         else {
1485                 /* no change */
1486                 return;
1487         }
1488         ED_area_tag_redraw(sa);
1489
1490         /* send space change notifier */
1491         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1492 }
1493
1494 static const char *editortype_pup(void)
1495 {
1496         const char *types = N_(
1497             "Editor type: %t"
1498             "|3D View %x1"
1499
1500             "|%l"
1501
1502             "|Timeline %x15"
1503             "|Graph Editor %x2"
1504             "|DopeSheet %x12"
1505             "|NLA Editor %x13"
1506
1507             "|%l"
1508
1509             "|UV/Image Editor %x6"
1510
1511             "|Video Sequence Editor %x8"
1512             "|Movie Clip Editor %x20"
1513             "|Text Editor %x9"
1514             "|Node Editor %x16"
1515             "|Logic Editor %x17"
1516
1517             "|%l"
1518
1519             "|Properties %x4"
1520             "|Outliner %x3"
1521             "|User Preferences %x19"
1522             "|Info %x7"
1523
1524             "|%l"
1525
1526             "|File Browser %x5"
1527
1528             "|%l"
1529
1530             "|Python Console %x18"
1531             );
1532
1533         return IFACE_(types);
1534 }
1535
1536 static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1537 {
1538         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1539         ED_area_tag_redraw(CTX_wm_area(C));
1540
1541         /* send space change notifier */
1542         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, CTX_wm_area(C));
1543 }
1544
1545 /* returns offset for next button in header */
1546 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1547 {
1548         ScrArea *sa = CTX_wm_area(C);
1549         uiBut *but;
1550         int xco = 0.4 * U.widget_unit;
1551         
1552         but = uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1553                                 editortype_pup(), xco, yco, 1.5 * U.widget_unit, U.widget_unit,
1554                                 &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0,
1555                                 TIP_("Display current editor type (click for a menu of available types)"));
1556         uiButSetFunc(but, spacefunc, NULL, NULL);
1557         uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
1558         
1559         return xco + 1.7 * U.widget_unit;
1560 }
1561
1562 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1563 {
1564         ScrArea *sa = CTX_wm_area(C);
1565         int xco = 0.4 * U.widget_unit;
1566         uiBut *but;
1567         
1568         if (!sa->full)
1569                 xco = ED_area_header_switchbutton(C, block, yco);
1570
1571         uiBlockSetEmboss(block, UI_EMBOSSN);
1572
1573         if (sa->flag & HEADER_NO_PULLDOWN) {
1574                 but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1575                                        ICON_DISCLOSURE_TRI_RIGHT,
1576                                        xco, yco, U.widget_unit, U.widget_unit*0.9,
1577                                        &(sa->flag), 0, 0, 0, 0,
1578                                        "Show pulldown menus");
1579         }
1580         else {
1581                 but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1582                                        ICON_DISCLOSURE_TRI_DOWN,
1583                                        xco, yco, U.widget_unit, U.widget_unit*0.9,
1584                                        &(sa->flag), 0, 0, 0, 0,
1585                                        "Hide pulldown menus");
1586         }
1587
1588         uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
1589
1590         uiBlockSetEmboss(block, UI_EMBOSS);
1591         
1592         return xco + U.widget_unit;
1593 }
1594
1595 /************************ standard UI regions ************************/
1596
1597 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1598 {
1599         ScrArea *sa = CTX_wm_area(C);
1600         uiStyle *style = UI_GetStyleDraw();
1601         uiBlock *block;
1602         PanelType *pt;
1603         Panel *panel;
1604         View2D *v2d = &ar->v2d;
1605         View2DScrollers *scrollers;
1606         int x, y, xco, yco, w, em, triangle, open, newcontext = 0;
1607         
1608         if (contextnr >= 0)
1609                 newcontext = UI_view2d_tab_set(v2d, contextnr);
1610
1611         if (vertical) {
1612                 w = BLI_rctf_size_x(&v2d->cur);
1613                 em = (ar->type->prefsizex) ? UI_UNIT_Y / 2 : UI_UNIT_Y;
1614         }
1615         else {
1616                 w = UI_PANEL_WIDTH;
1617                 em = (ar->type->prefsizex) ? UI_UNIT_Y / 2 : UI_UNIT_Y;
1618         }
1619
1620         /* create panels */
1621         uiBeginPanels(C, ar);
1622
1623         /* set view2d view matrix for scrolling (without scrollers) */
1624         UI_view2d_view_ortho(v2d);
1625
1626         for (pt = ar->type->paneltypes.first; pt; pt = pt->next) {
1627                 /* verify context */
1628                 if (context)
1629                         if (pt->context[0] && strcmp(context, pt->context) != 0)
1630                                 continue;
1631
1632                 /* draw panel */
1633                 if (pt->draw && (!pt->poll || pt->poll(C, pt))) {
1634                         block = uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1635                         panel = uiBeginPanel(sa, ar, block, pt, &open);
1636
1637                         /* bad fixed values */
1638                         triangle = (int)(UI_UNIT_Y * 1.1f);
1639
1640                         if (pt->draw_header && !(pt->flag & PNL_NO_HEADER) && (open || vertical)) {
1641                                 /* for enabled buttons */
1642                                 panel->layout = uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1643                                                               triangle, (UI_UNIT_Y * 1.1f) + style->panelspace, UI_UNIT_Y, 1, style);
1644
1645                                 pt->draw_header(C, panel);
1646
1647                                 uiBlockLayoutResolve(block, &xco, &yco);
1648                                 panel->labelofs = xco - triangle;
1649                                 panel->layout = NULL;
1650                         }
1651                         else {
1652                                 panel->labelofs = 0;
1653                         }
1654
1655                         if (open) {
1656                                 short panelContext;
1657                                 
1658                                 /* panel context can either be toolbar region or normal panels region */
1659                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1660                                         panelContext = UI_LAYOUT_TOOLBAR;
1661                                 else
1662                                         panelContext = UI_LAYOUT_PANEL;
1663                                 
1664                                 panel->layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1665                                                               style->panelspace, 0, w - 2 * style->panelspace, em, style);
1666
1667                                 pt->draw(C, panel);
1668
1669                                 uiBlockLayoutResolve(block, &xco, &yco);
1670                                 panel->layout = NULL;
1671
1672                                 yco -= 2 * style->panelspace;
1673                                 uiEndPanel(block, w, -yco);
1674                         }
1675                         else {
1676                                 yco = 0;
1677                                 uiEndPanel(block, w, 0);
1678                         }
1679
1680                         uiEndBlock(C, block);
1681                 }
1682         }
1683
1684         /* align panels and return size */
1685         uiEndPanels(C, ar, &x, &y);
1686
1687         /* clear */
1688         if (ar->overlap) {
1689                 /* view should be in pixelspace */
1690                 UI_view2d_view_restore(C);
1691                 glEnable(GL_BLEND);
1692                 UI_ThemeColor4((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
1693                 glRecti(0, 0, BLI_rcti_size_x(&ar->winrct), BLI_rcti_size_y(&ar->winrct));
1694                 glDisable(GL_BLEND);
1695         }
1696         else {
1697                 UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
1698                 glClear(GL_COLOR_BUFFER_BIT);
1699         }
1700         
1701         /* before setting the view */
1702         if (vertical) {
1703                 /* only allow scrolling in vertical direction */
1704                 v2d->keepofs |= V2D_LOCKOFS_X | V2D_KEEPOFS_Y;
1705                 v2d->keepofs &= ~(V2D_LOCKOFS_Y | V2D_KEEPOFS_X);
1706                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1707                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1708
1709                 /* ensure tot is set correctly, to keep views on bottons, with sliders */
1710                 y = min_ii(y, v2d->cur.ymin);
1711                 y = -y;
1712         }
1713         else {
1714                 /* for now, allow scrolling in both directions (since layouts are optimized for vertical,
1715                  * they often don't fit in horizontal layout)
1716                  */
1717                 v2d->keepofs &= ~(V2D_LOCKOFS_X | V2D_LOCKOFS_Y | V2D_KEEPOFS_X | V2D_KEEPOFS_Y);
1718                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1719                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1720                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1721                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1722
1723                 /* don't jump back when panels close or hide */
1724                 if (!newcontext)
1725                         x = max_ii(x, v2d->cur.xmax);
1726                 y = -y;
1727         }
1728
1729         /* +V2D_SCROLL_HEIGHT is workaround to set the actual height (needs to be int) */
1730         UI_view2d_totRect_set(v2d, x + (int)V2D_SCROLL_WIDTH, y + (int)V2D_SCROLL_HEIGHT);
1731
1732         /* set the view */
1733         UI_view2d_view_ortho(v2d);
1734
1735         /* draw panels */
1736         uiDrawPanels(C, ar);
1737
1738         /* restore view matrix */
1739         UI_view2d_view_restore(C);
1740         
1741         /* scrollers */
1742         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1743         UI_view2d_scrollers_draw(C, v2d, scrollers);
1744         UI_view2d_scrollers_free(scrollers);
1745 }
1746
1747 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1748 {
1749         wmKeyMap *keymap;
1750         
1751         /* XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1752          * scrollbars for button regions */
1753         ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
1754         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1755         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1756         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1757
1758         /* correctly initialized User-Prefs? */
1759         if (!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1760                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1761
1762         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1763
1764         keymap = WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1765         WM_event_add_keymap_handler(&ar->handlers, keymap);
1766 }
1767
1768 void ED_region_header(const bContext *C, ARegion *ar)
1769 {
1770         uiStyle *style = UI_GetStyleDraw();
1771         uiBlock *block;
1772         uiLayout *layout;
1773         HeaderType *ht;
1774         Header header = {NULL};
1775         int maxco, xco, yco;
1776         int headery = ED_area_headersize();
1777
1778         /* clear */
1779         UI_ThemeClearColor((ED_screen_area_active(C)) ? TH_HEADER : TH_HEADERDESEL);
1780         glClear(GL_COLOR_BUFFER_BIT);
1781         
1782         /* set view2d view matrix for scrolling (without scrollers) */
1783         UI_view2d_view_ortho(&ar->v2d);
1784
1785         xco = maxco = 0.4f * UI_UNIT_X;
1786         yco = headery - floor(0.2f * UI_UNIT_Y);
1787
1788         /* draw all headers types */
1789         for (ht = ar->type->headertypes.first; ht; ht = ht->next) {
1790                 block = uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1791                 layout = uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, UI_UNIT_Y, 1, style);
1792
1793                 if (ht->draw) {
1794                         header.type = ht;
1795                         header.layout = layout;
1796                         ht->draw(C, &header);
1797                         
1798                         /* for view2d */
1799                         xco = uiLayoutGetWidth(layout);
1800                         if (xco > maxco)
1801                                 maxco = xco;
1802                 }
1803
1804                 uiBlockLayoutResolve(block, &xco, &yco);
1805                 
1806                 /* for view2d */
1807                 if (xco > maxco)
1808                         maxco = xco;
1809                 
1810                 uiEndBlock(C, block);
1811                 uiDrawBlock(C, block);
1812         }
1813
1814         /* always as last  */
1815         UI_view2d_totRect_set(&ar->v2d, maxco + UI_UNIT_X + 80, headery);
1816
1817         /* restore view matrix? */
1818         UI_view2d_view_restore(C);
1819 }
1820
1821 void ED_region_header_init(ARegion *ar)
1822 {
1823         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1824 }
1825
1826 /* UI_UNIT_Y is defined as U variable now, depending dpi */
1827 int ED_area_headersize(void)
1828 {
1829         return (int)(1.3f * UI_UNIT_Y);
1830 }
1831
1832 void ED_region_info_draw(ARegion *ar, const char *text, int block, float alpha)
1833 {
1834         const int header_height = UI_UNIT_Y;
1835         uiStyle *style = UI_GetStyleDraw();
1836         int fontid = style->widget.uifont_id;
1837         rcti rect;
1838
1839         /* background box */
1840         ED_region_visible_rect(ar, &rect);
1841         rect.xmin = 0;
1842         rect.ymin = BLI_rcti_size_y(&ar->winrct) - header_height;
1843
1844         if (block) {
1845                 rect.xmax = BLI_rcti_size_x(&ar->winrct);
1846         }
1847         else {
1848                 rect.xmax = rect.xmin + BLF_width(fontid, text) + 24;
1849         }
1850
1851         rect.ymax = BLI_rcti_size_y(&ar->winrct);
1852
1853         glEnable(GL_BLEND);
1854         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1855         glColor4f(0.0f, 0.0f, 0.0f, alpha);
1856         glRecti(rect.xmin, rect.ymin, rect.xmax + 1, rect.ymax + 1);
1857         glDisable(GL_BLEND);
1858
1859         /* text */
1860         UI_ThemeColor(TH_TEXT_HI);
1861         BLF_position(fontid, 12, rect.ymin + 5, 0.0f);
1862         BLF_draw(fontid, text, BLF_DRAW_STR_DUMMY_MAX);
1863 }
1864
1865 void ED_region_grid_draw(ARegion *ar, float zoomx, float zoomy)
1866 {
1867         float gridsize, gridstep = 1.0f / 32.0f;
1868         float fac, blendfac;
1869         int x1, y1, x2, y2;
1870
1871         /* the image is located inside (0, 0), (1, 1) as set by view2d */
1872         UI_ThemeColorShade(TH_BACK, 20);
1873
1874         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
1875         UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
1876         glRectf(x1, y1, x2, y2);
1877
1878         /* gridsize adapted to zoom level */
1879         gridsize = 0.5f * (zoomx + zoomy);
1880         if (gridsize <= 0.0f)
1881                 return;
1882
1883         if (gridsize < 1.0f) {
1884                 while (gridsize < 1.0f) {
1885                         gridsize *= 4.0f;
1886                         gridstep *= 4.0f;
1887                 }
1888         }
1889         else {
1890                 while (gridsize >= 4.0f) {
1891                         gridsize /= 4.0f;
1892                         gridstep /= 4.0f;
1893                 }
1894         }
1895
1896         /* the fine resolution level */
1897         blendfac = 0.25f * gridsize - floorf(0.25f * gridsize);
1898         CLAMP(blendfac, 0.0f, 1.0f);
1899         UI_ThemeColorShade(TH_BACK, (int)(20.0f * (1.0f - blendfac)));
1900
1901         fac = 0.0f;
1902         glBegin(GL_LINES);
1903         while (fac < 1.0f) {
1904                 glVertex2f(x1, y1 * (1.0f - fac) + y2 * fac);
1905                 glVertex2f(x2, y1 * (1.0f - fac) + y2 * fac);
1906                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y1);
1907                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y2);
1908                 fac += gridstep;
1909         }
1910
1911         /* the large resolution level */
1912         UI_ThemeColor(TH_BACK);
1913
1914         fac = 0.0f;
1915         while (fac < 1.0f) {
1916                 glVertex2f(x1, y1 * (1.0f - fac) + y2 * fac);
1917                 glVertex2f(x2, y1 * (1.0f - fac) + y2 * fac);
1918                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y1);
1919                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y2);
1920                 fac += 4.0f * gridstep;
1921         }
1922         glEnd();
1923 }
1924
1925 /* If the area has overlapping regions, it returns visible rect for Region *ar */
1926 /* rect gets returned in local region coordinates */
1927 void ED_region_visible_rect(ARegion *ar, rcti *rect)
1928 {
1929         ARegion *arn = ar;
1930         
1931         /* allow function to be called without area */
1932         while (arn->prev)
1933                 arn = arn->prev;
1934         
1935         *rect = ar->winrct;
1936         
1937         /* check if a region overlaps with the current one */
1938         for (; arn; arn = arn->next) {
1939                 if (ar != arn && arn->overlap) {
1940                         if (BLI_rcti_isect(rect, &arn->winrct, NULL)) {
1941                                 /* overlap left */
1942                                 if (rect->xmin == arn->winrct.xmin)
1943                                         rect->xmin = arn->winrct.xmax;
1944                                 /* overlap right */
1945                                 if (rect->xmax == arn->winrct.xmax)
1946                                         rect->xmax = arn->winrct.xmin;
1947                         }
1948                 }
1949         }
1950         BLI_rcti_translate(rect, -ar->winrct.xmin, -ar->winrct.ymin);
1951 }
1952