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
33 #include "BLI_blenlib.h"
34
35 #include "BKE_global.h"
36 #include "BKE_library.h"
37 #include "BKE_main.h"
38 #include "BKE_screen.h"
39 #include "BKE_utildefines.h"
40
41 #include "BIF_gl.h"
42 #include "BIF_glutil.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_screen.h"
48 #include "ED_screen_types.h"
49
50 #include "wm_subwindow.h"
51
52 #include "screen_intern.h"      /* own module include */
53
54
55 /* ******************* screen vert, edge, area managing *********************** */
56
57 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
58 {
59         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
60         sv->vec.x= x;
61         sv->vec.y= y;
62         
63         BLI_addtail(&sc->vertbase, sv);
64         return sv;
65 }
66
67 static void sortscrvert(ScrVert **v1, ScrVert **v2)
68 {
69         ScrVert *tmp;
70         
71         if (*v1 > *v2) {
72                 tmp= *v1;
73                 *v1= *v2;
74                 *v2= tmp;       
75         }
76 }
77
78 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
79 {
80         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
81         
82         sortscrvert(&v1, &v2);
83         se->v1= v1;
84         se->v2= v2;
85         
86         BLI_addtail(&sc->edgebase, se);
87         return se;
88 }
89
90
91 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
92 {
93         ScrEdge *se;
94         
95         sortscrvert(&v1, &v2);
96         for (se= sc->edgebase.first; se; se= se->next)
97                 if(se->v1==v1 && se->v2==v2)
98                         return se;
99         
100         return NULL;
101 }
102
103 void removedouble_scrverts(bScreen *sc)
104 {
105         ScrVert *v1, *verg;
106         ScrEdge *se;
107         ScrArea *sa;
108         
109         verg= sc->vertbase.first;
110         while(verg) {
111                 if(verg->newv==NULL) {  /* !!! */
112                         v1= verg->next;
113                         while(v1) {
114                                 if(v1->newv==NULL) {    /* !?! */
115                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
116                                                 /* printf("doublevert\n"); */
117                                                 v1->newv= verg;
118                                         }
119                                 }
120                                 v1= v1->next;
121                         }
122                 }
123                 verg= verg->next;
124         }
125
126         /* replace pointers in edges and faces */
127         se= sc->edgebase.first;
128         while(se) {
129                 if(se->v1->newv) se->v1= se->v1->newv;
130                 if(se->v2->newv) se->v2= se->v2->newv;
131                 /* edges changed: so.... */
132                 sortscrvert(&(se->v1), &(se->v2));
133                 se= se->next;
134         }
135         sa= sc->areabase.first;
136         while(sa) {
137                 if(sa->v1->newv) sa->v1= sa->v1->newv;
138                 if(sa->v2->newv) sa->v2= sa->v2->newv;
139                 if(sa->v3->newv) sa->v3= sa->v3->newv;
140                 if(sa->v4->newv) sa->v4= sa->v4->newv;
141                 sa= sa->next;
142         }
143
144         /* remove */
145         verg= sc->vertbase.first;
146         while(verg) {
147                 v1= verg->next;
148                 if(verg->newv) {
149                         BLI_remlink(&sc->vertbase, verg);
150                         MEM_freeN(verg);
151                 }
152                 verg= v1;
153         }
154
155 }
156
157 void removenotused_scrverts(bScreen *sc)
158 {
159         ScrVert *sv, *svn;
160         ScrEdge *se;
161         
162         /* we assume edges are ok */
163         
164         se= sc->edgebase.first;
165         while(se) {
166                 se->v1->flag= 1;
167                 se->v2->flag= 1;
168                 se= se->next;
169         }
170         
171         sv= sc->vertbase.first;
172         while(sv) {
173                 svn= sv->next;
174                 if(sv->flag==0) {
175                         BLI_remlink(&sc->vertbase, sv);
176                         MEM_freeN(sv);
177                 }
178                 else sv->flag= 0;
179                 sv= svn;
180         }
181 }
182
183 void removedouble_scredges(bScreen *sc)
184 {
185         ScrEdge *verg, *se, *sn;
186         
187         /* compare */
188         verg= sc->edgebase.first;
189         while(verg) {
190                 se= verg->next;
191                 while(se) {
192                         sn= se->next;
193                         if(verg->v1==se->v1 && verg->v2==se->v2) {
194                                 BLI_remlink(&sc->edgebase, se);
195                                 MEM_freeN(se);
196                         }
197                         se= sn;
198                 }
199                 verg= verg->next;
200         }
201 }
202
203 void removenotused_scredges(bScreen *sc)
204 {
205         ScrEdge *se, *sen;
206         ScrArea *sa;
207         int a=0;
208         
209         /* sets flags when edge is used in area */
210         sa= sc->areabase.first;
211         while(sa) {
212                 se= screen_findedge(sc, sa->v1, sa->v2);
213                 if(se==0) printf("error: area %d edge 1 doesn't exist\n", a);
214                 else se->flag= 1;
215                 se= screen_findedge(sc, sa->v2, sa->v3);
216                 if(se==0) printf("error: area %d edge 2 doesn't exist\n", a);
217                 else se->flag= 1;
218                 se= screen_findedge(sc, sa->v3, sa->v4);
219                 if(se==0) printf("error: area %d edge 3 doesn't exist\n", a);
220                 else se->flag= 1;
221                 se= screen_findedge(sc, sa->v4, sa->v1);
222                 if(se==0) printf("error: area %d edge 4 doesn't exist\n", a);
223                 else se->flag= 1;
224                 sa= sa->next;
225                 a++;
226         }
227         se= sc->edgebase.first;
228         while(se) {
229                 sen= se->next;
230                 if(se->flag==0) {
231                         BLI_remlink(&sc->edgebase, se);
232                         MEM_freeN(se);
233                 }
234                 else se->flag= 0;
235                 se= sen;
236         }
237 }
238
239 int scredge_is_horizontal(ScrEdge *se)
240 {
241         return (se->v1->vec.y == se->v2->vec.y);
242 }
243
244 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
245 {
246         ScrEdge *se;
247         
248         for (se= sc->edgebase.first; se; se= se->next) {
249                 if (scredge_is_horizontal(se)) {
250                         short min, max;
251                         min= MIN2(se->v1->vec.x, se->v2->vec.x);
252                         max= MAX2(se->v1->vec.x, se->v2->vec.x);
253                         
254                         if (abs(my-se->v1->vec.y)<=2 && mx>=min && mx<=max)
255                                 return se;
256                 } 
257                 else {
258                         short min, max;
259                         min= MIN2(se->v1->vec.y, se->v2->vec.y);
260                         max= MAX2(se->v1->vec.y, se->v2->vec.y);
261                         
262                         if (abs(mx-se->v1->vec.x)<=2 && my>=min && my<=max)
263                                 return se;
264                 }
265         }
266         
267         return NULL;
268 }
269
270
271
272 /* adds no space data */
273 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
274 {
275         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
276         sa->v1= v1;
277         sa->v2= v2;
278         sa->v3= v3;
279         sa->v4= v4;
280         sa->headertype= headertype;
281         sa->spacetype= sa->butspacetype= spacetype;
282         
283         BLI_addtail(&sc->areabase, sa);
284         
285         return sa;
286 }
287
288 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
289 {
290         
291         ED_area_exit(C, sa);
292         
293         BKE_screen_area_free(sa);
294         
295         BLI_remlink(&sc->areabase, sa);
296         MEM_freeN(sa);
297 }
298
299 /* return 0: no split possible */
300 /* else return (integer) screencoordinate split point */
301 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
302 {
303         short x, y;
304         
305         // area big enough?
306         if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
307         if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
308         
309         // to be sure
310         if(fac<0.0) fac= 0.0;
311         if(fac>1.0) fac= 1.0;
312         
313         if(dir=='h') {
314                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
315                 
316                 if(y- sa->v1->vec.y < AREAMINY) 
317                         y= sa->v1->vec.y+ AREAMINY;
318                 else if(sa->v2->vec.y- y < AREAMINY) 
319                         y= sa->v2->vec.y- AREAMINY;
320                 else y-= (y % AREAGRID);
321                 
322                 return y;
323         }
324         else {
325                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
326                 
327                 if(x- sa->v1->vec.x < AREAMINX) 
328                         x= sa->v1->vec.x+ AREAMINX;
329                 else if(sa->v4->vec.x- x < AREAMINX) 
330                         x= sa->v4->vec.x- AREAMINX;
331                 else x-= (x % AREAGRID);
332                 
333                 return x;
334         }
335 }
336
337 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
338 {
339         ScrArea *newa=NULL;
340         ScrVert *sv1, *sv2;
341         short split;
342         
343         if(sa==NULL) return NULL;
344         
345         split= testsplitpoint(win, sa, dir, fac);
346         if(split==0) return NULL;
347         
348         if(dir=='h') {
349                 /* new vertices */
350                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
351                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
352                 
353                 /* new edges */
354                 screen_addedge(sc, sa->v1, sv1);
355                 screen_addedge(sc, sv1, sa->v2);
356                 screen_addedge(sc, sa->v3, sv2);
357                 screen_addedge(sc, sv2, sa->v4);
358                 screen_addedge(sc, sv1, sv2);
359                 
360                 /* new areas: top */
361                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
362                 area_copy_data(newa, sa, 0);
363                 
364                 /* area below */
365                 sa->v2= sv1;
366                 sa->v3= sv2;
367                 
368         }
369         else {
370                 /* new vertices */
371                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
372                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
373                 
374                 /* new edges */
375                 screen_addedge(sc, sa->v1, sv1);
376                 screen_addedge(sc, sv1, sa->v4);
377                 screen_addedge(sc, sa->v2, sv2);
378                 screen_addedge(sc, sv2, sa->v3);
379                 screen_addedge(sc, sv1, sv2);
380                 
381                 /* new areas: left */
382                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
383                 area_copy_data(newa, sa, 0);
384                 
385                 /* area right */
386                 sa->v1= sv1;
387                 sa->v2= sv2;
388         }
389         
390         /* remove double vertices en edges */
391         removedouble_scrverts(sc);
392         removedouble_scredges(sc);
393         removenotused_scredges(sc);
394         
395         return newa;
396 }
397
398 /* empty screen, with 1 dummy area without spacedata */
399 /* uses window size */
400 bScreen *screen_add(wmWindow *win, char *name)
401 {
402         bScreen *sc;
403         ScrVert *sv1, *sv2, *sv3, *sv4;
404         
405         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
406         sc->scene= G.scene;
407         sc->do_refresh= 1;
408         
409         win->screen= sc;
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_INFO);
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         
906         return newsc;
907 }
908
909
910 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
911 {
912         
913         /* generic notes */
914         switch(note->type) {
915                 case WM_NOTE_WINDOW_REDRAW:
916                         win->screen->do_draw= 1;
917                         break;
918                 case WM_NOTE_SCREEN_CHANGED:
919                         win->screen->do_draw= win->screen->do_refresh= 1;
920                         break;
921                 case WM_NOTE_GESTURE_REDRAW:
922                         win->screen->do_gesture= 1;     /* XXX gestures are stored in window, draw per region... a bit weak? wait for proper composite? (ton) */
923                         break;
924         }
925 }
926
927
928 void ED_screen_draw(wmWindow *win)
929 {
930         ScrArea *sa;
931         ScrArea *sa1=NULL;
932         ScrArea *sa2=NULL;
933         int dir = -1;
934         int dira = -1;
935
936         wm_subwindow_set(win, win->screen->mainwin);
937         
938         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
939                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
940                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
941                 drawscredge_area(sa);
942         }
943
944         /* blended join arrow */
945         if (sa1 && sa2) {
946                 dir = area_getorientation(win->screen, sa1, sa2);
947                 if (dir >= 0) {
948                         switch(dir) {
949                                 case 0: /* W */
950                                         dir = 'r';
951                                         dira = 'l';
952                                         break;
953                                 case 1: /* N */
954                                         dir = 'd';
955                                         dira = 'u';
956                                         break;
957                                 case 2: /* E */
958                                         dir = 'l';
959                                         dira = 'r';
960                                         break;
961                                 case 3: /* S */
962                                         dir = 'u';
963                                         dira = 'd';
964                                         break;
965                         }
966                 }
967                 scrarea_draw_shape_dark(sa2, dir);
968                 scrarea_draw_shape_light(sa1, dira);
969         }
970         
971         if(G.f & G_DEBUG) printf("draw screen\n");
972         win->screen->do_draw= 0;
973 }
974
975 /* make this screen usable */
976 /* for file read and first use, for scaling window, area moves */
977 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
978 {
979         ScrArea *sa;
980         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
981         
982         screen_test_scale(win->screen, win->sizex, win->sizey);
983         
984         if(win->screen->mainwin==0)
985                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
986         else
987                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
988         
989         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
990                 /* set spacetype and region callbacks, calls init() */
991                 /* sets subwindows for regions, adds handlers */
992                 ED_area_initialize(wm, win, sa);
993         }
994
995         if(G.f & G_DEBUG) printf("set screen\n");
996         win->screen->do_refresh= 0;
997 }
998
999 /* file read, set all screens, ... */
1000 void ED_screens_initialize(wmWindowManager *wm)
1001 {
1002         wmWindow *win;
1003         
1004         for(win= wm->windows.first; win; win= win->next) {
1005                 
1006                 if(win->screen==NULL)
1007                         win->screen= G.main->screen.first;
1008                 
1009                 ED_screen_refresh(wm, win);
1010         }
1011 }
1012
1013 void ED_region_exit(bContext *C, ARegion *ar)
1014 {
1015         ARegion *prevar= C->region;
1016
1017         C->region= ar;
1018         WM_event_remove_handlers(C, &ar->handlers);
1019         C->region= prevar;
1020 }
1021
1022 void ED_area_exit(bContext *C, ScrArea *sa)
1023 {
1024         ScrArea *prevsa= C->area;
1025         ARegion *ar;
1026
1027         C->area= sa;
1028         for(ar= sa->regionbase.first; ar; ar= ar->next)
1029                 ED_region_exit(C, ar);
1030
1031         WM_event_remove_handlers(C, &sa->handlers);
1032         C->area= prevsa;
1033 }
1034
1035 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1036 {
1037         wmWindow *prevwin= C?C->window:NULL;
1038         ScrArea *sa;
1039         ARegion *ar;
1040
1041         C->window= window;
1042         for(ar= screen->regionbase.first; ar; ar= ar->next)
1043                 ED_region_exit(C, ar);
1044
1045         for(sa= screen->areabase.first; sa; sa= sa->next)
1046                 ED_area_exit(C, sa);
1047
1048         WM_event_remove_handlers(C, &window->handlers);
1049         C->window= prevwin;
1050 }
1051
1052 /* case when on area-edge or in azones */
1053 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1054 {
1055         ScrArea *sa;
1056         
1057         for(sa= win->screen->areabase.first; sa; sa= sa->next)
1058                 if(is_in_area_actionzone(sa, event->x, event->y))
1059                         break;
1060         
1061         if(sa) {
1062                 WM_cursor_set(win, CURSOR_EDIT);
1063         }
1064         else {
1065                 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1066                 
1067                 if (actedge && scredge_is_horizontal(actedge)) {
1068                         WM_cursor_set(win, CURSOR_Y_MOVE);
1069                 } else {
1070                         WM_cursor_set(win, CURSOR_X_MOVE);
1071                 }
1072         } 
1073 }
1074
1075
1076 /* called in wm_event_system.c. sets state var in screen */
1077 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1078 {
1079         if(win->screen) {
1080                 bScreen *scr= win->screen;
1081                 ScrArea *sa;
1082                 ARegion *ar;
1083                 int oldswin= scr->subwinactive;
1084
1085                 for(sa= scr->areabase.first; sa; sa= sa->next) {
1086                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1087                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1088                                         if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1089                                                 break;
1090                 }
1091                 if(sa) {
1092                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1093                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1094                                         scr->subwinactive= ar->swinid;
1095                         }
1096                 }
1097                 else
1098                         scr->subwinactive= scr->mainwin;
1099                 
1100                 /* check for redraw headers */
1101                 if(oldswin!=scr->subwinactive) {
1102
1103                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1104                                 int do_draw= 0;
1105                                 
1106                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1107                                         if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1108                                                 do_draw= 1;
1109                                 
1110                                 if(do_draw) {
1111                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1112                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1113                                                         ar->do_draw= 1; /* XXX */
1114                                 }
1115                         }
1116                 }
1117                 
1118                 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1119                 if(scr->subwinactive==scr->mainwin) {
1120                         screen_cursor_set(win, event);
1121                 }
1122                 else if(oldswin!=scr->subwinactive) {
1123                         /* cursor space type switching */
1124                         for(sa= scr->areabase.first; sa; sa= sa->next) {
1125                                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1126                                         if(ar->swinid==scr->subwinactive) {
1127                                                 if(sa->type->cursor)
1128                                                         sa->type->cursor(win, ar);
1129                                                 else 
1130                                                         WM_cursor_set(win, CURSOR_STD);
1131                                         }
1132                                 }
1133                                         
1134                         }
1135                 }
1136         }
1137 }
1138
1139 int ED_screen_area_active(const bContext *C)
1140 {
1141
1142         if(C->screen && C->area) {
1143                 ARegion *ar;
1144                 for(ar= C->area->regionbase.first; ar; ar= ar->next)
1145                         if(ar->swinid == C->screen->subwinactive)
1146                                 return 1;
1147         }       
1148         return 0;
1149 }