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