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