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