2.5 audio cleanup:
[blender.git] / source / blender / editors / screen / screen_edit.c
index de01d0eb5be2225dbc7a6d2207848e5e8ea22d82..5519b2609a1494dda08edc354fee7278d5fcafa9 100644 (file)
  * ***** END GPL LICENSE BLOCK *****
  */
 
+#include <string.h>
+
 #include "MEM_guardedalloc.h"
 
+#include "DNA_vec_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_texture_types.h"
+#include "DNA_userdef_types.h"
+
 #include "BLI_blenlib.h"
 
+#include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
+#include "BKE_node.h"
 #include "BKE_screen.h"
+#include "BKE_scene.h"
 #include "BKE_utildefines.h"
 
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
-#include "BIF_resources.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
-#include "ED_area.h"
 #include "ED_screen.h"
+#include "ED_screen_types.h"
 
-
+/* XXX actually should be not here... solve later */
 #include "wm_subwindow.h"
 
 #include "screen_intern.h"     /* own module include */
 
+
 /* ******************* screen vert, edge, area managing *********************** */
 
 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
@@ -85,7 +96,7 @@ static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
 }
 
 
-static ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
+ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
 {
        ScrEdge *se;
        
@@ -97,51 +108,7 @@ static ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
        return NULL;
 }
 
-static ScrArea *screen_test_edge_area(bScreen* scr, ScrArea *sa, ScrEdge *se)
-{
-       /* test if edge is in area, if not, 
-          then find an area that has it */
-  
-       ScrEdge *se1=0, *se2=0, *se3=0, *se4=0;
-       
-       if(sa) {
-       se1= screen_findedge(scr, sa->v1, sa->v2);
-               se2= screen_findedge(scr, sa->v2, sa->v3);
-       se3= screen_findedge(scr, sa->v3, sa->v4);
-               se4= screen_findedge(scr, sa->v4, sa->v1);
-       }
-       if(se1!=se && se2!=se && se3!=se && se4!=se) {
-               
-               sa= scr->areabase.first;
-               while(sa) {
-                               /* a bit optimise? */
-                               if(se->v1==sa->v1 || se->v1==sa->v2 || se->v1==sa->v3 || se->v1==sa->v4) {
-                               se1= screen_findedge(scr, sa->v1, sa->v2);
-                                       se2= screen_findedge(scr, sa->v2, sa->v3);
-                                       se3= screen_findedge(scr, sa->v3, sa->v4);
-                                       se4= screen_findedge(scr, sa->v4, sa->v1);
-                                       if(se1==se || se2==se || se3==se || se4==se) return sa;
-                               }
-                               sa= sa->next;
-                       }
-       }
-
-       return sa;      /* is null when not find */
-}
-
-static ScrArea *screen_areahascursor(bScreen *scr, int x, int y)
-{
-       ScrArea *sa= NULL;
-       sa= scr->areabase.first;
-       while(sa) {
-               if(BLI_in_rcti(&sa->totrct, x, y)) break;
-               sa= sa->next;
-       }
-
-       return sa;
-}
-
-static void removedouble_scrverts(bScreen *sc)
+void removedouble_scrverts(bScreen *sc)
 {
        ScrVert *v1, *verg;
        ScrEdge *se;
@@ -195,7 +162,7 @@ static void removedouble_scrverts(bScreen *sc)
 
 }
 
-static void removenotused_scrverts(bScreen *sc)
+void removenotused_scrverts(bScreen *sc)
 {
        ScrVert *sv, *svn;
        ScrEdge *se;
@@ -221,7 +188,7 @@ static void removenotused_scrverts(bScreen *sc)
        }
 }
 
-static void removedouble_scredges(bScreen *sc)
+void removedouble_scredges(bScreen *sc)
 {
        ScrEdge *verg, *se, *sn;
        
@@ -241,7 +208,7 @@ static void removedouble_scredges(bScreen *sc)
        }
 }
 
-static void removenotused_scredges(bScreen *sc)
+void removenotused_scredges(bScreen *sc)
 {
        ScrEdge *se, *sen;
        ScrArea *sa;
@@ -277,12 +244,12 @@ static void removenotused_scredges(bScreen *sc)
        }
 }
 
-static int scredge_is_horizontal(ScrEdge *se)
+int scredge_is_horizontal(ScrEdge *se)
 {
        return (se->v1->vec.y == se->v2->vec.y);
 }
 
-static ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
+ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
 {
        ScrEdge *se;
        
@@ -308,49 +275,9 @@ static ScrEdge *screen_find_active_scredge(bScreen *sc, int mx, int my)
        return NULL;
 }
 
-/* danger: is used while areamove! */
-static void select_connected_scredge(bScreen *sc, ScrEdge *edge)
-{
-       ScrEdge *se;
-       ScrVert *sv;
-       int oneselected;
-       char dir;
-       
-       /* select connected, only in the right direction */
-       /* 'dir' is the direction of EDGE */
-       
-       if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
-       else dir= 'h';
-       
-       sv= sc->vertbase.first;
-       while(sv) {
-               sv->flag = 0;
-               sv= sv->next;
-       }
-       
-       edge->v1->flag= 1;
-       edge->v2->flag= 1;
-       
-       oneselected= 1;
-       while(oneselected) {
-               se= sc->edgebase.first;
-               oneselected= 0;
-               while(se) {
-                       if(se->v1->flag + se->v2->flag==1) {
-                               if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
-                                       se->v1->flag= se->v2->flag= 1;
-                                       oneselected= 1;
-                               }
-                               if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
-                                       se->v1->flag= se->v2->flag= 1;
-                                       oneselected= 1;
-                               }
-                       }
-                       se= se->next;
-               }
-       }
-}
 
+
+/* adds no space data */
 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
 {
        ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
@@ -359,28 +286,134 @@ static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v
        sa->v3= v3;
        sa->v4= v4;
        sa->headertype= headertype;
-       sa->spacetype= spacetype;
+       sa->spacetype= sa->butspacetype= spacetype;
        
        BLI_addtail(&sc->areabase, sa);
        
        return sa;
 }
 
-static void screen_delarea(bScreen *sc, ScrArea *sa)
-{      
+static void screen_delarea(bContext *C, bScreen *sc, ScrArea *sa)
+{
+       
+       ED_area_exit(C, sa);
+       
        BKE_screen_area_free(sa);
+       
        BLI_remlink(&sc->areabase, sa);
        MEM_freeN(sa);
 }
 
-bScreen *addscreen(wmWindow *win, char *name)
+/* return 0: no split possible */
+/* else return (integer) screencoordinate split point */
+static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
+{
+       short x, y;
+       
+       // area big enough?
+       if(dir=='v' && (sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX)) return 0;
+       if(dir=='h' && (sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY)) return 0;
+       
+       // to be sure
+       if(fac<0.0) fac= 0.0;
+       if(fac>1.0) fac= 1.0;
+       
+       if(dir=='h') {
+               y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
+               
+               if(y- sa->v1->vec.y < AREAMINY) 
+                       y= sa->v1->vec.y+ AREAMINY;
+               else if(sa->v2->vec.y- y < AREAMINY) 
+                       y= sa->v2->vec.y- AREAMINY;
+               else y-= (y % AREAGRID);
+               
+               return y;
+       }
+       else {
+               x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
+               
+               if(x- sa->v1->vec.x < AREAMINX) 
+                       x= sa->v1->vec.x+ AREAMINX;
+               else if(sa->v4->vec.x- x < AREAMINX) 
+                       x= sa->v4->vec.x- AREAMINX;
+               else x-= (x % AREAGRID);
+               
+               return x;
+       }
+}
+
+ScrArea *area_split(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
+{
+       ScrArea *newa=NULL;
+       ScrVert *sv1, *sv2;
+       short split;
+       
+       if(sa==NULL) return NULL;
+       
+       split= testsplitpoint(win, sa, dir, fac);
+       if(split==0) return NULL;
+       
+       if(dir=='h') {
+               /* new vertices */
+               sv1= screen_addvert(sc, sa->v1->vec.x, split);
+               sv2= screen_addvert(sc, sa->v4->vec.x, split);
+               
+               /* new edges */
+               screen_addedge(sc, sa->v1, sv1);
+               screen_addedge(sc, sv1, sa->v2);
+               screen_addedge(sc, sa->v3, sv2);
+               screen_addedge(sc, sv2, sa->v4);
+               screen_addedge(sc, sv1, sv2);
+               
+               /* new areas: top */
+               newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
+               area_copy_data(newa, sa, 0);
+               
+               /* area below */
+               sa->v2= sv1;
+               sa->v3= sv2;
+               
+       }
+       else {
+               /* new vertices */
+               sv1= screen_addvert(sc, split, sa->v1->vec.y);
+               sv2= screen_addvert(sc, split, sa->v2->vec.y);
+               
+               /* new edges */
+               screen_addedge(sc, sa->v1, sv1);
+               screen_addedge(sc, sv1, sa->v4);
+               screen_addedge(sc, sa->v2, sv2);
+               screen_addedge(sc, sv2, sa->v3);
+               screen_addedge(sc, sv1, sv2);
+               
+               /* new areas: left */
+               newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
+               area_copy_data(newa, sa, 0);
+               
+               /* area right */
+               sa->v1= sv1;
+               sa->v2= sv2;
+       }
+       
+       /* remove double vertices en edges */
+       removedouble_scrverts(sc);
+       removedouble_scredges(sc);
+       removenotused_scredges(sc);
+       
+       return newa;
+}
+
+/* empty screen, with 1 dummy area without spacedata */
+/* uses window size */
+bScreen *ED_screen_add(wmWindow *win, Scene *scene, char *name)
 {
        bScreen *sc;
        ScrVert *sv1, *sv2, *sv3, *sv4;
        
        sc= alloc_libblock(&G.main->screen, ID_SCR, name);
-       
-       sc->scene= G.scene;
+       sc->scene= scene;
+       sc->do_refresh= 1;
+       sc->winid= win->winid;
        
        sv1= screen_addvert(sc, 0, 0);
        sv2= screen_addvert(sc, 0, win->sizey-1);
@@ -392,12 +425,12 @@ bScreen *addscreen(wmWindow *win, char *name)
        screen_addedge(sc, sv3, sv4);
        screen_addedge(sc, sv4, sv1);
        
-       screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_INFO);
+       /* dummy type, no spacedata */
+       screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_EMPTY);
                
        return sc;
 }
 
