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