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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/screen/screen_edit.c
36 #include "MEM_guardedalloc.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_library.h"
49 #include "BKE_screen.h"
50 #include "BKE_scene.h"
53 #include "BIF_glutil.h"
59 #include "ED_object.h"
60 #include "ED_screen.h"
61 #include "ED_screen_types.h"
62 #include "ED_fileselect.h"
64 #include "UI_interface.h"
66 /* XXX actually should be not here... solve later */
67 #include "wm_subwindow.h"
69 #include "screen_intern.h" /* own module include */
72 /* ******************* screen vert, edge, area managing *********************** */
74 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
76 ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
80 BLI_addtail(&sc->vertbase, sv);
84 static void sortscrvert(ScrVert **v1, ScrVert **v2)
95 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
97 ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
99 sortscrvert(&v1, &v2);
103 BLI_addtail(&sc->edgebase, se);
108 ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
112 sortscrvert(&v1, &v2);
113 for (se= sc->edgebase.first; se; se= se->next)
114 if(se->v1==v1 && se->v2==v2)
120 void removedouble_scrverts(bScreen *sc)
126 verg= sc->vertbase.first;
128 if(verg->newv==NULL) { /* !!! */
131 if(v1->newv==NULL) { /* !?! */
132 if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
133 /* printf("doublevert\n"); */
143 /* replace pointers in edges and faces */
144 se= sc->edgebase.first;
146 if(se->v1->newv) se->v1= se->v1->newv;
147 if(se->v2->newv) se->v2= se->v2->newv;
148 /* edges changed: so.... */
149 sortscrvert(&(se->v1), &(se->v2));
152 sa= sc->areabase.first;
154 if(sa->v1->newv) sa->v1= sa->v1->newv;
155 if(sa->v2->newv) sa->v2= sa->v2->newv;
156 if(sa->v3->newv) sa->v3= sa->v3->newv;
157 if(sa->v4->newv) sa->v4= sa->v4->newv;
162 verg= sc->vertbase.first;
166 BLI_remlink(&sc->vertbase, verg);
174 void removenotused_scrverts(bScreen *sc)
179 /* we assume edges are ok */
181 se= sc->edgebase.first;
188 sv= sc->vertbase.first;
192 BLI_remlink(&sc->vertbase, sv);
200 void removedouble_scredges(bScreen *sc)
202 ScrEdge *verg, *se, *sn;
205 verg= sc->edgebase.first;
210 if(verg->v1==se->v1 && verg->v2==se->v2) {
211 BLI_remlink(&sc->edgebase, se);
220 void removenotused_scredges(bScreen *sc)
226 /* sets flags when edge is used in area */
227 sa= sc->areabase.first;
229 se= screen_findedge(sc, sa->v1, sa->v2);
230 if(se==NULL) printf("error: area %d edge 1 doesn't exist\n", a);
232 se= screen_findedge(sc, sa->v2, sa->v3);
233 if(se==NULL) printf("error: area %d edge 2 doesn't exist\n", a);
235 se= screen_findedge(sc, sa->v3, sa->v4);
236 if(se==NULL) printf("error: area %d edge 3 doesn't exist\n", a);
238 se= screen_findedge(sc, sa->v4, sa->v1);
239 if(se==NULL) printf("error: area %d edge 4 doesn't exist\n", a);
244 se= sc->edgebase.first;
248 BLI_remlink(&sc->edgebase, se);
256 int scredge_is_horizontal(ScrEdge *se)
258 return (se->v1->vec.y == se->v2->vec.y);
261 ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
265 for (se= sc->edgebase.first; se; se= se->next) {
266 if (scredge_is_horizontal(se)) {
268 min= MIN2(se->v1->vec.x, se->v2->vec.x);
269 max= MAX2(se->v1->vec.x, se->v2->vec.x);
271 if (abs(my-se->v1->vec.y)<=2 && mx>=min && mx<=max)
276 min= MIN2(se->v1->vec.y, se->v2->vec.y);
277 max= MAX2(se->v1->vec.y, se->v2->vec.y);
279 if (abs(mx-se->v1->vec.x)<=2 && my>=min && my<=max)
289 /* adds no space data */
290 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
292 ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
297 sa->headertype= headertype;
298 sa->spacetype= sa->butspacetype= spacetype;
300 BLI_addtail(&sc->areabase, sa);
305 static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
310 BKE_screen_area_free(sa);
312 BLI_remlink(&sc->areabase, sa);
316 /* return 0: no split possible */
317 /* else return (integer) screencoordinate split point */
318 static short testsplitpoint(ScrArea *sa, char dir, float fac)
323 if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
324 if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
327 CLAMP(fac, 0.0f, 1.0f);
330 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
332 if(y- sa->v1->vec.y < AREAMINY)
333 y= sa->v1->vec.y+ AREAMINY;
334 else if(sa->v2->vec.y- y < AREAMINY)
335 y= sa->v2->vec.y- AREAMINY;
336 else y-= (y % AREAGRID);
341 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
343 if(x- sa->v1->vec.x < AREAMINX)
344 x= sa->v1->vec.x+ AREAMINX;
345 else if(sa->v4->vec.x- x < AREAMINX)
346 x= sa->v4->vec.x- AREAMINX;
347 else x-= (x % AREAGRID);
353 ScrArea *area_split(bScreen *sc, ScrArea *sa, char dir, float fac, int merge)
359 if(sa==NULL) return NULL;
361 split= testsplitpoint(sa, dir, fac);
362 if(split==0) return NULL;
366 sv1= screen_addvert(sc, sa->v1->vec.x, split);
367 sv2= screen_addvert(sc, sa->v4->vec.x, split);
370 screen_addedge(sc, sa->v1, sv1);
371 screen_addedge(sc, sv1, sa->v2);
372 screen_addedge(sc, sa->v3, sv2);
373 screen_addedge(sc, sv2, sa->v4);
374 screen_addedge(sc, sv1, sv2);
377 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
378 area_copy_data(newa, sa, 0);
387 sv1= screen_addvert(sc, split, sa->v1->vec.y);
388 sv2= screen_addvert(sc, split, sa->v2->vec.y);
391 screen_addedge(sc, sa->v1, sv1);
392 screen_addedge(sc, sv1, sa->v4);
393 screen_addedge(sc, sa->v2, sv2);
394 screen_addedge(sc, sv2, sa->v3);
395 screen_addedge(sc, sv1, sv2);
397 /* new areas: left */
398 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
399 area_copy_data(newa, sa, 0);
406 /* remove double vertices en edges */
408 removedouble_scrverts(sc);
409 removedouble_scredges(sc);
410 removenotused_scredges(sc);
415 /* empty screen, with 1 dummy area without spacedata */
416 /* uses window size */
417 bScreen *ED_screen_add(wmWindow *win, Scene *scene, const char *name)
420 ScrVert *sv1, *sv2, *sv3, *sv4;
422 sc= alloc_libblock(&G.main->screen, ID_SCR, name);
425 sc->redraws_flag= TIME_ALL_3D_WIN|TIME_ALL_ANIM_WIN;
426 sc->winid= win->winid;
428 sv1= screen_addvert(sc, 0, 0);
429 sv2= screen_addvert(sc, 0, win->sizey-1);
430 sv3= screen_addvert(sc, win->sizex-1, win->sizey-1);
431 sv4= screen_addvert(sc, win->sizex-1, 0);
433 screen_addedge(sc, sv1, sv2);
434 screen_addedge(sc, sv2, sv3);
435 screen_addedge(sc, sv3, sv4);
436 screen_addedge(sc, sv4, sv1);
438 /* dummy type, no spacedata */
439 screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
444 static void screen_copy(bScreen *to, bScreen *from)
450 /* free contents of 'to', is from blenkernel screen.c */
453 BLI_duplicatelist(&to->vertbase, &from->vertbase);
454 BLI_duplicatelist(&to->edgebase, &from->edgebase);
455 BLI_duplicatelist(&to->areabase, &from->areabase);
456 to->regionbase.first= to->regionbase.last= NULL;
458 s2= to->vertbase.first;
459 for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
463 for(se= to->edgebase.first; se; se= se->next) {
464 se->v1= se->v1->newv;
465 se->v2= se->v2->newv;
466 sortscrvert(&(se->v1), &(se->v2));
469 saf= from->areabase.first;
470 for(sa= to->areabase.first; sa; sa= sa->next, saf= saf->next) {
471 sa->v1= sa->v1->newv;
472 sa->v2= sa->v2->newv;
473 sa->v3= sa->v3->newv;
474 sa->v4= sa->v4->newv;
476 sa->spacedata.first= sa->spacedata.last= NULL;
477 sa->regionbase.first= sa->regionbase.last= NULL;
478 sa->actionzones.first= sa->actionzones.last= NULL;
479 sa->handlers.first= sa->handlers.last= NULL;
481 area_copy_data(sa, saf, 0);
484 /* put at zero (needed?) */
485 for(s1= from->vertbase.first; s1; s1= s1->next)
491 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
492 /* -1 = not valid check */
493 /* used with join operator */
494 int area_getorientation(ScrArea *sa, ScrArea *sb)
496 ScrVert *sav1, *sav2, *sav3, *sav4;
497 ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
499 if(sa==NULL || sb==NULL) return -1;
510 if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
513 else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
516 else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
519 else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
526 /* Helper function to join 2 areas, it has a return value, 0=failed 1=success
527 * used by the split, join operators
529 int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2)
533 dir = area_getorientation(sa1, sa2);
534 /*printf("dir is : %i \n", dir);*/
538 if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
539 if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
546 screen_addedge(scr, sa1->v2, sa1->v3);
547 screen_addedge(scr, sa1->v1, sa1->v4);
552 screen_addedge(scr, sa1->v1, sa1->v2);
553 screen_addedge(scr, sa1->v3, sa1->v4);
558 screen_addedge(scr, sa1->v2, sa1->v3);
559 screen_addedge(scr, sa1->v1, sa1->v4);
564 screen_addedge(scr, sa1->v1, sa1->v2);
565 screen_addedge(scr, sa1->v3, sa1->v4);
568 screen_delarea(C, scr, sa2);
569 removedouble_scrverts(scr);
570 sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
575 void select_connected_scredge(bScreen *sc, ScrEdge *edge)
582 /* select connected, only in the right direction */
583 /* 'dir' is the direction of EDGE */
585 if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
588 sv= sc->vertbase.first;
599 se= sc->edgebase.first;
602 if(se->v1->flag + se->v2->flag==1) {
603 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
604 se->v1->flag= se->v2->flag= 1;
607 if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
608 se->v1->flag= se->v2->flag= 1;
617 /* test if screen vertices should be scaled */
618 static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
623 float facx, facy, tempf, min[2], max[2];
626 min[0]= min[1]= 10000.0f;
627 max[0]= max[1]= 0.0f;
629 for(sv= sc->vertbase.first; sv; sv= sv->next) {
630 min[0]= MIN2(min[0], sv->vec.x);
631 min[1]= MIN2(min[1], sv->vec.y);
632 max[0]= MAX2(max[0], sv->vec.x);
633 max[1]= MAX2(max[1], sv->vec.y);
636 /* always make 0.0 left under */
637 for(sv= sc->vertbase.first; sv; sv= sv->next) {
642 sizex= max[0]-min[0];
643 sizey= max[1]-min[1];
645 if(sizex!= winsizex || sizey!= winsizey) {
651 /* make sure it fits! */
652 for(sv= sc->vertbase.first; sv; sv= sv->next) {
653 /* FIXME, this resizing logic is no good when resizing the window + redrawing [#24428]
654 * need some way to store these as floats internally and re-apply from there. */
655 tempf= ((float)sv->vec.x)*facx;
656 sv->vec.x= (short)(tempf+0.5f);
657 sv->vec.x+= AREAGRID-1;
658 sv->vec.x-= (sv->vec.x % AREAGRID);
660 CLAMP(sv->vec.x, 0, winsizex);
662 tempf= ((float)sv->vec.y)*facy;
663 sv->vec.y= (short)(tempf+0.5f);
664 sv->vec.y+= AREAGRID-1;
665 sv->vec.y-= (sv->vec.y % AREAGRID);
667 CLAMP(sv->vec.y, 0, winsizey);
671 /* test for collapsed areas. This could happen in some blender version... */
672 /* ton: removed option now, it needs Context... */
674 /* make each window at least HEADERY high */
675 for(sa= sc->areabase.first; sa; sa= sa->next) {
676 int headery= HEADERY+1;
678 if(sa->v1->vec.y+headery > sa->v2->vec.y) {
680 ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
681 if(se && sa->v1!=sa->v2 ) {
684 select_connected_scredge(sc, se);
686 /* all selected vertices get the right offset */
687 yval= sa->v2->vec.y-headery;
688 sv= sc->vertbase.first;
690 /* if is a collapsed area */
691 if(sv!=sa->v2 && sv!=sa->v3) {
692 if(sv->flag) sv->vec.y= yval;
702 /* *********************** DRAWING **************************************** */
705 #define SCR_BACK 0.55
708 /* draw vertical shape visualising future joining (left as well
709 * right direction of future joining) */
710 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
715 float width = sa->v3->vec.x - sa->v1->vec.x;
716 float height = sa->v3->vec.y - sa->v1->vec.y;
727 points[0].x = sa->v1->vec.x;
728 points[0].y = sa->v1->vec.y + height/2;
730 points[1].x = sa->v1->vec.x;
731 points[1].y = sa->v1->vec.y;
733 points[2].x = sa->v4->vec.x - w;
734 points[2].y = sa->v4->vec.y;
736 points[3].x = sa->v4->vec.x - w;
737 points[3].y = sa->v4->vec.y + height/2 - 2*h;
739 points[4].x = sa->v4->vec.x - 2*w;
740 points[4].y = sa->v4->vec.y + height/2;
742 points[5].x = sa->v4->vec.x - w;
743 points[5].y = sa->v4->vec.y + height/2 + 2*h;
745 points[6].x = sa->v3->vec.x - w;
746 points[6].y = sa->v3->vec.y;
748 points[7].x = sa->v2->vec.x;
749 points[7].y = sa->v2->vec.y;
751 points[8].x = sa->v4->vec.x;
752 points[8].y = sa->v4->vec.y + height/2 - h;
754 points[9].x = sa->v4->vec.x;
755 points[9].y = sa->v4->vec.y + height/2 + h;
758 /* when direction is left, then we flip direction of arrow */
759 float cx = sa->v1->vec.x + width;
762 points[i].x = -points[i].x;
763 points[i].x += sa->v1->vec.x;
769 glVertex2f(points[i].x, points[i].y);
773 glVertex2f(points[i].x, points[i].y);
774 glVertex2f(points[0].x, points[0].y);
777 glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
778 glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
781 /* draw vertical shape visualising future joining (up/down direction) */
782 static void draw_vertical_join_shape(ScrArea *sa, char dir)
787 float width = sa->v3->vec.x - sa->v1->vec.x;
788 float height = sa->v3->vec.y - sa->v1->vec.y;
799 points[0].x = sa->v1->vec.x + width/2;
800 points[0].y = sa->v3->vec.y;
802 points[1].x = sa->v2->vec.x;
803 points[1].y = sa->v2->vec.y;
805 points[2].x = sa->v1->vec.x;
806 points[2].y = sa->v1->vec.y + h;
808 points[3].x = sa->v1->vec.x + width/2 - 2*w;
809 points[3].y = sa->v1->vec.y + h;
811 points[4].x = sa->v1->vec.x + width/2;
812 points[4].y = sa->v1->vec.y + 2*h;
814 points[5].x = sa->v1->vec.x + width/2 + 2*w;
815 points[5].y = sa->v1->vec.y + h;
817 points[6].x = sa->v4->vec.x;
818 points[6].y = sa->v4->vec.y + h;
820 points[7].x = sa->v3->vec.x;
821 points[7].y = sa->v3->vec.y;
823 points[8].x = sa->v1->vec.x + width/2 - w;
824 points[8].y = sa->v1->vec.y;
826 points[9].x = sa->v1->vec.x + width/2 + w;
827 points[9].y = sa->v1->vec.y;
830 /* when direction is up, then we flip direction of arrow */
831 float cy = sa->v1->vec.y + height;
834 points[i].y = -points[i].y;
835 points[i].y += sa->v1->vec.y;
841 glVertex2f(points[i].x, points[i].y);
845 glVertex2f(points[i].x, points[i].y);
846 glVertex2f(points[0].x, points[0].y);
849 glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
850 glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
853 /* draw join shape due to direction of joining */
854 static void draw_join_shape(ScrArea *sa, char dir)
856 if(dir=='u' || dir=='d')
857 draw_vertical_join_shape(sa, dir);
859 draw_horizontal_join_shape(sa, dir);
862 /* draw screen area darker with arrow (visualisation of future joining) */
863 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
865 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
867 glColor4ub(0, 0, 0, 50);
868 draw_join_shape(sa, dir);
872 /* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
873 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
875 glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
877 /* value 181 was hardly computed: 181~105 */
878 glColor4ub(255, 255, 255, 50);
879 /* draw_join_shape(sa, dir); */
880 glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
884 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a)
886 /* right border area */
888 sdrawline(x2+a, y1, x2+a, y2);
890 /* left border area */
891 if(x1>0) /* otherwise it draws the emboss of window over */
892 sdrawline(x1+a, y1, x1+a, y2);
894 /* top border area */
896 sdrawline(x1, y2+a, x2, y2+a);
898 /* bottom border area */
900 sdrawline(x1, y1+a, x2, y1+a);
904 /** screen edges drawing **/
905 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
907 short x1= sa->v1->vec.x;
908 short y1= sa->v1->vec.y;
909 short x2= sa->v3->vec.x;
910 short y2= sa->v3->vec.y;
913 rt= CLAMPIS(G.rt, 0, 16);
917 for(a=-rt; a<=rt; a++)
919 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
923 drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
927 /* ****************** EXPORTED API TO OTHER MODULES *************************** */
929 bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
933 if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
935 /* make new empty screen: */
936 newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
938 screen_copy(newsc, sc);
943 /* screen sets cursor based on swinid */
944 static void region_cursor_set(wmWindow *win, int swinid)
946 ScrArea *sa= win->screen->areabase.first;
948 for(;sa; sa= sa->next) {
949 ARegion *ar= sa->regionbase.first;
950 for(;ar; ar= ar->next) {
951 if(ar->swinid == swinid) {
952 if(ar->type && ar->type->cursor)
953 ar->type->cursor(win, sa, ar);
955 WM_cursor_set(win, CURSOR_STD);
962 void ED_screen_do_listen(bContext *C, wmNotifier *note)
964 wmWindow *win= CTX_wm_window(C);
967 switch(note->category) {
969 if(note->data==ND_FILEREAD)
970 win->screen->do_draw= 1;
973 win->screen->do_draw= 1;
976 if(note->data==ND_SUBWINACTIVE)
977 uiFreeActiveButtons(C, win->screen);
978 if(note->action==NA_EDITED)
979 win->screen->do_draw= win->screen->do_refresh= 1;
982 if(note->data==ND_MODE)
983 region_cursor_set(win, note->swinid);
988 /* only for edge lines between areas, and the blended join arrows */
989 void ED_screen_draw(wmWindow *win)
998 wmSubWindowSet(win, win->screen->mainwin);
1000 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1001 if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
1002 if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
1003 if (sa->flag & (AREA_FLAG_DRAWSPLIT_H|AREA_FLAG_DRAWSPLIT_V)) sa3 = sa;
1004 drawscredge_area(sa, win->sizex, win->sizey, 0);
1006 for(sa= win->screen->areabase.first; sa; sa= sa->next)
1007 drawscredge_area(sa, win->sizex, win->sizey, 1);
1009 /* blended join arrow */
1011 dir = area_getorientation(sa1, sa2);
1032 scrarea_draw_shape_dark(sa2, dir);
1033 scrarea_draw_shape_light(sa1, dira);
1039 glColor4ub(255, 255, 255, 100);
1041 if(sa3->flag & AREA_FLAG_DRAWSPLIT_H) {
1042 sdrawline(sa3->totrct.xmin, win->eventstate->y, sa3->totrct.xmax, win->eventstate->y);
1043 glColor4ub(0, 0, 0, 100);
1044 sdrawline(sa3->totrct.xmin, win->eventstate->y+1, sa3->totrct.xmax, win->eventstate->y+1);
1047 sdrawline(win->eventstate->x, sa3->totrct.ymin, win->eventstate->x, sa3->totrct.ymax);
1048 glColor4ub(0, 0, 0, 100);
1049 sdrawline(win->eventstate->x+1, sa3->totrct.ymin, win->eventstate->x+1, sa3->totrct.ymax);
1052 glDisable(GL_BLEND);
1055 win->screen->do_draw= 0;
1058 /* make this screen usable */
1059 /* for file read and first use, for scaling window, area moves */
1060 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
1062 /* exception for bg mode, we only need the screen context */
1063 if (!G.background) {
1068 winrct.xmax= win->sizex-1;
1070 winrct.ymax= win->sizey-1;
1072 screen_test_scale(win->screen, win->sizex, win->sizey);
1074 if(win->screen->mainwin==0)
1075 win->screen->mainwin= wm_subwindow_open(win, &winrct);
1077 wm_subwindow_position(win, win->screen->mainwin, &winrct);
1079 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1080 /* set spacetype and region callbacks, calls init() */
1081 /* sets subwindows for regions, adds handlers */
1082 ED_area_initialize(wm, win, sa);
1085 /* wake up animtimer */
1086 if(win->screen->animtimer)
1087 WM_event_timer_sleep(wm, win, win->screen->animtimer, 0);
1090 if(G.f & G_DEBUG) printf("set screen\n");
1091 win->screen->do_refresh= 0;
1093 win->screen->context= ed_screen_context;
1096 /* file read, set all screens, ... */
1097 void ED_screens_initialize(wmWindowManager *wm)
1101 for(win= wm->windows.first; win; win= win->next) {
1103 if(win->screen==NULL)
1104 win->screen= G.main->screen.first;
1106 ED_screen_refresh(wm, win);
1111 /* *********** exit calls are for closing running stuff ******** */
1113 void ED_region_exit(bContext *C, ARegion *ar)
1115 ARegion *prevar= CTX_wm_region(C);
1117 CTX_wm_region_set(C, ar);
1118 WM_event_remove_handlers(C, &ar->handlers);
1120 wm_subwindow_close(CTX_wm_window(C), ar->swinid);
1124 MEM_freeN(ar->headerstr);
1125 ar->headerstr= NULL;
1127 CTX_wm_region_set(C, prevar);
1130 void ED_area_exit(bContext *C, ScrArea *sa)
1132 ScrArea *prevsa= CTX_wm_area(C);
1135 if (sa->spacetype == SPACE_FILE) {
1136 SpaceLink *sl= sa->spacedata.first;
1137 if(sl && sl->spacetype == SPACE_FILE) {
1138 ED_fileselect_exit(C, (SpaceFile *)sl);
1142 CTX_wm_area_set(C, sa);
1143 for(ar= sa->regionbase.first; ar; ar= ar->next)
1144 ED_region_exit(C, ar);
1146 WM_event_remove_handlers(C, &sa->handlers);
1147 CTX_wm_area_set(C, prevsa);
1150 void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
1152 wmWindowManager *wm= CTX_wm_manager(C);
1153 wmWindow *prevwin= CTX_wm_window(C);
1157 CTX_wm_window_set(C, window);
1159 if(screen->animtimer)
1160 WM_event_remove_timer(wm, window, screen->animtimer);
1161 screen->animtimer= NULL;
1164 wm_subwindow_close(window, screen->mainwin);
1166 screen->subwinactive= 0;
1168 for(ar= screen->regionbase.first; ar; ar= ar->next)
1169 ED_region_exit(C, ar);
1171 for(sa= screen->areabase.first; sa; sa= sa->next)
1172 ED_area_exit(C, sa);
1174 /* mark it available for use for other windows */
1177 if (prevwin->screen->temp == 0) {
1178 /* use previous window if possible */
1179 CTX_wm_window_set(C, prevwin);
1181 /* none otherwise */
1182 CTX_wm_window_set(C, NULL);
1187 /* *********************************** */
1189 /* case when on area-edge or in azones, or outside window */
1190 static void screen_cursor_set(wmWindow *win, wmEvent *event)
1195 for(sa= win->screen->areabase.first; sa; sa= sa->next)
1196 if((az=is_in_area_actionzone(sa, event->x, event->y)))
1200 if(az->type==AZONE_AREA)
1201 WM_cursor_set(win, CURSOR_EDIT);
1202 else if(az->type==AZONE_REGION) {
1203 if(az->edge == AE_LEFT_TO_TOPRIGHT || az->edge == AE_RIGHT_TO_TOPLEFT)
1204 WM_cursor_set(win, CURSOR_X_MOVE);
1206 WM_cursor_set(win, CURSOR_Y_MOVE);
1210 ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
1213 if(scredge_is_horizontal(actedge))
1214 WM_cursor_set(win, CURSOR_Y_MOVE);
1216 WM_cursor_set(win, CURSOR_X_MOVE);
1219 WM_cursor_set(win, CURSOR_STD);
1224 /* called in wm_event_system.c. sets state vars in screen, cursors */
1225 /* event type is mouse move */
1226 void ED_screen_set_subwinactive(bContext *C, wmEvent *event)
1228 wmWindow *win= CTX_wm_window(C);
1231 bScreen *scr= win->screen;
1234 int oldswin= scr->subwinactive;
1236 for(sa= scr->areabase.first; sa; sa= sa->next) {
1237 if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
1238 if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
1239 if(NULL==is_in_area_actionzone(sa, event->x, event->y))
1243 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1244 if(BLI_in_rcti(&ar->winrct, event->x, event->y))
1245 scr->subwinactive= ar->swinid;
1249 scr->subwinactive= scr->mainwin;
1251 /* check for redraw headers */
1252 if(oldswin!=scr->subwinactive) {
1254 for(sa= scr->areabase.first; sa; sa= sa->next) {
1257 for(ar= sa->regionbase.first; ar; ar= ar->next)
1258 if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
1262 for(ar= sa->regionbase.first; ar; ar= ar->next)
1263 if(ar->regiontype==RGN_TYPE_HEADER)
1264 ED_region_tag_redraw(ar);
1269 /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
1270 if(scr->subwinactive==scr->mainwin) {
1271 screen_cursor_set(win, event);
1273 else if(oldswin!=scr->subwinactive) {
1274 region_cursor_set(win, scr->subwinactive);
1275 WM_event_add_notifier(C, NC_SCREEN|ND_SUBWINACTIVE, scr);
1280 int ED_screen_area_active(const bContext *C)
1282 wmWindow *win= CTX_wm_window(C);
1283 bScreen *sc= CTX_wm_screen(C);
1284 ScrArea *sa= CTX_wm_area(C);
1286 if(win && sc && sa) {
1287 AZone *az= is_in_area_actionzone(sa, win->eventstate->x, win->eventstate->y);
1290 if (az && az->type == AZONE_REGION)
1293 for(ar= sa->regionbase.first; ar; ar= ar->next)
1294 if(ar->swinid == sc->subwinactive)
1300 /* operator call, WM + Window + screen already existed before */
1301 /* Do NOT call in area/region queues! */
1302 void ED_screen_set(bContext *C, bScreen *sc)
1304 wmWindowManager *wm= CTX_wm_manager(C);
1305 wmWindow *win= CTX_wm_window(C);
1306 bScreen *oldscreen= CTX_wm_screen(C);
1309 /* validate screen, it's called with notifier reference */
1310 for(id= CTX_data_main(C)->screen.first; id; id= id->next)
1311 if(sc == (bScreen *)id)
1316 /* check for valid winid */
1317 if(sc->winid!=0 && sc->winid!=win->winid)
1320 if(sc->full) { /* find associated full */
1322 for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
1323 ScrArea *sa= sc1->areabase.first;
1331 if (oldscreen != sc) {
1332 wmTimer *wt= oldscreen->animtimer;
1335 /* remove handlers referencing areas in old screen */
1336 for(sa = oldscreen->areabase.first; sa; sa = sa->next) {
1337 WM_event_remove_area_handler(&win->modalhandlers, sa);
1340 /* we put timer to sleep, so screen_exit has to think there's no timer */
1341 oldscreen->animtimer= NULL;
1343 WM_event_timer_sleep(wm, win, wt, 1);
1345 ED_screen_exit(C, win, oldscreen);
1346 oldscreen->animtimer= wt;
1349 CTX_wm_window_set(C, win); // stores C->wm.screen... hrmf
1351 /* prevent multiwin errors */
1352 sc->winid= win->winid;
1354 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1355 WM_event_add_notifier(C, NC_WINDOW, NULL);
1356 WM_event_add_notifier(C, NC_SCREEN|ND_SCREENSET, sc);
1358 /* makes button hilites work */
1359 WM_event_add_mousemove(C);
1363 static int ed_screen_used(wmWindowManager *wm, bScreen *sc)
1367 for(win=wm->windows.first; win; win=win->next)
1368 if(win->screen == sc)
1374 /* only call outside of area/region loops */
1375 void ED_screen_delete(bContext *C, bScreen *sc)
1377 Main *bmain= CTX_data_main(C);
1378 wmWindowManager *wm= CTX_wm_manager(C);
1379 wmWindow *win= CTX_wm_window(C);
1383 /* don't allow deleting temp fullscreens for now */
1384 if (sc->full == SCREENFULL) {
1389 /* screen can only be in use by one window at a time, so as
1390 long as we are able to find a screen that is unused, we
1391 can safely assume ours is not in use anywhere an delete it */
1393 for(newsc= sc->id.prev; newsc; newsc=newsc->id.prev)
1394 if(!ed_screen_used(wm, newsc))
1398 for(newsc= sc->id.next; newsc; newsc=newsc->id.next)
1399 if(!ed_screen_used(wm, newsc))
1406 ED_screen_set(C, newsc);
1408 if(delete && win->screen != sc)
1409 free_libblock(&bmain->screen, sc);
1412 /* only call outside of area/region loops */
1413 void ED_screen_set_scene(bContext *C, Scene *scene)
1416 bScreen *curscreen= CTX_wm_screen(C);
1418 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
1420 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1421 if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1423 if(scene != sc->scene) {
1424 /* all areas endlocalview */
1425 // XXX ScrArea *sa= sc->areabase.first;
1427 // endlocalview(sa);
1436 // copy_view3d_lock(0); /* space.c */
1438 /* are there cameras in the views that are not in the scene? */
1439 for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
1440 if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
1441 ScrArea *sa= sc->areabase.first;
1443 SpaceLink *sl= sa->spacedata.first;
1445 if(sl->spacetype==SPACE_VIEW3D) {
1446 View3D *v3d= (View3D*) sl;
1448 BKE_screen_view3d_sync(v3d, scene);
1450 if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
1451 v3d->camera= scene_find_camera(sc->scene);
1452 // XXX if (sc==curscreen) handle_view3d_lock();
1455 for(ar=v3d->regionbase.first; ar; ar= ar->next) {
1456 if(ar->regiontype == RGN_TYPE_WINDOW) {
1457 RegionView3D *rv3d= ar->regiondata;
1459 if(rv3d->persp==RV3D_CAMOB)
1460 rv3d->persp= RV3D_PERSP;
1473 CTX_data_scene_set(C, scene);
1474 set_scene_bg(CTX_data_main(C), scene);
1476 ED_update_for_newframe(CTX_data_main(C), scene, curscreen, 1);
1478 /* complete redraw */
1479 WM_event_add_notifier(C, NC_WINDOW, NULL);
1483 /* only call outside of area/region loops */
1484 void ED_screen_delete_scene(bContext *C, Scene *scene)
1486 Main *bmain= CTX_data_main(C);
1490 newscene= scene->id.prev;
1491 else if(scene->id.next)
1492 newscene= scene->id.next;
1496 ED_screen_set_scene(C, newscene);
1498 unlink_scene(bmain, scene, newscene);
1501 int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
1503 wmWindow *win= CTX_wm_window(C);
1504 bScreen *screen= CTX_wm_screen(C);
1505 ScrArea *newsa= NULL;
1507 if(!sa || sa->full==NULL) {
1508 newsa= ED_screen_full_toggle(C, win, sa);
1513 /* if this has been called from the temporary info header generated in
1514 * temp fullscreen layouts, find the correct fullscreen area to change
1515 * to create a new space inside */
1516 for (newsa = screen->areabase.first; newsa; newsa=newsa->next) {
1517 if (!(sa->flag & AREA_TEMP_INFO))
1524 ED_area_newspace(C, newsa, type);
1529 void ED_screen_full_prevspace(bContext *C, ScrArea *sa)
1531 wmWindow *win= CTX_wm_window(C);
1533 ED_area_prevspace(C, sa);
1536 ED_screen_full_toggle(C, win, sa);
1539 /* restore a screen / area back to default operation, after temp fullscreen modes */
1540 void ED_screen_full_restore(bContext *C, ScrArea *sa)
1542 wmWindow *win= CTX_wm_window(C);
1543 SpaceLink *sl = sa->spacedata.first;
1545 /* if fullscreen area has a secondary space (such as a file browser or fullscreen render
1546 * overlaid on top of a existing setup) then return to the previous space */
1549 /* specific checks for space types */
1551 int sima_restore = 0;
1553 /* Special check added for non-render image window (back from fullscreen through "Back to Previous" button) */
1554 if (sl->spacetype == SPACE_IMAGE) {
1555 SpaceImage *sima= sa->spacedata.first;
1556 if (!(sima->flag & SI_PREVSPACE) && !(sima->flag & SI_FULLWINDOW))
1560 if (sl->spacetype == SPACE_IMAGE && !sima_restore) {
1561 SpaceImage *sima= sa->spacedata.first;
1562 if (sima->flag & SI_PREVSPACE)
1563 sima->flag &= ~SI_PREVSPACE;
1564 if (sima->flag & SI_FULLWINDOW) {
1565 sima->flag &= ~SI_FULLWINDOW;
1566 ED_screen_full_prevspace(C, sa);
1568 } else if (sl->spacetype == SPACE_FILE) {
1569 ED_screen_full_prevspace(C, sa);
1571 ED_screen_full_toggle(C, win, sa);
1573 /* otherwise just tile the area again */
1575 ED_screen_full_toggle(C, win, sa);
1579 /* this function toggles: if area is full then the parent will be restored */
1580 ScrArea *ED_screen_full_toggle(bContext *C, wmWindow *win, ScrArea *sa)
1582 bScreen *sc, *oldscreen;
1586 /* ensure we don't have a button active anymore, can crash when
1587 switching screens with tooltip open because region and tooltip
1588 are no longer in the same screen */
1589 for(ar=sa->regionbase.first; ar; ar=ar->next)
1590 uiFreeBlocks(C, &ar->uiblocks);
1592 /* prevent hanging header prints */
1593 ED_area_headerprint(sa, NULL);
1596 if(sa && sa->full) {
1600 sc= sa->full; /* the old screen to restore */
1601 oldscreen= win->screen; /* the one disappearing */
1603 fulltype = sc->full;
1606 /* removed: SCREENAUTOPLAY exception here */
1609 for(old= sc->areabase.first; old; old= old->next)
1610 if(old->full) break;
1613 printf("something wrong in areafullscreen\n");
1617 area_copy_data(old, sa, 1); /* 1 = swap spacelist */
1618 if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO;
1621 /* animtimer back */
1622 sc->animtimer= oldscreen->animtimer;
1623 oldscreen->animtimer= NULL;
1625 ED_screen_set(C, sc);
1627 free_screen(oldscreen);
1628 free_libblock(&CTX_data_main(C)->screen, oldscreen);
1633 char newname[MAX_ID_NAME-2];
1635 oldscreen= win->screen;
1637 /* nothing wrong with having only 1 area, as far as I can see...
1638 // is there only 1 area?
1639 if(oldscreen->areabase.first==oldscreen->areabase.last)
1643 oldscreen->full = SCREENFULL;
1644 BLI_snprintf(newname, sizeof(newname), "%s-%s", oldscreen->id.name+2, "full");
1645 sc= ED_screen_add(win, oldscreen->scene, newname);
1646 sc->full = SCREENFULL; // XXX
1649 sc->animtimer= oldscreen->animtimer;
1650 oldscreen->animtimer= NULL;
1652 /* returns the top small area */
1653 newa= area_split(sc, (ScrArea *)sc->areabase.first, 'h', 0.99f, 1);
1654 ED_area_newspace(C, newa, SPACE_INFO);
1656 /* use random area when we have no active one, e.g. when the
1657 mouse is outside of the window and we open a file browser */
1659 sa= oldscreen->areabase.first;
1663 area_copy_data(newa, sa, 1); /* 1 = swap spacelist */
1664 sa->flag |= AREA_TEMP_INFO;
1666 sa->full= oldscreen;
1667 newa->full= oldscreen;
1668 newa->next->full= oldscreen; // XXX
1670 ED_screen_set(C, sc);
1673 /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
1674 CTX_wm_area_set(C, sc->areabase.first);
1676 /* XXX retopo_force_update(); */
1678 return sc->areabase.first;
1681 /* update frame rate info for viewport drawing */
1682 void ED_refresh_viewport_fps(bContext *C)
1684 wmTimer *animtimer= CTX_wm_screen(C)->animtimer;
1685 Scene *scene= CTX_data_scene(C);
1687 /* is anim playback running? */
1688 if (animtimer && (U.uiflag & USER_SHOW_FPS)) {
1689 ScreenFrameRateInfo *fpsi= scene->fps_info;
1691 /* if there isn't any info, init it first */
1693 fpsi= scene->fps_info= MEM_callocN(sizeof(ScreenFrameRateInfo), "refresh_viewport_fps fps_info");
1695 /* update the values */
1696 fpsi->redrawtime= fpsi->lredrawtime;
1697 fpsi->lredrawtime= animtimer->ltime;
1700 /* playback stopped or shouldn't be running */
1701 if (scene->fps_info)
1702 MEM_freeN(scene->fps_info);
1703 scene->fps_info= NULL;
1707 /* redraws: uses defines from stime->redraws
1708 * enable: 1 - forward on, -1 - backwards on, 0 - off
1710 void ED_screen_animation_timer(bContext *C, int redraws, int refresh, int sync, int enable)
1712 bScreen *screen= CTX_wm_screen(C);
1713 wmWindowManager *wm= CTX_wm_manager(C);
1714 wmWindow *win= CTX_wm_window(C);
1715 Scene *scene= CTX_data_scene(C);
1717 if(screen->animtimer)
1718 WM_event_remove_timer(wm, win, screen->animtimer);
1719 screen->animtimer= NULL;
1722 ScreenAnimData *sad= MEM_callocN(sizeof(ScreenAnimData), "ScreenAnimData");
1724 screen->animtimer= WM_event_add_timer(wm, win, TIMER0, (1.0/FPS));
1726 sad->ar= CTX_wm_region(C);
1727 sad->sfra = scene->r.cfra;
1728 sad->redraws= redraws;
1729 sad->refresh= refresh;
1730 sad->flag |= (enable < 0)? ANIMPLAY_FLAG_REVERSE: 0;
1731 sad->flag |= (sync == 0)? ANIMPLAY_FLAG_NO_SYNC: (sync == 1)? ANIMPLAY_FLAG_SYNC: 0;
1733 screen->animtimer->customdata= sad;
1736 /* notifier catched by top header, for button */
1737 WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
1740 /* helper for screen_animation_play() - only to be used for TimeLine */
1741 static ARegion *time_top_left_3dwindow(bScreen *screen)
1743 ARegion *aret= NULL;
1747 for(sa= screen->areabase.first; sa; sa= sa->next) {
1748 if(sa->spacetype==SPACE_VIEW3D) {
1750 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1751 if(ar->regiontype==RGN_TYPE_WINDOW) {
1752 if(ar->winrct.xmin - ar->winrct.ymin < min) {
1754 min= ar->winrct.xmin - ar->winrct.ymin;
1764 void ED_screen_animation_timer_update(bScreen *screen, int redraws, int refresh)
1766 if(screen && screen->animtimer) {
1767 wmTimer *wt= screen->animtimer;
1768 ScreenAnimData *sad= wt->customdata;
1770 sad->redraws= redraws;
1771 sad->refresh= refresh;
1773 if(redraws & TIME_REGION)
1774 sad->ar= time_top_left_3dwindow(screen);
1778 /* results in fully updated anim system
1779 * screen can be NULL */
1780 void ED_update_for_newframe(Main *bmain, Scene *scene, bScreen *screen, int UNUSED(mute))
1782 #ifdef DURIAN_CAMERA_SWITCH
1783 void *camera= scene_camera_switch_find(scene);
1784 if(camera && scene->camera != camera) {
1786 scene->camera= camera;
1787 /* are there cameras in the views that are not in the scene? */
1788 for(sc= bmain->screen.first; sc; sc= sc->id.next) {
1789 BKE_screen_view3d_scene_sync(sc);
1794 //extern void audiostream_scrub(unsigned int frame); /* seqaudio.c */
1796 /* update animated image textures for gpu, etc,
1797 * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
1798 ED_image_update_frame(bmain, scene->r.cfra);
1800 /* this function applies the changes too */
1801 /* XXX future: do all windows */
1802 scene_update_for_newframe(bmain, scene, BKE_screen_visible_layers(screen, scene)); /* BKE_scene.h */
1804 //if ( (CFRA>1) && (!mute) && (scene->r.audio.flag & AUDIO_SCRUB))
1805 // audiostream_scrub( CFRA );
1807 /* 3d window, preview */
1808 //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
1810 /* all movie/sequence images */
1811 //BIF_image_update_frame();
1814 if(scene->use_nodes && scene->nodetree)
1815 ntreeCompositTagAnimated(scene->nodetree);
1817 /* update animated texture nodes */
1820 for(tex= bmain->tex.first; tex; tex= tex->id.next)
1821 if( tex->use_nodes && tex->nodetree ) {
1822 ntreeTexTagAnimated( tex->nodetree );