doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / space_node / node_edit.c
index 70c47a6..8086ce3 100644 (file)
@@ -1,5 +1,5 @@
-/**
- * $Id:
+/*
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2005 Blender Foundation.
  * All rights reserved.
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
+#include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
-#include "DNA_action_types.h"
-#include "DNA_brush_types.h"
-#include "DNA_color_types.h"
-#include "DNA_image_types.h"
-#include "DNA_ipo_types.h"
 #include "DNA_object_types.h"
 #include "DNA_material_types.h"
-#include "DNA_texture_types.h"
 #include "DNA_node_types.h"
-#include "DNA_space_types.h"
-#include "DNA_screen_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_userdef_types.h"
+
+#include "BLI_math.h"
+#include "BLI_blenlib.h"
+#include "BLI_storage_types.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_context.h"
-#include "BKE_colortools.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
 #include "BKE_library.h"
 #include "BKE_node.h"
 #include "BKE_material.h"
 #include "BKE_paint.h"
+#include "BKE_screen.h"
 #include "BKE_texture.h"
 #include "BKE_report.h"
-#include "BKE_scene.h"
-#include "BKE_utildefines.h"
-
-#include "BIF_gl.h"
-
-#include "BLI_math.h"
-#include "BLI_blenlib.h"
-#include "BLI_storage_types.h"
 
 #include "RE_pipeline.h"
 
 #include "IMB_imbuf_types.h"
 
 #include "ED_node.h"
-#include "ED_render.h"
 #include "ED_screen.h"
 #include "ED_space_api.h"
-#include "ED_transform.h"
-#include "ED_types.h"
+#include "ED_render.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 
 #include "UI_interface.h"
 #include "UI_view2d.h"
+
+#include "IMB_imbuf.h"
+
+#include "RNA_enum_types.h"
+
 #include "node_intern.h"
 
-#define SOCK_IN                1
-#define SOCK_OUT       2
+static EnumPropertyItem socket_in_out_items[] = {
+       { SOCK_IN, "IN", 0, "In", "" },
+       { SOCK_OUT, "OUT", 0, "Out", "" },
+       { 0, NULL, 0, NULL, NULL}
+};
 
 /* ***************** composite job manager ********************** */
 
@@ -101,6 +95,7 @@ typedef struct CompoJob {
        bNodeTree *localtree;
        short *stop;
        short *do_update;
+       float *progress;
 } CompoJob;
 
 /* called by compo, only to check job 'stop' value */
@@ -112,7 +107,7 @@ static int compo_breakjob(void *cjv)
 }
 
 /* called by compo, wmJob sends notifier */
-static void compo_redrawjob(void *cjv, char *str)
+static void compo_redrawjob(void *cjv, char *UNUSED(str))
 {
        CompoJob *cj= cjv;
        
@@ -146,9 +141,16 @@ static void compo_updatejob(void *cjv)
        ntreeLocalSync(cj->localtree, cj->ntree);
 }
 
+static void compo_progressjob(void *cjv, float progress)
+{
+       CompoJob *cj= cjv;
+       
+       *(cj->progress) = progress;
+}
+
 
 /* only this runs inside thread */
-static void compo_startjob(void *cjv, short *stop, short *do_update)
+static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
 {
        CompoJob *cj= cjv;
        bNodeTree *ntree= cj->localtree;
@@ -158,11 +160,14 @@ static void compo_startjob(void *cjv, short *stop, short *do_update)
        
        cj->stop= stop;
        cj->do_update= do_update;
+       cj->progress= progress;
        
        ntree->test_break= compo_breakjob;
        ntree->tbh= cj;
        ntree->stats_draw= compo_redrawjob;
        ntree->sdh= cj;
+       ntree->progress= compo_progressjob;
+       ntree->prh= cj;
        
        // XXX BIF_store_spare();
        
@@ -170,6 +175,7 @@ static void compo_startjob(void *cjv, short *stop, short *do_update)
        
        ntree->test_break= NULL;
        ntree->stats_draw= NULL;
+       ntree->progress= NULL;
 
 }
 
@@ -179,7 +185,7 @@ void snode_composite_job(const bContext *C, ScrArea *sa)
        wmJob *steve;
        CompoJob *cj;
 
-       steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, WM_JOB_EXCL_RENDER);
+       steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER|WM_JOB_PROGRESS);
        cj= MEM_callocN(sizeof(CompoJob), "compo job");
        
        /* customdata for preview thread */
@@ -189,7 +195,7 @@ void snode_composite_job(const bContext *C, ScrArea *sa)
        /* setup job */
        WM_jobs_customdata(steve, cj, compo_freejob);
        WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE|ND_COMPO_RESULT);
-       WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob);
+       WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
        
        WM_jobs_start(CTX_wm_manager(C), steve);
        
@@ -197,8 +203,19 @@ void snode_composite_job(const bContext *C, ScrArea *sa)
 
 /* ***************************************** */
 
+/* operator poll callback */
+static int composite_node_active(bContext *C)
+{
+       if( ED_operator_node_active(C)) {
+               SpaceNode *snode= CTX_wm_space_node(C);
+               if(snode->treetype==NTREE_COMPOSIT)
+                       return 1;
+       }
+       return 0;
+}
+
 /* also checks for edited groups */
-bNode *editnode_get_active(bNodeTree *ntree)
+static bNode *editnode_get_active(bNodeTree *ntree)
 {
        bNode *node;
        
@@ -212,8 +229,10 @@ bNode *editnode_get_active(bNodeTree *ntree)
                return nodeGetActive(ntree);
 }
 
-void snode_handle_recalc(bContext *C, SpaceNode *snode)
+void snode_notify(bContext *C, SpaceNode *snode)
 {
+       WM_event_add_notifier(C, NC_NODE|NA_EDITED, NULL);
+
        if(snode->treetype==NTREE_SHADER)
                WM_event_add_notifier(C, NC_MATERIAL|ND_NODES, snode->id);
        else if(snode->treetype==NTREE_COMPOSIT)
@@ -222,116 +241,6 @@ void snode_handle_recalc(bContext *C, SpaceNode *snode)
                WM_event_add_notifier(C, NC_TEXTURE|ND_NODES, snode->id);
 }
 
-#if 0
-static int image_detect_file_sequence(int *start_p, int *frames_p, char *str)
-{
-       SpaceFile *sfile;
-       char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX], filename[FILE_MAX];
-       int a, frame, totframe, found, minframe;
-       unsigned short numlen;
-
-       sfile= scrarea_find_space_of_type(curarea, SPACE_FILE);
-       if(sfile==NULL || sfile->filelist==NULL)
-               return 0;
-
-       /* find first frame */
-       found= 0;
-       minframe= 0;
-
-       for(a=0; a<sfile->totfile; a++) {
-               if(sfile->filelist[a].flags & ACTIVE) {
-                       BLI_strncpy(name, sfile->filelist[a].relname, sizeof(name));
-                       frame= BLI_stringdec(name, head, tail, &numlen);
-
-                       if(!found || frame < minframe) {
-                               BLI_strncpy(filename, name, sizeof(name));
-                               minframe= frame;
-                               found= 1;
-                       }
-               }
-       }
-
-       /* not one frame found */
-       if(!found)
-               return 0;
-
-       /* counter number of following frames */
-       found= 1;
-       totframe= 0;
-
-       for(frame=minframe; found; frame++) {
-               found= 0;
-               BLI_strncpy(name, filename, sizeof(name));
-               BLI_stringenc(name, head, tail, numlen, frame);
-
-               for(a=0; a<sfile->totfile; a++) {
-                       if(sfile->filelist[a].flags & ACTIVE) {
-                               if(strcmp(sfile->filelist[a].relname, name) == 0) {
-                                       found= 1;
-                                       totframe++;
-                                       break;
-                               }
-                       }
-               }
-       }
-
-       if(totframe > 1) {
-               BLI_strncpy(str, sfile->dir, sizeof(name));
-               strcat(str, filename);
-
-               *start_p= minframe;
-               *frames_p= totframe;
-               return 1;
-       }
-
-       return 0;
-}
-
-static void load_node_image(char *str) /* called from fileselect */
-{
-       SpaceNode *snode= curarea->spacedata.first;
-       bNode *node= nodeGetActive(snode->edittree);
-       Image *ima= NULL;
-       ImageUser *iuser= node->storage;
-       char filename[FILE_MAX];
-       int start=0, frames=0, sequence;
-
-       sequence= image_detect_file_sequence(&start, &frames, filename);
-       if(sequence)
-               str= filename;
-       
-       ima= BKE_add_image_file(str);
-       if(ima) {
-               if(node->id)
-                       node->id->us--;
-               
-               node->id= &ima->id;
-               id_us_plus(node->id);
-
-               BLI_strncpy(node->name, node->id->name+2, 21);
-
-               if(sequence) {
-                       ima->source= IMA_SRC_SEQUENCE;
-                       iuser->frames= frames;
-                       iuser->offset= start-1;
-               }
-                                  
-               BKE_image_signal(ima, node->storage, IMA_SIGNAL_RELOAD);
-               
-               NodeTagChanged(snode->edittree, node);
-               // XXX snode_handle_recalc(C, snode);
-       }
-}
-
-static void set_node_imagepath(char *str)      /* called from fileselect */
-{
-       SpaceNode *snode= curarea->spacedata.first;
-       bNode *node= nodeGetActive(snode->edittree);
-       BLI_strncpy(((NodeImageFile *)node->storage)->name, str, sizeof( ((NodeImageFile *)node->storage)->name ));
-}
-
-#endif /* 0 */
-
 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
 {
        bNode *gnode;
@@ -343,136 +252,6 @@ bNode *node_tree_get_editgroup(bNodeTree *nodetree)
        return gnode;
 }
 
-#if 0
-
-/* node has to be of type 'render layers' */
-/* is a bit clumsy copying renderdata here... scene nodes use render size of current render */
-static void composite_node_render(SpaceNode *snode, bNode *node)
-{
-       RenderData rd;
-       Scene *scene= NULL;
-       int scemode, actlay;
-       
-       /* the button press won't show up otherwise, button hilites disabled */
-       force_draw(0);
-       
-       if(node->id && node->id!=(ID *)G.scene) {
-               scene= G.scene;
-               set_scene_bg((Scene *)node->id);
-               rd= G.scene->r;
-               G.scene->r.xsch= scene->r.xsch;
-               G.scene->r.ysch= scene->r.ysch;
-               G.scene->r.size= scene->r.size;
-               G.scene->r.mode &= ~(R_BORDER|R_DOCOMP);
-               G.scene->r.mode |= scene->r.mode & R_BORDER;
-               G.scene->r.border= scene->r.border;
-               G.scene->r.cfra= scene->r.cfra;
-       }
-       
-       scemode= G.scene->r.scemode;
-       actlay= G.scene->r.actlay;
-
-       G.scene->r.scemode |= R_SINGLE_LAYER|R_COMP_RERENDER;
-       G.scene->r.actlay= node->custom1;
-       
-       BIF_do_render(0);
-       
-       G.scene->r.scemode= scemode;
-       G.scene->r.actlay= actlay;
-
-       node->custom2= 0;
-       
-       if(scene) {
-               G.scene->r= rd;
-               set_scene_bg(scene);
-       }
-}
-
-static void composit_node_event(SpaceNode *snode, short event)
-{
-       
-       switch(event) {
-               case B_REDR:
-                       // allqueue(REDRAWNODE, 1);
-                       break;
-               case B_NODE_SETIMAGE:
-               {
-                       bNode *node= nodeGetActive(snode->edittree);
-                       char name[FILE_MAXDIR+FILE_MAXFILE];
-                       
-                       strcpy(name, ((NodeImageFile *)node->storage)->name);
-                       if (G.qual & LR_CTRLKEY) {
-                               activate_imageselect(FILE_SPECIAL, "SELECT OUTPUT DIR", name, set_node_imagepath);
-                       } else {
-                               activate_fileselect(FILE_SPECIAL, "SELECT OUTPUT DIR", name, set_node_imagepath);
-                       }
-                       break;
-               }
-               case B_NODE_TREE_EXEC:
-                       // XXX                  snode_handle_recalc(snode);
-                       break;          
-               default:
-                       /* B_NODE_EXEC */
-               {
-                       bNode *node= BLI_findlink(&snode->edittree->nodes, event-B_NODE_EXEC);
-                       if(node) {
-                               NodeTagChanged(snode->edittree, node);
-                               /* don't use NodeTagIDChanged, it gives far too many recomposites for image, scene layers, ... */
-                               
-                               /* not the best implementation of the world... but we need it to work now :) */
-                               if(node->type==CMP_NODE_R_LAYERS && node->custom2) {
-                                       /* add event for this window (after render curarea can be changed) */
-                                       addqueue(curarea->win, UI_BUT_EVENT, B_NODE_TREE_EXEC);
-                                       
-                                       composite_node_render(snode, node);
-                                       // XXX                  snode_handle_recalc(snode);
-                                       
-                                       /* add another event, a render can go fullscreen and open new window */
-                                       addqueue(curarea->win, UI_BUT_EVENT, B_NODE_TREE_EXEC);
-                               }
-                               else {
-                                       node= node_tree_get_editgroup(snode->nodetree);
-                                       if(node)
-                                               NodeTagIDChanged(snode->nodetree, node->id);
-                                       
-                                       // XXX                  snode_handle_recalc(snode);
-                               }
-                       }
-               }                       
-       }
-}
-
-static void texture_node_event(SpaceNode *snode, short event)
-{
-       switch(event) {
-               case B_REDR:
-                       // allqueue(REDRAWNODE, 1);
-                       break;
-               case B_NODE_LOADIMAGE:
-               {
-                       bNode *node= nodeGetActive(snode->edittree);
-                       char name[FILE_MAXDIR+FILE_MAXFILE];
-                       
-                       if(node->id)
-                               strcpy(name, ((Image *)node->id)->name);
-                       else strcpy(name, U.textudir);
-                       if (G.qual & LR_CTRLKEY) {
-                               activate_imageselect(FILE_SPECIAL, "SELECT IMAGE", name, load_node_image);
-                       } else {
-                               activate_fileselect(FILE_SPECIAL, "SELECT IMAGE", name, load_node_image);
-                       }
-                       break;
-               }
-               default:
-                       /* B_NODE_EXEC */
-                       ntreeTexCheckCyclics( snode->nodetree );
-                       // XXX                  snode_handle_recalc(snode);
-                       // allqueue(REDRAWNODE, 1);
-                       break;
-       }
-}
-
-#endif /* 0  */
 /* assumes nothing being done in ntree yet, sets the default in/out node */
 /* called from shading buttons or header */
 void ED_node_shader_default(Material *ma)
