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