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