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