@@ -482,11 +261,12 @@ void ED_node_shader_default(Material *ma)
        
        /* but lets check it anyway */
        if(ma->nodetree) {
-               printf("error in shader initialize\n");
+               if (G.f & G_DEBUG)
+                       printf("error in shader initialize\n");
                return;
        }
        
-       ma->nodetree= ntreeAddTree(NTREE_SHADER);
+       ma->nodetree= ntreeAddTree("Shader Nodetree", NTREE_SHADER, FALSE);
        
        out= nodeAddNodeType(ma->nodetree, SH_NODE_OUTPUT, NULL, NULL);
        out->locx= 300.0f; out->locy= 300.0f;
@@ -512,19 +292,22 @@ void ED_node_composit_default(Scene *sce)
        
        /* but lets check it anyway */
        if(sce->nodetree) {
-               printf("error in composit initialize\n");
+               if (G.f & G_DEBUG)
+                       printf("error in composite initialize\n");
                return;
        }
        
-       sce->nodetree= ntreeAddTree(NTREE_COMPOSIT);
+       sce->nodetree= ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, FALSE);
        
        out= nodeAddNodeType(sce->nodetree, CMP_NODE_COMPOSITE, NULL, NULL);
        out->locx= 300.0f; out->locy= 400.0f;
        out->id= &sce->id;
+       id_us_plus(out->id);
        
        in= nodeAddNodeType(sce->nodetree, CMP_NODE_R_LAYERS, NULL, NULL);
        in->locx= 10.0f; in->locy= 400.0f;
        in->id= &sce->id;
+       id_us_plus(in->id);
        nodeSetActive(sce->nodetree, in);
        
        /* links from color to color */
@@ -546,11 +329,12 @@ void ED_node_texture_default(Tex *tx)
        
        /* but lets check it anyway */
        if(tx->nodetree) {
-               printf("error in texture initialize\n");
+               if (G.f & G_DEBUG)
+                       printf("error in texture initialize\n");
                return;
        }
        
-       tx->nodetree= ntreeAddTree(NTREE_TEXTURE);
+       tx->nodetree= ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, FALSE);
        
        out= nodeAddNodeType(tx->nodetree, TEX_NODE_OUTPUT, NULL, NULL);
        out->locx= 300.0f; out->locy= 300.0f;
@@ -566,6 +350,7 @@ void ED_node_texture_default(Tex *tx)
        ntreeSolveOrder(tx->nodetree);  /* needed for pointers */
 }
 
+/* id is supposed to contain a node tree */
 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
 {
        bNode *node= NULL;
@@ -583,6 +368,10 @@ void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *tre
                *ntree= ((Tex*)id)->nodetree;
                if(treetype) *treetype= NTREE_TEXTURE;
        }
+       else {
+               if(treetype) *treetype= 0;
+               return;
+       }
 
        /* find editable group */
        if(edittree) {
@@ -638,67 +427,61 @@ void snode_set_context(SpaceNode *snode, Scene *scene)
                                        snode->from= (ID*)give_current_material(ob, ob->actcol);
 
                                /* from is not set fully for material nodes, should be ID + Node then */
+                               snode->id= &tx->id;
                        }
                }
                else if(snode->texfrom==SNODE_TEX_WORLD) {
                        tx= give_current_world_texture(scene->world);
                        snode->from= (ID *)scene->world;
+                       snode->id= &tx->id;
                }
                else {
-                       Brush *brush= NULL;
+                       struct Brush *brush= NULL;
                        
                        if(ob && (ob->mode & OB_MODE_SCULPT))
                                brush= paint_brush(&scene->toolsettings->sculpt->paint);
                        else
                                brush= paint_brush(&scene->toolsettings->imapaint.paint);
 
-                       snode->from= (ID *)brush;
-                       tx= give_current_brush_texture(brush);
+                       if (brush) {
+                               snode->from= (ID *)brush;
+                               tx= give_current_brush_texture(brush);
+                               snode->id= &tx->id;
+                       }
                }
-               
-               snode->id= &tx->id;
        }
 
        if(snode->id)
                node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
 }
 
