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