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