Fix issue with hidden headers after previous commit.
[blender.git] / source / blender / editors / screen / area.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/screen/area.c
28  *  \ingroup edscr
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_userdef_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_linklist_stack.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_workspace.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_message.h"
55
56 #include "ED_screen.h"
57 #include "ED_screen_types.h"
58 #include "ED_space_api.h"
59
60 #include "GPU_immediate.h"
61 #include "GPU_immediate_util.h"
62 #include "GPU_matrix.h"
63 #include "GPU_draw.h"
64
65 #include "BLF_api.h"
66
67 #include "IMB_imbuf.h"
68 #include "IMB_imbuf_types.h"
69 #include "IMB_metadata.h"
70
71 #include "UI_interface.h"
72 #include "UI_interface_icons.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "screen_intern.h"
77
78 enum RegionEmbossSide {
79         REGION_EMBOSS_LEFT   = (1 << 0),
80         REGION_EMBOSS_TOP    = (1 << 1),
81         REGION_EMBOSS_BOTTOM = (1 << 2),
82         REGION_EMBOSS_RIGHT  = (1 << 3),
83         REGION_EMBOSS_ALL    = REGION_EMBOSS_LEFT | REGION_EMBOSS_TOP | REGION_EMBOSS_RIGHT | REGION_EMBOSS_BOTTOM,
84 };
85
86 /* general area and region code */
87
88 static void region_draw_emboss(const ARegion *ar, const rcti *scirct, int sides)
89 {
90         rcti rect;
91
92         /* translate scissor rect to region space */
93         rect.xmin = scirct->xmin - ar->winrct.xmin;
94         rect.ymin = scirct->ymin - ar->winrct.ymin;
95         rect.xmax = scirct->xmax - ar->winrct.xmin;
96         rect.ymax = scirct->ymax - ar->winrct.ymin;
97
98         /* set transp line */
99         glEnable(GL_BLEND);
100         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
101
102         float color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
103         UI_GetThemeColor3fv(TH_EDITOR_OUTLINE, color);
104
105         Gwn_VertFormat *format = immVertexFormat();
106         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
107         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
108         immUniformColor4fv(color);
109
110         immBeginAtMost(GWN_PRIM_LINES, 8);
111
112         /* right */
113         if (sides & REGION_EMBOSS_RIGHT) {
114                 immVertex2f(pos, rect.xmax, rect.ymax);
115                 immVertex2f(pos, rect.xmax, rect.ymin);
116         }
117
118         /* bottom */
119         if (sides & REGION_EMBOSS_BOTTOM) {
120                 immVertex2f(pos, rect.xmax, rect.ymin);
121                 immVertex2f(pos, rect.xmin, rect.ymin);
122         }
123
124         /* left */
125         if (sides & REGION_EMBOSS_LEFT) {
126                 immVertex2f(pos, rect.xmin, rect.ymin);
127                 immVertex2f(pos, rect.xmin, rect.ymax);
128         }
129
130         /* top */
131         if (sides & REGION_EMBOSS_TOP) {
132                 immVertex2f(pos, rect.xmin, rect.ymax);
133                 immVertex2f(pos, rect.xmax, rect.ymax);
134         }
135
136         immEnd();
137         immUnbindProgram();
138
139         glDisable(GL_BLEND);
140         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 }
142
143 void ED_region_pixelspace(ARegion *ar)
144 {
145         wmOrtho2_region_pixelspace(ar);
146         gpuLoadIdentity();
147 }
148
149 /* only exported for WM */
150 void ED_region_do_listen(bScreen *sc, ScrArea *sa, ARegion *ar, wmNotifier *note, const Scene *scene)
151 {
152         /* generic notes first */
153         switch (note->category) {
154                 case NC_WM:
155                         if (note->data == ND_FILEREAD)
156                                 ED_region_tag_redraw(ar);
157                         break;
158                 case NC_WINDOW:
159                         ED_region_tag_redraw(ar);
160                         break;
161         }
162
163         if (ar->type && ar->type->listener)
164                 ar->type->listener(sc, sa, ar, note, scene);
165 }
166
167 /* only exported for WM */
168 void ED_area_do_listen(bScreen *sc, ScrArea *sa, wmNotifier *note, Scene *scene, WorkSpace *workspace)
169 {
170         /* no generic notes? */
171         if (sa->type && sa->type->listener) {
172                 sa->type->listener(sc, sa, note, scene, workspace);
173         }
174 }
175
176 /* only exported for WM */
177 void ED_area_do_refresh(bContext *C, ScrArea *sa)
178 {
179         /* no generic notes? */
180         if (sa->type && sa->type->refresh) {
181                 sa->type->refresh(C, sa);
182         }
183         sa->do_refresh = false;
184 }
185
186 /**
187  * Action zones are only updated if the mouse is inside of them, but in some cases (currently only fullscreen icon)
188  * it might be needed to update their properties and redraw if the mouse isn't inside.
189  */
190 void ED_area_azones_update(ScrArea *sa, const int mouse_xy[2])
191 {
192         AZone *az;
193         bool changed = false;
194
195         for (az = sa->actionzones.first; az; az = az->next) {
196                 if (az->type == AZONE_FULLSCREEN) {
197                         /* only if mouse is not hovering the azone */
198                         if (BLI_rcti_isect_pt_v(&az->rect, mouse_xy) == false) {
199                                 az->alpha = 0.0f;
200                                 changed = true;
201
202                                 /* can break since currently only this is handled here */
203                                 break;
204                         }
205                 }
206         }
207
208         if (changed) {
209                 sa->flag &= ~AREA_FLAG_ACTIONZONES_UPDATE;
210                 ED_area_tag_redraw(sa);
211         }
212 }
213
214 /**
215  * \brief Corner widget use for quitting fullscreen.
216  */
217 static void area_draw_azone_fullscreen(short x1, short y1, short x2, short y2, float alpha)
218 {
219         int x = x2 - ((float) x2 - x1) * 0.5f / UI_DPI_FAC;
220         int y = y2 - ((float) y2 - y1) * 0.5f / UI_DPI_FAC;
221
222         /* adjust the icon distance from the corner */
223         x += 36.0f / UI_DPI_FAC;
224         y += 36.0f / UI_DPI_FAC;
225
226         /* draws from the left bottom corner of the icon */
227         x -= UI_DPI_ICON_SIZE;
228         y -= UI_DPI_ICON_SIZE;
229
230         alpha = min_ff(alpha, 0.75f);
231
232         UI_icon_draw_aspect(x, y, ICON_FULLSCREEN_EXIT, 0.7f / UI_DPI_FAC, alpha);
233
234         /* debug drawing :
235          * The click_rect is the same as defined in fullscreen_click_rcti_init
236          * Keep them both in sync */
237
238         if (G.debug_value == 1) {
239                 rcti click_rect;
240                 float icon_size = UI_DPI_ICON_SIZE + 7 * UI_DPI_FAC;
241                 unsigned char alpha_debug = 255 * alpha;
242
243                 BLI_rcti_init(&click_rect, x, x + icon_size, y, y + icon_size);
244
245                 Gwn_VertFormat *format = immVertexFormat();
246                 unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
247                 unsigned int color = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
248
249                 immAttrib4ub(color, 255, 0, 0, alpha_debug);
250                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
251                 imm_draw_box_wire_2d(pos, click_rect.xmin, click_rect.ymin, click_rect.xmax, click_rect.ymax);
252
253                 immAttrib4ub(color, 0, 255, 255, alpha_debug);
254                 immBegin(GWN_PRIM_LINES, 4);
255                 immVertex2f(pos, click_rect.xmin, click_rect.ymin);
256                 immVertex2f(pos, click_rect.xmax, click_rect.ymax);
257                 immVertex2f(pos, click_rect.xmin, click_rect.ymax);
258                 immVertex2f(pos, click_rect.xmax, click_rect.ymin);
259                 immEnd();
260
261                 immUnbindProgram();
262         }
263 }
264
265 /**
266  * \brief Corner widgets use for dragging and splitting the view.
267  */
268 static void area_draw_azone(short UNUSED(x1), short UNUSED(y1), short UNUSED(x2), short UNUSED(y2))
269 {
270         /* No drawing needed since all corners are action zone, and visually distinguishable. */
271 }
272
273 static void region_draw_azone_icon(AZone *az)
274 {
275         float midx = az->x1 + (az->x2 - az->x1) * 0.5f;
276         float midy = az->y1 + (az->y2 - az->y1) * 0.5f;
277
278         Gwn_VertFormat *format = immVertexFormat();
279         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
280
281         /* outlined circle */
282         GPU_enable_program_point_size(); /* TODO: make a fixed-size shader to avoid this */
283         immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA);
284         immUniformColor4f(1.0f, 1.0f, 1.0f, 0.8f);
285         immUniform4f("outlineColor", 0.2f, 0.2f, 0.2f, 0.9f);
286         immUniform1f("outlineWidth", 1.0f);
287         immUniform1f("size", 9.5f);
288         immBegin(GWN_PRIM_POINTS, 1);
289         immVertex2f(pos, midx, midy);
290         immEnd();
291         immUnbindProgram();
292         GPU_disable_program_point_size();
293
294         /* + */
295         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
296         immUniformColor4f(0.2f, 0.2f, 0.2f, 0.9f);
297         immBegin(GWN_PRIM_LINES, 4);
298         immVertex2f(pos, midx, midy - 2);
299         immVertex2f(pos, midx, midy + 3);
300         immVertex2f(pos, midx - 2, midy);
301         immVertex2f(pos, midx + 3, midy);
302         immEnd();
303         immUnbindProgram();
304 }
305
306 static void draw_azone_plus(float x1, float y1, float x2, float y2)
307 {
308         float width = 0.1f * U.widget_unit;
309         float pad = 0.2f * U.widget_unit;
310
311         Gwn_VertFormat *format = immVertexFormat();
312         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
313
314         glEnable(GL_BLEND);
315         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
316         immUniformColor4f(0.8f, 0.8f, 0.8f, 0.4f);
317
318         immRectf(pos, (x1 + x2 - width) * 0.5f, y1 + pad, (x1 + x2 + width) * 0.5f, y2 - pad);
319         immRectf(pos, x1 + pad, (y1 + y2 - width) * 0.5f, (x1 + x2 - width) * 0.5f, (y1 + y2 + width) * 0.5f);
320         immRectf(pos, (x1 + x2 + width) * 0.5f, (y1 + y2 - width) * 0.5f, x2 - pad, (y1 + y2 + width) * 0.5f);
321
322         immUnbindProgram();
323         glDisable(GL_BLEND);
324 }
325
326 static void region_draw_azone_tab_plus(AZone *az)
327 {
328         glEnable(GL_BLEND);
329         
330         /* add code to draw region hidden as 'too small' */
331         switch (az->edge) {
332                 case AE_TOP_TO_BOTTOMRIGHT:
333                         UI_draw_roundbox_corner_set(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
334                         break;
335                 case AE_BOTTOM_TO_TOPLEFT:
336                         UI_draw_roundbox_corner_set(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT);
337                         break;
338                 case AE_LEFT_TO_TOPRIGHT:
339                         UI_draw_roundbox_corner_set(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT);
340                         break;
341                 case AE_RIGHT_TO_TOPLEFT:
342                         UI_draw_roundbox_corner_set(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT);
343                         break;
344         }
345
346         float color[4] = {0.05f, 0.05f, 0.05f, 0.4f};
347         UI_draw_roundbox_aa(true, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, color);
348
349         draw_azone_plus((float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2);
350 }
351
352 static void region_draw_azone_tab(AZone *az)
353 {
354         float col[4], black[4] = {0.0f, 0.0f, 0.0f, 0.5f};
355         
356         glEnable(GL_BLEND);
357         UI_GetThemeColor3fv(TH_HEADER, col);
358         col[3] = 0.5f;
359
360         /* add code to draw region hidden as 'too small' */
361         switch (az->edge) {
362                 case AE_TOP_TO_BOTTOMRIGHT:
363                         UI_draw_roundbox_corner_set(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
364                         UI_draw_roundbox_shade_x(true, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f, col);
365                         UI_draw_roundbox_aa(false, (float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f, black);
366                         break;
367                 case AE_BOTTOM_TO_TOPLEFT:
368                         UI_draw_roundbox_corner_set(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT);
369                         UI_draw_roundbox_shade_x(true, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f, col);
370                         UI_draw_roundbox_aa(false, (float)az->x1, 0.3f + (float)az->y1, (float)az->x2, 0.3f + (float)az->y2, 4.0f, black);
371                         break;
372                 case AE_LEFT_TO_TOPRIGHT:
373                         UI_draw_roundbox_corner_set(UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT);
374                         UI_draw_roundbox_shade_x(true, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f, col);
375                         UI_draw_roundbox_aa(false, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, black);
376                         break;
377                 case AE_RIGHT_TO_TOPLEFT:
378                         UI_draw_roundbox_corner_set(UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT);
379                         UI_draw_roundbox_shade_x(true, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, -0.3f, 0.05f, col);
380                         UI_draw_roundbox_aa(false, (float)az->x1, (float)az->y1, (float)az->x2, (float)az->y2, 4.0f, black);
381                         break;
382         }
383         
384         glDisable(GL_BLEND);
385 }
386
387 static void region_draw_azone_tria(AZone *az)
388 {
389         glEnable(GL_BLEND);
390         //UI_GetThemeColor3fv(TH_HEADER, col);
391         float color[4] = {0.0f, 0.0f, 0.0f, 0.35f};
392         
393         /* add code to draw region hidden as 'too small' */
394         switch (az->edge) {
395                 case AE_TOP_TO_BOTTOMRIGHT:
396                         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, color);
397                         break;
398                         
399                 case AE_BOTTOM_TO_TOPLEFT:
400                         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, color);
401                         break;
402
403                 case AE_LEFT_TO_TOPRIGHT:
404                         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, color);
405                         break;
406                         
407                 case AE_RIGHT_TO_TOPLEFT:
408                         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, color);
409                         break;
410                         
411         }
412         
413         glDisable(GL_BLEND);
414 }
415
416 static void area_azone_tag_update(ScrArea *sa)
417 {
418         sa->flag |= AREA_FLAG_ACTIONZONES_UPDATE;
419 }
420
421 static void region_draw_azones(ScrArea *sa, ARegion *ar)
422 {
423         AZone *az;
424
425         if (!sa)
426                 return;
427
428         glLineWidth(1.0f);
429         glEnable(GL_BLEND);
430         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
431
432         gpuPushMatrix();
433         gpuTranslate2f(-ar->winrct.xmin, -ar->winrct.ymin);
434         
435         for (az = sa->actionzones.first; az; az = az->next) {
436                 /* test if action zone is over this region */
437                 rcti azrct;
438                 BLI_rcti_init(&azrct, az->x1, az->x2, az->y1, az->y2);
439
440                 if (BLI_rcti_isect(&ar->drawrct, &azrct, NULL)) {
441                         if (az->type == AZONE_AREA) {
442                                 area_draw_azone(az->x1, az->y1, az->x2, az->y2);
443                         }
444                         else if (az->type == AZONE_REGION) {
445                                 
446                                 if (az->ar) {
447                                         /* only display tab or icons when the region is hidden */
448                                         if (az->ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
449                                                 if (G.debug_value == 3)
450                                                         region_draw_azone_icon(az);
451                                                 else if (G.debug_value == 2)
452                                                         region_draw_azone_tria(az);
453                                                 else if (G.debug_value == 1)
454                                                         region_draw_azone_tab(az);
455                                                 else
456                                                         region_draw_azone_tab_plus(az);
457                                         }
458                                 }
459                         }
460                         else if (az->type == AZONE_FULLSCREEN) {
461                                 area_draw_azone_fullscreen(az->x1, az->y1, az->x2, az->y2, az->alpha);
462
463                                 if (az->alpha != 0.0f) {
464                                         area_azone_tag_update(sa);
465                                 }
466                         }
467                 }
468         }
469
470         gpuPopMatrix();
471
472         glDisable(GL_BLEND);
473 }
474
475 /* Follow wmMsgNotifyFn spec */
476 void ED_region_do_msg_notify_tag_redraw(
477         bContext *UNUSED(C), wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val)
478 {
479         ARegion *ar = msg_val->owner;
480         ED_region_tag_redraw(ar);
481
482         /* This avoids _many_ situations where header/properties control display settings.
483          * the common case is space properties in the header */
484         if (ELEM(ar->regiontype, RGN_TYPE_HEADER, RGN_TYPE_UI)) {
485                 while (ar && ar->prev) {
486                         ar = ar->prev;
487                 }
488                 for (; ar; ar = ar->next) {
489                         if (ELEM(ar->regiontype, RGN_TYPE_WINDOW, RGN_TYPE_CHANNELS)) {
490                                 ED_region_tag_redraw(ar);
491                         }
492                 }
493         }
494 }
495 /* Follow wmMsgNotifyFn spec */
496 void ED_area_do_msg_notify_tag_refresh(
497         bContext *UNUSED(C), wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val)
498 {
499         ScrArea *sa = msg_val->user_data;
500         ED_area_tag_refresh(sa);
501 }
502
503 /* only exported for WM */
504 void ED_region_do_layout(bContext *C, ARegion *ar)
505 {
506         /* This is optional, only needed for dynamically sized regions. */
507         ScrArea *sa = CTX_wm_area(C);
508         ARegionType *at = ar->type;
509
510         if (!at->layout) {
511                 return;
512         }
513
514         if (at->do_lock) {
515                 return;
516         }
517
518         ar->do_draw |= RGN_DRAWING;
519
520         UI_SetTheme(sa ? sa->spacetype : 0, at->regionid);
521         at->layout(C, ar);
522 }
523
524 /* only exported for WM */
525 void ED_region_do_draw(bContext *C, ARegion *ar)
526 {
527         wmWindow *win = CTX_wm_window(C);
528         ScrArea *sa = CTX_wm_area(C);
529         ARegionType *at = ar->type;
530
531         /* see BKE_spacedata_draw_locks() */
532         if (at->do_lock)
533                 return;
534
535         ar->do_draw |= RGN_DRAWING;
536         
537         /* Set viewport, scissor, ortho and ar->drawrct. */
538         wmPartialViewport(&ar->drawrct, &ar->winrct, &ar->drawrct);
539
540         wmOrtho2_region_pixelspace(ar);
541         
542         UI_SetTheme(sa ? sa->spacetype : 0, at->regionid);
543         
544         /* optional header info instead? */
545         if (ar->headerstr) {
546                 UI_ThemeClearColor(TH_HEADER);
547                 glClear(GL_COLOR_BUFFER_BIT);
548                 
549                 UI_FontThemeColor(BLF_default(), TH_TEXT);
550                 BLF_draw_default(UI_UNIT_X, 0.4f * UI_UNIT_Y, 0.0f, ar->headerstr, BLF_DRAW_STR_DUMMY_MAX);
551         }
552         else if (at->draw) {
553                 at->draw(C, ar);
554         }
555
556         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
557         ED_region_pixelspace(ar);
558
559         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
560
561         region_draw_azones(sa, ar);
562
563         /* for debugging unneeded area redraws and partial redraw */
564 #if 0
565         glEnable(GL_BLEND);
566         Gwn_VertFormat *format = immVertexFormat();
567         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
568         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
569         immUniformColor4f(drand48(), drand48(), drand48(), 0.1f);
570         immRectf(pos, ar->drawrct.xmin - ar->winrct.xmin, ar->drawrct.ymin - ar->winrct.ymin,
571                 ar->drawrct.xmax - ar->winrct.xmin, ar->drawrct.ymax - ar->winrct.ymin);
572         immUnbindProgram();
573         glDisable(GL_BLEND);
574 #endif
575
576         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
577         
578         UI_blocklist_free_inactive(C, &ar->uiblocks);
579
580         if (sa) {
581                 const bScreen *screen = WM_window_get_active_screen(win);
582
583                 /* Only draw region emboss for top-bar and quad-view. */
584                 if ((screen->state != SCREENFULL) && ED_area_is_global(sa)) {
585                         region_draw_emboss(ar, &ar->winrct, (REGION_EMBOSS_LEFT | REGION_EMBOSS_RIGHT));
586                 }
587                 else if ((ar->regiontype == RGN_TYPE_WINDOW) && (ar->alignment == RGN_ALIGN_QSPLIT)) {
588                         region_draw_emboss(ar, &ar->winrct, REGION_EMBOSS_ALL);
589                 }
590         }
591
592         /* We may want to detach message-subscriptions from drawing. */
593         {
594                 WorkSpace *workspace = CTX_wm_workspace(C);
595                 wmWindowManager *wm = CTX_wm_manager(C);
596                 bScreen *screen = WM_window_get_active_screen(win);
597                 Scene *scene = CTX_data_scene(C);
598                 struct wmMsgBus *mbus = wm->message_bus;
599                 WM_msgbus_clear_by_owner(mbus, ar);
600
601                 /* Cheat, always subscribe to this space type properties.
602                  *
603                  * This covers most cases and avoids copy-paste similar code for each space type.
604                  */
605                 if (ELEM(ar->regiontype, RGN_TYPE_WINDOW, RGN_TYPE_CHANNELS, RGN_TYPE_UI, RGN_TYPE_TOOLS)) {
606                         SpaceLink *sl = sa->spacedata.first;
607
608                         PointerRNA ptr;
609                         RNA_pointer_create(&screen->id, &RNA_Space, sl, &ptr);
610
611                         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
612                                 .owner = ar,
613                                 .user_data = ar,
614                                 .notify = ED_region_do_msg_notify_tag_redraw,
615                         };
616                         /* All properties for this space type. */
617                         WM_msg_subscribe_rna(mbus, &ptr, NULL, &msg_sub_value_region_tag_redraw, __func__);
618                 }
619
620                 ED_region_message_subscribe(C, workspace, scene, screen, sa, ar, mbus);
621         }
622 }
623
624 /* **********************************
625  * maybe silly, but let's try for now
626  * to keep these tags protected
627  * ********************************** */
628
629 void ED_region_tag_redraw(ARegion *ar)
630 {
631         /* don't tag redraw while drawing, it shouldn't happen normally
632          * but python scripts can cause this to happen indirectly */
633         if (ar && !(ar->do_draw & RGN_DRAWING)) {
634                 /* zero region means full region redraw */
635                 ar->do_draw &= ~RGN_DRAW_PARTIAL;
636                 ar->do_draw |= RGN_DRAW;
637                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
638         }
639 }
640
641 void ED_region_tag_redraw_overlay(ARegion *ar)
642 {
643         if (ar)
644                 ar->do_draw_overlay = RGN_DRAW;
645 }
646
647 void ED_region_tag_refresh_ui(ARegion *ar)
648 {
649         if (ar) {
650                 ar->do_draw |= RGN_DRAW_REFRESH_UI;
651         }
652 }
653
654 void ED_region_tag_redraw_partial(ARegion *ar, const rcti *rct)
655 {
656         if (ar && !(ar->do_draw & RGN_DRAWING)) {
657                 if (!(ar->do_draw & (RGN_DRAW | RGN_DRAW_PARTIAL))) {
658                         /* no redraw set yet, set partial region */
659                         ar->do_draw |= RGN_DRAW_PARTIAL;
660                         ar->drawrct = *rct;
661                 }
662                 else if (ar->drawrct.xmin != ar->drawrct.xmax) {
663                         BLI_assert((ar->do_draw & RGN_DRAW_PARTIAL) != 0);
664                         /* partial redraw already set, expand region */
665                         BLI_rcti_union(&ar->drawrct, rct);
666                 }
667                 else {
668                         BLI_assert((ar->do_draw & RGN_DRAW) != 0);
669                         /* Else, full redraw is already requested, nothing to do here. */
670                 }
671         }
672 }
673
674 void ED_area_tag_redraw(ScrArea *sa)
675 {
676         ARegion *ar;
677         
678         if (sa)
679                 for (ar = sa->regionbase.first; ar; ar = ar->next)
680                         ED_region_tag_redraw(ar);
681 }
682
683 void ED_area_tag_redraw_regiontype(ScrArea *sa, int regiontype)
684 {
685         ARegion *ar;
686         
687         if (sa) {
688                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
689                         if (ar->regiontype == regiontype) {
690                                 ED_region_tag_redraw(ar);
691                         }
692                 }
693         }
694 }
695
696 void ED_area_tag_refresh(ScrArea *sa)
697 {
698         if (sa)
699                 sa->do_refresh = true;
700 }
701
702 /* *************************************************************** */
703
704 /* use NULL to disable it */
705 void ED_area_headerprint(ScrArea *sa, const char *str)
706 {
707         ARegion *ar;
708
709         /* happens when running transform operators in backround mode */
710         if (sa == NULL)
711                 return;
712
713         for (ar = sa->regionbase.first; ar; ar = ar->next) {
714                 if (ar->regiontype == RGN_TYPE_HEADER) {
715                         if (str) {
716                                 if (ar->headerstr == NULL)
717                                         ar->headerstr = MEM_mallocN(UI_MAX_DRAW_STR, "headerprint");
718                                 BLI_strncpy(ar->headerstr, str, UI_MAX_DRAW_STR);
719                         }
720                         else if (ar->headerstr) {
721                                 MEM_freeN(ar->headerstr);
722                                 ar->headerstr = NULL;
723                         }
724                         ED_region_tag_redraw(ar);
725                 }
726         }
727 }
728
729 /* ************************************************************ */
730
731
732 static void area_azone_initialize(wmWindow *win, const bScreen *screen, ScrArea *sa)
733 {
734         AZone *az;
735         
736         /* reinitalize entirely, regions and fullscreen add azones too */
737         BLI_freelistN(&sa->actionzones);
738
739         if (screen->state != SCREENNORMAL) {
740                 return;
741         }
742
743         if (U.app_flag & USER_APP_LOCK_UI_LAYOUT) {
744                 return;
745         }
746
747         if (ED_area_is_global(sa)) {
748                 return;
749         }
750
751         float coords[4][4] = {
752             /* Bottom-left. */
753             {sa->totrct.xmin,
754              sa->totrct.ymin,
755              sa->totrct.xmin + (AZONESPOT - 1),
756              sa->totrct.ymin + (AZONESPOT - 1)},
757             /* Bottom-right. */
758             {sa->totrct.xmax,
759              sa->totrct.ymin,
760              sa->totrct.xmax - (AZONESPOT - 1),
761              sa->totrct.ymin + (AZONESPOT - 1)},
762             /* Top-left. */
763             {sa->totrct.xmin,
764              sa->totrct.ymax,
765              sa->totrct.xmin + (AZONESPOT - 1),
766              sa->totrct.ymax - (AZONESPOT - 1)},
767             /* Top-right. */
768             {sa->totrct.xmax,
769              sa->totrct.ymax,
770              sa->totrct.xmax - (AZONESPOT - 1),
771              sa->totrct.ymax - (AZONESPOT - 1)}};
772
773         for (int i = 0; i < 4; i++) {
774                 /* can't click on bottom corners on OS X, already used for resizing */
775 #ifdef __APPLE__
776                 if (!WM_window_is_fullscreen(win) &&
777                     ((coords[i][0] == 0 && coords[i][1] == 0) ||
778                      (coords[i][0] == WM_window_pixels_x(win) && coords[i][1] == 0)))
779                 {
780                         continue;
781                 }
782 #else
783                 (void)win;
784 #endif
785
786                 /* set area action zones */
787                 az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
788                 BLI_addtail(&(sa->actionzones), az);
789                 az->type = AZONE_AREA;
790                 az->x1 = coords[i][0];
791                 az->y1 = coords[i][1];
792                 az->x2 = coords[i][2];
793                 az->y2 = coords[i][3];
794                 BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
795         }
796 }
797
798 static void fullscreen_azone_initialize(ScrArea *sa, ARegion *ar)
799 {
800         AZone *az;
801
802         if (ED_area_is_global(sa) || (ar->regiontype != RGN_TYPE_WINDOW))
803                 return;
804
805         az = (AZone *)MEM_callocN(sizeof(AZone), "fullscreen action zone");
806         BLI_addtail(&(sa->actionzones), az);
807         az->type = AZONE_FULLSCREEN;
808         az->ar = ar;
809         az->alpha = 0.0f;
810
811         az->x1 = ar->winrct.xmax - (AZONEFADEOUT - 1);
812         az->y1 = ar->winrct.ymax - (AZONEFADEOUT - 1);
813         az->x2 = ar->winrct.xmax;
814         az->y2 = ar->winrct.ymax;
815         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
816 }
817
818 #define AZONEPAD_EDGE   (0.1f * U.widget_unit)
819 #define AZONEPAD_ICON   (0.45f * U.widget_unit)
820 static void region_azone_edge(AZone *az, ARegion *ar)
821 {
822         switch (az->edge) {
823                 case AE_TOP_TO_BOTTOMRIGHT:
824                         az->x1 = ar->winrct.xmin;
825                         az->y1 = ar->winrct.ymax - AZONEPAD_EDGE;
826                         az->x2 = ar->winrct.xmax;
827                         az->y2 = ar->winrct.ymax + AZONEPAD_EDGE;
828                         break;
829                 case AE_BOTTOM_TO_TOPLEFT:
830                         az->x1 = ar->winrct.xmin;
831                         az->y1 = ar->winrct.ymin + AZONEPAD_EDGE;
832                         az->x2 = ar->winrct.xmax;
833                         az->y2 = ar->winrct.ymin - AZONEPAD_EDGE;
834                         break;
835                 case AE_LEFT_TO_TOPRIGHT:
836                         az->x1 = ar->winrct.xmin - AZONEPAD_EDGE;
837                         az->y1 = ar->winrct.ymin;
838                         az->x2 = ar->winrct.xmin + AZONEPAD_EDGE;
839                         az->y2 = ar->winrct.ymax;
840                         break;
841                 case AE_RIGHT_TO_TOPLEFT:
842                         az->x1 = ar->winrct.xmax + AZONEPAD_EDGE;
843                         az->y1 = ar->winrct.ymin;
844                         az->x2 = ar->winrct.xmax - AZONEPAD_EDGE;
845                         az->y2 = ar->winrct.ymax;
846                         break;
847         }
848
849         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
850 }
851
852 static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
853 {
854         AZone *azt;
855         int tot = 0;
856         
857         /* count how many actionzones with along same edge are available.
858          * This allows for adding more action zones in the future without
859          * having to worry about correct offset */
860         for (azt = sa->actionzones.first; azt; azt = azt->next) {
861                 if (azt->edge == az->edge) tot++;
862         }
863         
864         switch (az->edge) {
865                 case AE_TOP_TO_BOTTOMRIGHT:
866                         az->x1 = ar->winrct.xmax - tot * 2 * AZONEPAD_ICON;
867                         az->y1 = ar->winrct.ymax + AZONEPAD_ICON;
868                         az->x2 = ar->winrct.xmax - tot * AZONEPAD_ICON;
869                         az->y2 = ar->winrct.ymax + 2 * AZONEPAD_ICON;
870                         break;
871                 case AE_BOTTOM_TO_TOPLEFT:
872                         az->x1 = ar->winrct.xmin + AZONEPAD_ICON;
873                         az->y1 = ar->winrct.ymin - 2 * AZONEPAD_ICON;
874                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_ICON;
875                         az->y2 = ar->winrct.ymin - AZONEPAD_ICON;
876                         break;
877                 case AE_LEFT_TO_TOPRIGHT:
878                         az->x1 = ar->winrct.xmin - 2 * AZONEPAD_ICON;
879                         az->y1 = ar->winrct.ymax - tot * 2 * AZONEPAD_ICON;
880                         az->x2 = ar->winrct.xmin - AZONEPAD_ICON;
881                         az->y2 = ar->winrct.ymax - tot * AZONEPAD_ICON;
882                         break;
883                 case AE_RIGHT_TO_TOPLEFT:
884                         az->x1 = ar->winrct.xmax + AZONEPAD_ICON;
885                         az->y1 = ar->winrct.ymax - tot * 2 * AZONEPAD_ICON;
886                         az->x2 = ar->winrct.xmax + 2 * AZONEPAD_ICON;
887                         az->y2 = ar->winrct.ymax - tot * AZONEPAD_ICON;
888                         break;
889         }
890
891         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
892         
893         /* if more azones on 1 spot, set offset */
894         for (azt = sa->actionzones.first; azt; azt = azt->next) {
895                 if (az != azt) {
896                         if (ABS(az->x1 - azt->x1) < 2 && ABS(az->y1 - azt->y1) < 2) {
897                                 if (az->edge == AE_TOP_TO_BOTTOMRIGHT || az->edge == AE_BOTTOM_TO_TOPLEFT) {
898                                         az->x1 += AZONESPOT;
899                                         az->x2 += AZONESPOT;
900                                 }
901                                 else {
902                                         az->y1 -= AZONESPOT;
903                                         az->y2 -= AZONESPOT;
904                                 }
905                                 BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
906                         }
907                 }
908         }
909 }
910
911 #define AZONEPAD_TAB_PLUSW  (0.7f * U.widget_unit)
912 #define AZONEPAD_TAB_PLUSH  (0.7f * U.widget_unit)
913
914 /* region already made zero sized, in shape of edge */
915 static void region_azone_tab_plus(ScrArea *sa, AZone *az, ARegion *ar)
916 {
917         AZone *azt;
918         int tot = 0, add;
919         
920         for (azt = sa->actionzones.first; azt; azt = azt->next) {
921                 if (azt->edge == az->edge) tot++;
922         }
923         
924         switch (az->edge) {
925                 case AE_TOP_TO_BOTTOMRIGHT:
926                         add = (ar->winrct.ymax == sa->totrct.ymin) ? 1 : 0;
927                         az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
928                         az->y1 = ar->winrct.ymax - add;
929                         az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
930                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TAB_PLUSH;
931                         break;
932                 case AE_BOTTOM_TO_TOPLEFT:
933                         az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
934                         az->y1 = ar->winrct.ymin - AZONEPAD_TAB_PLUSH;
935                         az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
936                         az->y2 = ar->winrct.ymin;
937                         break;
938                 case AE_LEFT_TO_TOPRIGHT:
939                         az->x1 = ar->winrct.xmin - AZONEPAD_TAB_PLUSH;
940                         az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
941                         az->x2 = ar->winrct.xmin;
942                         az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
943                         break;
944                 case AE_RIGHT_TO_TOPLEFT:
945                         az->x1 = ar->winrct.xmax - 1;
946                         az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
947                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TAB_PLUSH;
948                         az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
949                         break;
950         }
951         /* rect needed for mouse pointer test */
952         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
953 }       
954
955
956 #define AZONEPAD_TABW   (0.9f * U.widget_unit)
957 #define AZONEPAD_TABH   (0.35f * U.widget_unit)
958
959 /* region already made zero sized, in shape of edge */
960 static void region_azone_tab(ScrArea *sa, AZone *az, ARegion *ar)
961 {
962         AZone *azt;
963         int tot = 0, add;
964         
965         for (azt = sa->actionzones.first; azt; azt = azt->next) {
966                 if (azt->edge == az->edge) tot++;
967         }
968         
969         switch (az->edge) {
970                 case AE_TOP_TO_BOTTOMRIGHT:
971                         add = (ar->winrct.ymax == sa->totrct.ymin) ? 1 : 0;
972                         az->x1 = ar->winrct.xmax - 2 * AZONEPAD_TABW;
973                         az->y1 = ar->winrct.ymax - add;
974                         az->x2 = ar->winrct.xmax - AZONEPAD_TABW;
975                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TABH;
976                         break;
977                 case AE_BOTTOM_TO_TOPLEFT:
978                         az->x1 = ar->winrct.xmin + AZONEPAD_TABW;
979                         az->y1 = ar->winrct.ymin - AZONEPAD_TABH;
980                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_TABW;
981                         az->y2 = ar->winrct.ymin;
982                         break;
983                 case AE_LEFT_TO_TOPRIGHT:
984                         az->x1 = ar->winrct.xmin + 1 - AZONEPAD_TABH;
985                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TABW;
986                         az->x2 = ar->winrct.xmin + 1;
987                         az->y2 = ar->winrct.ymax - AZONEPAD_TABW;
988                         break;
989                 case AE_RIGHT_TO_TOPLEFT:
990                         az->x1 = ar->winrct.xmax - 1;
991                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TABW;
992                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TABH;
993                         az->y2 = ar->winrct.ymax - AZONEPAD_TABW;
994                         break;
995         }
996         /* rect needed for mouse pointer test */
997         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
998 }       
999
1000 #define AZONEPAD_TRIAW  (0.8f * U.widget_unit)
1001 #define AZONEPAD_TRIAH  (0.45f * U.widget_unit)
1002
1003
1004 /* region already made zero sized, in shape of edge */
1005 static void region_azone_tria(ScrArea *sa, AZone *az, ARegion *ar)
1006 {
1007         AZone *azt;
1008         int tot = 0, add;
1009         
1010         for (azt = sa->actionzones.first; azt; azt = azt->next) {
1011                 if (azt->edge == az->edge) tot++;
1012         }
1013         
1014         switch (az->edge) {
1015                 case AE_TOP_TO_BOTTOMRIGHT:
1016                         add = (ar->winrct.ymax == sa->totrct.ymin) ? 1 : 0;
1017                         az->x1 = ar->winrct.xmax - 2 * AZONEPAD_TRIAW;
1018                         az->y1 = ar->winrct.ymax - add;
1019                         az->x2 = ar->winrct.xmax - AZONEPAD_TRIAW;
1020                         az->y2 = ar->winrct.ymax - add + AZONEPAD_TRIAH;
1021                         break;
1022                 case AE_BOTTOM_TO_TOPLEFT:
1023                         az->x1 = ar->winrct.xmin + AZONEPAD_TRIAW;
1024                         az->y1 = ar->winrct.ymin - AZONEPAD_TRIAH;
1025                         az->x2 = ar->winrct.xmin + 2 * AZONEPAD_TRIAW;
1026                         az->y2 = ar->winrct.ymin;
1027                         break;
1028                 case AE_LEFT_TO_TOPRIGHT:
1029                         az->x1 = ar->winrct.xmin + 1 - AZONEPAD_TRIAH;
1030                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TRIAW;
1031                         az->x2 = ar->winrct.xmin + 1;
1032                         az->y2 = ar->winrct.ymax - AZONEPAD_TRIAW;
1033                         break;
1034                 case AE_RIGHT_TO_TOPLEFT:
1035                         az->x1 = ar->winrct.xmax - 1;
1036                         az->y1 = ar->winrct.ymax - 2 * AZONEPAD_TRIAW;
1037                         az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TRIAH;
1038                         az->y2 = ar->winrct.ymax - AZONEPAD_TRIAW;
1039                         break;
1040         }
1041         /* rect needed for mouse pointer test */
1042         BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
1043 }       
1044
1045
1046 static void region_azone_initialize(ScrArea *sa, ARegion *ar, AZEdge edge, const bool is_fullscreen)
1047 {
1048         AZone *az = NULL;
1049         const bool is_hidden = (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) == 0;
1050         
1051         if (is_hidden || !is_fullscreen) {
1052                 az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
1053                 BLI_addtail(&(sa->actionzones), az);
1054                 az->type = AZONE_REGION;
1055                 az->ar = ar;
1056                 az->edge = edge;
1057         }
1058         
1059         if (!is_hidden) {
1060                 if (!is_fullscreen) {
1061                         if (G.debug_value == 3)
1062                                 region_azone_icon(sa, az, ar);
1063                         else if (G.debug_value == 2)
1064                                 region_azone_tria(sa, az, ar);
1065                         else if (G.debug_value == 1)
1066                                 region_azone_tab(sa, az, ar);
1067                         else
1068                                 region_azone_tab_plus(sa, az, ar);
1069                 }
1070         }
1071         else {
1072                 region_azone_edge(az, ar);
1073         }
1074         
1075 }
1076
1077
1078 /* *************************************************************** */
1079
1080 static void region_azone_add(ScrArea *sa, ARegion *ar, const int alignment, const bool is_fullscreen)
1081 {
1082         /* edge code (t b l r) is along which area edge azone will be drawn */
1083         
1084         if (alignment == RGN_ALIGN_TOP)
1085                 region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT, is_fullscreen);
1086         else if (alignment == RGN_ALIGN_BOTTOM)
1087                 region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT, is_fullscreen);
1088         else if (alignment == RGN_ALIGN_RIGHT)
1089                 region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT, is_fullscreen);
1090         else if (alignment == RGN_ALIGN_LEFT)
1091                 region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT, is_fullscreen);
1092 }
1093
1094 /* dir is direction to check, not the splitting edge direction! */
1095 static int rct_fits(const rcti *rect, char dir, int size)
1096 {
1097         if (dir == 'h') {
1098                 return BLI_rcti_size_x(rect) + 1 - size;
1099         }
1100         else {  /* 'v' */
1101                 return BLI_rcti_size_y(rect) + 1 - size;
1102         }
1103 }
1104
1105 /* *************************************************************** */
1106
1107 /* ar should be overlapping */
1108 /* function checks if some overlapping region was defined before - on same place */
1109 static void region_overlap_fix(ScrArea *sa, ARegion *ar)
1110 {
1111         ARegion *ar1;
1112         const int align = ar->alignment & ~RGN_SPLIT_PREV;
1113         int align1 = 0;
1114
1115         /* find overlapping previous region on same place */
1116         for (ar1 = ar->prev; ar1; ar1 = ar1->prev) {
1117                 if (ar1->flag & (RGN_FLAG_HIDDEN)) {
1118                         continue;
1119                 }
1120
1121                 if (ar1->overlap && ((ar1->alignment & RGN_SPLIT_PREV) == 0)) {
1122                         align1 = ar1->alignment;
1123                         if (BLI_rcti_isect(&ar1->winrct, &ar->winrct, NULL)) {
1124                                 if (align1 != align) {
1125                                         /* Left overlapping right or vice-versa, forbid this! */
1126                                         ar->flag |= RGN_FLAG_TOO_SMALL;
1127                                         return;
1128                                 }
1129                                 /* Else, we have our previous region on same side. */
1130                                 break;
1131                         }
1132                 }
1133         }
1134
1135         /* translate or close */
1136         if (ar1) {
1137                 if (align1 == RGN_ALIGN_LEFT) {
1138                         if (ar->winrct.xmax + ar1->winx > sa->winx - U.widget_unit) {
1139                                 ar->flag |= RGN_FLAG_TOO_SMALL;
1140                                 return;
1141                         }
1142                         else {
1143                                 BLI_rcti_translate(&ar->winrct, ar1->winx, 0);
1144                         }
1145                 }
1146                 else if (align1 == RGN_ALIGN_RIGHT) {
1147                         if (ar->winrct.xmin - ar1->winx < U.widget_unit) {
1148                                 ar->flag |= RGN_FLAG_TOO_SMALL;
1149                                 return;
1150                         }
1151                         else {
1152                                 BLI_rcti_translate(&ar->winrct, -ar1->winx, 0);
1153                         }
1154                 }
1155         }
1156
1157         /* At this point, 'ar' is in its final position and still open.
1158          * Make a final check it does not overlap any previous 'other side' region. */
1159         for (ar1 = ar->prev; ar1; ar1 = ar1->prev) {
1160                 if (ar1->flag & (RGN_FLAG_HIDDEN)) {
1161                         continue;
1162                 }
1163
1164                 if (ar1->overlap && (ar1->alignment & RGN_SPLIT_PREV) == 0) {
1165                         if ((ar1->alignment != align) && BLI_rcti_isect(&ar1->winrct, &ar->winrct, NULL)) {
1166                                 /* Left overlapping right or vice-versa, forbid this! */
1167                                 ar->flag |= RGN_FLAG_TOO_SMALL;
1168                                 return;
1169                         }
1170                 }
1171         }
1172 }
1173
1174 /* overlapping regions only in the following restricted cases */
1175 static bool region_is_overlap(ScrArea *sa, ARegion *ar)
1176 {
1177         if (U.uiflag2 & USER_REGION_OVERLAP) {
1178                 if (ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ, SPACE_IMAGE)) {
1179                         if (ELEM(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS))
1180                                 return 1;
1181
1182                         if (ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_IMAGE)) {
1183                                 if (ar->regiontype == RGN_TYPE_HEADER)
1184                                         return 1;
1185                         }
1186                         else if(sa->spacetype == SPACE_SEQ) {
1187                                 if (ar->regiontype == RGN_TYPE_PREVIEW)
1188                                         return 1;
1189                         }
1190                 }
1191         }
1192
1193         return 0;
1194 }
1195
1196 static void region_rect_recursive(wmWindow *win, ScrArea *sa, ARegion *ar, rcti *remainder, rcti *overlap_remainder, int quad, bool add_azones)
1197 {
1198         rcti *remainder_prev = remainder;
1199         int prefsizex, prefsizey;
1200         int alignment;
1201         
1202         if (ar == NULL)
1203                 return;
1204         
1205         /* no returns in function, winrct gets set in the end again */
1206         BLI_rcti_init(&ar->winrct, 0, 0, 0, 0);
1207         
1208         /* for test; allow split of previously defined region */
1209         if (ar->alignment & RGN_SPLIT_PREV)
1210                 if (ar->prev)
1211                         remainder = &ar->prev->winrct;
1212         
1213         alignment = ar->alignment & ~RGN_SPLIT_PREV;
1214         
1215         /* set here, assuming userpref switching forces to call this again */
1216         ar->overlap = region_is_overlap(sa, ar);
1217
1218         /* clear state flags first */
1219         ar->flag &= ~RGN_FLAG_TOO_SMALL;
1220         /* user errors */
1221         if (ar->next == NULL && alignment != RGN_ALIGN_QSPLIT)
1222                 alignment = RGN_ALIGN_NONE;
1223
1224         /* prefsize, taking into account DPI */
1225         prefsizex = UI_DPI_FAC * ((ar->sizex > 1) ? ar->sizex + 0.5f : ar->type->prefsizex);
1226
1227         if (ar->regiontype == RGN_TYPE_HEADER) {
1228                 prefsizey = ED_area_headersize();
1229         }
1230         else if (ED_area_is_global(sa)) {
1231                 prefsizey = ED_region_global_size_y();
1232         }
1233         else if (ar->regiontype == RGN_TYPE_UI && sa->spacetype == SPACE_FILE) {
1234                 prefsizey = UI_UNIT_Y * 2 + (UI_UNIT_Y / 2);
1235         }
1236         else {
1237                 prefsizey = UI_DPI_FAC * (ar->sizey > 1 ? ar->sizey + 0.5f : ar->type->prefsizey);
1238         }
1239
1240
1241         if (ar->flag & RGN_FLAG_HIDDEN) {
1242                 /* hidden is user flag */
1243         }
1244         else if (alignment == RGN_ALIGN_FLOAT) {
1245                 /* XXX floating area region, not handled yet here */
1246         }
1247         else if (rct_fits(remainder, 'v', 1) < 0 || rct_fits(remainder, 'h', 1) < 0) {
1248                 /* remainder is too small for any usage */
1249                 ar->flag |= RGN_FLAG_TOO_SMALL;
1250         }
1251         else if (alignment == RGN_ALIGN_NONE) {
1252                 /* typically last region */
1253                 ar->winrct = *remainder;
1254                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1255         }
1256         else if (alignment == RGN_ALIGN_TOP || alignment == RGN_ALIGN_BOTTOM) {
1257                 rcti *winrct = (ar->overlap) ? overlap_remainder : remainder;
1258                 
1259                 if (rct_fits(winrct, 'v', prefsizey) < 0) {
1260                         ar->flag |= RGN_FLAG_TOO_SMALL;
1261                 }
1262                 else {
1263                         int fac = rct_fits(winrct, 'v', prefsizey);
1264
1265                         if (fac < 0)
1266                                 prefsizey += fac;
1267                         
1268                         ar->winrct = *winrct;
1269                         
1270                         if (alignment == RGN_ALIGN_TOP) {
1271                                 ar->winrct.ymin = ar->winrct.ymax - prefsizey + 1;
1272                                 winrct->ymax = ar->winrct.ymin - 1;
1273                         }
1274                         else {
1275                                 ar->winrct.ymax = ar->winrct.ymin + prefsizey - 1;
1276                                 winrct->ymin = ar->winrct.ymax + 1;
1277                         }
1278                 }
1279         }
1280         else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
1281                 rcti *winrct = (ar->overlap) ? overlap_remainder : remainder;
1282                 
1283                 if (rct_fits(winrct, 'h', prefsizex) < 0) {
1284                         ar->flag |= RGN_FLAG_TOO_SMALL;
1285                 }
1286                 else {
1287                         int fac = rct_fits(winrct, 'h', prefsizex);
1288                         
1289                         if (fac < 0)
1290                                 prefsizex += fac;
1291                         
1292                         ar->winrct = *winrct;
1293                         
1294                         if (alignment == RGN_ALIGN_RIGHT) {
1295                                 ar->winrct.xmin = ar->winrct.xmax - prefsizex + 1;
1296                                 winrct->xmax = ar->winrct.xmin - 1;
1297                         }
1298                         else {
1299                                 ar->winrct.xmax = ar->winrct.xmin + prefsizex - 1;
1300                                 winrct->xmin = ar->winrct.xmax + 1;
1301                         }
1302                 }
1303         }
1304         else if (alignment == RGN_ALIGN_VSPLIT || alignment == RGN_ALIGN_HSPLIT) {
1305                 /* percentage subdiv*/
1306                 ar->winrct = *remainder;
1307                 
1308                 if (alignment == RGN_ALIGN_HSPLIT) {
1309                         if (rct_fits(remainder, 'h', prefsizex) > 4) {
1310                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1311                                 remainder->xmin = ar->winrct.xmax + 1;
1312                         }
1313                         else {
1314                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1315                         }
1316                 }
1317                 else {
1318                         if (rct_fits(remainder, 'v', prefsizey) > 4) {
1319                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1320                                 remainder->ymin = ar->winrct.ymax + 1;
1321                         }
1322                         else {
1323                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1324                         }
1325                 }
1326         }
1327         else if (alignment == RGN_ALIGN_QSPLIT) {
1328                 ar->winrct = *remainder;
1329                 
1330                 /* test if there's still 4 regions left */
1331                 if (quad == 0) {
1332                         ARegion *artest = ar->next;
1333                         int count = 1;
1334                         
1335                         while (artest) {
1336                                 artest->alignment = RGN_ALIGN_QSPLIT;
1337                                 artest = artest->next;
1338                                 count++;
1339                         }
1340                         
1341                         if (count != 4) {
1342                                 /* let's stop adding regions */
1343                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1344                                 if (G.debug & G_DEBUG)
1345                                         printf("region quadsplit failed\n");
1346                         }
1347                         else {
1348                                 quad = 1;
1349                         }
1350                 }
1351                 if (quad) {
1352                         if (quad == 1) { /* left bottom */
1353                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1354                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1355                         }
1356                         else if (quad == 2) { /* left top */
1357                                 ar->winrct.xmax = BLI_rcti_cent_x(remainder);
1358                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1359                         }
1360                         else if (quad == 3) { /* right bottom */
1361                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1362                                 ar->winrct.ymax = BLI_rcti_cent_y(remainder);
1363                         }
1364                         else {  /* right top */
1365                                 ar->winrct.xmin = BLI_rcti_cent_x(remainder) + 1;
1366                                 ar->winrct.ymin = BLI_rcti_cent_y(remainder) + 1;
1367                                 BLI_rcti_init(remainder, 0, 0, 0, 0);
1368                         }
1369
1370                         quad++;
1371                 }
1372         }
1373         
1374         /* for speedup */
1375         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1376         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1377         
1378         /* if region opened normally, we store this for hide/reveal usage */
1379         /* prevent rounding errors for UI_DPI_FAC mult and divide */
1380         if (ar->winx > 1) ar->sizex = (ar->winx + 0.5f) /  UI_DPI_FAC;
1381         if (ar->winy > 1) ar->sizey = (ar->winy + 0.5f) /  UI_DPI_FAC;
1382                 
1383         /* exception for multiple overlapping regions on same spot */
1384         if (ar->overlap) {
1385                 region_overlap_fix(sa, ar);
1386         }
1387
1388         /* set winrect for azones */
1389         if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
1390                 ar->winrct = (ar->overlap) ? *overlap_remainder : *remainder;
1391                 
1392                 switch (alignment) {
1393                         case RGN_ALIGN_TOP:
1394                                 ar->winrct.ymin = ar->winrct.ymax;
1395                                 break;
1396                         case RGN_ALIGN_BOTTOM:
1397                                 ar->winrct.ymax = ar->winrct.ymin;
1398                                 break;
1399                         case RGN_ALIGN_RIGHT:
1400                                 ar->winrct.xmin = ar->winrct.xmax;
1401                                 break;
1402                         case RGN_ALIGN_LEFT:
1403                         default:
1404                                 /* prevent winrct to be valid */
1405                                 ar->winrct.xmax = ar->winrct.xmin;
1406                                 break;
1407                 }
1408         }
1409
1410         /* restore prev-split exception */
1411         if (ar->alignment & RGN_SPLIT_PREV) {
1412                 if (ar->prev) {
1413                         remainder = remainder_prev;
1414                         ar->prev->winx = BLI_rcti_size_x(&ar->prev->winrct) + 1;
1415                         ar->prev->winy = BLI_rcti_size_y(&ar->prev->winrct) + 1;
1416                 }
1417         }
1418
1419         /* After non-overlapping region, all following overlapping regions
1420          * fit within the remaining space again. */
1421         if (!ar->overlap) {
1422                 *overlap_remainder = *remainder;
1423         }
1424         
1425         /* in end, add azones, where appropriate */
1426         if (ar->regiontype == RGN_TYPE_HEADER && ar->winy + 6 > sa->winy) {
1427                 /* The logic for this is: when the header takes up the full area,
1428                  * disallow hiding it to view the main window.
1429                  *
1430                  * Without this, you can drag down the file selectors header and hide it
1431                  * by accident very easily (highly annoying!), the value 6 is arbitrary
1432                  * but accounts for small common rounding problems when scaling the UI,
1433                  * must be minimum '4' */
1434         }
1435         else if (add_azones) {
1436                 const bScreen *screen = WM_window_get_active_screen(win);
1437
1438                 if (ELEM(screen->state, SCREENNORMAL, SCREENMAXIMIZED)) {
1439                         region_azone_add(sa, ar, alignment, false);
1440                 }
1441                 else {
1442                         region_azone_add(sa, ar, alignment, true);
1443                         fullscreen_azone_initialize(sa, ar);
1444                 }
1445         }
1446
1447         region_rect_recursive(win, sa, ar->next, remainder, overlap_remainder, quad, add_azones);
1448 }
1449
1450 static void area_calc_totrct(ScrArea *sa, int window_size_x, int window_size_y)
1451 {
1452         short px = (short)U.pixelsize;
1453
1454         sa->totrct.xmin = sa->v1->vec.x;
1455         sa->totrct.xmax = sa->v4->vec.x;
1456         sa->totrct.ymin = sa->v1->vec.y;
1457         sa->totrct.ymax = sa->v2->vec.y;
1458
1459         /* scale down totrct by 1 pixel on all sides not matching window borders */
1460         if (sa->totrct.xmin > 0) {
1461                 sa->totrct.xmin += px;
1462         }
1463         if (sa->totrct.xmax < (window_size_x - 1)) {
1464                 sa->totrct.xmax -= px;
1465         }
1466         if (sa->totrct.ymin > 0) {
1467                 sa->totrct.ymin += px;
1468         }
1469         if (sa->totrct.ymax < (window_size_y - 1)) {
1470                 sa->totrct.ymax -= px;
1471         }
1472         /* Although the following asserts are correct they lead to a very unstable Blender.
1473          * And the asserts would fail even in 2.7x (they were added in 2.8x as part of the top-bar commit).
1474          * For more details see T54864. */
1475 #if 0
1476         BLI_assert(sa->totrct.xmin >= 0);
1477         BLI_assert(sa->totrct.xmax >= 0);
1478         BLI_assert(sa->totrct.ymin >= 0);
1479         BLI_assert(sa->totrct.ymax >= 0);
1480 #endif
1481
1482         /* for speedup */
1483         sa->winx = BLI_rcti_size_x(&sa->totrct) + 1;
1484         sa->winy = BLI_rcti_size_y(&sa->totrct) + 1;
1485 }
1486
1487
1488 /* used for area initialize below */
1489 static void region_subwindow(ARegion *ar)
1490 {
1491         bool hidden = (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) != 0;
1492
1493         if ((ar->alignment & RGN_SPLIT_PREV) && ar->prev)
1494                 hidden = hidden || (ar->prev->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL));
1495
1496         ar->visible = !hidden;
1497 }
1498
1499 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
1500 {
1501         /* note, add-handler checks if it already exists */
1502
1503         /* XXX it would be good to have boundbox checks for some of these... */
1504         if (flag & ED_KEYMAP_UI) {
1505                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "User Interface", 0, 0);
1506                 WM_event_add_keymap_handler(handlers, keymap);
1507
1508                 /* user interface widgets */
1509                 UI_region_handlers_add(handlers);
1510         }
1511         if (flag & ED_KEYMAP_VIEW2D) {
1512                 /* 2d-viewport handling+manipulation */
1513                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
1514                 WM_event_add_keymap_handler(handlers, keymap);
1515         }
1516         if (flag & ED_KEYMAP_MARKERS) {
1517                 /* time-markers */
1518                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
1519                 
1520                 /* use a boundbox restricted map */
1521                 ARegion *ar;
1522                 /* same local check for all areas */
1523                 static rcti rect = {0, 10000, 0, -1};
1524                 rect.ymax = UI_MARKER_MARGIN_Y;
1525                 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
1526                 if (ar) {
1527                         WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
1528                 }
1529         }
1530         if (flag & ED_KEYMAP_ANIMATION) {
1531                 /* frame changing and timeline operators (for time spaces) */
1532                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
1533                 WM_event_add_keymap_handler(handlers, keymap);
1534         }
1535         if (flag & ED_KEYMAP_FRAMES) {
1536                 /* frame changing/jumping (for all spaces) */
1537                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
1538                 WM_event_add_keymap_handler(handlers, keymap);
1539         }
1540         if (flag & ED_KEYMAP_GPENCIL) {
1541                 /* grease pencil */
1542                 /* NOTE: This is now 2 keymaps - One for basic functionality, 
1543                  *       and one that only applies when "Edit Mode" is enabled 
1544                  *       for strokes.
1545                  *
1546                  *       For now, it's easier to just include both, 
1547                  *       since you hardly want one without the other.
1548                  */
1549                 wmKeyMap *keymap_general = WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
1550                 wmKeyMap *keymap_edit = WM_keymap_find(wm->defaultconf, "Grease Pencil Stroke Edit Mode", 0, 0);
1551                 
1552                 WM_event_add_keymap_handler(handlers, keymap_general);
1553                 WM_event_add_keymap_handler(handlers, keymap_edit);
1554         }
1555         if (flag & ED_KEYMAP_HEADER) {
1556                 /* standard keymap for headers regions */
1557                 wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "Header", 0, 0);
1558                 WM_event_add_keymap_handler(handlers, keymap);
1559         }
1560 }
1561
1562 void ED_area_update_region_sizes(wmWindowManager *wm, wmWindow *win, ScrArea *area)
1563 {
1564         if (!(area->flag & AREA_FLAG_REGION_SIZE_UPDATE)) {
1565                 return;
1566         }
1567
1568         const int size_x = WM_window_pixels_x(win);
1569         const int size_y = WM_window_pixels_y(win);
1570         rcti rect, overlap_rect;
1571
1572         area_calc_totrct(area, size_x, size_y);
1573
1574         /* region rect sizes */
1575         rect = area->totrct;
1576         overlap_rect = rect;
1577         region_rect_recursive(win, area, area->regionbase.first, &rect, &overlap_rect, 0, false);
1578
1579         for (ARegion *ar = area->regionbase.first; ar; ar = ar->next) {
1580                 region_subwindow(ar);
1581
1582                 /* region size may have changed, init does necessary adjustments */
1583                 if (ar->type->init) {
1584                         ar->type->init(wm, ar);
1585                 }
1586         }
1587
1588         area->flag &= ~AREA_FLAG_REGION_SIZE_UPDATE;
1589 }
1590
1591 /* called in screen_refresh, or screens_init, also area size changes */
1592 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
1593 {
1594         const bScreen *screen = WM_window_get_active_screen(win);
1595         const int window_size_x = WM_window_pixels_x(win);
1596         const int window_size_y = WM_window_pixels_y(win);
1597         ARegion *ar;
1598         rcti rect, overlap_rect;
1599
1600         if (ED_area_is_global(sa) && (sa->global->flag & GLOBAL_AREA_IS_HIDDEN)) {
1601                 return;
1602         }
1603
1604         /* set typedefinitions */
1605         sa->type = BKE_spacetype_from_id(sa->spacetype);
1606         
1607         if (sa->type == NULL) {
1608                 sa->spacetype = SPACE_VIEW3D;
1609                 sa->type = BKE_spacetype_from_id(sa->spacetype);
1610         }
1611
1612         for (ar = sa->regionbase.first; ar; ar = ar->next)
1613                 ar->type = BKE_regiontype_from_id(sa->type, ar->regiontype);
1614
1615         /* area sizes */
1616         area_calc_totrct(sa, window_size_x, window_size_y);
1617
1618         /* clear all azones, add the area triange widgets */
1619         area_azone_initialize(win, screen, sa);
1620
1621         /* region rect sizes */
1622         rect = sa->totrct;
1623         overlap_rect = rect;
1624         region_rect_recursive(win, sa, sa->regionbase.first, &rect, &overlap_rect, 0, true);
1625         sa->flag &= ~AREA_FLAG_REGION_SIZE_UPDATE;
1626         
1627         /* default area handlers */
1628         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
1629         /* checks spacedata, adds own handlers */
1630         if (sa->type->init)
1631                 sa->type->init(wm, sa);
1632         
1633         /* region windows, default and own handlers */
1634         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1635                 region_subwindow(ar);
1636                 
1637                 if (ar->visible) {
1638                         /* default region handlers */
1639                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
1640                         /* own handlers */
1641                         if (ar->type->init) {
1642                                 ar->type->init(wm, ar);
1643                         }
1644                 }
1645                 else {
1646                         /* prevent uiblocks to run */
1647                         UI_blocklist_free(NULL, &ar->uiblocks);
1648                 }
1649         }
1650 }
1651
1652 static void region_update_rect(ARegion *ar)
1653 {
1654         ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
1655         ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
1656
1657         /* v2d mask is used to subtract scrollbars from a 2d view. Needs initialize here. */
1658         BLI_rcti_init(&ar->v2d.mask, 0, ar->winx - 1, 0, ar->winy -1);
1659 }
1660
1661 /**
1662  * Call to move a popup window (keep OpenGL context free!)
1663  */
1664 void ED_region_update_rect(bContext *UNUSED(C), ARegion *ar)
1665 {
1666         region_update_rect(ar);
1667 }
1668
1669 /* externally called for floating regions like menus */
1670 void ED_region_init(bContext *UNUSED(C), ARegion *ar)
1671 {
1672         /* refresh can be called before window opened */
1673         region_subwindow(ar);
1674
1675         region_update_rect(ar);
1676 }
1677
1678 void ED_region_cursor_set(wmWindow *win, ScrArea *sa, ARegion *ar)
1679 {
1680         if (ar && sa && ar->type && ar->type->cursor) {
1681                 ar->type->cursor(win, sa, ar);
1682         }
1683         else {
1684                 WM_cursor_set(win, CURSOR_STD);
1685         }
1686 }
1687
1688 /* for use after changing visiblity of regions */
1689 void ED_region_visibility_change_update(bContext *C, ARegion *ar)
1690 {
1691         ScrArea *sa = CTX_wm_area(C);
1692         
1693         if (ar->flag & RGN_FLAG_HIDDEN)
1694                 WM_event_remove_handlers(C, &ar->handlers);
1695         
1696         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1697         ED_area_tag_redraw(sa);
1698 }
1699
1700 /* for quick toggle, can skip fades */
1701 void region_toggle_hidden(bContext *C, ARegion *ar, const bool do_fade)
1702 {
1703         ScrArea *sa = CTX_wm_area(C);
1704         
1705         ar->flag ^= RGN_FLAG_HIDDEN;
1706         
1707         if (do_fade && ar->overlap) {
1708                 /* starts a timer, and in end calls the stuff below itself (region_sblend_invoke()) */
1709                 region_blend_start(C, sa, ar);
1710         }
1711         else {
1712                 ED_region_visibility_change_update(C, ar);
1713         }
1714 }
1715
1716 /* exported to all editors, uses fading default */
1717 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
1718 {
1719         region_toggle_hidden(C, ar, true);
1720 }
1721
1722 /**
1723  * we swap spaces for fullscreen to keep all allocated data area vertices were set
1724  */
1725 void ED_area_data_copy(ScrArea *sa_dst, ScrArea *sa_src, const bool do_free)
1726 {
1727         SpaceType *st;
1728         ARegion *ar;
1729         const char spacetype = sa_dst->spacetype;
1730         const short flag_copy = HEADER_NO_PULLDOWN;
1731         
1732         sa_dst->headertype = sa_src->headertype;
1733         sa_dst->spacetype = sa_src->spacetype;
1734         sa_dst->type = sa_src->type;
1735
1736         sa_dst->flag = (sa_dst->flag & ~flag_copy) | (sa_src->flag & flag_copy);
1737
1738         /* area */
1739         if (do_free) {
1740                 BKE_spacedata_freelist(&sa_dst->spacedata);
1741         }
1742         BKE_spacedata_copylist(&sa_dst->spacedata, &sa_src->spacedata);
1743
1744         /* Note; SPACE_EMPTY is possible on new screens */
1745
1746         /* regions */
1747         if (do_free) {
1748                 st = BKE_spacetype_from_id(spacetype);
1749                 for (ar = sa_dst->regionbase.first; ar; ar = ar->next)
1750                         BKE_area_region_free(st, ar);
1751                 BLI_freelistN(&sa_dst->regionbase);
1752         }
1753         st = BKE_spacetype_from_id(sa_src->spacetype);
1754         for (ar = sa_src->regionbase.first; ar; ar = ar->next) {
1755                 ARegion *newar = BKE_area_region_copy(st, ar);
1756                 BLI_addtail(&sa_dst->regionbase, newar);
1757         }
1758 }
1759
1760 void ED_area_data_swap(ScrArea *sa_dst, ScrArea *sa_src)
1761 {
1762         SWAP(short, sa_dst->headertype, sa_src->headertype);
1763         SWAP(char, sa_dst->spacetype, sa_src->spacetype);
1764         SWAP(SpaceType *, sa_dst->type, sa_src->type);
1765
1766
1767         SWAP(ListBase, sa_dst->spacedata, sa_src->spacedata);
1768         SWAP(ListBase, sa_dst->regionbase, sa_src->regionbase);
1769 }
1770
1771 /* *********** Space switching code *********** */
1772
1773 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
1774 {
1775         ScrArea *tmp = MEM_callocN(sizeof(ScrArea), "addscrarea");
1776
1777         ED_area_exit(C, sa1);
1778         ED_area_exit(C, sa2);
1779
1780         ED_area_data_copy(tmp, sa1, false);
1781         ED_area_data_copy(sa1, sa2, true);
1782         ED_area_data_copy(sa2, tmp, true);
1783         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
1784         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
1785
1786         BKE_screen_area_free(tmp);
1787         MEM_freeN(tmp);
1788
1789         /* tell WM to refresh, cursor types etc */
1790         WM_event_add_mousemove(C);
1791         
1792         ED_area_tag_redraw(sa1);
1793         ED_area_tag_refresh(sa1);
1794         ED_area_tag_redraw(sa2);
1795         ED_area_tag_refresh(sa2);
1796 }
1797
1798 /**
1799  * \param skip_ar_exit  Skip calling area exit callback. Set for opening temp spaces.
1800  */
1801 void ED_area_newspace(bContext *C, ScrArea *sa, int type, const bool skip_ar_exit)
1802 {
1803         wmWindow *win = CTX_wm_window(C);
1804
1805         if (sa->spacetype != type) {
1806                 SpaceType *st;
1807                 SpaceLink *slold;
1808                 SpaceLink *sl;
1809                 /* store sa->type->exit callback */
1810                 void *sa_exit = sa->type ? sa->type->exit : NULL;
1811
1812                 /* in some cases (opening temp space) we don't want to
1813                  * call area exit callback, so we temporarily unset it */
1814                 if (skip_ar_exit && sa->type) {
1815                         sa->type->exit = NULL;
1816                 }
1817
1818                 ED_area_exit(C, sa);
1819
1820                 /* restore old area exit callback */
1821                 if (skip_ar_exit && sa->type) {
1822                         sa->type->exit = sa_exit;
1823                 }
1824
1825                 st = BKE_spacetype_from_id(type);
1826                 slold = sa->spacedata.first;
1827
1828                 sa->spacetype = type;
1829                 sa->type = st;
1830
1831                 /* If st->new may be called, don't use context until then. The
1832                  * sa->type->context() callback has changed but data may be invalid
1833                  * (e.g. with properties editor) until space-data is properly created */
1834
1835                 /* check previously stored space */
1836                 for (sl = sa->spacedata.first; sl; sl = sl->next)
1837                         if (sl->spacetype == type)
1838                                 break;
1839                 
1840                 /* old spacedata... happened during work on 2.50, remove */
1841                 if (sl && BLI_listbase_is_empty(&sl->regionbase)) {
1842                         st->free(sl);
1843                         BLI_freelinkN(&sa->spacedata, sl);
1844                         if (slold == sl) {
1845                                 slold = NULL;
1846                         }
1847                         sl = NULL;
1848                 }
1849                 
1850                 if (sl) {
1851                         /* swap regions */
1852                         slold->regionbase = sa->regionbase;
1853                         sa->regionbase = sl->regionbase;
1854                         BLI_listbase_clear(&sl->regionbase);
1855                         
1856                         /* put in front of list */
1857                         BLI_remlink(&sa->spacedata, sl);
1858                         BLI_addhead(&sa->spacedata, sl);
1859                 }
1860                 else {
1861                         /* new space */
1862                         if (st) {
1863                                 /* Don't get scene from context here which may depend on space-data. */
1864                                 Scene *scene = WM_window_get_active_scene(win);
1865                                 sl = st->new(sa, scene);
1866                                 BLI_addhead(&sa->spacedata, sl);
1867                                 
1868                                 /* swap regions */
1869                                 if (slold)
1870                                         slold->regionbase = sa->regionbase;
1871                                 sa->regionbase = sl->regionbase;
1872                                 BLI_listbase_clear(&sl->regionbase);
1873                         }
1874                 }
1875                 
1876                 ED_area_initialize(CTX_wm_manager(C), win, sa);
1877                 
1878                 /* tell WM to refresh, cursor types etc */
1879                 WM_event_add_mousemove(C);
1880                                 
1881                 /* send space change notifier */
1882                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1883                 
1884                 ED_area_tag_refresh(sa);
1885         }
1886         
1887         /* also redraw when re-used */
1888         ED_area_tag_redraw(sa);
1889 }
1890
1891 void ED_area_prevspace(bContext *C, ScrArea *sa)
1892 {
1893         SpaceLink *sl = sa->spacedata.first;
1894
1895         if (sl && sl->next) {
1896                 ED_area_newspace(C, sa, sl->next->spacetype, false);
1897
1898                 /* keep old spacedata but move it to end, so calling
1899                  * ED_area_prevspace once more won't open it again */
1900                 BLI_remlink(&sa->spacedata, sl);
1901                 BLI_addtail(&sa->spacedata, sl);
1902         }
1903         else {
1904                 /* no change */
1905                 return;
1906         }
1907         sa->flag &= ~AREA_FLAG_STACKED_FULLSCREEN;
1908
1909         ED_area_tag_redraw(sa);
1910
1911         /* send space change notifier */
1912         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
1913 }
1914
1915 /* returns offset for next button in header */
1916 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1917 {
1918         ScrArea *sa = CTX_wm_area(C);
1919         bScreen *scr = CTX_wm_screen(C);
1920         PointerRNA areaptr;
1921         int xco = 0.4 * U.widget_unit;
1922
1923         RNA_pointer_create(&(scr->id), &RNA_Area, sa, &areaptr);
1924
1925         uiDefButR(block, UI_BTYPE_MENU, 0, "", xco, yco, 1.6 * U.widget_unit, U.widget_unit,
1926                   &areaptr, "type", 0, 0.0f, 0.0f, 0.0f, 0.0f, "");
1927
1928         return xco + 1.7 * U.widget_unit;
1929 }
1930
1931 /************************ standard UI regions ************************/
1932
1933 static ThemeColorID region_background_color_id(const bContext *C, const ARegion *region)
1934 {
1935         ScrArea *area = CTX_wm_area(C);
1936
1937         switch (region->regiontype) {
1938                 case RGN_TYPE_HEADER:
1939                         if (ED_screen_area_active(C) || ED_area_is_global(area)) {
1940                                 return TH_HEADER;
1941                         }
1942                         else {
1943                                 return TH_HEADERDESEL;
1944                         }
1945                 case RGN_TYPE_PREVIEW:
1946                         return TH_PREVIEW_BACK;
1947                 default:
1948                         return TH_BACK;
1949         }
1950 }
1951
1952 static void region_clear_color(const bContext *C, const ARegion *ar, ThemeColorID colorid)
1953 {
1954         if (ar->overlap) {
1955                 /* view should be in pixelspace */
1956                 UI_view2d_view_restore(C);
1957
1958                 float back[4];
1959                 UI_GetThemeColor4fv(colorid, back);
1960                 glClearColor(back[3] * back[0], back[3] * back[1], back[3] * back[2], back[3]);
1961                 glClear(GL_COLOR_BUFFER_BIT);
1962         }
1963         else {
1964                 UI_ThemeClearColor(colorid);
1965                 glClear(GL_COLOR_BUFFER_BIT);
1966         }
1967 }
1968
1969 void ED_region_panels(const bContext *C, ARegion *ar, const char *context, int contextnr, const bool vertical)
1970 {
1971         const WorkSpace *workspace = CTX_wm_workspace(C);
1972         ScrArea *sa = CTX_wm_area(C);
1973         uiStyle *style = UI_style_get_dpi();
1974         uiBlock *block;
1975         PanelType *pt;
1976         Panel *panel;
1977         View2D *v2d = &ar->v2d;
1978         View2DScrollers *scrollers;
1979         int x, y, xco, yco, w, em, triangle;
1980         bool is_context_new = 0;
1981         int redo;
1982         int scroll;
1983
1984         bool use_category_tabs = (ELEM(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI));  /* XXX, should use some better check? */
1985         /* offset panels for small vertical tab area */
1986         const char *category = NULL;
1987         const int category_tabs_width = UI_PANEL_CATEGORY_MARGIN_WIDTH;
1988         int margin_x = 0;
1989
1990         BLI_SMALLSTACK_DECLARE(pt_stack, PanelType *);
1991
1992         if (contextnr != -1)
1993                 is_context_new = UI_view2d_tab_set(v2d, contextnr);
1994         
1995         /* before setting the view */
1996         if (vertical) {
1997                 /* only allow scrolling in vertical direction */
1998                 v2d->keepofs |= V2D_LOCKOFS_X | V2D_KEEPOFS_Y;
1999                 v2d->keepofs &= ~(V2D_LOCKOFS_Y | V2D_KEEPOFS_X);
2000                 v2d->scroll &= ~(V2D_SCROLL_BOTTOM);
2001                 v2d->scroll |= (V2D_SCROLL_RIGHT);
2002         }
2003         else {
2004                 /* for now, allow scrolling in both directions (since layouts are optimized for vertical,
2005                  * they often don't fit in horizontal layout)
2006                  */
2007                 v2d->keepofs &= ~(V2D_LOCKOFS_X | V2D_LOCKOFS_Y | V2D_KEEPOFS_X | V2D_KEEPOFS_Y);
2008                 v2d->scroll |= (V2D_SCROLL_BOTTOM);
2009                 v2d->scroll &= ~(V2D_SCROLL_RIGHT);
2010         }
2011
2012         scroll = v2d->scroll;
2013
2014
2015         /* collect panels to draw */
2016         for (pt = ar->type->paneltypes.last; pt; pt = pt->prev) {
2017                 /* verify context */
2018                 if (context && pt->context[0] && !STREQ(context, pt->context)) {
2019                         continue;
2020                 }
2021
2022                 /* If we're tagged, only use compatible. */
2023                 if (pt->owner_id[0] && BKE_workspace_owner_id_check(workspace, pt->owner_id) == false) {
2024                         continue;
2025                 }
2026
2027                 /* draw panel */
2028                 if (pt->draw && (!pt->poll || pt->poll(C, pt))) {
2029                         BLI_SMALLSTACK_PUSH(pt_stack, pt);
2030                 }
2031         }
2032
2033
2034         /* collect categories */
2035         if (use_category_tabs) {
2036                 UI_panel_category_clear_all(ar);
2037
2038                 /* gather unique categories */
2039                 BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
2040                 {
2041                         if (pt->category[0]) {
2042                                 if (!UI_panel_category_find(ar, pt->category)) {
2043                                         UI_panel_category_add(ar, pt->category);
2044                                 }
2045                         }
2046                 }
2047                 BLI_SMALLSTACK_ITER_END;
2048
2049                 if (!UI_panel_category_is_visible(ar)) {
2050                         use_category_tabs = false;
2051                 }
2052                 else {
2053                         category = UI_panel_category_active_get(ar, true);
2054                         margin_x = category_tabs_width;
2055                 }
2056         }
2057
2058
2059         /* sortof hack - but we cannot predict the height of panels, until it's being generated */
2060         /* the layout engine works with fixed width (from v2d->cur), which is being set at end of the loop */
2061         /* in case scroller settings (hide flags) differ from previous, the whole loop gets done again */
2062         for (redo = 2; redo > 0; redo--) {
2063                 
2064                 if (vertical) {
2065                         w = BLI_rctf_size_x(&v2d->cur);
2066                         em = (ar->type->prefsizex) ? 10 : 20; /* works out to 10*UI_UNIT_X or 20*UI_UNIT_X */
2067                 }
2068                 else {
2069                         w = UI_PANEL_WIDTH;
2070                         em = (ar->type->prefsizex) ? 10 : 20;
2071                 }
2072
2073                 w -= margin_x;
2074                 
2075                 /* create panels */
2076                 UI_panels_begin(C, ar);
2077
2078                 /* set view2d view matrix  - UI_block_begin() stores it */
2079                 UI_view2d_view_ortho(v2d);
2080
2081                 BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
2082                 {
2083                         bool open;
2084
2085                         panel = UI_panel_find_by_type(ar, pt);
2086
2087                         if (use_category_tabs && pt->category[0] && !STREQ(category, pt->category)) {
2088                                 if ((panel == NULL) || ((panel->flag & PNL_PIN) == 0)) {
2089                                         continue;
2090                                 }
2091                         }
2092
2093                         /* draw panel */
2094                         block = UI_block_begin(C, ar, pt->idname, UI_EMBOSS);
2095                         panel = UI_panel_begin(sa, ar, block, pt, panel, &open);
2096
2097                         /* bad fixed values */
2098                         triangle = (int)(UI_UNIT_Y * 1.1f);
2099
2100                         if (pt->draw_header && !(pt->flag & PNL_NO_HEADER) && (open || vertical)) {
2101                                 /* for enabled buttons */
2102                                 panel->layout = UI_block_layout(
2103                                         block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
2104                                         triangle, (UI_UNIT_Y * 1.1f) + style->panelspace, UI_UNIT_Y, 1, 0, style);
2105
2106                                 pt->draw_header(C, panel);
2107
2108                                 UI_block_layout_resolve(block, &xco, &yco);
2109                                 panel->labelofs = xco - triangle;
2110                                 panel->layout = NULL;
2111                         }
2112                         else {
2113                                 panel->labelofs = 0;
2114                         }
2115
2116                         if (open) {
2117                                 short panelContext;
2118
2119                                 /* panel context can either be toolbar region or normal panels region */
2120                                 if (ar->regiontype == RGN_TYPE_TOOLS)
2121                                         panelContext = UI_LAYOUT_TOOLBAR;
2122                                 else
2123                                         panelContext = UI_LAYOUT_PANEL;
2124
2125                                 panel->layout = UI_block_layout(
2126                                         block, UI_LAYOUT_VERTICAL, panelContext,
2127                                         style->panelspace, 0, w - 2 * style->panelspace, em, 0, style);
2128
2129                                 pt->draw(C, panel);
2130
2131                                 UI_block_layout_resolve(block, &xco, &yco);
2132                                 panel->layout = NULL;
2133
2134                                 yco -= 2 * style->panelspace;
2135                                 UI_panel_end(block, w, -yco);
2136                         }
2137                         else {
2138                                 yco = 0;
2139                                 UI_panel_end(block, w, 0);
2140                         }
2141
2142                         UI_block_end(C, block);
2143                 }
2144                 BLI_SMALLSTACK_ITER_END;
2145
2146                 /* align panels and return size */
2147                 UI_panels_end(C, ar, &x, &y);
2148                 
2149                 /* before setting the view */
2150                 if (vertical) {
2151                         /* we always keep the scroll offset - so the total view gets increased with the scrolled away part */
2152                         if (v2d->cur.ymax < -FLT_EPSILON) {
2153                                 /* Clamp to lower view boundary */
2154                                 if (v2d->tot.ymin < -v2d->winy) {
2155                                         y = min_ii(y, 0);
2156                                 }
2157                                 else {
2158                                         y = min_ii(y, v2d->cur.ymin);
2159                                 }
2160                         }
2161
2162                         y = -y;
2163                 }
2164                 else {
2165                         /* don't jump back when panels close or hide */
2166                         if (!is_context_new) {
2167                                 if (v2d->tot.xmax > v2d->winx) {
2168                                         x = max_ii(x, 0);
2169                                 }
2170                                 else {
2171                                         x = max_ii(x, v2d->cur.xmax);
2172                                 }
2173                         }
2174
2175                         y = -y;
2176                 }
2177                 
2178                 /* this also changes the 'cur' */
2179                 UI_view2d_totRect_set(v2d, x, y);
2180                 
2181                 if (scroll != v2d->scroll) {
2182                         /* Note: this code scales fine, but because of rounding differences, positions of elements
2183                          * flip +1 or -1 pixel compared to redoing the entire layout again.
2184                          * Leaving in commented code for future tests */
2185 #if 0
2186                         UI_panels_scale(ar, BLI_rctf_size_x(&v2d->cur));
2187                         break;
2188 #endif
2189                 }
2190                 else {
2191                         break;
2192                 }
2193         }
2194
2195         region_clear_color(C, ar, (ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
2196         
2197         /* reset line width for drawing tabs */
2198         glLineWidth(1.0f);
2199
2200         /* set the view */
2201         UI_view2d_view_ortho(v2d);
2202
2203         /* draw panels */
2204         UI_panels_draw(C, ar);
2205
2206         /* restore view matrix */
2207         UI_view2d_view_restore(C);
2208         
2209         if (use_category_tabs) {
2210                 UI_panel_category_draw_all(ar, category);
2211         }
2212
2213         /* scrollers */
2214         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
2215         UI_view2d_scrollers_draw(C, v2d, scrollers);
2216         UI_view2d_scrollers_free(scrollers);
2217 }
2218
2219 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
2220 {
2221         wmKeyMap *keymap;
2222
2223         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
2224
2225         keymap = WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
2226         WM_event_add_keymap_handler(&ar->handlers, keymap);
2227 }
2228
2229 void ED_region_header_layout(const bContext *C, ARegion *ar)
2230 {
2231         uiStyle *style = UI_style_get_dpi();
2232         uiBlock *block;
2233         uiLayout *layout;
2234         HeaderType *ht;
2235         Header header = {NULL};
2236         int maxco, xco, yco;
2237         int headery = ED_area_headersize();
2238         const int start_ofs = 0.4f * UI_UNIT_X;
2239         bool region_layout_based = ar->flag & RGN_FLAG_DYNAMIC_SIZE;
2240
2241         /* set view2d view matrix for scrolling (without scrollers) */
2242         UI_view2d_view_ortho(&ar->v2d);
2243
2244         xco = maxco = start_ofs;
2245         yco = headery + (ar->winy - headery) / 2 - floor(0.2f * UI_UNIT_Y);
2246
2247         /* XXX workaround for 1 px alignment issue. Not sure what causes it... Would prefer a proper fix - Julian */
2248         if (CTX_wm_area(C)->spacetype == SPACE_TOPBAR) {
2249                 xco += 1;
2250                 yco += 1;
2251         }
2252
2253         /* draw all headers types */
2254         for (ht = ar->type->headertypes.first; ht; ht = ht->next) {
2255                 block = UI_block_begin(C, ar, ht->idname, UI_EMBOSS);
2256                 layout = UI_block_layout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, UI_UNIT_Y, 1, 0, style);
2257
2258                 if (ht->draw) {
2259                         header.type = ht;
2260                         header.layout = layout;
2261                         ht->draw(C, &header);
2262                         
2263                         /* for view2d */
2264                         xco = uiLayoutGetWidth(layout);
2265                         if (xco > maxco)
2266                                 maxco = xco;
2267                 }
2268
2269                 UI_block_layout_resolve(block, &xco, &yco);
2270                 
2271                 /* for view2d */
2272                 if (xco > maxco)
2273                         maxco = xco;
2274
2275                 int new_sizex = (maxco + start_ofs) / UI_DPI_FAC;
2276
2277                 if (region_layout_based && (ar->sizex != new_sizex)) {
2278                         /* region size is layout based and needs to be updated */
2279                         ScrArea *sa = CTX_wm_area(C);
2280
2281                         ar->sizex = new_sizex;
2282                         sa->flag |= AREA_FLAG_REGION_SIZE_UPDATE;
2283                 }
2284
2285                 UI_block_end(C, block);
2286         }
2287
2288         /* always as last  */
2289         UI_view2d_totRect_set(&ar->v2d, maxco + (region_layout_based ? 0 : UI_UNIT_X + 80), headery);
2290
2291         /* restore view matrix */
2292         UI_view2d_view_restore(C);
2293 }
2294
2295 void ED_region_header_draw(const bContext *C, ARegion *ar)
2296 {
2297         UI_view2d_view_ortho(&ar->v2d);
2298
2299         /* clear */
2300         region_clear_color(C, ar, region_background_color_id(C, ar));
2301
2302         /* View2D matrix might have changed due to dynamic sized regions. */
2303         UI_blocklist_update_window_matrix(C, &ar->uiblocks);
2304
2305         /* draw blocks */
2306         UI_blocklist_draw(C, &ar->uiblocks);
2307
2308         /* restore view matrix */
2309         UI_view2d_view_restore(C);
2310 }
2311
2312 void ED_region_header(const bContext *C, ARegion *ar)
2313 {
2314         /* TODO: remove? */
2315         ED_region_header_layout(C, ar);
2316         ED_region_header_draw(C, ar);
2317 }
2318
2319 void ED_region_header_init(ARegion *ar)
2320 {
2321         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
2322 }
2323
2324 /* UI_UNIT_Y is defined as U variable now, depending dpi */
2325 int ED_area_headersize(void)
2326 {
2327         return (int)(HEADERY * UI_DPI_FAC);
2328 }
2329
2330 /**
2331  * \return the final height of a global \a area, accounting for DPI.
2332  */
2333 int ED_area_global_size_y(const ScrArea *area)
2334 {
2335         BLI_assert(ED_area_is_global(area));
2336         return round_fl_to_int(area->global->cur_fixed_height * UI_DPI_FAC);
2337 }
2338
2339 bool ED_area_is_global(const ScrArea *area)
2340 {
2341         return area->global != NULL;
2342 }
2343
2344 ScrArea *ED_screen_areas_iter_first(const wmWindow *win, const bScreen *screen)
2345 {
2346         ScrArea *global_area = win->global_areas.areabase.first;
2347
2348         if (!global_area) {
2349                 return screen->areabase.first;
2350         }
2351         else if ((global_area->global->flag & GLOBAL_AREA_IS_HIDDEN) == 0) {
2352                 return global_area;
2353         }
2354         /* Find next visible area. */
2355         return ED_screen_areas_iter_next(screen, global_area);
2356 }
2357 ScrArea *ED_screen_areas_iter_next(const bScreen *screen, const ScrArea *area)
2358 {
2359         if (area->global) {
2360                 for (ScrArea *area_iter = area->next; area_iter; area_iter = area_iter->next) {
2361                         if ((area_iter->global->flag & GLOBAL_AREA_IS_HIDDEN) == 0) {
2362                                 return area_iter;
2363                         }
2364                 }
2365                 /* No visible next global area found, start iterating over layout areas. */
2366                 return screen->areabase.first;
2367         }
2368
2369         return area->next;
2370 }
2371
2372 /**
2373  * For now we just assume all global areas are made up out of horizontal bars
2374  * with the same size. A fixed size could be stored in ARegion instead if needed.
2375  *
2376  * \return the DPI aware height of a single bar/region in global areas.
2377  */
2378 int ED_region_global_size_y(void)
2379 {
2380         return ED_area_headersize(); /* same size as header */
2381 }
2382
2383 void ED_region_info_draw_multiline(ARegion *ar, const char *text_array[], float fill_color[4], const bool full_redraw)
2384 {
2385         const int header_height = UI_UNIT_Y;
2386         uiStyle *style = UI_style_get_dpi();
2387         int fontid = style->widget.uifont_id;
2388         GLint scissor[4];
2389         rcti rect;
2390         int num_lines = 0;
2391
2392         /* background box */
2393         ED_region_visible_rect(ar, &rect);
2394
2395         /* Box fill entire width or just around text. */
2396         if (!full_redraw) {
2397                 const char **text = &text_array[0];
2398                 while (*text) {
2399                         rect.xmax = min_ii(rect.xmax, rect.xmin + BLF_width(fontid, *text, BLF_DRAW_STR_DUMMY_MAX) + 1.2f * U.widget_unit);
2400                         text++;
2401                         num_lines++;
2402                 }
2403         }
2404         /* Just count the line number. */
2405         else {
2406                 const char **text = &text_array[0];
2407                 while (*text) {
2408                         text++;
2409                         num_lines++;
2410                 }
2411         }
2412
2413         rect.ymin = BLI_rcti_size_y(&ar->winrct) - header_height * num_lines;
2414         rect.ymax = BLI_rcti_size_y(&ar->winrct);
2415
2416         /* setup scissor */
2417         glGetIntegerv(GL_SCISSOR_BOX, scissor);
2418         glScissor(rect.xmin, rect.ymin,
2419                   BLI_rcti_size_x(&rect) + 1, BLI_rcti_size_y(&rect) + 1);
2420
2421         glEnable(GL_BLEND);
2422         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2423         Gwn_VertFormat *format = immVertexFormat();
2424         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
2425         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2426         immUniformColor4fv(fill_color);
2427         immRecti(pos, rect.xmin, rect.ymin, rect.xmax + 1, rect.ymax + 1);
2428         immUnbindProgram();
2429         glDisable(GL_BLEND);
2430
2431         /* text */
2432         UI_FontThemeColor(fontid, TH_TEXT_HI);
2433         BLF_clipping(fontid, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
2434         BLF_enable(fontid, BLF_CLIPPING);
2435         int offset = num_lines - 1;
2436         {
2437                 const char **text = &text_array[0];
2438                 while (*text) {
2439                         BLF_position(fontid, rect.xmin + 0.6f * U.widget_unit, rect.ymin + 0.3f * U.widget_unit + offset * header_height, 0.0f);
2440                         BLF_draw(fontid, *text, BLF_DRAW_STR_DUMMY_MAX);
2441                         text++;
2442                         offset--;
2443                 }
2444         }
2445
2446         BLF_disable(fontid, BLF_CLIPPING);
2447
2448         /* restore scissor as it was before */
2449         glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
2450 }
2451
2452 void ED_region_info_draw(ARegion *ar, const char *text, float fill_color[4], const bool full_redraw)
2453 {
2454         ED_region_info_draw_multiline(ar, (const char *[2]){text, NULL}, fill_color, full_redraw);
2455 }
2456
2457 #define MAX_METADATA_STR    1024
2458
2459 static const char *meta_data_list[] =
2460 {
2461         "File",
2462         "Strip",
2463         "Date",
2464         "RenderTime",
2465         "Note",
2466         "Marker",
2467         "Time",
2468         "Frame",
2469         "Camera",
2470         "Scene"
2471 };
2472
2473 BLI_INLINE bool metadata_is_valid(ImBuf *ibuf, char *r_str, short index, int offset)
2474 {
2475         return (IMB_metadata_get_field(ibuf->metadata, meta_data_list[index], r_str + offset, MAX_METADATA_STR - offset) && r_str[0]);
2476 }
2477
2478 static void metadata_draw_imbuf(ImBuf *ibuf, const rctf *rect, int fontid, const bool is_top)
2479 {
2480         char temp_str[MAX_METADATA_STR];
2481         int line_width;
2482         int ofs_y = 0;
2483         short i;
2484         int len;
2485         const float height = BLF_height_max(fontid);
2486         const float margin = height / 8;
2487         const float vertical_offset = (height + margin);
2488
2489         /* values taking margins into account */
2490         const float descender = BLF_descender(fontid);
2491         const float xmin = (rect->xmin + margin);
2492         const float xmax = (rect->xmax - margin);
2493         const float ymin = (rect->ymin + margin) - descender;
2494         const float ymax = (rect->ymax - margin) - descender;
2495
2496         if (is_top) {
2497                 for (i = 0; i < 4; i++) {
2498                         /* first line */
2499                         if (i == 0) {
2500                                 bool do_newline = false;
2501                                 len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[0]);
2502                                 if (metadata_is_valid(ibuf, temp_str, 0, len)) {
2503                                         BLF_position(fontid, xmin, ymax - vertical_offset, 0.0f);
2504                                         BLF_draw(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2505                                         do_newline = true;
2506                                 }
2507
2508                                 len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[1]);
2509                                 if (metadata_is_valid(ibuf, temp_str, 1, len)) {
2510                                         line_width = BLF_width(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2511                                         BLF_position(fontid, xmax - line_width, ymax - vertical_offset, 0.0f);
2512                                         BLF_draw(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2513                                         do_newline = true;
2514                                 }
2515
2516                                 if (do_newline)
2517                                         ofs_y += vertical_offset;
2518                         } /* Strip */
2519                         else if (i == 1 || i == 2) {
2520                                 len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[i + 1]);
2521                                 if (metadata_is_valid(ibuf, temp_str, i + 1, len)) {
2522                                         BLF_position(fontid, xmin, ymax - vertical_offset - ofs_y, 0.0f);
2523                                         BLF_draw(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2524                                         ofs_y += vertical_offset;
2525                                 }
2526                         } /* Note (wrapped) */
2527                         else if (i == 3) {
2528                                 len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[i + 1]);
2529                                 if (metadata_is_valid(ibuf, temp_str, i + 1, len)) {
2530                                         struct ResultBLF info;
2531                                         BLF_enable(fontid, BLF_WORD_WRAP);
2532                                         BLF_wordwrap(fontid, ibuf->x - (margin * 2));
2533                                         BLF_position(fontid, xmin, ymax - vertical_offset - ofs_y, 0.0f);
2534                                         BLF_draw_ex(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX, &info);
2535                                         BLF_wordwrap(fontid, 0);
2536                                         BLF_disable(fontid, BLF_WORD_WRAP);
2537                                         ofs_y += vertical_offset * info.lines;
2538                                 }
2539                         }
2540                         else {
2541                                 len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[i + 1]);
2542                                 if (metadata_is_valid(ibuf, temp_str, i + 1, len)) {
2543                                         line_width = BLF_width(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2544                                         BLF_position(fontid, xmax  - line_width, ymax - vertical_offset - ofs_y, 0.0f);
2545                                         BLF_draw(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2546                                         ofs_y += vertical_offset;
2547                                 }
2548                         }
2549                 }
2550         }
2551         else {
2552                 int ofs_x = 0;
2553                 for (i = 5; i < 10; i++) {
2554                         len = BLI_snprintf_rlen(temp_str, MAX_METADATA_STR, "%s: ", meta_data_list[i]);
2555                         if (metadata_is_valid(ibuf, temp_str, i, len)) {
2556                                 BLF_position(fontid, xmin + ofs_x, ymin, 0.0f);
2557                                 BLF_draw(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX);
2558         
2559                                 ofs_x += BLF_width(fontid, temp_str, BLF_DRAW_STR_DUMMY_MAX) + UI_UNIT_X;
2560                         }
2561                 }
2562         }
2563 }
2564
2565 static float metadata_box_height_get(ImBuf *ibuf, int fontid, const bool is_top)
2566 {
2567         const float height = BLF_height_max(fontid);
2568         const float margin = (height / 8);
2569         char str[MAX_METADATA_STR] = "";
2570         short i, count = 0;
2571
2572         if (is_top) {
2573                 if (metadata_is_valid(ibuf, str, 0, 0) || metadata_is_valid(ibuf, str, 1, 0)) {
2574                         count++;
2575                 }
2576                 for (i = 2; i < 5; i++) {
2577                         if (metadata_is_valid(ibuf, str, i, 0)) {
2578                                 if (i == 4) {
2579                                         struct {
2580                                                 struct ResultBLF info;
2581                                                 rctf rect;
2582                                         } wrap;
2583
2584                                         BLF_enable(fontid, BLF_WORD_WRAP);
2585                                         BLF_wordwrap(fontid, ibuf->x - (margin * 2));
2586                                         BLF_boundbox_ex(fontid, str, sizeof(str), &wrap.rect, &wrap.info);
2587                                         BLF_wordwrap(fontid, 0);
2588                                         BLF_disable(fontid, BLF_WORD_WRAP);
2589
2590                                         count += wrap.info.lines;
2591                                 }
2592                                 else {
2593                                         count++;
2594                                 }
2595                         }
2596                 }
2597         }
2598         else {
2599                 for (i = 5; i < 10; i++) {
2600                         if (metadata_is_valid(ibuf, str, i, 0)) {
2601                                 count = 1;
2602                         }
2603                 }
2604         }
2605
2606         if (count) {
2607                 return (height + margin) * count;
2608         }
2609
2610         return 0;
2611 }
2612
2613 #undef MAX_METADATA_STR
2614
2615 void ED_region_image_metadata_draw(int x, int y, ImBuf *ibuf, const rctf *frame, float zoomx, float zoomy)
2616 {
2617         float box_y;
2618         rctf rect;
2619         uiStyle *style = UI_style_get_dpi();
2620
2621         if (!ibuf->metadata)
2622                 return;
2623
2624         /* find window pixel coordinates of origin */
2625         gpuPushMatrix();
2626
2627         /* offset and zoom using ogl */
2628         gpuTranslate2f(x, y);
2629         gpuScale2f(zoomx, zoomy);
2630
2631         BLF_size(blf_mono_font, style->widgetlabel.points * 1.5f * U.pixelsize, U.dpi);
2632
2633         /* *** upper box*** */
2634
2635         /* get needed box height */
2636         box_y = metadata_box_height_get(ibuf, blf_mono_font, true);
2637
2638         if (box_y) {
2639                 /* set up rect */
2640                 BLI_rctf_init(&rect, frame->xmin, frame->xmax, frame->ymax, frame->ymax + box_y);
2641                 /* draw top box */
2642                 Gwn_VertFormat *format = immVertexFormat();
2643                 unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
2644                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2645                 immUniformThemeColor(TH_METADATA_BG);
2646                 immRectf(pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
2647                 immUnbindProgram();
2648
2649                 BLF_clipping(blf_mono_font, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
2650                 BLF_enable(blf_mono_font, BLF_CLIPPING);
2651
2652                 UI_FontThemeColor(blf_mono_font, TH_METADATA_TEXT);
2653                 metadata_draw_imbuf(ibuf, &rect, blf_mono_font, true);
2654
2655                 BLF_disable(blf_mono_font, BLF_CLIPPING);
2656         }
2657
2658
2659         /* *** lower box*** */
2660
2661         box_y = metadata_box_height_get(ibuf, blf_mono_font, false);
2662
2663         if (box_y) {
2664                 /* set up box rect */
2665                 BLI_rctf_init(&rect, frame->xmin, frame->xmax, frame->ymin - box_y, frame->ymin);
2666                 /* draw top box */
2667                 Gwn_VertFormat *format = immVertexFormat();
2668                 unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
2669                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2670                 immUniformThemeColor(TH_METADATA_BG);
2671                 immRectf(pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
2672                 immUnbindProgram();
2673
2674                 BLF_clipping(blf_mono_font, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
2675                 BLF_enable(blf_mono_font, BLF_CLIPPING);
2676
2677                 UI_FontThemeColor(blf_mono_font, TH_METADATA_TEXT);
2678                 metadata_draw_imbuf(ibuf, &rect, blf_mono_font, false);
2679
2680                 BLF_disable(blf_mono_font, BLF_CLIPPING);
2681         }
2682
2683         gpuPopMatrix();
2684 }
2685
2686 void ED_region_grid_draw(ARegion *ar, float zoomx, float zoomy)
2687 {
2688         float gridsize, gridstep = 1.0f / 32.0f;
2689         float fac, blendfac;
2690         int x1, y1, x2, y2;
2691
2692         /* the image is located inside (0, 0), (1, 1) as set by view2d */
2693         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
2694         UI_view2d_view_to_region(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
2695
2696         Gwn_VertFormat *format = immVertexFormat();
2697         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
2698         
2699         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2700         immUniformThemeColorShade(TH_BACK, 20);
2701         immRectf(pos, x1, y1, x2, y2);
2702         immUnbindProgram();
2703
2704         /* gridsize adapted to zoom level */
2705         gridsize = 0.5f * (zoomx + zoomy);
2706         if (gridsize <= 0.0f)
2707                 return;
2708
2709         if (gridsize < 1.0f) {
2710                 while (gridsize < 1.0f) {
2711                         gridsize *= 4.0f;
2712                         gridstep *= 4.0f;
2713                 }
2714         }
2715         else {
2716                 while (gridsize >= 4.0f) {
2717                         gridsize /= 4.0f;
2718                         gridstep /= 4.0f;
2719                 }
2720         }
2721
2722         blendfac = 0.25f * gridsize - floorf(0.25f * gridsize);
2723         CLAMP(blendfac, 0.0f, 1.0f);
2724
2725         int count_fine = 1.0f / gridstep;
2726         int count_large = 1.0f / (4.0f * gridstep);
2727
2728         if (count_fine > 0) {
2729                 GWN_vertformat_clear(format);
2730                 pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
2731                 unsigned color = GWN_vertformat_attr_add(format, "color", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
2732                 
2733                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
2734                 immBegin(GWN_PRIM_LINES, 4 * count_fine + 4 * count_large);
2735                 
2736                 float theme_color[3];
2737                 UI_GetThemeColorShade3fv(TH_BACK, (int)(20.0f * (1.0f - blendfac)), theme_color);
2738                 fac = 0.0f;
2739                 
2740                 /* the fine resolution level */
2741                 for (int i = 0; i < count_fine; i++) {
2742                         immAttrib3fv(color, theme_color);
2743                         immVertex2f(pos, x1, y1 * (1.0f - fac) + y2 * fac);
2744                         immAttrib3fv(color, theme_color);
2745                         immVertex2f(pos, x2, y1 * (1.0f - fac) + y2 * fac);
2746                         immAttrib3fv(color, theme_color);
2747                         immVertex2f(pos, x1 * (1.0f - fac) + x2 * fac, y1);
2748                         immAttrib3fv(color, theme_color);
2749                         immVertex2f(pos, x1 * (1.0f - fac) + x2 * fac, y2);
2750                         fac += gridstep;
2751                 }
2752
2753                 if (count_large > 0) {
2754                         UI_GetThemeColor3fv(TH_BACK, theme_color);
2755                         fac = 0.0f;
2756                         
2757                         /* the large resolution level */
2758                         for (int i = 0; i < count_large; i++) {
2759                                 immAttrib3fv(color, theme_color);
2760                                 immVertex2f(pos, x1, y1 * (1.0f - fac) + y2 * fac);
2761                                 immAttrib3fv(color, theme_color);
2762                                 immVertex2f(pos, x2, y1 * (1.0f - fac) + y2 * fac);
2763                                 immAttrib3fv(color, theme_color);
2764                                 immVertex2f(pos, x1 * (1.0f - fac) + x2 * fac, y1);
2765                                 immAttrib3fv(color, theme_color);
2766                                 immVertex2f(pos, x1 * (1.0f - fac) + x2 * fac, y2);
2767                                 fac += 4.0f * gridstep;
2768                         }
2769                 }
2770
2771                 immEnd();
2772                 immUnbindProgram();
2773         }
2774 }
2775
2776 /* If the area has overlapping regions, it returns visible rect for Region *ar */
2777 /* rect gets returned in local region coordinates */
2778 void ED_region_visible_rect(ARegion *ar, rcti *rect)
2779 {
2780         ARegion *arn = ar;
2781         
2782         /* allow function to be called without area */
2783         while (arn->prev)
2784                 arn = arn->prev;
2785         
2786         *rect = ar->winrct;
2787         
2788         /* check if a region overlaps with the current one */
2789         for (; arn; arn = arn->next) {
2790                 if (ar != arn && arn->overlap) {
2791                         if (BLI_rcti_isect(rect, &arn->winrct, NULL)) {
2792                                 
2793                                 /* overlap left, also check 1 pixel offset (2 regions on one side) */
2794                                 if (ABS(rect->xmin - arn->winrct.xmin) < 2)
2795                                         rect->xmin = arn->winrct.xmax;
2796
2797                                 /* overlap right */
2798                                 if (ABS(rect->xmax - arn->winrct.xmax) < 2)
2799                                         rect->xmax = arn->winrct.xmin;
2800                         }
2801                 }
2802         }
2803         BLI_rcti_translate(rect, -ar->winrct.xmin, -ar->winrct.ymin);
2804 }
2805
2806 /* Cache display helpers */
2807
2808 void ED_region_cache_draw_background(const ARegion *ar)
2809 {
2810         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
2811         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2812         immUniformColor4ub(128, 128, 255, 64);
2813         immRecti(pos, 0, 0, ar->winx, 8 * UI_DPI_FAC);
2814         immUnbindProgram();
2815 }
2816
2817 void ED_region_cache_draw_curfra_label(const int framenr, const float x, const float y)
2818 {
2819         uiStyle *style = UI_style_get();
2820         int fontid = style->widget.uifont_id;
2821         char numstr[32];
2822         float font_dims[2] = {0.0f, 0.0f};
2823
2824         /* frame number */
2825         BLF_size(fontid, 11.0f * U.pixelsize, U.dpi);
2826         BLI_snprintf(numstr, sizeof(numstr), "%d", framenr);
2827
2828         BLF_width_and_height(fontid, numstr, sizeof(numstr), &font_dims[0], &font_dims[1]);
2829
2830         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
2831         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2832         immUniformThemeColor(TH_CFRAME);
2833         immRecti(pos, x, y, x + font_dims[0] + 6.0f, y + font_dims[1] + 4.0f);
2834         immUnbindProgram();
2835
2836         UI_FontThemeColor(fontid, TH_TEXT);
2837         BLF_position(fontid, x + 2.0f, y + 2.0f, 0.0f);
2838         BLF_draw(fontid, numstr, sizeof(numstr));
2839 }
2840
2841 void ED_region_cache_draw_cached_segments(const ARegion *ar, const int num_segments, const int *points, const int sfra, const int efra)
2842 {
2843         if (num_segments) {
2844                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
2845                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2846                 immUniformColor4ub(128, 128, 255, 128);
2847
2848                 for (int a = 0; a < num_segments; a++) {
2849                         float x1 = (float)(points[a * 2] - sfra) / (efra - sfra + 1) * ar->winx;
2850                         float x2 = (float)(points[a * 2 + 1] - sfra + 1) / (efra - sfra + 1) * ar->winx;
2851
2852                         immRecti(pos, x1, 0, x2, 8 * UI_DPI_FAC);
2853                         /* TODO(merwin): use primitive restart to draw multiple rects more efficiently */
2854                 }
2855
2856                 immUnbindProgram();
2857         }
2858 }
2859
2860 /**
2861  * Generate subscriptions for this region.
2862  */
2863 void ED_region_message_subscribe(
2864         bContext *C,
2865         struct WorkSpace *workspace, struct Scene *scene,
2866         struct bScreen *screen, struct ScrArea *sa, struct ARegion *ar,
2867         struct wmMsgBus *mbus)
2868 {
2869         if (ar->manipulator_map != NULL) {
2870                 WM_manipulatormap_message_subscribe(C, ar->manipulator_map, ar, mbus);
2871         }
2872
2873         if (BLI_listbase_is_empty(&ar->uiblocks)) {
2874                 UI_region_message_subscribe(ar, mbus);
2875         }
2876
2877         if (ar->type->message_subscribe != NULL) {
2878                 ar->type->message_subscribe(C, workspace, scene, screen, sa, ar, mbus);
2879         }
2880 }
2881
2882 int ED_region_snap_size_test(const ARegion *ar)
2883 {
2884         /* Use a larger value because toggling scrollbars can jump in size. */
2885         const int snap_match_threshold = 16;
2886         if (ar->type->snap_size != NULL) {
2887                 return ((((ar->sizex - ar->type->snap_size(ar, ar->sizex, 0)) <= snap_match_threshold) << 0) |
2888                         (((ar->sizey - ar->type->snap_size(ar, ar->sizey, 1)) <= snap_match_threshold) << 1));
2889         }
2890         return 0;
2891 }
2892
2893 bool ED_region_snap_size_apply(ARegion *ar, int snap_flag)
2894 {
2895         bool changed = false;
2896         if (ar->type->snap_size != NULL) {
2897                 if (snap_flag & (1 << 0)) {
2898                         short snap_size = ar->type->snap_size(ar, ar->sizex, 0);
2899                         if (snap_size != ar->sizex) {
2900                                 ar->sizex = snap_size;
2901                                 changed = true;
2902                         }
2903                 }
2904                 if (snap_flag & (1 << 1)) {
2905                         short snap_size = ar->type->snap_size(ar, ar->sizey, 1);
2906                         if (snap_size != ar->sizey) {
2907                                 ar->sizey = snap_size;
2908                                 changed = true;
2909                         }
2910                 }
2911         }
2912         return changed;
2913 }