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