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