Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / connector-context.cpp
index 34f5ba3ed0dde0d5702e2dda9e4163ac06890937..f4b202451156cc5e83133d55d3fe5dfda76075b1 100644 (file)
-/*
+/**
  * Connector creation tool
  *
  * Authors:
  *   Michael Wybrow <mjwybrow@users.sourceforge.net>
+ *   Abhishek Sharma
+ *   Jon A. Cruz <jon@joncruz.org>
  *
- * Copyright (C) 2005 Michael Wybrow
+ * Copyright (C) 2005-2008  Michael Wybrow
+ * Copyright (C) 2009  Monash University
+ * Copyright (C) 2010  authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  *
  * TODO:
- *  o  Have shapes avoid coonvex hulls of objects, rather than their
- *     bounding box.  Possibly implement the unfinished ConvexHull
- *     class in libnr.
- *     (HOWEVER, using the convex hull C of a shape S does the wrong thing if a
- *     connector starts outside of S but inside C, or if the best route around
- *     an object involves going inside C but without entering S.)
- *  o  Draw connectors to shape edges rather than bounding box.
  *  o  Show a visual indicator for objects with the 'avoid' property set.
+ *  o  Allow user to change a object between a path and connector through
+ *     the interface.
  *  o  Create an interface for setting markers (arrow heads).
  *  o  Better distinguish between paths and connectors to prevent problems
- *     in the node tool and paths accidently being turned into connectors
+ *     in the node tool and paths accidentally being turned into connectors
  *     in the connector tool.  Perhaps have a way to convert between.
  *  o  Only call libavoid's updateEndPoint as required.  Currently we do it
  *     for both endpoints, even if only one is moving.
- *  o  Cleanup to remove unecessary borrowed DrawContext code.
  *  o  Allow user-placeable connection points.
- *  o  Deal sanely with connectors with both endpoints attached to the 
+ *  o  Deal sanely with connectors with both endpoints attached to the
  *     same connection point, and drawing of connectors attaching
- *     overlaping shapes (currently tries to adjust connector to be
+ *     overlapping shapes (currently tries to adjust connector to be
  *     outside both bounding boxes).
  *  o  Fix many special cases related to connectors updating,
  *     e.g., copying a couple of shapes and a connector that are
  *           attached to each other.
  *     e.g., detach connector when it is moved or transformed in
  *           one of the other contexts.
- *  o  Cope with shapes whose ids change when they have attached 
+ *  o  Cope with shapes whose ids change when they have attached
  *     connectors.
- *  o  gobble_motion_events(GDK_BUTTON1_MASK)?;
+ *  o  During dragging motion, gobble up to and use the final motion event.
+ *     Gobbling away all duplicates after the current can occasionally result
+ *     in the path lagging behind the mouse cursor if it is no longer being
+ *     dragged.
+ *  o  Fix up libavoid's representation after undo actions.  It doesn't see
+ *     any transform signals and hence doesn't know shapes have moved back to
+ *     there earlier positions.
+ *  o  Decide whether drawing/editing mode should be an Inkscape preference
+ *     or the connector tool should always start in drawing mode.
+ *  o  Correct the problem with switching to the select tool when pressing
+ *     space bar (there are moments when it refuses to do so).
+ *
+ * ----------------------------------------------------------------------------
+ *
+ * mjwybrow's observations on acracan's Summer of Code connector work:
+ *
+ *  -  GUI comments:
+ *
+ *      -  Buttons for adding and removing user-specified connection
+ *     points should probably have "+" and "-" symbols on them so they
+ *     are consistent with the similar buttons for the node tool.
+ *      -  Controls on the connector tool be should be reordered logically,
+ *     possibly as follows:
+ *
+ *     *Connector*: [Polyline-radio-button] [Orthgonal-radio-button]
+ *       [Curvature-control] | *Shape*: [Avoid-button] [Dont-avoid-button]
+ *       [Spacing-control] | *Connection pts*: [Edit-mode] [Add-pt] [Rm-pt]
+ *
+ *     I think that the network layout controls be moved to the
+ *     Align and Distribute dialog (there is already the layout button
+ *     there, but no options are exposed).
+ *
+ *     I think that the style change between polyline and orthogonal
+ *     would be much clearer with two buttons (radio behaviour -- just
+ *     one is true).
+ *
+ *     The other tools show a label change from "New:" to "Change:"
+ *     depending on whether an object is selected.  We could consider
+ *     this but there may not be space.
+ *
+ *     The Add-pt and Rm-pt buttons should be greyed out (inactive) if
+ *     we are not in connection point editing mode.  And probably also
+ *     if there is no shape selected, i.e. at the times they have no
+ *     effect when clicked.
+ *
+ *     Likewise for the avoid/ignore shapes buttons.  These should be
+ *     inactive when a shape is not selected in the connector context.
+ *
+ *  -  When creating/editing connection points:
+ *
+ *      -  Strange things can happen if you have connectors selected, or
+ *     try rerouting connectors by dragging their endpoints when in
+ *     connection point editing mode.
+ *
+ *      -  Possibly the selected shape's connection points should always
+ *     be shown (i.e., have knots) when in editing mode.
+ *
+ *      -  It is a little strange to be able to place connection points
+ *     competely outside shapes.  Especially when you later can't draw
+ *     connectors to them since the knots are only visible when you
+ *     are over the shape.  I think that you should only be able to
+ *     place connection points inside or on the boundary of the shape
+ *     itself.
+ *
+ *      -  The intended ability to place a new point at the current cursor
+ *     position by pressing RETURN does not seem to work.
+ *
+ *      -  The Status bar tooltip should change to reflect editing mode
+ *     and tell the user about RETURN and how to use the tool.
+ *
+ *  -  Connection points general:
+ *
+ *      -  Connection points that were inside the shape can end up outside
+ *     after a rotation is applied to the shape in the select tool.
+ *     It doesn't seem like the correct transform is being applied to
+ *     these, or it is being applied at the wrong time.  I'd expect
+ *     connection points to rotate with the shape, and stay at the
+ *     same position "on the shape"
+ *
+ *      -  I was able to make the connectors attached to a shape fall off
+ *     the shape after scaling it.  Not sure the exact cause, but may
+ *     require more investigation/debugging.
+ *
+ *      -  The user-defined connection points should be either absolute
+ *     (as the current ones are) or defined as a percentage of the
+ *     shape.  These would be based on a toggle setting on the
+ *     toolbar, and they would be placed in exactly the same way by
+ *     the user.  The only difference would be that they would be
+ *     store as percentage positions in the SVG connection-points
+ *     property and that they would update/move automatically if the
+ *     object was resized or scaled.
+ *
+ *      -  Thinking more, I think you always want to store and think about
+ *     the positions of connection points to be pre-transform, but
+ *     obviously the shape transform is applied to them.  That way,
+ *     they will rotate and scale automatically with the shape, when
+ *     the shape transform is altered.  The Percentage version would
+ *     compute their position from the pre-transform dimensions and
+ *     then have the transform applied to them, for example.
+ *
+ *      -  The connection points in the test_connection_points.svg file
+ *     seem to follow the shape when it is moved, but connection
+ *     points I add to new shapes, do not follow the shape, either
+ *     when the shape is just moved or transformed.  There is
+ *     something wrong here.  What exactly should the behaviour be
+ *     currently?
+ *
+ *      -  I see that connection points are specified at absolute canvas
+ *     positions.  I really think that they should be specified in
+ *     shape coordinated relative to the shapes.  There may be
+ *     transforms applied to layers and the canvas which would make
+ *     specifying them quite difficult.  I'd expect a position of 0, 0
+ *     to be on the shape in question or very close to it, for example.
  *
  */
 
+
+
 #include <gdk/gdkkeysyms.h>
+#include <string>
+#include <cstring>
 
 #include "connector-context.h"
 #include "pixmaps/cursor-connector.xpm"
+#include "pixmaps/cursor-node.xpm"
+//#include "pixmaps/cursor-node-m.xpm"
+//#include "pixmaps/cursor-node-d.xpm"
 #include "xml/node-event-vector.h"
 #include "xml/repr.h"
 #include "svg/svg.h"
 #include "desktop.h"
 #include "desktop-style.h"
-#include "desktop-affine.h"
 #include "desktop-handles.h"
 #include "document.h"
 #include "message-context.h"
 #include "message-stack.h"
 #include "selection.h"
 #include "inkscape.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "sp-path.h"
+#include "display/sp-canvas.h"
 #include "display/canvas-bpath.h"
 #include "display/sodipodi-ctrl.h"
 #include <glibmm/i18n.h>
+#include <glibmm/stringutils.h>
 #include "snap.h"
 #include "knot.h"
 #include "sp-conn-end.h"
+#include "sp-conn-end-pair.h"
 #include "conn-avoid-ref.h"
 #include "libavoid/vertices.h"
+#include "libavoid/router.h"
 #include "context-fns.h"
+#include "sp-namedview.h"
+#include "sp-text.h"
+#include "sp-flowtext.h"
+#include "display/curve.h"
 
