Code

Add missing namespaces in win32 code (thanks to Yann Papouin for pointing this out)
[inkscape.git] / src / event-context.cpp
index 5b845388bce1c8bdd44fd7a226d237fdf5850463..753d0679aea0a84e8e65d1ac41aee1b43de46575 100644 (file)
@@ -173,6 +173,10 @@ sp_event_context_dispose(GObject *object)
         delete ec->pref_observer;
     }
 
+    if (ec->_delayed_snap_event) {
+       delete ec->_delayed_snap_event;
+    }
+
     G_OBJECT_CLASS(parent_class)->dispose(object);
 }
 
@@ -344,14 +348,6 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
     /// @todo REmove redundant /value in preference keys
     tolerance = prefs->getIntLimited(
             "/options/dragtolerance/value", 0, 0, 100);
-    double const zoom_inc = prefs->getDoubleLimited(
-            "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
-    double const acceleration = prefs->getDoubleLimited(
-            "/options/scrollingacceleration/value", 0, 0, 6);
-    int const key_scroll = prefs->getIntLimited(
-            "/options/keyscroll/value", 10, 0, 1000);
-    int const wheel_scroll = prefs->getIntLimited(
-            "/options/wheelscroll/value", 40, 0, 1000);
 
     gint ret = FALSE;
 
@@ -378,7 +374,9 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
             switch (event->button.button) {
                 case 1:
                     if (event_context->space_panning) {
-                        panning = 1;
+                       // When starting panning, make sure there are no snap events pending because these might disable the panning again
+                       sp_event_context_discard_delayed_snap_event(event_context);
+                       panning = 1;
                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                             GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
                             NULL, event->button.time-1);
@@ -389,7 +387,9 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                     if (event->button.state == GDK_SHIFT_MASK) {
                         zoom_rb = 2;
                     } else {
-                        panning = 2;
+                       // When starting panning, make sure there are no snap events pending because these might disable the panning again
+                                               sp_event_context_discard_delayed_snap_event(event_context);
+                                               panning = 2;
                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
                             NULL, event->button.time-1);
@@ -399,7 +399,9 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                 case 3:
                     if (event->button.state & GDK_SHIFT_MASK
                             || event->button.state & GDK_CONTROL_MASK) {
-                        panning = 3;
+                       // When starting panning, make sure there are no snap events pending because these might disable the panning again
+                                               sp_event_context_discard_delayed_snap_event(event_context);
+                       panning = 3;
                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
                                 NULL, event->button.time);
@@ -419,7 +421,7 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
                    ) {
                     /* Gdk seems to lose button release for us sometimes :-( */
-                    panning = 0;
+                       panning = 0;
                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
                             event->button.time);
                     ret = TRUE;
@@ -482,6 +484,7 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                 }
                 Geom::Point const event_w(event->button.x, event->button.y);
                 Geom::Point const event_dt(desktop->w2d(event_w));
