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