Code

version to 0.44pre4
[inkscape.git] / src / nodepath.cpp
index c28ca7b80361f65cbb121f317cf2b594b5ecd6f8..96820d85bc8dbcfbaa0c2d3d6cfb08fae5d9f47b 100644 (file)
@@ -7,7 +7,7 @@
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
  *
- * This code is in public domain
+ * Portions of this code are in public domain; node sculpting functions written by bulia byak are under GNU GPL
  */
 
 #ifdef HAVE_CONFIG_H
 #include "prefs-utils.h"
 #include "sp-metrics.h"
 #include "sp-path.h"
-#include <libnr/nr-matrix-ops.h>
+#include "libnr/nr-matrix-ops.h"
 #include "splivarot.h"
 #include "svg/svg.h"
+#include "display/bezier-utils.h"
+#include <vector>
+#include <algorithm>
 
 class NR::Matrix;
 
@@ -47,7 +50,7 @@ class NR::Matrix;
 /// evil evil evil. FIXME: conflict of two different Path classes!
 /// There is a conflict in the namespace between two classes named Path.
 /// #include "sp-flowtext.h"
-/// #include "sp-flowregion.h" 
+/// #include "sp-flowregion.h"
 
 #define SP_TYPE_FLOWREGION            (sp_flowregion_get_type ())
 #define SP_IS_FLOWREGION(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SP_TYPE_FLOWREGION))
@@ -88,28 +91,29 @@ static void stamp_repr(Inkscape::NodePath::Path *np);
 static SPCurve *create_curve(Inkscape::NodePath::Path *np);
 static gchar *create_typestr(Inkscape::NodePath::Path *np);
 
-static void sp_node_ensure_ctrls(Inkscape::NodePath::Node *node);
+static void sp_node_update_handles(Inkscape::NodePath::Node *node, bool fire_move_signals = true);
 
 static void sp_nodepath_node_select(Inkscape::NodePath::Node *node, gboolean incremental, gboolean override);
 
 static void sp_node_set_selected(Inkscape::NodePath::Node *node, gboolean selected);
 
-/* Control knot placement, if node or other knot is moved */
-
-static void sp_node_adjust_knot(Inkscape::NodePath::Node *node, gint which_adjust);
-static void sp_node_adjust_knots(Inkscape::NodePath::Node *node);
-
-/* Knot event handlers */
+/* Adjust handle placement, if the node or the other handle is moved */
+static void sp_node_adjust_handle(Inkscape::NodePath::Node *node, gint which_adjust);
+static void sp_node_adjust_handles(Inkscape::NodePath::Node *node);
 
+/* Node event callbacks */
 static void node_clicked(SPKnot *knot, guint state, gpointer data);
 static void node_grabbed(SPKnot *knot, guint state, gpointer data);
 static void node_ungrabbed(SPKnot *knot, guint state, gpointer data);
 static gboolean node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data);
-static void node_ctrl_clicked(SPKnot *knot, guint state, gpointer data);
-static void node_ctrl_grabbed(SPKnot *knot, guint state, gpointer data);
-static void node_ctrl_ungrabbed(SPKnot *knot, guint state, gpointer data);
-static gboolean node_ctrl_request(SPKnot *knot, NR::Point *p, guint state, gpointer data);
-static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer data);
+
+/* Handle event callbacks */
+static void node_handle_clicked(SPKnot *knot, guint state, gpointer data);
+static void node_handle_grabbed(SPKnot *knot, guint state, gpointer data);
+static void node_handle_ungrabbed(SPKnot *knot, guint state, gpointer data);
+static gboolean node_handle_request(SPKnot *knot, NR::Point *p, guint state, gpointer data);
+static void node_handle_moved(SPKnot *knot, NR::Point *p, guint state, gpointer data);
+static gboolean node_handle_event(SPKnot *knot, GdkEvent *event, Inkscape::NodePath::Node *n);
 
 /* Constructors and destructors */
 
@@ -131,9 +135,9 @@ static NRPathcode sp_node_path_code_from_side(Inkscape::NodePath::Node *node,Ink
 static Inkscape::NodePath::Node *active_node = NULL;
 
 /**
- * \brief Creates new nodepath from item 
+ * \brief Creates new nodepath from item
  */
-Inkscape::NodePath::Path *sp_nodepath_new(SPDesktop *desktop, SPItem *item)
+Inkscape::NodePath::Path *sp_nodepath_new(SPDesktop *desktop, SPItem *item, bool show_handles)
 {
     Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
 
@@ -180,9 +184,12 @@ Inkscape::NodePath::Path *sp_nodepath_new(SPDesktop *desktop, SPItem *item)
     np->subpaths    = NULL;
     np->selected    = NULL;
     np->nodeContext = NULL; //Let the context that makes this set it
+    np->livarot_path = NULL;
+    np->local_change = 0;
+    np->show_handles = show_handles;
 
     // we need to update item's transform from the repr here,
-    // because they may be out of sync when we respond 
+    // because they may be out of sync when we respond
     // to a change in repr by regenerating nodepath     --bb
     sp_object_read_attr(SP_OBJECT(item), "transform");
 
@@ -190,17 +197,23 @@ Inkscape::NodePath::Path *sp_nodepath_new(SPDesktop *desktop, SPItem *item)
     np->d2i  = np->i2d.inverse();
     np->repr = repr;
 
-    /* Now the bitchy part (lauris) */
-
+    // create the subpath(s) from the bpath
     NArtBpath *b = bpath;
-
     while (b->code != NR_END) {
         b = subpath_from_bpath(np, b, typestr + (b - bpath));
     }
 
+    // reverse the list, because sp_nodepath_subpath_new() used g_list_prepend instead of append (for speed)
+    np->subpaths = g_list_reverse(np->subpaths);
+
     g_free(typestr);
     sp_curve_unref(curve);
 
+    // create the livarot representation from the same item
+    np->livarot_path = Path_for_item(item, true, true);
+    if (np->livarot_path)
+        np->livarot_path->ConvertWithBackData(0.01);
+
     return np;
 }
 
@@ -222,6 +235,11 @@ void sp_nodepath_destroy(Inkscape::NodePath::Path *np) {
 
     g_assert(!np->selected);
 
+    if (np->livarot_path) {
+        delete np->livarot_path;
+        np->livarot_path = NULL;
+    }
+
     np->desktop = NULL;
 
     g_free(np);
@@ -254,20 +272,64 @@ static gint sp_nodepath_get_node_count(Inkscape::NodePath::Path *np)
     return nodeCount;
 }
 
+/**
+ *  Return the subpath count of a given NodePath.
+ */
+static gint sp_nodepath_get_subpath_count(Inkscape::NodePath::Path *np)
+{
+    if (!np)
+        return 0;
+    return g_list_length (np->subpaths);
+}
+
+/**
+ *  Return the selected node count of a given NodePath.
+ */
+static gint sp_nodepath_selection_get_node_count(Inkscape::NodePath::Path *np)
+{
+    if (!np)
+        return 0;
+    return g_list_length (np->selected);
+}
 
+/**
+ *  Return the number of subpaths where nodes are selected in a given NodePath.
+ */
+static gint sp_nodepath_selection_get_subpath_count(Inkscape::NodePath::Path *np)
+{
+    if (!np)
+        return 0;
+    if (!np->selected)
+        return 0;
+    if (!np->selected->next)
+        return 1;
+    gint count = 0;
+    for (GList *spl = np->subpaths; spl != NULL; spl = spl->next) {
+        Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data;
+        for (GList *nl = subpath->nodes; nl != NULL; nl = nl->next) {
+            Inkscape::NodePath::Node *node = (Inkscape::NodePath::Node *) nl->data;
+            if (node->selected) {
+                count ++;
+                break;
+            }
+        }
+    }
+    return count;
+}
 /**
  * Clean up a nodepath after editing.
- * 
+ *
  * Currently we are deleting trivial subpaths.
  */
 static void sp_nodepath_cleanup(Inkscape::NodePath::Path *nodepath)
 {
     GList *badSubPaths = NULL;
 
-    //Check all subpaths to be >=2 nodes
+    //Check all closed subpaths to be >=1 nodes, all open subpaths to be >= 2 nodes
     for (GList *l = nodepath->subpaths; l ; l=l->next) {
        Inkscape::NodePath::SubPath *sp = (Inkscape::NodePath::SubPath *)l->data;
-        if (sp_nodepath_subpath_get_node_count(sp)<2)
+       if ((sp_nodepath_subpath_get_node_count(sp)<2 && !sp->closed) || (sp_nodepath_subpath_get_node_count(sp)<1 && sp->closed))
             badSubPaths = g_list_append(badSubPaths, sp);
     }
 
@@ -281,61 +343,6 @@ static void sp_nodepath_cleanup(Inkscape::NodePath::Path *nodepath)
     g_list_free(badSubPaths);
 }
 
-
-
-/**
- * \brief Returns true if the argument nodepath and the d attribute in 
- * its repr do not match. 
- *
- * This may happen if repr was changed in, e.g., XML editor or by undo. 
- * 
- * \todo
- * UGLY HACK, think how we can eliminate it.
- */
-gboolean nodepath_repr_d_changed(Inkscape::NodePath::Path *np, char const *newd)
-{
-    g_assert(np);
-
-    SPCurve *curve = create_curve(np);
-
-    gchar *svgpath = sp_svg_write_path(curve->bpath);
-
-    char const *attr_d = ( newd
-                           ? newd
-                           : SP_OBJECT(np->path)->repr->attribute("d") );
-
-    gboolean ret;
-    if (attr_d && svgpath)
-        ret = strcmp(attr_d, svgpath);
-    else 
-        ret = TRUE;
-
-    g_free(svgpath);
-    sp_curve_unref(curve);
-
-    return ret;
-}
-
-/**
- * \brief Returns true if the argument nodepath and the sodipodi:nodetypes 
- * attribute in its repr do not match. 
- *
- * This may happen if repr was changed in, e.g., the XML editor or by undo.
- */
-gboolean nodepath_repr_typestr_changed(Inkscape::NodePath::Path *np, char const *newtypestr)
-{
-    g_assert(np);
-    gchar *typestr = create_typestr(np);
-    char const *attr_typestr = ( newtypestr
-                                 ? newtypestr
-                                 : SP_OBJECT(np->path)->repr->attribute("sodipodi:nodetypes") );
-    gboolean const ret = (attr_typestr && strcmp(attr_typestr, typestr));
-
-    g_free(typestr);
-
-    return ret;
-}
-
 /**
  * Create new nodepath from b, make it subpath of np.
  * \param t The node type.
@@ -424,7 +431,8 @@ static gchar *parse_nodetypes(gchar const *types, gint length)
 }
 
 /**
- * Make curve out of path and associate it with it.
+ * Make curve out of nodepath, write it into that nodepath's SPShape item so that display is
+ * updated but repr is not (for speed). Used during curve and node drag.
  */
 static void update_object(Inkscape::NodePath::Path *np)
 {
@@ -448,10 +456,17 @@ static void update_repr_internal(Inkscape::NodePath::Path *np)
 
     SPCurve *curve = create_curve(np);
     gchar *typestr = create_typestr(np);
-    gchar *svgpath = sp_svg_write_path(curve->bpath);
+    gchar *svgpath = sp_svg_write_path(SP_CURVE_BPATH(curve));
+
+    if (repr->attribute("d") == NULL || strcmp(svgpath, repr->attribute("d"))) { // d changed
+        np->local_change++;
+        repr->setAttribute("d", svgpath);
+    }
 
-    repr->setAttribute("d", svgpath);
-    repr->setAttribute("sodipodi:nodetypes", typestr);
+    if (repr->attribute("sodipodi:nodetypes") == NULL || strcmp(typestr, repr->attribute("sodipodi:nodetypes"))) { // nodetypes changed
+        np->local_change++;
+        repr->setAttribute("sodipodi:nodetypes", typestr);
+    }
 
     g_free(svgpath);
     g_free(typestr);
@@ -461,19 +476,41 @@ static void update_repr_internal(Inkscape::NodePath::Path *np)
 /**
  * Update XML path node with data from path object, commit changes forever.
  */
-static void update_repr(Inkscape::NodePath::Path *np)
+void sp_nodepath_update_repr(Inkscape::NodePath::Path *np)
 {
     update_repr_internal(np);
-    sp_document_done(SP_DT_DOCUMENT(np->desktop));
+    sp_document_done(sp_desktop_document(np->desktop));
+
+    if (np->livarot_path) {
+        delete np->livarot_path;
+        np->livarot_path = NULL;
+    }
+
+    if (np->path && SP_IS_ITEM(np->path)) {
+        np->livarot_path = Path_for_item (np->path, true, true);
+        if (np->livarot_path)
+            np->livarot_path->ConvertWithBackData(0.01);
+    }
 }
 
 /**
  * Update XML path node with data from path object, commit changes with undo.
  */
-static void update_repr_keyed(Inkscape::NodePath::Path *np, gchar const *key)
+static void sp_nodepath_update_repr_keyed(Inkscape::NodePath::Path *np, gchar const *key)
 {
     update_repr_internal(np);
-    sp_document_maybe_done(SP_DT_DOCUMENT(np->desktop), key);
+    sp_document_maybe_done(sp_desktop_document(np->desktop), key);
+
+    if (np->livarot_path) {
+        delete np->livarot_path;
+        np->livarot_path = NULL;
+    }
+
+    if (np->path && SP_IS_ITEM(np->path)) {
+        np->livarot_path = Path_for_item (np->path, true, true);
+        if (np->livarot_path)
+            np->livarot_path->ConvertWithBackData(0.01);
+    }
 }
 
 /**
@@ -494,7 +531,7 @@ static void stamp_repr(Inkscape::NodePath::Path *np)
     SPCurve *curve = create_curve(np);
     gchar *typestr = create_typestr(np);
 
-    gchar *svgpath = sp_svg_write_path(curve->bpath);
+    gchar *svgpath = sp_svg_write_path(SP_CURVE_BPATH(curve));
 
     new_repr->setAttribute("d", svgpath);
     new_repr->setAttribute("sodipodi:nodetypes", typestr);
@@ -504,7 +541,7 @@ static void stamp_repr(Inkscape::NodePath::Path *np)
     // move to the saved position
     new_repr->setPosition(pos > 0 ? pos : 0);
 
-    sp_document_done(SP_DT_DOCUMENT(np->desktop));
+    sp_document_done(sp_desktop_document(np->desktop));
 
     Inkscape::GC::release(new_repr);
     g_free(svgpath);
@@ -640,7 +677,7 @@ static Inkscape::NodePath::Path *sp_nodepath_current()
 
 
 /**
- \brief Fills node and control positions for three nodes, splitting line
+ \brief Fills node and handle positions for three nodes, splitting line
   marked by end at distance t.
  */
 static void sp_nodepath_line_midpoint(Inkscape::NodePath::Node *new_path,Inkscape::NodePath::Node *end, gdouble t)
@@ -681,7 +718,7 @@ static void sp_nodepath_line_midpoint(Inkscape::NodePath::Node *new_path,Inkscap
 }
 
 /**
- * Adds new node on direct line between two nodes, activates handles of all 
+ * Adds new node on direct line between two nodes, activates handles of all
  * three nodes.
  */
 static Inkscape::NodePath::Node *sp_nodepath_line_add_node(Inkscape::NodePath::Node *end, gdouble t)
@@ -699,9 +736,9 @@ static Inkscape::NodePath::Node *sp_nodepath_line_add_node(Inkscape::NodePath::N
                                                &start->pos, &start->pos, &start->n.pos);
     sp_nodepath_line_midpoint(newnode, end, t);
 
-    sp_node_ensure_ctrls(start);
-    sp_node_ensure_ctrls(newnode);
-    sp_node_ensure_ctrls(end);
+    sp_node_update_handles(start);
+    sp_node_update_handles(newnode);
+    sp_node_update_handles(end);
 
     return newnode;
 }
@@ -769,12 +806,12 @@ static Inkscape::NodePath::Node *sp_nodepath_node_duplicate(Inkscape::NodePath::
         return newnode; // otherwise select the newly created node
 }
 
-static void sp_node_control_mirror_n_to_p(Inkscape::NodePath::Node *node)
+static void sp_node_handle_mirror_n_to_p(Inkscape::NodePath::Node *node)
 {
     node->p.pos = (node->pos + (node->pos - node->n.pos));
 }
 
-static void sp_node_control_mirror_p_to_n(Inkscape::NodePath::Node *node)
+static void sp_node_handle_mirror_p_to_n(Inkscape::NodePath::Node *node)
 {
     node->n.pos = (node->pos + (node->pos - node->p.pos));
 }
@@ -800,24 +837,24 @@ static void sp_nodepath_set_line_type(Inkscape::NodePath::Node *end, NRPathcode
         if (end->n.other) {
             if (end->n.other->code == NR_LINETO) end->type =Inkscape::NodePath::NODE_CUSP;
         }
-        sp_node_adjust_knot(start, -1);
-        sp_node_adjust_knot(end, 1);
+        sp_node_adjust_handle(start, -1);
+        sp_node_adjust_handle(end, 1);
     } else {
         NR::Point delta = end->pos - start->pos;
         start->n.pos = start->pos + delta / 3;
         end->p.pos = end->pos - delta / 3;
-        sp_node_adjust_knot(start, 1);
-        sp_node_adjust_knot(end, -1);
+        sp_node_adjust_handle(start, 1);
+        sp_node_adjust_handle(end, -1);
     }
 
-    sp_node_ensure_ctrls(start);
-    sp_node_ensure_ctrls(end);
+    sp_node_update_handles(start);
+    sp_node_update_handles(end);
 }
 
 /**
  * Change node type, and its handles accordingly.
  */
