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