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