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