4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * ***** END GPL LICENSE BLOCK *****
29 #include "MEM_guardedalloc.h"
31 #include "DNA_vec_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_texture_types.h"
35 #include "DNA_userdef_types.h"
37 #include "BLI_blenlib.h"
39 #include "BKE_context.h"
40 #include "BKE_global.h"
41 #include "BKE_library.h"
44 #include "BKE_screen.h"
45 #include "BKE_scene.h"
46 #include "BKE_utildefines.h"
49 #include "BIF_glutil.h"
54 #include "ED_screen.h"
55 #include "ED_screen_types.h"
57 #include "UI_interface.h"
59 /* XXX actually should be not here... solve later */
60 #include "wm_subwindow.h"
62 #include "screen_intern.h" /* own module include */
65 /* ******************* screen vert, edge, area managing *********************** */
67 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
69 ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
73 BLI_addtail(&sc->vertbase, sv);
77 static void sortscrvert(ScrVert **v1, ScrVert **v2)
88 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
90 ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
92 sortscrvert(&v1, &v2);
96 BLI_addtail(&sc->edgebase, se);
101 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
105 sortscrvert(&v1, &v2);
106 for (se= sc->edgebase.first; se; se= se->next)
107 if(se->v1==v1 && se->v2==v2)
113 void removedouble_scrverts(bScreen *sc)
119 verg= sc->vertbase.first;
121 if(verg->newv==NULL) { /* !!! */
124 if(v1->newv==NULL) { /* !?! */
125 if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
126 /* printf("doublevert\n"); */
136 /* replace pointers in edges and faces */
137 se= sc->edgebase.first;
139 if(se->v1->newv) se->v1= se->v1->newv;
140 if(se->v2->newv) se->v2= se->v2->newv;
141 /* edges changed: so.... */
142 sortscrvert(&(se->v1), &(se->v2));
145 sa= sc->areabase.first;
147 if(sa->v1->newv) sa->v1= sa->v1->newv;
148 if(sa->v2->newv) sa->v2= sa->v2->newv;
149 if(sa->v3->newv) sa->v3= sa->v3->newv;
150 if(sa->v4->newv) sa->v4= sa->v4->newv;
155 verg= sc->vertbase.first;
159 BLI_remlink(&sc->vertbase, verg);
167 void removenotused_scrverts(bScreen *sc)
172 /* we assume edges are ok */
174 se= sc->edgebase.first;
181 sv= sc->vertbase.first;
185 BLI_remlink(&sc->vertbase, sv);
193 void removedouble_scredges(bScreen *sc)
195 ScrEdge *verg, *se, *sn;
198 verg= sc->edgebase.first;
203 if(verg->v1==se->v1 && verg->v2==se->v2) {
204 BLI_remlink(&sc->edgebase, se);
213 void removenotused_scredges(bScreen *sc)
219 /* sets flags when edge is used in area */
220 sa= sc->areabase.first;
222 se= screen_findedge(sc, sa->v1, sa->v2);
223 if(se==0) printf("error: area %d edge 1 doesn't exist\n", a);
225 se= screen_findedge(sc, sa->v2, sa->v3);
226 if(se==0) printf("error: area %d edge 2 doesn't exist\n", a);
228 se= screen_findedge(sc, sa->v3, sa->v4);
229 if(se==0) printf("error: area %d edge 3 doesn't exist\n", a);
231 se= screen_findedge(sc, sa->v4, sa->v1);
232 if(se==0) printf("error: area %d edge 4 doesn't exist\n", a);
237 se= sc->edgebase.first;
241 BLI_remlink(&sc->edgebase, se);
249 int scredge_is_horizontal(ScrEdge *se)
251 return (se->v1->vec.y == se->v2->vec.y);
254 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
258 for (se= sc->edgebase.first; se; se= se->next) {
259 if (scredge_is_horizontal(se)) {
261 min= MIN2(se->v1->vec.x, se->v2->vec.x);
262 max= MAX2(se->v1->vec.x, se->v2->vec.x);
264 if (abs(my-se->v1->vec.y)<=2 && mx>=min && mx<=max)
269 min= MIN2(se->v1->vec.y, se->v2->vec.y);
270 max= MAX2(se->v1->vec.y, se->v2->vec.y);
272 if (abs(mx-se->v1->vec.x)<=2 && my>=min && my<=max)
282 /* adds no space data */
283 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
285 ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
290 sa->headertype= headertype;
291 sa->spacetype= sa->butspacetype= spacetype;
293 BLI_addtail(&sc->areabase, sa);
298 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
303 BKE_screen_area_free(sa);
305 BLI_remlink(&sc->areabase, sa);
309 /* return 0: no split possible */
310 /* else return (integer) screencoordinate split point */
311 static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
316 if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
317 if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
320 if(fac<0.0) fac= 0.0;
321 if(fac>1.0) fac= 1.0;
324 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
326 if(y- sa->v1->vec.y < AREAMINY)
327 y= sa->v1->vec.y+ AREAMINY;
328 else if(sa->v2->vec.y- y < AREAMINY)
329 y= sa->v2->vec.y- AREAMINY;
330 else y-= (y % AREAGRID);
335 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
337 if(x- sa->v1->vec.x < AREAMINX)
338 x= sa->v1->vec.x+ AREAMINX;
339 else if(sa->v4->vec.x- x < AREAMINX)
340 x= sa->v4->vec.x- AREAMINX;
341 else x-= (x % AREAGRID);
347 ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
353 if(sa==NULL) return NULL;
355 split= testsplitpoint(win, sa, dir, fac);
356 if(split==0) return NULL;
360 sv1= screen_addvert(sc, sa->v1->vec.x, split);
361 sv2= screen_addvert(sc, sa->v4->vec.x, split);
364 screen_addedge(sc, sa->v1, sv1);
365 screen_addedge(sc, sv1, sa->v2);
366 screen_addedge(sc, sa->v3, sv2);
367 screen_addedge(sc, sv2, sa->v4);
368 screen_addedge(sc, sv1, sv2);
371 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
372 area_copy_data(newa, sa, 0);
381 sv1= screen_addvert(sc, split, sa->v1->vec.y);
382 sv2= screen_addvert(sc, split, sa->v2->vec.y);
385 screen_addedge(sc, sa->v1, sv1);
386 screen_addedge(sc, sv1, sa->v4);
387 screen_addedge(sc, sa->v2, sv2);
388 screen_addedge(sc, sv2, sa->v3);
389 screen_addedge(sc, sv1, sv2);
391 /* new areas: left */
392 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
393 area_copy_data(newa, sa, 0);
400 /* remove double vertices en edges */
401 removedouble_scrverts(sc);
402 removedouble_scredges(sc);
403 removenotused_scredges(sc);
408 /* empty screen, with 1 dummy area without spacedata */
409 /* uses window size */
410 bScreen *ED_screen_add(wmWindow *win, Scene *scene, char *name)
413 ScrVert *sv1, *sv2, *sv3, *sv4;
415 sc= alloc_libblock(&G.main->screen, ID_SCR, name);
418 sc->winid= win->winid;
420 sv1= screen_addvert(sc, 0, 0);
421 sv2= screen_addvert(sc, 0, win->sizey-1);
422 sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
423 sv4= screen_addvert(sc, win->sizex-1, 0);
425 screen_addedge(sc, sv1, sv2);
426 screen_addedge(sc, sv2, sv3);
427 screen_addedge(sc, sv3, sv4);
428 screen_addedge(sc, sv4, sv1);
430 /* dummy type, no spacedata */
431 screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
436 static void screen_copy(bScreen *to, bScreen *from)
442 /* free contents of 'to', is from blenkernel screen.c */
445 BLI_duplicatelist(&to->vertbase, &from->vertbase);
446 BLI_duplicatelist(&to->edgebase, &from->edgebase);
447 BLI_duplicatelist(&to->areabase, &from->areabase);
448 to->regionbase.first= to->regionbase.last= NULL;
450 s2= to->vertbase.first;
451 for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
455 for(se= to->edgebase.first; se; se= se->next) {
456 se->v1= se->v1->newv;
457 se->v2= se->v2->newv;
458 sortscrvert(&(se->v1), &(se->v2));
461 saf= from->areabase.first;
462 for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
463 sa->v1= sa->v1->newv;
464 sa->v2= sa->v2->newv;
465 sa->v3= sa->v3->newv;
466 sa->v4= sa->v4->newv;
468 sa->spacedata.first= sa->spacedata.last= NULL;
469 sa->regionbase.first= sa->regionbase.last= NULL;
470 sa->actionzones.first= sa->actionzones.last= NULL;
472 area_copy_data(sa, saf, 0);
475 /* put at zero (needed?) */
476 for(s1= from->vertbase.first; s1; s1= s1->next)
482 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
483 /* -1 = not valid check */
484 /* used with join operator */
485 int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
487 ScrVert *sav1, *sav2, *sav3, *sav4;
488 ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
490 if(sa==NULL || sb==NULL) return -1;
501 if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
504 else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
507 else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
510 else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
517 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
518 * used by the split, join operators
520 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2)
524 dir = area_getorientation(scr, sa1, sa2);
525 /*printf("dir is : %i \n", dir);*/
529 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
530 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
537 screen_addedge(scr, sa1->v2, sa1->v3);
538 screen_addedge(scr, sa1->v1, sa1->v4);
543 screen_addedge(scr, sa1->v1, sa1->v2);
544 screen_addedge(scr, sa1->v3, sa1->v4);
549 screen_addedge(scr, sa1->v2, sa1->v3);
550 screen_addedge(scr, sa1->v1, sa1->v4);
555 screen_addedge(scr, sa1->v1, sa1->v2);
556 screen_addedge(scr, sa1->v3, sa1->v4);
559 screen_delarea(C, scr, sa2);
560 removedouble_scrverts(scr);
561 sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
566 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
573 /* select connected, only in the right direction */
574 /* 'dir' is the direction of EDGE */
576 if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
579 sv= sc->vertbase.first;
590 se= sc->edgebase.first;
593 if(se->v1->flag + se->v2->flag==1) {
594 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
595 se->v1->flag= se->v2->flag= 1;
598 if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
599 se->v1->flag= se->v2->flag= 1;
608 /* test if screen vertices should be scaled */
609 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
614 float facx, facy, tempf, min[2], max[2];
617 min[0]= min[1]= 10000.0f;
618 max[0]= max[1]= 0.0f;
620 for(sv= sc->vertbase.first; sv; sv= sv->next) {
621 min[0]= MIN2(min[0], sv->vec.x);
622 min[1]= MIN2(min[1], sv->vec.y);
623 max[0]= MAX2(max[0], sv->vec.x);
624 max[1]= MAX2(max[1], sv->vec.y);
627 /* always make 0.0 left under */
628 for(sv= sc->vertbase.first; sv; sv= sv->next) {
633 sizex= max[0]-min[0];
634 sizey= max[1]-min[1];
636 if(sizex!= winsizex || sizey!= winsizey) {
642 /* make sure it fits! */
643 for(sv= sc->vertbase.first; sv; sv= sv->next) {
644 tempf= ((float)sv->vec.x)*facx;
645 sv->vec.x= (short)(tempf+0.5);
646 sv->vec.x+= AREAGRID-1;
647 sv->vec.x-= (sv->vec.x % AREAGRID);
649 CLAMP(sv->vec.x, 0, winsizex);
651 tempf= ((float)sv->vec.y )*facy;
652 sv->vec.y= (short)(tempf+0.5);
653 sv->vec.y+= AREAGRID-1;
654 sv->vec.y-= (sv->vec.y % AREAGRID);
656 CLAMP(sv->vec.y, 0, winsizey);
660 /* test for collapsed areas. This could happen in some blender version... */
661 /* ton: removed option now, it needs Context... */
663 /* make each window at least HEADERY high */
664 for(sa= sc->areabase.first; sa; sa= sa->next) {
665 int headery= HEADERY+1;
667 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
669 ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
670 if(se && sa->v1!=sa->v2 ) {
673 select_connected_scredge(sc, se);
675 /* all selected vertices get the right offset */
676 yval= sa->v2->vec.y-headery;
677 sv= sc->vertbase.first;
679 /* if is a collapsed area */
680 if(sv!=sa->v2 && sv!=sa->v3) {
681 if(sv->flag) sv->vec.y= yval;
691 /* *********************** DRAWING **************************************** */
694 #define SCR_BACK 0.55
697 /* draw vertical shape visualising future joining (left as well
698 * right direction of future joining) */
699 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
704 float width = sa->v3->vec.x - sa->v1->vec.x;
705 float height = sa->v3->vec.y - sa->v1->vec.y;
716 points[0].x = sa->v1->vec.x;
717 points[0].y = sa->v1->vec.y + height/2;
719 points[1].x = sa->v1->vec.x;
720 points[1].y = sa->v1->vec.y;
722 points[2].x = sa->v4->vec.x - w;
723 points[2].y = sa->v4->vec.y;
725 points[3].x = sa->v4->vec.x - w;
726 points[3].y = sa->v4->vec.y + height/2 - 2*h;
728 points[4].x = sa->v4->vec.x - 2*w;
729 points[4].y = sa->v4->vec.y + height/2;
731 points[5].x = sa->v4->vec.x - w;
732 points[5].y = sa->v4->vec.y + height/2 + 2*h;
734 points[6].x = sa->v3->vec.x - w;
735 points[6].y = sa->v3->vec.y;
737 points[7].x = sa->v2->vec.x;
738 points[7].y = sa->v2->vec.y;
740 points[8].x = sa->v4->vec.x;
741 points[8].y = sa->v4->vec.y + height/2 - h;
743 points[9].x = sa->v4->vec.x;
744 points[9].y = sa->v4->vec.y + height/2 + h;
747 /* when direction is left, then we flip direction of arrow */
748 float cx = sa->v1->vec.x + width;
751 points[i].x = -points[i].x;
752 points[i].x += sa->v1->vec.x;
758 glVertex2f(points[i].x, points[i].y);
762 glVertex2f(points[i].x, points[i].y);
763 glVertex2f(points[0].x, points[0].y);
766 glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
767 glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
770 /* draw vertical shape visualising future joining (up/down direction) */
771 static void draw_vertical_join_shape(ScrArea *sa, char dir)
776 float width = sa->v3->vec.x - sa->v1->vec.x;
777 float height = sa->v3->vec.y - sa->v1->vec.y;
788 points[0].x = sa->v1->vec.x + width/2;
789 points[0].y = sa->v3->vec.y;
791 points[1].x = sa->v2->vec.x;
792 points[1].y = sa->v2->vec.y;
794 points[2].x = sa->v1->vec.x;
795 points[2].y = sa->v1->vec.y + h;
797 points[3].x = sa->v1->vec.x + width/2 - 2*w;
798 points[3].y = sa->v1->vec.y + h;
800 points[4].x = sa->v1->vec.x + width/2;
801 points[4].y = sa->v1->vec.y + 2*h;
803 points[5].x = sa->v1->vec.x + width/2 + 2*w;
804 points[5].y = sa->v1->vec.y + h;
806 points[6].x = sa->v4->vec.x;
807 points[6].y = sa->v4->vec.y + h;
809 points[7].x = sa->v3->vec.x;
810 points[7].y = sa->v3->vec.y;
812 points[8].x = sa->v1->vec.x + width/2 - w;
813 points[8].y = sa->v1->vec.y;
815 points[9].x = sa->v1->vec.x + width/2 + w;
816 points[9].y = sa->v1->vec.y;
819 /* when direction is up, then we flip direction of arrow */
820 float cy = sa->v1->vec.y + height;
823 points[i].y = -points[i].y;
824 points[i].y += sa->v1->vec.y;
830 glVertex2f(points[i].x, points[i].y);
834 glVertex2f(points[i].x, points[i].y);
835 glVertex2f(points[0].x, points[0].y);
838 glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
839 glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
842 /* draw join shape due to direction of joining */
843 static void draw_join_shape(ScrArea *sa, char dir)
845 if(dir=='u' || dir=='d')
846 draw_vertical_join_shape(sa, dir);
848 draw_horizontal_join_shape(sa, dir);
851 /* draw screen area darker with arrow (visualisation of future joining) */
852 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
854 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
856 glColor4ub(0, 0, 0, 50);
857 draw_join_shape(sa, dir);
861 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
862 static void scrarea_draw_shape_light(ScrArea *sa, char dir)
864 glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
866 /* value 181 was hardly computed: 181~105 */
867 glColor4ub(255, 255, 255, 50);
868 /* draw_join_shape(sa, dir); */
869 glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
873 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a)
875 /* right border area */
877 sdrawline(x2+a, y1, x2+a, y2);
879 /* left border area */
880 if(x1>0) /* otherwise it draws the emboss of window over */
881 sdrawline(x1+a, y1, x1+a, y2);
883 /* top border area */
885 sdrawline(x1, y2+a, x2, y2+a);
887 /* bottom border area */
889 sdrawline(x1, y1+a, x2, y1+a);
893 /** screen edges drawing **/
894 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
896 short x1= sa->v1->vec.x;
897 short y1= sa->v1->vec.y;
898 short x2= sa->v3->vec.x;
899 short y2= sa->v3->vec.y;
902 rt= CLAMPIS(G.rt, 0, 16);
906 for(a=-rt; a<=rt; a++)
908 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
912 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
916 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
918 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
922 if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
924 /* make new empty screen: */
925 newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
927 screen_copy(newsc, sc);
932 /* screen sets cursor based on swinid */
933 static void region_cursor_set(wmWindow *win, int swinid)
935 ScrArea *sa= win->screen->areabase.first;
937 for(;sa; sa= sa->next) {
938 ARegion *ar= sa->regionbase.first;
939 for(;ar; ar= ar->next) {
940 if(ar->swinid == swinid) {
941 if(ar->type && ar->type->cursor)
942 ar->type->cursor(win, sa, ar);
944 WM_cursor_set(win, CURSOR_STD);
951 void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
955 switch(note->category) {
957 if(note->data==ND_FILEREAD)
958 win->screen->do_draw= 1;
961 win->screen->do_draw= 1;
964 if(note->action==NA_EDITED)
965 win->screen->do_draw= win->screen->do_refresh= 1;
967 if(note->data==ND_MODE)
968 region_cursor_set(win, note->swinid);
973 /* only for edge lines between areas, and the blended join arrows */
974 void ED_screen_draw(wmWindow *win)
982 wmSubWindowSet(win, win->screen->mainwin);
984 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
985 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
986 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
987 drawscredge_area(sa, win->sizex, win->sizey, 0);
989 for(sa= win->screen->areabase.first; sa; sa= sa->next)
990 drawscredge_area(sa, win->sizex, win->sizey, 1);
992 /* blended join arrow */
994 dir = area_getorientation(win->screen, sa1, sa2);
1015 scrarea_draw_shape_dark(sa2, dir);
1016 scrarea_draw_shape_light(sa1, dira);
1019 // if(G.f & G_DEBUG) printf("draw screen\n");
1020 win->screen->do_draw= 0;
1023 /* make this screen usable */
1024 /* for file read and first use, for scaling window, area moves */
1025 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1028 rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
1030 screen_test_scale(win->screen, win->sizex, win->sizey);
1032 if(win->screen->mainwin==0)
1033 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1035 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1037 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1038 /* set spacetype and region callbacks, calls init() */
1039 /* sets subwindows for regions, adds handlers */
1040 ED_area_initialize(wm, win, sa);
1043 /* wake up animtimer */
1044 if(win->screen->animtimer)
1045 WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1047 if(G.f & G_DEBUG) printf("set screen\n");
1048 win->screen->do_refresh= 0;
1050 win->screen->context= ed_screen_context;
1053 /* file read, set all screens, ... */
1054 void ED_screens_initialize(wmWindowManager *wm)
1058 for(win= wm->windows.first; win; win= win->next) {
1060 if(win->screen==NULL)
1061 win->screen= G.main->screen.first;
1063 ED_screen_refresh(wm, win);
1068 /* *********** exit calls are for closing running stuff ******** */
1070 void ED_region_exit(bContext *C, ARegion *ar)
1072 ARegion *prevar= CTX_wm_region(C);
1074 CTX_wm_region_set(C, ar);
1075 WM_event_remove_handlers(C, &ar->handlers);
1077 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1081 MEM_freeN(ar->headerstr);
1082 ar->headerstr= NULL;
1084 CTX_wm_region_set(C, prevar);
1087 void ED_area_exit(bContext *C, ScrArea *sa)
1089 ScrArea *prevsa= CTX_wm_area(C);
1092 CTX_wm_area_set(C, sa);
1093 for(ar= sa->regionbase.first; ar; ar= ar->next)
1094 ED_region_exit(C, ar);
1096 WM_event_remove_handlers(C, &sa->handlers);
1097 CTX_wm_area_set(C, prevsa);
1100 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1102 wmWindowManager *wm= CTX_wm_manager(C);
1103 wmWindow *prevwin= CTX_wm_window(C);
1107 CTX_wm_window_set(C, window);
1109 if(screen->animtimer)
1110 WM_event_remove_timer(wm, window, screen->animtimer);
1111 screen->animtimer= NULL;
1114 wm_subwindow_close(window, screen->mainwin);
1116 screen->subwinactive= 0;
1118 for(ar= screen->regionbase.first; ar; ar= ar->next)
1119 ED_region_exit(C, ar);
1121 for(sa= screen->areabase.first; sa; sa= sa->next)
1122 ED_area_exit(C, sa);
1124 /* mark it available for use for other windows */
1127 CTX_wm_window_set(C, prevwin);
1130 /* *********************************** */
1132 /* case when on area-edge or in azones, or outside window */
1133 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1138 for(sa= win->screen->areabase.first; sa; sa= sa->next)
1139 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1143 if(az->type==AZONE_AREA)
1144 WM_cursor_set(win, CURSOR_EDIT);
1145 else if(az->type==AZONE_REGION) {
1146 if(az->edge == 'l' || az->edge == 'r')
1147 WM_cursor_set(win, CURSOR_X_MOVE);
1149 WM_cursor_set(win, CURSOR_Y_MOVE);
1153 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1156 if(scredge_is_horizontal(actedge))
1157 WM_cursor_set(win, CURSOR_Y_MOVE);
1159 WM_cursor_set(win, CURSOR_X_MOVE);
1162 WM_cursor_set(win, CURSOR_STD);
1167 /* called in wm_event_system.c. sets state vars in screen, cursors */
1168 /* event type is mouse move */
1169 void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
1172 bScreen *scr= win->screen;
1175 int oldswin= scr->subwinactive;
1177 for(sa= scr->areabase.first; sa; sa= sa->next) {
1178 if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1179 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1180 if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1184 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1185 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1186 scr->subwinactive= ar->swinid;
1190 scr->subwinactive= scr->mainwin;
1192 /* check for redraw headers */
1193 if(oldswin!=scr->subwinactive) {
1195 for(sa= scr->areabase.first; sa; sa= sa->next) {
1198 for(ar= sa->regionbase.first; ar; ar= ar->next)
1199 if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1203 for(ar= sa->regionbase.first; ar; ar= ar->next)
1204 if(ar->regiontype==RGN_TYPE_HEADER)
1205 ED_region_tag_redraw(ar);
1210 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1211 if(scr->subwinactive==scr->mainwin) {
1212 screen_cursor_set(win, event);
1214 else if(oldswin!=scr->subwinactive) {
1215 region_cursor_set(win, scr->subwinactive);
1220 int ED_screen_area_active(const bContext *C)
1222 bScreen *sc= CTX_wm_screen(C);
1223 ScrArea *sa= CTX_wm_area(C);
1227 for(ar= sa->regionbase.first; ar; ar= ar->next)
1228 if(ar->swinid == sc->subwinactive)
1234 /* operator call, WM + Window + screen already existed before */
1235 /* Do NOT call in area/region queues! */
1236 void ED_screen_set(bContext *C, bScreen *sc)
1238 wmWindowManager *wm= CTX_wm_manager(C);
1239 wmWindow *win= CTX_wm_window(C);
1240 bScreen *oldscreen= CTX_wm_screen(C);
1243 /* validate screen, it's called with notifier reference */
1244 for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1245 if(sc == (bScreen *)id)
1250 /* check for valid winid */
1251 if(sc->winid!=0 && sc->winid!=win->winid)
1254 if(sc->full) { /* find associated full */
1256 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1257 ScrArea *sa= sc1->areabase.first;
1265 if (oldscreen != sc) {
1266 wmTimer *wt= oldscreen->animtimer;
1268 /* we put timer to sleep, so screen_exit has to think there's no timer */
1269 oldscreen->animtimer= NULL;
1271 WM_event_timer_sleep(wm, win, wt, 1);
1273 ED_screen_exit(C, win, oldscreen);
1274 oldscreen->animtimer= wt;
1277 CTX_wm_window_set(C, win); // stores C->wm.screen... hrmf
1279 /* prevent multiwin errors */
1280 sc->winid= win->winid;
1282 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1283 WM_event_add_notifier(C, NC_WINDOW, NULL);
1285 /* makes button hilites work */
1286 WM_event_add_mousemove(C);
1290 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1294 for(win=wm->windows.first; win; win=win->next)
1295 if(win->screen == sc)
1301 /* only call outside of area/region loops */
1302 void ED_screen_delete(bContext *C, bScreen *sc)
1304 Main *bmain= CTX_data_main(C);
1305 wmWindowManager *wm= CTX_wm_manager(C);
1306 wmWindow *win= CTX_wm_window(C);
1310 /* screen can only be in use by one window at a time, so as
1311 long as we are able to find a screen that is unused, we
1312 can safely assume ours is not in use anywhere an delete it */
1314 for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1315 if(!ed_screen_used(wm, newsc))
1319 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1320 if(!ed_screen_used(wm, newsc))
1327 ED_screen_set(C, newsc);
1329 if(delete && win->screen != sc)
1330 free_libblock(&bmain->screen, sc);
1333 /* only call outside of area/region loops */
1334 void ED_screen_set_scene(bContext *C, Scene *scene)
1337 bScreen *curscreen= CTX_wm_screen(C);
1339 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1340 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1342 if(scene != sc->scene) {
1343 /* all areas endlocalview */
1344 // XXX ScrArea *sa= sc->areabase.first;
1346 // endlocalview(sa);
1355 // copy_view3d_lock(0); /* space.c */
1357 /* are there cameras in the views that are not in the scene? */
1358 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1359 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1360 ScrArea *sa= sc->areabase.first;
1362 SpaceLink *sl= sa->spacedata.first;
1364 if(sl->spacetype==SPACE_VIEW3D) {
1365 View3D *v3d= (View3D*) sl;
1366 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1367 v3d->camera= scene_find_camera(sc->scene);
1368 // XXX if (sc==curscreen) handle_view3d_lock();
1371 for(ar=v3d->regionbase.first; ar; ar= ar->next) {
1372 if(ar->regiontype == RGN_TYPE_WINDOW) {
1373 RegionView3D *rv3d= ar->regiondata;
1375 if(rv3d->persp==RV3D_CAMOB)
1376 rv3d->persp= RV3D_PERSP;
1389 CTX_data_scene_set(C, scene);
1390 set_scene_bg(scene);
1392 ED_update_for_newframe(C, 1);
1394 /* complete redraw */
1395 WM_event_add_notifier(C, NC_WINDOW, NULL);
1399 /* only call outside of area/region loops */
1400 void ED_screen_delete_scene(bContext *C, Scene *scene)
1402 Main *bmain= CTX_data_main(C);
1406 newscene= scene->id.prev;
1407 else if(scene->id.next)
1408 newscene= scene->id.next;
1412 ED_screen_set_scene(C, newscene);
1414 unlink_scene(bmain, scene, newscene);
1417 /* this function toggles: if area is full then the parent will be restored */
1418 ScrArea *ed_screen_fullarea(bContext *C, wmWindow *win, ScrArea *sa)
1420 bScreen *sc, *oldscreen;
1424 /* ensure we don't have a button active anymore, can crash when
1425 switching screens with tooltip open because region and tooltip
1426 are no longer in the same screen */
1427 for(ar=sa->regionbase.first; ar; ar=ar->next)
1428 uiFreeBlocks(C, &ar->uiblocks);
1431 if(sa && sa->full) {
1434 sc= sa->full; /* the old screen to restore */
1435 oldscreen= win->screen; /* the one disappearing */
1437 fulltype = sc->full;
1439 /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
1442 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
1448 for(old= sc->areabase.first; old; old= old->next)
1449 if(old->full) break;
1451 printf("something wrong in areafullscreen\n");
1454 // old feature described below (ton)
1455 // in autoplay screens the headers are disabled by
1456 // default. So use the old headertype instead
1458 area_copy_data(old, sa, 1); /* 1 = swap spacelist */
1462 /* animtimer back */
1463 sc->animtimer= oldscreen->animtimer;
1464 oldscreen->animtimer= NULL;
1466 ED_screen_set(C, sc);
1468 free_screen(oldscreen);
1469 free_libblock(&CTX_data_main(C)->screen, oldscreen);
1475 oldscreen= win->screen;
1477 /* is there only 1 area? */
1478 if(oldscreen->areabase.first==oldscreen->areabase.last)
1481 oldscreen->full = SCREENFULL;
1483 sc= ED_screen_add(win, oldscreen->scene, "temp");
1484 sc->full = SCREENFULL; // XXX
1487 sc->animtimer= oldscreen->animtimer;
1488 oldscreen->animtimer= NULL;
1490 /* returns the top small area */
1491 newa= area_split(win, sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
1492 ED_area_newspace(C, newa, SPACE_INFO);
1494 /* use random area when we have no active one, e.g. when the
1495 mouse is outside of the window and we open a file browser */
1497 sa= oldscreen->areabase.first;
1501 area_copy_data(newa, sa, 1); /* 1 = swap spacelist */
1503 sa->full= oldscreen;
1504 newa->full= oldscreen;
1505 newa->next->full= oldscreen; // XXX
1507 ED_screen_set(C, sc);
1510 /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1511 CTX_wm_area_set(C, sc->areabase.first);
1513 /* XXX retopo_force_update(); */
1515 return sc->areabase.first;
1518 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1520 wmWindow *win= CTX_wm_window(C);
1521 ScrArea *newsa= NULL;
1523 if(!sa || sa->full==0)
1524 newsa= ed_screen_fullarea(C, win, sa);
1528 ED_area_newspace(C, newsa, type);
1533 void ED_screen_full_prevspace(bContext *C)
1535 wmWindow *win= CTX_wm_window(C);
1536 ScrArea *sa= CTX_wm_area(C);
1538 ED_area_prevspace(C);
1541 ed_screen_fullarea(C, win, sa);
1544 /* redraws: uses defines from stime->redraws
1545 * enable: 1 - forward on, -1 - backwards on, 0 - off
1547 void ED_screen_animation_timer(bContext *C, int redraws, int sync, int enable)
1549 bScreen *screen= CTX_wm_screen(C);
1550 wmWindowManager *wm= CTX_wm_manager(C);
1551 wmWindow *win= CTX_wm_window(C);
1552 Scene *scene= CTX_data_scene(C);
1554 if(screen->animtimer)
1555 WM_event_remove_timer(wm, win, screen->animtimer);
1556 screen->animtimer= NULL;
1559 struct ScreenAnimData *sad= MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1561 screen->animtimer= WM_event_add_timer(wm, win, TIMER0, (1.0/FPS));
1562 sad->ar= CTX_wm_region(C);
1563 sad->redraws= redraws;
1564 sad->flag |= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1565 sad->flag |= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1566 screen->animtimer->customdata= sad;
1569 /* notifier catched by top header, for button */
1570 WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1573 /* helper for screen_animation_play() - only to be used for TimeLine */
1574 static ARegion *time_top_left_3dwindow(bScreen *screen)
1576 ARegion *aret= NULL;
1580 for(sa= screen->areabase.first; sa; sa= sa->next) {
1581 if(sa->spacetype==SPACE_VIEW3D) {
1583 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1584 if(ar->regiontype==RGN_TYPE_WINDOW) {
1585 if(ar->winrct.xmin - ar->winrct.ymin < min) {
1587 min= ar->winrct.xmin - ar->winrct.ymin;
1597 void ED_screen_animation_timer_update(bContext *C, int redraws)
1599 bScreen *screen= CTX_wm_screen(C);
1601 if(screen && screen->animtimer) {
1602 wmTimer *wt= screen->animtimer;
1603 ScreenAnimData *sad= wt->customdata;
1605 sad->redraws= redraws;
1607 if(redraws & TIME_REGION)
1608 sad->ar= time_top_left_3dwindow(screen);
1612 /* results in fully updated anim system */
1613 void ED_update_for_newframe(const bContext *C, int mute)
1615 bScreen *screen= CTX_wm_screen(C);
1616 Scene *scene= screen->scene;
1618 //extern void audiostream_scrub(unsigned int frame); /* seqaudio.c */
1620 /* this function applies the changes too */
1621 /* XXX future: do all windows */
1622 scene_update_for_newframe(scene, BKE_screen_visible_layers(screen)); /* BKE_scene.h */
1624 //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB))
1625 // audiostream_scrub( CFRA );
1627 /* 3d window, preview */
1628 //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1630 /* all movie/sequence images */
1631 //BIF_image_update_frame();
1634 if(scene->use_nodes && scene->nodetree)
1635 ntreeCompositTagAnimated(scene->nodetree);
1637 /* update animated texture nodes */
1640 for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
1641 if( tex->use_nodes && tex->nodetree ) {
1642 ntreeTexTagAnimated( tex->nodetree );