-
+using Inkscape::DocumentUndo;
 
 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
 static void sp_connector_context_init(SPConnectorContext *conn_context);
 static void sp_connector_context_dispose(GObject *object);
 
 static void sp_connector_context_setup(SPEventContext *ec);
+static void sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
 static void sp_connector_context_finish(SPEventContext *ec);
 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
 
 // Stuff borrowed from DrawContext
-static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
-static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
-static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
+static void spcc_connector_set_initial_point(SPConnectorContext *cc, Geom::Point const p);
+static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, Geom::Point const p);
+static void spcc_connector_finish_segment(SPConnectorContext *cc, Geom::Point p);
 static void spcc_reset_colors(SPConnectorContext *cc);
 static void spcc_connector_finish(SPConnectorContext *cc);
 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
@@ -94,13 +219,18 @@ static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEven
 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
 
+static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp);
 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
 static void cc_clear_active_shape(SPConnectorContext *cc);
 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
 static void cc_clear_active_conn(SPConnectorContext *cc);
-static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
+static bool conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid);
+static void cc_select_handle(SPKnot* knot);
+static void cc_deselect_handle(SPKnot* knot);
 static bool cc_item_is_shape(SPItem *item);
 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
+static void cc_connector_rerouting_finish(SPConnectorContext *const cc,
+        Geom::Point *const p);
 
 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
@@ -109,8 +239,11 @@ static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *nam
         gpointer data);
 
 
-static NR::Point connector_drag_origin_w(0, 0);
-static bool connector_within_tolerance = false;
+static char* cc_knot_tips[] = { _("<b>Connection point</b>: click or drag to create a new connector"),
+                           _("<b>Connection point</b>: click to select, drag to move") };
+
+/*static Geom::Point connector_drag_origin_w(0, 0);
+static bool connector_within_tolerance = false;*/
 static SPEventContextClass *parent_class;
 
 
@@ -165,6 +298,7 @@ sp_connector_context_class_init(SPConnectorContextClass *klass)
     object_class->dispose = sp_connector_context_dispose;
 
     event_context_class->setup = sp_connector_context_setup;
+    event_context_class->set = sp_connector_context_set;
     event_context_class->finish = sp_connector_context_finish;
     event_context_class->root_handler = sp_connector_context_root_handler;
     event_context_class->item_handler = sp_connector_context_item_handler;
@@ -182,13 +316,17 @@ sp_connector_context_init(SPConnectorContext *cc)
     ec->xp = 0;
     ec->yp = 0;
 
+    cc->mode = SP_CONNECTOR_CONTEXT_DRAWING_MODE;
+    cc->knot_tip = 0;
+
     cc->red_color = 0xff00007f;
-    
+
     cc->newconn = NULL;
     cc->newConnRef = NULL;
-   
+    cc->curvature = 0.0;
+
     cc->sel_changed_connection = sigc::connection();
-    
+
     cc->active_shape = NULL;
     cc->active_shape_repr = NULL;
     cc->active_shape_layer_repr = NULL;
@@ -197,17 +335,22 @@ sp_connector_context_init(SPConnectorContext *cc)
     cc->active_conn_repr = NULL;
 
     cc->active_handle = NULL;
-    
+
+    cc->selected_handle = NULL;
+
     cc->clickeditem = NULL;
     cc->clickedhandle = NULL;
-    
-    cc->connpthandle = NULL;
+
+    new (&cc->connpthandles) ConnectionPointMap();
+
     for (int i = 0; i < 2; ++i) {
         cc->endpt_handle[i] = NULL;
         cc->endpt_handler_id[i] = 0;
     }
-    cc->sid = NULL;
-    cc->eid = NULL;
+    cc->shref = NULL;
+    cc->scpid = NULL;
+    cc->ehref = NULL;
+    cc->ecpid = NULL;
     cc->npoints = 0;
     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
 }
@@ -220,23 +363,35 @@ sp_connector_context_dispose(GObject *object)
 
     cc->sel_changed_connection.disconnect();
 
-    if (cc->connpthandle) {
-        g_object_unref(cc->connpthandle);
-        cc->connpthandle = NULL;
+    if (!cc->connpthandles.empty()) {
+        for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
+                it != cc->connpthandles.end(); ++it) {
+            g_object_unref(it->first);
+        }
+        cc->connpthandles.clear();
     }
+    cc->connpthandles.~ConnectionPointMap();
     for (int i = 0; i < 2; ++i) {
         if (cc->endpt_handle[1]) {
             g_object_unref(cc->endpt_handle[i]);
             cc->endpt_handle[i] = NULL;
         }
     }
-    if (cc->sid) {
-        g_free(cc->sid);
-        cc->sid = NULL;
+    if (cc->shref) {
+        g_free(cc->shref);
+        cc->shref = NULL;
+    }
+    if (cc->scpid) {
+        g_free(cc->scpid);
+        cc->scpid = NULL;
     }
-    if (cc->eid) {
-        g_free(cc->eid);
-        cc->eid = NULL;
+    if (cc->ehref) {
+        g_free(cc->shref);
+        cc->shref = NULL;
+    }
+    if (cc->ecpid) {
+        g_free(cc->scpid);
+        cc->scpid = NULL;
     }
     g_assert( cc->newConnRef == NULL );
 
@@ -254,31 +409,100 @@ sp_connector_context_setup(SPEventContext *ec)
         ((SPEventContextClass *) parent_class)->setup(ec);
     }
 
-    cc->selection = SP_DT_SELECTION(dt);
+    cc->selection = sp_desktop_selection(dt);
 
     cc->sel_changed_connection.disconnect();
     cc->sel_changed_connection = cc->selection->connectChanged(
             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
             (gpointer) cc));
-    
+
     /* Create red bpath */
-    cc->red_bpath = sp_canvas_bpath_new(SP_DT_SKETCH(ec->desktop), NULL);
+    cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
             SP_WIND_RULE_NONZERO);
     /* Create red curve */
-    cc->red_curve = sp_curve_new_sized(4);
+    cc->red_curve = new SPCurve();
 
     /* Create green curve */
-    cc->green_curve = sp_curve_new_sized(64);
+    cc->green_curve = new SPCurve();
 
     // Notice the initial selection.
     cc_selection_changed(cc->selection, (gpointer) cc);
-    
-    if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
+
+    cc->within_tolerance = false;
+
+    sp_event_context_read(ec, "curvature");
+    sp_event_context_read(ec, "orthogonal");
+    sp_event_context_read(ec, "mode");
+    cc->knot_tip = cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE ? cc_knot_tips[0] : cc_knot_tips[1];
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (prefs->getBool("/tools/connector/selcue", 0)) {
         ec->enableSelectionCue();
     }
+
+    // Make sure we see all enter events for canvas items,
+    // even if a mouse button is depressed.
+    dt->canvas->gen_all_enter_events = true;
+}
+
+
+static void
+sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
+{
+    SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
+
+    /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
+     * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
+    Glib::ustring name = val->getEntryName();
+    if ( name == "curvature" ) {
+        cc->curvature = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
+    }
+    else if ( name == "orthogonal" ) {
+        cc->isOrthogonal = val->getBool();
+    }
+    else if ( name == "mode")
+    {
+        sp_connector_context_switch_mode(ec, val->getBool() ? SP_CONNECTOR_CONTEXT_EDITING_MODE : SP_CONNECTOR_CONTEXT_DRAWING_MODE);
+    }
+}
+
+void sp_connector_context_switch_mode(SPEventContext* ec, unsigned int newMode)
+{
+    SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
+
+    cc->mode = newMode;
+    if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
+    {
+        ec->cursor_shape = cursor_connector_xpm;
+        cc->knot_tip = cc_knot_tips[0];
+        if (cc->selected_handle)
+            cc_deselect_handle( cc->selected_handle );
+        cc->selected_handle = NULL;
+        // Show all default connection points
+
+    }
+    else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
+    {
+        ec->cursor_shape = cursor_node_xpm;
+        cc->knot_tip = cc_knot_tips[1];
+/*            if (cc->active_shape)
+        {
+            cc->selection->set( SP_OBJECT( cc->active_shape ) );
+        }
+        else
+        {
+            SPItem* item = cc->selection->singleItem();
+            if ( item )
+            {
+                cc_set_active_shape(cc, item);
+                cc->selection->set( SP_OBJECT( item ) );
+            }
+        }*/
+    }
+    sp_event_context_update_cursor(ec);
+
 }
 
 
@@ -286,18 +510,23 @@ static void
 sp_connector_context_finish(SPEventContext *ec)
 {
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
-    
+
     spcc_connector_finish(cc);
+    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
 
     if (((SPEventContextClass *) parent_class)->finish) {
         ((SPEventContextClass *) parent_class)->finish(ec);
     }
-    
+
     if (cc->selection) {
         cc->selection = NULL;
     }
     cc_clear_active_shape(cc);
     cc_clear_active_conn(cc);
+
+    // Restore the default event generating behaviour.
+    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
+    desktop->canvas->gen_all_enter_events = false;
 }
 
 
@@ -319,15 +548,18 @@ cc_clear_active_shape(SPConnectorContext *cc)
         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
         Inkscape::GC::release(cc->active_shape_repr);
         cc->active_shape_repr = NULL;
