2.5: User Preferences
[blender.git] / source / blender / editors / screen / area.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_arithb.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_screen.h"
44 #include "BKE_utildefines.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48 #include "wm_subwindow.h"
49
50 #include "ED_screen.h"
51 #include "ED_screen_types.h"
52 #include "ED_types.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56 #include "BLF_api.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #ifndef DISABLE_PYTHON
63 #include "BPY_extern.h"
64 #endif
65
66 #include "screen_intern.h"
67
68 /* general area and region code */
69
70 static void region_draw_emboss(ARegion *ar, rcti *scirct)
71 {
72         rcti rect;
73         
74         /* translate scissor rect to region space */
75         rect.xmin= scirct->xmin - ar->winrct.xmin;
76         rect.ymin= scirct->ymin - ar->winrct.ymin;
77         rect.xmax= scirct->xmax - ar->winrct.xmin;
78         rect.ymax= scirct->ymax - ar->winrct.ymin;
79         
80         /* set transp line */
81         glEnable( GL_BLEND );
82         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
83         
84         /* right  */
85         glColor4ub(0,0,0, 50);
86         sdrawline(rect.xmax, rect.ymin, rect.xmax, rect.ymax);
87         
88         /* bottom  */
89         glColor4ub(0,0,0, 80);
90         sdrawline(rect.xmin, rect.ymin, rect.xmax, rect.ymin);
91         
92         /* top  */
93         glColor4ub(255,255,255, 60);
94         sdrawline(rect.xmin, rect.ymax, rect.xmax, rect.ymax);
95
96         /* left  */
97         glColor4ub(255,255,255, 50);
98         sdrawline(rect.xmin, rect.ymin, rect.xmin, rect.ymax);
99         
100         glDisable( GL_BLEND );
101 }
102
103 void ED_region_pixelspace(ARegion *ar)
104 {
105         int width= ar->winrct.xmax-ar->winrct.xmin+1;
106         int height= ar->winrct.ymax-ar->winrct.ymin+1;
107         
108         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
109         wmLoadIdentity();
110 }
111
112 /* only exported for WM */
113 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
114 {
115         /* generic notes first */
116         switch(note->category) {
117                 case NC_WM:
118                         if(note->data==ND_FILEREAD)
119                                 ED_region_tag_redraw(ar);
120                         break;
121                 case NC_WINDOW:
122                         ED_region_tag_redraw(ar);
123                         break;
124                 case NC_SCREEN:
125                         if(note->action==NA_EDITED)
126                                 ED_region_tag_redraw(ar);
127                         /* pass on */
128                 default:
129                         if(ar->type && ar->type->listener)
130                                 ar->type->listener(ar, note);
131         }
132 }
133
134 /* only exported for WM */
135 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
136 {
137         /* no generic notes? */
138         if(sa->type && sa->type->listener) {
139                 sa->type->listener(sa, note);
140         }
141 }
142
143 /* only exported for WM */
144 void ED_area_do_refresh(bContext *C, ScrArea *sa)
145 {
146         /* no generic notes? */
147         if(sa->type && sa->type->refresh) {
148                 sa->type->refresh(C, sa);
149         }
150         sa->do_refresh= 0;
151 }
152
153 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
154 /* only exported for WM */
155 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
156 {
157         AZone *az;
158         
159         for(az= sa->actionzones.first; az; az= az->next) {
160                 int xs, ys;
161                 
162                 if(az->type==AZONE_AREA) {
163                         xs= (az->x1+az->x2)/2;
164                         ys= (az->y1+az->y2)/2;
165                 }
166                 else {
167                         xs= az->x3;
168                         ys= az->y3;
169                 }
170
171                 /* test if inside */
172                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
173                         az->do_draw= 1;
174                 }
175         }
176 }
177
178 static void area_draw_azone(short x1, short y1, short x2, short y2)
179 {
180         float xmin = x1;
181         float xmax = x2-2;
182         float ymin = y1-1;
183         float ymax = y2-3;
184         
185         float dx= 0.3f*(xmax-xmin);
186         float dy= 0.3f*(ymax-ymin);
187         
188         glColor4ub(255, 255, 255, 80);
189         fdrawline(xmin, ymax, xmax, ymin);
190         fdrawline(xmin, ymax-dy, xmax-dx, ymin);
191         fdrawline(xmin, ymax-2*dy, xmax-2*dx, ymin);
192         
193         glColor4ub(0, 0, 0, 150);
194         fdrawline(xmin, ymax+1, xmax+1, ymin);
195         fdrawline(xmin, ymax-dy+1, xmax-dx+1, ymin);
196         fdrawline(xmin, ymax-2*dy+1, xmax-2*dx+1, ymin);
197 }
198
199 static void region_draw_azone(ScrArea *sa, AZone *az)
200 {
201         if(az->ar==NULL) return;
202         
203         UI_SetTheme(sa->spacetype, az->ar->type->regionid);
204         
205         UI_ThemeColor(TH_BACK);
206         glBegin(GL_TRIANGLES);
207         glVertex2s(az->x1, az->y1);
208         glVertex2s(az->x2, az->y2);
209         glVertex2s(az->x3, az->y3);
210         glEnd();
211         
212         UI_ThemeColorShade(TH_BACK, 50);
213         sdrawline(az->x1, az->y1, az->x3, az->y3);
214         
215         UI_ThemeColorShade(TH_BACK, -50);
216         sdrawline(az->x2, az->y2, az->x3, az->y3);
217
218 }
219
220
221 /* only exported for WM */
222 void ED_area_overdraw(bContext *C)
223 {
224         wmWindow *win= CTX_wm_window(C);
225         bScreen *screen= CTX_wm_screen(C);
226         ScrArea *sa;
227         
228         /* Draw AZones, in screenspace */
229         wmSubWindowSet(win, screen->mainwin);
230
231         glEnable( GL_BLEND );
232         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
233         
234         for(sa= screen->areabase.first; sa; sa= sa->next) {
235                 AZone *az;
236                 for(az= sa->actionzones.first; az; az= az->next) {
237                         if(az->do_draw) {
238                                 if(az->type==AZONE_AREA)
239                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
240                                 else if(az->type==AZONE_REGION)
241                                         region_draw_azone(sa, az);
242                                 
243                                 az->do_draw= 0;
244                         }
245                 }
246         }       
247         glDisable( GL_BLEND );
248         
249 }
250
251 /* get scissor rect, checking overlapping regions */
252 static void region_scissor_winrct(ARegion *ar, rcti *winrct)
253 {
254         *winrct= ar->winrct;
255         
256         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
257                 return;
258
259         while(ar->prev) {
260                 ar= ar->prev;
261                 
262                 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
263                         if(ar->flag & RGN_FLAG_HIDDEN);
264                         else if(ar->alignment & RGN_SPLIT_PREV);
265                         else if(ar->alignment==RGN_OVERLAP_LEFT) {
266                                 winrct->xmin= ar->winrct.xmax + 1;
267                         }
268                         else if(ar->alignment==RGN_OVERLAP_RIGHT) {
269                                 winrct->xmax= ar->winrct.xmin - 1;
270                         }
271                         else break;
272                 }
273         }
274 }
275
276 /* only exported for WM */
277 void ED_region_do_draw(bContext *C, ARegion *ar)
278 {
279         wmWindow *win= CTX_wm_window(C);
280         ScrArea *sa= CTX_wm_area(C);
281         ARegionType *at= ar->type;
282         rcti winrct;
283         
284         /* checks other overlapping regions */
285         region_scissor_winrct(ar, &winrct);
286         
287         /* if no partial draw rect set, full rect */
288         if(ar->drawrct.xmin == ar->drawrct.xmax)
289                 ar->drawrct= winrct;
290         else {
291                 /* extra clip for safety */
292                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
293                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
294                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
295                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
296         }
297         
298         /* note; this sets state, so we can use wmOrtho and friends */
299         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
300         
301         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
302         
303         /* optional header info instead? */
304         if(ar->headerstr) {
305                 float col[3];
306                 UI_GetThemeColor3fv(TH_HEADER, col);
307                 glClearColor(col[0], col[1], col[2], 0.0);
308                 glClear(GL_COLOR_BUFFER_BIT);
309                 
310                 UI_ThemeColor(TH_TEXT);
311                 BLF_draw_default(20, 6, 0.0f, ar->headerstr);
312         }
313         else if(at->draw) {
314                 at->draw(C, ar);
315         }
316         
317         uiFreeInactiveBlocks(C, &ar->uiblocks);
318         
319         if(sa)
320                 region_draw_emboss(ar, &winrct);
321         
322         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
323         ED_region_pixelspace(ar);
324         
325         ar->do_draw= 0;
326         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
327 }
328
329 /* **********************************
330    maybe silly, but let's try for now
331    to keep these tags protected
332    ********************************** */
333
334 void ED_region_tag_redraw(ARegion *ar)
335 {
336         if(ar) {
337                 /* zero region means full region redraw */
338                 ar->do_draw= 1;
339                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
340         }
341 }
342
343 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
344 {
345         if(ar) {
346                 if(!ar->do_draw) {
347                         /* no redraw set yet, set partial region */
348                         ar->do_draw= 1;
349                         ar->drawrct= *rct;
350                 }
351                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
352                         /* partial redraw already set, expand region */
353                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
354                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
355                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
356                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
357                 }
358         }
359 }
360
361 void ED_area_tag_redraw(ScrArea *sa)
362 {
363         ARegion *ar;
364         
365         if(sa)
366                 for(ar= sa->regionbase.first; ar; ar= ar->next)
367                         ED_region_tag_redraw(ar);
368 }
369
370 void ED_area_tag_refresh(ScrArea *sa)
371 {
372         if(sa)
373                 sa->do_refresh= 1;
374 }
375
376 /* *************************************************************** */
377
378 /* use NULL to disable it */
379 void ED_area_headerprint(ScrArea *sa, const char *str)
380 {
381         ARegion *ar;
382         
383         for(ar= sa->regionbase.first; ar; ar= ar->next) {
384                 if(ar->regiontype==RGN_TYPE_HEADER) {
385                         if(str) {
386                                 if(ar->headerstr==NULL)
387                                         ar->headerstr= MEM_mallocN(256, "headerprint");
388                                 BLI_strncpy(ar->headerstr, str, 256);
389                         }
390                         else if(ar->headerstr) {
391                                 MEM_freeN(ar->headerstr);
392                                 ar->headerstr= NULL;
393                         }
394                         ED_region_tag_redraw(ar);
395                 }
396         }
397 }
398
399 /* ************************************************************ */
400
401
402 #define AZONESPOT               12
403 static void area_azone_initialize(ScrArea *sa) 
404 {
405         AZone *az;
406         
407         /* reinitalize entirely, regions add azones too */
408         BLI_freelistN(&sa->actionzones);
409         
410         /* set area action zones */
411         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
412         BLI_addtail(&(sa->actionzones), az);
413         az->type= AZONE_AREA;
414         az->x1= sa->totrct.xmin;
415         az->y1= sa->totrct.ymin;
416         az->x2= sa->totrct.xmin + AZONESPOT-1;
417         az->y2= sa->totrct.ymin + AZONESPOT-1;
418         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
419         
420         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
421         BLI_addtail(&(sa->actionzones), az);
422         az->type= AZONE_AREA;
423         az->x1= sa->totrct.xmax+1;
424         az->y1= sa->totrct.ymax+1;
425         az->x2= sa->totrct.xmax-AZONESPOT+1;
426         az->y2= sa->totrct.ymax-AZONESPOT+1;
427         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
428 }
429
430 static void region_azone_initialize(ScrArea *sa, ARegion *ar, char edge) 
431 {
432         AZone *az, *azt;
433         
434         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
435         BLI_addtail(&(sa->actionzones), az);
436         az->type= AZONE_REGION;
437         az->ar= ar;
438         az->edge= edge;
439         
440         if(edge=='t') {
441                 az->x1= ar->winrct.xmin+AZONESPOT;
442                 az->y1= ar->winrct.ymax;
443                 az->x2= ar->winrct.xmin+2*AZONESPOT;
444                 az->y2= ar->winrct.ymax;
445                 az->x3= (az->x1+az->x2)/2;
446                 az->y3= az->y2+AZONESPOT/2;
447                 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y3);
448         }
449         else if(edge=='b') {
450                 az->x1= ar->winrct.xmin+AZONESPOT;
451                 az->y1= ar->winrct.ymin;
452                 az->x2= ar->winrct.xmin+2*AZONESPOT;
453                 az->y2= ar->winrct.ymin;
454                 az->x3= (az->x1+az->x2)/2;
455                 az->y3= az->y2-AZONESPOT/2;
456                 BLI_init_rcti(&az->rect, az->x1, az->x2, az->y3, az->y1);
457         }
458         else if(edge=='l') {
459                 az->x1= ar->winrct.xmin;
460                 az->y1= ar->winrct.ymax-AZONESPOT;
461                 az->x2= ar->winrct.xmin;
462                 az->y2= ar->winrct.ymax-2*AZONESPOT;
463                 az->x3= az->x2-AZONESPOT/2;
464                 az->y3= (az->y1+az->y2)/2;
465                 BLI_init_rcti(&az->rect, az->x3, az->x1, az->y1, az->y2);
466         }
467         else { // if(edge=='r') {
468                 az->x1= ar->winrct.xmax;
469                 az->y1= ar->winrct.ymax-AZONESPOT;
470                 az->x2= ar->winrct.xmax;
471                 az->y2= ar->winrct.ymax-2*AZONESPOT;
472                 az->x3= az->x2+AZONESPOT/2;
473                 az->y3= (az->y1+az->y2)/2;
474                 BLI_init_rcti(&az->rect, az->x1, az->x3, az->y1, az->y2);
475         }
476         
477         /* if more azones on 1 spot, set offset */
478         for(azt= sa->actionzones.first; azt; azt= azt->next) {
479                 if(az!=azt) {
480                         if(az->x1==azt->x1 && az->y1==azt->y1) {
481                                 if(edge=='t' || edge=='b') {
482                                         az->x1+= AZONESPOT;
483                                         az->x2+= AZONESPOT;
484                                         az->x3+= AZONESPOT;
485                                         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y3);
486                                 }
487                                 else {
488                                         az->y1-= AZONESPOT;
489                                         az->y2-= AZONESPOT;
490                                         az->y3-= AZONESPOT;
491                                         BLI_init_rcti(&az->rect, az->x1, az->x3, az->y1, az->y2);
492                                 }
493                         }
494                 }
495         }
496         
497 }
498
499
500 /* *************************************************************** */
501
502 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
503 {
504          /* edge code (t b l r) is where azone will be drawn */
505         
506         if(alignment==RGN_ALIGN_TOP)
507                 region_azone_initialize(sa, ar, 'b');
508         else if(alignment==RGN_ALIGN_BOTTOM)
509                 region_azone_initialize(sa, ar, 't');
510         else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
511                 region_azone_initialize(sa, ar, 'l');
512         else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
513                 region_azone_initialize(sa, ar, 'r');
514                                                                 
515 }
516
517 /* dir is direction to check, not the splitting edge direction! */
518 static int rct_fits(rcti *rect, char dir, int size)
519 {
520         if(dir=='h') {
521                 return rect->xmax-rect->xmin - size;
522         }
523         else { // 'v'
524                 return rect->ymax-rect->ymin - size;
525         }
526 }
527
528 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
529 {
530         rcti *remainder_prev= remainder;
531         int prefsizex, prefsizey;
532         int alignment;
533         
534         if(ar==NULL)
535                 return;
536         
537         /* no returns in function, winrct gets set in the end again */
538         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
539         
540         /* for test; allow split of previously defined region */
541         if(ar->alignment & RGN_SPLIT_PREV)
542                 if(ar->prev)
543                         remainder= &ar->prev->winrct;
544         
545         alignment = ar->alignment & ~RGN_SPLIT_PREV;
546         
547         /* clear state flags first */
548         ar->flag &= ~RGN_FLAG_TOO_SMALL;
549         /* user errors */
550         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
551                 alignment= RGN_ALIGN_NONE;
552         
553         prefsizex= ar->type->minsizex;
554         prefsizey= ar->type->minsizey;
555         
556         /* hidden is user flag */
557         if(ar->flag & RGN_FLAG_HIDDEN);
558         /* XXX floating area region, not handled yet here */
559         else if(alignment == RGN_ALIGN_FLOAT);
560         /* remainder is too small for any usage */
561         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
562                 ar->flag |= RGN_FLAG_TOO_SMALL;
563         }
564         else if(alignment==RGN_ALIGN_NONE) {
565                 /* typically last region */
566                 ar->winrct= *remainder;
567                 BLI_init_rcti(remainder, 0, 0, 0, 0);
568         }
569         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
570                 
571                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
572                         ar->flag |= RGN_FLAG_TOO_SMALL;
573                 }
574                 else {
575                         int fac= rct_fits(remainder, 'v', prefsizey);
576
577                         if(fac < 0 )
578                                 prefsizey += fac;
579                         
580                         ar->winrct= *remainder;
581                         
582                         if(alignment==RGN_ALIGN_TOP) {
583                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
584                                 remainder->ymax= ar->winrct.ymin - 1;
585                         }
586                         else {
587                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
588                                 remainder->ymin= ar->winrct.ymax + 1;
589                         }
590                 }
591         }
592         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
593                 
594                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
595                         ar->flag |= RGN_FLAG_TOO_SMALL;
596                 }
597                 else {
598                         int fac= rct_fits(remainder, 'h', prefsizex);
599                         
600                         if(fac < 0 )
601                                 prefsizex += fac;
602                         
603                         ar->winrct= *remainder;
604                         
605                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
606                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
607                                 if(alignment==RGN_ALIGN_RIGHT)
608                                         remainder->xmax= ar->winrct.xmin - 1;
609                         }
610                         else {
611                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
612                                 if(alignment==RGN_ALIGN_LEFT)
613                                         remainder->xmin= ar->winrct.xmax + 1;
614                         }
615                 }
616         }
617         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
618                 /* percentage subdiv*/
619                 ar->winrct= *remainder;
620                 
621                 if(alignment==RGN_ALIGN_HSPLIT) {
622                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
623                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
624                                 remainder->xmin= ar->winrct.xmax+1;
625                         }
626                         else {
627                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
628                         }
629                 }
630                 else {
631                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
632                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
633                                 remainder->ymin= ar->winrct.ymax+1;
634                         }
635                         else {
636                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
637                         }
638                 }
639         }
640         else if(alignment==RGN_ALIGN_QSPLIT) {
641                 ar->winrct= *remainder;
642                 
643                 /* test if there's still 4 regions left */
644                 if(quad==0) {
645                         ARegion *artest= ar->next;
646                         int count= 1;
647                         
648                         while(artest) {
649                                 artest->alignment= RGN_ALIGN_QSPLIT;
650                                 artest= artest->next;
651                                 count++;
652                         }
653                         
654                         if(count!=4) {
655                                 /* let's stop adding regions */
656                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
657                                 printf("region quadsplit failed\n");
658                         }
659                         else quad= 1;
660                 }
661                 if(quad) {
662                         if(quad==1) { /* left bottom */
663                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
664                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
665                         }
666                         else if(quad==2) { /* left top */
667                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
668                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
669                         }
670                         else if(quad==3) { /* right bottom */
671                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
672                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
673                         }
674                         else {  /* right top */
675                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
676                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
677                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
678                         }
679
680                         quad++;
681                 }
682         }
683         
684         /* for speedup */
685         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
686         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
687         
688         /* restore test exception */
689         if(ar->alignment & RGN_SPLIT_PREV) {
690                 if(ar->prev) {
691                         remainder= remainder_prev;
692                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
693                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
694                 }
695         }
696
697         /* set winrect for azones */
698         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
699                 ar->winrct= *remainder;
700                 
701                 if(alignment==RGN_ALIGN_TOP)
702                         ar->winrct.ymin= ar->winrct.ymax;
703                 else if(alignment==RGN_ALIGN_BOTTOM)
704                         ar->winrct.ymax= ar->winrct.ymin;
705                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
706                         ar->winrct.xmin= ar->winrct.xmax;
707                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
708                         ar->winrct.xmax= ar->winrct.xmin;
709                 else /* prevent winrct to be valid */
710                         ar->winrct.xmax= ar->winrct.xmin;
711         }
712         /* in end, add azones, where appropriate */
713         region_azone_add(sa, ar, alignment);
714
715
716         region_rect_recursive(sa, ar->next, remainder, quad);
717 }
718
719 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
720 {
721         short rt= CLAMPIS(G.rt, 0, 16);
722
723         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
724         else sa->totrct.xmin= sa->v1->vec.x;
725         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
726         else sa->totrct.xmax= sa->v4->vec.x;
727         
728         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
729         else sa->totrct.ymin= sa->v1->vec.y;
730         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
731         else sa->totrct.ymax= sa->v2->vec.y;
732         
733         /* for speedup */
734         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
735         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
736 }
737
738
739 /* used for area initialize below */
740 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
741 {
742         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
743                 if(ar->swinid)
744                         wm_subwindow_close(win, ar->swinid);
745                 ar->swinid= 0;
746         }
747         else if(ar->swinid==0)
748                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
749         else 
750                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
751 }
752
753 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
754 {
755         /* note, add-handler checks if it already exists */
756         
757         // XXX it would be good to have boundbox checks for some of these...
758         if(flag & ED_KEYMAP_UI) {
759                 UI_add_region_handlers(handlers);
760         }
761         if(flag & ED_KEYMAP_VIEW2D) {
762                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
763                 WM_event_add_keymap_handler(handlers, keymap);
764         }
765         if(flag & ED_KEYMAP_MARKERS) {
766                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
767                 WM_event_add_keymap_handler(handlers, keymap);
768                 // XXX need boundbox check urgently!!!
769         }
770         if(flag & ED_KEYMAP_ANIMATION) {
771                 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
772                 WM_event_add_keymap_handler(handlers, keymap);
773         }
774         if(flag & ED_KEYMAP_FRAMES) {
775                 ListBase *keymap= WM_keymap_listbase(wm, "Frames", 0, 0);
776                 WM_event_add_keymap_handler(handlers, keymap);
777         }
778 }
779
780
781 /* called in screen_refresh, or screens_init, also area size changes */
782 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
783 {
784         ARegion *ar;
785         rcti rect;
786         
787         /* set typedefinitions */
788         sa->type= BKE_spacetype_from_id(sa->spacetype);
789         
790         if(sa->type==NULL) {
791                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
792                 sa->type= BKE_spacetype_from_id(sa->spacetype);
793         }
794         
795         for(ar= sa->regionbase.first; ar; ar= ar->next)
796                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
797         
798         /* area sizes */
799         area_calc_totrct(sa, win->sizex, win->sizey);
800         
801         /* clear all azones, add the area triange widgets */
802         area_azone_initialize(sa);
803
804         /* region rect sizes */
805         rect= sa->totrct;
806         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
807         
808         /* default area handlers */
809         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
810         /* checks spacedata, adds own handlers */
811         if(sa->type->init)
812                 sa->type->init(wm, sa);
813         
814         /* region windows, default and own handlers */
815         for(ar= sa->regionbase.first; ar; ar= ar->next) {
816                 region_subwindow(wm, win, ar);
817                 
818                 if(ar->swinid) {
819                         /* default region handlers */
820                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
821
822                         if(ar->type->init)
823                                 ar->type->init(wm, ar);
824                 }
825                 else {
826                         /* prevent uiblocks to run */
827                         uiFreeBlocks(NULL, &ar->uiblocks);      
828                 }
829                 
830         }
831 }
832
833 /* externally called for floating regions like menus */
834 void ED_region_init(bContext *C, ARegion *ar)
835 {
836 //      ARegionType *at= ar->type;
837         
838         /* refresh can be called before window opened */
839         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
840         
841         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
842         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
843         
844         /* UI convention */
845         wmLoadIdentity();
846         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
847         
848 }
849
850
851 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
852 /* area vertices were set */
853 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
854 {
855         SpaceType *st;
856         ARegion *ar;
857         
858         sa1->headertype= sa2->headertype;
859         sa1->spacetype= sa2->spacetype;
860         sa1->butspacetype= sa2->butspacetype;
861         
862         if(swap_space == 1) {
863                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
864                 /* exception: ensure preview is reset */
865 //              if(sa1->spacetype==SPACE_VIEW3D)
866 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
867         }
868         else if (swap_space == 2) {
869                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
870         }
871         else {
872                 BKE_spacedata_freelist(&sa1->spacedata);
873                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
874         }
875         
876         /* Note; SPACE_EMPTY is possible on new screens */
877         
878         /* regions */
879         if(swap_space<2) {
880                 st= BKE_spacetype_from_id(sa1->spacetype);
881                 for(ar= sa1->regionbase.first; ar; ar= ar->next)
882                         BKE_area_region_free(st, ar);
883                 BLI_freelistN(&sa1->regionbase);
884         }
885         
886         st= BKE_spacetype_from_id(sa2->spacetype);
887         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
888                 ARegion *newar= BKE_area_region_copy(st, ar);
889                 BLI_addtail(&sa1->regionbase, newar);
890         }
891                 
892 #ifndef DISABLE_PYTHON
893         /* scripts */
894         BPY_free_scriptlink(&sa1->scriptlink);
895         sa1->scriptlink= sa2->scriptlink;
896         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
897 #endif
898 }
899
900 /* *********** Space switching code *********** */
901
902 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
903 {
904         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
905
906         ED_area_exit(C, sa1);
907         ED_area_exit(C, sa2);
908
909         tmp->spacetype= sa1->spacetype;
910         tmp->butspacetype= sa1->butspacetype;
911         BKE_spacedata_copyfirst(&tmp->spacedata, &sa1->spacedata);
912
913         area_copy_data(tmp, sa1, 2);
914         area_copy_data(sa1, sa2, 0);
915         area_copy_data(sa2, tmp, 0);
916         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
917         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
918
919         BKE_screen_area_free(tmp);
920         MEM_freeN(tmp);
921
922         /* tell WM to refresh, cursor types etc */
923         WM_event_add_mousemove(C);
924         
925         ED_area_tag_redraw(sa1);
926         ED_area_tag_refresh(sa1);
927         ED_area_tag_redraw(sa2);
928         ED_area_tag_refresh(sa2);
929 }
930
931 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
932 {
933         if(sa->spacetype != type) {
934                 SpaceType *st;
935                 SpaceLink *slold;
936                 SpaceLink *sl;
937
938                 ED_area_exit(C, sa);
939
940                 st= BKE_spacetype_from_id(type);
941                 slold= sa->spacedata.first;
942
943                 sa->spacetype= type;
944                 sa->butspacetype= type;
945                 sa->type= st;
946                 
947                 /* check previously stored space */
948                 for (sl= sa->spacedata.first; sl; sl= sl->next)
949                         if(sl->spacetype==type)
950                                 break;
951                 
952                 /* old spacedata... happened during work on 2.50, remove */
953                 if(sl && sl->regionbase.first==NULL) {
954                         st->free(sl);
955                         BLI_freelinkN(&sa->spacedata, sl);
956                         sl= NULL;
957                 }
958                 
959                 if (sl) {
960                         
961                         /* swap regions */
962                         slold->regionbase= sa->regionbase;
963                         sa->regionbase= sl->regionbase;
964                         sl->regionbase.first= sl->regionbase.last= NULL;
965                         
966                         /* put in front of list */
967                         BLI_remlink(&sa->spacedata, sl);
968                         BLI_addhead(&sa->spacedata, sl);
969                 } 
970                 else {
971                         /* new space */
972                         if(st) {
973                                 sl= st->new(C);
974                                 BLI_addhead(&sa->spacedata, sl);
975                                 
976                                 /* swap regions */
977                                 if(slold)
978                                         slold->regionbase= sa->regionbase;
979                                 sa->regionbase= sl->regionbase;
980                                 sl->regionbase.first= sl->regionbase.last= NULL;
981                         }
982                 }
983                 
984                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
985                 
986                 /* tell WM to refresh, cursor types etc */
987                 WM_event_add_mousemove(C);
988                 
989                 ED_area_tag_redraw(sa);
990                 ED_area_tag_refresh(sa);
991         }
992 }
993
994 void ED_area_prevspace(bContext *C)
995 {
996         SpaceLink *sl= CTX_wm_space_data(C);
997         ScrArea *sa= CTX_wm_area(C);
998
999         /* cleanup */
1000 #if 0 // XXX needs to be space type specific
1001         if(sfile->spacetype==SPACE_FILE) {
1002                 if(sfile->pupmenu) {
1003                         MEM_freeN(sfile->pupmenu);
1004                         sfile->pupmenu= NULL;
1005                 }
1006         }
1007 #endif
1008
1009         if(sl->next) {
1010
1011 #if 0 // XXX check whether this is still needed
1012                 if (sfile->spacetype == SPACE_SCRIPT) {
1013                         SpaceScript *sc = (SpaceScript *)sfile;
1014                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
1015                 }
1016 #endif
1017
1018                 ED_area_newspace(C, sa, sl->next->spacetype);
1019         }
1020         else {
1021                 ED_area_newspace(C, sa, SPACE_INFO);
1022         }
1023         ED_area_tag_redraw(sa);
1024 }
1025
1026 static char *windowtype_pup(void)
1027 {
1028         return(
1029                    "Window type:%t" //14
1030                    "|3D View %x1" //30
1031                    
1032                    "|%l" // 33
1033                    
1034                    "|Graph Editor %x2" //54
1035                    "|DopeSheet %x12" //73
1036                    "|NLA Editor %x13" //94
1037                    
1038                    "|%l" //97
1039                    
1040                    "|UV/Image Editor %x6" //117
1041                    
1042                    "|Video Sequence Editor %x8" //143
1043                    "|Timeline %x15" //163
1044                    "|Audio Window %x11" //163
1045                    "|Text Editor %x9" //179
1046                    
1047                    "|%l" //192
1048                    
1049                    
1050                    "|User Preferences %x7" //213
1051                    "|Outliner %x3" //232
1052                    "|Buttons Window %x4" //251
1053                    "|Node Editor %x16"
1054                    "|Logic Editor %x17"
1055                    "|%l" //254
1056                    
1057                    "|File Browser %x5" //290
1058                    
1059                    "|%l" //293
1060                    
1061                    "|Scripts Window %x14"//313
1062                    );
1063 }
1064
1065 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
1066 {
1067         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1068         ED_area_tag_redraw(CTX_wm_area(C));
1069 }
1070
1071 /* returns offset for next button in header */
1072 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1073 {
1074         ScrArea *sa= CTX_wm_area(C);
1075         uiBut *but;
1076         int xco= 8;
1077         
1078         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1079                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
1080                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1081                                                    "Displays Current Window Type. "
1082                                                    "Click for menu of available types.");
1083         uiButSetFunc(but, spacefunc, NULL, NULL);
1084         
1085         xco += XIC + 14;
1086         
1087         uiBlockSetEmboss(block, UI_EMBOSSN);
1088         if (sa->flag & HEADER_NO_PULLDOWN) {
1089                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1090                                                  ICON_DISCLOSURE_TRI_RIGHT,
1091                                                  xco,yco,XIC,YIC-2,
1092                                                  &(sa->flag), 0, 0, 0, 0, 
1093                                                  "Show pulldown menus");
1094         }
1095         else {
1096                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1097                                                  ICON_DISCLOSURE_TRI_DOWN,
1098                                                  xco,yco,XIC,YIC-2,
1099                                                  &(sa->flag), 0, 0, 0, 0, 
1100                                                  "Hide pulldown menus");
1101         }
1102         xco+=XIC;
1103         
1104         uiBlockSetEmboss(block, UI_EMBOSS);
1105         
1106         return xco;
1107 }
1108
1109 /************************ standard UI regions ************************/
1110
1111 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, char *context)
1112 {
1113         ScrArea *sa= CTX_wm_area(C);
1114         uiStyle *style= U.uistyles.first;
1115         uiBlock *block;
1116         PanelType *pt;
1117         Panel *panel;
1118         View2D *v2d= &ar->v2d;
1119         View2DScrollers *scrollers;
1120         float col[3];
1121         int xco, yco, x, y, miny=0, w, em, header, triangle, open;
1122
1123         if(vertical) {
1124                 w= v2d->cur.xmax - v2d->cur.xmin;
1125                 em= (ar->type->minsizex)? 10: 20;
1126         }
1127         else {
1128                 w= UI_PANEL_WIDTH;
1129                 em= (ar->type->minsizex)? 10: 20;
1130         }
1131
1132         x= 0;
1133         y= -style->panelouter;
1134
1135         /* create panels */
1136         uiBeginPanels(C, ar);
1137
1138         /* set view2d view matrix for scrolling (without scrollers) */
1139         UI_view2d_view_ortho(C, v2d);
1140
1141         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1142                 /* verify context */
1143                 if(context)
1144                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1145                                 continue;
1146
1147                 /* draw panel */
1148                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1149                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1150                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1151
1152                         /* bad fixed values */
1153                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1154                         triangle= 22;
1155
1156                         if(vertical)
1157                                 y -= header;
1158
1159                         if(pt->draw_header && header && (open || vertical)) {
1160                                 /* for enabled buttons */
1161                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1162                                         triangle, header+style->panelspace, header, 1, style);
1163
1164                                 pt->draw_header(C, panel);
1165
1166                                 uiBlockLayoutResolve(C, block, &xco, &yco);
1167                                 panel->labelofs= xco - triangle;
1168                                 panel->layout= NULL;
1169                         }
1170
1171                         if(open) {
1172                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL,
1173                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1174
1175                                 pt->draw(C, panel);
1176
1177                                 uiBlockLayoutResolve(C, block, &xco, &yco);
1178                                 panel->layout= NULL;
1179
1180                                 yco -= 2*style->panelspace;
1181                                 uiEndPanel(block, w, -yco);
1182                         }
1183                         else {
1184                                 yco= 0;
1185                                 uiEndPanel(block, w, 0);
1186                         }
1187
1188                         uiEndBlock(C, block);
1189
1190                         if(vertical) {
1191                                 if(pt->flag & PNL_NO_HEADER)
1192                                         y += yco;
1193                                 else
1194                                         y += yco-style->panelouter;
1195                         }
1196                         else {
1197                                 x += w;
1198                                 miny= MIN2(y, yco-style->panelouter-header);
1199                         }
1200                 }
1201         }
1202
1203         if(vertical)
1204                 x += w;
1205         else
1206                 y= miny;
1207         
1208         /* in case there are no panels */
1209         if(x == 0 || y == 0) {
1210                 x= UI_PANEL_WIDTH;
1211                 y= UI_PANEL_WIDTH;
1212         }
1213
1214         /* clear */
1215         UI_GetThemeColor3fv(TH_BACK, col);
1216         glClearColor(col[0], col[1], col[2], 0.0);
1217         glClear(GL_COLOR_BUFFER_BIT);
1218
1219         /* before setting the view */
1220         if(vertical) {
1221                 v2d->keepofs |= V2D_LOCKOFS_X;
1222                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
1223                 
1224                 // don't jump back when panels close or hide
1225                 y= MAX2(-y, -v2d->cur.ymin);
1226         }
1227         else {
1228                 v2d->keepofs &= ~V2D_LOCKOFS_X;
1229                 v2d->keepofs |= V2D_LOCKOFS_Y;
1230
1231                 // don't jump back when panels close or hide
1232                 x= MAX2(x, v2d->cur.xmax);
1233                 y= -y;
1234         }
1235
1236         UI_view2d_totRect_set(v2d, x, y);
1237
1238         /* set the view */
1239         UI_view2d_view_ortho(C, v2d);
1240
1241         /* this does the actual drawing! */
1242         uiEndPanels(C, ar);
1243         
1244         /* restore view matrix */
1245         UI_view2d_view_restore(C);
1246         
1247         /* scrollers */
1248         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1249         UI_view2d_scrollers_draw(C, v2d, scrollers);
1250         UI_view2d_scrollers_free(scrollers);
1251 }
1252
1253 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1254 {
1255         ListBase *keymap;
1256         
1257         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1258         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1259         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1260                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1261         
1262         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1263
1264         keymap= WM_keymap_listbase(wm, "View2D Buttons List", 0, 0);
1265         WM_event_add_keymap_handler(&ar->handlers, keymap);
1266 }
1267
1268 void ED_region_header(const bContext *C, ARegion *ar)
1269 {
1270         uiStyle *style= U.uistyles.first;
1271         uiBlock *block;
1272         uiLayout *layout;
1273         HeaderType *ht;
1274         Header header = {0};
1275         float col[3];
1276         int xco, yco;
1277
1278         /* clear */
1279         if(ED_screen_area_active(C))
1280                 UI_GetThemeColor3fv(TH_HEADER, col);
1281         else
1282                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
1283         
1284         glClearColor(col[0], col[1], col[2], 0.0);
1285         glClear(GL_COLOR_BUFFER_BIT);
1286         
1287         /* set view2d view matrix for scrolling (without scrollers) */
1288         UI_view2d_view_ortho(C, &ar->v2d);
1289
1290         xco= 8;
1291         yco= HEADERY-3;
1292
1293         /* draw all headers types */
1294         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1295                 block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
1296                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1297
1298                 if(ht->draw) {
1299                         header.type= ht;
1300                         header.layout= layout;
1301                         ht->draw(C, &header);
1302                 }
1303
1304                 uiBlockLayoutResolve(C, block, &xco, &yco);
1305                 uiEndBlock(C, block);
1306                 uiDrawBlock(C, block);
1307         }
1308
1309         /* always as last  */
1310         UI_view2d_totRect_set(&ar->v2d, xco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1311
1312         /* restore view matrix? */
1313         UI_view2d_view_restore(C);
1314 }
1315
1316 void ED_region_header_init(ARegion *ar)
1317 {
1318         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1319 }
1320