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