-
 static void screen_copy(bScreen *to, bScreen *from)
 {
        ScrVert *s1, *s2;
@@ -410,6 +443,7 @@ static void screen_copy(bScreen *to, bScreen *from)
        BLI_duplicatelist(&to->vertbase, &from->vertbase);
        BLI_duplicatelist(&to->edgebase, &from->edgebase);
        BLI_duplicatelist(&to->areabase, &from->areabase);
+       to->regionbase.first= to->regionbase.last= NULL;
        
        s2= to->vertbase.first;
        for(s1= from->vertbase.first; s1; s1= s1->next, s2= s2->next) {
@@ -430,10 +464,8 @@ static void screen_copy(bScreen *to, bScreen *from)
                sa->v4= sa->v4->newv;
                
                sa->spacedata.first= sa->spacedata.last= NULL;
-               sa->uiblocks.first= sa->uiblocks.last= NULL;
-               sa->panels.first= sa->panels.last= NULL;
                sa->regionbase.first= sa->regionbase.last= NULL;
-               sa->scriptlink.totscript= 0;
+               sa->actionzones.first= sa->actionzones.last= NULL;
                
                area_copy_data(sa, saf, 0);
        }
@@ -444,53 +476,11 @@ static void screen_copy(bScreen *to, bScreen *from)
 
 }
 
-bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
-{
-       bScreen *newsc;
-       
-       if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
-       
-       /* make new screen: */
-       newsc= addscreen(win, sc->id.name+2);
-       /* copy all data */
-       screen_copy(newsc, sc);
-       
-       return newsc;
-}
-
-/* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
-/* used with split operator */
-static ScrEdge *area_findsharededge(bScreen *screen, ScrArea *sa, ScrArea *sb)
-{
-       ScrVert *sav1= sa->v1;
-       ScrVert *sav2= sa->v2;
-       ScrVert *sav3= sa->v3;
-       ScrVert *sav4= sa->v4;
-       ScrVert *sbv1= sb->v1;
-       ScrVert *sbv2= sb->v2;
-       ScrVert *sbv3= sb->v3;
-       ScrVert *sbv4= sb->v4;
-       
-       if(sav1==sbv4 && sav2==sbv3) { /* sa to right of sb = W */
-               return screen_findedge(screen, sav1, sav2);
-       }
-       else if(sav2==sbv1 && sav3==sbv4) { /* sa to bottom of sb = N */
-               return screen_findedge(screen, sav2, sav3);
-       }
-       else if(sav3==sbv2 && sav4==sbv1) { /* sa to left of sb = E */
-               return screen_findedge(screen, sav3, sav4);
-       }
-       else if(sav1==sbv2 && sav4==sbv3) { /* sa on top of sb = S*/
-               return screen_findedge(screen, sav1, sav4);
-       }
-       
-       return NULL;
-}
 
 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
 /* -1 = not valid check */
-/* used with split operator */
-static int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
+/* used with join operator */
+int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
 {
        ScrVert *sav1, *sav2, *sav3, *sav4;
        ScrVert *sbv1, *sbv2, *sbv3, *sbv4;
@@ -522,118 +512,102 @@ static int area_getorientation(bScreen *screen, ScrArea *sa, ScrArea *sb)
        return -1;
 }
 
