Rename any instance of scene layer or render layer in code with view layer
[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_workspace_types.h"
38 #include "DNA_userdef_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_icons.h"
46 #include "BKE_image.h"
47 #include "BKE_global.h"
48 #include "BKE_layer.h"
49 #include "BKE_library.h"
50 #include "BKE_library_remap.h"
51 #include "BKE_main.h"
52 #include "BKE_node.h"
53 #include "BKE_screen.h"
54 #include "BKE_scene.h"
55 #include "BKE_workspace.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(const 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 /**
461  * Empty screen, with 1 dummy area without spacedata. Uses window size.
462  */
463 bScreen *screen_add(const char *name, const int winsize_x, const int winsize_y)
464 {
465         bScreen *sc;
466         ScrVert *sv1, *sv2, *sv3, *sv4;
467         
468         sc = BKE_libblock_alloc(G.main, ID_SCR, name, 0);
469         sc->do_refresh = true;
470         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
471
472         sv1 = screen_addvert(sc, 0, 0);
473         sv2 = screen_addvert(sc, 0, winsize_y - 1);
474         sv3 = screen_addvert(sc, winsize_x - 1, winsize_y - 1);
475         sv4 = screen_addvert(sc, winsize_x - 1, 0);
476         
477         screen_addedge(sc, sv1, sv2);
478         screen_addedge(sc, sv2, sv3);
479         screen_addedge(sc, sv3, sv4);
480         screen_addedge(sc, sv4, sv1);
481         
482         /* dummy type, no spacedata */
483         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
484                 
485         return sc;
486 }
487
488 void screen_data_copy(bScreen *to, bScreen *from)
489 {
490         ScrVert *s1, *s2;
491         ScrEdge *se;
492         ScrArea *sa, *saf;
493         
494         /* free contents of 'to', is from blenkernel screen.c */
495         BKE_screen_free(to);
496         
497         BLI_duplicatelist(&to->vertbase, &from->vertbase);
498         BLI_duplicatelist(&to->edgebase, &from->edgebase);
499         BLI_duplicatelist(&to->areabase, &from->areabase);
500         BLI_listbase_clear(&to->regionbase);
501         
502         s2 = to->vertbase.first;
503         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
504                 s1->newv = s2;
505         }
506         
507         for (se = to->edgebase.first; se; se = se->next) {
508                 se->v1 = se->v1->newv;
509                 se->v2 = se->v2->newv;
510                 sortscrvert(&(se->v1), &(se->v2));
511         }
512         
513         saf = from->areabase.first;
514         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
515                 sa->v1 = sa->v1->newv;
516                 sa->v2 = sa->v2->newv;
517                 sa->v3 = sa->v3->newv;
518                 sa->v4 = sa->v4->newv;
519
520                 BLI_listbase_clear(&sa->spacedata);
521                 BLI_listbase_clear(&sa->regionbase);
522                 BLI_listbase_clear(&sa->actionzones);
523                 BLI_listbase_clear(&sa->handlers);
524                 
525                 ED_area_data_copy(sa, saf, true);
526         }
527         
528         /* put at zero (needed?) */
529         for (s1 = from->vertbase.first; s1; s1 = s1->next)
530                 s1->newv = NULL;
531 }
532
533 /**
534  * Prepare a newly created screen for initializing it as active screen.
535  */
536 void screen_new_activate_prepare(const wmWindow *win, bScreen *screen_new)
537 {
538         screen_new->winid = win->winid;
539         screen_new->do_refresh = true;
540         screen_new->do_draw = true;
541 }
542
543
544 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
545 /* -1 = not valid check */
546 /* used with join operator */
547 int area_getorientation(ScrArea *sa, ScrArea *sb)
548 {
549         ScrVert *sav1, *sav2, *sav3, *sav4;
550         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
551
552         if (sa == NULL || sb == NULL) return -1;
553
554         sav1 = sa->v1;
555         sav2 = sa->v2;
556         sav3 = sa->v3;
557         sav4 = sa->v4;
558         sbv1 = sb->v1;
559         sbv2 = sb->v2;
560         sbv3 = sb->v3;
561         sbv4 = sb->v4;
562         
563         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
564                 return 0;
565         }
566         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
567                 return 1;
568         }
569         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
570                 return 2;
571         }
572         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
573                 return 3;
574         }
575         
576         return -1;
577 }
578
579 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
580  *  used by the split, join operators
581  */
582 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
583 {
584         int dir;
585         
586         dir = area_getorientation(sa1, sa2);
587         /*printf("dir is : %i\n", dir);*/
588         
589         if (dir == -1) {
590                 if (sa1) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
591                 if (sa2) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
592                 return 0;
593         }
594         
595         if (dir == 0) {
596                 sa1->v1 = sa2->v1;
597                 sa1->v2 = sa2->v2;
598                 screen_addedge(scr, sa1->v2, sa1->v3);
599                 screen_addedge(scr, sa1->v1, sa1->v4);
600         }
601         else if (dir == 1) {
602                 sa1->v2 = sa2->v2;
603                 sa1->v3 = sa2->v3;
604                 screen_addedge(scr, sa1->v1, sa1->v2);
605                 screen_addedge(scr, sa1->v3, sa1->v4);
606         }
607         else if (dir == 2) {
608                 sa1->v3 = sa2->v3;
609                 sa1->v4 = sa2->v4;
610                 screen_addedge(scr, sa1->v2, sa1->v3);
611                 screen_addedge(scr, sa1->v1, sa1->v4);
612         }
613         else if (dir == 3) {
614                 sa1->v1 = sa2->v1;
615                 sa1->v4 = sa2->v4;
616                 screen_addedge(scr, sa1->v1, sa1->v2);
617                 screen_addedge(scr, sa1->v3, sa1->v4);
618         }
619         
620         screen_delarea(C, scr, sa2);
621         removedouble_scrverts(scr);
622         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
623         /* Update preview thumbnail */
624         BKE_icon_changed(scr->id.icon_id);
625
626         return 1;
627 }
628
629 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
630 {
631         ScrEdge *se;
632         ScrVert *sv;
633         int oneselected;
634         char dir;
635         
636         /* select connected, only in the right direction */
637         /* 'dir' is the direction of EDGE */
638         
639         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
640         else dir = 'h';
641         
642         sv = sc->vertbase.first;
643         while (sv) {
644                 sv->flag = 0;
645                 sv = sv->next;
646         }
647         
648         edge->v1->flag = 1;
649         edge->v2->flag = 1;
650         
651         oneselected = 1;
652         while (oneselected) {
653                 se = sc->edgebase.first;
654                 oneselected = 0;
655                 while (se) {
656                         if (se->v1->flag + se->v2->flag == 1) {
657                                 if (dir == 'h') {
658                                         if (se->v1->vec.y == se->v2->vec.y) {
659                                                 se->v1->flag = se->v2->flag = 1;
660                                                 oneselected = 1;
661                                         }
662                                 }
663                                 if (dir == 'v') {
664                                         if (se->v1->vec.x == se->v2->vec.x) {
665                                                 se->v1->flag = se->v2->flag = 1;
666                                                 oneselected = 1;
667                                         }
668                                 }
669                         }
670                         se = se->next;
671                 }
672         }
673 }
674
675 /* test if screen vertices should be scaled */
676 static void screen_test_scale(bScreen *sc, int winsize_x, int winsize_y)
677 {
678         /* clamp Y size of header sized areas when expanding windows
679          * avoids annoying empty space around file menu */
680 #define USE_HEADER_SIZE_CLAMP
681
682         const int headery_init = ED_area_headersize();
683         ScrVert *sv = NULL;
684         ScrArea *sa;
685         int winsize_x_prev, winsize_y_prev;
686         float facx, facy, tempf, min[2], max[2];
687         
688         /* calculate size */
689         min[0] = min[1] = 20000.0f;
690         max[0] = max[1] = 0.0f;
691         
692         for (sv = sc->vertbase.first; sv; sv = sv->next) {
693                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
694                 minmax_v2v2_v2(min, max, fv);
695         }
696         
697         /* always make 0.0 left under */
698         for (sv = sc->vertbase.first; sv; sv = sv->next) {
699                 sv->vec.x -= min[0];
700                 sv->vec.y -= min[1];
701         }
702         
703         winsize_x_prev = (max[0] - min[0]) + 1;
704         winsize_y_prev = (max[1] - min[1]) + 1;
705
706
707 #ifdef USE_HEADER_SIZE_CLAMP
708 #define TEMP_BOTTOM 1
709 #define TEMP_TOP 2
710
711         /* if the window's Y axis grows, clamp header sized areas */
712         if (winsize_y_prev < winsize_y) {  /* growing? */
713                 const int headery_margin_max = headery_init + 4;
714                 for (sa = sc->areabase.first; sa; sa = sa->next) {
715                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
716                         sa->temp = 0;
717
718                         if (ar && !(ar->flag & RGN_FLAG_HIDDEN)) {
719                                 if (sa->v2->vec.y == winsize_y_prev - 1) {
720                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
721                                                 sa->temp = TEMP_TOP;
722                                         }
723                                 }
724                                 else if (sa->v1->vec.y == 0) {
725                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
726                                                 sa->temp = TEMP_BOTTOM;
727                                         }
728                                 }
729                         }
730                 }
731         }
732 #endif
733
734
735         if (winsize_x_prev != winsize_x || winsize_y_prev != winsize_y) {
736                 facx = ((float)winsize_x - 1) / ((float)winsize_x_prev - 1);
737                 facy = ((float)winsize_y - 1) / ((float)winsize_y_prev - 1);
738                 
739                 /* make sure it fits! */
740                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
741                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
742                          * need some way to store these as floats internally and re-apply from there. */
743                         tempf = ((float)sv->vec.x) * facx;
744                         sv->vec.x = (short)(tempf + 0.5f);
745                         //sv->vec.x += AREAGRID - 1;
746                         //sv->vec.x -=  (sv->vec.x % AREAGRID);
747
748                         CLAMP(sv->vec.x, 0, winsize_x - 1);
749                         
750                         tempf = ((float)sv->vec.y) * facy;
751                         sv->vec.y = (short)(tempf + 0.5f);
752                         //sv->vec.y += AREAGRID - 1;
753                         //sv->vec.y -=  (sv->vec.y % AREAGRID);
754
755                         CLAMP(sv->vec.y, 0, winsize_y - 1);
756                 }
757         }
758
759
760 #ifdef USE_HEADER_SIZE_CLAMP
761         if (winsize_y_prev < winsize_y) {  /* growing? */
762                 for (sa = sc->areabase.first; sa; sa = sa->next) {
763                         ScrEdge *se = NULL;
764
765                         if (sa->temp == 0)
766                                 continue;
767
768                         if (sa->v1 == sa->v2)
769                                 continue;
770
771                         /* adjust headery if verts are along the edge of window */
772                         if (sa->temp == TEMP_TOP) {
773                                 /* lower edge */
774                                 const int yval = sa->v2->vec.y - headery_init;
775                                 se = screen_findedge(sc, sa->v4, sa->v1);
776                                 if (se != NULL) {
777                                         select_connected_scredge(sc, se);
778                                 }
779                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
780                                         if (sv != sa->v2 && sv != sa->v3) {
781                                                 if (sv->flag) {
782                                                         sv->vec.y = yval;
783                                                 }
784                                         }
785                                 }
786                         }
787                         else {
788                                 /* upper edge */
789                                 const int yval = sa->v1->vec.y + headery_init;
790                                 se = screen_findedge(sc, sa->v2, sa->v3);
791                                 if (se != NULL) {
792                                         select_connected_scredge(sc, se);
793                                 }
794                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
795                                         if (sv != sa->v1 && sv != sa->v4) {
796                                                 if (sv->flag) {
797                                                         sv->vec.y = yval;
798                                                 }
799                                         }
800                                 }
801                         }
802                 }
803         }
804
805 #undef USE_HEADER_SIZE_CLAMP
806 #undef TEMP_BOTTOM
807 #undef TEMP_TOP
808 #endif
809
810
811         /* test for collapsed areas. This could happen in some blender version... */
812         /* ton: removed option now, it needs Context... */
813         
814         /* make each window at least ED_area_headersize() high */
815         for (sa = sc->areabase.first; sa; sa = sa->next) {
816                 int headery = headery_init;
817                 
818                 /* adjust headery if verts are along the edge of window */
819                 if (sa->v1->vec.y > 0)
820                         headery += U.pixelsize;
821                 if (sa->v2->vec.y < winsize_y - 1)
822                         headery += U.pixelsize;
823                 
824                 if (sa->v2->vec.y - sa->v1->vec.y + 1 < headery) {
825                         /* lower edge */
826                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
827                         if (se && sa->v1 != sa->v2) {
828                                 int yval;
829                                 
830                                 select_connected_scredge(sc, se);
831                                 
832                                 /* all selected vertices get the right offset */
833                                 yval = sa->v2->vec.y - headery + 1;
834                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
835                                         /* if is a collapsed area */
836                                         if (sv != sa->v2 && sv != sa->v3) {
837                                                 if (sv->flag) {
838                                                         sv->vec.y = yval;
839                                                 }
840                                         }
841                                 }
842                         }
843                 }
844         }
845         
846 }
847
848
849 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
850
851 /* screen sets cursor based on swinid */
852 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
853 {
854         bScreen *screen = WM_window_get_active_screen(win);
855
856         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
857                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
858                         if (ar->swinid == swinid) {
859                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
860                                         if (ar->manipulator_map != NULL) {
861                                                 if (WM_manipulatormap_cursor_set(ar->manipulator_map, win)) {
862                                                         return;
863                                                 }
864                                         }
865                                         ED_region_cursor_set(win, sa, ar);
866                                 }
867                                 return;
868                         }
869                 }
870         }
871 }
872
873 void ED_screen_do_listen(bContext *C, wmNotifier *note)
874 {
875         wmWindow *win = CTX_wm_window(C);
876         bScreen *screen = CTX_wm_screen(C);
877
878         /* generic notes */
879         switch (note->category) {
880                 case NC_WM:
881                         if (note->data == ND_FILEREAD)
882                                 screen->do_draw = true;
883                         break;
884                 case NC_WINDOW:
885                         screen->do_draw = true;
886                         break;
887                 case NC_SCREEN:
888                         if (note->action == NA_EDITED)
889                                 screen->do_draw = screen->do_refresh = true;
890                         break;
891                 case NC_SCENE:
892                         if (note->data == ND_MODE)
893                                 region_cursor_set(win, note->swinid, true);
894                         break;
895         }
896 }
897
898 /* helper call for below, dpi changes headers */
899 static void screen_refresh_headersizes(void)
900 {
901         const ListBase *lb = BKE_spacetypes_list();
902         SpaceType *st;
903         
904         for (st = lb->first; st; st = st->next) {
905                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
906                 if (art) art->prefsizey = ED_area_headersize();
907         }
908 }
909
910 /* make this screen usable */
911 /* for file read and first use, for scaling window, area moves */
912 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
913 {
914         bScreen *screen = WM_window_get_active_screen(win);
915
916         /* exception for bg mode, we only need the screen context */
917         if (!G.background) {
918                 const int winsize_x = WM_window_pixels_x(win);
919                 const int winsize_y = WM_window_pixels_y(win);
920                 ScrArea *sa;
921                 rcti winrct;
922         
923                 winrct.xmin = 0;
924                 winrct.xmax = winsize_x - 1;
925                 winrct.ymin = 0;
926                 winrct.ymax = winsize_y - 1;
927                 
928                 /* header size depends on DPI, let's verify */
929                 WM_window_set_dpi(win);
930                 screen_refresh_headersizes();
931                 
932                 screen_test_scale(screen, winsize_x, winsize_y);
933                 
934                 if (screen->mainwin == 0) {
935                         screen->mainwin = wm_subwindow_open(win, &winrct, false);
936                 }
937                 else {
938                         wm_subwindow_position(win, screen->mainwin, &winrct, false);
939                 }
940                 
941                 for (sa = screen->areabase.first; sa; sa = sa->next) {
942                         /* set spacetype and region callbacks, calls init() */
943                         /* sets subwindows for regions, adds handlers */
944                         ED_area_initialize(wm, win, sa);
945                 }
946         
947                 /* wake up animtimer */
948                 if (screen->animtimer)
949                         WM_event_timer_sleep(wm, win, screen->animtimer, false);
950         }
951
952         if (G.debug & G_DEBUG_EVENTS) {
953                 printf("%s: set screen\n", __func__);
954         }
955         screen->do_refresh = false;
956         /* prevent multiwin errors */
957         screen->winid = win->winid;
958
959         screen->context = ed_screen_context;
960 }
961
962 /* file read, set all screens, ... */
963 void ED_screens_initialize(wmWindowManager *wm)
964 {
965         wmWindow *win;
966         
967         for (win = wm->windows.first; win; win = win->next) {
968                 if (WM_window_get_active_workspace(win) == NULL) {
969                         WM_window_set_active_workspace(win, G.main->workspaces.first);
970                 }
971
972                 ED_screen_refresh(wm, win);
973         }
974 }
975
976
977 /* *********** exit calls are for closing running stuff ******** */
978
979 void ED_region_exit(bContext *C, ARegion *ar)
980 {
981         wmWindowManager *wm = CTX_wm_manager(C);
982         wmWindow *win = CTX_wm_window(C);
983         ARegion *prevar = CTX_wm_region(C);
984
985         if (ar->type && ar->type->exit)
986                 ar->type->exit(wm, ar);
987
988         CTX_wm_region_set(C, ar);
989
990         WM_event_remove_handlers(C, &ar->handlers);
991         WM_event_modal_handler_region_replace(win, ar, NULL);
992         if (ar->swinid) {
993                 wm_subwindow_close(win, ar->swinid);
994                 ar->swinid = 0;
995         }
996         
997         if (ar->headerstr) {
998                 MEM_freeN(ar->headerstr);
999                 ar->headerstr = NULL;
1000         }
1001         
1002         if (ar->regiontimer) {
1003                 WM_event_remove_timer(wm, win, ar->regiontimer);
1004                 ar->regiontimer = NULL;
1005         }
1006
1007         CTX_wm_region_set(C, prevar);
1008 }
1009
1010 void ED_area_exit(bContext *C, ScrArea *sa)
1011 {
1012         wmWindowManager *wm = CTX_wm_manager(C);
1013         wmWindow *win = CTX_wm_window(C);
1014         ScrArea *prevsa = CTX_wm_area(C);
1015         ARegion *ar;
1016
1017         if (sa->type && sa->type->exit)
1018                 sa->type->exit(wm, sa);
1019
1020         CTX_wm_area_set(C, sa);
1021
1022         for (ar = sa->regionbase.first; ar; ar = ar->next)
1023                 ED_region_exit(C, ar);
1024
1025         WM_event_remove_handlers(C, &sa->handlers);
1026         WM_event_modal_handler_area_replace(win, sa, NULL);
1027
1028         CTX_wm_area_set(C, prevsa);
1029 }
1030
1031 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1032 {
1033         wmWindowManager *wm = CTX_wm_manager(C);
1034         wmWindow *prevwin = CTX_wm_window(C);
1035         ScrArea *sa;
1036         ARegion *ar;
1037
1038         CTX_wm_window_set(C, window);
1039         
1040         if (screen->animtimer)
1041                 WM_event_remove_timer(wm, window, screen->animtimer);
1042         screen->animtimer = NULL;
1043         screen->scrubbing = false;
1044
1045         if (screen->mainwin)
1046                 wm_subwindow_close(window, screen->mainwin);
1047         screen->mainwin = 0;
1048         screen->subwinactive = 0;
1049         
1050         for (ar = screen->regionbase.first; ar; ar = ar->next)
1051                 ED_region_exit(C, ar);
1052
1053         for (sa = screen->areabase.first; sa; sa = sa->next)
1054                 ED_area_exit(C, sa);
1055
1056         /* mark it available for use for other windows */
1057         screen->winid = 0;
1058         
1059         if (!WM_window_is_temp_screen(prevwin)) {
1060                 /* use previous window if possible */
1061                 CTX_wm_window_set(C, prevwin);
1062         }
1063         else {
1064                 /* none otherwise */
1065                 CTX_wm_window_set(C, NULL);
1066         }
1067         
1068 }
1069
1070 /* *********************************** */
1071
1072 /* case when on area-edge or in azones, or outside window */
1073 static void screen_cursor_set(wmWindow *win, const wmEvent *event)
1074 {
1075         const bScreen *screen = WM_window_get_active_screen(win);
1076         const int winsize_x = WM_window_pixels_x(win);
1077         const int winsize_y = WM_window_pixels_y(win);
1078
1079         AZone *az = NULL;
1080         ScrArea *sa;
1081         
1082         for (sa = screen->areabase.first; sa; sa = sa->next)
1083                 if ((az = is_in_area_actionzone(sa, &event->x)))
1084                         break;
1085         
1086         if (sa) {
1087                 if (az->type == AZONE_AREA)
1088                         WM_cursor_set(win, CURSOR_EDIT);
1089                 else if (az->type == AZONE_REGION) {
1090                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1091                                 WM_cursor_set(win, CURSOR_X_MOVE);
1092                         else
1093                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1094                 }
1095         }
1096         else {
1097                 ScrEdge *actedge = screen_find_active_scredge(screen, winsize_x, winsize_y, event->x, event->y);
1098                 
1099                 if (actedge) {
1100                         if (scredge_is_horizontal(actedge))
1101                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1102                         else
1103                                 WM_cursor_set(win, CURSOR_X_MOVE);
1104                 }
1105                 else
1106                         WM_cursor_set(win, CURSOR_STD);
1107         }
1108 }
1109
1110
1111 /* called in wm_event_system.c. sets state vars in screen, cursors */
1112 /* event type is mouse move */
1113 void ED_screen_set_subwinactive(bContext *C, const wmEvent *event)
1114 {
1115         wmWindow *win = CTX_wm_window(C);
1116         bScreen *scr = WM_window_get_active_screen(win);
1117
1118         if (scr) {
1119                 ScrArea *sa;
1120                 ARegion *ar;
1121                 int oldswin = scr->subwinactive;
1122
1123                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1124                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1125                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1126                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1127                                                 break;
1128                 }
1129                 if (sa) {
1130                         /* make overlap active when mouse over */
1131                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1132                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1133                                         scr->subwinactive = ar->swinid;
1134                                         break;
1135                                 }
1136                         }
1137                 }
1138                 else
1139                         scr->subwinactive = scr->mainwin;
1140                 
1141                 /* check for redraw headers */
1142                 if (oldswin != scr->subwinactive) {
1143
1144                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1145                                 bool do_draw = false;
1146                                 
1147                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1148                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1149                                                 do_draw = true;
1150                                 
1151                                 if (do_draw) {
1152                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1153                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1154                                                         ED_region_tag_redraw(ar);
1155                                 }
1156                         }
1157                 }
1158                 
1159                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1160                 if (scr->subwinactive == scr->mainwin) {
1161                         screen_cursor_set(win, event);
1162                 }
1163                 else {
1164                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1165                         if (oldswin != scr->subwinactive) {
1166                                 region_cursor_set(win, scr->subwinactive, true);
1167
1168                                 /* this used to be a notifier, but needs to be done immediate
1169                                  * because it can undo setting the right button as active due
1170                                  * to delayed notifier handling */
1171                                 UI_screen_free_active_but(C, scr);
1172                         }
1173                         else
1174                                 region_cursor_set(win, scr->subwinactive, false);
1175                 }
1176         }
1177 }
1178
1179 int ED_screen_area_active(const bContext *C)
1180 {
1181         wmWindow *win = CTX_wm_window(C);
1182         bScreen *sc = CTX_wm_screen(C);
1183         ScrArea *sa = CTX_wm_area(C);
1184
1185         if (win && sc && sa) {
1186                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1187                 ARegion *ar;
1188                 
1189                 if (az && az->type == AZONE_REGION)
1190                         return 1;
1191                 
1192                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1193                         if (ar->swinid == sc->subwinactive)
1194                                 return 1;
1195         }
1196         return 0;
1197 }
1198
1199
1200 /* -------------------------------------------------------------------- */
1201 /* Screen changing */
1202
1203 static bScreen *screen_fullscreen_find_associated_normal_screen(const Main *bmain, bScreen *screen)
1204 {
1205         for (bScreen *screen_iter = bmain->screen.first; screen_iter; screen_iter = screen_iter->id.next) {
1206                 ScrArea *sa = screen_iter->areabase.first;
1207                 if (sa->full == screen) {
1208                         return screen_iter;
1209                 }
1210         }
1211
1212         return screen;
1213 }
1214
1215 /**
1216  * \return the screen to activate.
1217  * \warning The returned screen may not always equal \a screen_new!
1218  */
1219 bScreen *screen_change_prepare(bScreen *screen_old, bScreen *screen_new, Main *bmain, bContext *C, wmWindow *win)
1220 {
1221         /* validate screen, it's called with notifier reference */
1222         if (BLI_findindex(&bmain->screen, screen_new) == -1) {
1223                 return NULL;
1224         }
1225
1226         if (ELEM(screen_new->state, SCREENMAXIMIZED, SCREENFULL)) {
1227                 screen_new = screen_fullscreen_find_associated_normal_screen(bmain, screen_new);
1228         }
1229
1230         /* check for valid winid */
1231         if (!(screen_new->winid == 0 || screen_new->winid == win->winid)) {
1232                 return NULL;
1233         }
1234
1235         if (screen_old != screen_new) {
1236                 wmTimer *wt = screen_old->animtimer;
1237
1238                 /* remove handlers referencing areas in old screen */
1239                 for (ScrArea *sa = screen_old->areabase.first; sa; sa = sa->next) {
1240                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1241                 }
1242
1243                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1244                 screen_old->animtimer = NULL;
1245                 if (wt) {
1246                         WM_event_timer_sleep(CTX_wm_manager(C), win, wt, true);
1247                 }
1248                 ED_screen_exit(C, win, screen_old);
1249
1250                 /* Same scene, "transfer" playback to new screen. */
1251                 if (wt) {
1252                         screen_new->animtimer = wt;
1253                 }
1254
1255                 return screen_new;
1256         }
1257
1258         return NULL;
1259 }
1260
1261 void screen_changed_update(bContext *C, wmWindow *win, bScreen *sc)
1262 {
1263         Scene *scene = WM_window_get_active_scene(win);
1264         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1265         WorkSpaceLayout *layout = BKE_workspace_layout_find(workspace, sc);
1266
1267         CTX_wm_window_set(C, win);  /* stores C->wm.screen... hrmf */
1268
1269         ED_screen_refresh(CTX_wm_manager(C), win);
1270
1271         BKE_screen_view3d_scene_sync(sc, scene); /* sync new screen with scene data */
1272         WM_event_add_notifier(C, NC_WINDOW, NULL);
1273         WM_event_add_notifier(C, NC_SCREEN | ND_LAYOUTSET, layout);
1274
1275         /* makes button hilites work */
1276         WM_event_add_mousemove(C);
1277 }
1278
1279
1280 /**
1281  * \brief Change the active screen.
1282  *
1283  * Operator call, WM + Window + screen already existed before
1284  *
1285  * \warning Do NOT call in area/region queues!
1286  * \returns if screen changing was successful.
1287  */
1288 bool ED_screen_change(bContext *C, bScreen *sc)
1289 {
1290         Main *bmain = CTX_data_main(C);
1291         wmWindow *win = CTX_wm_window(C);
1292         bScreen *screen_old = CTX_wm_screen(C);
1293         bScreen *screen_new = screen_change_prepare(screen_old, sc, bmain, C, win);
1294
1295         if (screen_new) {
1296                 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1297                 WM_window_set_active_screen(win, workspace, sc);
1298                 screen_changed_update(C, win, screen_new);
1299
1300                 return true;
1301         }
1302
1303         return false;
1304 }
1305
1306 static void screen_set_3dview_camera(Scene *scene, ViewLayer *view_layer, ScrArea *sa, View3D *v3d)
1307 {
1308         /* fix any cameras that are used in the 3d view but not in the scene */
1309         BKE_screen_view3d_sync(v3d, scene);
1310
1311         if (!v3d->camera || !BKE_view_layer_base_find(view_layer, v3d->camera)) {
1312                 v3d->camera = BKE_view_layer_camera_find(view_layer);
1313                 // XXX if (sc == curscreen) handle_view3d_lock();
1314                 if (!v3d->camera) {
1315                         ARegion *ar;
1316                         ListBase *regionbase;
1317                         
1318                         /* regionbase is in different place depending if space is active */
1319                         if (v3d == sa->spacedata.first)
1320                                 regionbase = &sa->regionbase;
1321                         else
1322                                 regionbase = &v3d->regionbase;
1323                                 
1324                         for (ar = regionbase->first; ar; ar = ar->next) {
1325                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1326                                         RegionView3D *rv3d = ar->regiondata;
1327                                         if (rv3d->persp == RV3D_CAMOB) {
1328                                                 rv3d->persp = RV3D_PERSP;
1329                                         }
1330                                 }
1331                         }
1332                 }
1333         }
1334 }
1335
1336 void ED_screen_update_after_scene_change(const bScreen *screen, Scene *scene_new, ViewLayer *view_layer)
1337 {
1338         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1339                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1340                         if (sl->spacetype == SPACE_VIEW3D) {
1341                                 View3D *v3d = (View3D *)sl;
1342                                 screen_set_3dview_camera(scene_new, view_layer, sa, v3d);
1343                         }
1344                 }
1345         }
1346 }
1347
1348 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1349 {
1350         wmWindow *win = CTX_wm_window(C);
1351         bScreen *screen = CTX_wm_screen(C);
1352         ScrArea *newsa = NULL;
1353
1354         if (!sa || sa->full == NULL) {
1355                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
1356         }
1357         
1358         if (!newsa) {
1359                 if (sa->full && (screen->state == SCREENMAXIMIZED)) {
1360                         /* if this has been called from the temporary info header generated in
1361                          * temp fullscreen layouts, find the correct fullscreen area to change
1362                          * to create a new space inside */
1363                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1364                                 if (!(sa->flag & AREA_TEMP_INFO))
1365                                         break;
1366                         }
1367                 }
1368                 else {
1369                         newsa = sa;
1370                 }
1371         }
1372
1373         BLI_assert(newsa);
1374
1375         if (sa && (sa->spacetype != type)) {
1376                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
1377         }
1378         else {
1379                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
1380         }
1381
1382         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
1383
1384         return newsa;
1385 }
1386
1387 /**
1388  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
1389  */
1390 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1391 {
1392         BLI_assert(sa->full);
1393
1394         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
1395                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
1396                 ED_area_prevspace(C, sa);
1397         }
1398         else {
1399                 ED_screen_restore_temp_type(C, sa);
1400         }
1401 }
1402
1403 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
1404 {
1405         /* incase nether functions below run */
1406         ED_area_tag_redraw(sa);
1407
1408         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1409                 ED_area_prevspace(C, sa);
1410                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1411         }
1412
1413         if (sa->full) {
1414                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1415         }
1416 }
1417
1418 /* restore a screen / area back to default operation, after temp fullscreen modes */
1419 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1420 {
1421         wmWindow *win = CTX_wm_window(C);
1422         SpaceLink *sl = sa->spacedata.first;
1423         bScreen *screen = CTX_wm_screen(C);
1424         short state = (screen ? screen->state : SCREENMAXIMIZED);
1425         
1426         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1427          * overlaid on top of an existing setup) then return to the previous space */
1428         
1429         if (sl->next) {
1430                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1431                         ED_screen_full_prevspace(C, sa);
1432                 }
1433                 else {
1434                         ED_screen_state_toggle(C, win, sa, state);
1435                 }
1436                 /* warning: 'sa' may be freed */
1437         }
1438         /* otherwise just tile the area again */
1439         else {
1440                 ED_screen_state_toggle(C, win, sa, state);
1441         }
1442 }
1443
1444 /**
1445  * this function toggles: if area is maximized/full then the parent will be restored
1446  *
1447  * \warning \a sa may be freed.
1448  */
1449 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1450 {
1451         wmWindowManager *wm = CTX_wm_manager(C);
1452         WorkSpace *workspace = WM_window_get_active_workspace(win);
1453         bScreen *sc, *oldscreen;
1454         ARegion *ar;
1455
1456         if (sa) {
1457                 /* ensure we don't have a button active anymore, can crash when
1458                  * switching screens with tooltip open because region and tooltip
1459                  * are no longer in the same screen */
1460                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1461                         UI_blocklist_free(C, &ar->uiblocks);
1462
1463                         if (ar->regiontimer) {
1464                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1465                                 ar->regiontimer = NULL;
1466                         }
1467                 }
1468
1469                 /* prevent hanging header prints */
1470                 ED_area_headerprint(sa, NULL);
1471         }
1472
1473         if (sa && sa->full) {
1474                 WorkSpaceLayout *layout_old = WM_window_get_active_layout(win);
1475                 /* restoring back to SCREENNORMAL */
1476                 sc = sa->full;       /* the old screen to restore */
1477                 oldscreen = WM_window_get_active_screen(win); /* the one disappearing */
1478
1479                 sc->state = SCREENNORMAL;
1480
1481                 /* find old area to restore from */
1482                 ScrArea *fullsa = NULL;
1483                 for (ScrArea *old = sc->areabase.first; old; old = old->next) {
1484                         /* area to restore from is always first */
1485                         if (old->full && !fullsa) {
1486                                 fullsa = old;
1487                         }
1488
1489                         /* clear full screen state */
1490                         old->full = NULL;
1491                         old->flag &= ~AREA_TEMP_INFO;
1492                 }
1493
1494                 sa->flag &= ~AREA_TEMP_INFO;
1495                 sa->full = NULL;
1496
1497                 if (fullsa == NULL) {
1498                         if (G.debug & G_DEBUG)
1499                                 printf("%s: something wrong in areafullscreen\n", __func__);
1500                         return NULL;
1501                 }
1502
1503                 if (state == SCREENFULL) {
1504                         /* restore the old side panels/header visibility */
1505                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1506                                 ar->flag = ar->flagfullscreen;
1507                         }
1508                 }
1509
1510                 ED_area_data_swap(fullsa, sa);
1511
1512                 /* animtimer back */
1513                 sc->animtimer = oldscreen->animtimer;
1514                 oldscreen->animtimer = NULL;
1515
1516                 ED_screen_change(C, sc);
1517
1518                 BKE_workspace_layout_remove(CTX_data_main(C), workspace, layout_old);
1519
1520                 /* After we've restored back to SCREENNORMAL, we have to wait with
1521                  * screen handling as it uses the area coords which aren't updated yet.
1522                  * Without doing so, the screen handling gets wrong area coords,
1523                  * which in worst case can lead to crashes (see T43139) */
1524                 sc->skip_handling = true;
1525         }
1526         else {
1527                 /* change from SCREENNORMAL to new state */
1528                 WorkSpaceLayout *layout_new;
1529                 ScrArea *newa;
1530                 char newname[MAX_ID_NAME - 2];
1531
1532                 oldscreen = WM_window_get_active_screen(win);
1533
1534                 oldscreen->state = state;
1535                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1536
1537                 layout_new = ED_workspace_layout_add(workspace, win, newname);
1538
1539                 sc = BKE_workspace_layout_screen_get(layout_new);
1540                 sc->state = state;
1541                 sc->redraws_flag = oldscreen->redraws_flag;
1542                 sc->temp = oldscreen->temp;
1543
1544                 /* timer */
1545                 sc->animtimer = oldscreen->animtimer;
1546                 oldscreen->animtimer = NULL;
1547
1548                 /* use random area when we have no active one, e.g. when the
1549                  * mouse is outside of the window and we open a file browser */
1550                 if (!sa)
1551                         sa = oldscreen->areabase.first;
1552
1553                 if (state == SCREENMAXIMIZED) {
1554                         /* returns the top small area */
1555                         newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1556                         ED_area_newspace(C, newa, SPACE_INFO, false);
1557
1558                         /* copy area */
1559                         newa = newa->prev;
1560                         ED_area_data_swap(newa, sa);
1561                         sa->flag |= AREA_TEMP_INFO;
1562
1563                         sa->full = oldscreen;
1564                         newa->full = oldscreen;
1565                         newa->next->full = oldscreen; // XXX
1566                 }
1567                 else if (state == SCREENFULL) {
1568                         newa = (ScrArea *)sc->areabase.first;
1569
1570                         /* copy area */
1571                         ED_area_data_swap(newa, sa);
1572                         newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1573
1574                         /* temporarily hide the side panels/header */
1575                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1576                                 ar->flagfullscreen = ar->flag;
1577
1578                                 if (ELEM(ar->regiontype,
1579                                          RGN_TYPE_UI,
1580                                          RGN_TYPE_HEADER,
1581                                          RGN_TYPE_TOOLS))
1582                                 {
1583                                         ar->flag |= RGN_FLAG_HIDDEN;
1584                                 }
1585                         }
1586
1587                         sa->full = oldscreen;
1588                         newa->full = oldscreen;
1589                 }
1590                 else {
1591                         BLI_assert(false);
1592                 }
1593
1594                 ED_screen_change(C, sc);
1595         }
1596
1597         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1598         CTX_wm_area_set(C, sc->areabase.first);
1599
1600         return sc->areabase.first;
1601 }
1602
1603 /* update frame rate info for viewport drawing */
1604 void ED_refresh_viewport_fps(bContext *C)
1605 {
1606         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1607         Scene *scene = CTX_data_scene(C);
1608         
1609         /* is anim playback running? */
1610         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1611                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1612                 
1613                 /* if there isn't any info, init it first */
1614                 if (fpsi == NULL)
1615                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1616                 
1617                 /* update the values */
1618                 fpsi->redrawtime = fpsi->lredrawtime;
1619                 fpsi->lredrawtime = animtimer->ltime;
1620         }
1621         else {
1622                 /* playback stopped or shouldn't be running */
1623                 if (scene->fps_info)
1624                         MEM_freeN(scene->fps_info);
1625                 scene->fps_info = NULL;
1626         }
1627 }
1628
1629 /* redraws: uses defines from stime->redraws 
1630  * enable: 1 - forward on, -1 - backwards on, 0 - off
1631  */
1632 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1633 {
1634         bScreen *screen = CTX_wm_screen(C);
1635         wmWindowManager *wm = CTX_wm_manager(C);
1636         wmWindow *win = CTX_wm_window(C);
1637         Scene *scene = CTX_data_scene(C);
1638         bScreen *stopscreen = ED_screen_animation_playing(wm);
1639         
1640         if (stopscreen) {
1641                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1642                 stopscreen->animtimer = NULL;
1643         }
1644         
1645         if (enable) {
1646                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1647                 
1648                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1649                 
1650                 sad->ar = CTX_wm_region(C);
1651                 /* if startframe is larger than current frame, we put currentframe on startframe.
1652                  * note: first frame then is not drawn! (ton) */
1653                 if (PRVRANGEON) {
1654                         if (scene->r.psfra > scene->r.cfra) {
1655                                 sad->sfra = scene->r.cfra;
1656                                 scene->r.cfra = scene->r.psfra;
1657                         }
1658                         else
1659                                 sad->sfra = scene->r.cfra;
1660                 }
1661                 else {
1662                         if (scene->r.sfra > scene->r.cfra) {
1663                                 sad->sfra = scene->r.cfra;
1664                                 scene->r.cfra = scene->r.sfra;
1665                         }
1666                         else
1667                                 sad->sfra = scene->r.cfra;
1668                 }
1669                 sad->redraws = redraws;
1670                 sad->refresh = refresh;
1671                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1672                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1673
1674                 ScrArea *sa = CTX_wm_area(C);
1675
1676                 char spacetype = -1;
1677
1678                 if (sa)
1679                         spacetype = sa->spacetype;
1680
1681                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA, SPACE_TIME));
1682
1683                 screen->animtimer->customdata = sad;
1684                 
1685         }
1686
1687         /* notifier catched by top header, for button */
1688         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1689 }
1690
1691 /* helper for screen_animation_play() - only to be used for TimeLine */
1692 static ARegion *time_top_left_3dwindow(bScreen *screen)
1693 {
1694         ARegion *aret = NULL;
1695         ScrArea *sa;
1696         int min = 10000;
1697         
1698         for (sa = screen->areabase.first; sa; sa = sa->next) {
1699                 if (sa->spacetype == SPACE_VIEW3D) {
1700                         ARegion *ar;
1701                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1702                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1703                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1704                                                 aret = ar;
1705                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1706                                         }
1707                                 }
1708                         }
1709                 }
1710         }
1711
1712         return aret;
1713 }
1714
1715 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1716 {
1717         if (screen && screen->animtimer) {
1718                 wmTimer *wt = screen->animtimer;
1719                 ScreenAnimData *sad = wt->customdata;
1720                 
1721                 sad->redraws = redraws;
1722                 sad->refresh = refresh;
1723                 sad->ar = NULL;
1724                 if (redraws & TIME_REGION)
1725                         sad->ar = time_top_left_3dwindow(screen);
1726         }
1727 }
1728
1729 /* results in fully updated anim system */
1730 void ED_update_for_newframe(Main *bmain, Scene *scene, ViewLayer *view_layer, struct Depsgraph *depsgraph)
1731 {
1732 #ifdef DURIAN_CAMERA_SWITCH
1733         void *camera = BKE_scene_camera_switch_find(scene);
1734         if (camera && scene->camera != camera) {
1735                 bScreen *sc;
1736                 scene->camera = camera;
1737                 /* are there cameras in the views that are not in the scene? */
1738                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1739                         BKE_screen_view3d_scene_sync(sc, scene);
1740                 }
1741         }
1742 #endif
1743         
1744         ED_clip_update_frame(bmain, scene->r.cfra);
1745
1746         /* this function applies the changes too */
1747         BKE_scene_graph_update_for_newframe(bmain->eval_ctx, depsgraph, bmain, scene, view_layer);
1748
1749         /* composite */
1750         if (scene->use_nodes && scene->nodetree)
1751                 ntreeCompositTagAnimated(scene->nodetree);
1752         
1753         /* update animated texture nodes */
1754         {
1755                 Tex *tex;
1756                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
1757                         if (tex->use_nodes && tex->nodetree) {
1758                                 ntreeTexTagAnimated(tex->nodetree);
1759                         }
1760                 }
1761         }
1762         
1763 }
1764
1765 /*
1766  * return true if any active area requires to see in 3D
1767  */
1768 bool ED_screen_stereo3d_required(const bScreen *screen, const Scene *scene)
1769 {
1770         ScrArea *sa;
1771         const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
1772
1773         for (sa = screen->areabase.first; sa; sa = sa->next) {
1774                 switch (sa->spacetype) {
1775                         case SPACE_VIEW3D:
1776                         {
1777                                 View3D *v3d;
1778
1779                                 if (!is_multiview)
1780                                         continue;
1781
1782                                 v3d = sa->spacedata.first;
1783                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
1784                                         ARegion *ar;
1785                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1786                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
1787                                                         RegionView3D *rv3d = ar->regiondata;
1788                                                         if (rv3d->persp == RV3D_CAMOB) {
1789                                                                 return true;
1790                                                         }
1791                                                 }
1792                                         }
1793                                 }
1794                                 break;
1795                         }
1796                         case SPACE_IMAGE:
1797                         {
1798                                 SpaceImage *sima;
1799
1800                                 /* images should always show in stereo, even if
1801                                  * the file doesn't have views enabled */
1802                                 sima = sa->spacedata.first;
1803                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
1804                                     (sima->iuser.flag & IMA_SHOW_STEREO))
1805                                 {
1806                                         return true;
1807                                 }
1808                                 break;
1809                         }
1810                         case SPACE_NODE:
1811                         {
1812                                 SpaceNode *snode;
1813
1814                                 if (!is_multiview)
1815                                         continue;
1816
1817                                 snode = sa->spacedata.first;
1818                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
1819                                         return true;
1820                                 }
1821                                 break;
1822                         }
1823                         case SPACE_SEQ:
1824                         {
1825                                 SpaceSeq *sseq;
1826
1827                                 if (!is_multiview)
1828                                         continue;
1829
1830                                 sseq = sa->spacedata.first;
1831                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
1832                                         return true;
1833                                 }
1834
1835                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1836                                         return true;
1837                                 }
1838
1839                                 break;
1840                         }
1841                 }
1842         }
1843
1844         return false;
1845 }
1846
1847 /**
1848  * Find the scene displayed in \a screen.
1849  * \note Assumes \a screen to be visible/active!
1850  */
1851 Scene *ED_screen_scene_find(const bScreen *screen, const wmWindowManager *wm)
1852 {
1853         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1854                 if (WM_window_get_active_screen(win) == screen) {
1855                         return WM_window_get_active_scene(win);
1856                 }
1857         }
1858
1859         BLI_assert(0);
1860         return NULL;
1861 }