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