Code

Remove redundancy from snapping API (type of snapsource no longer has to be specified...
[inkscape.git] / src / connector-context.cpp
index 228c81d29e0bdc26365ebe694bf4600778f901f3..bfdef103213dfb7f8775cd97592c6ef12dbbd3ba 100644 (file)
@@ -35,7 +35,7 @@
  *     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 
+ *  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
  * ----------------------------------------------------------------------------
  *
  * 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, 
+ *      -  Controls on the connector tool be should be reordered logically,
  *     possibly as follows:
- * 
- *     *Connector*: [Polyline-radio-button] [Orthgonal-radio-button] 
+ *
+ *     *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 
+ *
+ *     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:" 
+ *
+ *     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 
+ *
+ *     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 
+ *     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 
+ *     place connection points inside or on the boundary of the shape
  *     itself.
- *  
- *      -  The intended ability to place a new point at the current cursor 
+ *
+ *      -  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 
+ *
+ *      -  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 
+ *     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,
  *     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 
+ *     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
 #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 "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"
@@ -333,7 +333,7 @@ sp_connector_context_init(SPConnectorContext *cc)
     cc->clickedhandle = NULL;
 
     new (&cc->connpthandles) ConnectionPointMap();
-    
+
     for (int i = 0; i < 2; ++i) {
         cc->endpt_handle[i] = NULL;
         cc->endpt_handler_id[i] = 0;
@@ -353,7 +353,7 @@ sp_connector_context_dispose(GObject *object)
     cc->sel_changed_connection.disconnect();
 
     if (!cc->connpthandles.empty()) {
-        for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); 
+        for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
                 it != cc->connpthandles.end(); ++it) {
             g_object_unref(it->first);
         }
@@ -413,7 +413,7 @@ sp_connector_context_setup(SPEventContext *ec)
     cc_selection_changed(cc->selection, (gpointer) cc);
 
     cc->within_tolerance = false;
-    
+
     sp_event_context_read(ec, "curvature");
     sp_event_context_read(ec, "orthogonal");
     sp_event_context_read(ec, "mode");
@@ -472,7 +472,7 @@ void sp_connector_context_switch_mode(SPEventContext* ec, unsigned int newMode)
         {
             cc->selection->set( SP_OBJECT( cc->active_shape ) );
         }
-        else 
+        else
         {
             SPItem* item = cc->selection->singleItem();
             if ( item )
@@ -537,7 +537,7 @@ cc_clear_active_shape(SPConnectorContext *cc)
 
     // Hide the connection points if they exist.
     if (cc->connpthandles.size()) {
-        for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); 
+        for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
                 it != cc->connpthandles.end(); ++it) {
             sp_knot_hide(it->first);
         }
@@ -616,7 +616,7 @@ sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, G
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
 
     Geom::Point p(event->button.x, event->button.y);
-    
+
     switch (event->type) {
         case GDK_BUTTON_RELEASE:
             if (event->button.button == 1 && !event_context->space_panning) {
@@ -625,7 +625,6 @@ sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, G
                 {
                     spcc_reset_colors(cc);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(event_context);
                 }
                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
                     // Doing something else like rerouting.
@@ -660,11 +659,11 @@ sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, G
             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) 
+    /*                if (!(cc->active_shape)
                             // Don't show handle for another handle.
     //                         || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
                         )
@@ -757,7 +756,7 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
 
             switch (cc->state) {
                 case SP_CONNECTOR_CONTEXT_STOP:
-                    /* This is allowed, if we just cancelled curve */
+                    /* This is allowed, if we just canceled curve */
                 case SP_CONNECTOR_CONTEXT_IDLE:
                 {
                     if ( cc->npoints == 0 ) {
@@ -772,12 +771,10 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                         // Test whether we clicked on a connection point
                         cc->sid = conn_pt_handle_test(cc, p);
 
-//                        sp_event_context_snap_window_open(event_context);
-
                         if (!cc->sid) {
                             // This is the first point, so just snap it to the grid
                             // as there's no other points to go off.
-                            m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                            m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
                         }
                         spcc_connector_set_initial_point(cc, p);
 
@@ -789,7 +786,7 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                 case SP_CONNECTOR_CONTEXT_DRAGGING:
                 {
                     // This is the second click of a connector creation.
-                    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
 
                     spcc_connector_set_subsequent_point(cc, p);
                     spcc_connector_finish_segment(cc, p);
@@ -800,7 +797,6 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                     }
                     cc_set_active_conn(cc, cc->newconn);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(event_context);
                     ret = TRUE;
                     break;
                 }
@@ -819,7 +815,6 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                 cc_connector_rerouting_finish(cc, &p);
 
                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                sp_event_context_snap_window_closed(event_context);
 
                 // Don't set ret to TRUE, so we drop through to the
                 // parent handler which will open the context menu.
@@ -827,7 +822,6 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
             else if (cc->npoints != 0) {
                 spcc_connector_finish(cc);
                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                sp_event_context_snap_window_closed(event_context);
                 ret = TRUE;
             }
         }
@@ -847,7 +841,7 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
             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)
@@ -879,7 +873,6 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
 
                     if ( cc->selected_handle )
                     {
-//                         sp_event_context_snap_window_open(event_context);
                         cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
                         cc->selection->set( SP_OBJECT( cc->active_shape ) );
                     }
@@ -934,15 +927,14 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
     {
         SnapManager &m = dt->namedview->snap_manager;
         m.setup(dt);
-        
+
         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.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p,
-                            Inkscape::SNAPSOURCE_HANDLE);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
                     cc->selection->clear();
                     spcc_connector_set_subsequent_point(cc, p);
                     ret = TRUE;
@@ -954,8 +946,7 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
                 gobble_motion_events(GDK_BUTTON1_MASK);
                 g_assert( SP_IS_PATH(cc->clickeditem));
 
-                m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p,
-                        Inkscape::SNAPSOURCE_HANDLE);
+                m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
 
                 // Update the hidden path
                 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
