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