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