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