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