2.5: WM Compositing
[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         /* if no partial draw rect set, full rect */
197         if(ar->drawrct.xmin == ar->drawrct.xmax)
198                 ar->drawrct= ar->winrct;
199         
200         /* extra clip for safety */
201         ar->drawrct.xmin= MAX2(ar->winrct.xmin, ar->drawrct.xmin);
202         ar->drawrct.ymin= MAX2(ar->winrct.ymin, ar->drawrct.ymin);
203         ar->drawrct.xmax= MIN2(ar->winrct.xmax, ar->drawrct.xmax);
204         ar->drawrct.ymax= MIN2(ar->winrct.ymax, ar->drawrct.ymax);
205         
206         /* note; this sets state, so we can use wmOrtho and friends */
207         wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
208         
209         /* optional header info instead? */
210         if(ar->headerstr) {
211                 float col[3];
212                 UI_SetTheme(sa);
213                 UI_GetThemeColor3fv(TH_HEADER, col);
214                 glClearColor(col[0], col[1], col[2], 0.0);
215                 glClear(GL_COLOR_BUFFER_BIT);
216                 
217                 UI_ThemeColor(TH_MENU_TEXT);
218                 glRasterPos2i(20, 6);
219                 BMF_DrawString(G.font, ar->headerstr);
220         }
221         else if(at->draw) {
222                 UI_SetTheme(sa);
223                 at->draw(C, ar);
224                 UI_SetTheme(NULL);
225         }
226         
227         if(sa)
228                 region_draw_emboss(ar);
229
230         uiFreeInactiveBlocks(C, &ar->uiblocks);
231         
232         /* XXX test: add convention to end regions always in pixel space, for drawing of borders/gestures etc */
233         ED_region_pixelspace(ar);
234         
235         ar->do_draw= 0;
236         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
237 }
238
239 /* **********************************
240    maybe silly, but let's try for now
241    to keep these tags protected
242    ********************************** */
243
244 void ED_region_tag_redraw(ARegion *ar)
245 {
246         if(ar) {
247                 /* zero region means full region redraw */
248                 ar->do_draw= 1;
249                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
250         }
251 }
252
253 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
254 {
255         if(ar) {
256                 if(!ar->do_draw) {
257                         /* no redraw set yet, set partial region */
258                         ar->do_draw= 1;
259                         ar->drawrct= *rct;
260                 }
261                 else if(ar->drawrct.xmin != ar->drawrct.xmax) {
262                         /* partial redraw already set, expand region */
263                         ar->drawrct.xmin= MIN2(ar->drawrct.xmin, rct->xmin);
264                         ar->drawrct.ymin= MIN2(ar->drawrct.ymin, rct->ymin);
265                         ar->drawrct.xmax= MAX2(ar->drawrct.xmax, rct->xmax);
266                         ar->drawrct.ymax= MAX2(ar->drawrct.ymax, rct->ymax);
267                 }
268         }
269 }
270
271 void ED_area_tag_redraw(ScrArea *sa)
272 {
273         ARegion *ar;
274         
275         if(sa)
276                 for(ar= sa->regionbase.first; ar; ar= ar->next)
277                         ED_region_tag_redraw(ar);
278 }
279
280 void ED_area_tag_refresh(ScrArea *sa)
281 {
282         if(sa)
283                 sa->do_refresh= 1;
284 }
285
286 /* *************************************************************** */
287
288 /* use NULL to disable it */
289 void ED_area_headerprint(ScrArea *sa, const char *str)
290 {
291         ARegion *ar;
292         
293         for(ar= sa->regionbase.first; ar; ar= ar->next) {
294                 if(ar->regiontype==RGN_TYPE_HEADER) {
295                         if(str) {
296                                 if(ar->headerstr==NULL)
297                                         ar->headerstr= MEM_mallocN(256, "headerprint");
298                                 BLI_strncpy(ar->headerstr, str, 256);
299                         }
300                         else if(ar->headerstr) {
301                                 MEM_freeN(ar->headerstr);
302                                 ar->headerstr= NULL;
303                         }
304                         ED_region_tag_redraw(ar);
305                 }
306         }
307 }
308
309 /* *************************************************************** */
310
311 /* dir is direction to check, not the splitting edge direction! */
312 static int rct_fits(rcti *rect, char dir, int size)
313 {
314         if(dir=='h') {
315                 return rect->xmax-rect->xmin - size;
316         }
317         else { // 'v'
318                 return rect->ymax-rect->ymin - size;
319         }
320 }
321
322 static void region_rect_recursive(ARegion *ar, rcti *remainder, int quad)
323 {
324         int prefsizex, prefsizey;
325         
326         if(ar==NULL)
327                 return;
328         
329         /* clear state flags first */
330         ar->flag &= ~RGN_FLAG_TOO_SMALL;
331         /* user errors */
332         if(ar->next==NULL && ar->alignment!=RGN_ALIGN_QSPLIT)
333                 ar->alignment= RGN_ALIGN_NONE;
334         
335         prefsizex= ar->type->minsizex;
336         prefsizey= ar->type->minsizey;
337         
338         /* hidden is user flag */
339         if(ar->flag & RGN_FLAG_HIDDEN);
340         /* XXX floating area region, not handled yet here */
341         else if(ar->alignment == RGN_ALIGN_FLOAT);
342         /* remainder is too small for any usage */
343         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
344                 ar->flag |= RGN_FLAG_TOO_SMALL;
345         }
346         else if(ar->alignment==RGN_ALIGN_NONE) {
347                 /* typically last region */
348                 ar->winrct= *remainder;
349                 BLI_init_rcti(remainder, 0, 0, 0, 0);
350         }
351         else if(ar->alignment==RGN_ALIGN_TOP || ar->alignment==RGN_ALIGN_BOTTOM) {
352                 
353                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
354                         ar->flag |= RGN_FLAG_TOO_SMALL;
355                 }
356                 else {
357                         int fac= rct_fits(remainder, 'v', prefsizey);
358
359                         if(fac < 0 )
360                                 prefsizey += fac;
361                         
362                         ar->winrct= *remainder;
363                         
364                         if(ar->alignment==RGN_ALIGN_TOP) {
365                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
366                                 remainder->ymax= ar->winrct.ymin - 1;
367                         }
368                         else {
369                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
370                                 remainder->ymin= ar->winrct.ymax + 1;
371                         }
372                 }
373         }
374         else if(ar->alignment==RGN_ALIGN_LEFT || ar->alignment==RGN_ALIGN_RIGHT) {
375                 
376                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
377                         ar->flag |= RGN_FLAG_TOO_SMALL;
378                 }
379                 else {
380                         int fac= rct_fits(remainder, 'h', prefsizex);
381                         
382                         if(fac < 0 )
383                                 prefsizex += fac;
384                         
385                         ar->winrct= *remainder;
386                         
387                         if(ar->alignment==RGN_ALIGN_RIGHT) {
388                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
389                                 remainder->xmax= ar->winrct.xmin - 1;
390                         }
391                         else {
392                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
393                                 remainder->xmin= ar->winrct.xmax + 1;
394                         }
395                 }
396         }
397         else if(ar->alignment==RGN_ALIGN_VSPLIT || ar->alignment==RGN_ALIGN_HSPLIT) {
398                 /* percentage subdiv*/
399                 ar->winrct= *remainder;
400                 
401                 if(ar->alignment==RGN_ALIGN_HSPLIT) {
402                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
403                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
404                                 remainder->xmin= ar->winrct.xmax+1;
405                         }
406                         else {
407                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
408                         }
409                 }
410                 else {
411                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
412                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
413                                 remainder->ymin= ar->winrct.ymax+1;
414                         }
415                         else {
416                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
417                         }
418                 }
419         }
420         else if(ar->alignment==RGN_ALIGN_QSPLIT) {
421                 ar->winrct= *remainder;
422                 
423                 /* test if there's still 4 regions left */
424                 if(quad==0) {
425                         ARegion *artest= ar->next;
426                         int count= 1;
427                         
428                         while(artest) {
429                                 artest->alignment= RGN_ALIGN_QSPLIT;
430                                 artest= artest->next;
431                                 count++;
432                         }
433                         
434                         if(count!=4) {
435                                 /* let's stop adding regions */
436                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
437                                 printf("region quadsplit failed\n");
438                         }
439                         else quad= 1;
440                 }
441                 if(quad) {
442                         if(quad==1) { /* left bottom */
443                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
444                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
445                         }
446                         else if(quad==2) { /* left top */
447                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
448                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
449                         }
450                         else if(quad==3) { /* right bottom */
451                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
452                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
453                         }
454                         else {  /* right top */
455                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
456                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
457                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
458                         }
459
460                         quad++;
461                 }
462         }
463         
464         /* for speedup */
465         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
466         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
467         
468         region_rect_recursive(ar->next, remainder, quad);
469 }
470
471 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
472 {
473         
474         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
475         else sa->totrct.xmin= sa->v1->vec.x;
476         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
477         else sa->totrct.xmax= sa->v4->vec.x;
478         
479         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
480         else sa->totrct.ymin= sa->v1->vec.y;
481         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
482         else sa->totrct.ymax= sa->v2->vec.y;
483         
484         /* for speedup */
485         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
486         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
487 }
488
489
490 #define AZONESPOT               12
491 void area_azone_initialize(ScrArea *sa) 
492 {
493         AZone *az;
494         if(sa->actionzones.first==NULL) {
495                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
496                 /* (ton) for time being just area, ARegion split is not foreseen on user level */
497                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
498                 BLI_addtail(&(sa->actionzones), az);
499                 az->type= AZONE_TRI;
500                 az->pos= AZONE_SW;
501                 
502                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
503                 BLI_addtail(&(sa->actionzones), az);
504                 az->type= AZONE_TRI;
505                 az->pos= AZONE_NE;
506         }
507         
508         for(az= sa->actionzones.first; az; az= az->next) {
509                 if(az->pos==AZONE_SW) {
510                         az->x1= sa->v1->vec.x+1;
511                         az->y1= sa->v1->vec.y+1;
512                         az->x2= sa->v1->vec.x+AZONESPOT;
513                         az->y2= sa->v1->vec.y+AZONESPOT;
514                 } 
515                 else if (az->pos==AZONE_NE) {
516                         az->x1= sa->v3->vec.x;
517                         az->y1= sa->v3->vec.y;
518                         az->x2= sa->v3->vec.x-AZONESPOT;
519                         az->y2= sa->v3->vec.y-AZONESPOT;
520                 }
521         }
522 }
523
524 /* used for area initialize below */
525 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
526 {
527         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
528                 if(ar->swinid)
529                         wm_subwindow_close(win, ar->swinid);
530                 ar->swinid= 0;
531         }
532         else if(ar->swinid==0)
533                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
534         else 
535                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
536 }
537
538 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
539 {
540         /* note, add-handler checks if it already exists */
541         
542         // XXX it would be good to have boundbox checks for some of these...
543         if(flag & ED_KEYMAP_UI) {
544                 UI_add_region_handlers(handlers);
545         }
546         if(flag & ED_KEYMAP_VIEW2D) {
547                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
548                 WM_event_add_keymap_handler(handlers, keymap);
549         }
550         if(flag & ED_KEYMAP_MARKERS) {
551                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
552                 WM_event_add_keymap_handler(handlers, keymap);
553                 // XXX need boundbox check urgently!!!
554         }
555         if(flag & ED_KEYMAP_ANIMATION) {
556                 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
557                 WM_event_add_keymap_handler(handlers, keymap);
558         }
559 }
560
561
562 /* called in screen_refresh, or screens_init, also area size changes */
563 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
564 {
565         ARegion *ar;
566         rcti rect;
567         
568         /* set typedefinitions */
569         sa->type= BKE_spacetype_from_id(sa->spacetype);
570         
571         if(sa->type==NULL) {
572                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
573                 sa->type= BKE_spacetype_from_id(sa->spacetype);
574         }
575         
576         for(ar= sa->regionbase.first; ar; ar= ar->next)
577                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
578         
579         /* area sizes */
580         area_calc_totrct(sa, win->sizex, win->sizey);
581         
582         /* region rect sizes */
583         rect= sa->totrct;
584         region_rect_recursive(sa->regionbase.first, &rect, 0);
585         
586         /* default area handlers */
587         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
588         /* checks spacedata, adds own handlers */
589         if(sa->type->init)
590                 sa->type->init(wm, sa);
591         
592         /* region windows, default and own handlers */
593         for(ar= sa->regionbase.first; ar; ar= ar->next) {
594                 region_subwindow(wm, win, ar);
595                 
596                 /* default region handlers */
597                 ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
598
599                 if(ar->type->init)
600                         ar->type->init(wm, ar);
601                 
602         }
603         area_azone_initialize(sa);
604 }
605
606 /* externally called for floating regions like menus */
607 void ED_region_init(bContext *C, ARegion *ar)
608 {
609 //      ARegionType *at= ar->type;
610         
611         /* refresh can be called before window opened */
612         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
613         
614 }
615
616
617 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
618 /* area vertices were set */
619 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
620 {
621         SpaceType *st;
622         ARegion *ar;
623         
624         sa1->headertype= sa2->headertype;
625         sa1->spacetype= sa2->spacetype;
626         sa1->butspacetype= sa2->butspacetype;
627         
628         if(swap_space) {
629                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
630                 /* exception: ensure preview is reset */
631 //              if(sa1->spacetype==SPACE_VIEW3D)
632 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
633         }
634         else {
635                 BKE_spacedata_freelist(&sa1->spacedata);
636                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
637         }
638         
639         /* regions */
640         st= BKE_spacetype_from_id(sa1->spacetype);
641         for(ar= sa1->regionbase.first; ar; ar= ar->next)
642                 BKE_area_region_free(st, ar);
643         BLI_freelistN(&sa1->regionbase);
644         
645         st= BKE_spacetype_from_id(sa2->spacetype);
646         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
647                 ARegion *newar= BKE_area_region_copy(st, ar);
648                 BLI_addtail(&sa1->regionbase, newar);
649         }
650                 
651 #ifndef DISABLE_PYTHON
652         /* scripts */
653         BPY_free_scriptlink(&sa1->scriptlink);
654         sa1->scriptlink= sa2->scriptlink;
655         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
656 #endif
657 }
658
659 /* *********** Space switching code *********** */
660
661 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
662 {
663         if(sa->spacetype != type) {
664                 SpaceType *st;
665                 SpaceLink *slold;
666                 SpaceLink *sl;
667
668                 ED_area_exit(C, sa);
669
670                 st= BKE_spacetype_from_id(type);
671                 slold= sa->spacedata.first;
672
673                 sa->spacetype= type;
674                 sa->butspacetype= type;
675                 sa->type= st;
676                 
677                 /* check previously stored space */
678                 for (sl= sa->spacedata.first; sl; sl= sl->next)
679                         if(sl->spacetype==type)
680                                 break;
681                 
682                 /* old spacedata... happened during work on 2.50, remove */
683                 if(sl && sl->regionbase.first==NULL) {
684                         st->free(sl);
685                         BLI_freelinkN(&sa->spacedata, sl);
686                         sl= NULL;
687                 }
688                 
689                 if (sl) {
690                         
691                         /* swap regions */
692                         slold->regionbase= sa->regionbase;
693                         sa->regionbase= sl->regionbase;
694                         sl->regionbase.first= sl->regionbase.last= NULL;
695                         
696                         /* put in front of list */
697                         BLI_remlink(&sa->spacedata, sl);
698                         BLI_addhead(&sa->spacedata, sl);
699                 } 
700                 else {
701                         /* new space */
702                         if(st) {
703                                 sl= st->new(C);
704                                 BLI_addhead(&sa->spacedata, sl);
705                                 
706                                 /* swap regions */
707                                 if(slold)
708                                         slold->regionbase= sa->regionbase;
709                                 sa->regionbase= sl->regionbase;
710                                 sl->regionbase.first= sl->regionbase.last= NULL;
711                         }
712                 }
713                 
714                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
715                 
716                 /* tell WM to refresh, cursor types etc */
717                 WM_event_add_mousemove(C);
718                 
719                 ED_area_tag_redraw(sa);
720         }
721 }
722
723 void ED_area_prevspace(bContext *C)
724 {
725         SpaceLink *sl= CTX_wm_space_data(C);
726         ScrArea *sa= CTX_wm_area(C);
727
728         /* cleanup */
729 #if 0 // XXX needs to be space type specific
730         if(sfile->spacetype==SPACE_FILE) {
731                 if(sfile->pupmenu) {
732                         MEM_freeN(sfile->pupmenu);
733                         sfile->pupmenu= NULL;
734                 }
735         }
736 #endif
737
738         if(sl->next) {
739
740 #if 0 // XXX check whether this is still needed
741                 if (sfile->spacetype == SPACE_SCRIPT) {
742                         SpaceScript *sc = (SpaceScript *)sfile;
743                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
744                 }
745 #endif
746
747                 ED_area_newspace(C, sa, sl->next->spacetype);
748         }
749         else {
750                 ED_area_newspace(C, sa, SPACE_INFO);
751         }
752         ED_area_tag_redraw(sa);
753 }
754
755 static char *windowtype_pup(void)
756 {
757         return(
758                    "Window type:%t" //14
759                    "|3D View %x1" //30
760                    
761                    "|%l" // 33
762                    
763                    "|Ipo Curve Editor %x2" //54
764                    "|Action Editor %x12" //73
765                    "|NLA Editor %x13" //94
766                    
767                    "|%l" //97
768                    
769                    "|UV/Image Editor %x6" //117
770                    
771                    "|Video Sequence Editor %x8" //143
772                    "|Timeline %x15" //163
773                    "|Audio Window %x11" //163
774                    "|Text Editor %x9" //179
775                    
776                    "|%l" //192
777                    
778                    
779                    "|User Preferences %x7" //213
780                    "|Outliner %x3" //232
781                    "|Buttons Window %x4" //251
782                    "|Node Editor %x16"
783                    "|%l" //254
784                    
785                    "|File Browser %x5" //290
786                    
787                    "|%l" //293
788                    
789                    "|Scripts Window %x14"//313
790                    );
791 }
792
793 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
794 {
795         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
796         ED_area_tag_redraw(CTX_wm_area(C));
797 }
798
799 /* returns offset for next button in header */
800 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
801 {
802         ScrArea *sa= CTX_wm_area(C);
803         uiBut *but;
804         int xco= 8;
805         
806         if(ED_screen_area_active(C)) uiBlockSetCol(block, TH_HEADER);
807         else uiBlockSetCol(block, TH_HEADERDESEL);
808         
809         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
810                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
811                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
812                                                    "Displays Current Window Type. "
813                                                    "Click for menu of available types.");
814         uiButSetFunc(but, spacefunc, NULL, NULL);
815         
816         xco += XIC + 14;
817         
818         uiBlockSetEmboss(block, UI_EMBOSSN);
819         if (sa->flag & HEADER_NO_PULLDOWN) {
820                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
821                                                  ICON_DISCLOSURE_TRI_RIGHT,
822                                                  xco,yco,XIC,YIC-2,
823                                                  &(sa->flag), 0, 0, 0, 0, 
824                                                  "Show pulldown menus");
825         }
826         else {
827                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
828                                                  ICON_DISCLOSURE_TRI_DOWN,
829                                                  xco,yco,XIC,YIC-2,
830                                                  &(sa->flag), 0, 0, 0, 0, 
831                                                  "Hide pulldown menus");
832         }
833         xco+=XIC;
834         
835         return xco;
836 }
837