Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / event-context.cpp
1 /** \file
2  * Main event handling, and related helper functions.
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Frank Felfe <innerspace@iname.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Jon A. Cruz <jon@joncruz.org>
9  *
10  * Copyright (C) 1999-2010 authors
11  * Copyright (C) 2001-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 /** \class SPEventContext
17  * SPEventContext is an abstract base class of all tools. As the name
18  * indicates, event context implementations process UI events (mouse
19  * movements and keypresses) and take actions (like creating or modifying
20  * objects).  There is one event context implementation for each tool,
21  * plus few abstract base classes. Writing a new tool involves
22  * subclassing SPEventContext.
23  */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "event-context.h"
31 #include <string.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkmain.h>
34 #include <gtk/gtkmenu.h>
35 #include <glibmm/i18n.h>
36 #include <cstring>
37 #include <string>
39 #include "display/sp-canvas.h"
40 #include "xml/node-event-vector.h"
41 #include "sp-cursor.h"
42 #include "shortcuts.h"
43 #include "desktop.h"
44 #include "desktop-handles.h"
45 #include "desktop-events.h"
46 #include "desktop-style.h"
47 #include "widgets/desktop-widget.h"
48 #include "sp-namedview.h"
49 #include "selection.h"
50 #include "file.h"
51 #include "interface.h"
52 #include "macros.h"
53 #include "tools-switch.h"
54 #include "preferences.h"
55 #include "message-context.h"
56 #include "gradient-drag.h"
57 #include "object-edit.h"
58 #include "attributes.h"
59 #include "rubberband.h"
60 #include "selcue.h"
61 #include "lpe-tool-context.h"
62 #include "ui/tool/control-point.h"
63 #include "shape-editor.h"
64 #include "sp-guide.h"
65 #include "color.h"
67 static void sp_event_context_class_init(SPEventContextClass *klass);
68 static void sp_event_context_init(SPEventContext *event_context);
69 static void sp_event_context_dispose(GObject *object);
71 static void sp_event_context_private_setup(SPEventContext *ec);
72 static gint sp_event_context_private_root_handler(
73         SPEventContext *event_context, GdkEvent *event);
74 static gint sp_event_context_private_item_handler(
75         SPEventContext *event_context, SPItem *item, GdkEvent *event);
77 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
79 static GObjectClass *parent_class;
81 // globals for temporary switching to selector by space
82 static bool selector_toggled = FALSE;
83 static int switch_selector_to = 0;
85 // globals for temporary switching to dropper by 'D'
86 static bool dropper_toggled = FALSE;
87 static int switch_dropper_to = 0;
89 static gint xp = 0, yp = 0; // where drag started
90 static gint tolerance = 0;
91 static bool within_tolerance = false;
93 // globals for keeping track of keyboard scroll events in order to accelerate
94 static guint32 scroll_event_time = 0;
95 static gdouble scroll_multiply = 1;
96 static guint scroll_keyval = 0;
98 /**
99  * Registers the SPEventContext class with Glib and returns its type number.
100  */
101 GType sp_event_context_get_type(void) {
102     static GType type = 0;
103     if (!type) {
104         GTypeInfo info = { sizeof(SPEventContextClass), NULL, NULL,
105                 (GClassInitFunc) sp_event_context_class_init, NULL, NULL,
106                 sizeof(SPEventContext), 4,
107                 (GInstanceInitFunc) sp_event_context_init, NULL, /* value_table */
108         };
109         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info,
110                 (GTypeFlags) 0);
111     }
112     return type;
115 /**
116  * Callback to set up the SPEventContext vtable.
117  */
118 static void sp_event_context_class_init(SPEventContextClass *klass) {
119     GObjectClass *object_class;
121     object_class = (GObjectClass *) klass;
123     parent_class = (GObjectClass*) g_type_class_peek_parent(klass);
125     object_class->dispose = sp_event_context_dispose;
127     klass->setup = sp_event_context_private_setup;
128     klass->root_handler = sp_event_context_private_root_handler;
129     klass->item_handler = sp_event_context_private_item_handler;
132 /**
133  * Clears all SPEventContext object members.
134  */
135 static void sp_event_context_init(SPEventContext *event_context) {
136     event_context->desktop = NULL;
137     event_context->cursor = NULL;
138     event_context->_message_context = NULL;
139     event_context->_selcue = NULL;
140     event_context->_grdrag = NULL;
141     event_context->space_panning = false;
142     event_context->shape_editor = NULL;
143     event_context->_delayed_snap_event = NULL;
144     event_context->_dse_callback_in_process = false;
145     event_context->tool_url = NULL;
148 /**
149  * Callback to free and null member variables of SPEventContext object.
150  */
151 static void sp_event_context_dispose(GObject *object) {
152     SPEventContext *ec;
154     ec = SP_EVENT_CONTEXT(object);
156     if (ec->_message_context) {
157         delete ec->_message_context;
158     }
160     if (ec->cursor != NULL) {
161         gdk_cursor_unref(ec->cursor);
162         ec->cursor = NULL;
163     }
165     if (ec->desktop) {
166         ec->desktop = NULL;
167     }
169     if (ec->pref_observer) {
170         delete ec->pref_observer;
171     }
173     if (ec->_delayed_snap_event) {
174         delete ec->_delayed_snap_event;
175     }
177     G_OBJECT_CLASS(parent_class)->dispose(object);
180 /**
181  * Recreates and draws cursor on desktop related to SPEventContext.
182  */
183 void sp_event_context_update_cursor(SPEventContext *ec) {
184     GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
185     if (w->window) {
186         /* fixme: */
187         if (ec->cursor_shape) {
188             GdkDisplay *display = gdk_display_get_default();
189             if (ec->tool_url && gdk_display_supports_cursor_alpha(display) && gdk_display_supports_cursor_color(display)) {
190                 bool fillHasColor=false, strokeHasColor=false;
191                 guint32 fillColor = sp_desktop_get_color_tool(ec->desktop, ec->tool_url, true, &fillHasColor);
192                 guint32 strokeColor = sp_desktop_get_color_tool(ec->desktop, ec->tool_url, false, &strokeHasColor);
193                 double fillOpacity = fillHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, true) : 0;
194                 double strokeOpacity = strokeHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, false) : 0;
195                 GdkPixbuf *pixbuf = sp_cursor_pixbuf_from_xpm(
196                     ec->cursor_shape,
197                     w->style->black, w->style->white,
198                     SP_RGBA32_U_COMPOSE(SP_RGBA32_R_U(fillColor),SP_RGBA32_G_U(fillColor),SP_RGBA32_B_U(fillColor),SP_COLOR_F_TO_U(fillOpacity)),
199                     SP_RGBA32_U_COMPOSE(SP_RGBA32_R_U(strokeColor),SP_RGBA32_G_U(strokeColor),SP_RGBA32_B_U(strokeColor),SP_COLOR_F_TO_U(strokeOpacity))
200                     );
201                 if (pixbuf != NULL) {
202                     if (ec->cursor)
203                         gdk_cursor_unref(ec->cursor);
204                     ec->cursor = gdk_cursor_new_from_pixbuf(display, pixbuf, ec->hot_x, ec->hot_y);
205                     g_object_unref(pixbuf);
206                 }
207             } else {
208                 GdkBitmap *bitmap = NULL;
209                 GdkBitmap *mask = NULL;
210                 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
211                 if ((bitmap != NULL) && (mask != NULL)) {
212                     if (ec->cursor)
213                         gdk_cursor_unref(ec->cursor);
214                     ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
215                             &w->style->black, &w->style->white, ec->hot_x,
216                             ec->hot_y);
217                     g_object_unref(bitmap);
218                     g_object_unref(mask);
219                 }
220             }
221         }
222         gdk_window_set_cursor(w->window, ec->cursor);
223         gdk_flush();
224     }
225     ec->desktop->waiting_cursor = false;
228 /**
229  * Callback that gets called on initialization of SPEventContext object.
230  * Redraws mouse cursor, at the moment.
231  */
232 static void sp_event_context_private_setup(SPEventContext *ec) {
233     sp_event_context_update_cursor(ec);
236 /**
237  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
238  */
239 gint gobble_key_events(guint keyval, gint mask) {
240     GdkEvent *event_next;
241     gint i = 0;
243     event_next = gdk_event_get();
244     // while the next event is also a key notify with the same keyval and mask,
245     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type
246             == GDK_KEY_RELEASE) && event_next->key.keyval == keyval && (!mask
247             || (event_next->key.state & mask))) {
248         if (event_next->type == GDK_KEY_PRESS)
249             i++;
250         // kill it
251         gdk_event_free(event_next);
252         // get next
253         event_next = gdk_event_get();
254     }
255     // otherwise, put it back onto the queue
256     if (event_next)
257         gdk_event_put(event_next);
259     return i;
262 /**
263  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
264  */
265 gint gobble_motion_events(gint mask) {
266     GdkEvent *event_next;
267     gint i = 0;
269     event_next = gdk_event_get();
270     // while the next event is also a key notify with the same keyval and mask,
271     while (event_next && event_next->type == GDK_MOTION_NOTIFY
272             && (event_next->motion.state & mask)) {
273         // kill it
274         gdk_event_free(event_next);
275         // get next
276         event_next = gdk_event_get();
277         i++;
278     }
279     // otherwise, put it back onto the queue
280     if (event_next)
281         gdk_event_put(event_next);
283     return i;
286 /**
287  * Toggles current tool between active tool and selector tool.
288  * Subroutine of sp_event_context_private_root_handler().
289  */
290 static void sp_toggle_selector(SPDesktop *dt) {
291     if (!dt->event_context)
292         return;
294     if (tools_isactive(dt, TOOLS_SELECT)) {
295         if (selector_toggled) {
296             if (switch_selector_to)
297                 tools_switch(dt, switch_selector_to);
298             selector_toggled = FALSE;
299         } else
300             return;
301     } else {
302         selector_toggled = TRUE;
303         switch_selector_to = tools_active(dt);
304         tools_switch(dt, TOOLS_SELECT);
305     }
308 /**
309  * Toggles current tool between active tool and dropper tool.
310  * Subroutine of sp_event_context_private_root_handler().
311  */
312 static void sp_toggle_dropper(SPDesktop *dt) {
313     if (!dt->event_context)
314         return;
316     if (tools_isactive(dt, TOOLS_DROPPER)) {
317         if (dropper_toggled) {
318             if (switch_dropper_to)
319                 tools_switch(dt, switch_dropper_to);
320             dropper_toggled = FALSE;
321         } else
322             return;
323     } else {
324         dropper_toggled = TRUE;
325         switch_dropper_to = tools_active(dt);
326         tools_switch(dt, TOOLS_DROPPER);
327     }
330 /**
331  * Calculates and keeps track of scroll acceleration.
332  * Subroutine of sp_event_context_private_root_handler().
333  */
334 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration,
335         SPCanvas */*canvas*/) {
336     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
338     /* key pressed within 500ms ? (1/2 second) */
339     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
340         scroll_multiply = 1; // abort acceleration
341     } else {
342         scroll_multiply += acceleration; // continue acceleration
343     }
345     scroll_event_time = ((GdkEventKey *) event)->time;
346     scroll_keyval = event->key.keyval;
348     return scroll_multiply;
351 /**
352  * Main event dispatch, gets called from Gdk.
353  */
354 static gint sp_event_context_private_root_handler(
355         SPEventContext *event_context, GdkEvent *event) {
356     static Geom::Point button_w;
357     static unsigned int panning = 0;
358     static unsigned int zoom_rb = 0;
360     SPDesktop *desktop = event_context->desktop;
361     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
363     /// @todo REmove redundant /value in preference keys
364     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
366     gint ret = FALSE;
368     switch (event->type) {
369     case GDK_2BUTTON_PRESS:
370         if (panning) {
371             panning = 0;
372             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
373                     event->button.time);
374             ret = TRUE;
375         } else {
376             /* sp_desktop_dialog(); */
377         }
378         break;
379     case GDK_BUTTON_PRESS:
381         // save drag origin
382         xp = (gint) event->button.x;
383         yp = (gint) event->button.y;
384         within_tolerance = true;
386         button_w = Geom::Point(event->button.x, event->button.y);
388         switch (event->button.button) {
389         case 1:
390             if (event_context->space_panning) {
391                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
392                 sp_event_context_discard_delayed_snap_event(event_context);
393                 panning = 1;
394                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
395                         GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK
396                                 | GDK_POINTER_MOTION_MASK
397                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
398                         event->button.time - 1);
399                 ret = TRUE;
400             }
401             break;
402         case 2:
403             if (event->button.state & GDK_SHIFT_MASK) {
404                 zoom_rb = 2;
405             } else {
406                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
407                 sp_event_context_discard_delayed_snap_event(event_context);
408                 panning = 2;
409                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
410                         GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK
411                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
412                         event->button.time - 1);
413             }
414             ret = TRUE;
415             break;
416         case 3:
417             if (event->button.state & GDK_SHIFT_MASK || event->button.state
418                     & GDK_CONTROL_MASK) {
419                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
420                 sp_event_context_discard_delayed_snap_event(event_context);
421                 panning = 3;
422                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
423                         GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK
424                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
425                         event->button.time);
426                 ret = TRUE;
427             } else {
428                 sp_event_root_menu_popup(desktop, NULL, event);
429             }
430             break;
431         default:
432             break;
433         }
434         break;
435     case GDK_MOTION_NOTIFY:
436         if (panning) {
437             if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
438                     || (panning == 1 && !(event->motion.state
439                             & GDK_BUTTON1_MASK)) || (panning == 3
440                     && !(event->motion.state & GDK_BUTTON3_MASK))) {
441                 /* Gdk seems to lose button release for us sometimes :-( */
442                 panning = 0;
443                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
444                         event->button.time);
445                 ret = TRUE;
446             } else {
447                 if (within_tolerance && (abs((gint) event->motion.x - xp)
448                         < tolerance) && (abs((gint) event->motion.y - yp)
449                         < tolerance)) {
450                     // do not drag if we're within tolerance from origin
451                     break;
452                 }
453                 // Once the user has moved farther than tolerance from
454                 // the original location (indicating they intend to move
455                 // the object, not click), then always process the motion
456                 // notify coordinates as given (no snapping back to origin)
457                 within_tolerance = false;
459                 // gobble subsequent motion events to prevent "sticking"
460                 // when scrolling is slow
461                 gobble_motion_events(panning == 2 ? GDK_BUTTON2_MASK : (panning
462                         == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
464                 Geom::Point const motion_w(event->motion.x, event->motion.y);
465                 Geom::Point const moved_w(motion_w - button_w);
466                 event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
467                 ret = TRUE;
468             }
469         } else if (zoom_rb) {
470             Geom::Point const motion_w(event->motion.x, event->motion.y);
471             Geom::Point const motion_dt(desktop->w2d(motion_w));
473             if (within_tolerance && (abs((gint) event->motion.x - xp)
474                     < tolerance) && (abs((gint) event->motion.y - yp)
475                     < tolerance)) {
476                 break; // do not drag if we're within tolerance from origin
477             }
478             // Once the user has moved farther than tolerance from the original location
479             // (indicating they intend to move the object, not click), then always process the
480             // motion notify coordinates as given (no snapping back to origin)
481             within_tolerance = false;
483             if (Inkscape::Rubberband::get(desktop)->is_started()) {
484                 Inkscape::Rubberband::get(desktop)->move(motion_dt);
485             } else {
486                 Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
487             }
488             if (zoom_rb == 2)
489                 gobble_motion_events(GDK_BUTTON2_MASK);
490         }
491         break;
492     case GDK_BUTTON_RELEASE:
493         xp = yp = 0;
494         if (within_tolerance && (panning || zoom_rb)) {
495             zoom_rb = 0;
496             if (panning) {
497                 panning = 0;
498                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
499                         event->button.time);
500             }
501             Geom::Point const event_w(event->button.x, event->button.y);
502             Geom::Point const event_dt(desktop->w2d(event_w));
503             double const zoom_inc = prefs->getDoubleLimited(
504                     "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
505             desktop->zoom_relative_keep_point(event_dt, (event->button.state
506                     & GDK_SHIFT_MASK) ? 1 / zoom_inc : zoom_inc);
507             desktop->updateNow();
508             ret = TRUE;
509         } else if (panning == event->button.button) {
510             panning = 0;
511             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
512                     event->button.time);
514             // in slow complex drawings, some of the motion events are lost;
515             // to make up for this, we scroll it once again to the button-up event coordinates
516             // (i.e. canvas will always get scrolled all the way to the mouse release point,
517             // even if few intermediate steps were visible)
518             Geom::Point const motion_w(event->button.x, event->button.y);
519             Geom::Point const moved_w(motion_w - button_w);
520             event_context->desktop->scroll_world(moved_w);
521             desktop->updateNow();
522             ret = TRUE;
523         } else if (zoom_rb == event->button.button) {
524             zoom_rb = 0;
525             Geom::OptRect const b =
526                     Inkscape::Rubberband::get(desktop)->getRectangle();
527             Inkscape::Rubberband::get(desktop)->stop();
528             if (b && !within_tolerance) {
529                 desktop->set_display_area(*b, 10);
530             }
531             ret = TRUE;
532         }
533         break;
534     case GDK_KEY_PRESS: {
535         double const acceleration = prefs->getDoubleLimited(
536                 "/options/scrollingacceleration/value", 0, 0, 6);
537         int const key_scroll = prefs->getIntLimited("/options/keyscroll/value",
538                 10, 0, 1000);
540         switch (get_group0_keyval(&event->key)) {
541         // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
542         // in the editing window). So we resteal them back and run our regular shortcut
543         // invoker on them.
544         unsigned int shortcut;
545     case GDK_Tab:
546     case GDK_ISO_Left_Tab:
547     case GDK_F1:
548         shortcut = get_group0_keyval(&event->key);
549         if (event->key.state & GDK_SHIFT_MASK)
550             shortcut |= SP_SHORTCUT_SHIFT_MASK;
551         if (event->key.state & GDK_CONTROL_MASK)
552             shortcut |= SP_SHORTCUT_CONTROL_MASK;
553         if (event->key.state & GDK_MOD1_MASK)
554             shortcut |= SP_SHORTCUT_ALT_MASK;
555         ret = sp_shortcut_invoke(shortcut, desktop);
556         break;
558     case GDK_D:
559     case GDK_d:
560         if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
561             sp_toggle_dropper(desktop);
562             ret = TRUE;
563         }
564         break;
565     case GDK_Q:
566     case GDK_q:
567         if (desktop->quick_zoomed()) {
568             ret = TRUE;
569         }
570         if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
571             desktop->zoom_quick(true);
572             ret = TRUE;
573         }
574         break;
575     case GDK_W:
576     case GDK_w:
577     case GDK_F4:
578         /* Close view */
579         if (MOD__CTRL_ONLY) {
580             sp_ui_close_view(NULL);
581             ret = TRUE;
582         }
583         break;
584     case GDK_Left: // Ctrl Left
585     case GDK_KP_Left:
586     case GDK_KP_4:
587         if (MOD__CTRL_ONLY) {
588             int i = (int) floor(key_scroll * accelerate_scroll(event,
589                     acceleration, sp_desktop_canvas(desktop)));
590             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
591             event_context->desktop->scroll_world(i, 0);
592             ret = TRUE;
593         }
594         break;
595     case GDK_Up: // Ctrl Up
596     case GDK_KP_Up:
597     case GDK_KP_8:
598         if (MOD__CTRL_ONLY) {
599             int i = (int) floor(key_scroll * accelerate_scroll(event,
600                     acceleration, sp_desktop_canvas(desktop)));
601             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
602             event_context->desktop->scroll_world(0, i);
603             ret = TRUE;
604         }
605         break;
606     case GDK_Right: // Ctrl Right
607     case GDK_KP_Right:
608     case GDK_KP_6:
609         if (MOD__CTRL_ONLY) {
610             int i = (int) floor(key_scroll * accelerate_scroll(event,
611                     acceleration, sp_desktop_canvas(desktop)));
612             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
613             event_context->desktop->scroll_world(-i, 0);
614             ret = TRUE;
615         }
616         break;
617     case GDK_Down: // Ctrl Down
618     case GDK_KP_Down:
619     case GDK_KP_2:
620         if (MOD__CTRL_ONLY) {
621             int i = (int) floor(key_scroll * accelerate_scroll(event,
622                     acceleration, sp_desktop_canvas(desktop)));
623             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
624             event_context->desktop->scroll_world(0, -i);
625             ret = TRUE;
626         }
627         break;
628     case GDK_F10:
629         if (MOD__SHIFT_ONLY) {
630             sp_event_root_menu_popup(desktop, NULL, event);
631             ret = TRUE;
632         }
633         break;
634     case GDK_space:
635         if (prefs->getBool("/options/spacepans/value")) {
636             event_context->space_panning = true;
637             event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE,
638                     _("<b>Space+mouse drag</b> to pan canvas"));
639             ret = TRUE;
640         } else {
641             sp_toggle_selector(desktop);
642             ret = TRUE;
643         }
644         break;
645     case GDK_z:
646     case GDK_Z:
647         if (MOD__ALT_ONLY) {
648             desktop->zoom_grab_focus();
649             ret = TRUE;
650         }
651         break;
652     default:
653         break;
654         }
655     }
656         break;
657     case GDK_KEY_RELEASE:
658         switch (get_group0_keyval(&event->key)) {
659         case GDK_space:
660             if (event_context->space_panning) {
661                 event_context->space_panning = false;
662                 event_context->_message_context->clear();
663                 if (panning == 1) {
664                     panning = 0;
665                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
666                             event->key.time);
667                     desktop->updateNow();
668                 }
669                 ret = TRUE;
670             }
671             break;
672         case GDK_Q:
673         case GDK_q:
674             if (desktop->quick_zoomed()) {
675                 desktop->zoom_quick(false);
676                 ret = TRUE;
677             }
678             break;
679         default:
680             break;
681         }
682         break;
683     case GDK_SCROLL: {
684         bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
685         bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
686         int const wheel_scroll = prefs->getIntLimited(
687                 "/options/wheelscroll/value", 40, 0, 1000);
689         /* shift + wheel, pan left--right */
690         if (event->scroll.state & GDK_SHIFT_MASK) {
691             switch (event->scroll.direction) {
692             case GDK_SCROLL_UP:
693                 desktop->scroll_world(wheel_scroll, 0);
694                 break;
695             case GDK_SCROLL_DOWN:
696                 desktop->scroll_world(-wheel_scroll, 0);
697                 break;
698             default:
699                 break;
700             }
702             /* ctrl + wheel, zoom in--out */
703         } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
704             double rel_zoom;
705             double const zoom_inc = prefs->getDoubleLimited(
706                     "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
707             switch (event->scroll.direction) {
708             case GDK_SCROLL_UP:
709                 rel_zoom = zoom_inc;
710                 break;
711             case GDK_SCROLL_DOWN:
712                 rel_zoom = 1 / zoom_inc;
713                 break;
714             default:
715                 rel_zoom = 0.0;
716                 break;
717             }
718             if (rel_zoom != 0.0) {
719                 Geom::Point const scroll_dt = desktop->point();
720                 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
721             }
723             /* no modifier, pan up--down (left--right on multiwheel mice?) */
724         } else {
725             switch (event->scroll.direction) {
726             case GDK_SCROLL_UP:
727                 desktop->scroll_world(0, wheel_scroll);
728                 break;
729             case GDK_SCROLL_DOWN:
730                 desktop->scroll_world(0, -wheel_scroll);
731                 break;
732             case GDK_SCROLL_LEFT:
733                 desktop->scroll_world(wheel_scroll, 0);
734                 break;
735             case GDK_SCROLL_RIGHT:
736                 desktop->scroll_world(-wheel_scroll, 0);
737                 break;
738             }
739         }
740         break;
741     }
742     default:
743         break;
744     }
746     return ret;
749 /**
750  * Handles item specific events. Gets called from Gdk.
751  *
752  * Only reacts to right mouse button at the moment.
753  * \todo Fixme: do context sensitive popup menu on items.
754  */
755 gint sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item,
756         GdkEvent *event) {
757     int ret = FALSE;
759     switch (event->type) {
760     case GDK_BUTTON_PRESS:
761         if ((event->button.button == 3) && !(event->button.state
762                 & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
763             sp_event_root_menu_popup(ec->desktop, item, event);
764             ret = TRUE;
765         }
766         break;
767     default:
768         break;
769     }
771     return ret;
774 /**
775  * @brief: Returns true if we're hovering above a knot (needed because we don't want to pre-snap in that case)
776  */
778 bool sp_event_context_knot_mouseover(SPEventContext *ec)
780     if (ec->shape_editor) {
781         return ec->shape_editor->knot_mouseover();
782     }
784     return false;
787 /**
788  * @brief An observer that relays pref changes to the derived classes
789  */
790 class ToolPrefObserver: public Inkscape::Preferences::Observer {
791 public:
792     ToolPrefObserver(Glib::ustring const &path, SPEventContext *ec) :
793         Inkscape::Preferences::Observer(path), _ec(ec) {
794     }
795     virtual void notify(Inkscape::Preferences::Entry const &val) {
796         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set) {
797             ((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set(_ec,
798                     const_cast<Inkscape::Preferences::Entry*> (&val));
799         }
800     }
801 private:
802     SPEventContext * const _ec;
803 };
805 /**
806  * Creates new SPEventContext object and calls its virtual setup() function.
807  * @todo This is bogus. pref_path should be a private property of the inheriting objects.
808  */
809 SPEventContext *
810 sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path,
811         unsigned int key) {
812     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
813     g_return_val_if_fail(desktop != NULL, NULL);
815     SPEventContext * const ec = (SPEventContext*) g_object_new(type, NULL);
817     ec->desktop = desktop;
818     ec->_message_context
819             = new Inkscape::MessageContext(desktop->messageStack());
820     ec->key = key;
821     ec->pref_observer = NULL;
823     if (pref_path) {
824         ec->pref_observer = new ToolPrefObserver(pref_path, ec);
826         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
827         prefs->addObserver(*(ec->pref_observer));
828     }
830     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
831         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
833     return ec;
836 /**
837  * Finishes SPEventContext.
838  */
839 void sp_event_context_finish(SPEventContext *ec) {
840     g_return_if_fail(ec != NULL);
841     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
843     ec->enableSelectionCue(false);
845     if (ec->next) {
846         g_warning("Finishing event context with active link\n");
847     }
849     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
850         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
853 //-------------------------------member functions
855 /**
856  * Enables/disables the SPEventContext's SelCue.
857  */
858 void SPEventContext::enableSelectionCue(bool enable) {
859     if (enable) {
860         if (!_selcue) {
861             _selcue = new Inkscape::SelCue(desktop);
862         }
863     } else {
864         delete _selcue;
865         _selcue = NULL;
866     }
869 /**
870  * Enables/disables the SPEventContext's GrDrag.
871  */
872 void SPEventContext::enableGrDrag(bool enable) {
873     if (enable) {
874         if (!_grdrag) {
875             _grdrag = new GrDrag(desktop);
876         }
877     } else {
878         if (_grdrag) {
879             delete _grdrag;
880             _grdrag = NULL;
881         }
882     }
885 /**
886  * Calls virtual set() function of SPEventContext.
887  */
888 void sp_event_context_read(SPEventContext *ec, gchar const *key) {
889     g_return_if_fail(ec != NULL);
890     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
891     g_return_if_fail(key != NULL);
893     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
894         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
895         Inkscape::Preferences::Entry val = prefs->getEntry(
896                 ec->pref_observer->observed_path + '/' + key);
897         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, &val);
898     }
901 /**
902  * Calls virtual activate() function of SPEventContext.
903  */
904 void sp_event_context_activate(SPEventContext *ec) {
905     g_return_if_fail(ec != NULL);
906     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
908     // Make sure no delayed snapping events are carried over after switching contexts
909     // (this is only an additional safety measure against sloppy coding, because each
910     // context should take care of this by itself.
911     sp_event_context_discard_delayed_snap_event(ec);
913     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
914         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
917 /**
918  * Calls virtual deactivate() function of SPEventContext.
919  */
920 void sp_event_context_deactivate(SPEventContext *ec) {
921     g_return_if_fail(ec != NULL);
922     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
924     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
925         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
928 /**
929  * Calls virtual root_handler(), the main event handling function.
930  */
931 gint sp_event_context_root_handler(SPEventContext * event_context,
932         GdkEvent * event)
934     switch (event->type) {
935     case GDK_MOTION_NOTIFY:
936         sp_event_context_snap_delay_handler(event_context, NULL, NULL,
937                 (GdkEventMotion *) event,
938                 DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER);
939         break;
940     case GDK_BUTTON_RELEASE:
941         if (event_context->_delayed_snap_event) {
942             // If we have any pending snapping action, then invoke it now
943             sp_event_context_snap_watchdog_callback(
944                     event_context->_delayed_snap_event);
945         }
946         break;
947     case GDK_BUTTON_PRESS:
948     case GDK_2BUTTON_PRESS:
949     case GDK_3BUTTON_PRESS:
950         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
951         // drawing a new shape we really should snap though.
952         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(
953                 false);
954         break;
955     default:
956         break;
957     }
959     return sp_event_context_virtual_root_handler(event_context, event);
962 gint sp_event_context_virtual_root_handler(SPEventContext * event_context, GdkEvent * event) {
963     gint ret = false;
964     if (event_context) {    // If no event-context is available then do nothing, otherwise Inkscape would crash
965                             // (see the comment in SPDesktop::set_event_context, and bug LP #622350)
966         ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
967         set_event_location(event_context->desktop, event);
968     }
969     return ret;
972 /**
973  * Calls virtual item_handler(), the item event handling function.
974  */
975 gint sp_event_context_item_handler(SPEventContext * event_context,
976         SPItem * item, GdkEvent * event) {
977     switch (event->type) {
978     case GDK_MOTION_NOTIFY:
979         sp_event_context_snap_delay_handler(event_context, (gpointer) item, NULL, (GdkEventMotion *) event, DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER);
980         break;
981     case GDK_BUTTON_RELEASE:
982         if (event_context->_delayed_snap_event) {
983             // If we have any pending snapping action, then invoke it now
984             sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
985         }
986         break;
987     case GDK_BUTTON_PRESS:
988     case GDK_2BUTTON_PRESS:
989     case GDK_3BUTTON_PRESS:
990         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
991         // drawing a new shape we really should snap though.
992         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
993         break;
994     default:
995         break;
996     }
998     return sp_event_context_virtual_item_handler(event_context, item, event);
1001 gint sp_event_context_virtual_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event) {
1002     gint ret = false;
1003     if (event_context) {    // If no event-context is available then do nothing, otherwise Inkscape would crash
1004                             // (see the comment in SPDesktop::set_event_context, and bug LP #622350)
1005         ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
1006         if (!ret) {
1007             ret = sp_event_context_virtual_root_handler(event_context, event);
1008         } else {
1009             set_event_location(event_context->desktop, event);
1010         }
1011     }
1013     return ret;
1016 /**
1017  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
1018  */
1019 static void set_event_location(SPDesktop *desktop, GdkEvent *event) {
1020     if (event->type != GDK_MOTION_NOTIFY) {
1021         return;
1022     }
1024     Geom::Point const button_w(event->button.x, event->button.y);
1025     Geom::Point const button_dt(desktop->w2d(button_w));
1026     desktop->setPosition(button_dt);
1027     desktop->set_coordinate_status(button_dt);
1030 //-------------------------------------------------------------------
1031 /**
1032  * Create popup menu and tell Gtk to show it.
1033  */
1034 void sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event) {
1035     GtkWidget *menu;
1037     /* fixme: This is not what I want but works for now (Lauris) */
1038     if (event->type == GDK_KEY_PRESS) {
1039         item = sp_desktop_selection(desktop)->singleItem();
1040     }
1041     menu = sp_ui_context_menu(desktop, item);
1042     gtk_widget_show(menu);
1044     switch (event->type) {
1045     case GDK_BUTTON_PRESS:
1046         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL,
1047                 event->button.button, event->button.time);
1048         break;
1049     case GDK_KEY_PRESS:
1050         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
1051         break;
1052     default:
1053         break;
1054     }
1057 /**
1058  * Show tool context specific modifier tip.
1059  */
1060 void sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
1061         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
1062         gchar const *alt_tip) {
1063     guint keyval = get_group0_keyval(&event->key);
1065     bool ctrl = ctrl_tip && (MOD__CTRL || (keyval == GDK_Control_L) || (keyval
1066             == GDK_Control_R));
1067     bool shift = shift_tip && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval
1068             == GDK_Shift_R));
1069     bool alt = alt_tip && (MOD__ALT || (keyval == GDK_Alt_L) || (keyval
1070             == GDK_Alt_R) || (keyval == GDK_Meta_L) || (keyval == GDK_Meta_R));
1072     gchar *tip = g_strdup_printf("%s%s%s%s%s", (ctrl ? ctrl_tip : ""), (ctrl
1073             && (shift || alt) ? "; " : ""), (shift ? shift_tip : ""), ((ctrl
1074             || shift) && alt ? "; " : ""), (alt ? alt_tip : ""));
1076     if (strlen(tip) > 0) {
1077         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
1078     }
1080     g_free(tip);
1083 /**
1084  * Return the keyval corresponding to the key event in group 0, i.e.,
1085  * in the main (English) layout.
1086  *
1087  * Use this instead of simply event->keyval, so that your keyboard shortcuts
1088  * work regardless of layouts (e.g., in Cyrillic).
1089  */
1090 guint get_group0_keyval(GdkEventKey *event) {
1091     guint keyval = 0;
1092     gdk_keymap_translate_keyboard_state(gdk_keymap_get_for_display(
1093             gdk_display_get_default()), event->hardware_keycode,
1094             (GdkModifierType) event->state, 0 /*event->key.group*/, &keyval,
1095             NULL, NULL, NULL);
1096     return keyval;
1099 /**
1100  * Returns item at point p in desktop.
1101  *
1102  * If state includes alt key mask, cyclically selects under; honors
1103  * into_groups.
1104  */
1105 SPItem *sp_event_context_find_item(SPDesktop *desktop, Geom::Point const &p,
1106                                    bool select_under, bool into_groups)
1108     SPItem *item = 0;
1110     if (select_under) {
1111         SPItem *selected_at_point = desktop->getItemFromListAtPointBottom(
1112                 desktop->selection->itemList(), p);
1113         item = desktop->getItemAtPoint(p, into_groups, selected_at_point);
1114         if (item == NULL) { // we may have reached bottom, flip over to the top
1115             item = desktop->getItemAtPoint(p, into_groups, NULL);
1116         }
1117     } else {
1118         item = desktop->getItemAtPoint(p, into_groups, NULL);
1119     }
1121     return item;
1124 /**
1125  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1126  *
1127  * Honors into_groups.
1128  */
1129 SPItem *
1130 sp_event_context_over_item(SPDesktop *desktop, SPItem *item,
1131         Geom::Point const &p) {
1132     GSList *temp = NULL;
1133     temp = g_slist_prepend(temp, item);
1134     SPItem *item_at_point = desktop->getItemFromListAtPointBottom(temp, p);
1135     g_slist_free(temp);
1137     return item_at_point;
1140 ShapeEditor *
1141 sp_event_context_get_shape_editor(SPEventContext *ec) {
1142     return ec->shape_editor;
1145 void event_context_print_event_info(GdkEvent *event, bool print_return) {
1146     switch (event->type) {
1147     case GDK_BUTTON_PRESS:
1148         g_print("GDK_BUTTON_PRESS");
1149         break;
1150     case GDK_2BUTTON_PRESS:
1151         g_print("GDK_2BUTTON_PRESS");
1152         break;
1153     case GDK_3BUTTON_PRESS:
1154         g_print("GDK_3BUTTON_PRESS");
1155         break;
1157     case GDK_MOTION_NOTIFY:
1158         g_print("GDK_MOTION_NOTIFY");
1159         break;
1160     case GDK_ENTER_NOTIFY:
1161         g_print("GDK_ENTER_NOTIFY");
1162         break;
1164     case GDK_LEAVE_NOTIFY:
1165         g_print("GDK_LEAVE_NOTIFY");
1166         break;
1167     case GDK_BUTTON_RELEASE:
1168         g_print("GDK_BUTTON_RELEASE");
1169         break;
1171     case GDK_KEY_PRESS:
1172         g_print("GDK_KEY_PRESS: %d", get_group0_keyval(&event->key));
1173         break;
1174     case GDK_KEY_RELEASE:
1175         g_print("GDK_KEY_RELEASE: %d", get_group0_keyval(&event->key));
1176         break;
1177     default:
1178         //g_print ("even type not recognized");
1179         break;
1180     }
1182     if (print_return) {
1183         g_print("\n");
1184     }
1187 /**
1188  * \brief Analyses the current event, calculates the mouse speed, turns snapping off (temporarily) if the
1189  * mouse speed is above a threshold, and stores the current event such that it can be re-triggered when needed
1190  * (re-triggering is controlled by a watchdog timer)
1191  *
1192  * \param ec Pointer to the event context
1193  * \param dse_item Pointer that store a reference to a canvas or to an item
1194  * \param dse_item2 Another pointer, storing a reference to a knot or controlpoint
1195  * \param event Pointer to the motion event
1196  * \param origin Identifier (enum) specifying where the delay (and the call to this method) were initiated
1197  */
1198 void sp_event_context_snap_delay_handler(SPEventContext *ec,
1199         gpointer const dse_item, gpointer const dse_item2, GdkEventMotion *event,
1200         DelayedSnapEvent::DelayedSnapEventOrigin origin)
1202     static guint32 prev_time;
1203     static boost::optional<Geom::Point> prev_pos;
1205     if (ec->_dse_callback_in_process) {
1206         return;
1207     }
1209     // Snapping occurs when dragging with the left mouse button down, or when hovering e.g. in the pen tool with left mouse button up
1210     bool const c1 = event->state & GDK_BUTTON2_MASK; // We shouldn't hold back any events when other mouse buttons have been
1211     bool const c2 = event->state & GDK_BUTTON3_MASK; // pressed, e.g. when scrolling with the middle mouse button; if we do then
1212     // Inkscape will get stuck in an unresponsive state
1213     bool const c3 = tools_isactive(ec->desktop, TOOLS_CALLIGRAPHIC);
1214     // The snap delay will repeat the last motion event, which will lead to
1215     // erroneous points in the calligraphy context. And because we don't snap
1216     // in this context, we might just as well disable the snap delay all together
1218     if (c1 || c2 || c3) {
1219         // Make sure that we don't send any pending snap events to a context if we know in advance
1220         // that we're not going to snap any way (e.g. while scrolling with middle mouse button)
1221         // Any motion event might affect the state of the context, leading to unexpected behavior
1222         sp_event_context_discard_delayed_snap_event(ec);
1223     } else if (ec->desktop
1224             && ec->desktop->namedview->snap_manager.snapprefs.getSnapEnabledGlobally()) {
1225         // Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
1226         // i.e. snap when we're at stand still. A speed threshold enforces snapping for tablets, which might never
1227         // be fully at stand still and might keep spitting out motion events.
1228         ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
1230         Geom::Point event_pos(event->x, event->y);
1231         guint32 event_t = gdk_event_get_time((GdkEvent *) event);
1233         if (prev_pos) {
1234             Geom::Coord dist = Geom::L2(event_pos - *prev_pos);
1235             guint32 delta_t = event_t - prev_time;
1236             gdouble speed = delta_t > 0 ? dist / delta_t : 1000;
1237             //std::cout << "Mouse speed = " << speed << " px/msec " << std::endl;
1238             if (speed > 0.02) { // Jitter threshold, might be needed for tablets
1239                 // We're moving fast, so postpone any snapping until the next GDK_MOTION_NOTIFY event. We
1240                 // will keep on postponing the snapping as long as the speed is high.
1241                 // We must snap at some point in time though, so set a watchdog timer at some time from
1242                 // now, just in case there's no future motion event that drops under the speed limit (when
1243                 // stopping abruptly)
1244                 delete ec->_delayed_snap_event;
1245                 ec->_delayed_snap_event = new DelayedSnapEvent(ec, dse_item, dse_item2,
1246                         event, origin); // watchdog is reset, i.e. pushed forward in time
1247                 // If the watchdog expires before a new motion event is received, we will snap (as explained
1248                 // above). This means however that when the timer is too short, we will always snap and that the
1249                 // speed threshold is ineffective. In the extreme case the delay is set to zero, and snapping will
1250                 // be immediate, as it used to be in the old days ;-).
1251             } else { // Speed is very low, so we're virtually at stand still
1252                 // But if we're really standing still, then we should snap now. We could use some low-pass filtering,
1253                 // otherwise snapping occurs for each jitter movement. For this filtering we'll leave the watchdog to expire,
1254                 // snap, and set a new watchdog again.
1255                 if (ec->_delayed_snap_event == NULL) { // no watchdog has been set
1256                     // it might have already expired, so we'll set a new one; the snapping frequency will be limited this way
1257                     ec->_delayed_snap_event = new DelayedSnapEvent(ec, dse_item,
1258                             dse_item2, event, origin);
1259                 } // else: watchdog has been set before and we'll wait for it to expire
1260             }
1261         } else {
1262             // This is the first GDK_MOTION_NOTIFY event, so postpone snapping and set the watchdog
1263             g_assert(ec->_delayed_snap_event == NULL);
1264             ec->_delayed_snap_event = new DelayedSnapEvent(ec, dse_item, dse_item2,
1265                     event, origin);
1266         }
1268         prev_pos = event_pos;
1269         prev_time = event_t;
1270     }
1273 /**
1274  * \brief When the snap delay watchdog timer barks, this method will be called and will re-inject the last motion
1275  * event in an appropriate place, with snapping being turned on again
1276  */
1277 gboolean sp_event_context_snap_watchdog_callback(gpointer data) {
1278     // Snap NOW! For this the "postponed" flag will be reset and the last motion event will be repeated
1279     DelayedSnapEvent *dse = reinterpret_cast<DelayedSnapEvent*> (data);
1281     if (dse == NULL) {
1282         // This might occur when this method is called directly, i.e. not through the timer
1283         // E.g. on GDK_BUTTON_RELEASE in sp_event_context_root_handler()
1284         return FALSE;
1285     }
1287     SPEventContext *ec = dse->getEventContext();
1288     if (ec == NULL || ec->desktop == NULL) {
1289         return false;
1290     }
1291     ec->_dse_callback_in_process = true;
1293     SPDesktop *dt = ec->desktop;
1294     dt->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
1296     // Depending on where the delayed snap event originated from, we will inject it back at it's origin
1297     // The switch below takes care of that and prepares the relevant parameters
1298     switch (dse->getOrigin()) {
1299     case DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER:
1300         sp_event_context_virtual_root_handler(ec, dse->getEvent());
1301         break;
1302     case DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER: {
1303         SPItem* item = NULL;
1304         item = SP_ITEM(dse->getItem());
1305         if (item && SP_IS_ITEM(item)) {
1306             sp_event_context_virtual_item_handler(ec, item, dse->getEvent());
1307         }
1308     }
1309         break;
1310     case DelayedSnapEvent::KNOT_HANDLER: {
1311         SPKnot* knot = SP_KNOT(dse->getItem2());
1312         if (knot && SP_IS_KNOT(knot)) {
1313             sp_knot_handler_request_position(dse->getEvent(), knot);
1314         }
1315     }
1316         break;
1317     case DelayedSnapEvent::CONTROL_POINT_HANDLER: {
1318         using Inkscape::UI::ControlPoint;
1319         ControlPoint *point = reinterpret_cast<ControlPoint*> (dse->getItem2());
1320         point->_eventHandler(dse->getEvent());
1321     }
1322         break;
1323     case DelayedSnapEvent::GUIDE_HANDLER: {
1324         gpointer item = dse->getItem();
1325         gpointer item2 = dse->getItem2();
1326         if (item && item2) {
1327             g_assert(SP_IS_CANVAS_ITEM(item));
1328             g_assert(SP_IS_GUIDE(item2));
1329             sp_dt_guide_event(SP_CANVAS_ITEM(item), dse->getEvent(), item2);
1330         }
1331     }
1332         break;
1333     case DelayedSnapEvent::GUIDE_HRULER:
1334     case DelayedSnapEvent::GUIDE_VRULER: {
1335         gpointer item = dse->getItem();
1336         gpointer item2 = dse->getItem2();
1337         if (item && item2) {
1338             g_assert(GTK_IS_WIDGET(item));
1339             g_assert(SP_IS_DESKTOP_WIDGET(item2));
1340             if (dse->getOrigin() == DelayedSnapEvent::GUIDE_HRULER) {
1341                 sp_dt_hruler_event(GTK_WIDGET(item), dse->getEvent(), SP_DESKTOP_WIDGET(item2));
1342             } else {
1343                 sp_dt_vruler_event(GTK_WIDGET(item), dse->getEvent(), SP_DESKTOP_WIDGET(item2));
1344             }
1345         }
1346     }
1347         break;
1348     default:
1349         g_warning("Origin of snap-delay event has not been defined!;");
1350         break;
1351     }
1353     ec->_delayed_snap_event = NULL;
1354     delete dse;
1356     ec->_dse_callback_in_process = false;
1358     return FALSE; //Kills the timer and stops it from executing this callback over and over again.
1361 void sp_event_context_discard_delayed_snap_event(SPEventContext *ec) {
1362     delete ec->_delayed_snap_event;
1363     ec->_delayed_snap_event = NULL;
1364     ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
1367 /*
1368  Local Variables:
1369  mode:c++
1370  c-file-style:"stroustrup"
1371  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1372  indent-tabs-mode:nil
1373  fill-column:99
1374  End:
1375  */
1376 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :