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