7c22dff1b011451757936c059f812da9795c23db
[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         Main *bmain = CTX_data_main(C);
1341         wmWindowManager *wm = CTX_wm_manager(C);
1342         wmWindow *win = CTX_wm_window(C);
1343         bScreen *oldscreen = CTX_wm_screen(C);
1344         ID *id;
1345         
1346         /* validate screen, it's called with notifier reference */
1347         for (id = bmain->screen.first; id; id = id->next)
1348                 if (sc == (bScreen *)id)
1349                         break;
1350         if (id == NULL)
1351                 return;
1352         
1353         /* check for valid winid */
1354         if (sc->winid != 0 && sc->winid != win->winid)
1355                 return;
1356         
1357         if (sc->full) {             /* find associated full */
1358                 bScreen *sc1;
1359                 for (sc1 = bmain->screen.first; sc1; sc1 = sc1->id.next) {
1360                         ScrArea *sa = sc1->areabase.first;
1361                         if (sa->full == sc) {
1362                                 sc = sc1;
1363                                 break;
1364                         }
1365                 }
1366         }
1367         
1368         if (oldscreen != sc) {
1369                 wmTimer *wt = oldscreen->animtimer;
1370                 ScrArea *sa;
1371
1372                 /* remove handlers referencing areas in old screen */
1373                 for (sa = oldscreen->areabase.first; sa; sa = sa->next) {
1374                         WM_event_remove_area_handler(&win->modalhandlers, sa);
1375                 }
1376
1377                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1378                 oldscreen->animtimer = NULL;
1379                 if (wt)
1380                         WM_event_timer_sleep(wm, win, wt, 1);
1381                 
1382                 ED_screen_exit(C, win, oldscreen);
1383                 oldscreen->animtimer = wt;
1384                 
1385                 win->screen = sc;
1386                 CTX_wm_window_set(C, win);  // stores C->wm.screen... hrmf
1387                 
1388                 /* prevent multiwin errors */
1389                 sc->winid = win->winid;
1390                 
1391                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1392                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1393                 WM_event_add_notifier(C, NC_SCREEN | ND_SCREENSET, sc);
1394                 
1395                 /* makes button hilites work */
1396                 WM_event_add_mousemove(C);
1397         }
1398 }
1399
1400 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1401 {
1402         wmWindow *win;
1403
1404         for (win = wm->windows.first; win; win = win->next)
1405                 if (win->screen == sc)
1406                         return 1;
1407         
1408         return 0;
1409 }
1410
1411 /* only call outside of area/region loops */
1412 void ED_screen_delete(bContext *C, bScreen *sc)
1413 {
1414         Main *bmain = CTX_data_main(C);
1415         wmWindowManager *wm = CTX_wm_manager(C);
1416         wmWindow *win = CTX_wm_window(C);
1417         bScreen *newsc;
1418         int delete = 1;
1419         
1420         /* don't allow deleting temp fullscreens for now */
1421         if (sc->full == SCREENFULL) {
1422                 return;
1423         }
1424         
1425                 
1426         /* screen can only be in use by one window at a time, so as
1427          * long as we are able to find a screen that is unused, we
1428          * can safely assume ours is not in use anywhere an delete it */
1429
1430         for (newsc = sc->id.prev; newsc; newsc = newsc->id.prev)
1431                 if (!ed_screen_used(wm, newsc))
1432                         break;
1433         
1434         if (!newsc) {
1435                 for (newsc = sc->id.next; newsc; newsc = newsc->id.next)
1436                         if (!ed_screen_used(wm, newsc))
1437                                 break;
1438         }
1439
1440         if (!newsc)
1441                 return;
1442
1443         ED_screen_set(C, newsc);
1444
1445         if (delete && win->screen != sc)
1446                 BKE_libblock_free(&bmain->screen, sc);
1447 }
1448
1449 /* only call outside of area/region loops */
1450 void ED_screen_set_scene(bContext *C, bScreen *screen, Scene *scene)
1451 {
1452         Main *bmain = CTX_data_main(C);
1453         bScreen *sc;
1454
1455         if (screen == NULL)
1456                 return;
1457         
1458         if (ed_screen_used(CTX_wm_manager(C), screen))
1459                 ED_object_exit_editmode(C, EM_FREEDATA | EM_DO_UNDO);
1460
1461         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1462                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1463                         
1464                         if (scene != sc->scene) {
1465                                 /* all areas endlocalview */
1466                                 // XXX  ScrArea *sa = sc->areabase.first;
1467                                 //      while (sa) {
1468                                 //              endlocalview(sa);
1469                                 //              sa = sa->next;
1470                                 //      }
1471                                 sc->scene = scene;
1472                         }
1473                         
1474                 }
1475         }
1476         
1477         //  copy_view3d_lock(0);        /* space.c */
1478         
1479         /* are there cameras in the views that are not in the scene? */
1480         for (sc = CTX_data_main(C)->screen.first; sc; sc = sc->id.next) {
1481                 if ((U.flag & USER_SCENEGLOBAL) || sc == screen) {
1482                         ScrArea *sa = sc->areabase.first;
1483                         while (sa) {
1484                                 SpaceLink *sl = sa->spacedata.first;
1485                                 while (sl) {
1486                                         if (sl->spacetype == SPACE_VIEW3D) {
1487                                                 View3D *v3d = (View3D *) sl;
1488
1489                                                 BKE_screen_view3d_sync(v3d, scene);
1490
1491                                                 if (!v3d->camera || !BKE_scene_base_find(scene, v3d->camera)) {
1492                                                         v3d->camera = BKE_scene_camera_find(sc->scene);
1493                                                         // XXX if (sc == curscreen) handle_view3d_lock();
1494                                                         if (!v3d->camera) {
1495                                                                 ARegion *ar;
1496                                                                 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
1497                                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1498                                                                                 RegionView3D *rv3d = ar->regiondata;
1499
1500                                                                                 if (rv3d->persp == RV3D_CAMOB)
1501                                                                                         rv3d->persp = RV3D_PERSP;
1502                                                                         }
1503                                                                 }
1504                                                         }
1505                                                 }
1506                                         }
1507                                         sl = sl->next;
1508                                 }
1509                                 sa = sa->next;
1510                         }
1511                 }
1512         }
1513         
1514         CTX_data_scene_set(C, scene);
1515         BKE_scene_set_background(bmain, scene);
1516         DAG_on_visible_update(bmain, FALSE);
1517         
1518         ED_render_engine_changed(bmain);
1519         ED_update_for_newframe(bmain, scene, 1);
1520         
1521         /* complete redraw */
1522         WM_event_add_notifier(C, NC_WINDOW, NULL);
1523         
1524 }
1525
1526 /* only call outside of area/region loops */
1527 void ED_screen_delete_scene(bContext *C, Scene *scene)
1528 {
1529         Main *bmain = CTX_data_main(C);
1530         Scene *newscene;
1531
1532         if (scene->id.prev)
1533                 newscene = scene->id.prev;
1534         else if (scene->id.next)
1535                 newscene = scene->id.next;
1536         else
1537                 return;
1538
1539         ED_screen_set_scene(C, CTX_wm_screen(C), newscene);
1540
1541         BKE_scene_unlink(bmain, scene, newscene);
1542 }
1543
1544 ScrArea *ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1545 {
1546         wmWindow *win = CTX_wm_window(C);
1547         bScreen *screen = CTX_wm_screen(C);
1548         ScrArea *newsa = NULL;
1549
1550         if (!sa || sa->full == NULL) {
1551                 newsa = ED_screen_full_toggle(C, win, sa);
1552         }
1553         
1554         if (!newsa) {
1555                 if (sa->full) {
1556                         /* if this has been called from the temporary info header generated in
1557                          * temp fullscreen layouts, find the correct fullscreen area to change
1558                          * to create a new space inside */
1559                         for (newsa = screen->areabase.first; newsa; newsa = newsa->next) {
1560                                 if (!(sa->flag & AREA_TEMP_INFO))
1561                                         break;
1562                         }
1563                 }
1564                 else {
1565                         newsa = sa;
1566                 }
1567         }
1568         
1569         ED_area_newspace(C, newsa, type);
1570         
1571         return newsa;
1572 }
1573
1574 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1575 {
1576         wmWindow *win = CTX_wm_window(C);
1577
1578         ED_area_prevspace(C, sa);
1579         
1580         if (sa->full)
1581                 ED_screen_full_toggle(C, win, sa);
1582 }
1583
1584 /* restore a screen / area back to default operation, after temp fullscreen modes */
1585 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1586 {
1587         wmWindow *win = CTX_wm_window(C);
1588         SpaceLink *sl = sa->spacedata.first;
1589         
1590         /* if fullscreen area has a secondary space (such as a file browser or fullscreen render 
1591          * overlaid on top of a existing setup) then return to the previous space */
1592         
1593         if (sl->next) {
1594                 /* specific checks for space types */
1595
1596                 int sima_restore = 0;
1597
1598                 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1599                 if (sl->spacetype == SPACE_IMAGE) {
1600                         SpaceImage *sima = sa->spacedata.first;
1601                         if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1602                                 sima_restore = 1;
1603                 }
1604
1605                 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1606                         SpaceImage *sima = sa->spacedata.first;
1607                         if (sima->flag & SI_PREVSPACE)
1608                                 sima->flag &= ~SI_PREVSPACE;
1609                         if (sima->flag & SI_FULLWINDOW) {
1610                                 sima->flag &= ~SI_FULLWINDOW;
1611                                 ED_screen_full_prevspace(C, sa);
1612                         }
1613                 }
1614                 else if (sl->spacetype == SPACE_FILE) {
1615                         ED_screen_full_prevspace(C, sa);
1616                 }
1617                 else {
1618                         ED_screen_full_toggle(C, win, sa);
1619                 }
1620         }
1621         /* otherwise just tile the area again */
1622         else {
1623                 ED_screen_full_toggle(C, win, sa);
1624         }
1625 }
1626
1627 /* this function toggles: if area is full then the parent will be restored */
1628 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1629 {
1630         bScreen *sc, *oldscreen;
1631         ARegion *ar;
1632
1633         if (sa) {
1634                 /* ensure we don't have a button active anymore, can crash when
1635                  * switching screens with tooltip open because region and tooltip
1636                  * are no longer in the same screen */
1637                 for (ar = sa->regionbase.first; ar; ar = ar->next)
1638                         uiFreeBlocks(C, &ar->uiblocks);
1639                 
1640                 /* prevent hanging header prints */
1641                 ED_area_headerprint(sa, NULL);
1642         }
1643
1644         if (sa && sa->full) {
1645                 ScrArea *old;
1646                 /*short fulltype;*/ /*UNUSED*/
1647
1648                 sc = sa->full;       /* the old screen to restore */
1649                 oldscreen = win->screen; /* the one disappearing */
1650
1651                 /*fulltype = sc->full;*/
1652                 sc->full = 0;
1653
1654                 /* removed: SCREENAUTOPLAY exception here */
1655         
1656                 /* find old area */
1657                 for (old = sc->areabase.first; old; old = old->next)
1658                         if (old->full) break;
1659                 if (old == NULL) {
1660                         if (G.debug & G_DEBUG)
1661                                 printf("%s: something wrong in areafullscreen\n", __func__);
1662                         return NULL;
1663                 }
1664
1665                 area_copy_data(old, sa, 1); /*  1 = swap spacelist */
1666                 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1667                 old->full = NULL;
1668
1669                 /* animtimer back */
1670                 sc->animtimer = oldscreen->animtimer;
1671                 oldscreen->animtimer = NULL;
1672
1673                 ED_screen_set(C, sc);
1674
1675                 BKE_screen_free(oldscreen);
1676                 BKE_libblock_free(&CTX_data_main(C)->screen, oldscreen);
1677
1678         }
1679         else {
1680                 ScrArea *newa;
1681                 char newname[MAX_ID_NAME - 2];
1682
1683                 oldscreen = win->screen;
1684
1685                 /* nothing wrong with having only 1 area, as far as I can see...
1686                  * is there only 1 area? */
1687 #if 0
1688                 if (oldscreen->areabase.first == oldscreen->areabase.last)
1689                         return NULL;
1690 #endif
1691
1692                 oldscreen->full = SCREENFULL;
1693                 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name + 2, "full");
1694                 sc = ED_screen_add(win, oldscreen->scene, newname);
1695                 sc->full = SCREENFULL; // XXX
1696
1697                 /* timer */
1698                 sc->animtimer = oldscreen->animtimer;
1699                 oldscreen->animtimer = NULL;
1700
1701                 /* returns the top small area */
1702                 newa = area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1703                 ED_area_newspace(C, newa, SPACE_INFO);
1704
1705                 /* use random area when we have no active one, e.g. when the
1706                  * mouse is outside of the window and we open a file browser */
1707                 if (!sa)
1708                         sa = oldscreen->areabase.first;
1709
1710                 /* copy area */
1711                 newa = newa->prev;
1712                 area_copy_data(newa, sa, 1);  /* 1 = swap spacelist */
1713                 sa->flag |= AREA_TEMP_INFO;
1714
1715                 sa->full = oldscreen;
1716                 newa->full = oldscreen;
1717                 newa->next->full = oldscreen; // XXX
1718
1719                 ED_screen_set(C, sc);
1720         }
1721
1722         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1723         CTX_wm_area_set(C, sc->areabase.first);
1724
1725         return sc->areabase.first;
1726 }
1727
1728 /* update frame rate info for viewport drawing */
1729 void ED_refresh_viewport_fps(bContext *C)
1730 {
1731         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1732         Scene *scene = CTX_data_scene(C);
1733         
1734         /* is anim playback running? */
1735         if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1736                 ScreenFrameRateInfo *fpsi = scene->fps_info;
1737                 
1738                 /* if there isn't any info, init it first */
1739                 if (fpsi == NULL)
1740                         fpsi = scene->fps_info = MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1741                 
1742                 /* update the values */
1743                 fpsi->redrawtime = fpsi->lredrawtime;
1744                 fpsi->lredrawtime = animtimer->ltime;
1745         }
1746         else {
1747                 /* playback stopped or shouldn't be running */
1748                 if (scene->fps_info)
1749                         MEM_freeN(scene->fps_info);
1750                 scene->fps_info = NULL;
1751         }
1752 }
1753
1754 /* redraws: uses defines from stime->redraws 
1755  * enable: 1 - forward on, -1 - backwards on, 0 - off
1756  */
1757 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1758 {
1759         bScreen *screen = CTX_wm_screen(C);
1760         wmWindowManager *wm = CTX_wm_manager(C);
1761         wmWindow *win = CTX_wm_window(C);
1762         Scene *scene = CTX_data_scene(C);
1763         bScreen *stopscreen = ED_screen_animation_playing(wm);
1764         
1765         if (stopscreen) {
1766                 WM_event_remove_timer(wm, win, stopscreen->animtimer);
1767                 stopscreen->animtimer = NULL;
1768         }
1769         
1770         if (enable) {
1771                 ScreenAnimData *sad = MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1772                 
1773                 screen->animtimer = WM_event_add_timer(wm, win, TIMER0, (1.0 / FPS));
1774                 
1775                 sad->ar = CTX_wm_region(C);
1776                 /* if startframe is larger than current frame, we put currentframe on startframe.
1777                  * note: first frame then is not drawn! (ton) */
1778                 if (PRVRANGEON) {
1779                         if (scene->r.psfra > scene->r.cfra) {
1780                                 sad->sfra = scene->r.cfra;
1781                                 scene->r.cfra = scene->r.psfra;
1782                         }
1783                         else
1784                                 sad->sfra = scene->r.cfra;
1785                 }
1786                 else {
1787                         if (scene->r.sfra > scene->r.cfra) {
1788                                 sad->sfra = scene->r.cfra;
1789                                 scene->r.cfra = scene->r.sfra;
1790                         }
1791                         else
1792                                 sad->sfra = scene->r.cfra;
1793                 }
1794                 sad->redraws = redraws;
1795                 sad->refresh = refresh;
1796                 sad->flag |= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
1797                 sad->flag |= (sync == 0) ? ANIMPLAY_FLAG_NO_SYNC : (sync == 1) ? ANIMPLAY_FLAG_SYNC : 0;
1798                 
1799                 screen->animtimer->customdata = sad;
1800                 
1801         }
1802
1803         /* notifier catched by top header, for button */
1804         WM_event_add_notifier(C, NC_SCREEN | ND_ANIMPLAY, NULL);
1805 }
1806
1807 /* helper for screen_animation_play() - only to be used for TimeLine */
1808 static ARegion *time_top_left_3dwindow(bScreen *screen)
1809 {
1810         ARegion *aret = NULL;
1811         ScrArea *sa;
1812         int min = 10000;
1813         
1814         for (sa = screen->areabase.first; sa; sa = sa->next) {
1815                 if (sa->spacetype == SPACE_VIEW3D) {
1816                         ARegion *ar;
1817                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1818                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1819                                         if (ar->winrct.xmin - ar->winrct.ymin < min) {
1820                                                 aret = ar;
1821                                                 min = ar->winrct.xmin - ar->winrct.ymin;
1822                                         }
1823                                 }
1824                         }
1825                 }
1826         }
1827
1828         return aret;
1829 }
1830
1831 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1832 {
1833         if (screen && screen->animtimer) {
1834                 wmTimer *wt = screen->animtimer;
1835                 ScreenAnimData *sad = wt->customdata;
1836                 
1837                 sad->redraws = redraws;
1838                 sad->refresh = refresh;
1839                 sad->ar = NULL;
1840                 if (redraws & TIME_REGION)
1841                         sad->ar = time_top_left_3dwindow(screen);
1842         }
1843 }
1844
1845 /* results in fully updated anim system
1846  * screen can be NULL */
1847 void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute))
1848 {
1849         wmWindowManager *wm = bmain->wm.first;
1850         wmWindow *window;
1851         int layers = 0;
1852
1853 #ifdef DURIAN_CAMERA_SWITCH
1854         void *camera = BKE_scene_camera_switch_find(scene);
1855         if (camera && scene->camera != camera) {
1856                 bScreen *sc;
1857                 scene->camera = camera;
1858                 /* are there cameras in the views that are not in the scene? */
1859                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1860                         BKE_screen_view3d_scene_sync(sc);
1861                 }
1862         }
1863 #endif
1864
1865         //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
1866         
1867         /* update animated image textures for gpu, etc,
1868          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
1869         ED_image_update_frame(bmain, scene->r.cfra);
1870
1871         ED_clip_update_frame(bmain, scene->r.cfra);
1872
1873         /* get layers from all windows */
1874         for (window = wm->windows.first; window; window = window->next)
1875                 layers |= BKE_screen_visible_layers(window->screen, scene);
1876
1877         /* this function applies the changes too */
1878         BKE_scene_update_for_newframe(bmain, scene, layers); /* BKE_scene.h */
1879         
1880         //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB)) 
1881         //      audiostream_scrub( CFRA );
1882         
1883         /* 3d window, preview */
1884         //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1885         
1886         /* all movie/sequence images */
1887         //BIF_image_update_frame();
1888         
1889         /* composite */
1890         if (scene->use_nodes && scene->nodetree)
1891                 ntreeCompositTagAnimated(scene->nodetree);
1892         
1893         /* update animated texture nodes */
1894         {
1895                 Tex *tex;
1896                 for (tex = bmain->tex.first; tex; tex = tex->id.next)
1897                         if (tex->use_nodes && tex->nodetree) {
1898                                 ntreeTexTagAnimated(tex->nodetree);
1899                         }
1900         }
1901         
1902 }
1903
1904