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