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