d78fdbbae6e21092f964bd4a3f99c3b940d19bd7
[blender.git] / source / blender / editors / screen / screen_edit.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <string.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_vec_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34
35 #include "BLI_blenlib.h"
36
37 #include "BKE_context.h"
38 #include "BKE_global.h"
39 #include "BKE_library.h"
40 #include "BKE_main.h"
41 #include "BKE_screen.h"
42 #include "BKE_utildefines.h"
43
44 #include "BIF_gl.h"
45 #include "BIF_glutil.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_screen.h"
51 #include "ED_screen_types.h"
52
53 #include "wm_subwindow.h"
54
55 #include "screen_intern.h"      /* own module include */
56
57
58 /* ******************* screen vert, edge, area managing *********************** */
59
60 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
61 {
62         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
63         sv->vec.x= x;
64         sv->vec.y= y;
65         
66         BLI_addtail(&sc->vertbase, sv);
67         return sv;
68 }
69
70 static void sortscrvert(ScrVert **v1, ScrVert **v2)
71 {
72         ScrVert *tmp;
73         
74         if (*v1 > *v2) {
75                 tmp= *v1;
76                 *v1= *v2;
77                 *v2= tmp;       
78         }
79 }
80
81 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
82 {
83         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
84         
85         sortscrvert(&v1, &v2);
86         se->v1= v1;
87         se->v2= v2;
88         
89         BLI_addtail(&sc->edgebase, se);
90         return se;
91 }
92
93
94 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
95 {
96         ScrEdge *se;
97         
98         sortscrvert(&v1, &v2);
99         for (se= sc->edgebase.first; se; se= se->next)
100                 if(se->v1==v1 && se->v2==v2)
101                         return se;
102         
103         return NULL;
104 }
105
106 void removedouble_scrverts(bScreen *sc)
107 {
108         ScrVert *v1, *verg;
109         ScrEdge *se;
110         ScrArea *sa;
111         
112         verg= sc->vertbase.first;
113         while(verg) {
114                 if(verg->newv==NULL) {  /* !!! */
115                         v1= verg->next;
116                         while(v1) {
117                                 if(v1->newv==NULL) {    /* !?! */
118                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
119                                                 /* printf("doublevert\n"); */
120                                                 v1->newv= verg;
121                                         }
122                                 }
123                                 v1= v1->next;
124                         }
125                 }
126                 verg= verg->next;
127         }
128
129         /* replace pointers in edges and faces */
130         se= sc->edgebase.first;
131         while(se) {
132                 if(se->v1->newv) se->v1= se->v1->newv;
133                 if(se->v2->newv) se->v2= se->v2->newv;
134                 /* edges changed: so.... */
135                 sortscrvert(&(se->v1), &(se->v2));
136                 se= se->next;
137         }
138         sa= sc->areabase.first;
139         while(sa) {
140                 if(sa->v1->newv) sa->v1= sa->v1->newv;
141                 if(sa->v2->newv) sa->v2= sa->v2->newv;
142                 if(sa->v3->newv) sa->v3= sa->v3->newv;
143                 if(sa->v4->newv) sa->v4= sa->v4->newv;
144                 sa= sa->next;
145         }
146
147         /* remove */
148         verg= sc->vertbase.first;
149         while(verg) {
150                 v1= verg->next;
151                 if(verg->newv) {
152                         BLI_remlink(&sc->vertbase, verg);
153                         MEM_freeN(verg);
154                 }
155                 verg= v1;
156         }
157
158 }
159
160 void removenotused_scrverts(bScreen *sc)
161 {
162         ScrVert *sv, *svn;
163         ScrEdge *se;
164         
165         /* we assume edges are ok */
166         
167         se= sc->edgebase.first;
168         while(se) {
169                 se->v1->flag= 1;
170                 se->v2->flag= 1;
171                 se= se->next;
172         }
173         
174         sv= sc->vertbase.first;
175         while(sv) {
176                 svn= sv->next;
177                 if(sv->flag==0) {
178                         BLI_remlink(&sc->vertbase, sv);
179                         MEM_freeN(sv);
180                 }
181                 else sv->flag= 0;
182                 sv= svn;
183         }
184 }
185
186 void removedouble_scredges(bScreen *sc)
187 {
188         ScrEdge *verg, *se, *sn;
189         
190         /* compare */
191         verg= sc->edgebase.first;
192         while(verg) {
193                 se= verg->next;
194                 while(se) {
195                         sn= se->next;
196                         if(verg->v1==se->v1 && verg->v2==se->v2) {
197                                 BLI_remlink(&sc->edgebase, se);
198                                 MEM_freeN(se);
199                         }
200                         se= sn;
201                 }
202                 verg= verg->next;
203         }
204 }
205
206 void removenotused_scredges(bScreen *sc)
207 {
208         ScrEdge *se, *sen;
209         ScrArea *sa;
210         int a=0;
211         
212         /* sets flags when edge is used in area */
213         sa= sc->areabase.first;
214         while(sa) {
215                 se= screen_findedge(sc, sa->v1, sa->v2);
216                 if(se==0) printf("error: area %d edge 1 doesn't exist\n", a);
217                 else se->flag= 1;
218                 se= screen_findedge(sc, sa->v2, sa->v3);
219                 if(se==0) printf("error: area %d edge 2 doesn't exist\n", a);
220                 else se->flag= 1;
221                 se= screen_findedge(sc, sa->v3, sa->v4);
222                 if(se==0) printf("error: area %d edge 3 doesn't exist\n", a);
223                 else se->flag= 1;
224                 se= screen_findedge(sc, sa->v4, sa->v1);
225                 if(se==0) printf("error: area %d edge 4 doesn't exist\n", a);
226                 else se->flag= 1;
227                 sa= sa->next;
228                 a++;
229         }
230         se= sc->edgebase.first;
231         while(se) {
232                 sen= se->next;
233                 if(se->flag==0) {
234                         BLI_remlink(&sc->edgebase, se);
235                         MEM_freeN(se);
236                 }
237                 else se->flag= 0;
238                 se= sen;
239         }
240 }
241
242 int scredge_is_horizontal(ScrEdge *se)
243 {
244         return (se->v1->vec.y == se->v2->vec.y);
245 }
246
247 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
248 {
249         ScrEdge *se;
250         
251         for (se= sc->edgebase.first; se; se= se->next) {
252                 if (scredge_is_horizontal(se)) {
253                         short min, max;
254                         min= MIN2(se->v1->vec.x, se->v2->vec.x);
255                         max= MAX2(se->v1->vec.x, se->v2->vec.x);
256                         
257                         if (abs(my-se->v1->vec.y)<=2 && mx>=min && mx<=max)
258                                 return se;
259                 } 
260                 else {
261                         short min, max;
262                         min= MIN2(se->v1->vec.y, se->v2->vec.y);
263                         max= MAX2(se->v1->vec.y, se->v2->vec.y);
264                         
265                         if (abs(mx-se->v1->vec.x)<=2 && my>=min && my<=max)
266                                 return se;
267                 }
268         }
269         
270         return NULL;
271 }
272
273
274
275 /* adds no space data */
276 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
277 {
278         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
279         sa->v1= v1;
280         sa->v2= v2;
281         sa->v3= v3;
282         sa->v4= v4;
283         sa->headertype= headertype;
284         sa->spacetype= sa->butspacetype= spacetype;
285         
286         BLI_addtail(&sc->areabase, sa);
287         
288         return sa;
289 }
290
291 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
292 {
293         
294         ED_area_exit(C, sa);
295         
296         BKE_screen_area_free(sa);
297         
298         BLI_remlink(&sc->areabase, sa);
299         MEM_freeN(sa);
300 }
301
302 /* return 0: no split possible */
303 /* else return (integer) screencoordinate split point */
304 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
305 {
306         short x, y;
307         
308         // area big enough?
309         if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
310         if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
311         
312         // to be sure
313         if(fac<0.0) fac= 0.0;
314         if(fac>1.0) fac= 1.0;
315         
316         if(dir=='h') {
317                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
318                 
319                 if(y- sa->v1->vec.y < AREAMINY) 
320                         y= sa->v1->vec.y+ AREAMINY;
321                 else if(sa->v2->vec.y- y < AREAMINY) 
322                         y= sa->v2->vec.y- AREAMINY;
323                 else y-= (y % AREAGRID);
324                 
325                 return y;
326         }
327         else {
328                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
329                 
330                 if(x- sa->v1->vec.x < AREAMINX) 
331                         x= sa->v1->vec.x+ AREAMINX;
332                 else if(sa->v4->vec.x- x < AREAMINX) 
333                         x= sa->v4->vec.x- AREAMINX;
334                 else x-= (x % AREAGRID);
335                 
336                 return x;
337         }
338 }
339
340 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
341 {
342         ScrArea *newa=NULL;
343         ScrVert *sv1, *sv2;
344         short split;
345         
346         if(sa==NULL) return NULL;
347         
348         split= testsplitpoint(win, sa, dir, fac);
349         if(split==0) return NULL;
350         
351         if(dir=='h') {
352                 /* new vertices */
353                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
354                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
355                 
356                 /* new edges */
357                 screen_addedge(sc, sa->v1, sv1);
358                 screen_addedge(sc, sv1, sa->v2);
359                 screen_addedge(sc, sa->v3, sv2);
360                 screen_addedge(sc, sv2, sa->v4);
361                 screen_addedge(sc, sv1, sv2);
362                 
363                 /* new areas: top */
364                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
365                 area_copy_data(newa, sa, 0);
366                 
367                 /* area below */
368                 sa->v2= sv1;
369                 sa->v3= sv2;
370                 
371         }
372         else {
373                 /* new vertices */
374                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
375                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
376                 
377                 /* new edges */
378                 screen_addedge(sc, sa->v1, sv1);
379                 screen_addedge(sc, sv1, sa->v4);
380                 screen_addedge(sc, sa->v2, sv2);
381                 screen_addedge(sc, sv2, sa->v3);
382                 screen_addedge(sc, sv1, sv2);
383                 
384                 /* new areas: left */
385                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
386                 area_copy_data(newa, sa, 0);
387                 
388                 /* area right */
389                 sa->v1= sv1;
390                 sa->v2= sv2;
391         }
392         
393         /* remove double vertices en edges */
394         removedouble_scrverts(sc);
395         removedouble_scredges(sc);
396         removenotused_scredges(sc);
397         
398         return newa;
399 }
400
401 /* empty screen, with 1 dummy area without spacedata */
402 /* uses window size */
403 bScreen *screen_add(wmWindow *win, Scene *scene, char *name)
404 {
405         bScreen *sc;
406         ScrVert *sv1, *sv2, *sv3, *sv4;
407         
408         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
409         sc->scene= scene;
410         sc->do_refresh= 1;
411         
412         sv1= screen_addvert(sc, 0, 0);
413         sv2= screen_addvert(sc, 0, win->sizey-1);
414         sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
415         sv4= screen_addvert(sc, win->sizex-1, 0);
416         
417         screen_addedge(sc, sv1, sv2);
418         screen_addedge(sc, sv2, sv3);
419         screen_addedge(sc, sv3, sv4);
420         screen_addedge(sc, sv4, sv1);
421         
422         /* dummy type, no spacedata */
423         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
424                 
425         return sc;
426 }
427
428 static void screen_copy(bScreen *to, bScreen *from)
429 {
430         ScrVert *s1, *s2;
431         ScrEdge *se;
432         ScrArea *sa, *saf;
433         
434         /* free contents of 'to', is from blenkernel screen.c */
435         free_screen(to);
436         
437         BLI_duplicatelist(&to->vertbase, &from->vertbase);
438         BLI_duplicatelist(&to->edgebase, &from->edgebase);
439         BLI_duplicatelist(&to->areabase, &from->areabase);
440         to->regionbase.first= to->regionbase.last= NULL;
441         
442         s2= to->vertbase.first;
443         for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
444                 s1->newv= s2;
445         }
446         
447         for(se= to->edgebase.first; se; se= se->next) {
448                 se->v1= se->v1->newv;
449                 se->v2= se->v2->newv;
450                 sortscrvert(&(se->v1), &(se->v2));
451         }
452         
453         saf= from->areabase.first;
454         for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
455                 sa->v1= sa->v1->newv;
456                 sa->v2= sa->v2->newv;
457                 sa->v3= sa->v3->newv;
458                 sa->v4= sa->v4->newv;
459                 
460                 sa->spacedata.first= sa->spacedata.last= NULL;
461                 sa->regionbase.first= sa->regionbase.last= NULL;
462                 sa->actionzones.first= sa->actionzones.last= NULL;
463                 sa->scriptlink.totscript= 0;
464                 
465                 area_copy_data(sa, saf, 0);
466         }
467         
468         /* put at zero (needed?) */
469         for(s1= from->vertbase.first; s1; s1= s1->next)
470                 s1->newv= NULL;
471
472 }
473
474
475 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
476 /* -1 = not valid check */
477 /* used with join operator */
478 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
479 {
480         ScrVert *sav1, *sav2, *sav3, *sav4;
481         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
482
483         if(sa==NULL || sb==NULL) return -1;
484
485         sav1= sa->v1;
486         sav2= sa->v2;
487         sav3= sa->v3;
488         sav4= sa->v4;
489         sbv1= sb->v1;
490         sbv2= sb->v2;
491         sbv3= sb->v3;
492         sbv4= sb->v4;
493         
494         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
495                 return 0;
496         }
497         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
498                 return 1;
499         }
500         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
501                 return 2;
502         }
503         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
504                 return 3;
505         }
506         
507         return -1;
508 }
509
510 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
511 *       used by the split, join operators
512 */
513 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
514 {
515         int dir;
516         
517         dir = area_getorientation(scr, sa1, sa2);
518         /*printf("dir is : %i \n", dir);*/
519         
520         if (dir < 0)
521         {
522                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
523                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
524                 return 0;
525         }
526         
527         if(dir == 0) {
528                 sa1->v1= sa2->v1;
529                 sa1->v2= sa2->v2;
530                 screen_addedge(scr, sa1->v2, sa1->v3);
531                 screen_addedge(scr, sa1->v1, sa1->v4);
532         }
533         else if(dir == 1) {
534                 sa1->v2= sa2->v2;
535                 sa1->v3= sa2->v3;
536                 screen_addedge(scr, sa1->v1, sa1->v2);
537                 screen_addedge(scr, sa1->v3, sa1->v4);
538         }
539         else if(dir == 2) {
540                 sa1->v3= sa2->v3;
541                 sa1->v4= sa2->v4;
542                 screen_addedge(scr, sa1->v2, sa1->v3);
543                 screen_addedge(scr, sa1->v1, sa1->v4);
544         }
545         else if(dir == 3) {
546                 sa1->v1= sa2->v1;
547                 sa1->v4= sa2->v4;
548                 screen_addedge(scr, sa1->v1, sa1->v2);
549                 screen_addedge(scr, sa1->v3, sa1->v4);
550         }
551         
552         screen_delarea(C, scr, sa2);
553         removedouble_scrverts(scr);
554         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
555         
556         return 1;
557 }
558
559 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
560 {
561         ScrEdge *se;
562         ScrVert *sv;
563         int oneselected;
564         char dir;
565         
566         /* select connected, only in the right direction */
567         /* 'dir' is the direction of EDGE */
568         
569         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
570         else dir= 'h';
571         
572         sv= sc->vertbase.first;
573         while(sv) {
574                 sv->flag = 0;
575                 sv= sv->next;
576         }
577         
578         edge->v1->flag= 1;
579         edge->v2->flag= 1;
580         
581         oneselected= 1;
582         while(oneselected) {
583                 se= sc->edgebase.first;
584                 oneselected= 0;
585                 while(se) {
586                         if(se->v1->flag + se->v2->flag==1) {
587                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
588                                         se->v1->flag= se->v2->flag= 1;
589                                         oneselected= 1;
590                                 }
591                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
592                                                 se->v1->flag= se->v2->flag= 1;
593                                                 oneselected= 1;
594                                         }
595                         }
596                                 se= se->next;
597                 }
598         }
599 }
600
601 /* test if screen vertices should be scaled */
602 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
603 {
604         ScrVert *sv=NULL;
605         ScrArea *sa;
606         int sizex, sizey;
607         float facx, facy, tempf, min[2], max[2];
608         
609         /* calculate size */
610         min[0]= min[1]= 10000.0f;
611         max[0]= max[1]= 0.0f;
612         
613         for(sv= sc->vertbase.first; sv; sv= sv->next) {
614                 min[0]= MIN2(min[0], sv->vec.x);
615                 min[1]= MIN2(min[1], sv->vec.y);
616                 max[0]= MAX2(max[0], sv->vec.x);
617                 max[1]= MAX2(max[1], sv->vec.y);
618         }
619         
620         /* always make 0.0 left under */
621         for(sv= sc->vertbase.first; sv; sv= sv->next) {
622                 sv->vec.x -= min[0];
623                 sv->vec.y -= min[1];
624         }
625         
626         sizex= max[0]-min[0];
627         sizey= max[1]-min[1];
628         
629         if(sizex!= winsizex || sizey!= winsizey) {
630                 facx= winsizex;
631                 facx/= (float)sizex;
632                 facy= winsizey;
633                 facy/= (float)sizey;
634                 
635                 /* make sure it fits! */
636                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
637                         tempf= ((float)sv->vec.x)*facx;
638                         sv->vec.x= (short)(tempf+0.5);
639                         sv->vec.x+= AREAGRID-1;
640                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
641                         
642                         CLAMP(sv->vec.x, 0, winsizex);
643                         
644                         tempf= ((float)sv->vec.y )*facy;
645                         sv->vec.y= (short)(tempf+0.5);
646                         sv->vec.y+= AREAGRID-1;
647                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
648                         
649                         CLAMP(sv->vec.y, 0, winsizey);
650                 }
651         }
652         
653         /* test for collapsed areas. This could happen in some blender version... */
654         /* ton: removed option now, it needs Context... */
655         
656         /* make each window at least HEADERY high */
657         for(sa= sc->areabase.first; sa; sa= sa->next) {
658                 int headery= HEADERY+1;
659                 
660                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
661                         /* lower edge */
662                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
663                         if(se && sa->v1!=sa->v2 ) {
664                                 int yval;
665                                 
666                                 select_connected_scredge(sc, se);
667                                 
668                                 /* all selected vertices get the right offset */
669                                 yval= sa->v2->vec.y-headery;
670                                 sv= sc->vertbase.first;
671                                 while(sv) {
672                                         /* if is a collapsed area */
673                                         if(sv!=sa->v2 && sv!=sa->v3) {
674                                                 if(sv->flag) sv->vec.y= yval;
675                                         }
676                                         sv= sv->next;
677                                 }
678                         }
679                 }
680         }
681         
682 }
683
684 /* *********************** DRAWING **************************************** */
685
686
687 #define SCR_BACK 0.55
688 #define SCR_ROUND 12
689
690 /* draw vertical shape visualising future joining (left as well
691  * right direction of future joining) */
692 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
693 {
694         vec2f points[10];
695         short i;
696         float w, h;
697         float width = sa->v3->vec.x - sa->v1->vec.x;
698         float height = sa->v3->vec.y - sa->v1->vec.y;
699
700         if(height<width) {
701                 h = height/8;
702                 w = height/4;
703         }
704         else {
705                 h = width/8;
706                 w = width/4;
707         }
708
709         points[0].x = sa->v1->vec.x;
710         points[0].y = sa->v1->vec.y + height/2;
711
712         points[1].x = sa->v1->vec.x;
713         points[1].y = sa->v1->vec.y;
714
715         points[2].x = sa->v4->vec.x - w;
716         points[2].y = sa->v4->vec.y;
717
718         points[3].x = sa->v4->vec.x - w;
719         points[3].y = sa->v4->vec.y + height/2 - 2*h;
720
721         points[4].x = sa->v4->vec.x - 2*w;
722         points[4].y = sa->v4->vec.y + height/2;
723
724         points[5].x = sa->v4->vec.x - w;
725         points[5].y = sa->v4->vec.y + height/2 + 2*h;
726
727         points[6].x = sa->v3->vec.x - w;
728         points[6].y = sa->v3->vec.y;
729
730         points[7].x = sa->v2->vec.x;
731         points[7].y = sa->v2->vec.y;
732
733         points[8].x = sa->v4->vec.x;
734         points[8].y = sa->v4->vec.y + height/2 - h;
735
736         points[9].x = sa->v4->vec.x;
737         points[9].y = sa->v4->vec.y + height/2 + h;
738
739         if(dir=='l') {
740                 /* when direction is left, then we flip direction of arrow */
741                 float cx = sa->v1->vec.x + width;
742                 for(i=0;i<10;i++) {
743                         points[i].x -= cx;
744                         points[i].x = -points[i].x;
745                         points[i].x += sa->v1->vec.x;
746                 }
747         }
748
749         glBegin(GL_POLYGON);
750         for(i=0;i<5;i++)
751                 glVertex2f(points[i].x, points[i].y);
752         glEnd();
753         glBegin(GL_POLYGON);
754         for(i=4;i<8;i++)
755                 glVertex2f(points[i].x, points[i].y);
756         glVertex2f(points[0].x, points[0].y);
757         glEnd();
758
759         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
760         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
761 }
762
763 /* draw vertical shape visualising future joining (up/down direction) */
764 static void draw_vertical_join_shape(ScrArea *sa, char dir)
765 {
766         vec2f points[10];
767         short i;
768         float w, h;
769         float width = sa->v3->vec.x - sa->v1->vec.x;
770         float height = sa->v3->vec.y - sa->v1->vec.y;
771
772         if(height<width) {
773                 h = height/4;
774                 w = height/8;
775         }
776         else {
777                 h = width/4;
778                 w = width/8;
779         }
780
781         points[0].x = sa->v1->vec.x + width/2;
782         points[0].y = sa->v3->vec.y;
783
784         points[1].x = sa->v2->vec.x;
785         points[1].y = sa->v2->vec.y;
786
787         points[2].x = sa->v1->vec.x;
788         points[2].y = sa->v1->vec.y + h;
789
790         points[3].x = sa->v1->vec.x + width/2 - 2*w;
791         points[3].y = sa->v1->vec.y + h;
792
793         points[4].x = sa->v1->vec.x + width/2;
794         points[4].y = sa->v1->vec.y + 2*h;
795
796         points[5].x = sa->v1->vec.x + width/2 + 2*w;
797         points[5].y = sa->v1->vec.y + h;
798
799         points[6].x = sa->v4->vec.x;
800         points[6].y = sa->v4->vec.y + h;
801         
802         points[7].x = sa->v3->vec.x;
803         points[7].y = sa->v3->vec.y;
804
805         points[8].x = sa->v1->vec.x + width/2 - w;
806         points[8].y = sa->v1->vec.y;
807
808         points[9].x = sa->v1->vec.x + width/2 + w;
809         points[9].y = sa->v1->vec.y;
810
811         if(dir=='u') {
812                 /* when direction is up, then we flip direction of arrow */
813                 float cy = sa->v1->vec.y + height;
814                 for(i=0;i<10;i++) {
815                         points[i].y -= cy;
816                         points[i].y = -points[i].y;
817                         points[i].y += sa->v1->vec.y;
818                 }
819         }
820
821         glBegin(GL_POLYGON);
822         for(i=0;i<5;i++)
823                 glVertex2f(points[i].x, points[i].y);
824         glEnd();
825         glBegin(GL_POLYGON);
826         for(i=4;i<8;i++)
827                 glVertex2f(points[i].x, points[i].y);
828         glVertex2f(points[0].x, points[0].y);
829         glEnd();
830
831         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
832         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
833 }
834
835 /* draw join shape due to direction of joining */
836 static void draw_join_shape(ScrArea *sa, char dir)
837 {
838         if(dir=='u' || dir=='d')
839                 draw_vertical_join_shape(sa, dir);
840         else
841                 draw_horizontal_join_shape(sa, dir);
842 }
843
844 /* draw screen area darker with arrow (visualisation of future joining) */
845 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
846 {
847         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
848         glEnable(GL_BLEND);
849         glColor4ub(0, 0, 0, 50);
850         draw_join_shape(sa, dir);
851         glDisable(GL_BLEND);
852 }
853
854 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
855 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
856 {
857         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
858         glEnable(GL_BLEND);
859         /* value 181 was hardly computed: 181~105 */
860         glColor4ub(255, 255, 255, 50);          
861         /* draw_join_shape(sa, dir); */
862         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
863         glDisable(GL_BLEND);
864 }
865
866 /** screen edges drawing **/
867 static void drawscredge_area(ScrArea *sa)
868 {
869         short x1= sa->v1->vec.x;
870         short y1= sa->v1->vec.y;
871         short x2= sa->v3->vec.x;
872         short y2= sa->v3->vec.y;
873         
874         cpack(0x0);
875         
876         /* right border area */
877         sdrawline(x2, y1, x2, y2);
878         
879         /* left border area */
880         if(x1>0) { /* otherwise it draws the emboss of window over */
881                 sdrawline(x1, y1, x1, y2);
882         }
883         
884         /* top border area */
885         sdrawline(x1, y2, x2, y2);
886         
887         /* bottom border area */
888         sdrawline(x1, y1, x2, y1);
889         
890 }
891
892 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
893
894 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
895 {
896         bScreen *newsc;
897         
898         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
899         
900         /* make new empty screen: */
901         newsc= screen_add(win, sc->scene, sc->id.name+2);
902         /* copy all data */
903         screen_copy(newsc, sc);
904         /* set in window */
905         win->screen= newsc;
906         
907         return newsc;
908 }
909
910 /* screen sets cursor based on swinid */
911 static void region_cursor_set(wmWindow *win, int swinid)
912 {
913         ScrArea *sa= win->screen->areabase.first;
914         
915         for(;sa; sa= sa->next) {
916                 ARegion *ar= sa->regionbase.first;
917                 for(;ar; ar= ar->next) {
918                         if(ar->swinid == swinid) {
919                                 if(ar->type && ar->type->cursor)
920                                         ar->type->cursor(win, sa, ar);
921                                 else
922                                         WM_cursor_set(win, CURSOR_STD);
923                                 return;
924                         }
925                 }
926         }
927 }
928
929 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
930 {
931         
932         /* generic notes */
933         switch(note->category) {
934                 case NC_WINDOW:
935                         win->screen->do_draw= 1;
936                         break;
937                 case NC_SCREEN:
938                         if(note->action==NA_EDITED)
939                                 win->screen->do_draw= win->screen->do_refresh= 1;
940                 case NC_SCENE:
941                         if(note->data==ND_MODE)
942                                 region_cursor_set(win, note->swinid);                           
943                         break;
944         }
945 }
946
947 /* only for edge lines between areas, and the blended join arrows */
948 void ED_screen_draw(wmWindow *win)
949 {
950         ScrArea *sa;
951         ScrArea *sa1=NULL;
952         ScrArea *sa2=NULL;
953         int dir = -1;
954         int dira = -1;
955
956         wmSubWindowSet(win, win->screen->mainwin);
957         
958         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
959                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
960                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
961                 drawscredge_area(sa);
962         }
963
964         /* blended join arrow */
965         if (sa1 && sa2) {
966                 dir = area_getorientation(win->screen, sa1, sa2);
967                 if (dir >= 0) {
968                         switch(dir) {
969                                 case 0: /* W */
970                                         dir = 'r';
971                                         dira = 'l';
972                                         break;
973                                 case 1: /* N */
974                                         dir = 'd';
975                                         dira = 'u';
976                                         break;
977                                 case 2: /* E */
978                                         dir = 'l';
979                                         dira = 'r';
980                                         break;
981                                 case 3: /* S */
982                                         dir = 'u';
983                                         dira = 'd';
984                                         break;
985                         }
986                 }
987                 scrarea_draw_shape_dark(sa2, dir);
988                 scrarea_draw_shape_light(sa1, dira);
989         }
990         
991         if(G.f & G_DEBUG) printf("draw screen\n");
992         win->screen->do_draw= 0;
993 }
994
995 /* make this screen usable */
996 /* for file read and first use, for scaling window, area moves */
997 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
998 {
999         ScrArea *sa;
1000         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
1001         
1002         screen_test_scale(win->screen, win->sizex, win->sizey);
1003         
1004         if(win->screen->mainwin==0)
1005                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1006         else
1007                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1008         
1009         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1010                 /* set spacetype and region callbacks, calls init() */
1011                 /* sets subwindows for regions, adds handlers */
1012                 ED_area_initialize(wm, win, sa);
1013         }
1014
1015         /* wake up animtimer */
1016         if(win->screen->animtimer)
1017                 WM_event_window_timer_sleep(win, win->screen->animtimer, 0);
1018         
1019         if(G.f & G_DEBUG) printf("set screen\n");
1020         win->screen->do_refresh= 0;
1021
1022         win->screen->context= ed_screen_context;
1023 }
1024
1025 /* file read, set all screens, ... */
1026 void ED_screens_initialize(wmWindowManager *wm)
1027 {
1028         wmWindow *win;
1029         
1030         for(win= wm->windows.first; win; win= win->next) {
1031                 
1032                 if(win->screen==NULL)
1033                         win->screen= G.main->screen.first;
1034                 
1035                 ED_screen_refresh(wm, win);
1036         }
1037 }
1038
1039
1040 /* *********** exit calls are for closing running stuff ******** */
1041
1042 void ED_region_exit(bContext *C, ARegion *ar)
1043 {
1044         ARegion *prevar= CTX_wm_region(C);
1045
1046         CTX_wm_region_set(C, ar);
1047         WM_event_remove_handlers(C, &ar->handlers);
1048         if(ar->swinid)
1049                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1050         ar->swinid= 0;
1051         
1052         if(ar->headerstr)
1053                 MEM_freeN(ar->headerstr);
1054         ar->headerstr= NULL;
1055         
1056         CTX_wm_region_set(C, prevar);
1057 }
1058
1059 void ED_area_exit(bContext *C, ScrArea *sa)
1060 {
1061         ScrArea *prevsa= CTX_wm_area(C);
1062         ARegion *ar;
1063
1064         CTX_wm_area_set(C, sa);
1065         for(ar= sa->regionbase.first; ar; ar= ar->next)
1066                 ED_region_exit(C, ar);
1067
1068         WM_event_remove_handlers(C, &sa->handlers);
1069         CTX_wm_area_set(C, prevsa);
1070 }
1071
1072 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1073 {
1074         wmWindow *prevwin= CTX_wm_window(C);
1075         ScrArea *sa;
1076         ARegion *ar;
1077
1078         CTX_wm_window_set(C, window);
1079         
1080         if(screen->animtimer)
1081                 WM_event_remove_window_timer(window, screen->animtimer);
1082         screen->animtimer= NULL;
1083         
1084         for(ar= screen->regionbase.first; ar; ar= ar->next)
1085                 ED_region_exit(C, ar);
1086
1087         for(sa= screen->areabase.first; sa; sa= sa->next)
1088                 ED_area_exit(C, sa);
1089
1090         CTX_wm_window_set(C, prevwin);
1091 }
1092
1093 /* *********************************** */
1094
1095 /* case when on area-edge or in azones, or outside window */
1096 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1097 {
1098         ScrArea *sa;
1099         
1100         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1101                 if(is_in_area_actionzone(sa, event->x, event->y))
1102                         break;
1103         
1104         if(sa) {
1105                 WM_cursor_set(win, CURSOR_EDIT);
1106         }
1107         else {
1108                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1109                 
1110                 if (actedge) {
1111                         if(scredge_is_horizontal(actedge))
1112                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1113                         else
1114                                 WM_cursor_set(win, CURSOR_X_MOVE);
1115                 }
1116                 else
1117                         WM_cursor_set(win, CURSOR_STD);
1118         } 
1119 }
1120
1121
1122 /* called in wm_event_system.c. sets state vars in screen, cursors */
1123 /* event type is mouse move */
1124 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1125 {
1126         if(win->screen) {
1127                 bScreen *scr= win->screen;
1128                 ScrArea *sa;
1129                 ARegion *ar;
1130                 int oldswin= scr->subwinactive;
1131
1132                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1133                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1134                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1135                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1136                                                 break;
1137                 }
1138                 if(sa) {
1139                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1140                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1141                                         scr->subwinactive= ar->swinid;
1142                         }
1143                 }
1144                 else
1145                         scr->subwinactive= scr->mainwin;
1146                 
1147                 /* check for redraw headers */
1148                 if(oldswin!=scr->subwinactive) {
1149
1150                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1151                                 int do_draw= 0;
1152                                 
1153                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1154                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1155                                                 do_draw= 1;
1156                                 
1157                                 if(do_draw) {
1158                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1159                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1160                                                         ED_region_tag_redraw(ar);
1161                                 }
1162                         }
1163                 }
1164                 
1165                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1166                 if(scr->subwinactive==scr->mainwin) {
1167                         screen_cursor_set(win, event);
1168                 }
1169                 else if(oldswin!=scr->subwinactive) {
1170                         region_cursor_set(win, scr->subwinactive);
1171                 }
1172         }
1173 }
1174
1175 int ED_screen_area_active(const bContext *C)
1176 {
1177         bScreen *sc= CTX_wm_screen(C);
1178         ScrArea *sa= CTX_wm_area(C);
1179
1180         if(sc && sa) {
1181                 ARegion *ar;
1182                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1183                         if(ar->swinid == sc->subwinactive)
1184                                 return 1;
1185         }       
1186         return 0;
1187 }
1188
1189 /* operator call, WM + Window + screen already existed before */
1190 /* Do NOT call in area/region queues! */
1191 void ed_screen_set(bContext *C, bScreen *sc)
1192 {
1193         bScreen *oldscreen= CTX_wm_screen(C);
1194         
1195         if(sc->full) {                          /* find associated full */
1196                 bScreen *sc1;
1197                 for(sc1= G.main->screen.first; sc1; sc1= sc1->id.next) {
1198                         ScrArea *sa= sc1->areabase.first;
1199                         if(sa->full==sc) {
1200                                 sc= sc1;
1201                                 break;
1202                         }
1203                 }
1204                 if(sc1==NULL) printf("set screen error\n");
1205         }
1206         
1207         if (oldscreen != sc) {
1208                 wmWindow *win= CTX_wm_window(C);
1209                 wmTimer *wt= oldscreen->animtimer;
1210                 
1211                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1212                 oldscreen->animtimer= NULL;
1213                 if(wt)
1214                         WM_event_window_timer_sleep(win, wt, 1);
1215                 
1216                 ED_screen_exit(C, win, oldscreen);
1217                 oldscreen->animtimer= wt;
1218                 
1219                 win->screen= sc;
1220                 CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
1221                 
1222                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1223                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1224         }
1225 }
1226
1227 /* this function toggles: if area is full then the parent will be restored */
1228 void ed_screen_fullarea(bContext *C)
1229 {
1230         bScreen *sc, *oldscreen;
1231         ScrArea *newa, *old;
1232         short fulltype;
1233         
1234         if(CTX_wm_area(C)->full) {
1235                 sc= CTX_wm_area(C)->full;               /* the old screen to restore */
1236                 oldscreen= CTX_wm_screen(C);    /* the one disappearing */
1237                 
1238                 fulltype = sc->full;
1239                 
1240                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1241                    is set */
1242                 
1243                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1244                         sc->full= 0;
1245                         
1246                         /* find old area */
1247                         for(old= sc->areabase.first; old; old= old->next) 
1248                                 if(old->full) break;
1249                         if(old==NULL) {
1250                                 printf("something wrong in areafullscreen\n"); 
1251                                 return;
1252                         }
1253                             // old feature described below (ton)
1254                                 // in autoplay screens the headers are disabled by 
1255                                 // default. So use the old headertype instead
1256                         
1257                         area_copy_data(old, CTX_wm_area(C), 1); /*  1 = swap spacelist */
1258                         
1259                         old->full= NULL;
1260                         
1261                         /* animtimer back */
1262                         sc->animtimer= oldscreen->animtimer;
1263                         oldscreen->animtimer= NULL;
1264                         
1265                         ed_screen_set(C, sc);
1266                         
1267                         free_screen(oldscreen);
1268                         free_libblock(&G.main->screen, oldscreen);
1269                 }
1270         }
1271         else {
1272                 oldscreen= CTX_wm_screen(C);
1273
1274                 /* is there only 1 area? */
1275                 if(oldscreen->areabase.first==CTX_wm_screen(C)->areabase.last) return;
1276                 if(CTX_wm_area(C)->spacetype==SPACE_INFO) return;
1277                 
1278                 oldscreen->full = SCREENFULL;
1279                 
1280                 sc= screen_add(CTX_wm_window(C), CTX_data_scene(C), "temp");
1281                 
1282                 /* timer */
1283                 sc->animtimer= oldscreen->animtimer;
1284                 oldscreen->animtimer= NULL;
1285                 
1286                 /* returns the top small area */
1287                 newa= area_split(CTX_wm_window(C), sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1288                 ED_area_newspace(C, newa, SPACE_INFO);
1289
1290                 /* copy area */
1291                 newa= newa->prev;
1292                 area_copy_data(newa, CTX_wm_area(C), 1);        /* 1 = swap spacelist */
1293
1294                 CTX_wm_area(C)->full= oldscreen;
1295                 newa->full= oldscreen;
1296                 newa->next->full= oldscreen;
1297
1298                 ed_screen_set(C, sc);
1299         }
1300
1301         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1302         CTX_wm_area_set(C, sc->areabase.first);
1303
1304         /* XXX retopo_force_update(); */
1305
1306 }
1307
1308 void ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1309 {
1310         if(sa->full==0)
1311                 ed_screen_fullarea(C);
1312
1313         ED_area_newspace(C, CTX_wm_area(C), type);
1314 }
1315
1316 void ED_screen_full_prevspace(bContext *C)
1317 {
1318         ScrArea *sa= CTX_wm_area(C);
1319         
1320         ED_area_prevspace(C);
1321         
1322         if(sa->full)
1323                 ed_screen_fullarea(C);
1324 }
1325
1326 void ED_screen_animation_timer(bContext *C, int enable)
1327 {
1328         bScreen *screen= CTX_wm_screen(C);
1329         wmWindow *win= CTX_wm_window(C);
1330         Scene *scene= CTX_data_scene(C);
1331         
1332         if(screen->animtimer)
1333                 WM_event_remove_window_timer(win, screen->animtimer);
1334         screen->animtimer= NULL;
1335         
1336         if(enable)
1337                 screen->animtimer= WM_event_add_window_timer(win, TIMER0, (1.0/FPS));
1338 }
1339