Code

Make curvature work again by fixing a minor omission
[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-2005 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 "sp-namedview.h"
47 #include "selection.h"
48 #include "file.h"
49 #include "interface.h"
50 #include "macros.h"
51 #include "tools-switch.h"
52 #include "preferences.h"
53 #include "message-context.h"
54 #include "gradient-drag.h"
55 #include "object-edit.h"
56 #include "attributes.h"
57 #include "rubberband.h"
58 #include "selcue.h"
59 #include "node-context.h"
60 #include "lpe-tool-context.h"
62 static void sp_event_context_class_init(SPEventContextClass *klass);
63 static void sp_event_context_init(SPEventContext *event_context);
64 static void sp_event_context_dispose(GObject *object);
66 static void sp_event_context_private_setup(SPEventContext *ec);
67 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
68 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
70 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
72 static GObjectClass *parent_class;
74 // globals for temporary switching to selector by space
75 static bool selector_toggled = FALSE;
76 static int switch_selector_to = 0;
78 // globals for temporary switching to dropper by 'D'
79 static bool dropper_toggled = FALSE;
80 static int switch_dropper_to = 0;
82 static gint xp = 0, yp = 0; // where drag started
83 static gint tolerance = 0;
84 static bool within_tolerance = false;
86 // globals for keeping track of keyboard scroll events in order to accelerate
87 static guint32 scroll_event_time = 0;
88 static gdouble scroll_multiply = 1;
89 static guint scroll_keyval = 0;
91 /**
92  * Registers the SPEventContext class with Glib and returns its type number.
93  */
94 GType
95 sp_event_context_get_type(void)
96 {
97     static GType type = 0;
98     if (!type) {
99         GTypeInfo info = {
100             sizeof(SPEventContextClass),
101             NULL, NULL,
102             (GClassInitFunc) sp_event_context_class_init,
103             NULL, NULL,
104             sizeof(SPEventContext),
105             4,
106             (GInstanceInitFunc) sp_event_context_init,
107             NULL,    /* value_table */
108         };
109         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
110     }
111     return type;
114 /**
115  * Callback to set up the SPEventContext vtable.
116  */
117 static void
118 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
137 sp_event_context_init(SPEventContext *event_context)
139     event_context->desktop = NULL;
140     event_context->cursor = NULL;
141     event_context->_message_context = NULL;
142     event_context->_selcue = NULL;
143     event_context->_grdrag = NULL;
144     event_context->space_panning = false;
145     event_context->shape_editor = NULL;
146     event_context->_delayed_snap_event = NULL;
149 /**
150  * Callback to free and null member variables of SPEventContext object.
151  */
152 static void
153 sp_event_context_dispose(GObject *object)
155     SPEventContext *ec;
157     ec = SP_EVENT_CONTEXT(object);
159     if (ec->_message_context) {
160         delete ec->_message_context;
161     }
163     if (ec->cursor != NULL) {
164         gdk_cursor_unref(ec->cursor);
165         ec->cursor = NULL;
166     }
168     if (ec->desktop) {
169         ec->desktop = NULL;
170     }
172     if (ec->pref_observer) {
173         delete ec->pref_observer;
174     }
176     if (ec->_delayed_snap_event) {
177         delete ec->_delayed_snap_event;
178     }
180     G_OBJECT_CLASS(parent_class)->dispose(object);
183 /**
184  * Recreates and draws cursor on desktop related to SPEventContext.
185  */
186 void
187 sp_event_context_update_cursor(SPEventContext *ec)
189     GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
190     if (w->window) {
191         /* fixme: */
192         if (ec->cursor_shape) {
193             GdkBitmap *bitmap = NULL;
194             GdkBitmap *mask = NULL;
195             sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
196             if ((bitmap != NULL) && (mask != NULL)) {
197                 if (ec->cursor)
198                     gdk_cursor_unref (ec->cursor);
199                 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
200                                                         &w->style->black,
201                                                         &w->style->white,
202                                                         ec->hot_x, ec->hot_y);
203                 g_object_unref (bitmap);
204                 g_object_unref (mask);
205             }
206         }
207         gdk_window_set_cursor(w->window, ec->cursor);
208         gdk_flush();
209     }
210     ec->desktop->waiting_cursor = false;
213 /**
214  * Callback that gets called on initialization of SPEventContext object.
215  * Redraws mouse cursor, at the moment.
216  */
217 static void
218 sp_event_context_private_setup(SPEventContext *ec)
220     sp_event_context_update_cursor(ec);
223 /**
224  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
225  */
226 gint gobble_key_events(guint keyval, gint mask)
228     GdkEvent *event_next;
229     gint i = 0;
231     event_next = gdk_event_get();
232     // while the next event is also a key notify with the same keyval and mask,
233     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
234            && event_next->key.keyval == keyval
235            && (!mask || (event_next->key.state & mask))) {
236         if (event_next->type == GDK_KEY_PRESS)
237             i ++;
238         // kill it
239         gdk_event_free(event_next);
240         // get next
241         event_next = gdk_event_get();
242     }
243     // otherwise, put it back onto the queue
244     if (event_next) gdk_event_put(event_next);
246     return i;
249 /**
250  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
251 */
252 gint gobble_motion_events(gint mask)
254     GdkEvent *event_next;
255     gint i = 0;
257     event_next = gdk_event_get();
258     // while the next event is also a key notify with the same keyval and mask,
259     while (event_next && event_next->type == GDK_MOTION_NOTIFY
260            && (event_next->motion.state & mask)) {
261         // kill it
262         gdk_event_free(event_next);
263         // get next
264         event_next = gdk_event_get();
265         i ++;
266     }
267     // otherwise, put it back onto the queue
268     if (event_next) gdk_event_put(event_next);
270     return i;
273 /**
274  * Toggles current tool between active tool and selector tool.
275  * Subroutine of sp_event_context_private_root_handler().
276  */
277 static void
278 sp_toggle_selector(SPDesktop *dt)
280     if (!dt->event_context) return;
282     if (tools_isactive(dt, TOOLS_SELECT)) {
283         if (selector_toggled) {
284             if (switch_selector_to) tools_switch (dt, switch_selector_to);
285             selector_toggled = FALSE;
286         } else return;
287     } else {
288         selector_toggled = TRUE;
289         switch_selector_to = tools_active(dt);
290         tools_switch (dt, TOOLS_SELECT);
291     }
294 /**
295  * Toggles current tool between active tool and dropper tool.
296  * Subroutine of sp_event_context_private_root_handler().
297  */
298 static void
299 sp_toggle_dropper(SPDesktop *dt)
301     if (!dt->event_context) return;
303     if (tools_isactive(dt, TOOLS_DROPPER)) {
304         if (dropper_toggled) {
305             if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
306             dropper_toggled = FALSE;
307         } else return;
308     } else {
309         dropper_toggled = TRUE;
310         switch_dropper_to = tools_active(dt);
311         tools_switch (dt, TOOLS_DROPPER);
312     }
315 /**
316  * Calculates and keeps track of scroll acceleration.
317  * Subroutine of sp_event_context_private_root_handler().
318  */
319 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
321     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
323     /* key pressed within 500ms ? (1/2 second) */
324     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
325         scroll_multiply = 1; // abort acceleration
326     } else {
327         scroll_multiply += acceleration; // continue acceleration
328     }
330     scroll_event_time = ((GdkEventKey *) event)->time;
331     scroll_keyval = event->key.keyval;
333     return scroll_multiply;
336 /**
337  * Main event dispatch, gets called from Gdk.
338  */
339 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
341     static Geom::Point button_w;
342     static unsigned int panning = 0;
343     static unsigned int zoom_rb = 0;
345     SPDesktop *desktop = event_context->desktop;
346     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
348     /// @todo REmove redundant /value in preference keys
349     tolerance = prefs->getIntLimited(
350             "/options/dragtolerance/value", 0, 0, 100);
352     gint ret = FALSE;
354     switch (event->type) {
355         case GDK_2BUTTON_PRESS:
356             if (panning) {
357                 panning = 0;
358                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
359                         event->button.time);
360                 ret = TRUE;
361             } else {
362                 /* sp_desktop_dialog(); */
363             }
364             break;
365         case GDK_BUTTON_PRESS:
367             // save drag origin
368             xp = (gint) event->button.x;
369             yp = (gint) event->button.y;
370             within_tolerance = true;
372             button_w = Geom::Point(event->button.x, event->button.y);
374             switch (event->button.button) {
375                 case 1:
376                     if (event_context->space_panning) {
377                         // When starting panning, make sure there are no snap events pending because these might disable the panning again
378                         sp_event_context_discard_delayed_snap_event(event_context);
379                         panning = 1;
380                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
381                             GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
382                             NULL, event->button.time-1);
383                         ret = TRUE;
384                     }
385                     break;
386                 case 2:
387                     if (event->button.state & GDK_SHIFT_MASK) {
388                         zoom_rb = 2;
389                     } else {
390                         // When starting panning, make sure there are no snap events pending because these might disable the panning again
391                                                 sp_event_context_discard_delayed_snap_event(event_context);
392                                                 panning = 2;
393                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
394                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
395                             NULL, event->button.time-1);
396                     }
397                     ret = TRUE;
398                     break;
399                 case 3:
400                     if (event->button.state & GDK_SHIFT_MASK
401                             || event->button.state & GDK_CONTROL_MASK) {
402                         // When starting panning, make sure there are no snap events pending because these might disable the panning again
403                                                 sp_event_context_discard_delayed_snap_event(event_context);
404                         panning = 3;
405                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
406                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
407                                 NULL, event->button.time);
408                         ret = TRUE;
409                     } else {
410                         sp_event_root_menu_popup(desktop, NULL, event);
411                     }
412                     break;
413                 default:
414                     break;
415             }
416             break;
417         case GDK_MOTION_NOTIFY:
418             if (panning) {
419                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
420                         || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
421                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
422                    ) {
423                     /* Gdk seems to lose button release for us sometimes :-( */
424                         panning = 0;
425                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
426                             event->button.time);
427                     ret = TRUE;
428                 } else {
429                     if ( within_tolerance
430                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
431                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
432                     {
433                         // do not drag if we're within tolerance from origin
434                         break;
435                     }
436                     // Once the user has moved farther than tolerance from
437                     // the original location (indicating they intend to move
438                     // the object, not click), then always process the motion
439                     // notify coordinates as given (no snapping back to origin)
440                     within_tolerance = false;
442                     // gobble subsequent motion events to prevent "sticking"
443                     // when scrolling is slow
444                     gobble_motion_events(panning == 2 ?
445                                          GDK_BUTTON2_MASK :
446                                          (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
448                     Geom::Point const motion_w(event->motion.x, event->motion.y);
449                     Geom::Point const moved_w( motion_w - button_w );
450                     event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
451                     ret = TRUE;
452                 }
453             } else if (zoom_rb) {
454                 Geom::Point const motion_w(event->motion.x, event->motion.y);
455                 Geom::Point const motion_dt(desktop->w2d(motion_w));
457                 if ( within_tolerance
458                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
459                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
460                     break; // do not drag if we're within tolerance from origin
461                 }
462                 // Once the user has moved farther than tolerance from the original location
463                 // (indicating they intend to move the object, not click), then always process the
464                 // motion notify coordinates as given (no snapping back to origin)
465                 within_tolerance = false;
467                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
468                     Inkscape::Rubberband::get(desktop)->move(motion_dt);
469                 } else {
470                     Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
471                 }
472                 if (zoom_rb == 2)
473                     gobble_motion_events(GDK_BUTTON2_MASK);
474             }
475             break;
476         case GDK_BUTTON_RELEASE:
477             xp = yp = 0;
478             if (within_tolerance && (panning || zoom_rb)) {
479                 zoom_rb = 0;
480                 if (panning) {
481                     panning = 0;
482                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
483                                       event->button.time);
484                 }
485                 Geom::Point const event_w(event->button.x, event->button.y);
486                 Geom::Point const event_dt(desktop->w2d(event_w));
487                 double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
488                 desktop->zoom_relative_keep_point(event_dt,
489                           (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
490                 desktop->updateNow();
491                 ret = TRUE;
492             } else if (panning == event->button.button) {
493                 panning = 0;
494                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
495                                       event->button.time);
497                 // in slow complex drawings, some of the motion events are lost;
498                 // to make up for this, we scroll it once again to the button-up event coordinates
499                 // (i.e. canvas will always get scrolled all the way to the mouse release point,
500                 // even if few intermediate steps were visible)
501                 Geom::Point const motion_w(event->button.x, event->button.y);
502                 Geom::Point const moved_w( motion_w - button_w );
503                 event_context->desktop->scroll_world(moved_w);
504                 desktop->updateNow();
505                 ret = TRUE;
506             } else if (zoom_rb == event->button.button) {
507                 zoom_rb = 0;
508                 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
509                 Inkscape::Rubberband::get(desktop)->stop();
510                 if (b && !within_tolerance) {
511                     desktop->set_display_area(*b, 10);
512                 }
513                 ret = TRUE;
514             }
515             break;
516         case GDK_KEY_PRESS:
517                 {
518                 double const acceleration = prefs->getDoubleLimited("/options/scrollingacceleration/value", 0, 0, 6);
519                 int const key_scroll = prefs->getIntLimited("/options/keyscroll/value", 10, 0, 1000);
521                 switch (get_group0_keyval(&event->key)) {
522                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
523                 // in the editing window). So we resteal them back and run our regular shortcut
524                 // invoker on them.
525                 unsigned int shortcut;
526                 case GDK_Tab:
527                 case GDK_ISO_Left_Tab:
528                 case GDK_F1:
529                     shortcut = get_group0_keyval(&event->key);
530                     if (event->key.state & GDK_SHIFT_MASK)
531                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
532                     if (event->key.state & GDK_CONTROL_MASK)
533                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
534                     if (event->key.state & GDK_MOD1_MASK)
535                         shortcut |= SP_SHORTCUT_ALT_MASK;
536                     ret = sp_shortcut_invoke(shortcut, desktop);
537                     break;
539                 case GDK_D:
540                 case GDK_d:
541                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
542                         sp_toggle_dropper(desktop);
543                         ret = TRUE;
544                     }
545                     break;
546                 case GDK_Q:
547                 case GDK_q:
548                                         if (desktop->quick_zoomed()) {
549                                                 ret = TRUE;
550                                         }
551                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
552                                                 desktop->zoom_quick(true);
553                         ret = TRUE;
554                     }
555                     break;
556                 case GDK_W:
557                 case GDK_w:
558                 case GDK_F4:
559                     /* Close view */
560                     if (MOD__CTRL_ONLY) {
561                         sp_ui_close_view(NULL);
562                         ret = TRUE;
563                     }
564                     break;
565                 case GDK_Left: // Ctrl Left
566                 case GDK_KP_Left:
567                 case GDK_KP_4:
568                     if (MOD__CTRL_ONLY) {
569                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
570                         gobble_key_events(get_group0_keyval(&event->key),
571                                 GDK_CONTROL_MASK);
572                         event_context->desktop->scroll_world(i, 0);
573                         ret = TRUE;
574                     }
575                     break;
576                 case GDK_Up: // Ctrl Up
577                 case GDK_KP_Up:
578                 case GDK_KP_8:
579                     if (MOD__CTRL_ONLY) {
580                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
581                         gobble_key_events(get_group0_keyval(&event->key),
582                                 GDK_CONTROL_MASK);
583                         event_context->desktop->scroll_world(0, i);
584                         ret = TRUE;
585                     }
586                     break;
587                 case GDK_Right: // Ctrl Right
588                 case GDK_KP_Right:
589                 case GDK_KP_6:
590                     if (MOD__CTRL_ONLY) {
591                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
592                         gobble_key_events(get_group0_keyval(&event->key),
593                                 GDK_CONTROL_MASK);
594                         event_context->desktop->scroll_world(-i, 0);
595                         ret = TRUE;
596                     }
597                     break;
598                 case GDK_Down: // Ctrl Down
599                 case GDK_KP_Down:
600                 case GDK_KP_2:
601                     if (MOD__CTRL_ONLY) {
602                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
603                         gobble_key_events(get_group0_keyval(&event->key),
604                                 GDK_CONTROL_MASK);
605                         event_context->desktop->scroll_world(0, -i);
606                         ret = TRUE;
607                     }
608                     break;
609                 case GDK_F10:
610                     if (MOD__SHIFT_ONLY) {
611                         sp_event_root_menu_popup(desktop, NULL, event);
612                         ret= TRUE;
613                     }
614                     break;
615                 case GDK_space:
616                     if (prefs->getBool("/options/spacepans/value")) {
617                         event_context->space_panning = true;
618                         event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
619                         ret= TRUE;
620                     } else {
621                         sp_toggle_selector(desktop);
622                         ret= TRUE;
623                     }
624                     break;
625                 case GDK_z:
626                 case GDK_Z:
627                     if (MOD__ALT_ONLY) {
628                         desktop->zoom_grab_focus();
629                         ret = TRUE;
630                     }
631                     break;
632                 default:
633                     break;
634             }
635                 }
636             break;
637         case GDK_KEY_RELEASE:
638             switch (get_group0_keyval(&event->key)) {
639                 case GDK_space:
640                     if (event_context->space_panning) {
641                         event_context->space_panning = false;
642                         event_context->_message_context->clear();
643                         if (panning == 1) {
644                             panning = 0;
645                             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
646                                   event->key.time);
647                             desktop->updateNow();
648                         }
649                         ret= TRUE;
650                     }
651                     break;
652                 case GDK_Q:
653                 case GDK_q:
654                                         if (desktop->quick_zoomed()) {
655                                                 desktop->zoom_quick(false);
656                         ret = TRUE;
657                     }
658                     break;
659                 default:
660                     break;
661             }
662             break;
663         case GDK_SCROLL:
664         {
665             bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
666             bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
667             int const wheel_scroll = prefs->getIntLimited("/options/wheelscroll/value", 40, 0, 1000);
669             /* shift + wheel, pan left--right */
670             if (event->scroll.state & GDK_SHIFT_MASK) {
671                 switch (event->scroll.direction) {
672                     case GDK_SCROLL_UP:
673                         desktop->scroll_world(wheel_scroll, 0);
674                         break;
675                     case GDK_SCROLL_DOWN:
676                         desktop->scroll_world(-wheel_scroll, 0);
677                         break;
678                     default:
679                         break;
680                 }
682                 /* ctrl + wheel, zoom in--out */
683             } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
684                 double rel_zoom;
685                 double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
686                 switch (event->scroll.direction) {
687                     case GDK_SCROLL_UP:
688                         rel_zoom = zoom_inc;
689                         break;
690                     case GDK_SCROLL_DOWN:
691                         rel_zoom = 1 / zoom_inc;
692                         break;
693                     default:
694                         rel_zoom = 0.0;
695                         break;
696                 }
697                 if (rel_zoom != 0.0) {
698                     Geom::Point const scroll_dt = desktop->point();
699                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
700                 }
702                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
703             } else {
704                 switch (event->scroll.direction) {
705                     case GDK_SCROLL_UP:
706                         desktop->scroll_world(0, wheel_scroll);
707                         break;
708                     case GDK_SCROLL_DOWN:
709                         desktop->scroll_world(0, -wheel_scroll);
710                         break;
711                     case GDK_SCROLL_LEFT:
712                         desktop->scroll_world(wheel_scroll, 0);
713                         break;
714                     case GDK_SCROLL_RIGHT:
715                         desktop->scroll_world(-wheel_scroll, 0);
716                         break;
717                 }
718             }
719             break;
720         }
721         default:
722             break;
723     }
725     return ret;
728 /**
729  * Handles item specific events. Gets called from Gdk.
730  *
731  * Only reacts to right mouse button at the moment.
732  * \todo Fixme: do context sensitive popup menu on items.
733  */
734 gint
735 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
737     int ret = FALSE;
739     switch (event->type) {
740         case GDK_BUTTON_PRESS:
741             if ((event->button.button == 3)
742                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
743                 sp_event_root_menu_popup(ec->desktop, item, event);
744                 ret = TRUE;
745             }
746             break;
747         default:
748             break;
749     }
751     return ret;
754 /**
755  * @brief An observer that relays pref changes to the derived classes
756  */
757 class ToolPrefObserver : public Inkscape::Preferences::Observer {
758 public:
759     ToolPrefObserver(Glib::ustring const &path, SPEventContext *ec) :
760         Inkscape::Preferences::Observer(path),
761         _ec(ec) {}
762     virtual void notify(Inkscape::Preferences::Entry const &val)
763     {
764         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set) {
765             ((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set(_ec,
766                 const_cast<Inkscape::Preferences::Entry*>(&val));
767         }
768     }
769 private:
770     SPEventContext * const _ec;
771 };
773 /**
774  * Creates new SPEventContext object and calls its virtual setup() function.
775  * @todo This is bogus. pref_path should be a private property of the inheriting objects.
776  */
777 SPEventContext *
778 sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path, unsigned int key)
780     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
781     g_return_val_if_fail(desktop != NULL, NULL);
783     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
785     ec->desktop = desktop;
786     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
787     ec->key = key;
788     ec->pref_observer = NULL;
790     if (pref_path) {
791         ec->pref_observer = new ToolPrefObserver(pref_path, ec);
793         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
794         prefs->addObserver(*(ec->pref_observer));
795     }
797     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
798         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
800     return ec;
803 /**
804  * Finishes SPEventContext.
805  */
806 void
807 sp_event_context_finish(SPEventContext *ec)
809     g_return_if_fail(ec != NULL);
810     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
812     ec->enableSelectionCue(false);
814     if (ec->next) {
815         g_warning("Finishing event context with active link\n");
816     }
818     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
819         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
822 //-------------------------------member functions
824 /**
825  * Enables/disables the SPEventContext's SelCue.
826  */
827 void SPEventContext::enableSelectionCue(bool enable) {
828     if (enable) {
829         if (!_selcue) {
830             _selcue = new Inkscape::SelCue(desktop);
831         }
832     } else {
833         delete _selcue;
834         _selcue = NULL;
835     }
838 /**
839  * Enables/disables the SPEventContext's GrDrag.
840  */
841 void SPEventContext::enableGrDrag(bool enable) {
842     if (enable) {
843         if (!_grdrag) {
844             _grdrag = new GrDrag(desktop);
845         }
846     } else {
847         if (_grdrag) {
848             delete _grdrag;
849             _grdrag = NULL;
850         }
851     }
854 /**
855  * Calls virtual set() function of SPEventContext.
856  */
857 void
858 sp_event_context_read(SPEventContext *ec, gchar const *key)
860     g_return_if_fail(ec != NULL);
861     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
862     g_return_if_fail(key != NULL);
864     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
865         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
866         Inkscape::Preferences::Entry val = prefs->getEntry(
867             ec->pref_observer->observed_path + '/' + key );
868         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, &val);
869     }
872 /**
873  * Calls virtual activate() function of SPEventContext.
874  */
875 void
876 sp_event_context_activate(SPEventContext *ec)
878     g_return_if_fail(ec != NULL);
879     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
881     // Make sure no delayed snapping events are carried over after switching contexts
882     // (this is only an additional safety measure against sloppy coding, because each
883     // context should take care of this by itself.
884     sp_event_context_discard_delayed_snap_event(ec);
886     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
887         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
890 /**
891  * Calls virtual deactivate() function of SPEventContext.
892  */
893 void
894 sp_event_context_deactivate(SPEventContext *ec)
896     g_return_if_fail(ec != NULL);
897     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
899     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
900         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
903 /**
904  * Calls virtual root_handler(), the main event handling function.
905  */
906 gint
907 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
909     switch (event->type) {
910                 case GDK_MOTION_NOTIFY:
911                         sp_event_context_snap_delay_handler(event_context, NULL, NULL, (GdkEventMotion *)event, DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER);
912                         break;
913                 case GDK_BUTTON_RELEASE:
914                         if (event_context->_delayed_snap_event) {
915                                 // If we have any pending snapping action, then invoke it now
916                                 sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
917                         }
918                         break;
919                 case GDK_BUTTON_PRESS:
920         case GDK_2BUTTON_PRESS:
921         case GDK_3BUTTON_PRESS:
922                         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
923                         // drawing a new shape we really should snap though.
924                         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
925                         break;
926         default:
927                 break;
928     }
930     return sp_event_context_virtual_root_handler(event_context, event);
933 gint
934 sp_event_context_virtual_root_handler(SPEventContext * event_context, GdkEvent * event)
936         gint ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
937         set_event_location(event_context->desktop, event);
938         return ret;
941 /**
942  * Calls virtual item_handler(), the item event handling function.
943  */
944 gint
945 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
947         switch (event->type) {
948                 case GDK_MOTION_NOTIFY:
949                         sp_event_context_snap_delay_handler(event_context, item, NULL, (GdkEventMotion *)event, DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER);
950                         break;
951                 case GDK_BUTTON_RELEASE:
952                         if (event_context->_delayed_snap_event) {
953                                 // If we have any pending snapping action, then invoke it now
954                                 sp_event_context_snap_watchdog_callback(event_context->_delayed_snap_event);
955                         }
956                         break;
957                 /*case GDK_BUTTON_PRESS:
958                 case GDK_2BUTTON_PRESS:
959                 case GDK_3BUTTON_PRESS:
960                         // Snapping will be on hold if we're moving the mouse at high speeds. When starting
961                         // drawing a new shape we really should snap though.
962                         event_context->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
963                         break;
964                 */
965                 default:
966                         break;
967         }
969     return sp_event_context_virtual_item_handler(event_context, item, event);
972 gint
973 sp_event_context_virtual_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
975         gint ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
977         if (! ret) {
978                 ret = sp_event_context_virtual_root_handler(event_context, event);
979         } else {
980                 set_event_location(event_context->desktop, event);
981         }
983         return ret;
986 /**
987  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
988  */
989 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
991     if (event->type != GDK_MOTION_NOTIFY) {
992         return;
993     }
995     Geom::Point const button_w(event->button.x, event->button.y);
996     Geom::Point const button_dt(desktop->w2d(button_w));
997     desktop->setPosition(button_dt);
998     desktop->set_coordinate_status(button_dt);
1001 //-------------------------------------------------------------------
1002 /**
1003  * Create popup menu and tell Gtk to show it.
1004  */
1005 void
1006 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
1008     GtkWidget *menu;
1010     /* fixme: This is not what I want but works for now (Lauris) */
1011     if (event->type == GDK_KEY_PRESS) {
1012         item = sp_desktop_selection(desktop)->singleItem();
1013     }
1014     menu = sp_ui_context_menu(desktop, item);
1015     gtk_widget_show(menu);
1017     switch (event->type) {
1018         case GDK_BUTTON_PRESS:
1019             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
1020             break;
1021         case GDK_KEY_PRESS:
1022             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
1023             break;
1024         default:
1025             break;
1026     }
1029 /**
1030  * Show tool context specific modifier tip.
1031  */
1032 void
1033 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
1034         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
1035         gchar const *alt_tip)
1037     guint keyval = get_group0_keyval(&event->key);
1039     bool ctrl = ctrl_tip && (MOD__CTRL
1040             || (keyval == GDK_Control_L)
1041             || (keyval == GDK_Control_R));
1042     bool shift = shift_tip
1043         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
1044     bool alt = alt_tip
1045         && (MOD__ALT
1046                 || (keyval == GDK_Alt_L)
1047                 || (keyval == GDK_Alt_R)
1048                 || (keyval == GDK_Meta_L)
1049                 || (keyval == GDK_Meta_R));
1051     gchar *tip = g_strdup_printf("%s%s%s%s%s",
1052                                  ( ctrl ? ctrl_tip : "" ),
1053                                  ( ctrl && (shift || alt) ? "; " : "" ),
1054                                  ( shift ? shift_tip : "" ),
1055                                  ( (ctrl || shift) && alt ? "; " : "" ),
1056                                  ( alt ? alt_tip : "" ));
1058     if (strlen(tip) > 0) {
1059         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
1060     }
1062     g_free(tip);
1065 /**
1066  * Return the keyval corresponding to the key event in group 0, i.e.,
1067  * in the main (English) layout.
1068  *
1069  * Use this instead of simply event->keyval, so that your keyboard shortcuts
1070  * work regardless of layouts (e.g., in Cyrillic).
1071  */
1072 guint
1073 get_group0_keyval(GdkEventKey *event)
1075     guint keyval = 0;
1076     gdk_keymap_translate_keyboard_state(
1077             gdk_keymap_get_for_display(gdk_display_get_default()),
1078             event->hardware_keycode,
1079             (GdkModifierType) event->state,
1080             0   /*event->key.group*/,
1081             &keyval, NULL, NULL, NULL);
1082     return keyval;
1085 /**
1086  * Returns item at point p in desktop.
1087  *
1088  * If state includes alt key mask, cyclically selects under; honors
1089  * into_groups.
1090  */
1091 SPItem *
1092 sp_event_context_find_item (SPDesktop *desktop, Geom::Point const &p,
1093         bool select_under, bool into_groups)
1095     SPItem *item;
1097     if (select_under) {
1098         SPItem *selected_at_point =
1099             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1100         item = desktop->item_at_point(p, into_groups, selected_at_point);
1101         if (item == NULL) { // we may have reached bottom, flip over to the top
1102             item = desktop->item_at_point(p, into_groups, NULL);
1103         }
1104     } else
1105         item = desktop->item_at_point(p, into_groups, NULL);
1107     return item;
1110 /**
1111  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1112  *
1113  * Honors into_groups.
1114  */
1115 SPItem *
1116 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, Geom::Point const &p)
1118     GSList *temp = NULL;
1119     temp = g_slist_prepend (temp, item);
1120     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1121     g_slist_free (temp);
1123     return item_at_point;
1126 ShapeEditor *
1127 sp_event_context_get_shape_editor (SPEventContext *ec)
1129     return ec->shape_editor;
1132 void
1133 event_context_print_event_info(GdkEvent *event, bool print_return) {
1134     switch (event->type) {
1135         case GDK_BUTTON_PRESS:
1136             g_print ("GDK_BUTTON_PRESS");
1137             break;
1138         case GDK_2BUTTON_PRESS:
1139             g_print ("GDK_2BUTTON_PRESS");
1140             break;
1141         case GDK_3BUTTON_PRESS:
1142             g_print ("GDK_3BUTTON_PRESS");
1143             break;
1145         case GDK_MOTION_NOTIFY:
1146             g_print ("GDK_MOTION_NOTIFY");
1147             break;
1148         case GDK_ENTER_NOTIFY:
1149             g_print ("GDK_ENTER_NOTIFY");
1150             break;
1152         case GDK_LEAVE_NOTIFY:
1153             g_print ("GDK_LEAVE_NOTIFY");
1154             break;
1155         case GDK_BUTTON_RELEASE:
1156             g_print ("GDK_BUTTON_RELEASE");
1157             break;
1159         case GDK_KEY_PRESS:
1160             g_print ("GDK_KEY_PRESS: %d", get_group0_keyval(&event->key));
1161             break;
1162         case GDK_KEY_RELEASE:
1163             g_print ("GDK_KEY_RELEASE: %d", get_group0_keyval(&event->key));
1164             break;
1165         default:
1166             //g_print ("even type not recognized");
1167             break;
1168     }
1170     if (print_return) {
1171         g_print ("\n");
1172     }
1175 void sp_event_context_snap_delay_handler(SPEventContext *ec, SPItem* const item, SPKnot* const knot, GdkEventMotion *event, DelayedSnapEvent::DelayedSnapEventOrigin origin)
1177         static guint32 prev_time;
1178         static boost::optional<Geom::Point> prev_pos;
1180         // Snapping occurs when dragging with the left mouse button down, or when hovering e.g. in the pen tool with left mouse button up
1181     bool const c1 = event->state & GDK_BUTTON2_MASK; // We shouldn't hold back any events when other mouse buttons have been
1182     bool const c2 = event->state & GDK_BUTTON3_MASK; // pressed, e.g. when scrolling with the middle mouse button; if we do then
1183                                                                                                      // Inkscape will get stuck in an unresponsive state
1184     bool const c3 = tools_isactive(ec->desktop, TOOLS_CALLIGRAPHIC);
1185                                                      // The snap delay will repeat the last motion event, which will lead to
1186                                                      // erroneous points in the calligraphy context. And because we don't snap
1187                                                      // in this context, we might just as well disable the snap delay all together
1188  
1189     if (c1 || c2 || c3) {
1190         // Make sure that we don't send any pending snap events to a context if we know in advance
1191         // that we're not going to snap any way (e.g. while scrolling with middle mouse button)
1192         // Any motion event might affect the state of the context, leading to unexpected behavior
1193         sp_event_context_discard_delayed_snap_event(ec);
1194     } else if (ec->desktop && ec->desktop->namedview->snap_manager.snapprefs.getSnapEnabledGlobally()) {
1195                 // Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
1196                 // i.e. snap when we're at stand still. A speed threshold enforces snapping for tablets, which might never
1197                 // be fully at stand still and might keep spitting out motion events.
1198                 ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
1200                 Geom::Point event_pos(event->x, event->y);
1201                 guint32 event_t = gdk_event_get_time ( (GdkEvent *) event );
1203                 if (prev_pos) {
1204                         Geom::Coord dist = Geom::L2(event_pos - *prev_pos);
1205                         guint32 delta_t = event_t - prev_time;
1206                         gdouble speed = delta_t > 0 ? dist/delta_t : 1000;
1207                         //std::cout << "Mouse speed = " << speed << " px/msec " << std::endl;
1208                         if (speed > 0.02) { // Jitter threshold, might be needed for tablets
1209                                 // We're moving fast, so postpone any snapping until the next GDK_MOTION_NOTIFY event. We
1210                                 // will keep on postponing the snapping as long as the speed is high.
1211                                 // We must snap at some point in time though, so set a watchdog timer at some time from
1212                                 // now, just in case there's no future motion event that drops under the speed limit (when
1213                                 // stopping abruptly)
1214                                 delete ec->_delayed_snap_event;
1215                                 ec->_delayed_snap_event = new DelayedSnapEvent(ec, item, knot, event, origin); // watchdog is reset, i.e. pushed forward in time
1216                                 // If the watchdog expires before a new motion event is received, we will snap (as explained
1217                                 // above). This means however that when the timer is too short, we will always snap and that the
1218                                 // speed threshold is ineffective. In the extreme case the delay is set to zero, and snapping will
1219                                 // be immediate, as it used to be in the old days ;-).
1220                         } else { // Speed is very low, so we're virtually at stand still
1221                                 // But if we're really standing still, then we should snap now. We could use some low-pass filtering,
1222                                 // otherwise snapping occurs for each jitter movement. For this filtering we'll leave the watchdog to expire,
1223                                 // snap, and set a new watchdog again.
1224                                 if (ec->_delayed_snap_event == NULL) { // no watchdog has been set
1225                                         // it might have already expired, so we'll set a new one; the snapping frequency will be limited by this
1226                                         ec->_delayed_snap_event = new DelayedSnapEvent(ec, item, knot, event, origin);
1227                                 } // else: watchdog has been set before and we'll wait for it to expire
1228                         }
1229                 } else {
1230                         // This is the first GDK_MOTION_NOTIFY event, so postpone snapping and set the watchdog
1231                         g_assert(ec->_delayed_snap_event == NULL);
1232                         ec->_delayed_snap_event = new DelayedSnapEvent(ec, item, knot, event, origin);
1233                 }
1235                 prev_pos = event_pos;
1236                 prev_time = event_t;
1237     }
1240 gboolean sp_event_context_snap_watchdog_callback(gpointer data)
1242         // Snap NOW! For this the "postponed" flag will be reset and the last motion event will be repeated
1243         DelayedSnapEvent *dse = reinterpret_cast<DelayedSnapEvent*>(data);
1245         if (dse == NULL) {
1246                 // This might occur when this method is called directly, i.e. not through the timer
1247                 // E.g. on GDK_BUTTON_RELEASE in sp_event_context_root_handler()
1248                 return FALSE;
1249         }
1251         SPEventContext *ec = dse->getEventContext();
1252         if (ec == NULL || ec->desktop == NULL) {
1253                 return false;
1254         }
1256         SPDesktop *dt = ec->desktop;
1257         dt->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
1259         switch (dse->getOrigin()) {
1260                 case DelayedSnapEvent::EVENTCONTEXT_ROOT_HANDLER:
1261                         sp_event_context_virtual_root_handler(ec, dse->getEvent());
1262                         break;
1263                 case DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER:
1264                         {
1265                                 SPItem* item = NULL;
1266                                 item = dse->getItem();
1267                                 if (item && SP_IS_ITEM(item)) {
1268                                         sp_event_context_virtual_item_handler(ec, item, dse->getEvent());
1269                                 }
1270                         }
1271                         break;
1272                 case DelayedSnapEvent::KNOT_HANDLER:
1273                         {
1274                                 SPKnot* knot = dse->getKnot();
1275                                 if (knot && SP_IS_KNOT(knot)) {
1276                                         sp_knot_handler_request_position(dse->getEvent(), knot);
1277                                 }
1278                         }
1279                         break;
1280                 default:
1281                         g_warning("Origin of snap-delay event has not been defined!;");
1282                         break;
1283         }
1285         ec->_delayed_snap_event = NULL;
1286         delete dse;
1288         return FALSE; //Kills the timer and stops it from executing this callback over and over again.
1291 void sp_event_context_discard_delayed_snap_event(SPEventContext *ec)
1293         delete ec->_delayed_snap_event;
1294         ec->_delayed_snap_event = NULL;
1299 /*
1300   Local Variables:
1301   mode:c++
1302   c-file-style:"stroustrup"
1303   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1304   indent-tabs-mode:nil
1305   fill-column:99
1306   End:
1307 */
1308 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :