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