Fix T51889: new file or load factory settings results in broken UI.
[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                                 if (se != NULL) {
770                                         select_connected_scredge(sc, se);
771                                 }
772                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
773                                         if (sv != sa->v2 && sv != sa->v3) {
774                                                 if (sv->flag) {
775                                                         sv->vec.y = yval;
776                                                 }
777                                         }
778                                 }
779                         }
780                         else {
781                                 /* upper edge */
782                                 const int yval = sa->v1->vec.y + headery_init;
783                                 se = screen_findedge(sc, sa->v2, sa->v3);
784                                 if (se != NULL) {
785                                         select_connected_scredge(sc, se);
786                                 }
787                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
788                                         if (sv != sa->v1 && sv != sa->v4) {
789                                                 if (sv->flag) {
790                                                         sv->vec.y = yval;
791                                                 }
792                                         }
793                                 }
794                         }
795                 }
796         }
797
798 #undef USE_HEADER_SIZE_CLAMP
799 #undef TEMP_BOTTOM
800 #undef TEMP_TOP
801 #endif
802
803
804         /* test for collapsed areas. This could happen in some blender version... */
805         /* ton: removed option now, it needs Context... */
806         
807         /* make each window at least ED_area_headersize() high */
808         for (sa = sc->areabase.first; sa; sa = sa->next) {
809                 int headery = headery_init;
810                 
811                 /* adjust headery if verts are along the edge of window */
812                 if (sa->v1->vec.y > 0)
813                         headery += U.pixelsize;
814                 if (sa->v2->vec.y < winsize_y - 1)
815                         headery += U.pixelsize;
816                 
817                 if (sa->v2->vec.y - sa->v1->vec.y + 1 < headery) {
818                         /* lower edge */
819                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
820                         if (se && sa->v1 != sa->v2) {
821                                 int yval;
822                                 
823                                 select_connected_scredge(sc, se);
824                                 
825                                 /* all selected vertices get the right offset */
826                                 yval = sa->v2->vec.y - headery + 1;
827                                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
828                                         /* if is a collapsed area */
829                                         if (sv != sa->v2 && sv != sa->v3) {
830                                                 if (sv->flag) {
831                                                         sv->vec.y = yval;
832                                                 }
833                                         }
834                                 }
835                         }
836                 }
837         }
838         
839 }
840
841 /* *********************** DRAWING **************************************** */
842
843 /* draw vertical shape visualizing future joining (left as well
844  * right direction of future joining) */
845 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
846 {
847         vec2f points[10];
848         short i;
849         float w, h;
850         float width = sa->v3->vec.x - sa->v1->vec.x;
851         float height = sa->v3->vec.y - sa->v1->vec.y;
852
853         if (height < width) {
854                 h = height / 8;
855                 w = height / 4;
856         }
857         else {
858                 h = width / 8;
859                 w = width / 4;
860         }
861
862         points[0].x = sa->v1->vec.x;
863         points[0].y = sa->v1->vec.y + height / 2;
864
865         points[1].x = sa->v1->vec.x;
866         points[1].y = sa->v1->vec.y;
867
868         points[2].x = sa->v4->vec.x - w;
869         points[2].y = sa->v4->vec.y;
870
871         points[3].x = sa->v4->vec.x - w;
872         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
873
874         points[4].x = sa->v4->vec.x - 2 * w;
875         points[4].y = sa->v4->vec.y + height / 2;
876
877         points[5].x = sa->v4->vec.x - w;
878         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
879
880         points[6].x = sa->v3->vec.x - w;
881         points[6].y = sa->v3->vec.y;
882
883         points[7].x = sa->v2->vec.x;
884         points[7].y = sa->v2->vec.y;
885
886         points[8].x = sa->v4->vec.x;
887         points[8].y = sa->v4->vec.y + height / 2 - h;
888
889         points[9].x = sa->v4->vec.x;
890         points[9].y = sa->v4->vec.y + height / 2 + h;
891
892         if (dir == 'l') {
893                 /* when direction is left, then we flip direction of arrow */
894                 float cx = sa->v1->vec.x + width;
895                 for (i = 0; i < 10; i++) {
896                         points[i].x -= cx;
897                         points[i].x = -points[i].x;
898                         points[i].x += sa->v1->vec.x;
899                 }
900         }
901
902         glBegin(GL_POLYGON);
903         for (i = 0; i < 5; i++)
904                 glVertex2f(points[i].x, points[i].y);
905         glEnd();
906         glBegin(GL_POLYGON);
907         for (i = 4; i < 8; i++)
908                 glVertex2f(points[i].x, points[i].y);
909         glVertex2f(points[0].x, points[0].y);
910         glEnd();
911
912         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
913         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
914 }
915
916 /* draw vertical shape visualizing future joining (up/down direction) */
917 static void draw_vertical_join_shape(ScrArea *sa, char dir)
918 {
919         vec2f points[10];
920         short i;
921         float w, h;
922         float width = sa->v3->vec.x - sa->v1->vec.x;
923         float height = sa->v3->vec.y - sa->v1->vec.y;
924
925         if (height < width) {
926                 h = height / 4;
927                 w = height / 8;
928         }
929         else {
930                 h = width / 4;
931                 w = width / 8;
932         }
933
934         points[0].x = sa->v1->vec.x + width / 2;
935         points[0].y = sa->v3->vec.y;
936
937         points[1].x = sa->v2->vec.x;
938         points[1].y = sa->v2->vec.y;
939
940         points[2].x = sa->v1->vec.x;
941         points[2].y = sa->v1->vec.y + h;
942
943         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
944         points[3].y = sa->v1->vec.y + h;
945
946         points[4].x = sa->v1->vec.x + width / 2;
947         points[4].y = sa->v1->vec.y + 2 * h;
948
949         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
950         points[5].y = sa->v1->vec.y + h;
951
952         points[6].x = sa->v4->vec.x;
953         points[6].y = sa->v4->vec.y + h;
954         
955         points[7].x = sa->v3->vec.x;
956         points[7].y = sa->v3->vec.y;
957
958         points[8].x = sa->v1->vec.x + width / 2 - w;
959         points[8].y = sa->v1->vec.y;
960
961         points[9].x = sa->v1->vec.x + width / 2 + w;
962         points[9].y = sa->v1->vec.y;
963
964         if (dir == 'u') {
965                 /* when direction is up, then we flip direction of arrow */
966                 float cy = sa->v1->vec.y + height;
967                 for (i = 0; i < 10; i++) {
968                         points[i].y -= cy;
969                         points[i].y = -points[i].y;
970                         points[i].y += sa->v1->vec.y;
971                 }
972         }
973
974         glBegin(GL_POLYGON);
975         for (i = 0; i < 5; i++)
976                 glVertex2f(points[i].x, points[i].y);
977         glEnd();
978         glBegin(GL_POLYGON);
979         for (i = 4; i < 8; i++)
980                 glVertex2f(points[i].x, points[i].y);
981         glVertex2f(points[0].x, points[0].y);
982         glEnd();
983
984         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
985         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
986 }
987
988 /* draw join shape due to direction of joining */
989 static void draw_join_shape(ScrArea *sa, char dir)
990 {
991         if (dir == 'u' || dir == 'd')
992                 draw_vertical_join_shape(sa, dir);
993         else
994                 draw_horizontal_join_shape(sa, dir);
995 }
996
997 /* draw screen area darker with arrow (visualization of future joining) */
998 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
999 {
1000         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1001         glColor4ub(0, 0, 0, 50);
1002         draw_join_shape(sa, dir);
1003 }
1004
1005 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
1006 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
1007 {
1008         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
1009         /* value 181 was hardly computed: 181~105 */
1010         glColor4ub(255, 255, 255, 50);
1011         /* draw_join_shape(sa, dir); */
1012         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
1013 }
1014
1015 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2)
1016 {
1017         /* right border area */
1018         if (x2 < sizex - 1) {
1019                 glVertex2s(x2, y1);
1020                 glVertex2s(x2, y2);
1021         }
1022
1023         /* left border area */
1024         if (x1 > 0) { /* otherwise it draws the emboss of window over */
1025                 glVertex2s(x1, y1);
1026                 glVertex2s(x1, y2);
1027         }
1028
1029         /* top border area */
1030         if (y2 < sizey - 1) {
1031                 glVertex2s(x1, y2);
1032                 glVertex2s(x2, y2);
1033         }
1034
1035         /* bottom border area */
1036         if (y1 > 0) {
1037                 glVertex2s(x1, y1);
1038                 glVertex2s(x2, y1);
1039         }
1040 }
1041
1042 /** screen edges drawing **/
1043 static void drawscredge_area(ScrArea *sa, int sizex, int sizey)
1044 {
1045         short x1 = sa->v1->vec.x;
1046         short y1 = sa->v1->vec.y;
1047         short x2 = sa->v3->vec.x;
1048         short y2 = sa->v3->vec.y;
1049         
1050         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2);
1051 }
1052
1053 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
1054
1055 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
1056 {
1057         bScreen *newsc;
1058         
1059         if (sc->state != SCREENNORMAL) return NULL;  /* XXX handle this case! */
1060         
1061         /* make new empty screen: */
1062         newsc = ED_screen_add(win, sc->scene, sc->id.name + 2);
1063         /* copy all data */
1064         screen_copy(newsc, sc);
1065
1066         return newsc;
1067 }
1068
1069 /* screen sets cursor based on swinid */
1070 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
1071 {
1072         for (ScrArea *sa = win->screen->areabase.first; sa; sa = sa->next) {
1073                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
1074                         if (ar->swinid == swinid) {
1075                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
1076                                         ED_region_cursor_set(win, sa, ar);
1077                                 }
1078                                 return;
1079                         }
1080                 }
1081         }
1082 }
1083
1084 void ED_screen_do_listen(bContext *C, wmNotifier *note)
1085 {
1086         wmWindow *win = CTX_wm_window(C);
1087         
1088         /* generic notes */
1089         switch (note->category) {
1090                 case NC_WM:
1091                         if (note->data == ND_FILEREAD)
1092                                 win->screen->do_draw = true;
1093                         break;
1094                 case NC_WINDOW:
1095                         win->screen->do_draw = true;
1096                         break;
1097                 case NC_SCREEN:
1098                         if (note->action == NA_EDITED)
1099                                 win->screen->do_draw = win->screen->do_refresh = true;
1100                         break;
1101                 case NC_SCENE:
1102                         if (note->data == ND_MODE)
1103                                 region_cursor_set(win, note->swinid, true);
1104                         break;
1105         }
1106 }
1107
1108 /* only for edge lines between areas, and the blended join arrows */
1109 void ED_screen_draw(wmWindow *win)
1110 {
1111         const int winsize_x = WM_window_pixels_x(win);
1112         const int winsize_y = WM_window_pixels_y(win);
1113
1114         ScrArea *sa;
1115         ScrArea *sa1 = NULL;
1116         ScrArea *sa2 = NULL;
1117         ScrArea *sa3 = NULL;
1118
1119         wmSubWindowSet(win, win->screen->mainwin);
1120         
1121         /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
1122         if (U.pixelsize > 1.0f) {
1123                 /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
1124                 glLineWidth((2.0f * U.pixelsize) - 1);
1125                 glColor3ub(0x50, 0x50, 0x50);
1126                 glBegin(GL_LINES);
1127                 for (sa = win->screen->areabase.first; sa; sa = sa->next)
1128                         drawscredge_area(sa, winsize_x, winsize_y);
1129                 glEnd();
1130         }
1131
1132         glLineWidth(1);
1133         glColor3ub(0, 0, 0);
1134         glBegin(GL_LINES);
1135         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1136                 drawscredge_area(sa, winsize_x, winsize_y);
1137
1138                 /* gather area split/join info */
1139                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1140                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1141                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1142         }
1143         glEnd();
1144
1145         /* blended join arrow */
1146         if (sa1 && sa2) {
1147                 int dir = area_getorientation(sa1, sa2);
1148                 int dira = -1;
1149                 if (dir != -1) {
1150                         switch (dir) {
1151                                 case 0: /* W */
1152                                         dir = 'r';
1153                                         dira = 'l';
1154                                         break;
1155                                 case 1: /* N */
1156                                         dir = 'd';
1157                                         dira = 'u';
1158                                         break;
1159                                 case 2: /* E */
1160                                         dir = 'l';
1161                                         dira = 'r';
1162                                         break;
1163                                 case 3: /* S */
1164                                         dir = 'u';
1165                                         dira = 'd';
1166                                         break;
1167                         }
1168                 }
1169                 glEnable(GL_BLEND);
1170                 scrarea_draw_shape_dark(sa2, dir);
1171                 scrarea_draw_shape_light(sa1, dira);
1172                 glDisable(GL_BLEND);
1173         }
1174         
1175         /* splitpoint */
1176         if (sa3) {
1177                 glEnable(GL_BLEND);
1178                 glBegin(GL_LINES);
1179                 glColor4ub(255, 255, 255, 100);
1180                 
1181                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1182                         glVertex2s(sa3->totrct.xmin, win->eventstate->y);
1183                         glVertex2s(sa3->totrct.xmax, win->eventstate->y);
1184                         glColor4ub(0, 0, 0, 100);
1185                         glVertex2s(sa3->totrct.xmin, win->eventstate->y + 1);
1186                         glVertex2s(sa3->totrct.xmax, win->eventstate->y + 1);
1187                 }
1188                 else {
1189                         glVertex2s(win->eventstate->x, sa3->totrct.ymin);
1190                         glVertex2s(win->eventstate->x, sa3->totrct.ymax);
1191                         glColor4ub(0, 0, 0, 100);
1192                         glVertex2s(win->eventstate->x + 1, sa3->totrct.ymin);
1193                         glVertex2s(win->eventstate->x + 1, sa3->totrct.ymax);
1194                 }
1195                 glEnd();
1196                 glDisable(GL_BLEND);
1197         }
1198         
1199         win->screen->do_draw = false;
1200 }
1201
1202 /* helper call for below, dpi changes headers */
1203 static void screen_refresh_headersizes(void)
1204 {
1205         const ListBase *lb = BKE_spacetypes_list();
1206         SpaceType *st;
1207         
1208         for (st = lb->first; st; st = st->next) {
1209                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1210                 if (art) art->prefsizey = ED_area_headersize();
1211         }
1212 }
1213
1214 /* make this screen usable */
1215 /* for file read and first use, for scaling window, area moves */
1216 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1217 {       
1218         /* exception for bg mode, we only need the screen context */
1219         if (!G.background) {
1220                 const int winsize_x = WM_window_pixels_x(win);
1221                 const int winsize_y = WM_window_pixels_y(win);
1222                 ScrArea *sa;
1223                 rcti winrct;
1224         
1225                 winrct.xmin = 0;
1226                 winrct.xmax = winsize_x - 1;
1227                 winrct.ymin = 0;
1228                 winrct.ymax = winsize_y - 1;
1229                 
1230                 /* header size depends on DPI, let's verify */
1231                 WM_window_set_dpi(win);
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_libblock_remap(bmain, scene, newscene, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE);
1765
1766         BKE_libblock_free_us(bmain, scene);
1767
1768         return true;
1769 }
1770
1771 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1772 {
1773         wmWindow *win = CTX_wm_window(C);
1774         bScreen *screen = CTX_wm_screen(C);
1775         ScrArea *newsa = NULL;
1776
1777         if (!sa || sa->full == NULL) {
1778                 newsa = ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
1779         }
1780         
1781         if (!newsa) {
1782                 if (sa->full && (screen->state == SCREENMAXIMIZED)) {
1783                         /* if this has been called from the temporary info header generated in
1784                          * temp fullscreen layouts, find the correct fullscreen area to change
1785                          * to create a new space inside */
1786                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1787                                 if (!(sa->flag & AREA_TEMP_INFO))
1788                                         break;
1789                         }
1790                 }
1791                 else {
1792                         newsa = sa;
1793                 }
1794         }
1795
1796         BLI_assert(newsa);
1797
1798         if (sa && (sa->spacetype != type)) {
1799                 newsa->flag |= AREA_FLAG_TEMP_TYPE;
1800         }
1801         else {
1802                 newsa->flag &= ~AREA_FLAG_TEMP_TYPE;
1803         }
1804
1805         ED_area_newspace(C, newsa, type, (newsa->flag & AREA_FLAG_TEMP_TYPE));
1806
1807         return newsa;
1808 }
1809
1810 /**
1811  * \a was_prev_temp for the case previous space was a temporary fullscreen as well
1812  */
1813 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1814 {
1815         BLI_assert(sa->full);
1816
1817         if (sa->flag & AREA_FLAG_STACKED_FULLSCREEN) {
1818                 /* stacked fullscreen -> only go back to previous screen and don't toggle out of fullscreen */
1819                 ED_area_prevspace(C, sa);
1820         }
1821         else {
1822                 ED_screen_restore_temp_type(C, sa);
1823         }
1824 }
1825
1826 void ED_screen_restore_temp_type(bContext *C, ScrArea *sa)
1827 {
1828         /* incase nether functions below run */
1829         ED_area_tag_redraw(sa);
1830
1831         if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1832                 ED_area_prevspace(C, sa);
1833                 sa->flag &= ~AREA_FLAG_TEMP_TYPE;
1834         }
1835
1836         if (sa->full) {
1837                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
1838         }
1839 }
1840
1841 /* restore a screen / area back to default operation, after temp fullscreen modes */
1842 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1843 {
1844         wmWindow *win = CTX_wm_window(C);
1845         SpaceLink *sl = sa->spacedata.first;
1846         bScreen *screen = CTX_wm_screen(C);
1847         short state = (screen ? screen->state : SCREENMAXIMIZED);
1848         
1849         /* if fullscreen area has a temporary space (such as a file browser or fullscreen render
1850          * overlaid on top of an existing setup) then return to the previous space */
1851         
1852         if (sl->next) {
1853                 if (sa->flag & AREA_FLAG_TEMP_TYPE) {
1854                         ED_screen_full_prevspace(C, sa);
1855                 }
1856                 else {
1857                         ED_screen_state_toggle(C, win, sa, state);
1858                 }
1859                 /* warning: 'sa' may be freed */
1860         }
1861         /* otherwise just tile the area again */
1862         else {
1863                 ED_screen_state_toggle(C, win, sa, state);
1864         }
1865 }
1866
1867 /**
1868  * this function toggles: if area is maximized/full then the parent will be restored
1869  *
1870  * \warning \a sa may be freed.
1871  */
1872 ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const short state)
1873 {
1874         wmWindowManager *wm = CTX_wm_manager(C);
1875         bScreen *sc, *oldscreen;
1876         ARegion *ar;
1877
1878         if (sa) {
1879                 /* ensure we don't have a button active anymore, can crash when
1880                  * switching screens with tooltip open because region and tooltip
1881                  * are no longer in the same screen */
1882                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1883                         UI_blocklist_free(C, &ar->uiblocks);
1884
1885                         if (ar->regiontimer) {
1886                                 WM_event_remove_timer(wm, NULL, ar->regiontimer);
1887                                 ar->regiontimer = NULL;
1888                         }
1889                 }
1890
1891                 /* prevent hanging header prints */
1892                 ED_area_headerprint(sa, NULL);
1893         }
1894
1895         if (sa && sa->full) {
1896                 /* restoring back to SCREENNORMAL */
1897                 ScrArea *old;
1898
1899                 sc = sa->full;       /* the old screen to restore */
1900                 oldscreen = win->screen; /* the one disappearing */
1901
1902                 sc->state = SCREENNORMAL;
1903
1904                 /* find old area */
1905                 for (old = sc->areabase.first; old; old = old->next)
1906                         if (old->full) break;
1907                 if (old == NULL) {
1908                         if (G.debug & G_DEBUG)
1909                                 printf("%s: something wrong in areafullscreen\n", __func__);
1910                         return NULL;
1911                 }
1912
1913                 if (state == SCREENFULL) {
1914                         /* restore the old side panels/header visibility */
1915                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1916                                 ar->flag = ar->flagfullscreen;
1917                         }
1918                 }
1919
1920                 ED_area_data_swap(old, sa);
1921                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1922                 old->full = NULL;
1923
1924                 /* animtimer back */
1925                 sc->animtimer = oldscreen->animtimer;
1926                 oldscreen->animtimer = NULL;
1927
1928                 ED_screen_set(C, sc);
1929
1930                 BKE_libblock_free(CTX_data_main(C), oldscreen);
1931
1932                 /* After we've restored back to SCREENNORMAL, we have to wait with
1933                  * screen handling as it uses the area coords which aren't updated yet.
1934                  * Without doing so, the screen handling gets wrong area coords,
1935                  * which in worst case can lead to crashes (see T43139) */
1936                 sc->skip_handling = true;
1937         }
1938         else {
1939                 /* change from SCREENNORMAL to new state */
1940                 ScrArea *newa;
1941                 char newname[MAX_ID_NAME - 2];
1942
1943                 oldscreen = win->screen;
1944
1945                 oldscreen->state = state;
1946                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "nonnormal");
1947                 sc = ED_screen_add(win, oldscreen->scene, newname);
1948                 sc->state = state;
1949                 sc->redraws_flag = oldscreen->redraws_flag;
1950                 sc->temp = oldscreen->temp;
1951
1952                 /* timer */
1953                 sc->animtimer = oldscreen->animtimer;
1954                 oldscreen->animtimer = NULL;
1955
1956                 /* use random area when we have no active one, e.g. when the
1957                  * mouse is outside of the window and we open a file browser */
1958                 if (!sa)
1959                         sa = oldscreen->areabase.first;
1960
1961                 if (state == SCREENMAXIMIZED) {
1962                         /* returns the top small area */
1963                         newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1964                         ED_area_newspace(C, newa, SPACE_INFO, false);
1965
1966                         /* copy area */
1967                         newa = newa->prev;
1968                         ED_area_data_swap(newa, sa);
1969                         sa->flag |= AREA_TEMP_INFO;
1970
1971                         sa->full = oldscreen;
1972                         newa->full = oldscreen;
1973                         newa->next->full = oldscreen; // XXX
1974                 }
1975                 else if (state == SCREENFULL) {
1976                         newa = (ScrArea *)sc->areabase.first;
1977
1978                         /* copy area */
1979                         ED_area_data_swap(newa, sa);
1980                         newa->flag = sa->flag; /* mostly for AREA_FLAG_WASFULLSCREEN */
1981
1982                         /* temporarily hide the side panels/header */
1983                         for (ar = newa->regionbase.first; ar; ar = ar->next) {
1984                                 ar->flagfullscreen = ar->flag;
1985
1986                                 if (ELEM(ar->regiontype,
1987                                          RGN_TYPE_UI,
1988                                          RGN_TYPE_HEADER,
1989                                          RGN_TYPE_TOOLS))
1990                                 {
1991                                         ar->flag |= RGN_FLAG_HIDDEN;
1992                                 }
1993                         }
1994
1995                         sa->full = oldscreen;
1996                         newa->full = oldscreen;
1997                 }
1998                 else {
1999                         BLI_assert(false);
2000                 }
2001
2002                 ED_screen_set(C, sc);
2003         }
2004
2005         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
2006         CTX_wm_area_set(C, sc->areabase.first);
2007
2008         return sc->areabase.first;
2009 }
2010
2011 /* update frame rate info for viewport drawing */
2012 void ED_refresh_viewport_fps(bContext *C)
2013 {
2014         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
2015         Scene *scene = CTX_data_scene(C);
2016         
2017         /* is anim playback running? */
2018         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
2019                 ScreenFrameRateInfo *fpsi = scene->fps_info;
2020                 
2021                 /* if there isn't any info, init it first */
2022                 if (fpsi == NULL)
2023                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
2024                 
2025                 /* update the values */
2026                 fpsi->redrawtime = fpsi->lredrawtime;
2027                 fpsi->lredrawtime = animtimer->ltime;
2028         }
2029         else {
2030                 /* playback stopped or shouldn't be running */
2031                 if (scene->fps_info)
2032                         MEM_freeN(scene->fps_info);
2033                 scene->fps_info = NULL;
2034         }
2035 }
2036
2037 /* redraws: uses defines from stime->redraws 
2038  * enable: 1 - forward on, -1 - backwards on, 0 - off
2039  */
2040 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
2041 {
2042         bScreen *screen = CTX_wm_screen(C);
2043         wmWindowManager *wm = CTX_wm_manager(C);
2044         wmWindow *win = CTX_wm_window(C);
2045         Scene *scene = CTX_data_scene(C);
2046         bScreen *stopscreen = ED_screen_animation_playing(wm);
2047         
2048         if (stopscreen) {
2049                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
2050                 stopscreen->animtimer = NULL;
2051         }
2052         
2053         if (enable) {
2054                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
2055                 
2056                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
2057                 
2058                 sad->ar = CTX_wm_region(C);
2059                 /* if startframe is larger than current frame, we put currentframe on startframe.
2060                  * note: first frame then is not drawn! (ton) */
2061                 if (PRVRANGEON) {
2062                         if (scene->r.psfra > scene->r.cfra) {
2063                                 sad->sfra = scene->r.cfra;
2064                                 scene->r.cfra = scene->r.psfra;
2065                         }
2066                         else
2067                                 sad->sfra = scene->r.cfra;
2068                 }
2069                 else {
2070                         if (scene->r.sfra > scene->r.cfra) {
2071                                 sad->sfra = scene->r.cfra;
2072                                 scene->r.cfra = scene->r.sfra;
2073                         }
2074                         else
2075                                 sad->sfra = scene->r.cfra;
2076                 }
2077                 sad->redraws = redraws;
2078                 sad->refresh = refresh;
2079                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
2080                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
2081
2082                 ScrArea *sa = CTX_wm_area(C);
2083
2084                 char spacetype = -1;
2085
2086                 if (sa)
2087                         spacetype = sa->spacetype;
2088
2089                 sad->from_anim_edit = (ELEM(spacetype, SPACE_IPO, SPACE_ACTION, SPACE_NLA, SPACE_TIME));
2090
2091                 screen->animtimer->customdata = sad;
2092                 
2093         }
2094
2095         /* notifier catched by top header, for button */
2096         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
2097 }
2098
2099 /* helper for screen_animation_play() - only to be used for TimeLine */
2100 static ARegion *time_top_left_3dwindow(bScreen *screen)
2101 {
2102         ARegion *aret = NULL;
2103         ScrArea *sa;
2104         int min = 10000;
2105         
2106         for (sa = screen->areabase.first; sa; sa = sa->next) {
2107                 if (sa->spacetype == SPACE_VIEW3D) {
2108                         ARegion *ar;
2109                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2110                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2111                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
2112                                                 aret = ar;
2113                                                 min = ar->winrct.xmin - ar->winrct.ymin;
2114                                         }
2115                                 }
2116                         }
2117                 }
2118         }
2119
2120         return aret;
2121 }
2122
2123 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
2124 {
2125         if (screen && screen->animtimer) {
2126                 wmTimer *wt = screen->animtimer;
2127                 ScreenAnimData *sad = wt->customdata;
2128                 
2129                 sad->redraws = redraws;
2130                 sad->refresh = refresh;
2131                 sad->ar = NULL;
2132                 if (redraws & TIME_REGION)
2133                         sad->ar = time_top_left_3dwindow(screen);
2134         }
2135 }
2136
2137 /* results in fully updated anim system
2138  * screen can be NULL */
2139 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
2140 {
2141         wmWindowManager *wm = bmain->wm.first;
2142         wmWindow *window;
2143         int layers = 0;
2144
2145 #ifdef DURIAN_CAMERA_SWITCH
2146         void *camera = BKE_scene_camera_switch_find(scene);
2147         if (camera && scene->camera != camera) {
2148                 bScreen *sc;
2149                 scene->camera = camera;
2150                 /* are there cameras in the views that are not in the scene? */
2151                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
2152                         BKE_screen_view3d_scene_sync(sc);
2153                 }
2154         }
2155 #endif
2156         
2157         ED_clip_update_frame(bmain, scene->r.cfra);
2158
2159         /* get layers from all windows */
2160         for (window = wm->windows.first; window; window = window->next)
2161                 layers |= BKE_screen_visible_layers(window->screen, scene);
2162
2163         /* this function applies the changes too */
2164         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, layers);
2165
2166         /* composite */
2167         if (scene->use_nodes && scene->nodetree)
2168                 ntreeCompositTagAnimated(scene->nodetree);
2169         
2170         /* update animated texture nodes */
2171         {
2172                 Tex *tex;
2173                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
2174                         if (tex->use_nodes && tex->nodetree) {
2175                                 ntreeTexTagAnimated(tex->nodetree);
2176                         }
2177                 }
2178         }
2179         
2180 }
2181
2182 /*
2183  * return true if any active area requires to see in 3D
2184  */
2185 bool ED_screen_stereo3d_required(bScreen *screen)
2186 {
2187         ScrArea *sa;
2188         Scene *sce = screen->scene;
2189         const bool is_multiview = (sce->r.scemode & R_MULTIVIEW) != 0;
2190
2191         for (sa = screen->areabase.first; sa; sa = sa->next) {
2192                 switch (sa->spacetype) {
2193                         case SPACE_VIEW3D:
2194                         {
2195                                 View3D *v3d;
2196
2197                                 if (!is_multiview)
2198                                         continue;
2199
2200                                 v3d = sa->spacedata.first;
2201                                 if (v3d->camera && v3d->stereo3d_camera == STEREO_3D_ID) {
2202                                         ARegion *ar;
2203                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2204                                                 if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
2205                                                         RegionView3D *rv3d = ar->regiondata;
2206                                                         if (rv3d->persp == RV3D_CAMOB) {
2207                                                                 return true;
2208                                                         }
2209                                                 }
2210                                         }
2211                                 }
2212                                 break;
2213                         }
2214                         case SPACE_IMAGE:
2215                         {
2216                                 SpaceImage *sima;
2217
2218                                 /* images should always show in stereo, even if
2219                                  * the file doesn't have views enabled */
2220                                 sima = sa->spacedata.first;
2221                                 if (sima->image && BKE_image_is_stereo(sima->image) &&
2222                                     (sima->iuser.flag & IMA_SHOW_STEREO))
2223                                 {
2224                                         return true;
2225                                 }
2226                                 break;
2227                         }
2228                         case SPACE_NODE:
2229                         {
2230                                 SpaceNode *snode;
2231
2232                                 if (!is_multiview)
2233                                         continue;
2234
2235                                 snode = sa->spacedata.first;
2236                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
2237                                         return true;
2238                                 }
2239                                 break;
2240                         }
2241                         case SPACE_SEQ:
2242                         {
2243                                 SpaceSeq *sseq;
2244
2245                                 if (!is_multiview)
2246                                         continue;
2247
2248                                 sseq = sa->spacedata.first;
2249                                 if (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW)) {
2250                                         return true;
2251                                 }
2252
2253                                 if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
2254                                         return true;
2255                                 }
2256
2257                                 break;
2258                         }
2259                 }
2260         }
2261
2262         return false;
2263 }