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