e73a4e8e77b2d121e718fd81f8ba5a61b815c2e3
[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 "MEM_guardedalloc.h"
28
29 #include "BLI_blenlib.h"
30
31 #include "BKE_global.h"
32 #include "BKE_library.h"
33 #include "BKE_main.h"
34 #include "BKE_screen.h"
35 #include "BKE_utildefines.h"
36
37 #include "BIF_gl.h"
38 #include "BIF_glutil.h"
39 #include "BIF_resources.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43
44 #include "ED_area.h"
45 #include "ED_screen.h"
46
47 #include "wm_subwindow.h"
48
49 #include "screen_intern.h"      /* own module include */
50
51 /* ******************* screen vert, edge, area managing *********************** */
52
53 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
54 {
55         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
56         sv->vec.x= x;
57         sv->vec.y= y;
58         
59         BLI_addtail(&sc->vertbase, sv);
60         return sv;
61 }
62
63 static void sortscrvert(ScrVert **v1, ScrVert **v2)
64 {
65         ScrVert *tmp;
66         
67         if (*v1 > *v2) {
68                 tmp= *v1;
69                 *v1= *v2;
70                 *v2= tmp;       
71         }
72 }
73
74 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
75 {
76         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
77         
78         sortscrvert(&v1, &v2);
79         se->v1= v1;
80         se->v2= v2;
81         
82         BLI_addtail(&sc->edgebase, se);
83         return se;
84 }
85
86
87 static ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
88 {
89         ScrEdge *se;
90         
91         sortscrvert(&v1, &v2);
92         for (se= sc->edgebase.first; se; se= se->next)
93                 if(se->v1==v1 && se->v2==v2)
94                         return se;
95         
96         return NULL;
97 }
98
99 static void removedouble_scrverts(bScreen *sc)
100 {
101         ScrVert *v1, *verg;
102         ScrEdge *se;
103         ScrArea *sa;
104         
105         verg= sc->vertbase.first;
106         while(verg) {
107                 if(verg->newv==NULL) {  /* !!! */
108                         v1= verg->next;
109                         while(v1) {
110                                 if(v1->newv==NULL) {    /* !?! */
111                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
112                                                 /* printf("doublevert\n"); */
113                                                 v1->newv= verg;
114                                         }
115                                 }
116                                 v1= v1->next;
117                         }
118                 }
119                 verg= verg->next;
120         }
121
122         /* replace pointers in edges and faces */
123         se= sc->edgebase.first;
124         while(se) {
125                 if(se->v1->newv) se->v1= se->v1->newv;
126                 if(se->v2->newv) se->v2= se->v2->newv;
127                 /* edges changed: so.... */
128                 sortscrvert(&(se->v1), &(se->v2));
129                 se= se->next;
130         }
131         sa= sc->areabase.first;
132         while(sa) {
133                 if(sa->v1->newv) sa->v1= sa->v1->newv;
134                 if(sa->v2->newv) sa->v2= sa->v2->newv;
135                 if(sa->v3->newv) sa->v3= sa->v3->newv;
136                 if(sa->v4->newv) sa->v4= sa->v4->newv;
137                 sa= sa->next;
138         }
139
140         /* remove */
141         verg= sc->vertbase.first;
142         while(verg) {
143                 v1= verg->next;
144                 if(verg->newv) {
145                         BLI_remlink(&sc->vertbase, verg);
146                         MEM_freeN(verg);
147                 }
148                 verg= v1;
149         }
150
151 }
152
153 static void removenotused_scrverts(bScreen *sc)
154 {
155         ScrVert *sv, *svn;
156         ScrEdge *se;
157         
158         /* we assume edges are ok */
159         
160         se= sc->edgebase.first;
161         while(se) {
162                 se->v1->flag= 1;
163                 se->v2->flag= 1;
164                 se= se->next;
165         }
166         
167         sv= sc->vertbase.first;
168         while(sv) {
169                 svn= sv->next;
170                 if(sv->flag==0) {
171                         BLI_remlink(&sc->vertbase, sv);
172                         MEM_freeN(sv);
173                 }
174                 else sv->flag= 0;
175                 sv= svn;
176         }
177 }
178
179 static void removedouble_scredges(bScreen *sc)
180 {
181         ScrEdge *verg, *se, *sn;
182         
183         /* compare */
184         verg= sc->edgebase.first;
185         while(verg) {
186                 se= verg->next;
187                 while(se) {
188                         sn= se->next;
189                         if(verg->v1==se->v1 && verg->v2==se->v2) {
190                                 BLI_remlink(&sc->edgebase, se);
191                                 MEM_freeN(se);
192                         }
193                         se= sn;
194                 }
195                 verg= verg->next;
196         }
197 }
198
199 static void removenotused_scredges(bScreen *sc)
200 {
201         ScrEdge *se, *sen;
202         ScrArea *sa;
203         int a=0;
204         
205         /* sets flags when edge is used in area */
206         sa= sc->areabase.first;
207         while(sa) {
208                 se= screen_findedge(sc, sa->v1, sa->v2);
209                 if(se==0) printf("error: area %d edge 1 bestaat niet\n", a);
210                 else se->flag= 1;
211                 se= screen_findedge(sc, sa->v2, sa->v3);
212                 if(se==0) printf("error: area %d edge 2 bestaat niet\n", a);
213                 else se->flag= 1;
214                 se= screen_findedge(sc, sa->v3, sa->v4);
215                 if(se==0) printf("error: area %d edge 3 bestaat niet\n", a);
216                 else se->flag= 1;
217                 se= screen_findedge(sc, sa->v4, sa->v1);
218                 if(se==0) printf("error: area %d edge 4 bestaat niet\n", a);
219                 else se->flag= 1;
220                 sa= sa->next;
221                 a++;
222         }
223         se= sc->edgebase.first;
224         while(se) {
225                 sen= se->next;
226                 if(se->flag==0) {
227                         BLI_remlink(&sc->edgebase, se);
228                         MEM_freeN(se);
229                 }
230                 else se->flag= 0;
231                 se= sen;
232         }
233 }
234
235 static int scredge_is_horizontal(ScrEdge *se)
236 {
237         return (se->v1->vec.y == se->v2->vec.y);
238 }
239
240 static ScrEdge *screen_find_active_scredge(bScreen *sc, short *mval)
241 {
242         ScrEdge *se;
243         
244         for (se= sc->edgebase.first; se; se= se->next) {
245                 if (scredge_is_horizontal(se)) {
246                         short min, max;
247                         min= MIN2(se->v1->vec.x, se->v2->vec.x);
248                         max= MAX2(se->v1->vec.x, se->v2->vec.x);
249                         
250                         if (abs(mval[1]-se->v1->vec.y)<=2 && mval[0] >= min && mval[0]<=max)
251                                 return se;
252                 } 
253                 else {
254                         short min, max;
255                         min= MIN2(se->v1->vec.y, se->v2->vec.y);
256                         max= MAX2(se->v1->vec.y, se->v2->vec.y);
257                         
258                         if (abs(mval[0]-se->v1->vec.x)<=2 && mval[1] >= min && mval[1]<=max)
259                                 return se;
260                 }
261         }
262         
263         return NULL;
264 }
265
266 static void select_connected_scredge(bScreen *sc, ScrEdge *edge)
267 {
268         ScrEdge *se;
269         ScrVert *sv;
270         int oneselected;
271         char dir;
272         
273         /* select connected, only in the right direction */
274         /* 'dir' is the direction of EDGE */
275         
276         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
277         else dir= 'h';
278         
279         sv= sc->vertbase.first;
280         while(sv) {
281                 sv->flag= 0;
282                 sv= sv->next;
283         }
284         
285         edge->v1->flag= 1;
286         edge->v2->flag= 1;
287         
288         oneselected= 1;
289         while(oneselected) {
290                 se= sc->edgebase.first;
291                 oneselected= 0;
292                 while(se) {
293                         if(se->v1->flag + se->v2->flag==1) {
294                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
295                                         se->v1->flag= se->v2->flag= 1;
296                                         oneselected= 1;
297                                 }
298                                 if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
299                                         se->v1->flag= se->v2->flag= 1;
300                                         oneselected= 1;
301                                 }
302                         }
303                         se= se->next;
304                 }
305         }
306 }
307
308 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
309 {
310         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
311         sa->v1= v1;
312         sa->v2= v2;
313         sa->v3= v3;
314         sa->v4= v4;
315         sa->headertype= headertype;
316         sa->spacetype= spacetype;
317         
318         BLI_addtail(&sc->areabase, sa);
319         
320         return sa;
321 }
322
323 bScreen *addscreen(wmWindow *win, char *name)
324 {
325         bScreen *sc;
326         ScrVert *sv1, *sv2, *sv3, *sv4;
327         
328         sc= alloc_libblock(&G.main->screen, ID_SCR, name);
329         
330         sc->scene= G.scene;
331         
332         sv1= screen_addvert(sc, 0, 0);
333         sv2= screen_addvert(sc, 0, win->sizey-1);
334         sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
335         sv4= screen_addvert(sc, win->sizex-1, 0);
336         
337         screen_addedge(sc, sv1, sv2);
338         screen_addedge(sc, sv2, sv3);
339         screen_addedge(sc, sv3, sv4);
340         screen_addedge(sc, sv4, sv1);
341         
342         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_INFO);
343                 
344         return sc;
345 }
346
347
348 static void screen_copy(bScreen *to, bScreen *from)
349 {
350         ScrVert *s1, *s2;
351         ScrEdge *se;
352         ScrArea *sa, *saf;
353         
354         /* free contents of 'to', is from blenkernel screen.c */
355         free_screen(to);
356         
357         BLI_duplicatelist(&to->vertbase, &from->vertbase);
358         BLI_duplicatelist(&to->edgebase, &from->edgebase);
359         BLI_duplicatelist(&to->areabase, &from->areabase);
360         
361         s2= to->vertbase.first;
362         for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
363                 s1->newv= s2;
364         }
365         
366         for(se= to->edgebase.first; se; se= se->next) {
367                 se->v1= se->v1->newv;
368                 se->v2= se->v2->newv;
369                 sortscrvert(&(se->v1), &(se->v2));
370         }
371         
372         saf= from->areabase.first;
373         for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
374                 sa->v1= sa->v1->newv;
375                 sa->v2= sa->v2->newv;
376                 sa->v3= sa->v3->newv;
377                 sa->v4= sa->v4->newv;
378                 
379                 sa->spacedata.first= sa->spacedata.last= NULL;
380                 sa->uiblocks.first= sa->uiblocks.last= NULL;
381                 sa->panels.first= sa->panels.last= NULL;
382                 sa->regionbase.first= sa->regionbase.last= NULL;
383                 sa->scriptlink.totscript= 0;
384                 
385                 area_copy_data(sa, saf, 0);
386         }
387         
388         /* put at zero (needed?) */
389         for(s1= from->vertbase.first; s1; s1= s1->next)
390                 s1->newv= NULL;
391
392 }
393
394 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
395 {
396         bScreen *newsc;
397         
398         if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
399         
400         /* make new screen: */
401         newsc= addscreen(win, sc->id.name+2);
402         /* copy all data */
403         screen_copy(newsc, sc);
404         
405         return newsc;
406 }
407
408
409
410 /* *************************************************************** */
411
412 /* test if screen vertices should be scaled */
413 /* also check offset */
414 void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
415 {
416         ScrVert *sv=NULL;
417         ScrEdge *se;
418         ScrArea *sa, *san;
419         int sizex, sizey, yval;
420         float facx, facy, tempf, min[2], max[2];
421         
422         /* calculate size */
423         min[0]= min[1]= 10000.0f;
424         max[0]= max[1]= 0.0f;
425         
426         for(sv= sc->vertbase.first; sv; sv= sv->next) {
427                 min[0]= MIN2(min[0], sv->vec.x);
428                 min[1]= MIN2(min[1], sv->vec.y);
429                 max[0]= MAX2(max[0], sv->vec.x);
430                 max[1]= MAX2(max[1], sv->vec.y);
431         }
432         
433         /* always make 0.0 left under */
434         for(sv= sc->vertbase.first; sv; sv= sv->next) {
435                 sv->vec.x -= min[0];
436                 sv->vec.y -= min[1];
437         }
438         
439         sizex= max[0]-min[0];
440         sizey= max[1]-min[1];
441         
442         if(sizex!= winsizex || sizey!= winsizey) {
443                 facx= winsizex;
444                 facx/= (float)sizex;
445                 facy= winsizey;
446                 facy/= (float)sizey;
447                 
448                 /* make sure it fits! */
449                 for(sv= sc->vertbase.first; sv; sv= sv->next) {
450                         tempf= ((float)sv->vec.x)*facx;
451                         sv->vec.x= (short)(tempf+0.5);
452                         sv->vec.x+= AREAGRID-1;
453                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
454                         
455                         CLAMP(sv->vec.x, 0, winsizex);
456                         
457                         tempf= ((float)sv->vec.y )*facy;
458                         sv->vec.y= (short)(tempf+0.5);
459                         sv->vec.y+= AREAGRID-1;
460                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
461                         
462                         CLAMP(sv->vec.y, 0, winsizey);
463                 }
464         }
465         
466         /* test for collapsed areas. This could happen in some blender version... */
467         for(sa= sc->areabase.first; sa; sa= san) {
468                 san= sa->next;
469                 if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3) {
470                         BKE_screen_area_free(sa);
471                         BLI_remlink(&sc->areabase, sa);
472                         MEM_freeN(sa);
473                 }
474         }
475         
476         /* make each window at least HEADERY high */
477         for(sa= sc->areabase.first; sa; sa= sa->next) {
478                 
479                 if(sa->v1->vec.y+HEADERY > sa->v2->vec.y) {
480                         /* lower edge */
481                         se= screen_findedge(sc, sa->v4, sa->v1);
482                         if(se && sa->v1!=sa->v2 ) {
483                                 select_connected_scredge(sc, se);
484                                 
485                                 /* all selected vertices get the right offset */
486                                 yval= sa->v2->vec.y-HEADERY;
487                                 sv= sc->vertbase.first;
488                                 while(sv) {
489                                         /* if is a collapsed area */
490                                         if(sv!=sa->v2 && sv!=sa->v3) {
491                                                 if(sv->flag) sv->vec.y= yval;
492                                         }
493                                         sv= sv->next;
494                                 }
495                         }
496                 }
497         }
498 }
499
500
501
502 #define SCR_BACK 0.55
503 #define SCR_ROUND 12
504
505 static void drawscredge_area(ScrArea *sa)
506 {
507         short x1= sa->v1->vec.x;
508         short y1= sa->v1->vec.y;
509         short x2= sa->v3->vec.x;
510         short y2= sa->v3->vec.y;
511         
512         cpack(0x0);
513         
514         /* right border area */
515         sdrawline(x2, y1, x2, y2);
516         
517         /* left border area */
518         if(x1>0) { // otherwise it draws the emboss of window over
519                 sdrawline(x1, y1, x1, y2);
520         }       
521         /* top border area */
522         sdrawline(x1, y2, x2, y2);
523         
524         /* bottom border area */
525         sdrawline(x1, y1, x2, y1);
526 }
527
528 void ED_screen_do_listen(bScreen *screen, wmNotifier *note)
529 {
530         
531         /* generic notes */
532         if(note->type==WM_NOTE_REDRAW)
533                 screen->do_draw= 1;
534         if(note->type==WM_NOTE_REFRESH)
535                 if(note->swinid==0)
536                         screen->do_refresh= screen->do_draw= 1;
537         
538 }
539
540
541 void ED_screen_draw(wmWindow *win)
542 {
543         ScrArea *sa;
544         
545         wm_subwindow_set(win, win->screen->mainwin);
546         
547         for(sa= win->screen->areabase.first; sa; sa= sa->next)
548                 drawscredge_area(sa);
549
550         printf("draw screen\n");
551         win->screen->do_draw= 0;
552 }
553
554 /* make this screen usable */
555 /* for file read and first use, for scaling window */
556 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
557 {
558         ScrArea *sa;
559         rcti winrct= {0, win->sizex, 0, win->sizey};
560         
561         screen_test_scale(win->screen, win->sizex, win->sizey);
562         
563         if(win->screen->mainwin==0)
564                 win->screen->mainwin= wm_subwindow_open(win, &winrct);
565         else
566                 wm_subwindow_position(win, win->screen->mainwin, &winrct);
567         
568         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
569                 /* set spacetype and region callbacks */
570                 /* sets subwindow */
571                 ED_area_initialize(wm, win, sa);
572         }
573         
574         printf("set screen\n");
575         win->screen->do_refresh= 0;
576
577 }
578
579 /* file read, set all screens, ... */
580 void ED_screens_initialize(wmWindowManager *wm)
581 {
582         wmWindow *win;
583         
584         for(win= wm->windows.first; win; win= win->next) {
585                 
586                 if(win->screen==NULL)
587                         win->screen= G.main->screen.first;
588                 
589                 ED_screen_refresh(wm, win);
590         }
591 }
592
593 void placeholder()
594 {
595         removedouble_scrverts(NULL);
596         removenotused_scrverts(NULL);
597         removedouble_scredges(NULL);
598         removenotused_scredges(NULL);
599 }
600
601 /* *************************************************** */
602
603 /* operator cb */
604 int screen_cursor_test(bContext *C, wmOperator *op, wmEvent *event)
605 {
606         short mval[2]= {event->x, event->y};
607         ScrEdge *actedge= screen_find_active_scredge(C->screen, mval);
608         
609         if (actedge) {
610                 if (scredge_is_horizontal(actedge)) {
611                         WM_set_cursor(C, CURSOR_Y_MOVE);
612                 } else {
613                         WM_set_cursor(C, CURSOR_X_MOVE);
614                 }
615                 // this does global hotkeys too
616 //              screen_edge_edit_event(g_activearea, actedge, event, val);
617         } else {
618                 WM_set_cursor(C, CURSOR_STD);
619         }
620         
621         return 1;
622 }
623
624