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