doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / space_node / node_edit.c
index 8f46910..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)
@@ -242,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;
@@ -272,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 */
@@ -306,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;
@@ -326,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;
@@ -343,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) {
@@ -398,25 +427,28 @@ 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)
@@ -428,14 +460,28 @@ 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 */
                        
@@ -449,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))
@@ -477,6 +523,16 @@ void node_set_active(SpaceNode *snode, bNode *node)
                                        scene->r.actlay= node->custom1;
                                }
                        }
+                       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
@@ -499,7 +555,7 @@ void node_tree_verify_groups(bNodeTree *nodetree)
        
        /* does all materials */
        if(gnode)
-               nodeVerifyGroup((bNodeTree *)gnode->id);
+               nodeGroupVerify((bNodeTree *)gnode->id);
        
 }
 
@@ -538,11 +594,13 @@ void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
        ntreeSolveOrder(snode->nodetree);
 }
 
-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);
 
@@ -551,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;
@@ -569,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 */
@@ -581,6 +639,241 @@ void NODE_OT_group_edit(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+/* ***************** Add Group Socket operator ************* */
+
+static int node_group_socket_add_exec(bContext *C, wmOperator *op)
+{
+       SpaceNode *snode = CTX_wm_space_node(C);
+       int in_out= -1;
+       char name[32]= "";
+       int type= SOCK_VALUE;
+       bNodeTree *ngroup= snode->edittree;
+       bNodeSocket *sock;
+       
+       ED_preview_kill_jobs(C);
+       
+       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;
+       
+       sock = nodeGroupAddSocket(ngroup, name, type, in_out);
+       
+       node_tree_verify_groups(snode->nodetree);
+       
+       snode_notify(C, snode);
+       
+       return OPERATOR_FINISHED;
+}
+
+void NODE_OT_group_socket_add(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Add Group Socket";
+       ot->description = "Add node group socket";
+       ot->idname = "NODE_OT_group_socket_add";
+       
+       /* api callbacks */
+       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");
+}
+
+/* ***************** Remove Group Socket operator ************* */
+
+static int node_group_socket_remove_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;
+       
+       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;
+       
+       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;
+}
+
+void NODE_OT_group_socket_remove(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Remove Group Socket";
+       ot->description = "Removed node group socket";
+       ot->idname = "NODE_OT_group_socket_remove";
+       
+       /* api callbacks */
+       ot->exec = node_group_socket_remove_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_up_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, *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)
@@ -588,6 +881,8 @@ 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)
@@ -598,11 +893,11 @@ static int node_group_ungroup_exec(bContext *C, wmOperator *op)
                return OPERATOR_CANCELLED;
        
        if(gnode->type!=NODE_GROUP) {
-               BKE_report(op->reports, RPT_ERROR, "Not a group");
+               BKE_report(op->reports, RPT_WARNING, "Not a group");
                return OPERATOR_CANCELLED;
        }
        else if(!nodeGroupUnGroup(snode->edittree, gnode)) {
-               BKE_report(op->reports, RPT_ERROR, "Can't ungroup");
+               BKE_report(op->reports, RPT_WARNING, "Can't ungroup");
                return OPERATOR_CANCELLED;
        }
 
@@ -615,7 +910,7 @@ void NODE_OT_group_ungroup(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name = "Ungroup";
-       ot->description = "Ungroup selected nodes.";
+       ot->description = "Ungroup selected nodes";
        ot->idname = "NODE_OT_group_ungroup";
        
        /* api callbacks */
@@ -694,68 +989,296 @@ static bNode *visible_node(SpaceNode *snode, rctf *rct)
        return tnode;
 }
 
-#if 0
-static void snode_bg_viewmove(SpaceNode *snode)
+/* **************************** */
+
+typedef struct NodeViewMove {
+       short mvalo[2];
+       int xmin, ymin, xmax, ymax;
+} NodeViewMove;
+
+static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-       ScrArea *sa;
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       NodeViewMove *nvm= op->customdata;
+
+       switch (event->type) {
+               case MOUSEMOVE:
+                       
+                       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);
+                       
+                       ED_region_tag_redraw(ar);
+                       
+                       break;
+                       
+               case LEFTMOUSE:
+               case MIDDLEMOUSE:
+               case RIGHTMOUSE:
+                       
+                       MEM_freeN(nvm);
+                       op->customdata= NULL;
+            
+                       WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE, NULL);
+                       
+                       return OPERATOR_FINISHED;
+       }
+       
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       ARegion *ar= CTX_wm_region(C);
+       NodeViewMove *nvm;
        Image *ima;
        ImBuf *ibuf;
