Cycles: get rid of (+) icon for region open, all regions already accesible
[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         prefsizex= ar->sizex?ar->sizex:ar->type->prefsizex;
550         prefsizey= ar->sizey?ar->sizey:ar->type->prefsizey;
551         
552         /* hidden is user flag */
553         if(ar->flag & RGN_FLAG_HIDDEN);
554         /* XXX floating area region, not handled yet here */
555         else if(alignment == RGN_ALIGN_FLOAT);
556         /* remainder is too small for any usage */
557         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
558                 ar->flag |= RGN_FLAG_TOO_SMALL;
559         }
560         else if(alignment==RGN_ALIGN_NONE) {
561                 /* typically last region */
562                 ar->winrct= *remainder;
563                 BLI_init_rcti(remainder, 0, 0, 0, 0);
564         }
565         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
566                 
567                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
568                         ar->flag |= RGN_FLAG_TOO_SMALL;
569                 }
570                 else {
571                         int fac= rct_fits(remainder, 'v', prefsizey);
572
573                         if(fac < 0 )
574                                 prefsizey += fac;
575                         
576                         ar->winrct= *remainder;
577                         
578                         if(alignment==RGN_ALIGN_TOP) {
579                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
580                                 remainder->ymax= ar->winrct.ymin - 1;
581                         }
582                         else {
583                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
584                                 remainder->ymin= ar->winrct.ymax + 1;
585                         }
586                 }
587         }
588         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
589                 
590                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
591                         ar->flag |= RGN_FLAG_TOO_SMALL;
592                 }
593                 else {
594                         int fac= rct_fits(remainder, 'h', prefsizex);
595                         
596                         if(fac < 0 )
597                                 prefsizex += fac;
598                         
599                         ar->winrct= *remainder;
600                         
601                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
602                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
603                                 if(alignment==RGN_ALIGN_RIGHT)
604                                         remainder->xmax= ar->winrct.xmin - 1;
605                         }
606                         else {
607                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
608                                 if(alignment==RGN_ALIGN_LEFT)
609                                         remainder->xmin= ar->winrct.xmax + 1;
610                         }
611                 }
612         }
613         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
614                 /* percentage subdiv*/
615                 ar->winrct= *remainder;
616                 
617                 if(alignment==RGN_ALIGN_HSPLIT) {
618                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
619                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
620                                 remainder->xmin= ar->winrct.xmax+1;
621                         }
622                         else {
623                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
624                         }
625                 }
626                 else {
627                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
628                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
629                                 remainder->ymin= ar->winrct.ymax+1;
630                         }
631                         else {
632                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
633                         }
634                 }
635         }
636         else if(alignment==RGN_ALIGN_QSPLIT) {
637                 ar->winrct= *remainder;
638                 
639                 /* test if there's still 4 regions left */
640                 if(quad==0) {
641                         ARegion *artest= ar->next;
642                         int count= 1;
643                         
644                         while(artest) {
645                                 artest->alignment= RGN_ALIGN_QSPLIT;
646                                 artest= artest->next;
647                                 count++;
648                         }
649                         
650                         if(count!=4) {
651                                 /* let's stop adding regions */
652                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
653                                 if (G.f & G_DEBUG)
654                                         printf("region quadsplit failed\n");
655                         }
656                         else quad= 1;
657                 }
658                 if(quad) {
659                         if(quad==1) { /* left bottom */
660                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
661                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
662                         }
663                         else if(quad==2) { /* left top */
664                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
665                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
666                         }
667                         else if(quad==3) { /* right bottom */
668                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
669                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
670                         }
671                         else {  /* right top */
672                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
673                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
674                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
675                         }
676
677                         quad++;
678                 }
679         }
680         
681         /* for speedup */
682         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
683         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
684         
685         /* restore test exception */
686         if(ar->alignment & RGN_SPLIT_PREV) {
687                 if(ar->prev) {
688                         remainder= remainder_prev;
689                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
690                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
691                 }
692         }
693
694         /* set winrect for azones */
695         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
696                 ar->winrct= *remainder;
697                 
698                 if(alignment==RGN_ALIGN_TOP)
699                         ar->winrct.ymin= ar->winrct.ymax;
700                 else if(alignment==RGN_ALIGN_BOTTOM)
701                         ar->winrct.ymax= ar->winrct.ymin;
702                 else if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
703                         ar->winrct.xmin= ar->winrct.xmax;
704                 else if(ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
705                         ar->winrct.xmax= ar->winrct.xmin;
706                 else /* prevent winrct to be valid */
707                         ar->winrct.xmax= ar->winrct.xmin;
708         }
709         /* in end, add azones, where appropriate */
710         region_azone_add(sa, ar, alignment);
711
712
713         region_rect_recursive(sa, ar->next, remainder, quad);
714 }
715
716 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
717 {
718         short rt= CLAMPIS(G.rt, 0, 16);
719
720         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1+rt;
721         else sa->totrct.xmin= sa->v1->vec.x;
722         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1-rt;
723         else sa->totrct.xmax= sa->v4->vec.x;
724         
725         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1+rt;
726         else sa->totrct.ymin= sa->v1->vec.y;
727         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1-rt;
728         else sa->totrct.ymax= sa->v2->vec.y;
729         
730         /* for speedup */
731         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
732         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
733 }
734
735
736 /* used for area initialize below */
737 static void region_subwindow(wmWindow *win, ARegion *ar)
738 {
739         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
740                 if(ar->swinid)
741                         wm_subwindow_close(win, ar->swinid);
742                 ar->swinid= 0;
743         }
744         else if(ar->swinid==0)
745                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
746         else 
747                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
748 }
749
750 static void ed_default_handlers(wmWindowManager *wm, ScrArea *sa, ListBase *handlers, int flag)
751 {
752         /* note, add-handler checks if it already exists */
753         
754         // XXX it would be good to have boundbox checks for some of these...
755         if(flag & ED_KEYMAP_UI) {
756                 /* user interface widgets */
757                 UI_add_region_handlers(handlers);
758         }
759         if(flag & ED_KEYMAP_VIEW2D) {
760                 /* 2d-viewport handling+manipulation */
761                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View2D", 0, 0);
762                 WM_event_add_keymap_handler(handlers, keymap);
763         }
764         if(flag & ED_KEYMAP_MARKERS) {
765                 /* time-markers */
766                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Markers", 0, 0);
767                 
768                 /* time space only has this keymap, the others get a boundbox restricted map */
769                 if(sa->spacetype!=SPACE_TIME) {
770                         ARegion *ar;
771                         static rcti rect= {0, 10000, 0, 30};    /* same local check for all areas */
772
773                         for(ar= sa->regionbase.first; ar; ar= ar->next)
774                                 if(ar->regiontype == RGN_TYPE_WINDOW)
775                                         break;
776                         if(ar)
777                                 WM_event_add_keymap_handler_bb(handlers, keymap, &rect, &ar->winrct);
778                 }
779                 else
780                         WM_event_add_keymap_handler(handlers, keymap);
781         }
782         if(flag & ED_KEYMAP_ANIMATION) {
783                 /* frame changing and timeline operators (for time spaces) */
784                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Animation", 0, 0);
785                 WM_event_add_keymap_handler(handlers, keymap);
786         }
787         if(flag & ED_KEYMAP_FRAMES) {
788                 /* frame changing/jumping (for all spaces) */
789                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
790                 WM_event_add_keymap_handler(handlers, keymap);
791         }
792         if(flag & ED_KEYMAP_GPENCIL) {
793                 /* grease pencil */
794                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Grease Pencil", 0, 0);
795                 WM_event_add_keymap_handler(handlers, keymap);
796         }
797         if(flag & ED_KEYMAP_HEADER) {
798                 /* standard keymap for headers regions */
799                 wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "Header", 0, 0);
800                 WM_event_add_keymap_handler(handlers, keymap);
801         }
802 }
803
804
805 /* called in screen_refresh, or screens_init, also area size changes */
806 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
807 {
808         ARegion *ar;
809         rcti rect;
810         
811         /* set typedefinitions */
812         sa->type= BKE_spacetype_from_id(sa->spacetype);
813         
814         if(sa->type==NULL) {
815                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
816                 sa->type= BKE_spacetype_from_id(sa->spacetype);
817         }
818         
819         for(ar= sa->regionbase.first; ar; ar= ar->next)
820                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
821         
822         /* area sizes */
823         area_calc_totrct(sa, win->sizex, win->sizey);
824         
825         /* clear all azones, add the area triange widgets */
826         area_azone_initialize(sa);
827
828         /* region rect sizes */
829         rect= sa->totrct;
830         region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
831         
832         /* default area handlers */
833         ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
834         /* checks spacedata, adds own handlers */
835         if(sa->type->init)
836                 sa->type->init(wm, sa);
837         
838         /* region windows, default and own handlers */
839         for(ar= sa->regionbase.first; ar; ar= ar->next) {
840                 region_subwindow(win, ar);
841                 
842                 if(ar->swinid) {
843                         /* default region handlers */
844                         ed_default_handlers(wm, sa, &ar->handlers, ar->type->keymapflag);
845                         /* own handlers */
846                         if(ar->type->init)
847                                 ar->type->init(wm, ar);
848                 }
849                 else {
850                         /* prevent uiblocks to run */
851                         uiFreeBlocks(NULL, &ar->uiblocks);      
852                 }
853                 
854         }
855 }
856
857 /* externally called for floating regions like menus */
858 void ED_region_init(bContext *C, ARegion *ar)
859 {
860 //      ARegionType *at= ar->type;
861         
862         /* refresh can be called before window opened */
863         region_subwindow(CTX_wm_window(C), ar);
864         
865         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
866         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
867         
868         /* UI convention */
869         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
870         glLoadIdentity();
871 }
872
873 void ED_region_toggle_hidden(bContext *C, ARegion *ar)
874 {
875         ScrArea *sa= CTX_wm_area(C);
876
877         ar->flag ^= RGN_FLAG_HIDDEN;
878
879         if(ar->flag & RGN_FLAG_HIDDEN)
880                 WM_event_remove_handlers(C, &ar->handlers);
881
882         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
883         ED_area_tag_redraw(sa);
884 }
885
886 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
887 /* area vertices were set */
888 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
889 {
890         SpaceType *st;
891         ARegion *ar;
892         int spacetype= sa1->spacetype;
893         
894         sa1->headertype= sa2->headertype;
895         sa1->spacetype= sa2->spacetype;
896         sa1->butspacetype= sa2->butspacetype;
897         
898         if(swap_space == 1) {
899                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
900                 /* exception: ensure preview is reset */
901 //              if(sa1->spacetype==SPACE_VIEW3D)
902 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
903         }
904         else if (swap_space == 2) {
905                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
906         }
907         else {
908                 BKE_spacedata_freelist(&sa1->spacedata);
909                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
910         }
911         
912         /* Note; SPACE_EMPTY is possible on new screens */
913         
914         /* regions */
915         if(swap_space == 1) {
916                 SWAP(ListBase, sa1->regionbase, sa2->regionbase);
917         }
918         else {
919                 if(swap_space<2) {
920                         st= BKE_spacetype_from_id(spacetype);
921                         for(ar= sa1->regionbase.first; ar; ar= ar->next)
922                                 BKE_area_region_free(st, ar);
923                         BLI_freelistN(&sa1->regionbase);
924                 }
925                 
926                 st= BKE_spacetype_from_id(sa2->spacetype);
927                 for(ar= sa2->regionbase.first; ar; ar= ar->next) {
928                         ARegion *newar= BKE_area_region_copy(st, ar);
929                         BLI_addtail(&sa1->regionbase, newar);
930                 }
931         }
932 }
933
934 /* *********** Space switching code *********** */
935
936 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
937 {
938         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
939
940         ED_area_exit(C, sa1);
941         ED_area_exit(C, sa2);
942
943         area_copy_data(tmp, sa1, 2);
944         area_copy_data(sa1, sa2, 0);
945         area_copy_data(sa2, tmp, 0);
946         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
947         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
948
949         BKE_screen_area_free(tmp);
950         MEM_freeN(tmp);
951
952         /* tell WM to refresh, cursor types etc */
953         WM_event_add_mousemove(C);
954         
955         ED_area_tag_redraw(sa1);
956         ED_area_tag_refresh(sa1);
957         ED_area_tag_redraw(sa2);
958         ED_area_tag_refresh(sa2);
959 }
960
961 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
962 {
963         if(sa->spacetype != type) {
964                 SpaceType *st;
965                 SpaceLink *slold;
966                 SpaceLink *sl;
967
968                 ED_area_exit(C, sa);
969
970                 st= BKE_spacetype_from_id(type);
971                 slold= sa->spacedata.first;
972
973                 sa->spacetype= type;
974                 sa->butspacetype= type;
975                 sa->type= st;
976                 
977                 /* check previously stored space */
978                 for (sl= sa->spacedata.first; sl; sl= sl->next)
979                         if(sl->spacetype==type)
980                                 break;
981                 
982                 /* old spacedata... happened during work on 2.50, remove */
983                 if(sl && sl->regionbase.first==NULL) {
984                         st->free(sl);
985                         BLI_freelinkN(&sa->spacedata, sl);
986                         if(slold == sl) {
987                                 slold= NULL;
988                         }
989                         sl= NULL;
990                 }
991                 
992                 if (sl) {
993                         
994                         /* swap regions */
995                         slold->regionbase= sa->regionbase;
996                         sa->regionbase= sl->regionbase;
997                         sl->regionbase.first= sl->regionbase.last= NULL;
998                         
999                         /* put in front of list */
1000                         BLI_remlink(&sa->spacedata, sl);
1001                         BLI_addhead(&sa->spacedata, sl);
1002                 } 
1003                 else {
1004                         /* new space */
1005                         if(st) {
1006                                 sl= st->new(C);
1007                                 BLI_addhead(&sa->spacedata, sl);
1008                                 
1009                                 /* swap regions */
1010                                 if(slold)
1011                                         slold->regionbase= sa->regionbase;
1012                                 sa->regionbase= sl->regionbase;
1013                                 sl->regionbase.first= sl->regionbase.last= NULL;
1014                         }
1015                 }
1016                 
1017                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1018                 
1019                 /* tell WM to refresh, cursor types etc */
1020                 WM_event_add_mousemove(C);
1021                                 
1022                 /*send space change notifyer*/
1023                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1024                 
1025                 ED_area_tag_refresh(sa);
1026         }
1027         
1028         /* also redraw when re-used */
1029         ED_area_tag_redraw(sa);
1030 }
1031
1032 void ED_area_prevspace(bContext *C, ScrArea *sa)
1033 {
1034         SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
1035
1036         if(sl->next) {
1037                 /* workaround for case of double prevspace, render window
1038                    with a file browser on top of it */
1039                 if(sl->next->spacetype == SPACE_FILE && sl->next->next)
1040                         ED_area_newspace(C, sa, sl->next->next->spacetype);
1041                 else
1042                         ED_area_newspace(C, sa, sl->next->spacetype);
1043         }
1044         else {
1045                 ED_area_newspace(C, sa, SPACE_INFO);
1046         }
1047         ED_area_tag_redraw(sa);
1048
1049         /*send space change notifyer*/
1050         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, sa);
1051 }
1052
1053 static const char *editortype_pup(void)
1054 {
1055         return(
1056                    "Editor type:%t"
1057                    "|3D View %x1"
1058
1059                    "|%l"
1060                    
1061                    "|Timeline %x15"
1062                    "|Graph Editor %x2"
1063                    "|DopeSheet %x12"
1064                    "|NLA Editor %x13"
1065                    
1066                    "|%l"
1067                    
1068                    "|UV/Image Editor %x6"
1069                    
1070                    "|Video Sequence Editor %x8"
1071                    "|Text Editor %x9" 
1072                    "|Node Editor %x16"
1073                    "|Logic Editor %x17"
1074                    
1075                    "|%l"
1076                    
1077                    "|Properties %x4"
1078                    "|Outliner %x3"
1079                    "|User Preferences %x19"
1080                    "|Info%x7"
1081                                    
1082                    "|%l"
1083                    
1084                    "|File Browser %x5"
1085                    
1086                    "|%l"
1087                    
1088                    "|Python Console %x18"
1089                    );
1090 }
1091
1092 static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1093 {
1094         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
1095         ED_area_tag_redraw(CTX_wm_area(C));
1096
1097         /*send space change notifyer*/
1098         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CHANGED, CTX_wm_area(C));
1099 }
1100
1101 /* returns offset for next button in header */
1102 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
1103 {
1104         ScrArea *sa= CTX_wm_area(C);
1105         uiBut *but;
1106         int xco= 8;
1107         
1108         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
1109                                                    editortype_pup(), xco, yco, XIC+10, YIC, 
1110                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
1111                                                    "Displays current editor type. "
1112                                                    "Click for menu of available types");
1113         uiButSetFunc(but, spacefunc, NULL, NULL);
1114         
1115         return xco + XIC + 14;
1116 }
1117
1118 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
1119 {
1120         ScrArea *sa= CTX_wm_area(C);
1121         int xco= 8;
1122         
1123         if (!sa->full)
1124                 xco= ED_area_header_switchbutton(C, block, yco);
1125
1126         uiBlockSetEmboss(block, UI_EMBOSSN);
1127
1128         if (sa->flag & HEADER_NO_PULLDOWN) {
1129                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1130                                                  ICON_DISCLOSURE_TRI_RIGHT,
1131                                                  xco,yco,XIC,YIC-2,
1132                                                  &(sa->flag), 0, 0, 0, 0, 
1133                                                  "Show pulldown menus");
1134         }
1135         else {
1136                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
1137                                                  ICON_DISCLOSURE_TRI_DOWN,
1138                                                  xco,yco,XIC,YIC-2,
1139                                                  &(sa->flag), 0, 0, 0, 0, 
1140                                                  "Hide pulldown menus");
1141         }
1142
1143         uiBlockSetEmboss(block, UI_EMBOSS);
1144         
1145         return xco + XIC;
1146 }
1147
1148 /************************ standard UI regions ************************/
1149
1150 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
1151 {
1152         ScrArea *sa= CTX_wm_area(C);
1153         uiStyle *style= U.uistyles.first;
1154         uiBlock *block;
1155         PanelType *pt;
1156         Panel *panel;
1157         View2D *v2d= &ar->v2d;
1158         View2DScrollers *scrollers;
1159         int xco, yco, x, y, miny=0, w, em, header, triangle, open, newcontext= 0;
1160
1161         if(contextnr >= 0)
1162                 newcontext= UI_view2d_tab_set(v2d, contextnr);
1163
1164         if(vertical) {
1165                 w= v2d->cur.xmax - v2d->cur.xmin;
1166                 em= (ar->type->prefsizex)? 10: 20;
1167         }
1168         else {
1169                 w= UI_PANEL_WIDTH;
1170                 em= (ar->type->prefsizex)? 10: 20;
1171         }
1172
1173         x= 0;
1174         y= -style->panelouter;
1175
1176         /* create panels */
1177         uiBeginPanels(C, ar);
1178
1179         /* set view2d view matrix for scrolling (without scrollers) */
1180         UI_view2d_view_ortho(v2d);
1181
1182         for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
1183                 /* verify context */
1184                 if(context)
1185                         if(pt->context[0] && strcmp(context, pt->context) != 0)
1186                                 continue;
1187
1188                 /* draw panel */
1189                 if(pt->draw && (!pt->poll || pt->poll(C, pt))) {
1190                         block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
1191                         panel= uiBeginPanel(sa, ar, block, pt, &open);
1192
1193                         /* bad fixed values */
1194                         header= (pt->flag & PNL_NO_HEADER)? 0: 20;
1195                         triangle= 22;
1196
1197                         if(vertical)
1198                                 y -= header;
1199
1200                         if(pt->draw_header && header && (open || vertical)) {
1201                                 /* for enabled buttons */
1202                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
1203                                         triangle, header+style->panelspace, header, 1, style);
1204
1205                                 pt->draw_header(C, panel);
1206
1207                                 uiBlockLayoutResolve(block, &xco, &yco);
1208                                 panel->labelofs= xco - triangle;
1209                                 panel->layout= NULL;
1210                         }
1211                         else {
1212                                 panel->labelofs= 0;
1213                         }
1214
1215                         if(open) {
1216                                 short panelContext;
1217                                 
1218                                 /* panel context can either be toolbar region or normal panels region */
1219                                 if (ar->regiontype == RGN_TYPE_TOOLS)
1220                                         panelContext= UI_LAYOUT_TOOLBAR;
1221                                 else
1222                                         panelContext= UI_LAYOUT_PANEL;
1223                                 
1224                                 panel->layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
1225                                         style->panelspace, 0, w-2*style->panelspace, em, style);
1226
1227                                 pt->draw(C, panel);
1228
1229                                 uiBlockLayoutResolve(block, &xco, &yco);
1230                                 panel->layout= NULL;
1231
1232                                 yco -= 2*style->panelspace;
1233                                 uiEndPanel(block, w, -yco);
1234                         }
1235                         else {
1236                                 yco= 0;
1237                                 uiEndPanel(block, w, 0);
1238                         }
1239
1240                         uiEndBlock(C, block);
1241
1242                         if(vertical) {
1243                                 if(pt->flag & PNL_NO_HEADER)
1244                                         y += yco;
1245                                 else
1246                                         y += yco-style->panelouter;
1247                         }
1248                         else {
1249                                 x += w;
1250                                 miny= MIN2(y, yco-style->panelouter-header);
1251                         }
1252                 }
1253         }
1254
1255         if(vertical)
1256                 x += w;
1257         else
1258                 y= miny;
1259         
1260         /* in case there are no panels */
1261         if(x == 0 || y == 0) {
1262                 x= UI_PANEL_WIDTH;
1263                 y= UI_PANEL_WIDTH;
1264         }
1265
1266         /* clear */
1267         UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW)?TH_PREVIEW_BACK:TH_BACK);
1268         glClear(GL_COLOR_BUFFER_BIT);
1269         
1270         /* before setting the view */
1271         if(vertical) {
1272                 /* only allow scrolling in vertical direction */
1273                 v2d->keepofs |= V2D_LOCKOFS_X|V2D_KEEPOFS_Y;
1274                 v2d->keepofs &= ~(V2D_LOCKOFS_Y|V2D_KEEPOFS_X);
1275                 //v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1276                 //v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
1277                 
1278                 // don't jump back when panels close or hide
1279                 if(!newcontext)
1280                         y= MAX2(-y, -v2d->cur.ymin);
1281                 else
1282                         y= -y;
1283         }
1284         else {
1285                 /* for now, allow scrolling in both directions (since layouts are optimised for vertical,
1286                  * they often don't fit in horizontal layout)
1287                  */
1288                 v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_LOCKOFS_Y|V2D_KEEPOFS_X|V2D_KEEPOFS_Y);
1289                 //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
1290                 //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
1291                 //v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
1292                 //v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
1293                 
1294                 // don't jump back when panels close or hide
1295                 if(!newcontext)
1296                         x= MAX2(x, v2d->cur.xmax);
1297                 y= -y;
1298         }
1299
1300         // +V2D_SCROLL_HEIGHT is workaround to set the actual height
1301         UI_view2d_totRect_set(v2d, x+V2D_SCROLL_WIDTH, y+V2D_SCROLL_HEIGHT);
1302
1303         /* set the view */
1304         UI_view2d_view_ortho(v2d);
1305
1306         /* this does the actual drawing! */
1307         uiEndPanels(C, ar);
1308         
1309         /* restore view matrix */
1310         UI_view2d_view_restore(C);
1311         
1312         /* scrollers */
1313         //scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1314         //UI_view2d_scrollers_draw(C, v2d, scrollers);
1315         //UI_view2d_scrollers_free(scrollers);
1316 }
1317
1318 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
1319 {
1320         wmKeyMap *keymap;
1321         
1322         // XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
1323                 // scrollbars for button regions
1324         /*ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM); 
1325         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
1326         ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;*/
1327         ar->v2d.scroll= 0;
1328         ar->v2d.keepzoom |= V2D_KEEPZOOM;
1329
1330                 // correctly initialised User-Prefs?
1331         if(!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
1332                 ar->v2d.flag &= ~V2D_IS_INITIALISED;
1333         
1334         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
1335
1336         keymap= WM_keymap_find(wm->defaultconf, "View2D Buttons List", 0, 0);
1337         WM_event_add_keymap_handler(&ar->handlers, keymap);
1338 }
1339
1340 void ED_region_header(const bContext *C, ARegion *ar)
1341 {
1342         uiStyle *style= U.uistyles.first;
1343         uiBlock *block;
1344         uiLayout *layout;
1345         HeaderType *ht;
1346         Header header = {NULL};
1347         int maxco, xco, yco;
1348
1349         /* clear */     
1350         UI_ThemeClearColor((ED_screen_area_active(C))?TH_HEADER:TH_HEADERDESEL);
1351         glClear(GL_COLOR_BUFFER_BIT);
1352         
1353         /* set view2d view matrix for scrolling (without scrollers) */
1354         UI_view2d_view_ortho(&ar->v2d);
1355
1356         xco= maxco= 8;
1357         yco= HEADERY-4;
1358
1359         /* draw all headers types */
1360         for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
1361                 block= uiBeginBlock(C, ar, ht->idname, UI_EMBOSS);
1362                 layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, yco, HEADERY-6, 1, style);
1363
1364                 if(ht->draw) {
1365                         header.type= ht;
1366                         header.layout= layout;
1367                         ht->draw(C, &header);
1368                         
1369                         /* for view2d */
1370                         xco= uiLayoutGetWidth(layout);
1371                         if(xco > maxco)
1372                                 maxco= xco;
1373                 }
1374
1375                 uiBlockLayoutResolve(block, &xco, &yco);
1376                 
1377                 /* for view2d */
1378                 if(xco > maxco)
1379                         maxco= xco;
1380                 
1381                 uiEndBlock(C, block);
1382                 uiDrawBlock(C, block);
1383         }
1384
1385         /* always as last  */
1386         UI_view2d_totRect_set(&ar->v2d, maxco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
1387
1388         /* restore view matrix? */
1389         UI_view2d_view_restore(C);
1390 }
1391
1392 void ED_region_header_init(ARegion *ar)
1393 {
1394         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
1395 }
1396