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