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 #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, 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= G.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->uiblocks.first= sa->uiblocks.last= NULL;
462                 sa->panels.first= sa->panels.last= NULL;
463                 sa->regionbase.first= sa->regionbase.last= NULL;
464                 sa->actionzones.first= sa->actionzones.last= NULL;
465                 sa->scriptlink.totscript= 0;
466                 
467                 area_copy_data(sa, saf, 0);
468         }
469         
470         /* put at zero (needed?) */
471         for(s1= from->vertbase.first; s1; s1= s1->next)
472                 s1->newv= NULL;
473
474 }
475
476
477 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
478 /* -1 = not valid check */
479 /* used with join operator */
480 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
481 {
482         ScrVert *sav1, *sav2, *sav3, *sav4;
483         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
484
485         if(sa==NULL || sb==NULL) return -1;
486
487         sav1= sa->v1;
488         sav2= sa->v2;
489         sav3= sa->v3;
490         sav4= sa->v4;
491         sbv1= sb->v1;
492         sbv2= sb->v2;
493         sbv3= sb->v3;
494         sbv4= sb->v4;
495         
496         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
497                 return 0;
498         }
499         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
500                 return 1;
501         }
502         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
503                 return 2;
504         }
505         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
506                 return 3;
507         }
508         
509         return -1;
510 }
511
512 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
513 *       used by the split, join operators
514 */
515 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
516 {
517         int dir;
518         
519         dir = area_getorientation(scr, sa1, sa2);
520         /*printf("dir is : %i \n", dir);*/
521         
522         if (dir < 0)
523         {
524                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
525                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
526                 return 0;
527         }
528         
529         if(dir == 0) {
530                 sa1->v1= sa2->v1;
531                 sa1->v2= sa2->v2;
532                 screen_addedge(scr, sa1->v2, sa1->v3);
533                 screen_addedge(scr, sa1->v1, sa1->v4);
534         }
535         else if(dir == 1) {
536                 sa1->v2= sa2->v2;
537                 sa1->v3= sa2->v3;
538                 screen_addedge(scr, sa1->v1, sa1->v2);
539                 screen_addedge(scr, sa1->v3, sa1->v4);
540         }
541         else if(dir == 2) {
542                 sa1->v3= sa2->v3;
543                 sa1->v4= sa2->v4;
544                 screen_addedge(scr, sa1->v2, sa1->v3);
545                 screen_addedge(scr, sa1->v1, sa1->v4);
546         }
547         else if(dir == 3) {
548                 sa1->v1= sa2->v1;
549                 sa1->v4= sa2->v4;
550                 screen_addedge(scr, sa1->v1, sa1->v2);
551                 screen_addedge(scr, sa1->v3, sa1->v4);
552         }
553         
554         screen_delarea(C, scr, sa2);
555         removedouble_scrverts(scr);
556         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
557         
558         return 1;
559 }
560
561 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
562 {
563         ScrEdge *se;
564         ScrVert *sv;
565         int oneselected;
566         char dir;
567         
568         /* select connected, only in the right direction */
569         /* 'dir' is the direction of EDGE */
570         
571         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
572         else dir= 'h';
573         
574         sv= sc->vertbase.first;
575         while(sv) {
576                 sv->flag = 0;
577                 sv= sv->next;
578         }
579         
580         edge->v1->flag= 1;
581         edge->v2->flag= 1;
582         
583         oneselected= 1;
584         while(oneselected) {
585                 se= sc->edgebase.first;
586                 oneselected= 0;
587                 while(se) {
588                         if(se->v1->flag + se->v2->flag==1) {
589                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
590                                         se->v1->flag= se->v2->flag= 1;
591                                         oneselected= 1;
592                                 }
593                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
594                                                 se->v1->flag= se->v2->flag= 1;
595                                                 oneselected= 1;
596                                         }
597                         }
598                                 se= se->next;
599                 }
600         }
601 }
602
603 /* test if screen vertices should be scaled */
604 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
605 {
606         ScrVert *sv=NULL;
607         ScrArea *sa;
608         int sizex, sizey;
609         float facx, facy, tempf, min[2], max[2];
610         
611         /* calculate size */
612         min[0]= min[1]= 10000.0f;
613         max[0]= max[1]= 0.0f;
614         
615         for(sv= sc->vertbase.first; sv; sv= sv->next) {
616                 min[0]= MIN2(min[0], sv->vec.x);
617                 min[1]= MIN2(min[1], sv->vec.y);
618                 max[0]= MAX2(max[0], sv->vec.x);
619                 max[1]= MAX2(max[1], sv->vec.y);
620         }
621         
622         /* always make 0.0 left under */
623         for(sv= sc->vertbase.first; sv; sv= sv->next) {
624                 sv->vec.x -= min[0];
625                 sv->vec.y -= min[1];
626         }
627         
628         sizex= max[0]-min[0];
629         sizey= max[1]-min[1];
630         
631         if(sizex!= winsizex || sizey!= winsizey) {
632                 facx= winsizex;
633                 facx/= (float)sizex;
634                 facy= winsizey;
635                 facy/= (float)sizey;
636                 
637                 /* make sure it fits! */
638                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
639                         tempf= ((float)sv->vec.x)*facx;
640                         sv->vec.x= (short)(tempf+0.5);
641                         sv->vec.x+= AREAGRID-1;
642                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
643                         
644                         CLAMP(sv->vec.x, 0, winsizex);
645                         
646                         tempf= ((float)sv->vec.y )*facy;
647                         sv->vec.y= (short)(tempf+0.5);
648                         sv->vec.y+= AREAGRID-1;
649                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
650                         
651                         CLAMP(sv->vec.y, 0, winsizey);
652                 }
653         }
654         
655         /* test for collapsed areas. This could happen in some blender version... */
656         /* ton: removed option now, it needs Context... */
657         
658         /* make each window at least HEADERY high */
659         for(sa= sc->areabase.first; sa; sa= sa->next) {
660                 int headery= HEADERY+1;
661                 
662                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
663                         /* lower edge */
664                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
665                         if(se && sa->v1!=sa->v2 ) {
666                                 int yval;
667                                 
668                                 select_connected_scredge(sc, se);
669                                 
670                                 /* all selected vertices get the right offset */
671                                 yval= sa->v2->vec.y-headery;
672                                 sv= sc->vertbase.first;
673                                 while(sv) {
674                                         /* if is a collapsed area */
675                                         if(sv!=sa->v2 && sv!=sa->v3) {
676                                                 if(sv->flag) sv->vec.y= yval;
677                                         }
678                                         sv= sv->next;
679                                 }
680                         }
681                 }
682         }
683         
684 }
685
686 /* *********************** DRAWING **************************************** */
687
688
689 #define SCR_BACK 0.55
690 #define SCR_ROUND 12
691
692 /* draw vertical shape visualising future joining (left as well
693  * right direction of future joining) */
694 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
695 {
696         vec2f points[10];
697         short i;
698         float w, h;
699         float width = sa->v3->vec.x - sa->v1->vec.x;
700         float height = sa->v3->vec.y - sa->v1->vec.y;
701
702         if(height<width) {
703                 h = height/8;
704                 w = height/4;
705         }
706         else {
707                 h = width/8;
708                 w = width/4;
709         }
710
711         points[0].x = sa->v1->vec.x;
712         points[0].y = sa->v1->vec.y + height/2;
713
714         points[1].x = sa->v1->vec.x;
715         points[1].y = sa->v1->vec.y;
716
717         points[2].x = sa->v4->vec.x - w;
718         points[2].y = sa->v4->vec.y;
719
720         points[3].x = sa->v4->vec.x - w;
721         points[3].y = sa->v4->vec.y + height/2 - 2*h;
722
723         points[4].x = sa->v4->vec.x - 2*w;
724         points[4].y = sa->v4->vec.y + height/2;
725
726         points[5].x = sa->v4->vec.x - w;
727         points[5].y = sa->v4->vec.y + height/2 + 2*h;
728
729         points[6].x = sa->v3->vec.x - w;
730         points[6].y = sa->v3->vec.y;
731
732         points[7].x = sa->v2->vec.x;
733         points[7].y = sa->v2->vec.y;
734
735         points[8].x = sa->v4->vec.x;
736         points[8].y = sa->v4->vec.y + height/2 - h;
737
738         points[9].x = sa->v4->vec.x;
739         points[9].y = sa->v4->vec.y + height/2 + h;
740
741         if(dir=='l') {
742                 /* when direction is left, then we flip direction of arrow */
743                 float cx = sa->v1->vec.x + width;
744                 for(i=0;i<10;i++) {
745                         points[i].x -= cx;
746                         points[i].x = -points[i].x;
747                         points[i].x += sa->v1->vec.x;
748                 }
749         }
750
751         glBegin(GL_POLYGON);
752         for(i=0;i<5;i++)
753                 glVertex2f(points[i].x, points[i].y);
754         glEnd();
755         glBegin(GL_POLYGON);
756         for(i=4;i<8;i++)
757                 glVertex2f(points[i].x, points[i].y);
758         glVertex2f(points[0].x, points[0].y);
759         glEnd();
760
761         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
762         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
763 }
764
765 /* draw vertical shape visualising future joining (up/down direction) */
766 static void draw_vertical_join_shape(ScrArea *sa, char dir)
767 {
768         vec2f points[10];
769         short i;
770         float w, h;
771         float width = sa->v3->vec.x - sa->v1->vec.x;
772         float height = sa->v3->vec.y - sa->v1->vec.y;
773
774         if(height<width) {
775                 h = height/4;
776                 w = height/8;
777         }
778         else {
779                 h = width/4;
780                 w = width/8;
781         }
782
783         points[0].x = sa->v1->vec.x + width/2;
784         points[0].y = sa->v3->vec.y;
785
786         points[1].x = sa->v2->vec.x;
787         points[1].y = sa->v2->vec.y;
788
789         points[2].x = sa->v1->vec.x;
790         points[2].y = sa->v1->vec.y + h;
791
792         points[3].x = sa->v1->vec.x + width/2 - 2*w;
793         points[3].y = sa->v1->vec.y + h;
794
795         points[4].x = sa->v1->vec.x + width/2;
796         points[4].y = sa->v1->vec.y + 2*h;
797
798         points[5].x = sa->v1->vec.x + width/2 + 2*w;
799         points[5].y = sa->v1->vec.y + h;
800
801         points[6].x = sa->v4->vec.x;
802         points[6].y = sa->v4->vec.y + h;
803         
804         points[7].x = sa->v3->vec.x;
805         points[7].y = sa->v3->vec.y;
806
807         points[8].x = sa->v1->vec.x + width/2 - w;
808         points[8].y = sa->v1->vec.y;
809
810         points[9].x = sa->v1->vec.x + width/2 + w;
811         points[9].y = sa->v1->vec.y;
812
813         if(dir=='u') {
814                 /* when direction is up, then we flip direction of arrow */
815                 float cy = sa->v1->vec.y + height;
816                 for(i=0;i<10;i++) {
817                         points[i].y -= cy;
818                         points[i].y = -points[i].y;
819                         points[i].y += sa->v1->vec.y;
820                 }
821         }
822
823         glBegin(GL_POLYGON);
824         for(i=0;i<5;i++)
825                 glVertex2f(points[i].x, points[i].y);
826         glEnd();
827         glBegin(GL_POLYGON);
828         for(i=4;i<8;i++)
829                 glVertex2f(points[i].x, points[i].y);
830         glVertex2f(points[0].x, points[0].y);
831         glEnd();
832
833         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
834         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
835 }
836
837 /* draw join shape due to direction of joining */
838 static void draw_join_shape(ScrArea *sa, char dir)
839 {
840         if(dir=='u' || dir=='d')
841                 draw_vertical_join_shape(sa, dir);
842         else
843                 draw_horizontal_join_shape(sa, dir);
844 }
845
846 /* draw screen area darker with arrow (visualisation of future joining) */
847 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
848 {
849         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
850         glEnable(GL_BLEND);
851         glColor4ub(0, 0, 0, 50);
852         draw_join_shape(sa, dir);
853         glDisable(GL_BLEND);
854 }
855
856 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
857 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
858 {
859         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
860         glEnable(GL_BLEND);
861         /* value 181 was hardly computed: 181~105 */
862         glColor4ub(255, 255, 255, 50);          
863         /* draw_join_shape(sa, dir); */
864         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
865         glDisable(GL_BLEND);
866 }
867
868 /** screen edges drawing **/
869 static void drawscredge_area(ScrArea *sa)
870 {
871         short x1= sa->v1->vec.x;
872         short y1= sa->v1->vec.y;
873         short x2= sa->v3->vec.x;
874         short y2= sa->v3->vec.y;
875         
876         cpack(0x0);
877         
878         /* right border area */
879         sdrawline(x2, y1, x2, y2);
880         
881         /* left border area */
882         if(x1>0) { /* otherwise it draws the emboss of window over */
883                 sdrawline(x1, y1, x1, y2);
884         }
885         
886         /* top border area */
887         sdrawline(x1, y2, x2, y2);
888         
889         /* bottom border area */
890         sdrawline(x1, y1, x2, y1);
891         
892 }
893
894 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
895
896 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
897 {
898         bScreen *newsc;
899         
900         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
901         
902         /* make new empty screen: */
903         newsc= screen_add(win, sc->id.name+2);
904         /* copy all data */
905         screen_copy(newsc, sc);
906         /* set in window */
907         win->screen= newsc;
908         
909         return newsc;
910 }
911
912
913 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
914 {
915         
916         /* generic notes */
917         switch(note->type) {
918                 case WM_NOTE_WINDOW_REDRAW:
919                         win->screen->do_draw= 1;
920                         break;
921                 case WM_NOTE_SCREEN_CHANGED:
922                         win->screen->do_draw= win->screen->do_refresh= 1;
923                         break;
924                 case WM_NOTE_GESTURE_REDRAW:
925                         win->screen->do_gesture= 1;     /* XXX gestures are stored in window, draw per region... a bit weak? wait for proper composite? (ton) */
926                         break;
927         }
928 }
929
930 /* only for edge lines between areas, and the blended join arrows */
931 void ED_screen_draw(wmWindow *win)
932 {
933         ScrArea *sa;
934         ScrArea *sa1=NULL;
935         ScrArea *sa2=NULL;
936         int dir = -1;
937         int dira = -1;
938
939         wmSubWindowSet(win, win->screen->mainwin);
940         
941         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
942                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
943                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
944                 drawscredge_area(sa);
945         }
946
947         /* blended join arrow */
948         if (sa1 && sa2) {
949                 dir = area_getorientation(win->screen, sa1, sa2);
950                 if (dir >= 0) {
951                         switch(dir) {
952                                 case 0: /* W */
953                                         dir = 'r';
954                                         dira = 'l';
955                                         break;
956                                 case 1: /* N */
957                                         dir = 'd';
958                                         dira = 'u';
959                                         break;
960                                 case 2: /* E */
961                                         dir = 'l';
962                                         dira = 'r';
963                                         break;
964                                 case 3: /* S */
965                                         dir = 'u';
966                                         dira = 'd';
967                                         break;
968                         }
969                 }
970                 scrarea_draw_shape_dark(sa2, dir);
971                 scrarea_draw_shape_light(sa1, dira);
972         }
973         
974         if(G.f & G_DEBUG) printf("draw screen\n");
975         win->screen->do_draw= 0;
976 }
977
978 /* make this screen usable */
979 /* for file read and first use, for scaling window, area moves */
980 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
981 {
982         ScrArea *sa;
983         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
984         
985         screen_test_scale(win->screen, win->sizex, win->sizey);
986         
987         if(win->screen->mainwin==0)
988                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
989         else
990                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
991         
992         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
993                 /* set spacetype and region callbacks, calls init() */
994                 /* sets subwindows for regions, adds handlers */
995                 ED_area_initialize(wm, win, sa);
996         }
997
998         /* wake up animtimer */
999         if(win->screen->animtimer)
1000                 WM_event_window_timer_sleep(win, win->screen->animtimer, 0);
1001         
1002         if(G.f & G_DEBUG) printf("set screen\n");
1003         win->screen->do_refresh= 0;
1004 }
1005
1006 /* file read, set all screens, ... */
1007 void ED_screens_initialize(wmWindowManager *wm)
1008 {
1009         wmWindow *win;
1010         
1011         for(win= wm->windows.first; win; win= win->next) {
1012                 
1013                 if(win->screen==NULL)
1014                         win->screen= G.main->screen.first;
1015                 
1016                 ED_screen_refresh(wm, win);
1017         }
1018 }
1019
1020
1021 /* *********** exit calls are for closing running stuff ******** */
1022
1023 void ED_region_exit(bContext *C, ARegion *ar)
1024 {
1025         ARegion *prevar= CTX_wm_region(C);
1026
1027         CTX_wm_region_set(C, ar);
1028         WM_event_remove_handlers(C, &ar->handlers);
1029         if(ar->swinid)
1030                 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1031         ar->swinid= 0;
1032         
1033         if(ar->headerstr)
1034                 MEM_freeN(ar->headerstr);
1035         ar->headerstr= NULL;
1036         
1037         CTX_wm_region_set(C, prevar);
1038 }
1039
1040 void ED_area_exit(bContext *C, ScrArea *sa)
1041 {
1042         ScrArea *prevsa= CTX_wm_area(C);
1043         ARegion *ar;
1044
1045         CTX_wm_area_set(C, sa);
1046         for(ar= sa->regionbase.first; ar; ar= ar->next)
1047                 ED_region_exit(C, ar);
1048
1049         WM_event_remove_handlers(C, &sa->handlers);
1050         CTX_wm_area_set(C, prevsa);
1051 }
1052
1053 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1054 {
1055         wmWindow *prevwin= CTX_wm_window(C);
1056         ScrArea *sa;
1057         ARegion *ar;
1058
1059         CTX_wm_window_set(C, window);
1060         
1061         if(screen->animtimer)
1062                 WM_event_remove_window_timer(window, screen->animtimer);
1063         screen->animtimer= NULL;
1064         
1065         for(ar= screen->regionbase.first; ar; ar= ar->next)
1066                 ED_region_exit(C, ar);
1067
1068         for(sa= screen->areabase.first; sa; sa= sa->next)
1069                 ED_area_exit(C, sa);
1070
1071         CTX_wm_window_set(C, prevwin);
1072 }
1073
1074 /* case when on area-edge or in azones, or outside window */
1075 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1076 {
1077         ScrArea *sa;
1078         
1079         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1080                 if(is_in_area_actionzone(sa, event->x, event->y))
1081                         break;
1082         
1083         if(sa) {
1084                 WM_cursor_set(win, CURSOR_EDIT);
1085         }
1086         else {
1087                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1088                 
1089                 if (actedge) {
1090                         if(scredge_is_horizontal(actedge))
1091                                 WM_cursor_set(win, CURSOR_Y_MOVE);
1092                         else
1093                                 WM_cursor_set(win, CURSOR_X_MOVE);
1094                 }
1095                 else
1096                         WM_cursor_set(win, CURSOR_STD);
1097         } 
1098 }
1099
1100
1101 /* called in wm_event_system.c. sets state var in screen */
1102 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1103 {
1104         if(win->screen) {
1105                 bScreen *scr= win->screen;
1106                 ScrArea *sa;
1107                 ARegion *ar;
1108                 int oldswin= scr->subwinactive;
1109
1110                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1111                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1112                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1113                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1114                                                 break;
1115                 }
1116                 if(sa) {
1117                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1118                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1119                                         scr->subwinactive= ar->swinid;
1120                         }
1121                 }
1122                 else
1123                         scr->subwinactive= scr->mainwin;
1124                 
1125                 /* check for redraw headers */
1126                 if(oldswin!=scr->subwinactive) {
1127
1128                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1129                                 int do_draw= 0;
1130                                 
1131                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1132                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1133                                                 do_draw= 1;
1134                                 
1135                                 if(do_draw) {
1136                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1137                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1138                                                         ED_region_tag_redraw(ar);
1139                                 }
1140                         }
1141                 }
1142                 
1143                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1144                 if(scr->subwinactive==scr->mainwin) {
1145                         screen_cursor_set(win, event);
1146                 }
1147                 else if(oldswin!=scr->subwinactive) {
1148                         /* cursor space type switching */
1149                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1150                                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1151                                         if(ar->swinid==scr->subwinactive) {
1152                                                 if(sa->type->cursor)
1153                                                         sa->type->cursor(win, ar);
1154                                                 else 
1155                                                         WM_cursor_set(win, CURSOR_STD);
1156                                         }
1157                                 }
1158                                         
1159                         }
1160                 }
1161         }
1162 }
1163
1164 int ED_screen_area_active(const bContext *C)
1165 {
1166         bScreen *sc= CTX_wm_screen(C);
1167         ScrArea *sa= CTX_wm_area(C);
1168
1169         if(sc && sa) {
1170                 ARegion *ar;
1171                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1172                         if(ar->swinid == sc->subwinactive)
1173                                 return 1;
1174         }       
1175         return 0;
1176 }
1177
1178 /* operator call, WM + Window + screen already existed before */
1179 /* Do NOT call in area/region queues! */
1180 void ed_screen_set(bContext *C, bScreen *sc)
1181 {
1182         bScreen *oldscreen= CTX_wm_screen(C);
1183         
1184         if(sc->full) {                          /* find associated full */
1185                 bScreen *sc1;
1186                 for(sc1= G.main->screen.first; sc1; sc1= sc1->id.next) {
1187                         ScrArea *sa= sc1->areabase.first;
1188                         if(sa->full==sc) {
1189                                 sc= sc1;
1190                                 break;
1191                         }
1192                 }
1193                 if(sc1==NULL) printf("set screen error\n");
1194         }
1195         
1196         if (oldscreen != sc) {
1197                 wmTimer *wt= oldscreen->animtimer;
1198                 
1199                 /* we put timer to sleep, so screen_exit has to think there's no timer */
1200                 oldscreen->animtimer= NULL;
1201                 if(wt)
1202                         WM_event_window_timer_sleep(CTX_wm_window(C), wt, 1);
1203                 
1204                 ED_screen_exit(C, CTX_wm_window(C), oldscreen);
1205                 oldscreen->animtimer= wt;
1206                 
1207                 CTX_wm_window(C)->screen= sc;
1208                 
1209                 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1210                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
1211         }
1212 }
1213
1214 /* this function toggles: if area is full then the parent will be restored */
1215 void ed_screen_fullarea(bContext *C)
1216 {
1217         bScreen *sc, *oldscreen;
1218         ScrArea *newa, *old;
1219         short fulltype;
1220         
1221         if(CTX_wm_area(C)->full) {
1222                 sc= CTX_wm_area(C)->full;               /* the old screen to restore */
1223                 oldscreen= CTX_wm_screen(C);    /* the one disappearing */
1224                 
1225                 fulltype = sc->full;
1226                 
1227                 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1228                    is set */
1229                 
1230                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1231                         sc->full= 0;
1232                         
1233                         /* find old area */
1234                         for(old= sc->areabase.first; old; old= old->next) 
1235                                 if(old->full) break;
1236                         if(old==NULL) {
1237                                 printf("something wrong in areafullscreen\n"); 
1238                                 return;
1239                         }
1240                             // old feature described below (ton)
1241                                 // in autoplay screens the headers are disabled by 
1242                                 // default. So use the old headertype instead
1243                         
1244                         area_copy_data(old, CTX_wm_area(C), 1); /*  1 = swap spacelist */
1245                         
1246                         old->full= NULL;
1247                         
1248                         /* animtimer back */
1249                         sc->animtimer= oldscreen->animtimer;
1250                         oldscreen->animtimer= NULL;
1251                         
1252                         ed_screen_set(C, sc);
1253                         
1254                         free_screen(oldscreen);
1255                         free_libblock(&G.main->screen, oldscreen);
1256                 }
1257         }
1258         else {
1259                 oldscreen= CTX_wm_screen(C);
1260
1261                 /* is there only 1 area? */
1262                 if(oldscreen->areabase.first==CTX_wm_screen(C)->areabase.last) return;
1263                 if(CTX_wm_area(C)->spacetype==SPACE_INFO) return;
1264                 
1265                 oldscreen->full = SCREENFULL;
1266                 
1267                 sc= screen_add(CTX_wm_window(C), "temp");
1268                 
1269                 /* timer */
1270                 sc->animtimer= oldscreen->animtimer;
1271                 oldscreen->animtimer= NULL;
1272                 
1273                 /* returns the top small area */
1274                 newa= area_split(CTX_wm_window(C), sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1275                 area_newspace(C, newa, SPACE_INFO);
1276
1277                 /* copy area */
1278                 newa= newa->prev;
1279                 area_copy_data(newa, CTX_wm_area(C), 1);        /* 1 = swap spacelist */
1280
1281                 CTX_wm_area(C)->full= oldscreen;
1282                 newa->full= oldscreen;
1283                 newa->next->full= oldscreen;
1284
1285                 ed_screen_set(C, sc);
1286         }
1287
1288         /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1289         CTX_wm_area_set(C, sc->areabase.first);
1290
1291         /* XXX retopo_force_update(); */
1292
1293 }
1294
1295 void ED_screen_animation_timer(bContext *C, int enable)
1296 {
1297         bScreen *screen= CTX_wm_screen(C);
1298         wmWindow *win= CTX_wm_window(C);
1299         Scene *scene= CTX_data_scene(C);
1300         
1301         if(screen->animtimer)
1302                 WM_event_remove_window_timer(win, screen->animtimer);
1303         screen->animtimer= NULL;
1304         
1305         if(enable)
1306                 screen->animtimer= WM_event_add_window_timer(win, (1.0/FPS));
1307 }
1308