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