Code

Pot and Dutch translation update
[inkscape.git] / src / event-context.cpp
1 #define __SP_EVENT_CONTEXT_C__
3 /** \file
4  * Main event handling, and related helper functions.
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Frank Felfe <innerspace@iname.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 1999-2010 authors
12  * Copyright (C) 2001-2002 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 /** \class SPEventContext
18  * SPEventContext is an abstract base class of all tools. As the name
19  * indicates, event context implementations process UI events (mouse
20  * movements and keypresses) and take actions (like creating or modifying
21  * objects).  There is one event context implementation for each tool,
22  * plus few abstract base classes. Writing a new tool involves
23  * subclassing SPEventContext.
24  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "event-context.h"
32 #include <string.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkmain.h>
35 #include <gtk/gtkmenu.h>
36 #include <glibmm/i18n.h>
37 #include <cstring>
38 #include <string>
40 #include "display/sp-canvas.h"
41 #include "xml/node-event-vector.h"
42 #include "sp-cursor.h"
43 #include "shortcuts.h"
44 #include "desktop.h"
45 #include "desktop-handles.h"
46 #include "desktop-events.h"
47 #include "desktop-style.h"
48 #include "widgets/desktop-widget.h"
49 #include "sp-namedview.h"
50 #include "selection.h"
51 #include "file.h"
52 #include "interface.h"
53 #include "macros.h"
54 #include "tools-switch.h"
55 #include "preferences.h"
56 #include "message-context.h"
57 #include "gradient-drag.h"
58 #include "object-edit.h"
59 #include "attributes.h"
60 #include "rubberband.h"
61 #include "selcue.h"
62 #include "lpe-tool-context.h"
63 #include "ui/tool/control-point.h"
64 #include "shape-editor.h"
65 #include "sp-guide.h"
66 #include "color.h"
68 static void sp_event_context_class_init(SPEventContextClass *klass);
69 static void sp_event_context_init(SPEventContext *event_context);
70 static void sp_event_context_dispose(GObject *object);
72 static void sp_event_context_private_setup(SPEventContext *ec);
73 static gint sp_event_context_private_root_handler(
74         SPEventContext *event_context, GdkEvent *event);
75 static gint sp_event_context_private_item_handler(
76         SPEventContext *event_context, SPItem *item, GdkEvent *event);
78 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
80 static GObjectClass *parent_class;
82 // globals for temporary switching to selector by space
83 static bool selector_toggled = FALSE;
84 static int switch_selector_to = 0;
86 // globals for temporary switching to dropper by 'D'
87 static bool dropper_toggled = FALSE;
88 static int switch_dropper_to = 0;
90 static gint xp = 0, yp = 0; // where drag started
91 static gint tolerance = 0;
92 static bool within_tolerance = false;
94 // globals for keeping track of keyboard scroll events in order to accelerate
95 static guint32 scroll_event_time = 0;
96 static gdouble scroll_multiply = 1;
97 static guint scroll_keyval = 0;
99 /**
100  * Registers the SPEventContext class with Glib and returns its type number.
101  */
102 GType sp_event_context_get_type(void) {
103     static GType type = 0;
104     if (!type) {
105         GTypeInfo info = { sizeof(SPEventContextClass), NULL, NULL,
106                 (GClassInitFunc) sp_event_context_class_init, NULL, NULL,
107                 sizeof(SPEventContext), 4,
108                 (GInstanceInitFunc) sp_event_context_init, NULL, /* value_table */
109         };
110         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info,
111                 (GTypeFlags) 0);
112     }
113     return type;
116 /**
117  * Callback to set up the SPEventContext vtable.
118  */
119 static void sp_event_context_class_init(SPEventContextClass *klass) {
120     GObjectClass *object_class;
122     object_class = (GObjectClass *) klass;
124     parent_class = (GObjectClass*) g_type_class_peek_parent(klass);
126     object_class->dispose = sp_event_context_dispose;
128     klass->setup = sp_event_context_private_setup;
129     klass->root_handler = sp_event_context_private_root_handler;
130     klass->item_handler = sp_event_context_private_item_handler;
133 /**
134  * Clears all SPEventContext object members.
135  */
136 static void sp_event_context_init(SPEventContext *event_context) {
137     event_context->desktop = NULL;
138     event_context->cursor = NULL;
139     event_context->_message_context = NULL;
140     event_context->_selcue = NULL;
141     event_context->_grdrag = NULL;
142     event_context->space_panning = false;
143     event_context->shape_editor = NULL;
144     event_context->_delayed_snap_event = NULL;
145     event_context->_dse_callback_in_process = false;
146     event_context->tool_url = NULL;
149 /**
150  * Callback to free and null member variables of SPEventContext object.
151  */
152 static void sp_event_context_dispose(GObject *object) {
153     SPEventContext *ec;
155     ec = SP_EVENT_CONTEXT(object);
157     if (ec->_message_context) {
158         delete ec->_message_context;
159     }
161     if (ec->cursor != NULL) {
162         gdk_cursor_unref(ec->cursor);
163         ec->cursor = NULL;
164     }
166     if (ec->desktop) {
167         ec->desktop = NULL;
168     }
170     if (ec->pref_observer) {
171         delete ec->pref_observer;
172     }
174     if (ec->_delayed_snap_event) {
175         delete ec->_delayed_snap_event;
176     }
178     G_OBJECT_CLASS(parent_class)->dispose(object);
181 /**
182  * Recreates and draws cursor on desktop related to SPEventContext.
183  */
184 void sp_event_context_update_cursor(SPEventContext *ec) {
185     GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
186     if (w->window) {
187         /* fixme: */
188         if (ec->cursor_shape) {
189             GdkDisplay *display = gdk_display_get_default();
190             if (ec->tool_url && gdk_display_supports_cursor_alpha(display) && gdk_display_supports_cursor_color(display)) {
191                 bool fillHasColor=false, strokeHasColor=false;
192                 guint32 fillColor = sp_desktop_get_color_tool(ec->desktop, ec->tool_url, true, &fillHasColor);
193                 guint32 strokeColor = sp_desktop_get_color_tool(ec->desktop, ec->tool_url, false, &strokeHasColor);
194                 double fillOpacity = fillHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, true) : 0;
195                 double strokeOpacity = strokeHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, false) : 0;
196                 GdkPixbuf *pixbuf = sp_cursor_pixbuf_from_xpm(
197                     ec->cursor_shape,
198                     w->style->black, w->style->white,
199                     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)),
200                     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))
201                     );
202                 if (pixbuf != NULL) {
203                     if (ec->cursor)
204                         gdk_cursor_unref(ec->cursor);
205                     ec->cursor = gdk_cursor_new_from_pixbuf(display, pixbuf, ec->hot_x, ec->hot_y);
206                     g_object_unref(pixbuf);
207                 }
208             } else {
209                 GdkBitmap *bitmap = NULL;
210                 GdkBitmap *mask = NULL;
211                 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
212                 if ((bitmap != NULL) && (mask != NULL)) {
213                     if (ec->cursor)
214                         gdk_cursor_unref(ec->cursor);
215                     ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
216                             &w->style->black, &w->style->white, ec->hot_x,
217                             ec->hot_y);
218                     g_object_unref(bitmap);
219                     g_object_unref(mask);
220                 }
221             }
222         }
223         gdk_window_set_cursor(w->window, ec->cursor);
224         gdk_flush();
225     }
226     ec->desktop->waiting_cursor = false;
229 /**
230  * Callback that gets called on initialization of SPEventContext object.
231  * Redraws mouse cursor, at the moment.
232  */
233 static void sp_event_context_private_setup(SPEventContext *ec) {
234     sp_event_context_update_cursor(ec);
237 /**
238  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
239  */
240 gint gobble_key_events(guint keyval, gint mask) {
241     GdkEvent *event_next;
242     gint i = 0;
244     event_next = gdk_event_get();
245     // while the next event is also a key notify with the same keyval and mask,
246     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type
247             == GDK_KEY_RELEASE) && event_next->key.keyval == keyval && (!mask
248             || (event_next->key.state & mask))) {
249         if (event_next->type == GDK_KEY_PRESS)
250             i++;
251         // kill it
252         gdk_event_free(event_next);
253         // get next
254         event_next = gdk_event_get();
255     }
256     // otherwise, put it back onto the queue
257     if (event_next)
258         gdk_event_put(event_next);
260     return i;
263 /**
264  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
265  */
266 gint gobble_motion_events(gint mask) {
267     GdkEvent *event_next;
268     gint i = 0;
270     event_next = gdk_event_get();
271     // while the next event is also a key notify with the same keyval and mask,
272     while (event_next && event_next->type == GDK_MOTION_NOTIFY
273             && (event_next->motion.state & mask)) {
274         // kill it
275         gdk_event_free(event_next);
276         // get next
277         event_next = gdk_event_get();
278         i++;
279     }
280     // otherwise, put it back onto the queue
281     if (event_next)
282         gdk_event_put(event_next);
284     return i;
287 /**
288  * Toggles current tool between active tool and selector tool.
289  * Subroutine of sp_event_context_private_root_handler().
290  */
291 static void sp_toggle_selector(SPDesktop *dt) {
292     if (!dt->event_context)
293         return;
295     if (tools_isactive(dt, TOOLS_SELECT)) {
296         if (selector_toggled) {
297             if (switch_selector_to)
298                 tools_switch(dt, switch_selector_to);
299             selector_toggled = FALSE;
300         } else
301             return;
302     } else {
303         selector_toggled = TRUE;
304         switch_selector_to = tools_active(dt);
305         tools_switch(dt, TOOLS_SELECT);
306     }
309 /**
310  * Toggles current tool between active tool and dropper tool.
311  * Subroutine of sp_event_context_private_root_handler().
312  */
313 static void sp_toggle_dropper(SPDesktop *dt) {
314     if (!dt->event_context)
315         return;
317     if (tools_isactive(dt, TOOLS_DROPPER)) {
318         if (dropper_toggled) {
319             if (switch_dropper_to)
320                 tools_switch(dt, switch_dropper_to);
321             dropper_toggled = FALSE;
322         } else
323             return;
324     } else {
325         dropper_toggled = TRUE;
326         switch_dropper_to = tools_active(dt);
327         tools_switch(dt, TOOLS_DROPPER);
328     }
331 /**
332  * Calculates and keeps track of scroll acceleration.
333  * Subroutine of sp_event_context_private_root_handler().
334  */
335 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration,
336         SPCanvas */*canvas*/) {
337     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
339     /* key pressed within 500ms ? (1/2 second) */
340     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
341         scroll_multiply = 1; // abort acceleration
342     } else {
343         scroll_multiply += acceleration; // continue acceleration
344     }
346     scroll_event_time = ((GdkEventKey *) event)->time;
347     scroll_keyval = event->key.keyval;
349     return scroll_multiply;
352 /**
353  * Main event dispatch, gets called from Gdk.
354  */
355 static gint sp_event_context_private_root_handler(
356         SPEventContext *event_context, GdkEvent *event) {
357     static Geom::Point button_w;
358     static unsigned int panning = 0;
359     static unsigned int zoom_rb = 0;
361     SPDesktop *desktop = event_context->desktop;
362     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
364     /// @todo REmove redundant /value in preference keys
365     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
367     gint ret = FALSE;
369     switch (event->type) {
370     case GDK_2BUTTON_PRESS:
371         if (panning) {
372             panning = 0;
373             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
374                     event->button.time);
375             ret = TRUE;
376         } else {
377             /* sp_desktop_dialog(); */
378         }
379         break;
380     case GDK_BUTTON_PRESS:
382         // save drag origin
383         xp = (gint) event->button.x;
384         yp = (gint) event->button.y;
385         within_tolerance = true;
387         button_w = Geom::Point(event->button.x, event->button.y);
389         switch (event->button.button) {
390         case 1:
391             if (event_context->space_panning) {
392                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
393                 sp_event_context_discard_delayed_snap_event(event_context);
394                 panning = 1;
395                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
396                         GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK
397                                 | GDK_POINTER_MOTION_MASK
398                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
399                         event->button.time - 1);
400                 ret = TRUE;
401             }
402             break;
403         case 2:
404             if (event->button.state & GDK_SHIFT_MASK) {
405                 zoom_rb = 2;
406             } else {
407                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
408                 sp_event_context_discard_delayed_snap_event(event_context);
409                 panning = 2;
410                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
411                         GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK
412                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
413                         event->button.time - 1);
414             }
415             ret = TRUE;
416             break;
417         case 3:
418             if (event->button.state & GDK_SHIFT_MASK || event->button.state
419                     & GDK_CONTROL_MASK) {
420                 // When starting panning, make sure there are no snap events pending because these might disable the panning again
421                 sp_event_context_discard_delayed_snap_event(event_context);
422                 panning = 3;
423                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
424                         GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK
425                                 | GDK_POINTER_MOTION_HINT_MASK, NULL,
426                         event->button.time);
427                 ret = TRUE;
428             } else {
429                 sp_event_root_menu_popup(desktop, NULL, event);
430             }
431             break;
432         default:
433             break;
434         }
435         break;
436     case GDK_MOTION_NOTIFY:
437         if (panning) {
438             if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
439                     || (panning == 1 && !(event->motion.state
440                             & GDK_BUTTON1_MASK)) || (panning == 3
441                     && !(event->motion.state & GDK_BUTTON3_MASK))) {
442                 /* Gdk seems to lose button release for us sometimes :-( */
443                 panning = 0;
444                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
445                         event->button.time);
446                 ret = TRUE;
447             } else {
448                 if (within_tolerance && (abs((gint) event->motion.x - xp)
449                         < tolerance) && (abs((gint) event->motion.y - yp)
450                         < tolerance)) {
451                     // do not drag if we're within tolerance from origin
452                     break;
453                 }
454                 // Once the user has moved farther than tolerance from
455                 // the original location (indicating they intend to move
456                 // the object, not click), then always process the motion
457                 // notify coordinates as given (no snapping back to origin)
458                 within_tolerance = false;
460                 // gobble subsequent motion events to prevent "sticking"
461                 // when scrolling is slow
462                 gobble_motion_events(panning == 2 ? GDK_BUTTON2_MASK : (panning
463                         == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
465                 Geom::Point const motion_w(event->motion.x, event->motion.y);
466                 Geom::Point const moved_w(motion_w - button_w);
467                 event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
468                 ret = TRUE;
469             }
470         } else if (zoom_rb) {
471             Geom::Point const motion_w(event->motion.x, event->motion.y);
472             Geom::Point const motion_dt(desktop->w2d(motion_w));
474             if (within_tolerance && (abs((gint) event->motion.x - xp)
475                     < tolerance) && (abs((gint) event->motion.y - yp)
476                     < tolerance)) {
477                 break; // do not drag if we're within tolerance from origin
478             }
479             // Once the user has moved farther than tolerance from the original location
480             // (indicating they intend to move the object, not click), then always process the
481             // motion notify coordinates as given (no snapping back to origin)
482             within_tolerance = false;
484             if (Inkscape::Rubberband::get(desktop)->is_started()) {
485                 Inkscape::Rubberband::get(desktop)->move(motion_dt);
486             } else {
487                 Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
488             }
489             if (zoom_rb == 2)
490                 gobble_motion_events(GDK_BUTTON2_MASK);
491         }
492         break;
493     case GDK_BUTTON_RELEASE:
494         xp = yp = 0;
495         if (within_tolerance && (panning || zoom_rb)) {
496             zoom_rb = 0;
497             if (panning) {
498                 panning = 0;
499                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
500                         event->button.time);
501             }
502             Geom::Point const event_w(event->button.x, event->button.y);
503             Geom::Point const event_dt(desktop->w2d(event_w));
504             double const zoom_inc = prefs->getDoubleLimited(
505                     "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
506             desktop->zoom_relative_keep_point(event_dt, (event->button.state
507                     & GDK_SHIFT_MASK) ? 1 / zoom_inc : zoom_inc);
508             desktop->updateNow();
509             ret = TRUE;
510         } else if (panning == event->button.button) {
511             panning = 0;
512             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
513                     event->button.time);
515             // in slow complex drawings, some of the motion events are lost;
516             // to make up for this, we scroll it once again to the button-up event coordinates
517             // (i.e. canvas will always get scrolled all the way to the mouse release point,
518             // even if few intermediate steps were visible)
519             Geom::Point const motion_w(event->button.x, event->button.y);
520             Geom::Point const moved_w(motion_w - button_w);
521             event_context->desktop->scroll_world(moved_w);
522             desktop->updateNow();
523             ret = TRUE;
524         } else if (zoom_rb == event->button.button) {
525             zoom_rb = 0;
526             Geom::OptRect const b =
527                     Inkscape::Rubberband::get(desktop)->getRectangle();
528             Inkscape::Rubberband::get(desktop)->stop();
529             if (b && !within_tolerance) {
530                 desktop->set_display_area(*b, 10);
531             }
532             ret = TRUE;
533         }
534         break;
535     case GDK_KEY_PRESS: {
536         double const acceleration = prefs->getDoubleLimited(
537                 "/options/scrollingacceleration/value", 0, 0, 6);
538         int const key_scroll = prefs->getIntLimited("/options/keyscroll/value",
539                 10, 0, 1000);
541         switch (get_group0_keyval(&event->key)) {
542         // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
543         // in the editing window). So we resteal them back and run our regular shortcut
544         // invoker on them.
545         unsigned int shortcut;
546     case GDK_Tab:
547     case GDK_ISO_Left_Tab:
548     case GDK_F1:
549         shortcut = get_group0_keyval(&event->key);
550         if (event->key.state & GDK_SHIFT_MASK)
551             shortcut |= SP_SHORTCUT_SHIFT_MASK;
552         if (event->key.state & GDK_CONTROL_MASK)
553             shortcut |= SP_SHORTCUT_CONTROL_MASK;
554         if (event->key.state & GDK_MOD1_MASK)
555             shortcut |= SP_SHORTCUT_ALT_MASK;
556         ret = sp_shortcut_invoke(shortcut, desktop);
557         break;
559     case GDK_D:
560     case GDK_d:
561         if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
562             sp_toggle_dropper(desktop);
563             ret = TRUE;
564         }
565         break;
566     case GDK_Q:
567     case GDK_q:
568         if (desktop->quick_zoomed()) {
569             ret = TRUE;
570         }
571         if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
572             desktop->zoom_quick(true);
573             ret = TRUE;
574         }
575         break;
576     case GDK_W:
577     case GDK_w:
578     case GDK_F4:
579         /* Close view */
580         if (MOD__CTRL_ONLY) {
581             sp_ui_close_view(NULL);
582             ret = TRUE;
583         }
584         break;
585     case GDK_Left: // Ctrl Left
586     case GDK_KP_Left:
587     case GDK_KP_4:
588         if (MOD__CTRL_ONLY) {
589             int i = (int) floor(key_scroll * accelerate_scroll(event,
590                     acceleration, sp_desktop_canvas(desktop)));
591             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
592             event_context->desktop->scroll_world(i, 0);
593             ret = TRUE;
594         }
595         break;
596     case GDK_Up: // Ctrl Up
597     case GDK_KP_Up:
598     case GDK_KP_8:
599         if (MOD__CTRL_ONLY) {
600             int i = (int) floor(key_scroll * accelerate_scroll(event,
601                     acceleration, sp_desktop_canvas(desktop)));
602             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
603             event_context->desktop->scroll_world(0, i);
604             ret = TRUE;
605         }
606         break;
607     case GDK_Right: // Ctrl Right
608     case GDK_KP_Right:
609     case GDK_KP_6:
610         if (MOD__CTRL_ONLY) {
611             int i = (int) floor(key_scroll * accelerate_scroll(event,
612                     acceleration, sp_desktop_canvas(desktop)));
613             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
614             event_context->desktop->scroll_world(-i, 0);
615             ret = TRUE;
616         }
617         break;
618     case GDK_Down: // Ctrl Down
619     case GDK_KP_Down:
620     case GDK_KP_2:
621         if (MOD__CTRL_ONLY) {
622             int i = (int) floor(key_scroll * accelerate_scroll(event,
623                     acceleration, sp_desktop_canvas(desktop)));
624             gobble_key_events(get_group0_keyval(&event->key), GDK_CONTROL_MASK);
625             event_context->desktop->scroll_world(0, -i);
626             ret = TRUE;
627         }
628         break;
629     case GDK_F10:
630         if (MOD__SHIFT_ONLY) {
631             sp_event_root_menu_popup(desktop, NULL, event);
632             ret = TRUE;
633         }
634         break;
635     case GDK_space:
636         if (prefs->getBool("/options/spacepans/value")) {
637             event_context->space_panning = true;
638             event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE,
639                     _("<b>Space+mouse drag</b> to pan canvas"));
640             ret = TRUE;
641         } else {
642             sp_toggle_selector(desktop);
643             ret = TRUE;
644         }
645         break;
646     case GDK_z:
647     case GDK_Z:
648         if (MOD__ALT_ONLY) {
649             desktop->zoom_grab_focus();
650             ret = TRUE;
651         }
652         break;
653     default:
654         break;
655         }
656     }
657         break;
658     case GDK_KEY_RELEASE:
659         switch (get_group0_keyval(&event->key)) {
660         case GDK_space:
661             if (event_context->space_panning) {
662                 event_context->space_panning = false;
663                 event_context->_message_context->clear();
664                 if (panning == 1) {
665                     panning = 0;
666                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
667                             event->key.time);
668                     desktop->updateNow();
669                 }
670                 ret = TRUE;
671             }
672             break;
673         case GDK_Q:
674         case GDK_q:
675             if (desktop->quick_zoomed()) {
676                 desktop->zoom_quick(false);
677                 ret = TRUE;
678             }
679             break;
680         default:
681             break;
682         }
683         break;
684     case GDK_SCROLL: {
685         bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
686         bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
687         int const wheel_scroll = prefs->getIntLimited(
688                 "/options/wheelscroll/value", 40, 0, 1000);
690         /* shift + wheel, pan left--right */
691         if (event->scroll.state & GDK_SHIFT_MASK) {
692             switch (event->scroll.direction) {
693             case GDK_SCROLL_UP:
694                 desktop->scroll_world(wheel_scroll, 0);
695                 break;
696             case GDK_SCROLL_DOWN:
697                 desktop->scroll_world(-wheel_scroll, 0);
698                 break;
699             default:
700                 break;
701             }
703             /* ctrl + wheel, zoom in--out */
704         } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
705             double rel_zoom;
706             double const zoom_inc = prefs->getDoubleLimited(
707                     "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
708             switch (event->scroll.direction) {
709             case GDK_SCROLL_UP:
710                 rel_zoom = zoom_inc;
711                 break;
712             case GDK_SCROLL_DOWN:
713                 rel_zoom = 1 / zoom_inc;
714                 break;
715             default:
716                 rel_zoom = 0.0;
717                 break;
718             }
719             if (rel_zoom != 0.0) {
720                 Geom::Point const scroll_dt = desktop->point();
721                 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
722             }
724             /* no modifier, pan up--down (left--right on multiwheel mice?) */
725         } else {
726             switch (event->scroll.direction) {
727             case GDK_SCROLL_UP:
728                 desktop->scroll_world(0, wheel_scroll);
729                 break;
730             case GDK_SCROLL_DOWN:
731                 desktop->scroll_world(0, -wheel_scroll);
732                 break;
733             case GDK_SCROLL_LEFT:
734                 desktop->scroll_world(wheel_scroll, 0);
735                 break;
736             case GDK_SCROLL_RIGHT:
737                 desktop->scroll_world(-wheel_scroll, 0);
738                 break;
739             }
740         }
741         break;
742     }
743     default:
744         break;
745     }
747     return ret;
750 /**
751  * Handles item specific events. Gets called from Gdk.
752  *
753  * Only reacts to right mouse button at the moment.
754  * \todo Fixme: do context sensitive popup menu on items.
755  */
756 gint sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item,
757         GdkEvent *event) {
758     int ret = FALSE;
760     switch (event->type) {
761     case GDK_BUTTON_PRESS:
762         if ((event->button.button == 3) && !(event->button.state
763                 & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
764             sp_event_root_menu_popup(ec->desktop, item, event);
765             ret = TRUE;
766         }
767         break;
768     default:
769         break;
770     }
772     return ret;
775 /**
776  * @brief: Returns true if we're hovering above a knot (needed because we don't want to pre-snap in that case)
777  */
779 bool sp_event_context_knot_mouseover(SPEventContext *ec)
781     if (ec->shape_editor) {
782         return ec->shape_editor->knot_mouseover();
783     }
785     return false;
788 /**
789  * @brief An observer that relays pref changes to the derived classes
790  */
791 class ToolPrefObserver: public Inkscape::Preferences::Observer {
792 public:
793     ToolPrefObserver(Glib::ustring const &path, SPEventContext *ec) :
794         Inkscape::Preferences::Observer(path), _ec(ec) {
795     }
796     virtual void notify(Inkscape::Preferences::Entry const &val) {
797         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set) {
798             ((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set(_ec,
799                     const_cast<Inkscape::Preferences::Entry*> (&val));
800         }
801     }
802 private:
803     SPEventContext * const _ec;
804 };
806 /**
807  * Creates new SPEventContext object and calls its virtual setup() function.
808  * @todo This is bogus. pref_path should be a private property of the inheriting objects.
809  */
810 SPEventContext *
811 sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path,
812         unsigned int key) {
813     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
814     g_return_val_if_fail(desktop != NULL, NULL);
816     SPEventContext * const ec = (SPEventContext*) g_object_new(type, NULL);
818     ec->desktop = desktop;
819     ec->_message_context
820             = new Inkscape::MessageContext(desktop->messageStack());
821     ec->key = key;
822     ec->pref_observer = NULL;
824     if (pref_path) {
825         ec->pref_observer = new ToolPrefObserver(pref_path, ec);
827         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
828         prefs->addObserver(*(ec->pref_observer));
829     }
831     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
832         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
834     return ec;
837 /**
838  * Finishes SPEventContext.
839  */
840 void sp_event_context_finish(SPEventContext *ec) {
841     g_return_if_fail(ec != NULL);
842     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
844     ec->enableSelectionCue(false);
846     if (ec->next) {
847         g_warning("Finishing event context with active link\n");
848     }
850     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
851         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
854 //-------------------------------member functions
856 /**
857  * Enables/disables the SPEventContext's SelCue.
858  */
859 void SPEventContext::enableSelectionCue(bool enable) {
860     if (enable) {
861         if (!_selcue) {
862             _selcue = new Inkscape::SelCue(desktop);
863         }
864     } else {
865         delete _selcue;
866         _selcue = NULL;
867     }
870 /**
871  * Enables/disables the SPEventContext's GrDrag.
872  */
873 void SPEventContext::enableGrDrag(bool enable) {
874     if (enable) {
875         if (!_grdrag) {
876             _grdrag = new GrDrag(desktop);
877         }
878     } else {
879         if (_grdrag) {
880             delete _grdrag;
881             _grdrag = NULL;
882         }
883     }
886 /**
887  * Calls virtual set() function of SPEventContext.
888  */
889 void sp_event_context_read(SPEventContext *ec, gchar const *key) {
890     g_return_if_fail(ec != NULL);
891     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
892     g_return_if_fail(key != NULL);
894     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
895         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
896         Inkscape::Preferences::Entry val = prefs->getEntry(
897                 ec->pref_observer->observed_path + '/' + key);
898         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, &val);
899     }
902 /**
903  * Calls virtual activate() function of SPEventContext.
904  */
905 void sp_event_context_activate(SPEventContext *ec) {
906     g_return_if_fail(ec != NULL);
907     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
909     // Make sure no delayed snapping events are carried over after switching contexts
910     // (this is only an additional safety measure against sloppy coding, because each
911     // context should take care of this by itself.
912     sp_event_context_discard_delayed_snap_event(ec);
914     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
915         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
918 /**
919  * Calls virtual deactivate() function of SPEventContext.
920  */
921 void sp_event_context_deactivate(SPEventContext *ec) {
922     g_return_if_fail(ec != NULL);
923     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
925     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
926         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
929 /**
930  * Calls virtual root_handler(), the main event handling function.
931  */
932 gint sp_event_context_root_handler(SPEventContext * event_context,
933         GdkEvent * event)
935     switch (event->type) {
936     case GDK_MOTION_NOTIFY:
937         sp_event_context_snap_delay_handler(event_context, NULL, NULL,
938                 (GdkEventMotion *) event,
939                 DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER);
940         break;
941     case GDK_BUTTON_RELEASE:
942         if (event_context->_delayed_snap_event) {
943             // If we have any pending snapping action, then invoke it now
944             sp_event_context_snap_watchdog_callback(
945                     event_context->_delayed_snap_event);
946         }
947         break;
948     case GDK_BUTTON_PRESS:
949     case GDK_2BUTTON_PRESS:
950     case GDK_3BUTTON_PRESS:
951         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
952         // drawing a new shape we really should snap though.
953         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(
954                 false);
955         break;
956     default:
957         break;
958     }
960     return sp_event_context_virtual_root_handler(event_context, event);
963 gint sp_event_context_virtual_root_handler(SPEventContext * event_context, GdkEvent * event) {
964     gint ret = false;
965     if (event_context) {    // If no event-context is available then do nothing, otherwise Inkscape would crash
966                             // (see the comment in SPDesktop::set_event_context, and bug LP #622350)
967         ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
968         set_event_location(event_context->desktop, event);
969     }
970     return ret;
973 /**
974  * Calls virtual item_handler(), the item event handling function.
975  */
976 gint sp_event_context_item_handler(SPEventContext * event_context,
977         SPItem * item, GdkEvent * event) {
978     switch (event->type) {
979     case GDK_MOTION_NOTIFY:
980         sp_event_context_snap_delay_handler(event_context, (gpointer) item, NULL, (GdkEventMotion *) event, DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER);
981         break;
982     case GDK_BUTTON_RELEASE:
983         if (event_context->_delayed_snap_event) {
984             // If we have any pending snapping action, then invoke it now
985             sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
986         }
987         break;
988     case GDK_BUTTON_PRESS:
989     case GDK_2BUTTON_PRESS:
990     case GDK_3BUTTON_PRESS:
991         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
992         // drawing a new shape we really should snap though.
993         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
994         break;
995     default:
996         break;
997     }
999     return sp_event_context_virtual_item_handler(event_context, item, event);
1002 gint sp_event_context_virtual_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event) {
1003     gint ret = false;
1004     if (event_context) {    // If no event-context is available then do nothing, otherwise Inkscape would crash
1005                             // (see the comment in SPDesktop::set_event_context, and bug LP #622350)
1006         ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
1007         if (!ret) {
1008             ret = sp_event_context_virtual_root_handler(event_context, event);
1009         } else {
1010             set_event_location(event_context->desktop, event);
1011         }
1012     }
1014     return ret;
1017 /**
1018  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
1019  */
1020 static void set_event_location(SPDesktop *desktop, GdkEvent *event) {
1021     if (event->type != GDK_MOTION_NOTIFY) {
1022         return;
1023     }
1025     Geom::Point const button_w(event->button.x, event->button.y);
1026     Geom::Point const button_dt(desktop->w2d(button_w));
1027     desktop->setPosition(button_dt);
1028     desktop->set_coordinate_status(button_dt);
1031 //-------------------------------------------------------------------
1032 /**
1033  * Create popup menu and tell Gtk to show it.
1034  */
1035 void sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event) {
1036     GtkWidget *menu;
1038     /* fixme: This is not what I want but works for now (Lauris) */
1039     if (event->type == GDK_KEY_PRESS) {
1040         item = sp_desktop_selection(desktop)->singleItem();
1041     }
1042     menu = sp_ui_context_menu(desktop, item);
1043     gtk_widget_show(menu);
1045     switch (event->type) {
1046     case GDK_BUTTON_PRESS:
1047         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL,
1048                 event->button.button, event->button.time);
1049         break;
1050     case GDK_KEY_PRESS:
1051         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
1052         break;
1053     default:
1054         break;
1055     }
1058 /**
1059  * Show tool context specific modifier tip.
1060  */
1061 void sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
1062         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
1063         gchar const *alt_tip) {
1064     guint keyval = get_group0_keyval(&event->key);
1066     bool ctrl = ctrl_tip && (MOD__CTRL || (keyval == GDK_Control_L) || (keyval
1067             == GDK_Control_R));
1068     bool shift = shift_tip && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval
1069             == GDK_Shift_R));
1070     bool alt = alt_tip && (MOD__ALT || (keyval == GDK_Alt_L) || (keyval
1071             == GDK_Alt_R) || (keyval == GDK_Meta_L) || (keyval == GDK_Meta_R));
1073     gchar *tip = g_strdup_printf("%s%s%s%s%s", (ctrl ? ctrl_tip : ""), (ctrl
1074             && (shift || alt) ? "; " : ""), (shift ? shift_tip : ""), ((ctrl
1075             || shift) && alt ? "; " : ""), (alt ? alt_tip : ""));
1077     if (strlen(tip) > 0) {
1078         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
1079     }
1081     g_free(tip);
1084 /**
1085  * Return the keyval corresponding to the key event in group 0, i.e.,
1086  * in the main (English) layout.
1087  *
1088  * Use this instead of simply event->keyval, so that your keyboard shortcuts
1089  * work regardless of layouts (e.g., in Cyrillic).
1090  */
1091 guint get_group0_keyval(GdkEventKey *event) {
1092     guint keyval = 0;
1093     gdk_keymap_translate_keyboard_state(gdk_keymap_get_for_display(
1094             gdk_display_get_default()), event->hardware_keycode,
1095             (GdkModifierType) event->state, 0 /*event->key.group*/, &keyval,
1096             NULL, NULL, NULL);
1097     return keyval;
1100 /**
1101  * Returns item at point p in desktop.
1102  *
1103  * If state includes alt key mask, cyclically selects under; honors
1104  * into_groups.
1105  */
1106 SPItem *
1107 sp_event_context_find_item(SPDesktop *desktop, Geom::Point const &p,
1108         bool select_under, bool into_groups) {
1109     SPItem *item;
1111     if (select_under) {
1112         SPItem *selected_at_point = desktop->item_from_list_at_point_bottom(
1113                 desktop->selection->itemList(), p);
1114         item = desktop->item_at_point(p, into_groups, selected_at_point);
1115         if (item == NULL) { // we may have reached bottom, flip over to the top
1116             item = desktop->item_at_point(p, into_groups, NULL);
1117         }
1118     } else
1119         item = desktop->item_at_point(p, into_groups, NULL);
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->item_from_list_at_point_bottom(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 :