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