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