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