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