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