1ca8bff319314d39106a0510d3adde280159d578
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <math.h>
29
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_scene_types.h"
34 #include "DNA_userdef_types.h"
35
36 #include "BLI_blenlib.h"
37
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_library.h"
41 #include "BKE_main.h"
42 #include "BKE_node.h"
43 #include "BKE_screen.h"
44 #include "BKE_scene.h"
45
46 #include "BIF_gl.h"
47 #include "BIF_glutil.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "ED_image.h"
53 #include "ED_object.h"
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                 sa->handlers.first= sa->handlers.last= NULL;
472                 
473                 area_copy_data(sa, saf, 0);
474         }
475         
476         /* put at zero (needed?) */
477         for(s1= from->vertbase.first; s1; s1= s1->next)
478                 s1->newv= NULL;
479
480 }
481
482
483 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
484 /* -1 = not valid check */
485 /* used with join operator */
486 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
487 {
488         ScrVert *sav1, *sav2, *sav3, *sav4;
489         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
490
491         if(sa==NULL || sb==NULL) return -1;
492
493         sav1= sa->v1;
494         sav2= sa->v2;
495         sav3= sa->v3;
496         sav4= sa->v4;
497         sbv1= sb->v1;
498         sbv2= sb->v2;
499         sbv3= sb->v3;
500         sbv4= sb->v4;
501         
502         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
503                 return 0;
504         }
505         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
506                 return 1;
507         }
508         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
509                 return 2;
510         }
511         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
512                 return 3;
513         }
514         
515         return -1;
516 }
517
518 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
519 *       used by the split, join operators
520 */
521 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
522 {
523         int dir;
524         
525         dir = area_getorientation(scr, sa1, sa2);
526         /*printf("dir is : %i \n", dir);*/
527         
528         if (dir < 0)
529         {
530                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
531                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
532                 return 0;
533         }
534         
535         if(dir == 0) {
536                 sa1->v1= sa2->v1;
537                 sa1->v2= sa2->v2;
538                 screen_addedge(scr, sa1->v2, sa1->v3);
539                 screen_addedge(scr, sa1->v1, sa1->v4);
540         }
541         else if(dir == 1) {
542                 sa1->v2= sa2->v2;
543                 sa1->v3= sa2->v3;
544                 screen_addedge(scr, sa1->v1, sa1->v2);
545                 screen_addedge(scr, sa1->v3, sa1->v4);
546         }
547         else if(dir == 2) {
548                 sa1->v3= sa2->v3;
549                 sa1->v4= sa2->v4;
550                 screen_addedge(scr, sa1->v2, sa1->v3);
551                 screen_addedge(scr, sa1->v1, sa1->v4);
552         }
553         else if(dir == 3) {
554                 sa1->v1= sa2->v1;
555                 sa1->v4= sa2->v4;
556                 screen_addedge(scr, sa1->v1, sa1->v2);
557                 screen_addedge(scr, sa1->v3, sa1->v4);
558         }
559         
560         screen_delarea(C, scr, sa2);
561         removedouble_scrverts(scr);
562         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
563         
564         return 1;
565 }
566
567 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
568 {
569         ScrEdge *se;
570         ScrVert *sv;
571         int oneselected;
572         char dir;
573         
574         /* select connected, only in the right direction */
575         /* 'dir' is the direction of EDGE */
576         
577         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
578         else dir= 'h';
579         
580         sv= sc->vertbase.first;
581         while(sv) {
582                 sv->flag = 0;
583                 sv= sv->next;
584         }
585         
586         edge->v1->flag= 1;
587         edge->v2->flag= 1;
588         
589         oneselected= 1;
590         while(oneselected) {
591                 se= sc->edgebase.first;
592                 oneselected= 0;
593                 while(se) {
594                         if(se->v1->flag + se->v2->flag==1) {
595                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
596                                         se->v1->flag= se->v2->flag= 1;
597                                         oneselected= 1;
598                                 }
599                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
600                                                 se->v1->flag= se->v2->flag= 1;
601                                                 oneselected= 1;
602                                         }
603                         }
604                                 se= se->next;
605                 }
606         }
607 }
608
609 /* test if screen vertices should be scaled */
610 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
611 {
612         ScrVert *sv=NULL;
613         ScrArea *sa;
614         int sizex, sizey;
615         float facx, facy, tempf, min[2], max[2];
616         
617         /* calculate size */
618         min[0]= min[1]= 10000.0f;
619         max[0]= max[1]= 0.0f;
620         
621         for(sv= sc->vertbase.first; sv; sv= sv->next) {
622                 min[0]= MIN2(min[0], sv->vec.x);
623                 min[1]= MIN2(min[1], sv->vec.y);
624                 max[0]= MAX2(max[0], sv->vec.x);
625                 max[1]= MAX2(max[1], sv->vec.y);
626         }
627         
628         /* always make 0.0 left under */
629         for(sv= sc->vertbase.first; sv; sv= sv->next) {
630                 sv->vec.x -= min[0];
631                 sv->vec.y -= min[1];
632         }
633         
634         sizex= max[0]-min[0];
635         sizey= max[1]-min[1];
636         
637         if(sizex!= winsizex || sizey!= winsizey) {
638                 facx= winsizex;
639                 facx/= (float)sizex;
640                 facy= winsizey;
641                 facy/= (float)sizey;
642                 
643                 /* make sure it fits! */
644                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
645                         tempf= ((float)sv->vec.x)*facx;
646                         sv->vec.x= (short)(tempf+0.5);
647                         sv->vec.x+= AREAGRID-1;
648                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
649                         
650                         CLAMP(sv->vec.x, 0, winsizex);
651                         
652                         tempf= ((float)sv->vec.y )*facy;
653                         sv->vec.y= (short)(tempf+0.5);
654                         sv->vec.y+= AREAGRID-1;
655                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
656                         
657                         CLAMP(sv->vec.y, 0, winsizey);
658                 }
659         }
660         
661         /* test for collapsed areas. This could happen in some blender version... */
662         /* ton: removed option now, it needs Context... */
663         
664         /* make each window at least HEADERY high */
665         for(sa= sc->areabase.first; sa; sa= sa->next) {
666                 int headery= HEADERY+1;
667                 
668                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
669                         /* lower edge */
670                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
671                         if(se && sa->v1!=sa->v2 ) {
672                                 int yval;
673                                 
674                                 select_connected_scredge(sc, se);
675                                 
676                                 /* all selected vertices get the right offset */
677                                 yval= sa->v2->vec.y-headery;
678                                 sv= sc->vertbase.first;
679                                 while(sv) {
680                                         /* if is a collapsed area */
681                                         if(sv!=sa->v2 && sv!=sa->v3) {
682                                                 if(sv->flag) sv->vec.y= yval;
683                                         }
684                                         sv= sv->next;
685                                 }
686                         }
687                 }
688         }
689         
690 }
691
692 /* *********************** DRAWING **************************************** */
693
694
695 #define SCR_BACK 0.55
696 #define SCR_ROUND 12
697
698 /* draw vertical shape visualising future joining (left as well
699  * right direction of future joining) */
700 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
701 {
702         vec2f points[10];
703         short i;
704         float w, h;
705         float width = sa->v3->vec.x - sa->v1->vec.x;
706         float height = sa->v3->vec.y - sa->v1->vec.y;
707
708         if(height<width) {
709                 h = height/8;
710                 w = height/4;
711         }
712         else {
713                 h = width/8;
714                 w = width/4;
715         }
716
717         points[0].x = sa->v1->vec.x;
718         points[0].y = sa->v1->vec.y + height/2;
719
720         points[1].x = sa->v1->vec.x;
721         points[1].y = sa->v1->vec.y;
722
723         points[2].x = sa->v4->vec.x - w;
724         points[2].y = sa->v4->vec.y;
725
726         points[3].x = sa->v4->vec.x - w;
727         points[3].y = sa->v4->vec.y + height/2 - 2*h;
728
729         points[4].x = sa->v4->vec.x - 2*w;
730         points[4].y = sa->v4->vec.y + height/2;
731
732         points[5].x = sa->v4->vec.x - w;
733         points[5].y = sa->v4->vec.y + height/2 + 2*h;
734
735         points[6].x = sa->v3->vec.x - w;
736         points[6].y = sa->v3->vec.y;
737
738         points[7].x = sa->v2->vec.x;
739         points[7].y = sa->v2->vec.y;
740
741         points[8].x = sa->v4->vec.x;
742         points[8].y = sa->v4->vec.y + height/2 - h;
743
744         points[9].x = sa->v4->vec.x;
745         points[9].y = sa->v4->vec.y + height/2 + h;
746
747         if(dir=='l') {
748                 /* when direction is left, then we flip direction of arrow */
749                 float cx = sa->v1->vec.x + width;
750                 for(i=0;i<10;i++) {
751                         points[i].x -= cx;
752                         points[i].x = -points[i].x;
753                         points[i].x += sa->v1->vec.x;
754                 }
755         }
756
757         glBegin(GL_POLYGON);
758         for(i=0;i<5;i++)
759                 glVertex2f(points[i].x, points[i].y);
760         glEnd();
761         glBegin(GL_POLYGON);
762         for(i=4;i<8;i++)
763                 glVertex2f(points[i].x, points[i].y);
764         glVertex2f(points[0].x, points[0].y);
765         glEnd();
766
767         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
768         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
769 }
770
771 /* draw vertical shape visualising future joining (up/down direction) */
772 static void draw_vertical_join_shape(ScrArea *sa, char dir)
773 {
774         vec2f points[10];
775         short i;
776         float w, h;
777         float width = sa->v3->vec.x - sa->v1->vec.x;
778         float height = sa->v3->vec.y - sa->v1->vec.y;
779
780         if(height<width) {
781                 h = height/4;
782                 w = height/8;
783         }
784         else {
785                 h = width/4;
786                 w = width/8;
787         }
788
789         points[0].x = sa->v1->vec.x + width/2;
790         points[0].y = sa->v3->vec.y;
791
792         points[1].x = sa->v2->vec.x;
793         points[1].y = sa->v2->vec.y;
794
795         points[2].x = sa->v1->vec.x;
796         points[2].y = sa->v1->vec.y + h;
797
798         points[3].x = sa->v1->vec.x + width/2 - 2*w;
799         points[3].y = sa->v1->vec.y + h;
800
801         points[4].x = sa->v1->vec.x + width/2;
802         points[4].y = sa->v1->vec.y + 2*h;
803
804         points[5].x = sa->v1->vec.x + width/2 + 2*w;
805         points[5].y = sa->v1->vec.y + h;
806
807         points[6].x = sa->v4->vec.x;
808         points[6].y = sa->v4->vec.y + h;
809         
810         points[7].x = sa->v3->vec.x;
811         points[7].y = sa->v3->vec.y;
812
813         points[8].x = sa->v1->vec.x + width/2 - w;
814         points[8].y = sa->v1->vec.y;
815
816         points[9].x = sa->v1->vec.x + width/2 + w;
817         points[9].y = sa->v1->vec.y;
818
819         if(dir=='u') {
820                 /* when direction is up, then we flip direction of arrow */
821                 float cy = sa->v1->vec.y + height;
822                 for(i=0;i<10;i++) {
823                         points[i].y -= cy;
824                         points[i].y = -points[i].y;
825                         points[i].y += sa->v1->vec.y;
826                 }
827         }
828
829         glBegin(GL_POLYGON);
830         for(i=0;i<5;i++)
831                 glVertex2f(points[i].x, points[i].y);
832         glEnd();
833         glBegin(GL_POLYGON);
834         for(i=4;i<8;i++)
835                 glVertex2f(points[i].x, points[i].y);
836         glVertex2f(points[0].x, points[0].y);
837         glEnd();
838
839         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
840         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
841 }
842
843 /* draw join shape due to direction of joining */
844 static void draw_join_shape(ScrArea *sa, char dir)
845 {
846         if(dir=='u' || dir=='d')
847                 draw_vertical_join_shape(sa, dir);
848         else
849                 draw_horizontal_join_shape(sa, dir);
850 }
851
852 /* draw screen area darker with arrow (visualisation of future joining) */
853 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
854 {
855         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
856         glEnable(GL_BLEND);
857         glColor4ub(0, 0, 0, 50);
858         draw_join_shape(sa, dir);
859         glDisable(GL_BLEND);
860 }
861
862 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
863 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
864 {
865         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
866         glEnable(GL_BLEND);
867         /* value 181 was hardly computed: 181~105 */
868         glColor4ub(255, 255, 255, 50);          
869         /* draw_join_shape(sa, dir); */
870         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
871         glDisable(GL_BLEND);
872 }
873
874 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
875 {
876         /* right border area */
877         if(x2<sizex-1)
878                 sdrawline(x2+a, y1, x2+a, y2);
879         
880         /* left border area */
881         if(x1>0)  /* otherwise it draws the emboss of window over */
882                 sdrawline(x1+a, y1, x1+a, y2);
883         
884         /* top border area */
885         if(y2<sizey-1)
886                 sdrawline(x1, y2+a, x2, y2+a);
887         
888         /* bottom border area */
889         if(y1>0)
890                 sdrawline(x1, y1+a, x2, y1+a);
891         
892 }
893
894 /** screen edges drawing **/
895 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
896 {
897         short x1= sa->v1->vec.x;
898         short y1= sa->v1->vec.y;
899         short x2= sa->v3->vec.x;
900         short y2= sa->v3->vec.y;
901         short a, rt;
902         
903         rt= CLAMPIS(G.rt, 0, 16);
904         
905         if(center==0) {
906                 cpack(0x505050);
907                 for(a=-rt; a<=rt; a++)
908                         if(a!=0)
909                                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
910         }
911         else {
912                 cpack(0x0);
913                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
914         }
915 }
916
917 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
918
919 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
920 {
921         bScreen *newsc;
922         
923         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
924         
925         /* make new empty screen: */
926         newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
927         /* copy all data */
928         screen_copy(newsc, sc);
929
930         return newsc;
931 }
932
933 /* screen sets cursor based on swinid */
934 static void region_cursor_set(wmWindow *win, int swinid)
935 {
936         ScrArea *sa= win->screen->areabase.first;
937         
938         for(;sa; sa= sa->next) {
939                 ARegion *ar= sa->regionbase.first;
940                 for(;ar; ar= ar->next) {
941                         if(ar->swinid == swinid) {
942                                 if(ar->type && ar->type->cursor)
943                                         ar->type->cursor(win, sa, ar);
944                                 else
945                                         WM_cursor_set(win, CURSOR_STD);
946                                 return;
947                         }
948                 }
949         }
950 }
951
952 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
953 {
954         
955         /* generic notes */
956         switch(note->category) {
957                 case NC_WM:
958                         if(note->data==ND_FILEREAD)
959                                 win->screen->do_draw= 1;
960                         break;
961                 case NC_WINDOW:
962                         win->screen->do_draw= 1;
963                         break;
964                 case NC_SCREEN:
965                         if(note->action==NA_EDITED)
966                                 win->screen->do_draw= win->screen->do_refresh= 1;
967                 case NC_SCENE:
968                         if(note->data==ND_MODE)
969                                 region_cursor_set(win, note->swinid);                           
970                         break;
971         }
972 }
973
974 /* only for edge lines between areas, and the blended join arrows */
975 void ED_screen_draw(wmWindow *win)
976 {
977         ScrArea *sa;
978         ScrArea *sa1=NULL;
979         ScrArea *sa2=NULL;
980         int dir = -1;
981         int dira = -1;
982
983         wmSubWindowSet(win, win->screen->mainwin);
984         
985         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
986                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
987                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
988                 drawscredge_area(sa, win->sizex, win->sizey, 0);
989         }
990         for(sa= win->screen->areabase.first; sa; sa= sa->next)
991                 drawscredge_area(sa, win->sizex, win->sizey, 1);
992         
993         /* blended join arrow */
994         if (sa1 && sa2) {
995                 dir = area_getorientation(win->screen, sa1, sa2);
996                 if (dir >= 0) {
997                         switch(dir) {
998                                 case 0: /* W */
999                                         dir = 'r';
1000                                         dira = 'l';
1001                                         break;
1002                                 case 1: /* N */
1003                                         dir = 'd';
1004                                         dira = 'u';
1005                                         break;
1006                                 case 2: /* E */
1007                                         dir = 'l';
1008                                         dira = 'r';
1009                                         break;
1010                                 case 3: /* S */
1011                                         dir = 'u';
1012                                         dira = 'd';
1013                                         break;
1014                         }
1015                 }
1016                 scrarea_draw_shape_dark(sa2, dir);
1017                 scrarea_draw_shape_light(sa1, dira);
1018         }
1019         
1020 //      if(G.f & G_DEBUG) printf("draw screen\n");
1021         win->screen->do_draw= 0;
1022 }
1023
1024 /* make this screen usable */
1025 /* for file read and first use, for scaling window, area moves */
1026 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1027 {
1028         ScrArea *sa;
1029         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
1030         
1031         screen_test_scale(win->screen, win->sizex, win->sizey);
1032         
1033         if(win->screen->mainwin==0)
1034                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1035         else
1036                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1037         
1038         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1039                 /* set spacetype and region callbacks, calls init() */
1040                 /* sets subwindows for regions, adds handlers */
1041                 ED_area_initialize(wm, win, sa);
1042         }
1043
1044         /* wake up animtimer */
1045         if(win->screen->animtimer)
1046                 WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1047         
1048         if(G.f & G_DEBUG) printf("set screen\n");
1049         win->screen->do_refresh= 0;
1050
1051         win->screen->context= ed_screen_context;
1052 }
1053
1054 /* file read, set all screens, ... */
1055 void ED_screens_initialize(wmWindowManager *wm)
1056 {
1057         wmWindow *win;
1058         
1059         for(win= wm->windows.first; win; win= win->next) {
1060                 
1061                 if(win->screen==NULL)
1062                         win->screen= G.main->screen.first;
1063                 
1064                 ED_screen_refresh(wm, win);
1065         }
1066 }
1067
1068
1069 /* *********** exit calls are for closing running stuff ******** */
1070
1071 void ED_region_exit(bContext *C, ARegion *ar)
1072 {
1073         ARegion *prevar= CTX_wm_region(C);
1074
1075         CTX_wm_region_set(C, ar);
1076         WM_event_remove_handlers(C, &ar->handlers);
1077         if(ar->swinid)
1078                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1079         ar->swinid= 0;
1080         
1081         if(ar->headerstr)
1082                 MEM_freeN(ar->headerstr);
1083         ar->headerstr= NULL;
1084         
1085         CTX_wm_region_set(C, prevar);
1086 }
1087
1088 void ED_area_exit(bContext *C, ScrArea *sa)
1089 {
1090         ScrArea *prevsa= CTX_wm_area(C);
1091         ARegion *ar;
1092
1093         if (sa->spacetype == SPACE_FILE) {
1094                 ED_fileselect_exit(C, (SpaceFile*)(sa) ? sa->spacedata.first : CTX_wm_space_data(C));
1095         }
1096
1097         CTX_wm_area_set(C, sa);
1098         for(ar= sa->regionbase.first; ar; ar= ar->next)
1099                 ED_region_exit(C, ar);
1100
1101         WM_event_remove_handlers(C, &sa->handlers);
1102         CTX_wm_area_set(C, prevsa);
1103 }
1104
1105 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1106 {
1107         wmWindowManager *wm= CTX_wm_manager(C);
1108         wmWindow *prevwin= CTX_wm_window(C);
1109         ScrArea *sa;
1110         ARegion *ar;
1111
1112         CTX_wm_window_set(C, window);
1113         
1114         if(screen->animtimer)
1115                 WM_event_remove_timer(wm, window, screen->animtimer);
1116         screen->animtimer= NULL;
1117         
1118         if(screen->mainwin)
1119                 wm_subwindow_close(window, screen->mainwin);
1120         screen->mainwin= 0;
1121         screen->subwinactive= 0;
1122         
1123         for(ar= screen->regionbase.first; ar; ar= ar->next)
1124                 ED_region_exit(C, ar);
1125
1126         for(sa= screen->areabase.first; sa; sa= sa->next)
1127                 ED_area_exit(C, sa);
1128
1129         /* mark it available for use for other windows */
1130         screen->winid= 0;
1131         
1132         /* before deleting the temp screen or we get invalid access */
1133         if (prevwin->screen->full != SCREENTEMP) {
1134                 /* use previous window if possible */
1135                 CTX_wm_window_set(C, prevwin);
1136         } else {
1137                 /* none otherwise */
1138                 CTX_wm_window_set(C, NULL);
1139         }
1140         
1141         /* if temp screen, delete it */
1142         if(screen->full == SCREENTEMP) {
1143                 Main *bmain= CTX_data_main(C);
1144                 free_libblock(&bmain->screen, screen);
1145         }
1146 }
1147
1148 /* *********************************** */
1149
1150 /* case when on area-edge or in azones, or outside window */
1151 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1152 {
1153         AZone *az= NULL;
1154         ScrArea *sa;
1155         
1156         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1157                 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1158                         break;
1159         
1160         if(sa) {
1161                 if(az->type==AZONE_AREA)
1162                         WM_cursor_set(win, CURSOR_EDIT);
1163                 else if(az->type==AZONE_REGION) {
1164                         if(az->edge == 'l' || az->edge == 'r')
1165                                 WM_cursor_set(win, CURSOR_X_MOVE);
1166                         else
1167                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1168                 }
1169         }
1170         else {
1171                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1172                 
1173                 if (actedge) {
1174                         if(scredge_is_horizontal(actedge))
1175                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1176                         else
1177                                 WM_cursor_set(win, CURSOR_X_MOVE);
1178                 }
1179                 else
1180                         WM_cursor_set(win, CURSOR_STD);
1181         } 
1182 }
1183
1184
1185 /* called in wm_event_system.c. sets state vars in screen, cursors */
1186 /* event type is mouse move */
1187 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1188 {
1189         if(win->screen) {
1190                 bScreen *scr= win->screen;
1191                 ScrArea *sa;
1192                 ARegion *ar;
1193                 int oldswin= scr->subwinactive;
1194
1195                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1196                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1197                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1198                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1199                                                 break;
1200                 }
1201                 if(sa) {
1202                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1203                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1204                                         scr->subwinactive= ar->swinid;
1205                         }
1206                 }
1207                 else
1208                         scr->subwinactive= scr->mainwin;
1209                 
1210                 /* check for redraw headers */
1211                 if(oldswin!=scr->subwinactive) {
1212
1213                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1214                                 int do_draw= 0;
1215                                 
1216                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1217                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1218                                                 do_draw= 1;
1219                                 
1220                                 if(do_draw) {
1221                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1222                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1223                                                         ED_region_tag_redraw(ar);
1224                                 }
1225                         }
1226                 }
1227                 
1228                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1229                 if(scr->subwinactive==scr->mainwin) {
1230                         screen_cursor_set(win, event);
1231                 }
1232                 else if(oldswin!=scr->subwinactive) {
1233                         region_cursor_set(win, scr->subwinactive);
1234                 }
1235         }
1236 }
1237
1238 int ED_screen_area_active(const bContext *C)
1239 {
1240         wmWindow *win= CTX_wm_window(C);
1241         bScreen *sc= CTX_wm_screen(C);
1242         ScrArea *sa= CTX_wm_area(C);
1243
1244         if(win && sc && sa) {
1245                 AZone *az= is_in_area_actionzone(sa, win->eventstate->x, win->eventstate->y);
1246                 ARegion *ar;
1247                 
1248                 if (az && az->type == AZONE_REGION)
1249                         return 1;
1250                 
1251                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1252                         if(ar->swinid == sc->subwinactive)
1253                                 return 1;
1254         }       
1255         return 0;
1256 }
1257
1258 /* operator call, WM + Window + screen already existed before */
1259 /* Do NOT call in area/region queues! */
1260 void ED_screen_set(bContext *C, bScreen *sc)
1261 {
1262         wmWindowManager *wm= CTX_wm_manager(C);
1263         wmWindow *win= CTX_wm_window(C);
1264         bScreen *oldscreen= CTX_wm_screen(C);
1265         ID *id;
1266         
1267         /* validate screen, it's called with notifier reference */
1268         for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1269                 if(sc == (bScreen *)id)
1270                         break;
1271         if(id==NULL) 
1272                 return;
1273         
1274         /* check for valid winid */
1275         if(sc->winid!=0 && sc->winid!=win->winid)
1276                 return;
1277         
1278         if(sc->full) {                          /* find associated full */
1279                 bScreen *sc1;
1280                 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1281                         ScrArea *sa= sc1->areabase.first;
1282                         if(sa->full==sc) {
1283                                 sc= sc1;
1284                                 break;
1285                         }
1286                 }
1287         }
1288         
1289         if (oldscreen != sc) {
1290                 wmTimer *wt= oldscreen->animtimer;
1291                 ScrArea *sa;
1292
1293                 /* remove handlers referencing areas in old screen */
1294                 for(sa = oldscreen->areabase.first; sa; sa = sa->next) {
1295                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1296                 }
1297
1298                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1299                 oldscreen->animtimer= NULL;
1300                 if(wt)
1301                         WM_event_timer_sleep(wm, win, wt, 1);
1302                 
1303                 ED_screen_exit(C, win, oldscreen);
1304                 oldscreen->animtimer= wt;
1305                 
1306                 win->screen= sc;
1307                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1308                 
1309                 /* prevent multiwin errors */
1310                 sc->winid= win->winid;
1311                 
1312                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1313                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1314                 
1315                 /* makes button hilites work */
1316                 WM_event_add_mousemove(C);
1317         }
1318 }
1319
1320 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1321 {
1322         wmWindow *win;
1323
1324         for(win=wm->windows.first; win; win=win->next)
1325                 if(win->screen == sc)
1326                         return 1;
1327         
1328         return 0;
1329 }
1330
1331 /* only call outside of area/region loops */
1332 void ED_screen_delete(bContext *C, bScreen *sc)
1333 {
1334         Main *bmain= CTX_data_main(C);
1335         wmWindowManager *wm= CTX_wm_manager(C);
1336         wmWindow *win= CTX_wm_window(C);
1337         bScreen *newsc;
1338         int delete= 1;
1339         
1340         /* don't allow deleting temp fullscreens for now */
1341         if (sc->full == SCREENFULL) {
1342                 return;
1343         }
1344         
1345                 
1346         /* screen can only be in use by one window at a time, so as
1347            long as we are able to find a screen that is unused, we
1348            can safely assume ours is not in use anywhere an delete it */
1349
1350         for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1351                 if(!ed_screen_used(wm, newsc))
1352                         break;
1353         
1354         if(!newsc) {
1355                 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1356                         if(!ed_screen_used(wm, newsc))
1357                                 break;
1358         }
1359
1360         if(!newsc)
1361                 return;
1362
1363         ED_screen_set(C, newsc);
1364
1365         if(delete && win->screen != sc)
1366                 free_libblock(&bmain->screen, sc);
1367 }
1368
1369 /* only call outside of area/region loops */
1370 void ED_screen_set_scene(bContext *C, Scene *scene)
1371 {
1372         bScreen *sc;
1373         bScreen *curscreen= CTX_wm_screen(C);
1374         
1375         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
1376
1377         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1378                 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1379                         
1380                         if(scene != sc->scene) {
1381                                 /* all areas endlocalview */
1382                         // XXX  ScrArea *sa= sc->areabase.first;
1383                         //      while(sa) {
1384                         //              endlocalview(sa);
1385                         //              sa= sa->next;
1386                         //      }               
1387                                 sc->scene= scene;
1388                         }
1389                         
1390                 }
1391         }
1392         
1393         //  copy_view3d_lock(0);        /* space.c */
1394         
1395         /* are there cameras in the views that are not in the scene? */
1396         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1397                 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1398                         ScrArea *sa= sc->areabase.first;
1399                         while(sa) {
1400                                 SpaceLink *sl= sa->spacedata.first;
1401                                 while(sl) {
1402                                         if(sl->spacetype==SPACE_VIEW3D) {
1403                                                 View3D *v3d= (View3D*) sl;
1404
1405                                                 BKE_screen_view3d_sync(v3d, scene);
1406
1407                                                 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1408                                                         v3d->camera= scene_find_camera(sc->scene);
1409                                                         // XXX if (sc==curscreen) handle_view3d_lock();
1410                                                         if (!v3d->camera) {
1411                                                                 ARegion *ar;
1412                                                                 for(ar=v3d->regionbase.first; ar; ar= ar->next) {
1413                                                                         if(ar->regiontype == RGN_TYPE_WINDOW) {
1414                                                                                 RegionView3D *rv3d= ar->regiondata;
1415
1416                                                                                 if(rv3d->persp==RV3D_CAMOB)
1417                                                                                         rv3d->persp= RV3D_PERSP;
1418                                                                         }
1419                                                                 }
1420                                                         }
1421                                                 }
1422                                         }
1423                                         sl= sl->next;
1424                                 }
1425                                 sa= sa->next;
1426                         }
1427                 }
1428         }
1429         
1430         CTX_data_scene_set(C, scene);
1431         set_scene_bg(CTX_data_main(C), scene);
1432         
1433         ED_update_for_newframe(C, 1);
1434         
1435         /* complete redraw */
1436         WM_event_add_notifier(C, NC_WINDOW, NULL);
1437         
1438 }
1439
1440 /* only call outside of area/region loops */
1441 void ED_screen_delete_scene(bContext *C, Scene *scene)
1442 {
1443         Main *bmain= CTX_data_main(C);
1444         Scene *newscene;
1445
1446         if(scene->id.prev)
1447                 newscene= scene->id.prev;
1448         else if(scene->id.next)
1449                 newscene= scene->id.next;
1450         else
1451                 return;
1452
1453         ED_screen_set_scene(C, newscene);
1454
1455         unlink_scene(bmain, scene, newscene);
1456 }
1457
1458 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1459 {
1460         wmWindow *win= CTX_wm_window(C);
1461         bScreen *screen= CTX_wm_screen(C);
1462         ScrArea *newsa= NULL;
1463
1464         if(!sa || sa->full==0) {
1465                 newsa= ED_screen_full_toggle(C, win, sa);
1466         }
1467         
1468         if(!newsa) {
1469                 if (sa->full) {
1470                         /* if this has been called from the temporary info header generated in
1471                          * temp fullscreen layouts, find the correct fullscreen area to change
1472                          * to create a new space inside */
1473                         for (newsa = screen->areabase.first; newsa; newsa=newsa->next) {
1474                                 if (!(sa->flag & AREA_TEMP_INFO))
1475                                         break;
1476                         }
1477                 } else
1478                         newsa= sa;
1479         }
1480         
1481         ED_area_newspace(C, newsa, type);
1482         
1483         return 1;
1484 }
1485
1486 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1487 {
1488         wmWindow *win= CTX_wm_window(C);
1489
1490         ED_area_prevspace(C, sa);
1491         
1492         if(sa->full)
1493                 ED_screen_full_toggle(C, win, sa);
1494 }
1495
1496 /* restore a screen / area back to default operation, after temp fullscreen modes */
1497 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1498 {
1499         wmWindow *win= CTX_wm_window(C);
1500         SpaceLink *sl = sa->spacedata.first;
1501         
1502         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1503          * overlaid on top of a existing setup) then return to the previous space */
1504         
1505         if (sl->next) {
1506                 /* specific checks for space types */
1507
1508                 int sima_restore = 0;
1509
1510                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1511                 if (sl->spacetype == SPACE_IMAGE) {
1512                         SpaceImage *sima= sa->spacedata.first;
1513                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1514                                 sima_restore = 1;
1515                 }
1516
1517                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1518                         SpaceImage *sima= sa->spacedata.first;
1519                         if (sima->flag & SI_PREVSPACE)
1520                                 sima->flag &= ~SI_PREVSPACE;
1521                         if (sima->flag & SI_FULLWINDOW) {
1522                                 sima->flag &= ~SI_FULLWINDOW;
1523                                 ED_screen_full_prevspace(C, sa);
1524                         }
1525                 } else if (sl->spacetype == SPACE_FILE) {
1526                         ED_screen_full_prevspace(C, sa);
1527                 } else
1528                         ED_screen_full_toggle(C, win, sa);
1529         }
1530         /* otherwise just tile the area again */
1531         else {
1532                 ED_screen_full_toggle(C, win, sa);
1533         }
1534 }
1535
1536 /* this function toggles: if area is full then the parent will be restored */
1537 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1538 {
1539         bScreen *sc, *oldscreen;
1540         ARegion *ar;
1541
1542         if(sa) {
1543                 /* ensure we don't have a button active anymore, can crash when
1544                    switching screens with tooltip open because region and tooltip
1545                    are no longer in the same screen */
1546                 for(ar=sa->regionbase.first; ar; ar=ar->next)
1547                         uiFreeBlocks(C, &ar->uiblocks);
1548         }
1549
1550         if(sa && sa->full) {
1551                 short fulltype;
1552
1553                 sc= sa->full;           /* the old screen to restore */
1554                 oldscreen= win->screen; /* the one disappearing */
1555
1556                 fulltype = sc->full;
1557
1558                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1559                    is set */
1560
1561                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1562                         ScrArea *old;
1563
1564                         sc->full= 0;
1565
1566                         /* find old area */
1567                         for(old= sc->areabase.first; old; old= old->next)
1568                                 if(old->full) break;
1569                         if(old==NULL) {
1570                                 if (G.f & G_DEBUG)
1571                                         printf("something wrong in areafullscreen\n");
1572                                 return NULL;
1573                         }
1574                                 // old feature described below (ton)
1575                                 // in autoplay screens the headers are disabled by
1576                                 // default. So use the old headertype instead
1577
1578                         area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
1579                         if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1580                         old->full= NULL;
1581
1582                         /* animtimer back */
1583                         sc->animtimer= oldscreen->animtimer;
1584                         oldscreen->animtimer= NULL;
1585
1586                         ED_screen_set(C, sc);
1587
1588                         free_screen(oldscreen);
1589                         free_libblock(&CTX_data_main(C)->screen, oldscreen);
1590                 }
1591         }
1592         else {
1593                 ScrArea *newa;
1594                 char newname[20];
1595
1596                 oldscreen= win->screen;
1597
1598                 /* nothing wrong with having only 1 area, as far as I can see...
1599                 // is there only 1 area?
1600                 if(oldscreen->areabase.first==oldscreen->areabase.last)
1601                         return NULL;
1602                 */
1603
1604                 oldscreen->full = SCREENFULL;
1605                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name+2, "temp");
1606                 sc= ED_screen_add(win, oldscreen->scene, newname);
1607                 sc->full = SCREENFULL; // XXX
1608
1609                 /* timer */
1610                 sc->animtimer= oldscreen->animtimer;
1611                 oldscreen->animtimer= NULL;
1612
1613                 /* returns the top small area */
1614                 newa= area_split(win, sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1615                 ED_area_newspace(C, newa, SPACE_INFO);
1616
1617                 /* use random area when we have no active one, e.g. when the
1618                    mouse is outside of the window and we open a file browser */
1619                 if(!sa)
1620                         sa= oldscreen->areabase.first;
1621
1622                 /* copy area */
1623                 newa= newa->prev;
1624                 area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
1625                 sa->flag |= AREA_TEMP_INFO;
1626
1627                 sa->full= oldscreen;
1628                 newa->full= oldscreen;
1629                 newa->next->full= oldscreen; // XXX
1630
1631                 ED_screen_set(C, sc);
1632         }
1633
1634         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1635         CTX_wm_area_set(C, sc->areabase.first);
1636
1637         /* XXX retopo_force_update(); */
1638
1639         return sc->areabase.first;
1640 }
1641
1642 /* update frame rate info for viewport drawing */
1643 void ED_refresh_viewport_fps(bContext *C)
1644 {
1645         wmTimer *animtimer= CTX_wm_screen(C)->animtimer;
1646         Scene *scene= CTX_data_scene(C);
1647         
1648         /* is anim playback running? */
1649         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1650                 ScreenFrameRateInfo *fpsi= scene->fps_info;
1651                 
1652                 /* if there isn't any info, init it first */
1653                 if (fpsi == NULL)
1654                         fpsi= scene->fps_info= MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1655                 
1656                 /* update the values */
1657                 fpsi->redrawtime= fpsi->lredrawtime;
1658                 fpsi->lredrawtime= animtimer->ltime;
1659         }
1660         else {  
1661                 /* playback stopped or shouldn't be running */
1662                 if (scene->fps_info)
1663                         MEM_freeN(scene->fps_info);
1664                 scene->fps_info= NULL;
1665         }
1666 }
1667
1668 /* redraws: uses defines from stime->redraws 
1669  * enable: 1 - forward on, -1 - backwards on, 0 - off
1670  */
1671 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1672 {
1673         bScreen *screen= CTX_wm_screen(C);
1674         wmWindowManager *wm= CTX_wm_manager(C);
1675         wmWindow *win= CTX_wm_window(C);
1676         Scene *scene= CTX_data_scene(C);
1677         
1678         if(screen->animtimer)
1679                 WM_event_remove_timer(wm, win, screen->animtimer);
1680         screen->animtimer= NULL;
1681         
1682         if(enable) {
1683                 ScreenAnimData *sad= MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1684                 
1685                 screen->animtimer= WM_event_add_timer(wm, win, TIMER0, (1.0/FPS));
1686                 
1687                 sad->ar= CTX_wm_region(C);
1688                 sad->sfra = scene->r.cfra;
1689                 sad->redraws= redraws;
1690                 sad->refresh= refresh;
1691                 sad->flag |= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1692                 sad->flag |= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1693                 
1694                 screen->animtimer->customdata= sad;
1695                 
1696         }
1697         /* notifier catched by top header, for button */
1698         WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1699 }
1700
1701 /* helper for screen_animation_play() - only to be used for TimeLine */
1702 static ARegion *time_top_left_3dwindow(bScreen *screen)
1703 {
1704         ARegion *aret= NULL;
1705         ScrArea *sa;
1706         int min= 10000;
1707         
1708         for(sa= screen->areabase.first; sa; sa= sa->next) {
1709                 if(sa->spacetype==SPACE_VIEW3D) {
1710                         ARegion *ar;
1711                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1712                                 if(ar->regiontype==RGN_TYPE_WINDOW) {
1713                                         if(ar->winrct.xmin - ar->winrct.ymin < min) {
1714                                                 aret= ar;
1715                                                 min= ar->winrct.xmin - ar->winrct.ymin;
1716                                         }
1717                                 }
1718                         }
1719                 }
1720         }
1721
1722         return aret;
1723 }
1724
1725 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1726 {
1727         if(screen && screen->animtimer) {
1728                 wmTimer *wt= screen->animtimer;
1729                 ScreenAnimData *sad= wt->customdata;
1730                 
1731                 sad->redraws= redraws;
1732                 sad->refresh= refresh;
1733                 sad->ar= NULL;
1734                 if(redraws & TIME_REGION)
1735                         sad->ar= time_top_left_3dwindow(screen);
1736         }
1737 }
1738
1739 /* results in fully updated anim system */
1740 void ED_update_for_newframe(const bContext *C, int mute)
1741 {
1742         Main *bmain= CTX_data_main(C);
1743         bScreen *screen= CTX_wm_screen(C);
1744         Scene *scene= CTX_data_scene(C);
1745         
1746 #ifdef DURIAN_CAMERA_SWITCH
1747         void *camera= scene_camera_switch_find(scene);
1748         if(camera && scene->camera != camera) {
1749                 bScreen *sc;
1750                 scene->camera= camera;
1751                 /* are there cameras in the views that are not in the scene? */
1752                 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1753                         BKE_screen_view3d_scene_sync(sc);
1754                 }
1755         }
1756 #endif
1757
1758         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1759         
1760         /* update animated image textures for gpu, etc,
1761          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
1762         ED_image_update_frame(C);
1763
1764         /* this function applies the changes too */
1765         /* XXX future: do all windows */
1766         scene_update_for_newframe(bmain, scene, BKE_screen_visible_layers(screen, scene)); /* BKE_scene.h */
1767         
1768         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1769         //      audiostream_scrub( CFRA );
1770         
1771         /* 3d window, preview */
1772         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1773         
1774         /* all movie/sequence images */
1775         //BIF_image_update_frame();
1776         
1777         /* composite */
1778         if(scene->use_nodes && scene->nodetree)
1779                 ntreeCompositTagAnimated(scene->nodetree);
1780         
1781         /* update animated texture nodes */
1782         {
1783                 Tex *tex;
1784                 for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
1785                         if( tex->use_nodes && tex->nodetree ) {
1786                                 ntreeTexTagAnimated( tex->nodetree );
1787                         }
1788         }
1789         
1790 }
1791
1792