-#if 0
-/* on activate image viewer, check if we show it */
-static void node_active_image(Image *ima)
-{
-       ScrArea *sa;
-       SpaceImage *sima= NULL;
-       
-       /* find an imagewindow showing render result */
-       for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
-               if(sa->spacetype==SPACE_IMAGE) {
-                       sima= sa->spacedata.first;
-                       if(sima->image && sima->image->source!=IMA_SRC_VIEWER)
-                               break;
-               }
-       }
-       if(sa && sima) {
-               sima->image= ima;
-               scrarea_queue_winredraw(sa);
-               scrarea_queue_headredraw(sa);
-       }
-}
-#endif /* 0 */
-
 void node_set_active(SpaceNode *snode, bNode *node)
 {
        nodeSetActive(snode->edittree, node);
        
        if(node->type!=NODE_GROUP) {
+               int was_output= (node->flag & NODE_DO_OUTPUT);
+               
                /* tree specific activate calls */
                if(snode->treetype==NTREE_SHADER) {
-                       // XXX
-#if 0
-                       
                        /* when we select a material, active texture is cleared, for buttons */
                        if(node->id && GS(node->id->name)==ID_MA)
                                nodeClearActiveID(snode->edittree, ID_TE);
+                       
+                       if(node->type==SH_NODE_OUTPUT) {
+                               bNode *tnode;
+                               
+                               for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
+                                       if( tnode->type==SH_NODE_OUTPUT)
+                                               tnode->flag &= ~NODE_DO_OUTPUT;
+                               
+                               node->flag |= NODE_DO_OUTPUT;
+                               if(was_output==0)
+                                       ED_node_changed_update(snode->id, node);
+                       }
+
+                       // XXX
+#if 0
                        if(node->id)
                                ; // XXX BIF_preview_changed(-1);       /* temp hack to force texture preview to update */
                        
@@ -712,7 +495,7 @@ void node_set_active(SpaceNode *snode, bNode *node)
                        /* make active viewer, currently only 1 supported... */
                        if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
                                bNode *tnode;
-                               int was_output= node->flag & NODE_DO_OUTPUT;
+                               
 
                                for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
                                        if( ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
@@ -735,20 +518,21 @@ void node_set_active(SpaceNode *snode, bNode *node)
                                /* addnode() doesnt link this yet... */
                                node->id= (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
                        }
-                       else if(node->type==CMP_NODE_IMAGE) {
-                               // XXX
-#if 0
-                               if(node->id)
-                                       node_active_image((Image *)node->id);
-#endif
-                       }
                        else if(node->type==CMP_NODE_R_LAYERS) {
                                if(node->id==NULL || node->id==(ID *)scene) {
                                        scene->r.actlay= node->custom1;
-                                       // XXX
-                                       // allqueue(REDRAWBUTSSCENE, 0);
                                }
                        }
+                       else if(node->type==CMP_NODE_COMPOSITE) {
+                               bNode *tnode;
+                               
+                               for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
+                                       if( tnode->type==CMP_NODE_COMPOSITE)
+                                               tnode->flag &= ~NODE_DO_OUTPUT;
+                               
+                               node->flag |= NODE_DO_OUTPUT;
+                               ED_node_changed_update(snode->id, node);
+                       }
                }
                else if(snode->treetype==NTREE_TEXTURE) {
                        // XXX
@@ -762,6 +546,19 @@ void node_set_active(SpaceNode *snode, bNode *node)
        }
 }
 
+/* when links in groups change, inputs/outputs change, nodes added/deleted... */
+void node_tree_verify_groups(bNodeTree *nodetree)
+{
+       bNode *gnode;
+       
+       gnode= node_tree_get_editgroup(nodetree);
+       
+       /* does all materials */
+       if(gnode)
+               nodeGroupVerify((bNodeTree *)gnode->id);
+       
+}
+
 /* ***************** Edit Group operator ************* */
 
 void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
@@ -795,20 +592,15 @@ void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
                snode->edittree= snode->nodetree;
        
        ntreeSolveOrder(snode->nodetree);
-       
-       /* finally send out events for new active node */
-       if(snode->treetype==NTREE_SHADER) {
-               // allqueue(REDRAWBUTSSHADING, 0);
-               
-               // XXX BIF_preview_changed(-1); /* temp hack to force texture preview to update */
-       }
 }
 
-static int node_group_edit_exec(bContext *C, wmOperator *op)
+static int node_group_edit_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceNode *snode = CTX_wm_space_node(C);
        bNode *gnode;
 
+       ED_preview_kill_jobs(C);
+
        gnode= nodeGetActive(snode->edittree);
        snode_make_group_editable(snode, gnode);
 
@@ -817,7 +609,7 @@ static int node_group_edit_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int node_group_edit_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int node_group_edit_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        SpaceNode *snode = CTX_wm_space_node(C);
        bNode *gnode;
@@ -835,7 +627,7 @@ void NODE_OT_group_edit(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name = "Edit Group";
-       ot->description = "Edit node group.";
+       ot->description = "Edit node group";
        ot->idname = "NODE_OT_group_edit";
        
        /* api callbacks */
@@ -847,136 +639,298 @@ void NODE_OT_group_edit(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/* ******************** Ungroup operator ********************** */
+/* ***************** Add Group Socket operator ************* */
 
-static int node_group_ungroup_exec(bContext *C, wmOperator *op)
+static int node_group_socket_add_exec(bContext *C, wmOperator *op)
 {
        SpaceNode *snode = CTX_wm_space_node(C);
-       bNode *gnode;
-
-       /* are we inside of a group? */
-       gnode= node_tree_get_editgroup(snode->nodetree);
-       if(gnode)
-               snode_make_group_editable(snode, NULL);
+       int in_out= -1;
+       char name[32]= "";
+       int type= SOCK_VALUE;
+       bNodeTree *ngroup= snode->edittree;
+       bNodeSocket *sock;
        
-       gnode= nodeGetActive(snode->edittree);
-       if(gnode==NULL)
-               return OPERATOR_CANCELLED;
+       ED_preview_kill_jobs(C);
        
-       if(gnode->type!=NODE_GROUP) {
-               BKE_report(op->reports, RPT_ERROR, "Not a group");
-               return OPERATOR_CANCELLED;
-       }
-       else if(!nodeGroupUnGroup(snode->edittree, gnode)) {
-               BKE_report(op->reports, RPT_ERROR, "Can't ungroup");
+       if (RNA_property_is_set(op->ptr, "name"))
+               RNA_string_get(op->ptr, "name", name);
+       
+       if (RNA_property_is_set(op->ptr, "type"))
+               type = RNA_enum_get(op->ptr, "type");
+       
+       if (RNA_property_is_set(op->ptr, "in_out"))
+               in_out = RNA_enum_get(op->ptr, "in_out");
+       else
                return OPERATOR_CANCELLED;
-       }
-
-       WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
-
+       
+       sock = nodeGroupAddSocket(ngroup, name, type, in_out);
+       
+       node_tree_verify_groups(snode->nodetree);
+       
+       snode_notify(C, snode);
+       
        return OPERATOR_FINISHED;
 }
 
-void NODE_OT_group_ungroup(wmOperatorType *ot)
+void NODE_OT_group_socket_add(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Ungroup";
-       ot->description = "Ungroup selected nodes.";
-       ot->idname = "NODE_OT_group_ungroup";
+       ot->name = "Add Group Socket";
+       ot->description = "Add node group socket";
+       ot->idname = "NODE_OT_group_socket_add";
        
        /* api callbacks */
-       ot->exec = node_group_ungroup_exec;
+       ot->exec = node_group_socket_add_exec;
        ot->poll = ED_operator_node_active;
        
        /* flags */
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
+       RNA_def_string(ot->srna, "name", "", 32, "Name", "Group socket name");
+       RNA_def_enum(ot->srna, "type", node_socket_type_items, SOCK_VALUE, "Type", "Type of the group socket");
 }
 
-/* when links in groups change, inputs/outputs change, nodes added/deleted... */
-static void node_tree_verify_groups(bNodeTree *nodetree)
+/* ***************** Remove Group Socket operator ************* */
+
+static int node_group_socket_remove_exec(bContext *C, wmOperator *op)
 {
-       bNode *gnode;
+       SpaceNode *snode = CTX_wm_space_node(C);
+       int index= -1;
+       int in_out= -1;
+       bNodeTree *ngroup= snode->edittree;
+       bNodeSocket *sock;
        
-       gnode= node_tree_get_editgroup(nodetree);
+       ED_preview_kill_jobs(C);
        
-       /* does all materials */
-       if(gnode)
-               nodeVerifyGroup((bNodeTree *)gnode->id);
+       if (RNA_property_is_set(op->ptr, "index"))
+               index = RNA_int_get(op->ptr, "index");
+       else
+               return OPERATOR_CANCELLED;
+       
+       if (RNA_property_is_set(op->ptr, "in_out"))
+               in_out = RNA_enum_get(op->ptr, "in_out");
+       else
+               return OPERATOR_CANCELLED;
+       
+       sock = (bNodeSocket*)BLI_findlink(in_out==SOCK_IN ? &ngroup->inputs : &ngroup->outputs, index);
+       if (sock) {
+               nodeGroupRemoveSocket(ngroup, sock, in_out);
+               node_tree_verify_groups(snode->nodetree);
+               
+               snode_notify(C, snode);
+       }
        
+       return OPERATOR_FINISHED;
 }
 
-#if 0
-
-static void node_addgroup(SpaceNode *snode)
+void NODE_OT_group_socket_remove(wmOperatorType *ot)
 {
-       bNodeTree *ngroup;
-       int tot= 0, offs, val;
-       char *strp;
-       
-       if(snode->edittree!=snode->nodetree) {
-               error("Can not add a Group in a Group");
-               return;
-       }
+       /* identifiers */
+       ot->name = "Remove Group Socket";
+       ot->description = "Removed node group socket";
+       ot->idname = "NODE_OT_group_socket_remove";
        
-       /* construct menu with choices */
-       for(ngroup= G.main->nodetree.first; ngroup; ngroup= ngroup->id.next) {
-               if(ngroup->type==snode->treetype)
-                       tot++;
-       }
-       if(tot==0) {
-               error("No groups available in database");
-               return;
-       }
-       strp= MEM_mallocN(32*tot+32, "menu");
-       strcpy(strp, "Add Group %t");
-       offs= strlen(strp);
+       /* api callbacks */
+       ot->exec = node_group_socket_remove_exec;
+       ot->poll = ED_operator_node_active;
        
-       for(tot=0, ngroup= G.main->nodetree.first; ngroup; ngroup= ngroup->id.next, tot++) {
-               if(ngroup->type==snode->treetype)
-                       offs+= sprintf(strp+offs, "|%s %%x%d", ngroup->id.name+2, tot);
-       }       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       val= pupmenu(strp);
-       if(val>=0) {
-               ngroup= BLI_findlink(&G.main->nodetree, val);
-               if(ngroup) {
-                       bNode *node= nodeAddNodeType(snode->edittree, NODE_GROUP, ngroup, NULL);
-                       
-                       /* generics */
-                       if(node) {
-                               float locx, locy;
-                               short mval[2];
-
-                               node_deselectall(snode, 0);
-                               
-                               getmouseco_areawin(mval);
-                               areamouseco_to_ipoco(G.v2d, mval, &locx, &locy);
-                               
-                               node->locx= locx;
-                               node->locy= locy + 60.0f;               // arbitrary.. so its visible
-                               node->flag |= SELECT;
-                               
-                               id_us_plus(node->id);
-                               
-                               node_set_active(snode, node);
-                       }
-               }                       
-       }
-       MEM_freeN(strp);
+       RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
+       RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
 }
 
-#endif /* 0 */
-
-/* ************************** Node generic ************** */
+/* ***************** Move Group Socket Up operator ************* */
 
-/* allows to walk the list in order of visibility */
-bNode *next_node(bNodeTree *ntree)
+static int node_group_socket_move_up_exec(bContext *C, wmOperator *op)
 {
-       static bNode *current=NULL, *last= NULL;
+       SpaceNode *snode = CTX_wm_space_node(C);
+       int index= -1;
+       int in_out= -1;
+       bNodeTree *ngroup= snode->edittree;
+       bNodeSocket *sock, *prev;
        
-       if(ntree) {
-               /* set current to the first selected node */
-               for(current= ntree->nodes.last; current; current= current->prev)
+       ED_preview_kill_jobs(C);
+       
+       if (RNA_property_is_set(op->ptr, "index"))
+               index = RNA_int_get(op->ptr, "index");
+       else
+               return OPERATOR_CANCELLED;
+       
+       if (RNA_property_is_set(op->ptr, "in_out"))
+               in_out = RNA_enum_get(op->ptr, "in_out");
+       else
+               return OPERATOR_CANCELLED;
+       
+       /* swap */
+       if (in_out==SOCK_IN) {
+               sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
+               prev = sock->prev;
+               /* can't move up the first socket */
+               if (!prev)
+                       return OPERATOR_CANCELLED;
+               BLI_remlink(&ngroup->inputs, sock);
+               BLI_insertlinkbefore(&ngroup->inputs, prev, sock);
+       }
+       else if (in_out==SOCK_OUT) {
+               sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
+               prev = sock->prev;
+               /* can't move up the first socket */
+               if (!prev)
+                       return OPERATOR_CANCELLED;
+               BLI_remlink(&ngroup->outputs, sock);
+               BLI_insertlinkbefore(&ngroup->outputs, prev, sock);
+       }
+       node_tree_verify_groups(snode->nodetree);
+       
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_group_socket_move_up(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Move Group Socket Up";
+       ot->description = "Move up node group socket";
+       ot->idname = "NODE_OT_group_socket_move_up";
+       
+       /* api callbacks */
+       ot->exec = node_group_socket_move_up_exec;
+       ot->poll = ED_operator_node_active;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
+       RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
+}
+
+/* ***************** Move Group Socket Up operator ************* */
+
+static int node_group_socket_move_down_exec(bContext *C, wmOperator *op)
+{
+       SpaceNode *snode = CTX_wm_space_node(C);
+       int index= -1;
+       int in_out= -1;
+       bNodeTree *ngroup= snode->edittree;
+       bNodeSocket *sock, *next;
+       
+       ED_preview_kill_jobs(C);
+       
+       if (RNA_property_is_set(op->ptr, "index"))
+               index = RNA_int_get(op->ptr, "index");
+       else
+               return OPERATOR_CANCELLED;
+       
+       if (RNA_property_is_set(op->ptr, "in_out"))
+               in_out = RNA_enum_get(op->ptr, "in_out");
+       else
+               return OPERATOR_CANCELLED;
+       
+       /* swap */
+       if (in_out==SOCK_IN) {
+               sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
+               next = sock->next;
+               /* can't move down the last socket */
+               if (!next)
+                       return OPERATOR_CANCELLED;
+               BLI_remlink(&ngroup->inputs, sock);
+               BLI_insertlinkafter(&ngroup->inputs, next, sock);
+       }
+       else if (in_out==SOCK_OUT) {
+               sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
+               next = sock->next;
+               /* can't move down the last socket */
+               if (!next)
+                       return OPERATOR_CANCELLED;
+               BLI_remlink(&ngroup->outputs, sock);
+               BLI_insertlinkafter(&ngroup->outputs, next, sock);
+       }
+       node_tree_verify_groups(snode->nodetree);
+       
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_group_socket_move_down(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Move Group Socket Down";
+       ot->description = "Move down node group socket";
+       ot->idname = "NODE_OT_group_socket_move_down";
+       
+       /* api callbacks */
+       ot->exec = node_group_socket_move_down_exec;
+       ot->poll = ED_operator_node_active;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
+       RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
+}
+
+/* ******************** Ungroup operator ********************** */
+
+static int node_group_ungroup_exec(bContext *C, wmOperator *op)
+{
+       SpaceNode *snode = CTX_wm_space_node(C);
+       bNode *gnode;
+
+       ED_preview_kill_jobs(C);
+
+       /* are we inside of a group? */
+       gnode= node_tree_get_editgroup(snode->nodetree);
+       if(gnode)
+               snode_make_group_editable(snode, NULL);
+       
+       gnode= nodeGetActive(snode->edittree);
+       if(gnode==NULL)
+               return OPERATOR_CANCELLED;
+       
+       if(gnode->type!=NODE_GROUP) {
+               BKE_report(op->reports, RPT_WARNING, "Not a group");
+               return OPERATOR_CANCELLED;
+       }
+       else if(!nodeGroupUnGroup(snode->edittree, gnode)) {
+               BKE_report(op->reports, RPT_WARNING, "Can't ungroup");
+               return OPERATOR_CANCELLED;
+       }
+
+       WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
+
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_group_ungroup(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Ungroup";
+       ot->description = "Ungroup selected nodes";
+       ot->idname = "NODE_OT_group_ungroup";
+       
+       /* api callbacks */
+       ot->exec = node_group_ungroup_exec;
+       ot->poll = ED_operator_node_active;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+/* ************************** Node generic ************** */
+
+/* allows to walk the list in order of visibility */
+bNode *next_node(bNodeTree *ntree)
+{
+       static bNode *current=NULL, *last= NULL;
+       
+       if(ntree) {
+               /* set current to the first selected node */
+               for(current= ntree->nodes.last; current; current= current->prev)
                        if(current->flag & NODE_SELECT)
                                break;
                
@@ -1035,159 +989,43 @@ static bNode *visible_node(SpaceNode *snode, rctf *rct)
        return tnode;
 }
 
-void snode_home(ScrArea *sa, ARegion *ar, SpaceNode* snode)
-{
-       bNode *node;
-       rctf *cur, *tot;
-       float oldwidth, oldheight, width, height;
-       int first= 1;
-       
-       cur= &ar->v2d.cur;
-       tot= &ar->v2d.tot;
-       
-       oldwidth= cur->xmax - cur->xmin;
-       oldheight= cur->ymax - cur->ymin;
-       
-       cur->xmin= cur->ymin= 0.0f;
-       cur->xmax=ar->winx;
-       cur->xmax= ar->winy;
-       
-       if(snode->edittree) {
-               for(node= snode->edittree->nodes.first; node; node= node->next) {
-                       if(first) {
-                               first= 0;
-                               ar->v2d.cur= node->totr;
-                       }
-                       else {
-                               BLI_union_rctf(cur, &node->totr);
-                       }
-               }
-       }
-       
-       snode->xof= 0;
-       snode->yof= 0;
-       width= cur->xmax - cur->xmin;
-       height= cur->ymax- cur->ymin;
-       if(width > height) {
-               float newheight;
-               newheight= oldheight * width/oldwidth;
-               cur->ymin= cur->ymin - newheight/4;
-               cur->ymax= cur->ymin + newheight;
-       }
-       else {
-               float newwidth;
-               newwidth= oldwidth * height/oldheight;
-               cur->xmin= cur->xmin - newwidth/4;
-               cur->xmax= cur->xmin + newwidth;
-       }
-       
-       ar->v2d.tot= ar->v2d.cur;
-       UI_view2d_curRect_validate(&ar->v2d);
-}
-
-#if 0
-static void snode_bg_viewmove(SpaceNode *snode)
-{
-       ScrArea *sa;
-       Image *ima;
-       ImBuf *ibuf;
-       Window *win;
-       short mval[2], mvalo[2];
-       short rectx, recty, xmin, xmax, ymin, ymax, pad;
-       int oldcursor;
-       
-       ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
-       ibuf= BKE_image_get_ibuf(ima, NULL);
-       
-       sa = snode->area;
-       
-       if(ibuf) {
-               rectx = ibuf->x;
-               recty = ibuf->y;
-       } else {
-               rectx = recty = 1;
-       }
-       
-       pad = 10;
-       xmin = -(sa->winx/2) - rectx/2 + pad;
-       xmax = sa->winx/2 + rectx/2 - pad;
-       ymin = -(sa->winy/2) - recty/2 + pad;
-       ymax = sa->winy/2 + recty/2 - pad;
-       
-       getmouseco_sc(mvalo);
-       
-       /* store the old cursor to temporarily change it */
-       oldcursor=get_cursor();
-       win=winlay_get_active_window();
-       
-       SetBlenderCursor(BC_NSEW_SCROLLCURSOR);
-       
-       while(get_mbut()&(L_MOUSE|M_MOUSE)) {
-               
-               getmouseco_sc(mval);
-               
-               if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
-                       
-                       snode->xof -= (mvalo[0]-mval[0]);
-                       snode->yof -= (mvalo[1]-mval[1]);
-                       
-                       /* prevent dragging image outside of the window and losing it! */
-                       CLAMP(snode->xof, xmin, xmax);
-                       CLAMP(snode->yof, ymin, ymax);
-                       
-                       mvalo[0]= mval[0];
-                       mvalo[1]= mval[1];
-                       
-                       scrarea_do_windraw(curarea);
-                       screen_swapbuffers();
-               }
-               else BIF_wait_for_statechange();
-       }
-       
-       window_set_cursor(win, oldcursor);
-}
-#endif
-
-/* ********************** size widget operator ******************** */
+/* **************************** */
 
-typedef struct NodeSizeWidget {
-       float mxstart;
-       float oldwidth;
-} NodeSizeWidget;
+typedef struct NodeViewMove {
+       short mvalo[2];
+       int xmin, ymin, xmax, ymax;
+} NodeViewMove;
 
-static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
+static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceNode *snode= CTX_wm_space_node(C);
        ARegion *ar= CTX_wm_region(C);
-       bNode *node= editnode_get_active(snode->edittree);
-       NodeSizeWidget *nsw= op->customdata;
-       float mx, my;
-       
+       NodeViewMove *nvm= op->customdata;
+
        switch (event->type) {
                case MOUSEMOVE:
                        
-                       UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
-                                                                        &mx, &my);
+                       snode->xof -= (nvm->mvalo[0]-event->mval[0]);
+                       snode->yof -= (nvm->mvalo[1]-event->mval[1]);
+                       nvm->mvalo[0]= event->mval[0];
+                       nvm->mvalo[1]= event->mval[1];
+                       
+                       /* prevent dragging image outside of the window and losing it! */
+                       CLAMP(snode->xof, nvm->xmin, nvm->xmax);
+                       CLAMP(snode->yof, nvm->ymin, nvm->ymax);
                        
-                       if(node->flag & NODE_HIDDEN) {
-                               node->miniwidth= nsw->oldwidth + mx - nsw->mxstart;
-                               CLAMP(node->miniwidth, 0.0f, 100.0f);
-                       }
-                       else {
-                               node->width= nsw->oldwidth + mx - nsw->mxstart;
-                               CLAMP(node->width, node->typeinfo->minwidth, node->typeinfo->maxwidth);
-                       }
-                               
                        ED_region_tag_redraw(ar);
-
+                       
                        break;
                        
                case LEFTMOUSE:
                case MIDDLEMOUSE:
                case RIGHTMOUSE:
                        
-                       MEM_freeN(nsw);
+                       MEM_freeN(nvm);
                        op->customdata= NULL;
+            
+                       WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE, NULL);
                        
                        return OPERATOR_FINISHED;
        }
@@ -1195,246 +1033,451 @@ static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       SpaceNode *snode= CTX_wm_space_node(C);
        ARegion *ar= CTX_wm_region(C);
-       bNode *node= editnode_get_active(snode->edittree);
+       NodeViewMove *nvm;
+       Image *ima;
+       ImBuf *ibuf;
+       int pad= 10;
+       void *lock;
        
-       if(node) {
-               rctf totr;
-               
-               UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
-                                                                &snode->mx, &snode->my);
-               totr= node->totr;
-               totr.xmin= totr.xmax-10.0f;
-               totr.ymax= totr.ymin+10.0f;
-               
-               if(BLI_in_rctf(&totr, snode->mx, snode->my)) {
-                       NodeSizeWidget *nsw= MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
-                       
-                       op->customdata= nsw;
-                       nsw->mxstart= snode->mx;
-                       
-                       /* store old */
-                       if(node->flag & NODE_HIDDEN)
-                               nsw->oldwidth= node->miniwidth;
-                       else
-                               nsw->oldwidth= node->width;
-                       
-                       /* add modal handler */
-                       WM_event_add_modal_handler(C, op);
-
-                       return OPERATOR_RUNNING_MODAL;
-               }
+       ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
+       ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
+       
+       if(ibuf == NULL) {
+               BKE_image_release_ibuf(ima, lock);
+               return OPERATOR_CANCELLED;
        }
-       return OPERATOR_PASS_THROUGH;
+
+       nvm= MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
+       op->customdata= nvm;
+       nvm->mvalo[0]= event->mval[0];
+       nvm->mvalo[1]= event->mval[1];
+
+       nvm->xmin = -(ar->winx/2) - ibuf->x/2 + pad;
+       nvm->xmax = ar->winx/2 + ibuf->x/2 - pad;
+       nvm->ymin = -(ar->winy/2) - ibuf->y/2 + pad;
+       nvm->ymax = ar->winy/2 + ibuf->y/2 - pad;
+
+       BKE_image_release_ibuf(ima, lock);
+       
+       /* add modal handler */
+       WM_event_add_modal_handler(C, op);
+       
+       return OPERATOR_RUNNING_MODAL;
 }
 
-void NODE_OT_resize(wmOperatorType *ot)
+
+void NODE_OT_backimage_move(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Resize Node";
-       ot->idname= "NODE_OT_resize";
+       ot->name= "Background Image Move";
+       ot->description = "Move Node backdrop";
+       ot->idname= "NODE_OT_backimage_move";
        
        /* api callbacks */
-       ot->invoke= node_resize_invoke;
-       ot->modal= node_resize_modal;
-       ot->poll= ED_operator_node_active;
+       ot->invoke= snode_bg_viewmove_invoke;
+       ot->modal= snode_bg_viewmove_modal;
+       ot->poll= composite_node_active;
        
        /* flags */
        ot->flag= OPTYPE_BLOCKING;
 }
 
+static int backimage_zoom(bContext *C, wmOperator *op)
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       float fac= RNA_float_get(op->ptr, "factor");
 
-#if 0
+       snode->zoom *= fac;
+       ED_region_tag_redraw(ar);
 
-/* ******************** rename ******************* */
+       return OPERATOR_FINISHED;
+}
 
 
-/* should go through RNA */
-void node_rename(SpaceNode *snode)
+void NODE_OT_backimage_zoom(wmOperatorType *ot)
 {
-       bNode *node, *rename_node;
-       short found_node= 0;
-
-       /* check if a node is selected */
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
-               if(node->flag & SELECT) {
-                       found_node= 1;
-                       break;
-               }
-       }
-
-       if(found_node) {
-               rename_node= nodeGetActive(snode->edittree);
-               node_rename_but((char *)rename_node->name);
        
-               // allqueue(REDRAWNODE, 1);
-       }
+       /* identifiers */
+       ot->name= "Background Image Zoom";
+       ot->idname= "NODE_OT_backimage_zoom";
+       
+       /* api callbacks */
+       ot->exec= backimage_zoom;
+       ot->poll= composite_node_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_BLOCKING;
+
+       /* internal */
+       RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
 }
 
-/* ********************** select ******************** */
+/******************** sample backdrop operator ********************/
+
+typedef struct ImageSampleInfo {
+       ARegionType *art;
+       void *draw_handle;
+       int x, y;
+       int channels;
+       int color_manage;
+
+       char col[4];
+       float colf[4];
+
+       int draw;
+} ImageSampleInfo;
 
-/* used in buttons to check context, also checks for edited groups */
-bNode *editnode_get_active_idnode(bNodeTree *ntree, short id_code)
+static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
 {
-       return nodeGetActiveID(ntree, id_code);
+       ImageSampleInfo *info= arg_info;
+
+       draw_nodespace_color_info(ar, info->channels, info->x, info->y, info->col, info->colf);
 }
 
-/* used in buttons to check context, also checks for edited groups */
-Material *editnode_get_active_material(Material *ma)
+static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
 {
-       if(ma && ma->use_nodes && ma->nodetree) {
-               bNode *node= editnode_get_active_idnode(ma->nodetree, ID_MA);
-               if(node)
-                       return (Material *)node->id;
-               else
-                       return NULL;
-       }
-       return ma;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       ImageSampleInfo *info= op->customdata;
+       void *lock;
+       Image *ima;
+       ImBuf *ibuf;
+       float fx, fy, bufx, bufy;
+       int mx, my;
+       
+       ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
+       ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
+       if(!ibuf)
+               return;
+       
+       if(!ibuf->rect) {
+               if(info->color_manage)
+                       ibuf->profile = IB_PROFILE_LINEAR_RGB;
+               else
+                       ibuf->profile = IB_PROFILE_NONE;
+               IMB_rect_from_float(ibuf);
+       }
+
+       mx= event->x - ar->winrct.xmin;
+       my= event->y - ar->winrct.ymin;
+       /* map the mouse coords to the backdrop image space */
+       bufx = ibuf->x * snode->zoom;
+       bufy = ibuf->y * snode->zoom;
+       fx = (bufx > 0.0f ? ((float)mx - 0.5f*ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
+       fy = (bufy > 0.0f ? ((float)my - 0.5f*ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
+
+       if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
+               float *fp;
+               char *cp;
+               int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
+
+               CLAMP(x, 0, ibuf->x-1);
+               CLAMP(y, 0, ibuf->y-1);
+
+               info->x= x;
+               info->y= y;
+               info->draw= 1;
+               info->channels= ibuf->channels;
+
+               if(ibuf->rect) {
+                       cp= (char *)(ibuf->rect + y*ibuf->x + x);
+
+                       info->col[0]= cp[0];
+                       info->col[1]= cp[1];
+                       info->col[2]= cp[2];
+                       info->col[3]= cp[3];
+
+                       info->colf[0]= (float)cp[0]/255.0f;
+                       info->colf[1]= (float)cp[1]/255.0f;
+                       info->colf[2]= (float)cp[2]/255.0f;
+                       info->colf[3]= (float)cp[3]/255.0f;
+               }
+               if(ibuf->rect_float) {
+                       fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
+
+                       info->colf[0]= fp[0];
+                       info->colf[1]= fp[1];
+                       info->colf[2]= fp[2];
+                       info->colf[3]= fp[3];
+               }
+       }
+       else
+               info->draw= 0;
+
+       BKE_image_release_ibuf(ima, lock);
+       
+       ED_area_tag_redraw(CTX_wm_area(C));
 }
-#endif /* 0 */
 
+static void sample_exit(bContext *C, wmOperator *op)
+{
+       ImageSampleInfo *info= op->customdata;
 
-/* no undo here! */
-void node_deselectall(SpaceNode *snode)
+       ED_region_draw_cb_exit(info->art, info->draw_handle);
+       ED_area_tag_redraw(CTX_wm_area(C));
+       MEM_freeN(info);
+}
+
+static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       bNode *node;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       ImageSampleInfo *info;
+
+       if(snode->treetype!=NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
+               return OPERATOR_CANCELLED;
        
-       for(node= snode->edittree->nodes.first; node; node= node->next)
-               node->flag &= ~SELECT;
+       info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
+       info->art= ar->type;
+       info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
+       op->customdata= info;
+
+       sample_apply(C, op, event);
+
+       WM_event_add_modal_handler(C, op);
+
+       return OPERATOR_RUNNING_MODAL;
 }
 
-int node_has_hidden_sockets(bNode *node)
+static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-       bNodeSocket *sock;
+       switch(event->type) {
+               case LEFTMOUSE:
+               case RIGHTMOUSE: // XXX hardcoded
+                       sample_exit(C, op);
+                       return OPERATOR_CANCELLED;
+               case MOUSEMOVE:
+                       sample_apply(C, op, event);
+                       break;
+       }
+
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static int sample_cancel(bContext *C, wmOperator *op)
+{
+       sample_exit(C, op);
+       return OPERATOR_CANCELLED;
+}
+
+void NODE_OT_backimage_sample(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Backimage Sample";
+       ot->idname= "NODE_OT_backimage_sample";
        
-       for(sock= node->inputs.first; sock; sock= sock->next)
-               if(sock->flag & SOCK_HIDDEN)
-                       return 1;
-       for(sock= node->outputs.first; sock; sock= sock->next)
-               if(sock->flag & SOCK_HIDDEN)
-                       return 1;
-       return 0;
+       /* api callbacks */
+       ot->invoke= sample_invoke;
+       ot->modal= sample_modal;
+       ot->cancel= sample_cancel;
+       ot->poll= ED_operator_node_active;
+
+       /* flags */
+       ot->flag= OPTYPE_BLOCKING;
 }
 
+/* ********************** size widget operator ******************** */
+
+typedef struct NodeSizeWidget {
+       float mxstart;
+       float oldwidth;
+} NodeSizeWidget;
 
-static void node_hide_unhide_sockets(SpaceNode *snode, bNode *node)
+static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-       bNodeSocket *sock;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       bNode *node= editnode_get_active(snode->edittree);
+       NodeSizeWidget *nsw= op->customdata;
+       float mx, my;
        
-       /* unhide all */
-       if( node_has_hidden_sockets(node) ) {
-               for(sock= node->inputs.first; sock; sock= sock->next)
-                       sock->flag &= ~SOCK_HIDDEN;
-               for(sock= node->outputs.first; sock; sock= sock->next)
-                       sock->flag &= ~SOCK_HIDDEN;
-       }
-       else {
-               bNode *gnode= node_tree_get_editgroup(snode->nodetree);
-               
-               /* hiding inside group should not break links in other group users */
-               if(gnode) {
-                       nodeGroupSocketUseFlags((bNodeTree *)gnode->id);
-                       for(sock= node->inputs.first; sock; sock= sock->next)
-                               if(!(sock->flag & SOCK_IN_USE))
-                                       if(sock->link==NULL)
-                                               sock->flag |= SOCK_HIDDEN;
-                       for(sock= node->outputs.first; sock; sock= sock->next)
-                               if(!(sock->flag & SOCK_IN_USE))
-                                       if(nodeCountSocketLinks(snode->edittree, sock)==0)
-                                               sock->flag |= SOCK_HIDDEN;
-               }
-               else {
-                       /* hide unused sockets */
-                       for(sock= node->inputs.first; sock; sock= sock->next) {
-                               if(sock->link==NULL)
-                                       sock->flag |= SOCK_HIDDEN;
-                       }
-                       for(sock= node->outputs.first; sock; sock= sock->next) {
-                               if(nodeCountSocketLinks(snode->edittree, sock)==0)
-                                       sock->flag |= SOCK_HIDDEN;
+       switch (event->type) {
+               case MOUSEMOVE:
+                       
+                       UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
+                                                                        &mx, &my);
+                       
+                       if (node) {
+                               if(node->flag & NODE_HIDDEN) {
+                                       node->miniwidth= nsw->oldwidth + mx - nsw->mxstart;
+                                       CLAMP(node->miniwidth, 0.0f, 100.0f);
+                               }
+                               else {
+                                       node->width= nsw->oldwidth + mx - nsw->mxstart;
+                                       CLAMP(node->width, node->typeinfo->minwidth, node->typeinfo->maxwidth);
+                               }
                        }
-               }
+                               
+                       ED_region_tag_redraw(ar);
+
+                       break;
+                       
+               case LEFTMOUSE:
+               case MIDDLEMOUSE:
+               case RIGHTMOUSE:
+                       
+                       MEM_freeN(nsw);
+                       op->customdata= NULL;
+                       
+                       return OPERATOR_FINISHED;
        }
+       
+       return OPERATOR_RUNNING_MODAL;
+}
 
-       // allqueue(REDRAWNODE, 1);
-       node_tree_verify_groups(snode->nodetree);
+static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       bNode *node= editnode_get_active(snode->edittree);
+       
+       if(node) {
+               rctf totr;
+               
+               /* convert mouse coordinates to v2d space */
+               UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
+                                                                &snode->mx, &snode->my);
+               
+               /* rect we're interested in is just the bottom right corner */
+               totr= node->totr;
+               totr.xmin= totr.xmax-10.0f;
+               totr.ymax= totr.ymin+10.0f;
+               
+               if(BLI_in_rctf(&totr, snode->mx, snode->my)) {
+                       NodeSizeWidget *nsw= MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
+                       
+                       op->customdata= nsw;
+                       nsw->mxstart= snode->mx;
+                       
+                       /* store old */
+                       if(node->flag & NODE_HIDDEN)
+                               nsw->oldwidth= node->miniwidth;
+                       else
+                               nsw->oldwidth= node->width;
+                       
+                       /* add modal handler */
+                       WM_event_add_modal_handler(C, op);
 
+                       return OPERATOR_RUNNING_MODAL;
+               }
+       }
+       return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
 }
 
-/*static*/ int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
+void NODE_OT_resize(wmOperatorType *ot)
 {
-       rctf totr= node->totr;
+       /* identifiers */
+       ot->name= "Resize Node";
+       ot->idname= "NODE_OT_resize";
        
-       totr.ymin= totr.ymax-20.0f;
+       /* api callbacks */
+       ot->invoke= node_resize_invoke;
+       ot->modal= node_resize_modal;
+       ot->poll= ED_operator_node_active;
        
-       totr.xmax= totr.xmin+15.0f;
-       if(BLI_in_rctf(&totr, mx, my)) {
-               node->flag |= NODE_HIDDEN;
-               // allqueue(REDRAWNODE, 0);
-               return 1;
-       }       
-       
-       totr.xmax= node->totr.xmax;
-       totr.xmin= totr.xmax-18.0f;
-       if(node->typeinfo->flag & NODE_PREVIEW) {
-               if(BLI_in_rctf(&totr, mx, my)) {
-                       node->flag ^= NODE_PREVIEW;
-                       // allqueue(REDRAWNODE, 0);
-                       return 1;
-               }
-               totr.xmin-=18.0f;
+       /* flags */
+       ot->flag= OPTYPE_BLOCKING;
+}
+
+/* ********************** select ******************** */
+
+
+/* no undo here! */
+void node_deselectall(SpaceNode *snode)
+{
+       bNode *node;
+       
+       for(node= snode->edittree->nodes.first; node; node= node->next)
+               node->flag &= ~SELECT;
+}
+
+/* return 1 if we need redraw otherwise zero. */
+int node_select_same_type(SpaceNode *snode)
+{
+       bNode *nac, *p;
+       int redraw;
+
+       /* search for the active node. */
+       for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
+               if (nac->flag & SELECT)
+                       break;
        }
-       if(node->type == NODE_GROUP) {
-               if(BLI_in_rctf(&totr, mx, my)) {
-                       snode_make_group_editable(snode, node);
-                       return 1;
+
+       /* no active node, return. */
+       if (!nac)
+               return(0);
+
+       redraw= 0;
+       for (p= snode->edittree->nodes.first; p; p= p->next) {
+               if (p->type != nac->type && p->flag & SELECT) {
+                       /* if it's selected but different type, unselect */
+                       redraw= 1;
+                       p->flag &= ~SELECT;
                }
-               totr.xmin-=18.0f;
-       }
-       if(node->typeinfo->flag & NODE_OPTIONS) {
-               if(BLI_in_rctf(&totr, mx, my)) {
-                       node->flag ^= NODE_OPTIONS;
-                       // allqueue(REDRAWNODE, 0);
-                       return 1;
+               else if (p->type == nac->type && (!(p->flag & SELECT))) {
+                       /* if it's the same type and is not selected, select! */
+                       redraw= 1;
+                       p->flag |= SELECT;
                }
-               totr.xmin-=18.0f;
        }
-       /* hide unused sockets */
-       if(BLI_in_rctf(&totr, mx, my)) {
-               node_hide_unhide_sockets(snode, node);
+       return(redraw);
+}
+
+/* return 1 if we need redraw, otherwise zero.
+ * dir can be 0 == next or 0 != prev.
+ */
+int node_select_same_type_np(SpaceNode *snode, int dir)
+{
+       bNode *nac, *p;
+
+       /* search the active one. */
+       for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
+               if (nac->flag & SELECT)
+                       break;
        }
-       
-       
-       totr= node->totr;
-       totr.xmin= totr.xmax-10.0f;
-       totr.ymax= totr.ymin+10.0f;
-       if(BLI_in_rctf(&totr, mx, my)) {
-//             scale_node(snode, node);
-               return 1;
+
+       /* no active node, return. */
+       if (!nac)
+               return(0);
+
+       if (dir == 0)
+               p= nac->next;
+       else
+               p= nac->prev;
+
+       while (p) {
+               /* Now search the next with the same type. */
+               if (p->type == nac->type)
+                       break;
+
+               if (dir == 0)
+                       p= p->next;
+               else
+                       p= p->prev;
        }
-       return 0;
+
+       if (p) {
+               node_deselectall(snode);
+               p->flag |= SELECT;
+               return(1);
+       }
+       return(0);
 }
 
-/*static*/ int do_header_hidden_node(SpaceNode *snode, bNode *node, float mx, float my)
+int node_has_hidden_sockets(bNode *node)
 {
-       rctf totr= node->totr;
-       
-       totr.xmax= totr.xmin+15.0f;
-       if(BLI_in_rctf(&totr, mx, my)) {
-               node->flag &= ~NODE_HIDDEN;
-               // allqueue(REDRAWNODE, 0);
-               return 1;
-       }       
+       bNodeSocket *sock;
        
-       totr.xmax= node->totr.xmax;
-       totr.xmin= node->totr.xmax-15.0f;
-       if(BLI_in_rctf(&totr, mx, my)) {
-//             scale_node(snode, node);
-               return 1;
-       }
+       for(sock= node->inputs.first; sock; sock= sock->next)
+               if(sock->flag & SOCK_HIDDEN)
+                       return 1;
+       for(sock= node->outputs.first; sock; sock= sock->next)
+               if(sock->flag & SOCK_HIDDEN)
+                       return 1;
        return 0;
 }
 
@@ -1453,35 +1496,103 @@ static void node_link_viewer(SpaceNode *snode, bNode *tonode)
                if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
                        if(node->flag & NODE_DO_OUTPUT)
                                break;
+       /* no viewer, we make one active */
+       if(node==NULL) {
+               for(node= snode->edittree->nodes.first; node; node= node->next) {
+                       if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
+                               node->flag |= NODE_DO_OUTPUT;
+                               break;
+                       }
+               }
+       }
                
        if(node) {
                bNodeLink *link;
-               
-               /* get link to viewer */
+               bNodeSocket *sock= NULL;
+
+               /* try to find an already connected socket to cycle to the next */
                for(link= snode->edittree->links.first; link; link= link->next)
-                       if(link->tonode==node)
-                               break;
+                       if(link->tonode==node && link->fromnode==tonode)
+                               if(link->tosock==node->inputs.first)
+                                       break;
 
                if(link) {
-                       link->fromnode= tonode;
-                       link->fromsock= tonode->outputs.first;
-                       NodeTagChanged(snode->edittree, node);
+                       /* unlink existing connection */
+                       sock= link->fromsock;
+                       nodeRemLink(snode->edittree, link);
+
+                       /* find a socket after the previously connected socket */
+                       for(sock=sock->next; sock; sock= sock->next)
+                               if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
+                                       break;
+               }
+
+               /* find a socket starting from the first socket */
+               if(!sock) {
+                       for(sock= tonode->outputs.first; sock; sock= sock->next)
+                               if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
+                                       break;
+               }
+               
+               if(sock) {
+                       /* get link to viewer */
+                       for(link= snode->edittree->links.first; link; link= link->next)
+                               if(link->tonode==node && link->tosock==node->inputs.first)
+                                       break;
                        
-// XXX                 snode_handle_recalc(snode);
+                       if(link==NULL) {
+                               nodeAddLink(snode->edittree, tonode, sock, node, node->inputs.first);
+                       }
+                       else {
+                               link->fromnode= tonode;
+                               link->fromsock= sock;
+                       }
+                       ntreeSolveOrder(snode->edittree);
+                       NodeTagChanged(snode->edittree, node);
                }
        }
 }
 
 
-void node_active_link_viewer(SpaceNode *snode)
+static int node_active_link_viewer(bContext *C, wmOperator *UNUSED(op))
 {
-       bNode *node= editnode_get_active(snode->edittree);
-       if(node)
-               node_link_viewer(snode, node);
+       SpaceNode *snode= CTX_wm_space_node(C);
+       bNode *node;
+       
+       node= editnode_get_active(snode->edittree);
+       
+       if(!node)
+               return OPERATOR_CANCELLED;
+
+       ED_preview_kill_jobs(C);
+
+       node_link_viewer(snode, node);
+       snode_notify(C, snode);
+
+       return OPERATOR_FINISHED;
+}
+
+
+
+void NODE_OT_link_viewer(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Link to Viewer Node";
+       ot->description = "Link to Viewer Node";
+       ot->idname= "NODE_OT_link_viewer";
+       
+       /* api callbacks */
+       ot->exec= node_active_link_viewer;
+       ot->poll= ED_operator_node_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+
+
 /* return 0, nothing done */
-/*static*/ int node_mouse_groupheader(SpaceNode *snode)
+static int node_mouse_groupheader(SpaceNode *snode)
 {
        bNode *gnode;
        float mx=0, my=0;
@@ -1519,10 +1630,10 @@ static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **
        /* check if we click in a socket */
        for(node= snode->edittree->nodes.first; node; node= node->next) {
                
-               rect.xmin = snode->mx - NODE_SOCKSIZE+3;
-               rect.ymin = snode->my - NODE_SOCKSIZE+3;
-               rect.xmax = rect.xmin + 2*NODE_SOCKSIZE+6;
-               rect.ymax = rect.ymin + 2*NODE_SOCKSIZE+6;
+               rect.xmin = snode->mx - (NODE_SOCKSIZE+4);
+               rect.ymin = snode->my - (NODE_SOCKSIZE+4);
+               rect.xmax = snode->mx + (NODE_SOCKSIZE+4);
+               rect.ymax = snode->my + (NODE_SOCKSIZE+4);
                
                if (!(node->flag & NODE_HIDDEN)) {
                        /* extra padding inside and out - allow dragging on the text areas too */
@@ -1562,6 +1673,33 @@ static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **
                        }
                }
        }
+       
+       /* check group sockets
+        * NB: using ngroup->outputs as input sockets and vice versa here!
+        */
+       if(in_out & SOCK_IN) {
+               for(sock= snode->edittree->outputs.first; sock; sock= sock->next) {
+                       if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
+                               if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
+                                       *nodep= NULL;   /* NULL node pointer indicates group socket */
+                                       *sockp= sock;
+                                       return 1;
+                               }
+                       }
+               }
+       }
+       if(in_out & SOCK_OUT) {
+               for(sock= snode->edittree->inputs.first; sock; sock= sock->next) {
+                       if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
+                               if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
+                                       *nodep= NULL;   /* NULL node pointer indicates group socket */
+                                       *sockp= sock;
+                                       return 1;
+                               }
+                       }
+               }
+       }
+       
        return 0;
 }
 
@@ -1573,7 +1711,7 @@ static int node_socket_hilights(SpaceNode *snode, int in_out)
        
        if(snode->edittree==NULL) return 0;
        
-       /* deselect socks */
+       /* deselect sockets */
        for(node= snode->edittree->nodes.first; node; node= node->next) {
                for(sock= node->inputs.first; sock; sock= sock->next) {
                        if(sock->flag & SELECT) {
@@ -1602,60 +1740,171 @@ static int node_socket_hilights(SpaceNode *snode, int in_out)
        return redraw;
 }
 
+static int outside_group_rect(SpaceNode *snode)
+{
+       bNode *gnode= node_tree_get_editgroup(snode->nodetree);
+       if (gnode) {
+               return (snode->mx < gnode->totr.xmin || snode->mx >= gnode->totr.xmax
+                               || snode->my < gnode->totr.ymin || snode->my >= gnode->totr.ymax);
+       }
+       return 0;
+}
+
 /* ****************** Add *********************** */
 
-void snode_autoconnect(SpaceNode *snode, bNode *node_to, int flag)
-{
-       bNodeSocket *sock, *sockfrom[8];
-       bNode *node, *nodefrom[8];
-       int totsock= 0, socktype=0;
 
-       if(node_to==NULL || node_to->inputs.first==NULL)
-               return;
+typedef struct bNodeListItem {
+       struct bNodeListItem *next, *prev;
+       struct bNode *node;     
+} bNodeListItem;
+
+static int sort_nodes_locx(void *a, void *b)
+{
+       bNodeListItem *nli1 = (bNodeListItem *)a;
+       bNodeListItem *nli2 = (bNodeListItem *)b;
+       bNode *node1 = nli1->node;
+       bNode *node2 = nli2->node;
        
-       /* no inputs for node allowed (code it) */
+       if (node1->locx > node2->locx)
+               return 1;
+       else 
+               return 0;
+}
 
-       /* connect first 1 socket type now */
-       for(sock= node_to->inputs.first; sock; sock= sock->next)
-               if(socktype<sock->type)
-                       socktype= sock->type;
+static int socket_is_available(bNodeTree *ntree, bNodeSocket *sock, int allow_used)
+{
+       if (sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))
+               return 0;
+       
+       if (!allow_used) {
+               if (nodeCountSocketLinks(ntree, sock) > 0)
+                       return 0;
+       }
+       return 1;
+}
 
+static bNodeSocket *best_socket_output(bNodeTree *ntree, bNode *node, bNodeSocket *sock_target, int allow_multiple)
+{
+       bNodeSocket *sock;
        
-       /* find potential sockets, max 8 should work */
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
-               if((node->flag & flag) && node!=node_to) {
-                       for(sock= node->outputs.first; sock; sock= sock->next) {
-                               if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
-                                       sockfrom[totsock]= sock;
-                                       nodefrom[totsock]= node;
-                                       totsock++;
-                                       if(totsock>7)
-                                               break;
-                               }
-                       }
+       /* first try to find a socket with a matching name */
+       for (sock=node->outputs.first; sock; sock=sock->next) {
+
+               if (!socket_is_available(ntree, sock, allow_multiple))
+                       continue;
+
+               /* check for same types */
+               if (sock->type == sock_target->type) {
+                       if (strcmp(sock->name, sock_target->name)==0)
+                               return sock;
                }
-               if(totsock>7)
-                       break;
        }
+       
+       /* otherwise settle for the first available socket of the right type */
+       for (sock=node->outputs.first; sock; sock=sock->next) {
 
-       /* now just get matching socket types and create links */
-       for(sock= node_to->inputs.first; sock; sock= sock->next) {
-               int a;
+               if (!socket_is_available(ntree, sock, allow_multiple))
+                       continue;
                
-               for(a=0; a<totsock; a++) {
-                       if(sockfrom[a]) {
-                               if(sock->type==sockfrom[a]->type && sock->type==socktype) {
-                                       nodeAddLink(snode->edittree, nodefrom[a], sockfrom[a], node_to, sock);
-                                       sockfrom[a]= NULL;
-                                       break;
-                               }
+               /* check for same types */
+               if (sock->type == sock_target->type) {
+                       return sock;
+               }
+       }
+       
+       return NULL;
+}
+
+/* this is a bit complicated, but designed to prioritise finding 
+ * sockets of higher types, such as image, first */
+static bNodeSocket *best_socket_input(bNodeTree *ntree, bNode *node, int num, int replace)
+{
+       bNodeSocket *sock;
+       int socktype, maxtype=0;
+       int a = 0;
+       
+       for (sock=node->inputs.first; sock; sock=sock->next) {
+               maxtype = MAX2(sock->type, maxtype);
+       }
+       
+       /* find sockets of higher 'types' first (i.e. image) */
+       for (socktype=maxtype; socktype >= 0; socktype--) {
+               for (sock=node->inputs.first; sock; sock=sock->next) {
+                       
+                       if (!socket_is_available(ntree, sock, replace)) {
+                               a++;
+                               continue;
+                       }
+                               
+                       if (sock->type == socktype) {
+                               /* increment to make sure we don't keep finding 
+                                * the same socket on every attempt running this function */
+                               a++;
+                               if (a > num)
+                                       return sock;
                        }
                }
        }
        
-       ntreeSolveOrder(snode->edittree);
+       return NULL;
 }
 
+void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
+{
+       ListBase *nodelist = MEM_callocN(sizeof(ListBase), "items_list");
+       bNodeListItem *nli;
+       bNode *node;
+       int i, numlinks=0;
+       
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & NODE_SELECT) {
+                       nli = MEM_mallocN(sizeof(bNodeListItem), "temporary node list item");
+                       nli->node = node;
+                       BLI_addtail(nodelist, nli);
+               }
+       }
+       
+       /* sort nodes left to right */
+       BLI_sortlist(nodelist, sort_nodes_locx);
+       
+       for (nli=nodelist->first; nli; nli=nli->next) {
+               bNode *node_fr, *node_to;
+               bNodeSocket *sock_fr, *sock_to;
+               
+               if (nli->next == NULL) break;
+               
+               node_fr = nli->node;
+               node_to = nli->next->node;
+               
+               /* check over input sockets first */
+               for (i=0; i<BLI_countlist(&node_to->inputs); i++) {
+                       
+                       /* find the best guess input socket */
+                       sock_to = best_socket_input(snode->edittree, node_to, i, replace);
+                       if (!sock_to) continue;
+                       
+                       /* check for an appropriate output socket to connect from */
+                       sock_fr = best_socket_output(snode->edittree, node_fr, sock_to, allow_multiple);
+                       if (!sock_fr) continue;
+                       
+                       /* then we can connect */
+                       if (replace)
+                               nodeRemSocketLinks(snode->edittree, sock_to);
+                       nodeAddLink(snode->edittree, node_fr, sock_fr, node_to, sock_to);
+                       NodeTagChanged(snode->edittree, node_to);
+                       ++numlinks;
+                       break;
+               }
+       }
+       
+       if (numlinks > 0) {
+               node_tree_verify_groups(snode->nodetree);
+               ntreeSolveOrder(snode->edittree);
+       }
+       
+       BLI_freelistN(nodelist);
+       MEM_freeN(nodelist);
+}
 
 /* can be called from menus too, but they should do own undopush and redraws */
 bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float locy)
@@ -1684,7 +1933,7 @@ bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float
        /* generics */
        if(node) {
                node->locx= locx;
-               node->locy= locy + 60.0f;               // arbitrary.. so its visible
+               node->locy= locy + 60.0f;               // arbitrary.. so its visible, (0,0) is top of node
                node->flag |= SELECT;
                
                gnode= node_tree_get_editgroup(snode->nodetree);
@@ -1697,7 +1946,7 @@ bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float
                node_set_active(snode, node);
                
                if(snode->nodetree->type==NTREE_COMPOSIT) {
-                       if(ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE))
+                       if(ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE))
                                node->id = &scene->id;
                        
                        ntreeCompositForceHidden(snode->edittree, scene);
@@ -1716,114 +1965,62 @@ bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float
        return node;
 }
 
-#if 0
+/* ****************** Duplicate *********************** */
 
-void node_mute(SpaceNode *snode)
+static int node_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       bNode *node;
-
-       /* no disabling inside of groups */
-       if(node_tree_get_editgroup(snode->nodetree))
-               return;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       bNodeTree *ntree= snode->edittree;
+       bNode *node, *newnode, *last;
        
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
+       ED_preview_kill_jobs(C);
+       
+       last = ntree->nodes.last;
+       for(node= ntree->nodes.first; node; node= node->next) {
                if(node->flag & SELECT) {
-                       if(node->inputs.first && node->outputs.first) {
-                               if(node->flag & NODE_MUTED)
-                                       node->flag &= ~NODE_MUTED;
-                               else
-                                       node->flag |= NODE_MUTED;
+                       newnode = nodeCopyNode(ntree, node);
+                       
+                       /* deselect old node, select the copy instead */
+                       node->flag &= ~(NODE_SELECT|NODE_ACTIVE);
+                       newnode->flag |= NODE_SELECT;
+                       
+                       if(newnode->id) {
+                               /* simple id user adjustment, node internal functions dont touch this
+                                * but operators and readfile.c do. */
+                               id_us_plus(newnode->id);
+                               /* to ensure redraws or rerenders happen */
+                               ED_node_changed_update(snode->id, newnode);
                        }
                }
+               
+               /* make sure we don't copy new nodes again! */
+               if (node==last)
+                       break;
        }
        
-
-}
-
-#endif
-
-int node_duplicate_exec(bContext *C, wmOperator *op)
-{
-       SpaceNode *snode= CTX_wm_space_node(C);
-       
-       ntreeCopyTree(snode->edittree, 1);      /* 1 == internally selected nodes */
+       ntreeSolveOrder(ntree);
        
-       ntreeSolveOrder(snode->edittree);
        node_tree_verify_groups(snode->nodetree);
-       snode_handle_recalc(C, snode);
-
-       return OPERATOR_FINISHED;
-}
+       snode_notify(C, snode);
 
-static int node_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
-{
-       node_duplicate_exec(C, op);
-       
-       RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
-       WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
-       
        return OPERATOR_FINISHED;
 }
 
 void NODE_OT_duplicate(wmOperatorType *ot)
 {
-       
        /* identifiers */
        ot->name= "Duplicate Nodes";
-       ot->description = "Duplicate the nodes.";
+       ot->description = "Duplicate the nodes";
        ot->idname= "NODE_OT_duplicate";
        
        /* api callbacks */
-       ot->invoke= node_duplicate_invoke;
        ot->exec= node_duplicate_exec;
-       
        ot->poll= ED_operator_node_active;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
-       /* to give to transform */
-       RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
-}
-
-#if 0
-
-static void node_insert_convertor(SpaceNode *snode, bNodeLink *link)
-{
-       bNode *newnode= NULL;
-       
-       if(link->fromsock->type==SOCK_RGBA && link->tosock->type==SOCK_VALUE) {
-               if(snode->edittree->type==NTREE_SHADER)
-                       newnode= node_add_node(snode, SH_NODE_RGBTOBW, 0.0f, 0.0f);
-               else if(snode->edittree->type==NTREE_COMPOSIT)
-                       newnode= node_add_node(snode, CMP_NODE_RGBTOBW, 0.0f, 0.0f);
-               else
-                       newnode= NULL;
-       }
-       else if(link->fromsock->type==SOCK_VALUE && link->tosock->type==SOCK_RGBA) {
-               if(snode->edittree->type==NTREE_SHADER)
-                       newnode= node_add_node(snode, SH_NODE_VALTORGB, 0.0f, 0.0f);
-               else if(snode->edittree->type==NTREE_COMPOSIT)
-                       newnode= node_add_node(snode, CMP_NODE_VALTORGB, 0.0f, 0.0f);
-               else
-                       newnode= NULL;
-       }
-       
-       if(newnode) {
-               /* dangerous assumption to use first in/out socks, but thats fine for now */
-               newnode->flag |= NODE_HIDDEN;
-               newnode->locx= 0.5f*(link->fromsock->locx + link->tosock->locx);
-               newnode->locy= 0.5f*(link->fromsock->locy + link->tosock->locy) + HIDDEN_RAD;
-               
-               nodeAddLink(snode->edittree, newnode, newnode->outputs.first, link->tonode, link->tosock);
-               link->tonode= newnode;
-               link->tosock= newnode->inputs.first;
-       }
 }
 
-
-#endif /* 0 */
-
 /* *************************** add link op ******************** */
 
 /* temp data to pass on to modal */
@@ -1835,24 +2032,6 @@ typedef struct NodeLinkDrag
        int in_out;
 } NodeLinkDrag;
 
-/*static*/ void reset_sel_socket(SpaceNode *snode, int in_out)
-{
-       bNode *node;
-       bNodeSocket *sock;
-       
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
-               if(in_out & SOCK_IN) {
-                       for(sock= node->inputs.first; sock; sock= sock->next)
-                               if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
-               }
-               if(in_out & SOCK_OUT) {
-                       for(sock= node->outputs.first; sock; sock= sock->next)
-                               if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
-               }
-       }
-}
-
-
 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
 {
        bNodeLink *tlink;
@@ -1865,19 +2044,24 @@ static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeL
                                break;
                }
                if(tlink) {
-                       /* is there a free input socket with same type? */
-                       for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
-                               if(sock->type==tlink->fromsock->type)
-                                       if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
-                                               break;
-                       }
-                       if(sock) {
-                               tlink->tosock= sock;
-                               sock->flag &= ~SOCK_HIDDEN;
+                       /* try to move the existing link to the next available socket */
+                       if (tlink->tonode) {
+                               /* is there a free input socket with same type? */
+                               for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
+                                       if(sock->type==tlink->fromsock->type)
+                                               if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
+                                                       break;
+                               }
+                               if(sock) {
+                                       tlink->tosock= sock;
+                                       sock->flag &= ~SOCK_HIDDEN;
+                               }
+                               else {
+                                       nodeRemLink(snode->edittree, tlink);
+                               }
                        }
-                       else {
+                       else
                                nodeRemLink(snode->edittree, tlink);
-                       }
                }
        }
 }