-/* return 0: no split possible */
-/* else return (integer) screencoordinate split point */
-static short testsplitpoint(wmWindow *win, ScrArea *sa, char dir, float fac)
+/* Helper function to join 2 areas, it has a return value, 0=failed 1=success
+*      used by the split, join operators
+*/
+int screen_area_join(bContext *C, bScreen* scr, ScrArea *sa1, ScrArea *sa2) 
 {
-       short x, y;
+       int dir;
        
-       // area big enough?
-       if(sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX) return 0;
-       if(sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY) return 0;
-
-       // to be sure
-       if(fac<0.0) fac= 0.0;
-       if(fac>1.0) fac= 1.0;
+       dir = area_getorientation(scr, sa1, sa2);
+       /*printf("dir is : %i \n", dir);*/
        
-       if(dir=='h') {
-               y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
-               
-               if(sa->v2->vec.y==win->sizey-1 && sa->v2->vec.y- y < HEADERY) 
-                       y= sa->v2->vec.y- HEADERY;
-
-               else if(sa->v1->vec.y==0 && y- sa->v1->vec.y < HEADERY)
-                       y= sa->v1->vec.y+ HEADERY;
-
-               else if(y- sa->v1->vec.y < AREAMINY) y= sa->v1->vec.y+ AREAMINY;
-               else if(sa->v2->vec.y- y < AREAMINY) y= sa->v2->vec.y- AREAMINY;
-               else y-= (y % AREAGRID);
-
-               return y;
+       if (dir < 0)
+       {
+               if (sa1 ) sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
+               if (sa2 ) sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
+               return 0;
        }
-       else {
-               x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
-               if(x- sa->v1->vec.x < AREAMINX) x= sa->v1->vec.x+ AREAMINX;
-               else if(sa->v4->vec.x- x < AREAMINX) x= sa->v4->vec.x- AREAMINX;
-               else x-= (x % AREAGRID);
-
-               return x;
+       
+       if(dir == 0) {
+               sa1->v1= sa2->v1;
+               sa1->v2= sa2->v2;
+               screen_addedge(scr, sa1->v2, sa1->v3);
+               screen_addedge(scr, sa1->v1, sa1->v4);
        }
+       else if(dir == 1) {
+               sa1->v2= sa2->v2;
+               sa1->v3= sa2->v3;
+               screen_addedge(scr, sa1->v1, sa1->v2);
+               screen_addedge(scr, sa1->v3, sa1->v4);
+       }
+       else if(dir == 2) {
+               sa1->v3= sa2->v3;
+               sa1->v4= sa2->v4;
+               screen_addedge(scr, sa1->v2, sa1->v3);
+               screen_addedge(scr, sa1->v1, sa1->v4);
+       }
+       else if(dir == 3) {
+               sa1->v1= sa2->v1;
+               sa1->v4= sa2->v4;
+               screen_addedge(scr, sa1->v1, sa1->v2);
+               screen_addedge(scr, sa1->v3, sa1->v4);
+       }
+       
+       screen_delarea(C, scr, sa2);
+       removedouble_scrverts(scr);
+       sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
+       
+       return 1;
 }
 
-static ScrArea* splitarea(wmWindow *win, bScreen *sc, ScrArea *sa, char dir, float fac)
+void select_connected_scredge(bScreen *sc, ScrEdge *edge)
 {
-       ScrArea *newa=NULL;
-       ScrVert *sv1, *sv2;
-       short split;
+       ScrEdge *se;
+       ScrVert *sv;
+       int oneselected;
+       char dir;
        
-       if(sa==0) return NULL;
+       /* select connected, only in the right direction */
+       /* 'dir' is the direction of EDGE */
        
-       split= testsplitpoint(win, sa, dir, fac);
-       if(split==0) return NULL;
+       if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
+       else dir= 'h';
        
-       //sc= G.curscreen;
+       sv= sc->vertbase.first;
+       while(sv) {
+               sv->flag = 0;
+               sv= sv->next;
+       }
        
-       //areawinset(sa->win);
+       edge->v1->flag= 1;
+       edge->v2->flag= 1;
        
-       if(dir=='h') {
-               /* new vertices */
-               sv1= screen_addvert(sc, sa->v1->vec.x, split);
-               sv2= screen_addvert(sc, sa->v4->vec.x, split);
-               
-               /* new edges */
-               screen_addedge(sc, sa->v1, sv1);
-               screen_addedge(sc, sv1, sa->v2);
-               screen_addedge(sc, sa->v3, sv2);
-               screen_addedge(sc, sv2, sa->v4);
-               screen_addedge(sc, sv1, sv2);
-               
-               /* new areas: top */
-               newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
-               area_copy_data(newa, sa, 0);
-
-               /* area below */
-               sa->v2= sv1;
-               sa->v3= sv2;
-               
-       }
-       else {
-               /* new vertices */
-               sv1= screen_addvert(sc, split, sa->v1->vec.y);
-               sv2= screen_addvert(sc, split, sa->v2->vec.y);
-               
-               /* new edges */
-               screen_addedge(sc, sa->v1, sv1);
-               screen_addedge(sc, sv1, sa->v4);
-               screen_addedge(sc, sa->v2, sv2);
-               screen_addedge(sc, sv2, sa->v3);
-               screen_addedge(sc, sv1, sv2);
-               
-               /* new areas: left */
-               newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
-               area_copy_data(newa, sa, 0);
-
-               /* area right */
-               sa->v1= sv1;
-               sa->v2= sv2;
+       oneselected= 1;
+       while(oneselected) {
+               se= sc->edgebase.first;
+               oneselected= 0;
+               while(se) {
+                       if(se->v1->flag + se->v2->flag==1) {
+                               if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
+                                       se->v1->flag= se->v2->flag= 1;
+                                       oneselected= 1;
+                               }
+                                       if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
+                                               se->v1->flag= se->v2->flag= 1;
+                                               oneselected= 1;
+                                       }
+                       }
+                               se= se->next;
+               }
        }
-       
-       /* remove double vertices en edges */
-       removedouble_scrverts(sc);
-       removedouble_scredges(sc);
-       removenotused_scredges(sc);
-       
-       return newa;
 }
 
-
-/* *************************************************************** */
-
 /* test if screen vertices should be scaled */
-void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
+static void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
 {
        ScrVert *sv=NULL;
-       ScrArea *sa, *san;
+       ScrArea *sa;
        int sizex, sizey;
        float facx, facy, tempf, min[2], max[2];
        
@@ -682,69 +656,371 @@ void screen_test_scale(bScreen *sc, int winsizex, int winsizey)
        }
        
        /* test for collapsed areas. This could happen in some blender version... */
-       for(sa= sc->areabase.first; sa; sa= san) {
-               san= sa->next;
-               if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3) {
-                       BKE_screen_area_free(sa);
-                       BLI_remlink(&sc->areabase, sa);
-                       MEM_freeN(sa);
+       /* ton: removed option now, it needs Context... */
+       
+       /* make each window at least HEADERY high */
+       for(sa= sc->areabase.first; sa; sa= sa->next) {
+               int headery= HEADERY+1;
+               
+               if(sa->v1->vec.y+headery > sa->v2->vec.y) {
+                       /* lower edge */
+                       ScrEdge *se= screen_findedge(sc, sa->v4, sa->v1);
+                       if(se && sa->v1!=sa->v2 ) {
+                               int yval;
+                               
+                               select_connected_scredge(sc, se);
+                               
+                               /* all selected vertices get the right offset */
+                               yval= sa->v2->vec.y-headery;
+                               sv= sc->vertbase.first;
+                               while(sv) {
+                                       /* if is a collapsed area */
+                                       if(sv!=sa->v2 && sv!=sa->v3) {
+                                               if(sv->flag) sv->vec.y= yval;
+                                       }
+                                       sv= sv->next;
+                               }
+                       }
                }
        }
+       
 }
 
+/* *********************** DRAWING **************************************** */
 
 
 #define SCR_BACK 0.55
 #define SCR_ROUND 12
 
-static void drawscredge_area(ScrArea *sa)
+/* draw vertical shape visualising future joining (left as well
+ * right direction of future joining) */
+static void draw_horizontal_join_shape(ScrArea *sa, char dir)
+{
+       vec2f points[10];
+       short i;
+       float w, h;
+       float width = sa->v3->vec.x - sa->v1->vec.x;
+       float height = sa->v3->vec.y - sa->v1->vec.y;
+
+       if(height<width) {
+               h = height/8;
+               w = height/4;
+       }
+       else {
+               h = width/8;
+               w = width/4;
+       }
+
+       points[0].x = sa->v1->vec.x;
+       points[0].y = sa->v1->vec.y + height/2;
+
+       points[1].x = sa->v1->vec.x;
+       points[1].y = sa->v1->vec.y;
+
+       points[2].x = sa->v4->vec.x - w;
+       points[2].y = sa->v4->vec.y;
+
+       points[3].x = sa->v4->vec.x - w;
+       points[3].y = sa->v4->vec.y + height/2 - 2*h;
+
+       points[4].x = sa->v4->vec.x - 2*w;
+       points[4].y = sa->v4->vec.y + height/2;
+
+       points[5].x = sa->v4->vec.x - w;
+       points[5].y = sa->v4->vec.y + height/2 + 2*h;
+
+       points[6].x = sa->v3->vec.x - w;
+       points[6].y = sa->v3->vec.y;
+
+       points[7].x = sa->v2->vec.x;
+       points[7].y = sa->v2->vec.y;
+
+       points[8].x = sa->v4->vec.x;
+       points[8].y = sa->v4->vec.y + height/2 - h;
+
+       points[9].x = sa->v4->vec.x;
+       points[9].y = sa->v4->vec.y + height/2 + h;
+
+       if(dir=='l') {
+               /* when direction is left, then we flip direction of arrow */
+               float cx = sa->v1->vec.x + width;
+               for(i=0;i<10;i++) {
+                       points[i].x -= cx;
+                       points[i].x = -points[i].x;
+                       points[i].x += sa->v1->vec.x;
+               }
+       }
+
+       glBegin(GL_POLYGON);
+       for(i=0;i<5;i++)
+               glVertex2f(points[i].x, points[i].y);
+       glEnd();
+       glBegin(GL_POLYGON);
+       for(i=4;i<8;i++)
+               glVertex2f(points[i].x, points[i].y);
+       glVertex2f(points[0].x, points[0].y);
+       glEnd();
+
+       glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
+       glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
+}
+
+/* draw vertical shape visualising future joining (up/down direction) */
+static void draw_vertical_join_shape(ScrArea *sa, char dir)
+{
+       vec2f points[10];
+       short i;
+       float w, h;
+       float width = sa->v3->vec.x - sa->v1->vec.x;
+       float height = sa->v3->vec.y - sa->v1->vec.y;
+
+       if(height<width) {
+               h = height/4;
+               w = height/8;
+       }
+       else {
+               h = width/4;
+               w = width/8;
+       }
+
+       points[0].x = sa->v1->vec.x + width/2;
+       points[0].y = sa->v3->vec.y;
+
+       points[1].x = sa->v2->vec.x;
+       points[1].y = sa->v2->vec.y;
+
+       points[2].x = sa->v1->vec.x;
+       points[2].y = sa->v1->vec.y + h;
+
+       points[3].x = sa->v1->vec.x + width/2 - 2*w;
+       points[3].y = sa->v1->vec.y + h;
+
+       points[4].x = sa->v1->vec.x + width/2;
+       points[4].y = sa->v1->vec.y + 2*h;
+
+       points[5].x = sa->v1->vec.x + width/2 + 2*w;
+       points[5].y = sa->v1->vec.y + h;
+
+       points[6].x = sa->v4->vec.x;
+       points[6].y = sa->v4->vec.y + h;
+       
+       points[7].x = sa->v3->vec.x;
+       points[7].y = sa->v3->vec.y;
+
+       points[8].x = sa->v1->vec.x + width/2 - w;
+       points[8].y = sa->v1->vec.y;
+
+       points[9].x = sa->v1->vec.x + width/2 + w;
+       points[9].y = sa->v1->vec.y;
+
+       if(dir=='u') {
+               /* when direction is up, then we flip direction of arrow */
+               float cy = sa->v1->vec.y + height;
+               for(i=0;i<10;i++) {
+                       points[i].y -= cy;
+                       points[i].y = -points[i].y;
+                       points[i].y += sa->v1->vec.y;
+               }
+       }
+
+       glBegin(GL_POLYGON);
+       for(i=0;i<5;i++)
+               glVertex2f(points[i].x, points[i].y);
+       glEnd();
+       glBegin(GL_POLYGON);
+       for(i=4;i<8;i++)
+               glVertex2f(points[i].x, points[i].y);
+       glVertex2f(points[0].x, points[0].y);
+       glEnd();
+
+       glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
+       glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
+}
+
+/* draw join shape due to direction of joining */
+static void draw_join_shape(ScrArea *sa, char dir)
+{
+       if(dir=='u' || dir=='d')
+               draw_vertical_join_shape(sa, dir);
+       else
+               draw_horizontal_join_shape(sa, dir);
+}
+
+/* draw screen area darker with arrow (visualisation of future joining) */
+static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
+{
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glEnable(GL_BLEND);
+       glColor4ub(0, 0, 0, 50);
+       draw_join_shape(sa, dir);
+       glDisable(GL_BLEND);
+}
+
+/* draw screen area ligher with arrow shape ("eraser" of previous dark shape) */
+static void scrarea_draw_shape_light(ScrArea *sa, char dir)
+{
+       glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
+       glEnable(GL_BLEND);
+       /* value 181 was hardly computed: 181~105 */
+       glColor4ub(255, 255, 255, 50);          
+       /* draw_join_shape(sa, dir); */
+       glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
+       glDisable(GL_BLEND);
+}
+
+static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, short a) 
+{
+       /* right border area */
+       if(x2<sizex-1)
+               sdrawline(x2+a, y1, x2+a, y2);
+       
+       /* left border area */
+       if(x1>0)  /* otherwise it draws the emboss of window over */
+               sdrawline(x1+a, y1, x1+a, y2);
+       
+       /* top border area */
+       if(y2<sizey-1)
+               sdrawline(x1, y2+a, x2, y2+a);
+       
+       /* bottom border area */
+       if(y1>0)
+               sdrawline(x1, y1+a, x2, y1+a);
+       
+}
+
+/** screen edges drawing **/
+static void drawscredge_area(ScrArea *sa, int sizex, int sizey, int center)
 {
        short x1= sa->v1->vec.x;
        short y1= sa->v1->vec.y;
        short x2= sa->v3->vec.x;
        short y2= sa->v3->vec.y;
+       short a, rt;
        
-       cpack(0x0);
+       rt= CLAMPIS(G.rt, 0, 16);
        
-       /* right border area */
-       sdrawline(x2, y1, x2, y2);
+       if(center==0) {
+               cpack(0x505050);
+               for(a=-rt; a<=rt; a++)
+                       if(a!=0)
+                               drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, a);
+       }
+       else {
+               cpack(0x0);
+               drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, 0);
+       }
+}
+
+/* ****************** EXPORTED API TO OTHER MODULES *************************** */
+
+bScreen *ED_screen_duplicate(wmWindow *win, bScreen *sc)
+{
+       bScreen *newsc;
        
-       /* left border area */
-       if(x1>0) { // otherwise it draws the emboss of window over
-               sdrawline(x1, y1, x1, y2);
-       }       
-       /* top border area */
-       sdrawline(x1, y2, x2, y2);
+       if(sc->full != SCREENNORMAL) return NULL; /* XXX handle this case! */
        
-       /* bottom border area */
-       sdrawline(x1, y1, x2, y1);
+       /* make new empty screen: */
+       newsc= ED_screen_add(win, sc->scene, sc->id.name+2);
+       /* copy all data */
+       screen_copy(newsc, sc);
+       /* set in window */
+       win->screen= newsc;
+       
+       /* store identifier */
+       win->screen->winid= win->winid;
+       BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
+
+       return newsc;
 }
 