-       Window *win;
-       short mval[2], mvalo[2];
-       short rectx, recty, xmin, xmax, ymin, ymax, pad;
-       int oldcursor;
+       int pad= 10;
+       void *lock;
        
        ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
-       ibuf= BKE_image_get_ibuf(ima, NULL);
+       ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
        
-       sa = snode->area;
-       
-       if(ibuf) {
-               rectx = ibuf->x;
-               recty = ibuf->y;
-       } else {
-               rectx = recty = 1;
+       if(ibuf == NULL) {
+               BKE_image_release_ibuf(ima, lock);
+               return OPERATOR_CANCELLED;
        }
+
+       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);
        
-       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;
+       /* add modal handler */
+       WM_event_add_modal_handler(C, op);
        
-       getmouseco_sc(mvalo);
+       return OPERATOR_RUNNING_MODAL;
+}
+
+
+void NODE_OT_backimage_move(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Background Image Move";
+       ot->description = "Move Node backdrop";
+       ot->idname= "NODE_OT_backimage_move";
        
-       /* store the old cursor to temporarily change it */
-       oldcursor=get_cursor();
-       win=winlay_get_active_window();
+       /* api callbacks */
+       ot->invoke= snode_bg_viewmove_invoke;
+       ot->modal= snode_bg_viewmove_modal;
+       ot->poll= composite_node_active;
        
-       SetBlenderCursor(BC_NSEW_SCROLLCURSOR);
+       /* 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");
+
+       snode->zoom *= fac;
+       ED_region_tag_redraw(ar);
+
+       return OPERATOR_FINISHED;
+}
+
+
+void NODE_OT_backimage_zoom(wmOperatorType *ot)
+{
        
-       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();
+       /* 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);
+}
+
+/******************** 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;
+
+static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
+{
+       ImageSampleInfo *info= arg_info;
+
+       draw_nodespace_color_info(ar, info->channels, info->x, info->y, info->col, info->colf);
+}
+
+static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
+{
+       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 BIF_wait_for_statechange();
        }
+       else
+               info->draw= 0;
+
+       BKE_image_release_ibuf(ima, lock);
        
-       window_set_cursor(win, oldcursor);
+       ED_area_tag_redraw(CTX_wm_area(C));
+}
+
+static void sample_exit(bContext *C, wmOperator *op)
+{
+       ImageSampleInfo *info= op->customdata;
+
+       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)
+{
+       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;
+       
+       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;
+}
+
+static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
+{
+       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";
+       
+       /* 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;
 }
-#endif
 
 /* ********************** size widget operator ******************** */
 
@@ -872,6 +1395,79 @@ void node_deselectall(SpaceNode *snode)
                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;
+       }
+
+       /* 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;
+               }
+               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;
+               }
+       }
+       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;
+       }
+
+       /* 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;
+       }
+
+       if (p) {
+               node_deselectall(snode);
+               p->flag |= SELECT;
+               return(1);
+       }
+       return(0);
+}
+
 int node_has_hidden_sockets(bNode *node)
 {
        bNodeSocket *sock;
@@ -900,33 +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) {
+                       /* 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;
+                       
+                       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);
+               }
+       }
+}
+
+
+static int node_active_link_viewer(bContext *C, wmOperator *UNUSED(op))
+{
+       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);
 
-               if(link) {
-                       link->fromnode= tonode;
-                       link->fromsock= tonode->outputs.first;
-                       NodeTagChanged(snode->edittree, node);
-               }
-       }
+       node_link_viewer(snode, node);
+       snode_notify(C, snode);
+
+       return OPERATOR_FINISHED;
 }
 
 
-void node_active_link_viewer(SpaceNode *snode)
+
+void NODE_OT_link_viewer(wmOperatorType *ot)
 {
-       bNode *node= editnode_get_active(snode->edittree);
-       if(node)
-               node_link_viewer(snode, node);
+       /* 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;
@@ -964,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 */
@@ -1007,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;
 }
 
@@ -1047,6 +1740,16 @@ 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 *********************** */
 
 
@@ -1055,7 +1758,7 @@ typedef struct bNodeListItem {
        struct bNode *node;     
 } bNodeListItem;
 
-int sort_nodes_locx(void *a, void *b)
+static int sort_nodes_locx(void *a, void *b)
 {
        bNodeListItem *nli1 = (bNodeListItem *)a;
        bNodeListItem *nli2 = (bNodeListItem *)b;
@@ -1118,7 +1821,7 @@ static bNodeSocket *best_socket_input(bNodeTree *ntree, bNode *node, int num, in
 {
        bNodeSocket *sock;
        int socktype, maxtype=0;
-       int a;
+       int a = 0;
        
        for (sock=node->inputs.first; sock; sock=sock->next) {
                maxtype = MAX2(sock->type, maxtype);
@@ -1151,7 +1854,7 @@ void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
        ListBase *nodelist = MEM_callocN(sizeof(ListBase), "items_list");
        bNodeListItem *nli;
        bNode *node;
-       int i;
+       int i, numlinks=0;
        
        for(node= snode->edittree->nodes.first; node; node= node->next) {
                if(node->flag & NODE_SELECT) {
@@ -1188,11 +1891,16 @@ void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
                        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;
                }
        }
        
-       ntreeSolveOrder(snode->edittree);
+       if (numlinks > 0) {
+               node_tree_verify_groups(snode->nodetree);
+               ntreeSolveOrder(snode->edittree);
+       }
        
        BLI_freelistN(nodelist);
        MEM_freeN(nodelist);
@@ -1225,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);
@@ -1238,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(ELEM3(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS))
+                       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);
@@ -1259,15 +1967,41 @@ bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float
 
 /* ****************** Duplicate *********************** */
 
-static int node_duplicate_exec(bContext *C, wmOperator *op)
+static int node_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceNode *snode= CTX_wm_space_node(C);
+       bNodeTree *ntree= snode->edittree;
+       bNode *node, *newnode, *last;
        
-       ntreeCopyTree(snode->edittree, 1);      /* 1 == internally selected nodes */
+       ED_preview_kill_jobs(C);
+       
+       last = ntree->nodes.last;
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->flag & SELECT) {
+                       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;
+       }
+       
+       ntreeSolveOrder(ntree);
        
-       ntreeSolveOrder(snode->edittree);
        node_tree_verify_groups(snode->nodetree);
-       snode_handle_recalc(C, snode);
+       snode_notify(C, snode);
 
        return OPERATOR_FINISHED;
 }
@@ -1276,7 +2010,7 @@ 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 */
@@ -1310,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);
-                       }
                }
        }
 }
@@ -1353,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 */
@@ -1369,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 */
@@ -1390,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;
@@ -1421,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)
@@ -1475,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) {
@@ -1521,10 +2271,12 @@ static int node_make_link_exec(bContext *C, wmOperator *op)
        SpaceNode *snode= CTX_wm_space_node(C);
        int replace = RNA_boolean_get(op->ptr, "replace");
 
-       snode_autoconnect(snode, 0, replace);
+       ED_preview_kill_jobs(C);
+
+       snode_autoconnect(snode, 1, replace);
 
        node_tree_verify_groups(snode->nodetree);
-       snode_handle_recalc(C, snode);
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
@@ -1533,7 +2285,7 @@ void NODE_OT_link_make(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Make Links";
-       ot->description= "Makes a link between selected output in input sockets.";
+       ot->description= "Makes a link between selected output in input sockets";
        ot->idname= "NODE_OT_link_make";
        
        /* callbacks */
@@ -1557,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;
        }
@@ -1584,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;
@@ -1596,7 +2350,7 @@ static int cut_links_exec(bContext *C, wmOperator *op)
                
                ntreeSolveOrder(snode->edittree);
                node_tree_verify_groups(snode->nodetree);
-               snode_handle_recalc(C, snode);
+               snode_notify(C, snode);
                
                return OPERATOR_FINISHED;
        }
