60aad14efcf1c3b121d037336e53ab3cf1a33542
[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_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_context.h"
44 #include "BKE_depsgraph.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 #include "ED_clip.h"
64 #include "ED_render.h"
65
66 #include "UI_interface.h"
67
68 /* XXX actually should be not here... solve later */
69 #include "wm_subwindow.h"
70
71 #include "screen_intern.h"  /* own module include */
72
73
74 /* ******************* screen vert, edge, area managing *********************** */
75
76 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
77 {
78         ScrVert *sv = MEM_callocN(sizeof(ScrVert), "addscrvert");
79         sv->vec.x = x;
80         sv->vec.y = y;
81         
82         BLI_addtail(&sc->vertbase, sv);
83         return sv;
84 }
85
86 static void sortscrvert(ScrVert **v1, ScrVert **v2)
87 {
88         ScrVert *tmp;
89         
90         if (*v1 > *v2) {
91                 tmp = *v1;
92                 *v1 = *v2;
93                 *v2 = tmp;
94         }
95 }
96
97 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
98 {
99         ScrEdge *se = MEM_callocN(sizeof(ScrEdge), "addscredge");
100         
101         sortscrvert(&v1, &v2);
102         se->v1 = v1;
103         se->v2 = v2;
104         
105         BLI_addtail(&sc->edgebase, se);
106         return se;
107 }
108
109
110 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
111 {
112         ScrEdge *se;
113         
114         sortscrvert(&v1, &v2);
115         for (se = sc->edgebase.first; se; se = se->next)
116                 if (se->v1 == v1 && se->v2 == v2)
117                         return se;
118         
119         return NULL;
120 }
121
122 void removedouble_scrverts(bScreen *sc)
123 {
124         ScrVert *v1, *verg;
125         ScrEdge *se;
126         ScrArea *sa;
127         
128         verg = sc->vertbase.first;
129         while (verg) {
130                 if (verg->newv == NULL) { /* !!! */
131                         v1 = verg->next;
132                         while (v1) {
133                                 if (v1->newv == NULL) {   /* !?! */
134                                         if (v1->vec.x == verg->vec.x && v1->vec.y == verg->vec.y) {
135                                                 /* printf("doublevert\n"); */
136                                                 v1->newv = verg;
137                                         }
138                                 }
139                                 v1 = v1->next;
140                         }
141                 }
142                 verg = verg->next;
143         }
144
145         /* replace pointers in edges and faces */
146         se = sc->edgebase.first;
147         while (se) {
148                 if (se->v1->newv) se->v1 = se->v1->newv;
149                 if (se->v2->newv) se->v2 = se->v2->newv;
150                 /* edges changed: so.... */
151                 sortscrvert(&(se->v1), &(se->v2));
152                 se = se->next;
153         }
154         sa = sc->areabase.first;
155         while (sa) {
156                 if (sa->v1->newv) sa->v1 = sa->v1->newv;
157                 if (sa->v2->newv) sa->v2 = sa->v2->newv;
158                 if (sa->v3->newv) sa->v3 = sa->v3->newv;
159                 if (sa->v4->newv) sa->v4 = sa->v4->newv;
160                 sa = sa->next;
161         }
162
163         /* remove */
164         verg = sc->vertbase.first;
165         while (verg) {
166                 v1 = verg->next;
167                 if (verg->newv) {
168                         BLI_remlink(&sc->vertbase, verg);
169                         MEM_freeN(verg);
170                 }
171                 verg = v1;
172         }
173
174 }
175
176 void removenotused_scrverts(bScreen *sc)
177 {
178         ScrVert *sv, *svn;
179         ScrEdge *se;
180         
181         /* we assume edges are ok */
182         
183         se = sc->edgebase.first;
184         while (se) {
185                 se->v1->flag = 1;
186                 se->v2->flag = 1;
187                 se = se->next;
188         }
189         
190         sv = sc->vertbase.first;
191         while (sv) {
192                 svn = sv->next;
193                 if (sv->flag == 0) {
194                         BLI_remlink(&sc->vertbase, sv);
195                         MEM_freeN(sv);
196                 }
197                 else sv->flag = 0;
198                 sv = svn;
199         }
200 }
201
202 void removedouble_scredges(bScreen *sc)
203 {
204         ScrEdge *verg, *se, *sn;
205         
206         /* compare */
207         verg = sc->edgebase.first;
208         while (verg) {
209                 se = verg->next;
210                 while (se) {
211                         sn = se->next;
212                         if (verg->v1 == se->v1 && verg->v2 == se->v2) {
213                                 BLI_remlink(&sc->edgebase, se);
214                                 MEM_freeN(se);
215                         }
216                         se = sn;
217                 }
218                 verg = verg->next;
219         }
220 }
221
222 void removenotused_scredges(bScreen *sc)
223 {
224         ScrEdge *se, *sen;
225         ScrArea *sa;
226         int a = 0;
227         
228         /* sets flags when edge is used in area */
229         sa = sc->areabase.first;
230         while (sa) {
231                 se = screen_findedge(sc, sa->v1, sa->v2);
232                 if (se == NULL) printf("error: area %d edge 1 doesn't exist\n", a);
233                 else se->flag = 1;
234                 se = screen_findedge(sc, sa->v2, sa->v3);
235                 if (se == NULL) printf("error: area %d edge 2 doesn't exist\n", a);
236                 else se->flag = 1;
237                 se = screen_findedge(sc, sa->v3, sa->v4);
238                 if (se == NULL) printf("error: area %d edge 3 doesn't exist\n", a);
239                 else se->flag = 1;
240                 se = screen_findedge(sc, sa->v4, sa->v1);
241                 if (se == NULL) printf("error: area %d edge 4 doesn't exist\n", a);
242                 else se->flag = 1;
243                 sa = sa->next;
244                 a++;
245         }
246         se = sc->edgebase.first;
247         while (se) {
248                 sen = se->next;
249                 if (se->flag == 0) {
250                         BLI_remlink(&sc->edgebase, se);
251                         MEM_freeN(se);
252                 }
253                 else se->flag = 0;
254                 se = sen;
255         }
256 }
257
258 int scredge_is_horizontal(ScrEdge *se)
259 {
260         return (se->v1->vec.y == se->v2->vec.y);
261 }
262
263 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
264 {
265         ScrEdge *se;
266         
267         for (se = sc->edgebase.first; se; se = se->next) {
268                 if (scredge_is_horizontal(se)) {
269                         short min, max;
270                         min = MIN2(se->v1->vec.x, se->v2->vec.x);
271                         max = MAX2(se->v1->vec.x, se->v2->vec.x);
272                         
273                         if (abs(my - se->v1->vec.y) <= 2 && mx >= min && mx <= max)
274                                 return se;
275                 }
276                 else {
277                         short min, max;
278                         min = MIN2(se->v1->vec.y, se->v2->vec.y);
279                         max = MAX2(se->v1->vec.y, se->v2->vec.y);
280                         
281                         if (abs(mx - se->v1->vec.x) <= 2 && my >= min && my <= max)
282                                 return se;
283                 }
284         }
285         
286         return NULL;
287 }
288
289
290
291 /* adds no space data */
292 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
293 {
294         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
295         sa->v1 = v1;
296         sa->v2 = v2;
297         sa->v3 = v3;
298         sa->v4 = v4;
299         sa->headertype = headertype;
300         sa->spacetype = sa->butspacetype = spacetype;
301         
302         BLI_addtail(&sc->areabase, sa);
303         
304         return sa;
305 }
306
307 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
308 {
309         
310         ED_area_exit(C, sa);
311         
312         BKE_screen_area_free(sa);
313         
314         BLI_remlink(&sc->areabase, sa);
315         MEM_freeN(sa);
316 }
317
318 /* return 0: no split possible */
319 /* else return (integer) screencoordinate split point */
320 static short testsplitpoint(ScrArea *sa, char dir, float fac)
321 {
322         short x, y;
323         
324         // area big enough?
325         if (dir == 'v' && (sa->v4->vec.x - sa->v1->vec.x <= 2 * AREAMINX)) return 0;
326         if (dir == 'h' && (sa->v2->vec.y - sa->v1->vec.y <= 2 * AREAMINY)) return 0;
327         
328         // to be sure
329         CLAMP(fac, 0.0f, 1.0f);
330         
331         if (dir == 'h') {
332                 y = sa->v1->vec.y + fac * (sa->v2->vec.y - sa->v1->vec.y);
333                 
334                 if (y - sa->v1->vec.y < AREAMINY)
335                         y = sa->v1->vec.y + AREAMINY;
336                 else if (sa->v2->vec.y - y < AREAMINY)
337                         y = sa->v2->vec.y - AREAMINY;
338                 else y -= (y % AREAGRID);
339                 
340                 return y;
341         }
342         else {
343                 x = sa->v1->vec.x + fac * (sa->v4->vec.x - sa->v1->vec.x);
344                 
345                 if (x - sa->v1->vec.x < AREAMINX)
346                         x = sa->v1->vec.x + AREAMINX;
347                 else if (sa->v4->vec.x - x < AREAMINX)
348                         x = sa->v4->vec.x - AREAMINX;
349                 else x -= (x % AREAGRID);
350                 
351                 return x;
352         }
353 }
354
355 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
356 {
357         ScrArea *newa = NULL;
358         ScrVert *sv1, *sv2;
359         short split;
360         
361         if (sa == NULL) return NULL;
362         
363         split = testsplitpoint(sa, dir, fac);
364         if (split == 0) return NULL;
365         
366         if (dir == 'h') {
367                 /* new vertices */
368                 sv1 = screen_addvert(sc, sa->v1->vec.x, split);
369                 sv2 = screen_addvert(sc, sa->v4->vec.x, split);
370                 
371                 /* new edges */
372                 screen_addedge(sc, sa->v1, sv1);
373                 screen_addedge(sc, sv1, sa->v2);
374                 screen_addedge(sc, sa->v3, sv2);
375                 screen_addedge(sc, sv2, sa->v4);
376                 screen_addedge(sc, sv1, sv2);
377                 
378                 /* new areas: top */
379                 newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
380                 area_copy_data(newa, sa, 0);
381                 
382                 /* area below */
383                 sa->v2 = sv1;
384                 sa->v3 = sv2;
385                 
386         }
387         else {
388                 /* new vertices */
389                 sv1 = screen_addvert(sc, split, sa->v1->vec.y);
390                 sv2 = screen_addvert(sc, split, sa->v2->vec.y);
391                 
392                 /* new edges */
393                 screen_addedge(sc, sa->v1, sv1);
394                 screen_addedge(sc, sv1, sa->v4);
395                 screen_addedge(sc, sa->v2, sv2);
396                 screen_addedge(sc, sv2, sa->v3);
397                 screen_addedge(sc, sv1, sv2);
398                 
399                 /* new areas: left */
400                 newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
401                 area_copy_data(newa, sa, 0);
402                 
403                 /* area right */
404                 sa->v1 = sv1;
405                 sa->v2 = sv2;
406         }
407         
408         /* remove double vertices en edges */
409         if (merge)
410                 removedouble_scrverts(sc);
411         removedouble_scredges(sc);
412         removenotused_scredges(sc);
413         
414         return newa;
415 }
416
417 /* empty screen, with 1 dummy area without spacedata */
418 /* uses window size */
419 bScreen *ED_screen_add(wmWindow *win, Scene *scene, const char *name)
420 {
421         bScreen *sc;
422         ScrVert *sv1, *sv2, *sv3, *sv4;
423         
424         sc = BKE_libblock_alloc(&G.main->screen, ID_SCR, name);
425         sc->scene = scene;
426         sc->do_refresh = TRUE;
427         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
428         sc->winid = win->winid;
429
430         sv1 = screen_addvert(sc, 0, 0);
431         sv2 = screen_addvert(sc, 0, win->sizey - 1);
432         sv3 = screen_addvert(sc, win->sizex - 1, win->sizey - 1);
433         sv4 = screen_addvert(sc, win->sizex - 1, 0);
434         
435         screen_addedge(sc, sv1, sv2);
436         screen_addedge(sc, sv2, sv3);
437         screen_addedge(sc, sv3, sv4);
438         screen_addedge(sc, sv4, sv1);
439         
440         /* dummy type, no spacedata */
441         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
442                 
443         return sc;
444 }
445
446 static void screen_copy(bScreen *to, bScreen *from)
447 {
448         ScrVert *s1, *s2;
449         ScrEdge *se;
450         ScrArea *sa, *saf;
451         
452         /* free contents of 'to', is from blenkernel screen.c */
453         BKE_screen_free(to);
454         
455         BLI_duplicatelist(&to->vertbase, &from->vertbase);
456         BLI_duplicatelist(&to->edgebase, &from->edgebase);
457         BLI_duplicatelist(&to->areabase, &from->areabase);
458         to->regionbase.first = to->regionbase.last = NULL;
459         
460         s2 = to->vertbase.first;
461         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
462                 s1->newv = s2;
463         }
464         
465         for (se = to->edgebase.first; se; se = se->next) {
466                 se->v1 = se->v1->newv;
467                 se->v2 = se->v2->newv;
468                 sortscrvert(&(se->v1), &(se->v2));
469         }
470         
471         saf = from->areabase.first;
472         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
473                 sa->v1 = sa->v1->newv;
474                 sa->v2 = sa->v2->newv;
475                 sa->v3 = sa->v3->newv;
476                 sa->v4 = sa->v4->newv;
477
478                 sa->spacedata.first = sa->spacedata.last = NULL;
479                 sa->regionbase.first = sa->regionbase.last = NULL;
480                 sa->actionzones.first = sa->actionzones.last = NULL;
481                 sa->handlers.first = sa->handlers.last = NULL;
482                 
483                 area_copy_data(sa, saf, 0);
484         }
485         
486         /* put at zero (needed?) */
487         for (s1 = from->vertbase.first; s1; s1 = s1->next)
488                 s1->newv = NULL;
489
490 }
491
492
493 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
494 /* -1 = not valid check */
495 /* used with join operator */
496 int area_getorientation(ScrArea *sa, ScrArea *sb)
497 {
498         ScrVert *sav1, *sav2, *sav3, *sav4;
499         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
500
501         if (sa == NULL || sb == NULL) return -1;
502
503         sav1 = sa->v1;
504         sav2 = sa->v2;
505         sav3 = sa->v3;
506         sav4 = sa->v4;
507         sbv1 = sb->v1;
508         sbv2 = sb->v2;
509         sbv3 = sb->v3;
510         sbv4 = sb->v4;
511         
512         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
513                 return 0;
514         }
515         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
516                 return 1;
517         }
518         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
519                 return 2;
520         }
521         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
522                 return 3;
523         }
524         
525         return -1;
526 }
527
528 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
529  *  used by the split, join operators
530  */
531 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
532 {
533         int dir;
534         
535         dir = area_getorientation(sa1, sa2);
536         /*printf("dir is : %i\n", dir);*/
537         
538         if (dir < 0) {
539                 if (sa1) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
540                 if (sa2) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
541                 return 0;
542         }
543         
544         if (dir == 0) {
545                 sa1->v1 = sa2->v1;
546                 sa1->v2 = sa2->v2;
547                 screen_addedge(scr, sa1->v2, sa1->v3);
548                 screen_addedge(scr, sa1->v1, sa1->v4);
549         }
550         else if (dir == 1) {
551                 sa1->v2 = sa2->v2;
552                 sa1->v3 = sa2->v3;
553                 screen_addedge(scr, sa1->v1, sa1->v2);
554                 screen_addedge(scr, sa1->v3, sa1->v4);
555         }
556         else if (dir == 2) {
557                 sa1->v3 = sa2->v3;
558                 sa1->v4 = sa2->v4;
559                 screen_addedge(scr, sa1->v2, sa1->v3);
560                 screen_addedge(scr, sa1->v1, sa1->v4);
561         }
562         else if (dir == 3) {
563                 sa1->v1 = sa2->v1;
564                 sa1->v4 = sa2->v4;
565                 screen_addedge(scr, sa1->v1, sa1->v2);
566                 screen_addedge(scr, sa1->v3, sa1->v4);
567         }
568         
569         screen_delarea(C, scr, sa2);
570         removedouble_scrverts(scr);
571         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
572         
573         return 1;
574 }
575
576 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
577 {
578         ScrEdge *se;
579         ScrVert *sv;
580         int oneselected;
581         char dir;
582         
583         /* select connected, only in the right direction */
584         /* 'dir' is the direction of EDGE */
585         
586         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
587         else dir = 'h';
588         
589         sv = sc->vertbase.first;
590         while (sv) {
591                 sv->flag = 0;
592                 sv = sv->next;
593         }
594         
595         edge->v1->flag = 1;
596         edge->v2->flag = 1;
597         
598         oneselected = 1;
599         while (oneselected) {
600                 se = sc->edgebase.first;
601                 oneselected = 0;
602                 while (se) {
603                         if (se->v1->flag + se->v2->flag == 1) {
604                                 if (dir == 'h') {
605                                         if (se->v1->vec.y == se->v2->vec.y) {
606                                                 se->v1->flag = se->v2->flag = 1;
607                                                 oneselected = 1;
608                                         }
609                                 }
610                                 if (dir == 'v') {
611                                         if (se->v1->vec.x == se->v2->vec.x) {
612                                                 se->v1->flag = se->v2->flag = 1;
613                                                 oneselected = 1;
614                                         }
615                                 }
616                         }
617                         se = se->next;
618                 }
619         }
620 }
621
622 /* test if screen vertices should be scaled */
623 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
624 {
625         ScrVert *sv = NULL;
626         ScrArea *sa;
627         int sizex, sizey;
628         float facx, facy, tempf, min[2], max[2];
629         
630         /* calculate size */
631         min[0] = min[1] = 10000.0f;
632         max[0] = max[1] = 0.0f;
633         
634         for (sv = sc->vertbase.first; sv; sv = sv->next) {
635                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
636                 minmax_v2v2_v2(min, max, fv);
637         }
638         
639         /* always make 0.0 left under */
640         for (sv = sc->vertbase.first; sv; sv = sv->next) {
641                 sv->vec.x -= min[0];
642                 sv->vec.y -= min[1];
643         }
644         
645         sizex = max[0] - min[0];
646         sizey = max[1] - min[1];
647         
648         if (sizex != winsizex || sizey != winsizey) {
649                 facx = winsizex;
650                 facx /= (float)sizex;
651                 facy = winsizey;
652                 facy /= (float)sizey;
653                 
654                 /* make sure it fits! */
655                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
656                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
657                          * need some way to store these as floats internally and re-apply from there. */
658                         tempf = ((float)sv->vec.x) * facx;
659                         sv->vec.x = (short)(tempf + 0.5f);
660                         sv->vec.x += AREAGRID - 1;
661                         sv->vec.x -=  (sv->vec.x % AREAGRID);
662
663                         CLAMP(sv->vec.x, 0, winsizex);
664                         
665                         tempf = ((float)sv->vec.y) * facy;
666                         sv->vec.y = (short)(tempf + 0.5f);
667                         sv->vec.y += AREAGRID - 1;
668                         sv->vec.y -=  (sv->vec.y % AREAGRID);
669
670                         CLAMP(sv->vec.y, 0, winsizey);
671                 }
672         }
673         
674         /* test for collapsed areas. This could happen in some blender version... */
675         /* ton: removed option now, it needs Context... */
676         
677         /* make each window at least ED_area_headersize() high */
678         for (sa = sc->areabase.first; sa; sa = sa->next) {
679                 int headery = ED_area_headersize() + 1;
680                 
681                 if (sa->v1->vec.y + headery > sa->v2->vec.y) {
682                         /* lower edge */
683                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
684                         if (se && sa->v1 != sa->v2) {
685                                 int yval;
686                                 
687                                 select_connected_scredge(sc, se);
688                                 
689                                 /* all selected vertices get the right offset */
690                                 yval = sa->v2->vec.y - headery;
691                                 sv = sc->vertbase.first;
692                                 while (sv) {
693                                         /* if is a collapsed area */
694                                         if (sv != sa->v2 && sv != sa->v3) {
695                                                 if (sv->flag) sv->vec.y = yval;
696                                         }
697                                         sv = sv->next;
698                                 }
699                         }
700                 }
701         }
702         
703 }
704
705 /* *********************** DRAWING **************************************** */
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 (visualization 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.debug_value, 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, int swin_changed)
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 (swin_changed || (ar->type && ar->type->event_cursor)) {
952                                         if (ar->type && ar->type->cursor)
953                                                 ar->type->cursor(win, sa, ar);
954                                         else
955                                                 WM_cursor_set(win, CURSOR_STD);
956                                 }
957                                 return;
958                         }
959                 }
960         }
961 }
962
963 void ED_screen_do_listen(bContext *C, wmNotifier *note)
964 {
965         wmWindow *win = CTX_wm_window(C);
966         
967         /* generic notes */
968         switch (note->category) {
969                 case NC_WM:
970                         if (note->data == ND_FILEREAD)
971                                 win->screen->do_draw = TRUE;
972                         break;
973                 case NC_WINDOW:
974                         win->screen->do_draw = TRUE;
975                         break;
976                 case NC_SCREEN:
977                         if (note->data == ND_SUBWINACTIVE)
978                                 uiFreeActiveButtons(C, win->screen);
979                         if (note->action == NA_EDITED)
980                                 win->screen->do_draw = win->screen->do_refresh = TRUE;
981                         break;
982                 case NC_SCENE:
983                         if (note->data == ND_MODE)
984                                 region_cursor_set(win, note->swinid, TRUE);
985                         break;
986         }
987 }
988
989 /* only for edge lines between areas, and the blended join arrows */
990 void ED_screen_draw(wmWindow *win)
991 {
992         ScrArea *sa;
993         ScrArea *sa1 = NULL;
994         ScrArea *sa2 = NULL;
995         ScrArea *sa3 = NULL;
996         int dir = -1;
997         int dira = -1;
998
999         wmSubWindowSet(win, win->screen->mainwin);
1000         
1001         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1002                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1003                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1004                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1005                 drawscredge_area(sa, win->sizex, win->sizey, 0);
1006         }
1007         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1008                 drawscredge_area(sa, win->sizex, win->sizey, 1);
1009         
1010         /* blended join arrow */
1011         if (sa1 && sa2) {
1012                 dir = area_getorientation(sa1, sa2);
1013                 if (dir >= 0) {
1014                         switch (dir) {
1015                                 case 0: /* W */
1016                                         dir = 'r';
1017                                         dira = 'l';
1018                                         break;
1019                                 case 1: /* N */
1020                                         dir = 'd';
1021                                         dira = 'u';
1022                                         break;
1023                                 case 2: /* E */
1024                                         dir = 'l';
1025                                         dira = 'r';
1026                                         break;
1027                                 case 3: /* S */
1028                                         dir = 'u';
1029                                         dira = 'd';
1030                                         break;
1031                         }
1032                 }
1033                 scrarea_draw_shape_dark(sa2, dir);
1034                 scrarea_draw_shape_light(sa1, dira);
1035         }
1036         
1037         /* splitpoint */
1038         if (sa3) {
1039                 glEnable(GL_BLEND);
1040                 glColor4ub(255, 255, 255, 100);
1041                 
1042                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1043                         sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1044                         glColor4ub(0, 0, 0, 100);
1045                         sdrawline(sa3->totrct.xmin, win->eventstate->y + 1, sa3->totrct.xmax, win->eventstate->y + 1);
1046                 }
1047                 else {
1048                         sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1049                         glColor4ub(0, 0, 0, 100);
1050                         sdrawline(win->eventstate->x + 1, sa3->totrct.ymin, win->eventstate->x + 1, sa3->totrct.ymax);
1051                 }
1052                 
1053                 glDisable(GL_BLEND);
1054         }
1055         
1056         win->screen->do_draw = FALSE;
1057 }
1058
1059 /* helper call for below, dpi changes headers */
1060 static void screen_refresh_headersizes(void)
1061 {
1062         const ListBase *lb = BKE_spacetypes_list();
1063         SpaceType *st;
1064         
1065         for (st = lb->first; st; st = st->next) {
1066                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1067                 if (art) art->prefsizey = ED_area_headersize();
1068         }
1069 }
1070
1071 /* make this screen usable */
1072 /* for file read and first use, for scaling window, area moves */
1073 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1074 {       
1075         /* exception for bg mode, we only need the screen context */
1076         if (!G.background) {
1077                 ScrArea *sa;
1078                 rcti winrct;
1079         
1080                 winrct.xmin = 0;
1081                 winrct.xmax = win->sizex - 1;
1082                 winrct.ymin = 0;
1083                 winrct.ymax = win->sizey - 1;
1084                 
1085                 screen_test_scale(win->screen, win->sizex, win->sizey);
1086                 
1087                 if (win->screen->mainwin == 0)
1088                         win->screen->mainwin = wm_subwindow_open(win, &winrct);
1089                 else
1090                         wm_subwindow_position(win, win->screen->mainwin, &winrct);
1091                 
1092                 /* header size depends on DPI, let's verify */
1093                 screen_refresh_headersizes();
1094                 
1095                 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1096                         /* set spacetype and region callbacks, calls init() */
1097                         /* sets subwindows for regions, adds handlers */
1098                         ED_area_initialize(wm, win, sa);
1099                 }
1100         
1101                 /* wake up animtimer */
1102                 if (win->screen->animtimer)
1103                         WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1104         }
1105
1106         if (G.debug & G_DEBUG_EVENTS) {
1107                 printf("%s: set screen\n", __func__);
1108         }
1109         win->screen->do_refresh = FALSE;
1110
1111         win->screen->context = ed_screen_context;
1112 }
1113
1114 /* file read, set all screens, ... */
1115 void ED_screens_initialize(wmWindowManager *wm)
1116 {
1117         wmWindow *win;
1118         
1119         for (win = wm->windows.first; win; win = win->next) {
1120                 
1121                 if (win->screen == NULL)
1122                         win->screen = G.main->screen.first;
1123                 
1124                 ED_screen_refresh(wm, win);
1125         }
1126 }
1127
1128
1129 /* *********** exit calls are for closing running stuff ******** */
1130
1131 void ED_region_exit(bContext *C, ARegion *ar)
1132 {
1133         ARegion *prevar = CTX_wm_region(C);
1134
1135         CTX_wm_region_set(C, ar);
1136         WM_event_remove_handlers(C, &ar->handlers);
1137         if (ar->swinid)
1138                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1139         ar->swinid = 0;
1140         
1141         if (ar->headerstr)
1142                 MEM_freeN(ar->headerstr);
1143         ar->headerstr = NULL;
1144         
1145         CTX_wm_region_set(C, prevar);
1146 }
1147
1148 void ED_area_exit(bContext *C, ScrArea *sa)
1149 {
1150         ScrArea *prevsa = CTX_wm_area(C);
1151         ARegion *ar;
1152
1153         if (sa->spacetype == SPACE_FILE) {
1154                 SpaceLink *sl = sa->spacedata.first;
1155                 if (sl && sl->spacetype == SPACE_FILE) {
1156                         ED_fileselect_exit(C, (SpaceFile *)sl);
1157                 }
1158         }
1159         else if (sa->spacetype == SPACE_VIEW3D) {
1160                 ED_render_engine_area_exit(sa);
1161         }
1162
1163         CTX_wm_area_set(C, sa);
1164         for (ar = sa->regionbase.first; ar; ar = ar->next)
1165                 ED_region_exit(C, ar);
1166
1167         WM_event_remove_handlers(C, &sa->handlers);
1168         CTX_wm_area_set(C, prevsa);
1169 }
1170
1171 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1172 {
1173         wmWindowManager *wm = CTX_wm_manager(C);
1174         wmWindow *prevwin = CTX_wm_window(C);
1175         ScrArea *sa;
1176         ARegion *ar;
1177
1178         CTX_wm_window_set(C, window);
1179         
1180         if (screen->animtimer)
1181                 WM_event_remove_timer(wm, window, screen->animtimer);
1182         screen->animtimer = NULL;
1183         
1184         if (screen->mainwin)
1185                 wm_subwindow_close(window, screen->mainwin);
1186         screen->mainwin = 0;
1187         screen->subwinactive = 0;
1188         
1189         for (ar = screen->regionbase.first; ar; ar = ar->next)
1190                 ED_region_exit(C, ar);
1191
1192         for (sa = screen->areabase.first; sa; sa = sa->next)
1193                 ED_area_exit(C, sa);
1194
1195         /* mark it available for use for other windows */
1196         screen->winid = 0;
1197         
1198         if (prevwin->screen->temp == 0) {
1199                 /* use previous window if possible */
1200                 CTX_wm_window_set(C, prevwin);
1201         }
1202         else {
1203                 /* none otherwise */
1204                 CTX_wm_window_set(C, NULL);
1205         }
1206         
1207 }
1208
1209 /* *********************************** */
1210
1211 /* case when on area-edge or in azones, or outside window */
1212 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1213 {
1214         AZone *az = NULL;
1215         ScrArea *sa;
1216         
1217         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1218                 if ((az = is_in_area_actionzone(sa, &event->x)))
1219                         break;
1220         
1221         if (sa) {
1222                 if (az->type == AZONE_AREA)
1223                         WM_cursor_set(win, CURSOR_EDIT);
1224                 else if (az->type == AZONE_REGION) {
1225                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1226                                 WM_cursor_set(win, CURSOR_X_MOVE);
1227                         else
1228                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1229                 }
1230         }
1231         else {
1232                 ScrEdge *actedge = screen_find_active_scredge(win->screen, event->x, event->y);
1233                 
1234                 if (actedge) {
1235                         if (scredge_is_horizontal(actedge))
1236                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1237                         else
1238                                 WM_cursor_set(win, CURSOR_X_MOVE);
1239                 }
1240                 else
1241                         WM_cursor_set(win, CURSOR_STD);
1242         }
1243 }
1244
1245
1246 /* called in wm_event_system.c. sets state vars in screen, cursors */
1247 /* event type is mouse move */
1248 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1249 {
1250         wmWindow *win = CTX_wm_window(C);
1251         
1252         if (win->screen) {
1253                 bScreen *scr = win->screen;
1254                 ScrArea *sa;
1255                 ARegion *ar;
1256                 int oldswin = scr->subwinactive;
1257
1258                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1259                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1260                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1261                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1262                                                 break;
1263                 }
1264                 if (sa) {
1265                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1266                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x))
1267                                         scr->subwinactive = ar->swinid;
1268                         }
1269                 }
1270                 else
1271                         scr->subwinactive = scr->mainwin;
1272                 
1273                 /* check for redraw headers */
1274                 if (oldswin != scr->subwinactive) {
1275
1276                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1277                                 int do_draw = FALSE;
1278                                 
1279                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1280                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1281                                                 do_draw = TRUE;
1282                                 
1283                                 if (do_draw) {
1284                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1285                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1286                                                         ED_region_tag_redraw(ar);
1287                                 }
1288                         }
1289                 }
1290                 
1291                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1292                 if (scr->subwinactive == scr->mainwin) {
1293                         screen_cursor_set(win, event);
1294                 }
1295                 else {
1296                         if (oldswin != scr->subwinactive) {
1297                                 region_cursor_set(win, scr->subwinactive, TRUE);
1298                                 WM_event_add_notifier(C, NC_SCREEN | ND_SUBWINACTIVE, scr);
1299                         }
1300                         else
1301                                 region_cursor_set(win, scr->subwinactive, FALSE);
1302                 }
1303         }
1304 }
1305
1306 int ED_screen_area_active(const bContext *C)
1307 {
1308         wmWindow *win = CTX_wm_window(C);
1309         bScreen *sc = CTX_wm_screen(C);
1310         ScrArea *sa = CTX_wm_area(C);
1311
1312         if (win && sc && sa) {
1313                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1314                 ARegion *ar;
1315                 
1316                 if (az && az->type == AZONE_REGION)
1317                         return 1;
1318                 
1319                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1320                         if (ar->swinid == sc->subwinactive)
1321                                 return 1;
1322         }
1323         return 0;
1324 }
1325
1326 /* operator call, WM + Window + screen already existed before */
1327 /* Do NOT call in area/region queues! */
1328 void ED_screen_set(bContext *C, bScreen *sc)
1329 {
1330         wmWindowManager *wm = CTX_wm_manager(C);
1331         wmWindow *win = CTX_wm_window(C);
1332         bScreen *oldscreen = CTX_wm_screen(C);
1333         ID *id;
1334         
1335         /* validate screen, it's called with notifier reference */
1336         for (id = CTX_data_main(C)->screen.first; id; id = id->next)
1337                 if (sc == (bScreen *)id)
1338                         break;
1339         if (id == NULL)
1340                 return;
1341         
1342         /* check for valid winid */
1343         if (sc->winid != 0 && sc->winid != win->winid)
1344                 return;
1345         
1346         if (sc->full) {             /* find associated full */
1347                 bScreen *sc1;
1348                 for (sc1 = CTX_data_main(C)->screen.first; sc1; sc1 = sc1->id.next) {
1349                         ScrArea *sa = sc1->areabase.first;
1350                         if (sa->full == sc) {
1351                                 sc = sc1;
1352                                 break;
1353                         }
1354                 }
1355         }
1356         
1357         if (oldscreen != sc) {
1358                 wmTimer *wt = oldscreen->animtimer;
1359                 ScrArea *sa;
1360
1361                 /* remove handlers referencing areas in old screen */
1362                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1363                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1364                 }
1365
1366                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1367                 oldscreen->animtimer = NULL;
1368                 if (wt)
1369                         WM_event_timer_sleep(wm, win, wt, 1);
1370                 
1371                 ED_screen_exit(C, win, oldscreen);
1372                 oldscreen->animtimer = wt;
1373                 
1374                 win->screen = sc;
1375                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1376                 
1377                 /* prevent multiwin errors */
1378                 sc->winid = win->winid;
1379                 
1380                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1381                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1382                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1383                 
1384                 /* makes button hilites work */
1385                 WM_event_add_mousemove(C);
1386         }
1387 }
1388
1389 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1390 {
1391         wmWindow *win;
1392
1393         for (win = wm->windows.first; win; win = win->next)
1394                 if (win->screen == sc)
1395                         return 1;
1396         
1397         return 0;
1398 }
1399
1400 /* only call outside of area/region loops */
1401 void ED_screen_delete(bContext *C, bScreen *sc)
1402 {
1403         Main *bmain = CTX_data_main(C);
1404         wmWindowManager *wm = CTX_wm_manager(C);
1405         wmWindow *win = CTX_wm_window(C);
1406         bScreen *newsc;
1407         int delete = 1;
1408         
1409         /* don't allow deleting temp fullscreens for now */
1410         if (sc->full == SCREENFULL) {
1411                 return;
1412         }
1413         
1414                 
1415         /* screen can only be in use by one window at a time, so as
1416          * long as we are able to find a screen that is unused, we
1417          * can safely assume ours is not in use anywhere an delete it */
1418
1419         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1420                 if (!ed_screen_used(wm, newsc))
1421                         break;
1422         
1423         if (!newsc) {
1424                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1425                         if (!ed_screen_used(wm, newsc))
1426                                 break;
1427         }
1428
1429         if (!newsc)
1430                 return;
1431
1432         ED_screen_set(C, newsc);
1433
1434         if (delete && win->screen != sc)
1435                 BKE_libblock_free(&bmain->screen, sc);
1436 }
1437
1438 /* only call outside of area/region loops */
1439 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1440 {
1441         Main *bmain = CTX_data_main(C);
1442         bScreen *sc;
1443
1444         if (screen == NULL)
1445                 return;
1446         
1447         if (ed_screen_used(CTX_wm_manager(C), screen))
1448                 ED_object_exit_editmode(C, EM_FREEDATA | EM_DO_UNDO);
1449
1450         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1451                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1452                         
1453                         if (scene != sc->scene) {
1454                                 /* all areas endlocalview */
1455                                 // XXX  ScrArea *sa = sc->areabase.first;
1456                                 //      while (sa) {
1457                                 //              endlocalview(sa);
1458                                 //              sa = sa->next;
1459                                 //      }
1460                                 sc->scene = scene;
1461                         }
1462                         
1463                 }
1464         }
1465         
1466         //  copy_view3d_lock(0);        /* space.c */
1467         
1468         /* are there cameras in the views that are not in the scene? */
1469         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1470                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1471                         ScrArea *sa = sc->areabase.first;
1472                         while (sa) {
1473                                 SpaceLink *sl = sa->spacedata.first;
1474                                 while (sl) {
1475                                         if (sl->spacetype == SPACE_VIEW3D) {
1476                                                 View3D *v3d = (View3D *) sl;
1477
1478                                                 BKE_screen_view3d_sync(v3d, scene);
1479
1480                                                 if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1481                                                         v3d->camera = BKE_scene_camera_find(sc->scene);
1482                                                         // XXX if (sc == curscreen) handle_view3d_lock();
1483                                                         if (!v3d->camera) {
1484                                                                 ARegion *ar;
1485                                                                 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
1486                                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1487                                                                                 RegionView3D *rv3d = ar->regiondata;
1488
1489                                                                                 if (rv3d->persp == RV3D_CAMOB)
1490                                                                                         rv3d->persp = RV3D_PERSP;
1491                                                                         }
1492                                                                 }
1493                                                         }
1494                                                 }
1495                                         }
1496                                         sl = sl->next;
1497                                 }
1498                                 sa = sa->next;
1499                         }
1500                 }
1501         }
1502         
1503         CTX_data_scene_set(C, scene);
1504         BKE_scene_set_background(bmain, scene);
1505         DAG_on_visible_update(bmain, FALSE);
1506         
1507         ED_render_engine_changed(bmain);
1508         ED_update_for_newframe(bmain, scene, 1);
1509         
1510         /* complete redraw */
1511         WM_event_add_notifier(C, NC_WINDOW, NULL);
1512         
1513 }
1514
1515 /* only call outside of area/region loops */
1516 void ED_screen_delete_scene(bContext *C, Scene *scene)
1517 {
1518         Main *bmain = CTX_data_main(C);
1519         Scene *newscene;
1520
1521         if (scene->id.prev)
1522                 newscene = scene->id.prev;
1523         else if (scene->id.next)
1524                 newscene = scene->id.next;
1525         else
1526                 return;
1527
1528         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1529
1530         BKE_scene_unlink(bmain, scene, newscene);
1531 }
1532
1533 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1534 {
1535         wmWindow *win = CTX_wm_window(C);
1536         bScreen *screen = CTX_wm_screen(C);
1537         ScrArea *newsa = NULL;
1538
1539         if (!sa || sa->full == NULL) {
1540                 newsa = ED_screen_full_toggle(C, win, sa);
1541         }
1542         
1543         if (!newsa) {
1544                 if (sa->full) {
1545                         /* if this has been called from the temporary info header generated in
1546                          * temp fullscreen layouts, find the correct fullscreen area to change
1547                          * to create a new space inside */
1548                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1549                                 if (!(sa->flag & AREA_TEMP_INFO))
1550                                         break;
1551                         }
1552                 }
1553                 else {
1554                         newsa = sa;
1555                 }
1556         }
1557         
1558         ED_area_newspace(C, newsa, type);
1559         
1560         return newsa;
1561 }
1562
1563 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1564 {
1565         wmWindow *win = CTX_wm_window(C);
1566
1567         ED_area_prevspace(C, sa);
1568         
1569         if (sa->full)
1570                 ED_screen_full_toggle(C, win, sa);
1571 }
1572
1573 /* restore a screen / area back to default operation, after temp fullscreen modes */
1574 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1575 {
1576         wmWindow *win = CTX_wm_window(C);
1577         SpaceLink *sl = sa->spacedata.first;
1578         
1579         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1580          * overlaid on top of a existing setup) then return to the previous space */
1581         
1582         if (sl->next) {
1583                 /* specific checks for space types */
1584
1585                 int sima_restore = 0;
1586
1587                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1588                 if (sl->spacetype == SPACE_IMAGE) {
1589                         SpaceImage *sima = sa->spacedata.first;
1590                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1591                                 sima_restore = 1;
1592                 }
1593
1594                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1595                         SpaceImage *sima = sa->spacedata.first;
1596                         if (sima->flag & SI_PREVSPACE)
1597                                 sima->flag &= ~SI_PREVSPACE;
1598                         if (sima->flag & SI_FULLWINDOW) {
1599                                 sima->flag &= ~SI_FULLWINDOW;
1600                                 ED_screen_full_prevspace(C, sa);
1601                         }
1602                 }
1603                 else if (sl->spacetype == SPACE_FILE) {
1604                         ED_screen_full_prevspace(C, sa);
1605                 }
1606                 else {
1607                         ED_screen_full_toggle(C, win, sa);
1608                 }
1609         }
1610         /* otherwise just tile the area again */
1611         else {
1612                 ED_screen_full_toggle(C, win, sa);
1613         }
1614 }
1615
1616 /* this function toggles: if area is full then the parent will be restored */
1617 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1618 {
1619         bScreen *sc, *oldscreen;
1620         ARegion *ar;
1621
1622         if (sa) {
1623                 /* ensure we don't have a button active anymore, can crash when
1624                  * switching screens with tooltip open because region and tooltip
1625                  * are no longer in the same screen */
1626                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1627                         uiFreeBlocks(C, &ar->uiblocks);
1628                 
1629                 /* prevent hanging header prints */
1630                 ED_area_headerprint(sa, NULL);
1631         }
1632
1633         if (sa && sa->full) {
1634                 ScrArea *old;
1635                 /*short fulltype;*/ /*UNUSED*/
1636
1637                 sc = sa->full;       /* the old screen to restore */
1638                 oldscreen = win->screen; /* the one disappearing */
1639
1640                 /*fulltype = sc->full;*/
1641                 sc->full = 0;
1642
1643                 /* removed: SCREENAUTOPLAY exception here */
1644         
1645                 /* find old area */
1646                 for (old = sc->areabase.first; old; old = old->next)
1647                         if (old->full) break;
1648                 if (old == NULL) {
1649                         if (G.debug & G_DEBUG)
1650                                 printf("%s: something wrong in areafullscreen\n", __func__);
1651                         return NULL;
1652                 }
1653
1654                 area_copy_data(old, sa, 1); /*  1 = swap spacelist */
1655                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1656                 old->full = NULL;
1657
1658                 /* animtimer back */
1659                 sc->animtimer = oldscreen->animtimer;
1660                 oldscreen->animtimer = NULL;
1661
1662                 ED_screen_set(C, sc);
1663
1664                 BKE_screen_free(oldscreen);
1665                 BKE_libblock_free(&CTX_data_main(C)->screen, oldscreen);
1666
1667         }
1668         else {
1669                 ScrArea *newa;
1670                 char newname[MAX_ID_NAME - 2];
1671
1672                 oldscreen = win->screen;
1673
1674                 /* nothing wrong with having only 1 area, as far as I can see...
1675                  * is there only 1 area? */
1676 #if 0
1677                 if (oldscreen->areabase.first == oldscreen->areabase.last)
1678                         return NULL;
1679 #endif
1680
1681                 oldscreen->full = SCREENFULL;
1682                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "full");
1683                 sc = ED_screen_add(win, oldscreen->scene, newname);
1684                 sc->full = SCREENFULL; // XXX
1685
1686                 /* timer */
1687                 sc->animtimer = oldscreen->animtimer;
1688                 oldscreen->animtimer = NULL;
1689
1690                 /* returns the top small area */
1691                 newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1692                 ED_area_newspace(C, newa, SPACE_INFO);
1693
1694                 /* use random area when we have no active one, e.g. when the
1695                  * mouse is outside of the window and we open a file browser */
1696                 if (!sa)
1697                         sa = oldscreen->areabase.first;
1698
1699                 /* copy area */
1700                 newa = newa->prev;
1701                 area_copy_data(newa, sa, 1);  /* 1 = swap spacelist */
1702                 sa->flag |= AREA_TEMP_INFO;
1703
1704                 sa->full = oldscreen;
1705                 newa->full = oldscreen;
1706                 newa->next->full = oldscreen; // XXX
1707
1708                 ED_screen_set(C, sc);
1709         }
1710
1711         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1712         CTX_wm_area_set(C, sc->areabase.first);
1713
1714         return sc->areabase.first;
1715 }
1716
1717 /* update frame rate info for viewport drawing */
1718 void ED_refresh_viewport_fps(bContext *C)
1719 {
1720         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1721         Scene *scene = CTX_data_scene(C);
1722         
1723         /* is anim playback running? */
1724         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1725                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1726                 
1727                 /* if there isn't any info, init it first */
1728                 if (fpsi == NULL)
1729                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1730                 
1731                 /* update the values */
1732                 fpsi->redrawtime = fpsi->lredrawtime;
1733                 fpsi->lredrawtime = animtimer->ltime;
1734         }
1735         else {
1736                 /* playback stopped or shouldn't be running */
1737                 if (scene->fps_info)
1738                         MEM_freeN(scene->fps_info);
1739                 scene->fps_info = NULL;
1740         }
1741 }
1742
1743 /* redraws: uses defines from stime->redraws 
1744  * enable: 1 - forward on, -1 - backwards on, 0 - off
1745  */
1746 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1747 {
1748         bScreen *screen = CTX_wm_screen(C);
1749         wmWindowManager *wm = CTX_wm_manager(C);
1750         wmWindow *win = CTX_wm_window(C);
1751         Scene *scene = CTX_data_scene(C);
1752         bScreen *stopscreen = ED_screen_animation_playing(wm);
1753         
1754         if (stopscreen) {
1755                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1756                 stopscreen->animtimer = NULL;
1757         }
1758         
1759         if (enable) {
1760                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1761                 
1762                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1763                 
1764                 sad->ar = CTX_wm_region(C);
1765                 /* if startframe is larger than current frame, we put currentframe on startframe.
1766                  * note: first frame then is not drawn! (ton) */
1767                 if (PRVRANGEON) {
1768                         if (scene->r.psfra > scene->r.cfra) {
1769                                 sad->sfra = scene->r.cfra;
1770                                 scene->r.cfra = scene->r.psfra;
1771                         }
1772                         else
1773                                 sad->sfra = scene->r.cfra;
1774                 }
1775                 else {
1776                         if (scene->r.sfra > scene->r.cfra) {
1777                                 sad->sfra = scene->r.cfra;
1778                                 scene->r.cfra = scene->r.sfra;
1779                         }
1780                         else
1781                                 sad->sfra = scene->r.cfra;
1782                 }
1783                 sad->redraws = redraws;
1784                 sad->refresh = refresh;
1785                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1786                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1787                 
1788                 screen->animtimer->customdata = sad;
1789                 
1790         }
1791
1792         /* notifier catched by top header, for button */
1793         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1794 }
1795
1796 /* helper for screen_animation_play() - only to be used for TimeLine */
1797 static ARegion *time_top_left_3dwindow(bScreen *screen)
1798 {
1799         ARegion *aret = NULL;
1800         ScrArea *sa;
1801         int min = 10000;
1802         
1803         for (sa = screen->areabase.first; sa; sa = sa->next) {
1804                 if (sa->spacetype == SPACE_VIEW3D) {
1805                         ARegion *ar;
1806                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1807                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1808                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1809                                                 aret = ar;
1810                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1811                                         }
1812                                 }
1813                         }
1814                 }
1815         }
1816
1817         return aret;
1818 }
1819
1820 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1821 {
1822         if (screen && screen->animtimer) {
1823                 wmTimer *wt = screen->animtimer;
1824                 ScreenAnimData *sad = wt->customdata;
1825                 
1826                 sad->redraws = redraws;
1827                 sad->refresh = refresh;
1828                 sad->ar = NULL;
1829                 if (redraws & TIME_REGION)
1830                         sad->ar = time_top_left_3dwindow(screen);
1831         }
1832 }
1833
1834 /* results in fully updated anim system
1835  * screen can be NULL */
1836 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
1837 {
1838         wmWindowManager *wm = bmain->wm.first;
1839         wmWindow *window;
1840         int layers = 0;
1841
1842 #ifdef DURIAN_CAMERA_SWITCH
1843         void *camera = BKE_scene_camera_switch_find(scene);
1844         if (camera && scene->camera != camera) {
1845                 bScreen *sc;
1846                 scene->camera = camera;
1847                 /* are there cameras in the views that are not in the scene? */
1848                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1849                         BKE_screen_view3d_scene_sync(sc);
1850                 }
1851         }
1852 #endif
1853
1854         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1855         
1856         /* update animated image textures for gpu, etc,
1857          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
1858         ED_image_update_frame(bmain, scene->r.cfra);
1859
1860         ED_clip_update_frame(bmain, scene->r.cfra);
1861
1862         /* get layers from all windows */
1863         for (window = wm->windows.first; window; window = window->next)
1864                 layers |= BKE_screen_visible_layers(window->screen, scene);
1865
1866         /* this function applies the changes too */
1867         BKE_scene_update_for_newframe(bmain, scene, layers); /* BKE_scene.h */
1868         
1869         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1870         //      audiostream_scrub( CFRA );
1871         
1872         /* 3d window, preview */
1873         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1874         
1875         /* all movie/sequence images */
1876         //BIF_image_update_frame();
1877         
1878         /* composite */
1879         if (scene->use_nodes && scene->nodetree)
1880                 ntreeCompositTagAnimated(scene->nodetree);
1881         
1882         /* update animated texture nodes */
1883         {
1884                 Tex *tex;
1885                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
1886                         if (tex->use_nodes && tex->nodetree) {
1887                                 ntreeTexTagAnimated(tex->nodetree);
1888                         }
1889         }
1890         
1891 }
1892
1893