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