-void ED_screen_do_listen(bScreen *screen, wmNotifier *note)
+/* screen sets cursor based on swinid */
+static void region_cursor_set(wmWindow *win, int swinid)
+{
+       ScrArea *sa= win->screen->areabase.first;
+       
+       for(;sa; sa= sa->next) {
+               ARegion *ar= sa->regionbase.first;
+               for(;ar; ar= ar->next) {
+                       if(ar->swinid == swinid) {
+                               if(ar->type && ar->type->cursor)
+                                       ar->type->cursor(win, sa, ar);
+                               else
+                                       WM_cursor_set(win, CURSOR_STD);
+                               return;
+                       }
+               }
+       }
+}
+
+void ED_screen_do_listen(wmWindow *win, wmNotifier *note)
 {
        
        /* generic notes */
-       switch(note->type) {
-               case WM_NOTE_WINDOW_REDRAW:
-                       screen->do_draw= 1;
+       switch(note->category) {
+               case NC_WM:
+                       if(note->data==ND_FILEREAD)
+                               win->screen->do_draw= 1;
                        break;
-               case WM_NOTE_SCREEN_CHANGED:
-                       screen->do_draw= screen->do_refresh= 1;
+               case NC_WINDOW:
+                       win->screen->do_draw= 1;
+                       break;
+               case NC_SCREEN:
+                       if(note->action==NA_EDITED)
+                               win->screen->do_draw= win->screen->do_refresh= 1;
+               case NC_SCENE:
+                       if(note->data==ND_MODE)
+                               region_cursor_set(win, note->swinid);                           
                        break;
        }
 }
 
-
+/* only for edge lines between areas, and the blended join arrows */
 void ED_screen_draw(wmWindow *win)
 {
        ScrArea *sa;
+       ScrArea *sa1=NULL;
+       ScrArea *sa2=NULL;
+       int dir = -1;
+       int dira = -1;
+
+       wmSubWindowSet(win, win->screen->mainwin);
        
-       wm_subwindow_set(win, win->screen->mainwin);
-       
+       for(sa= win->screen->areabase.first; sa; sa= sa->next) {
+               if (sa->flag & AREA_FLAG_DRAWJOINFROM) sa1 = sa;
+               if (sa->flag & AREA_FLAG_DRAWJOINTO) sa2 = sa;
+               drawscredge_area(sa, win->sizex, win->sizey, 0);
+       }
        for(sa= win->screen->areabase.first; sa; sa= sa->next)
-               drawscredge_area(sa);
-
-       printf("draw screen\n");
+               drawscredge_area(sa, win->sizex, win->sizey, 1);
+       
+       /* blended join arrow */
+       if (sa1 && sa2) {
+               dir = area_getorientation(win->screen, sa1, sa2);
+               if (dir >= 0) {
+                       switch(dir) {
+                               case 0: /* W */
+                                       dir = 'r';
+                                       dira = 'l';
+                                       break;
+                               case 1: /* N */
+                                       dir = 'd';
+                                       dira = 'u';
+                                       break;
+                               case 2: /* E */
+                                       dir = 'l';
+                                       dira = 'r';
+                                       break;
+                               case 3: /* S */
+                                       dir = 'u';
+                                       dira = 'd';
+                                       break;
+                       }
+               }
+               scrarea_draw_shape_dark(sa2, dir);
+               scrarea_draw_shape_light(sa1, dira);
+       }
+       
+//     if(G.f & G_DEBUG) printf("draw screen\n");
        win->screen->do_draw= 0;
 }
 
@@ -753,7 +1029,7 @@ void ED_screen_draw(wmWindow *win)
 void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
 {
        ScrArea *sa;
-       rcti winrct= {0, win->sizex, 0, win->sizey};
+       rcti winrct= {0, win->sizex-1, 0, win->sizey-1};
        
        screen_test_scale(win->screen, win->sizex, win->sizey);
        
@@ -763,14 +1039,19 @@ void ED_screen_refresh(wmWindowManager *wm, wmWindow *win)
                wm_subwindow_position(win, win->screen->mainwin, &winrct);
        
        for(sa= win->screen->areabase.first; sa; sa= sa->next) {
-               /* set spacetype and region callbacks */
-               /* sets subwindow */
+               /* set spacetype and region callbacks, calls init() */
+               /* sets subwindows for regions, adds handlers */
                ED_area_initialize(wm, win, sa);
        }
+
+       /* wake up animtimer */
+       if(win->screen->animtimer)
+               WM_event_window_timer_sleep(win, win->screen->animtimer, 0);
        
-       printf("set screen\n");
+       if(G.f & G_DEBUG) printf("set screen\n");
        win->screen->do_refresh= 0;
 
+       win->screen->context= ed_screen_context;
 }
 
 /* file read, set all screens, ... */
@@ -787,750 +1068,473 @@ void ED_screens_initialize(wmWindowManager *wm)
        }
 }
 
-void placeholder()
-{
-       removenotused_scrverts(NULL);
-       removenotused_scredges(NULL);
-}
 
-/* called in wm_event_system.c. sets state var in screen */
-void ED_screen_set_subwinactive(wmWindow *win)
-{
-       if(win->screen) {
-               wmEvent *event= win->eventstate;
-               ScrArea *sa;
-               
-               for(sa= win->screen->areabase.first; sa; sa= sa->next) {
-                       if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
-                               if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
-                                       break;
-               }
-               if(sa) {
-                       ARegion *ar;
-                       for(ar= sa->regionbase.first; ar; ar= ar->next) {
-                               if(BLI_in_rcti(&ar->winrct, event->x, event->y))
-                                       win->screen->subwinactive= ar->swinid;
-                       }
-               }
-               else
-                       win->screen->subwinactive= win->screen->mainwin;
-               
-       }
-}
+/* *********** exit calls are for closing running stuff ******** */
 
-/* ****************** cursor near edge operator ********************************* */
-
-/* operator cb */
-int screen_cursor_test(bContext *C, wmOperator *op, wmEvent *event)
+void ED_region_exit(bContext *C, ARegion *ar)
 {
-       if (C->screen->subwinactive==C->screen->mainwin) {
-               ScrEdge *actedge= screen_find_active_scredge(C->screen, event->x, event->y);
-               
-               if (actedge && scredge_is_horizontal(actedge)) {
-                       WM_set_cursor(C, CURSOR_Y_MOVE);
-               } else {
-                       WM_set_cursor(C, CURSOR_X_MOVE);
-               }
-       } else {
-               WM_set_cursor(C, CURSOR_STD);
-       }
+       ARegion *prevar= CTX_wm_region(C);
+
+       CTX_wm_region_set(C, ar);
+       WM_event_remove_handlers(C, &ar->handlers);
+       if(ar->swinid)
+               wm_subwindow_close(CTX_wm_window(C), ar->swinid);
+       ar->swinid= 0;
        
-       return 1;
+       if(ar->headerstr)
+               MEM_freeN(ar->headerstr);
+       ar->headerstr= NULL;
+       
+       CTX_wm_region_set(C, prevar);
 }
 
-/* ************** move area edge operator ********************************************** */
+void ED_area_exit(bContext *C, ScrArea *sa)
+{
+       ScrArea *prevsa= CTX_wm_area(C);
+       ARegion *ar;
 
-/* operator state vars used:  
-           op->veci   mouse coord near edge
-           op->delta  movement of edge
+       CTX_wm_area_set(C, sa);
+       for(ar= sa->regionbase.first; ar; ar= ar->next)
+               ED_region_exit(C, ar);
 
-   callbacks:
+       WM_event_remove_handlers(C, &sa->handlers);
+       CTX_wm_area_set(C, prevsa);
+}
 
-   init()   find edge based on op->veci, test if the edge can be moved, select edges,
-            clear delta, calculate min and max movement
+void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen)
+{
+       wmWindow *prevwin= CTX_wm_window(C);
+       ScrArea *sa;
+       ARegion *ar;
 
-   exec()      apply op->delta on selection
-   
-   invoke() handler gets called on a mouse click near edge
-            call init()
-            add handler
+       CTX_wm_window_set(C, window);
+       
+       if(screen->animtimer)
+               WM_event_remove_window_timer(window, screen->animtimer);
+       screen->animtimer= NULL;
+       
+       if(screen->mainwin)
+               wm_subwindow_close(window, screen->mainwin);
+       screen->mainwin= 0;
+       screen->subwinactive= 0;
+       
+       for(ar= screen->regionbase.first; ar; ar= ar->next)
+               ED_region_exit(C, ar);
 
-   modal()     accept modal events while doing it
-                       call exec() with delta motion
-            call exit() and remove handler
+       for(sa= screen->areabase.first; sa; sa= sa->next)
+               ED_area_exit(C, sa);
 
-   exit()      cleanup, send notifier
+       /* mark it available for use for other windows */
+       screen->winid= 0;
+       
+       CTX_wm_window_set(C, prevwin);
+}
 