@@ -1908,7 +2092,7 @@ static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
                        if(in_out==SOCK_OUT) {
                                if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
                                        if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
-                                               if(tnode!=node  && link->tonode!=tnode && link->tosock!= tsock) {
+                                               if( link->tosock!= tsock && (!tnode || (tnode!=node && link->tonode!=tnode)) ) {
                                                        link->tonode= tnode;
                                                        link->tosock= tsock;
                                                        ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
@@ -1924,7 +2108,7 @@ static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
                                if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
                                        if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
                                                if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
-                                                       if(tnode!=node && link->fromnode!=tnode && link->fromsock!= tsock) {
+                                                       if( link->fromsock!= tsock && (!tnode || (tnode!=node && link->fromnode!=tnode)) ) {
                                                                link->fromnode= tnode;
                                                                link->fromsock= tsock;
                                                                ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
@@ -1945,23 +2129,32 @@ static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
                case LEFTMOUSE:
                case RIGHTMOUSE:
                case MIDDLEMOUSE:
-       
-                       /* remove link? */
-                       if(link->tonode==NULL || link->fromnode==NULL) {
-                               nodeRemLink(snode->edittree, link);
-                       }
-                       else {
+                       if(link->tosock && link->fromsock) {
                                /* send changed events for original tonode and new */
-                               if(link->tonode) 
+                               if(link->tonode)
                                        NodeTagChanged(snode->edittree, link->tonode);
                                
                                /* we might need to remove a link */
-                               if(in_out==SOCK_OUT) node_remove_extra_links(snode, link->tosock, link);
+                               if(in_out==SOCK_OUT)
+                                       node_remove_extra_links(snode, link->tosock, link);
+                       }
+                       else if (outside_group_rect(snode) && (link->tonode || link->fromnode)) {
+                               /* automatically add new group socket */
+                               if (link->tonode && link->tosock) {
+                                       link->fromsock = nodeGroupExposeSocket(snode->edittree, link->tosock, SOCK_IN);
+                                       link->fromnode = NULL;
+                               }
+                               else if (link->fromnode && link->fromsock) {
+                                       link->tosock = nodeGroupExposeSocket(snode->edittree, link->fromsock, SOCK_OUT);
+                                       link->tonode = NULL;
+                               }
                        }
+                       else
+                               nodeRemLink(snode->edittree, link);
                        
                        ntreeSolveOrder(snode->edittree);
                        node_tree_verify_groups(snode->nodetree);
-                       snode_handle_recalc(C, snode);
+                       snode_notify(C, snode);
                        
                        MEM_freeN(op->customdata);
                        op->customdata= NULL;
@@ -1976,7 +2169,7 @@ static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
 static int node_link_init(SpaceNode *snode, NodeLinkDrag *nldrag)
 {
        bNodeLink *link;
-       
+
        /* output indicated? */
        if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
                if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
@@ -2030,6 +2223,8 @@ static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
        UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
                                                         &snode->mx, &snode->my);
 
+       ED_preview_kill_jobs(C);
+
        nldrag->in_out= node_link_init(snode, nldrag);
                
        if(nldrag->in_out) {
@@ -2068,81 +2263,40 @@ void NODE_OT_link(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
 }
 
-
-void node_hide(SpaceNode *snode)
-{
-       bNode *node;
-       int nothidden=0, ishidden=0;
-       
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
-               if(node->flag & SELECT) {
-                       if(node->flag & NODE_HIDDEN)
-                               ishidden++;
-                       else
-                               nothidden++;
-               }
-       }
-       for(node= snode->edittree->nodes.first; node; node= node->next) {
-               if(node->flag & SELECT) {
-                       if( (ishidden && nothidden) || ishidden==0)
-                               node->flag |= NODE_HIDDEN;
-                       else 
-                               node->flag &= ~NODE_HIDDEN;
-               }
-       }
-}
-
-#if 0
-
-void node_insert_key(SpaceNode *snode)
-{
-       bNode *node= editnode_get_active(snode->edittree);
-       
-       if(node == NULL)
-               return;
-       
-       if(node->type==CMP_NODE_TIME) {
-               if(node->custom1<node->custom2) {
-
-                       CurveMapping *cumap= node->storage;
-                       float fval, curval;
-               
-                       curval= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
-                       fval= curvemapping_evaluateF(cumap, 0, curval);
-                       
-                       if(fbutton(&fval, 0.0f, 1.0f, 10, 10, "Insert Value")) {
-                               curvemap_insert(cumap->cm, curval, fval);
-
-                       }
-               }
-       }
-}
+/* ********************** Make Link operator ***************** */
 
 /* makes a link between selected output and input sockets */
-void node_make_link(SpaceNode *snode)
+static int node_make_link_exec(bContext *C, wmOperator *op)
 {
-       bNode *fromnode, *tonode;
-       bNodeLink *link;
-       bNodeSocket *outsock= snode->edittree->selout;
-       bNodeSocket *insock= snode->edittree->selin;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       int replace = RNA_boolean_get(op->ptr, "replace");
 
-       if(!insock || !outsock) return;
-       if(nodeFindLink(snode->edittree, outsock, insock)) return;
+       ED_preview_kill_jobs(C);
 
-       if(nodeFindNode(snode->edittree, outsock, &fromnode, NULL) &&
-               nodeFindNode(snode->edittree, insock, &tonode, NULL)) {
-               link= nodeAddLink(snode->edittree, fromnode, outsock, tonode, insock);
-               NodeTagChanged(snode->edittree, tonode);
-               node_remove_extra_links(snode, insock, link);
-       }
-       else return;
+       snode_autoconnect(snode, 1, replace);
 
-       ntreeSolveOrder(snode->edittree);
        node_tree_verify_groups(snode->nodetree);
-       // XXX                  snode_handle_recalc(snode);
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
 
+void NODE_OT_link_make(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Make Links";
+       ot->description= "Makes a link between selected output in input sockets";
+       ot->idname= "NODE_OT_link_make";
+       
+       /* callbacks */
+       ot->exec= node_make_link_exec;
+       ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_boolean(ot->srna, "replace", 0, "Replace", "Replace socket connections with the new links");
 }
-#endif
 
 /* ********************** Cut Link operator ***************** */
 
@@ -2155,7 +2309,7 @@ static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
        if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
 
                for(i=0; i<tot-1; i++)
-                       for(b=0; b<LINK_RESOL-1; b++)
+                       for(b=0; b<LINK_RESOL; b++)
                                if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
                                        return 1;
        }
@@ -2182,6 +2336,8 @@ static int cut_links_exec(bContext *C, wmOperator *op)
        
        if(i>1) {
                bNodeLink *link, *next;
+
+               ED_preview_kill_jobs(C);
                
                for(link= snode->edittree->links.first; link; link= next) {
                        next= link->next;
@@ -2191,15 +2347,15 @@ static int cut_links_exec(bContext *C, wmOperator *op)
                                nodeRemLink(snode->edittree, link);
                        }
                }
-
+               
                ntreeSolveOrder(snode->edittree);
                node_tree_verify_groups(snode->nodetree);
-               snode_handle_recalc(C, snode);
+               snode_notify(C, snode);
                
                return OPERATOR_FINISHED;
        }
        
-       return OPERATOR_PASS_THROUGH;;
+       return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
 }
 
 void NODE_OT_links_cut(wmOperatorType *ot)
@@ -2225,16 +2381,21 @@ void NODE_OT_links_cut(wmOperatorType *ot)
 }
 
 /* ******************************** */
+// XXX some code needing updating to operators...
 
-/* goes over all scenes, reads render layerss */
-void node_read_renderlayers(SpaceNode *snode)
+
+/* goes over all scenes, reads render layers */
+static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       Scene *curscene= NULL; // XXX
-       Scene *scene;
+       Main *bmain= CTX_data_main(C);
+       SpaceNode *snode= CTX_wm_space_node(C);
+       Scene *curscene= CTX_data_scene(C), *scene;
        bNode *node;
 
+       ED_preview_kill_jobs(C);
+
        /* first tag scenes unread */
-       for(scene= G.main->scene.first; scene; scene= scene->id.next) 
+       for(scene= bmain->scene.first; scene; scene= scene->id.next) 
                scene->id.flag |= LIB_DOIT;
 
        for(node= snode->edittree->nodes.first; node; node= node->next) {
@@ -2248,51 +2409,104 @@ void node_read_renderlayers(SpaceNode *snode)
                }
        }
        
-       // XXX                  snode_handle_recalc(snode);
+       snode_notify(C, snode);
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_read_renderlayers(wmOperatorType *ot)
+{
+       
+       ot->name= "Read Render Layers";
+       ot->idname= "NODE_OT_read_renderlayers";
+       
+       ot->exec= node_read_renderlayers_exec;
+       
+       ot->poll= composite_node_active;
+       
+       /* flags */
+       ot->flag= 0;
 }
 
-void node_read_fullsamplelayers(SpaceNode *snode)
+static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       Scene *curscene= NULL; // XXX
+       Main *bmain= CTX_data_main(C);
+       SpaceNode *snode= CTX_wm_space_node(C);
+       Scene *curscene= CTX_data_scene(C);
        Render *re= RE_NewRender(curscene->id.name);
 
        WM_cursor_wait(1);
 
-       //BIF_init_render_callbacks(re, 1);
-       RE_MergeFullSample(re, curscene, snode->nodetree);
-       //BIF_end_render_callbacks();
-       
-       // allqueue(REDRAWNODE, 1);
-       // allqueue(REDRAWIMAGE, 1);
+       RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
+       snode_notify(C, snode);
        
        WM_cursor_wait(0);
+       return OPERATOR_FINISHED;
 }
 
-void imagepaint_composite_tags(bNodeTree *ntree, Image *image, ImageUser *iuser)
+
+void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
 {
-       bNode *node;
        
-       if(ntree==NULL)
-               return;
+       ot->name= "Read Full Sample Layers";
+       ot->idname= "NODE_OT_read_fullsamplelayers";
        
-       /* search for renderresults */
-       if(image->type==IMA_TYPE_R_RESULT) {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
-                               /* imageuser comes from ImageWin, so indexes are offset 1 */
-                               if(node->custom1==iuser->layer-1)
-                                       NodeTagChanged(ntree, node);
-                       }
+       ot->exec= node_read_fullsamplelayers_exec;
+       
+       ot->poll= composite_node_active;
+       
+       /* flags */
+       ot->flag= 0;
+}
+
+int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Scene *sce= CTX_data_scene(C);
+       bNode *node;
+       
+       for(node= sce->nodetree->nodes.first; node; node= node->next) {
+               if(node->id==(ID *)sce && node->need_exec) {
+                       break;
                }
        }
-       else {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(node->id== &image->id)
-                               NodeTagChanged(ntree, node);
+       if(node) {
+               SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
+               
+               if(srl) {
+                       PointerRNA op_ptr;
+                       
+                       WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
+                       RNA_string_set(&op_ptr, "layer", srl->name);
+                       RNA_string_set(&op_ptr, "scene", sce->id.name+2);
+                       
+                       /* to keep keypositions */
+                       sce->r.scemode |= R_NO_FRAME_UPDATE;
+                       
+                       WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
+
+                       WM_operator_properties_free(&op_ptr);
+                       
+                       return OPERATOR_FINISHED;
                }
+                  
        }
+       return OPERATOR_CANCELLED;
+}
+
+void NODE_OT_render_changed(wmOperatorType *ot)
+{
+       
+       ot->name= "Render Changed Layer";
+       ot->idname= "NODE_OT_render_changed";
+       
+       ot->exec= node_render_changed_exec;
+       
+       ot->poll= composite_node_active;
+       
+       /* flags */
+       ot->flag= 0;
 }
 
+
 /* ****************** Make Group operator ******************* */
 
 static int node_group_make_exec(bContext *C, wmOperator *op)
@@ -2301,7 +2515,7 @@ static int node_group_make_exec(bContext *C, wmOperator *op)
        bNode *gnode;
        
        if(snode->edittree!=snode->nodetree) {
-               BKE_report(op->reports, RPT_ERROR, "Can not add a new Group in a Group");
+               BKE_report(op->reports, RPT_WARNING, "Can not add a new Group in a Group");
                return OPERATOR_CANCELLED;
        }
        
@@ -2312,23 +2526,27 @@ static int node_group_make_exec(bContext *C, wmOperator *op)
                                if(gnode->type==CMP_NODE_R_LAYERS)
                                        break;
                }
