code cleanup: favor braces when blocks have mixed brace use.
[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_global.h"
46 #include "BKE_library.h"
47 #include "BKE_main.h"
48 #include "BKE_node.h"
49 #include "BKE_screen.h"
50 #include "BKE_scene.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "ED_image.h"
59 #include "ED_object.h"
60 #include "ED_screen.h"
61 #include "ED_screen_types.h"
62 #include "ED_fileselect.h"
63 #include "ED_clip.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 int scredge_is_horizontal(ScrEdge *se)
263 {
264         return (se->v1->vec.y == se->v2->vec.y);
265 }
266
267 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
268 {
269         ScrEdge *se;
270         int safety = U.widget_unit / 10;
271         
272         if (safety < 2) safety = 2;
273         
274         for (se = sc->edgebase.first; se; se = se->next) {
275                 if (scredge_is_horizontal(se)) {
276                         short min, max;
277                         min = MIN2(se->v1->vec.x, se->v2->vec.x);
278                         max = MAX2(se->v1->vec.x, se->v2->vec.x);
279                         
280                         if (abs(my - se->v1->vec.y) <= safety && mx >= min && mx <= max)
281                                 return se;
282                 }
283                 else {
284                         short min, max;
285                         min = MIN2(se->v1->vec.y, se->v2->vec.y);
286                         max = MAX2(se->v1->vec.y, se->v2->vec.y);
287                         
288                         if (abs(mx - se->v1->vec.x) <= safety && my >= min && my <= max)
289                                 return se;
290                 }
291         }
292         
293         return NULL;
294 }
295
296
297
298 /* adds no space data */
299 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
300 {
301         ScrArea *sa = MEM_callocN(sizeof(ScrArea), "addscrarea");
302         sa->v1 = v1;
303         sa->v2 = v2;
304         sa->v3 = v3;
305         sa->v4 = v4;
306         sa->headertype = headertype;
307         sa->spacetype = sa->butspacetype = spacetype;
308         
309         BLI_addtail(&sc->areabase, sa);
310         
311         return sa;
312 }
313
314 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
315 {
316         
317         ED_area_exit(C, sa);
318         
319         BKE_screen_area_free(sa);
320         
321         BLI_remlink(&sc->areabase, sa);
322         MEM_freeN(sa);
323 }
324
325 /* return 0: no split possible */
326 /* else return (integer) screencoordinate split point */
327 static short testsplitpoint(ScrArea *sa, char dir, float fac)
328 {
329         short x, y;
330         
331         // area big enough?
332         if (dir == 'v' && (sa->v4->vec.x - sa->v1->vec.x <= 2 * AREAMINX)) return 0;
333         if (dir == 'h' && (sa->v2->vec.y - sa->v1->vec.y <= 2 * AREAMINY)) return 0;
334         
335         // to be sure
336         CLAMP(fac, 0.0f, 1.0f);
337         
338         if (dir == 'h') {
339                 y = sa->v1->vec.y + fac * (sa->v2->vec.y - sa->v1->vec.y);
340                 
341                 if (y - sa->v1->vec.y < AREAMINY)
342                         y = sa->v1->vec.y + AREAMINY;
343                 else if (sa->v2->vec.y - y < AREAMINY)
344                         y = sa->v2->vec.y - AREAMINY;
345                 else y -= (y % AREAGRID);
346                 
347                 return y;
348         }
349         else {
350                 x = sa->v1->vec.x + fac * (sa->v4->vec.x - sa->v1->vec.x);
351                 
352                 if (x - sa->v1->vec.x < AREAMINX)
353                         x = sa->v1->vec.x + AREAMINX;
354                 else if (sa->v4->vec.x - x < AREAMINX)
355                         x = sa->v4->vec.x - AREAMINX;
356                 else x -= (x % AREAGRID);
357                 
358                 return x;
359         }
360 }
361
362 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
363 {
364         ScrArea *newa = NULL;
365         ScrVert *sv1, *sv2;
366         short split;
367         
368         if (sa == NULL) return NULL;
369         
370         split = testsplitpoint(sa, dir, fac);
371         if (split == 0) return NULL;
372         
373         if (dir == 'h') {
374                 /* new vertices */
375                 sv1 = screen_addvert(sc, sa->v1->vec.x, split);
376                 sv2 = screen_addvert(sc, sa->v4->vec.x, split);
377                 
378                 /* new edges */
379                 screen_addedge(sc, sa->v1, sv1);
380                 screen_addedge(sc, sv1, sa->v2);
381                 screen_addedge(sc, sa->v3, sv2);
382                 screen_addedge(sc, sv2, sa->v4);
383                 screen_addedge(sc, sv1, sv2);
384                 
385                 /* new areas: top */
386                 newa = screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
387                 area_copy_data(newa, sa, 0);
388                 
389                 /* area below */
390                 sa->v2 = sv1;
391                 sa->v3 = sv2;
392                 
393         }
394         else {
395                 /* new vertices */
396                 sv1 = screen_addvert(sc, split, sa->v1->vec.y);
397                 sv2 = screen_addvert(sc, split, sa->v2->vec.y);
398                 
399                 /* new edges */
400                 screen_addedge(sc, sa->v1, sv1);
401                 screen_addedge(sc, sv1, sa->v4);
402                 screen_addedge(sc, sa->v2, sv2);
403                 screen_addedge(sc, sv2, sa->v3);
404                 screen_addedge(sc, sv1, sv2);
405                 
406                 /* new areas: left */
407                 newa = screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
408                 area_copy_data(newa, sa, 0);
409                 
410                 /* area right */
411                 sa->v1 = sv1;
412                 sa->v2 = sv2;
413         }
414         
415         /* remove double vertices en edges */
416         if (merge)
417                 removedouble_scrverts(sc);
418         removedouble_scredges(sc);
419         removenotused_scredges(sc);
420         
421         return newa;
422 }
423
424 /* empty screen, with 1 dummy area without spacedata */
425 /* uses window size */
426 bScreen *ED_screen_add(wmWindow *win, Scene *scene, const char *name)
427 {
428         bScreen *sc;
429         ScrVert *sv1, *sv2, *sv3, *sv4;
430         
431         sc = BKE_libblock_alloc(&G.main->screen, ID_SCR, name);
432         sc->scene = scene;
433         sc->do_refresh = TRUE;
434         sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN;
435         sc->winid = win->winid;
436
437         sv1 = screen_addvert(sc, 0, 0);
438         sv2 = screen_addvert(sc, 0, WM_window_pixels_y(win) - 1);
439         sv3 = screen_addvert(sc, WM_window_pixels_x(win) - 1, WM_window_pixels_y(win) - 1);
440         sv4 = screen_addvert(sc, WM_window_pixels_x(win) - 1, 0);
441         
442         screen_addedge(sc, sv1, sv2);
443         screen_addedge(sc, sv2, sv3);
444         screen_addedge(sc, sv3, sv4);
445         screen_addedge(sc, sv4, sv1);
446         
447         /* dummy type, no spacedata */
448         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
449                 
450         return sc;
451 }
452
453 static void screen_copy(bScreen *to, bScreen *from)
454 {
455         ScrVert *s1, *s2;
456         ScrEdge *se;
457         ScrArea *sa, *saf;
458         
459         /* free contents of 'to', is from blenkernel screen.c */
460         BKE_screen_free(to);
461         
462         BLI_duplicatelist(&to->vertbase, &from->vertbase);
463         BLI_duplicatelist(&to->edgebase, &from->edgebase);
464         BLI_duplicatelist(&to->areabase, &from->areabase);
465         to->regionbase.first = to->regionbase.last = NULL;
466         
467         s2 = to->vertbase.first;
468         for (s1 = from->vertbase.first; s1; s1 = s1->next, s2 = s2->next) {
469                 s1->newv = s2;
470         }
471         
472         for (se = to->edgebase.first; se; se = se->next) {
473                 se->v1 = se->v1->newv;
474                 se->v2 = se->v2->newv;
475                 sortscrvert(&(se->v1), &(se->v2));
476         }
477         
478         saf = from->areabase.first;
479         for (sa = to->areabase.first; sa; sa = sa->next, saf = saf->next) {
480                 sa->v1 = sa->v1->newv;
481                 sa->v2 = sa->v2->newv;
482                 sa->v3 = sa->v3->newv;
483                 sa->v4 = sa->v4->newv;
484
485                 sa->spacedata.first = sa->spacedata.last = NULL;
486                 sa->regionbase.first = sa->regionbase.last = NULL;
487                 sa->actionzones.first = sa->actionzones.last = NULL;
488                 sa->handlers.first = sa->handlers.last = NULL;
489                 
490                 area_copy_data(sa, saf, 0);
491         }
492         
493         /* put at zero (needed?) */
494         for (s1 = from->vertbase.first; s1; s1 = s1->next)
495                 s1->newv = NULL;
496
497 }
498
499
500 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
501 /* -1 = not valid check */
502 /* used with join operator */
503 int area_getorientation(ScrArea *sa, ScrArea *sb)
504 {
505         ScrVert *sav1, *sav2, *sav3, *sav4;
506         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
507
508         if (sa == NULL || sb == NULL) return -1;
509
510         sav1 = sa->v1;
511         sav2 = sa->v2;
512         sav3 = sa->v3;
513         sav4 = sa->v4;
514         sbv1 = sb->v1;
515         sbv2 = sb->v2;
516         sbv3 = sb->v3;
517         sbv4 = sb->v4;
518         
519         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
520                 return 0;
521         }
522         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
523                 return 1;
524         }
525         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
526                 return 2;
527         }
528         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
529                 return 3;
530         }
531         
532         return -1;
533 }
534
535 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
536  *  used by the split, join operators
537  */
538 int screen_area_join(bContext *C, bScreen *scr, ScrArea *sa1, ScrArea *sa2)
539 {
540         int dir;
541         
542         dir = area_getorientation(sa1, sa2);
543         /*printf("dir is : %i\n", dir);*/
544         
545         if (dir < 0) {
546                 if (sa1) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
547                 if (sa2) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
548                 return 0;
549         }
550         
551         if (dir == 0) {
552                 sa1->v1 = sa2->v1;
553                 sa1->v2 = sa2->v2;
554                 screen_addedge(scr, sa1->v2, sa1->v3);
555                 screen_addedge(scr, sa1->v1, sa1->v4);
556         }
557         else if (dir == 1) {
558                 sa1->v2 = sa2->v2;
559                 sa1->v3 = sa2->v3;
560                 screen_addedge(scr, sa1->v1, sa1->v2);
561                 screen_addedge(scr, sa1->v3, sa1->v4);
562         }
563         else if (dir == 2) {
564                 sa1->v3 = sa2->v3;
565                 sa1->v4 = sa2->v4;
566                 screen_addedge(scr, sa1->v2, sa1->v3);
567                 screen_addedge(scr, sa1->v1, sa1->v4);
568         }
569         else if (dir == 3) {
570                 sa1->v1 = sa2->v1;
571                 sa1->v4 = sa2->v4;
572                 screen_addedge(scr, sa1->v1, sa1->v2);
573                 screen_addedge(scr, sa1->v3, sa1->v4);
574         }
575         
576         screen_delarea(C, scr, sa2);
577         removedouble_scrverts(scr);
578         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
579         
580         return 1;
581 }
582
583 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
584 {
585         ScrEdge *se;
586         ScrVert *sv;
587         int oneselected;
588         char dir;
589         
590         /* select connected, only in the right direction */
591         /* 'dir' is the direction of EDGE */
592         
593         if (edge->v1->vec.x == edge->v2->vec.x) dir = 'v';
594         else dir = 'h';
595         
596         sv = sc->vertbase.first;
597         while (sv) {
598                 sv->flag = 0;
599                 sv = sv->next;
600         }
601         
602         edge->v1->flag = 1;
603         edge->v2->flag = 1;
604         
605         oneselected = 1;
606         while (oneselected) {
607                 se = sc->edgebase.first;
608                 oneselected = 0;
609                 while (se) {
610                         if (se->v1->flag + se->v2->flag == 1) {
611                                 if (dir == 'h') {
612                                         if (se->v1->vec.y == se->v2->vec.y) {
613                                                 se->v1->flag = se->v2->flag = 1;
614                                                 oneselected = 1;
615                                         }
616                                 }
617                                 if (dir == 'v') {
618                                         if (se->v1->vec.x == se->v2->vec.x) {
619                                                 se->v1->flag = se->v2->flag = 1;
620                                                 oneselected = 1;
621                                         }
622                                 }
623                         }
624                         se = se->next;
625                 }
626         }
627 }
628
629 /* test if screen vertices should be scaled */
630 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
631 {
632         ScrVert *sv = NULL;
633         ScrArea *sa;
634         int sizex, sizey;
635         float facx, facy, tempf, min[2], max[2];
636         
637         /* calculate size */
638         min[0] = min[1] = 20000.0f;
639         max[0] = max[1] = 0.0f;
640         
641         for (sv = sc->vertbase.first; sv; sv = sv->next) {
642                 const float fv[2] = {(float)sv->vec.x, (float)sv->vec.y};
643                 minmax_v2v2_v2(min, max, fv);
644         }
645         
646         /* always make 0.0 left under */
647         for (sv = sc->vertbase.first; sv; sv = sv->next) {
648                 sv->vec.x -= min[0];
649                 sv->vec.y -= min[1];
650         }
651         
652         sizex = max[0] - min[0];
653         sizey = max[1] - min[1];
654         
655         if (sizex != winsizex || sizey != winsizey) {
656                 facx = winsizex;
657                 facx /= (float)sizex;
658                 facy = winsizey;
659                 facy /= (float)sizey;
660                 
661                 /* make sure it fits! */
662                 for (sv = sc->vertbase.first; sv; sv = sv->next) {
663                         /* FIXME, this re-sizing logic is no good when re-sizing the window + redrawing [#24428]
664                          * need some way to store these as floats internally and re-apply from there. */
665                         tempf = ((float)sv->vec.x) * facx;
666                         sv->vec.x = (short)(tempf + 0.5f);
667                         sv->vec.x += AREAGRID - 1;
668                         sv->vec.x -=  (sv->vec.x % AREAGRID);
669
670                         CLAMP(sv->vec.x, 0, winsizex);
671                         
672                         tempf = ((float)sv->vec.y) * facy;
673                         sv->vec.y = (short)(tempf + 0.5f);
674                         sv->vec.y += AREAGRID - 1;
675                         sv->vec.y -=  (sv->vec.y % AREAGRID);
676
677                         CLAMP(sv->vec.y, 0, winsizey);
678                 }
679         }
680         
681         /* test for collapsed areas. This could happen in some blender version... */
682         /* ton: removed option now, it needs Context... */
683         
684         /* make each window at least ED_area_headersize() high */
685         for (sa = sc->areabase.first; sa; sa = sa->next) {
686                 int headery = ED_area_headersize() + U.pixelsize;
687                 
688                 if (sa->v1->vec.y + headery > sa->v2->vec.y) {
689                         /* lower edge */
690                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
691                         if (se && sa->v1 != sa->v2) {
692                                 int yval;
693                                 
694                                 select_connected_scredge(sc, se);
695                                 
696                                 /* all selected vertices get the right offset */
697                                 yval = sa->v2->vec.y - headery;
698                                 sv = sc->vertbase.first;
699                                 while (sv) {
700                                         /* if is a collapsed area */
701                                         if (sv != sa->v2 && sv != sa->v3) {
702                                                 if (sv->flag) sv->vec.y = yval;
703                                         }
704                                         sv = sv->next;
705                                 }
706                         }
707                 }
708         }
709         
710 }
711
712 /* *********************** DRAWING **************************************** */
713
714 /* draw vertical shape visualizing future joining (left as well
715  * right direction of future joining) */
716 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
717 {
718         vec2f points[10];
719         short i;
720         float w, h;
721         float width = sa->v3->vec.x - sa->v1->vec.x;
722         float height = sa->v3->vec.y - sa->v1->vec.y;
723
724         if (height < width) {
725                 h = height / 8;
726                 w = height / 4;
727         }
728         else {
729                 h = width / 8;
730                 w = width / 4;
731         }
732
733         points[0].x = sa->v1->vec.x;
734         points[0].y = sa->v1->vec.y + height / 2;
735
736         points[1].x = sa->v1->vec.x;
737         points[1].y = sa->v1->vec.y;
738
739         points[2].x = sa->v4->vec.x - w;
740         points[2].y = sa->v4->vec.y;
741
742         points[3].x = sa->v4->vec.x - w;
743         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
744
745         points[4].x = sa->v4->vec.x - 2 * w;
746         points[4].y = sa->v4->vec.y + height / 2;
747
748         points[5].x = sa->v4->vec.x - w;
749         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
750
751         points[6].x = sa->v3->vec.x - w;
752         points[6].y = sa->v3->vec.y;
753
754         points[7].x = sa->v2->vec.x;
755         points[7].y = sa->v2->vec.y;
756
757         points[8].x = sa->v4->vec.x;
758         points[8].y = sa->v4->vec.y + height / 2 - h;
759
760         points[9].x = sa->v4->vec.x;
761         points[9].y = sa->v4->vec.y + height / 2 + h;
762
763         if (dir == 'l') {
764                 /* when direction is left, then we flip direction of arrow */
765                 float cx = sa->v1->vec.x + width;
766                 for (i = 0; i < 10; i++) {
767                         points[i].x -= cx;
768                         points[i].x = -points[i].x;
769                         points[i].x += sa->v1->vec.x;
770                 }
771         }
772
773         glBegin(GL_POLYGON);
774         for (i = 0; i < 5; i++)
775                 glVertex2f(points[i].x, points[i].y);
776         glEnd();
777         glBegin(GL_POLYGON);
778         for (i = 4; i < 8; i++)
779                 glVertex2f(points[i].x, points[i].y);
780         glVertex2f(points[0].x, points[0].y);
781         glEnd();
782
783         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
784         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
785 }
786
787 /* draw vertical shape visualizing future joining (up/down direction) */
788 static void draw_vertical_join_shape(ScrArea *sa, char dir)
789 {
790         vec2f points[10];
791         short i;
792         float w, h;
793         float width = sa->v3->vec.x - sa->v1->vec.x;
794         float height = sa->v3->vec.y - sa->v1->vec.y;
795
796         if (height < width) {
797                 h = height / 4;
798                 w = height / 8;
799         }
800         else {
801                 h = width / 4;
802                 w = width / 8;
803         }
804
805         points[0].x = sa->v1->vec.x + width / 2;
806         points[0].y = sa->v3->vec.y;
807
808         points[1].x = sa->v2->vec.x;
809         points[1].y = sa->v2->vec.y;
810
811         points[2].x = sa->v1->vec.x;
812         points[2].y = sa->v1->vec.y + h;
813
814         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
815         points[3].y = sa->v1->vec.y + h;
816
817         points[4].x = sa->v1->vec.x + width / 2;
818         points[4].y = sa->v1->vec.y + 2 * h;
819
820         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
821         points[5].y = sa->v1->vec.y + h;
822
823         points[6].x = sa->v4->vec.x;
824         points[6].y = sa->v4->vec.y + h;
825         
826         points[7].x = sa->v3->vec.x;
827         points[7].y = sa->v3->vec.y;
828
829         points[8].x = sa->v1->vec.x + width / 2 - w;
830         points[8].y = sa->v1->vec.y;
831
832         points[9].x = sa->v1->vec.x + width / 2 + w;
833         points[9].y = sa->v1->vec.y;
834
835         if (dir == 'u') {
836                 /* when direction is up, then we flip direction of arrow */
837                 float cy = sa->v1->vec.y + height;
838                 for (i = 0; i < 10; i++) {
839                         points[i].y -= cy;
840                         points[i].y = -points[i].y;
841                         points[i].y += sa->v1->vec.y;
842                 }
843         }
844
845         glBegin(GL_POLYGON);
846         for (i = 0; i < 5; i++)
847                 glVertex2f(points[i].x, points[i].y);
848         glEnd();
849         glBegin(GL_POLYGON);
850         for (i = 4; i < 8; i++)
851                 glVertex2f(points[i].x, points[i].y);
852         glVertex2f(points[0].x, points[0].y);
853         glEnd();
854
855         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
856         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
857 }
858
859 /* draw join shape due to direction of joining */
860 static void draw_join_shape(ScrArea *sa, char dir)
861 {
862         if (dir == 'u' || dir == 'd')
863                 draw_vertical_join_shape(sa, dir);
864         else
865                 draw_horizontal_join_shape(sa, dir);
866 }
867
868 /* draw screen area darker with arrow (visualization of future joining) */
869 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
870 {
871         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
872         glEnable(GL_BLEND);
873         glColor4ub(0, 0, 0, 50);
874         draw_join_shape(sa, dir);
875         glDisable(GL_BLEND);
876 }
877
878 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
879 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
880 {
881         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
882         glEnable(GL_BLEND);
883         /* value 181 was hardly computed: 181~105 */
884         glColor4ub(255, 255, 255, 50);
885         /* draw_join_shape(sa, dir); */
886         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
887         glDisable(GL_BLEND);
888 }
889
890 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
891 {
892         /* right border area */
893         if (x2 < sizex - 1)
894                 sdrawline(x2 + a, y1, x2 + a, y2);
895         
896         /* left border area */
897         if (x1 > 0) /* otherwise it draws the emboss of window over */
898                 sdrawline(x1 + a, y1, x1 + a, y2);
899         
900         /* top border area */
901         if (y2 < sizey - 1)
902                 sdrawline(x1, y2 + a, x2, y2 + a);
903         
904         /* bottom border area */
905         if (y1 > 0)
906                 sdrawline(x1, y1 + a, x2, y1 + a);
907         
908 }
909
910 /** screen edges drawing **/
911 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
912 {
913         short x1 = sa->v1->vec.x;
914         short y1 = sa->v1->vec.y;
915         short x2 = sa->v3->vec.x;
916         short y2 = sa->v3->vec.y;
917         
918         if (center == 0) {
919                 if (U.pixelsize > 1.0f) {
920                 
921                         glColor3ub(0x50, 0x50, 0x50);
922                         glLineWidth(1.5f * U.pixelsize);
923                         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
924                         glLineWidth(1.0f);
925                 }
926         }
927         else {
928                 glColor3ub(0, 0, 0);
929                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
930         }
931 }
932
933 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
934
935 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
936 {
937         bScreen *newsc;
938         
939         if (sc->full != SCREENNORMAL) return NULL;  /* XXX handle this case! */
940         
941         /* make new empty screen: */
942         newsc = ED_screen_add(win, sc->scene, sc->id.name + 2);
943         /* copy all data */
944         screen_copy(newsc, sc);
945
946         return newsc;
947 }
948
949 /* screen sets cursor based on swinid */
950 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
951 {
952         ScrArea *sa = win->screen->areabase.first;
953         
954         for (; sa; sa = sa->next) {
955                 ARegion *ar = sa->regionbase.first;
956                 for (; ar; ar = ar->next) {
957                         if (ar->swinid == swinid) {
958                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
959                                         if (ar->type && ar->type->cursor)
960                                                 ar->type->cursor(win, sa, ar);
961                                         else
962                                                 WM_cursor_set(win, CURSOR_STD);
963                                 }
964                                 return;
965                         }
966                 }
967         }
968 }
969
970 void ED_screen_do_listen(bContext *C, wmNotifier *note)
971 {
972         wmWindow *win = CTX_wm_window(C);
973         
974         /* generic notes */
975         switch (note->category) {
976                 case NC_WM:
977                         if (note->data == ND_FILEREAD)
978                                 win->screen->do_draw = TRUE;
979                         break;
980                 case NC_WINDOW:
981                         win->screen->do_draw = TRUE;
982                         break;
983                 case NC_SCREEN:
984                         if (note->data == ND_SUBWINACTIVE)
985                                 uiFreeActiveButtons(C, win->screen);
986                         if (note->action == NA_EDITED)
987                                 win->screen->do_draw = win->screen->do_refresh = TRUE;
988                         break;
989                 case NC_SCENE:
990                         if (note->data == ND_MODE)
991                                 region_cursor_set(win, note->swinid, TRUE);
992                         break;
993         }
994 }
995
996 /* only for edge lines between areas, and the blended join arrows */
997 void ED_screen_draw(wmWindow *win)
998 {
999         ScrArea *sa;
1000         ScrArea *sa1 = NULL;
1001         ScrArea *sa2 = NULL;
1002         ScrArea *sa3 = NULL;
1003         int dir = -1;
1004         int dira = -1;
1005
1006         wmSubWindowSet(win, win->screen->mainwin);
1007         
1008         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1009                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1010                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1011                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1012                 drawscredge_area(sa, WM_window_pixels_x(win), WM_window_pixels_y(win), 0);
1013         }
1014         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1015                 drawscredge_area(sa, WM_window_pixels_x(win), WM_window_pixels_y(win), 1);
1016         
1017         /* blended join arrow */
1018         if (sa1 && sa2) {
1019                 dir = area_getorientation(sa1, sa2);
1020                 if (dir >= 0) {
1021                         switch (dir) {
1022                                 case 0: /* W */
1023                                         dir = 'r';
1024                                         dira = 'l';
1025                                         break;
1026                                 case 1: /* N */
1027                                         dir = 'd';
1028                                         dira = 'u';
1029                                         break;
1030                                 case 2: /* E */
1031                                         dir = 'l';
1032                                         dira = 'r';
1033                                         break;
1034                                 case 3: /* S */
1035                                         dir = 'u';
1036                                         dira = 'd';
1037                                         break;
1038                         }
1039                 }
1040                 scrarea_draw_shape_dark(sa2, dir);
1041                 scrarea_draw_shape_light(sa1, dira);
1042         }
1043         
1044         /* splitpoint */
1045         if (sa3) {
1046                 glEnable(GL_BLEND);
1047                 glColor4ub(255, 255, 255, 100);
1048                 
1049                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1050                         sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1051                         glColor4ub(0, 0, 0, 100);
1052                         sdrawline(sa3->totrct.xmin, win->eventstate->y + 1, sa3->totrct.xmax, win->eventstate->y + 1);
1053                 }
1054                 else {
1055                         sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1056                         glColor4ub(0, 0, 0, 100);
1057                         sdrawline(win->eventstate->x + 1, sa3->totrct.ymin, win->eventstate->x + 1, sa3->totrct.ymax);
1058                 }
1059                 
1060                 glDisable(GL_BLEND);
1061         }
1062         
1063         win->screen->do_draw = FALSE;
1064 }
1065
1066 /* helper call for below, dpi changes headers */
1067 static void screen_refresh_headersizes(void)
1068 {
1069         const ListBase *lb = BKE_spacetypes_list();
1070         SpaceType *st;
1071         
1072         for (st = lb->first; st; st = st->next) {
1073                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1074                 if (art) art->prefsizey = ED_area_headersize();
1075         }
1076 }
1077
1078 /* make this screen usable */
1079 /* for file read and first use, for scaling window, area moves */
1080 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1081 {       
1082         /* exception for bg mode, we only need the screen context */
1083         if (!G.background) {
1084                 ScrArea *sa;
1085                 rcti winrct;
1086         
1087                 winrct.xmin = 0;
1088                 winrct.xmax = WM_window_pixels_x(win) - 1;
1089                 winrct.ymin = 0;
1090                 winrct.ymax = WM_window_pixels_y(win) - 1;
1091                 
1092                 /* header size depends on DPI, let's verify */
1093                 screen_refresh_headersizes();
1094                 
1095                 screen_test_scale(win->screen, WM_window_pixels_x(win), WM_window_pixels_y(win));
1096                 
1097                 if (win->screen->mainwin == 0)
1098                         win->screen->mainwin = wm_subwindow_open(win, &winrct);
1099                 else
1100                         wm_subwindow_position(win, win->screen->mainwin, &winrct);
1101                 
1102                 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1103                         /* set spacetype and region callbacks, calls init() */
1104                         /* sets subwindows for regions, adds handlers */
1105                         ED_area_initialize(wm, win, sa);
1106                 }
1107         
1108                 /* wake up animtimer */
1109                 if (win->screen->animtimer)
1110                         WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1111         }
1112
1113         if (G.debug & G_DEBUG_EVENTS) {
1114                 printf("%s: set screen\n", __func__);
1115         }
1116         win->screen->do_refresh = FALSE;
1117
1118         win->screen->context = ed_screen_context;
1119 }
1120
1121 /* file read, set all screens, ... */
1122 void ED_screens_initialize(wmWindowManager *wm)
1123 {
1124         wmWindow *win;
1125         
1126         for (win = wm->windows.first; win; win = win->next) {
1127                 
1128                 if (win->screen == NULL)
1129                         win->screen = G.main->screen.first;
1130                 
1131                 ED_screen_refresh(wm, win);
1132         }
1133 }
1134
1135
1136 /* *********** exit calls are for closing running stuff ******** */
1137
1138 void ED_region_exit(bContext *C, ARegion *ar)
1139 {
1140         ARegion *prevar = CTX_wm_region(C);
1141
1142         CTX_wm_region_set(C, ar);
1143         WM_event_remove_handlers(C, &ar->handlers);
1144         if (ar->swinid)
1145                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1146         ar->swinid = 0;
1147         
1148         if (ar->headerstr)
1149                 MEM_freeN(ar->headerstr);
1150         ar->headerstr = NULL;
1151         
1152         if (ar->regiontimer)
1153                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), ar->regiontimer);
1154
1155         CTX_wm_region_set(C, prevar);
1156 }
1157
1158 void ED_area_exit(bContext *C, ScrArea *sa)
1159 {
1160         ScrArea *prevsa = CTX_wm_area(C);
1161         ARegion *ar;
1162
1163         if (sa->spacetype == SPACE_FILE) {
1164                 SpaceLink *sl = sa->spacedata.first;
1165                 if (sl && sl->spacetype == SPACE_FILE) {
1166                         ED_fileselect_exit(C, (SpaceFile *)sl);
1167                 }
1168         }
1169         else if (sa->spacetype == SPACE_VIEW3D) {
1170                 ED_render_engine_area_exit(sa);
1171         }
1172
1173         CTX_wm_area_set(C, sa);
1174         for (ar = sa->regionbase.first; ar; ar = ar->next)
1175                 ED_region_exit(C, ar);
1176
1177         WM_event_remove_handlers(C, &sa->handlers);
1178         CTX_wm_area_set(C, prevsa);
1179 }
1180
1181 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1182 {
1183         wmWindowManager *wm = CTX_wm_manager(C);
1184         wmWindow *prevwin = CTX_wm_window(C);
1185         ScrArea *sa;
1186         ARegion *ar;
1187
1188         CTX_wm_window_set(C, window);
1189         
1190         if (screen->animtimer)
1191                 WM_event_remove_timer(wm, window, screen->animtimer);
1192         screen->animtimer = NULL;
1193         
1194         if (screen->mainwin)
1195                 wm_subwindow_close(window, screen->mainwin);
1196         screen->mainwin = 0;
1197         screen->subwinactive = 0;
1198         
1199         for (ar = screen->regionbase.first; ar; ar = ar->next)
1200                 ED_region_exit(C, ar);
1201
1202         for (sa = screen->areabase.first; sa; sa = sa->next)
1203                 ED_area_exit(C, sa);
1204
1205         /* mark it available for use for other windows */
1206         screen->winid = 0;
1207         
1208         if (prevwin->screen->temp == 0) {
1209                 /* use previous window if possible */
1210                 CTX_wm_window_set(C, prevwin);
1211         }
1212         else {
1213                 /* none otherwise */
1214                 CTX_wm_window_set(C, NULL);
1215         }
1216         
1217 }
1218
1219 /* *********************************** */
1220
1221 /* case when on area-edge or in azones, or outside window */
1222 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1223 {
1224         AZone *az = NULL;
1225         ScrArea *sa;
1226         
1227         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1228                 if ((az = is_in_area_actionzone(sa, &event->x)))
1229                         break;
1230         
1231         if (sa) {
1232                 if (az->type == AZONE_AREA)
1233                         WM_cursor_set(win, CURSOR_EDIT);
1234                 else if (az->type == AZONE_REGION) {
1235                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1236                                 WM_cursor_set(win, CURSOR_X_MOVE);
1237                         else
1238                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1239                 }
1240         }
1241         else {
1242                 ScrEdge *actedge = screen_find_active_scredge(win->screen, event->x, event->y);
1243                 
1244                 if (actedge) {
1245                         if (scredge_is_horizontal(actedge))
1246                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1247                         else
1248                                 WM_cursor_set(win, CURSOR_X_MOVE);
1249                 }
1250                 else
1251                         WM_cursor_set(win, CURSOR_STD);
1252         }
1253 }
1254
1255
1256 /* called in wm_event_system.c. sets state vars in screen, cursors */
1257 /* event type is mouse move */
1258 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1259 {
1260         wmWindow *win = CTX_wm_window(C);
1261         
1262         if (win->screen) {
1263                 bScreen *scr = win->screen;
1264                 ScrArea *sa;
1265                 ARegion *ar;
1266                 int oldswin = scr->subwinactive;
1267
1268                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1269                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1270                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1271                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1272                                                 break;
1273                 }
1274                 if (sa) {
1275                         /* make overlap active when mouse over */
1276                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1277                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1278                                         scr->subwinactive = ar->swinid;
1279                                         break;
1280                                 }
1281                         }
1282                 }
1283                 else
1284                         scr->subwinactive = scr->mainwin;
1285                 
1286                 /* check for redraw headers */
1287                 if (oldswin != scr->subwinactive) {
1288
1289                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1290                                 int do_draw = FALSE;
1291                                 
1292                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1293                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1294                                                 do_draw = TRUE;
1295                                 
1296                                 if (do_draw) {
1297                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1298                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1299                                                         ED_region_tag_redraw(ar);
1300                                 }
1301                         }
1302                 }
1303                 
1304                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1305                 if (scr->subwinactive == scr->mainwin) {
1306                         screen_cursor_set(win, event);
1307                 }
1308                 else {
1309                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1310                         if (oldswin != scr->subwinactive) {
1311                                 region_cursor_set(win, scr->subwinactive, TRUE);
1312                                 WM_event_add_notifier(C, NC_SCREEN | ND_SUBWINACTIVE, scr);
1313                         }
1314                         else
1315                                 region_cursor_set(win, scr->subwinactive, FALSE);
1316                 }
1317         }
1318 }
1319
1320 int ED_screen_area_active(const bContext *C)
1321 {
1322         wmWindow *win = CTX_wm_window(C);
1323         bScreen *sc = CTX_wm_screen(C);
1324         ScrArea *sa = CTX_wm_area(C);
1325
1326         if (win && sc && sa) {
1327                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1328                 ARegion *ar;
1329                 
1330                 if (az && az->type == AZONE_REGION)
1331                         return 1;
1332                 
1333                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1334                         if (ar->swinid == sc->subwinactive)
1335                                 return 1;
1336         }
1337         return 0;
1338 }
1339
1340 /* operator call, WM + Window + screen already existed before */
1341 /* Do NOT call in area/region queues! */
1342 void ED_screen_set(bContext *C, bScreen *sc)
1343 {
1344         Main *bmain = CTX_data_main(C);
1345         wmWindowManager *wm = CTX_wm_manager(C);
1346         wmWindow *win = CTX_wm_window(C);
1347         bScreen *oldscreen = CTX_wm_screen(C);
1348         ID *id;
1349         
1350         /* validate screen, it's called with notifier reference */
1351         for (id = bmain->screen.first; id; id = id->next)
1352                 if (sc == (bScreen *)id)
1353                         break;
1354         if (id == NULL)
1355                 return;
1356         
1357         /* check for valid winid */
1358         if (sc->winid != 0 && sc->winid != win->winid)
1359                 return;
1360         
1361         if (sc->full) {             /* find associated full */
1362                 bScreen *sc1;
1363                 for (sc1 = bmain->screen.first; sc1; sc1 = sc1->id.next) {
1364                         ScrArea *sa = sc1->areabase.first;
1365                         if (sa->full == sc) {
1366                                 sc = sc1;
1367                                 break;
1368                         }
1369                 }
1370         }
1371         
1372         if (oldscreen != sc) {
1373                 wmTimer *wt = oldscreen->animtimer;
1374                 ScrArea *sa;
1375
1376                 /* remove handlers referencing areas in old screen */
1377                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1378                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1379                 }
1380
1381                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1382                 oldscreen->animtimer = NULL;
1383                 if (wt)
1384                         WM_event_timer_sleep(wm, win, wt, 1);
1385                 
1386                 ED_screen_exit(C, win, oldscreen);
1387                 oldscreen->animtimer = wt;
1388                 
1389                 win->screen = sc;
1390                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1391                 
1392                 /* prevent multiwin errors */
1393                 sc->winid = win->winid;
1394                 
1395                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1396                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1397                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1398                 
1399                 /* makes button hilites work */
1400                 WM_event_add_mousemove(C);
1401         }
1402 }
1403
1404 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1405 {
1406         wmWindow *win;
1407
1408         for (win = wm->windows.first; win; win = win->next)
1409                 if (win->screen == sc)
1410                         return 1;
1411         
1412         return 0;
1413 }
1414
1415 /* only call outside of area/region loops */
1416 void ED_screen_delete(bContext *C, bScreen *sc)
1417 {
1418         Main *bmain = CTX_data_main(C);
1419         wmWindowManager *wm = CTX_wm_manager(C);
1420         wmWindow *win = CTX_wm_window(C);
1421         bScreen *newsc;
1422         int delete = 1;
1423         
1424         /* don't allow deleting temp fullscreens for now */
1425         if (sc->full == SCREENFULL) {
1426                 return;
1427         }
1428         
1429                 
1430         /* screen can only be in use by one window at a time, so as
1431          * long as we are able to find a screen that is unused, we
1432          * can safely assume ours is not in use anywhere an delete it */
1433
1434         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1435                 if (!ed_screen_used(wm, newsc))
1436                         break;
1437         
1438         if (!newsc) {
1439                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1440                         if (!ed_screen_used(wm, newsc))
1441                                 break;
1442         }
1443
1444         if (!newsc)
1445                 return;
1446
1447         ED_screen_set(C, newsc);
1448
1449         if (delete && win->screen != sc)
1450                 BKE_libblock_free(&bmain->screen, sc);
1451 }
1452
1453 /* only call outside of area/region loops */
1454 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1455 {
1456         Main *bmain = CTX_data_main(C);
1457         bScreen *sc;
1458
1459         if (screen == NULL)
1460                 return;
1461         
1462         if (ed_screen_used(CTX_wm_manager(C), screen))
1463                 ED_object_exit_editmode(C, EM_FREEDATA | EM_DO_UNDO);
1464
1465         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1466                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1467                         
1468                         if (scene != sc->scene) {
1469                                 /* all areas endlocalview */
1470                                 // XXX  ScrArea *sa = sc->areabase.first;
1471                                 //      while (sa) {
1472                                 //              endlocalview(sa);
1473                                 //              sa = sa->next;
1474                                 //      }
1475                                 sc->scene = scene;
1476                         }
1477                         
1478                 }
1479         }
1480         
1481         //  copy_view3d_lock(0);        /* space.c */
1482         
1483         /* are there cameras in the views that are not in the scene? */
1484         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1485                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1486                         ScrArea *sa = sc->areabase.first;
1487                         while (sa) {
1488                                 SpaceLink *sl = sa->spacedata.first;
1489                                 while (sl) {
1490                                         if (sl->spacetype == SPACE_VIEW3D) {
1491                                                 View3D *v3d = (View3D *) sl;
1492
1493                                                 BKE_screen_view3d_sync(v3d, scene);
1494
1495                                                 if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1496                                                         v3d->camera = BKE_scene_camera_find(sc->scene);
1497                                                         // XXX if (sc == curscreen) handle_view3d_lock();
1498                                                         if (!v3d->camera) {
1499                                                                 ARegion *ar;
1500                                                                 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
1501                                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1502                                                                                 RegionView3D *rv3d = ar->regiondata;
1503
1504                                                                                 if (rv3d->persp == RV3D_CAMOB)
1505                                                                                         rv3d->persp = RV3D_PERSP;
1506                                                                         }
1507                                                                 }
1508                                                         }
1509                                                 }
1510                                         }
1511                                         sl = sl->next;
1512                                 }
1513                                 sa = sa->next;
1514                         }
1515                 }
1516         }
1517         
1518         CTX_data_scene_set(C, scene);
1519         BKE_scene_set_background(bmain, scene);
1520         DAG_on_visible_update(bmain, FALSE);
1521         
1522         ED_render_engine_changed(bmain);
1523         ED_update_for_newframe(bmain, scene, 1);
1524         
1525         /* complete redraw */
1526         WM_event_add_notifier(C, NC_WINDOW, NULL);
1527         
1528 }
1529
1530 /* only call outside of area/region loops */
1531 void ED_screen_delete_scene(bContext *C, Scene *scene)
1532 {
1533         Main *bmain = CTX_data_main(C);
1534         Scene *newscene;
1535
1536         if (scene->id.prev)
1537                 newscene = scene->id.prev;
1538         else if (scene->id.next)
1539                 newscene = scene->id.next;
1540         else
1541                 return;
1542
1543         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1544
1545         BKE_scene_unlink(bmain, scene, newscene);
1546 }
1547
1548 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1549 {
1550         wmWindow *win = CTX_wm_window(C);
1551         bScreen *screen = CTX_wm_screen(C);
1552         ScrArea *newsa = NULL;
1553
1554         if (!sa || sa->full == NULL) {
1555                 newsa = ED_screen_full_toggle(C, win, sa);
1556         }
1557         
1558         if (!newsa) {
1559                 if (sa->full) {
1560                         /* if this has been called from the temporary info header generated in
1561                          * temp fullscreen layouts, find the correct fullscreen area to change
1562                          * to create a new space inside */
1563                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1564                                 if (!(sa->flag & AREA_TEMP_INFO))
1565                                         break;
1566                         }
1567                 }
1568                 else {
1569                         newsa = sa;
1570                 }
1571         }
1572         
1573         ED_area_newspace(C, newsa, type);
1574         
1575         return newsa;
1576 }
1577
1578 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1579 {
1580         wmWindow *win = CTX_wm_window(C);
1581
1582         ED_area_prevspace(C, sa);
1583         
1584         if (sa->full)
1585                 ED_screen_full_toggle(C, win, sa);
1586 }
1587
1588 /* restore a screen / area back to default operation, after temp fullscreen modes */
1589 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1590 {
1591         wmWindow *win = CTX_wm_window(C);
1592         SpaceLink *sl = sa->spacedata.first;
1593         
1594         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1595          * overlaid on top of a existing setup) then return to the previous space */
1596         
1597         if (sl->next) {
1598                 /* specific checks for space types */
1599
1600                 int sima_restore = 0;
1601
1602                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1603                 if (sl->spacetype == SPACE_IMAGE) {
1604                         SpaceImage *sima = sa->spacedata.first;
1605                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1606                                 sima_restore = 1;
1607                 }
1608
1609                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1610                         SpaceImage *sima = sa->spacedata.first;
1611                         if (sima->flag & SI_PREVSPACE)
1612                                 sima->flag &= ~SI_PREVSPACE;
1613                         if (sima->flag & SI_FULLWINDOW) {
1614                                 sima->flag &= ~SI_FULLWINDOW;
1615                                 ED_screen_full_prevspace(C, sa);
1616                         }
1617                 }
1618                 else if (sl->spacetype == SPACE_FILE) {
1619                         ED_screen_full_prevspace(C, sa);
1620                 }
1621                 else {
1622                         ED_screen_full_toggle(C, win, sa);
1623                 }
1624         }
1625         /* otherwise just tile the area again */
1626         else {
1627                 ED_screen_full_toggle(C, win, sa);
1628         }
1629 }
1630
1631 /* this function toggles: if area is full then the parent will be restored */
1632 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1633 {
1634         bScreen *sc, *oldscreen;
1635         ARegion *ar;
1636
1637         if (sa) {
1638                 /* ensure we don't have a button active anymore, can crash when
1639                  * switching screens with tooltip open because region and tooltip
1640                  * are no longer in the same screen */
1641                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1642                         uiFreeBlocks(C, &ar->uiblocks);
1643                 
1644                 /* prevent hanging header prints */
1645                 ED_area_headerprint(sa, NULL);
1646         }
1647
1648         if (sa && sa->full) {
1649                 ScrArea *old;
1650                 /*short fulltype;*/ /*UNUSED*/
1651
1652                 sc = sa->full;       /* the old screen to restore */
1653                 oldscreen = win->screen; /* the one disappearing */
1654
1655                 /*fulltype = sc->full;*/
1656                 sc->full = 0;
1657
1658                 /* removed: SCREENAUTOPLAY exception here */
1659         
1660                 /* find old area */
1661                 for (old = sc->areabase.first; old; old = old->next)
1662                         if (old->full) break;
1663                 if (old == NULL) {
1664                         if (G.debug & G_DEBUG)
1665                                 printf("%s: something wrong in areafullscreen\n", __func__);
1666                         return NULL;
1667                 }
1668
1669                 area_copy_data(old, sa, 1); /*  1 = swap spacelist */
1670                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1671                 old->full = NULL;
1672
1673                 /* animtimer back */
1674                 sc->animtimer = oldscreen->animtimer;
1675                 oldscreen->animtimer = NULL;
1676
1677                 ED_screen_set(C, sc);
1678
1679                 BKE_screen_free(oldscreen);
1680                 BKE_libblock_free(&CTX_data_main(C)->screen, oldscreen);
1681
1682         }
1683         else {
1684                 ScrArea *newa;
1685                 char newname[MAX_ID_NAME - 2];
1686
1687                 oldscreen = win->screen;
1688
1689                 /* nothing wrong with having only 1 area, as far as I can see...
1690                  * is there only 1 area? */
1691 #if 0
1692                 if (oldscreen->areabase.first == oldscreen->areabase.last)
1693                         return NULL;
1694 #endif
1695
1696                 oldscreen->full = SCREENFULL;
1697                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "full");
1698                 sc = ED_screen_add(win, oldscreen->scene, newname);
1699                 sc->full = SCREENFULL; // XXX
1700
1701                 /* timer */
1702                 sc->animtimer = oldscreen->animtimer;
1703                 oldscreen->animtimer = NULL;
1704
1705                 /* returns the top small area */
1706                 newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1707                 ED_area_newspace(C, newa, SPACE_INFO);
1708
1709                 /* use random area when we have no active one, e.g. when the
1710                  * mouse is outside of the window and we open a file browser */
1711                 if (!sa)
1712                         sa = oldscreen->areabase.first;
1713
1714                 /* copy area */
1715                 newa = newa->prev;
1716                 area_copy_data(newa, sa, 1);  /* 1 = swap spacelist */
1717                 sa->flag |= AREA_TEMP_INFO;
1718
1719                 sa->full = oldscreen;
1720                 newa->full = oldscreen;
1721                 newa->next->full = oldscreen; // XXX
1722
1723                 ED_screen_set(C, sc);
1724         }
1725
1726         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1727         CTX_wm_area_set(C, sc->areabase.first);
1728
1729         return sc->areabase.first;
1730 }
1731
1732 /* update frame rate info for viewport drawing */
1733 void ED_refresh_viewport_fps(bContext *C)
1734 {
1735         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1736         Scene *scene = CTX_data_scene(C);
1737         
1738         /* is anim playback running? */
1739         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1740                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1741                 
1742                 /* if there isn't any info, init it first */
1743                 if (fpsi == NULL)
1744                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1745                 
1746                 /* update the values */
1747                 fpsi->redrawtime = fpsi->lredrawtime;
1748                 fpsi->lredrawtime = animtimer->ltime;
1749         }
1750         else {
1751                 /* playback stopped or shouldn't be running */
1752                 if (scene->fps_info)
1753                         MEM_freeN(scene->fps_info);
1754                 scene->fps_info = NULL;
1755         }
1756 }
1757
1758 /* redraws: uses defines from stime->redraws 
1759  * enable: 1 - forward on, -1 - backwards on, 0 - off
1760  */
1761 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1762 {
1763         bScreen *screen = CTX_wm_screen(C);
1764         wmWindowManager *wm = CTX_wm_manager(C);
1765         wmWindow *win = CTX_wm_window(C);
1766         Scene *scene = CTX_data_scene(C);
1767         bScreen *stopscreen = ED_screen_animation_playing(wm);
1768         
1769         if (stopscreen) {
1770                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1771                 stopscreen->animtimer = NULL;
1772         }
1773         
1774         if (enable) {
1775                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1776                 
1777                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1778                 
1779                 sad->ar = CTX_wm_region(C);
1780                 /* if startframe is larger than current frame, we put currentframe on startframe.
1781                  * note: first frame then is not drawn! (ton) */
1782                 if (PRVRANGEON) {
1783                         if (scene->r.psfra > scene->r.cfra) {
1784                                 sad->sfra = scene->r.cfra;
1785                                 scene->r.cfra = scene->r.psfra;
1786                         }
1787                         else
1788                                 sad->sfra = scene->r.cfra;
1789                 }
1790                 else {
1791                         if (scene->r.sfra > scene->r.cfra) {
1792                                 sad->sfra = scene->r.cfra;
1793                                 scene->r.cfra = scene->r.sfra;
1794                         }
1795                         else
1796                                 sad->sfra = scene->r.cfra;
1797                 }
1798                 sad->redraws = redraws;
1799                 sad->refresh = refresh;
1800                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1801                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1802                 
1803                 screen->animtimer->customdata = sad;
1804                 
1805         }
1806
1807         /* notifier catched by top header, for button */
1808         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1809 }
1810
1811 /* helper for screen_animation_play() - only to be used for TimeLine */
1812 static ARegion *time_top_left_3dwindow(bScreen *screen)
1813 {
1814         ARegion *aret = NULL;
1815         ScrArea *sa;
1816         int min = 10000;
1817         
1818         for (sa = screen->areabase.first; sa; sa = sa->next) {
1819                 if (sa->spacetype == SPACE_VIEW3D) {
1820                         ARegion *ar;
1821                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1822                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1823                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1824                                                 aret = ar;
1825                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1826                                         }
1827                                 }
1828                         }
1829                 }
1830         }
1831
1832         return aret;
1833 }
1834
1835 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1836 {
1837         if (screen && screen->animtimer) {
1838                 wmTimer *wt = screen->animtimer;
1839                 ScreenAnimData *sad = wt->customdata;
1840                 
1841                 sad->redraws = redraws;
1842                 sad->refresh = refresh;
1843                 sad->ar = NULL;
1844                 if (redraws & TIME_REGION)
1845                         sad->ar = time_top_left_3dwindow(screen);
1846         }
1847 }
1848
1849 /* results in fully updated anim system
1850  * screen can be NULL */
1851 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
1852 {
1853         wmWindowManager *wm = bmain->wm.first;
1854         wmWindow *window;
1855         int layers = 0;
1856
1857 #ifdef DURIAN_CAMERA_SWITCH
1858         void *camera = BKE_scene_camera_switch_find(scene);
1859         if (camera && scene->camera != camera) {
1860                 bScreen *sc;
1861                 scene->camera = camera;
1862                 /* are there cameras in the views that are not in the scene? */
1863                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1864                         BKE_screen_view3d_scene_sync(sc);
1865                 }
1866         }
1867 #endif
1868
1869         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1870         
1871         /* update animated image textures for gpu, etc,
1872          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
1873         ED_image_update_frame(bmain, scene->r.cfra);
1874
1875         ED_clip_update_frame(bmain, scene->r.cfra);
1876
1877         /* get layers from all windows */
1878         for (window = wm->windows.first; window; window = window->next)
1879                 layers |= BKE_screen_visible_layers(window->screen, scene);
1880
1881         /* this function applies the changes too */
1882         BKE_scene_update_for_newframe(bmain, scene, layers); /* BKE_scene.h */
1883         
1884         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1885         //      audiostream_scrub( CFRA );
1886         
1887         /* 3d window, preview */
1888         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1889         
1890         /* all movie/sequence images */
1891         //BIF_image_update_frame();
1892         
1893         /* composite */
1894         if (scene->use_nodes && scene->nodetree)
1895                 ntreeCompositTagAnimated(scene->nodetree);
1896         
1897         /* update animated texture nodes */
1898         {
1899                 Tex *tex;
1900                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
1901                         if (tex->use_nodes && tex->nodetree) {
1902                                 ntreeTexTagAnimated(tex->nodetree);
1903                         }
1904         }
1905         
1906 }
1907
1908