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