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