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