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