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 /* adds no space data */
240 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
241 {
242         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
243         sa->v1= v1;
244         sa->v2= v2;
245         sa->v3= v3;
246         sa->v4= v4;
247         sa->headertype= headertype;
248         sa->spacetype= sa->butspacetype= spacetype;
249         
250         BLI_addtail(&sc->areabase, sa);
251         
252         return sa;
253 }
254
255 static void screen_delarea(bScreen *sc, ScrArea *sa)
256 {
257         /* XXX need context to cancel operators ED_area_exit(C, sa); */
258         BKE_screen_area_free(sa);
259         BLI_remlink(&sc->areabase, sa);
260         MEM_freeN(sa);
261 }
262
263 /* return 0: no split possible */
264 /* else return (integer) screencoordinate split point */
265 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
266 {
267         short x, y;
268         
269         // area big enough?
270         if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
271         if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
272         
273         // to be sure
274         if(fac<0.0) fac= 0.0;
275         if(fac>1.0) fac= 1.0;
276         
277         if(dir=='h') {
278                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
279                 
280                 if(y- sa->v1->vec.y < AREAMINY) 
281                         y= sa->v1->vec.y+ AREAMINY;
282                 else if(sa->v2->vec.y- y < AREAMINY) 
283                         y= sa->v2->vec.y- AREAMINY;
284                 else y-= (y % AREAGRID);
285                 
286                 return y;
287         }
288         else {
289                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
290                 
291                 if(x- sa->v1->vec.x < AREAMINX) 
292                         x= sa->v1->vec.x+ AREAMINX;
293                 else if(sa->v4->vec.x- x < AREAMINX) 
294                         x= sa->v4->vec.x- AREAMINX;
295                 else x-= (x % AREAGRID);
296                 
297                 return x;
298         }
299 }
300
301 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
302 {
303         ScrArea *newa=NULL;
304         ScrVert *sv1, *sv2;
305         short split;
306         
307         if(sa==NULL) return NULL;
308         
309         split= testsplitpoint(win, sa, dir, fac);
310         if(split==0) return NULL;
311         
312         if(dir=='h') {
313                 /* new vertices */
314                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
315                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
316                 
317                 /* new edges */
318                 screen_addedge(sc, sa->v1, sv1);
319                 screen_addedge(sc, sv1, sa->v2);
320                 screen_addedge(sc, sa->v3, sv2);
321                 screen_addedge(sc, sv2, sa->v4);
322                 screen_addedge(sc, sv1, sv2);
323                 
324                 /* new areas: top */
325                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
326                 area_copy_data(newa, sa, 0);
327                 
328                 /* area below */
329                 sa->v2= sv1;
330                 sa->v3= sv2;
331                 
332         }
333         else {
334                 /* new vertices */
335                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
336                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
337                 
338                 /* new edges */
339                 screen_addedge(sc, sa->v1, sv1);
340                 screen_addedge(sc, sv1, sa->v4);
341                 screen_addedge(sc, sa->v2, sv2);
342                 screen_addedge(sc, sv2, sa->v3);
343                 screen_addedge(sc, sv1, sv2);
344                 
345                 /* new areas: left */
346                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
347                 area_copy_data(newa, sa, 0);
348                 
349                 /* area right */
350                 sa->v1= sv1;
351                 sa->v2= sv2;
352         }
353         
354         /* remove double vertices en edges */
355         removedouble_scrverts(sc);
356         removedouble_scredges(sc);
357         removenotused_scredges(sc);
358         
359         return newa;
360 }
361
362 /* empty screen, with 1 dummy area without spacedata */
363 /* uses window size */
364 bScreen *screen_add(wmWindow *win, char *name)
365 {
366         bScreen *sc;
367         ScrVert *sv1, *sv2, *sv3, *sv4;
368         
369         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
370         sc->scene= G.scene;
371         sc->do_refresh= 1;
372         
373         win->screen= sc;
374         
375         sv1= screen_addvert(sc, 0, 0);
376         sv2= screen_addvert(sc, 0, win->sizey-1);
377         sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
378         sv4= screen_addvert(sc, win->sizex-1, 0);
379         
380         screen_addedge(sc, sv1, sv2);
381         screen_addedge(sc, sv2, sv3);
382         screen_addedge(sc, sv3, sv4);
383         screen_addedge(sc, sv4, sv1);
384         
385         /* dummy type, no spacedata */
386         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_INFO);
387                 
388         return sc;
389 }
390
391 static void screen_copy(bScreen *to, bScreen *from)
392 {
393         ScrVert *s1, *s2;
394         ScrEdge *se;
395         ScrArea *sa, *saf;
396         
397         /* free contents of 'to', is from blenkernel screen.c */
398         free_screen(to);
399         
400         BLI_duplicatelist(&to->vertbase, &from->vertbase);
401         BLI_duplicatelist(&to->edgebase, &from->edgebase);
402         BLI_duplicatelist(&to->areabase, &from->areabase);
403         to->regionbase.first= to->regionbase.last= NULL;
404         
405         s2= to->vertbase.first;
406         for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
407                 s1->newv= s2;
408         }
409         
410         for(se= to->edgebase.first; se; se= se->next) {
411                 se->v1= se->v1->newv;
412                 se->v2= se->v2->newv;
413                 sortscrvert(&(se->v1), &(se->v2));
414         }
415         
416         saf= from->areabase.first;
417         for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
418                 sa->v1= sa->v1->newv;
419                 sa->v2= sa->v2->newv;
420                 sa->v3= sa->v3->newv;
421                 sa->v4= sa->v4->newv;
422                 
423                 sa->spacedata.first= sa->spacedata.last= NULL;
424                 sa->uiblocks.first= sa->uiblocks.last= NULL;
425                 sa->panels.first= sa->panels.last= NULL;
426                 sa->regionbase.first= sa->regionbase.last= NULL;
427                 sa->actionzones.first= sa->actionzones.last= NULL;
428                 sa->scriptlink.totscript= 0;
429                 
430                 area_copy_data(sa, saf, 0);
431         }
432         
433         /* put at zero (needed?) */
434         for(s1= from->vertbase.first; s1; s1= s1->next)
435                 s1->newv= NULL;
436
437 }
438
439
440 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
441 /* -1 = not valid check */
442 /* used with join operator */
443 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
444 {
445         ScrVert *sav1, *sav2, *sav3, *sav4;
446         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
447
448         if(sa==NULL || sb==NULL) return -1;
449
450         sav1= sa->v1;
451         sav2= sa->v2;
452         sav3= sa->v3;
453         sav4= sa->v4;
454         sbv1= sb->v1;
455         sbv2= sb->v2;
456         sbv3= sb->v3;
457         sbv4= sb->v4;
458         
459         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
460                 return 0;
461         }
462         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
463                 return 1;
464         }
465         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
466                 return 2;
467         }
468         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
469                 return 3;
470         }
471         
472         return -1;
473 }
474
475 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
476 *       used by the split, join operators
477 */
478 int screen_area_join(bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
479 {
480         int dir;
481         
482         dir = area_getorientation(scr, sa1, sa2);
483         /*printf("dir is : %i \n", dir);*/
484         
485         if (dir < 0)
486         {
487                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
488                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
489                 return 0;
490         }
491         
492         if(dir == 0) {
493                 sa1->v1= sa2->v1;
494                 sa1->v2= sa2->v2;
495                 screen_addedge(scr, sa1->v2, sa1->v3);
496                 screen_addedge(scr, sa1->v1, sa1->v4);
497         }
498         else if(dir == 1) {
499                 sa1->v2= sa2->v2;
500                 sa1->v3= sa2->v3;
501                 screen_addedge(scr, sa1->v1, sa1->v2);
502                 screen_addedge(scr, sa1->v3, sa1->v4);
503         }
504         else if(dir == 2) {
505                 sa1->v3= sa2->v3;
506                 sa1->v4= sa2->v4;
507                 screen_addedge(scr, sa1->v2, sa1->v3);
508                 screen_addedge(scr, sa1->v1, sa1->v4);
509         }
510         else if(dir == 3) {
511                 sa1->v1= sa2->v1;
512                 sa1->v4= sa2->v4;
513                 screen_addedge(scr, sa1->v1, sa1->v2);
514                 screen_addedge(scr, sa1->v3, sa1->v4);
515         }
516         
517         screen_delarea(scr, sa2);
518         removedouble_scrverts(scr);
519         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
520         
521         return 1;
522 }
523
524 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
525 {
526         ScrEdge *se;
527         ScrVert *sv;
528         int oneselected;
529         char dir;
530         
531         /* select connected, only in the right direction */
532         /* 'dir' is the direction of EDGE */
533         
534         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
535         else dir= 'h';
536         
537         sv= sc->vertbase.first;
538         while(sv) {
539                 sv->flag = 0;
540                 sv= sv->next;
541         }
542         
543         edge->v1->flag= 1;
544         edge->v2->flag= 1;
545         
546         oneselected= 1;
547         while(oneselected) {
548                 se= sc->edgebase.first;
549                 oneselected= 0;
550                 while(se) {
551                         if(se->v1->flag + se->v2->flag==1) {
552                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
553                                         se->v1->flag= se->v2->flag= 1;
554                                         oneselected= 1;
555                                 }
556                                         if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
557                                                 se->v1->flag= se->v2->flag= 1;
558                                                 oneselected= 1;
559                                         }
560                         }
561                                 se= se->next;
562                 }
563         }
564 }
565
566 /* test if screen vertices should be scaled */
567 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
568 {
569         ScrVert *sv=NULL;
570         ScrArea *sa, *san;
571         int sizex, sizey;
572         float facx, facy, tempf, min[2], max[2];
573         
574         /* calculate size */
575         min[0]= min[1]= 10000.0f;
576         max[0]= max[1]= 0.0f;
577         
578         for(sv= sc->vertbase.first; sv; sv= sv->next) {
579                 min[0]= MIN2(min[0], sv->vec.x);
580                 min[1]= MIN2(min[1], sv->vec.y);
581                 max[0]= MAX2(max[0], sv->vec.x);
582                 max[1]= MAX2(max[1], sv->vec.y);
583         }
584         
585         /* always make 0.0 left under */
586         for(sv= sc->vertbase.first; sv; sv= sv->next) {
587                 sv->vec.x -= min[0];
588                 sv->vec.y -= min[1];
589         }
590         
591         sizex= max[0]-min[0];
592         sizey= max[1]-min[1];
593         
594         if(sizex!= winsizex || sizey!= winsizey) {
595                 facx= winsizex;
596                 facx/= (float)sizex;
597                 facy= winsizey;
598                 facy/= (float)sizey;
599                 
600                 /* make sure it fits! */
601                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
602                         tempf= ((float)sv->vec.x)*facx;
603                         sv->vec.x= (short)(tempf+0.5);
604                         sv->vec.x+= AREAGRID-1;
605                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
606                         
607                         CLAMP(sv->vec.x, 0, winsizex);
608                         
609                         tempf= ((float)sv->vec.y )*facy;
610                         sv->vec.y= (short)(tempf+0.5);
611                         sv->vec.y+= AREAGRID-1;
612                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
613                         
614                         CLAMP(sv->vec.y, 0, winsizey);
615                 }
616         }
617         
618         /* test for collapsed areas. This could happen in some blender version... */
619         for(sa= sc->areabase.first; sa; sa= san) {
620                 san= sa->next;
621                 if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3)
622                         screen_delarea(sc, sa);
623         }
624         
625         /* make each window at least HEADERY high */
626         for(sa= sc->areabase.first; sa; sa= sa->next) {
627                 int headery= HEADERY+1;
628                 
629                 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
630                         /* lower edge */
631                         ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
632                         if(se && sa->v1!=sa->v2 ) {
633                                 int yval;
634                                 
635                                 select_connected_scredge(sc, se);
636                                 
637                                 /* all selected vertices get the right offset */
638                                 yval= sa->v2->vec.y-headery;
639                                 sv= sc->vertbase.first;
640                                 while(sv) {
641                                         /* if is a collapsed area */
642                                         if(sv!=sa->v2 && sv!=sa->v3) {
643                                                 if(sv->flag) sv->vec.y= yval;
644                                         }
645                                         sv= sv->next;
646                                 }
647                         }
648                 }
649         }
650         
651 }
652
653 /* *********************** DRAWING **************************************** */
654
655
656 #define SCR_BACK 0.55
657 #define SCR_ROUND 12
658
659 /* draw vertical shape visualising future joining (left as well
660  * right direction of future joining) */
661 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
662 {
663         vec2f points[10];
664         short i;
665         float w, h;
666         float width = sa->v3->vec.x - sa->v1->vec.x;
667         float height = sa->v3->vec.y - sa->v1->vec.y;
668
669         if(height<width) {
670                 h = height/8;
671                 w = height/4;
672         }
673         else {
674                 h = width/8;
675                 w = width/4;
676         }
677
678         points[0].x = sa->v1->vec.x;
679         points[0].y = sa->v1->vec.y + height/2;
680
681         points[1].x = sa->v1->vec.x;
682         points[1].y = sa->v1->vec.y;
683
684         points[2].x = sa->v4->vec.x - w;
685         points[2].y = sa->v4->vec.y;
686
687         points[3].x = sa->v4->vec.x - w;
688         points[3].y = sa->v4->vec.y + height/2 - 2*h;
689
690         points[4].x = sa->v4->vec.x - 2*w;
691         points[4].y = sa->v4->vec.y + height/2;
692
693         points[5].x = sa->v4->vec.x - w;
694         points[5].y = sa->v4->vec.y + height/2 + 2*h;
695
696         points[6].x = sa->v3->vec.x - w;
697         points[6].y = sa->v3->vec.y;
698
699         points[7].x = sa->v2->vec.x;
700         points[7].y = sa->v2->vec.y;
701
702         points[8].x = sa->v4->vec.x;
703         points[8].y = sa->v4->vec.y + height/2 - h;
704
705         points[9].x = sa->v4->vec.x;
706         points[9].y = sa->v4->vec.y + height/2 + h;
707
708         if(dir=='l') {
709                 /* when direction is left, then we flip direction of arrow */
710                 float cx = sa->v1->vec.x + width;
711                 for(i=0;i<10;i++) {
712                         points[i].x -= cx;
713                         points[i].x = -points[i].x;
714                         points[i].x += sa->v1->vec.x;
715                 }
716         }
717
718         glBegin(GL_POLYGON);
719         for(i=0;i<5;i++)
720                 glVertex2f(points[i].x, points[i].y);
721         glEnd();
722         glBegin(GL_POLYGON);
723         for(i=4;i<8;i++)
724                 glVertex2f(points[i].x, points[i].y);
725         glVertex2f(points[0].x, points[0].y);
726         glEnd();
727
728         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
729         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
730 }
731
732 /* draw vertical shape visualising future joining (up/down direction) */
733 static void draw_vertical_join_shape(ScrArea *sa, char dir)
734 {
735         vec2f points[10];
736         short i;
737         float w, h;
738         float width = sa->v3->vec.x - sa->v1->vec.x;
739         float height = sa->v3->vec.y - sa->v1->vec.y;
740
741         if(height<width) {
742                 h = height/4;
743                 w = height/8;
744         }
745         else {
746                 h = width/4;
747                 w = width/8;
748         }
749
750         points[0].x = sa->v1->vec.x + width/2;
751         points[0].y = sa->v3->vec.y;
752
753         points[1].x = sa->v2->vec.x;
754         points[1].y = sa->v2->vec.y;
755
756         points[2].x = sa->v1->vec.x;
757         points[2].y = sa->v1->vec.y + h;
758
759         points[3].x = sa->v1->vec.x + width/2 - 2*w;
760         points[3].y = sa->v1->vec.y + h;
761
762         points[4].x = sa->v1->vec.x + width/2;
763         points[4].y = sa->v1->vec.y + 2*h;
764
765         points[5].x = sa->v1->vec.x + width/2 + 2*w;
766         points[5].y = sa->v1->vec.y + h;
767
768         points[6].x = sa->v4->vec.x;
769         points[6].y = sa->v4->vec.y + h;
770         
771         points[7].x = sa->v3->vec.x;
772         points[7].y = sa->v3->vec.y;
773
774         points[8].x = sa->v1->vec.x + width/2 - w;
775         points[8].y = sa->v1->vec.y;
776
777         points[9].x = sa->v1->vec.x + width/2 + w;
778         points[9].y = sa->v1->vec.y;
779
780         if(dir=='u') {
781                 /* when direction is up, then we flip direction of arrow */
782                 float cy = sa->v1->vec.y + height;
783                 for(i=0;i<10;i++) {
784                         points[i].y -= cy;
785                         points[i].y = -points[i].y;
786                         points[i].y += sa->v1->vec.y;
787                 }
788         }
789
790         glBegin(GL_POLYGON);
791         for(i=0;i<5;i++)
792                 glVertex2f(points[i].x, points[i].y);
793         glEnd();
794         glBegin(GL_POLYGON);
795         for(i=4;i<8;i++)
796                 glVertex2f(points[i].x, points[i].y);
797         glVertex2f(points[0].x, points[0].y);
798         glEnd();
799
800         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
801         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
802 }
803
804 /* draw join shape due to direction of joining */
805 static void draw_join_shape(ScrArea *sa, char dir)
806 {
807         if(dir=='u' || dir=='d')
808                 draw_vertical_join_shape(sa, dir);
809         else
810                 draw_horizontal_join_shape(sa, dir);
811 }
812
813 /* draw screen area darker with arrow (visualisation of future joining) */
814 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
815 {
816         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
817         glEnable(GL_BLEND);
818         glColor4ub(0, 0, 0, 50);
819         draw_join_shape(sa, dir);
820         glDisable(GL_BLEND);
821 }
822
823 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
824 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
825 {
826         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
827         glEnable(GL_BLEND);
828         /* value 181 was hardly computed: 181~105 */
829         glColor4ub(255, 255, 255, 50);          
830         /* draw_join_shape(sa, dir); */
831         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
832         glDisable(GL_BLEND);
833 }
834
835 /** screen edges drawing **/
836 static void drawscredge_area(ScrArea *sa)
837 {
838         short x1= sa->v1->vec.x;
839         short y1= sa->v1->vec.y;
840         short x2= sa->v3->vec.x;
841         short y2= sa->v3->vec.y;
842         
843         cpack(0x0);
844         
845         /* right border area */
846         sdrawline(x2, y1, x2, y2);
847         
848         /* left border area */
849         if(x1>0) { /* otherwise it draws the emboss of window over */
850                 sdrawline(x1, y1, x1, y2);
851         }
852         
853         /* top border area */
854         sdrawline(x1, y2, x2, y2);
855         
856         /* bottom border area */
857         sdrawline(x1, y1, x2, y1);
858         
859 }
860
861 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
862
863 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
864 {
865         bScreen *newsc;
866         
867         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
868         
869         /* make new empty screen: */
870         newsc= screen_add(win, sc->id.name+2);
871         /* copy all data */
872         screen_copy(newsc, sc);
873         
874         return newsc;
875 }
876
877
878 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
879 {
880         
881         /* generic notes */
882         switch(note->type) {
883                 case WM_NOTE_WINDOW_REDRAW:
884                         win->screen->do_draw= 1;
885                         break;
886                 case WM_NOTE_SCREEN_CHANGED:
887                         win->screen->do_draw= win->screen->do_refresh= 1;
888                         break;
889                 case WM_NOTE_GESTURE_REDRAW:
890                         win->screen->do_gesture= 1;     /* XXX gestures are stored in window, draw per region... a bit weak? wait for proper composite? (ton) */
891                         break;
892         }
893 }
894
895
896 void ED_screen_draw(wmWindow *win)
897 {
898         ScrArea *sa;
899         ScrArea *sa1=NULL;
900         ScrArea *sa2=NULL;
901         int dir = -1;
902         int dira = -1;
903
904         wm_subwindow_set(win, win->screen->mainwin);
905         
906         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
907                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
908                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
909                 drawscredge_area(sa);
910         }
911
912         /* blended join arrow */
913         if (sa1 && sa2) {
914                 dir = area_getorientation(win->screen, sa1, sa2);
915                 if (dir >= 0) {
916                         switch(dir) {
917                                 case 0: /* W */
918                                         dir = 'r';
919                                         dira = 'l';
920                                         break;
921                                 case 1: /* N */
922                                         dir = 'd';
923                                         dira = 'u';
924                                         break;
925                                 case 2: /* E */
926                                         dir = 'l';
927                                         dira = 'r';
928                                         break;
929                                 case 3: /* S */
930                                         dir = 'u';
931                                         dira = 'd';
932                                         break;
933                         }
934                 }
935                 scrarea_draw_shape_dark(sa2, dir);
936                 scrarea_draw_shape_light(sa1, dira);
937         }
938         
939         if(G.f & G_DEBUG) printf("draw screen\n");
940         win->screen->do_draw= 0;
941 }
942
943 /* make this screen usable */
944 /* for file read and first use, for scaling window, area moves */
945 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
946 {
947         ScrArea *sa;
948         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
949         
950         screen_test_scale(win->screen, win->sizex, win->sizey);
951         
952         if(win->screen->mainwin==0)
953                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
954         else
955                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
956         
957         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
958                 /* set spacetype and region callbacks, calls init() */
959                 /* sets subwindows for regions, adds handlers */
960                 ED_area_initialize(wm, win, sa);
961         }
962
963         if(G.f & G_DEBUG) printf("set screen\n");
964         win->screen->do_refresh= 0;
965
966 }
967
968 /* file read, set all screens, ... */
969 void ED_screens_initialize(wmWindowManager *wm)
970 {
971         wmWindow *win;
972         
973         for(win= wm->windows.first; win; win= win->next) {
974                 
975                 if(win->screen==NULL)
976                         win->screen= G.main->screen.first;
977                 
978                 ED_screen_refresh(wm, win);
979         }
980 }
981
982 void ED_region_exit(bContext *C, ARegion *ar)
983 {
984         ARegion *prevar= C->region;
985
986         C->region= ar;
987         WM_event_remove_handlers(C, &ar->handlers);
988         C->region= prevar;
989 }
990
991 void ED_area_exit(bContext *C, ScrArea *sa)
992 {
993         ScrArea *prevsa= C->area;
994         ARegion *ar;
995
996         C->area= sa;
997         for(ar= sa->regionbase.first; ar; ar= ar->next)
998                 ED_region_exit(C, ar);
999
1000         WM_event_remove_handlers(C, &sa->handlers);
1001         C->area= prevsa;
1002 }
1003
1004 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1005 {
1006         wmWindow *prevwin= C?C->window:NULL;
1007         ScrArea *sa;
1008         ARegion *ar;
1009
1010         C->window= window;
1011         for(ar= screen->regionbase.first; ar; ar= ar->next)
1012                 ED_region_exit(C, ar);
1013
1014         for(sa= screen->areabase.first; sa; sa= sa->next)
1015                 ED_area_exit(C, sa);
1016
1017         WM_event_remove_handlers(C, &window->handlers);
1018         C->window= prevwin;
1019 }
1020
1021
1022 /* called in wm_event_system.c. sets state var in screen */
1023 void ED_screen_set_subwinactive(wmWindow *win)
1024 {
1025         if(win->screen) {
1026                 wmEvent *event= win->eventstate;
1027                 ScrArea *sa;
1028                 int oldswin= win->screen->subwinactive;
1029                 
1030                 /* XXX prevent this call for modal menus now */
1031                 if(win->screen->regionbase.first)
1032                         return;
1033                 
1034                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1035                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1036                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1037                                         break;
1038                 }
1039                 if(sa) {
1040                         ARegion *ar;
1041                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1042                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1043                                         win->screen->subwinactive= ar->swinid;
1044                         }
1045                 }
1046                 else
1047                         win->screen->subwinactive= win->screen->mainwin;
1048                 
1049                 /* check for redraw headers */
1050                 if(oldswin!=win->screen->subwinactive) {
1051                         
1052                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1053                                 ARegion *ar;
1054                                 int do_draw= 0;
1055                                 
1056                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
1057                                         if(ar->swinid==oldswin || ar->swinid==win->screen->subwinactive)
1058                                                 do_draw= 1;
1059                                 
1060                                 if(do_draw) {
1061                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
1062                                                 if(ar->regiontype==RGN_TYPE_HEADER)
1063                                                         ar->do_draw= 1; /* XXX */
1064                                 }
1065                         }
1066                 }
1067         }
1068 }
1069
1070 int ED_screen_area_active(const bContext *C)
1071 {
1072
1073         if(C->screen && C->area) {
1074                 ARegion *ar;
1075                 for(ar= C->area->regionbase.first; ar; ar= ar->next)
1076                         if(ar->swinid == C->screen->subwinactive)
1077                                 return 1;
1078         }       
1079         return 0;
1080 }