-static Inkscape::NodePath::Node *sp_nodepath_set_node_type(Inkscape::NodePath::Node *node,Inkscape::NodePath::NodeType type)
+static Inkscape::NodePath::Node *sp_nodepath_set_node_type(Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeType type)
 {
     g_assert(node);
     g_assert(node->subpath);
@@ -834,12 +871,25 @@ static Inkscape::NodePath::Node *sp_nodepath_set_node_type(Inkscape::NodePath::N
     node->type = type;
 
     if (node->type == Inkscape::NodePath::NODE_CUSP) {
-        g_object_set(G_OBJECT(node->knot), "shape", SP_KNOT_SHAPE_DIAMOND, "size", 9, NULL);
+        node->knot->setShape (SP_KNOT_SHAPE_DIAMOND);
+        node->knot->setSize (node->selected? 11 : 9);
+        sp_knot_update_ctrl(node->knot);
+    } else {
+        node->knot->setShape (SP_KNOT_SHAPE_SQUARE);
+        node->knot->setSize (node->selected? 9 : 7);
+        sp_knot_update_ctrl(node->knot);
+    }
+
+    // if one of handles is mouseovered, preserve its position
+    if (node->p.knot && SP_KNOT_IS_MOUSEOVER(node->p.knot)) {
+        sp_node_adjust_handle(node, 1);
+    } else if (node->n.knot && SP_KNOT_IS_MOUSEOVER(node->n.knot)) {
+        sp_node_adjust_handle(node, -1);
     } else {
-        g_object_set(G_OBJECT(node->knot), "shape", SP_KNOT_SHAPE_SQUARE, "size", 7, NULL);
+        sp_node_adjust_handles(node);
     }
 
-    sp_node_adjust_knots(node);
+    sp_node_update_handles(node);
 
     sp_nodepath_update_statusbar(node->subpath->nodepath);
 
@@ -847,7 +897,7 @@ static Inkscape::NodePath::Node *sp_nodepath_set_node_type(Inkscape::NodePath::N
 }
 
 /**
- * Same as sp_nodepath_set_node_type(), but also converts, if necessary, 
+ * Same as sp_nodepath_set_node_type(), but also converts, if necessary,
  * adjacent segments from lines to curves.
 */
 void sp_nodepath_convert_node_type(Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeType type)
@@ -859,10 +909,10 @@ void sp_nodepath_convert_node_type(Inkscape::NodePath::Node *node, Inkscape::Nod
             NR::Point delta;
             if (node->n.other != NULL)
                 delta = node->n.other->pos - node->p.other->pos;
-            else 
+            else
                 delta = node->pos - node->p.other->pos;
             node->p.pos = node->pos - delta / 4;
-            sp_node_ensure_ctrls(node);
+            sp_node_update_handles(node);
         }
 
         if ((node->n.other != NULL) && (node->n.other->code == NR_LINETO || node->pos == node->n.pos)) {
@@ -871,10 +921,10 @@ void sp_nodepath_convert_node_type(Inkscape::NodePath::Node *node, Inkscape::Nod
             NR::Point delta;
             if (node->p.other != NULL)
                 delta = node->p.other->pos - node->n.other->pos;
-            else 
+            else
                 delta = node->pos - node->n.other->pos;
             node->n.pos = node->pos - delta / 4;
-            sp_node_ensure_ctrls(node);
+            sp_node_update_handles(node);
         }
     }
 
@@ -894,18 +944,20 @@ void sp_node_moveto(Inkscape::NodePath::Node *node, NR::Point p)
 
     if (node->p.other) {
         if (node->code == NR_LINETO) {
-            sp_node_adjust_knot(node, 1);
-            sp_node_adjust_knot(node->p.other, -1);
+            sp_node_adjust_handle(node, 1);
+            sp_node_adjust_handle(node->p.other, -1);
         }
     }
     if (node->n.other) {
         if (node->n.other->code == NR_LINETO) {
-            sp_node_adjust_knot(node, -1);
-            sp_node_adjust_knot(node->n.other, 1);
+            sp_node_adjust_handle(node, -1);
+            sp_node_adjust_handle(node->n.other, 1);
         }
     }
 
-    sp_node_ensure_ctrls(node);
+    // this function is only called from batch movers that will update display at the end
+    // themselves, so here we just move all the knots without emitting move signals, for speed
+    sp_node_update_handles(node, false);
 }
 
 /**
@@ -914,22 +966,19 @@ void sp_node_moveto(Inkscape::NodePath::Node *node, NR::Point p)
 static void sp_nodepath_selected_nodes_move(Inkscape::NodePath::Path *nodepath, NR::Coord dx, NR::Coord dy,
                                             bool const snap = true)
 {
-    NR::Coord best[2] = { NR_HUGE, NR_HUGE };
+    NR::Coord best = NR_HUGE;
     NR::Point delta(dx, dy);
     NR::Point best_pt = delta;
 
     if (snap) {
+        SnapManager const &m = nodepath->desktop->namedview->snap_manager;
+        
         for (GList *l = nodepath->selected; l != NULL; l = l->next) {
-           Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
-            NR::Point p = n->pos + delta;
-            for (int dim = 0; dim < 2; dim++) {
-                NR::Coord dist = namedview_dim_snap(nodepath->desktop->namedview,
-                                                    Inkscape::Snapper::SNAP_POINT, p,
-                                                    NR::Dim2(dim), nodepath->path);
-                if (dist < best[dim]) {
-                    best[dim] = dist;
-                    best_pt[dim] = p[dim] - n->pos[dim];
-                }
+            Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
+            Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::Snapper::SNAP_POINT, n->pos + delta, NULL);
+            if (s.getDistance() < best) {
+                best = s.getDistance();
+                best_pt = s.getPoint() - n->pos;
             }
         }
     }
@@ -939,9 +988,217 @@ static void sp_nodepath_selected_nodes_move(Inkscape::NodePath::Path *nodepath,
         sp_node_moveto(n, n->pos + best_pt);
     }
 
+    // do not update repr here so that node dragging is acceptably fast
     update_object(nodepath);
 }
 
+/**
+Function mapping x (in the range 0..1) to y (in the range 1..0) using a smooth half-bell-like
+curve; the parameter alpha determines how blunt (alpha > 1) or sharp (alpha < 1) will be the curve
+near x = 0.
+ */
+double
+sculpt_profile (double x, double alpha, guint profile)
+{
+    if (x >= 1)
+        return 0;
+    if (x <= 0)
+        return 1;
+
+    switch (profile) {
+        case SCULPT_PROFILE_LINEAR:
+        return 1 - x;
+        case SCULPT_PROFILE_BELL:
+        return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
+        case SCULPT_PROFILE_ELLIPTIC:
+        return sqrt(1 - x*x);
+    }
+
+    return 1;
+}
+
+double
+bezier_length (NR::Point a, NR::Point ah, NR::Point bh, NR::Point b)
+{
+    // extremely primitive for now, don't have time to look for the real one
+    double lower = NR::L2(b - a);
+    double upper = NR::L2(ah - a) + NR::L2(bh - ah) + NR::L2(bh - b);
+    return (lower + upper)/2;
+}
+
+void
+sp_nodepath_move_node_and_handles (Inkscape::NodePath::Node *n, NR::Point delta, NR::Point delta_n, NR::Point delta_p)
+{
+    n->pos = n->origin + delta;
+    n->n.pos = n->n.origin + delta_n;
+    n->p.pos = n->p.origin + delta_p;
+    sp_node_adjust_handles(n);
+    sp_node_update_handles(n, false);
+}
+
+/**
+ * Displace selected nodes and their handles by fractions of delta (from their origins), depending
+ * on how far they are from the dragged node n.
+ */
+static void 
+sp_nodepath_selected_nodes_sculpt(Inkscape::NodePath::Path *nodepath, Inkscape::NodePath::Node *n, NR::Point delta)
+{
+    g_assert (n);
+    g_assert (nodepath);
+    g_assert (n->subpath->nodepath == nodepath);
+
+    double pressure = n->knot->pressure;
+    if (pressure == 0)
+        pressure = 0.5; // default
+    pressure = CLAMP (pressure, 0.2, 0.8);
+
+    // map pressure to alpha = 1/5 ... 5
+    double alpha = 1 - 2 * fabs(pressure - 0.5);
+    if (pressure > 0.5)
+        alpha = 1/alpha;
+
+    guint profile = prefs_get_int_attribute("tools.nodes", "sculpting_profile", SCULPT_PROFILE_BELL);
+
+    if (sp_nodepath_selection_get_subpath_count(nodepath) <= 1) {
+        // Only one subpath has selected nodes:
+        // use linear mode, where the distance from n to node being dragged is calculated along the path
+
+        double n_sel_range = 0, p_sel_range = 0;
+        guint n_nodes = 0, p_nodes = 0;
+        guint n_sel_nodes = 0, p_sel_nodes = 0;
+
+        // First pass: calculate ranges (TODO: we could cache them, as they don't change while dragging)
+        {
+            double n_range = 0, p_range = 0;
+            bool n_going = true, p_going = true;
+            Inkscape::NodePath::Node *n_node = n;
+            Inkscape::NodePath::Node *p_node = n;
+            do {
+                // Do one step in both directions from n, until reaching the end of subpath or bumping into each other
+                if (n_node && n_going)
+                    n_node = n_node->n.other;
+                if (n_node == NULL) {
+                    n_going = false;
+                } else {
+                    n_nodes ++;
+                    n_range += bezier_length (n_node->p.other->origin, n_node->p.other->n.origin, n_node->p.origin, n_node->origin);
+                    if (n_node->selected) {
+                        n_sel_nodes ++;
+                        n_sel_range = n_range;
+                    }
+                    if (n_node == p_node) {
+                        n_going = false;
+                        p_going = false;
+                    }
+                }
+                if (p_node && p_going)
+                    p_node = p_node->p.other;
+                if (p_node == NULL) {
+                    p_going = false;
+                } else {
+                    p_nodes ++;
+                    p_range += bezier_length (p_node->n.other->origin, p_node->n.other->p.origin, p_node->n.origin, p_node->origin);
+                    if (p_node->selected) {
+                        p_sel_nodes ++;
+                        p_sel_range = p_range;
+                    }
+                    if (p_node == n_node) {
+                        n_going = false;
+                        p_going = false;
+                    }
+                }
+            } while (n_going || p_going);
+        }
+
+        // Second pass: actually move nodes in this subpath
+        sp_nodepath_move_node_and_handles (n, delta, delta, delta);
+        {
+            double n_range = 0, p_range = 0;
+            bool n_going = true, p_going = true;
+            Inkscape::NodePath::Node *n_node = n;
+            Inkscape::NodePath::Node *p_node = n;
+            do {
+                // Do one step in both directions from n, until reaching the end of subpath or bumping into each other
+                if (n_node && n_going)
+                    n_node = n_node->n.other;
+                if (n_node == NULL) {
+                    n_going = false;
+                } else {
+                    n_range += bezier_length (n_node->p.other->origin, n_node->p.other->n.origin, n_node->p.origin, n_node->origin);
+                    if (n_node->selected) {
+                        sp_nodepath_move_node_and_handles (n_node, 
+                                                           sculpt_profile (n_range / n_sel_range, alpha, profile) * delta,
+                                                           sculpt_profile ((n_range + NR::L2(n_node->n.origin - n_node->origin)) / n_sel_range, alpha, profile) * delta,
+                                                           sculpt_profile ((n_range - NR::L2(n_node->p.origin - n_node->origin)) / n_sel_range, alpha, profile) * delta);
+                    }
+                    if (n_node == p_node) {
+                        n_going = false;
+                        p_going = false;
+                    }
+                }
+                if (p_node && p_going)
+                    p_node = p_node->p.other;
+                if (p_node == NULL) {
+                    p_going = false;
+                } else {
+                    p_range += bezier_length (p_node->n.other->origin, p_node->n.other->p.origin, p_node->n.origin, p_node->origin);
+                    if (p_node->selected) {
+                        sp_nodepath_move_node_and_handles (p_node, 
+                                                           sculpt_profile (p_range / p_sel_range, alpha, profile) * delta,
+                                                           sculpt_profile ((p_range - NR::L2(p_node->n.origin - p_node->origin)) / p_sel_range, alpha, profile) * delta,
+                                                           sculpt_profile ((p_range + NR::L2(p_node->p.origin - p_node->origin)) / p_sel_range, alpha, profile) * delta);
+                    }
+                    if (p_node == n_node) {
+                        n_going = false;
+                        p_going = false;
+                    }
+                }
+            } while (n_going || p_going);
+        }
+
+    } else {
+        // Multiple subpaths have selected nodes:
+        // use spatial mode, where the distance from n to node being dragged is measured directly as NR::L2.
+        // TODO: correct these distances taking into account their angle relative to the bisector, so as to 
+        // fix the pear-like shape when sculpting e.g. a ring
+
+        // First pass: calculate range
+        gdouble direct_range = 0;
+        for (GList *spl = nodepath->subpaths; spl != NULL; spl = spl->next) {
+            Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data;
+            for (GList *nl = subpath->nodes; nl != NULL; nl = nl->next) {
+                Inkscape::NodePath::Node *node = (Inkscape::NodePath::Node *) nl->data;
+                if (node->selected) {
+                    direct_range = MAX(direct_range, NR::L2(node->origin - n->origin));
+                }
+            }
+        }
+
+        // Second pass: actually move nodes
+        for (GList *spl = nodepath->subpaths; spl != NULL; spl = spl->next) {
+            Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data;
+            for (GList *nl = subpath->nodes; nl != NULL; nl = nl->next) {
+                Inkscape::NodePath::Node *node = (Inkscape::NodePath::Node *) nl->data;
+                if (node->selected) {
+                    if (direct_range > 1e-6) {
+                        sp_nodepath_move_node_and_handles (node,
+                                                       sculpt_profile (NR::L2(node->origin - n->origin) / direct_range, alpha, profile) * delta,
+                                                       sculpt_profile (NR::L2(node->n.origin - n->origin) / direct_range, alpha, profile) * delta,
+                                                       sculpt_profile (NR::L2(node->p.origin - n->origin) / direct_range, alpha, profile) * delta);
+                    } else {
+                        sp_nodepath_move_node_and_handles (node, delta, delta, delta);
+                    }
+
+                }
+            }
+        }
+    }
+
+    // do not update repr here so that node dragging is acceptably fast
+    update_object(nodepath);
+}
+
+
 /**
  * Move node selection to point, adjust its and neighbouring handles,
  * handle possible snapping, and commit the change with possible undo.
@@ -955,11 +1212,11 @@ sp_node_selected_move(gdouble dx, gdouble dy)
     sp_nodepath_selected_nodes_move(nodepath, dx, dy, false);
 
     if (dx == 0) {
-        update_repr_keyed(nodepath, "node:move:vertical");
+        sp_nodepath_update_repr_keyed(nodepath, "node:move:vertical");
     } else if (dy == 0) {
-        update_repr_keyed(nodepath, "node:move:horizontal");
+        sp_nodepath_update_repr_keyed(nodepath, "node:move:horizontal");
     } else {
-        update_repr(nodepath);
+        sp_nodepath_update_repr(nodepath);
     }
 }
 
@@ -983,46 +1240,95 @@ sp_node_selected_move_screen(gdouble dx, gdouble dy)
     sp_nodepath_selected_nodes_move(nodepath, zdx, zdy, false);
 
     if (dx == 0) {
-        update_repr_keyed(nodepath, "node:move:vertical");
+        sp_nodepath_update_repr_keyed(nodepath, "node:move:vertical");
     } else if (dy == 0) {
-        update_repr_keyed(nodepath, "node:move:horizontal");
+        sp_nodepath_update_repr_keyed(nodepath, "node:move:horizontal");
     } else {
-        update_repr(nodepath);
+        sp_nodepath_update_repr(nodepath);
+    }
+}
+
+/** If they don't yet exist, creates knot and line for the given side of the node */
+static void sp_node_ensure_knot_exists (SPDesktop *desktop, Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeSide *side)
+{
+    if (!side->knot) {
+        side->knot = sp_knot_new(desktop, _("<b>Node handle</b>: drag to shape the curve; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to lock length; with <b>Shift</b> to rotate both handles"));
+
+        side->knot->setShape (SP_KNOT_SHAPE_CIRCLE);
+        side->knot->setSize (7);
+        side->knot->setAnchor (GTK_ANCHOR_CENTER);
+        side->knot->setFill(KNOT_FILL, KNOT_FILL_HI, KNOT_FILL_HI);
+        side->knot->setStroke(KNOT_STROKE, KNOT_STROKE_HI, KNOT_STROKE_HI);
+        sp_knot_update_ctrl(side->knot);
+
+        g_signal_connect(G_OBJECT(side->knot), "clicked", G_CALLBACK(node_handle_clicked), node);
+        g_signal_connect(G_OBJECT(side->knot), "grabbed", G_CALLBACK(node_handle_grabbed), node);
+        g_signal_connect(G_OBJECT(side->knot), "ungrabbed", G_CALLBACK(node_handle_ungrabbed), node);
+        g_signal_connect(G_OBJECT(side->knot), "request", G_CALLBACK(node_handle_request), node);
+        g_signal_connect(G_OBJECT(side->knot), "moved", G_CALLBACK(node_handle_moved), node);
+        g_signal_connect(G_OBJECT(side->knot), "event", G_CALLBACK(node_handle_event), node);
+    }
+
+    if (!side->line) {
+        side->line = sp_canvas_item_new(sp_desktop_controls(desktop),
+                                        SP_TYPE_CTRLLINE, NULL);
     }
 }
 
 /**
- * Ensure knot on side of node is visible/invisible.
+ * Ensure the given handle of the node is visible/invisible, update its screen position
  */
-static void sp_node_ensure_knot(Inkscape::NodePath::Node *node, gint which, gboolean show_knot)
+static void sp_node_update_handle(Inkscape::NodePath::Node *node, gint which, gboolean show_handle, bool fire_move_signals)
 {
     g_assert(node != NULL);
 
    Inkscape::NodePath::NodeSide *side = sp_node_get_side(node, which);
     NRPathcode code = sp_node_path_code_from_side(node, side);
 
-    show_knot = show_knot && (code == NR_CURVETO) && (NR::L2(side->pos - node->pos) > 1e-6);
+    show_handle = show_handle && (code == NR_CURVETO) && (NR::L2(side->pos - node->pos) > 1e-6);
 
-    if (show_knot) {
-        if (!SP_KNOT_IS_VISIBLE(side->knot)) {
+    if (show_handle) {
+        if (!side->knot) { // No handle knot at all
+            sp_node_ensure_knot_exists(node->subpath->nodepath->desktop, node, side);
+            // Just created, so we shouldn't fire the node_moved callback - instead set the knot position directly
+            side->knot->pos = side->pos;
+            if (side->knot->item) 
+                SP_CTRL(side->knot->item)->moveto(side->pos);
+            sp_ctrlline_set_coords(SP_CTRLLINE(side->line), node->pos, side->pos);
             sp_knot_show(side->knot);
+        } else {
+            if (side->knot->pos != side->pos) { // only if it's really moved
+                if (fire_move_signals) {
+                    sp_knot_set_position(side->knot, &side->pos, 0); // this will set coords of the line as well
+                } else {
+                    sp_knot_moveto(side->knot, &side->pos);
+                    sp_ctrlline_set_coords(SP_CTRLLINE(side->line), node->pos, side->pos);
+                }
+            }
+            if (!SP_KNOT_IS_VISIBLE(side->knot)) {
+                sp_knot_show(side->knot);
+            }
         }
-
-        sp_knot_set_position(side->knot, &side->pos, 0);
         sp_canvas_item_show(side->line);
-
     } else {
-        if (SP_KNOT_IS_VISIBLE(side->knot)) {
-            sp_knot_hide(side->knot);
+        if (side->knot) {
+            if (SP_KNOT_IS_VISIBLE(side->knot)) {
+                sp_knot_hide(side->knot);
+            }
+        }
+        if (side->line) {
+            sp_canvas_item_hide(side->line);
         }
-        sp_canvas_item_hide(side->line);
     }
 }
 
 /**
- * Ensure handles on node and neighbours of node are visible if selected.
+ * Ensure the node itself is visible, its handles and those of the neighbours of the node are
+ * visible if selected, update their screen positions. If fire_move_signals, move the node and its
+ * handles so that the corresponding signals are fired, callbacks are activated, and curve is
+ * updated; otherwise, just move the knots silently (used in batch moves).
  */
-static void sp_node_ensure_ctrls(Inkscape::NodePath::Node *node)
+static void sp_node_update_handles(Inkscape::NodePath::Node *node, bool fire_move_signals)
 {
     g_assert(node != NULL);
 
@@ -1030,44 +1336,62 @@ static void sp_node_ensure_ctrls(Inkscape::NodePath::Node *node)
         sp_knot_show(node->knot);
     }
 
-    sp_knot_set_position(node->knot, &node->pos, 0);
+    if (node->knot->pos != node->pos) { // visible knot is in a different position, need to update
+        if (fire_move_signals)
+            sp_knot_set_position(node->knot, &node->pos, 0);
+        else 
+            sp_knot_moveto(node->knot, &node->pos);
+    }
 
-    gboolean show_knots = node->selected;
+    gboolean show_handles = node->selected;
     if (node->p.other != NULL) {
-        if (node->p.other->selected) show_knots = TRUE;
+        if (node->p.other->selected) show_handles = TRUE;
     }
     if (node->n.other != NULL) {
-        if (node->n.other->selected) show_knots = TRUE;
+        if (node->n.other->selected) show_handles = TRUE;
     }
 
-    sp_node_ensure_knot(node, -1, show_knots);
-    sp_node_ensure_knot(node, 1, show_knots);
+    if (node->subpath->nodepath->show_handles == false)
+        show_handles = FALSE;
+
+    sp_node_update_handle(node, -1, show_handles, fire_move_signals);
+    sp_node_update_handle(node, 1, show_handles, fire_move_signals);
 }
 
 /**
- * Call sp_node_ensure_ctrls() for all nodes on subpath.
+ * Call sp_node_update_handles() for all nodes on subpath.
  */
-static void sp_nodepath_subpath_ensure_ctrls(Inkscape::NodePath::SubPath *subpath)
+static void sp_nodepath_subpath_update_handles(Inkscape::NodePath::SubPath *subpath)
 {
     g_assert(subpath != NULL);
 
     for (GList *l = subpath->nodes; l != NULL; l = l->next) {
-        sp_node_ensure_ctrls((Inkscape::NodePath::Node *) l->data);
+        sp_node_update_handles((Inkscape::NodePath::Node *) l->data);
     }
 }
 
 /**
- * Call sp_nodepath_subpath_ensure_ctrls() for all subpaths of nodepath.
+ * Call sp_nodepath_subpath_update_handles() for all subpaths of nodepath.
  */
-static void sp_nodepath_ensure_ctrls(Inkscape::NodePath::Path *nodepath)
+static void sp_nodepath_update_handles(Inkscape::NodePath::Path *nodepath)
 {
     g_assert(nodepath != NULL);
 
     for (GList *l = nodepath->subpaths; l != NULL; l = l->next) {
-        sp_nodepath_subpath_ensure_ctrls((Inkscape::NodePath::SubPath *) l->data);
+        sp_nodepath_subpath_update_handles((Inkscape::NodePath::SubPath *) l->data);
     }
 }
 
+void
+sp_nodepath_show_handles(bool show)
+{
+    Inkscape::NodePath::Path *nodepath = sp_nodepath_current();
+    if (nodepath == NULL) return;
+
+    nodepath->show_handles = show;
+    sp_nodepath_update_handles(nodepath);
+}
+
 /**
  * Adds all selected nodes in nodepath to list.
  */
@@ -1098,11 +1422,8 @@ void sp_nodepath_selected_align(Inkscape::NodePath::Path *nodepath, NR::Dim2 axi
             sp_node_moveto(pNode, dest);
         }
     }
-    if (axis == NR::X) {
-        update_repr_keyed(nodepath, "node:move:vertical");
-    } else {
-        update_repr_keyed(nodepath, "node:move:horizontal");
-    }
+
+    sp_nodepath_update_repr(nodepath);
 }
 
 /// Helper struct.
@@ -1164,11 +1485,7 @@ void sp_nodepath_selected_distribute(Inkscape::NodePath::Path *nodepath, NR::Dim
         pos += step;
     }
 
-    if (axis == NR::X) {
-        update_repr_keyed(nodepath, "node:move:horizontal");
-    } else {
-        update_repr_keyed(nodepath, "node:move:vertical");
-    }
+    sp_nodepath_update_repr(nodepath);
 }
 
 
@@ -1201,9 +1518,9 @@ sp_node_selected_add_node(void)
     }
 
     /** \todo fixme: adjust ? */
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1212,26 +1529,26 @@ sp_node_selected_add_node(void)
  * Select segment nearest to point
  */
 void
-sp_nodepath_select_segment_near_point(SPItem * item, NR::Point p, bool toggle)
+sp_nodepath_select_segment_near_point(Inkscape::NodePath::Path *nodepath, NR::Point p, bool toggle)
 {
-    Inkscape::NodePath::Path *nodepath = sp_nodepath_current();
     if (!nodepath) {
         return;
     }
 
-    Path::cut_position position = get_nearest_position_on_Path(item, p);
+    Path::cut_position position = get_nearest_position_on_Path(nodepath->livarot_path, p);
 
     //find segment to segment
     Inkscape::NodePath::Node *e = sp_nodepath_get_node_by_index(position.piece);
 
     gboolean force = FALSE;
-    if (!(e->selected && e->p.other->selected)) {
+    if (!(e->selected && (!e->p.other || e->p.other->selected))) {
         force = TRUE;
-    } 
+    }
     sp_nodepath_node_select(e, (gboolean) toggle, force);
-    sp_nodepath_node_select(e->p.other, TRUE, force);
+    if (e->p.other)
+        sp_nodepath_node_select(e->p.other, TRUE, force);
 
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1240,18 +1557,17 @@ sp_nodepath_select_segment_near_point(SPItem * item, NR::Point p, bool toggle)
  * Add a node nearest to point
  */
 void
-sp_nodepath_add_node_near_point(SPItem * item, NR::Point p)
+sp_nodepath_add_node_near_point(Inkscape::NodePath::Path *nodepath, NR::Point p)
 {
-    Inkscape::NodePath::Path *nodepath = sp_nodepath_current();
     if (!nodepath) {
         return;
     }
 
-    Path::cut_position position = get_nearest_position_on_Path(item, p);
+    Path::cut_position position = get_nearest_position_on_Path(nodepath->livarot_path, p);
 
     //find segment to split
     Inkscape::NodePath::Node *e = sp_nodepath_get_node_by_index(position.piece);
-   
+
     //don't know why but t seems to flip for lines
     if (sp_node_path_code_from_side(e, sp_node_get_side(e, -1)) == NR_LINETO) {
         position.t = 1.0 - position.t;
@@ -1260,9 +1576,9 @@ sp_nodepath_add_node_near_point(SPItem * item, NR::Point p)
     sp_nodepath_node_select(n, FALSE, TRUE);
 
     /* fixme: adjust ? */
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1275,7 +1591,7 @@ sp_nodepath_add_node_near_point(SPItem * item, NR::Point p)
  * cf. app/vectors/gimpbezierstroke.c, gimp_bezier_stroke_point_move_relative()
  */
 void
-sp_nodepath_curve_drag(Inkscape::NodePath::Node * e, double t, NR::Point delta, char * key) 
+sp_nodepath_curve_drag(Inkscape::NodePath::Node * e, double t, NR::Point delta)
 {
     /* feel good is an arbitrary parameter that distributes the delta between handles
      * if t of the drag point is less than 1/6 distance form the endpoint only
@@ -1290,7 +1606,7 @@ sp_nodepath_curve_drag(Inkscape::NodePath::Node * e, double t, NR::Point delta,
         feel_good = (1 - pow((6 * (1-t) - 1) / 2.0, 3)) / 2 + 0.5;
     else
         feel_good = 1;
-    
+
     //if we're dragging a line convert it to a curve
     if (sp_node_path_code_from_side(e, sp_node_get_side(e, -1))==NR_LINETO) {
         sp_nodepath_set_line_type(e, NR_CURVETO);
@@ -1301,13 +1617,13 @@ sp_nodepath_curve_drag(Inkscape::NodePath::Node * e, double t, NR::Point delta,
     e->p.other->n.pos += offsetcoord0;
     e->p.pos += offsetcoord1;
 
-    // adjust controls of adjacent segments where necessary
-    sp_node_adjust_knot(e,1);
-    sp_node_adjust_knot(e->p.other,-1);
+    // adjust handles of adjacent nodes where necessary
+    sp_node_adjust_handle(e,1);
+    sp_node_adjust_handle(e->p.other,-1);
 
-    sp_nodepath_ensure_ctrls(e->subpath->nodepath);
+    sp_nodepath_update_handles(e->subpath->nodepath);
 
-    update_repr_keyed(e->subpath->nodepath, key);
+    update_object(e->subpath->nodepath);
 
     sp_nodepath_update_statusbar(e->subpath->nodepath);
 }
@@ -1336,9 +1652,9 @@ void sp_node_selected_break()
         sp_nodepath_node_select((Inkscape::NodePath::Node *) l->data, TRUE, TRUE);
     }
 
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 }
 
 /**
@@ -1366,9 +1682,9 @@ void sp_node_selected_duplicate()
         sp_nodepath_node_select((Inkscape::NodePath::Node *) l->data, TRUE, TRUE);
     }
 
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 }
 
 /**
@@ -1398,16 +1714,22 @@ void sp_node_selected_join()
 
     /* a and b are endpoints */
 
-    NR::Point c = (a->pos + b->pos) / 2;
+    NR::Point c;
+    if (a->knot && SP_KNOT_IS_MOUSEOVER(a->knot)) {
+        c = a->pos;
+    } else if (b->knot && SP_KNOT_IS_MOUSEOVER(b->knot)) {
+        c = b->pos;
+    } else {
+        c = (a->pos + b->pos) / 2;
+    }
 
     if (a->subpath == b->subpath) {
        Inkscape::NodePath::SubPath *sp = a->subpath;
         sp_nodepath_subpath_close(sp);
+        sp_node_moveto (sp->first, c);
 
-        sp_nodepath_ensure_ctrls(sp->nodepath);
-
-        update_repr(nodepath);
-
+        sp_nodepath_update_handles(sp->nodepath);
+        sp_nodepath_update_repr(nodepath);
         return;
     }
 
@@ -1457,9 +1779,9 @@ void sp_node_selected_join()
 
     sp_nodepath_subpath_destroy(sb);
 
-    sp_nodepath_ensure_ctrls(sa->nodepath);
+    sp_nodepath_update_handles(sa->nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1498,15 +1820,15 @@ void sp_node_selected_join_segment()
         sp->first->p.other = sp->last;
         sp->last->n.other  = sp->first;
 
-        sp_node_control_mirror_p_to_n(sp->last);
-        sp_node_control_mirror_n_to_p(sp->first);
+        sp_node_handle_mirror_p_to_n(sp->last);
+        sp_node_handle_mirror_n_to_p(sp->first);
 
         sp->first->code = sp->last->code;
         sp->first       = sp->last;
 
-        sp_nodepath_ensure_ctrls(sp->nodepath);
+        sp_nodepath_update_handles(sp->nodepath);
 
-        update_repr(nodepath);
+        sp_nodepath_update_repr(nodepath);
 
         return;
     }
@@ -1537,17 +1859,17 @@ void sp_node_selected_join_segment()
 
     if (b == sb->first) {
         n = sb->first;
-        sp_node_control_mirror_p_to_n(sa->last);
+        sp_node_handle_mirror_p_to_n(sa->last);
         sp_nodepath_node_new(sa, NULL,Inkscape::NodePath::NODE_CUSP, code, &n->p.pos, &n->pos, &n->n.pos);
-        sp_node_control_mirror_n_to_p(sa->last);
+        sp_node_handle_mirror_n_to_p(sa->last);
         for (n = n->n.other; n != NULL; n = n->n.other) {
             sp_nodepath_node_new(sa, NULL, (Inkscape::NodePath::NodeType)n->type, (NRPathcode)n->code, &n->p.pos, &n->pos, &n->n.pos);
         }
     } else if (b == sb->last) {
         n = sb->last;
-        sp_node_control_mirror_p_to_n(sa->last);
+        sp_node_handle_mirror_p_to_n(sa->last);
         sp_nodepath_node_new(sa, NULL,Inkscape::NodePath::NODE_CUSP, code, &p, &n->pos, &n->p.pos);
-        sp_node_control_mirror_n_to_p(sa->last);
+        sp_node_handle_mirror_n_to_p(sa->last);
         for (n = n->p.other; n != NULL; n = n->p.other) {
             sp_nodepath_node_new(sa, NULL, (Inkscape::NodePath::NodeType)n->type, (NRPathcode)n->n.other->code, &n->n.pos, &n->pos, &n->p.pos);
         }
@@ -1558,9 +1880,137 @@ void sp_node_selected_join_segment()
 
     sp_nodepath_subpath_destroy(sb);
 
-    sp_nodepath_ensure_ctrls(sa->nodepath);
+    sp_nodepath_update_handles(sa->nodepath);
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
+}
+
+/**
+ * Delete one or more selected nodes and preserve the shape of the path as much as possible.
+ */
+void sp_node_delete_preserve(GList *nodes_to_delete)
+{
+    GSList *nodepaths = NULL;
+    
+    while (nodes_to_delete) {
+        Inkscape::NodePath::Node *node = (Inkscape::NodePath::Node*) g_list_first(nodes_to_delete)->data;
+        Inkscape::NodePath::SubPath *sp = node->subpath;
+        Inkscape::NodePath::Path *nodepath = sp->nodepath;
+        Inkscape::NodePath::Node *sample_cursor = NULL;
+        Inkscape::NodePath::Node *sample_end = NULL;
+        Inkscape::NodePath::Node *delete_cursor = node;
+        bool just_delete = false;
+        
+        //find the start of this contiguous selection
+        //move left to the first node that is not selected
+        //or the start of the non-closed path
+        for (Inkscape::NodePath::Node *curr=node->p.other; curr && curr!=node && g_list_find(nodes_to_delete, curr); curr=curr->p.other) {
+            delete_cursor = curr;
+        }
+
+        //just delete at the beginning of an open path
+        if (!delete_cursor->p.other) {
+            sample_cursor = delete_cursor;
+            just_delete = true;
+        } else {
+            sample_cursor = delete_cursor->p.other;
+        }
+        
+        //calculate points for each segment
+        int rate = 5;
+        float period = 1.0 / rate;
+        std::vector<NR::Point> data;
+        if (!just_delete) {
+            data.push_back(sample_cursor->pos);
+            for (Inkscape::NodePath::Node *curr=sample_cursor; curr; curr=curr->n.other) {
+                //just delete at the end of an open path
+                if (!sp->closed && curr->n.other == sp->last) {
+                    just_delete = true;
+                    break;
+                }
+                
+                //sample points on the contiguous selected segment
+                NR::Point *bez;
+                bez = new NR::Point [4];
+                bez[0] = curr->pos;
+                bez[1] = curr->n.pos;
+                bez[2] = curr->n.other->p.pos;
+                bez[3] = curr->n.other->pos;
+                for (int i=1; i<rate; i++) {
+                    gdouble t = i * period;
+                    NR::Point p = bezier_pt(3, bez, t);
+                    data.push_back(p);
+                }
+                data.push_back(curr->n.other->pos);
+
+                sample_end = curr->n.other;
+                //break if we've come full circle or hit the end of the selection
+                if (!g_list_find(nodes_to_delete, curr->n.other) || curr->n.other==sample_cursor) {
+                    break;
+                }
+            }
+        }
+
+        if (!just_delete) {
+            //calculate the best fitting single segment and adjust the endpoints
+            NR::Point *adata;
+            adata = new NR::Point [data.size()];
+            copy(data.begin(), data.end(), adata);
+            
+            NR::Point *bez;
+            bez = new NR::Point [4];
+            //would decreasing error create a better fitting approximation?
+            gdouble error = 1.0;
+            gint ret;
+            ret = sp_bezier_fit_cubic (bez, adata, data.size(), error);
+
+            //adjust endpoints
+            sample_cursor->n.pos = bez[1];
+            sample_end->p.pos = bez[2];
+        }
+       
+        //destroy this contiguous selection
+        while (delete_cursor && g_list_find(nodes_to_delete, delete_cursor)) {
+            Inkscape::NodePath::Node *temp = delete_cursor;
+            if (delete_cursor->n.other == delete_cursor) {
+                // delete_cursor->n points to itself, which means this is the last node on a closed subpath
+                delete_cursor = NULL; 
+            } else {
+                delete_cursor = delete_cursor->n.other;
+            }
+            nodes_to_delete = g_list_remove(nodes_to_delete, temp);
+            sp_nodepath_node_destroy(temp);
+        }
+
+        sp_nodepath_update_handles(nodepath);
+
+        if (!g_slist_find(nodepaths, nodepath))
+            nodepaths = g_slist_prepend (nodepaths, nodepath);
+    }
+
+    for (GSList *i = nodepaths; i; i = i->next) {
+        // FIXME: when/if we teach node tool to have more than one nodepath, deleting nodes from
+        // different nodepaths will give us one undo event per nodepath
+        Inkscape::NodePath::Path *nodepath = (Inkscape::NodePath::Path *) i->data;
+
+        // if the entire nodepath is removed, delete the selected object.
+        if (nodepath->subpaths == NULL ||
+            //FIXME: a closed path CAN legally have one node, it's only an open one which must be
+            //at least 2
+            sp_nodepath_get_node_count(nodepath) < 2) {
+            SPDocument *document = sp_desktop_document (nodepath->desktop);
+            //FIXME: The following line will be wrong when we have mltiple nodepaths: we only want to
+            //delete this nodepath's object, not the entire selection! (though at this time, this
+            //does not matter)
+            sp_selection_delete();
+            sp_document_done (document);
+        } else {
+            sp_nodepath_update_repr(nodepath);
+            sp_nodepath_update_statusbar(nodepath);
+        }
+    }
+
+    g_slist_free (nodepaths);
 }
 
 /**
@@ -1582,19 +2032,18 @@ void sp_node_selected_delete()
     //clean up the nodepath (such as for trivial subpaths)
     sp_nodepath_cleanup(nodepath);
 
-    sp_nodepath_ensure_ctrls(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
     // if the entire nodepath is removed, delete the selected object.
     if (nodepath->subpaths == NULL ||
         sp_nodepath_get_node_count(nodepath) < 2) {
-        SPDocument *document = SP_DT_DOCUMENT (nodepath->desktop);
-        sp_nodepath_destroy(nodepath);
+        SPDocument *document = sp_desktop_document (nodepath->desktop);
         sp_selection_delete();
         sp_document_done (document);
         return;
     }
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1751,17 +2200,9 @@ sp_node_selected_delete_segment(void)
     //clean up the nodepath (such as for trivial subpaths)
     sp_nodepath_cleanup(nodepath);
 
-    sp_nodepath_ensure_ctrls(nodepath);
-
-    update_repr(nodepath);
+    sp_nodepath_update_handles(nodepath);
 
-    // if the entire nodepath is removed, delete the selected object.
-    if (nodepath->subpaths == NULL ||
-        sp_nodepath_get_node_count(nodepath) < 2) {
-        sp_nodepath_destroy(nodepath);
-        sp_selection_delete();
-        return;
-    }
+    sp_nodepath_update_repr(nodepath);
 
     sp_nodepath_update_statusbar(nodepath);
 }
@@ -1783,7 +2224,7 @@ sp_node_selected_set_line_type(NRPathcode code)
         }
     }
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 }
 
 /**
@@ -1799,7 +2240,7 @@ sp_node_selected_set_type(Inkscape::NodePath::NodeType type)
         sp_nodepath_convert_node_type((Inkscape::NodePath::Node *) l->data, type);
     }
 
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 }
 
 /**
@@ -1810,26 +2251,20 @@ static void sp_node_set_selected(Inkscape::NodePath::Node *node, gboolean select
     node->selected = selected;
 
     if (selected) {
-        g_object_set(G_OBJECT(node->knot),
-                     "fill", NODE_FILL_SEL,
-                     "fill_mouseover", NODE_FILL_SEL_HI,
-                     "stroke", NODE_STROKE_SEL,
-                     "stroke_mouseover", NODE_STROKE_SEL_HI,
-                     "size", (node->type == Inkscape::NodePath::NODE_CUSP) ? 11 : 9,
-                     NULL);
+        node->knot->setSize ((node->type == Inkscape::NodePath::NODE_CUSP) ? 11 : 9);
+        node->knot->setFill(NODE_FILL_SEL, NODE_FILL_SEL_HI, NODE_FILL_SEL_HI);
+        node->knot->setStroke(NODE_STROKE_SEL, NODE_STROKE_SEL_HI, NODE_STROKE_SEL_HI);
+        sp_knot_update_ctrl(node->knot);
     } else {
-        g_object_set(G_OBJECT(node->knot),
-                     "fill", NODE_FILL,
-                     "fill_mouseover", NODE_FILL_HI,
-                     "stroke", NODE_STROKE,
-                     "stroke_mouseover", NODE_STROKE_HI,
-                     "size", (node->type == Inkscape::NodePath::NODE_CUSP) ? 9 : 7,
-                     NULL);
+        node->knot->setSize ((node->type == Inkscape::NodePath::NODE_CUSP) ? 9 : 7);
+        node->knot->setFill(NODE_FILL, NODE_FILL_HI, NODE_FILL_HI);
+        node->knot->setStroke(NODE_STROKE, NODE_STROKE_HI, NODE_STROKE_HI);
+        sp_knot_update_ctrl(node->knot);
     }
 
-    sp_node_ensure_ctrls(node);
-    if (node->n.other) sp_node_ensure_ctrls(node->n.other);
-    if (node->p.other) sp_node_ensure_ctrls(node->p.other);
+    sp_node_update_handles(node);
+    if (node->n.other) sp_node_update_handles(node->n.other);
+    if (node->p.other) sp_node_update_handles(node->p.other);
 }
 
 /**
@@ -1845,7 +2280,7 @@ static void sp_nodepath_node_select(Inkscape::NodePath::Node *node, gboolean inc
     if (incremental) {
         if (override) {
             if (!g_list_find(nodepath->selected, node)) {
-                nodepath->selected = g_list_append(nodepath->selected, node);
+                nodepath->selected = g_list_prepend(nodepath->selected, node);
             }
             sp_node_set_selected(node, TRUE);
         } else { // toggle
@@ -1854,13 +2289,13 @@ static void sp_nodepath_node_select(Inkscape::NodePath::Node *node, gboolean inc
                 nodepath->selected = g_list_remove(nodepath->selected, node);
             } else {
                 g_assert(!g_list_find(nodepath->selected, node));
-                nodepath->selected = g_list_append(nodepath->selected, node);
+                nodepath->selected = g_list_prepend(nodepath->selected, node);
             }
             sp_node_set_selected(node, !node->selected);
         }
     } else {
         sp_nodepath_deselect(nodepath);
-        nodepath->selected = g_list_append(nodepath->selected, node);
+        nodepath->selected = g_list_prepend(nodepath->selected, node);
         sp_node_set_selected(node, TRUE);
     }
 
@@ -1900,10 +2335,10 @@ sp_nodepath_select_all(Inkscape::NodePath::Path *nodepath, bool invert)
     }
 }
 
-/** 
- * If nothing selected, does the same as sp_nodepath_select_all(); 
- * otherwise selects/inverts all nodes in all subpaths that have selected nodes 
- * (i.e., similar to "select all in layer", with the "selected" subpaths 
+/**
+ * If nothing selected, does the same as sp_nodepath_select_all();
+ * otherwise selects/inverts all nodes in all subpaths that have selected nodes
+ * (i.e., similar to "select all in layer", with the "selected" subpaths
  * being treated as "layers" in the path).
  */
 void
@@ -1939,7 +2374,7 @@ sp_nodepath_select_all_from_subpath(Inkscape::NodePath::Path *nodepath, bool inv
 }
 
 /**
- * \brief Select the node after the last selected; if none is selected, 
+ * \brief Select the node after the last selected; if none is selected,
  * select the first within path.
  */
 void sp_nodepath_select_next(Inkscape::NodePath::Path *nodepath)
@@ -1996,7 +2431,7 @@ void sp_nodepath_select_next(Inkscape::NodePath::Path *nodepath)
 }
 
 /**
- * \brief Select the node before the first selected; if none is selected, 
+ * \brief Select the node before the first selected; if none is selected,
  * select the last within path
  */
 void sp_nodepath_select_prev(Inkscape::NodePath::Path *nodepath)
@@ -2073,6 +2508,24 @@ void sp_nodepath_select_rect(Inkscape::NodePath::Path *nodepath, NR::Rect const
     }
 }
 
+
+/**
+\brief  Saves all nodes' and handles' current positions in their origin members
+*/
+void
+sp_nodepath_remember_origins(Inkscape::NodePath::Path *nodepath)
+{
+    for (GList *spl = nodepath->subpaths; spl != NULL; spl = spl->next) {
+       Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data;
+        for (GList *nl = subpath->nodes; nl != NULL; nl = nl->next) {
+           Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) nl->data;
+           n->origin = n->pos;
+           n->p.origin = n->p.pos;
+           n->n.origin = n->n.pos;
+        }
+    }
+} 
+
 /**
 \brief  Saves selected nodes in a nodepath into a list containing integer positions of all selected nodes
 */
@@ -2119,9 +2572,9 @@ void restore_nodepath_selection(Inkscape::NodePath::Path *nodepath, GList *r)
 }
 
 /**
-\brief Adjusts control point according to node type and line code.
+\brief Adjusts handle according to node type and line code.
 */
-static void sp_node_adjust_knot(Inkscape::NodePath::Node *node, gint which_adjust)
+static void sp_node_adjust_handle(Inkscape::NodePath::Node *node, gint which_adjust)
 {
     double len, otherlen, linelen;
 
@@ -2162,21 +2615,15 @@ static void sp_node_adjust_knot(Inkscape::NodePath::Node *node, gint which_adjus
         len = NR::L2(me->pos - node->pos);
         delta = node->pos - othernode->pos;
         linelen = NR::L2(delta);
-        if (linelen < 1e-18) return;
-
+        if (linelen < 1e-18) 
+            return;
         me->pos = node->pos + (len / linelen)*delta;
-        sp_knot_set_position(me->knot, &me->pos, 0);
-
-        sp_node_ensure_ctrls(node);
         return;
     }
 
     if (node->type == Inkscape::NodePath::NODE_SYMM) {
 
         me->pos = 2 * node->pos - other->pos;
-        sp_knot_set_position(me->knot, &me->pos, 0);
-
-        sp_node_ensure_ctrls(node);
         return;
     }
 
@@ -2188,15 +2635,12 @@ static void sp_node_adjust_knot(Inkscape::NodePath::Node *node, gint which_adjus
     if (otherlen < 1e-18) return;
 
     me->pos = node->pos - (len / otherlen) * delta;
-    sp_knot_set_position(me->knot, &me->pos, 0);
-
-    sp_node_ensure_ctrls(node);
 }
 
 /**
- \brief Adjusts control point according to node type and line code
+ \brief Adjusts both handles according to node type and line code
  */
-static void sp_node_adjust_knots(Inkscape::NodePath::Node *node)
+static void sp_node_adjust_handles(Inkscape::NodePath::Node *node)
 {
     g_assert(node);
 
@@ -2210,42 +2654,36 @@ static void sp_node_adjust_knots(Inkscape::NodePath::Node *node)
 
     if (node->code == NR_LINETO) {
         if (node->n.other->code == NR_LINETO) return;
-        sp_node_adjust_knot(node, 1);
-        sp_node_ensure_ctrls(node);
+        sp_node_adjust_handle(node, 1);
         return;
     }
 
     if (node->n.other->code == NR_LINETO) {
         if (node->code == NR_LINETO) return;
-        sp_node_adjust_knot(node, -1);
-        sp_node_ensure_ctrls(node);
+        sp_node_adjust_handle(node, -1);
         return;
     }
 
     /* both are curves */
-
     NR::Point const delta( node->n.pos - node->p.pos );
 
     if (node->type == Inkscape::NodePath::NODE_SYMM) {
         node->p.pos = node->pos - delta / 2;
         node->n.pos = node->pos + delta / 2;
-        sp_node_ensure_ctrls(node);
         return;
     }
 
     /* We are smooth */
-
     double plen = NR::L2(node->p.pos - node->pos);
     if (plen < 1e-18) return;
     double nlen = NR::L2(node->n.pos - node->pos);
     if (nlen < 1e-18) return;
     node->p.pos = node->pos - (plen / (plen + nlen)) * delta;
     node->n.pos = node->pos + (nlen / (plen + nlen)) * delta;
-    sp_node_ensure_ctrls(node);
 }
 
 /**
- * Knot events handler callback.
+ * Node event callback.
  */
 static gboolean node_event(SPKnot *knot, GdkEvent *event,Inkscape::NodePath::Node *n)
 {
@@ -2294,7 +2732,7 @@ gboolean node_key(GdkEvent *event)
             case GDK_BackSpace:
                 np = active_node->subpath->nodepath;
                 sp_nodepath_node_destroy(active_node);
-                update_repr(np);
+                sp_nodepath_update_repr(np);
                 active_node = NULL;
                 ret = TRUE;
                 break;
@@ -2338,27 +2776,13 @@ static void node_clicked(SPKnot *knot, guint state, gpointer data)
             } else {
                 sp_nodepath_convert_node_type (n,Inkscape::NodePath::NODE_CUSP);
             }
-            update_repr(nodepath);
+            sp_nodepath_update_repr(nodepath);
             sp_nodepath_update_statusbar(nodepath);
 
         } else { //ctrl+alt+click: delete node
-            sp_nodepath_node_destroy(n);
-            //clean up the nodepath (such as for trivial subpaths)
-            sp_nodepath_cleanup(nodepath);
-
-            // if the entire nodepath is removed, delete the selected object.
-            if (nodepath->subpaths == NULL ||
-                sp_nodepath_get_node_count(nodepath) < 2) {
-                SPDocument *document = SP_DT_DOCUMENT (nodepath->desktop);
-                sp_nodepath_destroy(nodepath);
-                sp_selection_delete();
-                sp_document_done (document);
-
-            } else {
-                sp_nodepath_ensure_ctrls(nodepath);
-                update_repr(nodepath);
-                sp_nodepath_update_statusbar(nodepath);
-            }
+            GList *node_to_delete = NULL;
+            node_to_delete = g_list_append(node_to_delete, n);
+            sp_node_delete_preserve(node_to_delete);
         }
 
     } else {
@@ -2373,11 +2797,11 @@ static void node_grabbed(SPKnot *knot, guint state, gpointer data)
 {
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
-    n->origin = knot->pos;
-
     if (!n->selected) {
         sp_nodepath_node_select(n, (state & GDK_SHIFT_MASK), FALSE);
     }
+
+    sp_nodepath_remember_origins (n->subpath->nodepath);
 }
 
 /**
@@ -2389,7 +2813,7 @@ static void node_ungrabbed(SPKnot *knot, guint state, gpointer data)
 
    n->dragging_out = NULL;
 
-   update_repr(n->subpath->nodepath);
+   sp_nodepath_update_repr(n->subpath->nodepath);
 }
 
 /**
@@ -2438,7 +2862,7 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
    // If either (Shift and some handle retracted), or (we're already dragging out a handle)
-   if (((state & GDK_SHIFT_MASK) && ((n->n.other && n->n.pos == n->pos) || (n->p.other && n->p.pos == n->pos))) || n->dragging_out) { 
+   if (((state & GDK_SHIFT_MASK) && ((n->n.other && n->n.pos == n->pos) || (n->p.other && n->p.pos == n->pos))) || n->dragging_out) {
 
        NR::Point mouse = (*p);
 
@@ -2487,11 +2911,15 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
            if (opposite->pos != n->pos) {
                mouse = n->pos - NR::L2(mouse - n->pos) * NR::unit_vector(opposite->pos - n->pos);
            }
+
+           // knots might not be created yet!
+           sp_node_ensure_knot_exists (n->subpath->nodepath->desktop, n, n->dragging_out);
+           sp_node_ensure_knot_exists (n->subpath->nodepath->desktop, n, opposite);
        }
 
        // pass this on to the handle-moved callback
-       node_ctrl_moved(n->dragging_out->knot, &mouse, state, (gpointer) n);
-       sp_node_ensure_ctrls(n);
+       node_handle_moved(n->dragging_out->knot, &mouse, state, (gpointer) n);
+       sp_node_update_handles(n);
        return TRUE;
    }
 
@@ -2505,8 +2933,8 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
         if ((n->n.other && n->n.other->code == NR_LINETO) || fabs(yn) + fabs(xn) < 1e-6) {
             if (n->n.other) { // if there is the next point
                 if (L2(n->n.other->p.pos - n->n.other->pos) < 1e-6) // and the next point has no handle either
-                    yn = n->n.other->pos[NR::Y] - n->origin[NR::Y]; // use origin because otherwise the direction will change as you drag
-                    xn = n->n.other->pos[NR::X] - n->origin[NR::X];
+                    yn = n->n.other->origin[NR::Y] - n->origin[NR::Y]; // use origin because otherwise the direction will change as you drag
+                    xn = n->n.other->origin[NR::X] - n->origin[NR::X];
             }
         }
         if (xn < 0) { xn = -xn; yn = -yn; } // limit the angle to between 0 and pi
@@ -2519,8 +2947,8 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
         if (n->code == NR_LINETO || fabs(yp) + fabs(xp) < 1e-6) {
             if (n->p.other) {
                 if (L2(n->p.other->n.pos - n->p.other->pos) < 1e-6)
-                    yp = n->p.other->pos[NR::Y] - n->origin[NR::Y];
-                    xp = n->p.other->pos[NR::X] - n->origin[NR::X];
+                    yp = n->p.other->origin[NR::Y] - n->origin[NR::Y];
+                    xp = n->p.other->origin[NR::X] - n->origin[NR::X];
             }
         }
         if (xp < 0) { xp = -xp; yp = -yp; } // limit the angle to between 0 and pi
@@ -2530,7 +2958,7 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
             // sliding on handles, only if at least one of the handles is non-vertical
             // (otherwise it's the same as ctrl+drag anyway)
 
-            // calculate angles of the control handles
+            // calculate angles of the handles
             if (xn == 0) {
                 if (yn == 0) { // no handle, consider it the continuation of the other one
                     an = 0;
@@ -2552,8 +2980,6 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
             if (an == 0) na = HUGE_VAL; else na = -1/an;
             if (ap == 0) pa = HUGE_VAL; else pa = -1/ap;
 
-            //g_print("an %g    ap %g\n", an, ap);
-
             // mouse point relative to the node's original pos
             pr = (*p) - n->origin;
 
@@ -2588,10 +3014,14 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
             }
         }
     } else { // move freely
-        sp_nodepath_selected_nodes_move(n->subpath->nodepath,
+        if (state & GDK_MOD1_MASK) { // sculpt
+            sp_nodepath_selected_nodes_sculpt(n->subpath->nodepath, n, (*p) - n->origin);
+        } else {
+            sp_nodepath_selected_nodes_move(n->subpath->nodepath,
                                         (*p)[NR::X] - n->pos[NR::X],
                                         (*p)[NR::Y] - n->pos[NR::Y],
                                         (state & GDK_SHIFT_MASK) == 0);
+       }
     }
 
     n->subpath->nodepath->desktop->scroll_to_point(p);
@@ -2602,7 +3032,7 @@ node_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
 /**
  * Node handle clicked callback.
  */
-static void node_ctrl_clicked(SPKnot *knot, guint state, gpointer data)
+static void node_handle_clicked(SPKnot *knot, guint state, gpointer data)
 {
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
@@ -2612,9 +3042,9 @@ static void node_ctrl_clicked(SPKnot *knot, guint state, gpointer data)
         } else if (n->n.knot == knot) {
             n->n.pos = n->pos;
         }
-        sp_node_ensure_ctrls(n);
+        sp_node_update_handles(n);
         Inkscape::NodePath::Path *nodepath = n->subpath->nodepath;
-        update_repr(nodepath);
+        sp_nodepath_update_repr(nodepath);
         sp_nodepath_update_statusbar(nodepath);
 
     } else { // just select or add to selection, depending in Shift
@@ -2625,7 +3055,7 @@ static void node_ctrl_clicked(SPKnot *knot, guint state, gpointer data)
 /**
  * Node handle grabbed callback.
  */
-static void node_ctrl_grabbed(SPKnot *knot, guint state, gpointer data)
+static void node_handle_grabbed(SPKnot *knot, guint state, gpointer data)
 {
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
@@ -2633,11 +3063,11 @@ static void node_ctrl_grabbed(SPKnot *knot, guint state, gpointer data)
         sp_nodepath_node_select(n, (state & GDK_SHIFT_MASK), FALSE);
     }
 
-    // remember the origin of the control
+    // remember the origin point of the handle
     if (n->p.knot == knot) {
-        n->p.origin = n->p.pos - n->pos;
+        n->p.origin_radial = n->p.pos - n->pos;
     } else if (n->n.knot == knot) {
-        n->n.origin = n->n.pos - n->pos;
+        n->n.origin_radial = n->n.pos - n->pos;
     } else {
         g_assert_not_reached();
     }
@@ -2647,28 +3077,28 @@ static void node_ctrl_grabbed(SPKnot *knot, guint state, gpointer data)
 /**
  * Node handle ungrabbed callback.
  */
-static void node_ctrl_ungrabbed(SPKnot *knot, guint state, gpointer data)
+static void node_handle_ungrabbed(SPKnot *knot, guint state, gpointer data)
 {
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
     // forget origin and set knot position once more (because it can be wrong now due to restrictions)
     if (n->p.knot == knot) {
-        n->p.origin.a = 0;
+        n->p.origin_radial.a = 0;
         sp_knot_set_position(knot, &n->p.pos, state);
     } else if (n->n.knot == knot) {
-        n->n.origin.a = 0;
+        n->n.origin_radial.a = 0;
         sp_knot_set_position(knot, &n->n.pos, state);
     } else {
         g_assert_not_reached();
     }
 
-    update_repr(n->subpath->nodepath);
+    sp_nodepath_update_repr(n->subpath->nodepath);
 }
 
 /**
  * Node handle "request" signal callback.
  */
-static gboolean node_ctrl_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
+static gboolean node_handle_request(SPKnot *knot, NR::Point *p, guint state, gpointer data)
 {
     Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
@@ -2690,7 +3120,7 @@ static gboolean node_ctrl_request(SPKnot *knot, NR::Point *p, guint state, gpoin
 
     NRPathcode const othercode = sp_node_path_code_from_side(n, opposite);
 
-    SnapManager const m(n->subpath->nodepath->desktop->namedview);
+    SnapManager const &m = n->subpath->nodepath->desktop->namedview->snap_manager;
 
     if (opposite->other && (n->type != Inkscape::NodePath::NODE_CUSP) && (othercode == NR_LINETO)) {
         /* We are smooth node adjacent with line */
@@ -2703,12 +3133,12 @@ static gboolean node_ctrl_request(SPKnot *knot, NR::Point *p, guint state, gpoin
             NR::Coord const scal = dot(delta, ndelta) / linelen;
             (*p) = n->pos + (scal / linelen) * ndelta;
         }
-        *p = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT, *p, ndelta, NULL).getPoint();
+        *p = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT, *p, Inkscape::Snapper::ConstraintLine(*p, ndelta), NULL).getPoint();
     } else {
         *p = m.freeSnap(Inkscape::Snapper::SNAP_POINT, *p, NULL).getPoint();
     }
 
-    sp_node_adjust_knot(n, -which);
+    sp_node_adjust_handle(n, -which);
 
     return FALSE;
 }
@@ -2716,7 +3146,7 @@ static gboolean node_ctrl_request(SPKnot *knot, NR::Point *p, guint state, gpoin
 /**
  * Node handle moved callback.
  */
-static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer data)
+static void node_handle_moved(SPKnot *knot, NR::Point *p, guint state, gpointer data)
 {
    Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) data;
 
@@ -2734,7 +3164,7 @@ static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer da
         g_assert_not_reached();
     }
 
-    // calculate radial coordinates of the grabbed control, other control, and the mouse point
+    // calculate radial coordinates of the grabbed handle, its other handle, and the mouse point
     Radial rme(me->pos - n->pos);
     Radial rother(other->pos - n->pos);
     Radial rnew(*p - n->pos);
@@ -2745,13 +3175,13 @@ static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer da
 
         // The closest PI/snaps angle, starting from zero.
         double const a_snapped = floor(rnew.a/(M_PI/snaps) + 0.5) * (M_PI/snaps);
-        if (me->origin.a == HUGE_VAL) {
-            // ortho doesn't exist: original control was zero length.
+        if (me->origin_radial.a == HUGE_VAL) {
+            // ortho doesn't exist: original handle was zero length.
             rnew.a = a_snapped;
         } else {
             /* The closest PI/2 angle, starting from original angle (i.e. snapping to original,
              * its opposite and perpendiculars). */
-            double const a_ortho = me->origin.a + floor((rnew.a - me->origin.a)/(M_PI/2) + 0.5) * (M_PI/2);
+            double const a_ortho = me->origin_radial.a + floor((rnew.a - me->origin_radial.a)/(M_PI/2) + 0.5) * (M_PI/2);
 
             // Snap to the closest.
             rnew.a = ( fabs(a_snapped - rnew.a) < fabs(a_ortho - rnew.a)
@@ -2762,7 +3192,7 @@ static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer da
 
     if (state & GDK_MOD1_MASK) {
         // lock handle length
-        rnew.r = me->origin.r;
+        rnew.r = me->origin_radial.r;
     }
 
     if (( n->type !=Inkscape::NodePath::NODE_CUSP || (state & GDK_SHIFT_MASK))
@@ -2810,7 +3240,7 @@ static void node_ctrl_moved(SPKnot *knot, NR::Point *p, guint state, gpointer da
 /**
  * Node handle event callback.
  */
-static gboolean node_ctrl_event(SPKnot *knot, GdkEvent *event,Inkscape::NodePath::Node *n)
+static gboolean node_handle_event(SPKnot *knot, GdkEvent *event,Inkscape::NodePath::Node *n)
 {
     gboolean ret = FALSE;
     switch (event->type) {
@@ -2931,7 +3361,9 @@ static void node_rotate_one (Inkscape::NodePath::Node *n, gdouble angle, int whi
         other->pos =  n->pos + NR::Point(rother);
     }
 
-    sp_node_ensure_ctrls(n);
+    // this function is only called from sp_nodepath_selected_nodes_rotate that will update display at the end,
+    // so here we just move all the knots without emitting move signals, for speed
+    sp_node_update_handles(n, false);
 }
 
 /**
@@ -2949,7 +3381,7 @@ void sp_nodepath_selected_nodes_rotate(Inkscape::NodePath::Path *nodepath, gdoub
 
         Inkscape::NodePath::Node *n0 = (Inkscape::NodePath::Node *) nodepath->selected->data;
         NR::Rect box (n0->pos, n0->pos); // originally includes the first selected node
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             box.expandTo (n->pos); // contain all selected nodes
         }
@@ -2964,23 +3396,21 @@ void sp_nodepath_selected_nodes_rotate(Inkscape::NodePath::Path *nodepath, gdoub
             rot = angle;
         }
 
-        NR::Matrix t = 
-            NR::Matrix (NR::translate(-box.midpoint())) * 
-            NR::Matrix (NR::rotate(rot)) * 
+        NR::Matrix t =
+            NR::Matrix (NR::translate(-box.midpoint())) *
+            NR::Matrix (NR::rotate(rot)) *
             NR::Matrix (NR::translate(box.midpoint()));
 
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             n->pos *= t;
             n->n.pos *= t;
             n->p.pos *= t;
-            sp_node_ensure_ctrls(n);
+            sp_node_update_handles(n, false);
         }
     }
 
-    update_object(nodepath);
-    /// \todo fixme: use _keyed
-    update_repr(nodepath);
+    sp_nodepath_update_repr_keyed(nodepath, angle > 0 ? "nodes:rot:p" : "nodes:rot:n");
 }
 
 /**
@@ -3001,14 +3431,14 @@ static void node_scale_one (Inkscape::NodePath::Node *n, gdouble grow, int which
     } else if (!n->p.other) {
         me = &(n->n);
         other = &(n->p);
-        if (n->n.other)    
+        if (n->n.other)
             n->n.other->code = NR_CURVETO;
     } else {
         if (which > 0) { // right handle
             if (xn > xp) {
                 me = &(n->n);
                 other = &(n->p);
-                if (n->n.other)    
+                if (n->n.other)
                     n->n.other->code = NR_CURVETO;
             } else {
                 me = &(n->p);
@@ -3019,7 +3449,7 @@ static void node_scale_one (Inkscape::NodePath::Node *n, gdouble grow, int which
             if (xn <= xp) {
                 me = &(n->n);
                 other = &(n->p);
-                if (n->n.other)    
+                if (n->n.other)
                     n->n.other->code = NR_CURVETO;
             } else {
                 me = &(n->p);
@@ -3031,7 +3461,7 @@ static void node_scale_one (Inkscape::NodePath::Node *n, gdouble grow, int which
             other = &(n->p);
             both = true;
             n->code = NR_CURVETO;
-            if (n->n.other)    
+            if (n->n.other)
                 n->n.other->code = NR_CURVETO;
         }
     }
@@ -3063,7 +3493,9 @@ static void node_scale_one (Inkscape::NodePath::Node *n, gdouble grow, int which
         other->pos = n->pos + NR::Point(rother);
     }
 
-    sp_node_ensure_ctrls(n);
+    // this function is only called from sp_nodepath_selected_nodes_scale that will update display at the end,
+    // so here we just move all the knots without emitting move signals, for speed
+    sp_node_update_handles(n, false);
 }
 
 /**
@@ -3082,30 +3514,28 @@ void sp_nodepath_selected_nodes_scale(Inkscape::NodePath::Path *nodepath, gdoubl
 
         Inkscape::NodePath::Node *n0 = (Inkscape::NodePath::Node *) nodepath->selected->data;
         NR::Rect box (n0->pos, n0->pos); // originally includes the first selected node
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             box.expandTo (n->pos); // contain all selected nodes
         }
 
         double scale = (box.maxExtent() + grow)/box.maxExtent();
 
-        NR::Matrix t = 
-            NR::Matrix (NR::translate(-box.midpoint())) * 
-            NR::Matrix (NR::scale(scale, scale)) * 
+        NR::Matrix t =
+            NR::Matrix (NR::translate(-box.midpoint())) *
+            NR::Matrix (NR::scale(scale, scale)) *
             NR::Matrix (NR::translate(box.midpoint()));
 
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             n->pos *= t;
             n->n.pos *= t;
             n->p.pos *= t;
-            sp_node_ensure_ctrls(n);
+            sp_node_update_handles(n, false);
         }
     }
 
-    update_object(nodepath);
-    /// \todo fixme: use _keyed
-    update_repr(nodepath);
+    sp_nodepath_update_repr_keyed(nodepath, grow > 0 ? "nodes:scale:p" : "nodes:scale:n");
 }
 
 void sp_nodepath_selected_nodes_scale_screen(Inkscape::NodePath::Path *nodepath, gdouble const grow, int const which)
@@ -3127,34 +3557,32 @@ void sp_nodepath_flip (Inkscape::NodePath::Path *nodepath, NR::Dim2 axis)
         double temp = n->p.pos[axis];
         n->p.pos[axis] = n->n.pos[axis];
         n->n.pos[axis] = temp;
-        sp_node_ensure_ctrls(n);
+        sp_node_update_handles(n, false);
     } else {
         // scale nodes as an "object":
 
         Inkscape::NodePath::Node *n0 = (Inkscape::NodePath::Node *) nodepath->selected->data;
         NR::Rect box (n0->pos, n0->pos); // originally includes the first selected node
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             box.expandTo (n->pos); // contain all selected nodes
         }
 
-        NR::Matrix t = 
-            NR::Matrix (NR::translate(-box.midpoint())) * 
-            NR::Matrix ((axis == NR::X)? NR::scale(-1, 1) : NR::scale(1, -1)) * 
+        NR::Matrix t =
+            NR::Matrix (NR::translate(-box.midpoint())) *
+            NR::Matrix ((axis == NR::X)? NR::scale(-1, 1) : NR::scale(1, -1)) *
             NR::Matrix (NR::translate(box.midpoint()));
 
-        for (GList *l = nodepath->selected; l != NULL; l = l->next) { 
+        for (GList *l = nodepath->selected; l != NULL; l = l->next) {
             Inkscape::NodePath::Node *n = (Inkscape::NodePath::Node *) l->data;
             n->pos *= t;
             n->n.pos *= t;
             n->p.pos *= t;
-            sp_node_ensure_ctrls(n);
+            sp_node_update_handles(n, false);
         }
     }
 
-    update_object(nodepath);
-    /// \todo fixme: use _keyed
-    update_repr(nodepath);
+    sp_nodepath_update_repr(nodepath);
 }
 
 //-----------------------------------------------
@@ -3174,13 +3602,9 @@ static Inkscape::NodePath::SubPath *sp_nodepath_subpath_new(Inkscape::NodePath::
     s->first = NULL;
     s->last = NULL;
 
-    // do not use prepend here because:
-    // if you have a path like "subpath_1 subpath_2 ... subpath_k" in the svg, you end up with
-    // subpath_k -> ... ->subpath_1 in the nodepath structure. thus the i-th node of the svg is not
-    // the i-th node in the nodepath (only if there are multiple subpaths)
-    // note that the problem only arise when called from subpath_from_bpath(), since for all the other
-    // cases, the repr is updated after the call to sp_nodepath_subpath_new()
-    nodepath->subpaths = g_list_append /*g_list_prepend*/ (nodepath->subpaths, s);
+    // using prepend here saves up to 10% of time on paths with many subpaths, but requires that
+    // the caller reverses the list after it's ready (this is done in sp_nodepath_new)
+    nodepath->subpaths = g_list_prepend (nodepath->subpaths, s);
 
     return s;
 }
@@ -3217,7 +3641,7 @@ static void sp_nodepath_subpath_close(Inkscape::NodePath::SubPath *sp)
     //Link the head to the tail
     sp->first->p.other = sp->last;
     sp->last->n.other  = sp->first;
-    sp->last->n.pos    = sp->first->n.pos;
+    sp->last->n.pos    = sp->last->pos + (sp->first->n.pos - sp->first->pos);
     sp->first          = sp->last;
 
     //Remove the extra end node
@@ -3252,7 +3676,7 @@ static void sp_nodepath_subpath_open(Inkscape::NodePath::SubPath *sp,Inkscape::N
  * Returns area in triangle given by points; may be negative.
  */
 inline double
-triangle_area (NR::Point p1, NR::Point p2, NR::Point p3) 
+triangle_area (NR::Point p1, NR::Point p2, NR::Point p3)
 {
     return (p1[NR::X]*p2[NR::Y] + p1[NR::Y]*p3[NR::X] + p2[NR::X]*p3[NR::Y] - p2[NR::Y]*p3[NR::X] - p1[NR::Y]*p2[NR::X] - p1[NR::X]*p3[NR::Y]);
 }
@@ -3304,7 +3728,7 @@ sp_nodepath_node_new(Inkscape::NodePath::SubPath *sp, Inkscape::NodePath::Node *
 
     Inkscape::NodePath::Node *prev;
     if (next) {
-        g_assert(g_list_find(sp->nodes, next));
+        //g_assert(g_list_find(sp->nodes, next));
         prev = next->p.other;
     } else {
         prev = sp->last;
@@ -3323,20 +3747,15 @@ sp_nodepath_node_new(Inkscape::NodePath::SubPath *sp, Inkscape::NodePath::Node *
     n->p.other = prev;
     n->n.other = next;
 
-    n->knot = sp_knot_new(sp->nodepath->desktop);
+    n->knot = sp_knot_new(sp->nodepath->desktop, _("<b>Node</b>: drag to edit the path; with <b>Ctrl</b> to snap to horizontal/vertical; with <b>Ctrl+Alt</b> to snap to handles' directions"));
     sp_knot_set_position(n->knot, pos, 0);
-    g_object_set(G_OBJECT(n->knot),
-                 "anchor", GTK_ANCHOR_CENTER,
-                 "fill", NODE_FILL,
-                 "fill_mouseover", NODE_FILL_HI,
-                 "stroke", NODE_STROKE,
-                 "stroke_mouseover", NODE_STROKE_HI,
-                 "tip", _("<b>Node</b>: drag to edit the path; with <b>Ctrl</b> to snap to horizontal/vertical; with <b>Ctrl+Alt</b> to snap to handles' directions"),
-                 NULL);
-    if (n->type == Inkscape::NodePath::NODE_CUSP)
-        g_object_set(G_OBJECT(n->knot), "shape", SP_KNOT_SHAPE_DIAMOND, "size", 9, NULL);
-    else
-        g_object_set(G_OBJECT(n->knot), "shape", SP_KNOT_SHAPE_SQUARE, "size", 7, NULL);
+
+    n->knot->setShape ((n->type == Inkscape::NodePath::NODE_CUSP)? SP_KNOT_SHAPE_DIAMOND : SP_KNOT_SHAPE_SQUARE);
+    n->knot->setSize ((n->type == Inkscape::NodePath::NODE_CUSP)? 9 : 7);
+    n->knot->setAnchor (GTK_ANCHOR_CENTER);
+    n->knot->setFill(NODE_FILL, NODE_FILL_HI, NODE_FILL_HI);
+    n->knot->setStroke(NODE_STROKE, NODE_STROKE_HI, NODE_STROKE_HI);
+    sp_knot_update_ctrl(n->knot);
 
     g_signal_connect(G_OBJECT(n->knot), "event", G_CALLBACK(node_event), n);
     g_signal_connect(G_OBJECT(n->knot), "clicked", G_CALLBACK(node_clicked), n);
@@ -3345,52 +3764,11 @@ sp_nodepath_node_new(Inkscape::NodePath::SubPath *sp, Inkscape::NodePath::Node *
     g_signal_connect(G_OBJECT(n->knot), "request", G_CALLBACK(node_request), n);
     sp_knot_show(n->knot);
 
-    n->p.knot = sp_knot_new(sp->nodepath->desktop);
-    sp_knot_set_position(n->p.knot, ppos, 0);
-    g_object_set(G_OBJECT(n->p.knot),
-                 "shape", SP_KNOT_SHAPE_CIRCLE,
-                 "size", 7,
-                 "anchor", GTK_ANCHOR_CENTER,
-                 "fill", KNOT_FILL,
-                 "fill_mouseover", KNOT_FILL_HI,
-                 "stroke", KNOT_STROKE,
-                 "stroke_mouseover", KNOT_STROKE_HI,
-                 "tip", _("<b>Node handle</b>: drag to shape the curve; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to lock length; with <b>Shift</b> to rotate both handles"),
-                 NULL);
-    g_signal_connect(G_OBJECT(n->p.knot), "clicked", G_CALLBACK(node_ctrl_clicked), n);
-    g_signal_connect(G_OBJECT(n->p.knot), "grabbed", G_CALLBACK(node_ctrl_grabbed), n);
-    g_signal_connect(G_OBJECT(n->p.knot), "ungrabbed", G_CALLBACK(node_ctrl_ungrabbed), n);
-    g_signal_connect(G_OBJECT(n->p.knot), "request", G_CALLBACK(node_ctrl_request), n);
-    g_signal_connect(G_OBJECT(n->p.knot), "moved", G_CALLBACK(node_ctrl_moved), n);
-    g_signal_connect(G_OBJECT(n->p.knot), "event", G_CALLBACK(node_ctrl_event), n);
-
-    sp_knot_hide(n->p.knot);
-    n->p.line = sp_canvas_item_new(SP_DT_CONTROLS(n->subpath->nodepath->desktop),
-                                   SP_TYPE_CTRLLINE, NULL);
-    sp_canvas_item_hide(n->p.line);
-
-    n->n.knot = sp_knot_new(sp->nodepath->desktop);
-    sp_knot_set_position(n->n.knot, npos, 0);
-    g_object_set(G_OBJECT(n->n.knot),
-                 "shape", SP_KNOT_SHAPE_CIRCLE,
-                 "size", 7,
-                 "anchor", GTK_ANCHOR_CENTER,
-                 "fill", KNOT_FILL,
-                 "fill_mouseover", KNOT_FILL_HI,
-                 "stroke", KNOT_STROKE,
-                 "stroke_mouseover", KNOT_STROKE_HI,
-                 "tip", _("<b>Node handle</b>: drag to shape the curve; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to lock length; with <b>Shift</b> to rotate the opposite handle in sync"),
-                 NULL);
-    g_signal_connect(G_OBJECT(n->n.knot), "clicked", G_CALLBACK(node_ctrl_clicked), n);
-    g_signal_connect(G_OBJECT(n->n.knot), "grabbed", G_CALLBACK(node_ctrl_grabbed), n);
-    g_signal_connect(G_OBJECT(n->n.knot), "ungrabbed", G_CALLBACK(node_ctrl_ungrabbed), n);
-    g_signal_connect(G_OBJECT(n->n.knot), "request", G_CALLBACK(node_ctrl_request), n);
-    g_signal_connect(G_OBJECT(n->n.knot), "moved", G_CALLBACK(node_ctrl_moved), n);
-    g_signal_connect(G_OBJECT(n->n.knot), "event", G_CALLBACK(node_ctrl_event), n);
-    sp_knot_hide(n->n.knot);
-    n->n.line = sp_canvas_item_new(SP_DT_CONTROLS(n->subpath->nodepath->desktop),
-                                   SP_TYPE_CTRLLINE, NULL);
-    sp_canvas_item_hide(n->n.line);
+    // We only create handle knots and lines on demand
+    n->p.knot = NULL;
+    n->p.line = NULL;
+    n->n.knot = NULL;
+    n->n.line = NULL;
 
     sp->nodes = g_list_prepend(sp->nodes, n);
 
@@ -3405,9 +3783,6 @@ static void sp_nodepath_node_destroy(Inkscape::NodePath::Node *node)
     g_assert(node);
     g_assert(node->subpath);
     g_assert(SP_IS_KNOT(node->knot));
-    g_assert(SP_IS_KNOT(node->p.knot));
-    g_assert(SP_IS_KNOT(node->n.knot));
-    g_assert(g_list_find(node->subpath->nodes, node));
 
    Inkscape::NodePath::SubPath *sp = node->subpath;
 
@@ -3419,11 +3794,15 @@ static void sp_nodepath_node_destroy(Inkscape::NodePath::Node *node)
     node->subpath->nodes = g_list_remove(node->subpath->nodes, node);
 
     g_object_unref(G_OBJECT(node->knot));
-    g_object_unref(G_OBJECT(node->p.knot));
-    g_object_unref(G_OBJECT(node->n.knot));
+    if (node->p.knot)
+        g_object_unref(G_OBJECT(node->p.knot));
+    if (node->n.knot)
+        g_object_unref(G_OBJECT(node->n.knot));
 
-    gtk_object_destroy(GTK_OBJECT(node->p.line));
-    gtk_object_destroy(GTK_OBJECT(node->n.line));
+    if (node->p.line)
+        gtk_object_destroy(GTK_OBJECT(node->p.line));
+    if (node->n.line)
+        gtk_object_destroy(GTK_OBJECT(node->n.line));
 
     if (sp->nodes) { // there are others nodes on the subpath
         if (sp->closed) {
@@ -3451,7 +3830,7 @@ static void sp_nodepath_node_destroy(Inkscape::NodePath::Node *node)
 }
 
 /**
- * Returns one of the node's two knots (node sides).
+ * Returns one of the node's two sides.
  * \param which Indicates which side.
  * \return Pointer to previous node side if which==-1, next if which==1.
  */
@@ -3474,9 +3853,9 @@ static Inkscape::NodePath::NodeSide *sp_node_get_side(Inkscape::NodePath::Node *
 }
 
 /**
- * Return knot on other side of node.
+ * Return the other side of the node, given one of its sides.
  */
-static Inkscape::NodePath::NodeSide *sp_node_opposite_side(Inkscape::NodePath::Node *node,Inkscape::NodePath::NodeSide *me)
+static Inkscape::NodePath::NodeSide *sp_node_opposite_side(Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeSide *me)
 {
     g_assert(node);
 
@@ -3489,7 +3868,7 @@ static Inkscape::NodePath::NodeSide *sp_node_opposite_side(Inkscape::NodePath::N
 }
 
 /**
- * Return NRPathcode on this knot's side of the node.
+ * Return NRPathcode on the given side of the node.
  */
 static NRPathcode sp_node_path_code_from_side(Inkscape::NodePath::Node *node,Inkscape::NodePath::NodeSide *me)
 {
@@ -3513,11 +3892,11 @@ static NRPathcode sp_node_path_code_from_side(Inkscape::NodePath::Node *node,Ink
 /**
  * Call sp_nodepath_line_add_node() at t on the segment denoted by piece
  */
-Inkscape::NodePath::Node * 
+Inkscape::NodePath::Node *
 sp_nodepath_get_node_by_index(int index)
 {
     Inkscape::NodePath::Node *e = NULL;
-    
+
     Inkscape::NodePath::Path *nodepath = sp_nodepath_current();
     if (!nodepath) {
         return e;
@@ -3525,13 +3904,13 @@ sp_nodepath_get_node_by_index(int index)
 
     //find segment
     for (GList *l = nodepath->subpaths; l ; l=l->next) {
-        
+
         Inkscape::NodePath::SubPath *sp = (Inkscape::NodePath::SubPath *)l->data;
         int n = g_list_length(sp->nodes);
         if (sp->closed) {
             n++;
-        } 
-        
+        }
+
         //if the piece belongs to this subpath grab it
         //otherwise move onto the next subpath
         if (index < n) {
@@ -3548,7 +3927,7 @@ sp_nodepath_get_node_by_index(int index)
             }
         }
     }
-    
+
     return e;
 }
 
@@ -3604,19 +3983,16 @@ static gchar const *sp_node_type_description(Inkscape::NodePath::Node *node)
 void
 sp_nodepath_update_statusbar(Inkscape::NodePath::Path *nodepath)
 {
-    gchar const *when_selected = _("<b>Drag</b> nodes or node handles; <b>arrow</b> keys to move nodes");
+    gchar const *when_selected = _("<b>Drag</b> nodes or node handles; <b>Alt+drag</b> nodes to sculpt; <b>arrow</b> keys to move nodes, <b>&lt; &gt;</b> to scale, <b>[ ]</b> to rotate");
     gchar const *when_selected_one = _("<b>Drag</b> the node or its handles; <b>arrow</b> keys to move the node");
 
-    gint total = 0;
-    gint selected = 0;
-    SPDesktop *desktop = NULL;
+    gint total_nodes = sp_nodepath_get_node_count(nodepath);
+    gint selected_nodes = sp_nodepath_selection_get_node_count(nodepath);
+    gint total_subpaths = sp_nodepath_get_subpath_count(nodepath);
+    gint selected_subpaths = sp_nodepath_selection_get_subpath_count(nodepath);
 
+    SPDesktop *desktop = NULL;
     if (nodepath) {
-        for (GList *spl = nodepath->subpaths; spl != NULL; spl = spl->next) {
-            Inkscape::NodePath::SubPath *subpath = (Inkscape::NodePath::SubPath *) spl->data;
-            total += g_list_length(subpath->nodes);
-        }
-        selected = g_list_length(nodepath->selected);
         desktop = nodepath->desktop;
     } else {
         desktop = SP_ACTIVE_DESKTOP;
@@ -3627,7 +4003,7 @@ sp_nodepath_update_statusbar(Inkscape::NodePath::Path *nodepath)
     Inkscape::MessageContext *mc = SP_NODE_CONTEXT (ec)->_node_message_context;
     if (!mc) return;
 
-    if (selected == 0) {
+    if (selected_nodes == 0) {
         Inkscape::Selection *sel = desktop->selection;
         if (!sel || sel->isEmpty()) {
             mc->setF(Inkscape::NORMAL_MESSAGE,
@@ -3637,8 +4013,8 @@ sp_nodepath_update_statusbar(Inkscape::NodePath::Path *nodepath)
             mc->setF(Inkscape::NORMAL_MESSAGE,
                      ngettext("<b>0</b> out of <b>%i</b> node selected. <b>Click</b>, <b>Shift+click</b>, or <b>drag around</b> nodes to select.",
                               "<b>0</b> out of <b>%i</b> nodes selected. <b>Click</b>, <b>Shift+click</b>, or <b>drag around</b> nodes to select.",
-                              total),
-                     total);
+                              total_nodes),
+                     total_nodes);
             } else {
                 if (g_slist_length((GSList *)sel->itemList()) == 1) {
                     mc->setF(Inkscape::NORMAL_MESSAGE, _("Drag the handles of the object to modify it."));
@@ -3647,18 +4023,26 @@ sp_nodepath_update_statusbar(Inkscape::NodePath::Path *nodepath)
                 }
             }
         }
-    } else if (nodepath && selected == 1) {
+    } else if (nodepath && selected_nodes == 1) {
         mc->setF(Inkscape::NORMAL_MESSAGE,
                  ngettext("<b>%i</b> of <b>%i</b> node selected; %s. %s.",
                           "<b>%i</b> of <b>%i</b> nodes selected; %s. %s.",
-                          total),
-                 selected, total, sp_node_type_description((Inkscape::NodePath::Node *) nodepath->selected->data), when_selected_one);
+                          total_nodes),
+                 selected_nodes, total_nodes, sp_node_type_description((Inkscape::NodePath::Node *) nodepath->selected->data), when_selected_one);
     } else {
-        mc->setF(Inkscape::NORMAL_MESSAGE,
-                 ngettext("<b>%i</b> of <b>%i</b> node selected. %s.",
-                          "<b>%i</b> of <b>%i</b> nodes selected. %s.",
-                          total),
-                 selected, total, when_selected);
+        if (selected_subpaths > 1) {
+            mc->setF(Inkscape::NORMAL_MESSAGE,
+                     ngettext("<b>%i</b> of <b>%i</b> node selected in <b>%i</b> of <b>%i</b> subpaths. %s.",
+                              "<b>%i</b> of <b>%i</b> nodes selected in <b>%i</b> of <b>%i</b> subpaths. %s.",
+                              total_nodes),
+                     selected_nodes, total_nodes, selected_subpaths, total_subpaths, when_selected);
+        } else {
+            mc->setF(Inkscape::NORMAL_MESSAGE,
+                     ngettext("<b>%i</b> of <b>%i</b> node selected. %s.",
+                              "<b>%i</b> of <b>%i</b> nodes selected. %s.",
+                              total_nodes),
+                     selected_nodes, total_nodes, when_selected);
+        }
     }
 }