UI: toolbar single column size snapping
[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 /* ******************* screen vert, edge, area managing *********************** */
78
79 static ScrVert *screen_addvert_ex(ScrAreaMap *area_map, short x, short y)
80 {
81         ScrVert *sv = MEM_callocN(sizeof(ScrVert), "addscrvert");
82         sv->vec.x = x;
83         sv->vec.y = y;
84         
85         BLI_addtail(&area_map->vertbase, sv);
86         return sv;
87 }
88 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
89 {
90         return screen_addvert_ex(AREAMAP_FROM_SCREEN(sc), x, y);
91 }
92
93 static ScrEdge *screen_addedge_ex(ScrAreaMap *area_map, ScrVert *v1, ScrVert *v2)
94 {
95         ScrEdge *se = MEM_callocN(sizeof(ScrEdge), "addscredge");
96
97         BKE_screen_sort_scrvert(&v1, &v2);
98         se->v1 = v1;
99         se->v2 = v2;
100
101         BLI_addtail(&area_map->edgebase, se);
102         return se;
103 }
104 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
105 {
106         return screen_addedge_ex(AREAMAP_FROM_SCREEN(sc), v1, v2);
107 }
108
109 bool scredge_is_horizontal(ScrEdge *se)
110 {
111         return (se->v1->vec.y == se->v2->vec.y);
112 }
113
114 /* need win size to make sure not to include edges along screen edge */
115 ScrEdge *screen_area_map_find_active_scredge(
116         const ScrAreaMap *area_map,
117         const int winsize_x, const int winsize_y,
118         const int mx, const int my)
119 {
120         int safety = U.widget_unit / 10;
121
122         CLAMP_MIN(safety, 2);
123
124         for (ScrEdge *se = area_map->edgebase.first; se; se = se->next) {
125                 if (scredge_is_horizontal(se)) {
126                         if (se->v1->vec.y > 0 && se->v1->vec.y < winsize_y - 1) {
127                                 short min, max;
128                                 min = MIN2(se->v1->vec.x, se->v2->vec.x);
129                                 max = MAX2(se->v1->vec.x, se->v2->vec.x);
130                                 
131                                 if (abs(my - se->v1->vec.y) <= safety && mx >= min && mx <= max)
132                                         return se;
133                         }
134                 }
135                 else {
136                         if (se->v1->vec.x > 0 && se->v1->vec.x < winsize_x - 1) {
137                                 short min, max;
138                                 min = MIN2(se->v1->vec.y, se->v2->vec.y);
139                                 max = MAX2(se->v1->vec.y, se->v2->vec.y);
140                                 
141                                 if (abs(mx - se->v1->vec.x) <= safety && my >= min && my <= max)
142                                         return se;
143                         }
144                 }
145         }
146
147         return NULL;
148 }
149
150 /* need win size to make sure not to include edges along screen edge */
151 ScrEdge *screen_find_active_scredge(
152         const wmWindow *win, const bScreen *screen,
153         const int mx, const int my)
154 {
155         /* Use layout size (screen excluding global areas) for screen-layout area edges */
156         const int screen_x = WM_window_screen_pixels_x(win), screen_y = WM_window_screen_pixels_y(win);
157         ScrEdge *se = screen_area_map_find_active_scredge(AREAMAP_FROM_SCREEN(screen), screen_x, screen_y, mx, my);
158
159         if (!se) {
160                 /* Use entire window size (screen including global areas) for global area edges */
161                 const int win_x = WM_window_pixels_x(win), win_y = WM_window_pixels_y(win);
162                 se = screen_area_map_find_active_scredge(&win->global_areas, win_x, win_y, mx, my);
163         }
164         return se;
165 }
166
167
168
169 /* adds no space data */
170 static ScrArea *screen_addarea_ex(
171         ScrAreaMap *area_map,
172         ScrVert *bottom_left, ScrVert *top_left, ScrVert *top_right, ScrVert *bottom_right,
173         short headertype, short spacetype)
174 {
175         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
176
177         sa->v1 = bottom_left;
178         sa->v2 = top_left;
179         sa->v3 = top_right;
180         sa->v4 = bottom_right;
181         sa->headertype = headertype;
182         sa->spacetype = spacetype;
183
184         BLI_addtail(&area_map->areabase, sa);
185
186         return sa;
187 }
188 static ScrArea *screen_addarea(
189         bScreen *sc,
190         ScrVert *left_bottom, ScrVert *left_top, ScrVert *right_top, ScrVert *right_bottom,
191         short headertype, short spacetype)
192 {
193         return screen_addarea_ex(AREAMAP_FROM_SCREEN(sc), left_bottom, left_top, right_top, right_bottom,
194                                  headertype, spacetype);
195 }
196
197 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
198 {
199         
200         ED_area_exit(C, sa);
201         
202         BKE_screen_area_free(sa);
203         
204         BLI_remlink(&sc->areabase, sa);
205         MEM_freeN(sa);
206 }
207
208 /* return 0: no split possible */
209 /* else return (integer) screencoordinate split point */
210 static short testsplitpoint(ScrArea *sa, char dir, float fac)
211 {
212         short x, y;
213         const short area_min_x = AREAMINX;
214         const short area_min_y = ED_area_headersize();
215         
216         // area big enough?
217         if (dir == 'v' && (sa->v4->vec.x - sa->v1->vec.x <= 2 * area_min_x)) return 0;
218         if (dir == 'h' && (sa->v2->vec.y - sa->v1->vec.y <= 2 * area_min_y)) return 0;
219         
220         // to be sure
221         CLAMP(fac, 0.0f, 1.0f);
222         
223         if (dir == 'h') {
224                 y = sa->v1->vec.y + fac * (sa->v2->vec.y - sa->v1->vec.y);
225                 
226                 if (y - sa->v1->vec.y < area_min_y)
227                         y = sa->v1->vec.y + area_min_y;
228                 else if (sa->v2->vec.y - y < area_min_y)
229                         y = sa->v2->vec.y - area_min_y;
230                 else y -= (y % AREAGRID);
231                 
232                 return y;
233         }
234         else {
235                 x = sa->v1->vec.x + fac * (sa->v4->vec.x - sa->v1->vec.x);
236                 
237                 if (x - sa->v1->vec.x < area_min_x)
238                         x = sa->v1->vec.x + area_min_x;
239                 else if (sa->v4->vec.x - x < area_min_x)
240                         x = sa->v4->vec.x - area_min_x;
241                 else x -= (x % AREAGRID);
242                 
243                 return x;
244         }
245 }
246
247 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
248 {
249         ScrArea *newa = NULL;
250         ScrVert *sv1, *sv2;
251         short split;
252         
253         if (sa == NULL) return NULL;
254         
255         split = testsplitpoint(sa, dir, fac);
256         if (split == 0) return NULL;
257         
258         /* note regarding (fac > 0.5f) checks below.
259          * normally it shouldn't matter which is used since the copy should match the original
260          * however with viewport rendering and python console this isn't the case. - campbell */
261
262         if (dir == 'h') {
263                 /* new vertices */
264                 sv1 = screen_addvert(sc, sa->v1->vec.x, split);
265                 sv2 = screen_addvert(sc, sa->v4->vec.x, split);
266                 
267                 /* new edges */
268                 screen_addedge(sc, sa->v1, sv1);
269                 screen_addedge(sc, sv1, sa->v2);
270                 screen_addedge(sc, sa->v3, sv2);
271                 screen_addedge(sc, sv2, sa->v4);
272                 screen_addedge(sc, sv1, sv2);
273                 
274                 if (fac > 0.5f) {
275                         /* new areas: top */
276                         newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
277
278                         /* area below */
279                         sa->v2 = sv1;
280                         sa->v3 = sv2;
281                 }
282                 else {
283                         /* new areas: bottom */
284                         newa = screen_addarea(sc, sa->v1, sv1, sv2, sa->v4, sa->headertype, sa->spacetype);
285
286                         /* area above */
287                         sa->v1 = sv1;
288                         sa->v4 = sv2;
289                 }
290
291                 ED_area_data_copy(newa, sa, true);
292                 
293         }
294         else {
295                 /* new vertices */
296                 sv1 = screen_addvert(sc, split, sa->v1->vec.y);
297                 sv2 = screen_addvert(sc, split, sa->v2->vec.y);
298                 
299                 /* new edges */
300                 screen_addedge(sc, sa->v1, sv1);
301                 screen_addedge(sc, sv1, sa->v4);
302                 screen_addedge(sc, sa->v2, sv2);
303                 screen_addedge(sc, sv2, sa->v3);
304                 screen_addedge(sc, sv1, sv2);
305                 
306                 if (fac > 0.5f) {
307                         /* new areas: right */
308                         newa = screen_addarea(sc, sv1, sv2, sa->v3, sa->v4, sa->headertype, sa->spacetype);
309
310                         /* area left */
311                         sa->v3 = sv2;
312                         sa->v4 = sv1;
313                 }
314                 else {
315                         /* new areas: left */
316                         newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
317
318                         /* area right */
319                         sa->v1 = sv1;
320                         sa->v2 = sv2;
321                 }
322
323                 ED_area_data_copy(newa, sa, true);
324         }
325         
326         /* remove double vertices en edges */
327         if (merge)
328                 BKE_screen_remove_double_scrverts(sc);
329         BKE_screen_remove_double_scredges(sc);
330         BKE_screen_remove_unused_scredges(sc);
331         
332         return newa;
333 }
334
335 /**
336  * Empty screen, with 1 dummy area without spacedata. Uses window size.
337  */
338 bScreen *screen_add(const char *name, const int winsize_x, const int winsize_y)
339 {
340         bScreen *sc;
341         ScrVert *sv1, *sv2, *sv3, *sv4;
342         
343         sc = BKE_libblock_alloc(G.main, ID_SCR, name, 0);
344         sc->do_refresh = true;
345         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
346
347         sv1 = screen_addvert(sc, 0, 0);
348         sv2 = screen_addvert(sc, 0, winsize_y - 1);
349         sv3 = screen_addvert(sc, winsize_x - 1, winsize_y - 1);
350         sv4 = screen_addvert(sc, winsize_x - 1, 0);
351         
352         screen_addedge(sc, sv1, sv2);
353         screen_addedge(sc, sv2, sv3);
354         screen_addedge(sc, sv3, sv4);
355         screen_addedge(sc, sv4, sv1);
356         
357         /* dummy type, no spacedata */
358         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
359                 
360         return sc;
361 }
362
363 void screen_data_copy(bScreen *to, bScreen *from)
364 {
365         ScrVert *s1, *s2;
366         ScrEdge *se;
367         ScrArea *sa, *saf;
368         
369         /* free contents of 'to', is from blenkernel screen.c */
370         BKE_screen_free(to);
371         
372         BLI_duplicatelist(&to->vertbase, &from->vertbase);
373         BLI_duplicatelist(&to->edgebase, &from->edgebase);
374         BLI_duplicatelist(&to->areabase, &from->areabase);
375         BLI_listbase_clear(&to->regionbase);
376         
377         s2 = to->vertbase.first;
378         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
379                 s1->newv = s2;
380         }
381         
382         for (se = to->edgebase.first; se; se = se->next) {
383                 se->v1 = se->v1->newv;
384                 se->v2 = se->v2->newv;
385                 BKE_screen_sort_scrvert(&(se->v1), &(se->v2));
386         }
387         
388         saf = from->areabase.first;
389         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
390                 sa->v1 = sa->v1->newv;
391                 sa->v2 = sa->v2->newv;
392                 sa->v3 = sa->v3->newv;
393                 sa->v4 = sa->v4->newv;
394
395                 BLI_listbase_clear(&sa->spacedata);
396                 BLI_listbase_clear(&sa->regionbase);
397                 BLI_listbase_clear(&sa->actionzones);
398                 BLI_listbase_clear(&sa->handlers);
399                 
400                 ED_area_data_copy(sa, saf, true);
401         }
402         
403         /* put at zero (needed?) */
404         for (s1 = from->vertbase.first; s1; s1 = s1->next)
405                 s1->newv = NULL;
406 }
407
408 /**
409  * Prepare a newly created screen for initializing it as active screen.
410  */
411 void screen_new_activate_prepare(const wmWindow *win, bScreen *screen_new)
412 {
413         screen_new->winid = win->winid;
414         screen_new->do_refresh = true;
415         screen_new->do_draw = true;
416 }
417
418
419 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
420 /* -1 = not valid check */
421 /* used with join operator */
422 int area_getorientation(ScrArea *sa, ScrArea *sb)
423 {
424         ScrVert *sav1, *sav2, *sav3, *sav4;
425         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
426
427         if (sa == NULL || sb == NULL) return -1;
428
429         sav1 = sa->v1;
430         sav2 = sa->v2;
431         sav3 = sa->v3;
432         sav4 = sa->v4;
433         sbv1 = sb->v1;
434         sbv2 = sb->v2;
435         sbv3 = sb->v3;
436         sbv4 = sb->v4;
437         
438         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
439                 return 0;
440         }
441         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
442                 return 1;
443         }
444         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
445                 return 2;
446         }
447         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
448                 return 3;
449         }
450         
451         return -1;
452 }
453
454 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
455  *  used by the split, join operators
456  */
457 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
458 {
459         int dir;
460         
461         dir = area_getorientation(sa1, sa2);
462         /*printf("dir is : %i\n", dir);*/
463         
464         if (dir == -1) {
465                 return 0;
466         }
467         
468         if (dir == 0) {
469                 sa1->v1 = sa2->v1;
470                 sa1->v2 = sa2->v2;
471                 screen_addedge(scr, sa1->v2, sa1->v3);
472                 screen_addedge(scr, sa1->v1, sa1->v4);
473         }
474         else if (dir == 1) {
475                 sa1->v2 = sa2->v2;
476                 sa1->v3 = sa2->v3;
477                 screen_addedge(scr, sa1->v1, sa1->v2);
478                 screen_addedge(scr, sa1->v3, sa1->v4);
479         }
480         else if (dir == 2) {
481                 sa1->v3 = sa2->v3;
482                 sa1->v4 = sa2->v4;
483                 screen_addedge(scr, sa1->v2, sa1->v3);
484                 screen_addedge(scr, sa1->v1, sa1->v4);
485         }
486         else if (dir == 3) {
487                 sa1->v1 = sa2->v1;
488                 sa1->v4 = sa2->v4;
489                 screen_addedge(scr, sa1->v1, sa1->v2);
490                 screen_addedge(scr, sa1->v3, sa1->v4);
491         }
492         
493         screen_delarea(C, scr, sa2);
494         BKE_screen_remove_double_scrverts(scr);
495         /* Update preview thumbnail */
496         BKE_icon_changed(scr->id.icon_id);
497
498         return 1;
499 }
500
501 void select_connected_scredge(const wmWindow *win, ScrEdge *edge)
502 {
503         bScreen *sc = WM_window_get_active_screen(win);
504         ScrEdge *se;
505         int oneselected;
506         char dir;
507         
508         /* select connected, only in the right direction */
509         /* 'dir' is the direction of EDGE */
510         
511         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
512         else dir = 'h';
513         
514         ED_screen_verts_iter(win, sc, sv) {
515                 sv->flag = 0;
516         }
517
518         edge->v1->flag = 1;
519         edge->v2->flag = 1;
520         
521         oneselected = 1;
522         while (oneselected) {
523                 se = sc->edgebase.first;
524                 oneselected = 0;
525                 while (se) {
526                         if (se->v1->flag + se->v2->flag == 1) {
527                                 if (dir == 'h') {
528                                         if (se->v1->vec.y == se->v2->vec.y) {
529                                                 se->v1->flag = se->v2->flag = 1;
530                                                 oneselected = 1;
531                                         }
532                                 }
533                                 if (dir == 'v') {
534                                         if (se->v1->vec.x == se->v2->vec.x) {
535                                                 se->v1->flag = se->v2->flag = 1;
536                                                 oneselected = 1;
537                                         }
538                                 }
539                         }
540                         se = se->next;
541                 }
542         }
543 }
544
545 /**
546  * Test if screen vertices should be scaled and do if needed.
547  */
548 static void screen_vertices_scale(
549         const wmWindow *win, bScreen *sc,
550         int window_size_x, int window_size_y,
551         int screen_size_x, int screen_size_y)
552 {
553         /* clamp Y size of header sized areas when expanding windows
554          * avoids annoying empty space around file menu */
555 #define USE_HEADER_SIZE_CLAMP
556
557         const int headery_init = ED_area_headersize();
558         ScrVert *sv = NULL;
559         ScrArea *sa;
560         int screen_size_x_prev, screen_size_y_prev;
561         float facx, facy, tempf, min[2], max[2];
562         
563         /* calculate size */
564         min[0] = min[1] = 20000.0f;
565         max[0] = max[1] = 0.0f;
566         
567         for (sv = sc->vertbase.first; sv; sv = sv->next) {
568                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
569                 minmax_v2v2_v2(min, max, fv);
570         }
571         
572         /* always make 0.0 left under */
573         for (sv = sc->vertbase.first; sv; sv = sv->next) {
574                 sv->vec.x -= min[0];
575                 sv->vec.y -= min[1];
576         }
577         
578         screen_size_x_prev = (max[0] - min[0]) + 1;
579         screen_size_y_prev = (max[1] - min[1]) + 1;
580
581
582 #ifdef USE_HEADER_SIZE_CLAMP
583 #define TEMP_BOTTOM 1
584 #define TEMP_TOP 2
585
586         /* if the window's Y axis grows, clamp header sized areas */
587         if (screen_size_y_prev < screen_size_y) {  /* growing? */
588                 const int headery_margin_max = headery_init + 4;
589                 for (sa = sc->areabase.first; sa; sa = sa->next) {
590                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
591                         sa->temp = 0;
592
593                         if (ar && !(ar->flag & RGN_FLAG_HIDDEN)) {
594                                 if (sa->v2->vec.y == screen_size_y_prev) {
595                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
596                                                 sa->temp = TEMP_TOP;
597                                         }
598                                 }
599                                 else if (sa->v1->vec.y == 0) {
600                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
601                                                 sa->temp = TEMP_BOTTOM;
602                                         }
603                                 }
604                         }
605                 }
606         }
607 #endif
608
609
610         if (screen_size_x_prev != screen_size_x || screen_size_y_prev != screen_size_y) {
611                 facx = ((float)screen_size_x - 1) / ((float)screen_size_x_prev - 1);
612                 facy = ((float)screen_size_y) / ((float)screen_size_y_prev);
613                 
614                 /* make sure it fits! */
615                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
616                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
617                          * need some way to store these as floats internally and re-apply from there. */
618                         tempf = ((float)sv->vec.x) * facx;
619                         sv->vec.x = (short)(tempf + 0.5f);
620                         //sv->vec.x += AREAGRID - 1;
621                         //sv->vec.x -=  (sv->vec.x % AREAGRID);
622
623                         CLAMP(sv->vec.x, 0, screen_size_x - 1);
624                         
625                         tempf = ((float)sv->vec.y) * facy;
626                         sv->vec.y = (short)(tempf + 0.5f);
627                         //sv->vec.y += AREAGRID - 1;
628                         //sv->vec.y -=  (sv->vec.y % AREAGRID);
629
630                         CLAMP(sv->vec.y, 0, screen_size_y);
631                 }
632         }
633
634
635 #ifdef USE_HEADER_SIZE_CLAMP
636         if (screen_size_y_prev < screen_size_y) {  /* growing? */
637                 for (sa = sc->areabase.first; sa; sa = sa->next) {
638                         ScrEdge *se = NULL;
639
640                         if (sa->temp == 0)
641                                 continue;
642
643                         if (sa->v1 == sa->v2)
644                                 continue;
645
646                         /* adjust headery if verts are along the edge of window */
647                         if (sa->temp == TEMP_TOP) {
648                                 /* lower edge */
649                                 const int yval = sa->v2->vec.y - headery_init;
650                                 se = BKE_screen_find_edge(sc, sa->v4, sa->v1);
651                                 if (se != NULL) {
652                                         select_connected_scredge(win, se);
653                                 }
654                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
655                                         if (sv != sa->v2 && sv != sa->v3) {
656                                                 if (sv->flag) {
657                                                         sv->vec.y = yval;
658                                                 }
659                                         }
660                                 }
661                         }
662                         else {
663                                 /* upper edge */
664                                 const int yval = sa->v1->vec.y + headery_init;
665                                 se = BKE_screen_find_edge(sc, sa->v2, sa->v3);
666                                 if (se != NULL) {
667                                         select_connected_scredge(win, se);
668                                 }
669                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
670                                         if (sv != sa->v1 && sv != sa->v4) {
671                                                 if (sv->flag) {
672                                                         sv->vec.y = yval;
673                                                 }
674                                         }
675                                 }
676                         }
677                 }
678         }
679
680 #undef USE_HEADER_SIZE_CLAMP
681 #undef TEMP_BOTTOM
682 #undef TEMP_TOP
683 #endif
684
685
686         /* test for collapsed areas. This could happen in some blender version... */
687         /* ton: removed option now, it needs Context... */
688         
689         /* make each window at least ED_area_headersize() high */
690         for (sa = sc->areabase.first; sa; sa = sa->next) {
691                 int headery = headery_init;
692                 
693                 /* adjust headery if verts are along the edge of window */
694                 if (sa->v1->vec.y > 0)
695                         headery += U.pixelsize;
696                 if (sa->v2->vec.y < screen_size_y)
697                         headery += U.pixelsize;
698                 
699                 if (sa->v2->vec.y - sa->v1->vec.y + 1 < headery) {
700                         /* lower edge */
701                         ScrEdge *se = BKE_screen_find_edge(sc, sa->v4, sa->v1);
702                         if (se && sa->v1 != sa->v2) {
703                                 int yval;
704                                 
705                                 select_connected_scredge(win, se);
706                                 
707                                 /* all selected vertices get the right offset */
708                                 yval = sa->v2->vec.y - headery + 1;
709                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
710                                         /* if is a collapsed area */
711                                         if (sv != sa->v2 && sv != sa->v3) {
712                                                 if (sv->flag) {
713                                                         sv->vec.y = yval;
714                                                 }
715                                         }
716                                 }
717                         }
718                 }
719         }
720
721         /* Global areas have a fixed size that only changes with the DPI. Here we ensure that exactly this size is set.
722          * TODO Assumes global area to be top-aligned. Should be made more generic */
723         for (ScrArea *area = win->global_areas.areabase.first; area; area = area->next) {
724                 if (area->global->flag & GLOBAL_AREA_IS_HIDDEN) {
725                         continue;
726                 }
727                 /* width */
728                 area->v1->vec.x = area->v2->vec.x = 0;
729                 area->v3->vec.x = area->v4->vec.x = window_size_x - 1;
730                 /* height */
731                 area->v2->vec.y = area->v3->vec.y = window_size_y - 1;
732                 area->v1->vec.y = area->v4->vec.y = area->v2->vec.y - ED_area_global_size_y(area);
733         }
734 }
735
736
737 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
738
739 /* screen sets cursor based on active region */
740 static void region_cursor_set(wmWindow *win, bool swin_changed)
741 {
742         bScreen *screen = WM_window_get_active_screen(win);
743
744         ED_screen_areas_iter(win, screen, sa) {
745                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
746                         if (ar == screen->active_region) {
747                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
748                                         if (ar->manipulator_map != NULL) {
749                                                 if (WM_manipulatormap_cursor_set(ar->manipulator_map, win)) {
750                                                         return;
751                                                 }
752                                         }
753                                         ED_region_cursor_set(win, sa, ar);
754                                 }
755                                 return;
756                         }
757                 }
758         }
759 }
760
761 void ED_screen_do_listen(bContext *C, wmNotifier *note)
762 {
763         wmWindow *win = CTX_wm_window(C);
764         bScreen *screen = CTX_wm_screen(C);
765
766         /* generic notes */
767         switch (note->category) {
768                 case NC_WM:
769                         if (note->data == ND_FILEREAD)
770                                 screen->do_draw = true;
771                         break;
772                 case NC_WINDOW:
773                         screen->do_draw = true;
774                         break;
775                 case NC_SCREEN:
776                         if (note->action == NA_EDITED)
777                                 screen->do_draw = screen->do_refresh = true;
778                         break;
779                 case NC_SCENE:
780                         if (note->data == ND_MODE)
781                                 region_cursor_set(win, true);
782                         break;
783         }
784 }
785
786 /* helper call for below, dpi changes headers */
787 static void screen_refresh_headersizes(void)
788 {
789         const ListBase *lb = BKE_spacetypes_list();
790         SpaceType *st;
791         
792         for (st = lb->first; st; st = st->next) {
793                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
794                 if (art) art->prefsizey = ED_area_headersize();
795         }
796 }
797
798 /* make this screen usable */
799 /* for file read and first use, for scaling window, area moves */
800 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
801 {
802         bScreen *screen = WM_window_get_active_screen(win);
803
804         /* exception for bg mode, we only need the screen context */
805         if (!G.background) {
806                 const int window_size_x = WM_window_pixels_x(win);
807                 const int window_size_y = WM_window_pixels_y(win);
808                 const int screen_size_x = WM_window_screen_pixels_x(win);
809                 const int screen_size_y = WM_window_screen_pixels_y(win);
810
811                 /* header size depends on DPI, let's verify */
812                 WM_window_set_dpi(win);
813                 screen_refresh_headersizes();
814
815                 screen_vertices_scale(win, screen, window_size_x, window_size_y, screen_size_x, screen_size_y);
816
817                 ED_screen_areas_iter(win, screen, area) {
818                         /* set spacetype and region callbacks, calls init() */
819                         /* sets subwindows for regions, adds handlers */
820                         ED_area_initialize(wm, win, area);
821                 }
822         
823                 /* wake up animtimer */
824                 if (screen->animtimer)
825                         WM_event_timer_sleep(wm, win, screen->animtimer, false);
826         }
827
828         if (G.debug & G_DEBUG_EVENTS) {
829                 printf("%s: set screen\n", __func__);
830         }
831         screen->do_refresh = false;
832         /* prevent multiwin errors */
833         screen->winid = win->winid;
834
835         screen->context = ed_screen_context;
836 }
837
838 static bool screen_regions_need_size_refresh(
839         const wmWindow *win, const bScreen *screen)
840 {
841         ED_screen_areas_iter(win, screen, area) {
842                 if (area->flag & AREA_FLAG_REGION_SIZE_UPDATE) {
843                         return true;
844                 }
845         }
846
847         return false;
848 }
849
850 static void screen_refresh_region_sizes_only(
851         wmWindowManager *wm, wmWindow *win,
852         bScreen *screen)
853 {
854         const int window_size_x = WM_window_pixels_x(win);
855         const int window_size_y = WM_window_pixels_y(win);
856         const int screen_size_x = WM_window_screen_pixels_x(win);
857         const int screen_size_y = WM_window_screen_pixels_y(win);
858
859         screen_vertices_scale(win, screen, window_size_x, window_size_y, screen_size_x, screen_size_y);
860
861         ED_screen_areas_iter(win, screen, area) {
862                 screen_area_update_region_sizes(wm, win, area);
863                 /* XXX hack to force drawing */
864                 ED_area_tag_redraw(area);
865         }
866 }
867
868 /* file read, set all screens, ... */
869 void ED_screens_initialize(wmWindowManager *wm)
870 {
871         wmWindow *win;
872         
873         for (win = wm->windows.first; win; win = win->next) {
874                 if (WM_window_get_active_workspace(win) == NULL) {
875                         WM_window_set_active_workspace(win, G.main->workspaces.first);
876                 }
877
878                 if (BLI_listbase_is_empty(&win->global_areas.areabase)) {
879                         ED_screen_global_areas_create(win);
880                 }
881                 ED_screen_refresh(wm, win);
882         }
883 }
884
885 void ED_screen_ensure_updated(wmWindowManager *wm, wmWindow *win, bScreen *screen)
886 {
887         if (screen->do_refresh) {
888                 ED_screen_refresh(wm, win);
889         }
890         else if (screen_regions_need_size_refresh(win, screen)) {
891                 screen_refresh_region_sizes_only(wm, win, screen);
892         }
893 }
894
895
896 /* *********** exit calls are for closing running stuff ******** */
897
898 void ED_region_exit(bContext *C, ARegion *ar)
899 {
900         wmWindowManager *wm = CTX_wm_manager(C);
901         wmWindow *win = CTX_wm_window(C);
902         ARegion *prevar = CTX_wm_region(C);
903
904         if (ar->type && ar->type->exit)
905                 ar->type->exit(wm, ar);
906
907         CTX_wm_region_set(C, ar);
908
909         WM_event_remove_handlers(C, &ar->handlers);
910         WM_event_modal_handler_region_replace(win, ar, NULL);
911         ar->visible = 0;
912         
913         if (ar->headerstr) {
914                 MEM_freeN(ar->headerstr);
915                 ar->headerstr = NULL;
916         }
917         
918         if (ar->regiontimer) {
919                 WM_event_remove_timer(wm, win, ar->regiontimer);
920                 ar->regiontimer = NULL;
921         }
922
923         WM_msgbus_clear_by_owner(wm->message_bus, ar);
924
925         CTX_wm_region_set(C, prevar);
926 }
927
928 void ED_area_exit(bContext *C, ScrArea *sa)
929 {
930         wmWindowManager *wm = CTX_wm_manager(C);
931         wmWindow *win = CTX_wm_window(C);
932         ScrArea *prevsa = CTX_wm_area(C);
933         ARegion *ar;
934
935         if (sa->type && sa->type->exit)
936                 sa->type->exit(wm, sa);
937
938         CTX_wm_area_set(C, sa);
939
940         for (ar = sa->regionbase.first; ar; ar = ar->next)
941                 ED_region_exit(C, ar);
942
943         WM_event_remove_handlers(C, &sa->handlers);
944         WM_event_modal_handler_area_replace(win, sa, NULL);
945
946         CTX_wm_area_set(C, prevsa);
947 }
948
949 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
950 {
951         wmWindowManager *wm = CTX_wm_manager(C);
952         wmWindow *prevwin = CTX_wm_window(C);
953         ScrArea *sa;
954         ARegion *ar;
955
956         CTX_wm_window_set(C, window);
957         
958         if (screen->animtimer)
959                 WM_event_remove_timer(wm, window, screen->animtimer);
960         screen->animtimer = NULL;
961         screen->scrubbing = false;
962
963         screen->active_region = NULL;
964         
965         for (ar = screen->regionbase.first; ar; ar = ar->next) {
966                 ED_region_exit(C, ar);
967         }
968         for (sa = screen->areabase.first; sa; sa = sa->next) {
969                 ED_area_exit(C, sa);
970         }
971         for (sa = window->global_areas.areabase.first; sa; sa = sa->next) {
972                 ED_area_exit(C, sa);
973         }
974
975         /* mark it available for use for other windows */
976         screen->winid = 0;
977         
978         if (!WM_window_is_temp_screen(prevwin)) {
979                 /* use previous window if possible */
980                 CTX_wm_window_set(C, prevwin);
981         }
982         else {
983                 /* none otherwise */
984                 CTX_wm_window_set(C, NULL);
985         }
986         
987 }
988
989 /* *********************************** */
990
991 /* case when on area-edge or in azones, or outside window */
992 static void screen_cursor_set(wmWindow *win, const int xy[2])
993 {
994         const bScreen *screen = WM_window_get_active_screen(win);
995         AZone *az = NULL;
996         ScrArea *sa;
997         
998         for (sa = screen->areabase.first; sa; sa = sa->next)
999                 if ((az = is_in_area_actionzone(sa, xy)))
1000                         break;
1001         
1002         if (sa) {
1003                 if (az->type == AZONE_AREA)
1004                         WM_cursor_set(win, CURSOR_EDIT);
1005                 else if (az->type == AZONE_REGION) {
1006                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1007                                 WM_cursor_set(win, CURSOR_X_MOVE);
1008                         else
1009                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1010                 }
1011         }
1012         else {
1013                 ScrEdge *actedge = screen_find_active_scredge(win, screen, xy[0], xy[1]);
1014
1015                 if (actedge) {
1016                         if (scredge_is_horizontal(actedge))
1017                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1018                         else
1019                                 WM_cursor_set(win, CURSOR_X_MOVE);
1020                 }
1021                 else
1022                         WM_cursor_set(win, CURSOR_STD);
1023         }
1024 }
1025
1026
1027 /* called in wm_event_system.c. sets state vars in screen, cursors */
1028 /* event type is mouse move */
1029 void ED_screen_set_active_region(bContext *C, const int xy[2])
1030 {
1031         wmWindow *win = CTX_wm_window(C);
1032         bScreen *scr = WM_window_get_active_screen(win);
1033
1034         if (scr) {
1035                 ScrArea *sa = NULL;
1036                 ARegion *ar;
1037                 ARegion *old_ar = scr->active_region;
1038
1039                 ED_screen_areas_iter(win, scr, area_iter) {
1040                         if (xy[0] > area_iter->totrct.xmin && xy[0] < area_iter->totrct.xmax) {
1041                                 if (xy[1] > area_iter->totrct.ymin && xy[1] < area_iter->totrct.ymax) {
1042                                         if (is_in_area_actionzone(area_iter, xy) == NULL) {
1043                                                 sa = area_iter;
1044                                                 break;
1045                                         }
1046                                 }
1047                         }
1048                 }
1049                 if (sa) {
1050                         /* make overlap active when mouse over */
1051                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1052                                 if (BLI_rcti_isect_pt_v(&ar->winrct, xy)) {
1053                                         scr->active_region = ar;
1054                                         break;
1055                                 }
1056                         }
1057                 }
1058                 else
1059                         scr->active_region = NULL;
1060                 
1061                 /* check for redraw headers */
1062                 if (old_ar != scr->active_region) {
1063
1064                         ED_screen_areas_iter(win, scr, area_iter) {
1065                                 bool do_draw = false;
1066                                 
1067                                 for (ar = area_iter->regionbase.first; ar; ar = ar->next) {
1068                                         if (ar == old_ar || ar == scr->active_region) {
1069                                                 do_draw = true;
1070                                         }
1071                                 }
1072                                 
1073                                 if (do_draw) {
1074                                         for (ar = area_iter->regionbase.first; ar; ar = ar->next) {
1075                                                 if (ar->regiontype == RGN_TYPE_HEADER) {
1076                                                         ED_region_tag_redraw(ar);
1077                                                 }
1078                                         }
1079                                 }
1080                         }
1081                 }
1082                 
1083                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1084                 if (scr->active_region == NULL) {
1085                         screen_cursor_set(win, xy);
1086                 }
1087                 else {
1088                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1089                         if (old_ar != scr->active_region) {
1090                                 region_cursor_set(win, true);
1091
1092                                 /* this used to be a notifier, but needs to be done immediate
1093                                  * because it can undo setting the right button as active due
1094                                  * to delayed notifier handling */
1095                                 UI_screen_free_active_but(C, scr);
1096                         }
1097                         else
1098                                 region_cursor_set(win, false);
1099                 }
1100         }
1101 }
1102
1103 int ED_screen_area_active(const bContext *C)
1104 {
1105         wmWindow *win = CTX_wm_window(C);
1106         bScreen *sc = CTX_wm_screen(C);
1107         ScrArea *sa = CTX_wm_area(C);
1108
1109         if (win && sc && sa) {
1110                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1111                 ARegion *ar;
1112                 
1113                 if (az && az->type == AZONE_REGION)
1114                         return 1;
1115                 
1116                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1117                         if (ar == sc->active_region)
1118                                 return 1;
1119         }
1120         return 0;
1121 }
1122
1123 /**
1124  * Add an area and geometry (screen-edges and -vertices) for it to \a area_map,
1125  * with coordinates/dimensions matching \a rect.
1126  */
1127 static ScrArea *screen_area_create_with_geometry(
1128         ScrAreaMap *area_map, const rcti *rect,
1129         short headertype, short spacetype)
1130 {
1131         ScrVert *bottom_left  = screen_addvert_ex(area_map, rect->xmin, rect->ymin);
1132         ScrVert *top_left     = screen_addvert_ex(area_map, rect->xmin, rect->ymax);
1133         ScrVert *top_right    = screen_addvert_ex(area_map, rect->xmax, rect->ymax);
1134         ScrVert *bottom_right = screen_addvert_ex(area_map, rect->xmax, rect->ymin);
1135
1136         screen_addedge_ex(area_map, bottom_left, top_left);
1137         screen_addedge_ex(area_map, top_left, top_right);
1138         screen_addedge_ex(area_map, top_right, bottom_right);
1139         screen_addedge_ex(area_map, bottom_right, bottom_left);
1140
1141         return screen_addarea_ex(area_map, bottom_left, top_left, top_right, bottom_right, headertype, spacetype);
1142 }
1143
1144 void ED_screen_global_topbar_area_create(wmWindow *win, const bScreen *screen)
1145 {
1146         if (screen->temp == 0) {
1147                 const short size_y = 2.25 * HEADERY;
1148                 SpaceType *st;
1149                 SpaceLink *sl;
1150                 ScrArea *sa;
1151                 rcti rect;
1152
1153                 BLI_rcti_init(&rect, 0, WM_window_pixels_x(win) - 1, 0, WM_window_pixels_y(win) - 1);
1154                 rect.ymin = rect.ymax - size_y;
1155
1156                 sa = screen_area_create_with_geometry(&win->global_areas, &rect, HEADERTOP, SPACE_TOPBAR);
1157                 st = BKE_spacetype_from_id(SPACE_TOPBAR);
1158                 sl = st->new(sa, WM_window_get_active_scene(win));
1159                 sa->regionbase = sl->regionbase;
1160
1161                 /* Data specific to global areas. */
1162                 sa->global = MEM_callocN(sizeof(*sa->global), __func__);
1163                 sa->global->cur_fixed_height = size_y;
1164                 sa->global->size_max = size_y;
1165                 sa->global->size_min = HEADERY;
1166
1167                 BLI_addhead(&sa->spacedata, sl);
1168                 BLI_listbase_clear(&sl->regionbase);
1169         }
1170         /* Do not create more area types here! Function is called on file load (wm_window_ghostwindows_ensure). TODO */
1171 }
1172
1173 void ED_screen_global_areas_create(wmWindow *win)
1174 {
1175         const bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
1176         ED_screen_global_topbar_area_create(win, screen);
1177 }
1178
1179
1180 /* -------------------------------------------------------------------- */
1181 /* Screen changing */
1182
1183 static bScreen *screen_fullscreen_find_associated_normal_screen(const Main *bmain, bScreen *screen)
1184 {
1185         for (bScreen *screen_iter = bmain->screen.first; screen_iter; screen_iter = screen_iter->id.next) {
1186                 ScrArea *sa = screen_iter->areabase.first;
1187                 if (sa->full == screen) {
1188                         return screen_iter;
1189                 }
1190         }
1191
1192         return screen;
1193 }
1194
1195 /**
1196  * \return the screen to activate.
1197  * \warning The returned screen may not always equal \a screen_new!
1198  */
1199 bScreen *screen_change_prepare(bScreen *screen_old, bScreen *screen_new, Main *bmain, bContext *C, wmWindow *win)
1200 {
1201         /* validate screen, it's called with notifier reference */
1202         if (BLI_findindex(&bmain->screen, screen_new) == -1) {
1203                 return NULL;
1204         }
1205
1206         if (ELEM(screen_new->state, SCREENMAXIMIZED, SCREENFULL)) {
1207                 screen_new = screen_fullscreen_find_associated_normal_screen(bmain, screen_new);
1208         }
1209
1210         /* check for valid winid */
1211         if (!(screen_new->winid == 0 || screen_new->winid == win->winid)) {
1212                 return NULL;
1213         }
1214
1215         if (screen_old != screen_new) {
1216                 wmTimer *wt = screen_old->animtimer;
1217
1218                 /* remove handlers referencing areas in old screen */
1219                 for (ScrArea *sa = screen_old->areabase.first; sa; sa = sa->next) {
1220                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1221                 }
1222
1223                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1224                 screen_old->animtimer = NULL;
1225                 if (wt) {
1226                         WM_event_timer_sleep(CTX_wm_manager(C), win, wt, true);
1227                 }
1228                 ED_screen_exit(C, win, screen_old);
1229
1230                 /* Same scene, "transfer" playback to new screen. */
1231                 if (wt) {
1232                         screen_new->animtimer = wt;
1233                 }
1234
1235                 return screen_new;
1236         }
1237
1238         return NULL;
1239 }
1240
1241 void screen_change_update(bContext *C, wmWindow *win, bScreen *sc)
1242 {
1243         Scene *scene = WM_window_get_active_scene(win);
1244         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1245         WorkSpaceLayout *layout = BKE_workspace_layout_find(workspace, sc);
1246
1247         CTX_wm_window_set(C, win);  /* stores C->wm.screen... hrmf */
1248
1249         ED_screen_refresh(CTX_wm_manager(C), win);
1250
1251         BKE_screen_view3d_scene_sync(sc, scene); /* sync new screen with scene data */
1252         WM_event_add_notifier(C, NC_WINDOW, NULL);
1253         WM_event_add_notifier(C, NC_SCREEN | ND_LAYOUTSET, layout);
1254
1255         /* makes button hilites work */
1256         WM_event_add_mousemove(C);
1257 }
1258
1259
1260 /**
1261  * \brief Change the active screen.
1262  *
1263  * Operator call, WM + Window + screen already existed before
1264  *
1265  * \warning Do NOT call in area/region queues!
1266  * \returns if screen changing was successful.
1267  */
1268 bool ED_screen_change(bContext *C, bScreen *sc)
1269 {
1270         Main *bmain = CTX_data_main(C);
1271         wmWindow *win = CTX_wm_window(C);
1272         bScreen *screen_old = CTX_wm_screen(C);
1273         bScreen *screen_new = screen_change_prepare(screen_old, sc, bmain, C, win);
1274
1275         if (screen_new) {
1276                 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
1277                 WM_window_set_active_screen(win, workspace, sc);
1278                 screen_change_update(C, win, screen_new);
1279
1280                 return true;
1281         }
1282
1283         return false;
1284 }
1285
1286 static void screen_set_3dview_camera(Scene *scene, ViewLayer *view_layer, ScrArea *sa, View3D *v3d)
1287 {
1288         /* fix any cameras that are used in the 3d view but not in the scene */
1289         BKE_screen_view3d_sync(v3d, scene);
1290
1291         if (!v3d->camera || !BKE_view_layer_base_find(view_layer, v3d->camera)) {
1292                 v3d->camera = BKE_view_layer_camera_find(view_layer);
1293                 // XXX if (sc == curscreen) handle_view3d_lock();
1294                 if (!v3d->camera) {
1295                         ARegion *ar;
1296                         ListBase *regionbase;
1297                         
1298                         /* regionbase is in different place depending if space is active */
1299                         if (v3d == sa->spacedata.first)
1300                                 regionbase = &sa->regionbase;
1301                         else
1302                                 regionbase = &v3d->regionbase;
1303                                 
1304                         for (ar = regionbase->first; ar; ar = ar->next) {
1305                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1306                                         RegionView3D *rv3d = ar->regiondata;
1307                                         if (rv3d->persp == RV3D_CAMOB) {
1308                                                 rv3d->persp = RV3D_PERSP;
1309                                         }
1310                                 }
1311                         }
1312                 }
1313         }
1314 }
1315
1316 void ED_screen_update_after_scene_change(const bScreen *screen, Scene *scene_new, ViewLayer *view_layer)
1317 {
1318         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1319                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1320                         if (sl->spacetype == SPACE_VIEW3D) {
1321                                 View3D *v3d = (View3D *)sl;
1322                                 screen_set_3dview_camera(scene_new, view_layer, sa, v3d);
1323                         }
1324                 }
1325         }
1326 }
1327
1328 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1329 {
1330         wmWindow *win = CTX_wm_window(C);
1331         ScrArea *newsa = NULL;
1332
1333         if (!sa || sa->full == NULL) {
1334                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
1335         }
1336         
1337         if (!newsa) {
1338                 newsa = sa;
1339         }
1340
1341         BLI_assert(newsa);
1342
1343         if (sa && (sa->spacetype != type)) {
1344                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
1345         }
1346         else {
1347                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
1348         }
1349
1350         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
1351
1352         return newsa;
1353 }
1354
1355 /**
1356  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
1357  */
1358 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1359 {
1360         BLI_assert(sa->full);
1361
1362         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
1363                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
1364                 ED_area_prevspace(C, sa);
1365         }
1366         else {
1367                 ED_screen_restore_temp_type(C, sa);
1368         }
1369 }
1370
1371 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
1372 {
1373         /* incase nether functions below run */
1374         ED_area_tag_redraw(sa);
1375
1376         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1377                 ED_area_prevspace(C, sa);
1378                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1379         }
1380
1381         if (sa->full) {
1382                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1383         }
1384 }
1385
1386 /* restore a screen / area back to default operation, after temp fullscreen modes */
1387 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1388 {
1389         wmWindow *win = CTX_wm_window(C);
1390         SpaceLink *sl = sa->spacedata.first;
1391         bScreen *screen = CTX_wm_screen(C);
1392         short state = (screen ? screen->state : SCREENMAXIMIZED);
1393         
1394         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1395          * overlaid on top of an existing setup) then return to the previous space */
1396         
1397         if (sl->next) {
1398                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1399                         ED_screen_full_prevspace(C, sa);
1400                 }
1401                 else {
1402                         ED_screen_state_toggle(C, win, sa, state);
1403                 }
1404                 /* warning: 'sa' may be freed */
1405         }
1406         /* otherwise just tile the area again */
1407         else {
1408                 ED_screen_state_toggle(C, win, sa, state);
1409         }
1410 }
1411
1412 /**
1413  * this function toggles: if area is maximized/full then the parent will be restored
1414  *
1415  * \warning \a sa may be freed.
1416  */
1417 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1418 {
1419         wmWindowManager *wm = CTX_wm_manager(C);
1420         WorkSpace *workspace = WM_window_get_active_workspace(win);
1421         bScreen *sc, *oldscreen;
1422         ARegion *ar;
1423
1424         if (sa) {
1425                 /* ensure we don't have a button active anymore, can crash when
1426                  * switching screens with tooltip open because region and tooltip
1427                  * are no longer in the same screen */
1428                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1429                         UI_blocklist_free(C, &ar->uiblocks);
1430
1431                         if (ar->regiontimer) {
1432                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1433                                 ar->regiontimer = NULL;
1434                         }
1435                 }
1436
1437                 /* prevent hanging header prints */
1438                 ED_area_headerprint(sa, NULL);
1439         }
1440
1441         if (sa && sa->full) {
1442                 WorkSpaceLayout *layout_old = WM_window_get_active_layout(win);
1443                 /* restoring back to SCREENNORMAL */
1444                 sc = sa->full;       /* the old screen to restore */
1445                 oldscreen = WM_window_get_active_screen(win); /* the one disappearing */
1446
1447                 sc->state = SCREENNORMAL;
1448
1449                 /* find old area to restore from */
1450                 ScrArea *fullsa = NULL;
1451                 for (ScrArea *old = sc->areabase.first; old; old = old->next) {
1452                         /* area to restore from is always first */
1453                         if (old->full && !fullsa) {
1454                                 fullsa = old;
1455                         }
1456
1457                         /* clear full screen state */
1458                         old->full = NULL;
1459                 }
1460
1461                 sa->full = NULL;
1462
1463                 if (fullsa == NULL) {
1464                         if (G.debug & G_DEBUG)
1465                                 printf("%s: something wrong in areafullscreen\n", __func__);
1466                         return NULL;
1467                 }
1468
1469                 if (state == SCREENFULL) {
1470                         /* unhide global areas */
1471                         for (ScrArea *glob_area = win->global_areas.areabase.first; glob_area; glob_area = glob_area->next) {
1472                                 glob_area->global->flag &= ~GLOBAL_AREA_IS_HIDDEN;
1473                         }
1474                         /* restore the old side panels/header visibility */
1475                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1476                                 ar->flag = ar->flagfullscreen;
1477                         }
1478                 }
1479
1480                 ED_area_data_swap(fullsa, sa);
1481
1482                 /* animtimer back */
1483                 sc->animtimer = oldscreen->animtimer;
1484                 oldscreen->animtimer = NULL;
1485
1486                 ED_screen_change(C, sc);
1487
1488                 BKE_workspace_layout_remove(CTX_data_main(C), workspace, layout_old);
1489
1490                 /* After we've restored back to SCREENNORMAL, we have to wait with
1491                  * screen handling as it uses the area coords which aren't updated yet.
1492                  * Without doing so, the screen handling gets wrong area coords,
1493                  * which in worst case can lead to crashes (see T43139) */
1494                 sc->skip_handling = true;
1495         }
1496         else {
1497                 /* change from SCREENNORMAL to new state */
1498                 WorkSpaceLayout *layout_new;
1499                 ScrArea *newa;
1500                 char newname[MAX_ID_NAME - 2];
1501
1502                 BLI_assert(ELEM(state, SCREENMAXIMIZED, SCREENFULL));
1503
1504                 oldscreen = WM_window_get_active_screen(win);
1505
1506                 oldscreen->state = state;
1507                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1508
1509                 layout_new = ED_workspace_layout_add(workspace, win, newname);
1510
1511                 sc = BKE_workspace_layout_screen_get(layout_new);
1512                 sc->state = state;
1513                 sc->redraws_flag = oldscreen->redraws_flag;
1514                 sc->temp = oldscreen->temp;
1515
1516                 /* timer */
1517                 sc->animtimer = oldscreen->animtimer;
1518                 oldscreen->animtimer = NULL;
1519
1520                 /* use random area when we have no active one, e.g. when the
1521                  * mouse is outside of the window and we open a file browser */
1522                 if (!sa) {
1523                         sa = oldscreen->areabase.first;
1524                 }
1525
1526                 newa = (ScrArea *)sc->areabase.first;
1527
1528                 /* copy area */
1529                 ED_area_data_swap(newa, sa);
1530                 newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1531
1532                 if (state == SCREENFULL) {
1533                         /* temporarily hide global areas */
1534                         for (ScrArea *glob_area = win->global_areas.areabase.first; glob_area; glob_area = glob_area->next) {
1535                                 glob_area->global->flag |= GLOBAL_AREA_IS_HIDDEN;
1536                         }
1537                         /* temporarily hide the side panels/header */
1538                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1539                                 ar->flagfullscreen = ar->flag;
1540
1541                                 if (ELEM(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_HEADER, RGN_TYPE_TOOLS)) {
1542                                         ar->flag |= RGN_FLAG_HIDDEN;
1543                                 }
1544                         }
1545                 }
1546
1547                 sa->full = oldscreen;
1548                 newa->full = oldscreen;
1549
1550                 ED_screen_change(C, sc);
1551         }
1552
1553         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1554         CTX_wm_area_set(C, sc->areabase.first);
1555
1556         return sc->areabase.first;
1557 }
1558
1559 /* update frame rate info for viewport drawing */
1560 void ED_refresh_viewport_fps(bContext *C)
1561 {
1562         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1563         Scene *scene = CTX_data_scene(C);
1564         
1565         /* is anim playback running? */
1566         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1567                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1568                 
1569                 /* if there isn't any info, init it first */
1570                 if (fpsi == NULL)
1571                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1572                 
1573                 /* update the values */
1574                 fpsi->redrawtime = fpsi->lredrawtime;
1575                 fpsi->lredrawtime = animtimer->ltime;
1576         }
1577         else {
1578                 /* playback stopped or shouldn't be running */
1579                 if (scene->fps_info)
1580                         MEM_freeN(scene->fps_info);
1581                 scene->fps_info = NULL;
1582         }
1583 }
1584
1585 /* redraws: uses defines from stime->redraws 
1586  * enable: 1 - forward on, -1 - backwards on, 0 - off
1587  */
1588 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1589 {
1590         bScreen *screen = CTX_wm_screen(C);
1591         wmWindowManager *wm = CTX_wm_manager(C);
1592         wmWindow *win = CTX_wm_window(C);
1593         Scene *scene = CTX_data_scene(C);
1594         bScreen *stopscreen = ED_screen_animation_playing(wm);
1595         
1596         if (stopscreen) {
1597                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1598                 stopscreen->animtimer = NULL;
1599         }
1600         
1601         if (enable) {
1602                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1603                 
1604                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1605                 
1606                 sad->ar = CTX_wm_region(C);
1607                 /* if startframe is larger than current frame, we put currentframe on startframe.
1608                  * note: first frame then is not drawn! (ton) */
1609                 if (PRVRANGEON) {
1610                         if (scene->r.psfra > scene->r.cfra) {
1611                                 sad->sfra = scene->r.cfra;
1612                                 scene->r.cfra = scene->r.psfra;
1613                         }
1614                         else
1615                                 sad->sfra = scene->r.cfra;
1616                 }
1617                 else {
1618                         if (scene->r.sfra > scene->r.cfra) {
1619                                 sad->sfra = scene->r.cfra;
1620                                 scene->r.cfra = scene->r.sfra;
1621                         }
1622                         else
1623                                 sad->sfra = scene->r.cfra;
1624                 }
1625                 sad->redraws = redraws;
1626                 sad->refresh = refresh;
1627                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1628                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1629
1630                 ScrArea *sa = CTX_wm_area(C);
1631
1632                 char spacetype = -1;
1633
1634                 if (sa)
1635                         spacetype = sa->spacetype;
1636
1637                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA));
1638
1639                 screen->animtimer->customdata = sad;
1640                 
1641         }
1642
1643         /* notifier catched by top header, for button */
1644         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1645 }
1646
1647 /* helper for screen_animation_play() - only to be used for TimeLine */
1648 static ARegion *time_top_left_3dwindow(bScreen *screen)
1649 {
1650         ARegion *aret = NULL;
1651         ScrArea *sa;
1652         int min = 10000;
1653         
1654         for (sa = screen->areabase.first; sa; sa = sa->next) {
1655                 if (sa->spacetype == SPACE_VIEW3D) {
1656                         ARegion *ar;
1657                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1658                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1659                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1660                                                 aret = ar;
1661                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1662                                         }
1663                                 }
1664                         }
1665                 }
1666         }
1667
1668         return aret;
1669 }
1670
1671 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1672 {
1673         if (screen && screen->animtimer) {
1674                 wmTimer *wt = screen->animtimer;
1675                 ScreenAnimData *sad = wt->customdata;
1676                 
1677                 sad->redraws = redraws;
1678                 sad->refresh = refresh;
1679                 sad->ar = NULL;
1680                 if (redraws & TIME_REGION)
1681                         sad->ar = time_top_left_3dwindow(screen);
1682         }
1683 }
1684
1685 /* results in fully updated anim system */
1686 void ED_update_for_newframe(Main *bmain, Depsgraph *depsgraph)
1687 {
1688         Scene *scene = DEG_get_input_scene(depsgraph);
1689
1690 #ifdef DURIAN_CAMERA_SWITCH
1691         void *camera = BKE_scene_camera_switch_find(scene);
1692         if (camera && scene->camera != camera) {
1693                 bScreen *sc;
1694                 scene->camera = camera;
1695                 /* are there cameras in the views that are not in the scene? */
1696                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1697                         BKE_screen_view3d_scene_sync(sc, scene);
1698                 }
1699         }
1700 #endif
1701         
1702         ED_clip_update_frame(bmain, scene->r.cfra);
1703
1704         /* this function applies the changes too */
1705         BKE_scene_graph_update_for_newframe(depsgraph, bmain);
1706
1707         /* composite */
1708         if (scene->use_nodes && scene->nodetree)
1709                 ntreeCompositTagAnimated(scene->nodetree);
1710         
1711         /* update animated texture nodes */
1712         {
1713                 Tex *tex;
1714                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
1715                         if (tex->use_nodes && tex->nodetree) {
1716                                 ntreeTexTagAnimated(tex->nodetree);
1717                         }
1718                 }
1719         }
1720         
1721 }
1722
1723 /*
1724  * return true if any active area requires to see in 3D
1725  */
1726 bool ED_screen_stereo3d_required(const bScreen *screen, const Scene *scene)
1727 {
1728         ScrArea *sa;
1729         const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
1730
1731         for (sa = screen->areabase.first; sa; sa = sa->next) {
1732                 switch (sa->spacetype) {
1733                         case SPACE_VIEW3D:
1734                         {
1735                                 View3D *v3d;
1736
1737                                 if (!is_multiview)
1738                                         continue;
1739
1740                                 v3d = sa->spacedata.first;
1741                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
1742                                         ARegion *ar;
1743                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1744                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
1745                                                         RegionView3D *rv3d = ar->regiondata;
1746                                                         if (rv3d->persp == RV3D_CAMOB) {
1747                                                                 return true;
1748                                                         }
1749                                                 }
1750                                         }
1751                                 }
1752                                 break;
1753                         }
1754                         case SPACE_IMAGE:
1755                         {
1756                                 SpaceImage *sima;
1757
1758                                 /* images should always show in stereo, even if
1759                                  * the file doesn't have views enabled */
1760                                 sima = sa->spacedata.first;
1761                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
1762                                     (sima->iuser.flag & IMA_SHOW_STEREO))
1763                                 {
1764                                         return true;
1765                                 }
1766                                 break;
1767                         }
1768                         case SPACE_NODE:
1769                         {
1770                                 SpaceNode *snode;
1771
1772                                 if (!is_multiview)
1773                                         continue;
1774
1775                                 snode = sa->spacedata.first;
1776                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
1777                                         return true;
1778                                 }
1779                                 break;
1780                         }
1781                         case SPACE_SEQ:
1782                         {
1783                                 SpaceSeq *sseq;
1784
1785                                 if (!is_multiview)
1786                                         continue;
1787
1788                                 sseq = sa->spacedata.first;
1789                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
1790                                         return true;
1791                                 }
1792
1793                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1794                                         return true;
1795                                 }
1796
1797                                 break;
1798                         }
1799                 }
1800         }
1801
1802         return false;
1803 }
1804
1805 /**
1806  * Find the scene displayed in \a screen.
1807  * \note Assumes \a screen to be visible/active!
1808  */
1809
1810 Scene *ED_screen_scene_find_with_window(const bScreen *screen, const wmWindowManager *wm, struct wmWindow **r_window)
1811 {
1812         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1813                 if (WM_window_get_active_screen(win) == screen) {
1814                         if (r_window) {
1815                                 *r_window = win;
1816                         }
1817                         return WM_window_get_active_scene(win);
1818                 }
1819         }
1820
1821         BLI_assert(0);
1822         return NULL;
1823 }
1824
1825
1826 Scene *ED_screen_scene_find(const bScreen *screen, const wmWindowManager *wm)
1827 {
1828         return ED_screen_scene_find_with_window(screen, wm, NULL);
1829 }
1830
1831
1832 wmWindow *ED_screen_window_find(const bScreen *screen, const wmWindowManager *wm)
1833 {
1834         for (wmWindow *win = wm->windows.first; win; win = win->next) {
1835                 if (WM_window_get_active_screen(win) == screen) {
1836                         return win;
1837                 }
1838         }
1839         return NULL;
1840 }