-
+               
                if(gnode) {
-                       BKE_report(op->reports, RPT_ERROR, "Can not add RenderLayer in a Group");
+                       BKE_report(op->reports, RPT_WARNING, "Can not add RenderLayer in a Group");
                        return OPERATOR_CANCELLED;
                }
        }
+
+       ED_preview_kill_jobs(C);
        
        gnode= nodeMakeGroupFromSelected(snode->nodetree);
        if(gnode==NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Can not make Group");
+               BKE_report(op->reports, RPT_WARNING, "Can not make Group");
                return OPERATOR_CANCELLED;
        }
        else {
                nodeSetActive(snode->nodetree, gnode);
                ntreeSolveOrder(snode->nodetree);
        }
-
+       
+       snode_notify(C, snode);
+       
        return OPERATOR_FINISHED;
 }
 
@@ -2336,7 +2554,7 @@ void NODE_OT_group_make(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name = "Group";
-       ot->description = "Make group from selected nodes.";
+       ot->description = "Make group from selected nodes";
        ot->idname = "NODE_OT_group_make";
        
        /* api callbacks */
@@ -2347,235 +2565,204 @@ void NODE_OT_group_make(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-#if 0
+/* ****************** Hide operator *********************** */
+
+static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
+{
+       int tot_eq= 0, tot_neq= 0;
+       bNode *node;
+
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
 
-/* ******************** main event loop ****************** */
+                       if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
+                               continue;
 
-void winqreadnodespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
+                       if(node->flag & toggle_flag)
+                               tot_eq++;
+                       else
+                               tot_neq++;
+               }
+       }
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
+
+                       if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
+                               continue;
+
+                       if( (tot_eq && tot_neq) || tot_eq==0)
+                               node->flag |= toggle_flag;
+                       else
+                               node->flag &= ~toggle_flag;
+               }
+       }
+}
+
+static int node_hide_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       SpaceNode *snode= spacedata;
-       bNode *actnode;
-       bNodeSocket *actsock;
-       unsigned short event= evt->event;
-       short val= evt->val, doredraw=0, fromlib= 0;
+       SpaceNode *snode= CTX_wm_space_node(C);
        
