remove scriptlinks,
[blender.git] / source / blender / editors / screen / screen_edit.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  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <string.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_vec_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_texture_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_context.h"
40 #include "BKE_global.h"
41 #include "BKE_library.h"
42 #include "BKE_main.h"
43 #include "BKE_node.h"
44 #include "BKE_screen.h"
45 #include "BKE_scene.h"
46 #include "BKE_utildefines.h"
47
48 #include "BIF_gl.h"
49 #include "BIF_glutil.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "ED_screen.h"
55 #include "ED_screen_types.h"
56
57 #include "wm_subwindow.h"
58
59 #include "screen_intern.h"      /* own module include */
60
61
62 /* ******************* screen vert, edge, area managing *********************** */
63
64 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
65 {
66         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
67         sv->vec.x= x;
68         sv->vec.y= y;
69         
70         BLI_addtail(&sc->vertbase, sv);
71         return sv;
72 }
73
74 static void sortscrvert(ScrVert **v1, ScrVert **v2)
75 {
76         ScrVert *tmp;
77         
78         if (*v1 > *v2) {
79                 tmp= *v1;
80                 *v1= *v2;
81                 *v2= tmp;       
82         }
83 }
84
85 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
86 {
87         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
88         
89         sortscrvert(&v1, &v2);
90         se->v1= v1;
91         se->v2= v2;
92         
93         BLI_addtail(&sc->edgebase, se);
94         return se;
95 }
96
97
98 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
99 {
100         ScrEdge *se;
101         
102         sortscrvert(&v1, &v2);
103         for (se= sc->edgebase.first; se; se= se->next)
104                 if(se->v1==v1 && se->v2==v2)
105                         return se;
106         
107         return NULL;
108 }
109
110 void removedouble_scrverts(bScreen *sc)
111 {
112         ScrVert *v1, *verg;
113         ScrEdge *se;
114         ScrArea *sa;
115         
116         verg= sc->vertbase.first;
117         while(verg) {
118                 if(verg->newv==NULL) {  /* !!! */
119                         v1= verg->next;
120                         while(v1) {
121                                 if(v1->newv==NULL) {    /* !?! */
122                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
123                                                 /* printf("doublevert\n"); */
124                                                 v1->newv= verg;
125                                         }
126                                 }
127                                 v1= v1->next;
128                         }
129                 }
130                 verg= verg->next;
131         }
132
133         /* replace pointers in edges and faces */
134         se= sc->edgebase.first;
135         while(se) {
136                 if(se->v1->newv) se->v1= se->v1->newv;
137                 if(se->v2->newv) se->v2= se->v2->newv;
138                 /* edges changed: so.... */
139                 sortscrvert(&(se->v1), &(se->v2));
140                 se= se->next;
141         }
142         sa= sc->areabase.first;
143         while(sa) {
144                 if(sa->v1->newv) sa->v1= sa->v1->newv;
145                 if(sa->v2->newv) sa->v2= sa->v2->newv;
146                 if(sa->v3->newv) sa->v3= sa->v3->newv;
147                 if(sa->v4->newv) sa->v4= sa->v4->newv;
148                 sa= sa->next;
149         }
150
151         /* remove */
152         verg= sc->vertbase.first;
153         while(verg) {
154                 v1= verg->next;
155                 if(verg->newv) {
156                         BLI_remlink(&sc->vertbase, verg);
157                         MEM_freeN(verg);
158                 }
159                 verg= v1;
160         }
161
162 }
163
164 void removenotused_scrverts(bScreen *sc)
165 {
166         ScrVert *sv, *svn;
167         ScrEdge *se;
168         
169         /* we assume edges are ok */
170         
171         se= sc->edgebase.first;
172         while(se) {
173                 se->v1->flag= 1;
174                 se->v2->flag= 1;
175                 se= se->next;
176         }
177         
178         sv= sc->vertbase.first;
179         while(sv) {
180                 svn= sv->next;
181                 if(sv->flag==0) {
182                         BLI_remlink(&sc->vertbase, sv);
183                         MEM_freeN(sv);
184                 }
185                 else sv->flag= 0;
186                 sv= svn;
187         }
188 }
189
190 void removedouble_scredges(bScreen *sc)
191 {
192         ScrEdge *verg, *se, *sn;
193         
194         /* compare */
195         verg= sc->edgebase.first;
196         while(verg) {
197                 se= verg->next;
198                 while(se) {
199                         sn= se->next;
200                         if(verg->v1==se->v1 && verg->v2==se->v2) {
201                                 BLI_remlink(&sc->edgebase, se);
202                                 MEM_freeN(se);
203                         }
204                         se= sn;
205                 }
206                 verg= verg->next;
207         }
208 }
209
210 void removenotused_scredges(bScreen *sc)
211 {
212         ScrEdge *se, *sen;
213         ScrArea *sa;
214         int a=0;
215         
216         /* sets flags when edge is used in area */
217         sa= sc->areabase.first;
218         while(sa) {
219                 se= screen_findedge(sc, sa->v1, sa->v2);
220                 if(se==0) printf("error: area %d edge 1 doesn't exist\n", a);
221                 else se->flag= 1;
222                 se= screen_findedge(sc, sa->v2, sa->v3);
223                 if(se==0) printf("error: area %d edge 2 doesn't exist\n", a);
224                 else se->flag= 1;
225                 se= screen_findedge(sc, sa->v3, sa->v4);
226                 if(se==0) printf("error: area %d edge 3 doesn't exist\n", a);
227                 else se->flag= 1;
228                 se= screen_findedge(sc, sa->v4, sa->v1);
229                 if(se==0) printf("error: area %d edge 4 doesn't exist\n", a);
230                 else se->flag= 1;
231                 sa= sa->next;
232                 a++;
233         }
234         se= sc->edgebase.first;
235         while(se) {
236                 sen= se->next;
237                 if(se->flag==0) {
238                         BLI_remlink(&sc->edgebase, se);
239                         MEM_freeN(se);
240                 }
241                 else se->flag= 0;
242                 se= sen;
243         }
244 }
245
246 int scredge_is_horizontal(ScrEdge *se)
247 {
248         return (se->v1->vec.y == se->v2->vec.y);
249 }
250
251 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
252 {
253         ScrEdge *se;
254         
255         for (se= sc->edgebase.first; se; se= se->next) {
256                 if (scredge_is_horizontal(se)) {
257                         short min, max;
258                         min= MIN2(se->v1->vec.x, se->v2->vec.x);
259                         max= MAX2(se->v1->vec.x, se->v2->vec.x);
260                         
261                         if (abs(my-se->v1->vec.y)<=2 && mx>=min && mx<=max)
262                                 return se;
263                 } 
264                 else {
265                         short min, max;
266                         min= MIN2(se->v1->vec.y, se->v2->vec.y);
267                         max= MAX2(se->v1->vec.y, se->v2->vec.y);
268                         
269                         if (abs(mx-se->v1->vec.x)<=2 && my>=min && my<=max)
270                                 return se;
271                 }
272         }
273         
274         return NULL;
275 }
276
277
278
279 /* adds no space data */
280 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
281 {
282         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
283         sa->v1= v1;
284         sa->v2= v2;
285         sa->v3= v3;
286         sa->v4= v4;
287         sa->headertype= headertype;
288         sa->spacetype= sa->butspacetype= spacetype;
289         
290         BLI_addtail(&sc->areabase, sa);
291         
292         return sa;
293 }
294
295 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
296 {
297         
298         ED_area_exit(C, sa);
299         
300         BKE_screen_area_free(sa);
301         
302         BLI_remlink(&sc->areabase, sa);
303         MEM_freeN(sa);
304 }
305
306 /* return 0: no split possible */
307 /* else return (integer) screencoordinate split point */
308 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
309 {
310         short x, y;
311         
312         // area big enough?
313         if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
314         if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
315         
316         // to be sure
317         if(fac<0.0) fac= 0.0;
318         if(fac>1.0) fac= 1.0;
319         
320         if(dir=='h') {
321                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
322                 
323                 if(y- sa->v1->vec.y < AREAMINY) 
324                         y= sa->v1->vec.y+ AREAMINY;
325                 else if(sa->v2->vec.y- y < AREAMINY) 
326                         y= sa->v2->vec.y- AREAMINY;
327                 else y-= (y % AREAGRID);
328                 
329                 return y;
330         }
331         else {
332                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
333                 
334                 if(x- sa->v1->vec.x < AREAMINX) 
335                         x= sa->v1->vec.x+ AREAMINX;
336                 else if(sa->v4->vec.x- x < AREAMINX) 
337                         x= sa->v4->vec.x- AREAMINX;
338                 else x-= (x % AREAGRID);
339                 
340                 return x;
341         }
342 }
343
344 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
345 {
346         ScrArea *newa=NULL;
347         ScrVert *sv1, *sv2;
348         short split;
349         
350         if(sa==NULL) return NULL;
351         
352         split= testsplitpoint(win, sa, dir, fac);
353         if(split==0) return NULL;
354         
355         if(dir=='h') {
356                 /* new vertices */
357                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
358                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
359                 
360                 /* new edges */
361                 screen_addedge(sc, sa->v1, sv1);
362                 screen_addedge(sc, sv1, sa->v2);
363                 screen_addedge(sc, sa->v3, sv2);
364                 screen_addedge(sc, sv2, sa->v4);
365                 screen_addedge(sc, sv1, sv2);
366                 
367                 /* new areas: top */
368                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
369                 area_copy_data(newa, sa, 0);
370                 
371                 /* area below */
372                 sa->v2= sv1;
373                 sa->v3= sv2;
374                 
375         }
376         else {
377                 /* new vertices */
378                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
379                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
380                 
381                 /* new edges */
382                 screen_addedge(sc, sa->v1, sv1);
383                 screen_addedge(sc, sv1, sa->v4);
384                 screen_addedge(sc, sa->v2, sv2);
385                 screen_addedge(sc, sv2, sa->v3);
386                 screen_addedge(sc, sv1, sv2);
387                 
388                 /* new areas: left */
389                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
390                 area_copy_data(newa, sa, 0);
391                 
392                 /* area right */
393                 sa->v1= sv1;
394                 sa->v2= sv2;
395         }
396         
397         /* remove double vertices en edges */
398         removedouble_scrverts(sc);
399         removedouble_scredges(sc);
400         removenotused_scredges(sc);
401         
402         return newa;
403 }
404
405 /* empty screen, with 1 dummy area without spacedata */
406 /* uses window size */
407 bScreen *screen_add(wmWindow *win, Scene *scene, char *name)
408 {
409         bScreen *sc;
410         ScrVert *sv1, *sv2, *sv3, *sv4;
411         
412         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
413         sc->scene= scene;
414         sc->do_refresh= 1;
415         sc->winid= win->winid;
416         
417         sv1= screen_addvert(sc, 0, 0);
418         sv2= screen_addvert(sc, 0, win->sizey-1);
419         sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
420         sv4= screen_addvert(sc, win->sizex-1, 0);
421         
422         screen_addedge(sc, sv1, sv2);
423         screen_addedge(sc, sv2, sv3);
424         screen_addedge(sc, sv3, sv4);
425         screen_addedge(sc, sv4, sv1);
426         
427         /* dummy type, no spacedata */
428         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
429                 
430         return sc;
431 }
432
433 static void screen_copy(bScreen *to, bScreen *from)
434 {
435         ScrVert *s1, *s2;
436         ScrEdge *se;
437         ScrArea *sa, *saf;
438         
439         /* free contents of 'to', is from blenkernel screen.c */
440         free_screen(to);
441         
442         BLI_duplicatelist(&to->vertbase, &from->vertbase);
443         BLI_duplicatelist(&to->edgebase, &from->edgebase);
444         BLI_duplicatelist(&to->areabase, &from->areabase);
445         to->regionbase.first= to->regionbase.last= NULL;
446         
447         s2= to->vertbase.first;
448         for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
449                 s1->newv= s2;
450         }
451         
452         for(se= to->edgebase.first; se; se= se->next) {
453                 se->v1= se->v1->newv;
454                 se->v2= se->v2->newv;
455                 sortscrvert(&(se->v1), &(se->v2));
456         }
457         
458         saf= from->areabase.first;
459         for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
460                 sa->v1= sa->v1->newv;
461                 sa->v2= sa->v2->newv;
462                 sa->v3= sa->v3->newv;
463                 sa->v4= sa->v4->newv;
464                 
465                 sa->spacedata.first= sa->spacedata.last= NULL;
466                 sa->regionbase.first= sa->regionbase.last= NULL;
467                 sa->actionzones.first= sa->actionzones.last= NULL;
468                 
469                 area_copy_data(sa, saf, 0);
470         }
471         
472         /* put at zero (needed?) */
473         for(s1= from->vertbase.first; s1; s1= s1->next)
474                 s1->newv= NULL;
475
476 }
477
478
479 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
480 /* -1 = not valid check */
481 /* used with join operator */
482 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
483 {
484         ScrVert *sav1, *sav2, *sav3, *sav4;
485         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
486
487         if(sa==NULL || sb==NULL) return -1;
488
489         sav1= sa->v1;
490         sav2= sa->v2;
491         sav3= sa->v3;
492         sav4= sa->v4;
493         sbv1= sb->v1;
494         sbv2= sb->v2;
495         sbv3= sb->v3;
496         sbv4= sb->v4;
497         
498         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
499                 return 0;
500         }
501         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
502                 return 1;
503         }
504         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
505                 return 2;
506         }
507         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
508                 return 3;
509         }
510         
511         return -1;
512 }
513
514 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
515 *       used by the split, join operators
516 */
517 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
518 {
519         int dir;
520         
521         dir = area_getorientation(scr, sa1, sa2);
522         /*printf("dir is : %i \n", dir);*/
523         
524         if (dir < 0)
525         {
526                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
527                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
528                 return 0;
529         }
530         
531         if(dir == 0) {
532                 sa1->v1= sa2->v1;
533                 sa1->v2= sa2->v2;
534                 screen_addedge(scr, sa1->v2, sa1->v3);
535                 screen_addedge(scr, sa1->v1, sa1->v4);
536         }
537         else if(dir == 1) {
538                 sa1->v2= sa2->v2;
539                 sa1->v3= sa2->v3;
540                 screen_addedge(scr, sa1->v1, sa1->v2);
541                 screen_addedge(scr, sa1->v3, sa1->v4);
542         }
543         else if(dir == 2) {
544                 sa1->v3= sa2->v3;
545                 sa1->v4= sa2->v4;
546                 screen_addedge(scr, sa1->v2, sa1->v3);
547                 screen_addedge(scr, sa1->v1, sa1->v4);
548         }
549         else if(dir == 3) {
550                 sa1->v1= sa2->v1;
551                 sa1->v4= sa2->v4;
552                 screen_addedge(scr, sa1->v1, sa1->v2);
553                 screen_addedge(scr, sa1->v3, sa1->v4);
554         }
555         
556         screen_delarea(C, scr, sa2);
557         removedouble_scrverts(scr);
558         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
559         
560         return 1;
561 }
562
563 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
564 {
565         ScrEdge *se;
566         ScrVert *sv;
567         int oneselected;
568         char dir;
569         
570         /* select connected, only in the right direction */
571         /* 'dir' is the direction of EDGE */
572         
573         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
574         else dir= 'h';
575         
576         sv= sc->vertbase.first;
577         while(sv) {
578                 sv->flag = 0;
579                 sv= sv->next;
580         }
581         
582         edge->v1->flag= 1;
583         edge->v2->flag= 1;
584         
585         oneselected= 1;
586         while(oneselected) {
587                 se= sc->edgebase.first;
588                 oneselected= 0;
589                 while(se) {
590                         if(se->v1->flag + se->v2->flag==1) {
591                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
592                                         se->v1->flag= se->v2->flag= 1;
593                                         oneselected= 1;
594                                 }
595                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
596                                                 se->v1->flag= se->v2->flag= 1;
597                                                 oneselected= 1;
598                                         }
599                         }
600                                 se= se->next;
601                 }
602         }
603 }
604
605 /* helper call for below, correct buttons view */
606 static void screen_test_scale_region(ListBase *regionbase, float facx, float facy)
607 {
608         ARegion *ar;
609         
610         for(ar= regionbase->first; ar; ar= ar->next) {
611                 if(ar->regiontype==RGN_TYPE_WINDOW) {
612                         ar->v2d.cur.xmin *= facx;
613                         ar->v2d.cur.xmax *= facx;
614                         ar->v2d.cur.ymin *= facy;
615                         ar->v2d.cur.ymax *= facy;
616                 }
617         }
618 }
619
620 /* test if screen vertices should be scaled */
621 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
622 {
623         ScrVert *sv=NULL;
624         ScrArea *sa;
625         int sizex, sizey;
626         float facx, facy, tempf, min[2], max[2];
627         
628         /* calculate size */
629         min[0]= min[1]= 10000.0f;
630         max[0]= max[1]= 0.0f;
631         
632         for(sv= sc->vertbase.first; sv; sv= sv->next) {
633                 min[0]= MIN2(min[0], sv->vec.x);
634                 min[1]= MIN2(min[1], sv->vec.y);
635                 max[0]= MAX2(max[0], sv->vec.x);
636                 max[1]= MAX2(max[1], sv->vec.y);
637         }
638         
639         /* always make 0.0 left under */
640         for(sv= sc->vertbase.first; sv; sv= sv->next) {
641                 sv->vec.x -= min[0];
642                 sv->vec.y -= min[1];
643         }
644         
645         sizex= max[0]-min[0];
646         sizey= max[1]-min[1];
647         
648         if(sizex!= winsizex || sizey!= winsizey) {
649                 facx= winsizex;
650                 facx/= (float)sizex;
651                 facy= winsizey;
652                 facy/= (float)sizey;
653                 
654                 /* make sure it fits! */
655                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
656                         tempf= ((float)sv->vec.x)*facx;
657                         sv->vec.x= (short)(tempf+0.5);
658                         sv->vec.x+= AREAGRID-1;
659                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
660                         
661                         CLAMP(sv->vec.x, 0, winsizex);
662                         
663                         tempf= ((float)sv->vec.y )*facy;
664                         sv->vec.y= (short)(tempf+0.5);
665                         sv->vec.y+= AREAGRID-1;
666                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
667                         
668                         CLAMP(sv->vec.y, 0, winsizey);
669                 }
670                 
671                 /* keep buttons view2d same size */
672                 for(sa= sc->areabase.first; sa; sa= sa->next) {
673                         SpaceLink *sl;
674                         
675                         if(sa->spacetype==SPACE_BUTS)
676                                 screen_test_scale_region(&sa->regionbase, facx, facy);
677                         
678                         for(sl= sa->spacedata.first; sl; sl= sl->next)
679                                 if(sl->spacetype==SPACE_BUTS)
680                                         screen_test_scale_region(&sl->regionbase, facx, facy);
681                 }
682                 
683         }
684         
685         /* test for collapsed areas. This could happen in some blender version... */
686         /* ton: removed option now, it needs Context... */
687         
688         /* make each window at least HEADERY high */
689         for(sa= sc->areabase.first; sa; sa= sa->next) {
690                 int headery= HEADERY+1;
691                 
692                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
693                         /* lower edge */
694                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
695                         if(se && sa->v1!=sa->v2 ) {
696                                 int yval;
697                                 
698                                 select_connected_scredge(sc, se);
699                                 
700                                 /* all selected vertices get the right offset */
701                                 yval= sa->v2->vec.y-headery;
702                                 sv= sc->vertbase.first;
703                                 while(sv) {
704                                         /* if is a collapsed area */
705                                         if(sv!=sa->v2 && sv!=sa->v3) {
706                                                 if(sv->flag) sv->vec.y= yval;
707                                         }
708                                         sv= sv->next;
709                                 }
710                         }
711                 }
712         }
713         
714 }
715
716 /* *********************** DRAWING **************************************** */
717
718
719 #define SCR_BACK 0.55
720 #define SCR_ROUND 12
721
722 /* draw vertical shape visualising future joining (left as well
723  * right direction of future joining) */
724 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
725 {
726         vec2f points[10];
727         short i;
728         float w, h;
729         float width = sa->v3->vec.x - sa->v1->vec.x;
730         float height = sa->v3->vec.y - sa->v1->vec.y;
731
732         if(height<width) {
733                 h = height/8;
734                 w = height/4;
735         }
736         else {
737                 h = width/8;
738                 w = width/4;
739         }
740
741         points[0].x = sa->v1->vec.x;
742         points[0].y = sa->v1->vec.y + height/2;
743
744         points[1].x = sa->v1->vec.x;
745         points[1].y = sa->v1->vec.y;
746
747         points[2].x = sa->v4->vec.x - w;
748         points[2].y = sa->v4->vec.y;
749
750         points[3].x = sa->v4->vec.x - w;
751         points[3].y = sa->v4->vec.y + height/2 - 2*h;
752
753         points[4].x = sa->v4->vec.x - 2*w;
754         points[4].y = sa->v4->vec.y + height/2;
755
756         points[5].x = sa->v4->vec.x - w;
757         points[5].y = sa->v4->vec.y + height/2 + 2*h;
758
759         points[6].x = sa->v3->vec.x - w;
760         points[6].y = sa->v3->vec.y;
761
762         points[7].x = sa->v2->vec.x;
763         points[7].y = sa->v2->vec.y;
764
765         points[8].x = sa->v4->vec.x;
766         points[8].y = sa->v4->vec.y + height/2 - h;
767
768         points[9].x = sa->v4->vec.x;
769         points[9].y = sa->v4->vec.y + height/2 + h;
770
771         if(dir=='l') {
772                 /* when direction is left, then we flip direction of arrow */
773                 float cx = sa->v1->vec.x + width;
774                 for(i=0;i<10;i++) {
775                         points[i].x -= cx;
776                         points[i].x = -points[i].x;
777                         points[i].x += sa->v1->vec.x;
778                 }
779         }
780
781         glBegin(GL_POLYGON);
782         for(i=0;i<5;i++)
783                 glVertex2f(points[i].x, points[i].y);
784         glEnd();
785         glBegin(GL_POLYGON);
786         for(i=4;i<8;i++)
787                 glVertex2f(points[i].x, points[i].y);
788         glVertex2f(points[0].x, points[0].y);
789         glEnd();
790
791         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
792         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
793 }
794
795 /* draw vertical shape visualising future joining (up/down direction) */
796 static void draw_vertical_join_shape(ScrArea *sa, char dir)
797 {
798         vec2f points[10];
799         short i;
800         float w, h;
801         float width = sa->v3->vec.x - sa->v1->vec.x;
802         float height = sa->v3->vec.y - sa->v1->vec.y;
803
804         if(height<width) {
805                 h = height/4;
806                 w = height/8;
807         }
808         else {
809                 h = width/4;
810                 w = width/8;
811         }
812
813         points[0].x = sa->v1->vec.x + width/2;
814         points[0].y = sa->v3->vec.y;
815
816         points[1].x = sa->v2->vec.x;
817         points[1].y = sa->v2->vec.y;
818
819         points[2].x = sa->v1->vec.x;
820         points[2].y = sa->v1->vec.y + h;
821
822         points[3].x = sa->v1->vec.x + width/2 - 2*w;
823         points[3].y = sa->v1->vec.y + h;
824
825         points[4].x = sa->v1->vec.x + width/2;
826         points[4].y = sa->v1->vec.y + 2*h;
827
828         points[5].x = sa->v1->vec.x + width/2 + 2*w;
829         points[5].y = sa->v1->vec.y + h;
830
831         points[6].x = sa->v4->vec.x;
832         points[6].y = sa->v4->vec.y + h;
833         
834         points[7].x = sa->v3->vec.x;
835         points[7].y = sa->v3->vec.y;
836
837         points[8].x = sa->v1->vec.x + width/2 - w;
838         points[8].y = sa->v1->vec.y;
839
840         points[9].x = sa->v1->vec.x + width/2 + w;
841         points[9].y = sa->v1->vec.y;
842
843         if(dir=='u') {
844                 /* when direction is up, then we flip direction of arrow */
845                 float cy = sa->v1->vec.y + height;
846                 for(i=0;i<10;i++) {
847                         points[i].y -= cy;
848                         points[i].y = -points[i].y;
849                         points[i].y += sa->v1->vec.y;
850                 }
851         }
852
853         glBegin(GL_POLYGON);
854         for(i=0;i<5;i++)
855                 glVertex2f(points[i].x, points[i].y);
856         glEnd();
857         glBegin(GL_POLYGON);
858         for(i=4;i<8;i++)
859                 glVertex2f(points[i].x, points[i].y);
860         glVertex2f(points[0].x, points[0].y);
861         glEnd();
862
863         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
864         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
865 }
866
867 /* draw join shape due to direction of joining */
868 static void draw_join_shape(ScrArea *sa, char dir)
869 {
870         if(dir=='u' || dir=='d')
871                 draw_vertical_join_shape(sa, dir);
872         else
873                 draw_horizontal_join_shape(sa, dir);
874 }
875
876 /* draw screen area darker with arrow (visualisation of future joining) */
877 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
878 {
879         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
880         glEnable(GL_BLEND);
881         glColor4ub(0, 0, 0, 50);
882         draw_join_shape(sa, dir);
883         glDisable(GL_BLEND);
884 }
885
886 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
887 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
888 {
889         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
890         glEnable(GL_BLEND);
891         /* value 181 was hardly computed: 181~105 */
892         glColor4ub(255, 255, 255, 50);          
893         /* draw_join_shape(sa, dir); */
894         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
895         glDisable(GL_BLEND);
896 }
897
898 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
899 {
900         /* right border area */
901         if(x2<sizex-1)
902                 sdrawline(x2+a, y1, x2+a, y2);
903         
904         /* left border area */
905         if(x1>0)  /* otherwise it draws the emboss of window over */
906                 sdrawline(x1+a, y1, x1+a, y2);
907         
908         /* top border area */
909         if(y2<sizey-1)
910                 sdrawline(x1, y2+a, x2, y2+a);
911         
912         /* bottom border area */
913         if(y1>0)
914                 sdrawline(x1, y1+a, x2, y1+a);
915         
916 }
917
918 /** screen edges drawing **/
919 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
920 {
921         short x1= sa->v1->vec.x;
922         short y1= sa->v1->vec.y;
923         short x2= sa->v3->vec.x;
924         short y2= sa->v3->vec.y;
925         short a, rt;
926         
927         rt= CLAMPIS(G.rt, 0, 16);
928         
929         if(center==0) {
930                 cpack(0x505050);
931                 for(a=-rt; a<=rt; a++)
932                         if(a!=0)
933                                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
934         }
935         else {
936                 cpack(0x0);
937                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
938         }
939 }
940
941 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
942
943 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
944 {
945         bScreen *newsc;
946         
947         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
948         
949         /* make new empty screen: */
950         newsc= screen_add(win, sc->scene, sc->id.name+2);
951         /* copy all data */
952         screen_copy(newsc, sc);
953         /* set in window */
954         win->screen= newsc;
955         
956         /* store identifier */
957         win->screen->winid= win->winid;
958         BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
959
960         return newsc;
961 }
962
963 /* screen sets cursor based on swinid */
964 static void region_cursor_set(wmWindow *win, int swinid)
965 {
966         ScrArea *sa= win->screen->areabase.first;
967         
968         for(;sa; sa= sa->next) {
969                 ARegion *ar= sa->regionbase.first;
970                 for(;ar; ar= ar->next) {
971                         if(ar->swinid == swinid) {
972                                 if(ar->type && ar->type->cursor)
973                                         ar->type->cursor(win, sa, ar);
974                                 else
975                                         WM_cursor_set(win, CURSOR_STD);
976                                 return;
977                         }
978                 }
979         }
980 }
981
982 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
983 {
984         
985         /* generic notes */
986         switch(note->category) {
987                 case NC_WM:
988                         if(note->data==ND_FILEREAD)
989                                 win->screen->do_draw= 1;
990                         break;
991                 case NC_WINDOW:
992                         win->screen->do_draw= 1;
993                         break;
994                 case NC_SCREEN:
995                         if(note->action==NA_EDITED)
996                                 win->screen->do_draw= win->screen->do_refresh= 1;
997                 case NC_SCENE:
998                         if(note->data==ND_MODE)
999                                 region_cursor_set(win, note->swinid);                           
1000                         break;
1001         }
1002 }
1003
1004 /* only for edge lines between areas, and the blended join arrows */
1005 void ED_screen_draw(wmWindow *win)
1006 {
1007         ScrArea *sa;
1008         ScrArea *sa1=NULL;
1009         ScrArea *sa2=NULL;
1010         int dir = -1;
1011         int dira = -1;
1012
1013         wmSubWindowSet(win, win->screen->mainwin);
1014         
1015         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1016                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1017                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1018                 drawscredge_area(sa, win->sizex, win->sizey, 0);
1019         }
1020         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1021                 drawscredge_area(sa, win->sizex, win->sizey, 1);
1022         
1023         /* blended join arrow */
1024         if (sa1 && sa2) {
1025                 dir = area_getorientation(win->screen, sa1, sa2);
1026                 if (dir >= 0) {
1027                         switch(dir) {
1028                                 case 0: /* W */
1029                                         dir = 'r';
1030                                         dira = 'l';
1031                                         break;
1032                                 case 1: /* N */
1033                                         dir = 'd';
1034                                         dira = 'u';
1035                                         break;
1036                                 case 2: /* E */
1037                                         dir = 'l';
1038                                         dira = 'r';
1039                                         break;
1040                                 case 3: /* S */
1041                                         dir = 'u';
1042                                         dira = 'd';
1043                                         break;
1044                         }
1045                 }
1046                 scrarea_draw_shape_dark(sa2, dir);
1047                 scrarea_draw_shape_light(sa1, dira);
1048         }
1049         
1050 //      if(G.f & G_DEBUG) printf("draw screen\n");
1051         win->screen->do_draw= 0;
1052 }
1053
1054 /* make this screen usable */
1055 /* for file read and first use, for scaling window, area moves */
1056 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1057 {
1058         ScrArea *sa;
1059         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
1060         
1061         screen_test_scale(win->screen, win->sizex, win->sizey);
1062         
1063         if(win->screen->mainwin==0)
1064                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1065         else
1066                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1067         
1068         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1069                 /* set spacetype and region callbacks, calls init() */
1070                 /* sets subwindows for regions, adds handlers */
1071                 ED_area_initialize(wm, win, sa);
1072         }
1073
1074         /* wake up animtimer */
1075         if(win->screen->animtimer)
1076                 WM_event_window_timer_sleep(win, win->screen->animtimer, 0);
1077         
1078         if(G.f & G_DEBUG) printf("set screen\n");
1079         win->screen->do_refresh= 0;
1080
1081         win->screen->context= ed_screen_context;
1082 }
1083
1084 /* file read, set all screens, ... */
1085 void ED_screens_initialize(wmWindowManager *wm)
1086 {
1087         wmWindow *win;
1088         
1089         for(win= wm->windows.first; win; win= win->next) {
1090                 
1091                 if(win->screen==NULL)
1092                         win->screen= G.main->screen.first;
1093                 
1094                 ED_screen_refresh(wm, win);
1095         }
1096 }
1097
1098
1099 /* *********** exit calls are for closing running stuff ******** */
1100
1101 void ED_region_exit(bContext *C, ARegion *ar)
1102 {
1103         ARegion *prevar= CTX_wm_region(C);
1104
1105         CTX_wm_region_set(C, ar);
1106         WM_event_remove_handlers(C, &ar->handlers);
1107         if(ar->swinid)
1108                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1109         ar->swinid= 0;
1110         
1111         if(ar->headerstr)
1112                 MEM_freeN(ar->headerstr);
1113         ar->headerstr= NULL;
1114         
1115         CTX_wm_region_set(C, prevar);
1116 }
1117
1118 void ED_area_exit(bContext *C, ScrArea *sa)
1119 {
1120         ScrArea *prevsa= CTX_wm_area(C);
1121         ARegion *ar;
1122
1123         CTX_wm_area_set(C, sa);
1124         for(ar= sa->regionbase.first; ar; ar= ar->next)
1125                 ED_region_exit(C, ar);
1126
1127         WM_event_remove_handlers(C, &sa->handlers);
1128         CTX_wm_area_set(C, prevsa);
1129 }
1130
1131 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1132 {
1133         wmWindow *prevwin= CTX_wm_window(C);
1134         ScrArea *sa;
1135         ARegion *ar;
1136
1137         CTX_wm_window_set(C, window);
1138         
1139         if(screen->animtimer)
1140                 WM_event_remove_window_timer(window, screen->animtimer);
1141         screen->animtimer= NULL;
1142         
1143         if(screen->mainwin)
1144                 wm_subwindow_close(window, screen->mainwin);
1145         screen->mainwin= 0;
1146         screen->subwinactive= 0;
1147         
1148         for(ar= screen->regionbase.first; ar; ar= ar->next)
1149                 ED_region_exit(C, ar);
1150
1151         for(sa= screen->areabase.first; sa; sa= sa->next)
1152                 ED_area_exit(C, sa);
1153
1154         /* mark it available for use for other windows */
1155         screen->winid= 0;
1156         
1157         CTX_wm_window_set(C, prevwin);
1158 }
1159
1160 /* *********************************** */
1161
1162 /* case when on area-edge or in azones, or outside window */
1163 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1164 {
1165         AZone *az= NULL;
1166         ScrArea *sa;
1167         
1168         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1169                 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1170                         break;
1171         
1172         if(sa) {
1173                 if(az->type==AZONE_AREA)
1174                         WM_cursor_set(win, CURSOR_EDIT);
1175                 else if(az->type==AZONE_REGION) {
1176                         if(az->x1==az->x2)
1177                                 WM_cursor_set(win, CURSOR_X_MOVE);
1178                         else
1179                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1180                 }
1181         }
1182         else {
1183                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1184                 
1185                 if (actedge) {
1186                         if(scredge_is_horizontal(actedge))
1187                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1188                         else
1189                                 WM_cursor_set(win, CURSOR_X_MOVE);
1190                 }
1191                 else
1192                         WM_cursor_set(win, CURSOR_STD);
1193         } 
1194 }
1195
1196
1197 /* called in wm_event_system.c. sets state vars in screen, cursors */
1198 /* event type is mouse move */
1199 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1200 {
1201         if(win->screen) {
1202                 bScreen *scr= win->screen;
1203                 ScrArea *sa;
1204                 ARegion *ar;
1205                 int oldswin= scr->subwinactive;
1206
1207                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1208                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1209                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1210                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1211                                                 break;
1212                 }
1213                 if(sa) {
1214                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1215                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1216                                         scr->subwinactive= ar->swinid;
1217                         }
1218                 }
1219                 else
1220                         scr->subwinactive= scr->mainwin;
1221                 
1222                 /* check for redraw headers */
1223                 if(oldswin!=scr->subwinactive) {
1224
1225                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1226                                 int do_draw= 0;
1227                                 
1228                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1229                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1230                                                 do_draw= 1;
1231                                 
1232                                 if(do_draw) {
1233                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1234                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1235                                                         ED_region_tag_redraw(ar);
1236                                 }
1237                         }
1238                 }
1239                 
1240                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1241                 if(scr->subwinactive==scr->mainwin) {
1242                         screen_cursor_set(win, event);
1243                 }
1244                 else if(oldswin!=scr->subwinactive) {
1245                         region_cursor_set(win, scr->subwinactive);
1246                 }
1247         }
1248 }
1249
1250 int ED_screen_area_active(const bContext *C)
1251 {
1252         bScreen *sc= CTX_wm_screen(C);
1253         ScrArea *sa= CTX_wm_area(C);
1254
1255         if(sc && sa) {
1256                 ARegion *ar;
1257                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1258                         if(ar->swinid == sc->subwinactive)
1259                                 return 1;
1260         }       
1261         return 0;
1262 }
1263
1264 /* operator call, WM + Window + screen already existed before */
1265 /* Do NOT call in area/region queues! */
1266 void ED_screen_set(bContext *C, bScreen *sc)
1267 {
1268         wmWindow *win= CTX_wm_window(C);
1269         bScreen *oldscreen= CTX_wm_screen(C);
1270         ID *id;
1271         
1272         /* validate screen, it's called with notifier reference */
1273         for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1274                 if(sc == (bScreen *)id)
1275                         break;
1276         if(id==NULL) 
1277                 return;
1278         
1279         /* check for valid winid */
1280         if(sc->winid!=0 && sc->winid!=win->winid)
1281                 return;
1282         
1283         if(sc->full) {                          /* find associated full */
1284                 bScreen *sc1;
1285                 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1286                         ScrArea *sa= sc1->areabase.first;
1287                         if(sa->full==sc) {
1288                                 sc= sc1;
1289                                 break;
1290                         }
1291                 }
1292         }
1293         
1294         if (oldscreen != sc) {
1295                 wmTimer *wt= oldscreen->animtimer;
1296                 
1297                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1298                 oldscreen->animtimer= NULL;
1299                 if(wt)
1300                         WM_event_window_timer_sleep(win, wt, 1);
1301                 
1302                 ED_screen_exit(C, win, oldscreen);
1303                 oldscreen->animtimer= wt;
1304                 
1305                 win->screen= sc;
1306                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1307                 
1308                 /* prevent multiwin errors */
1309                 sc->winid= win->winid;
1310                 
1311                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1312                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1313                 
1314                 /* makes button hilites work */
1315                 WM_event_add_mousemove(C);
1316         }
1317 }
1318
1319 /* only call outside of area/region loops */
1320 void ED_screen_set_scene(bContext *C, Scene *scene)
1321 {
1322         bScreen *sc;
1323         bScreen *curscreen= CTX_wm_screen(C);
1324         
1325         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1326                 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1327                         
1328                         if(scene != sc->scene) {
1329                                 /* all areas endlocalview */
1330                         // XXX  ScrArea *sa= sc->areabase.first;
1331                         //      while(sa) {
1332                         //              endlocalview(sa);
1333                         //              sa= sa->next;
1334                         //      }               
1335                                 sc->scene= scene;
1336                         }
1337                         
1338                 }
1339         }
1340         
1341         //  copy_view3d_lock(0);        /* space.c */
1342         
1343         /* are there cameras in the views that are not in the scene? */
1344         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1345                 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1346                         ScrArea *sa= sc->areabase.first;
1347                         while(sa) {
1348                                 SpaceLink *sl= sa->spacedata.first;
1349                                 while(sl) {
1350                                         if(sl->spacetype==SPACE_VIEW3D) {
1351                                                 View3D *v3d= (View3D*) sl;
1352                                                 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1353                                                         v3d->camera= scene_find_camera(sc->scene);
1354                                                         // XXX if (sc==curscreen) handle_view3d_lock();
1355                                                         if (!v3d->camera && v3d->persp==V3D_CAMOB) 
1356                                                                 v3d->persp= V3D_PERSP;
1357                                                 }
1358                                         }
1359                                         sl= sl->next;
1360                                 }
1361                                 sa= sa->next;
1362                         }
1363                 }
1364         }
1365         
1366         CTX_data_scene_set(C, scene);
1367         set_scene_bg(scene);
1368         
1369         ED_update_for_newframe(C, 1);
1370         
1371 //      set_radglobal();
1372         
1373         /* complete redraw */
1374         WM_event_add_notifier(C, NC_WINDOW, NULL);
1375         
1376 }
1377
1378 /* this function toggles: if area is full then the parent will be restored */
1379 void ed_screen_fullarea(bContext *C, ScrArea *sa)
1380 {
1381         bScreen *sc, *oldscreen;
1382         
1383         if(sa==NULL) {
1384                 return;
1385         }
1386         else if(sa->full) {
1387                 short fulltype;
1388                 
1389                 sc= sa->full;           /* the old screen to restore */
1390                 oldscreen= CTX_wm_screen(C);    /* the one disappearing */
1391                 
1392                 fulltype = sc->full;
1393                 
1394                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1395                    is set */
1396                 
1397                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1398                         ScrArea *old;
1399                         
1400                         sc->full= 0;
1401                         
1402                         /* find old area */
1403                         for(old= sc->areabase.first; old; old= old->next) 
1404                                 if(old->full) break;
1405                         if(old==NULL) {
1406                                 printf("something wrong in areafullscreen\n"); 
1407                                 return;
1408                         }
1409                             // old feature described below (ton)
1410                                 // in autoplay screens the headers are disabled by 
1411                                 // default. So use the old headertype instead
1412                         
1413                         area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
1414                         
1415                         old->full= NULL;
1416                         
1417                         /* animtimer back */
1418                         sc->animtimer= oldscreen->animtimer;
1419                         oldscreen->animtimer= NULL;
1420                         
1421                         ED_screen_set(C, sc);
1422                         
1423                         free_screen(oldscreen);
1424                         free_libblock(&CTX_data_main(C)->screen, oldscreen);
1425                 }
1426         }
1427         else {
1428                 ScrArea *newa;
1429                 
1430                 oldscreen= CTX_wm_screen(C);
1431
1432                 /* is there only 1 area? */
1433                 if(oldscreen->areabase.first==oldscreen->areabase.last) return;
1434                 
1435                 oldscreen->full = SCREENFULL;
1436                 
1437                 sc= screen_add(CTX_wm_window(C), CTX_data_scene(C), "temp");
1438                 sc->full = SCREENFULL; // XXX
1439                 
1440                 /* timer */
1441                 sc->animtimer= oldscreen->animtimer;
1442                 oldscreen->animtimer= NULL;
1443                 
1444                 /* returns the top small area */
1445                 newa= area_split(CTX_wm_window(C), sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1446                 ED_area_newspace(C, newa, SPACE_INFO);
1447
1448                 /* copy area */
1449                 newa= newa->prev;
1450                 area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
1451
1452                 sa->full= oldscreen;
1453                 newa->full= oldscreen;
1454                 newa->next->full= oldscreen; // XXX
1455
1456                 ED_screen_set(C, sc);
1457         }
1458
1459         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1460         CTX_wm_area_set(C, sc->areabase.first);
1461
1462         /* XXX retopo_force_update(); */
1463
1464 }
1465
1466 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1467 {
1468         if(sa==NULL)
1469                 return 0;
1470         
1471         if(sa->full==0)
1472                 ed_screen_fullarea(C, sa);
1473
1474         /* CTX_wm_area(C) is new area */
1475         ED_area_newspace(C, CTX_wm_area(C), type);
1476         
1477         return 1;
1478 }
1479
1480 void ED_screen_full_prevspace(bContext *C)
1481 {
1482         ScrArea *sa= CTX_wm_area(C);
1483         
1484         ED_area_prevspace(C);
1485         
1486         if(sa->full)
1487                 ed_screen_fullarea(C, sa);
1488 }
1489
1490 /* redraws: uses defines from stime->redraws 
1491  * enable: 1 - forward on, -1 - backwards on, 0 - off
1492  */
1493 void ED_screen_animation_timer(bContext *C, int redraws, int enable)
1494 {
1495         bScreen *screen= CTX_wm_screen(C);
1496         wmWindow *win= CTX_wm_window(C);
1497         Scene *scene= CTX_data_scene(C);
1498         
1499         if(screen->animtimer)
1500                 WM_event_remove_window_timer(win, screen->animtimer);
1501         screen->animtimer= NULL;
1502         
1503         if(enable) {
1504                 struct ScreenAnimData *sad= MEM_mallocN(sizeof(ScreenAnimData), "ScreenAnimData");
1505                 
1506                 screen->animtimer= WM_event_add_window_timer(win, TIMER0, (1.0/FPS));
1507                 sad->ar= CTX_wm_region(C);
1508                 sad->redraws= redraws;
1509                 sad->reverse= (enable < 0);
1510                 screen->animtimer->customdata= sad;
1511                 
1512         }
1513         /* notifier catched by top header, for button */
1514         WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1515 }
1516
1517 unsigned int ED_screen_view3d_layers(bScreen *screen)
1518 {
1519         if(screen) {
1520                 unsigned int layer= screen->scene->lay; /* as minimum this */
1521                 ScrArea *sa;
1522                 
1523                 /* get all used view3d layers */
1524                 for(sa= screen->areabase.first; sa; sa= sa->next) {
1525                         if(sa->spacetype==SPACE_VIEW3D)
1526                                 layer |= ((View3D *)sa->spacedata.first)->lay;
1527                 }
1528                 return layer;
1529         }
1530         return 0;
1531 }
1532
1533
1534 /* results in fully updated anim system */
1535 /* in future sound should be on WM level, only 1 sound can play! */
1536 void ED_update_for_newframe(const bContext *C, int mute)
1537 {
1538         bScreen *screen= CTX_wm_screen(C);
1539         Scene *scene= screen->scene;
1540         
1541         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1542         
1543         /* this function applies the changes too */
1544         /* XXX future: do all windows */
1545         scene_update_for_newframe(scene, ED_screen_view3d_layers(screen)); /* BKE_scene.h */
1546         
1547         //if ( (CFRA>1) && (!mute) && (scene->audio.flag & AUDIO_SCRUB)) 
1548         //      audiostream_scrub( CFRA );
1549         
1550         /* 3d window, preview */
1551         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1552         
1553         /* all movie/sequence images */
1554         //BIF_image_update_frame();
1555         
1556         /* composite */
1557         if(scene->use_nodes && scene->nodetree)
1558                 ntreeCompositTagAnimated(scene->nodetree);
1559         
1560         /* update animated texture nodes */
1561         {
1562                 Tex *tex;
1563                 for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
1564                         if( tex->use_nodes && tex->nodetree ) {
1565                                 ntreeTexTagAnimated( tex->nodetree );
1566                         }
1567         }
1568 }
1569
1570