cca713ec14a358d96c50dab6f6d83bf1903a1125
[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         
677         /* test for collapsed areas. This could happen in some blender version... */
678         /* ton: removed option now, it needs Context... */
679         
680         /* make each window at least ED_area_headersize() high */
681         for (sa = sc->areabase.first; sa; sa = sa->next) {
682                 int headery = ED_area_headersize() + U.pixelsize;
683                 
684                 if (sa->v1->vec.y + headery > sa->v2->vec.y) {
685                         /* lower edge */
686                         ScrEdge *se = screen_findedge(sc, sa->v4, sa->v1);
687                         if (se && sa->v1 != sa->v2) {
688                                 int yval;
689                                 
690                                 select_connected_scredge(sc, se);
691                                 
692                                 /* all selected vertices get the right offset */
693                                 yval = sa->v2->vec.y - headery;
694                                 sv = sc->vertbase.first;
695                                 while (sv) {
696                                         /* if is a collapsed area */
697                                         if (sv != sa->v2 && sv != sa->v3) {
698                                                 if (sv->flag) sv->vec.y = yval;
699                                         }
700                                         sv = sv->next;
701                                 }
702                         }
703                 }
704         }
705         
706 }
707
708 /* *********************** DRAWING **************************************** */
709
710 /* draw vertical shape visualizing future joining (left as well
711  * right direction of future joining) */
712 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
713 {
714         vec2f points[10];
715         short i;
716         float w, h;
717         float width = sa->v3->vec.x - sa->v1->vec.x;
718         float height = sa->v3->vec.y - sa->v1->vec.y;
719
720         if (height < width) {
721                 h = height / 8;
722                 w = height / 4;
723         }
724         else {
725                 h = width / 8;
726                 w = width / 4;
727         }
728
729         points[0].x = sa->v1->vec.x;
730         points[0].y = sa->v1->vec.y + height / 2;
731
732         points[1].x = sa->v1->vec.x;
733         points[1].y = sa->v1->vec.y;
734
735         points[2].x = sa->v4->vec.x - w;
736         points[2].y = sa->v4->vec.y;
737
738         points[3].x = sa->v4->vec.x - w;
739         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
740
741         points[4].x = sa->v4->vec.x - 2 * w;
742         points[4].y = sa->v4->vec.y + height / 2;
743
744         points[5].x = sa->v4->vec.x - w;
745         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
746
747         points[6].x = sa->v3->vec.x - w;
748         points[6].y = sa->v3->vec.y;
749
750         points[7].x = sa->v2->vec.x;
751         points[7].y = sa->v2->vec.y;
752
753         points[8].x = sa->v4->vec.x;
754         points[8].y = sa->v4->vec.y + height / 2 - h;
755
756         points[9].x = sa->v4->vec.x;
757         points[9].y = sa->v4->vec.y + height / 2 + h;
758
759         if (dir == 'l') {
760                 /* when direction is left, then we flip direction of arrow */
761                 float cx = sa->v1->vec.x + width;
762                 for (i = 0; i < 10; i++) {
763                         points[i].x -= cx;
764                         points[i].x = -points[i].x;
765                         points[i].x += sa->v1->vec.x;
766                 }
767         }
768
769         glBegin(GL_POLYGON);
770         for (i = 0; i < 5; i++)
771                 glVertex2f(points[i].x, points[i].y);
772         glEnd();
773         glBegin(GL_POLYGON);
774         for (i = 4; i < 8; i++)
775                 glVertex2f(points[i].x, points[i].y);
776         glVertex2f(points[0].x, points[0].y);
777         glEnd();
778
779         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
780         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
781 }
782
783 /* draw vertical shape visualizing future joining (up/down direction) */
784 static void draw_vertical_join_shape(ScrArea *sa, char dir)
785 {
786         vec2f points[10];
787         short i;
788         float w, h;
789         float width = sa->v3->vec.x - sa->v1->vec.x;
790         float height = sa->v3->vec.y - sa->v1->vec.y;
791
792         if (height < width) {
793                 h = height / 4;
794                 w = height / 8;
795         }
796         else {
797                 h = width / 4;
798                 w = width / 8;
799         }
800
801         points[0].x = sa->v1->vec.x + width / 2;
802         points[0].y = sa->v3->vec.y;
803
804         points[1].x = sa->v2->vec.x;
805         points[1].y = sa->v2->vec.y;
806
807         points[2].x = sa->v1->vec.x;
808         points[2].y = sa->v1->vec.y + h;
809
810         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
811         points[3].y = sa->v1->vec.y + h;
812
813         points[4].x = sa->v1->vec.x + width / 2;
814         points[4].y = sa->v1->vec.y + 2 * h;
815
816         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
817         points[5].y = sa->v1->vec.y + h;
818
819         points[6].x = sa->v4->vec.x;
820         points[6].y = sa->v4->vec.y + h;
821         
822         points[7].x = sa->v3->vec.x;
823         points[7].y = sa->v3->vec.y;
824
825         points[8].x = sa->v1->vec.x + width / 2 - w;
826         points[8].y = sa->v1->vec.y;
827
828         points[9].x = sa->v1->vec.x + width / 2 + w;
829         points[9].y = sa->v1->vec.y;
830
831         if (dir == 'u') {
832                 /* when direction is up, then we flip direction of arrow */
833                 float cy = sa->v1->vec.y + height;
834                 for (i = 0; i < 10; i++) {
835                         points[i].y -= cy;
836                         points[i].y = -points[i].y;
837                         points[i].y += sa->v1->vec.y;
838                 }
839         }
840
841         glBegin(GL_POLYGON);
842         for (i = 0; i < 5; i++)
843                 glVertex2f(points[i].x, points[i].y);
844         glEnd();
845         glBegin(GL_POLYGON);
846         for (i = 4; i < 8; i++)
847                 glVertex2f(points[i].x, points[i].y);
848         glVertex2f(points[0].x, points[0].y);
849         glEnd();
850
851         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
852         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
853 }
854
855 /* draw join shape due to direction of joining */
856 static void draw_join_shape(ScrArea *sa, char dir)
857 {
858         if (dir == 'u' || dir == 'd')
859                 draw_vertical_join_shape(sa, dir);
860         else
861                 draw_horizontal_join_shape(sa, dir);
862 }
863
864 /* draw screen area darker with arrow (visualization of future joining) */
865 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
866 {
867         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
868         glEnable(GL_BLEND);
869         glColor4ub(0, 0, 0, 50);
870         draw_join_shape(sa, dir);
871         glDisable(GL_BLEND);
872 }
873
874 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
875 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
876 {
877         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
878         glEnable(GL_BLEND);
879         /* value 181 was hardly computed: 181~105 */
880         glColor4ub(255, 255, 255, 50);
881         /* draw_join_shape(sa, dir); */
882         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
883         glDisable(GL_BLEND);
884 }
885
886 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
887 {
888         /* right border area */
889         if (x2 < sizex - 1)
890                 sdrawline(x2 + a, y1, x2 + a, y2);
891         
892         /* left border area */
893         if (x1 > 0) /* otherwise it draws the emboss of window over */
894                 sdrawline(x1 + a, y1, x1 + a, y2);
895         
896         /* top border area */
897         if (y2 < sizey - 1)
898                 sdrawline(x1, y2 + a, x2, y2 + a);
899         
900         /* bottom border area */
901         if (y1 > 0)
902                 sdrawline(x1, y1 + a, x2, y1 + a);
903         
904 }
905
906 /** screen edges drawing **/
907 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
908 {
909         short x1 = sa->v1->vec.x;
910         short y1 = sa->v1->vec.y;
911         short x2 = sa->v3->vec.x;
912         short y2 = sa->v3->vec.y;
913         
914         if (center == 0) {
915                 if (U.pixelsize > 1.0f) {
916                 
917                         glColor3ub(0x50, 0x50, 0x50);
918                         glLineWidth(1.5f * U.pixelsize);
919                         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
920                         glLineWidth(1.0f);
921                 }
922         }
923         else {
924                 glColor3ub(0, 0, 0);
925                 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
926         }
927 }
928
929 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
930
931 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
932 {
933         bScreen *newsc;
934         
935         if (sc->full != SCREENNORMAL) return NULL;  /* XXX handle this case! */
936         
937         /* make new empty screen: */
938         newsc = ED_screen_add(win, sc->scene, sc->id.name + 2);
939         /* copy all data */
940         screen_copy(newsc, sc);
941
942         return newsc;
943 }
944
945 /* screen sets cursor based on swinid */
946 static void region_cursor_set(wmWindow *win, int swinid, int swin_changed)
947 {
948         ScrArea *sa = win->screen->areabase.first;
949         
950         for (; sa; sa = sa->next) {
951                 ARegion *ar = sa->regionbase.first;
952                 for (; ar; ar = ar->next) {
953                         if (ar->swinid == swinid) {
954                                 if (swin_changed || (ar->type && ar->type->event_cursor)) {
955                                         if (ar->type && ar->type->cursor)
956                                                 ar->type->cursor(win, sa, ar);
957                                         else
958                                                 WM_cursor_set(win, CURSOR_STD);
959                                 }
960                                 return;
961                         }
962                 }
963         }
964 }
965
966 void ED_screen_do_listen(bContext *C, wmNotifier *note)
967 {
968         wmWindow *win = CTX_wm_window(C);
969         
970         /* generic notes */
971         switch (note->category) {
972                 case NC_WM:
973                         if (note->data == ND_FILEREAD)
974                                 win->screen->do_draw = TRUE;
975                         break;
976                 case NC_WINDOW:
977                         win->screen->do_draw = TRUE;
978                         break;
979                 case NC_SCREEN:
980                         if (note->data == ND_SUBWINACTIVE)
981                                 uiFreeActiveButtons(C, win->screen);
982                         if (note->action == NA_EDITED)
983                                 win->screen->do_draw = win->screen->do_refresh = TRUE;
984                         break;
985                 case NC_SCENE:
986                         if (note->data == ND_MODE)
987                                 region_cursor_set(win, note->swinid, TRUE);
988                         break;
989         }
990 }
991
992 /* only for edge lines between areas, and the blended join arrows */
993 void ED_screen_draw(wmWindow *win)
994 {
995         ScrArea *sa;
996         ScrArea *sa1 = NULL;
997         ScrArea *sa2 = NULL;
998         ScrArea *sa3 = NULL;
999         int dir = -1;
1000         int dira = -1;
1001
1002         wmSubWindowSet(win, win->screen->mainwin);
1003         
1004         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1005                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1006                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1007                 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1008                 drawscredge_area(sa, WM_window_pixels_x(win), WM_window_pixels_y(win), 0);
1009         }
1010         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1011                 drawscredge_area(sa, WM_window_pixels_x(win), WM_window_pixels_y(win), 1);
1012         
1013         /* blended join arrow */
1014         if (sa1 && sa2) {
1015                 dir = area_getorientation(sa1, sa2);
1016                 if (dir >= 0) {
1017                         switch (dir) {
1018                                 case 0: /* W */
1019                                         dir = 'r';
1020                                         dira = 'l';
1021                                         break;
1022                                 case 1: /* N */
1023                                         dir = 'd';
1024                                         dira = 'u';
1025                                         break;
1026                                 case 2: /* E */
1027                                         dir = 'l';
1028                                         dira = 'r';
1029                                         break;
1030                                 case 3: /* S */
1031                                         dir = 'u';
1032                                         dira = 'd';
1033                                         break;
1034                         }
1035                 }
1036                 scrarea_draw_shape_dark(sa2, dir);
1037                 scrarea_draw_shape_light(sa1, dira);
1038         }
1039         
1040         /* splitpoint */
1041         if (sa3) {
1042                 glEnable(GL_BLEND);
1043                 glColor4ub(255, 255, 255, 100);
1044                 
1045                 if (sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1046                         sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1047                         glColor4ub(0, 0, 0, 100);
1048                         sdrawline(sa3->totrct.xmin, win->eventstate->y + 1, sa3->totrct.xmax, win->eventstate->y + 1);
1049                 }
1050                 else {
1051                         sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1052                         glColor4ub(0, 0, 0, 100);
1053                         sdrawline(win->eventstate->x + 1, sa3->totrct.ymin, win->eventstate->x + 1, sa3->totrct.ymax);
1054                 }
1055                 
1056                 glDisable(GL_BLEND);
1057         }
1058         
1059         win->screen->do_draw = FALSE;
1060 }
1061
1062 /* helper call for below, dpi changes headers */
1063 static void screen_refresh_headersizes(void)
1064 {
1065         const ListBase *lb = BKE_spacetypes_list();
1066         SpaceType *st;
1067         
1068         for (st = lb->first; st; st = st->next) {
1069                 ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_HEADER);
1070                 if (art) art->prefsizey = ED_area_headersize();
1071         }
1072 }
1073
1074 /* make this screen usable */
1075 /* for file read and first use, for scaling window, area moves */
1076 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1077 {       
1078         /* exception for bg mode, we only need the screen context */
1079         if (!G.background) {
1080                 ScrArea *sa;
1081                 rcti winrct;
1082         
1083                 winrct.xmin = 0;
1084                 winrct.xmax = WM_window_pixels_x(win) - 1;
1085                 winrct.ymin = 0;
1086                 winrct.ymax = WM_window_pixels_y(win) - 1;
1087                 
1088                 /* header size depends on DPI, let's verify */
1089                 screen_refresh_headersizes();
1090                 
1091                 screen_test_scale(win->screen, WM_window_pixels_x(win), WM_window_pixels_y(win));
1092                 
1093                 if (win->screen->mainwin == 0)
1094                         win->screen->mainwin = wm_subwindow_open(win, &winrct);
1095                 else
1096                         wm_subwindow_position(win, win->screen->mainwin, &winrct);
1097                 
1098                 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
1099                         /* set spacetype and region callbacks, calls init() */
1100                         /* sets subwindows for regions, adds handlers */
1101                         ED_area_initialize(wm, win, sa);
1102                 }
1103         
1104                 /* wake up animtimer */
1105                 if (win->screen->animtimer)
1106                         WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1107         }
1108
1109         if (G.debug & G_DEBUG_EVENTS) {
1110                 printf("%s: set screen\n", __func__);
1111         }
1112         win->screen->do_refresh = FALSE;
1113
1114         win->screen->context = ed_screen_context;
1115 }
1116
1117 /* file read, set all screens, ... */
1118 void ED_screens_initialize(wmWindowManager *wm)
1119 {
1120         wmWindow *win;
1121         
1122         for (win = wm->windows.first; win; win = win->next) {
1123                 
1124                 if (win->screen == NULL)
1125                         win->screen = G.main->screen.first;
1126                 
1127                 ED_screen_refresh(wm, win);
1128         }
1129 }
1130
1131
1132 /* *********** exit calls are for closing running stuff ******** */
1133
1134 void ED_region_exit(bContext *C, ARegion *ar)
1135 {
1136         ARegion *prevar = CTX_wm_region(C);
1137
1138         CTX_wm_region_set(C, ar);
1139         WM_event_remove_handlers(C, &ar->handlers);
1140         if (ar->swinid)
1141                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1142         ar->swinid = 0;
1143         
1144         if (ar->headerstr)
1145                 MEM_freeN(ar->headerstr);
1146         ar->headerstr = NULL;
1147         
1148         if (ar->regiontimer)
1149                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), ar->regiontimer);
1150
1151         CTX_wm_region_set(C, prevar);
1152 }
1153
1154 void ED_area_exit(bContext *C, ScrArea *sa)
1155 {
1156         ScrArea *prevsa = CTX_wm_area(C);
1157         ARegion *ar;
1158
1159         if (sa->spacetype == SPACE_FILE) {
1160                 SpaceLink *sl = sa->spacedata.first;
1161                 if (sl && sl->spacetype == SPACE_FILE) {
1162                         ED_fileselect_exit(C, (SpaceFile *)sl);
1163                 }
1164         }
1165         else if (sa->spacetype == SPACE_VIEW3D) {
1166                 ED_render_engine_area_exit(sa);
1167         }
1168
1169         CTX_wm_area_set(C, sa);
1170         for (ar = sa->regionbase.first; ar; ar = ar->next)
1171                 ED_region_exit(C, ar);
1172
1173         WM_event_remove_handlers(C, &sa->handlers);
1174         CTX_wm_area_set(C, prevsa);
1175 }
1176
1177 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1178 {
1179         wmWindowManager *wm = CTX_wm_manager(C);
1180         wmWindow *prevwin = CTX_wm_window(C);
1181         ScrArea *sa;
1182         ARegion *ar;
1183
1184         CTX_wm_window_set(C, window);
1185         
1186         if (screen->animtimer)
1187                 WM_event_remove_timer(wm, window, screen->animtimer);
1188         screen->animtimer = NULL;
1189         
1190         if (screen->mainwin)
1191                 wm_subwindow_close(window, screen->mainwin);
1192         screen->mainwin = 0;
1193         screen->subwinactive = 0;
1194         
1195         for (ar = screen->regionbase.first; ar; ar = ar->next)
1196                 ED_region_exit(C, ar);
1197
1198         for (sa = screen->areabase.first; sa; sa = sa->next)
1199                 ED_area_exit(C, sa);
1200
1201         /* mark it available for use for other windows */
1202         screen->winid = 0;
1203         
1204         if (prevwin->screen->temp == 0) {
1205                 /* use previous window if possible */
1206                 CTX_wm_window_set(C, prevwin);
1207         }
1208         else {
1209                 /* none otherwise */
1210                 CTX_wm_window_set(C, NULL);
1211         }
1212         
1213 }
1214
1215 /* *********************************** */
1216
1217 /* case when on area-edge or in azones, or outside window */
1218 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1219 {
1220         AZone *az = NULL;
1221         ScrArea *sa;
1222         
1223         for (sa = win->screen->areabase.first; sa; sa = sa->next)
1224                 if ((az = is_in_area_actionzone(sa, &event->x)))
1225                         break;
1226         
1227         if (sa) {
1228                 if (az->type == AZONE_AREA)
1229                         WM_cursor_set(win, CURSOR_EDIT);
1230                 else if (az->type == AZONE_REGION) {
1231                         if (az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1232                                 WM_cursor_set(win, CURSOR_X_MOVE);
1233                         else
1234                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1235                 }
1236         }
1237         else {
1238                 ScrEdge *actedge = screen_find_active_scredge(win->screen, event->x, event->y);
1239                 
1240                 if (actedge) {
1241                         if (scredge_is_horizontal(actedge))
1242                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1243                         else
1244                                 WM_cursor_set(win, CURSOR_X_MOVE);
1245                 }
1246                 else
1247                         WM_cursor_set(win, CURSOR_STD);
1248         }
1249 }
1250
1251
1252 /* called in wm_event_system.c. sets state vars in screen, cursors */
1253 /* event type is mouse move */
1254 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1255 {
1256         wmWindow *win = CTX_wm_window(C);
1257         
1258         if (win->screen) {
1259                 bScreen *scr = win->screen;
1260                 ScrArea *sa;
1261                 ARegion *ar;
1262                 int oldswin = scr->subwinactive;
1263
1264                 for (sa = scr->areabase.first; sa; sa = sa->next) {
1265                         if (event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1266                                 if (event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1267                                         if (NULL == is_in_area_actionzone(sa, &event->x))
1268                                                 break;
1269                 }
1270                 if (sa) {
1271                         /* make overlap active when mouse over */
1272                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1273                                 if (BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
1274                                         scr->subwinactive = ar->swinid;
1275                                         break;
1276                                 }
1277                         }
1278                 }
1279                 else
1280                         scr->subwinactive = scr->mainwin;
1281                 
1282                 /* check for redraw headers */
1283                 if (oldswin != scr->subwinactive) {
1284
1285                         for (sa = scr->areabase.first; sa; sa = sa->next) {
1286                                 int do_draw = FALSE;
1287                                 
1288                                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1289                                         if (ar->swinid == oldswin || ar->swinid == scr->subwinactive)
1290                                                 do_draw = TRUE;
1291                                 
1292                                 if (do_draw) {
1293                                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1294                                                 if (ar->regiontype == RGN_TYPE_HEADER)
1295                                                         ED_region_tag_redraw(ar);
1296                                 }
1297                         }
1298                 }
1299                 
1300                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1301                 if (scr->subwinactive == scr->mainwin) {
1302                         screen_cursor_set(win, event);
1303                 }
1304                 else {
1305                         /* notifier invokes freeing the buttons... causing a bit too much redraws */
1306                         if (oldswin != scr->subwinactive) {
1307                                 region_cursor_set(win, scr->subwinactive, TRUE);
1308                                 WM_event_add_notifier(C, NC_SCREEN | ND_SUBWINACTIVE, scr);
1309                         }
1310                         else
1311                                 region_cursor_set(win, scr->subwinactive, FALSE);
1312                 }
1313         }
1314 }
1315
1316 int ED_screen_area_active(const bContext *C)
1317 {
1318         wmWindow *win = CTX_wm_window(C);
1319         bScreen *sc = CTX_wm_screen(C);
1320         ScrArea *sa = CTX_wm_area(C);
1321
1322         if (win && sc && sa) {
1323                 AZone *az = is_in_area_actionzone(sa, &win->eventstate->x);
1324                 ARegion *ar;
1325                 
1326                 if (az && az->type == AZONE_REGION)
1327                         return 1;
1328                 
1329                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1330                         if (ar->swinid == sc->subwinactive)
1331                                 return 1;
1332         }
1333         return 0;
1334 }
1335
1336 /* operator call, WM + Window + screen already existed before */
1337 /* Do NOT call in area/region queues! */
1338 void ED_screen_set(bContext *C, bScreen *sc)
1339 {
1340         wmWindowManager *wm = CTX_wm_manager(C);
1341         wmWindow *win = CTX_wm_window(C);
1342         bScreen *oldscreen = CTX_wm_screen(C);
1343         ID *id;
1344         
1345         /* validate screen, it's called with notifier reference */
1346         for (id = CTX_data_main(C)->screen.first; id; id = id->next)
1347                 if (sc == (bScreen *)id)
1348                         break;
1349         if (id == NULL)
1350                 return;
1351         
1352         /* check for valid winid */
1353         if (sc->winid != 0 && sc->winid != win->winid)
1354                 return;
1355         
1356         if (sc->full) {             /* find associated full */
1357                 bScreen *sc1;
1358                 for (sc1 = CTX_data_main(C)->screen.first; sc1; sc1 = sc1->id.next) {
1359                         ScrArea *sa = sc1->areabase.first;
1360                         if (sa->full == sc) {
1361                                 sc = sc1;
1362                                 break;
1363                         }
1364                 }
1365         }
1366         
1367         if (oldscreen != sc) {
1368                 wmTimer *wt = oldscreen->animtimer;
1369                 ScrArea *sa;
1370
1371                 /* remove handlers referencing areas in old screen */
1372                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1373                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1374                 }
1375
1376                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1377                 oldscreen->animtimer = NULL;
1378                 if (wt)
1379                         WM_event_timer_sleep(wm, win, wt, 1);
1380                 
1381                 ED_screen_exit(C, win, oldscreen);
1382                 oldscreen->animtimer = wt;
1383                 
1384                 win->screen = sc;
1385                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1386                 
1387                 /* prevent multiwin errors */
1388                 sc->winid = win->winid;
1389                 
1390                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1391                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1392                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1393                 
1394                 /* makes button hilites work */
1395                 WM_event_add_mousemove(C);
1396         }
1397 }
1398
1399 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1400 {
1401         wmWindow *win;
1402
1403         for (win = wm->windows.first; win; win = win->next)
1404                 if (win->screen == sc)
1405                         return 1;
1406         
1407         return 0;
1408 }
1409
1410 /* only call outside of area/region loops */
1411 void ED_screen_delete(bContext *C, bScreen *sc)
1412 {
1413         Main *bmain = CTX_data_main(C);
1414         wmWindowManager *wm = CTX_wm_manager(C);
1415         wmWindow *win = CTX_wm_window(C);
1416         bScreen *newsc;
1417         int delete = 1;
1418         
1419         /* don't allow deleting temp fullscreens for now */
1420         if (sc->full == SCREENFULL) {
1421                 return;
1422         }
1423         
1424                 
1425         /* screen can only be in use by one window at a time, so as
1426          * long as we are able to find a screen that is unused, we
1427          * can safely assume ours is not in use anywhere an delete it */
1428
1429         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1430                 if (!ed_screen_used(wm, newsc))
1431                         break;
1432         
1433         if (!newsc) {
1434                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1435                         if (!ed_screen_used(wm, newsc))
1436                                 break;
1437         }
1438
1439         if (!newsc)
1440                 return;
1441
1442         ED_screen_set(C, newsc);
1443
1444         if (delete && win->screen != sc)
1445                 BKE_libblock_free(&bmain->screen, sc);
1446 }
1447
1448 /* only call outside of area/region loops */
1449 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1450 {
1451         Main *bmain = CTX_data_main(C);
1452         bScreen *sc;
1453
1454         if (screen == NULL)
1455                 return;
1456         
1457         if (ed_screen_used(CTX_wm_manager(C), screen))
1458                 ED_object_exit_editmode(C, EM_FREEDATA | EM_DO_UNDO);
1459
1460         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1461                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1462                         
1463                         if (scene != sc->scene) {
1464                                 /* all areas endlocalview */
1465                                 // XXX  ScrArea *sa = sc->areabase.first;
1466                                 //      while (sa) {
1467                                 //              endlocalview(sa);
1468                                 //              sa = sa->next;
1469                                 //      }
1470                                 sc->scene = scene;
1471                         }
1472                         
1473                 }
1474         }
1475         
1476         //  copy_view3d_lock(0);        /* space.c */
1477         
1478         /* are there cameras in the views that are not in the scene? */
1479         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1480                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1481                         ScrArea *sa = sc->areabase.first;
1482                         while (sa) {
1483                                 SpaceLink *sl = sa->spacedata.first;
1484                                 while (sl) {
1485                                         if (sl->spacetype == SPACE_VIEW3D) {
1486                                                 View3D *v3d = (View3D *) sl;
1487
1488                                                 BKE_screen_view3d_sync(v3d, scene);
1489
1490                                                 if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1491                                                         v3d->camera = BKE_scene_camera_find(sc->scene);
1492                                                         // XXX if (sc == curscreen) handle_view3d_lock();
1493                                                         if (!v3d->camera) {
1494                                                                 ARegion *ar;
1495                                                                 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
1496                                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1497                                                                                 RegionView3D *rv3d = ar->regiondata;
1498
1499                                                                                 if (rv3d->persp == RV3D_CAMOB)
1500                                                                                         rv3d->persp = RV3D_PERSP;
1501                                                                         }
1502                                                                 }
1503                                                         }
1504                                                 }
1505                                         }
1506                                         sl = sl->next;
1507                                 }
1508                                 sa = sa->next;
1509                         }
1510                 }
1511         }
1512         
1513         CTX_data_scene_set(C, scene);
1514         BKE_scene_set_background(bmain, scene);
1515         DAG_on_visible_update(bmain, FALSE);
1516         
1517         ED_render_engine_changed(bmain);
1518         ED_update_for_newframe(bmain, scene, 1);
1519         
1520         /* complete redraw */
1521         WM_event_add_notifier(C, NC_WINDOW, NULL);
1522         
1523 }
1524
1525 /* only call outside of area/region loops */
1526 void ED_screen_delete_scene(bContext *C, Scene *scene)
1527 {
1528         Main *bmain = CTX_data_main(C);
1529         Scene *newscene;
1530
1531         if (scene->id.prev)
1532                 newscene = scene->id.prev;
1533         else if (scene->id.next)
1534                 newscene = scene->id.next;
1535         else
1536                 return;
1537
1538         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1539
1540         BKE_scene_unlink(bmain, scene, newscene);
1541 }
1542
1543 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1544 {
1545         wmWindow *win = CTX_wm_window(C);
1546         bScreen *screen = CTX_wm_screen(C);
1547         ScrArea *newsa = NULL;
1548
1549         if (!sa || sa->full == NULL) {
1550                 newsa = ED_screen_full_toggle(C, win, sa);
1551         }
1552         
1553         if (!newsa) {
1554                 if (sa->full) {
1555                         /* if this has been called from the temporary info header generated in
1556                          * temp fullscreen layouts, find the correct fullscreen area to change
1557                          * to create a new space inside */
1558                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1559                                 if (!(sa->flag & AREA_TEMP_INFO))
1560                                         break;
1561                         }
1562                 }
1563                 else {
1564                         newsa = sa;
1565                 }
1566         }
1567         
1568         ED_area_newspace(C, newsa, type);
1569         
1570         return newsa;
1571 }
1572
1573 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1574 {
1575         wmWindow *win = CTX_wm_window(C);
1576
1577         ED_area_prevspace(C, sa);
1578         
1579         if (sa->full)
1580                 ED_screen_full_toggle(C, win, sa);
1581 }
1582
1583 /* restore a screen / area back to default operation, after temp fullscreen modes */
1584 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1585 {
1586         wmWindow *win = CTX_wm_window(C);
1587         SpaceLink *sl = sa->spacedata.first;
1588         
1589         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1590          * overlaid on top of a existing setup) then return to the previous space */
1591         
1592         if (sl->next) {
1593                 /* specific checks for space types */
1594
1595                 int sima_restore = 0;
1596
1597                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1598                 if (sl->spacetype == SPACE_IMAGE) {
1599                         SpaceImage *sima = sa->spacedata.first;
1600                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1601                                 sima_restore = 1;
1602                 }
1603
1604                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1605                         SpaceImage *sima = sa->spacedata.first;
1606                         if (sima->flag & SI_PREVSPACE)
1607                                 sima->flag &= ~SI_PREVSPACE;
1608                         if (sima->flag & SI_FULLWINDOW) {
1609                                 sima->flag &= ~SI_FULLWINDOW;
1610                                 ED_screen_full_prevspace(C, sa);
1611                         }
1612                 }
1613                 else if (sl->spacetype == SPACE_FILE) {
1614                         ED_screen_full_prevspace(C, sa);
1615                 }
1616                 else {
1617                         ED_screen_full_toggle(C, win, sa);
1618                 }
1619         }
1620         /* otherwise just tile the area again */
1621         else {
1622                 ED_screen_full_toggle(C, win, sa);
1623         }
1624 }
1625
1626 /* this function toggles: if area is full then the parent will be restored */
1627 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1628 {
1629         bScreen *sc, *oldscreen;
1630         ARegion *ar;
1631
1632         if (sa) {
1633                 /* ensure we don't have a button active anymore, can crash when
1634                  * switching screens with tooltip open because region and tooltip
1635                  * are no longer in the same screen */
1636                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1637                         uiFreeBlocks(C, &ar->uiblocks);
1638                 
1639                 /* prevent hanging header prints */
1640                 ED_area_headerprint(sa, NULL);
1641         }
1642
1643         if (sa && sa->full) {
1644                 ScrArea *old;
1645                 /*short fulltype;*/ /*UNUSED*/
1646
1647                 sc = sa->full;       /* the old screen to restore */
1648                 oldscreen = win->screen; /* the one disappearing */
1649
1650                 /*fulltype = sc->full;*/
1651                 sc->full = 0;
1652
1653                 /* removed: SCREENAUTOPLAY exception here */
1654         
1655                 /* find old area */
1656                 for (old = sc->areabase.first; old; old = old->next)
1657                         if (old->full) break;
1658                 if (old == NULL) {
1659                         if (G.debug & G_DEBUG)
1660                                 printf("%s: something wrong in areafullscreen\n", __func__);
1661                         return NULL;
1662                 }
1663
1664                 area_copy_data(old, sa, 1); /*  1 = swap spacelist */
1665                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1666                 old->full = NULL;
1667
1668                 /* animtimer back */
1669                 sc->animtimer = oldscreen->animtimer;
1670                 oldscreen->animtimer = NULL;
1671
1672                 ED_screen_set(C, sc);
1673
1674                 BKE_screen_free(oldscreen);
1675                 BKE_libblock_free(&CTX_data_main(C)->screen, oldscreen);
1676
1677         }
1678         else {
1679                 ScrArea *newa;
1680                 char newname[MAX_ID_NAME - 2];
1681
1682                 oldscreen = win->screen;
1683
1684                 /* nothing wrong with having only 1 area, as far as I can see...
1685                  * is there only 1 area? */
1686 #if 0
1687                 if (oldscreen->areabase.first == oldscreen->areabase.last)
1688                         return NULL;
1689 #endif
1690
1691                 oldscreen->full = SCREENFULL;
1692                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "full");
1693                 sc = ED_screen_add(win, oldscreen->scene, newname);
1694                 sc->full = SCREENFULL; // XXX
1695
1696                 /* timer */
1697                 sc->animtimer = oldscreen->animtimer;
1698                 oldscreen->animtimer = NULL;
1699
1700                 /* returns the top small area */
1701                 newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1702                 ED_area_newspace(C, newa, SPACE_INFO);
1703
1704                 /* use random area when we have no active one, e.g. when the
1705                  * mouse is outside of the window and we open a file browser */
1706                 if (!sa)
1707                         sa = oldscreen->areabase.first;
1708
1709                 /* copy area */
1710                 newa = newa->prev;
1711                 area_copy_data(newa, sa, 1);  /* 1 = swap spacelist */
1712                 sa->flag |= AREA_TEMP_INFO;
1713
1714                 sa->full = oldscreen;
1715                 newa->full = oldscreen;
1716                 newa->next->full = oldscreen; // XXX
1717
1718                 ED_screen_set(C, sc);
1719         }
1720
1721         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1722         CTX_wm_area_set(C, sc->areabase.first);
1723
1724         return sc->areabase.first;
1725 }
1726
1727 /* update frame rate info for viewport drawing */
1728 void ED_refresh_viewport_fps(bContext *C)
1729 {
1730         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1731         Scene *scene = CTX_data_scene(C);
1732         
1733         /* is anim playback running? */
1734         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1735                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1736                 
1737                 /* if there isn't any info, init it first */
1738                 if (fpsi == NULL)
1739                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1740                 
1741                 /* update the values */
1742                 fpsi->redrawtime = fpsi->lredrawtime;
1743                 fpsi->lredrawtime = animtimer->ltime;
1744         }
1745         else {
1746                 /* playback stopped or shouldn't be running */
1747                 if (scene->fps_info)
1748                         MEM_freeN(scene->fps_info);
1749                 scene->fps_info = NULL;
1750         }
1751 }
1752
1753 /* redraws: uses defines from stime->redraws 
1754  * enable: 1 - forward on, -1 - backwards on, 0 - off
1755  */
1756 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1757 {
1758         bScreen *screen = CTX_wm_screen(C);
1759         wmWindowManager *wm = CTX_wm_manager(C);
1760         wmWindow *win = CTX_wm_window(C);
1761         Scene *scene = CTX_data_scene(C);
1762         bScreen *stopscreen = ED_screen_animation_playing(wm);
1763         
1764         if (stopscreen) {
1765                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1766                 stopscreen->animtimer = NULL;
1767         }
1768         
1769         if (enable) {
1770                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1771                 
1772                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1773                 
1774                 sad->ar = CTX_wm_region(C);
1775                 /* if startframe is larger than current frame, we put currentframe on startframe.
1776                  * note: first frame then is not drawn! (ton) */
1777                 if (PRVRANGEON) {
1778                         if (scene->r.psfra > scene->r.cfra) {
1779                                 sad->sfra = scene->r.cfra;
1780                                 scene->r.cfra = scene->r.psfra;
1781                         }
1782                         else
1783                                 sad->sfra = scene->r.cfra;
1784                 }
1785                 else {
1786                         if (scene->r.sfra > scene->r.cfra) {
1787                                 sad->sfra = scene->r.cfra;
1788                                 scene->r.cfra = scene->r.sfra;
1789                         }
1790                         else
1791                                 sad->sfra = scene->r.cfra;
1792                 }
1793                 sad->redraws = redraws;
1794                 sad->refresh = refresh;
1795                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1796                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1797                 
1798                 screen->animtimer->customdata = sad;
1799                 
1800         }
1801
1802         /* notifier catched by top header, for button */
1803         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1804 }
1805
1806 /* helper for screen_animation_play() - only to be used for TimeLine */
1807 static ARegion *time_top_left_3dwindow(bScreen *screen)
1808 {
1809         ARegion *aret = NULL;
1810         ScrArea *sa;
1811         int min = 10000;
1812         
1813         for (sa = screen->areabase.first; sa; sa = sa->next) {
1814                 if (sa->spacetype == SPACE_VIEW3D) {
1815                         ARegion *ar;
1816                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1817                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1818                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1819                                                 aret = ar;
1820                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1821                                         }
1822                                 }
1823                         }
1824                 }
1825         }
1826
1827         return aret;
1828 }
1829
1830 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1831 {
1832         if (screen && screen->animtimer) {
1833                 wmTimer *wt = screen->animtimer;
1834                 ScreenAnimData *sad = wt->customdata;
1835                 
1836                 sad->redraws = redraws;
1837                 sad->refresh = refresh;
1838                 sad->ar = NULL;
1839                 if (redraws & TIME_REGION)
1840                         sad->ar = time_top_left_3dwindow(screen);
1841         }
1842 }
1843
1844 /* results in fully updated anim system
1845  * screen can be NULL */
1846 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
1847 {
1848         wmWindowManager *wm = bmain->wm.first;
1849         wmWindow *window;
1850         int layers = 0;
1851
1852 #ifdef DURIAN_CAMERA_SWITCH
1853         void *camera = BKE_scene_camera_switch_find(scene);
1854         if (camera && scene->camera != camera) {
1855                 bScreen *sc;
1856                 scene->camera = camera;
1857                 /* are there cameras in the views that are not in the scene? */
1858                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1859                         BKE_screen_view3d_scene_sync(sc);
1860                 }
1861         }
1862 #endif
1863
1864         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1865         
1866         /* update animated image textures for gpu, etc,
1867          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
1868         ED_image_update_frame(bmain, scene->r.cfra);
1869
1870         ED_clip_update_frame(bmain, scene->r.cfra);
1871
1872         /* get layers from all windows */
1873         for (window = wm->windows.first; window; window = window->next)
1874                 layers |= BKE_screen_visible_layers(window->screen, scene);
1875
1876         /* this function applies the changes too */
1877         BKE_scene_update_for_newframe(bmain, scene, layers); /* BKE_scene.h */
1878         
1879         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1880         //      audiostream_scrub( CFRA );
1881         
1882         /* 3d window, preview */
1883         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1884         
1885         /* all movie/sequence images */
1886         //BIF_image_update_frame();
1887         
1888         /* composite */
1889         if (scene->use_nodes && scene->nodetree)
1890                 ntreeCompositTagAnimated(scene->nodetree);
1891         
1892         /* update animated texture nodes */
1893         {
1894                 Tex *tex;
1895                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
1896                         if (tex->use_nodes && tex->nodetree) {
1897                                 ntreeTexTagAnimated(tex->nodetree);
1898                         }
1899         }
1900         
1901 }
1902
1903