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