Cleanup: Move area geometry management into an own file
[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_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_workspace_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_icons.h"
47 #include "BKE_image.h"
48 #include "BKE_global.h"
49 #include "BKE_layer.h"
50 #include "BKE_library.h"
51 #include "BKE_library_remap.h"
52 #include "BKE_main.h"
53 #include "BKE_node.h"
54 #include "BKE_screen.h"
55 #include "BKE_scene.h"
56 #include "BKE_workspace.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "ED_object.h"
62 #include "ED_screen.h"
63 #include "ED_screen_types.h"
64 #include "ED_clip.h"
65 #include "ED_node.h"
66 #include "ED_render.h"
67
68 #include "UI_interface.h"
69
70 #include "WM_message.h"
71
72 #include "DEG_depsgraph_query.h"
73
74 #include "screen_intern.h"  /* own module include */
75
76
77 /* adds no space data */
78 static ScrArea *screen_addarea_ex(
79         ScrAreaMap *area_map,
80         ScrVert *bottom_left, ScrVert *top_left, ScrVert *top_right, ScrVert *bottom_right,
81         short spacetype)
82 {
83         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
84
85         sa->v1 = bottom_left;
86         sa->v2 = top_left;
87         sa->v3 = top_right;
88         sa->v4 = bottom_right;
89         sa->spacetype = spacetype;
90
91         BLI_addtail(&area_map->areabase, sa);
92
93         return sa;
94 }
95 static ScrArea *screen_addarea(
96         bScreen *sc,
97         ScrVert *left_bottom, ScrVert *left_top, ScrVert *right_top, ScrVert *right_bottom,
98         short spacetype)
99 {
100         return screen_addarea_ex(AREAMAP_FROM_SCREEN(sc), left_bottom, left_top, right_top, right_bottom,
101                                  spacetype);
102 }
103
104 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
105 {
106
107         ED_area_exit(C, sa);
108
109         BKE_screen_area_free(sa);
110
111         BLI_remlink(&sc->areabase, sa);
112         MEM_freeN(sa);
113 }
114
115 ScrArea *area_split(const wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
116 {
117         ScrArea *newa = NULL;
118         ScrVert *sv1, *sv2;
119         short split;
120         rcti window_rect;
121
122         if (sa == NULL) return NULL;
123
124         WM_window_rect_calc(win, &window_rect);
125
126         split = screen_geom_find_area_split_point(sa, &window_rect, dir, fac);
127         if (split == 0) return NULL;
128
129         /* note regarding (fac > 0.5f) checks below.
130          * normally it shouldn't matter which is used since the copy should match the original
131          * however with viewport rendering and python console this isn't the case. - campbell */
132
133         if (dir == 'h') {
134                 /* new vertices */
135                 sv1 = screen_geom_vertex_add(sc, sa->v1->vec.x, split);
136                 sv2 = screen_geom_vertex_add(sc, sa->v4->vec.x, split);
137
138                 /* new edges */
139                 screen_geom_edge_add(sc, sa->v1, sv1);
140                 screen_geom_edge_add(sc, sv1, sa->v2);
141                 screen_geom_edge_add(sc, sa->v3, sv2);
142                 screen_geom_edge_add(sc, sv2, sa->v4);
143                 screen_geom_edge_add(sc, sv1, sv2);
144
145                 if (fac > 0.5f) {
146                         /* new areas: top */
147                         newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->spacetype);
148
149                         /* area below */
150                         sa->v2 = sv1;
151                         sa->v3 = sv2;
152                 }
153                 else {
154                         /* new areas: bottom */
155                         newa = screen_addarea(sc, sa->v1, sv1, sv2, sa->v4, sa->spacetype);
156
157                         /* area above */
158                         sa->v1 = sv1;
159                         sa->v4 = sv2;
160                 }
161
162                 ED_area_data_copy(newa, sa, true);
163
164         }
165         else {
166                 /* new vertices */
167                 sv1 = screen_geom_vertex_add(sc, split, sa->v1->vec.y);
168                 sv2 = screen_geom_vertex_add(sc, split, sa->v2->vec.y);
169
170                 /* new edges */
171                 screen_geom_edge_add(sc, sa->v1, sv1);
172                 screen_geom_edge_add(sc, sv1, sa->v4);
173                 screen_geom_edge_add(sc, sa->v2, sv2);
174                 screen_geom_edge_add(sc, sv2, sa->v3);
175                 screen_geom_edge_add(sc, sv1, sv2);
176
177                 if (fac > 0.5f) {
178                         /* new areas: right */
179                         newa = screen_addarea(sc, sv1, sv2, sa->v3, sa->v4, sa->spacetype);
180
181                         /* area left */
182                         sa->v3 = sv2;
183                         sa->v4 = sv1;
184                 }
185                 else {
186                         /* new areas: left */
187                         newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->spacetype);
188
189                         /* area right */
190                         sa->v1 = sv1;
191                         sa->v2 = sv2;
192                 }
193
194                 ED_area_data_copy(newa, sa, true);
195         }
196
197         /* remove double vertices en edges */
198         if (merge)
199                 BKE_screen_remove_double_scrverts(sc);
200         BKE_screen_remove_double_scredges(sc);
201         BKE_screen_remove_unused_scredges(sc);
202
203         return newa;
204 }
205
206 /**
207  * Empty screen, with 1 dummy area without spacedata. Uses window size.
208  */
209 bScreen *screen_add(Main *bmain, const char *name, const rcti *rect)
210 {
211         bScreen *sc;
212         ScrVert *sv1, *sv2, *sv3, *sv4;
213
214         sc = BKE_libblock_alloc(bmain, ID_SCR, name, 0);
215         sc->do_refresh = true;
216         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
217
218         sv1 = screen_geom_vertex_add(sc, rect->xmin,     rect->ymin);
219         sv2 = screen_geom_vertex_add(sc, rect->xmin,     rect->ymax - 1);
220         sv3 = screen_geom_vertex_add(sc, rect->xmax - 1, rect->ymax - 1);
221         sv4 = screen_geom_vertex_add(sc, rect->xmax - 1, rect->ymin);
222
223         screen_geom_edge_add(sc, sv1, sv2);
224         screen_geom_edge_add(sc, sv2, sv3);
225         screen_geom_edge_add(sc, sv3, sv4);
226         screen_geom_edge_add(sc, sv4, sv1);
227
228         /* dummy type, no spacedata */
229         screen_addarea(sc, sv1, sv2, sv3, sv4, SPACE_EMPTY);
230
231         return sc;
232 }
233
234 void screen_data_copy(bScreen *to, bScreen *from)
235 {
236         ScrVert *s1, *s2;
237         ScrEdge *se;
238         ScrArea *sa, *saf;
239
240         /* free contents of 'to', is from blenkernel screen.c */
241         BKE_screen_free(to);
242
243         BLI_duplicatelist(&to->vertbase, &from->vertbase);
244         BLI_duplicatelist(&to->edgebase, &from->edgebase);
245         BLI_duplicatelist(&to->areabase, &from->areabase);
246         BLI_listbase_clear(&to->regionbase);
247
248         s2 = to->vertbase.first;
249         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
250                 s1->newv = s2;
251         }
252
253         for (se = to->edgebase.first; se; se = se->next) {
254                 se->v1 = se->v1->newv;
255                 se->v2 = se->v2->newv;
256                 BKE_screen_sort_scrvert(&(se->v1), &(se->v2));
257         }
258
259         saf = from->areabase.first;
260         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
261                 sa->v1 = sa->v1->newv;
262                 sa->v2 = sa->v2->newv;
263                 sa->v3 = sa->v3->newv;
264                 sa->v4 = sa->v4->newv;
265
266                 BLI_listbase_clear(&sa->spacedata);
267                 BLI_listbase_clear(&sa->regionbase);
268                 BLI_listbase_clear(&sa->actionzones);
269                 BLI_listbase_clear(&sa->handlers);
270
271                 ED_area_data_copy(sa, saf, true);
272         }
273
274         /* put at zero (needed?) */
275         for (s1 = from->vertbase.first; s1; s1 = s1->next)
276                 s1->newv = NULL;
277 }
278
279 /**
280  * Prepare a newly created screen for initializing it as active screen.
281  */
282 void screen_new_activate_prepare(const wmWindow *win, bScreen *screen_new)
283 {
284         screen_new->winid = win->winid;
285         screen_new->do_refresh = true;
286         screen_new->do_draw = true;
287 }
288
289
290 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
291 /* -1 = not valid check */
292 /* used with join operator */
293 int area_getorientation(ScrArea *sa, ScrArea *sb)
294 {
295         ScrVert *sav1, *sav2, *sav3, *sav4;
296         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
297
298         if (sa == NULL || sb == NULL) return -1;
299
300         sav1 = sa->v1;
301         sav2 = sa->v2;
302         sav3 = sa->v3;
303         sav4 = sa->v4;
304         sbv1 = sb->v1;
305         sbv2 = sb->v2;
306         sbv3 = sb->v3;
307         sbv4 = sb->v4;
308
309         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
310                 return 0;
311         }
312         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
313                 return 1;
314         }
315         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
316                 return 2;
317         }
318         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
319                 return 3;
320         }
321
322         return -1;
323 }
324
325 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
326  *  used by the split, join operators
327  */
328 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
329 {
330         int dir;
331
332         dir = area_getorientation(sa1, sa2);
333         /*printf("dir is : %i\n", dir);*/
334
335         if (dir == -1) {
336                 return 0;
337         }
338
339         if (dir == 0) {
340                 sa1->v1 = sa2->v1;
341                 sa1->v2 = sa2->v2;
342                 screen_geom_edge_add(scr, sa1->v2, sa1->v3);
343                 screen_geom_edge_add(scr, sa1->v1, sa1->v4);
344         }
345         else if (dir == 1) {
346                 sa1->v2 = sa2->v2;
347                 sa1->v3 = sa2->v3;
348                 screen_geom_edge_add(scr, sa1->v1, sa1->v2);
349                 screen_geom_edge_add(scr, sa1->v3, sa1->v4);
350         }
351         else if (dir == 2) {
352                 sa1->v3 = sa2->v3;
353                 sa1->v4 = sa2->v4;
354                 screen_geom_edge_add(scr, sa1->v2, sa1->v3);
355                 screen_geom_edge_add(scr, sa1->v1, sa1->v4);
356         }
357         else if (dir == 3) {
358                 sa1->v1 = sa2->v1;
359                 sa1->v4 = sa2->v4;
360                 screen_geom_edge_add(scr, sa1->v1, sa1->v2);
361                 screen_geom_edge_add(scr, sa1->v3, sa1->v4);
362         }
363
364         screen_delarea(C, scr, sa2);
365         BKE_screen_remove_double_scrverts(scr);
366         /* Update preview thumbnail */
367         BKE_icon_changed(scr->id.icon_id);
368
369         return 1;
370 }
371
372
373 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
374
375 /* screen sets cursor based on active region */
376 static void region_cursor_set(wmWindow *win, bool swin_changed)
377 {
378         bScreen *screen = WM_window_get_active_screen(win);
379
380         ED_screen_areas_iter(win, screen, sa) {
381                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
382                         if (ar == screen->active_region) {
383                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
384                                         if (ar->manipulator_map != NULL) {
385                                                 if (WM_manipulatormap_cursor_set(ar->manipulator_map, win)) {
386                                                         return;
387                                                 }
388                                         }
389                                         ED_region_cursor_set(win, sa, ar);
390                                 }
391                                 return;
392                         }
393                 }
394         }
395 }
396
397 void ED_screen_do_listen(bContext *C, wmNotifier *note)
398 {
399         wmWindow *win = CTX_wm_window(C);
400         bScreen *screen = CTX_wm_screen(C);
401
402         /* generic notes */
403         switch (note->category) {
404                 case NC_WM:
405                         if (note->data == ND_FILEREAD)
406                                 screen->do_draw = true;
407                         break;
408                 case NC_WINDOW:
409                         screen->do_draw = true;
410                         break;
411                 case NC_SCREEN:
412                         if (note->action == NA_EDITED)
413                                 screen->do_draw = screen->do_refresh = true;
414                         break;
415                 case NC_SCENE:
416                         if (note->data == ND_MODE)
417                                 region_cursor_set(win, true);
418                         break;
419         }
420 }
421
422 /* helper call for below, dpi changes headers */
423 static void screen_refresh_headersizes(void)
424 {
425         const ListBase *lb = BKE_spacetypes_list();
426         SpaceType *st;
427
428         for (st = lb->first; st; st = st->next) {
429                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
430                 if (art) art->prefsizey = ED_area_headersize();
431         }
432 }
433
434 /* make this screen usable */
435 /* for file read and first use, for scaling window, area moves */
436 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
437 {
438         bScreen *screen = WM_window_get_active_screen(win);
439
440         /* exception for bg mode, we only need the screen context */
441         if (!G.background) {
442                 rcti window_rect, screen_rect;
443
444                 /* header size depends on DPI, let's verify */
445                 WM_window_set_dpi(win);
446                 screen_refresh_headersizes();
447
448                 WM_window_rect_calc(win, &window_rect);
449                 WM_window_screen_rect_calc(win, &screen_rect); /* Get screen bounds __after__ updating window DPI! */
450
451                 screen_geom_vertices_scale(win, screen);
452
453                 ED_screen_areas_iter(win, screen, area) {
454                         /* set spacetype and region callbacks, calls init() */
455                         /* sets subwindows for regions, adds handlers */
456                         ED_area_initialize(wm, win, area);
457                 }
458
459                 /* wake up animtimer */
460                 if (screen->animtimer)
461                         WM_event_timer_sleep(wm, win, screen->animtimer, false);
462         }
463
464         if (G.debug & G_DEBUG_EVENTS) {
465                 printf("%s: set screen\n", __func__);
466         }
467         screen->do_refresh = false;
468         /* prevent multiwin errors */
469         screen->winid = win->winid;
470
471         screen->context = ed_screen_context;
472 }
473
474 /* file read, set all screens, ... */
475 void ED_screens_initialize(Main *bmain, wmWindowManager *wm)
476 {
477         wmWindow *win;
478
479         for (win = wm->windows.first; win; win = win->next) {
480                 if (WM_window_get_active_workspace(win) == NULL) {
481                         WM_window_set_active_workspace(win, bmain->workspaces.first);
482                 }
483
484                 if (BLI_listbase_is_empty(&win->global_areas.areabase)) {
485                         ED_screen_global_areas_create(win);
486                 }
487                 ED_screen_refresh(wm, win);
488                 if (win->eventstate) {
489                         ED_screen_set_active_region(NULL, win, &win->eventstate->x);
490                 }
491         }
492 }
493
494 void ED_screen_ensure_updated(wmWindowManager *wm, wmWindow *win, bScreen *screen)
495 {
496         if (screen->do_refresh) {
497                 ED_screen_refresh(wm, win);
498         }
499 }
500
501
502 /* *********** exit calls are for closing running stuff ******** */
503
504 void ED_region_exit(bContext *C, ARegion *ar)
505 {
506         wmWindowManager *wm = CTX_wm_manager(C);
507         wmWindow *win = CTX_wm_window(C);
508         ARegion *prevar = CTX_wm_region(C);
509
510         if (ar->type && ar->type->exit)
511                 ar->type->exit(wm, ar);
512
513         CTX_wm_region_set(C, ar);
514
515         WM_event_remove_handlers(C, &ar->handlers);
516         WM_event_modal_handler_region_replace(win, ar, NULL);
517         WM_draw_region_free(ar);
518
519         if (ar->headerstr) {
520                 MEM_freeN(ar->headerstr);
521                 ar->headerstr = NULL;
522         }
523
524         if (ar->regiontimer) {
525                 WM_event_remove_timer(wm, win, ar->regiontimer);
526                 ar->regiontimer = NULL;
527         }
528
529         WM_msgbus_clear_by_owner(wm->message_bus, ar);
530
531         CTX_wm_region_set(C, prevar);
532 }
533
534 void ED_area_exit(bContext *C, ScrArea *sa)
535 {
536         wmWindowManager *wm = CTX_wm_manager(C);
537         wmWindow *win = CTX_wm_window(C);
538         ScrArea *prevsa = CTX_wm_area(C);
539         ARegion *ar;
540
541         if (sa->type && sa->type->exit)
542                 sa->type->exit(wm, sa);
543
544         CTX_wm_area_set(C, sa);
545
546         for (ar = sa->regionbase.first; ar; ar = ar->next)
547                 ED_region_exit(C, ar);
548
549         WM_event_remove_handlers(C, &sa->handlers);
550         WM_event_modal_handler_area_replace(win, sa, NULL);
551
552         CTX_wm_area_set(C, prevsa);
553 }
554
555 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
556 {
557         wmWindowManager *wm = CTX_wm_manager(C);
558         wmWindow *prevwin = CTX_wm_window(C);
559
560         CTX_wm_window_set(C, window);
561
562         if (screen->animtimer)
563                 WM_event_remove_timer(wm, window, screen->animtimer);
564         screen->animtimer = NULL;
565         screen->scrubbing = false;
566
567         screen->active_region = NULL;
568
569         for (ARegion *ar = screen->regionbase.first; ar; ar = ar->next) {
570                 ED_region_exit(C, ar);
571         }
572         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
573                 ED_area_exit(C, sa);
574         }
575         /* Don't use ED_screen_areas_iter here, it skips hidden areas. */
576         for (ScrArea *sa = window->global_areas.areabase.first; sa; sa = sa->next) {
577                 ED_area_exit(C, sa);
578         }
579
580         /* mark it available for use for other windows */
581         screen->winid = 0;
582
583         if (!WM_window_is_temp_screen(prevwin)) {
584                 /* use previous window if possible */
585                 CTX_wm_window_set(C, prevwin);
586         }
587         else {
588                 /* none otherwise */
589                 CTX_wm_window_set(C, NULL);
590         }
591
592 }
593
594 /* *********************************** */
595
596 /* case when on area-edge or in azones, or outside window */
597 static void screen_cursor_set(wmWindow *win, const int xy[2])
598 {
599         const bScreen *screen = WM_window_get_active_screen(win);
600         AZone *az = NULL;
601         ScrArea *sa;
602
603         for (sa = screen->areabase.first; sa; sa = sa->next)
604                 if ((az = ED_area_actionzone_find_xy(sa, xy)))
605                         break;
606
607         if (sa) {
608                 if (az->type == AZONE_AREA)
609                         WM_cursor_set(win, CURSOR_EDIT);
610                 else if (az->type == AZONE_REGION) {
611                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
612                                 WM_cursor_set(win, CURSOR_X_MOVE);
613                         else
614                                 WM_cursor_set(win, CURSOR_Y_MOVE);
615                 }
616         }
617         else {
618                 ScrEdge *actedge = screen_geom_find_active_scredge(win, screen, xy[0], xy[1]);
619
620                 if (actedge) {
621                         if (screen_geom_edge_is_horizontal(actedge))
622                                 WM_cursor_set(win, CURSOR_Y_MOVE);
623                         else
624                                 WM_cursor_set(win, CURSOR_X_MOVE);
625                 }
626                 else
627                         WM_cursor_set(win, CURSOR_STD);
628         }
629 }
630
631
632 /* called in wm_event_system.c. sets state vars in screen, cursors */
633 /* event type is mouse move */
634 void ED_screen_set_active_region(bContext *C, wmWindow *win, const int xy[2])
635 {
636         bScreen *scr = WM_window_get_active_screen(win);
637
638         if (scr) {
639                 ScrArea *sa = NULL;
640                 ARegion *ar;
641                 ARegion *old_ar = scr->active_region;
642
643                 ED_screen_areas_iter(win, scr, area_iter) {
644                         if (xy[0] > area_iter->totrct.xmin && xy[0] < area_iter->totrct.xmax) {
645                                 if (xy[1] > area_iter->totrct.ymin && xy[1] < area_iter->totrct.ymax) {
646                                         if (ED_area_actionzone_refresh_xy(area_iter, xy) == NULL) {
647                                                 sa = area_iter;
648                                                 break;
649                                         }
650                                 }
651                         }
652                 }
653                 if (sa) {
654                         /* make overlap active when mouse over */
655                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
656                                 if (BLI_rcti_isect_pt_v(&ar->winrct, xy)) {
657                                         scr->active_region = ar;
658                                         break;
659                                 }
660                         }
661                 }
662                 else
663                         scr->active_region = NULL;
664
665                 /* check for redraw headers */
666                 if (old_ar != scr->active_region) {
667
668                         ED_screen_areas_iter(win, scr, area_iter) {
669                                 bool do_draw = false;
670
671                                 for (ar = area_iter->regionbase.first; ar; ar = ar->next) {
672                                         if (ar == old_ar || ar == scr->active_region) {
673                                                 do_draw = true;
674                                         }
675                                 }
676
677                                 if (do_draw) {
678                                         for (ar = area_iter->regionbase.first; ar; ar = ar->next) {
679                                                 if (ar->regiontype == RGN_TYPE_HEADER) {
680                                                         ED_region_tag_redraw_no_rebuild(ar);
681                                                 }
682                                         }
683                                 }
684                         }
685                 }
686
687                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
688                 if (scr->active_region == NULL) {
689                         screen_cursor_set(win, xy);
690                 }
691                 else {
692                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
693                         if (old_ar != scr->active_region) {
694                                 region_cursor_set(win, true);
695
696                                 /* this used to be a notifier, but needs to be done immediate
697                                  * because it can undo setting the right button as active due
698                                  * to delayed notifier handling */
699                                 if (C) {
700                                         UI_screen_free_active_but(C, scr);
701                                 }
702                         }
703                         else
704                                 region_cursor_set(win, false);
705                 }
706         }
707 }
708
709 int ED_screen_area_active(const bContext *C)
710 {
711         wmWindow *win = CTX_wm_window(C);
712         bScreen *sc = CTX_wm_screen(C);
713         ScrArea *sa = CTX_wm_area(C);
714
715         if (win && sc && sa) {
716                 AZone *az = ED_area_actionzone_find_xy(sa, &win->eventstate->x);
717                 ARegion *ar;
718
719                 if (az && az->type == AZONE_REGION)
720                         return 1;
721
722                 for (ar = sa->regionbase.first; ar; ar = ar->next)
723                         if (ar == sc->active_region)
724                                 return 1;
725         }
726         return 0;
727 }
728
729 /**
730  * Add an area and geometry (screen-edges and -vertices) for it to \a area_map,
731  * with coordinates/dimensions matching \a rect.
732  */
733 static ScrArea *screen_area_create_with_geometry(
734         ScrAreaMap *area_map, const rcti *rect,
735         short spacetype)
736 {
737         ScrVert *bottom_left  = screen_geom_vertex_add_ex(area_map, rect->xmin, rect->ymin);
738         ScrVert *top_left     = screen_geom_vertex_add_ex(area_map, rect->xmin, rect->ymax);
739         ScrVert *top_right    = screen_geom_vertex_add_ex(area_map, rect->xmax, rect->ymax);
740         ScrVert *bottom_right = screen_geom_vertex_add_ex(area_map, rect->xmax, rect->ymin);
741
742         screen_geom_edge_add_ex(area_map, bottom_left, top_left);
743         screen_geom_edge_add_ex(area_map, top_left, top_right);
744         screen_geom_edge_add_ex(area_map, top_right, bottom_right);
745         screen_geom_edge_add_ex(area_map, bottom_right, bottom_left);
746
747         return screen_addarea_ex(area_map, bottom_left, top_left, top_right, bottom_right, spacetype);
748 }
749
750 static void screen_global_area_create(
751         wmWindow *win, eSpace_Type space_type, GlobalAreaAlign align, const rcti *rect,
752         const short height_cur, const short height_min, const short height_max)
753 {
754         ScrArea *area = screen_area_create_with_geometry(&win->global_areas, rect, space_type);
755         SpaceType *stype = BKE_spacetype_from_id(space_type);
756         SpaceLink *slink = stype->new(area, WM_window_get_active_scene(win));
757
758         area->regionbase = slink->regionbase;
759
760         /* Data specific to global areas. */
761         area->global = MEM_callocN(sizeof(*area->global), __func__);
762         area->global->cur_fixed_height = height_cur;
763         area->global->size_max = height_max;
764         area->global->size_min = height_min;
765         area->global->align = align;
766
767         BLI_addhead(&area->spacedata, slink);
768         BLI_listbase_clear(&slink->regionbase);
769 }
770
771 static void screen_global_topbar_area_create(wmWindow *win)
772 {
773         const short size_y = 2.25 * HEADERY;
774         rcti rect;
775
776         BLI_rcti_init(&rect, 0, WM_window_pixels_x(win) - 1, 0, WM_window_pixels_y(win) - 1);
777         rect.ymin = rect.ymax - size_y;
778
779         screen_global_area_create(win, SPACE_TOPBAR, GLOBAL_AREA_ALIGN_TOP, &rect, size_y, HEADERY, size_y);
780 }
781
782 static void screen_global_statusbar_area_create(wmWindow *win)
783 {
784         const short size_y = 0.8f * HEADERY;
785         rcti rect;
786
787         BLI_rcti_init(&rect, 0, WM_window_pixels_x(win) - 1, 0, WM_window_pixels_y(win) - 1);
788         rect.ymax = rect.ymin + size_y;
789
790         screen_global_area_create(win, SPACE_STATUSBAR, GLOBAL_AREA_ALIGN_BOTTOM, &rect, size_y, size_y, size_y);
791 }
792
793 void ED_screen_global_areas_create(wmWindow *win)
794 {
795         bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
796         if (screen->temp == 0) {
797                 screen_global_topbar_area_create(win);
798                 screen_global_statusbar_area_create(win);
799         }
800 }
801
802
803 /* -------------------------------------------------------------------- */
804 /* Screen changing */
805
806 static bScreen *screen_fullscreen_find_associated_normal_screen(const Main *bmain, bScreen *screen)
807 {
808         for (bScreen *screen_iter = bmain->screen.first; screen_iter; screen_iter = screen_iter->id.next) {
809                 ScrArea *sa = screen_iter->areabase.first;
810                 if (sa && sa->full == screen) {
811                         return screen_iter;
812                 }
813         }
814
815         return screen;
816 }
817
818 /**
819  * \return the screen to activate.
820  * \warning The returned screen may not always equal \a screen_new!
821  */
822 bScreen *screen_change_prepare(bScreen *screen_old, bScreen *screen_new, Main *bmain, bContext *C, wmWindow *win)
823 {
824         /* validate screen, it's called with notifier reference */
825         if (BLI_findindex(&bmain->screen, screen_new) == -1) {
826                 return NULL;
827         }
828
829         if (ELEM(screen_new->state, SCREENMAXIMIZED, SCREENFULL)) {
830                 screen_new = screen_fullscreen_find_associated_normal_screen(bmain, screen_new);
831         }
832
833         /* check for valid winid */
834         if (!(screen_new->winid == 0 || screen_new->winid == win->winid)) {
835                 return NULL;
836         }
837
838         if (screen_old != screen_new) {
839                 wmTimer *wt = screen_old->animtimer;
840
841                 /* remove handlers referencing areas in old screen */
842                 for (ScrArea *sa = screen_old->areabase.first; sa; sa = sa->next) {
843                         WM_event_remove_area_handler(&win->modalhandlers, sa);
844                 }
845
846                 /* we put timer to sleep, so screen_exit has to think there's no timer */
847                 screen_old->animtimer = NULL;
848                 if (wt) {
849                         WM_event_timer_sleep(CTX_wm_manager(C), win, wt, true);
850                 }
851                 ED_screen_exit(C, win, screen_old);
852
853                 /* Same scene, "transfer" playback to new screen. */
854                 if (wt) {
855                         screen_new->animtimer = wt;
856                 }
857
858                 return screen_new;
859         }
860
861         return NULL;
862 }
863
864 void screen_change_update(bContext *C, wmWindow *win, bScreen *sc)
865 {
866         Scene *scene = WM_window_get_active_scene(win);
867         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
868         WorkSpaceLayout *layout = BKE_workspace_layout_find(workspace, sc);
869
870         CTX_wm_window_set(C, win);  /* stores C->wm.screen... hrmf */
871
872         ED_screen_refresh(CTX_wm_manager(C), win);
873
874         BKE_screen_view3d_scene_sync(sc, scene); /* sync new screen with scene data */
875         WM_event_add_notifier(C, NC_WINDOW, NULL);
876         WM_event_add_notifier(C, NC_SCREEN | ND_LAYOUTSET, layout);
877
878         /* makes button hilites work */
879         WM_event_add_mousemove(C);
880 }
881
882
883 /**
884  * \brief Change the active screen.
885  *
886  * Operator call, WM + Window + screen already existed before
887  *
888  * \warning Do NOT call in area/region queues!
889  * \returns if screen changing was successful.
890  */
891 bool ED_screen_change(bContext *C, bScreen *sc)
892 {
893         Main *bmain = CTX_data_main(C);
894         wmWindow *win = CTX_wm_window(C);
895         bScreen *screen_old = CTX_wm_screen(C);
896         bScreen *screen_new = screen_change_prepare(screen_old, sc, bmain, C, win);
897
898         if (screen_new) {
899                 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
900                 WM_window_set_active_screen(win, workspace, sc);
901                 screen_change_update(C, win, screen_new);
902
903                 return true;
904         }
905
906         return false;
907 }
908
909 static void screen_set_3dview_camera(Scene *scene, ViewLayer *view_layer, ScrArea *sa, View3D *v3d)
910 {
911         /* fix any cameras that are used in the 3d view but not in the scene */
912         BKE_screen_view3d_sync(v3d, scene);
913
914         if (!v3d->camera || !BKE_view_layer_base_find(view_layer, v3d->camera)) {
915                 v3d->camera = BKE_view_layer_camera_find(view_layer);
916                 // XXX if (sc == curscreen) handle_view3d_lock();
917                 if (!v3d->camera) {
918                         ARegion *ar;
919                         ListBase *regionbase;
920
921                         /* regionbase is in different place depending if space is active */
922                         if (v3d == sa->spacedata.first)
923                                 regionbase = &sa->regionbase;
924                         else
925                                 regionbase = &v3d->regionbase;
926
927                         for (ar = regionbase->first; ar; ar = ar->next) {
928                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
929                                         RegionView3D *rv3d = ar->regiondata;
930                                         if (rv3d->persp == RV3D_CAMOB) {
931                                                 rv3d->persp = RV3D_PERSP;
932                                         }
933                                 }
934                         }
935                 }
936         }
937 }
938
939 void ED_screen_update_after_scene_change(const bScreen *screen, Scene *scene_new, ViewLayer *view_layer)
940 {
941         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
942                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
943                         if (sl->spacetype == SPACE_VIEW3D) {
944                                 View3D *v3d = (View3D *)sl;
945                                 screen_set_3dview_camera(scene_new, view_layer, sa, v3d);
946                         }
947                 }
948         }
949 }
950
951 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
952 {
953         wmWindow *win = CTX_wm_window(C);
954         ScrArea *newsa = NULL;
955
956         if (!sa || sa->full == NULL) {
957                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
958         }
959
960         if (!newsa) {
961                 newsa = sa;
962         }
963
964         BLI_assert(newsa);
965
966         if (sa && (sa->spacetype != type)) {
967                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
968         }
969         else {
970                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
971         }
972
973         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
974
975         return newsa;
976 }
977
978 /**
979  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
980  */
981 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
982 {
983         BLI_assert(sa->full);
984
985         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
986                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
987                 ED_area_prevspace(C, sa);
988         }
989         else {
990                 ED_screen_restore_temp_type(C, sa);
991         }
992 }
993
994 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
995 {
996         /* incase nether functions below run */
997         ED_area_tag_redraw(sa);
998
999         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1000                 ED_area_prevspace(C, sa);
1001                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1002         }
1003
1004         if (sa->full) {
1005                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1006         }
1007 }
1008
1009 /* restore a screen / area back to default operation, after temp fullscreen modes */
1010 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1011 {
1012         wmWindow *win = CTX_wm_window(C);
1013         SpaceLink *sl = sa->spacedata.first;
1014         bScreen *screen = CTX_wm_screen(C);
1015         short state = (screen ? screen->state : SCREENMAXIMIZED);
1016
1017         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1018          * overlaid on top of an existing setup) then return to the previous space */
1019
1020         if (sl->next) {
1021                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1022                         ED_screen_full_prevspace(C, sa);
1023                 }
1024                 else {
1025                         ED_screen_state_toggle(C, win, sa, state);
1026                 }
1027                 /* warning: 'sa' may be freed */
1028         }
1029         /* otherwise just tile the area again */
1030         else {
1031                 ED_screen_state_toggle(C, win, sa, state);
1032         }
1033 }
1034
1035 /**
1036  * this function toggles: if area is maximized/full then the parent will be restored
1037  *
1038  * \warning \a sa may be freed.
1039  */
1040 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1041 {
1042         Main *bmain = CTX_data_main(C);
1043         wmWindowManager *wm = CTX_wm_manager(C);
1044         WorkSpace *workspace = WM_window_get_active_workspace(win);
1045         bScreen *sc, *oldscreen;
1046         ARegion *ar;
1047
1048         if (sa) {
1049                 /* ensure we don't have a button active anymore, can crash when
1050                  * switching screens with tooltip open because region and tooltip
1051                  * are no longer in the same screen */
1052                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1053                         UI_blocklist_free(C, &ar->uiblocks);
1054
1055                         if (ar->regiontimer) {
1056                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1057                                 ar->regiontimer = NULL;
1058                         }
1059                 }
1060
1061                 /* prevent hanging status prints */
1062                 ED_area_status_text(sa, NULL);
1063                 ED_workspace_status_text(C, NULL);
1064         }
1065
1066         if (sa && sa->full) {
1067                 WorkSpaceLayout *layout_old = WM_window_get_active_layout(win);
1068                 /* restoring back to SCREENNORMAL */
1069                 sc = sa->full;       /* the old screen to restore */
1070                 oldscreen = WM_window_get_active_screen(win); /* the one disappearing */
1071
1072                 sc->state = SCREENNORMAL;
1073
1074                 /* find old area to restore from */
1075                 ScrArea *fullsa = NULL;
1076                 for (ScrArea *old = sc->areabase.first; old; old = old->next) {
1077                         /* area to restore from is always first */
1078                         if (old->full && !fullsa) {
1079                                 fullsa = old;
1080                         }
1081
1082                         /* clear full screen state */
1083                         old->full = NULL;
1084                 }
1085
1086                 sa->full = NULL;
1087
1088                 if (fullsa == NULL) {
1089                         if (G.debug & G_DEBUG)
1090                                 printf("%s: something wrong in areafullscreen\n", __func__);
1091                         return NULL;
1092                 }
1093
1094                 if (state == SCREENFULL) {
1095                         /* unhide global areas */
1096                         for (ScrArea *glob_area = win->global_areas.areabase.first; glob_area; glob_area = glob_area->next) {
1097                                 glob_area->global->flag &= ~GLOBAL_AREA_IS_HIDDEN;
1098                         }
1099                         /* restore the old side panels/header visibility */
1100                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1101                                 ar->flag = ar->flagfullscreen;
1102                         }
1103                 }
1104
1105                 ED_area_data_swap(fullsa, sa);
1106
1107                 /* animtimer back */
1108                 sc->animtimer = oldscreen->animtimer;
1109                 oldscreen->animtimer = NULL;
1110
1111                 ED_screen_change(C, sc);
1112
1113                 BKE_workspace_layout_remove(CTX_data_main(C), workspace, layout_old);
1114
1115                 /* After we've restored back to SCREENNORMAL, we have to wait with
1116                  * screen handling as it uses the area coords which aren't updated yet.
1117                  * Without doing so, the screen handling gets wrong area coords,
1118                  * which in worst case can lead to crashes (see T43139) */
1119                 sc->skip_handling = true;
1120         }
1121         else {
1122                 /* change from SCREENNORMAL to new state */
1123                 WorkSpaceLayout *layout_new;
1124                 ScrArea *newa;
1125                 char newname[MAX_ID_NAME - 2];
1126
1127                 BLI_assert(ELEM(state, SCREENMAXIMIZED, SCREENFULL));
1128
1129                 oldscreen = WM_window_get_active_screen(win);
1130
1131                 oldscreen->state = state;
1132                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1133
1134                 layout_new = ED_workspace_layout_add(bmain, workspace, win, newname);
1135
1136                 sc = BKE_workspace_layout_screen_get(layout_new);
1137                 sc->state = state;
1138                 sc->redraws_flag = oldscreen->redraws_flag;
1139                 sc->temp = oldscreen->temp;
1140
1141                 /* timer */
1142                 sc->animtimer = oldscreen->animtimer;
1143                 oldscreen->animtimer = NULL;
1144
1145                 /* use random area when we have no active one, e.g. when the
1146                  * mouse is outside of the window and we open a file browser */
1147                 if (!sa || sa->global) {
1148                         sa = oldscreen->areabase.first;
1149                 }
1150
1151                 newa = (ScrArea *)sc->areabase.first;
1152
1153                 /* copy area */
1154                 ED_area_data_swap(newa, sa);
1155                 newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1156
1157                 if (state == SCREENFULL) {
1158                         /* temporarily hide global areas */
1159                         for (ScrArea *glob_area = win->global_areas.areabase.first; glob_area; glob_area = glob_area->next) {
1160                                 glob_area->global->flag |= GLOBAL_AREA_IS_HIDDEN;
1161                         }
1162                         /* temporarily hide the side panels/header */
1163                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1164                                 ar->flagfullscreen = ar->flag;
1165
1166                                 if (ELEM(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_HEADER, RGN_TYPE_TOOLS)) {
1167                                         ar->flag |= RGN_FLAG_HIDDEN;
1168                                 }
1169                         }
1170                 }
1171
1172                 sa->full = oldscreen;
1173                 newa->full = oldscreen;
1174
1175                 ED_screen_change(C, sc);
1176         }
1177
1178         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1179         CTX_wm_area_set(C, sc->areabase.first);
1180
1181         return sc->areabase.first;
1182 }
1183
1184 /* update frame rate info for viewport drawing */
1185 void ED_refresh_viewport_fps(bContext *C)
1186 {
1187         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1188         Scene *scene = CTX_data_scene(C);
1189
1190         /* is anim playback running? */
1191         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1192                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1193
1194                 /* if there isn't any info, init it first */
1195                 if (fpsi == NULL)
1196                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1197
1198                 /* update the values */
1199                 fpsi->redrawtime = fpsi->lredrawtime;
1200                 fpsi->lredrawtime = animtimer->ltime;
1201         }
1202         else {
1203                 /* playback stopped or shouldn't be running */
1204                 if (scene->fps_info)
1205                         MEM_freeN(scene->fps_info);
1206                 scene->fps_info = NULL;
1207         }
1208 }
1209
1210 /* redraws: uses defines from stime->redraws
1211  * enable: 1 - forward on, -1 - backwards on, 0 - off
1212  */
1213 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1214 {
1215         bScreen *screen = CTX_wm_screen(C);
1216         wmWindowManager *wm = CTX_wm_manager(C);
1217         wmWindow *win = CTX_wm_window(C);
1218         Scene *scene = CTX_data_scene(C);
1219         bScreen *stopscreen = ED_screen_animation_playing(wm);
1220
1221         if (stopscreen) {
1222                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1223                 stopscreen->animtimer = NULL;
1224         }
1225
1226         if (enable) {
1227                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1228
1229                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1230
1231                 sad->ar = CTX_wm_region(C);
1232                 /* if startframe is larger than current frame, we put currentframe on startframe.
1233                  * note: first frame then is not drawn! (ton) */
1234                 if (PRVRANGEON) {
1235                         if (scene->r.psfra > scene->r.cfra) {
1236                                 sad->sfra = scene->r.cfra;
1237                                 scene->r.cfra = scene->r.psfra;
1238                         }
1239                         else
1240                                 sad->sfra = scene->r.cfra;
1241                 }
1242                 else {
1243                         if (scene->r.sfra > scene->r.cfra) {
1244                                 sad->sfra = scene->r.cfra;
1245                                 scene->r.cfra = scene->r.sfra;
1246                         }
1247                         else
1248                                 sad->sfra = scene->r.cfra;
1249                 }
1250                 sad->redraws = redraws;
1251                 sad->refresh = refresh;
1252                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1253                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1254
1255                 ScrArea *sa = CTX_wm_area(C);
1256
1257                 char spacetype = -1;
1258
1259                 if (sa)
1260                         spacetype = sa->spacetype;
1261
1262                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA));
1263
1264                 screen->animtimer->customdata = sad;
1265
1266         }
1267
1268         /* notifier catched by top header, for button */
1269         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1270 }
1271
1272 /* helper for screen_animation_play() - only to be used for TimeLine */
1273 static ARegion *time_top_left_3dwindow(bScreen *screen)
1274 {
1275         ARegion *aret = NULL;
1276         ScrArea *sa;
1277         int min = 10000;
1278
1279         for (sa = screen->areabase.first; sa; sa = sa->next) {
1280                 if (sa->spacetype == SPACE_VIEW3D) {
1281                         ARegion *ar;
1282                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1283                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1284                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1285                                                 aret = ar;
1286                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1287                                         }
1288                                 }
1289                         }
1290                 }
1291         }
1292
1293         return aret;
1294 }
1295
1296 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1297 {
1298         if (screen && screen->animtimer) {
1299                 wmTimer *wt = screen->animtimer;
1300                 ScreenAnimData *sad = wt->customdata;
1301
1302                 sad->redraws = redraws;
1303                 sad->refresh = refresh;
1304                 sad->ar = NULL;
1305                 if (redraws & TIME_REGION)
1306                         sad->ar = time_top_left_3dwindow(screen);
1307         }
1308 }
1309
1310 /* results in fully updated anim system */
1311 void ED_update_for_newframe(Main *bmain, Depsgraph *depsgraph)
1312 {
1313         Scene *scene = DEG_get_input_scene(depsgraph);
1314
1315 #ifdef DURIAN_CAMERA_SWITCH
1316         void *camera = BKE_scene_camera_switch_find(scene);
1317         if (camera && scene->camera != camera) {
1318                 bScreen *sc;
1319                 scene->camera = camera;
1320                 /* are there cameras in the views that are not in the scene? */
1321                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1322                         BKE_screen_view3d_scene_sync(sc, scene);
1323                 }
1324                 DEG_id_tag_update(&scene->id, DEG_TAG_COPY_ON_WRITE);
1325         }
1326 #endif
1327
1328         ED_clip_update_frame(bmain, scene->r.cfra);
1329
1330         /* this function applies the changes too */
1331         BKE_scene_graph_update_for_newframe(depsgraph, bmain);
1332
1333         /* composite */
1334         if (scene->use_nodes && scene->nodetree)
1335                 ntreeCompositTagAnimated(scene->nodetree);
1336
1337         /* update animated texture nodes */
1338         {
1339                 Tex *tex;
1340                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
1341                         if (tex->use_nodes && tex->nodetree) {
1342                                 ntreeTexTagAnimated(tex->nodetree);
1343                         }
1344                 }
1345         }
1346
1347 }
1348
1349 /*
1350  * return true if any active area requires to see in 3D
1351  */
1352 bool ED_screen_stereo3d_required(const bScreen *screen, const Scene *scene)
1353 {
1354         ScrArea *sa;
1355         const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
1356
1357         for (sa = screen->areabase.first; sa; sa = sa->next) {
1358                 switch (sa->spacetype) {
1359                         case SPACE_VIEW3D:
1360                         {
1361                                 View3D *v3d;
1362
1363                                 if (!is_multiview)
1364                                         continue;
1365
1366                                 v3d = sa->spacedata.first;
1367                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
1368                                         ARegion *ar;
1369                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1370                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
1371                                                         RegionView3D *rv3d = ar->regiondata;
1372                                                         if (rv3d->persp == RV3D_CAMOB) {
1373                                                                 return true;
1374                                                         }
1375                                                 }
1376                                         }
1377                                 }
1378                                 break;
1379                         }
1380                         case SPACE_IMAGE:
1381                         {
1382                                 SpaceImage *sima;
1383
1384                                 /* images should always show in stereo, even if
1385                                  * the file doesn't have views enabled */
1386                                 sima = sa->spacedata.first;
1387                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
1388                                     (sima->iuser.flag & IMA_SHOW_STEREO))
1389                                 {
1390                                         return true;
1391                                 }
1392                                 break;
1393                         }
1394                         case SPACE_NODE:
1395                         {
1396                                 SpaceNode *snode;
1397
1398                                 if (!is_multiview)
1399                                         continue;
1400
1401                                 snode = sa->spacedata.first;
1402                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
1403                                         return true;
1404                                 }
1405                                 break;
1406                         }
1407                         case SPACE_SEQ:
1408                         {
1409                                 SpaceSeq *sseq;
1410
1411                                 if (!is_multiview)
1412                                         continue;
1413
1414                                 sseq = sa->spacedata.first;
1415                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
1416                                         return true;
1417                                 }
1418
1419                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1420                                         return true;
1421                                 }
1422
1423                                 break;
1424                         }
1425                 }
1426         }
1427
1428         return false;
1429 }
1430
1431 /**
1432  * Find the scene displayed in \a screen.
1433  * \note Assumes \a screen to be visible/active!
1434  */
1435
1436 Scene *ED_screen_scene_find_with_window(const bScreen *screen, const wmWindowManager *wm, struct wmWindow **r_window)
1437 {
1438         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1439                 if (WM_window_get_active_screen(win) == screen) {
1440                         if (r_window) {
1441                                 *r_window = win;
1442                         }
1443                         return WM_window_get_active_scene(win);
1444                 }
1445         }
1446
1447         BLI_assert(0);
1448         return NULL;
1449 }
1450
1451
1452 Scene *ED_screen_scene_find(const bScreen *screen, const wmWindowManager *wm)
1453 {
1454         return ED_screen_scene_find_with_window(screen, wm, NULL);
1455 }
1456
1457
1458 wmWindow *ED_screen_window_find(const bScreen *screen, const wmWindowManager *wm)
1459 {
1460         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1461                 if (WM_window_get_active_screen(win) == screen) {
1462                         return win;
1463                 }
1464         }
1465         return NULL;
1466 }