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