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