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