Cycles: svn merge -r37904:37957 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender-staging.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/editors/screen/area.c
30  *  \ingroup edscr
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_rand.h"
44 #include "BLI_utildefines.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 void ED_region_pixelspace(ARegion *ar)
73 {
74         int width= ar->winrct.xmax-ar->winrct.xmin+1;
75         int height= ar->winrct.ymax-ar->winrct.ymin+1;
76         
77         wmOrtho2(-0.375f, (float)width-0.375f, -0.375f, (float)height-0.375f);
78         glLoadIdentity();
79 }
80
81 /* only exported for WM */
82 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
83 {
84         /* generic notes first */
85         switch(note->category) {
86                 case NC_WM:
87                         if(note->data==ND_FILEREAD)
88                                 ED_region_tag_redraw(ar);
89                         break;
90                 case NC_WINDOW:
91                         ED_region_tag_redraw(ar);
92                         break;
93         }
94
95         if(ar->type && ar->type->listener)
96                 ar->type->listener(ar, note);
97 }
98
99 /* only exported for WM */
100 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
101 {
102         /* no generic notes? */
103         if(sa->type && sa->type->listener) {
104                 sa->type->listener(sa, note);
105         }
106 }
107
108 /* only exported for WM */
109 void ED_area_do_refresh(bContext *C, ScrArea *sa)
110 {
111         /* no generic notes? */
112         if(sa->type && sa->type->refresh) {
113                 sa->type->refresh(C, sa);
114         }
115         sa->do_refresh= 0;
116 }
117
118 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
119 /* only exported for WM */
120 void ED_area_overdraw_flush(ScrArea *sa, ARegion *ar)
121 {
122         AZone *az;
123         
124         for(az= sa->actionzones.first; az; az= az->next) {
125                 int xs, ys;
126                 
127                 xs= (az->x1+az->x2)/2;
128                 ys= (az->y1+az->y2)/2;
129
130                 /* test if inside */
131                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
132                         az->do_draw= 1;
133                 }
134         }
135 }
136
137 static void area_draw_azone(short x1, short y1, short x2, short y2)
138 {
139         int dx= floor(0.9f*(x2-x1));
140         int dy= floor(0.9f*(y2-y1));
141
142         glColor4f(0.0f, 0.0f, 0.0f, 0.25f);
143         glBegin(GL_TRIANGLES);
144         glVertex2f(x1, y1);
145         glVertex2f(x1+dx, y1);
146         glVertex2f(x1, y1+dy);
147         glEnd();
148 }
149
150 static void region_draw_azone(AZone *az)
151 {
152         GLUquadricObj *qobj = NULL; 
153         short midx = az->x1 + (az->x2 - az->x1)/2;
154         short midy = az->y1 + (az->y2 - az->y1)/2;
155         
156         if(az->ar==NULL) return;
157         
158         /* only display action zone icons when the region is hidden */
159         if (!(az->ar->flag & RGN_FLAG_HIDDEN)) return;
160         
161         qobj = gluNewQuadric();
162         
163         glPushMatrix();         
164         glTranslatef(midx, midy, 0.); 
165         
166         /* outlined circle */
167         glEnable(GL_LINE_SMOOTH);
168
169         glColor4f(1.f, 1.f, 1.f, 0.8f);
170
171         gluQuadricDrawStyle(qobj, GLU_FILL); 
172         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
173
174         glColor4f(0.2f, 0.2f, 0.2f, 0.9f);
175         
176         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
177         gluDisk( qobj, 0.0,  4.25f, 16, 1); 
178         
179         glDisable(GL_LINE_SMOOTH);
180         
181         glPopMatrix();
182         gluDeleteQuadric(qobj);
183         
184         /* + */
185         sdrawline(midx, midy-2, midx, midy+3);
186         sdrawline(midx-2, midy, midx+3, midy);
187 }
188
189
190 /* only exported for WM */
191 void ED_area_overdraw(bContext *C)
192 {
193         wmWindow *win= CTX_wm_window(C);
194         bScreen *screen= CTX_wm_screen(C);
195         ScrArea *sa;
196         
197         /* Draw AZones, in screenspace */
198         wmSubWindowSet(win, screen->mainwin);
199
200         glEnable( GL_BLEND );
201         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
202         
203         for(sa= screen->areabase.first; sa; sa= sa->next) {
204                 AZone *az;
205                 for(az= sa->actionzones.first; az; az= az->next) {
206                         if(az->do_draw) {
207                                 if(az->type==AZONE_AREA) {
208                                         area_draw_azone(az->x1, az->y1, az->x2, az->y2);
209                                 } else if(az->type==AZONE_REGION) {
210                                         region_draw_azone(az);
211                                 }
212                                 
213                                 az->do_draw= 0;
214                         }
215                 }
216         }       
217         glDisable( GL_BLEND );
218         
219 }
220
221 /* get scissor rect, checking overlapping regions */
222 void region_scissor_winrct(ARegion *ar, rcti *winrct)
223 {
224         *winrct= ar->winrct;
225         
226         if(ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
227                 return;
228
229         while(ar->prev) {
230                 ar= ar->prev;
231                 
232                 if(BLI_isect_rcti(winrct, &ar->winrct, NULL)) {
233                         if(ar->flag & RGN_FLAG_HIDDEN);
234                         else if(ar->alignment & RGN_SPLIT_PREV);
235                         else if(ar->alignment==RGN_OVERLAP_LEFT) {
236                                 winrct->xmin= ar->winrct.xmax + 1;
237                         }
238                         else if(ar->alignment==RGN_OVERLAP_RIGHT) {
239                                 winrct->xmax= ar->winrct.xmin - 1;
240                         }
241                         else break;
242                 }
243         }
244 }
245
246 /* only exported for WM */
247 /* makes region ready for drawing, sets pixelspace */
248 void ED_region_set(const bContext *C, ARegion *ar)
249 {
250         wmWindow *win= CTX_wm_window(C);
251         ScrArea *sa= CTX_wm_area(C);
252         rcti winrct;
253         
254         /* checks other overlapping regions */
255         region_scissor_winrct(ar, &winrct);
256         
257         ar->drawrct= winrct;
258         
259         /* note; this sets state, so we can use wmOrtho and friends */
260         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
261         
262         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
263         
264         ED_region_pixelspace(ar);
265 }
266
267
268 /* only exported for WM */
269 void ED_region_do_draw(bContext *C, ARegion *ar)
270 {
271         wmWindow *win= CTX_wm_window(C);
272         ScrArea *sa= CTX_wm_area(C);
273         ARegionType *at= ar->type;
274         rcti winrct;
275         
276         /* see BKE_spacedata_draw_locks() */
277         if(at->do_lock)
278                 return;
279         
280         /* checks other overlapping regions */
281         region_scissor_winrct(ar, &winrct);
282         
283         /* if no partial draw rect set, full rect */
284         if(ar->drawrct.xmin == ar->drawrct.xmax)
285                 ar->drawrct= winrct;
286         else {
287                 /* extra clip for safety */
288                 ar->drawrct.xmin= MAX2(winrct.xmin, ar->drawrct.xmin);
289                 ar->drawrct.ymin= MAX2(winrct.ymin, ar->drawrct.ymin);
290                 ar->drawrct.xmax= MIN2(winrct.xmax, ar->drawrct.xmax);
291                 ar->drawrct.ymax= MIN2(winrct.ymax, ar->drawrct.ymax);
292         }
293         
294         /* note; this sets state, so we can use wmOrtho and friends */
295         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
296
297         ar->do_draw= 0;
298         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
299         
300         UI_SetTheme(sa?sa->spacetype:0, ar->type?ar->type->regionid:0);
301         
302         /* optional header info instead? */
303         if(ar->headerstr) {
304                 UI_ThemeClearColor(TH_HEADER);
305                 glClear(GL_COLOR_BUFFER_BIT);
306                 
307                 UI_ThemeColor(TH_TEXT);
308                 BLF_draw_default(20, 8, 0.0f, ar->headerstr, 65535); /* XXX, use real length */
309         }
310         else if(at->draw) {
311                 at->draw(C, ar);
312         }
313
314         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
315         
316         uiFreeInactiveBlocks(C, &ar->uiblocks);
317         
318         /*if(sa)
319                 region_draw_emboss(ar, &winrct);*/
320         
321         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
322         ED_region_pixelspace(ar);
323 }
324
325 /* **********************************
326    maybe silly, but let's try for now
327    to keep these tags protected
328    ********************************** */
329
330 void ED_region_tag_redraw(ARegion *ar)
331 {
332         if(ar) {
333                 /* zero region means full region redraw */
334                 ar->do_draw= RGN_DRAW;
335                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
336         }
337 }
338
339 void ED_region_tag_redraw_overlay(ARegion *ar)
340 {
341         if(ar)
342                 ar->do_draw_overlay= RGN_DRAW;
343 }
344
345 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
346 {
347         if(ar) {
348                 if(!ar->do_draw) {
349                         /* no redraw set yet, set partial region */
350                         ar->do_draw= RGN_DRAW_PARTIAL;
351                         ar->drawrct= *rct;
352                 }
353                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
354                         /* partial redraw already set, expand region */
355                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
356                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
357                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
358                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
359                 }
360         }
361 }
362
363 void ED_area_tag_redraw(ScrArea *sa)
364 {
365         ARegion *ar;
366         
367         if(sa)
368                 for(ar= sa->regionbase.first; ar; ar= ar->next)
369                         ED_region_tag_redraw(ar);
370 }
371
372 void ED_area_tag_redraw_regiontype(ScrArea *sa, int regiontype)
373 {
374         ARegion *ar;
375         
376         if(sa) {
377                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
378                         if(ar->regiontype == regiontype) {
379                                 ED_region_tag_redraw(ar);
380                         }
381                 }
382         }
383 }
384
385 void ED_area_tag_refresh(ScrArea *sa)
386 {
387         if(sa)
388                 sa->do_refresh= 1;
389 }
390
391 /* *************************************************************** */
392
393 /* use NULL to disable it */
394 void ED_area_headerprint(ScrArea *sa, const char *str)
395 {
396         ARegion *ar;
397
398         /* happens when running transform operators in backround mode */
399         if(sa == NULL)
400                 return;
401
402         for(ar= sa->regionbase.first; ar; ar= ar->next) {
403                 if(ar->regiontype==RGN_TYPE_HEADER) {
404                         if(str) {
405                                 if(ar->headerstr==NULL)
406                                         ar->headerstr= MEM_mallocN(256, "headerprint");
407                                 BLI_strncpy(ar->headerstr, str, 256);
408                         }
409                         else if(ar->headerstr) {
410                                 MEM_freeN(ar->headerstr);
411                                 ar->headerstr= NULL;
412                         }
413                         ED_region_tag_redraw(ar);
414                 }
415         }
416 }
417
418 /* ************************************************************ */
419
420
421 static void area_azone_initialize(ScrArea *sa) 
422 {
423         AZone *az;
424         
425         /* reinitalize entirely, regions add azones too */
426         BLI_freelistN(&sa->actionzones);
427         
428         /* set area action zones */
429         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
430         BLI_addtail(&(sa->actionzones), az);
431         az->type= AZONE_AREA;
432         az->x1= sa->totrct.xmin;
433         az->y1= sa->totrct.ymin;
434         az->x2= sa->totrct.xmin + AZONESPOT;
435         az->y2= sa->totrct.ymin + AZONESPOT;
436         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
437         
438         az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
439         BLI_addtail(&(sa->actionzones), az);
440         az->type= AZONE_AREA;
441         az->x1= sa->totrct.xmax+1;
442         az->y1= sa->totrct.ymax+1;
443         az->x2= sa->totrct.xmax-AZONESPOT;
444         az->y2= sa->totrct.ymax-AZONESPOT;
445         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
446 }
447
448 #define AZONEPAD_EDGE   4
449 #define AZONEPAD_ICON   9
450 static void region_azone_edge(AZone *az, ARegion *ar)
451 {
452         switch(az->edge) {
453                 case AE_TOP_TO_BOTTOMRIGHT:
454                         az->x1= ar->winrct.xmin;
455                         az->y1= ar->winrct.ymax - AZONEPAD_EDGE;
456                         az->x2= ar->winrct.xmax;
457                         az->y2= ar->winrct.ymax;
458                         break;
459                 case AE_BOTTOM_TO_TOPLEFT:
460                         az->x1= ar->winrct.xmin;
461                         az->y1= ar->winrct.ymin + AZONEPAD_EDGE;
462                         az->x2= ar->winrct.xmax;
463                         az->y2= ar->winrct.ymin;
464                         break;
465                 case AE_LEFT_TO_TOPRIGHT:
466                         az->x1= ar->winrct.xmin;
467                         az->y1= ar->winrct.ymin;
468                         az->x2= ar->winrct.xmin + AZONEPAD_EDGE;
469                         az->y2= ar->winrct.ymax;
470                         break;
471                 case AE_RIGHT_TO_TOPLEFT:
472                         az->x1= ar->winrct.xmax;
473                         az->y1= ar->winrct.ymin;
474                         az->x2= ar->winrct.xmax - AZONEPAD_EDGE;
475                         az->y2= ar->winrct.ymax;
476                         break;
477         }
478
479         BLI_init_rcti(&az->rect, az->x1, az->x2, az->y1, az->y2);
480 }
481
482 static void region_azone_initialize(ScrArea *sa, ARegion *ar, AZEdge edge) 
483 {
484         if(!(ar->flag & RGN_FLAG_HIDDEN)) {
485                 AZone *az;
486                 
487                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
488                 BLI_addtail(&(sa->actionzones), az);
489                 az->type= AZONE_REGION;
490                 az->ar= ar;
491                 az->edge= edge;
492                 
493                 region_azone_edge(az, ar);
494         }
495 }
496
497 /* *************************************************************** */
498
499 static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
500 {
501          /* edge code (t b l r) is along which area edge azone will be drawn */
502         
503         if(alignment==RGN_ALIGN_TOP)
504                 region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT);
505         else if(alignment==RGN_ALIGN_BOTTOM)
506                 region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT);
507         else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
508                 region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT);
509         else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
510                 region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT);
511 }
512
513 /* dir is direction to check, not the splitting edge direction! */
514 static int rct_fits(rcti *rect, char dir, int size)
515 {
516         if(dir=='h') {
517                 return rect->xmax-rect->xmin - size;
518         }
519         else { // 'v'
520                 return rect->ymax-rect->ymin - size;
521         }
522 }
523
524 static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
525 {
526         rcti *remainder_prev= remainder;
527         int prefsizex, prefsizey;
528         int alignment;
529         
530         if(ar==NULL)
531                 return;
532         
533         /* no returns in function, winrct gets set in the end again */
534         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
535         
536         /* for test; allow split of previously defined region */
537         if(ar->alignment & RGN_SPLIT_PREV)
538                 if(ar->prev)
539                         remainder= &ar->prev->winrct;
540         
541         alignment = ar->alignment & ~RGN_SPLIT_PREV;
542         
543         /* clear state flags first */
544         ar->flag &= ~RGN_FLAG_TOO_SMALL;
545         /* user errors */
546         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
547                 alignment= RGN_ALIGN_NONE;
548         
549         /* prefsize, for header we stick to exception */
550         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
551         if(ar->regiontype==RGN_TYPE_HEADER)
552                 prefsizey= ar->type->prefsizey;
553         else
554                 prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
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                                 if (G.f & G_DEBUG)
658                                         printf("region quadsplit failed\n");
659                         }
660                         else quad= 1;
661                 }
662                 if(quad) {
663                         if(quad==1) { /* left bottom */
664                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
665                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
666                         }
667                         else if(quad==2) { /* left top */
668                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
669                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
670                         }
671                         else if(quad==3) { /* right bottom */
672                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
673                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
674                         }
675                         else {  /* right top */
676                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
677                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
678                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
679                         }
680
681                         quad++;
682                 }
683         }
684         
685         /* for speedup */
686         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
687         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
688         
689         /* restore test exception */
690         if(ar->alignment & RGN_SPLIT_PREV) {
691                 if(ar->prev) {
692                         remainder= remainder_prev;
693                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
694                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
695                 }
696         }
697
698         /* set winrect for azones */
699         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
700                 ar->winrct= *remainder;
701                 
702                 if(alignment==RGN_ALIGN_TOP)
703                         ar->winrct.ymin= ar->winrct.ymax;
704                 else if(alignment==RGN_ALIGN_BOTTOM)
705                         ar->winrct.ymax= ar->winrct.ymin;
706                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
707                         ar->winrct.xmin= ar->winrct.xmax;
708                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
709                         ar->winrct.xmax= ar->winrct.xmin;
710                 else /* prevent winrct to be valid */
711                         ar->winrct.xmax= ar->winrct.xmin;
712         }
713         /* in end, add azones, where appropriate */
714         if(ar->regiontype == RGN_TYPE_HEADER && ar->winy + 6 > sa->winy) {
715                 /* The logic for this is: when the header takes up the full area,
716                  * disallow hiding it to view the main window.
717                  *
718                  * Without this, uou can drag down the file selectors header and hide it
719                  * by accident very easily (highly annoying!), the value 6 is arbitrary
720                  * but accounts for small common rounding problems when scaling the UI,
721                  * must be minimum '4' */
722         }
723         else {
724                 region_azone_add(sa, ar, alignment);
725         }
726
727         region_rect_recursive(sa, ar->next, remainder, quad);
728 }
729
730 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
731 {
732         short rt= CLAMPIS(G.rt, 0, 16);
733
734         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
735         else sa->totrct.xmin= sa->v1->vec.x;
736         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
737         else sa->totrct.xmax= sa->v4->vec.x;
738         
739         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
740         else sa->totrct.ymin= sa->v1->vec.y;
741         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
742         else sa->totrct.ymax= sa->v2->vec.y;
743         
744         /* for speedup */
745         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
746         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
747 }
748
749
750 /* used for area initialize below */
751 static void region_subwindow(wmWindow *win, ARegion *ar)
752 {
753         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
754                 if(ar->swinid)
755                         wm_subwindow_close(win, ar->swinid);
756                 ar->swinid= 0;
757         }
758         else if(ar->swinid==0)
759                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
760         else 
761                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
762 }
763
764 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
765 {
766         /* note, add-handler checks if it already exists */
767         
768         // XXX it would be good to have boundbox checks for some of these...
769         if(flag & ED_KEYMAP_UI) {
770                 /* user interface widgets */
771                 UI_add_region_handlers(handlers);
772         }
773         if(flag & ED_KEYMAP_VIEW2D) {
774                 /* 2d-viewport handling+manipulation */
775                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
776                 WM_event_add_keymap_handler(handlers, keymap);
777         }
778         if(flag & ED_KEYMAP_MARKERS) {
779                 /* time-markers */
780                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
781                 
782                 /* time space only has this keymap, the others get a boundbox restricted map */
783                 if(sa->spacetype!=SPACE_TIME) {
784                         ARegion *ar;
785                         static rcti rect= {0, 10000, 0, 30};    /* same local check for all areas */
786                         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
787                         if(ar) {
788                                 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
789                         }
790                 }
791                 else
792                         WM_event_add_keymap_handler(handlers, keymap);
793         }
794         if(flag & ED_KEYMAP_ANIMATION) {
795                 /* frame changing and timeline operators (for time spaces) */
796                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
797                 WM_event_add_keymap_handler(handlers, keymap);
798         }
799         if(flag & ED_KEYMAP_FRAMES) {
800                 /* frame changing/jumping (for all spaces) */
801                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
802                 WM_event_add_keymap_handler(handlers, keymap);
803         }
804         if(flag & ED_KEYMAP_GPENCIL) {
805                 /* grease pencil */
806                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
807                 WM_event_add_keymap_handler(handlers, keymap);
808         }
809         if(flag & ED_KEYMAP_HEADER) {
810                 /* standard keymap for headers regions */
811                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
812                 WM_event_add_keymap_handler(handlers, keymap);
813         }
814 }
815
816
817 /* called in screen_refresh, or screens_init, also area size changes */
818 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
819 {
820         ARegion *ar;
821         rcti rect;
822         
823         /* set typedefinitions */
824         sa->type= BKE_spacetype_from_id(sa->spacetype);
825         
826         if(sa->type==NULL) {
827                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
828                 sa->type= BKE_spacetype_from_id(sa->spacetype);
829         }
830         
831         for(ar= sa->regionbase.first; ar; ar= ar->next)
832                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
833         
834         /* area sizes */
835         area_calc_totrct(sa, win->sizex, win->sizey);
836         
837         /* clear all azones, add the area triange widgets */
838         area_azone_initialize(sa);
839
840         /* region rect sizes */
841         rect= sa->totrct;
842         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
843         
844         /* default area handlers */
845         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
846         /* checks spacedata, adds own handlers */
847         if(sa->type->init)
848                 sa->type->init(wm, sa);
849         
850         /* region windows, default and own handlers */
851         for(ar= sa->regionbase.first; ar; ar= ar->next) {
852                 region_subwindow(win, ar);
853                 
854                 if(ar->swinid) {
855                         /* default region handlers */
856                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
857                         /* own handlers */
858                         if(ar->type->init)
859                                 ar->type->init(wm, ar);
860                 }
861                 else {
862                         /* prevent uiblocks to run */
863                         uiFreeBlocks(NULL, &ar->uiblocks);      
864                 }
865                 
866                 /* rechecks 2d matrix for header on dpi changing, do not do for other regions, it resets view && blocks view2d operator polls (ton) */
867                 if(ar->regiontype==RGN_TYPE_HEADER)
868                         ar->v2d.flag &= ~V2D_IS_INITIALISED;
869         }
870 }
871
872 /* externally called for floating regions like menus */
873 void ED_region_init(bContext *C, ARegion *ar)
874 {
875 //      ARegionType *at= ar->type;
876         
877         /* refresh can be called before window opened */
878         region_subwindow(CTX_wm_window(C), ar);
879         
880         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
881         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
882         
883         /* UI convention */
884         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
885         glLoadIdentity();
886 }
887
888 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
889 {
890         ScrArea *sa= CTX_wm_area(C);
891
892         ar->flag ^= RGN_FLAG_HIDDEN;
893
894         if(ar->flag & RGN_FLAG_HIDDEN)
895                 WM_event_remove_handlers(C, &ar->handlers);
896
897         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
898         ED_area_tag_redraw(sa);
899 }
900
901 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
902 /* area vertices were set */
903 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
904 {
905         SpaceType *st;
906         ARegion *ar;
907         int spacetype= sa1->spacetype;
908         
909         sa1->headertype= sa2->headertype;
910         sa1->spacetype= sa2->spacetype;
911         sa1->butspacetype= sa2->butspacetype;
912         
913         if(swap_space == 1) {
914                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
915                 /* exception: ensure preview is reset */
916 //              if(sa1->spacetype==SPACE_VIEW3D)
917 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
918         }
919         else if (swap_space == 2) {
920                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
921         }
922         else {
923                 BKE_spacedata_freelist(&sa1->spacedata);
924                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
925         }
926         
927         /* Note; SPACE_EMPTY is possible on new screens */
928         
929         /* regions */
930         if(swap_space == 1) {
931                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
932         }
933         else {
934                 if(swap_space<2) {
935                         st= BKE_spacetype_from_id(spacetype);
936                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
937                                 BKE_area_region_free(st, ar);
938                         BLI_freelistN(&sa1->regionbase);
939                 }
940                 
941                 st= BKE_spacetype_from_id(sa2->spacetype);
942                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
943                         ARegion *newar= BKE_area_region_copy(st, ar);
944                         BLI_addtail(&sa1->regionbase, newar);
945                 }
946         }
947 }
948
949 /* *********** Space switching code *********** */
950
951 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
952 {
953         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
954
955         ED_area_exit(C, sa1);
956         ED_area_exit(C, sa2);
957
958         area_copy_data(tmp, sa1, 2);
959         area_copy_data(sa1, sa2, 0);
960         area_copy_data(sa2, tmp, 0);
961         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
962         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
963
964         BKE_screen_area_free(tmp);
965         MEM_freeN(tmp);
966
967         /* tell WM to refresh, cursor types etc */
968         WM_event_add_mousemove(C);
969         
970         ED_area_tag_redraw(sa1);
971         ED_area_tag_refresh(sa1);
972         ED_area_tag_redraw(sa2);
973         ED_area_tag_refresh(sa2);
974 }
975
976 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
977 {
978         if(sa->spacetype != type) {
979                 SpaceType *st;
980                 SpaceLink *slold;
981                 SpaceLink *sl;
982
983                 ED_area_exit(C, sa);
984
985                 st= BKE_spacetype_from_id(type);
986                 slold= sa->spacedata.first;
987
988                 sa->spacetype= type;
989                 sa->butspacetype= type;
990                 sa->type= st;
991                 
992                 /* check previously stored space */
993                 for (sl= sa->spacedata.first; sl; sl= sl->next)
994                         if(sl->spacetype==type)
995                                 break;
996                 
997                 /* old spacedata... happened during work on 2.50, remove */
998                 if(sl && sl->regionbase.first==NULL) {
999                         st->free(sl);
1000                         BLI_freelinkN(&sa->spacedata, sl);
1001                         if(slold == sl) {
1002                                 slold= NULL;
1003                         }
1004                         sl= NULL;
1005                 }
1006                 
1007                 if (sl) {
1008                         
1009                         /* swap regions */
1010                         slold->regionbase= sa->regionbase;
1011                         sa->regionbase= sl->regionbase;
1012                         sl->regionbase.first= sl->regionbase.last= NULL;
1013                         
1014                         /* put in front of list */
1015                         BLI_remlink(&sa->spacedata, sl);
1016                         BLI_addhead(&sa->spacedata, sl);
1017                 } 
1018                 else {
1019                         /* new space */
1020                         if(st) {
1021                                 sl= st->new(C);
1022                                 BLI_addhead(&sa->spacedata, sl);
1023                                 
1024                                 /* swap regions */
1025                                 if(slold)
1026                                         slold->regionbase= sa->regionbase;
1027                                 sa->regionbase= sl->regionbase;
1028                                 sl->regionbase.first= sl->regionbase.last= NULL;
1029                         }
1030                 }
1031                 
1032                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1033                 
1034                 /* tell WM to refresh, cursor types etc */
1035                 WM_event_add_mousemove(C);
1036                                 
1037                 /*send space change notifier*/
1038                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1039                 
1040                 ED_area_tag_refresh(sa);
1041         }
1042         
1043         /* also redraw when re-used */
1044         ED_area_tag_redraw(sa);
1045 }
1046
1047 void ED_area_prevspace(bContext *C, ScrArea *sa)
1048 {
1049         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1050
1051         if(sl->next) {
1052                 /* workaround for case of double prevspace, render window
1053                    with a file browser on top of it */
1054                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1055                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1056                 else
1057                         ED_area_newspace(C, sa, sl->next->spacetype);
1058         }
1059         else {
1060                 ED_area_newspace(C, sa, SPACE_INFO);
1061         }
1062         ED_area_tag_redraw(sa);
1063
1064         /*send space change notifier*/
1065         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1066 }
1067
1068 static const char *editortype_pup(void)
1069 {
1070         return(
1071                    "Editor type:%t"
1072                    "|3D View %x1"
1073
1074                    "|%l"
1075                    
1076                    "|Timeline %x15"
1077                    "|Graph Editor %x2"
1078                    "|DopeSheet %x12"
1079                    "|NLA Editor %x13"
1080                    
1081                    "|%l"
1082                    
1083                    "|UV/Image Editor %x6"
1084                    
1085                    "|Video Sequence Editor %x8"
1086                    "|Text Editor %x9" 
1087                    "|Node Editor %x16"
1088                    "|Logic Editor %x17"
1089                    
1090                    "|%l"
1091                    
1092                    "|Properties %x4"
1093                    "|Outliner %x3"
1094                    "|User Preferences %x19"
1095                    "|Info%x7"
1096                                    
1097                    "|%l"
1098                    
1099                    "|File Browser %x5"
1100                    
1101                    "|%l"
1102                    
1103                    "|Python Console %x18"
1104                    );
1105 }
1106
1107 static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1108 {
1109         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1110         ED_area_tag_redraw(CTX_wm_area(C));
1111
1112         /*send space change notifier*/
1113         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1114 }
1115
1116 /* returns offset for next button in header */
1117 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1118 {
1119         ScrArea *sa= CTX_wm_area(C);
1120         uiBut *but;
1121         int xco= 8;
1122         
1123         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1124                                                    editortype_pup(), xco, yco, UI_UNIT_X+10, UI_UNIT_Y, 
1125                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1126                                                    "Displays current editor type. "
1127                                                    "Click for menu of available types");
1128         uiButSetFunc(but, spacefunc, NULL, NULL);
1129         
1130         return xco + UI_UNIT_X + 14;
1131 }
1132
1133 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1134 {
1135         ScrArea *sa= CTX_wm_area(C);
1136         int xco= 8;
1137         
1138         if (!sa->full)
1139                 xco= ED_area_header_switchbutton(C, block, yco);
1140
1141         uiBlockSetEmboss(block, UI_EMBOSSN);
1142
1143         if (sa->flag & HEADER_NO_PULLDOWN) {
1144                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1145                                                  ICON_DISCLOSURE_TRI_RIGHT,
1146                                                  xco,yco,UI_UNIT_X,UI_UNIT_Y-2,
1147                                                  &(sa->flag), 0, 0, 0, 0, 
1148                                                  "Show pulldown menus");
1149         }
1150         else {
1151                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1152                                                  ICON_DISCLOSURE_TRI_DOWN,
1153                                                  xco,yco,UI_UNIT_X,UI_UNIT_Y-2,
1154                                                  &(sa->flag), 0, 0, 0, 0, 
1155                                                  "Hide pulldown menus");
1156         }
1157
1158         uiBlockSetEmboss(block, UI_EMBOSS);
1159         
1160         return xco + UI_UNIT_X;
1161 }
1162
1163 /************************ standard UI regions ************************/
1164
1165 static void region_panels_layout(const bContext *C, ARegion *ar, int vertical, const char *context, int w, int em, int *r_x, int *r_y)
1166 {
1167         ScrArea *sa= CTX_wm_area(C);
1168         PanelType *pt;
1169         Panel *panel;
1170         uiBlock *block;
1171         uiStyle *style= U.uistyles.first;
1172         int x, y, miny=0, xco, yco;
1173         int open, header, triangle;
1174
1175         x= 0;
1176         y= -style->panelouter;
1177
1178         /* create panels */
1179         uiBeginPanels(C, ar);
1180
1181         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1182                 /* verify context */
1183                 if(context)
1184                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1185                                 continue;
1186
1187                 /* draw panel */
1188                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1189                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1190                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1191
1192                         /* bad fixed values */
1193                         header= (pt->flag & PNL_NO_HEADER)? 0: UI_UNIT_Y;
1194                         triangle= (int)(UI_UNIT_Y * 1.1f);
1195
1196                         if(vertical)
1197                                 y -= header;
1198
1199                         if(pt->draw_header && header && (open || vertical)) {
1200                                 /* for enabled buttons */
1201                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1202                                         triangle, header+style->panelspace, header, 1, style);
1203
1204                                 pt->draw_header(C, panel);
1205
1206                                 uiBlockLayoutResolve(block, &xco, &yco);
1207                                 panel->labelofs= xco - triangle;
1208                                 panel->layout= NULL;
1209                         }
1210                         else {
1211                                 panel->labelofs= 0;
1212                         }
1213
1214                         if(open) {
1215                                 short panelContext;
1216                                 
1217                                 /* panel context can either be toolbar region or normal panels region */
1218                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1219                                         panelContext= UI_LAYOUT_TOOLBAR;
1220                                 else
1221                                         panelContext= UI_LAYOUT_PANEL;
1222                                 
1223                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1224                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1225
1226                                 pt->draw(C, panel);
1227
1228                                 uiBlockLayoutResolve(block, &xco, &yco);
1229                                 panel->layout= NULL;
1230
1231                                 yco -= 2*style->panelspace;
1232                                 uiEndPanel(block, w, -yco);
1233                         }
1234                         else {
1235                                 yco= 0;
1236                                 uiEndPanel(block, w, 0);
1237                         }
1238
1239                         uiEndBlock(C, block);
1240
1241                         if(vertical) {
1242                                 if(pt->flag & PNL_NO_HEADER)
1243                                         y += yco;
1244                                 else
1245                                         y += yco-style->panelouter;
1246                         }
1247                         else {
1248                                 x += w;
1249                                 miny= MIN2(y, yco-style->panelouter-header);
1250                         }
1251                 }
1252         }
1253
1254         if(vertical)
1255                 x += w;
1256         else
1257                 y= miny;
1258         
1259         /* in case there are no panels */
1260         if(x == 0 || y == 0) {
1261                 x= UI_PANEL_WIDTH;
1262                 y= UI_PANEL_WIDTH;
1263         }
1264
1265         uiEndPanels(C, ar);
1266
1267         *r_x = x;
1268         *r_y = y;
1269 }
1270
1271 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1272 {
1273         View2D *v2d= &ar->v2d;
1274         View2DScrollers *scrollers;
1275         int x, y, w, em, newcontext= 0, need_scrollers;
1276
1277         if(contextnr >= 0)
1278                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1279
1280         if(vertical) {
1281                 w= v2d->cur.xmax - v2d->cur.xmin;
1282                 em= (ar->type->prefsizex)? UI_UNIT_Y/2: UI_UNIT_Y;
1283         }
1284         else {
1285                 w= UI_PANEL_WIDTH;
1286                 em= (ar->type->prefsizex)? UI_UNIT_Y/2: UI_UNIT_Y;
1287         }
1288
1289         /* try to draw without scrollbars */
1290         UI_view2d_view_ortho(v2d);
1291
1292         region_panels_layout(C, ar, vertical, context, ar->winx, em, &x, &y);
1293
1294         /* if it doesn't fit, draw again in smaller space with scrollers */
1295         need_scrollers = (abs(y) > ar->winy);
1296         if(need_scrollers)
1297                 region_panels_layout(C, ar, vertical, context, ar->winx-V2D_SCROLL_WIDTH, em, &x, &y);
1298
1299         /* clear */
1300         UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1301         glClear(GL_COLOR_BUFFER_BIT);
1302         
1303         /* before setting the view */
1304         if(vertical) {
1305                 /* only allow scrolling in vertical direction */
1306                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1307                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1308                 v2d->scroll &= ~(V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_VERTICAL_HIDE);
1309                 if(!need_scrollers)
1310                         v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1311                 
1312                 // don't jump back when panels close or hide
1313                 if(!newcontext)
1314                         y= MAX2(-y, -v2d->cur.ymin);
1315                 else
1316                         y= -y;
1317         }
1318         else {
1319                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1320                  * they often don't fit in horizontal layout)
1321                  */
1322                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1323                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1324                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1325                 v2d->scroll &= ~(V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_VERTICAL_HIDE);
1326                 if(!need_scrollers)
1327                         v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1328                 
1329                 // don't jump back when panels close or hide
1330                 if(!newcontext)
1331                         x= MAX2(x, v2d->cur.xmax);
1332                 y= -y;
1333         }
1334
1335         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1336         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1337         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1338
1339         /* set the view */
1340         UI_view2d_view_ortho(v2d);
1341
1342         /* this does the actual drawing! */
1343         uiDrawPanels(C, ar);
1344         
1345         /* restore view matrix */
1346         UI_view2d_view_restore(C);
1347         
1348         /* scrollers */
1349         if(need_scrollers) {
1350                 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1351                 UI_view2d_scrollers_draw(C, v2d, scrollers);
1352                 UI_view2d_scrollers_free(scrollers);
1353         }
1354 }
1355
1356 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1357 {
1358         wmKeyMap *keymap;
1359         
1360         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1361                 // scrollbars for button regions
1362         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1363         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1364         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1365         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1366
1367                 // correctly initialised User-Prefs?
1368         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1369                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1370         
1371         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1372
1373         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1374         WM_event_add_keymap_handler(&ar->handlers, keymap);
1375 }
1376
1377 void ED_region_header(const bContext *C, ARegion *ar)
1378 {
1379         uiStyle *style= U.uistyles.first;
1380         uiBlock *block;
1381         uiLayout *layout;
1382         HeaderType *ht;
1383         Header header = {NULL};
1384         int maxco, xco, yco;
1385         int headery= ED_area_headersize();
1386
1387         /* clear */     
1388         UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1389         glClear(GL_COLOR_BUFFER_BIT);
1390         
1391         /* set view2d view matrix for scrolling (without scrollers) */
1392         UI_view2d_view_ortho(&ar->v2d);
1393
1394         xco= maxco= 8;
1395         yco= headery-4;
1396
1397         /* draw all headers types */
1398         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1399                 block= uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1400                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, UI_UNIT_Y, 1, style);
1401
1402                 if(ht->draw) {
1403                         header.type= ht;
1404                         header.layout= layout;
1405                         ht->draw(C, &header);
1406                         
1407                         /* for view2d */
1408                         xco= uiLayoutGetWidth(layout);
1409                         if(xco > maxco)
1410                                 maxco= xco;
1411                 }
1412
1413                 uiBlockLayoutResolve(block, &xco, &yco);
1414                 
1415                 /* for view2d */
1416                 if(xco > maxco)
1417                         maxco= xco;
1418                 
1419                 uiEndBlock(C, block);
1420                 uiDrawBlock(C, block);
1421         }
1422
1423         /* always as last  */
1424         UI_view2d_totRect_set(&ar->v2d, maxco+UI_UNIT_X+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1425
1426         /* restore view matrix? */
1427         UI_view2d_view_restore(C);
1428 }
1429
1430 void ED_region_header_init(ARegion *ar)
1431 {
1432         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1433 }
1434
1435 /* UI_UNIT_Y is defined as U variable now, depending dpi */
1436 int ED_area_headersize(void)
1437 {
1438         return UI_UNIT_Y+6;
1439 }