Fix T37843: area split widget missing in python console editor.
[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 #include "BLI_alloca.h"
43 #include "BLI_linklist_stack.h"
44
45 #include "BLF_translation.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_screen.h"
50
51 #include "RNA_access.h"
52 #include "RNA_types.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56 #include "wm_subwindow.h"
57
58 #include "ED_screen.h"
59 #include "ED_screen_types.h"
60 #include "ED_space_api.h"
61 #include "ED_types.h"
62 #include "ED_fileselect.h" 
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66 #include "BLF_api.h"
67
68 #include "UI_interface.h"
69 #include "UI_resources.h"
70 #include "UI_view2d.h"
71
72 #include "screen_intern.h"
73
74 extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
75
76 /* general area and region code */
77
78 static void region_draw_emboss(ARegion *ar, rcti *scirct)
79 {
80         rcti rect;
81         
82         /* translate scissor rect to region space */
83         rect.xmin = scirct->xmin - ar->winrct.xmin;
84         rect.ymin = scirct->ymin - ar->winrct.ymin;
85         rect.xmax = scirct->xmax - ar->winrct.xmin;
86         rect.ymax = scirct->ymax - ar->winrct.ymin;
87         
88         /* set transp line */
89         glEnable(GL_BLEND);
90         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
91         
92         /* right  */
93         glColor4ub(0, 0, 0, 30);
94         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
95         
96         /* bottom  */
97         glColor4ub(0, 0, 0, 30);
98         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
99         
100         /* top  */
101         glColor4ub(255, 255, 255, 30);
102         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
103
104         /* left  */
105         glColor4ub(255, 255, 255, 30);
106         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
107         
108         glDisable(GL_BLEND);
109 }
110
111 void ED_region_pixelspace(ARegion *ar)
112 {
113         int width  = BLI_rcti_size_x(&ar->winrct) + 1;
114         int height = BLI_rcti_size_y(&ar->winrct) + 1;
115         
116         wmOrtho2(-GLA_PIXEL_OFS, (float)width - GLA_PIXEL_OFS, -GLA_PIXEL_OFS, (float)height - GLA_PIXEL_OFS);
117         glLoadIdentity();
118 }
119
120 /* only exported for WM */
121 void ED_region_do_listen(bScreen *sc, ScrArea *sa, ARegion *ar, wmNotifier *note)
122 {
123         /* generic notes first */
124         switch (note->category) {
125                 case NC_WM:
126                         if (note->data == ND_FILEREAD)
127                                 ED_region_tag_redraw(ar);
128                         break;
129                 case NC_WINDOW:
130                         ED_region_tag_redraw(ar);
131                         break;
132         }
133
134         if (ar->type && ar->type->listener)
135                 ar->type->listener(sc, sa, ar, note);
136 }
137
138 /* only exported for WM */
139 void ED_area_do_listen(bScreen *sc, ScrArea *sa, wmNotifier *note)
140 {
141         /* no generic notes? */
142         if (sa->type && sa->type->listener) {
143                 sa->type->listener(sc, sa, note);
144         }
145 }
146
147 /* only exported for WM */
148 void ED_area_do_refresh(bContext *C, ScrArea *sa)
149 {
150         /* no generic notes? */
151         if (sa->type && sa->type->refresh) {
152                 sa->type->refresh(C, sa);
153         }
154         sa->do_refresh = FALSE;
155 }
156
157 /**
158  * \brief Corner widgets use for dragging and splitting the view.
159  */
160 static void area_draw_azone(short x1, short y1, short x2, short y2)
161 {
162         int dx = x2 - x1;
163         int dy = y2 - y1;
164
165         dx = copysign(ceilf(0.3f * abs(dx)), dx);
166         dy = copysign(ceilf(0.3f * abs(dy)), dy);
167
168         glEnable(GL_BLEND);
169         glEnable(GL_LINE_SMOOTH);
170         
171         glColor4ub(255, 255, 255, 180);
172         fdrawline(x1, y2, x2, y1);
173         glColor4ub(255, 255, 255, 130);
174         fdrawline(x1, y2 - dy, x2 - dx, y1);
175         glColor4ub(255, 255, 255, 80);
176         fdrawline(x1, y2 - 2 * dy, x2 - 2 * dx, y1);
177         
178         glColor4ub(0, 0, 0, 210);
179         fdrawline(x1, y2 + 1, x2 + 1, y1);
180         glColor4ub(0, 0, 0, 180);
181         fdrawline(x1, y2 - dy + 1, x2 - dx + 1, y1);
182         glColor4ub(0, 0, 0, 150);
183         fdrawline(x1, y2 - 2 * dy + 1, x2 - 2 * dx + 1, y1);
184
185         glDisable(GL_LINE_SMOOTH);
186         glDisable(GL_BLEND);
187 }
188
189 static void region_draw_azone_icon(AZone *az)
190 {
191         GLUquadricObj *qobj = NULL; 
192         short midx = az->x1 + (az->x2 - az->x1) / 2;
193         short midy = az->y1 + (az->y2 - az->y1) / 2;
194                 
195         qobj = gluNewQuadric();
196         
197         glPushMatrix();
198         glTranslatef(midx, midy, 0.0);
199         
200         /* outlined circle */
201         glEnable(GL_LINE_SMOOTH);
202
203         glColor4f(1.f, 1.f, 1.f, 0.8f);
204
205         gluQuadricDrawStyle(qobj, GLU_FILL); 
206         gluDisk(qobj, 0.0,  4.25f, 16, 1);
207
208         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
209         
210         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
211         gluDisk(qobj, 0.0,  4.25f, 16, 1);
212         
213         glDisable(GL_LINE_SMOOTH);
214         
215         glPopMatrix();
216         gluDeleteQuadric(qobj);
217         
218         /* + */
219         sdrawline(midx, midy - 2, midx, midy + 3);
220         sdrawline(midx - 2, midy, midx + 3, midy);
221 }
222
223 static void draw_azone_plus(float x1, float y1, float x2, float y2)
224 {
225         float width = 0.1f * U.widget_unit;
226         float pad = 0.2f * U.widget_unit;
227         
228         glRectf((x1 + x2 - width) * 0.5f, y1 + pad, (x1 + x2 + width) * 0.5f, y2 - pad);
229         glRectf(x1 + pad, (y1 + y2 - width) * 0.5f, (x1 + x2 - width) * 0.5f, (y1 + y2 + width) * 0.5f);
230         glRectf((x1 + x2 + width) * 0.5f, (y1 + y2 - width) * 0.5f, x2 - pad, (y1 + y2 + width) * 0.5f);
231 }
232
233 static void region_draw_azone_tab_plus(AZone *az)
234 {
235         glEnable(GL_BLEND);
236         
237         /* add code to draw region hidden as 'too small' */
238         switch (az->edge) {
239                 case AE_TOP_TO_BOTTOMRIGHT:
240                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
241                         break;
242                 case AE_BOTTOM_TO_TOPLEFT:
243                         uiSetRoundBox(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT);
244                         break;
245                 case AE_LEFT_TO_TOPRIGHT:
246                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT);
247                         break;
248                 case AE_RIGHT_TO_TOPLEFT:
249                         uiSetRoundBox(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT);
250                         break;
251         }
252
253         glColor4f(0.05f, 0.05f, 0.05f, 0.4f);
254         uiRoundBox((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
255
256         glEnable(GL_BLEND);
257
258         glColor4f(0.8f, 0.8f, 0.8f, 0.4f);
259         draw_azone_plus((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2);
260
261         glDisable(GL_BLEND);
262 }
263
264 static void region_draw_azone_tab(AZone *az)
265 {
266         float col[3];
267         
268         glEnable(GL_BLEND);
269         UI_GetThemeColor3fv(TH_HEADER, col);
270         glColor4f(col[0], col[1], col[2], 0.5f);
271         
272         /* add code to draw region hidden as 'too small' */
273         switch (az->edge) {
274                 case AE_TOP_TO_BOTTOMRIGHT:
275                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT | UI_RB_ALPHA);
276                         
277                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
278                         glColor4ub(0, 0, 0, 255);
279                         uiRoundRect((float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f);
280                         break;
281                 case AE_BOTTOM_TO_TOPLEFT:
282                         uiSetRoundBox(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT | UI_RB_ALPHA);
283                         
284                         uiDrawBoxShade(GL_POLYGON, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f);
285                         glColor4ub(0, 0, 0, 255);
286                         uiRoundRect((float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f);
287                         break;
288                 case AE_LEFT_TO_TOPRIGHT:
289                         uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT | 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, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
294                         break;
295                 case AE_RIGHT_TO_TOPLEFT:
296                         uiSetRoundBox(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT | 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, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f);
301                         break;
302         }
303         
304         glDisable(GL_BLEND);
305 }
306
307 static void region_draw_azone_tria(AZone *az)
308 {
309         glEnable(GL_BLEND);
310         //UI_GetThemeColor3fv(TH_HEADER, col);
311         glColor4f(0.0f, 0.0f, 0.0f, 0.35f);
312         
313         /* add code to draw region hidden as 'too small' */
314         switch (az->edge) {
315                 case AE_TOP_TO_BOTTOMRIGHT:
316                         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);
317                         break;
318                         
319                 case AE_BOTTOM_TO_TOPLEFT:
320                         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);
321                         break;
322
323                 case AE_LEFT_TO_TOPRIGHT:
324                         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);
325                         break;
326                         
327                 case AE_RIGHT_TO_TOPLEFT:
328                         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);
329                         break;
330                         
331         }
332         
333         glDisable(GL_BLEND);
334 }
335
336 static void region_draw_azones(ScrArea *sa, ARegion *ar)
337 {
338         AZone *az;
339
340         if (!sa)
341                 return;
342
343         glEnable(GL_BLEND);
344         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
345
346         glPushMatrix();
347         glTranslatef(-ar->winrct.xmin, -ar->winrct.ymin, 0.0f);
348         
349         for (az = sa->actionzones.first; az; az = az->next) {
350                 /* test if action zone is over this region */
351                 rcti azrct;
352                 BLI_rcti_init(&azrct, az->x1, az->x2, az->y1, az->y2);
353
354                 if (BLI_rcti_isect(&ar->drawrct, &azrct, NULL)) {
355                         if (az->type == AZONE_AREA) {
356                                 area_draw_azone(az->x1, az->y1, az->x2, az->y2);
357                         }
358                         else if (az->type == AZONE_REGION) {
359                                 
360                                 if (az->ar) {
361                                         /* only display tab or icons when the region is hidden */
362                                         if (az->ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
363                                                 if (G.debug_value == 3)
364                                                         region_draw_azone_icon(az);
365                                                 else if (G.debug_value == 2)
366                                                         region_draw_azone_tria(az);
367                                                 else if (G.debug_value == 1)
368                                                         region_draw_azone_tab(az);
369                                                 else
370                                                         region_draw_azone_tab_plus(az);
371                                         }
372                                 }
373                         }
374                 }
375         }
376
377         glPopMatrix();
378
379         glDisable(GL_BLEND);
380 }
381
382 /* only exported for WM */
383 /* makes region ready for drawing, sets pixelspace */
384 void ED_region_set(const bContext *C, ARegion *ar)
385 {
386         wmWindow *win = CTX_wm_window(C);
387         ScrArea *sa = CTX_wm_area(C);
388         
389         ar->drawrct = ar->winrct;
390         
391         /* note; this sets state, so we can use wmOrtho and friends */
392         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct, true);
393         
394         UI_SetTheme(sa ? sa->spacetype : 0, ar->type ? ar->type->regionid : 0);
395         
396         ED_region_pixelspace(ar);
397 }
398
399
400 /* only exported for WM */
401 void ED_region_do_draw(bContext *C, ARegion *ar)
402 {
403         wmWindow *win = CTX_wm_window(C);
404         ScrArea *sa = CTX_wm_area(C);
405         ARegionType *at = ar->type;
406         bool scissor_pad;
407
408         /* see BKE_spacedata_draw_locks() */
409         if (at->do_lock)
410                 return;
411         
412         /* if no partial draw rect set, full rect */
413         if (ar->drawrct.xmin == ar->drawrct.xmax) {
414                 ar->drawrct = ar->winrct;
415                 scissor_pad = true;
416         }
417         else {
418                 /* extra clip for safety */
419                 BLI_rcti_isect(&ar->winrct, &ar->drawrct, &ar->drawrct);
420                 scissor_pad = false;
421         }
422
423         ar->do_draw |= RGN_DRAWING;
424         
425         /* note; this sets state, so we can use wmOrtho and friends */
426         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct, scissor_pad);
427         
428         UI_SetTheme(sa ? sa->spacetype : 0, at->regionid);
429         
430         /* optional header info instead? */
431         if (ar->headerstr) {
432                 UI_ThemeClearColor(TH_HEADER);
433                 glClear(GL_COLOR_BUFFER_BIT);
434                 
435                 UI_ThemeColor(TH_TEXT);
436                 BLF_draw_default(UI_UNIT_X, 0.4f * UI_UNIT_Y, 0.0f, ar->headerstr, BLF_DRAW_STR_DUMMY_MAX);
437         }
438         else if (at->draw) {
439                 at->draw(C, ar);
440         }
441
442         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
443         ED_region_pixelspace(ar);
444
445         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
446
447         region_draw_azones(sa, ar);
448
449         /* for debugging unneeded area redraws and partial redraw */
450 #if 0
451         glEnable(GL_BLEND);
452         glColor4f(drand48(), drand48(), drand48(), 0.1f);
453         glRectf(ar->drawrct.xmin - ar->winrct.xmin, ar->drawrct.ymin - ar->winrct.ymin,
454                 ar->drawrct.xmax - ar->winrct.xmin, ar->drawrct.ymax - ar->winrct.ymin);
455         glDisable(GL_BLEND);
456 #endif
457
458         ar->do_draw = 0;
459         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
460         
461         uiFreeInactiveBlocks(C, &ar->uiblocks);
462
463         if (sa)
464                 region_draw_emboss(ar, &ar->winrct);
465 }
466
467 /* **********************************
468  * maybe silly, but let's try for now
469  * to keep these tags protected
470  * ********************************** */
471
472 void ED_region_tag_redraw(ARegion *ar)
473 {
474         /* don't tag redraw while drawing, it shouldn't happen normally
475          * but python scripts can cause this to happen indirectly */
476         if (ar && !(ar->do_draw & RGN_DRAWING)) {
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 && !(ar->do_draw & RGN_DRAWING)) {
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;
578         az->y1 = sa->totrct.ymin;
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;
587         az->y1 = sa->totrct.ymax;
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.1f * 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 + AZONEPAD_EDGE;
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 - AZONEPAD_EDGE;
609                         break;
610                 case AE_LEFT_TO_TOPRIGHT:
611                         az->x1 = ar->winrct.xmin - AZONEPAD_EDGE;
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 + AZONEPAD_EDGE;
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) + 1 - size;
869         }
870         else {  /* 'v' */
871                 return BLI_rcti_size_y(rect) + 1 - 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(ScrArea *sa, 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 or close */
894         if (ar1) {
895                 int align1 = ar1->alignment & ~RGN_SPLIT_PREV;
896
897                 if (align1 == RGN_ALIGN_LEFT) {
898                         if (ar->winrct.xmax + ar1->winx > sa->winx - U.widget_unit)
899                                 ar->flag |= RGN_FLAG_TOO_SMALL;
900                         else
901                                 BLI_rcti_translate(&ar->winrct, ar1->winx, 0);
902                 }
903                 else if (align1 == RGN_ALIGN_RIGHT) {
904                         if (ar->winrct.xmin - ar1->winx < U.widget_unit)
905                                 ar->flag |= RGN_FLAG_TOO_SMALL;
906                         else
907                                 BLI_rcti_translate(&ar->winrct, -ar1->winx, 0);
908                 }
909         }
910
911         
912         
913 }
914
915 /* overlapping regions only in the following restricted cases */
916 static int region_is_overlap(wmWindow *win, ScrArea *sa, ARegion *ar)
917 {
918         if (U.uiflag2 & USER_REGION_OVERLAP) {
919                 if (WM_is_draw_triple(win)) {
920                         if (ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ)) {
921                                 if (ELEM3(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS))
922                                         return 1;
923                         }
924                         else if (sa->spacetype == SPACE_IMAGE) {
925                                 if (ELEM4(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS, RGN_TYPE_PREVIEW))
926                                         return 1;
927                         }
928                 }
929         }
930
931         return 0;
932 }
933
934 static void region_rect_recursive(wmWindow *win, ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
935 {
936         rcti *remainder_prev = remainder;
937         int prefsizex, prefsizey;
938         int alignment;
939         
940         if (ar == NULL)
941                 return;
942         
943         /* no returns in function, winrct gets set in the end again */
944         BLI_rcti_init(&ar->winrct, 0, 0, 0, 0);
945         
946         /* for test; allow split of previously defined region */
947         if (ar->alignment & RGN_SPLIT_PREV)
948                 if (ar->prev)
949                         remainder = &ar->prev->winrct;
950         
951         alignment = ar->alignment & ~RGN_SPLIT_PREV;
952         
953         /* set here, assuming userpref switching forces to call this again */
954         ar->overlap = region_is_overlap(win, sa, ar);
955
956         /* clear state flags first */
957         ar->flag &= ~RGN_FLAG_TOO_SMALL;
958         /* user errors */
959         if (ar->next == NULL && alignment != RGN_ALIGN_QSPLIT)
960                 alignment = RGN_ALIGN_NONE;
961         
962         /* prefsize, for header we stick to exception (prevent dpi rounding error) */
963         prefsizex = UI_DPI_FAC * (ar->sizex > 1 ? ar->sizex + 0.5f : ar->type->prefsizex);
964         
965         if (ar->regiontype == RGN_TYPE_HEADER) {
966                 prefsizey = ED_area_headersize();
967         }
968         else if (ar->regiontype == RGN_TYPE_UI && sa->spacetype == SPACE_FILE) {
969                 prefsizey = UI_UNIT_Y * 2 + (UI_UNIT_Y / 2);
970         }
971         else {
972                 prefsizey = UI_DPI_FAC * (ar->sizey > 1 ? ar->sizey + 0.5f : ar->type->prefsizey);
973         }
974
975
976         if (ar->flag & RGN_FLAG_HIDDEN) {
977                 /* hidden is user flag */
978         }
979         else if (alignment == RGN_ALIGN_FLOAT) {
980                 /* XXX floating area region, not handled yet here */
981         }
982         else if (rct_fits(remainder, 'v', 1) < 0 || rct_fits(remainder, 'h', 1) < 0) {
983                 /* remainder is too small for any usage */
984                 ar->flag |= RGN_FLAG_TOO_SMALL;
985         }
986         else if (alignment == RGN_ALIGN_NONE) {
987                 /* typically last region */
988                 ar->winrct = *remainder;
989                 BLI_rcti_init(remainder, 0, 0, 0, 0);
990         }
991         else if (alignment == RGN_ALIGN_TOP || alignment == RGN_ALIGN_BOTTOM) {
992                 
993                 if (rct_fits(remainder, 'v', prefsizey) < 0) {
994                         ar->flag |= RGN_FLAG_TOO_SMALL;
995                 }
996                 else {
997                         int fac = rct_fits(remainder, 'v', prefsizey);
998
999                         if (fac < 0)
1000                                 prefsizey += fac;
1001                         
1002                         ar->winrct = *remainder;
1003                         
1004                         if (alignment == RGN_ALIGN_TOP) {
1005                                 ar->winrct.ymin = ar->winrct.ymax - prefsizey + 1;
1006                                 remainder->ymax = ar->winrct.ymin - 1;
1007                         }
1008                         else {
1009                                 ar->winrct.ymax = ar->winrct.ymin + prefsizey - 1;
1010                                 remainder->ymin = ar->winrct.ymax + 1;
1011                         }
1012                 }
1013         }
1014         else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
1015                 
1016                 if (rct_fits(remainder, 'h', prefsizex) < 0) {
1017                         ar->flag |= RGN_FLAG_TOO_SMALL;
1018                 }
1019                 else {
1020                         int fac = rct_fits(remainder, 'h', prefsizex);
1021                         
1022                         if (fac < 0)
1023                                 prefsizex += fac;
1024                         
1025                         ar->winrct = *remainder;
1026                         
1027                         if (alignment == RGN_ALIGN_RIGHT) {
1028                                 ar->winrct.xmin = ar->winrct.xmax - prefsizex + 1;
1029                                 if (ar->overlap == 0)
1030                                         remainder->xmax = ar->winrct.xmin - 1;
1031                         }
1032                         else {
1033                                 ar->winrct.xmax = ar->winrct.xmin + prefsizex - 1;
1034                                 if (ar->overlap == 0)
1035                                         remainder->xmin = ar->winrct.xmax + 1;
1036                         }
1037                 }
1038         }
1039         else if (alignment == RGN_ALIGN_VSPLIT || alignment == RGN_ALIGN_HSPLIT) {
1040                 /* percentage subdiv*/
1041                 ar->winrct = *remainder;
1042                 
1043                 if (alignment == RGN_ALIGN_HSPLIT) {
1044                         if (rct_fits(remainder, 'h', prefsizex) > 4) {
1045                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1046                                 remainder->xmin = ar->winrct.xmax + 1;
1047                         }
1048                         else {
1049                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1050                         }
1051                 }
1052                 else {
1053                         if (rct_fits(remainder, 'v', prefsizey) > 4) {
1054                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1055                                 remainder->ymin = ar->winrct.ymax + 1;
1056                         }
1057                         else {
1058                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1059                         }
1060                 }
1061         }
1062         else if (alignment == RGN_ALIGN_QSPLIT) {
1063                 ar->winrct = *remainder;
1064                 
1065                 /* test if there's still 4 regions left */
1066                 if (quad == 0) {
1067                         ARegion *artest = ar->next;
1068                         int count = 1;
1069                         
1070                         while (artest) {
1071                                 artest->alignment = RGN_ALIGN_QSPLIT;
1072                                 artest = artest->next;
1073                                 count++;
1074                         }
1075                         
1076                         if (count != 4) {
1077                                 /* let's stop adding regions */
1078                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1079                                 if (G.debug & G_DEBUG)
1080                                         printf("region quadsplit failed\n");
1081                         }
1082                         else {
1083                                 quad = 1;
1084                         }
1085                 }
1086                 if (quad) {
1087                         if (quad == 1) { /* left bottom */
1088                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1089                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1090                         }
1091                         else if (quad == 2) { /* left top */
1092                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1093                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1094                         }
1095                         else if (quad == 3) { /* right bottom */
1096                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1097                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1098                         }
1099                         else {  /* right top */
1100                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1101                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1102                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1103                         }
1104
1105                         quad++;
1106                 }
1107         }
1108         
1109         /* for speedup */
1110         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1111         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1112         
1113         /* if region opened normally, we store this for hide/reveal usage */
1114         /* prevent rounding errors for UI_DPI_FAC mult and divide */
1115         if (ar->winx > 1) ar->sizex = (ar->winx + 0.5f) /  UI_DPI_FAC;
1116         if (ar->winy > 1) ar->sizey = (ar->winy + 0.5f) /  UI_DPI_FAC;
1117                 
1118         /* exception for multiple overlapping regions on same spot */
1119         if (ar->overlap)
1120                 region_overlap_fix(sa, ar);
1121
1122         /* set winrect for azones */
1123         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
1124                 ar->winrct = *remainder;
1125                 
1126                 if (alignment == RGN_ALIGN_TOP)
1127                         ar->winrct.ymin = ar->winrct.ymax;
1128                 else if (alignment == RGN_ALIGN_BOTTOM)
1129                         ar->winrct.ymax = ar->winrct.ymin;
1130                 else if (alignment == RGN_ALIGN_RIGHT)
1131                         ar->winrct.xmin = ar->winrct.xmax;
1132                 else if (alignment == RGN_ALIGN_LEFT)
1133                         ar->winrct.xmax = ar->winrct.xmin;
1134                 else /* prevent winrct to be valid */
1135                         ar->winrct.xmax = ar->winrct.xmin;
1136         }
1137
1138         /* restore prev-split exception */
1139         if (ar->alignment & RGN_SPLIT_PREV) {
1140                 if (ar->prev) {
1141                         remainder = remainder_prev;
1142                         ar->prev->winx = BLI_rcti_size_x(&ar->prev->winrct) + 1;
1143                         ar->prev->winy = BLI_rcti_size_y(&ar->prev->winrct) + 1;
1144                 }
1145         }
1146         
1147         /* in end, add azones, where appropriate */
1148         if (ar->regiontype == RGN_TYPE_HEADER && ar->winy + 6 > sa->winy) {
1149                 /* The logic for this is: when the header takes up the full area,
1150                  * disallow hiding it to view the main window.
1151                  *
1152                  * Without this, you can drag down the file selectors header and hide it
1153                  * by accident very easily (highly annoying!), the value 6 is arbitrary
1154                  * but accounts for small common rounding problems when scaling the UI,
1155                  * must be minimum '4' */
1156         }
1157         else {
1158                 region_azone_add(sa, ar, alignment);
1159         }
1160
1161         region_rect_recursive(win, sa, ar->next, remainder, quad);
1162 }
1163
1164 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
1165 {
1166         short rt = (short) U.pixelsize;
1167
1168         if (sa->v1->vec.x > 0) sa->totrct.xmin = sa->v1->vec.x + rt;
1169         else sa->totrct.xmin = sa->v1->vec.x;
1170         if (sa->v4->vec.x < sizex - 1) sa->totrct.xmax = sa->v4->vec.x - rt;
1171         else sa->totrct.xmax = sa->v4->vec.x;
1172         
1173         if (sa->v1->vec.y > 0) sa->totrct.ymin = sa->v1->vec.y + rt;
1174         else sa->totrct.ymin = sa->v1->vec.y;
1175         if (sa->v2->vec.y < sizey - 1) sa->totrct.ymax = sa->v2->vec.y - rt;
1176         else sa->totrct.ymax = sa->v2->vec.y;
1177         
1178         /* for speedup */
1179         sa->winx = BLI_rcti_size_x(&sa->totrct) + 1;
1180         sa->winy = BLI_rcti_size_y(&sa->totrct) + 1;
1181 }
1182
1183
1184 /* used for area initialize below */
1185 static void region_subwindow(wmWindow *win, ARegion *ar)
1186 {
1187         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
1188                 if (ar->swinid)
1189                         wm_subwindow_close(win, ar->swinid);
1190                 ar->swinid = 0;
1191         }
1192         else if (ar->swinid == 0)
1193                 ar->swinid = wm_subwindow_open(win, &ar->winrct);
1194         else 
1195                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
1196 }
1197
1198 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
1199 {
1200         /* note, add-handler checks if it already exists */
1201         
1202         /* XXX it would be good to have boundbox checks for some of these... */
1203         if (flag & ED_KEYMAP_UI) {
1204                 /* user interface widgets */
1205                 UI_add_region_handlers(handlers);
1206         }
1207         if (flag & ED_KEYMAP_VIEW2D) {
1208                 /* 2d-viewport handling+manipulation */
1209                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
1210                 WM_event_add_keymap_handler(handlers, keymap);
1211         }
1212         if (flag & ED_KEYMAP_MARKERS) {
1213                 /* time-markers */
1214                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
1215                 
1216                 /* time space only has this keymap, the others get a boundbox restricted map */
1217                 if (sa->spacetype != SPACE_TIME) {
1218                         ARegion *ar;
1219                         static rcti rect = {0, 10000, 0, 30};    /* same local check for all areas */
1220                         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
1221                         if (ar) {
1222                                 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
1223                         }
1224                 }
1225                 else
1226                         WM_event_add_keymap_handler(handlers, keymap);
1227         }
1228         if (flag & ED_KEYMAP_ANIMATION) {
1229                 /* frame changing and timeline operators (for time spaces) */
1230                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
1231                 WM_event_add_keymap_handler(handlers, keymap);
1232         }
1233         if (flag & ED_KEYMAP_FRAMES) {
1234                 /* frame changing/jumping (for all spaces) */
1235                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
1236                 WM_event_add_keymap_handler(handlers, keymap);
1237         }
1238         if (flag & ED_KEYMAP_GPENCIL) {
1239                 /* grease pencil */
1240                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
1241                 WM_event_add_keymap_handler(handlers, keymap);
1242         }
1243         if (flag & ED_KEYMAP_HEADER) {
1244                 /* standard keymap for headers regions */
1245                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Header", 0, 0);
1246                 WM_event_add_keymap_handler(handlers, keymap);
1247         }
1248 }
1249
1250
1251 /* called in screen_refresh, or screens_init, also area size changes */
1252 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
1253 {
1254         ARegion *ar;
1255         rcti rect;
1256         
1257         /* set typedefinitions */
1258         sa->type = BKE_spacetype_from_id(sa->spacetype);
1259         
1260         if (sa->type == NULL) {
1261                 sa->butspacetype = sa->spacetype = SPACE_VIEW3D;
1262                 sa->type = BKE_spacetype_from_id(sa->spacetype);
1263         }
1264         
1265         for (ar = sa->regionbase.first; ar; ar = ar->next)
1266                 ar->type = BKE_regiontype_from_id(sa->type, ar->regiontype);
1267         
1268         /* area sizes */
1269         area_calc_totrct(sa, WM_window_pixels_x(win), WM_window_pixels_y(win));
1270         
1271         /* clear all azones, add the area triange widgets */
1272         area_azone_initialize(win->screen, sa);
1273
1274         /* region rect sizes */
1275         rect = sa->totrct;
1276         region_rect_recursive(win, sa, sa->regionbase.first, &rect, 0);
1277         
1278         /* default area handlers */
1279         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
1280         /* checks spacedata, adds own handlers */
1281         if (sa->type->init)
1282                 sa->type->init(wm, sa);
1283         
1284         /* region windows, default and own handlers */
1285         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1286                 region_subwindow(win, ar);
1287                 
1288                 if (ar->swinid) {
1289                         /* default region handlers */
1290                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
1291                         /* own handlers */
1292                         if (ar->type->init)
1293                                 ar->type->init(wm, ar);
1294                 }
1295                 else {
1296                         /* prevent uiblocks to run */
1297                         uiFreeBlocks(NULL, &ar->uiblocks);
1298                 }
1299         }
1300 }
1301
1302 /* externally called for floating regions like menus */
1303 void ED_region_init(bContext *C, ARegion *ar)
1304 {
1305 //      ARegionType *at = ar->type;
1306         
1307         /* refresh can be called before window opened */
1308         region_subwindow(CTX_wm_window(C), ar);
1309         
1310         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1311         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1312         
1313         /* v2d mask is used to subtract scrollbars from a 2d view. Needs initialize here. */
1314         BLI_rcti_init(&ar->v2d.mask, 0, ar->winx - 1, 0, ar->winy -1);
1315
1316         /* UI convention */
1317         wmOrtho2(-0.01f, ar->winx - 0.01f, -0.01f, ar->winy - 0.01f);
1318         glLoadIdentity();
1319 }
1320
1321 /* for quick toggle, can skip fades */
1322 void region_toggle_hidden(bContext *C, ARegion *ar, int do_fade)
1323 {
1324         ScrArea *sa = CTX_wm_area(C);
1325         
1326         ar->flag ^= RGN_FLAG_HIDDEN;
1327         
1328         if (do_fade && ar->overlap) {
1329                 /* starts a timer, and in end calls the stuff below itself (region_sblend_invoke()) */
1330                 region_blend_start(C, sa, ar);
1331         }
1332         else {
1333                 if (ar->flag & RGN_FLAG_HIDDEN)
1334                         WM_event_remove_handlers(C, &ar->handlers);
1335                 
1336                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1337                 ED_area_tag_redraw(sa);
1338         }
1339 }
1340
1341 /* exported to all editors, uses fading default */
1342 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
1343 {
1344         region_toggle_hidden(C, ar, 1);
1345 }
1346
1347 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
1348 /* area vertices were set */
1349 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
1350 {
1351         SpaceType *st;
1352         ARegion *ar;
1353         int spacetype = sa1->spacetype;
1354         
1355         sa1->headertype = sa2->headertype;
1356         sa1->spacetype = sa2->spacetype;
1357         sa1->type = sa2->type;
1358         sa1->butspacetype = sa2->butspacetype;
1359         
1360         if (swap_space == 1) {
1361                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
1362                 /* exception: ensure preview is reset */
1363 //              if (sa1->spacetype == SPACE_VIEW3D)
1364 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
1365         }
1366         else if (swap_space == 2) {
1367                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1368         }
1369         else {
1370                 BKE_spacedata_freelist(&sa1->spacedata);
1371                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
1372         }
1373         
1374         /* Note; SPACE_EMPTY is possible on new screens */
1375         
1376         /* regions */
1377         if (swap_space == 1) {
1378                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
1379         }
1380         else {
1381                 if (swap_space < 2) {
1382                         st = BKE_spacetype_from_id(spacetype);
1383                         for (ar = sa1->regionbase.first; ar; ar = ar->next)
1384                                 BKE_area_region_free(st, ar);
1385                         BLI_freelistN(&sa1->regionbase);
1386                 }
1387                 
1388                 st = BKE_spacetype_from_id(sa2->spacetype);
1389                 for (ar = sa2->regionbase.first; ar; ar = ar->next) {
1390                         ARegion *newar = BKE_area_region_copy(st, ar);
1391                         BLI_addtail(&sa1->regionbase, newar);
1392                 }
1393         }
1394 }
1395
1396 /* *********** Space switching code *********** */
1397
1398 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1399 {
1400         ScrArea *tmp = MEM_callocN(sizeof(ScrArea), "addscrarea");
1401
1402         ED_area_exit(C, sa1);
1403         ED_area_exit(C, sa2);
1404
1405         area_copy_data(tmp, sa1, 2);
1406         area_copy_data(sa1, sa2, 0);
1407         area_copy_data(sa2, tmp, 0);
1408         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1409         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1410
1411         BKE_screen_area_free(tmp);
1412         MEM_freeN(tmp);
1413
1414         /* tell WM to refresh, cursor types etc */
1415         WM_event_add_mousemove(C);
1416         
1417         ED_area_tag_redraw(sa1);
1418         ED_area_tag_refresh(sa1);
1419         ED_area_tag_redraw(sa2);
1420         ED_area_tag_refresh(sa2);
1421 }
1422
1423 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
1424 {
1425         if (sa->spacetype != type) {
1426                 SpaceType *st;
1427                 SpaceLink *slold;
1428                 SpaceLink *sl;
1429
1430                 ED_area_exit(C, sa);
1431
1432                 st = BKE_spacetype_from_id(type);
1433                 slold = sa->spacedata.first;
1434
1435                 sa->spacetype = type;
1436                 sa->butspacetype = type;
1437                 sa->type = st;
1438                 
1439                 /* check previously stored space */
1440                 for (sl = sa->spacedata.first; sl; sl = sl->next)
1441                         if (sl->spacetype == type)
1442                                 break;
1443                 
1444                 /* old spacedata... happened during work on 2.50, remove */
1445                 if (sl && sl->regionbase.first == NULL) {
1446                         st->free(sl);
1447                         BLI_freelinkN(&sa->spacedata, sl);
1448                         if (slold == sl) {
1449                                 slold = NULL;
1450                         }
1451                         sl = NULL;
1452                 }
1453                 
1454                 if (sl) {
1455                         /* swap regions */
1456                         slold->regionbase = sa->regionbase;
1457                         sa->regionbase = sl->regionbase;
1458                         sl->regionbase.first = sl->regionbase.last = NULL;
1459                         
1460                         /* put in front of list */
1461                         BLI_remlink(&sa->spacedata, sl);
1462                         BLI_addhead(&sa->spacedata, sl);
1463                 }
1464                 else {
1465                         /* new space */
1466                         if (st) {
1467                                 sl = st->new(C);
1468                                 BLI_addhead(&sa->spacedata, sl);
1469                                 
1470                                 /* swap regions */
1471                                 if (slold)
1472                                         slold->regionbase = sa->regionbase;
1473                                 sa->regionbase = sl->regionbase;
1474                                 sl->regionbase.first = sl->regionbase.last = NULL;
1475                         }
1476                 }
1477                 
1478                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1479                 
1480                 /* tell WM to refresh, cursor types etc */
1481                 WM_event_add_mousemove(C);
1482                                 
1483                 /* send space change notifier */
1484                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1485                 
1486                 ED_area_tag_refresh(sa);
1487         }
1488         
1489         /* also redraw when re-used */
1490         ED_area_tag_redraw(sa);
1491 }
1492
1493 void ED_area_prevspace(bContext *C, ScrArea *sa)
1494 {
1495         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1496
1497         if (sl && sl->next) {
1498                 /* workaround for case of double prevspace, render window
1499                  * with a file browser on top of it */
1500                 if (sl->next->spacetype == SPACE_FILE && sl->next->next)
1501                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1502                 else
1503                         ED_area_newspace(C, sa, sl->next->spacetype);
1504         }
1505         else {
1506                 /* no change */
1507                 return;
1508         }
1509         ED_area_tag_redraw(sa);
1510
1511         /* send space change notifier */
1512         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1513 }
1514
1515 /* returns offset for next button in header */
1516 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1517 {
1518         ScrArea *sa = CTX_wm_area(C);
1519         bScreen *scr = CTX_wm_screen(C);
1520         PointerRNA areaptr;
1521         int xco = 0.4 * U.widget_unit;
1522
1523         RNA_pointer_create(&(scr->id), &RNA_Area, sa, &areaptr);
1524
1525         uiDefButR(block, MENU, 0, NULL, xco, yco, 1.5 * U.widget_unit, U.widget_unit,
1526                   &areaptr, "type", 0, 0.0f, 0.0f, 0.0f, 0.0f, "");
1527
1528         return xco + 1.7 * U.widget_unit;
1529 }
1530
1531 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1532 {
1533         ScrArea *sa = CTX_wm_area(C);
1534         int xco = 0.4 * U.widget_unit;
1535         uiBut *but;
1536         
1537         if (!sa->full)
1538                 xco = ED_area_header_switchbutton(C, block, yco);
1539
1540         uiBlockSetEmboss(block, UI_EMBOSSN);
1541
1542         if (sa->flag & HEADER_NO_PULLDOWN) {
1543                 but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1544                                        ICON_DISCLOSURE_TRI_RIGHT,
1545                                        xco, yco, U.widget_unit, U.widget_unit * 0.9f,
1546                                        &(sa->flag), 0, 0, 0, 0,
1547                                        TIP_("Show pulldown menus"));
1548         }
1549         else {
1550                 but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
1551                                        ICON_DISCLOSURE_TRI_DOWN,
1552                                        xco, yco, U.widget_unit, U.widget_unit * 0.9f,
1553                                        &(sa->flag), 0, 0, 0, 0,
1554                                        TIP_("Hide pulldown menus"));
1555         }
1556
1557         uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
1558
1559         uiBlockSetEmboss(block, UI_EMBOSS);
1560         
1561         return xco + U.widget_unit;
1562 }
1563
1564 /************************ standard UI regions ************************/
1565
1566 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1567 {
1568         ScrArea *sa = CTX_wm_area(C);
1569         uiStyle *style = UI_GetStyleDraw();
1570         uiBlock *block;
1571         PanelType *pt;
1572         Panel *panel;
1573         View2D *v2d = &ar->v2d;
1574         View2DScrollers *scrollers;
1575         int x, y, xco, yco, w, em, triangle;
1576         bool is_context_new = 0;
1577         int redo;
1578         int scroll;
1579
1580         bool use_category_tabs = (ar->regiontype == RGN_TYPE_TOOLS);  /* XXX, should use some better check? */
1581         /* offset panels for small vertical tab area */
1582         const char *category = NULL;
1583         const int category_tabs_width = UI_PANEL_CATEGORY_MARGIN_WIDTH;
1584         int margin_x = 0;
1585
1586         BLI_SMALLSTACK_DECLARE(pt_stack, PanelType *);
1587
1588         if (contextnr >= 0)
1589                 is_context_new = UI_view2d_tab_set(v2d, contextnr);
1590         
1591         /* before setting the view */
1592         if (vertical) {
1593                 /* only allow scrolling in vertical direction */
1594                 v2d->keepofs |= V2D_LOCKOFS_X | V2D_KEEPOFS_Y;
1595                 v2d->keepofs &= ~(V2D_LOCKOFS_Y | V2D_KEEPOFS_X);
1596                 v2d->scroll &= ~(V2D_SCROLL_BOTTOM);
1597                 v2d->scroll |= (V2D_SCROLL_RIGHT);
1598         }
1599         else {
1600                 /* for now, allow scrolling in both directions (since layouts are optimized for vertical,
1601                  * they often don't fit in horizontal layout)
1602                  */
1603                 v2d->keepofs &= ~(V2D_LOCKOFS_X | V2D_LOCKOFS_Y | V2D_KEEPOFS_X | V2D_KEEPOFS_Y);
1604                 v2d->scroll |= (V2D_SCROLL_BOTTOM);
1605                 v2d->scroll &= ~(V2D_SCROLL_RIGHT);
1606         }
1607
1608         scroll = v2d->scroll;
1609
1610
1611         /* collect panels to draw */
1612         for (pt = ar->type->paneltypes.last; pt; pt = pt->prev) {
1613                 /* verify context */
1614                 if (context && pt->context[0] && !STREQ(context, pt->context)) {
1615                         continue;
1616                 }
1617
1618                 /* draw panel */
1619                 if (pt->draw && (!pt->poll || pt->poll(C, pt))) {
1620                         BLI_SMALLSTACK_PUSH(pt_stack, pt);
1621                 }
1622         }
1623
1624
1625         /* collect categories */
1626         if (use_category_tabs) {
1627                 UI_panel_category_clear_all(ar);
1628
1629                 /* gather unique categories */
1630                 BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
1631                 {
1632                         if (pt->category[0]) {
1633                                 if (!UI_panel_category_find(ar, pt->category)) {
1634                                         UI_panel_category_add(ar, pt->category);
1635                                 }
1636                         }
1637                 }
1638                 BLI_SMALLSTACK_ITER_END;
1639
1640                 if (!UI_panel_category_is_visible(ar)) {
1641                         use_category_tabs = false;
1642                 }
1643                 else {
1644                         category = UI_panel_category_active_get(ar, true);
1645                         margin_x = category_tabs_width;
1646                 }
1647         }
1648
1649
1650         /* sortof hack - but we cannot predict the height of panels, until it's being generated */
1651         /* the layout engine works with fixed width (from v2d->cur), which is being set at end of the loop */
1652         /* in case scroller settings (hide flags) differ from previous, the whole loop gets done again */
1653         for (redo = 2; redo > 0; redo--) {
1654                 
1655                 if (vertical) {
1656                         w = BLI_rctf_size_x(&v2d->cur);
1657                         em = (ar->type->prefsizex) ? 10 : 20; /* works out to 10*UI_UNIT_X or 20*UI_UNIT_X */
1658                 }
1659                 else {
1660                         w = UI_PANEL_WIDTH;
1661                         em = (ar->type->prefsizex) ? 10 : 20;
1662                 }
1663
1664                 w -= margin_x;
1665                 
1666                 /* create panels */
1667                 uiBeginPanels(C, ar);
1668
1669                 /* set view2d view matrix  - uiBeginBlock() stores it */
1670                 UI_view2d_view_ortho(v2d);
1671
1672                 BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
1673                 {
1674                         bool open;
1675
1676                         panel = uiPanelFindByType(ar, pt);
1677
1678                         if (use_category_tabs && pt->category[0] && !STREQ(category, pt->category)) {
1679                                 if ((panel == NULL) || ((panel->flag & PNL_PIN) == 0)) {
1680                                         continue;
1681                                 }
1682                         }
1683
1684                         /* draw panel */
1685                         block = uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1686                         panel = uiBeginPanel(sa, ar, block, pt, panel, &open);
1687
1688                         /* bad fixed values */
1689                         triangle = (int)(UI_UNIT_Y * 1.1f);
1690
1691                         if (pt->draw_header && !(pt->flag & PNL_NO_HEADER) && (open || vertical)) {
1692                                 /* for enabled buttons */
1693                                 panel->layout = uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1694                                                               triangle, (UI_UNIT_Y * 1.1f) + style->panelspace, UI_UNIT_Y, 1, style);
1695
1696                                 pt->draw_header(C, panel);
1697
1698                                 uiBlockLayoutResolve(block, &xco, &yco);
1699                                 panel->labelofs = xco - triangle;
1700                                 panel->layout = NULL;
1701                         }
1702                         else {
1703                                 panel->labelofs = 0;
1704                         }
1705
1706                         if (open) {
1707                                 short panelContext;
1708
1709                                 /* panel context can either be toolbar region or normal panels region */
1710                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1711                                         panelContext = UI_LAYOUT_TOOLBAR;
1712                                 else
1713                                         panelContext = UI_LAYOUT_PANEL;
1714
1715                                 panel->layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1716                                                               style->panelspace, 0, w - 2 * style->panelspace, em, style);
1717
1718                                 pt->draw(C, panel);
1719
1720                                 uiBlockLayoutResolve(block, &xco, &yco);
1721                                 panel->layout = NULL;
1722
1723                                 yco -= 2 * style->panelspace;
1724                                 uiEndPanel(block, w, -yco);
1725                         }
1726                         else {
1727                                 yco = 0;
1728                                 uiEndPanel(block, w, 0);
1729                         }
1730
1731                         uiEndBlock(C, block);
1732                 }
1733                 BLI_SMALLSTACK_ITER_END;
1734
1735                 /* align panels and return size */
1736                 uiEndPanels(C, ar, &x, &y);
1737                 
1738                 /* before setting the view */
1739                 if (vertical) {
1740                         /* we always keep the scroll offset - so the total view gets increased with the scrolled away part */
1741                         if (v2d->cur.ymax < - 0.001f)
1742                                 y = min_ii(y, v2d->cur.ymin);
1743                         
1744                         y = -y;
1745                 }
1746                 else {
1747                         /* don't jump back when panels close or hide */
1748                         if (!is_context_new)
1749                                 x = max_ii(x, v2d->cur.xmax);
1750                         y = -y;
1751                 }
1752                 
1753                 /* this also changes the 'cur' */
1754                 UI_view2d_totRect_set(v2d, x, y);
1755                 
1756                 if (scroll != v2d->scroll) {
1757                         /* Note: this code scales fine, but because of rounding differences, positions of elements
1758                          * flip +1 or -1 pixel compared to redoing the entire layout again.
1759                          * Leaving in commented code for future tests */
1760 #if 0
1761                         uiScalePanels(ar, BLI_rctf_size_x(&v2d->cur));
1762                         break;
1763 #endif
1764                 }
1765                 else {
1766                         break;
1767                 }
1768         }
1769         
1770         BLI_SMALLSTACK_FREE(pt_stack);
1771         
1772         /* clear */
1773         if (ar->overlap) {
1774                 /* view should be in pixelspace */
1775                 UI_view2d_view_restore(C);
1776                 glEnable(GL_BLEND);
1777                 UI_ThemeColor4((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
1778                 glRecti(0, 0, BLI_rcti_size_x(&ar->winrct), BLI_rcti_size_y(&ar->winrct));
1779                 glDisable(GL_BLEND);
1780         }
1781         else {
1782                 UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
1783                 glClear(GL_COLOR_BUFFER_BIT);
1784         }
1785         
1786
1787         /* set the view */
1788         UI_view2d_view_ortho(v2d);
1789
1790         /* draw panels */
1791         uiDrawPanels(C, ar);
1792
1793         /* restore view matrix */
1794         UI_view2d_view_restore(C);
1795         
1796         if (use_category_tabs) {
1797                 UI_panel_category_draw_all(ar, category);
1798         }
1799
1800         /* scrollers */
1801         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1802         UI_view2d_scrollers_draw(C, v2d, scrollers);
1803         UI_view2d_scrollers_free(scrollers);
1804 }
1805
1806 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1807 {
1808         wmKeyMap *keymap;
1809
1810         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1811
1812         keymap = WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1813         WM_event_add_keymap_handler(&ar->handlers, keymap);
1814 }
1815
1816 void ED_region_header(const bContext *C, ARegion *ar)
1817 {
1818         uiStyle *style = UI_GetStyleDraw();
1819         uiBlock *block;
1820         uiLayout *layout;
1821         HeaderType *ht;
1822         Header header = {NULL};
1823         int maxco, xco, yco;
1824         int headery = ED_area_headersize();
1825
1826         /* clear */
1827         UI_ThemeClearColor((ED_screen_area_active(C)) ? TH_HEADER : TH_HEADERDESEL);
1828         glClear(GL_COLOR_BUFFER_BIT);
1829         
1830         /* set view2d view matrix for scrolling (without scrollers) */
1831         UI_view2d_view_ortho(&ar->v2d);
1832
1833         xco = maxco = 0.4f * UI_UNIT_X;
1834         yco = headery - floor(0.2f * UI_UNIT_Y);
1835
1836         /* draw all headers types */
1837         for (ht = ar->type->headertypes.first; ht; ht = ht->next) {
1838                 block = uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1839                 layout = uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, UI_UNIT_Y, 1, style);
1840
1841                 if (ht->draw) {
1842                         header.type = ht;
1843                         header.layout = layout;
1844                         ht->draw(C, &header);
1845                         
1846                         /* for view2d */
1847                         xco = uiLayoutGetWidth(layout);
1848                         if (xco > maxco)
1849                                 maxco = xco;
1850                 }
1851
1852                 uiBlockLayoutResolve(block, &xco, &yco);
1853                 
1854                 /* for view2d */
1855                 if (xco > maxco)
1856                         maxco = xco;
1857                 
1858                 uiEndBlock(C, block);
1859                 uiDrawBlock(C, block);
1860         }
1861
1862         /* always as last  */
1863         UI_view2d_totRect_set(&ar->v2d, maxco + UI_UNIT_X + 80, headery);
1864
1865         /* restore view matrix? */
1866         UI_view2d_view_restore(C);
1867 }
1868
1869 void ED_region_header_init(ARegion *ar)
1870 {
1871         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1872 }
1873
1874 /* UI_UNIT_Y is defined as U variable now, depending dpi */
1875 int ED_area_headersize(void)
1876 {
1877         return (int)(HEADERY * UI_DPI_FAC);
1878 }
1879
1880 void ED_region_info_draw(ARegion *ar, const char *text, int block, float fill_color[4])
1881 {
1882         const int header_height = UI_UNIT_Y;
1883         uiStyle *style = UI_GetStyleDraw();
1884         int fontid = style->widget.uifont_id;
1885         GLint scissor[4];
1886         rcti rect;
1887
1888         /* background box */
1889         ED_region_visible_rect(ar, &rect);
1890         rect.ymin = BLI_rcti_size_y(&ar->winrct) - header_height;
1891
1892         /* box fill entire width or just around text */
1893         if (!block)
1894                 rect.xmax = min_ii(rect.xmax, rect.xmin + BLF_width(fontid, text, BLF_DRAW_STR_DUMMY_MAX) + 1.2f * U.widget_unit);
1895
1896         rect.ymax = BLI_rcti_size_y(&ar->winrct);
1897
1898         /* setup scissor */
1899         glGetIntegerv(GL_SCISSOR_BOX, scissor);
1900         glScissor(ar->winrct.xmin + rect.xmin, ar->winrct.ymin + rect.ymin,
1901                   BLI_rcti_size_x(&rect) + 1, BLI_rcti_size_y(&rect) + 1);
1902
1903         glEnable(GL_BLEND);
1904         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1905         glColor4fv(fill_color);
1906         glRecti(rect.xmin, rect.ymin, rect.xmax + 1, rect.ymax + 1);
1907         glDisable(GL_BLEND);
1908
1909         /* text */
1910         UI_ThemeColor(TH_TEXT_HI);
1911         BLF_clipping(fontid, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
1912         BLF_enable(fontid, BLF_CLIPPING);
1913         BLF_position(fontid, rect.xmin + 0.6f * U.widget_unit, rect.ymin + 0.3f * U.widget_unit, 0.0f);
1914
1915         BLF_draw(fontid, text, BLF_DRAW_STR_DUMMY_MAX);
1916
1917         BLF_disable(fontid, BLF_CLIPPING);
1918
1919         /* restore scissor as it was before */
1920         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
1921 }
1922
1923 void ED_region_grid_draw(ARegion *ar, float zoomx, float zoomy)
1924 {
1925         float gridsize, gridstep = 1.0f / 32.0f;
1926         float fac, blendfac;
1927         int x1, y1, x2, y2;
1928
1929         /* the image is located inside (0, 0), (1, 1) as set by view2d */
1930         UI_ThemeColorShade(TH_BACK, 20);
1931
1932         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
1933         UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
1934         glRectf(x1, y1, x2, y2);
1935
1936         /* gridsize adapted to zoom level */
1937         gridsize = 0.5f * (zoomx + zoomy);
1938         if (gridsize <= 0.0f)
1939                 return;
1940
1941         if (gridsize < 1.0f) {
1942                 while (gridsize < 1.0f) {
1943                         gridsize *= 4.0f;
1944                         gridstep *= 4.0f;
1945                 }
1946         }
1947         else {
1948                 while (gridsize >= 4.0f) {
1949                         gridsize /= 4.0f;
1950                         gridstep /= 4.0f;
1951                 }
1952         }
1953
1954         /* the fine resolution level */
1955         blendfac = 0.25f * gridsize - floorf(0.25f * gridsize);
1956         CLAMP(blendfac, 0.0f, 1.0f);
1957         UI_ThemeColorShade(TH_BACK, (int)(20.0f * (1.0f - blendfac)));
1958
1959         fac = 0.0f;
1960         glBegin(GL_LINES);
1961         while (fac < 1.0f) {
1962                 glVertex2f(x1, y1 * (1.0f - fac) + y2 * fac);
1963                 glVertex2f(x2, y1 * (1.0f - fac) + y2 * fac);
1964                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y1);
1965                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y2);
1966                 fac += gridstep;
1967         }
1968
1969         /* the large resolution level */
1970         UI_ThemeColor(TH_BACK);
1971
1972         fac = 0.0f;
1973         while (fac < 1.0f) {
1974                 glVertex2f(x1, y1 * (1.0f - fac) + y2 * fac);
1975                 glVertex2f(x2, y1 * (1.0f - fac) + y2 * fac);
1976                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y1);
1977                 glVertex2f(x1 * (1.0f - fac) + x2 * fac, y2);
1978                 fac += 4.0f * gridstep;
1979         }
1980         glEnd();
1981 }
1982
1983 /* If the area has overlapping regions, it returns visible rect for Region *ar */
1984 /* rect gets returned in local region coordinates */
1985 void ED_region_visible_rect(ARegion *ar, rcti *rect)
1986 {
1987         ARegion *arn = ar;
1988         
1989         /* allow function to be called without area */
1990         while (arn->prev)
1991                 arn = arn->prev;
1992         
1993         *rect = ar->winrct;
1994         
1995         /* check if a region overlaps with the current one */
1996         for (; arn; arn = arn->next) {
1997                 if (ar != arn && arn->overlap) {
1998                         if (BLI_rcti_isect(rect, &arn->winrct, NULL)) {
1999                                 
2000                                 /* overlap left, also check 1 pixel offset (2 regions on one side) */
2001                                 if (ABS(rect->xmin - arn->winrct.xmin) < 2)
2002                                         rect->xmin = arn->winrct.xmax;
2003
2004                                 /* overlap right */
2005                                 if (ABS(rect->xmax - arn->winrct.xmax) < 2)
2006                                         rect->xmax = arn->winrct.xmin;
2007                         }
2008                 }
2009         }
2010         BLI_rcti_translate(rect, -ar->winrct.xmin, -ar->winrct.ymin);
2011 }
2012