cddc7d391c8da2445c035a4c06c50407d2b740d8
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36 #include "BLI_rand.h"
37
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_screen.h"
41 #include "BKE_utildefines.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45 #include "wm_subwindow.h"
46
47 #include "ED_screen.h"
48 #include "ED_screen_types.h"
49 #include "ED_types.h"
50
51 #include "BIF_gl.h"
52 #include "BIF_glutil.h"
53
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "BMF_Api.h"
59
60 #ifndef DISABLE_PYTHON
61 #include "BPY_extern.h"
62 #endif
63
64 #include "screen_intern.h"
65
66 /* general area and region code */
67
68 static void region_draw_emboss(ARegion *ar)
69 {
70         short winx, winy;
71         
72         winx= ar->winrct.xmax-ar->winrct.xmin;
73         winy= ar->winrct.ymax-ar->winrct.ymin;
74         
75         /* set transp line */
76         glEnable( GL_BLEND );
77         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
78         
79         /* right  */
80         glColor4ub(0,0,0, 50);
81         sdrawline(winx, 0, winx, winy);
82         
83         /* bottom  */
84         glColor4ub(0,0,0, 80);
85         sdrawline(0, 0, winx, 0);
86         
87         /* top  */
88         glColor4ub(255,255,255, 60);
89         sdrawline(0, winy, winx, winy);
90
91         /* left  */
92         glColor4ub(255,255,255, 50);
93         sdrawline(0, 0, 0, winy);
94         
95         glDisable( GL_BLEND );
96 }
97
98 void ED_region_pixelspace(ARegion *ar)
99 {
100         int width= ar->winrct.xmax-ar->winrct.xmin+1;
101         int height= ar->winrct.ymax-ar->winrct.ymin+1;
102         
103         wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
104         wmLoadIdentity();
105 }
106
107 /* only exported for WM */
108 void ED_region_do_listen(ARegion *ar, wmNotifier *note)
109 {
110         /* generic notes first */
111         switch(note->category) {
112                 case NC_WINDOW:
113                         ED_region_tag_redraw(ar);
114                         break;
115                 case NC_SCREEN:
116                         if(note->action==NA_EDITED)
117                                 ED_region_tag_redraw(ar);
118                         break;
119                 default:
120                         if(ar->type && ar->type->listener)
121                                 ar->type->listener(ar, note);
122         }
123 }
124
125 /* only exported for WM */
126 void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
127 {
128         /* no generic notes? */
129         if(sa->type && sa->type->listener) {
130                 sa->type->listener(sa, note);
131         }
132 }
133
134 /* only exported for WM */
135 void ED_area_do_refresh(bContext *C, ScrArea *sa)
136 {
137         /* no generic notes? */
138         if(sa->type && sa->type->refresh) {
139                 sa->type->refresh(C, sa);
140         }
141         sa->do_refresh= 0;
142 }
143
144 /* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
145 /* only exported for WM */
146 void ED_area_overdraw_flush(bContext *C, ScrArea *sa, ARegion *ar)
147 {
148         AZone *az;
149         
150         for(az= sa->actionzones.first; az; az= az->next) {
151                 int xs= (az->x1+az->x2)/2, ys= (az->y1+az->y2)/2;
152
153                 /* test if inside */
154                 if(BLI_in_rcti(&ar->winrct, xs, ys)) {
155                         az->do_draw= 1;
156                 }
157         }
158 }
159
160 /* only exported for WM */
161 void ED_area_overdraw(bContext *C)
162 {
163         wmWindow *win= CTX_wm_window(C);
164         bScreen *screen= CTX_wm_screen(C);
165         ScrArea *sa;
166         
167         /* Draw AZones, in screenspace */
168         wmSubWindowSet(win, screen->mainwin);
169
170         glEnable( GL_BLEND );
171         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
172         
173         for(sa= screen->areabase.first; sa; sa= sa->next) {
174                 AZone *az;
175                 for(az= sa->actionzones.first; az; az= az->next) {
176                         if(az->do_draw) {
177                                 if(az->type==AZONE_TRI) {
178                                         glColor4ub(0, 0, 0, 70);
179                                         sdrawtrifill(az->x1, az->y1, az->x2, az->y2);
180                                 }
181                                 az->do_draw= 0;
182                         }
183                 }
184         }       
185         glDisable( GL_BLEND );
186         
187 }
188
189 /* only exported for WM */
190 void ED_region_do_draw(bContext *C, ARegion *ar)
191 {
192         wmWindow *win= CTX_wm_window(C);
193         ScrArea *sa= CTX_wm_area(C);
194         ARegionType *at= ar->type;
195         
196         /* note; this sets state, so we can use wmOrtho and friends */
197         wmSubWindowSet(win, ar->swinid);
198         
199         /* optional header info instead? */
200         if(ar->headerstr) {
201                 float col[3];
202                 UI_SetTheme(sa);
203                 UI_GetThemeColor3fv(TH_HEADER, col);
204                 glClearColor(col[0], col[1], col[2], 0.0);
205                 glClear(GL_COLOR_BUFFER_BIT);
206                 
207                 UI_ThemeColor(TH_MENU_TEXT);
208                 glRasterPos2i(20, 6);
209                 BMF_DrawString(G.font, ar->headerstr);
210         }
211         else if(at->draw) {
212                 UI_SetTheme(sa);
213                 at->draw(C, ar);
214                 UI_SetTheme(NULL);
215         }
216         
217         if(sa)
218                 region_draw_emboss(ar);
219
220         uiFreeInactiveBlocks(C, &ar->uiblocks);
221         
222         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
223         ED_region_pixelspace(ar);
224         
225         ar->do_draw= 0;
226 }
227
228 /* **********************************
229    maybe silly, but let's try for now
230    to keep these tags protected
231    ********************************** */
232
233 void ED_region_tag_redraw(ARegion *ar)
234 {
235         if(ar)
236                 ar->do_draw= 1;
237 }
238
239 void ED_area_tag_redraw(ScrArea *sa)
240 {
241         ARegion *ar;
242         
243         if(sa)
244                 for(ar= sa->regionbase.first; ar; ar= ar->next)
245                         ar->do_draw= 1;
246 }
247
248 void ED_area_tag_refresh(ScrArea *sa)
249 {
250         if(sa)
251                 sa->do_refresh= 1;
252 }
253
254 /* *************************************************************** */
255
256 /* use NULL to disable it */
257 void ED_area_headerprint(ScrArea *sa, const char *str)
258 {
259         ARegion *ar;
260         
261         for(ar= sa->regionbase.first; ar; ar= ar->next) {
262                 if(ar->regiontype==RGN_TYPE_HEADER) {
263                         if(str) {
264                                 if(ar->headerstr==NULL)
265                                         ar->headerstr= MEM_mallocN(256, "headerprint");
266                                 BLI_strncpy(ar->headerstr, str, 256);
267                         }
268                         else if(ar->headerstr) {
269                                 MEM_freeN(ar->headerstr);
270                                 ar->headerstr= NULL;
271                         }
272                         ED_region_tag_redraw(ar);
273                 }
274         }
275 }
276
277 /* *************************************************************** */
278
279 /* dir is direction to check, not the splitting edge direction! */
280 static int rct_fits(rcti *rect, char dir, int size)
281 {
282         if(dir=='h') {
283                 return rect->xmax-rect->xmin - size;
284         }
285         else { // 'v'
286                 return rect->ymax-rect->ymin - size;
287         }
288 }
289
290 static void region_rect_recursive(ARegion *ar, rcti *remainder, int quad)
291 {
292         int prefsizex, prefsizey;
293         
294         if(ar==NULL)
295                 return;
296         
297         /* clear state flags first */
298         ar->flag &= ~RGN_FLAG_TOO_SMALL;
299         /* user errors */
300         if(ar->next==NULL && ar->alignment!=RGN_ALIGN_QSPLIT)
301                 ar->alignment= RGN_ALIGN_NONE;
302         
303         prefsizex= ar->type->minsizex;
304         prefsizey= ar->type->minsizey;
305         
306         /* hidden is user flag */
307         if(ar->flag & RGN_FLAG_HIDDEN);
308         /* XXX floating area region, not handled yet here */
309         else if(ar->alignment == RGN_ALIGN_FLOAT);
310         /* remainder is too small for any usage */
311         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
312                 ar->flag |= RGN_FLAG_TOO_SMALL;
313         }
314         else if(ar->alignment==RGN_ALIGN_NONE) {
315                 /* typically last region */
316                 ar->winrct= *remainder;
317                 BLI_init_rcti(remainder, 0, 0, 0, 0);
318         }
319         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
320                 
321                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
322                         ar->flag |= RGN_FLAG_TOO_SMALL;
323                 }
324                 else {
325                         int fac= rct_fits(remainder, 'v', prefsizey);
326
327                         if(fac < 0 )
328                                 prefsizey += fac;
329                         
330                         ar->winrct= *remainder;
331                         
332                         if(ar->alignment==RGN_ALIGN_TOP) {
333                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
334                                 remainder->ymax= ar->winrct.ymin - 1;
335                         }
336                         else {
337                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
338                                 remainder->ymin= ar->winrct.ymax + 1;
339                         }
340                 }
341         }
342         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
343                 
344                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
345                         ar->flag |= RGN_FLAG_TOO_SMALL;
346                 }
347                 else {
348                         int fac= rct_fits(remainder, 'h', prefsizex);
349                         
350                         if(fac < 0 )
351                                 prefsizex += fac;
352                         
353                         ar->winrct= *remainder;
354                         
355                         if(ar->alignment==RGN_ALIGN_RIGHT) {
356                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
357                                 remainder->xmax= ar->winrct.xmin - 1;
358                         }
359                         else {
360                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
361                                 remainder->xmin= ar->winrct.xmax + 1;
362                         }
363                 }
364         }
365         else if(ar->alignment==RGN_ALIGN_VSPLIT || ar->alignment==RGN_ALIGN_HSPLIT) {
366                 /* percentage subdiv*/
367                 ar->winrct= *remainder;
368                 
369                 if(ar->alignment==RGN_ALIGN_HSPLIT) {
370                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
371                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
372                                 remainder->xmin= ar->winrct.xmax+1;
373                         }
374                         else {
375                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
376                         }
377                 }
378                 else {
379                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
380                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
381                                 remainder->ymin= ar->winrct.ymax+1;
382                         }
383                         else {
384                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
385                         }
386                 }
387         }
388         else if(ar->alignment==RGN_ALIGN_QSPLIT) {
389                 ar->winrct= *remainder;
390                 
391                 /* test if there's still 4 regions left */
392                 if(quad==0) {
393                         ARegion *artest= ar->next;
394                         int count= 1;
395                         
396                         while(artest) {
397                                 artest->alignment= RGN_ALIGN_QSPLIT;
398                                 artest= artest->next;
399                                 count++;
400                         }
401                         
402                         if(count!=4) {
403                                 /* let's stop adding regions */
404                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
405                                 printf("region quadsplit failed\n");
406                         }
407                         else quad= 1;
408                 }
409                 if(quad) {
410                         if(quad==1) { /* left bottom */
411                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
412                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
413                         }
414                         else if(quad==2) { /* left top */
415                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
416                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
417                         }
418                         else if(quad==3) { /* right bottom */
419                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
420                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
421                         }
422                         else {  /* right top */
423                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
424                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
425                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
426                         }
427
428                         quad++;
429                 }
430         }
431         
432         /* for speedup */
433         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
434         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
435         
436         region_rect_recursive(ar->next, remainder, quad);
437 }
438
439 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
440 {
441         
442         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
443         else sa->totrct.xmin= sa->v1->vec.x;
444         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
445         else sa->totrct.xmax= sa->v4->vec.x;
446         
447         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
448         else sa->totrct.ymin= sa->v1->vec.y;
449         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
450         else sa->totrct.ymax= sa->v2->vec.y;
451         
452         /* for speedup */
453         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
454         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
455 }
456
457
458 #define AZONESPOT               12
459 void area_azone_initialize(ScrArea *sa) 
460 {
461         AZone *az;
462         if(sa->actionzones.first==NULL) {
463                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
464                 /* (ton) for time being just area, ARegion split is not foreseen on user level */
465                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
466                 BLI_addtail(&(sa->actionzones), az);
467                 az->type= AZONE_TRI;
468                 az->pos= AZONE_SW;
469                 
470                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
471                 BLI_addtail(&(sa->actionzones), az);
472                 az->type= AZONE_TRI;
473                 az->pos= AZONE_NE;
474         }
475         
476         for(az= sa->actionzones.first; az; az= az->next) {
477                 if(az->pos==AZONE_SW) {
478                         az->x1= sa->v1->vec.x+1;
479                         az->y1= sa->v1->vec.y+1;
480                         az->x2= sa->v1->vec.x+AZONESPOT;
481                         az->y2= sa->v1->vec.y+AZONESPOT;
482                 } 
483                 else if (az->pos==AZONE_NE) {
484                         az->x1= sa->v3->vec.x;
485                         az->y1= sa->v3->vec.y;
486                         az->x2= sa->v3->vec.x-AZONESPOT;
487                         az->y2= sa->v3->vec.y-AZONESPOT;
488                 }
489         }
490 }
491
492 /* used for area initialize below */
493 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
494 {
495         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
496                 if(ar->swinid)
497                         wm_subwindow_close(win, ar->swinid);
498                 ar->swinid= 0;
499         }
500         else if(ar->swinid==0)
501                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
502         else 
503                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
504 }
505
506 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
507 {
508         /* note, add-handler checks if it already exists */
509         
510         // XXX it would be good to have boundbox checks for some of these...
511         if(flag & ED_KEYMAP_UI) {
512                 UI_add_region_handlers(handlers);
513         }
514         if(flag & ED_KEYMAP_VIEW2D) {
515                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
516                 WM_event_add_keymap_handler(handlers, keymap);
517         }
518         if(flag & ED_KEYMAP_MARKERS) {
519                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
520                 WM_event_add_keymap_handler(handlers, keymap);
521                 // XXX need boundbox check urgently!!!
522         }
523         if(flag & ED_KEYMAP_ANIMATION) {
524                 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
525                 WM_event_add_keymap_handler(handlers, keymap);
526         }
527 }
528
529
530 /* called in screen_refresh, or screens_init, also area size changes */
531 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
532 {
533         ARegion *ar;
534         rcti rect;
535         
536         /* set typedefinitions */
537         sa->type= BKE_spacetype_from_id(sa->spacetype);
538         
539         if(sa->type==NULL) {
540                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
541                 sa->type= BKE_spacetype_from_id(sa->spacetype);
542         }
543         
544         for(ar= sa->regionbase.first; ar; ar= ar->next)
545                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
546         
547         /* area sizes */
548         area_calc_totrct(sa, win->sizex, win->sizey);
549         
550         /* region rect sizes */
551         rect= sa->totrct;
552         region_rect_recursive(sa->regionbase.first, &rect, 0);
553         
554         /* default area handlers */
555         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
556         /* checks spacedata, adds own handlers */
557         if(sa->type->init)
558                 sa->type->init(wm, sa);
559         
560         /* region windows, default and own handlers */
561         for(ar= sa->regionbase.first; ar; ar= ar->next) {
562                 region_subwindow(wm, win, ar);
563                 
564                 /* default region handlers */
565                 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
566
567                 if(ar->type->init)
568                         ar->type->init(wm, ar);
569                 
570         }
571         area_azone_initialize(sa);
572 }
573
574 /* externally called for floating regions like menus */
575 void ED_region_init(bContext *C, ARegion *ar)
576 {
577 //      ARegionType *at= ar->type;
578         
579         /* refresh can be called before window opened */
580         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
581         
582 }
583
584
585 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
586 /* area vertices were set */
587 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
588 {
589         SpaceType *st;
590         ARegion *ar;
591         
592         sa1->headertype= sa2->headertype;
593         sa1->spacetype= sa2->spacetype;
594         sa1->butspacetype= sa2->butspacetype;
595         
596         if(swap_space) {
597                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
598                 /* exception: ensure preview is reset */
599 //              if(sa1->spacetype==SPACE_VIEW3D)
600 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
601         }
602         else {
603                 BKE_spacedata_freelist(&sa1->spacedata);
604                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
605         }
606         
607         /* regions */
608         st= BKE_spacetype_from_id(sa1->spacetype);
609         for(ar= sa1->regionbase.first; ar; ar= ar->next)
610                 BKE_area_region_free(st, ar);
611         BLI_freelistN(&sa1->regionbase);
612         
613         st= BKE_spacetype_from_id(sa2->spacetype);
614         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
615                 ARegion *newar= BKE_area_region_copy(st, ar);
616                 BLI_addtail(&sa1->regionbase, newar);
617         }
618                 
619 #ifndef DISABLE_PYTHON
620         /* scripts */
621         BPY_free_scriptlink(&sa1->scriptlink);
622         sa1->scriptlink= sa2->scriptlink;
623         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
624 #endif
625 }
626
627 /* *********** Space switching code *********** */
628
629 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
630 {
631         if(sa->spacetype != type) {
632                 SpaceType *st;
633                 SpaceLink *slold;
634                 SpaceLink *sl;
635
636                 ED_area_exit(C, sa);
637
638                 st= BKE_spacetype_from_id(type);
639                 slold= sa->spacedata.first;
640
641                 sa->spacetype= type;
642                 sa->butspacetype= type;
643                 sa->type= st;
644                 
645                 /* check previously stored space */
646                 for (sl= sa->spacedata.first; sl; sl= sl->next)
647                         if(sl->spacetype==type)
648                                 break;
649                 
650                 /* old spacedata... happened during work on 2.50, remove */
651                 if(sl && sl->regionbase.first==NULL) {
652                         st->free(sl);
653                         BLI_freelinkN(&sa->spacedata, sl);
654                         sl= NULL;
655                 }
656                 
657                 if (sl) {
658                         
659                         /* swap regions */
660                         slold->regionbase= sa->regionbase;
661                         sa->regionbase= sl->regionbase;
662                         sl->regionbase.first= sl->regionbase.last= NULL;
663                         
664                         /* put in front of list */
665                         BLI_remlink(&sa->spacedata, sl);
666                         BLI_addhead(&sa->spacedata, sl);
667                 } 
668                 else {
669                         /* new space */
670                         if(st) {
671                                 sl= st->new(C);
672                                 BLI_addhead(&sa->spacedata, sl);
673                                 
674                                 /* swap regions */
675                                 if(slold)
676                                         slold->regionbase= sa->regionbase;
677                                 sa->regionbase= sl->regionbase;
678                                 sl->regionbase.first= sl->regionbase.last= NULL;
679                         }
680                 }
681                 
682                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
683                 
684                 /* tell WM to refresh, cursor types etc */
685                 WM_event_add_mousemove(C);
686                 
687                 ED_area_tag_redraw(sa);
688         }
689 }
690
691 void ED_area_prevspace(bContext *C)
692 {
693         SpaceLink *sl= CTX_wm_space_data(C);
694         ScrArea *sa= CTX_wm_area(C);
695
696         /* cleanup */
697 #if 0 // XXX needs to be space type specific
698         if(sfile->spacetype==SPACE_FILE) {
699                 if(sfile->pupmenu) {
700                         MEM_freeN(sfile->pupmenu);
701                         sfile->pupmenu= NULL;
702                 }
703         }
704 #endif
705
706         if(sl->next) {
707
708 #if 0 // XXX check whether this is still needed
709                 if (sfile->spacetype == SPACE_SCRIPT) {
710                         SpaceScript *sc = (SpaceScript *)sfile;
711                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
712                 }
713 #endif
714
715                 ED_area_newspace(C, sa, sl->next->spacetype);
716         }
717         else {
718                 ED_area_newspace(C, sa, SPACE_INFO);
719         }
720         ED_area_tag_redraw(sa);
721 }
722
723 static char *windowtype_pup(void)
724 {
725         return(
726                    "Window type:%t" //14
727                    "|3D View %x1" //30
728                    
729                    "|%l" // 33
730                    
731                    "|Ipo Curve Editor %x2" //54
732                    "|Action Editor %x12" //73
733                    "|NLA Editor %x13" //94
734                    
735                    "|%l" //97
736                    
737                    "|UV/Image Editor %x6" //117
738                    
739                    "|Video Sequence Editor %x8" //143
740                    "|Timeline %x15" //163
741                    "|Audio Window %x11" //163
742                    "|Text Editor %x9" //179
743                    
744                    "|%l" //192
745                    
746                    
747                    "|User Preferences %x7" //213
748                    "|Outliner %x3" //232
749                    "|Buttons Window %x4" //251
750                    "|Node Editor %x16"
751                    "|%l" //254
752                    
753                    "|File Browser %x5" //290
754                    
755                    "|%l" //293
756                    
757                    "|Scripts Window %x14"//313
758                    );
759 }
760
761 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
762 {
763         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
764         ED_area_tag_redraw(CTX_wm_area(C));
765 }
766
767 /* returns offset for next button in header */
768 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
769 {
770         ScrArea *sa= CTX_wm_area(C);
771         uiBut *but;
772         int xco= 8;
773         
774         if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
775         else uiBlockSetCol(block, TH_HEADERDESEL);
776         
777         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
778                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
779                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
780                                                    "Displays Current Window Type. "
781                                                    "Click for menu of available types.");
782         uiButSetFunc(but, spacefunc, NULL, NULL);
783         
784         xco += XIC + 14;
785         
786         uiBlockSetEmboss(block, UI_EMBOSSN);
787         if (sa->flag & HEADER_NO_PULLDOWN) {
788                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
789                                                  ICON_DISCLOSURE_TRI_RIGHT,
790                                                  xco,yco,XIC,YIC-2,
791                                                  &(sa->flag), 0, 0, 0, 0, 
792                                                  "Show pulldown menus");
793         }
794         else {
795                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
796                                                  ICON_DISCLOSURE_TRI_DOWN,
797                                                  xco,yco,XIC,YIC-2,
798                                                  &(sa->flag), 0, 0, 0, 0, 
799                                                  "Hide pulldown menus");
800         }
801         xco+=XIC;
802         
803         return xco;
804 }
805