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