Code

more unreffing temporary styles properly
[inkscape.git] / src / knot.cpp
index 01b1bafa2b844252e677d5f08ab53de5eb83a5a1..dc5b78b1754c54f0fe4ff9cee1d3fcfa54f75123 100644 (file)
 #include "message-stack.h"
 #include "message-context.h"
 #include "event-context.h"
+#include "sp-namedview.h"
+#include "snap.h"
+#include "selection.h"
 
 
 #define KNOT_EVENT_MASK (GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | \
-                        GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | \
+                        GDK_POINTER_MOTION_MASK | \
                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK)
 
 static bool nograb = false;
@@ -62,7 +65,6 @@ static void sp_knot_init(SPKnot *knot);
 static void sp_knot_dispose(GObject *object);
 
 static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot);
-static void sp_knot_set_flag(SPKnot *knot, guint flag, bool set);
 static void sp_knot_set_ctrl_state(SPKnot *knot);
 
 static GObjectClass *parent_class;
@@ -196,6 +198,7 @@ static void sp_knot_init(SPKnot *knot)
     knot->mode = SP_KNOT_MODE_XOR;
     knot->tip = NULL;
     knot->_event_handler_id = 0;
+    knot->pressure = 0;
 
     knot->fill[SP_KNOT_STATE_NORMAL] = 0xffffff00;
     knot->fill[SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
@@ -287,19 +290,19 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
     g_assert(knot != NULL);
     g_assert(SP_IS_KNOT(knot));
 
-    g_object_ref(knot);
-    tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
+    /* Run client universal event handler, if present */
 
     gboolean consumed = FALSE;
 
-    /* Run client universal event handler, if present */
-
     g_signal_emit(knot, knot_signals[EVENT], 0, event, &consumed);
 
     if (consumed) {
         return TRUE;
     }
 
+    g_object_ref(knot);
+    tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
+
     switch (event->type) {
        case GDK_2BUTTON_PRESS:
             if (event->button.button == 1) {
@@ -319,6 +322,7 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
             break;
        case GDK_BUTTON_RELEASE:
             if (event->button.button == 1) {
+                knot->pressure = 0;
                 if (transform_escaped) {
                     transform_escaped = false;
                     consumed = TRUE;
@@ -360,6 +364,11 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
                 // motion notify coordinates as given (no snapping back to origin)
                 within_tolerance = false;
 
+                if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &knot->pressure))
+                    knot->pressure = CLAMP (knot->pressure, 0, 1);
+                else
+                    knot->pressure = 0.5;
+
                 if (!moved) {
                     g_signal_emit(knot,
                                   knot_signals[GRABBED], 0,
@@ -369,10 +378,22 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
                                      TRUE);
                 }
                 NR::Point const motion_w(event->motion.x, event->motion.y);
-                NR::Point const motion_dt = knot->desktop->w2d(motion_w);
-                NR::Point p = motion_dt - knot->grabbed_rel_pos;
-                sp_knot_request_position (knot, &p, event->motion.state);
+                NR::Point motion_dt = knot->desktop->w2d(motion_w) - knot->grabbed_rel_pos;
+                
+                // Only snap path nodes, for which the anchor is set to GTK_ANCHOR_CENTER
+                // (according to default value as set in sp_knot_init())
+                // If we have one of the transform handles at hand, then the anchor will be
+                // set to e.g. GTK_ANCHOR_SW (see seltrans-handles.cpp). The transform handles
+                // will be snapped in seltrans.cpp, not here
+                if (knot->anchor == GTK_ANCHOR_CENTER) {
+                    SnapManager const &m = knot->desktop->namedview->snap_manager;
+                    SPItem *curr = knot->desktop->selection->singleItem(); //Is this really the only way to get to the item to which this knot belongs?
+                    motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, curr).getPoint();
+                }
+            
+                sp_knot_request_position (knot, &motion_dt, event->motion.state);
                 knot->desktop->scroll_to_point (&motion_dt);
+                knot->desktop->set_coordinate_status(knot->pos); // display the coordinate of knot, not cursor - they may be different!
                 moved = TRUE;
             }
             break;
@@ -415,7 +436,7 @@ static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
                         g_signal_emit(knot,
                                       knot_signals[UNGRABBED], 0,
                                       event->button.state);
-                        sp_document_undo(SP_DT_DOCUMENT(knot->desktop));
+                        sp_document_undo(sp_desktop_document(knot->desktop));
                         knot->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Node or handle drag canceled."));
                         transform_escaped = true;
                         consumed = TRUE;
@@ -452,7 +473,7 @@ SPKnot *sp_knot_new(SPDesktop *desktop, const gchar *tip)
         knot->tip = g_strdup (tip);
     }
 
-    knot->item = sp_canvas_item_new(SP_DT_CONTROLS (desktop),
+    knot->item = sp_canvas_item_new(sp_desktop_controls (desktop),
                                     SP_TYPE_CTRL,
                                     "anchor", GTK_ANCHOR_CENTER,
                                     "size", 8.0,
@@ -582,7 +603,7 @@ NR::Point sp_knot_position(SPKnot const *knot)
 /**
  * Set flag in knot, with side effects.
  */
-static void sp_knot_set_flag(SPKnot *knot, guint flag, bool set)
+void sp_knot_set_flag(SPKnot *knot, guint flag, bool set)
 {
     g_assert(knot != NULL);
     g_assert(SP_IS_KNOT(knot));