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