Fix for Francesco Siddi: ALT+A should always start with the scene start frame!
[blender-staging.git] / source / blender / editors / screen / screen_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/screen/screen_edit.c
26  *  \ingroup edscr
27  */
28
29
30 #include <string.h>
31 #include <math.h>
32
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_scene_types.h"
37 #include "DNA_userdef_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43 #include "BKE_global.h"
44 #include "BKE_library.h"
45 #include "BKE_main.h"
46 #include "BKE_node.h"
47 #include "BKE_screen.h"
48 #include "BKE_scene.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_glutil.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "ED_image.h"
57 #include "ED_object.h"
58 #include "ED_screen.h"
59 #include "ED_screen_types.h"
60 #include "ED_fileselect.h"
61 #include "ED_clip.h"
62 #include "ED_render.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                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
538                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
539                 return 0;
540         }
541         
542         if(dir == 0) {
543                 sa1->v1= sa2->v1;
544                 sa1->v2= sa2->v2;
545                 screen_addedge(scr, sa1->v2, sa1->v3);
546                 screen_addedge(scr, sa1->v1, sa1->v4);
547         }
548         else if(dir == 1) {
549                 sa1->v2= sa2->v2;
550                 sa1->v3= sa2->v3;
551                 screen_addedge(scr, sa1->v1, sa1->v2);
552                 screen_addedge(scr, sa1->v3, sa1->v4);
553         }
554         else if(dir == 2) {
555                 sa1->v3= sa2->v3;
556                 sa1->v4= sa2->v4;
557                 screen_addedge(scr, sa1->v2, sa1->v3);
558                 screen_addedge(scr, sa1->v1, sa1->v4);
559         }
560         else if(dir == 3) {
561                 sa1->v1= sa2->v1;
562                 sa1->v4= sa2->v4;
563                 screen_addedge(scr, sa1->v1, sa1->v2);
564                 screen_addedge(scr, sa1->v3, sa1->v4);
565         }
566         
567         screen_delarea(C, scr, sa2);
568         removedouble_scrverts(scr);
569         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
570         
571         return 1;
572 }
573
574 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
575 {
576         ScrEdge *se;
577         ScrVert *sv;
578         int oneselected;
579         char dir;
580         
581         /* select connected, only in the right direction */
582         /* 'dir' is the direction of EDGE */
583         
584         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
585         else dir= 'h';
586         
587         sv= sc->vertbase.first;
588         while(sv) {
589                 sv->flag = 0;
590                 sv= sv->next;
591         }
592         
593         edge->v1->flag= 1;
594         edge->v2->flag= 1;
595         
596         oneselected= 1;
597         while(oneselected) {
598                 se= sc->edgebase.first;
599                 oneselected= 0;
600                 while(se) {
601                         if(se->v1->flag + se->v2->flag==1) {
602                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
603                                         se->v1->flag= se->v2->flag= 1;
604                                         oneselected= 1;
605                                 }
606                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
607                                                 se->v1->flag= se->v2->flag= 1;
608                                                 oneselected= 1;
609                                         }
610                         }
611                                 se= se->next;
612                 }
613         }
614 }
615
616 /* test if screen vertices should be scaled */
617 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
618 {
619         ScrVert *sv=NULL;
620         ScrArea *sa;
621         int sizex, sizey;
622         float facx, facy, tempf, min[2], max[2];
623         
624         /* calculate size */
625         min[0]= min[1]= 10000.0f;
626         max[0]= max[1]= 0.0f;
627         
628         for(sv= sc->vertbase.first; sv; sv= sv->next) {
629                 min[0]= MIN2(min[0], sv->vec.x);
630                 min[1]= MIN2(min[1], sv->vec.y);
631                 max[0]= MAX2(max[0], sv->vec.x);
632                 max[1]= MAX2(max[1], sv->vec.y);
633         }
634         
635         /* always make 0.0 left under */
636         for(sv= sc->vertbase.first; sv; sv= sv->next) {
637                 sv->vec.x -= min[0];
638                 sv->vec.y -= min[1];
639         }
640         
641         sizex= max[0]-min[0];
642         sizey= max[1]-min[1];
643         
644         if(sizex!= winsizex || sizey!= winsizey) {
645                 facx= winsizex;
646                 facx/= (float)sizex;
647                 facy= winsizey;
648                 facy/= (float)sizey;
649                 
650                 /* make sure it fits! */
651                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
652                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
653                          * need some way to store these as floats internally and re-apply from there. */
654                         tempf= ((float)sv->vec.x)*facx;
655                         sv->vec.x= (short)(tempf+0.5f);
656                         sv->vec.x+= AREAGRID-1;
657                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
658
659                         CLAMP(sv->vec.x, 0, winsizex);
660                         
661                         tempf= ((float)sv->vec.y)*facy;
662                         sv->vec.y= (short)(tempf+0.5f);
663                         sv->vec.y+= AREAGRID-1;
664                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
665
666                         CLAMP(sv->vec.y, 0, winsizey);
667                 }
668         }
669         
670         /* test for collapsed areas. This could happen in some blender version... */
671         /* ton: removed option now, it needs Context... */
672         
673         /* make each window at least ED_area_headersize() high */
674         for(sa= sc->areabase.first; sa; sa= sa->next) {
675                 int headery= ED_area_headersize()+1;
676                 
677                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
678                         /* lower edge */
679                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
680                         if(se && sa->v1!=sa->v2 ) {
681                                 int yval;
682                                 
683                                 select_connected_scredge(sc, se);
684                                 
685                                 /* all selected vertices get the right offset */
686                                 yval= sa->v2->vec.y-headery;
687                                 sv= sc->vertbase.first;
688                                 while(sv) {
689                                         /* if is a collapsed area */
690                                         if(sv!=sa->v2 && sv!=sa->v3) {
691                                                 if(sv->flag) sv->vec.y= yval;
692                                         }
693                                         sv= sv->next;
694                                 }
695                         }
696                 }
697         }
698         
699 }
700
701 /* *********************** DRAWING **************************************** */
702
703
704 #define SCR_BACK 0.55
705 #define SCR_ROUND 12
706
707 /* draw vertical shape visualizing future joining (left as well
708  * right direction of future joining) */
709 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
710 {
711         vec2f points[10];
712         short i;
713         float w, h;
714         float width = sa->v3->vec.x - sa->v1->vec.x;
715         float height = sa->v3->vec.y - sa->v1->vec.y;
716
717         if(height<width) {
718                 h = height/8;
719                 w = height/4;
720         }
721         else {
722                 h = width/8;
723                 w = width/4;
724         }
725
726         points[0].x = sa->v1->vec.x;
727         points[0].y = sa->v1->vec.y + height/2;
728
729         points[1].x = sa->v1->vec.x;
730         points[1].y = sa->v1->vec.y;
731
732         points[2].x = sa->v4->vec.x - w;
733         points[2].y = sa->v4->vec.y;
734
735         points[3].x = sa->v4->vec.x - w;
736         points[3].y = sa->v4->vec.y + height/2 - 2*h;
737
738         points[4].x = sa->v4->vec.x - 2*w;
739         points[4].y = sa->v4->vec.y + height/2;
740
741         points[5].x = sa->v4->vec.x - w;
742         points[5].y = sa->v4->vec.y + height/2 + 2*h;
743
744         points[6].x = sa->v3->vec.x - w;
745         points[6].y = sa->v3->vec.y;
746
747         points[7].x = sa->v2->vec.x;
748         points[7].y = sa->v2->vec.y;
749
750         points[8].x = sa->v4->vec.x;
751         points[8].y = sa->v4->vec.y + height/2 - h;
752
753         points[9].x = sa->v4->vec.x;
754         points[9].y = sa->v4->vec.y + height/2 + h;
755
756         if(dir=='l') {
757                 /* when direction is left, then we flip direction of arrow */
758                 float cx = sa->v1->vec.x + width;
759                 for(i=0;i<10;i++) {
760                         points[i].x -= cx;
761                         points[i].x = -points[i].x;
762                         points[i].x += sa->v1->vec.x;
763                 }
764         }
765
766         glBegin(GL_POLYGON);
767         for(i=0;i<5;i++)
768                 glVertex2f(points[i].x, points[i].y);
769         glEnd();
770         glBegin(GL_POLYGON);
771         for(i=4;i<8;i++)
772                 glVertex2f(points[i].x, points[i].y);
773         glVertex2f(points[0].x, points[0].y);
774         glEnd();
775
776         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
777         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
778 }
779
780 /* draw vertical shape visualizing future joining (up/down direction) */
781 static void draw_vertical_join_shape(ScrArea *sa, char dir)
782 {
783         vec2f points[10];
784         short i;
785         float w, h;
786         float width = sa->v3->vec.x - sa->v1->vec.x;
787         float height = sa->v3->vec.y - sa->v1->vec.y;
788
789         if(height<width) {
790                 h = height/4;
791                 w = height/8;
792         }
793         else {
794                 h = width/4;
795                 w = width/8;
796         }
797
798         points[0].x = sa->v1->vec.x + width/2;
799         points[0].y = sa->v3->vec.y;
800
801         points[1].x = sa->v2->vec.x;
802         points[1].y = sa->v2->vec.y;
803
804         points[2].x = sa->v1->vec.x;
805         points[2].y = sa->v1->vec.y + h;
806
807         points[3].x = sa->v1->vec.x + width/2 - 2*w;
808         points[3].y = sa->v1->vec.y + h;
809
810         points[4].x = sa->v1->vec.x + width/2;
811         points[4].y = sa->v1->vec.y + 2*h;
812
813         points[5].x = sa->v1->vec.x + width/2 + 2*w;
814         points[5].y = sa->v1->vec.y + h;
815
816         points[6].x = sa->v4->vec.x;
817         points[6].y = sa->v4->vec.y + h;
818         
819         points[7].x = sa->v3->vec.x;
820         points[7].y = sa->v3->vec.y;
821
822         points[8].x = sa->v1->vec.x + width/2 - w;
823         points[8].y = sa->v1->vec.y;
824
825         points[9].x = sa->v1->vec.x + width/2 + w;
826         points[9].y = sa->v1->vec.y;
827
828         if(dir=='u') {
829                 /* when direction is up, then we flip direction of arrow */
830                 float cy = sa->v1->vec.y + height;
831                 for(i=0;i<10;i++) {
832                         points[i].y -= cy;
833                         points[i].y = -points[i].y;
834                         points[i].y += sa->v1->vec.y;
835                 }
836         }
837
838         glBegin(GL_POLYGON);
839         for(i=0;i<5;i++)
840                 glVertex2f(points[i].x, points[i].y);
841         glEnd();
842         glBegin(GL_POLYGON);
843         for(i=4;i<8;i++)
844                 glVertex2f(points[i].x, points[i].y);
845         glVertex2f(points[0].x, points[0].y);
846         glEnd();
847
848         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
849         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
850 }
851
852 /* draw join shape due to direction of joining */
853 static void draw_join_shape(ScrArea *sa, char dir)
854 {
855         if(dir=='u' || dir=='d')
856                 draw_vertical_join_shape(sa, dir);
857         else
858                 draw_horizontal_join_shape(sa, dir);
859 }
860
861 /* draw screen area darker with arrow (visualisation of future joining) */
862 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
863 {
864         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
865         glEnable(GL_BLEND);
866         glColor4ub(0, 0, 0, 50);
867         draw_join_shape(sa, dir);
868         glDisable(GL_BLEND);
869 }
870
871 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
872 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
873 {
874         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
875         glEnable(GL_BLEND);
876         /* value 181 was hardly computed: 181~105 */
877         glColor4ub(255, 255, 255, 50);          
878         /* draw_join_shape(sa, dir); */
879         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
880         glDisable(GL_BLEND);
881 }
882
883 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
884 {
885         /* right border area */
886         if(x2<sizex-1)
887                 sdrawline(x2+a, y1, x2+a, y2);
888         
889         /* left border area */
890         if(x1>0)  /* otherwise it draws the emboss of window over */
891                 sdrawline(x1+a, y1, x1+a, y2);
892         
893         /* top border area */
894         if(y2<sizey-1)
895                 sdrawline(x1, y2+a, x2, y2+a);
896         
897         /* bottom border area */
898         if(y1>0)
899                 sdrawline(x1, y1+a, x2, y1+a);
900         
901 }
902
903 /** screen edges drawing **/
904 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
905 {
906         short x1= sa->v1->vec.x;
907         short y1= sa->v1->vec.y;
908         short x2= sa->v3->vec.x;
909         short y2= sa->v3->vec.y;
910         short a, rt;
911         
912         rt= 0; // CLAMPIS(G.rt, 0, 16);
913         
914         if(center==0) {
915                 cpack(0x505050);
916                 for(a=-rt; a<=rt; a++)
917                         if(a!=0)
918                                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
919         }
920         else {
921                 cpack(0x0);
922                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
923         }
924 }
925
926 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
927
928 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
929 {
930         bScreen *newsc;
931         
932         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
933         
934         /* make new empty screen: */
935         newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
936         /* copy all data */
937         screen_copy(newsc, sc);
938
939         return newsc;
940 }
941
942 /* screen sets cursor based on swinid */
943 static void region_cursor_set(wmWindow *win, int swinid)
944 {
945         ScrArea *sa= win->screen->areabase.first;
946         
947         for(;sa; sa= sa->next) {
948                 ARegion *ar= sa->regionbase.first;
949                 for(;ar; ar= ar->next) {
950                         if(ar->swinid == swinid) {
951                                 if(ar->type && ar->type->cursor)
952                                         ar->type->cursor(win, sa, ar);
953                                 else
954                                         WM_cursor_set(win, CURSOR_STD);
955                                 return;
956                         }
957                 }
958         }
959 }
960
961 void ED_screen_do_listen(bContext *C, wmNotifier *note)
962 {
963         wmWindow *win= CTX_wm_window(C);
964         
965         /* generic notes */
966         switch(note->category) {
967                 case NC_WM:
968                         if(note->data==ND_FILEREAD)
969                                 win->screen->do_draw= 1;
970                         break;
971                 case NC_WINDOW:
972                         win->screen->do_draw= 1;
973                         break;
974                 case NC_SCREEN:
975                         if(note->data==ND_SUBWINACTIVE)
976                                 uiFreeActiveButtons(C, win->screen);
977                         if(note->action==NA_EDITED)
978                                 win->screen->do_draw= win->screen->do_refresh= 1;
979                         break;
980                 case NC_SCENE:
981                         if(note->data==ND_MODE)
982                                 region_cursor_set(win, note->swinid);                           
983                         break;
984         }
985 }
986
987 /* only for edge lines between areas, and the blended join arrows */
988 void ED_screen_draw(wmWindow *win)
989 {
990         ScrArea *sa;
991         ScrArea *sa1= NULL;
992         ScrArea *sa2= NULL;
993         ScrArea *sa3= NULL;
994         int dir = -1;
995         int dira = -1;
996
997         wmSubWindowSet(win, win->screen->mainwin);
998         
999         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1000                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1001                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1002                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H|AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1003                 drawscredge_area(sa, win->sizex, win->sizey, 0);
1004         }
1005         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1006                 drawscredge_area(sa, win->sizex, win->sizey, 1);
1007         
1008         /* blended join arrow */
1009         if (sa1 && sa2) {
1010                 dir = area_getorientation(sa1, sa2);
1011                 if (dir >= 0) {
1012                         switch(dir) {
1013                                 case 0: /* W */
1014                                         dir = 'r';
1015                                         dira = 'l';
1016                                         break;
1017                                 case 1: /* N */
1018                                         dir = 'd';
1019                                         dira = 'u';
1020                                         break;
1021                                 case 2: /* E */
1022                                         dir = 'l';
1023                                         dira = 'r';
1024                                         break;
1025                                 case 3: /* S */
1026                                         dir = 'u';
1027                                         dira = 'd';
1028                                         break;
1029                         }
1030                 }
1031                 scrarea_draw_shape_dark(sa2, dir);
1032                 scrarea_draw_shape_light(sa1, dira);
1033         }
1034         
1035         /* splitpoint */
1036         if(sa3) {
1037                 glEnable(GL_BLEND);
1038                 glColor4ub(255, 255, 255, 100);
1039                 
1040                 if(sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1041                         sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1042                         glColor4ub(0, 0, 0, 100);
1043                         sdrawline(sa3->totrct.xmin, win->eventstate->y+1, sa3->totrct.xmax, win->eventstate->y+1);
1044                 }
1045                 else {
1046                         sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1047                         glColor4ub(0, 0, 0, 100);
1048                         sdrawline(win->eventstate->x+1, sa3->totrct.ymin, win->eventstate->x+1, sa3->totrct.ymax);
1049                 }
1050                 
1051                 glDisable(GL_BLEND);
1052         }
1053         
1054         win->screen->do_draw= 0;
1055 }
1056
1057 /* helper call for below, dpi changes headers */
1058 static void screen_refresh_headersizes(void)
1059 {
1060         const ListBase *lb= BKE_spacetypes_list();
1061         SpaceType *st;
1062         
1063         for(st= lb->first; st; st= st->next) {
1064                 ARegionType *art= BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1065                 if(art) art->prefsizey= ED_area_headersize();
1066         }               
1067 }
1068
1069 /* make this screen usable */
1070 /* for file read and first use, for scaling window, area moves */
1071 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1072 {       
1073         /* exception for bg mode, we only need the screen context */
1074         if (!G.background) {
1075                 ScrArea *sa;
1076                 rcti winrct;
1077         
1078                 winrct.xmin= 0;
1079                 winrct.xmax= win->sizex-1;
1080                 winrct.ymin= 0;
1081                 winrct.ymax= win->sizey-1;
1082                 
1083                 screen_test_scale(win->screen, win->sizex, win->sizey);
1084                 
1085                 if(win->screen->mainwin==0)
1086                         win->screen->mainwin= wm_subwindow_open(win, &winrct);
1087                 else
1088                         wm_subwindow_position(win, win->screen->mainwin, &winrct);
1089                 
1090                 /* header size depends on DPI, let's verify */
1091                 screen_refresh_headersizes();
1092                 
1093                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1094                         /* set spacetype and region callbacks, calls init() */
1095                         /* sets subwindows for regions, adds handlers */
1096                         ED_area_initialize(wm, win, sa);
1097                 }
1098         
1099                 /* wake up animtimer */
1100                 if(win->screen->animtimer)
1101                         WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1102         }
1103
1104         if(G.f & G_DEBUG) printf("set screen\n");
1105         win->screen->do_refresh= 0;
1106
1107         win->screen->context= ed_screen_context;
1108 }
1109
1110 /* file read, set all screens, ... */
1111 void ED_screens_initialize(wmWindowManager *wm)
1112 {
1113         wmWindow *win;
1114         
1115         for(win= wm->windows.first; win; win= win->next) {
1116                 
1117                 if(win->screen==NULL)
1118                         win->screen= G.main->screen.first;
1119                 
1120                 ED_screen_refresh(wm, win);
1121         }
1122 }
1123
1124
1125 /* *********** exit calls are for closing running stuff ******** */
1126
1127 void ED_region_exit(bContext *C, ARegion *ar)
1128 {
1129         ARegion *prevar= CTX_wm_region(C);
1130
1131         CTX_wm_region_set(C, ar);
1132         WM_event_remove_handlers(C, &ar->handlers);
1133         if(ar->swinid)
1134                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1135         ar->swinid= 0;
1136         
1137         if(ar->headerstr)
1138                 MEM_freeN(ar->headerstr);
1139         ar->headerstr= NULL;
1140         
1141         CTX_wm_region_set(C, prevar);
1142 }
1143
1144 void ED_area_exit(bContext *C, ScrArea *sa)
1145 {
1146         ScrArea *prevsa= CTX_wm_area(C);
1147         ARegion *ar;
1148
1149         if (sa->spacetype == SPACE_FILE) {
1150                 SpaceLink *sl= sa->spacedata.first;
1151                 if(sl && sl->spacetype == SPACE_FILE) {
1152                         ED_fileselect_exit(C, (SpaceFile *)sl);
1153                 }
1154         }
1155
1156         CTX_wm_area_set(C, sa);
1157         for(ar= sa->regionbase.first; ar; ar= ar->next)
1158                 ED_region_exit(C, ar);
1159
1160         WM_event_remove_handlers(C, &sa->handlers);
1161         CTX_wm_area_set(C, prevsa);
1162 }
1163
1164 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1165 {
1166         wmWindowManager *wm= CTX_wm_manager(C);
1167         wmWindow *prevwin= CTX_wm_window(C);
1168         ScrArea *sa;
1169         ARegion *ar;
1170
1171         CTX_wm_window_set(C, window);
1172         
1173         if(screen->animtimer)
1174                 WM_event_remove_timer(wm, window, screen->animtimer);
1175         screen->animtimer= NULL;
1176         
1177         if(screen->mainwin)
1178                 wm_subwindow_close(window, screen->mainwin);
1179         screen->mainwin= 0;
1180         screen->subwinactive= 0;
1181         
1182         for(ar= screen->regionbase.first; ar; ar= ar->next)
1183                 ED_region_exit(C, ar);
1184
1185         for(sa= screen->areabase.first; sa; sa= sa->next)
1186                 ED_area_exit(C, sa);
1187
1188         /* mark it available for use for other windows */
1189         screen->winid= 0;
1190         
1191         if (prevwin->screen->temp == 0) {
1192                 /* use previous window if possible */
1193                 CTX_wm_window_set(C, prevwin);
1194         } else {
1195                 /* none otherwise */
1196                 CTX_wm_window_set(C, NULL);
1197         }
1198         
1199 }
1200
1201 /* *********************************** */
1202
1203 /* case when on area-edge or in azones, or outside window */
1204 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1205 {
1206         AZone *az= NULL;
1207         ScrArea *sa;
1208         
1209         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1210                 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1211                         break;
1212         
1213         if(sa) {
1214                 if(az->type==AZONE_AREA)
1215                         WM_cursor_set(win, CURSOR_EDIT);
1216                 else if(az->type==AZONE_REGION) {
1217                         if(az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1218                                 WM_cursor_set(win, CURSOR_X_MOVE);
1219                         else
1220                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1221                 }
1222         }
1223         else {
1224                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1225                 
1226                 if (actedge) {
1227                         if(scredge_is_horizontal(actedge))
1228                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1229                         else
1230                                 WM_cursor_set(win, CURSOR_X_MOVE);
1231                 }
1232                 else
1233                         WM_cursor_set(win, CURSOR_STD);
1234         } 
1235 }
1236
1237
1238 /* called in wm_event_system.c. sets state vars in screen, cursors */
1239 /* event type is mouse move */
1240 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1241 {
1242         wmWindow *win= CTX_wm_window(C);
1243         
1244         if(win->screen) {
1245                 bScreen *scr= win->screen;
1246                 ScrArea *sa;
1247                 ARegion *ar;
1248                 int oldswin= scr->subwinactive;
1249
1250                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1251                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1252                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1253                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1254                                                 break;
1255                 }
1256                 if(sa) {
1257                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1258                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1259                                         scr->subwinactive= ar->swinid;
1260                         }
1261                 }
1262                 else
1263                         scr->subwinactive= scr->mainwin;
1264                 
1265                 /* check for redraw headers */
1266                 if(oldswin!=scr->subwinactive) {
1267
1268                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1269                                 int do_draw= 0;
1270                                 
1271                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1272                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1273                                                 do_draw= 1;
1274                                 
1275                                 if(do_draw) {
1276                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1277                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1278                                                         ED_region_tag_redraw(ar);
1279                                 }
1280                         }
1281                 }
1282                 
1283                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1284                 if(scr->subwinactive==scr->mainwin) {
1285                         screen_cursor_set(win, event);
1286                 }
1287                 else if(oldswin!=scr->subwinactive) {
1288                         region_cursor_set(win, scr->subwinactive);
1289                         WM_event_add_notifier(C, NC_SCREEN|ND_SUBWINACTIVE, scr);
1290                 }
1291         }
1292 }
1293
1294 int ED_screen_area_active(const bContext *C)
1295 {
1296         wmWindow *win= CTX_wm_window(C);
1297         bScreen *sc= CTX_wm_screen(C);
1298         ScrArea *sa= CTX_wm_area(C);
1299
1300         if(win && sc && sa) {
1301                 AZone *az= is_in_area_actionzone(sa, win->eventstate->x, win->eventstate->y);
1302                 ARegion *ar;
1303                 
1304                 if (az && az->type == AZONE_REGION)
1305                         return 1;
1306                 
1307                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1308                         if(ar->swinid == sc->subwinactive)
1309                                 return 1;
1310         }       
1311         return 0;
1312 }
1313
1314 /* operator call, WM + Window + screen already existed before */
1315 /* Do NOT call in area/region queues! */
1316 void ED_screen_set(bContext *C, bScreen *sc)
1317 {
1318         wmWindowManager *wm= CTX_wm_manager(C);
1319         wmWindow *win= CTX_wm_window(C);
1320         bScreen *oldscreen= CTX_wm_screen(C);
1321         ID *id;
1322         
1323         /* validate screen, it's called with notifier reference */
1324         for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1325                 if(sc == (bScreen *)id)
1326                         break;
1327         if(id==NULL) 
1328                 return;
1329         
1330         /* check for valid winid */
1331         if(sc->winid!=0 && sc->winid!=win->winid)
1332                 return;
1333         
1334         if(sc->full) {                          /* find associated full */
1335                 bScreen *sc1;
1336                 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1337                         ScrArea *sa= sc1->areabase.first;
1338                         if(sa->full==sc) {
1339                                 sc= sc1;
1340                                 break;
1341                         }
1342                 }
1343         }
1344         
1345         if (oldscreen != sc) {
1346                 wmTimer *wt= oldscreen->animtimer;
1347                 ScrArea *sa;
1348
1349                 /* remove handlers referencing areas in old screen */
1350                 for(sa = oldscreen->areabase.first; sa; sa = sa->next) {
1351                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1352                 }
1353
1354                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1355                 oldscreen->animtimer= NULL;
1356                 if(wt)
1357                         WM_event_timer_sleep(wm, win, wt, 1);
1358                 
1359                 ED_screen_exit(C, win, oldscreen);
1360                 oldscreen->animtimer= wt;
1361                 
1362                 win->screen= sc;
1363                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1364                 
1365                 /* prevent multiwin errors */
1366                 sc->winid= win->winid;
1367                 
1368                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1369                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1370                 WM_event_add_notifier(C, NC_SCREEN|ND_SCREENSET, sc);
1371                 
1372                 /* makes button hilites work */
1373                 WM_event_add_mousemove(C);
1374         }
1375 }
1376
1377 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1378 {
1379         wmWindow *win;
1380
1381         for(win=wm->windows.first; win; win=win->next)
1382                 if(win->screen == sc)
1383                         return 1;
1384         
1385         return 0;
1386 }
1387
1388 /* only call outside of area/region loops */
1389 void ED_screen_delete(bContext *C, bScreen *sc)
1390 {
1391         Main *bmain= CTX_data_main(C);
1392         wmWindowManager *wm= CTX_wm_manager(C);
1393         wmWindow *win= CTX_wm_window(C);
1394         bScreen *newsc;
1395         int delete= 1;
1396         
1397         /* don't allow deleting temp fullscreens for now */
1398         if (sc->full == SCREENFULL) {
1399                 return;
1400         }
1401         
1402                 
1403         /* screen can only be in use by one window at a time, so as
1404          * long as we are able to find a screen that is unused, we
1405          * can safely assume ours is not in use anywhere an delete it */
1406
1407         for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1408                 if(!ed_screen_used(wm, newsc))
1409                         break;
1410         
1411         if(!newsc) {
1412                 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1413                         if(!ed_screen_used(wm, newsc))
1414                                 break;
1415         }
1416
1417         if(!newsc)
1418                 return;
1419
1420         ED_screen_set(C, newsc);
1421
1422         if(delete && win->screen != sc)
1423                 free_libblock(&bmain->screen, sc);
1424 }
1425
1426 /* only call outside of area/region loops */
1427 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1428 {
1429         Main *bmain= CTX_data_main(C);
1430         bScreen *sc;
1431
1432         if(screen == NULL)
1433                 return;
1434         
1435         if(ed_screen_used(CTX_wm_manager(C), screen))
1436                 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
1437
1438         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1439                 if((U.flag & USER_SCENEGLOBAL) || sc==screen) {
1440                         
1441                         if(scene != sc->scene) {
1442                                 /* all areas endlocalview */
1443                         // XXX  ScrArea *sa= sc->areabase.first;
1444                         //      while(sa) {
1445                         //              endlocalview(sa);
1446                         //              sa= sa->next;
1447                         //      }               
1448                                 sc->scene= scene;
1449                         }
1450                         
1451                 }
1452         }
1453         
1454         //  copy_view3d_lock(0);        /* space.c */
1455         
1456         /* are there cameras in the views that are not in the scene? */
1457         for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1458                 if( (U.flag & USER_SCENEGLOBAL) || sc==screen) {
1459                         ScrArea *sa= sc->areabase.first;
1460                         while(sa) {
1461                                 SpaceLink *sl= sa->spacedata.first;
1462                                 while(sl) {
1463                                         if(sl->spacetype==SPACE_VIEW3D) {
1464                                                 View3D *v3d= (View3D*) sl;
1465
1466                                                 BKE_screen_view3d_sync(v3d, scene);
1467
1468                                                 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1469                                                         v3d->camera= scene_find_camera(sc->scene);
1470                                                         // XXX if (sc==curscreen) handle_view3d_lock();
1471                                                         if (!v3d->camera) {
1472                                                                 ARegion *ar;
1473                                                                 for(ar=v3d->regionbase.first; ar; ar= ar->next) {
1474                                                                         if(ar->regiontype == RGN_TYPE_WINDOW) {
1475                                                                                 RegionView3D *rv3d= ar->regiondata;
1476
1477                                                                                 if(rv3d->persp==RV3D_CAMOB)
1478                                                                                         rv3d->persp= RV3D_PERSP;
1479                                                                         }
1480                                                                 }
1481                                                         }
1482                                                 }
1483                                         }
1484                                         sl= sl->next;
1485                                 }
1486                                 sa= sa->next;
1487                         }
1488                 }
1489         }
1490         
1491         CTX_data_scene_set(C, scene);
1492         set_scene_bg(bmain, scene);
1493         
1494         ED_render_engine_changed(bmain);
1495         ED_update_for_newframe(bmain, scene, screen, 1);
1496         
1497         /* complete redraw */
1498         WM_event_add_notifier(C, NC_WINDOW, NULL);
1499         
1500 }
1501
1502 /* only call outside of area/region loops */
1503 void ED_screen_delete_scene(bContext *C, Scene *scene)
1504 {
1505         Main *bmain= CTX_data_main(C);
1506         Scene *newscene;
1507
1508         if(scene->id.prev)
1509                 newscene= scene->id.prev;
1510         else if(scene->id.next)
1511                 newscene= scene->id.next;
1512         else
1513                 return;
1514
1515         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1516
1517         unlink_scene(bmain, scene, newscene);
1518 }
1519
1520 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1521 {
1522         wmWindow *win= CTX_wm_window(C);
1523         bScreen *screen= CTX_wm_screen(C);
1524         ScrArea *newsa= NULL;
1525
1526         if(!sa || sa->full==NULL) {
1527                 newsa= ED_screen_full_toggle(C, win, sa);
1528         }
1529         
1530         if(!newsa) {
1531                 if (sa->full) {
1532                         /* if this has been called from the temporary info header generated in
1533                          * temp fullscreen layouts, find the correct fullscreen area to change
1534                          * to create a new space inside */
1535                         for (newsa = screen->areabase.first; newsa; newsa=newsa->next) {
1536                                 if (!(sa->flag & AREA_TEMP_INFO))
1537                                         break;
1538                         }
1539                 } else
1540                         newsa= sa;
1541         }
1542         
1543         ED_area_newspace(C, newsa, type);
1544         
1545         return newsa;
1546 }
1547
1548 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1549 {
1550         wmWindow *win= CTX_wm_window(C);
1551
1552         ED_area_prevspace(C, sa);
1553         
1554         if(sa->full)
1555                 ED_screen_full_toggle(C, win, sa);
1556 }
1557
1558 /* restore a screen / area back to default operation, after temp fullscreen modes */
1559 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1560 {
1561         wmWindow *win= CTX_wm_window(C);
1562         SpaceLink *sl = sa->spacedata.first;
1563         
1564         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1565          * overlaid on top of a existing setup) then return to the previous space */
1566         
1567         if (sl->next) {
1568                 /* specific checks for space types */
1569
1570                 int sima_restore = 0;
1571
1572                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1573                 if (sl->spacetype == SPACE_IMAGE) {
1574                         SpaceImage *sima= sa->spacedata.first;
1575                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1576                                 sima_restore = 1;
1577                 }
1578
1579                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1580                         SpaceImage *sima= sa->spacedata.first;
1581                         if (sima->flag & SI_PREVSPACE)
1582                                 sima->flag &= ~SI_PREVSPACE;
1583                         if (sima->flag & SI_FULLWINDOW) {
1584                                 sima->flag &= ~SI_FULLWINDOW;
1585                                 ED_screen_full_prevspace(C, sa);
1586                         }
1587                 } else if (sl->spacetype == SPACE_FILE) {
1588                         ED_screen_full_prevspace(C, sa);
1589                 } else
1590                         ED_screen_full_toggle(C, win, sa);
1591         }
1592         /* otherwise just tile the area again */
1593         else {
1594                 ED_screen_full_toggle(C, win, sa);
1595         }
1596 }
1597
1598 /* this function toggles: if area is full then the parent will be restored */
1599 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1600 {
1601         bScreen *sc, *oldscreen;
1602         ARegion *ar;
1603
1604         if(sa) {
1605                 /* ensure we don't have a button active anymore, can crash when
1606                  * switching screens with tooltip open because region and tooltip
1607                  * are no longer in the same screen */
1608                 for(ar=sa->regionbase.first; ar; ar=ar->next)
1609                         uiFreeBlocks(C, &ar->uiblocks);
1610                 
1611                 /* prevent hanging header prints */
1612                 ED_area_headerprint(sa, NULL);
1613         }
1614
1615         if(sa && sa->full) {
1616                 ScrArea *old;
1617                 /*short fulltype;*/ /*UNUSED*/
1618
1619                 sc= sa->full;           /* the old screen to restore */
1620                 oldscreen= win->screen; /* the one disappearing */
1621
1622                 /*fulltype = sc->full;*/
1623                 sc->full= 0;
1624
1625                 /* removed: SCREENAUTOPLAY exception here */
1626         
1627                 /* find old area */
1628                 for(old= sc->areabase.first; old; old= old->next)
1629                         if(old->full) break;
1630                 if(old==NULL) {
1631                         if (G.f & G_DEBUG)
1632                                 printf("something wrong in areafullscreen\n");
1633                         return NULL;
1634                 }
1635
1636                 area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
1637                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1638                 old->full= NULL;
1639
1640                 /* animtimer back */
1641                 sc->animtimer= oldscreen->animtimer;
1642                 oldscreen->animtimer= NULL;
1643
1644                 ED_screen_set(C, sc);
1645
1646                 free_screen(oldscreen);
1647                 free_libblock(&CTX_data_main(C)->screen, oldscreen);
1648
1649         }
1650         else {
1651                 ScrArea *newa;
1652                 char newname[MAX_ID_NAME-2];
1653
1654                 oldscreen= win->screen;
1655
1656                 /* nothing wrong with having only 1 area, as far as I can see...
1657                  * is there only 1 area? */
1658 #if 0
1659                 if(oldscreen->areabase.first==oldscreen->areabase.last)
1660                         return NULL;
1661 #endif
1662
1663                 oldscreen->full = SCREENFULL;
1664                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name+2, "full");
1665                 sc= ED_screen_add(win, oldscreen->scene, newname);
1666                 sc->full = SCREENFULL; // XXX
1667
1668                 /* timer */
1669                 sc->animtimer= oldscreen->animtimer;
1670                 oldscreen->animtimer= NULL;
1671
1672                 /* returns the top small area */
1673                 newa= area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1674                 ED_area_newspace(C, newa, SPACE_INFO);
1675
1676                 /* use random area when we have no active one, e.g. when the
1677                  * mouse is outside of the window and we open a file browser */
1678                 if(!sa)
1679                         sa= oldscreen->areabase.first;
1680
1681                 /* copy area */
1682                 newa= newa->prev;
1683                 area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
1684                 sa->flag |= AREA_TEMP_INFO;
1685
1686                 sa->full= oldscreen;
1687                 newa->full= oldscreen;
1688                 newa->next->full= oldscreen; // XXX
1689
1690                 ED_screen_set(C, sc);
1691         }
1692
1693         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1694         CTX_wm_area_set(C, sc->areabase.first);
1695
1696         return sc->areabase.first;
1697 }
1698
1699 /* update frame rate info for viewport drawing */
1700 void ED_refresh_viewport_fps(bContext *C)
1701 {
1702         wmTimer *animtimer= CTX_wm_screen(C)->animtimer;
1703         Scene *scene= CTX_data_scene(C);
1704         
1705         /* is anim playback running? */
1706         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1707                 ScreenFrameRateInfo *fpsi= scene->fps_info;
1708                 
1709                 /* if there isn't any info, init it first */
1710                 if (fpsi == NULL)
1711                         fpsi= scene->fps_info= MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1712                 
1713                 /* update the values */
1714                 fpsi->redrawtime= fpsi->lredrawtime;
1715                 fpsi->lredrawtime= animtimer->ltime;
1716         }
1717         else {  
1718                 /* playback stopped or shouldn't be running */
1719                 if (scene->fps_info)
1720                         MEM_freeN(scene->fps_info);
1721                 scene->fps_info= NULL;
1722         }
1723 }
1724
1725 /* redraws: uses defines from stime->redraws 
1726  * enable: 1 - forward on, -1 - backwards on, 0 - off
1727  */
1728 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1729 {
1730         bScreen *screen= CTX_wm_screen(C);
1731         wmWindowManager *wm= CTX_wm_manager(C);
1732         wmWindow *win= CTX_wm_window(C);
1733         Scene *scene= CTX_data_scene(C);
1734         
1735         if(screen->animtimer)
1736                 WM_event_remove_timer(wm, win, screen->animtimer);
1737         screen->animtimer= NULL;
1738         
1739         if(enable) {
1740                 ScreenAnimData *sad= MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1741                 
1742                 screen->animtimer= WM_event_add_timer(wm, win, TIMER0, (1.0/FPS));
1743                 
1744                 sad->ar= CTX_wm_region(C);
1745                 /* if startframe is larger than current frame, we put currentframe on startframe.
1746                    note: first frame then is not drawn! (ton) */
1747                 if(scene->r.sfra > scene->r.cfra) {
1748                         sad->sfra= scene->r.cfra;
1749                         scene->r.cfra= scene->r.sfra;
1750                 }
1751                 else
1752                         sad->sfra = scene->r.cfra;
1753                 sad->redraws= redraws;
1754                 sad->refresh= refresh;
1755                 sad->flag |= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1756                 sad->flag |= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1757                 
1758                 screen->animtimer->customdata= sad;
1759                 
1760         }
1761         /* notifier catched by top header, for button */
1762         WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1763 }
1764
1765 /* helper for screen_animation_play() - only to be used for TimeLine */
1766 static ARegion *time_top_left_3dwindow(bScreen *screen)
1767 {
1768         ARegion *aret= NULL;
1769         ScrArea *sa;
1770         int min= 10000;
1771         
1772         for(sa= screen->areabase.first; sa; sa= sa->next) {
1773                 if(sa->spacetype==SPACE_VIEW3D) {
1774                         ARegion *ar;
1775                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1776                                 if(ar->regiontype==RGN_TYPE_WINDOW) {
1777                                         if(ar->winrct.xmin - ar->winrct.ymin < min) {
1778                                                 aret= ar;
1779                                                 min= ar->winrct.xmin - ar->winrct.ymin;
1780                                         }
1781                                 }
1782                         }
1783                 }
1784         }
1785
1786         return aret;
1787 }
1788
1789 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1790 {
1791         if(screen && screen->animtimer) {
1792                 wmTimer *wt= screen->animtimer;
1793                 ScreenAnimData *sad= wt->customdata;
1794                 
1795                 sad->redraws= redraws;
1796                 sad->refresh= refresh;
1797                 sad->ar= NULL;
1798                 if(redraws & TIME_REGION)
1799                         sad->ar= time_top_left_3dwindow(screen);
1800         }
1801 }
1802
1803 /* results in fully updated anim system
1804  * screen can be NULL */
1805 void ED_update_for_newframe(Main *bmain, Scene *scene, bScreen *screen, int UNUSED(mute))
1806 {       
1807 #ifdef DURIAN_CAMERA_SWITCH
1808         void *camera= scene_camera_switch_find(scene);
1809         if(camera && scene->camera != camera) {
1810                 bScreen *sc;
1811                 scene->camera= camera;
1812                 /* are there cameras in the views that are not in the scene? */
1813                 for(sc= bmain->screen.first; sc; sc= sc->id.next) {
1814                         BKE_screen_view3d_scene_sync(sc);
1815                 }
1816         }
1817 #endif
1818
1819         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1820         
1821         /* update animated image textures for gpu, etc,
1822          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
1823         ED_image_update_frame(bmain, scene->r.cfra);
1824
1825         ED_clip_update_frame(bmain, scene->r.cfra);
1826
1827         /* this function applies the changes too */
1828         /* XXX future: do all windows */
1829         scene_update_for_newframe(bmain, scene, BKE_screen_visible_layers(screen, scene)); /* BKE_scene.h */
1830         
1831         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1832         //      audiostream_scrub( CFRA );
1833         
1834         /* 3d window, preview */
1835         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1836         
1837         /* all movie/sequence images */
1838         //BIF_image_update_frame();
1839         
1840         /* composite */
1841         if(scene->use_nodes && scene->nodetree)
1842                 ntreeCompositTagAnimated(scene->nodetree);
1843         
1844         /* update animated texture nodes */
1845         {
1846                 Tex *tex;
1847                 for(tex= bmain->tex.first; tex; tex= tex->id.next)
1848                         if( tex->use_nodes && tex->nodetree ) {
1849                                 ntreeTexTagAnimated( tex->nodetree );
1850                         }
1851         }
1852         
1853 }
1854
1855