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