merge with trunk at r31523
[blender.git] / source / blender / editors / screen / screen_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 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         CTX_wm_area_set(C, sa);
1094         for(ar= sa->regionbase.first; ar; ar= ar->next)
1095                 ED_region_exit(C, ar);
1096
1097         WM_event_remove_handlers(C, &sa->handlers);
1098         CTX_wm_area_set(C, prevsa);
1099 }
1100
1101 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1102 {
1103         wmWindowManager *wm= CTX_wm_manager(C);
1104         wmWindow *prevwin= CTX_wm_window(C);
1105         ScrArea *sa;
1106         ARegion *ar;
1107
1108         CTX_wm_window_set(C, window);
1109         
1110         if(screen->animtimer)
1111                 WM_event_remove_timer(wm, window, screen->animtimer);
1112         screen->animtimer= NULL;
1113         
1114         if(screen->mainwin)
1115                 wm_subwindow_close(window, screen->mainwin);
1116         screen->mainwin= 0;
1117         screen->subwinactive= 0;
1118         
1119         for(ar= screen->regionbase.first; ar; ar= ar->next)
1120                 ED_region_exit(C, ar);
1121
1122         for(sa= screen->areabase.first; sa; sa= sa->next)
1123                 ED_area_exit(C, sa);
1124
1125         /* mark it available for use for other windows */
1126         screen->winid= 0;
1127         
1128         /* before deleting the temp screen or we get invalid access */
1129         if (prevwin->screen->full != SCREENTEMP) {
1130                 /* use previous window if possible */
1131                 CTX_wm_window_set(C, prevwin);
1132         } else {
1133                 /* none otherwise */
1134                 CTX_wm_window_set(C, NULL);
1135         }
1136         
1137         /* if temp screen, delete it */
1138         if(screen->full == SCREENTEMP) {
1139                 Main *bmain= CTX_data_main(C);
1140                 free_libblock(&bmain->screen, screen);
1141         }
1142 }
1143
1144 /* *********************************** */
1145
1146 /* case when on area-edge or in azones, or outside window */
1147 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1148 {
1149         AZone *az= NULL;
1150         ScrArea *sa;
1151         
1152         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1153                 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1154                         break;
1155         
1156         if(sa) {
1157                 if(az->type==AZONE_AREA)
1158                         WM_cursor_set(win, CURSOR_EDIT);
1159                 else if(az->type==AZONE_REGION) {
1160                         if(az->edge == 'l' || az->edge == 'r')
1161                                 WM_cursor_set(win, CURSOR_X_MOVE);
1162                         else
1163                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1164                 }
1165         }
1166         else {
1167                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1168                 
1169                 if (actedge) {
1170                         if(scredge_is_horizontal(actedge))
1171                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1172                         else
1173                                 WM_cursor_set(win, CURSOR_X_MOVE);
1174                 }
1175                 else
1176                         WM_cursor_set(win, CURSOR_STD);
1177         } 
1178 }
1179
1180
1181 /* called in wm_event_system.c. sets state vars in screen, cursors */
1182 /* event type is mouse move */
1183 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1184 {
1185         if(win->screen) {
1186                 bScreen *scr= win->screen;
1187                 ScrArea *sa;
1188                 ARegion *ar;
1189                 int oldswin= scr->subwinactive;
1190
1191                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1192                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1193                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1194                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1195                                                 break;
1196                 }
1197                 if(sa) {
1198                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1199                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1200                                         scr->subwinactive= ar->swinid;
1201                         }
1202                 }
1203                 else
1204                         scr->subwinactive= scr->mainwin;
1205                 
1206                 /* check for redraw headers */
1207                 if(oldswin!=scr->subwinactive) {
1208
1209                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1210                                 int do_draw= 0;
1211                                 
1212                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1213                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1214                                                 do_draw= 1;
1215                                 
1216                                 if(do_draw) {
1217                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1218                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1219                                                         ED_region_tag_redraw(ar);
1220                                 }
1221                         }
1222                 }
1223                 
1224                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1225                 if(scr->subwinactive==scr->mainwin) {
1226                         screen_cursor_set(win, event);
1227                 }
1228                 else if(oldswin!=scr->subwinactive) {
1229                         region_cursor_set(win, scr->subwinactive);
1230                 }
1231         }
1232 }
1233
1234 int ED_screen_area_active(const bContext *C)
1235 {
1236         wmWindow *win= CTX_wm_window(C);
1237         bScreen *sc= CTX_wm_screen(C);
1238         ScrArea *sa= CTX_wm_area(C);
1239
1240         if(win && sc && sa) {
1241                 AZone *az= is_in_area_actionzone(sa, win->eventstate->x, win->eventstate->y);
1242                 ARegion *ar;
1243                 
1244                 if (az && az->type == AZONE_REGION)
1245                         return 1;
1246                 
1247                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1248                         if(ar->swinid == sc->subwinactive)
1249                                 return 1;
1250         }       
1251         return 0;
1252 }
1253
1254 /* operator call, WM + Window + screen already existed before */
1255 /* Do NOT call in area/region queues! */
1256 void ED_screen_set(bContext *C, bScreen *sc)
1257 {
1258         wmWindowManager *wm= CTX_wm_manager(C);
1259         wmWindow *win= CTX_wm_window(C);
1260         bScreen *oldscreen= CTX_wm_screen(C);
1261         ID *id;
1262         
1263         /* validate screen, it's called with notifier reference */
1264         for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1265                 if(sc == (bScreen *)id)
1266                         break;
1267         if(id==NULL) 
1268                 return;
1269         
1270         /* check for valid winid */
1271         if(sc->winid!=0 && sc->winid!=win->winid)
1272                 return;
1273         
1274         if(sc->full) {                          /* find associated full */
1275                 bScreen *sc1;
1276                 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1277                         ScrArea *sa= sc1->areabase.first;
1278                         if(sa->full==sc) {
1279                                 sc= sc1;
1280                                 break;
1281                         }
1282                 }
1283         }
1284         
1285         if (oldscreen != sc) {
1286                 wmTimer *wt= oldscreen->animtimer;
1287                 ScrArea *sa;
1288
1289                 /* remove handlers referencing areas in old screen */
1290                 for(sa = oldscreen->areabase.first; sa; sa = sa->next) {
1291                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1292                 }
1293
1294                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1295                 oldscreen->animtimer= NULL;
1296                 if(wt)
1297                         WM_event_timer_sleep(wm, win, wt, 1);
1298                 
1299                 ED_screen_exit(C, win, oldscreen);
1300                 oldscreen->animtimer= wt;
1301                 
1302                 win->screen= sc;
1303                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1304                 
1305                 /* prevent multiwin errors */
1306                 sc->winid= win->winid;
1307                 
1308                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1309                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1310                 
1311                 /* makes button hilites work */
1312                 WM_event_add_mousemove(C);
1313         }
1314 }
1315
1316 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1317 {
1318         wmWindow *win;
1319
1320         for(win=wm->windows.first; win; win=win->next)
1321                 if(win->screen == sc)
1322                         return 1;
1323         
1324         return 0;
1325 }
1326
1327 /* only call outside of area/region loops */
1328 void ED_screen_delete(bContext *C, bScreen *sc)
1329 {
1330         Main *bmain= CTX_data_main(C);
1331         wmWindowManager *wm= CTX_wm_manager(C);
1332         wmWindow *win= CTX_wm_window(C);
1333         bScreen *newsc;
1334         int delete= 1;
1335         
1336         /* don't allow deleting temp fullscreens for now */
1337         if (sc->full == SCREENFULL) {
1338                 return;
1339         }
1340         
1341                 
1342         /* screen can only be in use by one window at a time, so as
1343            long as we are able to find a screen that is unused, we
1344            can safely assume ours is not in use anywhere an delete it */
1345
1346         for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1347                 if(!ed_screen_used(wm, newsc))
1348                         break;
1349         
1350         if(!newsc) {
1351                 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1352                         if(!ed_screen_used(wm, newsc))
1353                                 break;
1354         }
1355
1356         if(!newsc)
1357                 return;
1358
1359         ED_screen_set(C, newsc);
1360
1361         if(delete && win->screen != sc)
1362                 free_libblock(&bmain->screen, sc);
1363 }
1364
1365 /* only call outside of area/region loops */
1366 void ED_screen_set_scene(bContext *C, Scene *scene)
1367 {
1368         bScreen *sc;
1369         bScreen *curscreen= CTX_wm_screen(C);
1370         
1371         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
1372
1373         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1374                 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1375                         
1376                         if(scene != sc->scene) {
1377                                 /* all areas endlocalview */
1378                         // XXX  ScrArea *sa= sc->areabase.first;
1379                         //      while(sa) {
1380                         //              endlocalview(sa);
1381                         //              sa= sa->next;
1382                         //      }               
1383                                 sc->scene= scene;
1384                         }
1385                         
1386                 }
1387         }
1388         
1389         //  copy_view3d_lock(0);        /* space.c */
1390         
1391         /* are there cameras in the views that are not in the scene? */
1392         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1393                 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1394                         ScrArea *sa= sc->areabase.first;
1395                         while(sa) {
1396                                 SpaceLink *sl= sa->spacedata.first;
1397                                 while(sl) {
1398                                         if(sl->spacetype==SPACE_VIEW3D) {
1399                                                 View3D *v3d= (View3D*) sl;
1400
1401                                                 BKE_screen_view3d_sync(v3d, scene);
1402
1403                                                 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1404                                                         v3d->camera= scene_find_camera(sc->scene);
1405                                                         // XXX if (sc==curscreen) handle_view3d_lock();
1406                                                         if (!v3d->camera) {
1407                                                                 ARegion *ar;
1408                                                                 for(ar=v3d->regionbase.first; ar; ar= ar->next) {
1409                                                                         if(ar->regiontype == RGN_TYPE_WINDOW) {
1410                                                                                 RegionView3D *rv3d= ar->regiondata;
1411
1412                                                                                 if(rv3d->persp==RV3D_CAMOB)
1413                                                                                         rv3d->persp= RV3D_PERSP;
1414                                                 }
1415                                         }
1416                                                         }
1417                                                 }
1418                                         }
1419                                         sl= sl->next;
1420                                 }
1421                                 sa= sa->next;
1422                         }
1423                 }
1424         }
1425         
1426         CTX_data_scene_set(C, scene);
1427         set_scene_bg(CTX_data_main(C), scene);
1428         
1429         ED_update_for_newframe(C, 1);
1430         
1431         /* complete redraw */
1432         WM_event_add_notifier(C, NC_WINDOW, NULL);
1433         
1434 }
1435
1436 /* only call outside of area/region loops */
1437 void ED_screen_delete_scene(bContext *C, Scene *scene)
1438 {
1439         Main *bmain= CTX_data_main(C);
1440         Scene *newscene;
1441
1442         if(scene->id.prev)
1443                 newscene= scene->id.prev;
1444         else if(scene->id.next)
1445                 newscene= scene->id.next;
1446         else
1447                 return;
1448
1449         ED_screen_set_scene(C, newscene);
1450
1451         unlink_scene(bmain, scene, newscene);
1452 }
1453
1454 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1455 {
1456         wmWindow *win= CTX_wm_window(C);
1457         bScreen *screen= CTX_wm_screen(C);
1458         ScrArea *newsa= NULL;
1459
1460         if(!sa || sa->full==0) {
1461                 newsa= ED_screen_full_toggle(C, win, sa);
1462         }
1463         
1464         if(!newsa) {
1465                 if (sa->full) {
1466                         /* if this has been called from the temporary info header generated in
1467                          * temp fullscreen layouts, find the correct fullscreen area to change
1468                          * to create a new space inside */
1469                         for (newsa = screen->areabase.first; newsa; newsa=newsa->next) {
1470                                 if (!(sa->flag & AREA_TEMP_INFO))
1471                                         break;
1472                         }
1473                 } else
1474                         newsa= sa;
1475         }
1476         
1477         ED_area_newspace(C, newsa, type);
1478         
1479         return 1;
1480 }
1481
1482 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1483 {
1484         wmWindow *win= CTX_wm_window(C);
1485
1486         ED_area_prevspace(C, sa);
1487         
1488         if(sa->full)
1489                 ED_screen_full_toggle(C, win, sa);
1490 }
1491
1492 /* restore a screen / area back to default operation, after temp fullscreen modes */
1493 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1494 {
1495         wmWindow *win= CTX_wm_window(C);
1496         SpaceLink *sl = sa->spacedata.first;
1497         
1498         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1499          * overlaid on top of a existing setup) then return to the previous space */
1500         
1501         if (sl->next) {
1502                 /* specific checks for space types */
1503                 if (sl->spacetype == SPACE_IMAGE) {
1504                         SpaceImage *sima= sa->spacedata.first;
1505                         if (sima->flag & SI_PREVSPACE)
1506                                 sima->flag &= ~SI_PREVSPACE;
1507                         if (sima->flag & SI_FULLWINDOW) {
1508                                 sima->flag &= ~SI_FULLWINDOW;
1509                                 ED_screen_full_prevspace(C, sa);
1510                         }
1511                 } else if (sl->spacetype == SPACE_FILE) {
1512                         ED_screen_full_prevspace(C, sa);
1513                 } else
1514                         ED_screen_full_toggle(C, win, sa);
1515         }
1516         /* otherwise just tile the area again */
1517         else {
1518                 ED_screen_full_toggle(C, win, sa);
1519         }
1520 }
1521
1522 /* this function toggles: if area is full then the parent will be restored */
1523 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1524 {
1525         bScreen *sc, *oldscreen;
1526         ARegion *ar;
1527
1528         if(sa) {
1529                 /* ensure we don't have a button active anymore, can crash when
1530                    switching screens with tooltip open because region and tooltip
1531                    are no longer in the same screen */
1532                 for(ar=sa->regionbase.first; ar; ar=ar->next)
1533                         uiFreeBlocks(C, &ar->uiblocks);
1534         }
1535
1536         if(sa && sa->full) {
1537                 short fulltype;
1538
1539                 sc= sa->full;           /* the old screen to restore */
1540                 oldscreen= win->screen; /* the one disappearing */
1541
1542                 fulltype = sc->full;
1543
1544                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1545                    is set */
1546
1547                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1548                         ScrArea *old;
1549
1550                         sc->full= 0;
1551
1552                         /* find old area */
1553                         for(old= sc->areabase.first; old; old= old->next)
1554                                 if(old->full) break;
1555                         if(old==NULL) {
1556                                 if (G.f & G_DEBUG)
1557                                         printf("something wrong in areafullscreen\n");
1558                                 return NULL;
1559                         }
1560                                 // old feature described below (ton)
1561                                 // in autoplay screens the headers are disabled by
1562                                 // default. So use the old headertype instead
1563
1564                         area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
1565                         if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1566                         old->full= NULL;
1567
1568                         /* animtimer back */
1569                         sc->animtimer= oldscreen->animtimer;
1570                         oldscreen->animtimer= NULL;
1571
1572                         ED_screen_set(C, sc);
1573
1574                         free_screen(oldscreen);
1575                         free_libblock(&CTX_data_main(C)->screen, oldscreen);
1576                 }
1577         }
1578         else {
1579                 ScrArea *newa;
1580                 char newname[20];
1581
1582                 oldscreen= win->screen;
1583
1584                 /* nothing wrong with having only 1 area, as far as I can see...
1585                 // is there only 1 area?
1586                 if(oldscreen->areabase.first==oldscreen->areabase.last)
1587                         return NULL;
1588                 */
1589
1590                 oldscreen->full = SCREENFULL;
1591                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name+2, "temp");
1592                 sc= ED_screen_add(win, oldscreen->scene, newname);
1593                 sc->full = SCREENFULL; // XXX
1594
1595                 /* timer */
1596                 sc->animtimer= oldscreen->animtimer;
1597                 oldscreen->animtimer= NULL;
1598
1599                 /* returns the top small area */
1600                 newa= area_split(win, sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1601                 ED_area_newspace(C, newa, SPACE_INFO);
1602
1603                 /* use random area when we have no active one, e.g. when the
1604                    mouse is outside of the window and we open a file browser */
1605                 if(!sa)
1606                         sa= oldscreen->areabase.first;
1607
1608                 /* copy area */
1609                 newa= newa->prev;
1610                 area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
1611                 sa->flag |= AREA_TEMP_INFO;
1612
1613                 sa->full= oldscreen;
1614                 newa->full= oldscreen;
1615                 newa->next->full= oldscreen; // XXX
1616
1617                 ED_screen_set(C, sc);
1618         }
1619
1620         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1621         CTX_wm_area_set(C, sc->areabase.first);
1622
1623         /* XXX retopo_force_update(); */
1624
1625         return sc->areabase.first;
1626 }
1627
1628 /* update frame rate info for viewport drawing */
1629 void ED_refresh_viewport_fps(bContext *C)
1630 {
1631         wmTimer *animtimer= CTX_wm_screen(C)->animtimer;
1632         Scene *scene= CTX_data_scene(C);
1633         
1634         /* is anim playback running? */
1635         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1636                 ScreenFrameRateInfo *fpsi= scene->fps_info;
1637                 
1638                 /* if there isn't any info, init it first */
1639                 if (fpsi == NULL)
1640                         fpsi= scene->fps_info= MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1641                 
1642                 /* update the values */
1643                 fpsi->redrawtime= fpsi->lredrawtime;
1644                 fpsi->lredrawtime= animtimer->ltime;
1645         }
1646         else {  
1647                 /* playback stopped or shouldn't be running */
1648                 if (scene->fps_info)
1649                         MEM_freeN(scene->fps_info);
1650                 scene->fps_info= NULL;
1651         }
1652 }
1653
1654 /* redraws: uses defines from stime->redraws 
1655  * enable: 1 - forward on, -1 - backwards on, 0 - off
1656  */
1657 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1658 {
1659         bScreen *screen= CTX_wm_screen(C);
1660         wmWindowManager *wm= CTX_wm_manager(C);
1661         wmWindow *win= CTX_wm_window(C);
1662         Scene *scene= CTX_data_scene(C);
1663         
1664         if(screen->animtimer)
1665                 WM_event_remove_timer(wm, win, screen->animtimer);
1666         screen->animtimer= NULL;
1667         
1668         if(enable) {
1669                 ScreenAnimData *sad= MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1670                 
1671                 screen->animtimer= WM_event_add_timer(wm, win, TIMER0, (1.0/FPS));
1672                 
1673                 sad->ar= CTX_wm_region(C);
1674                 sad->sfra = scene->r.cfra;
1675                 sad->redraws= redraws;
1676                 sad->refresh= refresh;
1677                 sad->flag |= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1678                 sad->flag |= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1679                 
1680                 screen->animtimer->customdata= sad;
1681                 
1682         }
1683         /* notifier catched by top header, for button */
1684         WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1685 }
1686
1687 /* helper for screen_animation_play() - only to be used for TimeLine */
1688 static ARegion *time_top_left_3dwindow(bScreen *screen)
1689 {
1690         ARegion *aret= NULL;
1691         ScrArea *sa;
1692         int min= 10000;
1693         
1694         for(sa= screen->areabase.first; sa; sa= sa->next) {
1695                 if(sa->spacetype==SPACE_VIEW3D) {
1696                         ARegion *ar;
1697                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1698                                 if(ar->regiontype==RGN_TYPE_WINDOW) {
1699                                         if(ar->winrct.xmin - ar->winrct.ymin < min) {
1700                                                 aret= ar;
1701                                                 min= ar->winrct.xmin - ar->winrct.ymin;
1702                                         }
1703                                 }
1704                         }
1705                 }
1706         }
1707
1708         return aret;
1709 }
1710
1711 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1712 {
1713         if(screen && screen->animtimer) {
1714                 wmTimer *wt= screen->animtimer;
1715                 ScreenAnimData *sad= wt->customdata;
1716                 
1717                 sad->redraws= redraws;
1718                 sad->refresh= refresh;
1719                 sad->ar= NULL;
1720                 if(redraws & TIME_REGION)
1721                         sad->ar= time_top_left_3dwindow(screen);
1722         }
1723 }
1724
1725 /* results in fully updated anim system */
1726 void ED_update_for_newframe(const bContext *C, int mute)
1727 {
1728         Main *bmain= CTX_data_main(C);
1729         bScreen *screen= CTX_wm_screen(C);
1730         Scene *scene= CTX_data_scene(C);
1731         
1732 #ifdef DURIAN_CAMERA_SWITCH
1733         void *camera= scene_camera_switch_find(scene);
1734         if(camera && scene->camera != camera) {
1735                 bScreen *sc;
1736                 scene->camera= camera;
1737                 /* are there cameras in the views that are not in the scene? */
1738                 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1739                         BKE_screen_view3d_scene_sync(sc);
1740                 }
1741         }
1742 #endif
1743
1744         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1745         
1746         /* update animated image textures for gpu, etc,
1747          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
1748         ED_image_update_frame(C);
1749
1750         /* this function applies the changes too */
1751         /* XXX future: do all windows */
1752         scene_update_for_newframe(bmain, scene, BKE_screen_visible_layers(screen, scene)); /* BKE_scene.h */
1753         
1754         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1755         //      audiostream_scrub( CFRA );
1756         
1757         /* 3d window, preview */
1758         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1759         
1760         /* all movie/sequence images */
1761         //BIF_image_update_frame();
1762         
1763         /* composite */
1764         if(scene->use_nodes && scene->nodetree)
1765                 ntreeCompositTagAnimated(scene->nodetree);
1766         
1767         /* update animated texture nodes */
1768         {
1769                 Tex *tex;
1770                 for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
1771                         if( tex->use_nodes && tex->nodetree ) {
1772                                 ntreeTexTagAnimated( tex->nodetree );
1773                         }
1774         }
1775         
1776 }
1777
1778