Code

Fix self-snapping when dragging the transformation center of a selection containing...
[inkscape.git] / src / spiral-context.cpp
index 35d5144a7881d58f8b67f87321bd848fc10b6fa6..822fafb75fa13d58eab8fae4f7eaace5e672f856 100644 (file)
@@ -27,7 +27,6 @@
 #include "sp-namedview.h"
 #include "selection.h"
 #include "desktop-handles.h"
-#include "desktop-affine.h"
 #include "snap.h"
 #include "desktop.h"
 #include "desktop-style.h"
@@ -47,12 +46,14 @@ static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
 static void sp_spiral_context_dispose(GObject *object);
 static void sp_spiral_context_setup(SPEventContext *ec);
+static void sp_spiral_context_finish(SPEventContext *ec);
 static void sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
 
 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
 
 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
 static void sp_spiral_finish(SPSpiralContext *sc);
+static void sp_spiral_cancel(SPSpiralContext *sc);
 
 static SPEventContextClass *parent_class;
 
@@ -87,6 +88,7 @@ sp_spiral_context_class_init(SPSpiralContextClass *klass)
     object_class->dispose = sp_spiral_context_dispose;
 
     event_context_class->setup = sp_spiral_context_setup;
+    event_context_class->finish = sp_spiral_context_finish;
     event_context_class->set = sp_spiral_context_set;
     event_context_class->root_handler = sp_spiral_context_root_handler;
 }
@@ -114,6 +116,20 @@ sp_spiral_context_init(SPSpiralContext *spiral_context)
     new (&spiral_context->sel_changed_connection) sigc::connection();
 }
 
+static void sp_spiral_context_finish(SPEventContext *ec)
+{
+    SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
+       SPDesktop *desktop = ec->desktop;
+
+       sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
+       sp_spiral_finish(sc);
+    sc->sel_changed_connection.disconnect();
+
+    if (((SPEventContextClass *) parent_class)->finish) {
+               ((SPEventContextClass *) parent_class)->finish(ec);
+       }
+}
+
 static void
 sp_spiral_context_dispose(GObject *object)
 {
@@ -221,15 +237,12 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
             if (event->button.button == 1 && !event_context->space_panning) {
 
                 dragging = TRUE;
-                sp_canvas_set_snap_delay_active(desktop->canvas, true);
                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
 
                 SnapManager &m = desktop->namedview->snap_manager;
                 m.setup(desktop);
-                Geom::Point pt2g = to_2geom(sc->center);
-                m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
-                sc->center = from_2geom(pt2g);
-
+                m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
+                m.unSetup();
                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                                     ( GDK_KEY_PRESS_MASK |
                                       GDK_BUTTON_RELEASE_MASK |
@@ -258,19 +271,27 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
 
                 SnapManager &m = desktop->namedview->snap_manager;
                 m.setup(desktop, true, sc->item);
-                m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt);
+                m.freeSnapReturnByRef(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
+                m.unSetup();
                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
 
                 gobble_motion_events(GDK_BUTTON1_MASK);
 
                 ret = TRUE;
+            } else if (!sp_event_context_knot_mouseover(sc)) {
+                SnapManager &m = desktop->namedview->snap_manager;
+                m.setup(desktop);
+                Geom::Point const motion_w(event->motion.x, event->motion.y);
+                Geom::Point motion_dt(desktop->w2d(motion_w));
+                m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
+                m.unSetup();
             }
             break;
         case GDK_BUTTON_RELEASE:
             event_context->xp = event_context->yp = 0;
             if (event->button.button == 1 && !event_context->space_panning) {
                 dragging = FALSE;
-                sp_canvas_set_snap_delay_active(desktop->canvas, false);
+                sp_event_context_discard_delayed_snap_event(event_context);
                 if (!event_context->within_tolerance) {
                     // we've been dragging, finish the spiral
                     sp_spiral_finish(sc);
@@ -321,18 +342,23 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     }
                     break;
                 case GDK_Escape:
-                    sp_desktop_selection(desktop)->clear();
-                    //TODO: make dragging escapable by Esc
-                    break;
+                       if (dragging) {
+                               dragging = false;
+                               sp_event_context_discard_delayed_snap_event(event_context);
+                               // if drawing, cancel, otherwise pass it up for deselecting
+                               sp_spiral_cancel(sc);
+                               ret = TRUE;
+                       }
+                       break;
 
                 case GDK_space:
                     if (dragging) {
                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
                                               event->button.time);
                         dragging = false;
-                        sp_canvas_set_snap_delay_active(desktop->canvas, false);
+                        sp_event_context_discard_delayed_snap_event(event_context);
                         if (!event_context->within_tolerance) {
-                            // we've been dragging, finish the rect
+                            // we've been dragging, finish the spiral
                             sp_spiral_finish(sc);
                         }
                         // do not return true, so that space would work switching to selector
@@ -404,10 +430,10 @@ sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
     SnapManager &m = desktop->namedview->snap_manager;
     m.setup(desktop, true, sc->item);
     Geom::Point pt2g = to_2geom(p);
-    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
-
-    Geom::Point const p0 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, sc->center));
-    Geom::Point const p1 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, from_2geom(pt2g)));
+    m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
+    Geom::Point const p0 = desktop->dt2doc(sc->center);
+    Geom::Point const p1 = desktop->dt2doc(pt2g);
 
     SPSpiral *spiral = SP_SPIRAL(sc->item);
 
@@ -441,8 +467,13 @@ sp_spiral_finish(SPSpiralContext *sc)
     sc->_message_context->clear();
 
     if (sc->item != NULL) {
-        SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
-        SPSpiral  *spiral = SP_SPIRAL(sc->item);
+       SPSpiral *spiral = SP_SPIRAL(sc->item);
+       if (spiral->rad == 0) {
+               sp_spiral_cancel(sc); // Don't allow the creating of zero sized spiral, for example when the start and and point snap to the snap grid point
+               return;
+       }
+
+       SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
 
         sp_shape_set_shape(SP_SHAPE(spiral));
         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
@@ -457,6 +488,27 @@ sp_spiral_finish(SPSpiralContext *sc)
     }
 }
 
+static void sp_spiral_cancel(SPSpiralContext *sc)
+{
+       SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
+
+       sp_desktop_selection(desktop)->clear();
+       sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
+
+    if (sc->item != NULL) {
+       SP_OBJECT(sc->item)->deleteObject();
+       sc->item = NULL;
+    }
+
+    sc->within_tolerance = false;
+    sc->xp = 0;
+    sc->yp = 0;
+    sc->item_to_select = NULL;
+
+    sp_canvas_end_forced_full_redraws(desktop->canvas);
+
+    sp_document_cancel(sp_desktop_document(desktop));
+}
 
 /*
   Local Variables: