Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / source / blender / editors / screen / screen_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/screen/screen_edit.c
26  *  \ingroup edscr
27  */
28
29
30 #include <string.h>
31 #include <math.h>
32
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_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(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(G.main, ID_SCR, name);
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                 if (sa1) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
586                 if (sa2) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
587                 return 0;
588         }
589         
590         if (dir == 0) {
591                 sa1->v1 = sa2->v1;
592                 sa1->v2 = sa2->v2;
593                 screen_addedge(scr, sa1->v2, sa1->v3);
594                 screen_addedge(scr, sa1->v1, sa1->v4);
595         }
596         else if (dir == 1) {
597                 sa1->v2 = sa2->v2;
598                 sa1->v3 = sa2->v3;
599                 screen_addedge(scr, sa1->v1, sa1->v2);
600                 screen_addedge(scr, sa1->v3, sa1->v4);
601         }
602         else if (dir == 2) {
603                 sa1->v3 = sa2->v3;
604                 sa1->v4 = sa2->v4;
605                 screen_addedge(scr, sa1->v2, sa1->v3);
606                 screen_addedge(scr, sa1->v1, sa1->v4);
607         }
608         else if (dir == 3) {
609                 sa1->v1 = sa2->v1;
610                 sa1->v4 = sa2->v4;
611                 screen_addedge(scr, sa1->v1, sa1->v2);
612                 screen_addedge(scr, sa1->v3, sa1->v4);
613         }
614         
615         screen_delarea(C, scr, sa2);
616         removedouble_scrverts(scr);
617         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
618         
619         return 1;
620 }
621
622 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
623 {
624         ScrEdge *se;
625         ScrVert *sv;
626         int oneselected;
627         char dir;
628         
629         /* select connected, only in the right direction */
630         /* 'dir' is the direction of EDGE */
631         
632         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
633         else dir = 'h';
634         
635         sv = sc->vertbase.first;
636         while (sv) {
637                 sv->flag = 0;
638                 sv = sv->next;
639         }
640         
641         edge->v1->flag = 1;
642         edge->v2->flag = 1;
643         
644         oneselected = 1;
645         while (oneselected) {
646                 se = sc->edgebase.first;
647                 oneselected = 0;
648                 while (se) {
649                         if (se->v1->flag + se->v2->flag == 1) {
650                                 if (dir == 'h') {
651                                         if (se->v1->vec.y == se->v2->vec.y) {
652                                                 se->v1->flag = se->v2->flag = 1;
653                                                 oneselected = 1;
654                                         }
655                                 }
656                                 if (dir == 'v') {
657                                         if (se->v1->vec.x == se->v2->vec.x) {
658                                                 se->v1->flag = se->v2->flag = 1;
659                                                 oneselected = 1;
660                                         }
661                                 }
662                         }
663                         se = se->next;
664                 }
665         }
666 }
667
668 /* test if screen vertices should be scaled */
669 static void screen_test_scale(bScreen *sc, int winsize_x, int winsize_y)
670 {
671         /* clamp Y size of header sized areas when expanding windows
672          * avoids annoying empty space around file menu */
673 #define USE_HEADER_SIZE_CLAMP
674
675         const int headery_init = ED_area_headersize();
676         ScrVert *sv = NULL;
677         ScrArea *sa;
678         int winsize_x_prev, winsize_y_prev;
679         float facx, facy, tempf, min[2], max[2];
680         
681         /* calculate size */
682         min[0] = min[1] = 20000.0f;
683         max[0] = max[1] = 0.0f;
684         
685         for (sv = sc->vertbase.first; sv; sv = sv->next) {
686                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
687                 minmax_v2v2_v2(min, max, fv);
688         }
689         
690         /* always make 0.0 left under */
691         for (sv = sc->vertbase.first; sv; sv = sv->next) {
692                 sv->vec.x -= min[0];
693                 sv->vec.y -= min[1];
694         }
695         
696         winsize_x_prev = (max[0] - min[0]) + 1;
697         winsize_y_prev = (max[1] - min[1]) + 1;
698
699
700 #ifdef USE_HEADER_SIZE_CLAMP
701 #define TEMP_BOTTOM 1
702 #define TEMP_TOP 2
703
704         /* if the window's Y axis grows, clamp header sized areas */
705         if (winsize_y_prev < winsize_y) {  /* growing? */
706                 const int headery_margin_max = headery_init + 4;
707                 for (sa = sc->areabase.first; sa; sa = sa->next) {
708                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
709                         sa->temp = 0;
710
711                         if (ar && !(ar->flag & RGN_FLAG_HIDDEN)) {
712                                 if (sa->v2->vec.y == winsize_y_prev - 1) {
713                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
714                                                 sa->temp = TEMP_TOP;
715                                         }
716                                 }
717                                 else if (sa->v1->vec.y == 0) {
718                                         if ((sa->v2->vec.y - sa->v1->vec.y) < headery_margin_max) {
719                                                 sa->temp = TEMP_BOTTOM;
720                                         }
721                                 }
722                         }
723                 }
724         }
725 #endif
726
727
728         if (winsize_x_prev != winsize_x || winsize_y_prev != winsize_y) {
729                 facx = ((float)winsize_x - 1) / ((float)winsize_x_prev - 1);
730                 facy = ((float)winsize_y - 1) / ((float)winsize_y_prev - 1);
731                 
732                 /* make sure it fits! */
733                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
734                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
735                          * need some way to store these as floats internally and re-apply from there. */
736                         tempf = ((float)sv->vec.x) * facx;
737                         sv->vec.x = (short)(tempf + 0.5f);
738                         //sv->vec.x += AREAGRID - 1;
739                         //sv->vec.x -=  (sv->vec.x % AREAGRID);
740
741                         CLAMP(sv->vec.x, 0, winsize_x - 1);
742                         
743                         tempf = ((float)sv->vec.y) * facy;
744                         sv->vec.y = (short)(tempf + 0.5f);
745                         //sv->vec.y += AREAGRID - 1;
746                         //sv->vec.y -=  (sv->vec.y % AREAGRID);
747
748                         CLAMP(sv->vec.y, 0, winsize_y - 1);
749                 }
750         }
751
752
753 #ifdef USE_HEADER_SIZE_CLAMP
754         if (winsize_y_prev < winsize_y) {  /* growing? */
755                 for (sa = sc->areabase.first; sa; sa = sa->next) {
756                         ScrEdge *se = NULL;
757
758                         if (sa->temp == 0)
759                                 continue;
760
761                         if (sa->v1 == sa->v2)
762                                 continue;
763
764                         /* adjust headery if verts are along the edge of window */
765                         if (sa->temp == TEMP_TOP) {
766                                 /* lower edge */
767                                 const int yval = sa->v2->vec.y - headery_init;
768                                 se = screen_findedge(sc, sa->v4, sa->v1);
769                                 select_connected_scredge(sc, se);
770                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
771                                         if (sv != sa->v2 && sv != sa->v3) {
772                                                 if (sv->flag) {
773                                                         sv->vec.y = yval;
774                                                 }
775                                         }
776                                 }
777                         }
778                         else {
779                                 /* upper edge */
780                                 const int yval = sa->v1->vec.y + headery_init;
781                                 se = screen_findedge(sc, sa->v2, sa->v3);
782                                 select_connected_scredge(sc, se);
783                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
784                                         if (sv != sa->v1 && sv != sa->v4) {
785                                                 if (sv->flag) {
786                                                         sv->vec.y = yval;
787                                                 }
788                                         }
789                                 }
790                         }
791                 }
792         }
793
794 #undef USE_HEADER_SIZE_CLAMP
795 #undef TEMP_BOTTOM
796 #undef TEMP_TOP
797 #endif
798
799
800         /* test for collapsed areas. This could happen in some blender version... */
801         /* ton: removed option now, it needs Context... */
802         
803         /* make each window at least ED_area_headersize() high */
804         for (sa = sc->areabase.first; sa; sa = sa->next) {
805                 int headery = headery_init;
806                 
807                 /* adjust headery if verts are along the edge of window */
808                 if (sa->v1->vec.y > 0)
809                         headery += U.pixelsize;
810                 if (sa->v2->vec.y < winsize_y - 1)
811                         headery += U.pixelsize;
812                 
813                 if (sa->v2->vec.y - sa->v1->vec.y + 1 < headery) {
814                         /* lower edge */
815                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
816                         if (se && sa->v1 != sa->v2) {
817                                 int yval;
818                                 
819                                 select_connected_scredge(sc, se);
820                                 
821                                 /* all selected vertices get the right offset */
822                                 yval = sa->v2->vec.y - headery + 1;
823                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
824                                         /* if is a collapsed area */
825                                         if (sv != sa->v2 && sv != sa->v3) {
826                                                 if (sv->flag) {
827                                                         sv->vec.y = yval;
828                                                 }
829                                         }
830                                 }
831                         }
832                 }
833         }
834         
835 }
836
837 /* *********************** DRAWING **************************************** */
838
839 /* draw vertical shape visualizing future joining (left as well
840  * right direction of future joining) */
841 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
842 {
843         vec2f points[10];
844         short i;
845         float w, h;
846         float width = sa->v3->vec.x - sa->v1->vec.x;
847         float height = sa->v3->vec.y - sa->v1->vec.y;
848
849         if (height < width) {
850                 h = height / 8;
851                 w = height / 4;
852         }
853         else {
854                 h = width / 8;
855                 w = width / 4;
856         }
857
858         points[0].x = sa->v1->vec.x;
859         points[0].y = sa->v1->vec.y + height / 2;
860
861         points[1].x = sa->v1->vec.x;
862         points[1].y = sa->v1->vec.y;
863
864         points[2].x = sa->v4->vec.x - w;
865         points[2].y = sa->v4->vec.y;
866
867         points[3].x = sa->v4->vec.x - w;
868         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
869
870         points[4].x = sa->v4->vec.x - 2 * w;
871         points[4].y = sa->v4->vec.y + height / 2;
872
873         points[5].x = sa->v4->vec.x - w;
874         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
875
876         points[6].x = sa->v3->vec.x - w;
877         points[6].y = sa->v3->vec.y;
878
879         points[7].x = sa->v2->vec.x;
880         points[7].y = sa->v2->vec.y;
881
882         points[8].x = sa->v4->vec.x;
883         points[8].y = sa->v4->vec.y + height / 2 - h;
884
885         points[9].x = sa->v4->vec.x;
886         points[9].y = sa->v4->vec.y + height / 2 + h;
887
888         if (dir == 'l') {
889                 /* when direction is left, then we flip direction of arrow */
890                 float cx = sa->v1->vec.x + width;
891                 for (i = 0; i < 10; i++) {
892                         points[i].x -= cx;
893                         points[i].x = -points[i].x;
894                         points[i].x += sa->v1->vec.x;
895                 }
896         }
897
898         glBegin(GL_POLYGON);
899         for (i = 0; i < 5; i++)
900                 glVertex2f(points[i].x, points[i].y);
901         glEnd();
902         glBegin(GL_POLYGON);
903         for (i = 4; i < 8; i++)
904                 glVertex2f(points[i].x, points[i].y);
905         glVertex2f(points[0].x, points[0].y);
906         glEnd();
907
908         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
909         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
910 }
911
912 /* draw vertical shape visualizing future joining (up/down direction) */
913 static void draw_vertical_join_shape(ScrArea *sa, char dir)
914 {
915         vec2f points[10];
916         short i;
917         float w, h;
918         float width = sa->v3->vec.x - sa->v1->vec.x;
919         float height = sa->v3->vec.y - sa->v1->vec.y;
920
921         if (height < width) {
922                 h = height / 4;
923                 w = height / 8;
924         }
925         else {
926                 h = width / 4;
927                 w = width / 8;
928         }
929
930         points[0].x = sa->v1->vec.x + width / 2;
931         points[0].y = sa->v3->vec.y;
932
933         points[1].x = sa->v2->vec.x;
934         points[1].y = sa->v2->vec.y;
935
936         points[2].x = sa->v1->vec.x;
937         points[2].y = sa->v1->vec.y + h;
938
939         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
940         points[3].y = sa->v1->vec.y + h;
941
942         points[4].x = sa->v1->vec.x + width / 2;
943         points[4].y = sa->v1->vec.y + 2 * h;
944
945         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
946         points[5].y = sa->v1->vec.y + h;
947
948         points[6].x = sa->v4->vec.x;
949         points[6].y = sa->v4->vec.y + h;
950         
951         points[7].x = sa->v3->vec.x;
952         points[7].y = sa->v3->vec.y;
953
954         points[8].x = sa->v1->vec.x + width / 2 - w;
955         points[8].y = sa->v1->vec.y;
956
957         points[9].x = sa->v1->vec.x + width / 2 + w;
958         points[9].y = sa->v1->vec.y;
959
960         if (dir == 'u') {
961                 /* when direction is up, then we flip direction of arrow */
962                 float cy = sa->v1->vec.y + height;
963                 for (i = 0; i < 10; i++) {
964                         points[i].y -= cy;
965                         points[i].y = -points[i].y;
966                         points[i].y += sa->v1->vec.y;
967                 }
968         }
969
970         glBegin(GL_POLYGON);
971         for (i = 0; i < 5; i++)
972                 glVertex2f(points[i].x, points[i].y);
973         glEnd();
974         glBegin(GL_POLYGON);
975         for (i = 4; i < 8; i++)
976                 glVertex2f(points[i].x, points[i].y);
977         glVertex2f(points[0].x, points[0].y);
978         glEnd();
979
980         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
981         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
982 }
983
984 /* draw join shape due to direction of joining */
985 static void draw_join_shape(ScrArea *sa, char dir)
986 {
987         if (dir == 'u' || dir == 'd')
988                 draw_vertical_join_shape(sa, dir);
989         else
990                 draw_horizontal_join_shape(sa, dir);
991 }
992
993 /* draw screen area darker with arrow (visualization of future joining) */
994 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
995 {
996         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
997         glColor4ub(0, 0, 0, 50);
998         draw_join_shape(sa, dir);
999 }
1000
1001 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
1002 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
1003 {
1004         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
1005         /* value 181 was hardly computed: 181~105 */
1006         glColor4ub(255, 255, 255, 50);
1007         /* draw_join_shape(sa, dir); */
1008         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
1009 }
1010
1011 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2)
1012 {
1013         /* right border area */
1014         if (x2 < sizex - 1) {
1015                 glVertex2s(x2, y1);
1016                 glVertex2s(x2, y2);
1017         }
1018
1019         /* left border area */
1020         if (x1 > 0) { /* otherwise it draws the emboss of window over */
1021                 glVertex2s(x1, y1);
1022                 glVertex2s(x1, y2);
1023         }
1024
1025         /* top border area */
1026         if (y2 < sizey - 1) {
1027                 glVertex2s(x1, y2);
1028                 glVertex2s(x2, y2);
1029         }
1030
1031         /* bottom border area */
1032         if (y1 > 0) {
1033                 glVertex2s(x1, y1);
1034                 glVertex2s(x2, y1);
1035         }
1036 }
1037
1038 /** screen edges drawing **/
1039 static void drawscredge_area(ScrArea *sa, int sizex, int sizey)
1040 {
1041         short x1 = sa->v1->vec.x;
1042         short y1 = sa->v1->vec.y;
1043         short x2 = sa->v3->vec.x;
1044         short y2 = sa->v3->vec.y;
1045         
1046         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2);
1047 }
1048
1049 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
1050
1051 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
1052 {
1053         bScreen *newsc;
1054         
1055         if (sc->state != SCREENNORMAL) return NULL;  /* XXX handle this case! */
1056         
1057         /* make new empty screen: */
1058         newsc = ED_screen_add(win, sc->scene, sc->id.name + 2);
1059         /* copy all data */
1060         screen_copy(newsc, sc);
1061
1062         return newsc;
1063 }
1064
1065 /* screen sets cursor based on swinid */
1066 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
1067 {
1068         for (ScrArea *sa = win->screen->areabase.first; sa; sa = sa->next) {
1069                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
1070                         if (ar->swinid == swinid) {
1071                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
1072                                         if (WM_manipulatormap_cursor_set(ar->manipulator_map, win)) {
1073                                                 return;
1074                                         }
1075                                         ED_region_cursor_set(win, sa, ar);
1076                                 }
1077                                 return;
1078                         }
1079                 }
1080         }
1081 }
1082
1083 void ED_screen_do_listen(bContext *C, wmNotifier *note)
1084 {
1085         wmWindow *win = CTX_wm_window(C);
1086         
1087         /* generic notes */
1088         switch (note->category) {
1089                 case NC_WM:
1090                         if (note->data == ND_FILEREAD)
1091                                 win->screen->do_draw = true;
1092                         break;
1093                 case NC_WINDOW:
1094                         win->screen->do_draw = true;
1095                         break;
1096                 case NC_SCREEN:
1097                         if (note->action == NA_EDITED)
1098                                 win->screen->do_draw = win->screen->do_refresh = true;
1099                         break;
1100                 case NC_SCENE:
1101                         if (note->data == ND_MODE)
1102                                 region_cursor_set(win, note->swinid, true);
1103                         break;
1104         }
1105 }
1106
1107 /* only for edge lines between areas, and the blended join arrows */
1108 void ED_screen_draw(wmWindow *win)
1109 {
1110         const int winsize_x = WM_window_pixels_x(win);
1111         const int winsize_y = WM_window_pixels_y(win);
1112
1113         ScrArea *sa;
1114         ScrArea *sa1 = NULL;
1115         ScrArea *sa2 = NULL;
1116         ScrArea *sa3 = NULL;
1117
1118         wmSubWindowSet(win, win->screen->mainwin);
1119         
1120         /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
1121         if (U.pixelsize > 1.0f) {
1122                 /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
1123                 glLineWidth((2.0f * U.pixelsize) - 1);
1124                 glColor3ub(0x50, 0x50, 0x50);
1125                 glBegin(GL_LINES);
1126                 for (sa = win->screen->areabase.first; sa; sa = sa->next)
1127                         drawscredge_area(sa, winsize_x, winsize_y);
1128                 glEnd();
1129         }
1130
1131         glLineWidth(1);
1132         glColor3ub(0, 0, 0);
1133         glBegin(GL_LINES);
1134         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1135                 drawscredge_area(sa, winsize_x, winsize_y);
1136
1137                 /* gather area split/join info */
1138                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1139                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1140                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1141         }
1142         glEnd();
1143
1144         /* blended join arrow */
1145         if (sa1 && sa2) {
1146                 int dir = area_getorientation(sa1, sa2);
1147                 int dira = -1;
1148                 if (dir != -1) {
1149                         switch (dir) {
1150                                 case 0: /* W */
1151                                         dir = 'r';
1152                                         dira = 'l';
1153                                         break;
1154                                 case 1: /* N */
1155                                         dir = 'd';
1156                                         dira = 'u';
1157                                         break;
1158                                 case 2: /* E */
1159                                         dir = 'l';
1160                                         dira = 'r';
1161                                         break;
1162                                 case 3: /* S */
1163                                         dir = 'u';
1164                                         dira = 'd';
1165                                         break;
1166                         }
1167                 }
1168                 glEnable(GL_BLEND);
1169                 scrarea_draw_shape_dark(sa2, dir);
1170                 scrarea_draw_shape_light(sa1, dira);
1171                 glDisable(GL_BLEND);
1172         }
1173         
1174         /* splitpoint */
1175         if (sa3) {
1176                 glEnable(GL_BLEND);
1177                 glBegin(GL_LINES);
1178                 glColor4ub(255, 255, 255, 100);
1179                 
1180                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1181                         glVertex2s(sa3->totrct.xmin, win->eventstate->y);
1182                         glVertex2s(sa3->totrct.xmax, win->eventstate->y);
1183                         glColor4ub(0, 0, 0, 100);
1184                         glVertex2s(sa3->totrct.xmin, win->eventstate->y + 1);
1185                         glVertex2s(sa3->totrct.xmax, win->eventstate->y + 1);
1186                 }
1187                 else {
1188                         glVertex2s(win->eventstate->x, sa3->totrct.ymin);
1189                         glVertex2s(win->eventstate->x, sa3->totrct.ymax);
1190                         glColor4ub(0, 0, 0, 100);
1191                         glVertex2s(win->eventstate->x + 1, sa3->totrct.ymin);
1192                         glVertex2s(win->eventstate->x + 1, sa3->totrct.ymax);
1193                 }
1194                 glEnd();
1195                 glDisable(GL_BLEND);
1196         }
1197         
1198         win->screen->do_draw = false;
1199 }
1200
1201 /* helper call for below, dpi changes headers */
1202 static void screen_refresh_headersizes(void)
1203 {
1204         const ListBase *lb = BKE_spacetypes_list();
1205         SpaceType *st;
1206         
1207         for (st = lb->first; st; st = st->next) {
1208                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1209                 if (art) art->prefsizey = ED_area_headersize();
1210         }
1211 }
1212
1213 /* make this screen usable */
1214 /* for file read and first use, for scaling window, area moves */
1215 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1216 {       
1217         /* exception for bg mode, we only need the screen context */
1218         if (!G.background) {
1219                 const int winsize_x = WM_window_pixels_x(win);
1220                 const int winsize_y = WM_window_pixels_y(win);
1221                 ScrArea *sa;
1222                 rcti winrct;
1223         
1224                 winrct.xmin = 0;
1225                 winrct.xmax = winsize_x - 1;
1226                 winrct.ymin = 0;
1227                 winrct.ymax = winsize_y - 1;
1228                 
1229                 /* header size depends on DPI, let's verify */
1230                 screen_refresh_headersizes();
1231                 
1232                 screen_test_scale(win->screen, winsize_x, winsize_y);
1233                 
1234                 if (win->screen->mainwin == 0) {
1235                         win->screen->mainwin = wm_subwindow_open(win, &winrct, false);
1236                 }
1237                 else {
1238                         wm_subwindow_position(win, win->screen->mainwin, &winrct, false);
1239                 }
1240                 
1241                 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1242                         /* set spacetype and region callbacks, calls init() */
1243                         /* sets subwindows for regions, adds handlers */
1244                         ED_area_initialize(wm, win, sa);
1245                 }
1246         
1247                 /* wake up animtimer */
1248                 if (win->screen->animtimer)
1249                         WM_event_timer_sleep(wm, win, win->screen->animtimer, false);
1250         }
1251
1252         if (G.debug & G_DEBUG_EVENTS) {
1253                 printf("%s: set screen\n", __func__);
1254         }
1255         win->screen->do_refresh = false;
1256
1257         win->screen->context = ed_screen_context;
1258 }
1259
1260 /* file read, set all screens, ... */
1261 void ED_screens_initialize(wmWindowManager *wm)
1262 {
1263         wmWindow *win;
1264         
1265         for (win = wm->windows.first; win; win = win->next) {
1266                 
1267                 if (win->screen == NULL)
1268                         win->screen = G.main->screen.first;
1269                 
1270                 ED_screen_refresh(wm, win);
1271         }
1272 }
1273
1274
1275 /* *********** exit calls are for closing running stuff ******** */
1276
1277 void ED_region_exit(bContext *C, ARegion *ar)
1278 {
1279         wmWindowManager *wm = CTX_wm_manager(C);
1280         wmWindow *win = CTX_wm_window(C);
1281         ARegion *prevar = CTX_wm_region(C);
1282
1283         if (ar->type && ar->type->exit)
1284                 ar->type->exit(wm, ar);
1285
1286         CTX_wm_region_set(C, ar);
1287
1288         WM_event_remove_handlers(C, &ar->handlers);
1289         WM_event_modal_handler_region_replace(win, ar, NULL);
1290         if (ar->swinid) {
1291                 wm_subwindow_close(win, ar->swinid);
1292                 ar->swinid = 0;
1293         }
1294
1295         if (ar->headerstr) {
1296                 MEM_freeN(ar->headerstr);
1297                 ar->headerstr = NULL;
1298         }
1299         
1300         if (ar->regiontimer) {
1301                 WM_event_remove_timer(wm, win, ar->regiontimer);
1302                 ar->regiontimer = NULL;
1303         }
1304
1305         CTX_wm_region_set(C, prevar);
1306 }
1307
1308 void ED_area_exit(bContext *C, ScrArea *sa)
1309 {
1310         wmWindowManager *wm = CTX_wm_manager(C);
1311         wmWindow *win = CTX_wm_window(C);
1312         ScrArea *prevsa = CTX_wm_area(C);
1313         ARegion *ar;
1314
1315         if (sa->type && sa->type->exit)
1316                 sa->type->exit(wm, sa);
1317
1318         CTX_wm_area_set(C, sa);
1319
1320         for (ar = sa->regionbase.first; ar; ar = ar->next)
1321                 ED_region_exit(C, ar);
1322
1323         WM_event_remove_handlers(C, &sa->handlers);
1324         WM_event_modal_handler_area_replace(win, sa, NULL);
1325
1326         CTX_wm_area_set(C, prevsa);
1327 }
1328
1329 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1330 {
1331         wmWindowManager *wm = CTX_wm_manager(C);
1332         wmWindow *prevwin = CTX_wm_window(C);
1333         ScrArea *sa;
1334         ARegion *ar;
1335
1336         CTX_wm_window_set(C, window);
1337         
1338         if (screen->animtimer)
1339                 WM_event_remove_timer(wm, window, screen->animtimer);
1340         screen->animtimer = NULL;
1341         screen->scrubbing = false;
1342
1343         if (screen->mainwin)
1344                 wm_subwindow_close(window, screen->mainwin);
1345         screen->mainwin = 0;
1346         screen->subwinactive = 0;
1347         
1348         for (ar = screen->regionbase.first; ar; ar = ar->next)
1349                 ED_region_exit(C, ar);
1350
1351         for (sa = screen->areabase.first; sa; sa = sa->next)
1352                 ED_area_exit(C, sa);
1353
1354         /* mark it available for use for other windows */
1355         screen->winid = 0;
1356         
1357         if (prevwin->screen->temp == 0) {
1358                 /* use previous window if possible */
1359                 CTX_wm_window_set(C, prevwin);
1360         }
1361         else {
1362                 /* none otherwise */
1363                 CTX_wm_window_set(C, NULL);
1364         }
1365         
1366 }
1367
1368 /* *********************************** */
1369
1370 /* case when on area-edge or in azones, or outside window */
1371 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1372 {
1373         const int winsize_x = WM_window_pixels_x(win);
1374         const int winsize_y = WM_window_pixels_y(win);
1375
1376         AZone *az = NULL;
1377         ScrArea *sa;
1378         
1379         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1380                 if ((az = is_in_area_actionzone(sa, &event->x)))
1381                         break;
1382         
1383         if (sa) {
1384                 if (az->type == AZONE_AREA)
1385                         WM_cursor_set(win, CURSOR_EDIT);
1386                 else if (az->type == AZONE_REGION) {
1387                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1388                                 WM_cursor_set(win, CURSOR_X_MOVE);
1389                         else
1390                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1391                 }
1392         }
1393         else {
1394                 ScrEdge *actedge = screen_find_active_scredge(win->screen, winsize_x, winsize_y, event->x, event->y);
1395                 
1396                 if (actedge) {
1397                         if (scredge_is_horizontal(actedge))
1398                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1399                         else
1400                                 WM_cursor_set(win, CURSOR_X_MOVE);
1401                 }
1402                 else
1403                         WM_cursor_set(win, CURSOR_STD);
1404         }
1405 }
1406
1407
1408 /* called in wm_event_system.c. sets state vars in screen, cursors */
1409 /* event type is mouse move */
1410 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1411 {
1412         wmWindow *win = CTX_wm_window(C);
1413         
1414         if (win->screen) {
1415                 bScreen *scr = win->screen;
1416                 ScrArea *sa;
1417                 ARegion *ar;
1418                 int oldswin = scr->subwinactive;
1419
1420                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1421                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1422                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1423                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1424                                                 break;
1425                 }
1426                 if (sa) {
1427                         /* make overlap active when mouse over */
1428                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1429                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1430                                         scr->subwinactive = ar->swinid;
1431                                         break;
1432                                 }
1433                         }
1434                 }
1435                 else
1436                         scr->subwinactive = scr->mainwin;
1437                 
1438                 /* check for redraw headers */
1439                 if (oldswin != scr->subwinactive) {
1440
1441                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1442                                 bool do_draw = false;
1443                                 
1444                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1445                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1446                                                 do_draw = true;
1447                                 
1448                                 if (do_draw) {
1449                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1450                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1451                                                         ED_region_tag_redraw(ar);
1452                                 }
1453                         }
1454                 }
1455                 
1456                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1457                 if (scr->subwinactive == scr->mainwin) {
1458                         screen_cursor_set(win, event);
1459                 }
1460                 else {
1461                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1462                         if (oldswin != scr->subwinactive) {
1463                                 region_cursor_set(win, scr->subwinactive, true);
1464
1465                                 /* this used to be a notifier, but needs to be done immediate
1466                                  * because it can undo setting the right button as active due
1467                                  * to delayed notifier handling */
1468                                 UI_screen_free_active_but(C, win->screen);
1469                         }
1470                         else
1471                                 region_cursor_set(win, scr->subwinactive, false);
1472                 }
1473         }
1474 }
1475
1476 int ED_screen_area_active(const bContext *C)
1477 {
1478         wmWindow *win = CTX_wm_window(C);
1479         bScreen *sc = CTX_wm_screen(C);
1480         ScrArea *sa = CTX_wm_area(C);
1481
1482         if (win && sc && sa) {
1483                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1484                 ARegion *ar;
1485                 
1486                 if (az && az->type == AZONE_REGION)
1487                         return 1;
1488                 
1489                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1490                         if (ar->swinid == sc->subwinactive)
1491                                 return 1;
1492         }
1493         return 0;
1494 }
1495
1496 /**
1497  * operator call, WM + Window + screen already existed before
1498  *
1499  * \warning Do NOT call in area/region queues!
1500  * \returns success.
1501  */
1502 bool ED_screen_set(bContext *C, bScreen *sc)
1503 {
1504         Main *bmain = CTX_data_main(C);
1505         wmWindowManager *wm = CTX_wm_manager(C);
1506         wmWindow *win = CTX_wm_window(C);
1507         bScreen *oldscreen = CTX_wm_screen(C);
1508         
1509         /* validate screen, it's called with notifier reference */
1510         if (BLI_findindex(&bmain->screen, sc) == -1) {
1511                 return true;
1512         }
1513
1514         if (ELEM(sc->state, SCREENMAXIMIZED, SCREENFULL)) {
1515                 /* find associated full */
1516                 bScreen *sc1;
1517                 for (sc1 = bmain->screen.first; sc1; sc1 = sc1->id.next) {
1518                         ScrArea *sa = sc1->areabase.first;
1519                         if (sa->full == sc) {
1520                                 sc = sc1;
1521                                 break;
1522                         }
1523                 }
1524         }
1525
1526         /* check for valid winid */
1527         if (sc->winid != 0 && sc->winid != win->winid) {
1528                 return false;
1529         }
1530         
1531         if (oldscreen != sc) {
1532                 wmTimer *wt = oldscreen->animtimer;
1533                 ScrArea *sa;
1534                 Scene *oldscene = oldscreen->scene;
1535
1536                 /* remove handlers referencing areas in old screen */
1537                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1538                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1539                 }
1540
1541                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1542                 oldscreen->animtimer = NULL;
1543                 if (wt) {
1544                         WM_event_timer_sleep(wm, win, wt, true);
1545                 }
1546
1547                 ED_screen_exit(C, win, oldscreen);
1548
1549                 /* Same scene, "transfer" playback to new screen. */
1550                 if (wt) {
1551                         if (oldscene == sc->scene) {
1552                                 sc->animtimer = wt;
1553                         }
1554                         /* Else, stop playback. */
1555                         else {
1556                                 oldscreen->animtimer = wt;
1557                                 ED_screen_animation_play(C, 0, 0);
1558                         }
1559                 }
1560
1561                 win->screen = sc;
1562                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1563                 
1564                 /* prevent multiwin errors */
1565                 sc->winid = win->winid;
1566                 
1567                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1568                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1569                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1570                 
1571                 /* makes button hilites work */
1572                 WM_event_add_mousemove(C);
1573
1574                 /* Needed to make sure all the derivedMeshes are
1575                  * up-to-date before viewport starts acquiring this.
1576                  *
1577                  * This is needed in cases when, for example, boolean
1578                  * modifier uses operant from invisible layer.
1579                  * Without this trick boolean wouldn't apply correct.
1580                  *
1581                  * Quite the same happens when setting screen's scene,
1582                  * so perhaps this is in fact correct thing to do.
1583                  */
1584                 if (oldscene != sc->scene) {
1585                         BKE_scene_set_background(bmain, sc->scene);
1586                 }
1587
1588                 /* Always do visible update since it's possible new screen will
1589                  * have different layers visible in 3D view-ports.
1590                  * This is possible because of view3d.lock_camera_and_layers option.
1591                  */
1592                 DAG_on_visible_update(bmain, false);
1593         }
1594
1595         return true;
1596 }
1597
1598 static bool ed_screen_used(wmWindowManager *wm, bScreen *sc)
1599 {
1600         wmWindow *win;
1601
1602         for (win = wm->windows.first; win; win = win->next) {
1603                 if (win->screen == sc) {
1604                         return true;
1605                 }
1606
1607                 if (ELEM(win->screen->state, SCREENMAXIMIZED, SCREENFULL)) {
1608                         ScrArea *sa = win->screen->areabase.first;
1609                         if (sa->full == sc) {
1610                                 return true;
1611                         }
1612                 }
1613         }
1614
1615         return false;
1616 }
1617
1618 /* only call outside of area/region loops */
1619 bool ED_screen_delete(bContext *C, bScreen *sc)
1620 {
1621         Main *bmain = CTX_data_main(C);
1622         wmWindowManager *wm = CTX_wm_manager(C);
1623         wmWindow *win = CTX_wm_window(C);
1624         bScreen *newsc;
1625         
1626         /* don't allow deleting temp fullscreens for now */
1627         if (ELEM(sc->state, SCREENMAXIMIZED, SCREENFULL)) {
1628                 return false;
1629         }
1630
1631         /* screen can only be in use by one window at a time, so as
1632          * long as we are able to find a screen that is unused, we
1633          * can safely assume ours is not in use anywhere an delete it */
1634
1635         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1636                 if (!ed_screen_used(wm, newsc) && !newsc->temp)
1637                         break;
1638         
1639         if (!newsc) {
1640                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1641                         if (!ed_screen_used(wm, newsc) && !newsc->temp)
1642                                 break;
1643         }
1644
1645         if (!newsc) {
1646                 return false;
1647         }
1648
1649         ED_screen_set(C, newsc);
1650
1651         if (win->screen != sc) {
1652                 BKE_libblock_free(bmain, sc);
1653                 return true;
1654         }
1655         else {
1656                 return false;
1657         }
1658 }
1659
1660 static void ed_screen_set_3dview_camera(Scene *scene, bScreen *sc, ScrArea *sa, View3D *v3d)
1661 {
1662         /* fix any cameras that are used in the 3d view but not in the scene */
1663         BKE_screen_view3d_sync(v3d, scene);
1664
1665         if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1666                 v3d->camera = BKE_scene_camera_find(sc->scene);
1667                 // XXX if (sc == curscreen) handle_view3d_lock();
1668                 if (!v3d->camera) {
1669                         ARegion *ar;
1670                         ListBase *regionbase;
1671                         
1672                         /* regionbase is in different place depending if space is active */
1673                         if (v3d == sa->spacedata.first)
1674                                 regionbase = &sa->regionbase;
1675                         else
1676                                 regionbase = &v3d->regionbase;
1677                                 
1678                         for (ar = regionbase->first; ar; ar = ar->next) {
1679                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1680                                         RegionView3D *rv3d = ar->regiondata;
1681                                         if (rv3d->persp == RV3D_CAMOB) {
1682                                                 rv3d->persp = RV3D_PERSP;
1683                                         }
1684                                 }
1685                         }
1686                 }
1687         }
1688 }
1689
1690 /* only call outside of area/region loops */
1691 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1692 {
1693         Main *bmain = CTX_data_main(C);
1694         bScreen *sc;
1695
1696         if (screen == NULL)
1697                 return;
1698         
1699         if (ed_screen_used(CTX_wm_manager(C), screen))
1700                 ED_object_editmode_exit(C, EM_FREEDATA | EM_DO_UNDO);
1701
1702         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1703                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1704                         
1705                         if (scene != sc->scene) {
1706                                 /* all areas endlocalview */
1707                                 // XXX  ScrArea *sa = sc->areabase.first;
1708                                 //      while (sa) {
1709                                 //              endlocalview(sa);
1710                                 //              sa = sa->next;
1711                                 //      }
1712                                 sc->scene = scene;
1713                         }
1714                         
1715                 }
1716         }
1717         
1718         //  copy_view3d_lock(0);        /* space.c */
1719         
1720         /* are there cameras in the views that are not in the scene? */
1721         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1722                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1723                         ScrArea *sa = sc->areabase.first;
1724                         while (sa) {
1725                                 SpaceLink *sl = sa->spacedata.first;
1726                                 while (sl) {
1727                                         if (sl->spacetype == SPACE_VIEW3D) {
1728                                                 View3D *v3d = (View3D *) sl;
1729                                                 ed_screen_set_3dview_camera(scene, sc, sa, v3d);
1730
1731                                         }
1732                                         sl = sl->next;
1733                                 }
1734                                 sa = sa->next;
1735                         }
1736                 }
1737         }
1738         
1739         CTX_data_scene_set(C, scene);
1740         BKE_scene_set_background(bmain, scene);
1741         DAG_on_visible_update(bmain, false);
1742         
1743         ED_render_engine_changed(bmain);
1744         ED_update_for_newframe(bmain, scene, 1);
1745         
1746         /* complete redraw */
1747         WM_event_add_notifier(C, NC_WINDOW, NULL);
1748         
1749 }
1750
1751 /**
1752  * \note Only call outside of area/region loops
1753  * \return true if successful
1754  */
1755 bool ED_screen_delete_scene(bContext *C, Scene *scene)
1756 {
1757         Main *bmain = CTX_data_main(C);
1758         Scene *newscene;
1759
1760         if (scene->id.prev)
1761                 newscene = scene->id.prev;
1762         else if (scene->id.next)
1763                 newscene = scene->id.next;
1764         else
1765                 return false;
1766
1767         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1768
1769         BKE_libblock_remap(bmain, scene, newscene, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE);
1770
1771         BKE_libblock_free(bmain, scene);
1772
1773         return true;
1774 }
1775
1776 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1777 {
1778         wmWindow *win = CTX_wm_window(C);
1779         bScreen *screen = CTX_wm_screen(C);
1780         ScrArea *newsa = NULL;
1781
1782         if (!sa || sa->full == NULL) {
1783                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
1784         }
1785         
1786         if (!newsa) {
1787                 if (sa->full && (screen->state == SCREENMAXIMIZED)) {
1788                         /* if this has been called from the temporary info header generated in
1789                          * temp fullscreen layouts, find the correct fullscreen area to change
1790                          * to create a new space inside */
1791                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1792                                 if (!(sa->flag & AREA_TEMP_INFO))
1793                                         break;
1794                         }
1795                 }
1796                 else {
1797                         newsa = sa;
1798                 }
1799         }
1800
1801         BLI_assert(newsa);
1802
1803         if (sa && (sa->spacetype != type)) {
1804                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
1805         }
1806         else {
1807                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
1808         }
1809
1810         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
1811
1812         return newsa;
1813 }
1814
1815 /**
1816  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
1817  */
1818 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1819 {
1820         BLI_assert(sa->full);
1821
1822         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
1823                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
1824                 ED_area_prevspace(C, sa);
1825         }
1826         else {
1827                 ED_screen_restore_temp_type(C, sa);
1828         }
1829 }
1830
1831 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
1832 {
1833         /* incase nether functions below run */
1834         ED_area_tag_redraw(sa);
1835
1836         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1837                 ED_area_prevspace(C, sa);
1838                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1839         }
1840
1841         if (sa->full) {
1842                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1843         }
1844 }
1845
1846 /* restore a screen / area back to default operation, after temp fullscreen modes */
1847 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1848 {
1849         wmWindow *win = CTX_wm_window(C);
1850         SpaceLink *sl = sa->spacedata.first;
1851         bScreen *screen = CTX_wm_screen(C);
1852         short state = (screen ? screen->state : SCREENMAXIMIZED);
1853         
1854         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1855          * overlaid on top of an existing setup) then return to the previous space */
1856         
1857         if (sl->next) {
1858                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1859                         ED_screen_full_prevspace(C, sa);
1860                 }
1861                 else {
1862                         ED_screen_state_toggle(C, win, sa, state);
1863                 }
1864                 /* warning: 'sa' may be freed */
1865         }
1866         /* otherwise just tile the area again */
1867         else {
1868                 ED_screen_state_toggle(C, win, sa, state);
1869         }
1870 }
1871
1872 /**
1873  * this function toggles: if area is maximized/full then the parent will be restored
1874  *
1875  * \warning \a sa may be freed.
1876  */
1877 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1878 {
1879         wmWindowManager *wm = CTX_wm_manager(C);
1880         bScreen *sc, *oldscreen;
1881         ARegion *ar;
1882
1883         if (sa) {
1884                 /* ensure we don't have a button active anymore, can crash when
1885                  * switching screens with tooltip open because region and tooltip
1886                  * are no longer in the same screen */
1887                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1888                         UI_blocklist_free(C, &ar->uiblocks);
1889
1890                         if (ar->regiontimer) {
1891                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1892                                 ar->regiontimer = NULL;
1893                         }
1894                 }
1895
1896                 /* prevent hanging header prints */
1897                 ED_area_headerprint(sa, NULL);
1898         }
1899
1900         if (sa && sa->full) {
1901                 /* restoring back to SCREENNORMAL */
1902                 ScrArea *old;
1903
1904                 sc = sa->full;       /* the old screen to restore */
1905                 oldscreen = win->screen; /* the one disappearing */
1906
1907                 sc->state = SCREENNORMAL;
1908
1909                 /* find old area */
1910                 for (old = sc->areabase.first; old; old = old->next)
1911                         if (old->full) break;
1912                 if (old == NULL) {
1913                         if (G.debug & G_DEBUG)
1914                                 printf("%s: something wrong in areafullscreen\n", __func__);
1915                         return NULL;
1916                 }
1917
1918                 if (state == SCREENFULL) {
1919                         /* restore the old side panels/header visibility */
1920                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1921                                 ar->flag = ar->flagfullscreen;
1922                         }
1923                 }
1924
1925                 ED_area_data_swap(old, sa);
1926                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1927                 old->full = NULL;
1928
1929                 /* animtimer back */
1930                 sc->animtimer = oldscreen->animtimer;
1931                 oldscreen->animtimer = NULL;
1932
1933                 ED_screen_set(C, sc);
1934
1935                 BKE_screen_free(oldscreen);
1936                 BKE_libblock_free(CTX_data_main(C), oldscreen);
1937
1938                 /* After we've restored back to SCREENNORMAL, we have to wait with
1939                  * screen handling as it uses the area coords which aren't updated yet.
1940                  * Without doing so, the screen handling gets wrong area coords,
1941                  * which in worst case can lead to crashes (see T43139) */
1942                 sc->skip_handling = true;
1943         }
1944         else {
1945                 /* change from SCREENNORMAL to new state */
1946                 ScrArea *newa;
1947                 char newname[MAX_ID_NAME - 2];
1948
1949                 oldscreen = win->screen;
1950
1951                 oldscreen->state = state;
1952                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1953                 sc = ED_screen_add(win, oldscreen->scene, newname);
1954                 sc->state = state;
1955                 sc->redraws_flag = oldscreen->redraws_flag;
1956                 sc->temp = oldscreen->temp;
1957
1958                 /* timer */
1959                 sc->animtimer = oldscreen->animtimer;
1960                 oldscreen->animtimer = NULL;
1961
1962                 /* use random area when we have no active one, e.g. when the
1963                  * mouse is outside of the window and we open a file browser */
1964                 if (!sa)
1965                         sa = oldscreen->areabase.first;
1966
1967                 if (state == SCREENMAXIMIZED) {
1968                         /* returns the top small area */
1969                         newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1970                         ED_area_newspace(C, newa, SPACE_INFO, false);
1971
1972                         /* copy area */
1973                         newa = newa->prev;
1974                         ED_area_data_swap(newa, sa);
1975                         sa->flag |= AREA_TEMP_INFO;
1976
1977                         sa->full = oldscreen;
1978                         newa->full = oldscreen;
1979                         newa->next->full = oldscreen; // XXX
1980                 }
1981                 else if (state == SCREENFULL) {
1982                         newa = (ScrArea *)sc->areabase.first;
1983
1984                         /* copy area */
1985                         ED_area_data_swap(newa, sa);
1986                         newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1987
1988                         /* temporarily hide the side panels/header */
1989                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1990                                 ar->flagfullscreen = ar->flag;
1991
1992                                 if (ELEM(ar->regiontype,
1993                                          RGN_TYPE_UI,
1994                                          RGN_TYPE_HEADER,
1995                                          RGN_TYPE_TOOLS))
1996                                 {
1997                                         ar->flag |= RGN_FLAG_HIDDEN;
1998                                 }
1999                         }
2000
2001                         sa->full = oldscreen;
2002                         newa->full = oldscreen;
2003                 }
2004                 else {
2005                         BLI_assert(false);
2006                 }
2007
2008                 ED_screen_set(C, sc);
2009         }
2010
2011         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
2012         CTX_wm_area_set(C, sc->areabase.first);
2013
2014         return sc->areabase.first;
2015 }
2016
2017 /* update frame rate info for viewport drawing */
2018 void ED_refresh_viewport_fps(bContext *C)
2019 {
2020         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
2021         Scene *scene = CTX_data_scene(C);
2022         
2023         /* is anim playback running? */
2024         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
2025                 ScreenFrameRateInfo *fpsi = scene->fps_info;
2026                 
2027                 /* if there isn't any info, init it first */
2028                 if (fpsi == NULL)
2029                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
2030                 
2031                 /* update the values */
2032                 fpsi->redrawtime = fpsi->lredrawtime;
2033                 fpsi->lredrawtime = animtimer->ltime;
2034         }
2035         else {
2036                 /* playback stopped or shouldn't be running */
2037                 if (scene->fps_info)
2038                         MEM_freeN(scene->fps_info);
2039                 scene->fps_info = NULL;
2040         }
2041 }
2042
2043 /* redraws: uses defines from stime->redraws 
2044  * enable: 1 - forward on, -1 - backwards on, 0 - off
2045  */
2046 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
2047 {
2048         bScreen *screen = CTX_wm_screen(C);
2049         wmWindowManager *wm = CTX_wm_manager(C);
2050         wmWindow *win = CTX_wm_window(C);
2051         Scene *scene = CTX_data_scene(C);
2052         bScreen *stopscreen = ED_screen_animation_playing(wm);
2053         
2054         if (stopscreen) {
2055                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
2056                 stopscreen->animtimer = NULL;
2057         }
2058         
2059         if (enable) {
2060                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
2061                 
2062                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
2063                 
2064                 sad->ar = CTX_wm_region(C);
2065                 /* if startframe is larger than current frame, we put currentframe on startframe.
2066                  * note: first frame then is not drawn! (ton) */
2067                 if (PRVRANGEON) {
2068                         if (scene->r.psfra > scene->r.cfra) {
2069                                 sad->sfra = scene->r.cfra;
2070                                 scene->r.cfra = scene->r.psfra;
2071                         }
2072                         else
2073                                 sad->sfra = scene->r.cfra;
2074                 }
2075                 else {
2076                         if (scene->r.sfra > scene->r.cfra) {
2077                                 sad->sfra = scene->r.cfra;
2078                                 scene->r.cfra = scene->r.sfra;
2079                         }
2080                         else
2081                                 sad->sfra = scene->r.cfra;
2082                 }
2083                 sad->redraws = redraws;
2084                 sad->refresh = refresh;
2085                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
2086                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
2087
2088                 ScrArea *sa = CTX_wm_area(C);
2089
2090                 char spacetype = -1;
2091
2092                 if (sa)
2093                         spacetype = sa->spacetype;
2094
2095                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA, SPACE_TIME));
2096
2097                 screen->animtimer->customdata = sad;
2098                 
2099         }
2100
2101         /* notifier catched by top header, for button */
2102         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
2103 }
2104
2105 /* helper for screen_animation_play() - only to be used for TimeLine */
2106 static ARegion *time_top_left_3dwindow(bScreen *screen)
2107 {
2108         ARegion *aret = NULL;
2109         ScrArea *sa;
2110         int min = 10000;
2111         
2112         for (sa = screen->areabase.first; sa; sa = sa->next) {
2113                 if (sa->spacetype == SPACE_VIEW3D) {
2114                         ARegion *ar;
2115                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2116                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2117                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
2118                                                 aret = ar;
2119                                                 min = ar->winrct.xmin - ar->winrct.ymin;
2120                                         }
2121                                 }
2122                         }
2123                 }
2124         }
2125
2126         return aret;
2127 }
2128
2129 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
2130 {
2131         if (screen && screen->animtimer) {
2132                 wmTimer *wt = screen->animtimer;
2133                 ScreenAnimData *sad = wt->customdata;
2134                 
2135                 sad->redraws = redraws;
2136                 sad->refresh = refresh;
2137                 sad->ar = NULL;
2138                 if (redraws & TIME_REGION)
2139                         sad->ar = time_top_left_3dwindow(screen);
2140         }
2141 }
2142
2143 /* results in fully updated anim system
2144  * screen can be NULL */
2145 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
2146 {
2147         wmWindowManager *wm = bmain->wm.first;
2148         wmWindow *window;
2149         int layers = 0;
2150
2151 #ifdef DURIAN_CAMERA_SWITCH
2152         void *camera = BKE_scene_camera_switch_find(scene);
2153         if (camera && scene->camera != camera) {
2154                 bScreen *sc;
2155                 scene->camera = camera;
2156                 /* are there cameras in the views that are not in the scene? */
2157                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
2158                         BKE_screen_view3d_scene_sync(sc);
2159                 }
2160         }
2161 #endif
2162         
2163         ED_clip_update_frame(bmain, scene->r.cfra);
2164
2165         /* get layers from all windows */
2166         for (window = wm->windows.first; window; window = window->next)
2167                 layers |= BKE_screen_visible_layers(window->screen, scene);
2168
2169         /* this function applies the changes too */
2170         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, layers);
2171
2172         /* composite */
2173         if (scene->use_nodes && scene->nodetree)
2174                 ntreeCompositTagAnimated(scene->nodetree);
2175         
2176         /* update animated texture nodes */
2177         {
2178                 Tex *tex;
2179                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
2180                         if (tex->use_nodes && tex->nodetree) {
2181                                 ntreeTexTagAnimated(tex->nodetree);
2182                         }
2183         }
2184         
2185 }
2186
2187 /*
2188  * return true if any active area requires to see in 3D
2189  */
2190 bool ED_screen_stereo3d_required(bScreen *screen)
2191 {
2192         ScrArea *sa;
2193         Scene *sce = screen->scene;
2194         const bool is_multiview = (sce->r.scemode & R_MULTIVIEW) != 0;
2195
2196         for (sa = screen->areabase.first; sa; sa = sa->next) {
2197                 switch (sa->spacetype) {
2198                         case SPACE_VIEW3D:
2199                         {
2200                                 View3D *v3d;
2201
2202                                 if (!is_multiview)
2203                                         continue;
2204
2205                                 v3d = sa->spacedata.first;
2206                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
2207                                         ARegion *ar;
2208                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2209                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
2210                                                         RegionView3D *rv3d = ar->regiondata;
2211                                                         if (rv3d->persp == RV3D_CAMOB) {
2212                                                                 return true;
2213                                                         }
2214                                                 }
2215                                         }
2216                                 }
2217                                 break;
2218                         }
2219                         case SPACE_IMAGE:
2220                         {
2221                                 SpaceImage *sima;
2222
2223                                 /* images should always show in stereo, even if
2224                                  * the file doesn't have views enabled */
2225                                 sima = sa->spacedata.first;
2226                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
2227                                     (sima->iuser.flag & IMA_SHOW_STEREO))
2228                                 {
2229                                         return true;
2230                                 }
2231                                 break;
2232                         }
2233                         case SPACE_NODE:
2234                         {
2235                                 SpaceNode *snode;
2236
2237                                 if (!is_multiview)
2238                                         continue;
2239
2240                                 snode = sa->spacedata.first;
2241                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
2242                                         return true;
2243                                 }
2244                                 break;
2245                         }
2246                         case SPACE_SEQ:
2247                         {
2248                                 SpaceSeq *sseq;
2249
2250                                 if (!is_multiview)
2251                                         continue;
2252
2253                                 sseq = sa->spacedata.first;
2254                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
2255                                         return true;
2256                                 }
2257
2258                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
2259                                         return true;
2260                                 }
2261
2262                                 break;
2263                         }
2264                 }
2265         }
2266
2267         return false;
2268 }