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