Merge branch 'master' into blender2.8
[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 #include "WM_message.h"
70
71 /* XXX actually should be not here... solve later */
72 #include "wm_subwindow.h"
73
74 #include "screen_intern.h"  /* own module include */
75
76
77 /* ******************* screen vert, edge, area managing *********************** */
78
79 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
80 {
81         ScrVert *sv = MEM_callocN(sizeof(ScrVert), "addscrvert");
82         sv->vec.x = x;
83         sv->vec.y = y;
84         
85         BLI_addtail(&sc->vertbase, sv);
86         return sv;
87 }
88
89 static void sortscrvert(ScrVert **v1, ScrVert **v2)
90 {
91         ScrVert *tmp;
92         
93         if (*v1 > *v2) {
94                 tmp = *v1;
95                 *v1 = *v2;
96                 *v2 = tmp;
97         }
98 }
99
100 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
101 {
102         ScrEdge *se = MEM_callocN(sizeof(ScrEdge), "addscredge");
103         
104         sortscrvert(&v1, &v2);
105         se->v1 = v1;
106         se->v2 = v2;
107         
108         BLI_addtail(&sc->edgebase, se);
109         return se;
110 }
111
112
113 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
114 {
115         ScrEdge *se;
116         
117         sortscrvert(&v1, &v2);
118         for (se = sc->edgebase.first; se; se = se->next)
119                 if (se->v1 == v1 && se->v2 == v2)
120                         return se;
121         
122         return NULL;
123 }
124
125 void removedouble_scrverts(bScreen *sc)
126 {
127         ScrVert *v1, *verg;
128         ScrEdge *se;
129         ScrArea *sa;
130         
131         verg = sc->vertbase.first;
132         while (verg) {
133                 if (verg->newv == NULL) { /* !!! */
134                         v1 = verg->next;
135                         while (v1) {
136                                 if (v1->newv == NULL) {   /* !?! */
137                                         if (v1->vec.x == verg->vec.x && v1->vec.y == verg->vec.y) {
138                                                 /* printf("doublevert\n"); */
139                                                 v1->newv = verg;
140                                         }
141                                 }
142                                 v1 = v1->next;
143                         }
144                 }
145                 verg = verg->next;
146         }
147
148         /* replace pointers in edges and faces */
149         se = sc->edgebase.first;
150         while (se) {
151                 if (se->v1->newv) se->v1 = se->v1->newv;
152                 if (se->v2->newv) se->v2 = se->v2->newv;
153                 /* edges changed: so.... */
154                 sortscrvert(&(se->v1), &(se->v2));
155                 se = se->next;
156         }
157         sa = sc->areabase.first;
158         while (sa) {
159                 if (sa->v1->newv) sa->v1 = sa->v1->newv;
160                 if (sa->v2->newv) sa->v2 = sa->v2->newv;
161                 if (sa->v3->newv) sa->v3 = sa->v3->newv;
162                 if (sa->v4->newv) sa->v4 = sa->v4->newv;
163                 sa = sa->next;
164         }
165
166         /* remove */
167         verg = sc->vertbase.first;
168         while (verg) {
169                 v1 = verg->next;
170                 if (verg->newv) {
171                         BLI_remlink(&sc->vertbase, verg);
172                         MEM_freeN(verg);
173                 }
174                 verg = v1;
175         }
176
177 }
178
179 void removenotused_scrverts(bScreen *sc)
180 {
181         ScrVert *sv, *svn;
182         ScrEdge *se;
183         
184         /* we assume edges are ok */
185         
186         se = sc->edgebase.first;
187         while (se) {
188                 se->v1->flag = 1;
189                 se->v2->flag = 1;
190                 se = se->next;
191         }
192         
193         sv = sc->vertbase.first;
194         while (sv) {
195                 svn = sv->next;
196                 if (sv->flag == 0) {
197                         BLI_remlink(&sc->vertbase, sv);
198                         MEM_freeN(sv);
199                 }
200                 else {
201                         sv->flag = 0;
202                 }
203                 sv = svn;
204         }
205 }
206
207 void removedouble_scredges(bScreen *sc)
208 {
209         ScrEdge *verg, *se, *sn;
210         
211         /* compare */
212         verg = sc->edgebase.first;
213         while (verg) {
214                 se = verg->next;
215                 while (se) {
216                         sn = se->next;
217                         if (verg->v1 == se->v1 && verg->v2 == se->v2) {
218                                 BLI_remlink(&sc->edgebase, se);
219                                 MEM_freeN(se);
220                         }
221                         se = sn;
222                 }
223                 verg = verg->next;
224         }
225 }
226
227 void removenotused_scredges(bScreen *sc)
228 {
229         ScrEdge *se, *sen;
230         ScrArea *sa;
231         int a = 0;
232         
233         /* sets flags when edge is used in area */
234         sa = sc->areabase.first;
235         while (sa) {
236                 se = screen_findedge(sc, sa->v1, sa->v2);
237                 if (se == NULL) printf("error: area %d edge 1 doesn't exist\n", a);
238                 else se->flag = 1;
239                 se = screen_findedge(sc, sa->v2, sa->v3);
240                 if (se == NULL) printf("error: area %d edge 2 doesn't exist\n", a);
241                 else se->flag = 1;
242                 se = screen_findedge(sc, sa->v3, sa->v4);
243                 if (se == NULL) printf("error: area %d edge 3 doesn't exist\n", a);
244                 else se->flag = 1;
245                 se = screen_findedge(sc, sa->v4, sa->v1);
246                 if (se == NULL) printf("error: area %d edge 4 doesn't exist\n", a);
247                 else se->flag = 1;
248                 sa = sa->next;
249                 a++;
250         }
251         se = sc->edgebase.first;
252         while (se) {
253                 sen = se->next;
254                 if (se->flag == 0) {
255                         BLI_remlink(&sc->edgebase, se);
256                         MEM_freeN(se);
257                 }
258                 else {
259                         se->flag = 0;
260                 }
261                 se = sen;
262         }
263 }
264
265 bool scredge_is_horizontal(ScrEdge *se)
266 {
267         return (se->v1->vec.y == se->v2->vec.y);
268 }
269
270 /* need win size to make sure not to include edges along screen edge */
271 ScrEdge *screen_find_active_scredge(const bScreen *sc,
272                                     const int winsize_x, const int winsize_y,
273                                     const int mx, const int my)
274 {
275         ScrEdge *se;
276         int safety = U.widget_unit / 10;
277         
278         if (safety < 2) safety = 2;
279         
280         for (se = sc->edgebase.first; se; se = se->next) {
281                 if (scredge_is_horizontal(se)) {
282                         if (se->v1->vec.y > 0 && se->v1->vec.y < winsize_y - 1) {
283                                 short min, max;
284                                 min = MIN2(se->v1->vec.x, se->v2->vec.x);
285                                 max = MAX2(se->v1->vec.x, se->v2->vec.x);
286                                 
287                                 if (abs(my - se->v1->vec.y) <= safety && mx >= min && mx <= max)
288                                         return se;
289                         }
290                 }
291                 else {
292                         if (se->v1->vec.x > 0 && se->v1->vec.x < winsize_x - 1) {
293                                 short min, max;
294                                 min = MIN2(se->v1->vec.y, se->v2->vec.y);
295                                 max = MAX2(se->v1->vec.y, se->v2->vec.y);
296                                 
297                                 if (abs(mx - se->v1->vec.x) <= safety && my >= min && my <= max)
298                                         return se;
299                         }
300                 }
301         }
302         
303         return NULL;
304 }
305
306
307
308 /* adds no space data */
309 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
310 {
311         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
312         sa->v1 = v1;
313         sa->v2 = v2;
314         sa->v3 = v3;
315         sa->v4 = v4;
316         sa->headertype = headertype;
317         sa->spacetype = sa->butspacetype = spacetype;
318         
319         BLI_addtail(&sc->areabase, sa);
320         
321         return sa;
322 }
323
324 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
325 {
326         
327         ED_area_exit(C, sa);
328         
329         BKE_screen_area_free(sa);
330         
331         BLI_remlink(&sc->areabase, sa);
332         MEM_freeN(sa);
333 }
334
335 /* return 0: no split possible */
336 /* else return (integer) screencoordinate split point */
337 static short testsplitpoint(ScrArea *sa, char dir, float fac)
338 {
339         short x, y;
340         const short area_min_x = AREAMINX;
341         const short area_min_y = ED_area_headersize();
342         
343         // area big enough?
344         if (dir == 'v' && (sa->v4->vec.x - sa->v1->vec.x <= 2 * area_min_x)) return 0;
345         if (dir == 'h' && (sa->v2->vec.y - sa->v1->vec.y <= 2 * area_min_y)) return 0;
346         
347         // to be sure
348         CLAMP(fac, 0.0f, 1.0f);
349         
350         if (dir == 'h') {
351                 y = sa->v1->vec.y + fac * (sa->v2->vec.y - sa->v1->vec.y);
352                 
353                 if (y - sa->v1->vec.y < area_min_y)
354                         y = sa->v1->vec.y + area_min_y;
355                 else if (sa->v2->vec.y - y < area_min_y)
356                         y = sa->v2->vec.y - area_min_y;
357                 else y -= (y % AREAGRID);
358                 
359                 return y;
360         }
361         else {
362                 x = sa->v1->vec.x + fac * (sa->v4->vec.x - sa->v1->vec.x);
363                 
364                 if (x - sa->v1->vec.x < area_min_x)
365                         x = sa->v1->vec.x + area_min_x;
366                 else if (sa->v4->vec.x - x < area_min_x)
367                         x = sa->v4->vec.x - area_min_x;
368                 else x -= (x % AREAGRID);
369                 
370                 return x;
371         }
372 }
373
374 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
375 {
376         ScrArea *newa = NULL;
377         ScrVert *sv1, *sv2;
378         short split;
379         
380         if (sa == NULL) return NULL;
381         
382         split = testsplitpoint(sa, dir, fac);
383         if (split == 0) return NULL;
384         
385         /* note regarding (fac > 0.5f) checks below.
386          * normally it shouldn't matter which is used since the copy should match the original
387          * however with viewport rendering and python console this isn't the case. - campbell */
388
389         if (dir == 'h') {
390                 /* new vertices */
391                 sv1 = screen_addvert(sc, sa->v1->vec.x, split);
392                 sv2 = screen_addvert(sc, sa->v4->vec.x, split);
393                 
394                 /* new edges */
395                 screen_addedge(sc, sa->v1, sv1);
396                 screen_addedge(sc, sv1, sa->v2);
397                 screen_addedge(sc, sa->v3, sv2);
398                 screen_addedge(sc, sv2, sa->v4);
399                 screen_addedge(sc, sv1, sv2);
400                 
401                 if (fac > 0.5f) {
402                         /* new areas: top */
403                         newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
404
405                         /* area below */
406                         sa->v2 = sv1;
407                         sa->v3 = sv2;
408                 }
409                 else {
410                         /* new areas: bottom */
411                         newa = screen_addarea(sc, sa->v1, sv1, sv2, sa->v4, sa->headertype, sa->spacetype);
412
413                         /* area above */
414                         sa->v1 = sv1;
415                         sa->v4 = sv2;
416                 }
417
418                 ED_area_data_copy(newa, sa, true);
419                 
420         }
421         else {
422                 /* new vertices */
423                 sv1 = screen_addvert(sc, split, sa->v1->vec.y);
424                 sv2 = screen_addvert(sc, split, sa->v2->vec.y);
425                 
426                 /* new edges */
427                 screen_addedge(sc, sa->v1, sv1);
428                 screen_addedge(sc, sv1, sa->v4);
429                 screen_addedge(sc, sa->v2, sv2);
430                 screen_addedge(sc, sv2, sa->v3);
431                 screen_addedge(sc, sv1, sv2);
432                 
433                 if (fac > 0.5f) {
434                         /* new areas: right */
435                         newa = screen_addarea(sc, sv1, sv2, sa->v3, sa->v4, sa->headertype, sa->spacetype);
436
437                         /* area left */
438                         sa->v3 = sv2;
439                         sa->v4 = sv1;
440                 }
441                 else {
442                         /* new areas: left */
443                         newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
444
445                         /* area right */
446                         sa->v1 = sv1;
447                         sa->v2 = sv2;
448                 }
449
450                 ED_area_data_copy(newa, sa, true);
451         }
452         
453         /* remove double vertices en edges */
454         if (merge)
455                 removedouble_scrverts(sc);
456         removedouble_scredges(sc);
457         removenotused_scredges(sc);
458         
459         return newa;
460 }
461
462 /**
463  * Empty screen, with 1 dummy area without spacedata. Uses window size.
464  */
465 bScreen *screen_add(const char *name, const int winsize_x, const int winsize_y)
466 {
467         bScreen *sc;
468         ScrVert *sv1, *sv2, *sv3, *sv4;
469         
470         sc = BKE_libblock_alloc(G.main, ID_SCR, name, 0);
471         sc->do_refresh = true;
472         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
473
474         sv1 = screen_addvert(sc, 0, 0);
475         sv2 = screen_addvert(sc, 0, winsize_y - 1);
476         sv3 = screen_addvert(sc, winsize_x - 1, winsize_y - 1);
477         sv4 = screen_addvert(sc, winsize_x - 1, 0);
478         
479         screen_addedge(sc, sv1, sv2);
480         screen_addedge(sc, sv2, sv3);
481         screen_addedge(sc, sv3, sv4);
482         screen_addedge(sc, sv4, sv1);
483         
484         /* dummy type, no spacedata */
485         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
486                 
487         return sc;
488 }
489
490 void screen_data_copy(bScreen *to, bScreen *from)
491 {
492         ScrVert *s1, *s2;
493         ScrEdge *se;
494         ScrArea *sa, *saf;
495         
496         /* free contents of 'to', is from blenkernel screen.c */
497         BKE_screen_free(to);
498         
499         BLI_duplicatelist(&to->vertbase, &from->vertbase);
500         BLI_duplicatelist(&to->edgebase, &from->edgebase);
501         BLI_duplicatelist(&to->areabase, &from->areabase);
502         BLI_listbase_clear(&to->regionbase);
503         
504         s2 = to->vertbase.first;
505         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
506                 s1->newv = s2;
507         }
508         
509         for (se = to->edgebase.first; se; se = se->next) {
510                 se->v1 = se->v1->newv;
511                 se->v2 = se->v2->newv;
512                 sortscrvert(&(se->v1), &(se->v2));
513         }
514         
515         saf = from->areabase.first;
516         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
517                 sa->v1 = sa->v1->newv;
518                 sa->v2 = sa->v2->newv;
519                 sa->v3 = sa->v3->newv;
520                 sa->v4 = sa->v4->newv;
521
522                 BLI_listbase_clear(&sa->spacedata);
523                 BLI_listbase_clear(&sa->regionbase);
524                 BLI_listbase_clear(&sa->actionzones);
525                 BLI_listbase_clear(&sa->handlers);
526                 
527                 ED_area_data_copy(sa, saf, true);
528         }
529         
530         /* put at zero (needed?) */
531         for (s1 = from->vertbase.first; s1; s1 = s1->next)
532                 s1->newv = NULL;
533 }
534
535 /**
536  * Prepare a newly created screen for initializing it as active screen.
537  */
538 void screen_new_activate_prepare(const wmWindow *win, bScreen *screen_new)
539 {
540         screen_new->winid = win->winid;
541         screen_new->do_refresh = true;
542         screen_new->do_draw = true;
543 }
544
545
546 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
547 /* -1 = not valid check */
548 /* used with join operator */
549 int area_getorientation(ScrArea *sa, ScrArea *sb)
550 {
551         ScrVert *sav1, *sav2, *sav3, *sav4;
552         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
553
554         if (sa == NULL || sb == NULL) return -1;
555
556         sav1 = sa->v1;
557         sav2 = sa->v2;
558         sav3 = sa->v3;
559         sav4 = sa->v4;
560         sbv1 = sb->v1;
561         sbv2 = sb->v2;
562         sbv3 = sb->v3;
563         sbv4 = sb->v4;
564         
565         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
566                 return 0;
567         }
568         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
569                 return 1;
570         }
571         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
572                 return 2;
573         }
574         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
575                 return 3;
576         }
577         
578         return -1;
579 }
580
581 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
582  *  used by the split, join operators
583  */
584 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
585 {
586         int dir;
587         
588         dir = area_getorientation(sa1, sa2);
589         /*printf("dir is : %i\n", dir);*/
590         
591         if (dir == -1) {
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
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         WM_msgbus_clear_by_owner(wm->message_bus, ar);
1008
1009         CTX_wm_region_set(C, prevar);
1010 }
1011
1012 void ED_area_exit(bContext *C, ScrArea *sa)
1013 {
1014         wmWindowManager *wm = CTX_wm_manager(C);
1015         wmWindow *win = CTX_wm_window(C);
1016         ScrArea *prevsa = CTX_wm_area(C);
1017         ARegion *ar;
1018
1019         if (sa->type && sa->type->exit)
1020                 sa->type->exit(wm, sa);
1021
1022         CTX_wm_area_set(C, sa);
1023
1024         for (ar = sa->regionbase.first; ar; ar = ar->next)
1025                 ED_region_exit(C, ar);
1026
1027         WM_event_remove_handlers(C, &sa->handlers);
1028         WM_event_modal_handler_area_replace(win, sa, NULL);
1029
1030         CTX_wm_area_set(C, prevsa);
1031 }
1032
1033 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1034 {
1035         wmWindowManager *wm = CTX_wm_manager(C);
1036         wmWindow *prevwin = CTX_wm_window(C);
1037         ScrArea *sa;
1038         ARegion *ar;
1039
1040         CTX_wm_window_set(C, window);
1041         
1042         if (screen->animtimer)
1043                 WM_event_remove_timer(wm, window, screen->animtimer);
1044         screen->animtimer = NULL;
1045         screen->scrubbing = false;
1046
1047         if (screen->mainwin)
1048                 wm_subwindow_close(window, screen->mainwin);
1049         screen->mainwin = 0;
1050         screen->subwinactive = 0;
1051         
1052         for (ar = screen->regionbase.first; ar; ar = ar->next)
1053                 ED_region_exit(C, ar);
1054
1055         for (sa = screen->areabase.first; sa; sa = sa->next)
1056                 ED_area_exit(C, sa);
1057
1058         /* mark it available for use for other windows */
1059         screen->winid = 0;
1060         
1061         if (!WM_window_is_temp_screen(prevwin)) {
1062                 /* use previous window if possible */
1063                 CTX_wm_window_set(C, prevwin);
1064         }
1065         else {
1066                 /* none otherwise */
1067                 CTX_wm_window_set(C, NULL);
1068         }
1069         
1070 }
1071
1072 /* *********************************** */
1073
1074 /* case when on area-edge or in azones, or outside window */
1075 static void screen_cursor_set(wmWindow *win, const wmEvent *event)
1076 {
1077         const bScreen *screen = WM_window_get_active_screen(win);
1078         const int winsize_x = WM_window_pixels_x(win);
1079         const int winsize_y = WM_window_pixels_y(win);
1080
1081         AZone *az = NULL;
1082         ScrArea *sa;
1083         
1084         for (sa = screen->areabase.first; sa; sa = sa->next)
1085                 if ((az = is_in_area_actionzone(sa, &event->x)))
1086                         break;
1087         
1088         if (sa) {
1089                 if (az->type == AZONE_AREA)
1090                         WM_cursor_set(win, CURSOR_EDIT);
1091                 else if (az->type == AZONE_REGION) {
1092                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1093                                 WM_cursor_set(win, CURSOR_X_MOVE);
1094                         else
1095                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1096                 }
1097         }
1098         else {
1099                 ScrEdge *actedge = screen_find_active_scredge(screen, winsize_x, winsize_y, event->x, event->y);
1100                 
1101                 if (actedge) {
1102                         if (scredge_is_horizontal(actedge))
1103                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1104                         else
1105                                 WM_cursor_set(win, CURSOR_X_MOVE);
1106                 }
1107                 else
1108                         WM_cursor_set(win, CURSOR_STD);
1109         }
1110 }
1111
1112
1113 /* called in wm_event_system.c. sets state vars in screen, cursors */
1114 /* event type is mouse move */
1115 void ED_screen_set_subwinactive(bContext *C, const wmEvent *event)
1116 {
1117         wmWindow *win = CTX_wm_window(C);
1118         bScreen *scr = WM_window_get_active_screen(win);
1119
1120         if (scr) {
1121                 ScrArea *sa;
1122                 ARegion *ar;
1123                 int oldswin = scr->subwinactive;
1124
1125                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1126                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1127                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1128                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1129                                                 break;
1130                 }
1131                 if (sa) {
1132                         /* make overlap active when mouse over */
1133                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1134                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1135                                         scr->subwinactive = ar->swinid;
1136                                         break;
1137                                 }
1138                         }
1139                 }
1140                 else
1141                         scr->subwinactive = scr->mainwin;
1142                 
1143                 /* check for redraw headers */
1144                 if (oldswin != scr->subwinactive) {
1145
1146                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1147                                 bool do_draw = false;
1148                                 
1149                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1150                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1151                                                 do_draw = true;
1152                                 
1153                                 if (do_draw) {
1154                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1155                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1156                                                         ED_region_tag_redraw(ar);
1157                                 }
1158                         }
1159                 }
1160                 
1161                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1162                 if (scr->subwinactive == scr->mainwin) {
1163                         screen_cursor_set(win, event);
1164                 }
1165                 else {
1166                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1167                         if (oldswin != scr->subwinactive) {
1168                                 region_cursor_set(win, scr->subwinactive, true);
1169
1170                                 /* this used to be a notifier, but needs to be done immediate
1171                                  * because it can undo setting the right button as active due
1172                                  * to delayed notifier handling */
1173                                 UI_screen_free_active_but(C, scr);
1174                         }
1175                         else
1176                                 region_cursor_set(win, scr->subwinactive, false);
1177                 }
1178         }
1179 }
1180
1181 int ED_screen_area_active(const bContext *C)
1182 {
1183         wmWindow *win = CTX_wm_window(C);
1184         bScreen *sc = CTX_wm_screen(C);
1185         ScrArea *sa = CTX_wm_area(C);
1186
1187         if (win && sc && sa) {
1188                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1189                 ARegion *ar;
1190                 
1191                 if (az && az->type == AZONE_REGION)
1192                         return 1;
1193                 
1194                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1195                         if (ar->swinid == sc->subwinactive)
1196                                 return 1;
1197         }
1198         return 0;
1199 }
1200
1201
1202 /* -------------------------------------------------------------------- */
1203 /* Screen changing */
1204
1205 static bScreen *screen_fullscreen_find_associated_normal_screen(const Main *bmain, bScreen *screen)
1206 {
1207         for (bScreen *screen_iter = bmain->screen.first; screen_iter; screen_iter = screen_iter->id.next) {
1208                 ScrArea *sa = screen_iter->areabase.first;
1209                 if (sa->full == screen) {
1210                         return screen_iter;
1211                 }
1212         }
1213
1214         return screen;
1215 }
1216
1217 /**
1218  * \return the screen to activate.
1219  * \warning The returned screen may not always equal \a screen_new!
1220  */
1221 bScreen *screen_change_prepare(bScreen *screen_old, bScreen *screen_new, Main *bmain, bContext *C, wmWindow *win)
1222 {
1223         /* validate screen, it's called with notifier reference */
1224         if (BLI_findindex(&bmain->screen, screen_new) == -1) {
1225                 return NULL;
1226         }
1227
1228         if (ELEM(screen_new->state, SCREENMAXIMIZED, SCREENFULL)) {
1229                 screen_new = screen_fullscreen_find_associated_normal_screen(bmain, screen_new);
1230         }
1231
1232         /* check for valid winid */
1233         if (!(screen_new->winid == 0 || screen_new->winid == win->winid)) {
1234                 return NULL;
1235         }
1236
1237         if (screen_old != screen_new) {
1238                 wmTimer *wt = screen_old->animtimer;
1239
1240                 /* remove handlers referencing areas in old screen */
1241                 for (ScrArea *sa = screen_old->areabase.first; sa; sa = sa->next) {
1242                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1243                 }
1244
1245                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1246                 screen_old->animtimer = NULL;
1247                 if (wt) {
1248                         WM_event_timer_sleep(CTX_wm_manager(C), win, wt, true);
1249                 }
1250                 ED_screen_exit(C, win, screen_old);
1251
1252                 /* Same scene, "transfer" playback to new screen. */
1253                 if (wt) {
1254                         screen_new->animtimer = wt;
1255                 }
1256
1257                 return screen_new;
1258         }
1259
1260         return NULL;
1261 }
1262
1263 void screen_changed_update(bContext *C, wmWindow *win, bScreen *sc)
1264 {
1265         Scene *scene = WM_window_get_active_scene(win);
1266         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1267         WorkSpaceLayout *layout = BKE_workspace_layout_find(workspace, sc);
1268
1269         CTX_wm_window_set(C, win);  /* stores C->wm.screen... hrmf */
1270
1271         ED_screen_refresh(CTX_wm_manager(C), win);
1272
1273         BKE_screen_view3d_scene_sync(sc, scene); /* sync new screen with scene data */
1274         WM_event_add_notifier(C, NC_WINDOW, NULL);
1275         WM_event_add_notifier(C, NC_SCREEN | ND_LAYOUTSET, layout);
1276
1277         /* makes button hilites work */
1278         WM_event_add_mousemove(C);
1279 }
1280
1281
1282 /**
1283  * \brief Change the active screen.
1284  *
1285  * Operator call, WM + Window + screen already existed before
1286  *
1287  * \warning Do NOT call in area/region queues!
1288  * \returns if screen changing was successful.
1289  */
1290 bool ED_screen_change(bContext *C, bScreen *sc)
1291 {
1292         Main *bmain = CTX_data_main(C);
1293         wmWindow *win = CTX_wm_window(C);
1294         bScreen *screen_old = CTX_wm_screen(C);
1295         bScreen *screen_new = screen_change_prepare(screen_old, sc, bmain, C, win);
1296
1297         if (screen_new) {
1298                 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1299                 WM_window_set_active_screen(win, workspace, sc);
1300                 screen_changed_update(C, win, screen_new);
1301
1302                 return true;
1303         }
1304
1305         return false;
1306 }
1307
1308 static void screen_set_3dview_camera(Scene *scene, ViewLayer *view_layer, ScrArea *sa, View3D *v3d)
1309 {
1310         /* fix any cameras that are used in the 3d view but not in the scene */
1311         BKE_screen_view3d_sync(v3d, scene);
1312
1313         if (!v3d->camera || !BKE_view_layer_base_find(view_layer, v3d->camera)) {
1314                 v3d->camera = BKE_view_layer_camera_find(view_layer);
1315                 // XXX if (sc == curscreen) handle_view3d_lock();
1316                 if (!v3d->camera) {
1317                         ARegion *ar;
1318                         ListBase *regionbase;
1319                         
1320                         /* regionbase is in different place depending if space is active */
1321                         if (v3d == sa->spacedata.first)
1322                                 regionbase = &sa->regionbase;
1323                         else
1324                                 regionbase = &v3d->regionbase;
1325                                 
1326                         for (ar = regionbase->first; ar; ar = ar->next) {
1327                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1328                                         RegionView3D *rv3d = ar->regiondata;
1329                                         if (rv3d->persp == RV3D_CAMOB) {
1330                                                 rv3d->persp = RV3D_PERSP;
1331                                         }
1332                                 }
1333                         }
1334                 }
1335         }
1336 }
1337
1338 void ED_screen_update_after_scene_change(const bScreen *screen, Scene *scene_new, ViewLayer *view_layer)
1339 {
1340         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1341                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1342                         if (sl->spacetype == SPACE_VIEW3D) {
1343                                 View3D *v3d = (View3D *)sl;
1344                                 screen_set_3dview_camera(scene_new, view_layer, sa, v3d);
1345                         }
1346                 }
1347         }
1348 }
1349
1350 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1351 {
1352         wmWindow *win = CTX_wm_window(C);
1353         bScreen *screen = CTX_wm_screen(C);
1354         ScrArea *newsa = NULL;
1355
1356         if (!sa || sa->full == NULL) {
1357                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
1358         }
1359         
1360         if (!newsa) {
1361                 if (sa->full && (screen->state == SCREENMAXIMIZED)) {
1362                         /* if this has been called from the temporary info header generated in
1363                          * temp fullscreen layouts, find the correct fullscreen area to change
1364                          * to create a new space inside */
1365                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1366                                 if (!(sa->flag & AREA_TEMP_INFO))
1367                                         break;
1368                         }
1369                 }
1370                 else {
1371                         newsa = sa;
1372                 }
1373         }
1374
1375         BLI_assert(newsa);
1376
1377         if (sa && (sa->spacetype != type)) {
1378                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
1379         }
1380         else {
1381                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
1382         }
1383
1384         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
1385
1386         return newsa;
1387 }
1388
1389 /**
1390  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
1391  */
1392 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1393 {
1394         BLI_assert(sa->full);
1395
1396         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
1397                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
1398                 ED_area_prevspace(C, sa);
1399         }
1400         else {
1401                 ED_screen_restore_temp_type(C, sa);
1402         }
1403 }
1404
1405 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
1406 {
1407         /* incase nether functions below run */
1408         ED_area_tag_redraw(sa);
1409
1410         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1411                 ED_area_prevspace(C, sa);
1412                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1413         }
1414
1415         if (sa->full) {
1416                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1417         }
1418 }
1419
1420 /* restore a screen / area back to default operation, after temp fullscreen modes */
1421 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1422 {
1423         wmWindow *win = CTX_wm_window(C);
1424         SpaceLink *sl = sa->spacedata.first;
1425         bScreen *screen = CTX_wm_screen(C);
1426         short state = (screen ? screen->state : SCREENMAXIMIZED);
1427         
1428         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1429          * overlaid on top of an existing setup) then return to the previous space */
1430         
1431         if (sl->next) {
1432                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1433                         ED_screen_full_prevspace(C, sa);
1434                 }
1435                 else {
1436                         ED_screen_state_toggle(C, win, sa, state);
1437                 }
1438                 /* warning: 'sa' may be freed */
1439         }
1440         /* otherwise just tile the area again */
1441         else {
1442                 ED_screen_state_toggle(C, win, sa, state);
1443         }
1444 }
1445
1446 /**
1447  * this function toggles: if area is maximized/full then the parent will be restored
1448  *
1449  * \warning \a sa may be freed.
1450  */
1451 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1452 {
1453         wmWindowManager *wm = CTX_wm_manager(C);
1454         WorkSpace *workspace = WM_window_get_active_workspace(win);
1455         bScreen *sc, *oldscreen;
1456         ARegion *ar;
1457
1458         if (sa) {
1459                 /* ensure we don't have a button active anymore, can crash when
1460                  * switching screens with tooltip open because region and tooltip
1461                  * are no longer in the same screen */
1462                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1463                         UI_blocklist_free(C, &ar->uiblocks);
1464
1465                         if (ar->regiontimer) {
1466                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1467                                 ar->regiontimer = NULL;
1468                         }
1469                 }
1470
1471                 /* prevent hanging header prints */
1472                 ED_area_headerprint(sa, NULL);
1473         }
1474
1475         if (sa && sa->full) {
1476                 WorkSpaceLayout *layout_old = WM_window_get_active_layout(win);
1477                 /* restoring back to SCREENNORMAL */
1478                 sc = sa->full;       /* the old screen to restore */
1479                 oldscreen = WM_window_get_active_screen(win); /* the one disappearing */
1480
1481                 sc->state = SCREENNORMAL;
1482
1483                 /* find old area to restore from */
1484                 ScrArea *fullsa = NULL;
1485                 for (ScrArea *old = sc->areabase.first; old; old = old->next) {
1486                         /* area to restore from is always first */
1487                         if (old->full && !fullsa) {
1488                                 fullsa = old;
1489                         }
1490
1491                         /* clear full screen state */
1492                         old->full = NULL;
1493                         old->flag &= ~AREA_TEMP_INFO;
1494                 }
1495
1496                 sa->flag &= ~AREA_TEMP_INFO;
1497                 sa->full = NULL;
1498
1499                 if (fullsa == NULL) {
1500                         if (G.debug & G_DEBUG)
1501                                 printf("%s: something wrong in areafullscreen\n", __func__);
1502                         return NULL;
1503                 }
1504
1505                 if (state == SCREENFULL) {
1506                         /* restore the old side panels/header visibility */
1507                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1508                                 ar->flag = ar->flagfullscreen;
1509                         }
1510                 }
1511
1512                 ED_area_data_swap(fullsa, sa);
1513
1514                 /* animtimer back */
1515                 sc->animtimer = oldscreen->animtimer;
1516                 oldscreen->animtimer = NULL;
1517
1518                 ED_screen_change(C, sc);
1519
1520                 BKE_workspace_layout_remove(CTX_data_main(C), workspace, layout_old);
1521
1522                 /* After we've restored back to SCREENNORMAL, we have to wait with
1523                  * screen handling as it uses the area coords which aren't updated yet.
1524                  * Without doing so, the screen handling gets wrong area coords,
1525                  * which in worst case can lead to crashes (see T43139) */
1526                 sc->skip_handling = true;
1527         }
1528         else {
1529                 /* change from SCREENNORMAL to new state */
1530                 WorkSpaceLayout *layout_new;
1531                 ScrArea *newa;
1532                 char newname[MAX_ID_NAME - 2];
1533
1534                 oldscreen = WM_window_get_active_screen(win);
1535
1536                 oldscreen->state = state;
1537                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1538
1539                 layout_new = ED_workspace_layout_add(workspace, win, newname);
1540
1541                 sc = BKE_workspace_layout_screen_get(layout_new);
1542                 sc->state = state;
1543                 sc->redraws_flag = oldscreen->redraws_flag;
1544                 sc->temp = oldscreen->temp;
1545
1546                 /* timer */
1547                 sc->animtimer = oldscreen->animtimer;
1548                 oldscreen->animtimer = NULL;
1549
1550                 /* use random area when we have no active one, e.g. when the
1551                  * mouse is outside of the window and we open a file browser */
1552                 if (!sa)
1553                         sa = oldscreen->areabase.first;
1554
1555                 if (state == SCREENMAXIMIZED) {
1556                         /* returns the top small area */
1557                         newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1558                         ED_area_newspace(C, newa, SPACE_INFO, false);
1559
1560                         /* copy area */
1561                         newa = newa->prev;
1562                         ED_area_data_swap(newa, sa);
1563                         sa->flag |= AREA_TEMP_INFO;
1564
1565                         sa->full = oldscreen;
1566                         newa->full = oldscreen;
1567                         newa->next->full = oldscreen; // XXX
1568                 }
1569                 else if (state == SCREENFULL) {
1570                         newa = (ScrArea *)sc->areabase.first;
1571
1572                         /* copy area */
1573                         ED_area_data_swap(newa, sa);
1574                         newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1575
1576                         /* temporarily hide the side panels/header */
1577                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1578                                 ar->flagfullscreen = ar->flag;
1579
1580                                 if (ELEM(ar->regiontype,
1581                                          RGN_TYPE_UI,
1582                                          RGN_TYPE_HEADER,
1583                                          RGN_TYPE_TOOLS))
1584                                 {
1585                                         ar->flag |= RGN_FLAG_HIDDEN;
1586                                 }
1587                         }
1588
1589                         sa->full = oldscreen;
1590                         newa->full = oldscreen;
1591                 }
1592                 else {
1593                         BLI_assert(false);
1594                 }
1595
1596                 ED_screen_change(C, sc);
1597         }
1598
1599         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1600         CTX_wm_area_set(C, sc->areabase.first);
1601
1602         return sc->areabase.first;
1603 }
1604
1605 /* update frame rate info for viewport drawing */
1606 void ED_refresh_viewport_fps(bContext *C)
1607 {
1608         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1609         Scene *scene = CTX_data_scene(C);
1610         
1611         /* is anim playback running? */
1612         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1613                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1614                 
1615                 /* if there isn't any info, init it first */
1616                 if (fpsi == NULL)
1617                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1618                 
1619                 /* update the values */
1620                 fpsi->redrawtime = fpsi->lredrawtime;
1621                 fpsi->lredrawtime = animtimer->ltime;
1622         }
1623         else {
1624                 /* playback stopped or shouldn't be running */
1625                 if (scene->fps_info)
1626                         MEM_freeN(scene->fps_info);
1627                 scene->fps_info = NULL;
1628         }
1629 }
1630
1631 /* redraws: uses defines from stime->redraws 
1632  * enable: 1 - forward on, -1 - backwards on, 0 - off
1633  */
1634 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1635 {
1636         bScreen *screen = CTX_wm_screen(C);
1637         wmWindowManager *wm = CTX_wm_manager(C);
1638         wmWindow *win = CTX_wm_window(C);
1639         Scene *scene = CTX_data_scene(C);
1640         bScreen *stopscreen = ED_screen_animation_playing(wm);
1641         
1642         if (stopscreen) {
1643                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1644                 stopscreen->animtimer = NULL;
1645         }
1646         
1647         if (enable) {
1648                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1649                 
1650                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1651                 
1652                 sad->ar = CTX_wm_region(C);
1653                 /* if startframe is larger than current frame, we put currentframe on startframe.
1654                  * note: first frame then is not drawn! (ton) */
1655                 if (PRVRANGEON) {
1656                         if (scene->r.psfra > scene->r.cfra) {
1657                                 sad->sfra = scene->r.cfra;
1658                                 scene->r.cfra = scene->r.psfra;
1659                         }
1660                         else
1661                                 sad->sfra = scene->r.cfra;
1662                 }
1663                 else {
1664                         if (scene->r.sfra > scene->r.cfra) {
1665                                 sad->sfra = scene->r.cfra;
1666                                 scene->r.cfra = scene->r.sfra;
1667                         }
1668                         else
1669                                 sad->sfra = scene->r.cfra;
1670                 }
1671                 sad->redraws = redraws;
1672                 sad->refresh = refresh;
1673                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1674                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1675
1676                 ScrArea *sa = CTX_wm_area(C);
1677
1678                 char spacetype = -1;
1679
1680                 if (sa)
1681                         spacetype = sa->spacetype;
1682
1683                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA, SPACE_TIME));
1684
1685                 screen->animtimer->customdata = sad;
1686                 
1687         }
1688
1689         /* notifier catched by top header, for button */
1690         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1691 }
1692
1693 /* helper for screen_animation_play() - only to be used for TimeLine */
1694 static ARegion *time_top_left_3dwindow(bScreen *screen)
1695 {
1696         ARegion *aret = NULL;
1697         ScrArea *sa;
1698         int min = 10000;
1699         
1700         for (sa = screen->areabase.first; sa; sa = sa->next) {
1701                 if (sa->spacetype == SPACE_VIEW3D) {
1702                         ARegion *ar;
1703                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1704                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1705                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1706                                                 aret = ar;
1707                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1708                                         }
1709                                 }
1710                         }
1711                 }
1712         }
1713
1714         return aret;
1715 }
1716
1717 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1718 {
1719         if (screen && screen->animtimer) {
1720                 wmTimer *wt = screen->animtimer;
1721                 ScreenAnimData *sad = wt->customdata;
1722                 
1723                 sad->redraws = redraws;
1724                 sad->refresh = refresh;
1725                 sad->ar = NULL;
1726                 if (redraws & TIME_REGION)
1727                         sad->ar = time_top_left_3dwindow(screen);
1728         }
1729 }
1730
1731 /* results in fully updated anim system */
1732 void ED_update_for_newframe(Main *bmain, Scene *scene, ViewLayer *view_layer, struct Depsgraph *depsgraph)
1733 {
1734 #ifdef DURIAN_CAMERA_SWITCH
1735         void *camera = BKE_scene_camera_switch_find(scene);
1736         if (camera && scene->camera != camera) {
1737                 bScreen *sc;
1738                 scene->camera = camera;
1739                 /* are there cameras in the views that are not in the scene? */
1740                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1741                         BKE_screen_view3d_scene_sync(sc, scene);
1742                 }
1743         }
1744 #endif
1745         
1746         ED_clip_update_frame(bmain, scene->r.cfra);
1747
1748         /* this function applies the changes too */
1749         BKE_scene_graph_update_for_newframe(bmain->eval_ctx, depsgraph, bmain, scene, view_layer);
1750
1751         /* composite */
1752         if (scene->use_nodes && scene->nodetree)
1753                 ntreeCompositTagAnimated(scene->nodetree);
1754         
1755         /* update animated texture nodes */
1756         {
1757                 Tex *tex;
1758                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
1759                         if (tex->use_nodes && tex->nodetree) {
1760                                 ntreeTexTagAnimated(tex->nodetree);
1761                         }
1762                 }
1763         }
1764         
1765 }
1766
1767 /*
1768  * return true if any active area requires to see in 3D
1769  */
1770 bool ED_screen_stereo3d_required(const bScreen *screen, const Scene *scene)
1771 {
1772         ScrArea *sa;
1773         const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
1774
1775         for (sa = screen->areabase.first; sa; sa = sa->next) {
1776                 switch (sa->spacetype) {
1777                         case SPACE_VIEW3D:
1778                         {
1779                                 View3D *v3d;
1780
1781                                 if (!is_multiview)
1782                                         continue;
1783
1784                                 v3d = sa->spacedata.first;
1785                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
1786                                         ARegion *ar;
1787                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1788                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
1789                                                         RegionView3D *rv3d = ar->regiondata;
1790                                                         if (rv3d->persp == RV3D_CAMOB) {
1791                                                                 return true;
1792                                                         }
1793                                                 }
1794                                         }
1795                                 }
1796                                 break;
1797                         }
1798                         case SPACE_IMAGE:
1799                         {
1800                                 SpaceImage *sima;
1801
1802                                 /* images should always show in stereo, even if
1803                                  * the file doesn't have views enabled */
1804                                 sima = sa->spacedata.first;
1805                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
1806                                     (sima->iuser.flag & IMA_SHOW_STEREO))
1807                                 {
1808                                         return true;
1809                                 }
1810                                 break;
1811                         }
1812                         case SPACE_NODE:
1813                         {
1814                                 SpaceNode *snode;
1815
1816                                 if (!is_multiview)
1817                                         continue;
1818
1819                                 snode = sa->spacedata.first;
1820                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
1821                                         return true;
1822                                 }
1823                                 break;
1824                         }
1825                         case SPACE_SEQ:
1826                         {
1827                                 SpaceSeq *sseq;
1828
1829                                 if (!is_multiview)
1830                                         continue;
1831
1832                                 sseq = sa->spacedata.first;
1833                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
1834                                         return true;
1835                                 }
1836
1837                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1838                                         return true;
1839                                 }
1840
1841                                 break;
1842                         }
1843                 }
1844         }
1845
1846         return false;
1847 }
1848
1849 /**
1850  * Find the scene displayed in \a screen.
1851  * \note Assumes \a screen to be visible/active!
1852  */
1853 Scene *ED_screen_scene_find(const bScreen *screen, const wmWindowManager *wm)
1854 {
1855         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1856                 if (WM_window_get_active_screen(win) == screen) {
1857                         return WM_window_get_active_scene(win);
1858                 }
1859         }
1860
1861         BLI_assert(0);
1862         return NULL;
1863 }