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