UI: don't use operators anymore for handling user interface events, but rather
[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_area.h"
48 #include "ED_screen.h"
49 #include "ED_screen_types.h"
50
51 #include "wm_subwindow.h"
52
53 #include "screen_intern.h"      /* own module include */
54
55
56 /* ******************* screen vert, edge, area managing *********************** */
57
58 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
59 {
60         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
61         sv->vec.x= x;
62         sv->vec.y= y;
63         
64         BLI_addtail(&sc->vertbase, sv);
65         return sv;
66 }
67
68 static void sortscrvert(ScrVert **v1, ScrVert **v2)
69 {
70         ScrVert *tmp;
71         
72         if (*v1 > *v2) {
73                 tmp= *v1;
74                 *v1= *v2;
75                 *v2= tmp;       
76         }
77 }
78
79 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
80 {
81         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
82         
83         sortscrvert(&v1, &v2);
84         se->v1= v1;
85         se->v2= v2;
86         
87         BLI_addtail(&sc->edgebase, se);
88         return se;
89 }
90
91
92 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
93 {
94         ScrEdge *se;
95         
96         sortscrvert(&v1, &v2);
97         for (se= sc->edgebase.first; se; se= se->next)
98                 if(se->v1==v1 && se->v2==v2)
99                         return se;
100         
101         return NULL;
102 }
103
104 void removedouble_scrverts(bScreen *sc)
105 {
106         ScrVert *v1, *verg;
107         ScrEdge *se;
108         ScrArea *sa;
109         
110         verg= sc->vertbase.first;
111         while(verg) {
112                 if(verg->newv==NULL) {  /* !!! */
113                         v1= verg->next;
114                         while(v1) {
115                                 if(v1->newv==NULL) {    /* !?! */
116                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
117                                                 /* printf("doublevert\n"); */
118                                                 v1->newv= verg;
119                                         }
120                                 }
121                                 v1= v1->next;
122                         }
123                 }
124                 verg= verg->next;
125         }
126
127         /* replace pointers in edges and faces */
128         se= sc->edgebase.first;
129         while(se) {
130                 if(se->v1->newv) se->v1= se->v1->newv;
131                 if(se->v2->newv) se->v2= se->v2->newv;
132                 /* edges changed: so.... */
133                 sortscrvert(&(se->v1), &(se->v2));
134                 se= se->next;
135         }
136         sa= sc->areabase.first;
137         while(sa) {
138                 if(sa->v1->newv) sa->v1= sa->v1->newv;
139                 if(sa->v2->newv) sa->v2= sa->v2->newv;
140                 if(sa->v3->newv) sa->v3= sa->v3->newv;
141                 if(sa->v4->newv) sa->v4= sa->v4->newv;
142                 sa= sa->next;
143         }
144
145         /* remove */
146         verg= sc->vertbase.first;
147         while(verg) {
148                 v1= verg->next;
149                 if(verg->newv) {
150                         BLI_remlink(&sc->vertbase, verg);
151                         MEM_freeN(verg);
152                 }
153                 verg= v1;
154         }
155
156 }
157
158 void removenotused_scrverts(bScreen *sc)
159 {
160         ScrVert *sv, *svn;
161         ScrEdge *se;
162         
163         /* we assume edges are ok */
164         
165         se= sc->edgebase.first;
166         while(se) {
167                 se->v1->flag= 1;
168                 se->v2->flag= 1;
169                 se= se->next;
170         }
171         
172         sv= sc->vertbase.first;
173         while(sv) {
174                 svn= sv->next;
175                 if(sv->flag==0) {
176                         BLI_remlink(&sc->vertbase, sv);
177                         MEM_freeN(sv);
178                 }
179                 else sv->flag= 0;
180                 sv= svn;
181         }
182 }
183
184 void removedouble_scredges(bScreen *sc)
185 {
186         ScrEdge *verg, *se, *sn;
187         
188         /* compare */
189         verg= sc->edgebase.first;
190         while(verg) {
191                 se= verg->next;
192                 while(se) {
193                         sn= se->next;
194                         if(verg->v1==se->v1 && verg->v2==se->v2) {
195                                 BLI_remlink(&sc->edgebase, se);
196                                 MEM_freeN(se);
197                         }
198                         se= sn;
199                 }
200                 verg= verg->next;
201         }
202 }
203
204 void removenotused_scredges(bScreen *sc)
205 {
206         ScrEdge *se, *sen;
207         ScrArea *sa;
208         int a=0;
209         
210         /* sets flags when edge is used in area */
211         sa= sc->areabase.first;
212         while(sa) {
213                 se= screen_findedge(sc, sa->v1, sa->v2);
214                 if(se==0) printf("error: area %d edge 1 doesn't exist\n", a);
215                 else se->flag= 1;
216                 se= screen_findedge(sc, sa->v2, sa->v3);
217                 if(se==0) printf("error: area %d edge 2 doesn't exist\n", a);
218                 else se->flag= 1;
219                 se= screen_findedge(sc, sa->v3, sa->v4);
220                 if(se==0) printf("error: area %d edge 3 doesn't exist\n", a);
221                 else se->flag= 1;
222                 se= screen_findedge(sc, sa->v4, sa->v1);
223                 if(se==0) printf("error: area %d edge 4 doesn't exist\n", a);
224                 else se->flag= 1;
225                 sa= sa->next;
226                 a++;
227         }
228         se= sc->edgebase.first;
229         while(se) {
230                 sen= se->next;
231                 if(se->flag==0) {
232                         BLI_remlink(&sc->edgebase, se);
233                         MEM_freeN(se);
234                 }
235                 else se->flag= 0;
236                 se= sen;
237         }
238 }
239
240 /* adds no space data */
241 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
242 {
243         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
244         sa->v1= v1;
245         sa->v2= v2;
246         sa->v3= v3;
247         sa->v4= v4;
248         sa->headertype= headertype;
249         sa->spacetype= spacetype;
250         
251         BLI_addtail(&sc->areabase, sa);
252         
253         return sa;
254 }
255
256 static void screen_delarea(bScreen *sc, ScrArea *sa)
257 {
258         /* XXX need context to cancel operators ED_area_exit(C, sa); */
259         BKE_screen_area_free(sa);
260         BLI_remlink(&sc->areabase, sa);
261         MEM_freeN(sa);
262 }
263
264 /* return 0: no split possible */
265 /* else return (integer) screencoordinate split point */
266 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
267 {
268         short x, y;
269         
270         // area big enough?
271         if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
272         if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
273         
274         // to be sure
275         if(fac<0.0) fac= 0.0;
276         if(fac>1.0) fac= 1.0;
277         
278         if(dir=='h') {
279                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
280                 
281                 if(y- sa->v1->vec.y < AREAMINY) 
282                         y= sa->v1->vec.y+ AREAMINY;
283                 else if(sa->v2->vec.y- y < AREAMINY) 
284                         y= sa->v2->vec.y- AREAMINY;
285                 else y-= (y % AREAGRID);
286                 
287                 return y;
288         }
289         else {
290                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
291                 
292                 if(x- sa->v1->vec.x < AREAMINX) 
293                         x= sa->v1->vec.x+ AREAMINX;
294                 else if(sa->v4->vec.x- x < AREAMINX) 
295                         x= sa->v4->vec.x- AREAMINX;
296                 else x-= (x % AREAGRID);
297                 
298                 return x;
299         }
300 }
301
302 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
303 {
304         ScrArea *newa=NULL;
305         ScrVert *sv1, *sv2;
306         short split;
307         
308         if(sa==NULL) return NULL;
309         
310         split= testsplitpoint(win, sa, dir, fac);
311         if(split==0) return NULL;
312         
313         if(dir=='h') {
314                 /* new vertices */
315                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
316                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
317                 
318                 /* new edges */
319                 screen_addedge(sc, sa->v1, sv1);
320                 screen_addedge(sc, sv1, sa->v2);
321                 screen_addedge(sc, sa->v3, sv2);
322                 screen_addedge(sc, sv2, sa->v4);
323                 screen_addedge(sc, sv1, sv2);
324                 
325                 /* new areas: top */
326                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
327                 area_copy_data(newa, sa, 0);
328                 
329                 /* area below */
330                 sa->v2= sv1;
331                 sa->v3= sv2;
332                 
333         }
334         else {
335                 /* new vertices */
336                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
337                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
338                 
339                 /* new edges */
340                 screen_addedge(sc, sa->v1, sv1);
341                 screen_addedge(sc, sv1, sa->v4);
342                 screen_addedge(sc, sa->v2, sv2);
343                 screen_addedge(sc, sv2, sa->v3);
344                 screen_addedge(sc, sv1, sv2);
345                 
346                 /* new areas: left */
347                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
348                 area_copy_data(newa, sa, 0);
349                 
350                 /* area right */
351                 sa->v1= sv1;
352                 sa->v2= sv2;
353         }
354         
355         /* remove double vertices en edges */
356         removedouble_scrverts(sc);
357         removedouble_scredges(sc);
358         removenotused_scredges(sc);
359         
360         return newa;
361 }
362
363 /* empty screen, with 1 dummy area without spacedata */
364 /* uses window size */
365 bScreen *screen_add(wmWindow *win, char *name)
366 {
367         bScreen *sc;
368         ScrVert *sv1, *sv2, *sv3, *sv4;
369         
370         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
371         sc->scene= G.scene;
372         sc->do_refresh= 1;
373         
374         win->screen= sc;
375         
376         sv1= screen_addvert(sc, 0, 0);
377         sv2= screen_addvert(sc, 0, win->sizey-1);
378         sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
379         sv4= screen_addvert(sc, win->sizex-1, 0);
380         
381         screen_addedge(sc, sv1, sv2);
382         screen_addedge(sc, sv2, sv3);
383         screen_addedge(sc, sv3, sv4);
384         screen_addedge(sc, sv4, sv1);
385         
386         /* dummy type, no spacedata */
387         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_INFO);
388                 
389         return sc;
390 }
391
392 static void screen_copy(bScreen *to, bScreen *from)
393 {
394         ScrVert *s1, *s2;
395         ScrEdge *se;
396         ScrArea *sa, *saf;
397         
398         /* free contents of 'to', is from blenkernel screen.c */
399         free_screen(to);
400         
401         BLI_duplicatelist(&to->vertbase, &from->vertbase);
402         BLI_duplicatelist(&to->edgebase, &from->edgebase);
403         BLI_duplicatelist(&to->areabase, &from->areabase);
404         to->regionbase.first= to->regionbase.last= NULL;
405         
406         s2= to->vertbase.first;
407         for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
408                 s1->newv= s2;
409         }
410         
411         for(se= to->edgebase.first; se; se= se->next) {
412                 se->v1= se->v1->newv;
413                 se->v2= se->v2->newv;
414                 sortscrvert(&(se->v1), &(se->v2));
415         }
416         
417         saf= from->areabase.first;
418         for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
419                 sa->v1= sa->v1->newv;
420                 sa->v2= sa->v2->newv;
421                 sa->v3= sa->v3->newv;
422                 sa->v4= sa->v4->newv;
423                 
424                 sa->spacedata.first= sa->spacedata.last= NULL;
425                 sa->uiblocks.first= sa->uiblocks.last= NULL;
426                 sa->panels.first= sa->panels.last= NULL;
427                 sa->regionbase.first= sa->regionbase.last= NULL;
428                 sa->actionzones.first= sa->actionzones.last= NULL;
429                 sa->scriptlink.totscript= 0;
430                 
431                 area_copy_data(sa, saf, 0);
432         }
433         
434         /* put at zero (needed?) */
435         for(s1= from->vertbase.first; s1; s1= s1->next)
436                 s1->newv= NULL;
437
438 }
439
440
441 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
442 /* -1 = not valid check */
443 /* used with join operator */
444 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
445 {
446         ScrVert *sav1, *sav2, *sav3, *sav4;
447         ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
448
449         if(sa==NULL || sb==NULL) return -1;
450
451         sav1= sa->v1;
452         sav2= sa->v2;
453         sav3= sa->v3;
454         sav4= sa->v4;
455         sbv1= sb->v1;
456         sbv2= sb->v2;
457         sbv3= sb->v3;
458         sbv4= sb->v4;
459         
460         if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
461                 return 0;
462         }
463         else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
464                 return 1;
465         }
466         else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
467                 return 2;
468         }
469         else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
470                 return 3;
471         }
472         
473         return -1;
474 }
475
476 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
477 *       used by the split, join operators
478 */
479 int screen_area_join(bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
480 {
481         int dir;
482         
483         dir = area_getorientation(scr, sa1, sa2);
484         /*printf("dir is : %i \n", dir);*/
485         
486         if (dir < 0)
487         {
488                 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
489                 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
490                 return 0;
491         }
492         
493         if(dir == 0) {
494                 sa1->v1= sa2->v1;
495                 sa1->v2= sa2->v2;
496                 screen_addedge(scr, sa1->v2, sa1->v3);
497                 screen_addedge(scr, sa1->v1, sa1->v4);
498         }
499         else if(dir == 1) {
500                 sa1->v2= sa2->v2;
501                 sa1->v3= sa2->v3;
502                 screen_addedge(scr, sa1->v1, sa1->v2);
503                 screen_addedge(scr, sa1->v3, sa1->v4);
504         }
505         else if(dir == 2) {
506                 sa1->v3= sa2->v3;
507                 sa1->v4= sa2->v4;
508                 screen_addedge(scr, sa1->v2, sa1->v3);
509                 screen_addedge(scr, sa1->v1, sa1->v4);
510         }
511         else if(dir == 3) {
512                 sa1->v1= sa2->v1;
513                 sa1->v4= sa2->v4;
514                 screen_addedge(scr, sa1->v1, sa1->v2);
515                 screen_addedge(scr, sa1->v3, sa1->v4);
516         }
517         
518         screen_delarea(scr, sa2);
519         removedouble_scrverts(scr);
520         sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
521         
522         return 1;
523 }
524
525
526 /* test if screen vertices should be scaled */
527 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
528 {
529         ScrVert *sv=NULL;
530         ScrArea *sa, *san;
531         int sizex, sizey;
532         float facx, facy, tempf, min[2], max[2];
533         
534         /* calculate size */
535         min[0]= min[1]= 10000.0f;
536         max[0]= max[1]= 0.0f;
537         
538         for(sv= sc->vertbase.first; sv; sv= sv->next) {
539                 min[0]= MIN2(min[0], sv->vec.x);
540                 min[1]= MIN2(min[1], sv->vec.y);
541                 max[0]= MAX2(max[0], sv->vec.x);
542                 max[1]= MAX2(max[1], sv->vec.y);
543         }
544         
545         /* always make 0.0 left under */
546         for(sv= sc->vertbase.first; sv; sv= sv->next) {
547                 sv->vec.x -= min[0];
548                 sv->vec.y -= min[1];
549         }
550         
551         sizex= max[0]-min[0];
552         sizey= max[1]-min[1];
553         
554         if(sizex!= winsizex || sizey!= winsizey) {
555                 facx= winsizex;
556                 facx/= (float)sizex;
557                 facy= winsizey;
558                 facy/= (float)sizey;
559                 
560                 /* make sure it fits! */
561                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
562                         tempf= ((float)sv->vec.x)*facx;
563                         sv->vec.x= (short)(tempf+0.5);
564                         sv->vec.x+= AREAGRID-1;
565                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
566                         
567                         CLAMP(sv->vec.x, 0, winsizex);
568                         
569                         tempf= ((float)sv->vec.y )*facy;
570                         sv->vec.y= (short)(tempf+0.5);
571                         sv->vec.y+= AREAGRID-1;
572                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
573                         
574                         CLAMP(sv->vec.y, 0, winsizey);
575                 }
576         }
577         
578         /* test for collapsed areas. This could happen in some blender version... */
579         for(sa= sc->areabase.first; sa; sa= san) {
580                 san= sa->next;
581                 if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3)
582                         screen_delarea(sc, sa);
583         }
584 }
585
586 /* *********************** DRAWING **************************************** */
587
588
589 #define SCR_BACK 0.55
590 #define SCR_ROUND 12
591
592 /* draw vertical shape visualising future joining (left as well
593  * right direction of future joining) */
594 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
595 {
596         vec2f points[10];
597         short i;
598         float w, h;
599         float width = sa->v3->vec.x - sa->v1->vec.x;
600         float height = sa->v3->vec.y - sa->v1->vec.y;
601
602         if(height<width) {
603                 h = height/8;
604                 w = height/4;
605         }
606         else {
607                 h = width/8;
608                 w = width/4;
609         }
610
611         points[0].x = sa->v1->vec.x;
612         points[0].y = sa->v1->vec.y + height/2;
613
614         points[1].x = sa->v1->vec.x;
615         points[1].y = sa->v1->vec.y;
616
617         points[2].x = sa->v4->vec.x - w;
618         points[2].y = sa->v4->vec.y;
619
620         points[3].x = sa->v4->vec.x - w;
621         points[3].y = sa->v4->vec.y + height/2 - 2*h;
622
623         points[4].x = sa->v4->vec.x - 2*w;
624         points[4].y = sa->v4->vec.y + height/2;
625
626         points[5].x = sa->v4->vec.x - w;
627         points[5].y = sa->v4->vec.y + height/2 + 2*h;
628
629         points[6].x = sa->v3->vec.x - w;
630         points[6].y = sa->v3->vec.y;
631
632         points[7].x = sa->v2->vec.x;
633         points[7].y = sa->v2->vec.y;
634
635         points[8].x = sa->v4->vec.x;
636         points[8].y = sa->v4->vec.y + height/2 - h;
637
638         points[9].x = sa->v4->vec.x;
639         points[9].y = sa->v4->vec.y + height/2 + h;
640
641         if(dir=='l') {
642                 /* when direction is left, then we flip direction of arrow */
643                 float cx = sa->v1->vec.x + width;
644                 for(i=0;i<10;i++) {
645                         points[i].x -= cx;
646                         points[i].x = -points[i].x;
647                         points[i].x += sa->v1->vec.x;
648                 }
649         }
650
651         glBegin(GL_POLYGON);
652         for(i=0;i<5;i++)
653                 glVertex2f(points[i].x, points[i].y);
654         glEnd();
655         glBegin(GL_POLYGON);
656         for(i=4;i<8;i++)
657                 glVertex2f(points[i].x, points[i].y);
658         glVertex2f(points[0].x, points[0].y);
659         glEnd();
660
661         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
662         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
663 }
664
665 /* draw vertical shape visualising future joining (up/down direction) */
666 static void draw_vertical_join_shape(ScrArea *sa, char dir)
667 {
668         vec2f points[10];
669         short i;
670         float w, h;
671         float width = sa->v3->vec.x - sa->v1->vec.x;
672         float height = sa->v3->vec.y - sa->v1->vec.y;
673
674         if(height<width) {
675                 h = height/4;
676                 w = height/8;
677         }
678         else {
679                 h = width/4;
680                 w = width/8;
681         }
682
683         points[0].x = sa->v1->vec.x + width/2;
684         points[0].y = sa->v3->vec.y;
685
686         points[1].x = sa->v2->vec.x;
687         points[1].y = sa->v2->vec.y;
688
689         points[2].x = sa->v1->vec.x;
690         points[2].y = sa->v1->vec.y + h;
691
692         points[3].x = sa->v1->vec.x + width/2 - 2*w;
693         points[3].y = sa->v1->vec.y + h;
694
695         points[4].x = sa->v1->vec.x + width/2;
696         points[4].y = sa->v1->vec.y + 2*h;
697
698         points[5].x = sa->v1->vec.x + width/2 + 2*w;
699         points[5].y = sa->v1->vec.y + h;
700
701         points[6].x = sa->v4->vec.x;
702         points[6].y = sa->v4->vec.y + h;
703         
704         points[7].x = sa->v3->vec.x;
705         points[7].y = sa->v3->vec.y;
706
707         points[8].x = sa->v1->vec.x + width/2 - w;
708         points[8].y = sa->v1->vec.y;
709
710         points[9].x = sa->v1->vec.x + width/2 + w;
711         points[9].y = sa->v1->vec.y;
712
713         if(dir=='u') {
714                 /* when direction is up, then we flip direction of arrow */
715                 float cy = sa->v1->vec.y + height;
716                 for(i=0;i<10;i++) {
717                         points[i].y -= cy;
718                         points[i].y = -points[i].y;
719                         points[i].y += sa->v1->vec.y;
720                 }
721         }
722
723         glBegin(GL_POLYGON);
724         for(i=0;i<5;i++)
725                 glVertex2f(points[i].x, points[i].y);
726         glEnd();
727         glBegin(GL_POLYGON);
728         for(i=4;i<8;i++)
729                 glVertex2f(points[i].x, points[i].y);
730         glVertex2f(points[0].x, points[0].y);
731         glEnd();
732
733         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
734         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
735 }
736
737 /* draw join shape due to direction of joining */
738 static void draw_join_shape(ScrArea *sa, char dir)
739 {
740         if(dir=='u' || dir=='d')
741                 draw_vertical_join_shape(sa, dir);
742         else
743                 draw_horizontal_join_shape(sa, dir);
744 }
745
746 /* draw screen area darker with arrow (visualisation of future joining) */
747 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
748 {
749         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
750         glEnable(GL_BLEND);
751         glColor4ub(0, 0, 0, 50);
752         draw_join_shape(sa, dir);
753         glDisable(GL_BLEND);
754 }
755
756 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
757 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
758 {
759         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
760         glEnable(GL_BLEND);
761         /* value 181 was hardly computed: 181~105 */
762         glColor4ub(255, 255, 255, 50);          
763         /* draw_join_shape(sa, dir); */
764         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
765         glDisable(GL_BLEND);
766 }
767
768 /** screen edges drawing **/
769 static void drawscredge_area(ScrArea *sa)
770 {
771         short x1= sa->v1->vec.x;
772         short y1= sa->v1->vec.y;
773         short x2= sa->v3->vec.x;
774         short y2= sa->v3->vec.y;
775         
776         cpack(0x0);
777         
778         /* right border area */
779         sdrawline(x2, y1, x2, y2);
780         
781         /* left border area */
782         if(x1>0) { /* otherwise it draws the emboss of window over */
783                 sdrawline(x1, y1, x1, y2);
784         }
785         
786         /* top border area */
787         sdrawline(x1, y2, x2, y2);
788         
789         /* bottom border area */
790         sdrawline(x1, y1, x2, y1);
791         
792 }
793
794 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
795
796 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
797 {
798         bScreen *newsc;
799         
800         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
801         
802         /* make new empty screen: */
803         newsc= screen_add(win, sc->id.name+2);
804         /* copy all data */
805         screen_copy(newsc, sc);
806         
807         return newsc;
808 }
809
810
811 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
812 {
813         
814         /* generic notes */
815         switch(note->type) {
816                 case WM_NOTE_WINDOW_REDRAW:
817                         win->screen->do_draw= 1;
818                         break;
819                 case WM_NOTE_SCREEN_CHANGED:
820                         win->screen->do_draw= win->screen->do_refresh= 1;
821                         break;
822                 case WM_NOTE_GESTURE_REDRAW:
823                         win->screen->do_gesture= 1;     /* XXX gestures are stored in window, draw per region... a bit weak? wait for proper composite? (ton) */
824                         break;
825         }
826 }
827
828
829 void ED_screen_draw(wmWindow *win)
830 {
831         ScrArea *sa;
832         ScrArea *sa1=NULL;
833         ScrArea *sa2=NULL;
834         int dir = -1;
835         int dira = -1;
836
837         wm_subwindow_set(win, win->screen->mainwin);
838         
839         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
840                 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
841                 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
842                 drawscredge_area(sa);
843         }
844
845         /* blended join arrow */
846         if (sa1 && sa2) {
847                 dir = area_getorientation(win->screen, sa1, sa2);
848                 if (dir >= 0) {
849                         switch(dir) {
850                                 case 0: /* W */
851                                         dir = 'r';
852                                         dira = 'l';
853                                         break;
854                                 case 1: /* N */
855                                         dir = 'd';
856                                         dira = 'u';
857                                         break;
858                                 case 2: /* E */
859                                         dir = 'l';
860                                         dira = 'r';
861                                         break;
862                                 case 3: /* S */
863                                         dir = 'u';
864                                         dira = 'd';
865                                         break;
866                         }
867                 }
868                 scrarea_draw_shape_dark(sa2, dir);
869                 scrarea_draw_shape_light(sa1, dira);
870         }
871         
872         if(G.f & G_DEBUG) printf("draw screen\n");
873         win->screen->do_draw= 0;
874 }
875
876 /* make this screen usable */
877 /* for file read and first use, for scaling window, area moves */
878 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
879 {
880         ScrArea *sa;
881         ARegion *ar;
882         rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
883         
884         screen_test_scale(win->screen, win->sizex, win->sizey);
885         
886         if(win->screen->mainwin==0)
887                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
888         else
889                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
890         
891         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
892                 /* set spacetype and region callbacks */
893                 /* sets subwindow */
894                 ED_area_initialize(wm, win, sa);
895         }
896
897         for(ar= win->screen->regionbase.first; ar; ar= ar->next) {
898                 /* set subwindow */
899                 ED_region_initialize(wm, win, ar);
900         }
901         
902         if(G.f & G_DEBUG) printf("set screen\n");
903         win->screen->do_refresh= 0;
904
905 }
906
907 /* file read, set all screens, ... */
908 void ED_screens_initialize(wmWindowManager *wm)
909 {
910         wmWindow *win;
911         
912         for(win= wm->windows.first; win; win= win->next) {
913                 
914                 if(win->screen==NULL)
915                         win->screen= G.main->screen.first;
916                 
917                 ED_screen_refresh(wm, win);
918         }
919 }
920
921 void ED_region_exit(bContext *C, ARegion *ar)
922 {
923         ARegion *prevar= C->region;
924
925         C->region= ar;
926         WM_event_remove_handlers(C, &ar->handlers);
927         C->region= prevar;
928 }
929
930 void ED_area_exit(bContext *C, ScrArea *sa)
931 {
932         ScrArea *prevsa= C->area;
933         ARegion *ar;
934
935         C->area= sa;
936         for(ar= sa->regionbase.first; ar; ar= ar->next)
937                 ED_region_exit(C, ar);
938
939         WM_event_remove_handlers(C, &sa->handlers);
940         C->area= prevsa;
941 }
942
943 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
944 {
945         wmWindow *prevwin= C->window;
946         ScrArea *sa;
947         ARegion *ar;
948
949         C->window= window;
950         for(ar= screen->regionbase.first; ar; ar= ar->next)
951                 ED_region_exit(C, ar);
952
953         for(sa= screen->areabase.first; sa; sa= sa->next)
954                 ED_area_exit(C, sa);
955
956         WM_event_remove_handlers(C, &window->handlers);
957         C->window= prevwin;
958 }
959
960
961 /* called in wm_event_system.c. sets state var in screen */
962 void ED_screen_set_subwinactive(wmWindow *win)
963 {
964         if(win->screen) {
965                 wmEvent *event= win->eventstate;
966                 ScrArea *sa;
967                 
968                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
969                         if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
970                                 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
971                                         break;
972                 }
973                 if(sa) {
974                         ARegion *ar;
975                         for(ar= sa->regionbase.first; ar; ar= ar->next) {
976                                 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
977                                         win->screen->subwinactive= ar->swinid;
978                         }
979                 }
980                 else
981                         win->screen->subwinactive= win->screen->mainwin;
982                 
983         }
984 }
985
986