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