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
525 /* test if screen vertices should be scaled */
526 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
527 {
528         ScrVert *sv=NULL;
529         ScrArea *sa, *san;
530         int sizex, sizey;
531         float facx, facy, tempf, min[2], max[2];
532         
533         /* calculate size */
534         min[0]= min[1]= 10000.0f;
535         max[0]= max[1]= 0.0f;
536         
537         for(sv= sc->vertbase.first; sv; sv= sv->next) {
538                 min[0]= MIN2(min[0], sv->vec.x);
539                 min[1]= MIN2(min[1], sv->vec.y);
540                 max[0]= MAX2(max[0], sv->vec.x);
541                 max[1]= MAX2(max[1], sv->vec.y);
542         }
543         
544         /* always make 0.0 left under */
545         for(sv= sc->vertbase.first; sv; sv= sv->next) {
546                 sv->vec.x -= min[0];
547                 sv->vec.y -= min[1];
548         }
549         
550         sizex= max[0]-min[0];
551         sizey= max[1]-min[1];
552         
553         if(sizex!= winsizex || sizey!= winsizey) {
554                 facx= winsizex;
555                 facx/= (float)sizex;
556                 facy= winsizey;
557                 facy/= (float)sizey;
558                 
559                 /* make sure it fits! */
560                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
561                         tempf= ((float)sv->vec.x)*facx;
562                         sv->vec.x= (short)(tempf+0.5);
563                         sv->vec.x+= AREAGRID-1;
564                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
565                         
566                         CLAMP(sv->vec.x, 0, winsizex);
567                         
568                         tempf= ((float)sv->vec.y )*facy;
569                         sv->vec.y= (short)(tempf+0.5);
570                         sv->vec.y+= AREAGRID-1;
571                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
572                         
573                         CLAMP(sv->vec.y, 0, winsizey);
574                 }
575         }
576         
577         /* test for collapsed areas. This could happen in some blender version... */
578         for(sa= sc->areabase.first; sa; sa= san) {
579                 san= sa->next;
580                 if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3)
581                         screen_delarea(sc, sa);
582         }
583 }
584
585 /* *********************** DRAWING **************************************** */
586
587
588 #define SCR_BACK 0.55
589 #define SCR_ROUND 12
590
591 /* draw vertical shape visualising future joining (left as well
592  * right direction of future joining) */
593 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
594 {
595         vec2f points[10];
596         short i;
597         float w, h;
598         float width = sa->v3->vec.x - sa->v1->vec.x;
599         float height = sa->v3->vec.y - sa->v1->vec.y;
600
601         if(height<width) {
602                 h = height/8;
603                 w = height/4;
604         }
605         else {
606                 h = width/8;
607                 w = width/4;
608         }
609
610         points[0].x = sa->v1->vec.x;
611         points[0].y = sa->v1->vec.y + height/2;
612
613         points[1].x = sa->v1->vec.x;
614         points[1].y = sa->v1->vec.y;
615
616         points[2].x = sa->v4->vec.x - w;
617         points[2].y = sa->v4->vec.y;
618
619         points[3].x = sa->v4->vec.x - w;
620         points[3].y = sa->v4->vec.y + height/2 - 2*h;
621
622         points[4].x = sa->v4->vec.x - 2*w;
623         points[4].y = sa->v4->vec.y + height/2;
624
625         points[5].x = sa->v4->vec.x - w;
626         points[5].y = sa->v4->vec.y + height/2 + 2*h;
627
628         points[6].x = sa->v3->vec.x - w;
629         points[6].y = sa->v3->vec.y;
630
631         points[7].x = sa->v2->vec.x;
632         points[7].y = sa->v2->vec.y;
633
634         points[8].x = sa->v4->vec.x;
635         points[8].y = sa->v4->vec.y + height/2 - h;
636
637         points[9].x = sa->v4->vec.x;
638         points[9].y = sa->v4->vec.y + height/2 + h;
639
640         if(dir=='l') {
641                 /* when direction is left, then we flip direction of arrow */
642                 float cx = sa->v1->vec.x + width;
643                 for(i=0;i<10;i++) {
644                         points[i].x -= cx;
645                         points[i].x = -points[i].x;
646                         points[i].x += sa->v1->vec.x;
647                 }
648         }
649
650         glBegin(GL_POLYGON);
651         for(i=0;i<5;i++)
652                 glVertex2f(points[i].x, points[i].y);
653         glEnd();
654         glBegin(GL_POLYGON);
655         for(i=4;i<8;i++)
656                 glVertex2f(points[i].x, points[i].y);
657         glVertex2f(points[0].x, points[0].y);
658         glEnd();
659
660         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
661         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
662 }
663
664 /* draw vertical shape visualising future joining (up/down direction) */
665 static void draw_vertical_join_shape(ScrArea *sa, char dir)
666 {
667         vec2f points[10];
668         short i;
669         float w, h;
670         float width = sa->v3->vec.x - sa->v1->vec.x;
671         float height = sa->v3->vec.y - sa->v1->vec.y;
672
673         if(height<width) {
674                 h = height/4;
675                 w = height/8;
676         }
677         else {
678                 h = width/4;
679                 w = width/8;
680         }
681
682         points[0].x = sa->v1->vec.x + width/2;
683         points[0].y = sa->v3->vec.y;
684
685         points[1].x = sa->v2->vec.x;
686         points[1].y = sa->v2->vec.y;
687
688         points[2].x = sa->v1->vec.x;
689         points[2].y = sa->v1->vec.y + h;
690
691         points[3].x = sa->v1->vec.x + width/2 - 2*w;
692         points[3].y = sa->v1->vec.y + h;
693
694         points[4].x = sa->v1->vec.x + width/2;
695         points[4].y = sa->v1->vec.y + 2*h;
696
697         points[5].x = sa->v1->vec.x + width/2 + 2*w;
698         points[5].y = sa->v1->vec.y + h;
699
700         points[6].x = sa->v4->vec.x;
701         points[6].y = sa->v4->vec.y + h;
702         
703         points[7].x = sa->v3->vec.x;
704         points[7].y = sa->v3->vec.y;
705
706         points[8].x = sa->v1->vec.x + width/2 - w;
707         points[8].y = sa->v1->vec.y;
708
709         points[9].x = sa->v1->vec.x + width/2 + w;
710         points[9].y = sa->v1->vec.y;
711
712         if(dir=='u') {
713                 /* when direction is up, then we flip direction of arrow */
714                 float cy = sa->v1->vec.y + height;
715                 for(i=0;i<10;i++) {
716                         points[i].y -= cy;
717                         points[i].y = -points[i].y;
718                         points[i].y += sa->v1->vec.y;
719                 }
720         }
721
722         glBegin(GL_POLYGON);
723         for(i=0;i<5;i++)
724                 glVertex2f(points[i].x, points[i].y);
725         glEnd();
726         glBegin(GL_POLYGON);
727         for(i=4;i<8;i++)
728                 glVertex2f(points[i].x, points[i].y);
729         glVertex2f(points[0].x, points[0].y);
730         glEnd();
731
732         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
733         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
734 }
735
736 /* draw join shape due to direction of joining */
737 static void draw_join_shape(ScrArea *sa, char dir)
738 {
739         if(dir=='u' || dir=='d')
740                 draw_vertical_join_shape(sa, dir);
741         else
742                 draw_horizontal_join_shape(sa, dir);
743 }
744
745 /* draw screen area darker with arrow (visualisation of future joining) */
746 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
747 {
748         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
749         glEnable(GL_BLEND);
750         glColor4ub(0, 0, 0, 50);
751         draw_join_shape(sa, dir);
752         glDisable(GL_BLEND);
753 }
754
755 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
756 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
757 {
758         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
759         glEnable(GL_BLEND);
760         /* value 181 was hardly computed: 181~105 */
761         glColor4ub(255, 255, 255, 50);          
762         /* draw_join_shape(sa, dir); */
763         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
764         glDisable(GL_BLEND);
765 }
766
767 /** screen edges drawing **/
768 static void drawscredge_area(ScrArea *sa)
769 {
770         short x1= sa->v1->vec.x;
771         short y1= sa->v1->vec.y;
772         short x2= sa->v3->vec.x;
773         short y2= sa->v3->vec.y;
774         
775         cpack(0x0);
776         
777         /* right border area */
778         sdrawline(x2, y1, x2, y2);
779         
780         /* left border area */
781         if(x1>0) { /* otherwise it draws the emboss of window over */
782                 sdrawline(x1, y1, x1, y2);
783         }
784         
785         /* top border area */
786         sdrawline(x1, y2, x2, y2);
787         
788         /* bottom border area */
789         sdrawline(x1, y1, x2, y1);
790         
791 }
792
793 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
794
795 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
796 {
797         bScreen *newsc;
798         
799         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
800         
801         /* make new empty screen: */
802         newsc= screen_add(win, sc->id.name+2);
803         /* copy all data */
804         screen_copy(newsc, sc);
805         
806         return newsc;
807 }
808
809
810 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
811 {
812         
813         /* generic notes */
814         switch(note->type) {
815                 case WM_NOTE_WINDOW_REDRAW:
816                         win->screen->do_draw= 1;
817                         break;
818                 case WM_NOTE_SCREEN_CHANGED:
819                         win->screen->do_draw= win->screen->do_refresh= 1;
820                         break;
821                 case WM_NOTE_GESTURE_REDRAW:
822                         win->screen->do_gesture= 1;     /* XXX gestures are stored in window, draw per region... a bit weak? wait for proper composite? (ton) */
823                         break;
824         }
825 }
826
827
828 void ED_screen_draw(wmWindow *win)
829 {
830         ScrArea *sa;
831         ScrArea *sa1=NULL;
832         ScrArea *sa2=NULL;
833         int dir = -1;
834         int dira = -1;
835
836         wm_subwindow_set(win, win->screen->mainwin);
837         
838         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
839                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
840                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
841                 drawscredge_area(sa);
842         }
843
844         /* blended join arrow */
845         if (sa1 && sa2) {
846                 dir = area_getorientation(win->screen, sa1, sa2);
847                 if (dir >= 0) {
848                         switch(dir) {
849                                 case 0: /* W */
850                                         dir = 'r';
851                                         dira = 'l';
852                                         break;
853                                 case 1: /* N */
854                                         dir = 'd';
855                                         dira = 'u';
856                                         break;
857                                 case 2: /* E */
858                                         dir = 'l';
859                                         dira = 'r';
860                                         break;
861                                 case 3: /* S */
862                                         dir = 'u';
863                                         dira = 'd';
864                                         break;
865                         }
866                 }
867                 scrarea_draw_shape_dark(sa2, dir);
868                 scrarea_draw_shape_light(sa1, dira);
869         }
870         
871         if(G.f & G_DEBUG) printf("draw screen\n");
872         win->screen->do_draw= 0;
873 }
874
875 /* make this screen usable */
876 /* for file read and first use, for scaling window, area moves */
877 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
878 {
879         ScrArea *sa;
880         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
881         
882         screen_test_scale(win->screen, win->sizex, win->sizey);
883         
884         if(win->screen->mainwin==0)
885                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
886         else
887                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
888         
889         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
890                 /* set spacetype and region callbacks, calls init() */
891                 /* sets subwindows for regions, adds handlers */
892                 ED_area_initialize(wm, win, sa);
893         }
894
895         if(G.f & G_DEBUG) printf("set screen\n");
896         win->screen->do_refresh= 0;
897
898 }
899
900 /* file read, set all screens, ... */
901 void ED_screens_initialize(wmWindowManager *wm)
902 {
903         wmWindow *win;
904         
905         for(win= wm->windows.first; win; win= win->next) {
906                 
907                 if(win->screen==NULL)
908                         win->screen= G.main->screen.first;
909                 
910                 ED_screen_refresh(wm, win);
911         }
912 }
913
914 void ED_region_exit(bContext *C, ARegion *ar)
915 {
916         ARegion *prevar= C->region;
917
918         C->region= ar;
919         WM_event_remove_handlers(C, &ar->handlers);
920         C->region= prevar;
921 }
922
923 void ED_area_exit(bContext *C, ScrArea *sa)
924 {
925         ScrArea *prevsa= C->area;
926         ARegion *ar;
927
928         C->area= sa;
929         for(ar= sa->regionbase.first; ar; ar= ar->next)
930                 ED_region_exit(C, ar);
931
932         WM_event_remove_handlers(C, &sa->handlers);
933         C->area= prevsa;
934 }
935
936 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
937 {
938         wmWindow *prevwin= C?C->window:NULL;
939         ScrArea *sa;
940         ARegion *ar;
941
942         C->window= window;
943         for(ar= screen->regionbase.first; ar; ar= ar->next)
944                 ED_region_exit(C, ar);
945
946         for(sa= screen->areabase.first; sa; sa= sa->next)
947                 ED_area_exit(C, sa);
948
949         WM_event_remove_handlers(C, &window->handlers);
950         C->window= prevwin;
951 }
952
953
954 /* called in wm_event_system.c. sets state var in screen */
955 void ED_screen_set_subwinactive(wmWindow *win)
956 {
957         if(win->screen) {
958                 wmEvent *event= win->eventstate;
959                 ScrArea *sa;
960                 int oldswin= win->screen->subwinactive;
961                 
962                 /* XXX prevent this call for modal menus now */
963                 if(win->screen->regionbase.first)
964                         return;
965                 
966                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
967                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
968                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
969                                         break;
970                 }
971                 if(sa) {
972                         ARegion *ar;
973                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
974                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
975                                         win->screen->subwinactive= ar->swinid;
976                         }
977                 }
978                 else
979                         win->screen->subwinactive= win->screen->mainwin;
980                 
981                 /* check for redraw headers */
982                 if(oldswin!=win->screen->subwinactive) {
983                         
984                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
985                                 ARegion *ar;
986                                 int do_draw= 0;
987                                 
988                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
989                                         if(ar->swinid==oldswin || ar->swinid==win->screen->subwinactive)
990                                                 do_draw= 1;
991                                 
992                                 if(do_draw) {
993                                         for(ar= sa->regionbase.first; ar; ar= ar->next)
994                                                 if(ar->regiontype==RGN_TYPE_HEADER)
995                                                         ar->do_draw= 1; /* XXX */
996                                 }
997                         }
998                 }
999         }
1000 }
1001
1002 int ED_screen_area_active(const bContext *C)
1003 {
1004
1005         if(C->screen && C->area) {
1006                 ARegion *ar;
1007                 for(ar= C->area->regionbase.first; ar; ar= ar->next)
1008                         if(ar->swinid == C->screen->subwinactive)
1009                                 return 1;
1010         }       
1011         return 0;
1012 }