@@ -1629,15 +2383,19 @@ 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) {
@@ -1651,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)
@@ -1704,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;
        }
        
@@ -1717,14 +2528,16 @@ static int node_group_make_exec(bContext *C, wmOperator *op)
                }
                
                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 {
@@ -1732,7 +2545,7 @@ static int node_group_make_exec(bContext *C, wmOperator *op)
                ntreeSolveOrder(snode->nodetree);
        }
        
-       snode_handle_recalc(C, snode);
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
@@ -1741,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 */
@@ -1754,56 +2567,151 @@ void NODE_OT_group_make(wmOperatorType *ot)
 
 /* ****************** Hide operator *********************** */
 
-static int node_hide_exec(bContext *C, wmOperator *op)
+static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
 {
-       SpaceNode *snode= CTX_wm_space_node(C);
+       int tot_eq= 0, tot_neq= 0;
        bNode *node;
-       int nothidden=0, ishidden=0;
-       
-       /* sanity checking (poll callback checks this already) */
-       if((snode == NULL) || (snode->edittree == NULL))
-               return OPERATOR_CANCELLED;
-       
+
        for(node= snode->edittree->nodes.first; node; node= node->next) {
                if(node->flag & SELECT) {
-                       if(node->flag & NODE_HIDDEN)
-                               ishidden++;
+
+                       if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
+                               continue;
+
+                       if(node->flag & toggle_flag)
+                               tot_eq++;
                        else
-                               nothidden++;
+                               tot_neq++;
                }
        }
        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(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= CTX_wm_space_node(C);
+       
+       /* sanity checking (poll callback checks this already) */
+       if((snode == NULL) || (snode->edittree == NULL))
+               return OPERATOR_CANCELLED;
        
-       snode_handle_recalc(C, snode);
+       node_flag_toggle_exec(snode, NODE_HIDDEN);
+       
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
 
-void NODE_OT_hide(wmOperatorType *ot)
+void NODE_OT_hide_toggle(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Hide";
-       ot->description= "Toggle hiding of the nodes.";
-       ot->idname= "NODE_OT_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;
+                       }
+               }
+       }
+
+       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 *op)
+static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceNode *snode= CTX_wm_space_node(C);
        bNode *node;
@@ -1812,25 +2720,28 @@ static int node_mute_exec(bContext *C, wmOperator *op)
        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);
                        }
                }
        }
        
-       snode_handle_recalc(C, snode);
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
 
-void NODE_OT_mute(wmOperatorType *ot)
+void NODE_OT_mute_toggle(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Mute";
-       ot->description= "Toggle muting of the nodes.";
-       ot->idname= "NODE_OT_mute";
+       ot->name= "Toggle Node Mute";
+       ot->description= "Toggle muting of the nodes";
+       ot->idname= "NODE_OT_mute_toggle";
        
        /* callbacks */
        ot->exec= node_mute_exec;
@@ -1842,11 +2753,13 @@ void NODE_OT_mute(wmOperatorType *ot)
 
 /* ****************** 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;
        
+       ED_preview_kill_jobs(C);
+
        for(node= snode->edittree->nodes.first; node; node= next) {
                next= node->next;
                if(node->flag & SELECT) {
@@ -1859,7 +2772,7 @@ static int node_delete_exec(bContext *C, wmOperator *op)
        
        node_tree_verify_groups(snode->nodetree);
 
-       snode_handle_recalc(C, snode);
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
@@ -1868,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 */
@@ -1881,13 +2794,13 @@ void NODE_OT_delete(wmOperatorType *ot)
 
 /* ****************** Show Cyclic Dependencies Operator  ******************* */
 
-static int node_show_cycles_exec(bContext *C, wmOperator *op)
+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_handle_recalc(C, snode);
+       snode_notify(C, snode);
        
        return OPERATOR_FINISHED;
 }
@@ -1896,7 +2809,7 @@ 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->description= "Sort the nodes and show the cyclic dependencies between the nodes";
        ot->idname= "NODE_OT_show_cyclic_dependencies";
        
        /* callbacks */
@@ -1907,4 +2820,95 @@ void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
        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.");
+}