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