-        
+
         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
         Inkscape::GC::release(cc->active_shape_layer_repr);
         cc->active_shape_layer_repr = NULL;
     }
 
-    // Hide the center connection point if it exists.
-    if (cc->connpthandle) {
-        sp_knot_hide(cc->connpthandle);
+    // Hide the connection points if they exist.
+    if (cc->connpthandles.size()) {
+        for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
+                it != cc->connpthandles.end(); ++it) {
+            sp_knot_hide(it->first);
+        }
     }
 }
 
@@ -357,70 +589,113 @@ cc_clear_active_conn(SPConnectorContext *cc)
 }
 
 
-static gchar *
-conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
+static bool
+conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid)
 {
     // TODO: this will need to change when there are more connection
     //       points available for each shape.
-   
-    SPKnot *centerpt = cc->connpthandle;
-    if (cc->active_handle && (cc->active_handle == centerpt))
+
+    if (cc->active_handle && (cc->connpthandles.find(cc->active_handle) != cc->connpthandles.end()))
     {
-        p = centerpt->pos;
-        return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
+        p = cc->active_handle->pos;
+        const ConnectionPoint& cp = cc->connpthandles[cc->active_handle];
+        *href = g_strdup_printf("#%s", cc->active_shape->getId());
+        *cpid = g_strdup_printf("%c%d", cp.type == ConnPointDefault ? 'd' : 'u' , cp.id);
+        return true;
     }
-    return NULL;
+    *href = NULL;
+    *cpid = NULL;
+    return false;
 }
 
+static void
+cc_select_handle(SPKnot* knot)
+{
+    knot->setShape(SP_KNOT_SHAPE_SQUARE);
+    knot->setSize(10);
+    knot->setAnchor(GTK_ANCHOR_CENTER);
+    knot->setFill(0x0000ffff, 0x0000ffff, 0x0000ffff);
+    sp_knot_update_ctrl(knot);
+}
 
+static void
+cc_deselect_handle(SPKnot* knot)
+{
+    knot->setShape(SP_KNOT_SHAPE_SQUARE);
+    knot->setSize(8);
+    knot->setAnchor(GTK_ANCHOR_CENTER);
+    knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
+    sp_knot_update_ctrl(knot);
+}
 
 static gint
-sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
+sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
 {
     gint ret = FALSE;
 
-    SPDesktop *desktop = ec->desktop;
+    SPDesktop *desktop = event_context->desktop;
 
-    SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
+    SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
 
-    NR::Point p(event->button.x, event->button.y);
+    Geom::Point p(event->button.x, event->button.y);
 
     switch (event->type) {
         case GDK_BUTTON_RELEASE:
-            if (event->button.button == 1) {
+            if (event->button.button == 1 && !event_context->space_panning) {
                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
-                        (connector_within_tolerance))
+                        (event_context->within_tolerance))
                 {
                     spcc_reset_colors(cc);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                 }
                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
-                    // Doing simething else like rerouting.
+                    // Doing something else like rerouting.
                     break;
                 }
-                // find out clicked item, disregarding groups, honoring Alt
-                SPItem *item_ungrouped = sp_event_context_find_item(desktop,
-                        p, event->button.state & GDK_MOD1_MASK, TRUE);
+                // find out clicked item, honoring Alt
+                SPItem *item = sp_event_context_find_item(desktop,
+                        p, event->button.state & GDK_MOD1_MASK, FALSE);
 
                 if (event->button.state & GDK_SHIFT_MASK) {
-                    cc->selection->toggle(item_ungrouped);
+                    cc->selection->toggle(item);
                 } else {
-                    cc->selection->set(item_ungrouped);
+                    cc->selection->set(item);
+                    if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && cc->selected_handle )
+                    {
+                        cc_deselect_handle( cc->selected_handle );
+                        cc->selected_handle = NULL;
+                    }
+                    /* When selecting a new item,
+                       do not allow showing connection points
+                       on connectors. (yet?)
+                    */
+                    if ( item != cc->active_shape && !cc_item_is_connector( item ) )
+                        cc_set_active_shape( cc, item );
                 }
                 ret = TRUE;
