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