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