+                double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
                 desktop->zoom_relative_keep_point(event_dt,
                           (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
                 desktop->updateNow();
@@ -511,7 +514,11 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
             }
             break;
         case GDK_KEY_PRESS:
-            switch (get_group0_keyval(&event->key)) {
+               {
+               double const acceleration = prefs->getDoubleLimited("/options/scrollingacceleration/value", 0, 0, 6);
+               int const key_scroll = prefs->getIntLimited("/options/keyscroll/value", 10, 0, 1000);
+
+               switch (get_group0_keyval(&event->key)) {
                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
                 // in the editing window). So we resteal them back and run our regular shortcut
                 // invoker on them.
@@ -625,6 +632,7 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                 default:
                     break;
             }
+               }
             break;
         case GDK_KEY_RELEASE:
             switch (get_group0_keyval(&event->key)) {
@@ -656,6 +664,8 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
         {
             bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
             bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
+            int const wheel_scroll = prefs->getIntLimited("/options/wheelscroll/value", 40, 0, 1000);
+
             /* shift + wheel, pan left--right */
             if (event->scroll.state & GDK_SHIFT_MASK) {
                 switch (event->scroll.direction) {
@@ -672,6 +682,7 @@ static gint sp_event_context_private_root_handler(SPEventContext *event_context,
                 /* ctrl + wheel, zoom in--out */
             } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
                 double rel_zoom;
+                double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
                 switch (event->scroll.direction) {
                     case GDK_SCROLL_UP:
                         rel_zoom = zoom_inc;
@@ -869,9 +880,8 @@ sp_event_context_activate(SPEventContext *ec)
 
     // Make sure no delayed snapping events are carried over after switching contexts
     // (this is only an additional safety measure against sloppy coding, because each
-    // context should take care of this by itself. It might be hard to get each and every
-    // context perfect though)
-    sp_event_context_snap_window_closed(ec, false);
+    // context should take care of this by itself.
+    sp_event_context_discard_delayed_snap_event(ec);
 
     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
@@ -896,13 +906,15 @@ sp_event_context_deactivate(SPEventContext *ec)
 gint
 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
 {
-    //std::cout << "sp_event_context_root_handler" << std::endl;
-       switch (event->type) {
+    switch (event->type) {
                case GDK_MOTION_NOTIFY:
                        sp_event_context_snap_delay_handler(event_context, NULL, NULL, (GdkEventMotion *)event, DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER);
                        break;
                case GDK_BUTTON_RELEASE:
-                       sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event); // If we have any pending snapping action, then invoke it now
+                       if (event_context->_delayed_snap_event) {
+                               // If we have any pending snapping action, then invoke it now
+                               sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
+                       }
                        break;
                case GDK_BUTTON_PRESS:
         case GDK_2BUTTON_PRESS:
@@ -921,8 +933,6 @@ sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
 gint
 sp_event_context_virtual_root_handler(SPEventContext * event_context, GdkEvent * event)
 {
-       //std::cout << "sp_event_context_virtual_root_handler -> postponed: " << event_context->desktop->namedview->snap_manager.snapprefs.getSnapPostponedGlobally() << std::endl;
-
        gint ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
        set_event_location(event_context->desktop, event);
        return ret;
@@ -934,13 +944,15 @@ sp_event_context_virtual_root_handler(SPEventContext * event_context, GdkEvent *
 gint
 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
 {
-       //std::cout << "sp_event_context_item_handler" << std::endl;
        switch (event->type) {
                case GDK_MOTION_NOTIFY:
                        sp_event_context_snap_delay_handler(event_context, item, NULL, (GdkEventMotion *)event, DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER);
                        break;
                case GDK_BUTTON_RELEASE:
-                       sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event); // If we have any pending snapping action, then invoke it now
+                       if (event_context->_delayed_snap_event) {
+                               // If we have any pending snapping action, then invoke it now
+                               sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
+                       }
                        break;
                /*case GDK_BUTTON_PRESS:
                case GDK_2BUTTON_PRESS:
@@ -1170,13 +1182,18 @@ void sp_event_context_snap_delay_handler(SPEventContext *ec, SPItem* const item,
     bool const c2 = event->state & GDK_BUTTON3_MASK; // pressed, e.g. when scrolling with the middle mouse button; if we do then
                                                                                                     // Inkscape will get stuck in an unresponsive state
 
-    if (ec->_snap_window_open && !c1 && !c2 && ec->desktop && ec->desktop->namedview->snap_manager.snapprefs.getSnapEnabledGlobally()) {
-       // Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
+    if (c1 || c2) {
+       // Make sure that we don't send any pending snap events to a context if we know in advance
+       // that we're not going to snap any way (e.g. while scrolling with middle mouse button)
+       // Any motion event might affect the state of the context, leading to unexpected behavior
+       sp_event_context_discard_delayed_snap_event(ec);
+    } else if (ec->desktop && ec->desktop->namedview->snap_manager.snapprefs.getSnapEnabledGlobally()) {
+               // Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
                // i.e. snap when we're at stand still. A speed threshold enforces snapping for tablets, which might never
                // be fully at stand still and might keep spitting out motion events.
-       ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
+               ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
 
-       Geom::Point event_pos(event->x, event->y);
+               Geom::Point event_pos(event->x, event->y);
                guint32 event_t = gdk_event_get_time ( (GdkEvent *) event );
 
                if (prev_pos) {
@@ -1213,7 +1230,7 @@ void sp_event_context_snap_delay_handler(SPEventContext *ec, SPItem* const item,
 
                prev_pos = event_pos;
                prev_time = event_t;
-       }
+    }
 }
 
 gboolean sp_event_context_snap_watchdog_callback(gpointer data)
@@ -1240,12 +1257,21 @@ gboolean sp_event_context_snap_watchdog_callback(gpointer data)
                        sp_event_context_virtual_root_handler(ec, dse->getEvent());
                        break;
                case DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER:
-                       g_assert(dse->getItem() != NULL);
-                       sp_event_context_virtual_item_handler(ec, dse->getItem(), dse->getEvent());
+                       {
+                               SPItem* item = NULL;
+                               item = dse->getItem();
+                               if (item && SP_IS_ITEM(item)) {
+                                       sp_event_context_virtual_item_handler(ec, item, dse->getEvent());
+                               }
+                       }
                        break;
                case DelayedSnapEvent::KNOT_HANDLER:
-                       g_assert(dse->getKnot() != NULL);
-                       sp_knot_handler_request_position(dse->getEvent(), dse->getKnot());
+                       {
+                               SPKnot* knot = dse->getKnot();
+                               if (knot && SP_IS_KNOT(knot)) {
+                                       sp_knot_handler_request_position(dse->getEvent(), knot);
+                               }
+                       }
                        break;
                default:
                        g_warning("Origin of snap-delay event has not been defined!;");
@@ -1258,51 +1284,8 @@ gboolean sp_event_context_snap_watchdog_callback(gpointer data)
        return FALSE; //Kills the timer and stops it from executing this callback over and over again.
 }
 
-void sp_event_context_snap_window_open(SPEventContext *ec, bool show_debug_warnings)
+void sp_event_context_discard_delayed_snap_event(SPEventContext *ec)
 {
-       // Only when ec->_snap_window_open has been set, Inkscape will know that snapping is active
-       // and will delay any snapping events (but only when asked to through the preferences)
-
-       // When snapping is being delayed, then that will also mean that at some point the last event
-       // might be re-triggered. This should only occur when Inkscape is still in the same tool or context,
-       // and even more specifically, the tool should even be in the same state. If for example snapping is being delayed while
-       // creating a rectangle, then the rect-context will be active and it will be in the "dragging" state
-       // (see the static boolean variable "dragging" in the sp_rect_context_root_handler). The procedure is
-       // as follows: call sp_event_context_snap_window_open(*, TRUE) when entering the "dragging" state, which will delay
-       // snapping from that moment on, and call sp_event_context_snap_window_open(*, FALSE) when leaving the "dragging"
-       // state. This last call will also make sure that any pending snap events will be canceled.
-
-       //std::cout << "sp_event_context_snap_window_open" << std::endl;
-       if (!ec) {
-               if (show_debug_warnings) {
-                       g_warning("sp_event_context_snap_window_open() has been called without providing an event context!");
-               }
-               return;
-       }
-
-       if (ec->_snap_window_open == true && show_debug_warnings) {
-               g_warning("Snap window was already open! This is a bug, please report it.");
-       }
-
-       ec->_snap_window_open = true;
-}
-
-void sp_event_context_snap_window_closed(SPEventContext *ec, bool show_debug_warnings)
-{
-       //std::cout << "sp_event_context_snap_window_closed" << std::endl;
-       if (!ec) {
-               if (show_debug_warnings) {
-                       g_warning("sp_event_context_snap_window_closed() has been called without providing an event context!");
-               }
-               return;
-       }
-
-       if (ec->_snap_window_open == false && show_debug_warnings) {
-               g_warning("Snap window was already closed! This is a bug, please report it.");
-       }
-
-       ec->_snap_window_open = false;
-
        delete ec->_delayed_snap_event;
        ec->_delayed_snap_event = NULL;
 }