Threaded object update and EvaluationContext
[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 {
198                         sv->flag = 0;
199                 }
200                 sv = svn;
201         }
202 }
203
204 void removedouble_scredges(bScreen *sc)
205 {
206         ScrEdge *verg, *se, *sn;
207         
208         /* compare */
209         verg = sc->edgebase.first;
210         while (verg) {
211                 se = verg->next;
212                 while (se) {
213                         sn = se->next;
214                         if (verg->v1 == se->v1 && verg->v2 == se->v2) {
215                                 BLI_remlink(&sc->edgebase, se);
216                                 MEM_freeN(se);
217                         }
218                         se = sn;
219                 }
220                 verg = verg->next;
221         }
222 }
223
224 void removenotused_scredges(bScreen *sc)
225 {
226         ScrEdge *se, *sen;
227         ScrArea *sa;
228         int a = 0;
229         
230         /* sets flags when edge is used in area */
231         sa = sc->areabase.first;
232         while (sa) {
233                 se = screen_findedge(sc, sa->v1, sa->v2);
234                 if (se == NULL) printf("error: area %d edge 1 doesn't exist\n", a);
235                 else se->flag = 1;
236                 se = screen_findedge(sc, sa->v2, sa->v3);
237                 if (se == NULL) printf("error: area %d edge 2 doesn't exist\n", a);
238                 else se->flag = 1;
239                 se = screen_findedge(sc, sa->v3, sa->v4);
240                 if (se == NULL) printf("error: area %d edge 3 doesn't exist\n", a);
241                 else se->flag = 1;
242                 se = screen_findedge(sc, sa->v4, sa->v1);
243                 if (se == NULL) printf("error: area %d edge 4 doesn't exist\n", a);
244                 else se->flag = 1;
245                 sa = sa->next;
246                 a++;
247         }
248         se = sc->edgebase.first;
249         while (se) {
250                 sen = se->next;
251                 if (se->flag == 0) {
252                         BLI_remlink(&sc->edgebase, se);
253                         MEM_freeN(se);
254                 }
255                 else {
256                         se->flag = 0;
257                 }
258                 se = sen;
259         }
260 }
261
262 int scredge_is_horizontal(ScrEdge *se)
263 {
264         return (se->v1->vec.y == se->v2->vec.y);
265 }
266
267 /* need win size to make sure not to include edges along screen edge */
268 ScrEdge *screen_find_active_scredge(bScreen *sc,
269                                     const int winsize_x, const int winsize_y,
270                                     const int mx, const int my)
271 {
272         ScrEdge *se;
273         int safety = U.widget_unit / 10;
274         
275         if (safety < 2) safety = 2;
276         
277         for (se = sc->edgebase.first; se; se = se->next) {
278                 if (scredge_is_horizontal(se)) {
279                         if (se->v1->vec.y > 0 && se->v1->vec.y < winsize_y - 1) {
280                                 short min, max;
281                                 min = MIN2(se->v1->vec.x, se->v2->vec.x);
282                                 max = MAX2(se->v1->vec.x, se->v2->vec.x);
283                                 
284                                 if (abs(my - se->v1->vec.y) <= safety && mx >= min && mx <= max)
285                                         return se;
286                         }
287                 }
288                 else {
289                         if (se->v1->vec.x > 0 && se->v1->vec.x < winsize_x - 1) {
290                                 short min, max;
291                                 min = MIN2(se->v1->vec.y, se->v2->vec.y);
292                                 max = MAX2(se->v1->vec.y, se->v2->vec.y);
293                                 
294                                 if (abs(mx - se->v1->vec.x) <= safety && my >= min && my <= max)
295                                         return se;
296                         }
297                 }
298         }
299         
300         return NULL;
301 }
302
303
304
305 /* adds no space data */
306 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
307 {
308         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
309         sa->v1 = v1;
310         sa->v2 = v2;
311         sa->v3 = v3;
312         sa->v4 = v4;
313         sa->headertype = headertype;
314         sa->spacetype = sa->butspacetype = spacetype;
315         
316         BLI_addtail(&sc->areabase, sa);
317         
318         return sa;
319 }
320
321 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
322 {
323         
324         ED_area_exit(C, sa);
325         
326         BKE_screen_area_free(sa);
327         
328         BLI_remlink(&sc->areabase, sa);
329         MEM_freeN(sa);
330 }
331
332 /* return 0: no split possible */
333 /* else return (integer) screencoordinate split point */
334 static short testsplitpoint(ScrArea *sa, char dir, float fac)
335 {
336         short x, y;
337         const short area_min_x = AREAMINX;
338         const short area_min_y = ED_area_headersize();
339         
340         // area big enough?
341         if (dir == 'v' && (sa->v4->vec.x - sa->v1->vec.x <= 2 * area_min_x)) return 0;
342         if (dir == 'h' && (sa->v2->vec.y - sa->v1->vec.y <= 2 * area_min_y)) return 0;
343         
344         // to be sure
345         CLAMP(fac, 0.0f, 1.0f);
346         
347         if (dir == 'h') {
348                 y = sa->v1->vec.y + fac * (sa->v2->vec.y - sa->v1->vec.y);
349                 
350                 if (y - sa->v1->vec.y < area_min_y)
351                         y = sa->v1->vec.y + area_min_y;
352                 else if (sa->v2->vec.y - y < area_min_y)
353                         y = sa->v2->vec.y - area_min_y;
354                 else y -= (y % AREAGRID);
355                 
356                 return y;
357         }
358         else {
359                 x = sa->v1->vec.x + fac * (sa->v4->vec.x - sa->v1->vec.x);
360                 
361                 if (x - sa->v1->vec.x < area_min_x)
362                         x = sa->v1->vec.x + area_min_x;
363                 else if (sa->v4->vec.x - x < area_min_x)
364                         x = sa->v4->vec.x - area_min_x;
365                 else x -= (x % AREAGRID);
366                 
367                 return x;
368         }
369 }
370
371 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
372 {
373         ScrArea *newa = NULL;
374         ScrVert *sv1, *sv2;
375         short split;
376         
377         if (sa == NULL) return NULL;
378         
379         split = testsplitpoint(sa, dir, fac);
380         if (split == 0) return NULL;
381         
382         /* note regarding (fac > 0.5f) checks below.
383          * notmally it shouldn't matter which is used since the copy should match the original
384          * however with viewport rendering and python console this isn't the case. - campbell */
385
386         if (dir == 'h') {
387                 /* new vertices */
388                 sv1 = screen_addvert(sc, sa->v1->vec.x, split);
389                 sv2 = screen_addvert(sc, sa->v4->vec.x, split);
390                 
391                 /* new edges */
392                 screen_addedge(sc, sa->v1, sv1);
393                 screen_addedge(sc, sv1, sa->v2);
394                 screen_addedge(sc, sa->v3, sv2);
395                 screen_addedge(sc, sv2, sa->v4);
396                 screen_addedge(sc, sv1, sv2);
397                 
398                 if (fac > 0.5f) {
399                         /* new areas: top */
400                         newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
401
402                         /* area below */
403                         sa->v2 = sv1;
404                         sa->v3 = sv2;
405                 }
406                 else {
407                         /* new areas: bottom */
408                         newa = screen_addarea(sc, sa->v1, sv1, sv2, sa->v4, sa->headertype, sa->spacetype);
409
410                         /* area above */
411                         sa->v1 = sv1;
412                         sa->v4 = sv2;
413                 }
414
415                 area_copy_data(newa, sa, 0);
416                 
417         }
418         else {
419                 /* new vertices */
420                 sv1 = screen_addvert(sc, split, sa->v1->vec.y);
421                 sv2 = screen_addvert(sc, split, sa->v2->vec.y);
422                 
423                 /* new edges */
424                 screen_addedge(sc, sa->v1, sv1);
425                 screen_addedge(sc, sv1, sa->v4);
426                 screen_addedge(sc, sa->v2, sv2);
427                 screen_addedge(sc, sv2, sa->v3);
428                 screen_addedge(sc, sv1, sv2);
429                 
430                 if (fac > 0.5f) {
431                         /* new areas: right */
432                         newa = screen_addarea(sc, sv1, sv2, sa->v3, sa->v4, sa->headertype, sa->spacetype);
433
434                         /* area left */
435                         sa->v3 = sv2;
436                         sa->v4 = sv1;
437                 }
438                 else {
439                         /* new areas: left */
440                         newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
441
442                         /* area right */
443                         sa->v1 = sv1;
444                         sa->v2 = sv2;
445                 }
446
447                 area_copy_data(newa, sa, 0);
448         }
449         
450         /* remove double vertices en edges */
451         if (merge)
452                 removedouble_scrverts(sc);
453         removedouble_scredges(sc);
454         removenotused_scredges(sc);
455         
456         return newa;
457 }
458
459 /* empty screen, with 1 dummy area without spacedata */
460 /* uses window size */
461 bScreen *ED_screen_add(wmWindow *win, Scene *scene, const char *name)
462 {
463         const int winsize_x = WM_window_pixels_x(win);
464         const int winsize_y = WM_window_pixels_y(win);
465
466         bScreen *sc;
467         ScrVert *sv1, *sv2, *sv3, *sv4;
468         
469         sc = BKE_libblock_alloc(&G.main->screen, ID_SCR, name);
470         sc->scene = scene;
471         sc->do_refresh = TRUE;
472         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
473         sc->winid = win->winid;
474
475         sv1 = screen_addvert(sc, 0, 0);
476         sv2 = screen_addvert(sc, 0, winsize_y - 1);
477         sv3 = screen_addvert(sc, winsize_x - 1, winsize_y - 1);
478         sv4 = screen_addvert(sc, winsize_x - 1, 0);
479         
480         screen_addedge(sc, sv1, sv2);
481         screen_addedge(sc, sv2, sv3);
482         screen_addedge(sc, sv3, sv4);
483         screen_addedge(sc, sv4, sv1);
484         
485         /* dummy type, no spacedata */
486         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
487                 
488         return sc;
489 }
490
491 static void screen_copy(bScreen *to, bScreen *from)
492 {
493         ScrVert *s1, *s2;
494         ScrEdge *se;
495         ScrArea *sa, *saf;
496         
497         /* free contents of 'to', is from blenkernel screen.c */
498         BKE_screen_free(to);
499         
500         BLI_duplicatelist(&to->vertbase, &from->vertbase);
501         BLI_duplicatelist(&to->edgebase, &from->edgebase);
502         BLI_duplicatelist(&to->areabase, &from->areabase);
503         to->regionbase.first = to->regionbase.last = NULL;
504         
505         s2 = to->vertbase.first;
506         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
507                 s1->newv = s2;
508         }
509         
510         for (se = to->edgebase.first; se; se = se->next) {
511                 se->v1 = se->v1->newv;
512                 se->v2 = se->v2->newv;
513                 sortscrvert(&(se->v1), &(se->v2));
514         }
515         
516         saf = from->areabase.first;
517         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
518                 sa->v1 = sa->v1->newv;
519                 sa->v2 = sa->v2->newv;
520                 sa->v3 = sa->v3->newv;
521                 sa->v4 = sa->v4->newv;
522
523                 sa->spacedata.first = sa->spacedata.last = NULL;
524                 sa->regionbase.first = sa->regionbase.last = NULL;
525                 sa->actionzones.first = sa->actionzones.last = NULL;
526                 sa->handlers.first = sa->handlers.last = NULL;
527                 
528                 area_copy_data(sa, saf, 0);
529         }
530         
531         /* put at zero (needed?) */
532         for (s1 = from->vertbase.first; s1; s1 = s1->next)
533                 s1->newv = NULL;
534
535 }
536
537
538 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
539 /* -1 = not valid check */
540 /* used with join operator */
541 int area_getorientation(ScrArea *sa, ScrArea *sb)
542 {
543         ScrVert *sav1, *sav2, *sav3, *sav4;
544         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
545
546         if (sa == NULL || sb == NULL) return -1;
547
548         sav1 = sa->v1;
549         sav2 = sa->v2;
550         sav3 = sa->v3;
551         sav4 = sa->v4;
552         sbv1 = sb->v1;
553         sbv2 = sb->v2;
554         sbv3 = sb->v3;
555         sbv4 = sb->v4;
556         
557         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
558                 return 0;
559         }
560         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
561                 return 1;
562         }
563         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
564                 return 2;
565         }
566         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
567                 return 3;
568         }
569         
570         return -1;
571 }
572
573 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
574  *  used by the split, join operators
575  */
576 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
577 {
578         int dir;
579         
580         dir = area_getorientation(sa1, sa2);
581         /*printf("dir is : %i\n", dir);*/
582         
583         if (dir < 0) {
584                 if (sa1) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
585                 if (sa2) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
586                 return 0;
587         }
588         
589         if (dir == 0) {
590                 sa1->v1 = sa2->v1;
591                 sa1->v2 = sa2->v2;
592                 screen_addedge(scr, sa1->v2, sa1->v3);
593                 screen_addedge(scr, sa1->v1, sa1->v4);
594         }
595         else if (dir == 1) {
596                 sa1->v2 = sa2->v2;
597                 sa1->v3 = sa2->v3;
598                 screen_addedge(scr, sa1->v1, sa1->v2);
599                 screen_addedge(scr, sa1->v3, sa1->v4);
600         }
601         else if (dir == 2) {
602                 sa1->v3 = sa2->v3;
603                 sa1->v4 = sa2->v4;
604                 screen_addedge(scr, sa1->v2, sa1->v3);
605                 screen_addedge(scr, sa1->v1, sa1->v4);
606         }
607         else if (dir == 3) {
608                 sa1->v1 = sa2->v1;
609                 sa1->v4 = sa2->v4;
610                 screen_addedge(scr, sa1->v1, sa1->v2);
611                 screen_addedge(scr, sa1->v3, sa1->v4);
612         }
613         
614         screen_delarea(C, scr, sa2);
615         removedouble_scrverts(scr);
616         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
617         
618         return 1;
619 }
620
621 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
622 {
623         ScrEdge *se;
624         ScrVert *sv;
625         int oneselected;
626         char dir;
627         
628         /* select connected, only in the right direction */
629         /* 'dir' is the direction of EDGE */
630         
631         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
632         else dir = 'h';
633         
634         sv = sc->vertbase.first;
635         while (sv) {
636                 sv->flag = 0;
637                 sv = sv->next;
638         }
639         
640         edge->v1->flag = 1;
641         edge->v2->flag = 1;
642         
643         oneselected = 1;
644         while (oneselected) {
645                 se = sc->edgebase.first;
646                 oneselected = 0;
647                 while (se) {
648                         if (se->v1->flag + se->v2->flag == 1) {
649                                 if (dir == 'h') {
650                                         if (se->v1->vec.y == se->v2->vec.y) {
651                                                 se->v1->flag = se->v2->flag = 1;
652                                                 oneselected = 1;
653                                         }
654                                 }
655                                 if (dir == 'v') {
656                                         if (se->v1->vec.x == se->v2->vec.x) {
657                                                 se->v1->flag = se->v2->flag = 1;
658                                                 oneselected = 1;
659                                         }
660                                 }
661                         }
662                         se = se->next;
663                 }
664         }
665 }
666
667 /* test if screen vertices should be scaled */
668 static void screen_test_scale(bScreen *sc, int winsize_x, int winsize_y)
669 {
670         /* clamp Y size of header sized areas when expanding windows
671          * avoids annoying empty space around file menu */
672 #define USE_HEADER_SIZE_CLAMP
673
674         const int headery_init = ED_area_headersize();
675         ScrVert *sv = NULL;
676         ScrArea *sa;
677         int winsize_x_prev, winsize_y_prev;
678         float facx, facy, tempf, min[2], max[2];
679         
680         /* calculate size */
681         min[0] = min[1] = 20000.0f;
682         max[0] = max[1] = 0.0f;
683         
684         for (sv = sc->vertbase.first; sv; sv = sv->next) {
685                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
686                 minmax_v2v2_v2(min, max, fv);
687         }
688         
689         /* always make 0.0 left under */
690         for (sv = sc->vertbase.first; sv; sv = sv->next) {
691                 sv->vec.x -= min[0];
692                 sv->vec.y -= min[1];
693         }
694         
695         winsize_x_prev = (max[0] - min[0]) + 1;
696         winsize_y_prev = (max[1] - min[1]) + 1;
697
698
699 #ifdef USE_HEADER_SIZE_CLAMP
700 #define TEMP_BOTTOM 1
701 #define TEMP_TOP 2
702
703         /* if the window's Y axis grows, clamp header sized areas */
704         if (winsize_y_prev < winsize_y) {  /* growing? */
705                 const int headery_margin_max = headery_init + 4;
706                 for (sa = sc->areabase.first; sa; sa = sa->next) {
707                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
708                         sa->temp = 0;
709
710                         if (ar && !(ar->flag & RGN_FLAG_HIDDEN)) {
711                                 if (sa->v2->vec.y == winsize_y_prev - 1) {
712                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
713                                                 sa->temp = TEMP_TOP;
714                                         }
715                                 }
716                                 else if (sa->v1->vec.y == 0) {
717                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
718                                                 sa->temp = TEMP_BOTTOM;
719                                         }
720                                 }
721                         }
722                 }
723         }
724 #endif
725
726
727         if (winsize_x_prev != winsize_x || winsize_y_prev != winsize_y) {
728                 facx = ((float)winsize_x - 1) / ((float)winsize_x_prev - 1);
729                 facy = ((float)winsize_y - 1) / ((float)winsize_y_prev - 1);
730                 
731                 /* make sure it fits! */
732                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
733                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
734                          * need some way to store these as floats internally and re-apply from there. */
735                         tempf = ((float)sv->vec.x) * facx;
736                         sv->vec.x = (short)(tempf + 0.5f);
737                         //sv->vec.x += AREAGRID - 1;
738                         //sv->vec.x -=  (sv->vec.x % AREAGRID);
739
740                         CLAMP(sv->vec.x, 0, winsize_x - 1);
741                         
742                         tempf = ((float)sv->vec.y) * facy;
743                         sv->vec.y = (short)(tempf + 0.5f);
744                         //sv->vec.y += AREAGRID - 1;
745                         //sv->vec.y -=  (sv->vec.y % AREAGRID);
746
747                         CLAMP(sv->vec.y, 0, winsize_y - 1);
748                 }
749         }
750
751
752 #ifdef USE_HEADER_SIZE_CLAMP
753         if (winsize_y_prev < winsize_y) {  /* growing? */
754                 for (sa = sc->areabase.first; sa; sa = sa->next) {
755                         ScrEdge *se = NULL;
756
757                         if (sa->temp == 0)
758                                 continue;
759
760                         if (sa->v1 == sa->v2)
761                                 continue;
762
763                         /* adjust headery if verts are along the edge of window */
764                         if (sa->temp == TEMP_TOP) {
765                                 /* lower edge */
766                                 const int yval = sa->v2->vec.y - headery_init;
767                                 se = screen_findedge(sc, sa->v4, sa->v1);
768                                 select_connected_scredge(sc, se);
769                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
770                                         if (sv != sa->v2 && sv != sa->v3) {
771                                                 if (sv->flag) {
772                                                         sv->vec.y = yval;
773                                                 }
774                                         }
775                                 }
776                         }
777                         else {
778                                 /* upper edge */
779                                 const int yval = sa->v1->vec.y + headery_init;
780                                 se = screen_findedge(sc, sa->v2, sa->v3);
781                                 select_connected_scredge(sc, se);
782                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
783                                         if (sv != sa->v1 && sv != sa->v4) {
784                                                 if (sv->flag) {
785                                                         sv->vec.y = yval;
786                                                 }
787                                         }
788                                 }
789                         }
790                 }
791         }
792
793 #undef USE_HEADER_SIZE_CLAMP
794 #undef TEMP_BOTTOM
795 #undef TEMP_TOP
796 #endif
797
798
799         /* test for collapsed areas. This could happen in some blender version... */
800         /* ton: removed option now, it needs Context... */
801         
802         /* make each window at least ED_area_headersize() high */
803         for (sa = sc->areabase.first; sa; sa = sa->next) {
804                 int headery = headery_init;
805                 
806                 /* adjust headery if verts are along the edge of window */
807                 if (sa->v1->vec.y > 0)
808                         headery += U.pixelsize;
809                 if (sa->v2->vec.y < winsize_y - 1)
810                         headery += U.pixelsize;
811                 
812                 if (sa->v2->vec.y - sa->v1->vec.y + 1 < headery) {
813                         /* lower edge */
814                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
815                         if (se && sa->v1 != sa->v2) {
816                                 int yval;
817                                 
818                                 select_connected_scredge(sc, se);
819                                 
820                                 /* all selected vertices get the right offset */
821                                 yval = sa->v2->vec.y - headery + 1;
822                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
823                                         /* if is a collapsed area */
824                                         if (sv != sa->v2 && sv != sa->v3) {
825                                                 if (sv->flag) {
826                                                         sv->vec.y = yval;
827                                                 }
828                                         }
829                                 }
830                         }
831                 }
832         }
833         
834 }
835
836 /* *********************** DRAWING **************************************** */
837
838 /* draw vertical shape visualizing future joining (left as well
839  * right direction of future joining) */
840 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
841 {
842         vec2f points[10];
843         short i;
844         float w, h;
845         float width = sa->v3->vec.x - sa->v1->vec.x;
846         float height = sa->v3->vec.y - sa->v1->vec.y;
847
848         if (height < width) {
849                 h = height / 8;
850                 w = height / 4;
851         }
852         else {
853                 h = width / 8;
854                 w = width / 4;
855         }
856
857         points[0].x = sa->v1->vec.x;
858         points[0].y = sa->v1->vec.y + height / 2;
859
860         points[1].x = sa->v1->vec.x;
861         points[1].y = sa->v1->vec.y;
862
863         points[2].x = sa->v4->vec.x - w;
864         points[2].y = sa->v4->vec.y;
865
866         points[3].x = sa->v4->vec.x - w;
867         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
868
869         points[4].x = sa->v4->vec.x - 2 * w;
870         points[4].y = sa->v4->vec.y + height / 2;
871
872         points[5].x = sa->v4->vec.x - w;
873         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
874
875         points[6].x = sa->v3->vec.x - w;
876         points[6].y = sa->v3->vec.y;
877
878         points[7].x = sa->v2->vec.x;
879         points[7].y = sa->v2->vec.y;
880
881         points[8].x = sa->v4->vec.x;
882         points[8].y = sa->v4->vec.y + height / 2 - h;
883
884         points[9].x = sa->v4->vec.x;
885         points[9].y = sa->v4->vec.y + height / 2 + h;
886
887         if (dir == 'l') {
888                 /* when direction is left, then we flip direction of arrow */
889                 float cx = sa->v1->vec.x + width;
890                 for (i = 0; i < 10; i++) {
891                         points[i].x -= cx;
892                         points[i].x = -points[i].x;
893                         points[i].x += sa->v1->vec.x;
894                 }
895         }
896
897         glBegin(GL_POLYGON);
898         for (i = 0; i < 5; i++)
899                 glVertex2f(points[i].x, points[i].y);
900         glEnd();
901         glBegin(GL_POLYGON);
902         for (i = 4; i < 8; i++)
903                 glVertex2f(points[i].x, points[i].y);
904         glVertex2f(points[0].x, points[0].y);
905         glEnd();
906
907         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
908         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
909 }
910
911 /* draw vertical shape visualizing future joining (up/down direction) */
912 static void draw_vertical_join_shape(ScrArea *sa, char dir)
913 {
914         vec2f points[10];
915         short i;
916         float w, h;
917         float width = sa->v3->vec.x - sa->v1->vec.x;
918         float height = sa->v3->vec.y - sa->v1->vec.y;
919
920         if (height < width) {
921                 h = height / 4;
922                 w = height / 8;
923         }
924         else {
925                 h = width / 4;
926                 w = width / 8;
927         }
928
929         points[0].x = sa->v1->vec.x + width / 2;
930         points[0].y = sa->v3->vec.y;
931
932         points[1].x = sa->v2->vec.x;
933         points[1].y = sa->v2->vec.y;
934
935         points[2].x = sa->v1->vec.x;
936         points[2].y = sa->v1->vec.y + h;
937
938         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
939         points[3].y = sa->v1->vec.y + h;
940
941         points[4].x = sa->v1->vec.x + width / 2;
942         points[4].y = sa->v1->vec.y + 2 * h;
943
944         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
945         points[5].y = sa->v1->vec.y + h;
946
947         points[6].x = sa->v4->vec.x;
948         points[6].y = sa->v4->vec.y + h;
949         
950         points[7].x = sa->v3->vec.x;
951         points[7].y = sa->v3->vec.y;
952
953         points[8].x = sa->v1->vec.x + width / 2 - w;
954         points[8].y = sa->v1->vec.y;
955
956         points[9].x = sa->v1->vec.x + width / 2 + w;
957         points[9].y = sa->v1->vec.y;
958
959         if (dir == 'u') {
960                 /* when direction is up, then we flip direction of arrow */
961                 float cy = sa->v1->vec.y + height;
962                 for (i = 0; i < 10; i++) {
963                         points[i].y -= cy;
964                         points[i].y = -points[i].y;
965                         points[i].y += sa->v1->vec.y;
966                 }
967         }
968
969         glBegin(GL_POLYGON);
970         for (i = 0; i < 5; i++)
971                 glVertex2f(points[i].x, points[i].y);
972         glEnd();
973         glBegin(GL_POLYGON);
974         for (i = 4; i < 8; i++)
975                 glVertex2f(points[i].x, points[i].y);
976         glVertex2f(points[0].x, points[0].y);
977         glEnd();
978
979         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
980         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
981 }
982
983 /* draw join shape due to direction of joining */
984 static void draw_join_shape(ScrArea *sa, char dir)
985 {
986         if (dir == 'u' || dir == 'd')
987                 draw_vertical_join_shape(sa, dir);
988         else
989                 draw_horizontal_join_shape(sa, dir);
990 }
991
992 /* draw screen area darker with arrow (visualization of future joining) */
993 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
994 {
995         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
996         glEnable(GL_BLEND);
997         glColor4ub(0, 0, 0, 50);
998         draw_join_shape(sa, dir);
999         glDisable(GL_BLEND);
1000 }
1001
1002 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
1003 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
1004 {
1005         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
1006         glEnable(GL_BLEND);
1007         /* value 181 was hardly computed: 181~105 */
1008         glColor4ub(255, 255, 255, 50);
1009         /* draw_join_shape(sa, dir); */
1010         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
1011         glDisable(GL_BLEND);
1012 }
1013
1014 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
1015 {
1016         /* right border area */
1017         if (x2 < sizex - 1)
1018                 sdrawline(x2 + a, y1, x2 + a, y2);
1019         
1020         /* left border area */
1021         if (x1 > 0) /* otherwise it draws the emboss of window over */
1022                 sdrawline(x1 + a, y1, x1 + a, y2);
1023         
1024         /* top border area */
1025         if (y2 < sizey - 1)
1026                 sdrawline(x1, y2 + a, x2, y2 + a);
1027         
1028         /* bottom border area */
1029         if (y1 > 0)
1030                 sdrawline(x1, y1 + a, x2, y1 + a);
1031         
1032 }
1033
1034 /** screen edges drawing **/
1035 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
1036 {
1037         short x1 = sa->v1->vec.x;
1038         short y1 = sa->v1->vec.y;
1039         short x2 = sa->v3->vec.x;
1040         short y2 = sa->v3->vec.y;
1041         
1042         if (center == 0) {
1043                 if (U.pixelsize > 1.0f) {
1044                 
1045                         glColor3ub(0x50, 0x50, 0x50);
1046                         glLineWidth((2.0f * U.pixelsize) - 1);
1047                         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
1048                         glLineWidth(1.0f);
1049                 }
1050         }
1051         else {
1052                 glColor3ub(0, 0, 0);
1053                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
1054         }
1055 }
1056
1057 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
1058
1059 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
1060 {
1061         bScreen *newsc;
1062         
1063         if (sc->full != SCREENNORMAL) return NULL;  /* XXX handle this case! */
1064         
1065         /* make new empty screen: */
1066         newsc = ED_screen_add(win, sc->scene, sc->id.name + 2);
1067         /* copy all data */
1068         screen_copy(newsc, sc);
1069
1070         return newsc;
1071 }
1072
1073 /* screen sets cursor based on swinid */
1074 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
1075 {
1076         ScrArea *sa = win->screen->areabase.first;
1077         
1078         for (; sa; sa = sa->next) {
1079                 ARegion *ar = sa->regionbase.first;
1080                 for (; ar; ar = ar->next) {
1081                         if (ar->swinid == swinid) {
1082                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
1083                                         if (ar->type && ar->type->cursor)
1084                                                 ar->type->cursor(win, sa, ar);
1085                                         else
1086                                                 WM_cursor_set(win, CURSOR_STD);
1087                                 }
1088                                 return;
1089                         }
1090                 }
1091         }
1092 }
1093
1094 void ED_screen_do_listen(bContext *C, wmNotifier *note)
1095 {
1096         wmWindow *win = CTX_wm_window(C);
1097         
1098         /* generic notes */
1099         switch (note->category) {
1100                 case NC_WM:
1101                         if (note->data == ND_FILEREAD)
1102                                 win->screen->do_draw = TRUE;
1103                         break;
1104                 case NC_WINDOW:
1105                         win->screen->do_draw = TRUE;
1106                         break;
1107                 case NC_SCREEN:
1108                         if (note->action == NA_EDITED)
1109                                 win->screen->do_draw = win->screen->do_refresh = TRUE;
1110                         break;
1111                 case NC_SCENE:
1112                         if (note->data == ND_MODE)
1113                                 region_cursor_set(win, note->swinid, TRUE);
1114                         break;
1115         }
1116 }
1117
1118 /* only for edge lines between areas, and the blended join arrows */
1119 void ED_screen_draw(wmWindow *win)
1120 {
1121         const int winsize_x = WM_window_pixels_x(win);
1122         const int winsize_y = WM_window_pixels_y(win);
1123
1124         ScrArea *sa;
1125         ScrArea *sa1 = NULL;
1126         ScrArea *sa2 = NULL;
1127         ScrArea *sa3 = NULL;
1128         int dir = -1;
1129         int dira = -1;
1130
1131         wmSubWindowSet(win, win->screen->mainwin);
1132         
1133         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1134                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1135                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1136                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1137                 drawscredge_area(sa, winsize_x, winsize_y, 0);
1138         }
1139         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1140                 drawscredge_area(sa, winsize_x, winsize_y, 1);
1141         
1142         /* blended join arrow */
1143         if (sa1 && sa2) {
1144                 dir = area_getorientation(sa1, sa2);
1145                 if (dir >= 0) {
1146                         switch (dir) {
1147                                 case 0: /* W */
1148                                         dir = 'r';
1149                                         dira = 'l';
1150                                         break;
1151                                 case 1: /* N */
1152                                         dir = 'd';
1153                                         dira = 'u';
1154                                         break;
1155                                 case 2: /* E */
1156                                         dir = 'l';
1157                                         dira = 'r';
1158                                         break;
1159                                 case 3: /* S */
1160                                         dir = 'u';
1161                                         dira = 'd';
1162                                         break;
1163                         }
1164                 }
1165                 scrarea_draw_shape_dark(sa2, dir);
1166                 scrarea_draw_shape_light(sa1, dira);
1167         }
1168         
1169         /* splitpoint */
1170         if (sa3) {
1171                 glEnable(GL_BLEND);
1172                 glColor4ub(255, 255, 255, 100);
1173                 
1174                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1175                         sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1176                         glColor4ub(0, 0, 0, 100);
1177                         sdrawline(sa3->totrct.xmin, win->eventstate->y + 1, sa3->totrct.xmax, win->eventstate->y + 1);
1178                 }
1179                 else {
1180                         sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1181                         glColor4ub(0, 0, 0, 100);
1182                         sdrawline(win->eventstate->x + 1, sa3->totrct.ymin, win->eventstate->x + 1, sa3->totrct.ymax);
1183                 }
1184                 
1185                 glDisable(GL_BLEND);
1186         }
1187         
1188         win->screen->do_draw = FALSE;
1189 }
1190
1191 /* helper call for below, dpi changes headers */
1192 static void screen_refresh_headersizes(void)
1193 {
1194         const ListBase *lb = BKE_spacetypes_list();
1195         SpaceType *st;
1196         
1197         for (st = lb->first; st; st = st->next) {
1198                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1199                 if (art) art->prefsizey = ED_area_headersize();
1200         }
1201 }
1202
1203 /* make this screen usable */
1204 /* for file read and first use, for scaling window, area moves */
1205 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1206 {       
1207         /* exception for bg mode, we only need the screen context */
1208         if (!G.background) {
1209                 const int winsize_x = WM_window_pixels_x(win);
1210                 const int winsize_y = WM_window_pixels_y(win);
1211                 ScrArea *sa;
1212                 rcti winrct;
1213         
1214                 winrct.xmin = 0;
1215                 winrct.xmax = winsize_x - 1;
1216                 winrct.ymin = 0;
1217                 winrct.ymax = winsize_y - 1;
1218                 
1219                 /* header size depends on DPI, let's verify */
1220                 screen_refresh_headersizes();
1221                 
1222                 screen_test_scale(win->screen, winsize_x, winsize_y);
1223                 
1224                 if (win->screen->mainwin == 0)
1225                         win->screen->mainwin = wm_subwindow_open(win, &winrct);
1226                 else
1227                         wm_subwindow_position(win, win->screen->mainwin, &winrct);
1228                 
1229                 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1230                         /* set spacetype and region callbacks, calls init() */
1231                         /* sets subwindows for regions, adds handlers */
1232                         ED_area_initialize(wm, win, sa);
1233                 }
1234         
1235                 /* wake up animtimer */
1236                 if (win->screen->animtimer)
1237                         WM_event_timer_sleep(wm, win, win->screen->animtimer, false);
1238         }
1239
1240         if (G.debug & G_DEBUG_EVENTS) {
1241                 printf("%s: set screen\n", __func__);
1242         }
1243         win->screen->do_refresh = FALSE;
1244
1245         win->screen->context = ed_screen_context;
1246 }
1247
1248 /* file read, set all screens, ... */
1249 void ED_screens_initialize(wmWindowManager *wm)
1250 {
1251         wmWindow *win;
1252         
1253         for (win = wm->windows.first; win; win = win->next) {
1254                 
1255                 if (win->screen == NULL)
1256                         win->screen = G.main->screen.first;
1257                 
1258                 ED_screen_refresh(wm, win);
1259         }
1260 }
1261
1262
1263 /* *********** exit calls are for closing running stuff ******** */
1264
1265 void ED_region_exit(bContext *C, ARegion *ar)
1266 {
1267         wmWindowManager *wm = CTX_wm_manager(C);
1268         ARegion *prevar = CTX_wm_region(C);
1269
1270         if (ar->type && ar->type->exit)
1271                 ar->type->exit(wm, ar);
1272
1273         CTX_wm_region_set(C, ar);
1274         WM_event_remove_handlers(C, &ar->handlers);
1275         if (ar->swinid)
1276                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1277         ar->swinid = 0;
1278         
1279         if (ar->headerstr)
1280                 MEM_freeN(ar->headerstr);
1281         ar->headerstr = NULL;
1282         
1283         if (ar->regiontimer)
1284                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), ar->regiontimer);
1285
1286         CTX_wm_region_set(C, prevar);
1287 }
1288
1289 void ED_area_exit(bContext *C, ScrArea *sa)
1290 {
1291         wmWindowManager *wm = CTX_wm_manager(C);
1292         ScrArea *prevsa = CTX_wm_area(C);
1293         ARegion *ar;
1294
1295         if (sa->type && sa->type->exit)
1296                 sa->type->exit(wm, sa);
1297
1298         CTX_wm_area_set(C, sa);
1299         for (ar = sa->regionbase.first; ar; ar = ar->next)
1300                 ED_region_exit(C, ar);
1301
1302         WM_event_remove_handlers(C, &sa->handlers);
1303         CTX_wm_area_set(C, prevsa);
1304 }
1305
1306 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1307 {
1308         wmWindowManager *wm = CTX_wm_manager(C);
1309         wmWindow *prevwin = CTX_wm_window(C);
1310         ScrArea *sa;
1311         ARegion *ar;
1312
1313         CTX_wm_window_set(C, window);
1314         
1315         if (screen->animtimer)
1316                 WM_event_remove_timer(wm, window, screen->animtimer);
1317         screen->animtimer = NULL;
1318         
1319         if (screen->mainwin)
1320                 wm_subwindow_close(window, screen->mainwin);
1321         screen->mainwin = 0;
1322         screen->subwinactive = 0;
1323         
1324         for (ar = screen->regionbase.first; ar; ar = ar->next)
1325                 ED_region_exit(C, ar);
1326
1327         for (sa = screen->areabase.first; sa; sa = sa->next)
1328                 ED_area_exit(C, sa);
1329
1330         /* mark it available for use for other windows */
1331         screen->winid = 0;
1332         
1333         if (prevwin->screen->temp == 0) {
1334                 /* use previous window if possible */
1335                 CTX_wm_window_set(C, prevwin);
1336         }
1337         else {
1338                 /* none otherwise */
1339                 CTX_wm_window_set(C, NULL);
1340         }
1341         
1342 }
1343
1344 /* *********************************** */
1345
1346 /* case when on area-edge or in azones, or outside window */
1347 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1348 {
1349         const int winsize_x = WM_window_pixels_x(win);
1350         const int winsize_y = WM_window_pixels_y(win);
1351
1352         AZone *az = NULL;
1353         ScrArea *sa;
1354         
1355         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1356                 if ((az = is_in_area_actionzone(sa, &event->x)))
1357                         break;
1358         
1359         if (sa) {
1360                 if (az->type == AZONE_AREA)
1361                         WM_cursor_set(win, CURSOR_EDIT);
1362                 else if (az->type == AZONE_REGION) {
1363                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1364                                 WM_cursor_set(win, CURSOR_X_MOVE);
1365                         else
1366                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1367                 }
1368         }
1369         else {
1370                 ScrEdge *actedge = screen_find_active_scredge(win->screen, winsize_x, winsize_y, event->x, event->y);
1371                 
1372                 if (actedge) {
1373                         if (scredge_is_horizontal(actedge))
1374                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1375                         else
1376                                 WM_cursor_set(win, CURSOR_X_MOVE);
1377                 }
1378                 else
1379                         WM_cursor_set(win, CURSOR_STD);
1380         }
1381 }
1382
1383
1384 /* called in wm_event_system.c. sets state vars in screen, cursors */
1385 /* event type is mouse move */
1386 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1387 {
1388         wmWindow *win = CTX_wm_window(C);
1389         
1390         if (win->screen) {
1391                 bScreen *scr = win->screen;
1392                 ScrArea *sa;
1393                 ARegion *ar;
1394                 int oldswin = scr->subwinactive;
1395
1396                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1397                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1398                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1399                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1400                                                 break;
1401                 }
1402                 if (sa) {
1403                         /* make overlap active when mouse over */
1404                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1405                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1406                                         scr->subwinactive = ar->swinid;
1407                                         break;
1408                                 }
1409                         }
1410                 }
1411                 else
1412                         scr->subwinactive = scr->mainwin;
1413                 
1414                 /* check for redraw headers */
1415                 if (oldswin != scr->subwinactive) {
1416
1417                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1418                                 int do_draw = FALSE;
1419                                 
1420                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1421                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1422                                                 do_draw = TRUE;
1423                                 
1424                                 if (do_draw) {
1425                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1426                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1427                                                         ED_region_tag_redraw(ar);
1428                                 }
1429                         }
1430                 }
1431                 
1432                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1433                 if (scr->subwinactive == scr->mainwin) {
1434                         screen_cursor_set(win, event);
1435                 }
1436                 else {
1437                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1438                         if (oldswin != scr->subwinactive) {
1439                                 region_cursor_set(win, scr->subwinactive, TRUE);
1440
1441                                 /* this used to be a notifier, but needs to be done immediate
1442                                  * because it can undo setting the right button as active due
1443                                  * to delayed notifier handling */
1444                                 uiFreeActiveButtons(C, win->screen);
1445                         }
1446                         else
1447                                 region_cursor_set(win, scr->subwinactive, FALSE);
1448                 }
1449         }
1450 }
1451
1452 int ED_screen_area_active(const bContext *C)
1453 {
1454         wmWindow *win = CTX_wm_window(C);
1455         bScreen *sc = CTX_wm_screen(C);
1456         ScrArea *sa = CTX_wm_area(C);
1457
1458         if (win && sc && sa) {
1459                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1460                 ARegion *ar;
1461                 
1462                 if (az && az->type == AZONE_REGION)
1463                         return 1;
1464                 
1465                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1466                         if (ar->swinid == sc->subwinactive)
1467                                 return 1;
1468         }
1469         return 0;
1470 }
1471
1472 /* operator call, WM + Window + screen already existed before */
1473 /* Do NOT call in area/region queues! */
1474 void ED_screen_set(bContext *C, bScreen *sc)
1475 {
1476         Main *bmain = CTX_data_main(C);
1477         wmWindowManager *wm = CTX_wm_manager(C);
1478         wmWindow *win = CTX_wm_window(C);
1479         bScreen *oldscreen = CTX_wm_screen(C);
1480         ID *id;
1481         
1482         /* validate screen, it's called with notifier reference */
1483         for (id = bmain->screen.first; id; id = id->next)
1484                 if (sc == (bScreen *)id)
1485                         break;
1486         if (id == NULL)
1487                 return;
1488         
1489
1490         if (sc->full) {             /* find associated full */
1491                 bScreen *sc1;
1492                 for (sc1 = bmain->screen.first; sc1; sc1 = sc1->id.next) {
1493                         ScrArea *sa = sc1->areabase.first;
1494                         if (sa->full == sc) {
1495                                 sc = sc1;
1496                                 break;
1497                         }
1498                 }
1499         }
1500
1501         /* check for valid winid */
1502         if (sc->winid != 0 && sc->winid != win->winid)
1503                 return;
1504         
1505         if (oldscreen != sc) {
1506                 wmTimer *wt = oldscreen->animtimer;
1507                 ScrArea *sa;
1508                 Scene *oldscene = oldscreen->scene;
1509
1510                 /* remove handlers referencing areas in old screen */
1511                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1512                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1513                 }
1514
1515                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1516                 oldscreen->animtimer = NULL;
1517                 if (wt)
1518                         WM_event_timer_sleep(wm, win, wt, true);
1519                 
1520                 ED_screen_exit(C, win, oldscreen);
1521                 oldscreen->animtimer = wt;
1522                 
1523                 win->screen = sc;
1524                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1525                 
1526                 /* prevent multiwin errors */
1527                 sc->winid = win->winid;
1528                 
1529                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1530                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1531                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1532                 
1533                 /* makes button hilites work */
1534                 WM_event_add_mousemove(C);
1535
1536                 /* Needed to make sure all the derivedMeshes are
1537                  * up-to-date before viewport starts acquiring this.
1538                  *
1539                  * This is needed in cases when, for example, boolean
1540                  * modifier uses operant from invisible layer.
1541                  * Without this trick boolean wouldn't apply correct.
1542                  *
1543                  * Quite the same happens when setting screen's scene,
1544                  * so perhaps this is in fact correct thing to do.
1545                  */
1546                 if (oldscene != sc->scene) {
1547                         BKE_scene_set_background(bmain, sc->scene);
1548                         DAG_on_visible_update(bmain, FALSE);
1549                 }
1550         }
1551 }
1552
1553 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1554 {
1555         wmWindow *win;
1556
1557         for (win = wm->windows.first; win; win = win->next)
1558                 if (win->screen == sc)
1559                         return 1;
1560         
1561         return 0;
1562 }
1563
1564 /* only call outside of area/region loops */
1565 void ED_screen_delete(bContext *C, bScreen *sc)
1566 {
1567         Main *bmain = CTX_data_main(C);
1568         wmWindowManager *wm = CTX_wm_manager(C);
1569         wmWindow *win = CTX_wm_window(C);
1570         bScreen *newsc;
1571         int delete = 1;
1572         
1573         /* don't allow deleting temp fullscreens for now */
1574         if (sc->full == SCREENFULL) {
1575                 return;
1576         }
1577         
1578                 
1579         /* screen can only be in use by one window at a time, so as
1580          * long as we are able to find a screen that is unused, we
1581          * can safely assume ours is not in use anywhere an delete it */
1582
1583         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1584                 if (!ed_screen_used(wm, newsc))
1585                         break;
1586         
1587         if (!newsc) {
1588                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1589                         if (!ed_screen_used(wm, newsc))
1590                                 break;
1591         }
1592
1593         if (!newsc)
1594                 return;
1595
1596         ED_screen_set(C, newsc);
1597
1598         if (delete && win->screen != sc)
1599                 BKE_libblock_free(&bmain->screen, sc);
1600 }
1601
1602 static void ed_screen_set_3dview_camera(Scene *scene, bScreen *sc, ScrArea *sa, View3D *v3d)
1603 {
1604         /* fix any cameras that are used in the 3d view but not in the scene */
1605         BKE_screen_view3d_sync(v3d, scene);
1606
1607         if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1608                 v3d->camera = BKE_scene_camera_find(sc->scene);
1609                 // XXX if (sc == curscreen) handle_view3d_lock();
1610                 if (!v3d->camera) {
1611                         ARegion *ar;
1612                         ListBase *regionbase;
1613                         
1614                         /* regionbase is in different place depending if space is active */
1615                         if (v3d == sa->spacedata.first)
1616                                 regionbase = &sa->regionbase;
1617                         else
1618                                 regionbase = &v3d->regionbase;
1619                                 
1620                         for (ar = regionbase->first; ar; ar = ar->next) {
1621                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1622                                         RegionView3D *rv3d = ar->regiondata;
1623                                         if (rv3d->persp == RV3D_CAMOB) {
1624                                                 rv3d->persp = RV3D_PERSP;
1625                                         }
1626                                 }
1627                         }
1628                 }
1629         }
1630 }
1631
1632 /* only call outside of area/region loops */
1633 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1634 {
1635         Main *bmain = CTX_data_main(C);
1636         bScreen *sc;
1637
1638         if (screen == NULL)
1639                 return;
1640         
1641         if (ed_screen_used(CTX_wm_manager(C), screen))
1642                 ED_object_editmode_exit(C, EM_FREEDATA | EM_DO_UNDO);
1643
1644         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1645                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1646                         
1647                         if (scene != sc->scene) {
1648                                 /* all areas endlocalview */
1649                                 // XXX  ScrArea *sa = sc->areabase.first;
1650                                 //      while (sa) {
1651                                 //              endlocalview(sa);
1652                                 //              sa = sa->next;
1653                                 //      }
1654                                 sc->scene = scene;
1655                         }
1656                         
1657                 }
1658         }
1659         
1660         //  copy_view3d_lock(0);        /* space.c */
1661         
1662         /* are there cameras in the views that are not in the scene? */
1663         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1664                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1665                         ScrArea *sa = sc->areabase.first;
1666                         while (sa) {
1667                                 SpaceLink *sl = sa->spacedata.first;
1668                                 while (sl) {
1669                                         if (sl->spacetype == SPACE_VIEW3D) {
1670                                                 View3D *v3d = (View3D *) sl;
1671                                                 ed_screen_set_3dview_camera(scene, sc, sa, v3d);
1672
1673                                         }
1674                                         sl = sl->next;
1675                                 }
1676                                 sa = sa->next;
1677                         }
1678                 }
1679         }
1680         
1681         CTX_data_scene_set(C, scene);
1682         BKE_scene_set_background(bmain, scene);
1683         DAG_on_visible_update(bmain, FALSE);
1684         
1685         ED_render_engine_changed(bmain);
1686         ED_update_for_newframe(bmain, scene, 1);
1687         
1688         /* complete redraw */
1689         WM_event_add_notifier(C, NC_WINDOW, NULL);
1690         
1691 }
1692
1693 /* only call outside of area/region loops */
1694 void ED_screen_delete_scene(bContext *C, Scene *scene)
1695 {
1696         Main *bmain = CTX_data_main(C);
1697         Scene *newscene;
1698
1699         if (scene->id.prev)
1700                 newscene = scene->id.prev;
1701         else if (scene->id.next)
1702                 newscene = scene->id.next;
1703         else
1704                 return;
1705
1706         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1707
1708         BKE_scene_unlink(bmain, scene, newscene);
1709 }
1710
1711 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1712 {
1713         wmWindow *win = CTX_wm_window(C);
1714         bScreen *screen = CTX_wm_screen(C);
1715         ScrArea *newsa = NULL;
1716
1717         if (!sa || sa->full == NULL) {
1718                 newsa = ED_screen_full_toggle(C, win, sa);
1719         }
1720         
1721         if (!newsa) {
1722                 if (sa->full) {
1723                         /* if this has been called from the temporary info header generated in
1724                          * temp fullscreen layouts, find the correct fullscreen area to change
1725                          * to create a new space inside */
1726                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1727                                 if (!(sa->flag & AREA_TEMP_INFO))
1728                                         break;
1729                         }
1730                 }
1731                 else {
1732                         newsa = sa;
1733                 }
1734         }
1735         
1736         ED_area_newspace(C, newsa, type);
1737         
1738         return newsa;
1739 }
1740
1741 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1742 {
1743         wmWindow *win = CTX_wm_window(C);
1744
1745         ED_area_prevspace(C, sa);
1746         
1747         if (sa->full)
1748                 ED_screen_full_toggle(C, win, sa);
1749 }
1750
1751 /* restore a screen / area back to default operation, after temp fullscreen modes */
1752 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1753 {
1754         wmWindow *win = CTX_wm_window(C);
1755         SpaceLink *sl = sa->spacedata.first;
1756         
1757         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1758          * overlaid on top of a existing setup) then return to the previous space */
1759         
1760         if (sl->next) {
1761                 /* specific checks for space types */
1762
1763                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1764                 if (sl->spacetype == SPACE_IMAGE) {
1765                         SpaceImage *sima = sa->spacedata.first;
1766
1767                         if (sima->flag & (SI_PREVSPACE | SI_FULLWINDOW)) {
1768                                 sima->flag &= ~SI_PREVSPACE;
1769                                 sima->flag &= ~SI_FULLWINDOW;
1770                                 ED_screen_full_prevspace(C, sa);
1771                         }
1772                         else
1773                                 ED_screen_full_toggle(C, win, sa);
1774                 }
1775                 else if (sl->spacetype == SPACE_FILE) {
1776                         ED_screen_full_prevspace(C, sa);
1777                 }
1778                 else {
1779                         ED_screen_full_toggle(C, win, sa);
1780                 }
1781         }
1782         /* otherwise just tile the area again */
1783         else {
1784                 ED_screen_full_toggle(C, win, sa);
1785         }
1786 }
1787
1788 /* this function toggles: if area is full then the parent will be restored */
1789 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1790 {
1791         bScreen *sc, *oldscreen;
1792         ARegion *ar;
1793
1794         if (sa) {
1795                 /* ensure we don't have a button active anymore, can crash when
1796                  * switching screens with tooltip open because region and tooltip
1797                  * are no longer in the same screen */
1798                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1799                         uiFreeBlocks(C, &ar->uiblocks);
1800                 
1801                 /* prevent hanging header prints */
1802                 ED_area_headerprint(sa, NULL);
1803         }
1804
1805         if (sa && sa->full) {
1806                 ScrArea *old;
1807                 /*short fulltype;*/ /*UNUSED*/
1808
1809                 sc = sa->full;       /* the old screen to restore */
1810                 oldscreen = win->screen; /* the one disappearing */
1811
1812                 /*fulltype = sc->full;*/
1813                 sc->full = 0;
1814
1815                 /* removed: SCREENAUTOPLAY exception here */
1816         
1817                 /* find old area */
1818                 for (old = sc->areabase.first; old; old = old->next)
1819                         if (old->full) break;
1820                 if (old == NULL) {
1821                         if (G.debug & G_DEBUG)
1822                                 printf("%s: something wrong in areafullscreen\n", __func__);
1823                         return NULL;
1824                 }
1825
1826                 area_copy_data(old, sa, 1); /*  1 = swap spacelist */
1827                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1828                 old->full = NULL;
1829
1830                 /* animtimer back */
1831                 sc->animtimer = oldscreen->animtimer;
1832                 oldscreen->animtimer = NULL;
1833
1834                 ED_screen_set(C, sc);
1835
1836                 BKE_screen_free(oldscreen);
1837                 BKE_libblock_free(&CTX_data_main(C)->screen, oldscreen);
1838
1839         }
1840         else {
1841                 ScrArea *newa;
1842                 char newname[MAX_ID_NAME - 2];
1843
1844                 oldscreen = win->screen;
1845
1846                 /* nothing wrong with having only 1 area, as far as I can see...
1847                  * is there only 1 area? */
1848 #if 0
1849                 if (oldscreen->areabase.first == oldscreen->areabase.last)
1850                         return NULL;
1851 #endif
1852
1853                 oldscreen->full = SCREENFULL;
1854                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "full");
1855                 sc = ED_screen_add(win, oldscreen->scene, newname);
1856                 sc->full = SCREENFULL; // XXX
1857
1858                 /* timer */
1859                 sc->animtimer = oldscreen->animtimer;
1860                 oldscreen->animtimer = NULL;
1861
1862                 /* returns the top small area */
1863                 newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1864                 ED_area_newspace(C, newa, SPACE_INFO);
1865
1866                 /* use random area when we have no active one, e.g. when the
1867                  * mouse is outside of the window and we open a file browser */
1868                 if (!sa)
1869                         sa = oldscreen->areabase.first;
1870
1871                 /* copy area */
1872                 newa = newa->prev;
1873                 area_copy_data(newa, sa, 1);  /* 1 = swap spacelist */
1874                 sa->flag |= AREA_TEMP_INFO;
1875
1876                 sa->full = oldscreen;
1877                 newa->full = oldscreen;
1878                 newa->next->full = oldscreen; // XXX
1879
1880                 ED_screen_set(C, sc);
1881         }
1882
1883         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1884         CTX_wm_area_set(C, sc->areabase.first);
1885
1886         return sc->areabase.first;
1887 }
1888
1889 /* update frame rate info for viewport drawing */
1890 void ED_refresh_viewport_fps(bContext *C)
1891 {
1892         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1893         Scene *scene = CTX_data_scene(C);
1894         
1895         /* is anim playback running? */
1896         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1897                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1898                 
1899                 /* if there isn't any info, init it first */
1900                 if (fpsi == NULL)
1901                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1902                 
1903                 /* update the values */
1904                 fpsi->redrawtime = fpsi->lredrawtime;
1905                 fpsi->lredrawtime = animtimer->ltime;
1906         }
1907         else {
1908                 /* playback stopped or shouldn't be running */
1909                 if (scene->fps_info)
1910                         MEM_freeN(scene->fps_info);
1911                 scene->fps_info = NULL;
1912         }
1913 }
1914
1915 /* redraws: uses defines from stime->redraws 
1916  * enable: 1 - forward on, -1 - backwards on, 0 - off
1917  */
1918 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1919 {
1920         bScreen *screen = CTX_wm_screen(C);
1921         wmWindowManager *wm = CTX_wm_manager(C);
1922         wmWindow *win = CTX_wm_window(C);
1923         Scene *scene = CTX_data_scene(C);
1924         bScreen *stopscreen = ED_screen_animation_playing(wm);
1925         
1926         if (stopscreen) {
1927                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1928                 stopscreen->animtimer = NULL;
1929         }
1930         
1931         if (enable) {
1932                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1933                 
1934                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1935                 
1936                 sad->ar = CTX_wm_region(C);
1937                 /* if startframe is larger than current frame, we put currentframe on startframe.
1938                  * note: first frame then is not drawn! (ton) */
1939                 if (PRVRANGEON) {
1940                         if (scene->r.psfra > scene->r.cfra) {
1941                                 sad->sfra = scene->r.cfra;
1942                                 scene->r.cfra = scene->r.psfra;
1943                         }
1944                         else
1945                                 sad->sfra = scene->r.cfra;
1946                 }
1947                 else {
1948                         if (scene->r.sfra > scene->r.cfra) {
1949                                 sad->sfra = scene->r.cfra;
1950                                 scene->r.cfra = scene->r.sfra;
1951                         }
1952                         else
1953                                 sad->sfra = scene->r.cfra;
1954                 }
1955                 sad->redraws = redraws;
1956                 sad->refresh = refresh;
1957                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1958                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1959                 
1960                 screen->animtimer->customdata = sad;
1961                 
1962         }
1963
1964         /* notifier catched by top header, for button */
1965         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1966 }
1967
1968 /* helper for screen_animation_play() - only to be used for TimeLine */
1969 static ARegion *time_top_left_3dwindow(bScreen *screen)
1970 {
1971         ARegion *aret = NULL;
1972         ScrArea *sa;
1973         int min = 10000;
1974         
1975         for (sa = screen->areabase.first; sa; sa = sa->next) {
1976                 if (sa->spacetype == SPACE_VIEW3D) {
1977                         ARegion *ar;
1978                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1979                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1980                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1981                                                 aret = ar;
1982                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1983                                         }
1984                                 }
1985                         }
1986                 }
1987         }
1988
1989         return aret;
1990 }
1991
1992 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1993 {
1994         if (screen && screen->animtimer) {
1995                 wmTimer *wt = screen->animtimer;
1996                 ScreenAnimData *sad = wt->customdata;
1997                 
1998                 sad->redraws = redraws;
1999                 sad->refresh = refresh;
2000                 sad->ar = NULL;
2001                 if (redraws & TIME_REGION)
2002                         sad->ar = time_top_left_3dwindow(screen);
2003         }
2004 }
2005
2006 /* results in fully updated anim system
2007  * screen can be NULL */
2008 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
2009 {
2010         wmWindowManager *wm = bmain->wm.first;
2011         wmWindow *window;
2012         int layers = 0;
2013
2014 #ifdef DURIAN_CAMERA_SWITCH
2015         void *camera = BKE_scene_camera_switch_find(scene);
2016         if (camera && scene->camera != camera) {
2017                 bScreen *sc;
2018                 scene->camera = camera;
2019                 /* are there cameras in the views that are not in the scene? */
2020                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
2021                         BKE_screen_view3d_scene_sync(sc);
2022                 }
2023         }
2024 #endif
2025
2026         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
2027         
2028         ED_clip_update_frame(bmain, scene->r.cfra);
2029
2030         /* get layers from all windows */
2031         for (window = wm->windows.first; window; window = window->next)
2032                 layers |= BKE_screen_visible_layers(window->screen, scene);
2033
2034         /* this function applies the changes too */
2035         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, layers);
2036         
2037         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
2038         //      audiostream_scrub( CFRA );
2039         
2040         /* 3d window, preview */
2041         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2042         
2043         /* all movie/sequence images */
2044         //BIF_image_update_frame();
2045         
2046         /* composite */
2047         if (scene->use_nodes && scene->nodetree)
2048                 ntreeCompositTagAnimated(scene->nodetree);
2049         
2050         /* update animated texture nodes */
2051         {
2052                 Tex *tex;
2053                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
2054                         if (tex->use_nodes && tex->nodetree) {
2055                                 ntreeTexTagAnimated(tex->nodetree);
2056                         }
2057         }
2058         
2059 }
2060
2061