@@ -1030,7 +1021,7 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
                 //case SP_CONNECTOR_CONTEXT_POINT:
                 case SP_CONNECTOR_CONTEXT_DRAGGING:
                 {
-                    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
 
                     if (cc->within_tolerance)
                     {
@@ -1047,17 +1038,15 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
                     }
                     cc_set_active_conn(cc, cc->newconn);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(event_context);
                     break;
                 }
                 case SP_CONNECTOR_CONTEXT_REROUTING:
                 {
-                    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
                     cc_connector_rerouting_finish(cc, &p);
 
                     sp_document_ensure_up_to_date(doc);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(event_context);
                     return TRUE;
                     break;
                 }
@@ -1074,13 +1063,11 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
             switch ( cc->state )
             {
                 case SP_CONNECTOR_CONTEXT_DRAGGING:
-                    
+
                     if (!cc->within_tolerance)
                     {
-//                        sp_event_context_snap_window_open(event_context);
-                        m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                        m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
                         sp_knot_set_position(cc->selected_handle, p, 0);
-//                        sp_event_context_snap_window_closed(event_context);
                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
@@ -1092,11 +1079,9 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
 
 
                 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
-//                    sp_event_context_snap_window_open( event_context );
-                    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
-                    
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+
                     sp_knot_set_position(cc->selected_handle, p, 0);
-//                    sp_event_context_snap_window_closed(event_context);
 
                     ConnectionPoint cp;
                     cp.type = ConnPointUserDefined;
@@ -1118,7 +1103,7 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
             }
         }
     }
-    
+
 
     return ret;
 }
@@ -1137,7 +1122,6 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                 if (cc->npoints != 0) {
                     spcc_connector_finish(cc);
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
                     ret = TRUE;
                 }
                 break;
@@ -1152,7 +1136,6 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     sp_document_undo(doc);
 
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
-//                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
                             _("Connector endpoint drag cancelled."));
                     ret = TRUE;
@@ -1160,7 +1143,6 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                 else if (cc->npoints != 0) {
                     // if drawing, cancel, otherwise pass it up for deselecting
                     cc->state = SP_CONNECTOR_CONTEXT_STOP;
-//                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
                     spcc_reset_colors(cc);
                     ret = TRUE;
                 }
@@ -1196,14 +1178,12 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     SnapManager &m = desktop->namedview->snap_manager;
                     m.setup(desktop);
                     Geom::Point p = cc->selected_handle->pos;
-                    SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
-                    
+//                     SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
+
                     if (!cc->within_tolerance)
                     {
-//                        sp_event_context_snap_window_open(event_context);
-                        m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+                        m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
                         sp_knot_set_position(cc->selected_handle, p, 0);
-//                        sp_event_context_snap_window_closed(event_context);
                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
@@ -1229,13 +1209,10 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     SnapManager &m = desktop->namedview->snap_manager;
                     m.setup(desktop);
                     Geom::Point p = cc->selected_handle->pos;
-                    SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
-                    
-//                    sp_event_context_snap_window_open( event_context );
-                    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
-                    
+
+                    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+
                     sp_knot_set_position(cc->selected_handle, p, 0);
-//                    sp_event_context_snap_window_closed(event_context);
 
                     ConnectionPoint cp;
                     cp.type = ConnPointUserDefined;
@@ -1263,7 +1240,7 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     cc->selected_handle = NULL;
                     ret = TRUE;
                 }
-                
+
                 break;
         }
     }
@@ -1453,7 +1430,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
         // Process pending updates.
         cc->newconn->updateRepr();
         sp_document_ensure_up_to_date(doc);
-        
+
         if (connection) {
             // Adjust endpoints to shape edge.
             sp_conn_reroute_path_immediate(SP_PATH(cc->newconn));
@@ -1461,7 +1438,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
         }
 
         // Only set the selection after we are finished with creating the attributes of
-        // the connector.  Otherwise, the selection change may alter the defaults for 
+        // 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);
@@ -1571,7 +1548,6 @@ endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
                 cc->clickedhandle = cc->active_handle;
                 cc_clear_active_conn(cc);
                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
-//                sp_event_context_snap_window_open(SP_EVENT_CONTEXT(cc));
 
                 // Disconnect from attached shape
                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
@@ -1630,7 +1606,7 @@ static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, Connectio
 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
 {
     g_assert(item != NULL );
-    
+
     std::map<int, ConnectionPoint>* connpts = &item->avoidRef->connection_points;
 
     if (cc->active_shape != item)
@@ -1671,7 +1647,7 @@ static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
         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