+
             }
             break;
         case GDK_ENTER_NOTIFY:
         {
-            if (cc_item_is_shape(item)) {
-                // This is a shape, so show connection point(s).
-                if (!(cc->active_shape) ||
-                        // Don't show handle for another handle.
-                        (item != ((SPItem *) cc->connpthandle))) {
+            if (cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE || (cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && !cc->selected_handle))
+            {
+                if (cc_item_is_shape(item)) {
+
+                    // I don't really understand what the above does,
+                    // so I commented it.
+                    // This is a shape, so show connection point(s).
+    /*                if (!(cc->active_shape)
+                            // Don't show handle for another handle.
+    //                         || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
+                        )
+                    {
+                        cc_set_active_shape(cc, item);
+                    }*/
                     cc_set_active_shape(cc, item);
                 }
+                ret = TRUE;
             }
-            ret = TRUE;
             break;
         }
         default:
@@ -473,86 +748,169 @@ sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
 static gint
 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
 {
-    NR::Point const event_w(bevent.x, bevent.y);
+    Geom::Point const event_w(bevent.x, bevent.y);
     /* Find desktop coordinates */
-    NR::Point p = cc->desktop->w2d(event_w);
+    Geom::Point p = cc->desktop->w2d(event_w);
+    SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
 
     gint ret = FALSE;
-    if ( bevent.button == 1 ) {
+    if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
+    {
+        if ( bevent.button == 1 && !event_context->space_panning ) {
 
-        SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+            SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
 
-        if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
-            return TRUE;
-        }
-        
-        NR::Point const event_w(bevent.x,
-                                bevent.y);
-        connector_drag_origin_w = event_w;
-        connector_within_tolerance = true;
+            if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
+                return TRUE;
+            }
 
-        NR::Point const event_dt = cc->desktop->w2d(event_w);
-        switch (cc->state) {
-            case SP_CONNECTOR_CONTEXT_STOP:
-                /* This is allowed, if we just cancelled curve */
-            case SP_CONNECTOR_CONTEXT_IDLE:
-            {
-                if ( cc->npoints == 0 ) {
-                    /* Set start anchor */
-                    NR::Point p;
-                        
-                    cc_clear_active_conn(cc);
+            Geom::Point const event_w(bevent.x,
+                                    bevent.y);
+//             connector_drag_origin_w = event_w;
+            cc->xp = bevent.x;
+            cc->yp = bevent.y;
+            cc->within_tolerance = true;
+
+            Geom::Point const event_dt = cc->desktop->w2d(event_w);
 
-                    SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
+            SnapManager &m = cc->desktop->namedview->snap_manager;
 
-                    /* Create green anchor */
-                    p = event_dt;
-                    
+            switch (cc->state) {
+                case SP_CONNECTOR_CONTEXT_STOP:
+                    /* This is allowed, if we just canceled curve */
+                case SP_CONNECTOR_CONTEXT_IDLE:
+                {
+                    if ( cc->npoints == 0 ) {
+                        cc_clear_active_conn(cc);
+
+                        SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
+
+                        /* Set start anchor */
+                        /* Create green anchor */
+                        Geom::Point p = event_dt;
+
+                        // Test whether we clicked on a connection point
+                        bool found = conn_pt_handle_test(cc, p, &cc->shref, &cc->scpid);
+
+                        if (!found) {
+                            // This is the first point, so just snap it to the grid
+                            // as there's no other points to go off.
+                            m.setup(cc->desktop);
+                            m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                            m.unSetup();
+                        }
+                        spcc_connector_set_initial_point(cc, p);
+
+                    }
+                    cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
+                    ret = TRUE;
+                    break;
+                }
+                case SP_CONNECTOR_CONTEXT_DRAGGING:
+                {
+                    // This is the second click of a connector creation.
+                    m.setup(cc->desktop);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+
+                    spcc_connector_set_subsequent_point(cc, p);
+                    spcc_connector_finish_segment(cc, p);
                     // Test whether we clicked on a connection point
-                    cc->sid = conn_pt_handle_test(cc, p);
-                    
-                    if (!cc->sid) {
-                        // This is the first point, so just snap it to the grid
-                        // as there's no other points to go off.
-                        SnapManager const m(cc->desktop->namedview);
-                        p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
-                                       p, NULL).getPoint();
-                    }                        
-                    spcc_connector_set_initial_point(cc, p);
-                    
+                    /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
+                    if (cc->npoints != 0) {
+                        spcc_connector_finish(cc);
+                    }
+                    cc_set_active_conn(cc, cc->newconn);
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
+                    break;
                 }
-                cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
-                ret = TRUE;
-                break;
-            }   
-            case SP_CONNECTOR_CONTEXT_DRAGGING:
-            {
-                // This is the second click of a connector creation.
-                
-                spcc_connector_set_subsequent_point(cc, p);
-                spcc_connector_finish_segment(cc, p);
-                // Test whether we clicked on a connection point
-                cc->eid = conn_pt_handle_test(cc, p);
-                if (cc->npoints != 0) {
-                    spcc_connector_finish(cc);
+                case SP_CONNECTOR_CONTEXT_CLOSE:
+                {
+                    g_warning("Button down in CLOSE state");
+                    break;
                 }
-                cc_set_active_conn(cc, cc->newconn);
+                default:
+                    break;
+            }
+        } else if (bevent.button == 3) {
+            if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
+                // A context menu is going to be triggered here,
+                // so end the rerouting operation.
+                cc_connector_rerouting_finish(cc, &p);
+
+                cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+
+                // Don't set ret to TRUE, so we drop through to the
+                // parent handler which will open the context menu.
+            }
+            else if (cc->npoints != 0) {
+                spcc_connector_finish(cc);
                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                 ret = TRUE;
-                break;
             }
-            case SP_CONNECTOR_CONTEXT_CLOSE:
+        }
+    }
+    else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
+    {
+        if ( bevent.button == 1 && !event_context->space_panning )
+        {
+            // Initialize variables in case of dragging
+
+            SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+
+            if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
+                return TRUE;
+            }
+
+            cc->xp = bevent.x;
+            cc->yp = bevent.y;
+            cc->within_tolerance = true;
+
+            ConnectionPointMap::iterator const& active_knot_it = cc->connpthandles.find( cc->active_handle );
+
+            switch (cc->state)
             {
-                g_warning("Button down in CLOSE state");
-                break;
+                case SP_CONNECTOR_CONTEXT_IDLE:
+                    if ( active_knot_it != cc->connpthandles.end() )
+                    {
+                        // We do not allow selecting and, thereby, moving default knots
+                        if ( active_knot_it->second.type != ConnPointDefault)
+                        {
+                            if (cc->selected_handle != cc->active_handle)
+                            {
+                                if ( cc->selected_handle )
+                                    cc_deselect_handle( cc->selected_handle );
+                                cc->selected_handle = cc->active_handle;
+                                cc_select_handle( cc->selected_handle );
+                            }
+                        }
+                        else
+                            // Just ignore the default connection point
+                            return FALSE;
+                    }
+                    else
+                        if ( cc->selected_handle )
+                        {
+                            cc_deselect_handle( cc->selected_handle );
+                            cc->selected_handle = NULL;
+                        }
+
+                    if ( cc->selected_handle )
+                    {
+                        cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
+                        cc->selection->set( SP_OBJECT( cc->active_shape ) );
+                    }
+
+                    ret = TRUE;
+                    break;
+                // Dragging valid because of the way we create
+                // new connection points.
+                case SP_CONNECTOR_CONTEXT_DRAGGING:
+                    // Do nothing.
+                    ret = TRUE;
+                    break;
             }
-            default:
-                break;
-        }
-    } else if (bevent.button == 3) {
-        if (cc->npoints != 0) {
-            spcc_connector_finish(cc);
-            ret = TRUE;
         }
     }
     return ret;
@@ -563,75 +921,110 @@ static gint
 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
 {
     gint ret = FALSE;
+    SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
-    if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
+    if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
         // allow middle-button scrolling
         return FALSE;
     }
 
-    NR::Point const event_w(mevent.x, mevent.y);
+    Geom::Point const event_w(mevent.x, mevent.y);
 
-    if (connector_within_tolerance) {
-        gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
-                                                               "value", 0, 0, 100);
-        if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
+    if (cc->within_tolerance) {
+        cc->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
+        if ( ( abs( (gint) mevent.x - cc->xp ) < cc->tolerance ) &&
+             ( abs( (gint) mevent.y - cc->yp ) < cc->tolerance ) ) {
             return FALSE;   // Do not drag if we're within tolerance from origin.
         }
     }
     // Once the user has moved farther than tolerance from the original location
     // (indicating they intend to move the object, not click), then always process
     // the motion notify coordinates as given (no snapping back to origin)
-    connector_within_tolerance = false;
+    cc->within_tolerance = false;
 
     SPDesktop *const dt = cc->desktop;
 
     /* Find desktop coordinates */
-    NR::Point p = dt->w2d(event_w);
+    Geom::Point p = dt->w2d(event_w);
 
-    switch (cc->state) {
-        case SP_CONNECTOR_CONTEXT_DRAGGING:
-        {
-            // This is movement during a connector creation.
-            
-            if ( cc->npoints > 0 ) {
-                cc->selection->clear();
-                spcc_connector_set_subsequent_point(cc, p);
-                ret = TRUE;
-            }
-            break;
-        }
-        case SP_CONNECTOR_CONTEXT_REROUTING:
-        {
-            g_assert( SP_IS_PATH(cc->clickeditem));
+    if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
+    {
+        SnapManager &m = dt->namedview->snap_manager;
 
-            // Update the hidden path
-            NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
-            NR::Matrix d2i = i2d.inverse();
-            SPPath *path = SP_PATH(cc->clickeditem);
-            SPCurve *curve = (SP_SHAPE(path))->curve;
-            if (cc->clickedhandle == cc->endpt_handle[0]) {
-                NR::Point o = cc->endpt_handle[1]->pos;
-                sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
-            }
-            else {
-                NR::Point o = cc->endpt_handle[0]->pos;
-                sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
+        switch (cc->state) {
+            case SP_CONNECTOR_CONTEXT_DRAGGING:
+            {
+                gobble_motion_events(mevent.state);
+                // This is movement during a connector creation.
+                if ( cc->npoints > 0 ) {
+                    m.setup(dt);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+                    cc->selection->clear();
+                    spcc_connector_set_subsequent_point(cc, p);
+                    ret = TRUE;
+                }
+                break;
             }
-            sp_conn_adjust_path(path);
+            case SP_CONNECTOR_CONTEXT_REROUTING:
+            {
+                gobble_motion_events(GDK_BUTTON1_MASK);
+                g_assert( SP_IS_PATH(cc->clickeditem));
+
+                m.setup(dt);
+                m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                m.unSetup();
+
+                // Update the hidden path
+                Geom::Matrix i2d = (cc->clickeditem)->i2d_affine();
+                Geom::Matrix d2i = i2d.inverse();
+                SPPath *path = SP_PATH(cc->clickeditem);
+                SPCurve *curve = path->original_curve ? path->original_curve : path->curve;
+                if (cc->clickedhandle == cc->endpt_handle[0]) {
+                    Geom::Point o = cc->endpt_handle[1]->pos;
+                    curve->stretch_endpoints(p * d2i, o * d2i);
+                }
+                else {
+                    Geom::Point o = cc->endpt_handle[0]->pos;
+                    curve->stretch_endpoints(o * d2i, p * d2i);
+                }
+                sp_conn_reroute_path_immediate(path);
 
-            // Copy this to the temporary visible path
-            cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
-            sp_curve_transform(cc->red_curve, i2d);
+                // Copy this to the temporary visible path
+                cc->red_curve = path->original_curve ?
+                        path->original_curve->copy() : path->curve->copy();
+                cc->red_curve->transform(i2d);
 
-            sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
-            ret = TRUE;
-            break;
+                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
+                ret = TRUE;
+                break;
+            }
+            case SP_CONNECTOR_CONTEXT_STOP:
+                /* This is perfectly valid */
+                break;
+            default:
+                if (!sp_event_context_knot_mouseover(cc)) {
+                    m.setup(dt);
+                    m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
+                    m.unSetup();
+                }
+                break;
+        }
+    }
+    else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
+    {
+        switch ( cc->state )
+        {
+            case SP_CONNECTOR_CONTEXT_DRAGGING:
+                sp_knot_set_position(cc->selected_handle, p, 0);
+                ret = TRUE;
+                break;
+            case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
+                sp_knot_set_position(cc->selected_handle, p, 0);
+                ret = TRUE;
+                break;
         }
-        case SP_CONNECTOR_CONTEXT_STOP:
-            /* This is perfectly valid */
-            break;
-        default:
-            break;
     }
 
     return ret;
@@ -642,105 +1035,293 @@ static gint
 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
 {
     gint ret = FALSE;
-    if ( revent.button == 1 ) {
+    SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
+    if ( revent.button == 1 && !event_context->space_panning ) {
 
         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
-        SPDocument *doc = SP_DT_DOCUMENT(desktop);
+        SPDocument *doc = sp_desktop_document(desktop);
 
-        NR::Point const event_w(revent.x, revent.y);
+        SnapManager &m = desktop->namedview->snap_manager;
 
-        /* Find desktop coordinates */
-        NR::Point p = cc->desktop->w2d(event_w);
+        Geom::Point const event_w(revent.x, revent.y);
 
-        switch (cc->state) {
-            //case SP_CONNECTOR_CONTEXT_POINT:
-            case SP_CONNECTOR_CONTEXT_DRAGGING:
-            {
-                if (connector_within_tolerance)
+        /* Find desktop coordinates */
+        Geom::Point p = cc->desktop->w2d(event_w);
+        if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
+        {
+            switch (cc->state) {
+                //case SP_CONNECTOR_CONTEXT_POINT:
+                case SP_CONNECTOR_CONTEXT_DRAGGING:
                 {
+                    m.setup(desktop);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+
+                    if (cc->within_tolerance)
+                    {
+                        spcc_connector_finish_segment(cc, p);
+                        return TRUE;
+                    }
+                    // Connector has been created via a drag, end it now.
+                    spcc_connector_set_subsequent_point(cc, p);
                     spcc_connector_finish_segment(cc, p);
+                    // Test whether we clicked on a connection point
+                    /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
+                    if (cc->npoints != 0) {
+                        spcc_connector_finish(cc);
+                    }
+                    cc_set_active_conn(cc, cc->newconn);
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    break;
+                }
+                case SP_CONNECTOR_CONTEXT_REROUTING:
+                {
+                    m.setup(desktop);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+                    cc_connector_rerouting_finish(cc, &p);
+
+                    doc->ensureUpToDate();
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                     return TRUE;
+                    break;
                 }
-                // Connector has been created via a drag, end it now.
-                spcc_connector_set_subsequent_point(cc, p);
-                spcc_connector_finish_segment(cc, p);
-                // Test whether we clicked on a connection point
-                cc->eid = conn_pt_handle_test(cc, p);
+                case SP_CONNECTOR_CONTEXT_STOP:
+                    /* This is allowed, if we just cancelled curve */
+                    break;
+                default:
+                    break;
+            }
+            ret = TRUE;
+        }
+        else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
+        {
+            switch ( cc->state )
+            {
+                case SP_CONNECTOR_CONTEXT_DRAGGING:
+
+                    if (!cc->within_tolerance)
+                    {
+                        m.setup(desktop);
+                        m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                        m.unSetup();
+                        sp_knot_set_position(cc->selected_handle, p, 0);
+                        ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
+                        cp.pos = p * (cc->active_shape)->dt2i_affine();
+                        cc->active_shape->avoidRef->updateConnectionPoint(cp);
+                    }
+
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
+                    break;
+
+
+                case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
+                    m.setup(desktop);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+
+                    sp_knot_set_position(cc->selected_handle, p, 0);
+
+                    ConnectionPoint cp;
+                    cp.type = ConnPointUserDefined;
+                    cp.pos = p * (cc->active_shape)->dt2i_affine();
+                    cp.dir = Avoid::ConnDirAll;
+                    g_object_unref(cc->selected_handle);
+                    cc->active_shape->avoidRef->addConnectionPoint(cp);
+                    doc->ensureUpToDate();
+                    for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
+                        if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
+                        {
+                            cc->selected_handle = it->first;
+                            break;
+                        }
+                    cc_select_handle( cc->selected_handle );
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
+                    break;
+            }
+        }
+    }
+
+
+    return ret;
+}
+
+
+static gint
+connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
+{
+    gint ret = FALSE;
+    /* fixme: */
+    if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
+    {
+        switch (keyval) {
+            case GDK_Return:
+            case GDK_KP_Enter:
                 if (cc->npoints != 0) {
                     spcc_connector_finish(cc);
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
                 }
-                cc_set_active_conn(cc, cc->newconn);
-                cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                 break;
-            }
-            case SP_CONNECTOR_CONTEXT_REROUTING:
-            {
-                // Clear the temporary path:
-                sp_curve_reset(cc->red_curve);
-                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
+            case GDK_Escape:
+                if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
 
-                // Test whether we clicked on a connection point
-                gchar *shape_label = conn_pt_handle_test(cc, p);
+                    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+                    SPDocument *doc = sp_desktop_document(desktop);
 
-                if (shape_label) {
-                    if (cc->clickedhandle == cc->endpt_handle[0]) {
-                        sp_object_setAttribute(cc->clickeditem,
-                                "inkscape:connection-start",shape_label, false);
-                    }
-                    else {
-                        sp_object_setAttribute(cc->clickeditem,
-                                "inkscape:connection-end",shape_label, false);
+                    cc_connector_rerouting_finish(cc, NULL);
+
+                    DocumentUndo::undo(doc);
+
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
+                            _("Connector endpoint drag cancelled."));
+                    ret = TRUE;
+                }
+                else if (cc->npoints != 0) {
+                    // if drawing, cancel, otherwise pass it up for deselecting
+                    cc->state = SP_CONNECTOR_CONTEXT_STOP;
+                    spcc_reset_colors(cc);
+                    ret = TRUE;
+                }
+                break;
+            default:
+                break;
+        }
+    }
+    else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
+    {
+        switch ( cc->state )
+        {
+            case SP_CONNECTOR_CONTEXT_DRAGGING:
+                if ( keyval == GDK_Escape )
+                {
+                    // Cancel connection point dragging
+
+                    // Obtain original position
+                    ConnectionPoint const& cp = cc->connpthandles[cc->selected_handle];
+                    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+                    const Geom::Matrix& i2doc = (cc->active_shape)->i2doc_affine();
+                    sp_knot_set_position(cc->selected_handle, cp.pos * i2doc * desktop->doc2dt(), 0);
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
+                        _("Connection point drag cancelled."));
+                    ret = TRUE;
+                }
+                else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
+                {
+                    // Put connection point at current position
+
+                    Geom::Point p = cc->selected_handle->pos;
+
+                    if (!cc->within_tolerance)
+                    {
+                        SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+                        SnapManager &m = desktop->namedview->snap_manager;
+                        m.setup(desktop);
+                        m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                        m.unSetup();
+                        sp_knot_set_position(cc->selected_handle, p, 0);
+                        ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
+                        cp.pos = p * (cc->active_shape)->dt2i_affine();
+                        cc->active_shape->avoidRef->updateConnectionPoint(cp);
                     }
-                    g_free(shape_label);
+
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
                 }
-                cc->clickeditem->setHidden(false);
-                sp_conn_adjust_path(SP_PATH(cc->clickeditem));
-                cc->clickeditem->updateRepr(); 
-                sp_document_done(doc);
-                cc_set_active_conn(cc, cc->clickeditem);
-                sp_document_ensure_up_to_date(doc);
-                cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-                return TRUE;
                 break;
-            }
-            case SP_CONNECTOR_CONTEXT_STOP:
-                /* This is allowed, if we just cancelled curve */
+            case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
+                if ( keyval == GDK_Escape )
+                {
+                    // Just destroy the knot
+                    g_object_unref( cc->selected_handle );
+                    cc->selected_handle = NULL;
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
+                }
+                else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
+                {
+                    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+                    SPDocument *doc = sp_desktop_document(desktop);
+                    SnapManager &m = desktop->namedview->snap_manager;
+                    m.setup(desktop);
+                    Geom::Point p = cc->selected_handle->pos;
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
+                    sp_knot_set_position(cc->selected_handle, p, 0);
+
+                    ConnectionPoint cp;
+                    cp.type = ConnPointUserDefined;
+                    cp.pos = p * (cc->active_shape)->dt2i_affine();
+                    cp.dir = Avoid::ConnDirAll;
+                    g_object_unref(cc->selected_handle);
+                    cc->active_shape->avoidRef->addConnectionPoint(cp);
+                    doc->ensureUpToDate();
+                    for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
+                        if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
+                        {
+                            cc->selected_handle = it->first;
+                            break;
+                        }
+                    cc_select_handle( cc->selected_handle );
+                    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
+                    ret = TRUE;
+                }
+
                 break;
-            default:
+            case SP_CONNECTOR_CONTEXT_IDLE:
+                if ( keyval == GDK_Delete && cc->selected_handle )
+                {
+                    cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
+                    cc->selected_handle = NULL;
+                    ret = TRUE;
+                }
+
                 break;
         }
-        ret = TRUE;
     }
 
     return ret;
 }
 
 
-static gint
-connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
+static void
+cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p)
 {
-    gint ret = FALSE;
-    /* fixme: */
-    switch (keyval) {
-        case GDK_Return:
-        case GDK_KP_Enter:
-            if (cc->npoints != 0) {
-                spcc_connector_finish(cc);
-                ret = TRUE;
+    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+    SPDocument *doc = sp_desktop_document(desktop);
+
+    // Clear the temporary path:
+    cc->red_curve->reset();
+    sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
+
+    if (p != NULL)
+    {
+        // Test whether we clicked on a connection point
+        gchar *shape_label, *cpid;
+        bool found = conn_pt_handle_test(cc, *p, &shape_label, &cpid);
+
+        if (found) {
+            if (cc->clickedhandle == cc->endpt_handle[0]) {
+                cc->clickeditem->setAttribute("inkscape:connection-start", shape_label, false);
+                cc->clickeditem->setAttribute("inkscape:connection-start-point", cpid, false);
             }
-            break;
-        case GDK_Escape:
-            if (cc->npoints != 0) {
-                // if drawing, cancel, otherwise pass it up for deselecting
-                cc->state = SP_CONNECTOR_CONTEXT_STOP;
-                spcc_reset_colors(cc);
-                ret = TRUE;
+            else {
+                cc->clickeditem->setAttribute("inkscape:connection-end", shape_label, false);
+                cc->clickeditem->setAttribute("inkscape:connection-end-point", cpid, false);
             }
-            break;
-        default:
-            break;
+            g_free(shape_label);
+        }
     }
-    return ret;
+    cc->clickeditem->setHidden(false);
+    sp_conn_reroute_path_immediate(SP_PATH(cc->clickeditem));
+    cc->clickeditem->updateRepr();
+    DocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR,
+                     _("Reroute connector"));
+    cc_set_active_conn(cc, cc->clickeditem);
 }
 
 
@@ -748,16 +1329,16 @@ static void
 spcc_reset_colors(SPConnectorContext *cc)
 {
     /* Red */
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
-    
-    sp_curve_reset(cc->green_curve);
+
+    cc->green_curve->reset();
     cc->npoints = 0;
 }
 
 
 static void
-spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
+spcc_connector_set_initial_point(SPConnectorContext *const cc, Geom::Point const p)
 {
     g_assert( cc->npoints == 0 );
 
@@ -769,37 +1350,33 @@ spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p
 
 
 static void
-spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
+spcc_connector_set_subsequent_point(SPConnectorContext *const cc, Geom::Point const p)
 {
     g_assert( cc->npoints != 0 );
 
     SPDesktop *dt = cc->desktop;
-    NR::Point o = dt->dt2doc(cc->p[0]);
-    NR::Point d = dt->dt2doc(p);
-    Avoid::Point src = { o[NR::X], o[NR::Y] };
-    Avoid::Point dst = { d[NR::X], d[NR::Y] };
+    Geom::Point o = dt->dt2doc(cc->p[0]);
+    Geom::Point d = dt->dt2doc(p);
+    Avoid::Point src(o[Geom::X], o[Geom::Y]);
+    Avoid::Point dst(d[Geom::X], d[Geom::Y]);
 
     if (!cc->newConnRef) {
-        cc->newConnRef = new Avoid::ConnRef(0, src, dst);
-        cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
+        Avoid::Router *router = sp_desktop_document(dt)->router;
+        cc->newConnRef = new Avoid::ConnRef(router);
+        cc->newConnRef->setEndpoint(Avoid::VertID::src, src);
+        if (cc->isOrthogonal)
+            cc->newConnRef->setRoutingType(Avoid::ConnType_Orthogonal);
+        else
+            cc->newConnRef->setRoutingType(Avoid::ConnType_PolyLine);
     }
-    cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
-
+    // Set new endpoint.
+    cc->newConnRef->setEndpoint(Avoid::VertID::tar, dst);
+    // Immediately generate new routes for connector.
     cc->newConnRef->makePathInvalid();
-    cc->newConnRef->generatePath(src, dst);
-
-    Avoid::PolyLine route = cc->newConnRef->route();
-    cc->newConnRef->calcRouteDist();
-
-    sp_curve_reset(cc->red_curve);
-    NR::Point pt(route.ps[0].x, route.ps[0].y);
-    sp_curve_moveto(cc->red_curve, pt);
-
-    for (int i = 1; i < route.pn; ++i) {
-        NR::Point p(route.ps[i].x, route.ps[i].y);
-        sp_curve_lineto(cc->red_curve, p);
-    }
-    sp_curve_transform(cc->red_curve, dt->doc2dt());
+    cc->newConnRef->router()->processTransaction();
+    // Recreate curve from libavoid route.
+    recreateCurve( cc->red_curve, cc->newConnRef, cc->curvature );
+    cc->red_curve->transform(dt->doc2dt());
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
 }
 
@@ -813,19 +1390,19 @@ static void
 spcc_concat_colors_and_flush(SPConnectorContext *cc)
 {
     SPCurve *c = cc->green_curve;
-    cc->green_curve = sp_curve_new_sized(64);
+    cc->green_curve = new SPCurve();
 
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
 
-    if (sp_curve_empty(c)) {
-        sp_curve_unref(c);
+    if (c->is_empty()) {
+        c->unref();
         return;
     }
 
     spcc_flush_white(cc, c);
 
-    sp_curve_unref(c);
+    c->unref();
 }
 
 
@@ -844,80 +1421,91 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
 
     if (gc) {
         c = gc;
-        sp_curve_ref(c);
+        c->ref();
     } else {
         return;
     }
 
     /* Now we have to go back to item coordinates at last */
-    sp_curve_transform(c,
-            sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
+    c->transform(SP_EVENT_CONTEXT_DESKTOP(cc)->dt2doc());
 
     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
-    SPDocument *doc = SP_DT_DOCUMENT(desktop);
+    SPDocument *doc = sp_desktop_document(desktop);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
-    if ( c && !sp_curve_empty(c) ) {
+    if ( c && !c->is_empty() ) {
         /* We actually have something to write */
 
-        Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+        Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         /* Set style */
-        sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
+        sp_desktop_apply_style_tool(desktop, repr, "/tools/connector", false);
 
-        gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
+        gchar *str = sp_svg_write_path( c->get_pathvector() );
         g_assert( str != NULL );
         repr->setAttribute("d", str);
         g_free(str);
 
         /* Attach repr */
         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
-        cc->selection->set(repr);
-        Inkscape::GC::release(repr);
-        cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
-        cc->newconn->updateRepr();
+        cc->newconn->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
 
         bool connection = false;
-        sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
-                "polyline", false);
-        if (cc->sid)
+        cc->newconn->setAttribute( "inkscape:connector-type",
+                                   cc->isOrthogonal ? "orthogonal" : "polyline", false );
+        cc->newconn->setAttribute( "inkscape:connector-curvature",
+                                   Glib::Ascii::dtostr(cc->curvature).c_str(), false );
+        if (cc->shref)
         {
-            sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
-                    cc->sid, false);
+            cc->newconn->setAttribute( "inkscape:connection-start", cc->shref, false);
+            if (cc->scpid) {
+                cc->newconn->setAttribute( "inkscape:connection-start-point", cc->scpid, false);
+            }
             connection = true;
         }
 
-        if (cc->eid)
+        if (cc->ehref)
         {
-            sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
-                    cc->eid, false);
+            cc->newconn->setAttribute( "inkscape:connection-end", cc->ehref, false);
+            if (cc->ecpid) {
+                cc->newconn->setAttribute( "inkscape:connection-end-point", cc->ecpid, false);
+            }
             connection = true;
         }
+        // Process pending updates.
         cc->newconn->updateRepr();
+        doc->ensureUpToDate();
+
         if (connection) {
             // Adjust endpoints to shape edge.
-            sp_conn_adjust_path(SP_PATH(cc->newconn));
+            sp_conn_reroute_path_immediate(SP_PATH(cc->newconn));
+            cc->newconn->updateRepr();
         }
-        cc->newconn->updateRepr();
+
+        // Only set the selection after we are finished with creating the attributes of
+        // the connector.  Otherwise, the selection change may alter the defaults for
+        // values like curvature in the connector context, preventing subsequent lookup
+        // of their original values.
+        cc->selection->set(repr);
+        Inkscape::GC::release(repr);
     }
 
-    sp_curve_unref(c);
+    c->unref();
 
-    /* Flush pending updates */
-    sp_document_done(doc);
-    sp_document_ensure_up_to_date(doc);
+    DocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
 }
 
 
 static void
-spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
+spcc_connector_finish_segment(SPConnectorContext *const cc, Geom::Point const /*p*/)
 {
-    if (!sp_curve_empty(cc->red_curve)) {
-        sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
+    if (!cc->red_curve->is_empty()) {
+        cc->green_curve->append_continuous(cc->red_curve, 0.0625);
 
         cc->p[0] = cc->p[3];
         cc->p[1] = cc->p[4];
         cc->npoints = 2;
 
-        sp_curve_reset(cc->red_curve);
+        cc->red_curve->reset();
     }
 }
 
@@ -928,17 +1516,16 @@ spcc_connector_finish(SPConnectorContext *const cc)
     SPDesktop *const desktop = cc->desktop;
     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
 
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     spcc_concat_colors_and_flush(cc);
 
     cc->npoints = 0;
-    
+
     if (cc->newConnRef) {
         cc->newConnRef->removeFromGraph();
         delete cc->newConnRef;
         cc->newConnRef = NULL;
     }
-    cc->state = SP_CONNECTOR_CONTEXT_IDLE;
 }
 
 
@@ -947,37 +1534,33 @@ cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
 {
     g_assert (knot != NULL);
 
+    g_object_ref(knot);
+
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
             knot->desktop->event_context);
 
     gboolean consumed = FALSE;
 
+    gchar* knot_tip = knot->tip ? knot->tip : cc->knot_tip;
     switch (event->type) {
         case GDK_ENTER_NOTIFY:
-            gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
-                    knot->fill [SP_KNOT_STATE_MOUSEOVER], NULL);
-            gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
-                    knot->stroke [SP_KNOT_STATE_MOUSEOVER], NULL);
+            sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
 
             cc->active_handle = knot;
-
-            if (knot->tip)
+            if (knot_tip)
             {
                 knot->desktop->event_context->defaultMessageContext()->set(
-                        Inkscape::NORMAL_MESSAGE, knot->tip);
+                        Inkscape::NORMAL_MESSAGE, knot_tip);
             }
 
             consumed = TRUE;
             break;
         case GDK_LEAVE_NOTIFY:
-            gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
-                    knot->fill [SP_KNOT_STATE_NORMAL], NULL);
-            gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
-                    knot->stroke [SP_KNOT_STATE_NORMAL], NULL);
-            
+            sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
+
             cc->active_handle = NULL;
 
-            if (knot->tip) {
+            if (knot_tip) {
                 knot->desktop->event_context->defaultMessageContext()->clear();
             }
 
@@ -987,12 +1570,14 @@ cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
             break;
     }
 
+    g_object_unref(knot);
+
     return consumed;
 }
 
 
-static gboolean 
-endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
+static gboolean
+endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
 {
     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
 
@@ -1007,12 +1592,12 @@ endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
                 cc->clickedhandle = cc->active_handle;
                 cc_clear_active_conn(cc);
                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
-                
+
                 // Disconnect from attached shape
                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
                 sp_conn_end_detach(cc->clickeditem, ind);
-              
-                NR::Point origin;
+
+                Geom::Point origin;
                 if (cc->clickedhandle == cc->endpt_handle[0]) {
                     origin = cc->endpt_handle[1]->pos;
                 }
@@ -1021,11 +1606,10 @@ endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
                 }
 
                 // Show the red path for dragging.
-                cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
-                NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
-                sp_curve_transform(cc->red_curve, i2d);
-                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
-                        cc->red_curve);
+                cc->red_curve = SP_PATH(cc->clickeditem)->original_curve ? SP_PATH(cc->clickeditem)->original_curve->copy() : SP_PATH(cc->clickeditem)->curve->copy();
+                Geom::Matrix i2d = (cc->clickeditem)->i2d_affine();
+                cc->red_curve->transform(i2d);
+                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
 
                 cc->clickeditem->setHidden(true);
 
@@ -1041,73 +1625,138 @@ endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
     return consumed;
 }
 
+static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp)
+{
+        SPKnot *knot = sp_knot_new(desktop, 0);
+
+        knot->setShape(SP_KNOT_SHAPE_SQUARE);
+        knot->setSize(8);
+        knot->setAnchor(GTK_ANCHOR_CENTER);
+        knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
+        sp_knot_update_ctrl(knot);
+
+        // We don't want to use the standard knot handler.
+        g_signal_handler_disconnect(G_OBJECT(knot->item),
+                knot->_event_handler_id);
+        knot->_event_handler_id = 0;
+
+        gtk_signal_connect(GTK_OBJECT(knot->item), "event",
+                GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
+        sp_knot_set_position(knot, item->avoidRef->getConnectionPointPos(cp.type, cp.id) * desktop->doc2dt(), 0);
+        sp_knot_show(knot);
+        cphandles[knot] = cp;
+}
 
 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
 {
     g_assert(item != NULL );
-   
-    cc->active_shape = item;
 
-    // Remove existing active shape listeners
-    if (cc->active_shape_repr) {
-        sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
-        Inkscape::GC::release(cc->active_shape_repr);
-        
-        sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
-        Inkscape::GC::release(cc->active_shape_layer_repr);
+    std::map<int, ConnectionPoint>* connpts = &item->avoidRef->connection_points;
+
+    if (cc->active_shape != item)
+    {
+        // The active shape has changed
+        // Rebuild everything
+        cc->active_shape = item;
+        // Remove existing active shape listeners
+        if (cc->active_shape_repr) {
+            sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
+            Inkscape::GC::release(cc->active_shape_repr);
+
+            sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
+            Inkscape::GC::release(cc->active_shape_layer_repr);
+        }
+
+        // Listen in case the active shape changes
+        cc->active_shape_repr = SP_OBJECT_REPR(item);
+        if (cc->active_shape_repr) {
+            Inkscape::GC::anchor(cc->active_shape_repr);
+            sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
+
+            cc->active_shape_layer_repr = cc->active_shape_repr->parent();
+            Inkscape::GC::anchor(cc->active_shape_layer_repr);
+            sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
+        }
+
+
+        // Set the connection points.
+        if ( cc->connpthandles.size() )
+            // destroy the old list
+            while (! cc->connpthandles.empty() )
+            {
+                g_object_unref(cc->connpthandles.begin()->first);
+                cc->connpthandles.erase(cc->connpthandles.begin());
+            }
+        // build the new one
+        if ( connpts->size() )
+        for (std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it)
+            cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
+
+        // Also add default connection points
+        // For now, only centre default connection point will
+        // be available
+        ConnectionPoint centre;
+        centre.type = ConnPointDefault;
+        centre.id = ConnPointPosCC;
+        cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, centre);
+    }
+    else
+    {
+        // The active shape didn't change
+        // Update only the connection point knots
+
+        // Ensure the item's connection_points map
+        // has been updated
+        item->document->ensureUpToDate();
+
+        std::set<int> seen;
+        for  ( ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end() ;)
+        {
+            bool removed = false;
+            if ( it->second.type == ConnPointUserDefined )
+            {
+                std::map<int, ConnectionPoint>::iterator p = connpts->find(it->second.id);
+                if (p != connpts->end())
+                {
+                    if ( it->second != p->second )
+                        // Connection point position has changed
+                        // Update knot position
+                        sp_knot_set_position(it->first,
+                                             item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
+                    seen.insert(it->second.id);
+                    sp_knot_show(it->first);
+                }
+                else
+                {
+                    // This connection point does no longer exist,
+                    // remove the knot
+                    ConnectionPointMap::iterator curr = it;
+                    ++it;
+                    g_object_unref( curr->first );
+                    cc->connpthandles.erase(curr);
+                    removed = true;
+                }
+            }
+            else
+            {
+                // It's a default connection point
+                // Just make sure it's position is correct
+                sp_knot_set_position(it->first,
+                                     item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
+                sp_knot_show(it->first);
+
+            }
+            if ( !removed )
+                ++it;
+        }
+        // Add knots for new connection points.
+        if (connpts->size())
+            for ( std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it )
+                if ( seen.find(it->first) == seen.end() )
+                    // A new connection point has been added
+                    // to the shape. Add a knot for it.
+                    cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
     }
-    
-    // Listen in case the active shape changes
-    cc->active_shape_repr = SP_OBJECT_REPR(item); 
-    if (cc->active_shape_repr) {
-        Inkscape::GC::anchor(cc->active_shape_repr);
-        sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
-    
-        cc->active_shape_layer_repr = cc->active_shape_repr->parent();
-        Inkscape::GC::anchor(cc->active_shape_layer_repr);
-        sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
-    }
-
-    // Set center connection point.
-    if ( cc->connpthandle == NULL ) {
-       SPKnot * knot = (SPKnot*)g_object_new (SP_TYPE_KNOT, 0);
-
-       knot->desktop = cc->desktop;
-       knot->flags = SP_KNOT_VISIBLE;
-
-       knot->item = sp_canvas_item_new (SP_DT_CONTROLS(cc->desktop),
-               SP_TYPE_CTRL,
-               "anchor", GTK_ANCHOR_CENTER,
-               "filled", TRUE,
-               "stroked", TRUE,
-               "mode", SP_KNOT_MODE_XOR,
-               NULL);
-
-       gtk_signal_connect (GTK_OBJECT (knot->item), "event",
-               GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
-
-        knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
-        knot->fill [SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
-        knot->stroke [SP_KNOT_STATE_NORMAL] = 0x01000000;
-
-        g_object_set(G_OBJECT(knot),
-                     "shape", SP_KNOT_SHAPE_SQUARE,
-                     "size", 8,
-                     "anchor", GTK_ANCHOR_CENTER,
-                     "tip", _("<b>Connection point</b>: click or drag to create a new connector"),
-                     NULL);
-        
-        cc->connpthandle = knot;
-    }
-    
-
-    NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
-    NR::Point center = bbox.midpoint();
-    sp_knot_set_position(cc->connpthandle, &center, 0);
-
-    sp_knot_show(cc->connpthandle);
-    
 }
 
 
@@ -1116,21 +1765,31 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
 {
     g_assert( SP_IS_PATH(item) );
 
-    SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
-    NR::Matrix i2d = sp_item_i2d_affine(item);
-    
+    SPCurve *curve = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve : SP_PATH(item)->curve;
+    Geom::Matrix i2d = item->i2d_affine();
+
     if (cc->active_conn == item)
     {
-        // Just adjust handle positions.
-        NR::Point startpt = sp_curve_first_point(curve) * i2d;
-        sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
-        
-        NR::Point endpt = sp_curve_last_point(curve) * i2d;
-        sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
-        
+        if (curve->is_empty())
+        {
+            // Connector is invisible because it is clipped to the boundary of
+            // two overlpapping shapes.
+            sp_knot_hide(cc->endpt_handle[0]);
+            sp_knot_hide(cc->endpt_handle[1]);
+        }
+        else
+        {
+            // Just adjust handle positions.
+            Geom::Point startpt = *(curve->first_point()) * i2d;
+            sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
+
+            Geom::Point endpt = *(curve->last_point()) * i2d;
+            sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
+        }
+
         return;
     }
-    
+
     cc->active_conn = item;
 
     // Remove existing active conn listeners
@@ -1139,9 +1798,9 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
         Inkscape::GC::release(cc->active_conn_repr);
         cc->active_conn_repr = NULL;
     }
-    
+
     // Listen in case the active conn changes
-    cc->active_conn_repr = SP_OBJECT_REPR(item); 
+    cc->active_conn_repr = SP_OBJECT_REPR(item);
     if (cc->active_conn_repr) {
         Inkscape::GC::anchor(cc->active_conn_repr);
         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
@@ -1151,36 +1810,28 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
 
         // Create the handle if it doesn't exist
         if ( cc->endpt_handle[i] == NULL ) {
-            SPKnot * knot = (SPKnot*) g_object_new (SP_TYPE_KNOT, 0);
+            SPKnot *knot = sp_knot_new(cc->desktop,
+                    _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
 
-            knot->desktop = cc->desktop;
-            knot->flags = SP_KNOT_VISIBLE;
+            knot->setShape(SP_KNOT_SHAPE_SQUARE);
+            knot->setSize(7);
+            knot->setAnchor(GTK_ANCHOR_CENTER);
+            knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
+            knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
+            sp_knot_update_ctrl(knot);
 
-            knot->item = sp_canvas_item_new (SP_DT_CONTROLS (cc->desktop),
-                    SP_TYPE_CTRL,
-                    "anchor", GTK_ANCHOR_CENTER,
-                    "filled", TRUE,
-                    "stroked", TRUE,
-                    "mode", SP_KNOT_MODE_XOR,
-                    NULL);
+            // We don't want to use the standard knot handler,
+            // since we don't want this knot to be draggable.
+            g_signal_handler_disconnect(G_OBJECT(knot->item),
+                    knot->_event_handler_id);
+            knot->_event_handler_id = 0;
 
-            knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
-            knot->stroke [SP_KNOT_STATE_NORMAL] = 0x000000ff;
-            knot->stroke [SP_KNOT_STATE_DRAGGING] = 0x000000ff;
-            knot->stroke [SP_KNOT_STATE_MOUSEOVER] = 0x000000ff;
-
-            g_object_set(G_OBJECT(knot),
-                    "shape", SP_KNOT_SHAPE_DIAMOND,
-                    "size", 10,
-                    "tip", _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"),
-                    NULL);
-
-            gtk_signal_connect (GTK_OBJECT (knot->item), "event",
-                    GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
+            gtk_signal_connect(GTK_OBJECT(knot->item), "event",
+                    GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
 
             cc->endpt_handle[i] = knot;
         }
-    
+
         // Remove any existing handlers
         if (cc->endpt_handler_id[i]) {
             g_signal_handlers_disconnect_by_func(
@@ -1189,34 +1840,77 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
             cc->endpt_handler_id[i] = 0;
         }
 
-        // Setup handlers for connector endpoints, this is 
+        // Setup handlers for connector endpoints, this is
         // is as 'after' so that cc_generic_knot_handler is
         // triggered first for any endpoint.
         cc->endpt_handler_id[i] = g_signal_connect_after(
                 G_OBJECT(cc->endpt_handle[i]->item), "event",
                 G_CALLBACK(endpt_handler), cc);
     }
-    
-    NR::Point startpt = sp_curve_first_point(curve) * i2d;
-    sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
-    
-    NR::Point endpt = sp_curve_last_point(curve) * i2d;
-    sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
-    
+
+    if (curve->is_empty())
+    {
+        // Connector is invisible because it is clipped to the boundary 
+        // of two overlpapping shapes.  So, it doesn't need endpoints.
+        return;
+    }
+
+    Geom::Point startpt = *(curve->first_point()) * i2d;
+    sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
+
+    Geom::Point endpt = *(curve->last_point()) * i2d;
+    sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
+
     sp_knot_show(cc->endpt_handle[0]);
     sp_knot_show(cc->endpt_handle[1]);
 }
 
+void cc_create_connection_point(SPConnectorContext* cc)
+{
+    if (cc->active_shape && cc->state == SP_CONNECTOR_CONTEXT_IDLE)
+    {
+        if (cc->selected_handle)
+        {
+            cc_deselect_handle( cc->selected_handle );
+        }
+        SPKnot *knot = sp_knot_new(cc->desktop, 0);
+        // We do not process events on this knot.
+        g_signal_handler_disconnect(G_OBJECT(knot->item),
+                                    knot->_event_handler_id);
+        knot->_event_handler_id = 0;
+
+        cc_select_handle( knot );
+        cc->selected_handle = knot;
+        sp_knot_show(cc->selected_handle);
+        cc->state = SP_CONNECTOR_CONTEXT_NEWCONNPOINT;
+    }
+}
+
+void cc_remove_connection_point(SPConnectorContext* cc)
+{
+    if (cc->selected_handle && cc->state == SP_CONNECTOR_CONTEXT_IDLE )
+    {
+        cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
+        cc->selected_handle = NULL;
+    }
+}
 
 static bool cc_item_is_shape(SPItem *item)
 {
     if (SP_IS_PATH(item)) {
         SPCurve *curve = (SP_SHAPE(item))->curve;
-        if ( curve && !(curve->closed) ) {
+        if ( curve && !(curve->is_closed()) ) {
             // Open paths are connectors.
             return false;
         }
     }
+    else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+        if (prefs->getBool("/tools/connector/ignoretext", true)) {
+            // Don't count text as a shape we can connect connector to.
+            return false;
+        }
+    }
     return true;
 }
 
@@ -1224,15 +1918,17 @@ static bool cc_item_is_shape(SPItem *item)
 bool cc_item_is_connector(SPItem *item)
 {
     if (SP_IS_PATH(item)) {
-        if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
-            g_assert( !(SP_SHAPE(item)->curve->closed) );
+        bool closed = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve->is_closed() : SP_PATH(item)->curve->is_closed();
+        if (SP_PATH(item)->connEndPair.isAutoRoutingConn() && !closed) {
+            // To be considered a connector, an object must be a non-closed 
+            // path that is marked with a "inkscape:connector-type" attribute.
             return true;
         }
     }
     return false;
 }
 
-    
+
 void cc_selection_set_avoid(bool const set_avoid)
 {
     SPDesktop *desktop = inkscape_active_desktop();
@@ -1240,9 +1936,9 @@ void cc_selection_set_avoid(bool const set_avoid)
         return;
     }
 
-    SPDocument *document = SP_DT_DOCUMENT(desktop);
+    SPDocument *document = sp_desktop_document(desktop);
 
-    Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
+    Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     GSList *l = (GSList *) selection->itemList();
 
@@ -1252,14 +1948,13 @@ void cc_selection_set_avoid(bool const set_avoid)
         SPItem *item = (SPItem *) l->data;
 
         char const *value = (set_avoid) ? "true" : NULL;
-        
+
         if (cc_item_is_shape(item)) {
-            sp_object_setAttribute(item, "inkscape:connector-avoid",
-                    value, false);
+            item->setAttribute("inkscape:connector-avoid", value, false);
             item->avoidRef->handleSettingChange();
             changes++;
         }
-        
+
         l = l->next;
     }
 
@@ -1269,9 +1964,12 @@ void cc_selection_set_avoid(bool const set_avoid)
         return;
     }
 
-    sp_document_done(document);
+    char *event_desc = (set_avoid) ?
+            _("Make connectors avoid selected objects") :
+            _("Make connectors ignore selected objects");
+    DocumentUndo::done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
 }
-    
+
 
 static void
 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
@@ -1280,10 +1978,10 @@ cc_selection_changed(Inkscape::Selection *selection, gpointer data)
     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
 
     SPItem *item = selection->singleItem();
-    
+
     if (cc->active_conn == item)
     {
-        // Noting to change.
+        // Nothing to change.
         return;
     }
     if (item == NULL)
@@ -1291,7 +1989,7 @@ cc_selection_changed(Inkscape::Selection *selection, gpointer data)
         cc_clear_active_conn(cc);
         return;
     }
-    
+
     if (cc_item_is_connector(item)) {
         cc_set_active_conn(cc, item);
     }
@@ -1299,8 +1997,8 @@ cc_selection_changed(Inkscape::Selection *selection, gpointer data)
 
 
 static void
-shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
-        Inkscape::XML::Node *ref, gpointer data)
+shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
+                         Inkscape::XML::Node */*ref*/, gpointer data)
 {
     g_assert(data);
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
@@ -1314,13 +2012,13 @@ shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
 
 static void
 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
-                            gchar const *old_value, gchar const *new_value,
-                            bool is_interactive, gpointer data)
+                         gchar const */*old_value*/, gchar const */*new_value*/,
+                         bool /*is_interactive*/, gpointer data)
 {
     g_assert(data);
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
-  
-    // Look for changes than result in onscreen movement.
+
+    // Look for changes that result in onscreen movement.
     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
             !strcmp(name, "width") || !strcmp(name, "height") ||
             !strcmp(name, "transform"))
@@ -1335,6 +2033,12 @@ shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
             cc_set_active_conn(cc, cc->active_conn);
         }
     }
+    else
+        if ( !strcmp(name, "inkscape:connection-points") )
+            if (repr == cc->active_shape_repr)
+                // The connection points of the active shape
+                // have changed. Update them.
+                cc_set_active_shape(cc, cc->active_shape);
 }
 
 
@@ -1347,4 +2051,4 @@ shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :