2.5
[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                         /* pass on */
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         rcti *remainder_prev= remainder;
329         int prefsizex, prefsizey;
330         int alignment;
331         
332         if(ar==NULL)
333                 return;
334         
335         BLI_init_rcti(&ar->winrct, 0, 0, 0, 0);
336         
337         /* for test; allow split of previously defined region */
338         if(ar->alignment & RGN_SPLIT_PREV)
339                 if(ar->prev)
340                         remainder= &ar->prev->winrct;
341         
342         alignment = ar->alignment & ~RGN_SPLIT_PREV;
343         
344         /* clear state flags first */
345         ar->flag &= ~RGN_FLAG_TOO_SMALL;
346         /* user errors */
347         if(ar->next==NULL && alignment!=RGN_ALIGN_QSPLIT)
348                 alignment= RGN_ALIGN_NONE;
349         
350         prefsizex= ar->type->minsizex;
351         prefsizey= ar->type->minsizey;
352         
353         /* hidden is user flag */
354         if(ar->flag & RGN_FLAG_HIDDEN);
355         /* XXX floating area region, not handled yet here */
356         else if(alignment == RGN_ALIGN_FLOAT);
357         /* remainder is too small for any usage */
358         else if( rct_fits(remainder, 'v', 1)<0 || rct_fits(remainder, 'h', 1) < 0 ) {
359                 ar->flag |= RGN_FLAG_TOO_SMALL;
360         }
361         else if(alignment==RGN_ALIGN_NONE) {
362                 /* typically last region */
363                 ar->winrct= *remainder;
364                 BLI_init_rcti(remainder, 0, 0, 0, 0);
365         }
366         else if(alignment==RGN_ALIGN_TOP || alignment==RGN_ALIGN_BOTTOM) {
367                 
368                 if( rct_fits(remainder, 'v', prefsizey) < 0 ) {
369                         ar->flag |= RGN_FLAG_TOO_SMALL;
370                 }
371                 else {
372                         int fac= rct_fits(remainder, 'v', prefsizey);
373
374                         if(fac < 0 )
375                                 prefsizey += fac;
376                         
377                         ar->winrct= *remainder;
378                         
379                         if(alignment==RGN_ALIGN_TOP) {
380                                 ar->winrct.ymin= ar->winrct.ymax - prefsizey + 1;
381                                 remainder->ymax= ar->winrct.ymin - 1;
382                         }
383                         else {
384                                 ar->winrct.ymax= ar->winrct.ymin + prefsizey - 1;
385                                 remainder->ymin= ar->winrct.ymax + 1;
386                         }
387                 }
388         }
389         else if( ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
390                 
391                 if( rct_fits(remainder, 'h', prefsizex) < 0 ) {
392                         ar->flag |= RGN_FLAG_TOO_SMALL;
393                 }
394                 else {
395                         int fac= rct_fits(remainder, 'h', prefsizex);
396                         
397                         if(fac < 0 )
398                                 prefsizex += fac;
399                         
400                         ar->winrct= *remainder;
401                         
402                         if(ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
403                                 ar->winrct.xmin= ar->winrct.xmax - prefsizex + 1;
404                                 if(alignment==RGN_ALIGN_RIGHT)
405                                         remainder->xmax= ar->winrct.xmin - 1;
406                         }
407                         else {
408                                 ar->winrct.xmax= ar->winrct.xmin + prefsizex - 1;
409                                 if(alignment==RGN_ALIGN_LEFT)
410                                         remainder->xmin= ar->winrct.xmax + 1;
411                         }
412                 }
413         }
414         else if(alignment==RGN_ALIGN_VSPLIT || alignment==RGN_ALIGN_HSPLIT) {
415                 /* percentage subdiv*/
416                 ar->winrct= *remainder;
417                 
418                 if(alignment==RGN_ALIGN_HSPLIT) {
419                         if( rct_fits(remainder, 'h', prefsizex) > 4) {
420                                 ar->winrct.xmax= (remainder->xmin+remainder->xmax)/2;
421                                 remainder->xmin= ar->winrct.xmax+1;
422                         }
423                         else {
424                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
425                         }
426                 }
427                 else {
428                         if( rct_fits(remainder, 'v', prefsizey) > 4) {
429                                 ar->winrct.ymax= (remainder->ymin+remainder->ymax)/2;
430                                 remainder->ymin= ar->winrct.ymax+1;
431                         }
432                         else {
433                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
434                         }
435                 }
436         }
437         else if(alignment==RGN_ALIGN_QSPLIT) {
438                 ar->winrct= *remainder;
439                 
440                 /* test if there's still 4 regions left */
441                 if(quad==0) {
442                         ARegion *artest= ar->next;
443                         int count= 1;
444                         
445                         while(artest) {
446                                 artest->alignment= RGN_ALIGN_QSPLIT;
447                                 artest= artest->next;
448                                 count++;
449                         }
450                         
451                         if(count!=4) {
452                                 /* let's stop adding regions */
453                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
454                                 printf("region quadsplit failed\n");
455                         }
456                         else quad= 1;
457                 }
458                 if(quad) {
459                         if(quad==1) { /* left bottom */
460                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
461                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
462                         }
463                         else if(quad==2) { /* left top */
464                                 ar->winrct.xmax = (remainder->xmin + remainder->xmax)/2;
465                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
466                         }
467                         else if(quad==3) { /* right bottom */
468                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
469                                 ar->winrct.ymax = (remainder->ymin + remainder->ymax)/2;
470                         }
471                         else {  /* right top */
472                                 ar->winrct.xmin = 1 + (remainder->xmin + remainder->xmax)/2;
473                                 ar->winrct.ymin = 1 + (remainder->ymin + remainder->ymax)/2;
474                                 BLI_init_rcti(remainder, 0, 0, 0, 0);
475                         }
476
477                         quad++;
478                 }
479         }
480         
481         /* for speedup */
482         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
483         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
484         
485         /* restore test exception */
486         if(ar->alignment & RGN_SPLIT_PREV) {
487                 if(ar->prev) {
488                         remainder= remainder_prev;
489                         ar->prev->winx= ar->prev->winrct.xmax - ar->prev->winrct.xmin + 1;
490                         ar->prev->winy= ar->prev->winrct.ymax - ar->prev->winrct.ymin + 1;
491                 }
492         }
493         region_rect_recursive(ar->next, remainder, quad);
494 }
495
496 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
497 {
498         
499         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
500         else sa->totrct.xmin= sa->v1->vec.x;
501         if(sa->v4->vec.x<sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
502         else sa->totrct.xmax= sa->v4->vec.x;
503         
504         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
505         else sa->totrct.ymin= sa->v1->vec.y;
506         if(sa->v2->vec.y<sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
507         else sa->totrct.ymax= sa->v2->vec.y;
508         
509         /* for speedup */
510         sa->winx= sa->totrct.xmax-sa->totrct.xmin+1;
511         sa->winy= sa->totrct.ymax-sa->totrct.ymin+1;
512 }
513
514
515 #define AZONESPOT               12
516 void area_azone_initialize(ScrArea *sa) 
517 {
518         AZone *az;
519         if(sa->actionzones.first==NULL) {
520                 /* set action zones - should these actually be ARegions? With these we can easier check area hotzones */
521                 /* (ton) for time being just area, ARegion split is not foreseen on user level */
522                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
523                 BLI_addtail(&(sa->actionzones), az);
524                 az->type= AZONE_TRI;
525                 az->pos= AZONE_SW;
526                 
527                 az= (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
528                 BLI_addtail(&(sa->actionzones), az);
529                 az->type= AZONE_TRI;
530                 az->pos= AZONE_NE;
531         }
532         
533         for(az= sa->actionzones.first; az; az= az->next) {
534                 if(az->pos==AZONE_SW) {
535                         az->x1= sa->v1->vec.x+1;
536                         az->y1= sa->v1->vec.y+1;
537                         az->x2= sa->v1->vec.x+AZONESPOT;
538                         az->y2= sa->v1->vec.y+AZONESPOT;
539                 } 
540                 else if (az->pos==AZONE_NE) {
541                         az->x1= sa->v3->vec.x;
542                         az->y1= sa->v3->vec.y;
543                         az->x2= sa->v3->vec.x-AZONESPOT;
544                         az->y2= sa->v3->vec.y-AZONESPOT;
545                 }
546         }
547 }
548
549 /* used for area initialize below */
550 static void region_subwindow(wmWindowManager *wm, wmWindow *win, ARegion *ar)
551 {
552         if(ar->flag & (RGN_FLAG_HIDDEN|RGN_FLAG_TOO_SMALL)) {
553                 if(ar->swinid)
554                         wm_subwindow_close(win, ar->swinid);
555                 ar->swinid= 0;
556         }
557         else if(ar->swinid==0)
558                 ar->swinid= wm_subwindow_open(win, &ar->winrct);
559         else 
560                 wm_subwindow_position(win, ar->swinid, &ar->winrct);
561 }
562
563 static void ed_default_handlers(wmWindowManager *wm, ListBase *handlers, int flag)
564 {
565         /* note, add-handler checks if it already exists */
566         
567         // XXX it would be good to have boundbox checks for some of these...
568         if(flag & ED_KEYMAP_UI) {
569                 UI_add_region_handlers(handlers);
570         }
571         if(flag & ED_KEYMAP_VIEW2D) {
572                 ListBase *keymap= WM_keymap_listbase(wm, "View2D", 0, 0);
573                 WM_event_add_keymap_handler(handlers, keymap);
574         }
575         if(flag & ED_KEYMAP_MARKERS) {
576                 ListBase *keymap= WM_keymap_listbase(wm, "Markers", 0, 0);
577                 WM_event_add_keymap_handler(handlers, keymap);
578                 // XXX need boundbox check urgently!!!
579         }
580         if(flag & ED_KEYMAP_ANIMATION) {
581                 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
582                 WM_event_add_keymap_handler(handlers, keymap);
583         }
584         if(flag & ED_KEYMAP_FRAMES) {
585                 ListBase *keymap= WM_keymap_listbase(wm, "Frames", 0, 0);
586                 WM_event_add_keymap_handler(handlers, keymap);
587         }
588 }
589
590
591 /* called in screen_refresh, or screens_init, also area size changes */
592 void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
593 {
594         ARegion *ar;
595         rcti rect;
596         
597         /* set typedefinitions */
598         sa->type= BKE_spacetype_from_id(sa->spacetype);
599         
600         if(sa->type==NULL) {
601                 sa->butspacetype= sa->spacetype= SPACE_VIEW3D;
602                 sa->type= BKE_spacetype_from_id(sa->spacetype);
603         }
604         
605         for(ar= sa->regionbase.first; ar; ar= ar->next)
606                 ar->type= BKE_regiontype_from_id(sa->type, ar->regiontype);
607         
608         /* area sizes */
609         area_calc_totrct(sa, win->sizex, win->sizey);
610         
611         /* region rect sizes */
612         rect= sa->totrct;
613         region_rect_recursive(sa->regionbase.first, &rect, 0);
614         
615         /* default area handlers */
616         ed_default_handlers(wm, &sa->handlers, sa->type->keymapflag);
617         /* checks spacedata, adds own handlers */
618         if(sa->type->init)
619                 sa->type->init(wm, sa);
620         
621         /* region windows, default and own handlers */
622         for(ar= sa->regionbase.first; ar; ar= ar->next) {
623                 region_subwindow(wm, win, ar);
624                 
625                 if(ar->swinid) {
626                         /* default region handlers */
627                         ed_default_handlers(wm, &ar->handlers, ar->type->keymapflag);
628
629                         if(ar->type->init)
630                                 ar->type->init(wm, ar);
631                 }
632                 else {
633                         /* prevent uiblocks to run */
634                         uiFreeBlocks(NULL, &ar->uiblocks);      
635                 }
636                 
637         }
638         area_azone_initialize(sa);
639 }
640
641 /* externally called for floating regions like menus */
642 void ED_region_init(bContext *C, ARegion *ar)
643 {
644 //      ARegionType *at= ar->type;
645         
646         /* refresh can be called before window opened */
647         region_subwindow(CTX_wm_manager(C), CTX_wm_window(C), ar);
648         
649         ar->winx= ar->winrct.xmax - ar->winrct.xmin + 1;
650         ar->winy= ar->winrct.ymax - ar->winrct.ymin + 1;
651 }
652
653
654 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
655 /* area vertices were set */
656 void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
657 {
658         SpaceType *st;
659         ARegion *ar;
660         
661         sa1->headertype= sa2->headertype;
662         sa1->spacetype= sa2->spacetype;
663         sa1->butspacetype= sa2->butspacetype;
664         
665         if(swap_space == 1) {
666                 SWAP(ListBase, sa1->spacedata, sa2->spacedata);
667                 /* exception: ensure preview is reset */
668 //              if(sa1->spacetype==SPACE_VIEW3D)
669 // XXX                  BIF_view3d_previewrender_free(sa1->spacedata.first);
670         }
671         else if (swap_space == 2) {
672                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
673         }
674         else {
675                 BKE_spacedata_freelist(&sa1->spacedata);
676                 BKE_spacedata_copylist(&sa1->spacedata, &sa2->spacedata);
677         }
678         
679         /* Note; SPACE_EMPTY is possible on new screens */
680         
681         /* regions */
682         if(swap_space<2) {
683                 st= BKE_spacetype_from_id(sa1->spacetype);
684                 for(ar= sa1->regionbase.first; ar; ar= ar->next)
685                         BKE_area_region_free(st, ar);
686                 BLI_freelistN(&sa1->regionbase);
687         }
688         
689         st= BKE_spacetype_from_id(sa2->spacetype);
690         for(ar= sa2->regionbase.first; ar; ar= ar->next) {
691                 ARegion *newar= BKE_area_region_copy(st, ar);
692                 BLI_addtail(&sa1->regionbase, newar);
693         }
694                 
695 #ifndef DISABLE_PYTHON
696         /* scripts */
697         BPY_free_scriptlink(&sa1->scriptlink);
698         sa1->scriptlink= sa2->scriptlink;
699         BPY_copy_scriptlink(&sa1->scriptlink);  /* copies internal pointers */
700 #endif
701 }
702
703 /* *********** Space switching code *********** */
704
705 void ED_area_swapspace(bContext *C, ScrArea *sa1, ScrArea *sa2)
706 {
707         ScrArea *tmp= MEM_callocN(sizeof(ScrArea), "addscrarea");
708
709         ED_area_exit(C, sa1);
710         ED_area_exit(C, sa2);
711
712         tmp->spacetype= sa1->spacetype;
713         tmp->butspacetype= sa1->butspacetype;
714         BKE_spacedata_copyfirst(&tmp->spacedata, &sa1->spacedata);
715
716         area_copy_data(tmp, sa1, 2);
717         area_copy_data(sa1, sa2, 0);
718         area_copy_data(sa2, tmp, 0);
719         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa1);
720         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa2);
721
722         BKE_screen_area_free(tmp);
723         MEM_freeN(tmp);
724
725         /* tell WM to refresh, cursor types etc */
726         WM_event_add_mousemove(C);
727         
728         ED_area_tag_redraw(sa1);
729         ED_area_tag_refresh(sa1);
730         ED_area_tag_redraw(sa2);
731         ED_area_tag_refresh(sa2);
732 }
733
734 void ED_area_newspace(bContext *C, ScrArea *sa, int type)
735 {
736         if(sa->spacetype != type) {
737                 SpaceType *st;
738                 SpaceLink *slold;
739                 SpaceLink *sl;
740
741                 ED_area_exit(C, sa);
742
743                 st= BKE_spacetype_from_id(type);
744                 slold= sa->spacedata.first;
745
746                 sa->spacetype= type;
747                 sa->butspacetype= type;
748                 sa->type= st;
749                 
750                 /* check previously stored space */
751                 for (sl= sa->spacedata.first; sl; sl= sl->next)
752                         if(sl->spacetype==type)
753                                 break;
754                 
755                 /* old spacedata... happened during work on 2.50, remove */
756                 if(sl && sl->regionbase.first==NULL) {
757                         st->free(sl);
758                         BLI_freelinkN(&sa->spacedata, sl);
759                         sl= NULL;
760                 }
761                 
762                 if (sl) {
763                         
764                         /* swap regions */
765                         slold->regionbase= sa->regionbase;
766                         sa->regionbase= sl->regionbase;
767                         sl->regionbase.first= sl->regionbase.last= NULL;
768                         
769                         /* put in front of list */
770                         BLI_remlink(&sa->spacedata, sl);
771                         BLI_addhead(&sa->spacedata, sl);
772                 } 
773                 else {
774                         /* new space */
775                         if(st) {
776                                 sl= st->new(C);
777                                 BLI_addhead(&sa->spacedata, sl);
778                                 
779                                 /* swap regions */
780                                 if(slold)
781                                         slold->regionbase= sa->regionbase;
782                                 sa->regionbase= sl->regionbase;
783                                 sl->regionbase.first= sl->regionbase.last= NULL;
784                         }
785                 }
786                 
787                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
788                 
789                 /* tell WM to refresh, cursor types etc */
790                 WM_event_add_mousemove(C);
791                 
792                 ED_area_tag_redraw(sa);
793                 ED_area_tag_refresh(sa);
794         }
795 }
796
797 void ED_area_prevspace(bContext *C)
798 {
799         SpaceLink *sl= CTX_wm_space_data(C);
800         ScrArea *sa= CTX_wm_area(C);
801
802         /* cleanup */
803 #if 0 // XXX needs to be space type specific
804         if(sfile->spacetype==SPACE_FILE) {
805                 if(sfile->pupmenu) {
806                         MEM_freeN(sfile->pupmenu);
807                         sfile->pupmenu= NULL;
808                 }
809         }
810 #endif
811
812         if(sl->next) {
813
814 #if 0 // XXX check whether this is still needed
815                 if (sfile->spacetype == SPACE_SCRIPT) {
816                         SpaceScript *sc = (SpaceScript *)sfile;
817                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
818                 }
819 #endif
820
821                 ED_area_newspace(C, sa, sl->next->spacetype);
822         }
823         else {
824                 ED_area_newspace(C, sa, SPACE_INFO);
825         }
826         ED_area_tag_redraw(sa);
827 }
828
829 static char *windowtype_pup(void)
830 {
831         return(
832                    "Window type:%t" //14
833                    "|3D View %x1" //30
834                    
835                    "|%l" // 33
836                    
837                    "|Graph Editor %x2" //54
838                    "|DopeSheet %x12" //73
839                    "|NLA Editor %x13" //94
840                    
841                    "|%l" //97
842                    
843                    "|UV/Image Editor %x6" //117
844                    
845                    "|Video Sequence Editor %x8" //143
846                    "|Timeline %x15" //163
847                    "|Audio Window %x11" //163
848                    "|Text Editor %x9" //179
849                    
850                    "|%l" //192
851                    
852                    
853                    "|User Preferences %x7" //213
854                    "|Outliner %x3" //232
855                    "|Buttons Window %x4" //251
856                    "|Node Editor %x16"
857                    "|%l" //254
858                    
859                    "|File Browser %x5" //290
860                    
861                    "|%l" //293
862                    
863                    "|Scripts Window %x14"//313
864                    );
865 }
866
867 static void spacefunc(struct bContext *C, void *arg1, void *arg2)
868 {
869         ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
870         ED_area_tag_redraw(CTX_wm_area(C));
871 }
872
873 /* returns offset for next button in header */
874 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
875 {
876         ScrArea *sa= CTX_wm_area(C);
877         uiBut *but;
878         int xco= 8;
879         
880         but= uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
881                                                    windowtype_pup(), xco, yco, XIC+10, YIC, 
882                                                    &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
883                                                    "Displays Current Window Type. "
884                                                    "Click for menu of available types.");
885         uiButSetFunc(but, spacefunc, NULL, NULL);
886         
887         xco += XIC + 14;
888         
889         uiBlockSetEmboss(block, UI_EMBOSSN);
890         if (sa->flag & HEADER_NO_PULLDOWN) {
891                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
892                                                  ICON_DISCLOSURE_TRI_RIGHT,
893                                                  xco,yco,XIC,YIC-2,
894                                                  &(sa->flag), 0, 0, 0, 0, 
895                                                  "Show pulldown menus");
896         }
897         else {
898                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0, 
899                                                  ICON_DISCLOSURE_TRI_DOWN,
900                                                  xco,yco,XIC,YIC-2,
901                                                  &(sa->flag), 0, 0, 0, 0, 
902                                                  "Hide pulldown menus");
903         }
904         xco+=XIC;
905         
906         uiBlockSetEmboss(block, UI_EMBOSS);
907         
908         return xco;
909 }
910