2.5: Fix timeline space redraw properties, these were not RNA wrapped
[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 /* test if screen vertices should be scaled */
607 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
608 {
609         ScrVert *sv=NULL;
610         ScrArea *sa;
611         int sizex, sizey;
612         float facx, facy, tempf, min[2], max[2];
613         
614         /* calculate size */
615         min[0]= min[1]= 10000.0f;
616         max[0]= max[1]= 0.0f;
617         
618         for(sv= sc->vertbase.first; sv; sv= sv->next) {
619                 min[0]= MIN2(min[0], sv->vec.x);
620                 min[1]= MIN2(min[1], sv->vec.y);
621                 max[0]= MAX2(max[0], sv->vec.x);
622                 max[1]= MAX2(max[1], sv->vec.y);
623         }
624         
625         /* always make 0.0 left under */
626         for(sv= sc->vertbase.first; sv; sv= sv->next) {
627                 sv->vec.x -= min[0];
628                 sv->vec.y -= min[1];
629         }
630         
631         sizex= max[0]-min[0];
632         sizey= max[1]-min[1];
633         
634         if(sizex!= winsizex || sizey!= winsizey) {
635                 facx= winsizex;
636                 facx/= (float)sizex;
637                 facy= winsizey;
638                 facy/= (float)sizey;
639                 
640                 /* make sure it fits! */
641                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
642                         tempf= ((float)sv->vec.x)*facx;
643                         sv->vec.x= (short)(tempf+0.5);
644                         sv->vec.x+= AREAGRID-1;
645                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
646                         
647                         CLAMP(sv->vec.x, 0, winsizex);
648                         
649                         tempf= ((float)sv->vec.y )*facy;
650                         sv->vec.y= (short)(tempf+0.5);
651                         sv->vec.y+= AREAGRID-1;
652                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
653                         
654                         CLAMP(sv->vec.y, 0, winsizey);
655                 }
656         }
657         
658         /* test for collapsed areas. This could happen in some blender version... */
659         /* ton: removed option now, it needs Context... */
660         
661         /* make each window at least HEADERY high */
662         for(sa= sc->areabase.first; sa; sa= sa->next) {
663                 int headery= HEADERY+1;
664                 
665                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
666                         /* lower edge */
667                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
668                         if(se && sa->v1!=sa->v2 ) {
669                                 int yval;
670                                 
671                                 select_connected_scredge(sc, se);
672                                 
673                                 /* all selected vertices get the right offset */
674                                 yval= sa->v2->vec.y-headery;
675                                 sv= sc->vertbase.first;
676                                 while(sv) {
677                                         /* if is a collapsed area */
678                                         if(sv!=sa->v2 && sv!=sa->v3) {
679                                                 if(sv->flag) sv->vec.y= yval;
680                                         }
681                                         sv= sv->next;
682                                 }
683                         }
684                 }
685         }
686         
687 }
688
689 /* *********************** DRAWING **************************************** */
690
691
692 #define SCR_BACK 0.55
693 #define SCR_ROUND 12
694
695 /* draw vertical shape visualising future joining (left as well
696  * right direction of future joining) */
697 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
698 {
699         vec2f points[10];
700         short i;
701         float w, h;
702         float width = sa->v3->vec.x - sa->v1->vec.x;
703         float height = sa->v3->vec.y - sa->v1->vec.y;
704
705         if(height<width) {
706                 h = height/8;
707                 w = height/4;
708         }
709         else {
710                 h = width/8;
711                 w = width/4;
712         }
713
714         points[0].x = sa->v1->vec.x;
715         points[0].y = sa->v1->vec.y + height/2;
716
717         points[1].x = sa->v1->vec.x;
718         points[1].y = sa->v1->vec.y;
719
720         points[2].x = sa->v4->vec.x - w;
721         points[2].y = sa->v4->vec.y;
722
723         points[3].x = sa->v4->vec.x - w;
724         points[3].y = sa->v4->vec.y + height/2 - 2*h;
725
726         points[4].x = sa->v4->vec.x - 2*w;
727         points[4].y = sa->v4->vec.y + height/2;
728
729         points[5].x = sa->v4->vec.x - w;
730         points[5].y = sa->v4->vec.y + height/2 + 2*h;
731
732         points[6].x = sa->v3->vec.x - w;
733         points[6].y = sa->v3->vec.y;
734
735         points[7].x = sa->v2->vec.x;
736         points[7].y = sa->v2->vec.y;
737
738         points[8].x = sa->v4->vec.x;
739         points[8].y = sa->v4->vec.y + height/2 - h;
740
741         points[9].x = sa->v4->vec.x;
742         points[9].y = sa->v4->vec.y + height/2 + h;
743
744         if(dir=='l') {
745                 /* when direction is left, then we flip direction of arrow */
746                 float cx = sa->v1->vec.x + width;
747                 for(i=0;i<10;i++) {
748                         points[i].x -= cx;
749                         points[i].x = -points[i].x;
750                         points[i].x += sa->v1->vec.x;
751                 }
752         }
753
754         glBegin(GL_POLYGON);
755         for(i=0;i<5;i++)
756                 glVertex2f(points[i].x, points[i].y);
757         glEnd();
758         glBegin(GL_POLYGON);
759         for(i=4;i<8;i++)
760                 glVertex2f(points[i].x, points[i].y);
761         glVertex2f(points[0].x, points[0].y);
762         glEnd();
763
764         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
765         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
766 }
767
768 /* draw vertical shape visualising future joining (up/down direction) */
769 static void draw_vertical_join_shape(ScrArea *sa, char dir)
770 {
771         vec2f points[10];
772         short i;
773         float w, h;
774         float width = sa->v3->vec.x - sa->v1->vec.x;
775         float height = sa->v3->vec.y - sa->v1->vec.y;
776
777         if(height<width) {
778                 h = height/4;
779                 w = height/8;
780         }
781         else {
782                 h = width/4;
783                 w = width/8;
784         }
785
786         points[0].x = sa->v1->vec.x + width/2;
787         points[0].y = sa->v3->vec.y;
788
789         points[1].x = sa->v2->vec.x;
790         points[1].y = sa->v2->vec.y;
791
792         points[2].x = sa->v1->vec.x;
793         points[2].y = sa->v1->vec.y + h;
794
795         points[3].x = sa->v1->vec.x + width/2 - 2*w;
796         points[3].y = sa->v1->vec.y + h;
797
798         points[4].x = sa->v1->vec.x + width/2;
799         points[4].y = sa->v1->vec.y + 2*h;
800
801         points[5].x = sa->v1->vec.x + width/2 + 2*w;
802         points[5].y = sa->v1->vec.y + h;
803
804         points[6].x = sa->v4->vec.x;
805         points[6].y = sa->v4->vec.y + h;
806         
807         points[7].x = sa->v3->vec.x;
808         points[7].y = sa->v3->vec.y;
809
810         points[8].x = sa->v1->vec.x + width/2 - w;
811         points[8].y = sa->v1->vec.y;
812
813         points[9].x = sa->v1->vec.x + width/2 + w;
814         points[9].y = sa->v1->vec.y;
815
816         if(dir=='u') {
817                 /* when direction is up, then we flip direction of arrow */
818                 float cy = sa->v1->vec.y + height;
819                 for(i=0;i<10;i++) {
820                         points[i].y -= cy;
821                         points[i].y = -points[i].y;
822                         points[i].y += sa->v1->vec.y;
823                 }
824         }
825
826         glBegin(GL_POLYGON);
827         for(i=0;i<5;i++)
828                 glVertex2f(points[i].x, points[i].y);
829         glEnd();
830         glBegin(GL_POLYGON);
831         for(i=4;i<8;i++)
832                 glVertex2f(points[i].x, points[i].y);
833         glVertex2f(points[0].x, points[0].y);
834         glEnd();
835
836         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
837         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
838 }
839
840 /* draw join shape due to direction of joining */
841 static void draw_join_shape(ScrArea *sa, char dir)
842 {
843         if(dir=='u' || dir=='d')
844                 draw_vertical_join_shape(sa, dir);
845         else
846                 draw_horizontal_join_shape(sa, dir);
847 }
848
849 /* draw screen area darker with arrow (visualisation of future joining) */
850 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
851 {
852         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
853         glEnable(GL_BLEND);
854         glColor4ub(0, 0, 0, 50);
855         draw_join_shape(sa, dir);
856         glDisable(GL_BLEND);
857 }
858
859 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
860 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
861 {
862         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
863         glEnable(GL_BLEND);
864         /* value 181 was hardly computed: 181~105 */
865         glColor4ub(255, 255, 255, 50);          
866         /* draw_join_shape(sa, dir); */
867         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
868         glDisable(GL_BLEND);
869 }
870
871 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
872 {
873         /* right border area */
874         if(x2<sizex-1)
875                 sdrawline(x2+a, y1, x2+a, y2);
876         
877         /* left border area */
878         if(x1>0)  /* otherwise it draws the emboss of window over */
879                 sdrawline(x1+a, y1, x1+a, y2);
880         
881         /* top border area */
882         if(y2<sizey-1)
883                 sdrawline(x1, y2+a, x2, y2+a);
884         
885         /* bottom border area */
886         if(y1>0)
887                 sdrawline(x1, y1+a, x2, y1+a);
888         
889 }
890
891 /** screen edges drawing **/
892 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
893 {
894         short x1= sa->v1->vec.x;
895         short y1= sa->v1->vec.y;
896         short x2= sa->v3->vec.x;
897         short y2= sa->v3->vec.y;
898         short a, rt;
899         
900         rt= CLAMPIS(G.rt, 0, 16);
901         
902         if(center==0) {
903                 cpack(0x505050);
904                 for(a=-rt; a<=rt; a++)
905                         if(a!=0)
906                                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
907         }
908         else {
909                 cpack(0x0);
910                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
911         }
912 }
913
914 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
915
916 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
917 {
918         bScreen *newsc;
919         
920         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
921         
922         /* make new empty screen: */
923         newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
924         /* copy all data */
925         screen_copy(newsc, sc);
926
927         return newsc;
928 }
929
930 /* screen sets cursor based on swinid */
931 static void region_cursor_set(wmWindow *win, int swinid)
932 {
933         ScrArea *sa= win->screen->areabase.first;
934         
935         for(;sa; sa= sa->next) {
936                 ARegion *ar= sa->regionbase.first;
937                 for(;ar; ar= ar->next) {
938                         if(ar->swinid == swinid) {
939                                 if(ar->type && ar->type->cursor)
940                                         ar->type->cursor(win, sa, ar);
941                                 else
942                                         WM_cursor_set(win, CURSOR_STD);
943                                 return;
944                         }
945                 }
946         }
947 }
948
949 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
950 {
951         
952         /* generic notes */
953         switch(note->category) {
954                 case NC_WM:
955                         if(note->data==ND_FILEREAD)
956                                 win->screen->do_draw= 1;
957                         break;
958                 case NC_WINDOW:
959                         win->screen->do_draw= 1;
960                         break;
961                 case NC_SCREEN:
962                         if(note->action==NA_EDITED)
963                                 win->screen->do_draw= win->screen->do_refresh= 1;
964                 case NC_SCENE:
965                         if(note->data==ND_MODE)
966                                 region_cursor_set(win, note->swinid);                           
967                         break;
968         }
969 }
970
971 /* only for edge lines between areas, and the blended join arrows */
972 void ED_screen_draw(wmWindow *win)
973 {
974         ScrArea *sa;
975         ScrArea *sa1=NULL;
976         ScrArea *sa2=NULL;
977         int dir = -1;
978         int dira = -1;
979
980         wmSubWindowSet(win, win->screen->mainwin);
981         
982         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
983                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
984                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
985                 drawscredge_area(sa, win->sizex, win->sizey, 0);
986         }
987         for(sa= win->screen->areabase.first; sa; sa= sa->next)
988                 drawscredge_area(sa, win->sizex, win->sizey, 1);
989         
990         /* blended join arrow */
991         if (sa1 && sa2) {
992                 dir = area_getorientation(win->screen, sa1, sa2);
993                 if (dir >= 0) {
994                         switch(dir) {
995                                 case 0: /* W */
996                                         dir = 'r';
997                                         dira = 'l';
998                                         break;
999                                 case 1: /* N */
1000                                         dir = 'd';
1001                                         dira = 'u';
1002                                         break;
1003                                 case 2: /* E */
1004                                         dir = 'l';
1005                                         dira = 'r';
1006                                         break;
1007                                 case 3: /* S */
1008                                         dir = 'u';
1009                                         dira = 'd';
1010                                         break;
1011                         }
1012                 }
1013                 scrarea_draw_shape_dark(sa2, dir);
1014                 scrarea_draw_shape_light(sa1, dira);
1015         }
1016         
1017 //      if(G.f & G_DEBUG) printf("draw screen\n");
1018         win->screen->do_draw= 0;
1019 }
1020
1021 /* make this screen usable */
1022 /* for file read and first use, for scaling window, area moves */
1023 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1024 {
1025         ScrArea *sa;
1026         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
1027         
1028         screen_test_scale(win->screen, win->sizex, win->sizey);
1029         
1030         if(win->screen->mainwin==0)
1031                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1032         else
1033                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1034         
1035         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1036                 /* set spacetype and region callbacks, calls init() */
1037                 /* sets subwindows for regions, adds handlers */
1038                 ED_area_initialize(wm, win, sa);
1039         }
1040
1041         /* wake up animtimer */
1042         if(win->screen->animtimer)
1043                 WM_event_window_timer_sleep(win, win->screen->animtimer, 0);
1044         
1045         if(G.f & G_DEBUG) printf("set screen\n");
1046         win->screen->do_refresh= 0;
1047
1048         win->screen->context= ed_screen_context;
1049 }
1050
1051 /* file read, set all screens, ... */
1052 void ED_screens_initialize(wmWindowManager *wm)
1053 {
1054         wmWindow *win;
1055         
1056         for(win= wm->windows.first; win; win= win->next) {
1057                 
1058                 if(win->screen==NULL)
1059                         win->screen= G.main->screen.first;
1060                 
1061                 ED_screen_refresh(wm, win);
1062         }
1063 }
1064
1065
1066 /* *********** exit calls are for closing running stuff ******** */
1067
1068 void ED_region_exit(bContext *C, ARegion *ar)
1069 {
1070         ARegion *prevar= CTX_wm_region(C);
1071
1072         CTX_wm_region_set(C, ar);
1073         WM_event_remove_handlers(C, &ar->handlers);
1074         if(ar->swinid)
1075                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1076         ar->swinid= 0;
1077         
1078         if(ar->headerstr)
1079                 MEM_freeN(ar->headerstr);
1080         ar->headerstr= NULL;
1081         
1082         CTX_wm_region_set(C, prevar);
1083 }
1084
1085 void ED_area_exit(bContext *C, ScrArea *sa)
1086 {
1087         ScrArea *prevsa= CTX_wm_area(C);
1088         ARegion *ar;
1089
1090         CTX_wm_area_set(C, sa);
1091         for(ar= sa->regionbase.first; ar; ar= ar->next)
1092                 ED_region_exit(C, ar);
1093
1094         WM_event_remove_handlers(C, &sa->handlers);
1095         CTX_wm_area_set(C, prevsa);
1096 }
1097
1098 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1099 {
1100         wmWindow *prevwin= CTX_wm_window(C);
1101         ScrArea *sa;
1102         ARegion *ar;
1103
1104         CTX_wm_window_set(C, window);
1105         
1106         if(screen->animtimer)
1107                 WM_event_remove_window_timer(window, screen->animtimer);
1108         screen->animtimer= NULL;
1109         
1110         if(screen->mainwin)
1111                 wm_subwindow_close(window, screen->mainwin);
1112         screen->mainwin= 0;
1113         screen->subwinactive= 0;
1114         
1115         for(ar= screen->regionbase.first; ar; ar= ar->next)
1116                 ED_region_exit(C, ar);
1117
1118         for(sa= screen->areabase.first; sa; sa= sa->next)
1119                 ED_area_exit(C, sa);
1120
1121         /* mark it available for use for other windows */
1122         screen->winid= 0;
1123         
1124         CTX_wm_window_set(C, prevwin);
1125 }
1126
1127 /* *********************************** */
1128
1129 /* case when on area-edge or in azones, or outside window */
1130 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1131 {
1132         AZone *az= NULL;
1133         ScrArea *sa;
1134         
1135         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1136                 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1137                         break;
1138         
1139         if(sa) {
1140                 if(az->type==AZONE_AREA)
1141                         WM_cursor_set(win, CURSOR_EDIT);
1142                 else if(az->type==AZONE_REGION) {
1143                         if(az->x1==az->x2)
1144                                 WM_cursor_set(win, CURSOR_X_MOVE);
1145                         else
1146                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1147                 }
1148         }
1149         else {
1150                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1151                 
1152                 if (actedge) {
1153                         if(scredge_is_horizontal(actedge))
1154                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1155                         else
1156                                 WM_cursor_set(win, CURSOR_X_MOVE);
1157                 }
1158                 else
1159                         WM_cursor_set(win, CURSOR_STD);
1160         } 
1161 }
1162
1163
1164 /* called in wm_event_system.c. sets state vars in screen, cursors */
1165 /* event type is mouse move */
1166 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1167 {
1168         if(win->screen) {
1169                 bScreen *scr= win->screen;
1170                 ScrArea *sa;
1171                 ARegion *ar;
1172                 int oldswin= scr->subwinactive;
1173
1174                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1175                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1176                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1177                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1178                                                 break;
1179                 }
1180                 if(sa) {
1181                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1182                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1183                                         scr->subwinactive= ar->swinid;
1184                         }
1185                 }
1186                 else
1187                         scr->subwinactive= scr->mainwin;
1188                 
1189                 /* check for redraw headers */
1190                 if(oldswin!=scr->subwinactive) {
1191
1192                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1193                                 int do_draw= 0;
1194                                 
1195                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1196                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1197                                                 do_draw= 1;
1198                                 
1199                                 if(do_draw) {
1200                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1201                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1202                                                         ED_region_tag_redraw(ar);
1203                                 }
1204                         }
1205                 }
1206                 
1207                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1208                 if(scr->subwinactive==scr->mainwin) {
1209                         screen_cursor_set(win, event);
1210                 }
1211                 else if(oldswin!=scr->subwinactive) {
1212                         region_cursor_set(win, scr->subwinactive);
1213                 }
1214         }
1215 }
1216
1217 int ED_screen_area_active(const bContext *C)
1218 {
1219         bScreen *sc= CTX_wm_screen(C);
1220         ScrArea *sa= CTX_wm_area(C);
1221
1222         if(sc && sa) {
1223                 ARegion *ar;
1224                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1225                         if(ar->swinid == sc->subwinactive)
1226                                 return 1;
1227         }       
1228         return 0;
1229 }
1230
1231 /* operator call, WM + Window + screen already existed before */
1232 /* Do NOT call in area/region queues! */
1233 void ED_screen_set(bContext *C, bScreen *sc)
1234 {
1235         wmWindow *win= CTX_wm_window(C);
1236         bScreen *oldscreen= CTX_wm_screen(C);
1237         ID *id;
1238         
1239         /* validate screen, it's called with notifier reference */
1240         for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1241                 if(sc == (bScreen *)id)
1242                         break;
1243         if(id==NULL) 
1244                 return;
1245         
1246         /* check for valid winid */
1247         if(sc->winid!=0 && sc->winid!=win->winid)
1248                 return;
1249         
1250         if(sc->full) {                          /* find associated full */
1251                 bScreen *sc1;
1252                 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1253                         ScrArea *sa= sc1->areabase.first;
1254                         if(sa->full==sc) {
1255                                 sc= sc1;
1256                                 break;
1257                         }
1258                 }
1259         }
1260         
1261         if (oldscreen != sc) {
1262                 wmTimer *wt= oldscreen->animtimer;
1263                 
1264                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1265                 oldscreen->animtimer= NULL;
1266                 if(wt)
1267                         WM_event_window_timer_sleep(win, wt, 1);
1268                 
1269                 ED_screen_exit(C, win, oldscreen);
1270                 oldscreen->animtimer= wt;
1271                 
1272                 win->screen= sc;
1273                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1274                 
1275                 /* prevent multiwin errors */
1276                 sc->winid= win->winid;
1277                 
1278                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1279                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1280                 
1281                 /* makes button hilites work */
1282                 WM_event_add_mousemove(C);
1283         }
1284 }
1285
1286 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1287 {
1288         wmWindow *win;
1289
1290         for(win=wm->windows.first; win; win=win->next)
1291                 if(win->screen == sc)
1292                         return 1;
1293         
1294         return 0;
1295 }
1296
1297 /* only call outside of area/region loops */
1298 void ED_screen_delete(bContext *C, bScreen *sc)
1299 {
1300         Main *bmain= CTX_data_main(C);
1301         wmWindowManager *wm= CTX_wm_manager(C);
1302         wmWindow *win= CTX_wm_window(C);
1303         bScreen *newsc;
1304         int delete= 1;
1305
1306         /* screen can only be in use by one window at a time, so as
1307            long as we are able to find a screen that is unused, we
1308            can safely assume ours is not in use anywhere an delete it */
1309
1310         for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1311                 if(!ed_screen_used(wm, newsc))
1312                         break;
1313         
1314         if(!newsc) {
1315                 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1316                         if(!ed_screen_used(wm, newsc))
1317                                 break;
1318         }
1319
1320         if(!newsc)
1321                 return;
1322
1323         ED_screen_set(C, newsc);
1324
1325         if(delete && win->screen != sc)
1326                 free_libblock(&bmain->screen, sc);
1327 }
1328
1329 /* only call outside of area/region loops */
1330 void ED_screen_set_scene(bContext *C, Scene *scene)
1331 {
1332         bScreen *sc;
1333         bScreen *curscreen= CTX_wm_screen(C);
1334         
1335         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1336                 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1337                         
1338                         if(scene != sc->scene) {
1339                                 /* all areas endlocalview */
1340                         // XXX  ScrArea *sa= sc->areabase.first;
1341                         //      while(sa) {
1342                         //              endlocalview(sa);
1343                         //              sa= sa->next;
1344                         //      }               
1345                                 sc->scene= scene;
1346                         }
1347                         
1348                 }
1349         }
1350         
1351         //  copy_view3d_lock(0);        /* space.c */
1352         
1353         /* are there cameras in the views that are not in the scene? */
1354         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1355                 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1356                         ScrArea *sa= sc->areabase.first;
1357                         while(sa) {
1358                                 SpaceLink *sl= sa->spacedata.first;
1359                                 while(sl) {
1360                                         if(sl->spacetype==SPACE_VIEW3D) {
1361                                                 View3D *v3d= (View3D*) sl;
1362                                                 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1363                                                         v3d->camera= scene_find_camera(sc->scene);
1364                                                         // XXX if (sc==curscreen) handle_view3d_lock();
1365                                                         if (!v3d->camera && v3d->persp==V3D_CAMOB) 
1366                                                                 v3d->persp= V3D_PERSP;
1367                                                 }
1368                                         }
1369                                         sl= sl->next;
1370                                 }
1371                                 sa= sa->next;
1372                         }
1373                 }
1374         }
1375         
1376         CTX_data_scene_set(C, scene);
1377         set_scene_bg(scene);
1378         
1379         ED_update_for_newframe(C, 1);
1380         
1381         /* complete redraw */
1382         WM_event_add_notifier(C, NC_WINDOW, NULL);
1383         
1384 }
1385
1386 /* only call outside of area/region loops */
1387 void ED_screen_delete_scene(bContext *C, Scene *scene)
1388 {
1389         Main *bmain= CTX_data_main(C);
1390         Scene *newscene;
1391
1392         if(scene->id.prev)
1393                 newscene= scene->id.prev;
1394         else if(scene->id.next)
1395                 newscene= scene->id.next;
1396         else
1397                 return;
1398
1399         ED_screen_set_scene(C, newscene);
1400
1401         unlink_scene(bmain, scene, newscene);
1402 }
1403
1404 /* this function toggles: if area is full then the parent will be restored */
1405 void ed_screen_fullarea(bContext *C, ScrArea *sa)
1406 {
1407         bScreen *sc, *oldscreen;
1408         
1409         if(sa==NULL) {
1410                 return;
1411         }
1412         else if(sa->full) {
1413                 short fulltype;
1414                 
1415                 sc= sa->full;           /* the old screen to restore */
1416                 oldscreen= CTX_wm_screen(C);    /* the one disappearing */
1417                 
1418                 fulltype = sc->full;
1419                 
1420                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1421                    is set */
1422                 
1423                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1424                         ScrArea *old;
1425                         
1426                         sc->full= 0;
1427                         
1428                         /* find old area */
1429                         for(old= sc->areabase.first; old; old= old->next) 
1430                                 if(old->full) break;
1431                         if(old==NULL) {
1432                                 printf("something wrong in areafullscreen\n"); 
1433                                 return;
1434                         }
1435                             // old feature described below (ton)
1436                                 // in autoplay screens the headers are disabled by 
1437                                 // default. So use the old headertype instead
1438                         
1439                         area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
1440                         
1441                         old->full= NULL;
1442                         
1443                         /* animtimer back */
1444                         sc->animtimer= oldscreen->animtimer;
1445                         oldscreen->animtimer= NULL;
1446                         
1447                         ED_screen_set(C, sc);
1448                         
1449                         free_screen(oldscreen);
1450                         free_libblock(&CTX_data_main(C)->screen, oldscreen);
1451                 }
1452         }
1453         else {
1454                 ScrArea *newa;
1455                 
1456                 oldscreen= CTX_wm_screen(C);
1457
1458                 /* is there only 1 area? */
1459                 if(oldscreen->areabase.first==oldscreen->areabase.last) return;
1460                 
1461                 oldscreen->full = SCREENFULL;
1462                 
1463                 sc= ED_screen_add(CTX_wm_window(C), CTX_data_scene(C), "temp");
1464                 sc->full = SCREENFULL; // XXX
1465                 
1466                 /* timer */
1467                 sc->animtimer= oldscreen->animtimer;
1468                 oldscreen->animtimer= NULL;
1469                 
1470                 /* returns the top small area */
1471                 newa= area_split(CTX_wm_window(C), sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1472                 ED_area_newspace(C, newa, SPACE_INFO);
1473
1474                 /* copy area */
1475                 newa= newa->prev;
1476                 area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
1477
1478                 sa->full= oldscreen;
1479                 newa->full= oldscreen;
1480                 newa->next->full= oldscreen; // XXX
1481
1482                 ED_screen_set(C, sc);
1483         }
1484
1485         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1486         CTX_wm_area_set(C, sc->areabase.first);
1487
1488         /* XXX retopo_force_update(); */
1489
1490 }
1491
1492 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1493 {
1494         if(sa==NULL)
1495                 return 0;
1496         
1497         if(sa->full==0)
1498                 ed_screen_fullarea(C, sa);
1499
1500         /* CTX_wm_area(C) is new area */
1501         ED_area_newspace(C, CTX_wm_area(C), type);
1502         
1503         return 1;
1504 }
1505
1506 void ED_screen_full_prevspace(bContext *C)
1507 {
1508         ScrArea *sa= CTX_wm_area(C);
1509         
1510         ED_area_prevspace(C);
1511         
1512         if(sa->full)
1513                 ed_screen_fullarea(C, sa);
1514 }
1515
1516 /* redraws: uses defines from stime->redraws 
1517  * enable: 1 - forward on, -1 - backwards on, 0 - off
1518  */
1519 void ED_screen_animation_timer(bContext *C, int redraws, int sync, int enable)
1520 {
1521         bScreen *screen= CTX_wm_screen(C);
1522         wmWindow *win= CTX_wm_window(C);
1523         Scene *scene= CTX_data_scene(C);
1524         
1525         if(screen->animtimer)
1526                 WM_event_remove_window_timer(win, screen->animtimer);
1527         screen->animtimer= NULL;
1528         
1529         if(enable) {
1530                 struct ScreenAnimData *sad= MEM_mallocN(sizeof(ScreenAnimData), "ScreenAnimData");
1531                 
1532                 screen->animtimer= WM_event_add_window_timer(win, TIMER0, (1.0/FPS));
1533                 sad->ar= CTX_wm_region(C);
1534                 sad->redraws= redraws;
1535                 sad->flag= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1536                 sad->flag= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1537                 screen->animtimer->customdata= sad;
1538                 
1539         }
1540         /* notifier catched by top header, for button */
1541         WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1542 }
1543
1544 /* helper for screen_animation_play() - only to be used for TimeLine */
1545 static ARegion *time_top_left_3dwindow(bScreen *screen)
1546 {
1547         ARegion *aret= NULL;
1548         ScrArea *sa;
1549         int min= 10000;
1550         
1551         for(sa= screen->areabase.first; sa; sa= sa->next) {
1552                 if(sa->spacetype==SPACE_VIEW3D) {
1553                         ARegion *ar;
1554                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1555                                 if(ar->regiontype==RGN_TYPE_WINDOW) {
1556                                         if(ar->winrct.xmin - ar->winrct.ymin < min) {
1557                                                 aret= ar;
1558                                                 min= ar->winrct.xmin - ar->winrct.ymin;
1559                                         }
1560                                 }
1561                         }
1562                 }
1563         }
1564
1565         return aret;
1566 }
1567
1568 void ED_screen_animation_timer_update(bContext *C, int redraws)
1569 {
1570         bScreen *screen= CTX_wm_screen(C);
1571         
1572         if(screen && screen->animtimer) {
1573                 wmTimer *wt= screen->animtimer;
1574                 ScreenAnimData *sad= wt->customdata;
1575                 
1576                 sad->redraws= redraws;
1577                 sad->ar= NULL;
1578                 if(redraws & TIME_REGION)
1579                         sad->ar= time_top_left_3dwindow(screen);
1580         }
1581 }
1582
1583 unsigned int ED_screen_view3d_layers(bScreen *screen)
1584 {
1585         if(screen) {
1586                 unsigned int layer= screen->scene->lay; /* as minimum this */
1587                 ScrArea *sa;
1588                 
1589                 /* get all used view3d layers */
1590                 for(sa= screen->areabase.first; sa; sa= sa->next) {
1591                         if(sa->spacetype==SPACE_VIEW3D)
1592                                 layer |= ((View3D *)sa->spacedata.first)->lay;
1593                 }
1594                 return layer;
1595         }
1596         return 0;
1597 }
1598
1599
1600 /* results in fully updated anim system */
1601 void ED_update_for_newframe(const bContext *C, int mute)
1602 {
1603         bScreen *screen= CTX_wm_screen(C);
1604         Scene *scene= screen->scene;
1605         
1606         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1607         
1608         /* this function applies the changes too */
1609         /* XXX future: do all windows */
1610         scene_update_for_newframe(scene, ED_screen_view3d_layers(screen)); /* BKE_scene.h */
1611         
1612         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1613         //      audiostream_scrub( CFRA );
1614         
1615         /* 3d window, preview */
1616         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1617         
1618         /* all movie/sequence images */
1619         //BIF_image_update_frame();
1620         
1621         /* composite */
1622         if(scene->use_nodes && scene->nodetree)
1623                 ntreeCompositTagAnimated(scene->nodetree);
1624         
1625         /* update animated texture nodes */
1626         {
1627                 Tex *tex;
1628                 for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
1629                         if( tex->use_nodes && tex->nodetree ) {
1630                                 ntreeTexTagAnimated( tex->nodetree );
1631                         }
1632         }
1633 }
1634
1635