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