-       if(sa->win==0) return;
+       /* sanity checking (poll callback checks this already) */
+       if((snode == NULL) || (snode->edittree == NULL))
+               return OPERATOR_CANCELLED;
        
-       if(snode->nodetree==NULL) {
-               /* no other events should be handled, but floating panels still should get handled */
-               uiDoBlocks(&curarea->uiblocks, event, 1);
-               return;
-       }
+       node_flag_toggle_exec(snode, NODE_HIDDEN);
        
-       if(val) {
-               if( node_uiDoBlocks(sa, event)!=UI_NOTHING ) event= 0;
-               
-               fromlib= (snode->id && snode->id->lib);
-               
-               switch(event) {
-               case LEFTMOUSE:
-                       if(gpencil_do_paint(sa, L_MOUSE)) {
-                               return;
-                       }
-                       else if(fromlib) {
-                               if(node_mouse_groupheader(snode)==0)
-                                       //node_mouse_select(snode, event);
-                       }
-                       else {
-                               
-                               if(G.qual & LR_CTRLKEY)
-                                       if(gesture())
-                                               break;
-                                       
-//                             if(node_add_link(snode)==0)
-                                       if(node_mouse_groupheader(snode)==0)
-//                                             if(node_mouse_select(snode, event)==0)
-                                                       node_border_link_delete(snode);
-                       }
-                       break;
-                       
-               case RIGHTMOUSE: 
-                       if(gpencil_do_paint(sa, R_MOUSE)) {
-                               return;
-                       }
-                       else if(find_indicated_socket(snode, &actnode, &actsock, SOCK_IN)) {
-                               if(actsock->flag & SOCK_SEL) {
-                                       snode->edittree->selin= NULL;
-                                       actsock->flag&= ~SOCK_SEL;
-                               }
-                               else {
-                                       snode->edittree->selin= actsock;
-                                       reset_sel_socket(snode, SOCK_IN);
-                                       actsock->flag|= SOCK_SEL;
-                               }
-                       }
-                       else if(find_indicated_socket(snode, &actnode, &actsock, SOCK_OUT)) {
-                               if(actsock->flag & SOCK_SEL) {
-                                       snode->edittree->selout= NULL;
-                                       actsock->flag&= ~SOCK_SEL;
-                               }
-                               else {
-                                       snode->edittree->selout= actsock;
-                                       reset_sel_socket(snode, SOCK_OUT);
-                                       actsock->flag|= SOCK_SEL;
-                               }
-                       }
-//                     else if(!node_mouse_select(snode, event)) 
-//                             toolbox_n();
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
 
-                       break;
-               case MIDDLEMOUSE:
-                       if((snode->flag & SNODE_BACKDRAW) && (snode->treetype==NTREE_COMPOSIT)
-                          && (G.qual==LR_SHIFTKEY)) {
-                               snode_bg_viewmove(snode);
-                       } else {
-                               view2dmove(event);
-                       }
-               case WHEELUPMOUSE:
-               case WHEELDOWNMOUSE:
-                       view2dmove(event);      /* in drawipo.c */
-                       break;
-                       
-               case MOUSEY:
-                       doredraw= node_socket_hilights(snode, SOCK_IN|SOCK_OUT);
-                       break;
-               
-               case UI_BUT_EVENT:
-                       /* future: handlerize this! */
-                       if(snode->treetype==NTREE_SHADER)
-                               shader_node_event(snode, val);
-                       else if(snode->treetype==NTREE_COMPOSIT)
-                               composit_node_event(snode, val);
-                       else if(snode->treetype==NTREE_TEXTURE)
-                               texture_node_event(snode, val);
-                       break;
-                       
-               case PADPLUSKEY:
-                       snode_zoom_in(sa);
-                       doredraw= 1;
-                       break;
-               case PADMINUS:
-                       snode_zoom_out(sa);
-                       doredraw= 1;
-                       break;
-               case HOMEKEY:
-                       snode_home(sa, snode);
-                       doredraw= 1;
-                       break;
-               case TABKEY:
-                       if(fromlib) fromlib= -1;
-                       else snode_make_group_editable(snode, NULL);
-                       break;
-                       
-               case AKEY:
-                       if(G.qual==LR_SHIFTKEY) {
-                               if(fromlib) fromlib= -1;
-                               else toolbox_n_add();
-                       }
-                       break;
-               case BKEY:
-                       if(G.qual==0)
-                               node_border_select(snode);
-                       break;
-               case CKEY:      /* sort again, showing cyclics */
-                       ntreeSolveOrder(snode->edittree);
-                       doredraw= 1;
-                       break;
-               case DKEY:
-                       if(G.qual==LR_SHIFTKEY) {
-                               if(fromlib) fromlib= -1;
-                               else node_adduplicate(snode);
-                       }
-                       break;
-               case EKEY:
-                       // XXX                  snode_handle_recalc(snode);
-                       break;
-               case FKEY:
-                       node_make_link(snode);
-                       break;
-               case GKEY:
-                       if(fromlib) fromlib= -1;
-                       else {
-                               if(G.qual==LR_CTRLKEY) {
-                                       if(okee("Make Group"))
-                                               node_make_group(snode);
-                               }
-                               else if(G.qual==LR_ALTKEY) {
-                                       if(okee("Ungroup"))
-                                               node_ungroup(snode);
-                               }
-                               else if(G.qual==LR_SHIFTKEY) {
-                                       node_addgroup(snode);
-                               }
-//                             else
-// XXX                                 transform_nodes(snode->edittree, 'g', "Move Node");
-                       }
-                       break;
-               case HKEY:
-                       node_hide(snode);
-                       break;
-               case IKEY:
-                       node_insert_key(snode);
-                       break;
-               case LKEY:
-                       node_select_linked(snode, G.qual==LR_SHIFTKEY);
-                       break;
-               case MKEY:
-                       node_mute(snode);
-                       break;
-               case RKEY:
-                       if(G.qual==LR_CTRLKEY) {
-                               node_rename(snode);
-                       } 
-                       else if(G.qual==LR_SHIFTKEY) {
-                               if(okee("Read saved Full Sample Layers"))
-                                       node_read_fullsamplelayers(snode);
-                       }
-                       else {
-                               if(okee("Read saved Render Layers"))
-                                       node_read_renderlayers(snode);
-                       }
-                       break;
-               case DELKEY:
-               case XKEY:
-                       if(G.qual==LR_ALTKEY) {
-                               gpencil_delete_menu();
+void NODE_OT_hide_toggle(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Hide";
+       ot->description= "Toggle hiding of selected nodes";
+       ot->idname= "NODE_OT_hide_toggle";
+       
+       /* callbacks */
+       ot->exec= node_hide_exec;
+       ot->poll= ED_operator_node_active;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int node_preview_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+
+       /* sanity checking (poll callback checks this already) */
+       if((snode == NULL) || (snode->edittree == NULL))
+               return OPERATOR_CANCELLED;
+
+       ED_preview_kill_jobs(C);
+
+       node_flag_toggle_exec(snode, NODE_PREVIEW);
+
+       snode_notify(C, snode);
+
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_preview_toggle(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Toggle Node Preview";
+       ot->description= "Toggle preview display for selected nodes";
+       ot->idname= "NODE_OT_preview_toggle";
+
+       /* callbacks */
+       ot->exec= node_preview_exec;
+       ot->poll= ED_operator_node_active;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       bNode *node;
+       int hidden= 0;
+
+       /* sanity checking (poll callback checks this already) */
+       if((snode == NULL) || (snode->edittree == NULL))
+               return OPERATOR_CANCELLED;
+
+       ED_preview_kill_jobs(C);
+
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
+                       if(node_has_hidden_sockets(node)) {
+                               hidden= 1;
+                               break;
                        }
-                       else {
-                               if(fromlib) fromlib= -1;
-                               else node_delete(snode);
+               }
+       }
+
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
+                       node_set_hidden_sockets(snode, node, !hidden);
+               }
+       }
+
+       node_tree_verify_groups(snode->nodetree);
+
+       snode_notify(C, snode);
+
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Toggle Hidden Node Sockets";
+       ot->description= "Toggle unused node socket display";
+       ot->idname= "NODE_OT_hide_socket_toggle";
+
+       /* callbacks */
+       ot->exec= node_socket_toggle_exec;
+       ot->poll= ED_operator_node_active;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+/* ****************** Mute operator *********************** */
+
+static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       bNode *node;
+
+       /* no disabling inside of groups */
+       if(node_tree_get_editgroup(snode->nodetree))
+               return OPERATOR_CANCELLED;
+       
+       ED_preview_kill_jobs(C);
+
+       for(node= snode->edittree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
+                       if(node->inputs.first && node->outputs.first) {
+                               node->flag ^= NODE_MUTED;
+                               NodeTagChanged(snode->edittree, node);
                        }
-                       break;
                }
        }
+       
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
 
-       if(fromlib==-1)
-               error_libdata();
-       if(doredraw)
-               scrarea_queue_winredraw(sa);
-       if(doredraw==2)
-               scrarea_queue_headredraw(sa);
+void NODE_OT_mute_toggle(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Toggle Node Mute";
+       ot->description= "Toggle muting of the nodes";
+       ot->idname= "NODE_OT_mute_toggle";
+       
+       /* callbacks */
+       ot->exec= node_mute_exec;
+       ot->poll= ED_operator_node_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
-#endif
 
 /* ****************** Delete operator ******************* */
 
-static int node_delete_exec(bContext *C, wmOperator *op)
+static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceNode *snode= CTX_wm_space_node(C);
        bNode *node, *next;
-       bNodeSocket *sock;
        
+       ED_preview_kill_jobs(C);
+
        for(node= snode->edittree->nodes.first; node; node= next) {
                next= node->next;
                if(node->flag & SELECT) {
-                       /* set selin and selout NULL if the sockets belong to a node to be deleted */
-                       for(sock= node->inputs.first; sock; sock= sock->next)
-                               if(snode->edittree->selin == sock) snode->edittree->selin= NULL;
-
-                       for(sock= node->outputs.first; sock; sock= sock->next)
-                               if(snode->edittree->selout == sock) snode->edittree->selout= NULL;
-
                        /* check id user here, nodeFreeNode is called for free dbase too */
                        if(node->id)
                                node->id->us--;
@@ -2585,11 +2772,8 @@ static int node_delete_exec(bContext *C, wmOperator *op)
        
        node_tree_verify_groups(snode->nodetree);
 
-       // NODE_FIX_ME
-       // snode_handle_recalc(snode);
-
-       WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
-
+       snode_notify(C, snode);
+       
        return OPERATOR_FINISHED;
 }
 
@@ -2597,7 +2781,7 @@ void NODE_OT_delete(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Delete";
-       ot->description = "Delete selected nodes.";
+       ot->description = "Delete selected nodes";
        ot->idname= "NODE_OT_delete";
        
        /* api callbacks */
@@ -2608,3 +2792,123 @@ void NODE_OT_delete(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+/* ****************** Show Cyclic Dependencies Operator  ******************* */
+
+static int node_show_cycles_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       
+       /* this is just a wrapper around this call... */
+       ntreeSolveOrder(snode->edittree);
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Show Cyclic Dependencies";
+       ot->description= "Sort the nodes and show the cyclic dependencies between the nodes";
+       ot->idname= "NODE_OT_show_cyclic_dependencies";
+       
+       /* callbacks */
+       ot->exec= node_show_cycles_exec;
+       ot->poll= ED_operator_node_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+/* ****************** Add File Node Operator  ******************* */
+
+static int node_add_file_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       SpaceNode *snode= CTX_wm_space_node(C);
+       bNode *node;
+       Image *ima= NULL;
+       int ntype=0;
+
+       /* check input variables */
+       if (RNA_property_is_set(op->ptr, "filepath"))
+       {
+               char path[FILE_MAX];
+               RNA_string_get(op->ptr, "filepath", path);
+
+               errno= 0;
+
+               ima= BKE_add_image_file(path);
+
+               if(!ima) {
+                       BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", path, errno ? strerror(errno) : "Unsupported image format");
+                       return OPERATOR_CANCELLED;
+               }
+       }
+       else if(RNA_property_is_set(op->ptr, "name"))
+       {
+               char name[32];
+               RNA_string_get(op->ptr, "name", name);
+               ima= (Image *)find_id("IM", name);
+
+               if(!ima) {
+                       BKE_reportf(op->reports, RPT_ERROR, "Image named \"%s\", not found.", name);
+                       return OPERATOR_CANCELLED;
+               }
+       }
+       
+       node_deselectall(snode);
+       
+       if (snode->nodetree->type==NTREE_COMPOSIT)
+               ntype = CMP_NODE_IMAGE;
+
+       ED_preview_kill_jobs(C);
+       
+       node = node_add_node(snode, scene, ntype, snode->mx, snode->my);
+       
+       if (!node) {
+               BKE_report(op->reports, RPT_WARNING, "Could not add an image node.");
+               return OPERATOR_CANCELLED;
+       }
+       
+       node->id = (ID *)ima;
+       
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
+
+static int node_add_file_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       ARegion *ar= CTX_wm_region(C);
+       SpaceNode *snode= CTX_wm_space_node(C);
+       
+       /* convert mouse coordinates to v2d space */
+       UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
+                                                        &snode->mx, &snode->my);
+       
+       if (RNA_property_is_set(op->ptr, "filepath") || RNA_property_is_set(op->ptr, "name"))
+               return node_add_file_exec(C, op);
+       else
+               return WM_operator_filesel(C, op, event);
+}
+
+void NODE_OT_add_file(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Add File Node";
+       ot->description= "Add a file node to the current node editor";
+       ot->idname= "NODE_OT_add_file";
+       
+       /* callbacks */
+       ot->exec= node_add_file_exec;
+       ot->invoke= node_add_file_invoke;
+       ot->poll= composite_node_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH);  //XXX TODO, relative_path
+       RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Datablock name to assign.");
+}
+