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