-*/
+/* *********************************** */
 
-/* validate selection inside screen, set variables OK */
-/* return 0: init failed */
-static int move_areas_init (bContext *C, wmOperator *op)
+/* case when on area-edge or in azones, or outside window */
+static void screen_cursor_set(wmWindow *win, wmEvent *event)
 {
-       ScrEdge *actedge= screen_find_active_scredge(C->screen, op->veci.x, op->veci.y);
+       AZone *az= NULL;
        ScrArea *sa;
-       int bigger, smaller, dir, origval;
-       
-       if(actedge==NULL) return 0;
-       
-       dir= scredge_is_horizontal(actedge)?'h':'v';
-       if(dir=='h') origval= actedge->v1->vec.y;
-       else origval= actedge->v1->vec.x;
-       
-       select_connected_scredge(C->screen, actedge);
-
-       /* now all verices with 'flag==1' are the ones that can be moved. */
-       /* we check all areas and test for free space with MINSIZE */
-       bigger= smaller= 10000;
-       for(sa= C->screen->areabase.first; sa; sa= sa->next) {
-               if(dir=='h') {  /* if top or down edge selected, test height */
-                  
-                  if(sa->v1->flag && sa->v4->flag) {
-                          int y1= sa->v2->vec.y - sa->v1->vec.y-AREAMINY;
-                          bigger= MIN2(bigger, y1);
-                  }
-                  else if(sa->v2->flag && sa->v3->flag) {
-                          int y1= sa->v2->vec.y - sa->v1->vec.y-AREAMINY;
-                          smaller= MIN2(smaller, y1);
-                  }
-               }
-               else {  /* if left or right edge selected, test width */
-                       if(sa->v1->flag && sa->v2->flag) {
-                               int x1= sa->v4->vec.x - sa->v1->vec.x-AREAMINX;
-                               bigger= MIN2(bigger, x1);
-                       }
-                       else if(sa->v3->flag && sa->v4->flag) {
-                               int x1= sa->v4->vec.x - sa->v1->vec.x-AREAMINX;
-                               smaller= MIN2(smaller, x1);
-                       }
+       
+       for(sa= win->screen->areabase.first; sa; sa= sa->next)
+               if((az=is_in_area_actionzone(sa, event->x, event->y)))
+                       break;
+       
+       if(sa) {
+               if(az->type==AZONE_AREA)
+                       WM_cursor_set(win, CURSOR_EDIT);
+               else if(az->type==AZONE_REGION) {
+                       if(az->x1==az->x2)
+                               WM_cursor_set(win, CURSOR_X_MOVE);
+                       else
+                               WM_cursor_set(win, CURSOR_Y_MOVE);
                }
        }
-
-       OP_set_int(op, "bigger", bigger);
-       OP_set_int(op, "smaller", smaller);
-       OP_set_int(op, "dir", dir);
-       OP_set_int(op, "origval", origval);
-
-       return 1;
+       else {
+               ScrEdge *actedge= screen_find_active_scredge(win->screen, event->x, event->y);
+               
+               if (actedge) {
+                       if(scredge_is_horizontal(actedge))
+                               WM_cursor_set(win, CURSOR_Y_MOVE);
+                       else
+                               WM_cursor_set(win, CURSOR_X_MOVE);
+               }
+               else
+                       WM_cursor_set(win, CURSOR_STD);
+       } 
 }
 
-/* moves selected screen edge amount of delta */
-/* needs init call to work */
-static int move_areas_exec(bContext *C, wmOperator *op)
+
+/* called in wm_event_system.c. sets state vars in screen, cursors */
+/* event type is mouse move */
+void ED_screen_set_subwinactive(wmWindow *win, wmEvent *event)
 {
-       ScrVert *v1;
-       int bigger, smaller, dir, origval;
-
-       OP_get_int(op, "bigger", &bigger);
-       OP_get_int(op, "smaller", &smaller);
-       OP_get_int(op, "dir", &dir);
-       OP_get_int(op, "origval", &origval);
-       
-       op->delta= CLAMPIS(op->delta, -smaller, bigger);
-       
-       for (v1= C->screen->vertbase.first; v1; v1= v1->next) {
-               if (v1->flag) {
-                       /* that way a nice AREAGRID  */
-                       if((dir=='v') && v1->vec.x>0 && v1->vec.x<C->window->sizex-1) {
-                               v1->vec.x= origval + op->delta;
-                               if(op->delta != bigger && op->delta != -smaller) v1->vec.x-= (v1->vec.x % AREAGRID);
+       if(win->screen) {
+               bScreen *scr= win->screen;
+               ScrArea *sa;
+               ARegion *ar;
+               int oldswin= scr->subwinactive;
+
+               for(sa= scr->areabase.first; sa; sa= sa->next) {
+                       if(event->x > sa->totrct.xmin && event->x < sa->totrct.xmax)
+                               if(event->y > sa->totrct.ymin && event->y < sa->totrct.ymax)
+                                       if(NULL==is_in_area_actionzone(sa, event->x, event->y))
+                                               break;
+               }
+               if(sa) {
+                       for(ar= sa->regionbase.first; ar; ar= ar->next) {
+                               if(BLI_in_rcti(&ar->winrct, event->x, event->y))
+                                       scr->subwinactive= ar->swinid;
                        }
-                       if((dir=='h') && v1->vec.y>0 && v1->vec.y<C->window->sizey-1) {
-                               v1->vec.y= origval + op->delta;
+               }
+               else
+                       scr->subwinactive= scr->mainwin;
+               
+               /* check for redraw headers */
+               if(oldswin!=scr->subwinactive) {
 
-                               v1->vec.y+= AREAGRID-1;
-                               v1->vec.y-= (v1->vec.y % AREAGRID);
+                       for(sa= scr->areabase.first; sa; sa= sa->next) {
+                               int do_draw= 0;
+                               
+                               for(ar= sa->regionbase.first; ar; ar= ar->next)
+                                       if(ar->swinid==oldswin || ar->swinid==scr->subwinactive)
+                                               do_draw= 1;
                                
-                               /* prevent too small top header */
-                               if(v1->vec.y > C->window->sizey-HEADERY)
-                                       v1->vec.y= C->window->sizey-HEADERY;
+                               if(do_draw) {
+                                       for(ar= sa->regionbase.first; ar; ar= ar->next)
+                                               if(ar->regiontype==RGN_TYPE_HEADER)
+                                                       ED_region_tag_redraw(ar);
+                               }
                        }
                }
+               
+               /* cursors, for time being set always on edges, otherwise aregion doesnt switch */
+               if(scr->subwinactive==scr->mainwin) {
+                       screen_cursor_set(win, event);
+               }
+               else if(oldswin!=scr->subwinactive) {
+                       region_cursor_set(win, scr->subwinactive);
+               }
        }
-       return 1;
 }
 
-static int move_areas_exit(bContext *C, wmOperator *op)
+int ED_screen_area_active(const bContext *C)
 {
-       WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);
-
-       /* this makes sure aligned edges will result in aligned grabbing */
-       removedouble_scrverts(C->screen);
-       removedouble_scredges(C->screen);
-
-       OP_free_property(op);   
-       return 1;
-}
-
-/* interaction callback */
-/* return 0 = stop evaluating for next handlers */
-static int move_areas_invoke (bContext *C, wmOperator *op, wmEvent *event)
-{
-       
-       /* operator arguments and storage */
-       op->properties = NULL;
-       op->delta= 0;
-       op->veci.x= event->x;
-       op->veci.y= event->y;
-       
-       if(0==move_areas_init(C, op)) 
-               return 1;
-       
-       /* add temp handler */
-       WM_event_add_modal_handler(&C->window->handlers, op);
-       
+       bScreen *sc= CTX_wm_screen(C);
+       ScrArea *sa= CTX_wm_area(C);
+
+       if(sc && sa) {
+               ARegion *ar;
+               for(ar= sa->regionbase.first; ar; ar= ar->next)
+                       if(ar->swinid == sc->subwinactive)
+                               return 1;
+       }       
        return 0;
 }
 
-/* modal callback for while moving edges */
-/* return 0 = stop evaluating for next handlers */
-static int move_areas_modal (bContext *C, wmOperator *op, wmEvent *event)
+/* operator call, WM + Window + screen already existed before */
+/* Do NOT call in area/region queues! */
+void ED_screen_set(bContext *C, bScreen *sc)
 {
-       int dir;
-
-       OP_get_int(op, "dir", &dir);
-
-       /* execute the events */
-       switch(event->type) {
-               case MOUSEMOVE:
-                       
-                       if(dir=='v')
-                               op->delta= event->x - op->veci.x;
-                       else
-                               op->delta= event->y - op->veci.y;
-                       
-                       move_areas_exec(C, op);
-                       WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);
+       wmWindow *win= CTX_wm_window(C);
+       bScreen *oldscreen= CTX_wm_screen(C);
+       ID *id;
+       
+       /* validate screen, it's called with notifier reference */
+       for(id= CTX_data_main(C)->screen.first; id; id= id->next)
+               if(sc == (bScreen *)id)
                        break;
-                       
-               case LEFTMOUSE:
-                       if(event->val==0) {
-                               move_areas_exit(C, op);
-                               WM_event_remove_modal_handler(&C->window->handlers, op);                                
+       if(id==NULL) 
+               return;
+       
+       /* check for valid winid */
+       if(sc->winid!=0 && sc->winid!=win->winid)
+               return;
+       
+       if(sc->full) {                          /* find associated full */
+               bScreen *sc1;
+               for(sc1= CTX_data_main(C)->screen.first; sc1; sc1= sc1->id.next) {
+                       ScrArea *sa= sc1->areabase.first;
+                       if(sa->full==sc) {
+                               sc= sc1;
+                               break;
                        }
-                       break;
-                       
-               case ESCKEY:
-                       op->delta= 0;
-                       move_areas_exec(C, op);
-                       
-                       WM_event_remove_modal_handler(&C->window->handlers, op);
-                       move_areas_exit(C, op);
-                       break;
+               }
        }
        
-       return 1;
-}
-
-void ED_SCR_OT_move_areas(wmOperatorType *ot)
-{
-       
-       /* identifiers */
-       ot->name= "Move area edges";
-       ot->idname= "ED_SCR_OT_move_areas";
-
-       ot->init= move_areas_init;
-       ot->invoke= move_areas_invoke;
-       ot->modal= move_areas_modal;
-       ot->exec= move_areas_exec;
-       ot->exit= move_areas_exit;
-
-       ot->poll= ED_operator_screen_mainwinactive;
-}
-
-/****************** split area ********************/
-/* we do split on init, then we work like move_areas
-       if operation gets cancelled -> join
-       if operation gets confirmed -> yay
-*/
-
-typedef struct sAreaSplitData
-{
-       int dir; /* direction of new edge */
-       int origval; /* for move areas */
-       int min,max; /* constraints for moving new edge */
-       int pos; /* with sa as center, ne is located at: 0=W, 1=N, 2=E, 3=S */
-       ScrEdge *nedge; /* new edge */
-       ScrEdge *aedge; /* active edge */
-       ScrArea *sarea; /* start area */
-       ScrArea *narea; /* new area */
-} sAreaSplitData;
-
-/* the moving of the new egde */
-static int split_area_exec(bContext *C, wmOperator *op)
-{
-       sAreaSplitData *sd= (sAreaSplitData *)op->customdata;
-       int newval= sd->origval + op->delta;
-       
-       newval= CLAMPIS(newval, -sd->min, sd->max);
-       
-       if((sd->dir=='v') && (newval > sd->min && newval < sd->max-1)) {
-               sd->nedge->v1->vec.x= newval;
-               sd->nedge->v2->vec.x= newval;
-       }
-       if((sd->dir=='h') && (newval > sd->min+HEADERY && newval < sd->max-HEADERY)) {
-               sd->nedge->v1->vec.y= newval;           
-               sd->nedge->v2->vec.y= newval;
+       if (oldscreen != sc) {
+               wmTimer *wt= oldscreen->animtimer;
+               
+               /* we put timer to sleep, so screen_exit has to think there's no timer */
+               oldscreen->animtimer= NULL;
+               if(wt)
+                       WM_event_window_timer_sleep(win, wt, 1);
+               
+               ED_screen_exit(C, win, oldscreen);
+               oldscreen->animtimer= wt;
+               
+               win->screen= sc;
+               CTX_wm_window_set(C, win);      // stores C->wm.screen... hrmf
+               
+               /* prevent multiwin errors */
+               sc->winid= win->winid;
+               
+               ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
+               WM_event_add_notifier(C, NC_WINDOW, NULL);
+               
+               /* makes button hilites work */
+               WM_event_add_mousemove(C);
        }
-       return 1;
 }
 
-static int split_area_exit(bContext *C, wmOperator *op)
+/* only call outside of area/region loops */
+void ED_screen_set_scene(bContext *C, Scene *scene)
 {
-       if (op->customdata) {
-               MEM_freeN(op->customdata);
-               op->customdata = NULL;
-       }
-       
-       WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);
-
-       /* this makes sure aligned edges will result in aligned grabbing */
-       removedouble_scrverts(C->screen);
-       removedouble_scredges(C->screen);
+       bScreen *sc;
+       bScreen *curscreen= CTX_wm_screen(C);
        
-       return 1;
-}
-
-static int split_initintern(bContext *C, wmOperator *op, sAreaSplitData *sd)
-{
-       float fac= 0.0;
-       if(sd->dir=='h') {
-               sd->origval= op->veci.y;
-               fac= 1.0 - ((float)(sd->sarea->v3->vec.y - op->veci.y)) / (float)sd->sarea->winy;
-               sd->min= sd->aedge->v1->vec.y;
-               sd->max= sd->aedge->v2->vec.y;
-       }
-       else {
-               sd->origval= op->veci.x;
-               fac= 1.0 - ((float)(sd->sarea->v4->vec.x - op->veci.x)) / (float)sd->sarea->winx;
-               sd->min= sd->aedge->v1->vec.x;
-               sd->max= sd->aedge->v2->vec.x;
+       for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
+               if((U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
+                       
+                       if(scene != sc->scene) {
+                               /* all areas endlocalview */
+                       // XXX  ScrArea *sa= sc->areabase.first;
+                       //      while(sa) {
+                       //              endlocalview(sa);
+                       //              sa= sa->next;
+                       //      }               
+                               sc->scene= scene;
+                       }
+                       
+               }
        }
        
-       sd->narea= splitarea(C->window, C->screen, sd->sarea, sd->dir, fac);
-       
-       if(sd->narea==NULL) return 0;
-       
-       sd->nedge= area_findsharededge(C->screen, sd->sarea, sd->narea);
+       //  copy_view3d_lock(0);        /* space.c */
+       
+       /* are there cameras in the views that are not in the scene? */
+       for(sc= CTX_data_main(C)->screen.first; sc; sc= sc->id.next) {
+               if( (U.flag & USER_SCENEGLOBAL) || sc==curscreen) {
+                       ScrArea *sa= sc->areabase.first;
+                       while(sa) {
+                               SpaceLink *sl= sa->spacedata.first;
+                               while(sl) {
+                                       if(sl->spacetype==SPACE_VIEW3D) {
+                                               View3D *v3d= (View3D*) sl;
+                                               if (!v3d->camera || !object_in_scene(v3d->camera, scene)) {
+                                                       v3d->camera= scene_find_camera(sc->scene);
+                                                       // XXX if (sc==curscreen) handle_view3d_lock();
+                                                       if (!v3d->camera && v3d->persp==V3D_CAMOB) 
+                                                               v3d->persp= V3D_PERSP;
+                                               }
+                                       }
+                                       sl= sl->next;
+                               }
+                               sa= sa->next;
+                       }
+               }
+       }
        
-       /* select newly created edge */
-       select_connected_scredge(C->screen, sd->nedge);
+       CTX_data_scene_set(C, scene);
+       set_scene_bg(scene);
        
-       WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);
+       ED_update_for_newframe(C, 1);
        
-       return 1;
-}
-
-static int split_area_init (bContext *C, wmOperator *op)
-{
-       sAreaSplitData *sd= NULL;
-       ScrEdge *actedge= screen_find_active_scredge(C->screen, op->veci.x, op->veci.y);
+       /* complete redraw */
+       WM_event_add_notifier(C, NC_WINDOW, NULL);
        
-       if(actedge==NULL) return 0;
-       
-       sd= (sAreaSplitData*)MEM_callocN(sizeof (sAreaSplitData), "op_split_area");
-       op->customdata= sd;
-       /* for split dir is in which new edge comes */
-       sd->dir= scredge_is_horizontal(actedge) ? 'v':'h';
-       sd->sarea= screen_test_edge_area(C->screen, C->curarea, actedge);
-       sd->aedge= actedge;
-
-       return split_initintern(C, op, sd);
 }
 
-static int split_area_invoke(bContext *C, wmOperator *op, wmEvent *event)
+/* this function toggles: if area is full then the parent will be restored */
+void ed_screen_fullarea(bContext *C, ScrArea *sa)
 {
-       /* operator arguments and storage */
-       op->customdata= NULL;
-       op->delta= 0;
-       op->veci.x= event->x;
-       op->veci.y= event->y;
-
-       op->customdata= NULL;
+       bScreen *sc, *oldscreen;
        
-       if(0==split_area_init(C, op)) 
-               return 1;
-       
-       /* add temp handler */
-       WM_event_add_modal_handler(&C->window->handlers, op);
-       
-       return 0;
-}
-
-/* join areas
-static void split_joincurrent(bContext *C, sAreaSplitData *sd)
-{
-       int orientation= area_getorientation(C->window->screen, sd->sarea, sd->narea);
-       if(orientation>-1) {
-               if(orientation==0) {
-                       sd->sarea->v1= sd->narea->v1;
-                       sd->sarea->v2= sd->narea->v2;
-                       screen_addedge(C->screen, sd->sarea->v2, sd->sarea->v3);
-                       screen_addedge(C->screen, sd->sarea->v1, sd->sarea->v4);
-               }
-               else if(orientation==1) {
-                       sd->sarea->v2= sd->narea->v2;
-                       sd->sarea->v3= sd->narea->v3;
-                       screen_addedge(C->screen, sd->sarea->v1, sd->sarea->v2);
-                       screen_addedge(C->screen, sd->sarea->v3, sd->sarea->v4);
-               }
-               else if(orientation==2) {
-                       sd->sarea->v3= sd->narea->v3;
-                       sd->sarea->v4= sd->narea->v4;
-                       screen_addedge(C->screen, sd->sarea->v2,sd-> sarea->v3);
-                       screen_addedge(C->screen, sd->sarea->v1, sd->sarea->v4);
-               }
-               else if(orientation==3) {
-                       sd->sarea->v1= sd->narea->v1;
-                       sd->sarea->v4= sd->narea->v4;
-                       screen_addedge(C->screen, sd->sarea->v1, sd->sarea->v2);
-                       screen_addedge(C->screen, sd->sarea->v3, sd->sarea->v4);
-               }
-
-               if (C->curarea == sd->narea) {
-                       C->curarea = NULL;
-               }
-               screen_delarea(C->screen, sd->narea);
-               sd->narea = NULL;
-               removedouble_scrverts(C->screen);
-               removedouble_scredges(C->screen);
+       if(sa==NULL) {
+               return;
        }
-}
-
-static int split_area_modal(bContext *C, wmOperator *op, wmEvent *event)
-{
-       ScrArea *sa= NULL, *sold=NULL;
-       sAreaSplitData *sd= (sAreaSplitData *)op->customdata;
-
-       /* execute the events */
-       switch(event->type) {
-               case MOUSEMOVE:
-                       sa= screen_areahascursor(C->screen, event->x, event->y);
-
-                       /* area containing cursor has changed */
-                       if(sa && sd->sarea!=sa && sd->narea!=sa) {
-                               sold= sd->sarea;
-                               split_joincurrent(C, sd);
-
-                               /* now find aedge with same orientation as sd->dir (inverted) */
-                               if(sd->dir=='v') {
-                                       sd->aedge= screen_findedge(C->screen, sa->v1, sa->v4);
-                                       if(sd->aedge==NULL) sd->aedge= screen_findedge(C->screen, sa->v2, sa->v3);
-                               }
-                               else {
-                                       sd->aedge= screen_findedge(C->screen, sa->v1, sa->v2);
-                                       if(sd->aedge==NULL) sd->aedge= screen_findedge(C->screen, sa->v3, sa->v4);
-                               }
-
-                               /* set sd and op to new init state */
-                               sd->sarea= sa;
-                               op->delta= 0;
-                               op->veci.x= event->x;
-                               op->veci.y= event->y;
-                               split_initintern(C, op, sd);
-                       }
-                       else {
-                               /* all is cool, update delta according complicated formula */
-                               if(sd->dir=='v')
-                                       op->delta= event->x - op->veci.x;
-                               else
-                                       op->delta= event->y - op->veci.y;
-                               
-                               split_area_exec(C, op);
-                       }
-                       WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);
-                       break;
-               case RIGHTMOUSE:
-                       if(event->val==0) { /* mouse up => confirm */
-                               split_area_exit(C, op);
-                               WM_event_remove_modal_handler(&C->window->handlers, op);
+       else if(sa->full) {
+               short fulltype;
+               
+               sc= sa->full;           /* the old screen to restore */
+               oldscreen= CTX_wm_screen(C);    /* the one disappearing */
+               
+               fulltype = sc->full;
+               
+               /* refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
+                  is set */
+               
+               if (fulltype != SCREENAUTOPLAY || (G.flags & G_FILE_AUTOPLAY) == 0) {
+                       ScrArea *old;
+                       
+                       sc->full= 0;
+                       
+                       /* find old area */
+                       for(old= sc->areabase.first; old; old= old->next) 
+                               if(old->full) break;
+                       if(old==NULL) {
+                               printf("something wrong in areafullscreen\n"); 
+                               return;
                        }
-                       break;
-               case LEFTMOUSE: /* cancel operation */
-               case ESCKEY:
-                       op->delta= 0;
-                       split_joincurrent(C, sd);
-                       WM_event_remove_modal_handler(&C->window->handlers, op);
-                       split_area_exit(C, op);
-                       break;
+                           // old feature described below (ton)
+                               // in autoplay screens the headers are disabled by 
+                               // default. So use the old headertype instead
+                       
+                       area_copy_data(old, sa, 1);     /*  1 = swap spacelist */
+                       
+                       old->full= NULL;
+                       
+                       /* animtimer back */
+                       sc->animtimer= oldscreen->animtimer;
+                       oldscreen->animtimer= NULL;
+                       
+                       ED_screen_set(C, sc);
+                       
+                       free_screen(oldscreen);
+                       free_libblock(&CTX_data_main(C)->screen, oldscreen);
+               }
        }
-       
-       return 1;
-}
-
-void ED_SCR_OT_split_area(wmOperatorType *ot)
-{
-       ot->name = "Split area";
-       ot->idname = "ED_SCR_OT_split_area";
-       
-       ot->init= split_area_init;
-       ot->invoke= split_area_invoke;
-       ot->modal= split_area_modal;
-       ot->exec= split_area_exec;
-       ot->exit= split_area_exit;
-       
-       ot->poll= ED_operator_screenactive;
-}
-
-/* ************** join area operator ********************************************** */
-
-/* operator state vars used:  
-           op->veci   mouse coord near edge
-           op->delta  movement of edge
-
-   callbacks:
-
-   init()   find edge based on op->veci, 
-                       test if the edge divides two areas, 
-                       store active and nonactive area,
-            
-
-   exec()      remove active window, 
-                       recalc size,
-                       make nonactive window active, 
-                       add notifier for redraw.
-   
-   invoke() handler gets called on Alt+RMB near edge
-            call init()
-            add handler
-
-   modal()     accept modal events while doing it
-                       call exec() with active window and nonactive window
-            call exit() and remove handler when LMB confirm
-
-   exit()      cleanup, send notifier
-
-*/
+       else {
+               ScrArea *newa;
+               
+               oldscreen= CTX_wm_screen(C);
 
-typedef struct sAreaJoinData
-{
-       int dir;
-       ScrArea *up;
-       ScrArea *down;
-       ScrArea *left;
-       ScrArea *right;
-       ScrArea *sa1; /* first area to be considered */
-       ScrArea *sa2; /* second area to be considered */
-       ScrArea *scr; /* designed for removal */
+               /* is there only 1 area? */
+               if(oldscreen->areabase.first==oldscreen->areabase.last) return;
+               
+               oldscreen->full = SCREENFULL;
+               
+               sc= ED_screen_add(CTX_wm_window(C), CTX_data_scene(C), "temp");
+               sc->full = SCREENFULL; // XXX
+               
+               /* timer */
+               sc->animtimer= oldscreen->animtimer;
+               oldscreen->animtimer= NULL;
+               
+               /* returns the top small area */
+               newa= area_split(CTX_wm_window(C), sc, (ScrArea *)sc->areabase.first, 'h', 0.99f);
+               ED_area_newspace(C, newa, SPACE_INFO);
 
-} sAreaJoinData;
+               /* copy area */
+               newa= newa->prev;
+               area_copy_data(newa, sa, 1);    /* 1 = swap spacelist */
 
+               sa->full= oldscreen;
+               newa->full= oldscreen;
+               newa->next->full= oldscreen; // XXX
 
-/* validate selection inside screen, set variables OK */
-/* return 0: init failed */
-static int join_areas_init (bContext *C, wmOperator *op)
-{
-       ScrEdge *actedge= screen_find_active_scredge(C->screen, op->veci.x, op->veci.y);
-       ScrArea *sa = C->curarea;
-       ScrEdge *se;
-       sAreaJoinData* jd= NULL;
-       short val = 0;
-
-       if(actedge==NULL) return 0;
-       
-       jd = (sAreaJoinData*)MEM_callocN(sizeof (sAreaJoinData), "op_join_areas");
-       jd->dir= scredge_is_horizontal(actedge) ? 'h':'v';
-       op->customdata = jd;
-       
-       select_connected_scredge(C->screen, actedge);
-
-       jd->sa1 = screen_test_edge_area(C->screen, sa, actedge);
-       if(jd->sa1==0) return 0;
-
-       /* find directions with same edge */
-       jd->sa2= C->screen->areabase.first;
-       while(jd->sa2) {
-               if(jd->sa2 != jd->sa1) {
-                       se= screen_findedge(C->screen, jd->sa2->v1, jd->sa2->v2);
-                       if(actedge==se) jd->right= jd->sa2;
-                       se= screen_findedge(C->screen, jd->sa2->v2, jd->sa2->v3);
-                       if(actedge==se) jd->down= jd->sa2;
-                       se= screen_findedge(C->screen, jd->sa2->v3, jd->sa2->v4);
-                       if(actedge==se) jd->left= jd->sa2;
-                       se= screen_findedge(C->screen, jd->sa2->v4, jd->sa2->v1);
-                       if(actedge==se) jd->up= jd->sa2;
-               }
-               jd->sa2= jd->sa2->next;
+               ED_screen_set(C, sc);
        }
 
-       if(jd->left) val++;
-       if(jd->up) val++;
-       if(jd->right) val++;
-       if(jd->down) val++;
-       
-       if(val==0) return 0;
-       else if(val==1) {
-               if(jd->left) {
-                       jd->right = jd->sa1;
-                       jd->sa2 = jd->left;
-                       jd->dir = 'h';
-               }
-               else if(jd->right) {
-                       jd->left = jd->sa1;
-                       jd->sa2 = jd->right;
-                       jd->dir = 'h';
-               }
-               else if(jd->up) {
-                       jd->down = jd->sa1;
-                       jd->sa2= jd->up;
-                       jd->dir = 'v';
-               }
-               else if(jd->down) {
-                       jd->up = jd->sa1;
-                       jd->sa2 = jd->down;
-                       jd->dir = 'v';
-               }
-       }
+       /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
+       CTX_wm_area_set(C, sc->areabase.first);
 
-       /* initial set up screen area asigned for destroying */
-       jd->scr = jd->sa2;
+       /* XXX retopo_force_update(); */
 
-       return 1;
 }
 
-/* apply the join of the areas (space types) */
-static int join_areas_exec(bContext *C, wmOperator *op)
+int ED_screen_full_newspace(bContext *C, ScrArea *sa, int type)
 {
-       sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
-       if (!jd) return 0;
-
-       if(jd->sa2!=jd->scr) {
-               jd->sa1 = jd->sa2;
-               jd->sa2 = jd->scr;
-       }
-
-       if(jd->sa2==jd->left) {
-               jd->sa1->v1= jd->sa2->v1;
-               jd->sa1->v2= jd->sa2->v2;
-               screen_addedge(C->screen, jd->sa1->v2, jd->sa1->v3);
-               screen_addedge(C->screen, jd->sa1->v1, jd->sa1->v4);
-       }
-       else if(jd->sa2==jd->up) {
-               jd->sa1->v2= jd->sa2->v2;
-               jd->sa1->v3= jd->sa2->v3;
-               screen_addedge(C->screen, jd->sa1->v1, jd->sa1->v2);
-               screen_addedge(C->screen, jd->sa1->v3, jd->sa1->v4);
-       }
-       else if(jd->sa2==jd->right) {
-               jd->sa1->v3= jd->sa2->v3;
-               jd->sa1->v4= jd->sa2->v4;
-               screen_addedge(C->screen, jd->sa1->v2,jd-> sa1->v3);
-               screen_addedge(C->screen, jd->sa1->v1, jd->sa1->v4);
-       }
-       else if(jd->sa2==jd->down) {
-               jd->sa1->v1= jd->sa2->v1;
-               jd->sa1->v4= jd->sa2->v4;
-               screen_addedge(C->screen, jd->sa1->v1, jd->sa1->v2);
-               screen_addedge(C->screen, jd->sa1->v3, jd->sa1->v4);
-       }
+       if(sa==NULL)
+               return 0;
+       
+       if(sa->full==0)
+               ed_screen_fullarea(C, sa);
 
-       if (C->curarea == jd->sa2) {
-               C->curarea = NULL;
-       }
-       screen_delarea(C->screen, jd->sa2);
-       jd->sa2 = NULL;
+       /* CTX_wm_area(C) is new area */
+       ED_area_newspace(C, CTX_wm_area(C), type);
+       
        return 1;
 }
 
-/* interaction callback */
-/* return 0 = stop evaluating for next handlers */
-static int join_areas_invoke (bContext *C, wmOperator *op, wmEvent *event)
+void ED_screen_full_prevspace(bContext *C)
 {
-       /* operator arguments and storage */
-       op->delta= 0;
-       op->veci.x= event->x;
-       op->veci.y= event->y;
-       op->customdata = NULL;
-
-       if(0==join_areas_init(C, op)) 
-               return 1;
+       ScrArea *sa= CTX_wm_area(C);
        
-       /* add temp handler */
-       WM_event_add_modal_handler(&C->window->handlers, op);
+       ED_area_prevspace(C);
        
-       return 0;
+       if(sa->full)
+               ed_screen_fullarea(C, sa);
 }
 
-static int is_inside_area(ScrArea *ar, short x, short y)
+/* redraws: uses defines from stime->redraws 
+ * enable: 1 - forward on, -1 - backwards on, 0 - off
+ */
+void ED_screen_animation_timer(bContext *C, int redraws, int enable)
 {
-       int is_inside = 0;
-       if ( (ar->v1->vec.x < x) && (x < ar->v3->vec.x) ) {
-               if ( (y<ar->v3->vec.y) && (ar->v1->vec.y<y) ) {
-                       is_inside = 1;
-               }
+       bScreen *screen= CTX_wm_screen(C);
+       wmWindow *win= CTX_wm_window(C);
+       Scene *scene= CTX_data_scene(C);
+       
+       if(screen->animtimer)
+               WM_event_remove_window_timer(win, screen->animtimer);
+       screen->animtimer= NULL;
+       
+       if(enable) {
+               struct ScreenAnimData *sad= MEM_mallocN(sizeof(ScreenAnimData), "ScreenAnimData");
+               
+               screen->animtimer= WM_event_add_window_timer(win, TIMER0, (1.0/FPS));
+               sad->ar= CTX_wm_region(C);
+               sad->redraws= redraws;
+               sad->flag= (enable < 0) ? ANIMPLAY_FLAG_REVERSE : 0;
+               screen->animtimer->customdata= sad;
+               
        }
-       return is_inside;
+       /* notifier catched by top header, for button */
+       WM_event_add_notifier(C, NC_SCREEN|ND_ANIMPLAY, screen);
 }
 
-
-/* finish operation */
-static int join_areas_exit(bContext *C, wmOperator *op)
+unsigned int ED_screen_view3d_layers(bScreen *screen)
 {
-       if (op->customdata) {
-               MEM_freeN(op->customdata);
-               op->customdata = NULL;
+       if(screen) {
+               unsigned int layer= screen->scene->lay; /* as minimum this */
+               ScrArea *sa;
+               
+               /* get all used view3d layers */
+               for(sa= screen->areabase.first; sa; sa= sa->next) {
+                       if(sa->spacetype==SPACE_VIEW3D)
+                               layer |= ((View3D *)sa->spacedata.first)->lay;
+               }
+               return layer;
        }
-
-       /* this makes sure aligned edges will result in aligned grabbing */
-       removedouble_scredges(C->screen);
-       removenotused_scredges(C->screen);
-       removenotused_scrverts(C->screen);
-
-       return 1;
+       return 0;
 }
 
-/* modal callback while selecting area (space) that will be removed */
-/* return 0 = stop evaluating for next handlers */
-static int join_areas_modal (bContext *C, wmOperator *op, wmEvent *event)
+
+/* results in fully updated anim system */
+void ED_update_for_newframe(const bContext *C, int mute)
 {
-       /* execute the events */
-       switch(event->type) {
-                       
-               case MOUSEMOVE:
-                       {
-                               sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
-                               ScrArea *sa = screen_areahascursor(C->screen, event->x, event->y);
-                               if (sa && sa != jd->sa2) {
-                                       if (sa == jd->sa1) {
-                                               jd->scr = jd->sa1;
-                                               jd->sa1 = jd->sa2;
-                                               jd->sa2 = jd->scr;
-                                       } 
-                               }
-                               break;
-                       }
-               case LEFTMOUSE:
-                       if(event->val==0) {
-                               join_areas_exec(C, op);
-                               WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0);                          
-                               join_areas_exit(C, op);
-                               WM_event_remove_modal_handler(&C->window->handlers, op);
+       bScreen *screen= CTX_wm_screen(C);
+       Scene *scene= screen->scene;
+       
+       //extern void audiostream_scrub(unsigned int frame);    /* seqaudio.c */
+       
+       /* this function applies the changes too */
+       /* XXX future: do all windows */
+       scene_update_for_newframe(scene, ED_screen_view3d_layers(screen)); /* BKE_scene.h */
+       
+       //if ( (CFRA>1) && (!mute) && (scene->audio.flag & AUDIO_SCRUB)) 
+       //      audiostream_scrub( CFRA );
+       
+       /* 3d window, preview */
+       //BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
+       
+       /* all movie/sequence images */
+       //BIF_image_update_frame();
+       
+       /* composite */
+       if(scene->use_nodes && scene->nodetree)
+               ntreeCompositTagAnimated(scene->nodetree);
+       
+       /* update animated texture nodes */
+       {
+               Tex *tex;
+               for(tex= CTX_data_main(C)->tex.first; tex; tex= tex->id.next)
+                       if( tex->use_nodes && tex->nodetree ) {
+                               ntreeTexTagAnimated( tex->nodetree );
                        }
-                       break;
-                       
-               case ESCKEY:
-                       op->delta= 0;
-                       join_areas_exit(C, op);
-                       WM_event_remove_modal_handler(&C->window->handlers, op);                        
-                       break;
        }
-       return 1;
 }
 
-/* Operator for joining two areas (space types) */
-void ED_SCR_OT_join_areas(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Join area";
-       ot->idname= "ED_SCR_OT_join_areas";
-
-       /* api callbacks */
-       ot->init= join_areas_init;
-       ot->invoke= join_areas_invoke;
-       ot->modal= join_areas_modal;
-       ot->exec= join_areas_exec;
-       ot->exit= join_areas_exit;
-
-       ot->poll= ED_operator_screen_mainwinactive;
-}