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 "selection.h"
47 #include "file.h"
48 #include "interface.h"
49 #include "macros.h"
50 #include "tools-switch.h"
51 #include "preferences.h"
52 #include "message-context.h"
53 #include "gradient-drag.h"
54 #include "object-edit.h"
55 #include "attributes.h"
56 #include "rubberband.h"
57 #include "selcue.h"
58 #include "node-context.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 }
146 /**
147 * Callback to free and null member variables of SPEventContext object.
148 */
149 static void
150 sp_event_context_dispose(GObject *object)
151 {
152 SPEventContext *ec;
154 ec = SP_EVENT_CONTEXT(object);
156 if (ec->_message_context) {
157 delete ec->_message_context;
158 }
160 if (ec->cursor != NULL) {
161 gdk_cursor_unref(ec->cursor);
162 ec->cursor = NULL;
163 }
165 if (ec->desktop) {
166 ec->desktop = NULL;
167 }
169 if (ec->pref_observer) {
170 delete ec->pref_observer;
171 }
173 G_OBJECT_CLASS(parent_class)->dispose(object);
174 }
176 /**
177 * Recreates and draws cursor on desktop related to SPEventContext.
178 */
179 void
180 sp_event_context_update_cursor(SPEventContext *ec)
181 {
182 GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
183 if (w->window) {
184 /* fixme: */
185 if (ec->cursor_shape) {
186 GdkBitmap *bitmap = NULL;
187 GdkBitmap *mask = NULL;
188 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
189 if ((bitmap != NULL) && (mask != NULL)) {
190 if (ec->cursor)
191 gdk_cursor_unref (ec->cursor);
192 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
193 &w->style->black,
194 &w->style->white,
195 ec->hot_x, ec->hot_y);
196 g_object_unref (bitmap);
197 g_object_unref (mask);
198 }
199 }
200 gdk_window_set_cursor(w->window, ec->cursor);
201 gdk_flush();
202 }
203 ec->desktop->waiting_cursor = false;
204 }
206 /**
207 * Callback that gets called on initialization of SPEventContext object.
208 * Redraws mouse cursor, at the moment.
209 */
210 static void
211 sp_event_context_private_setup(SPEventContext *ec)
212 {
213 sp_event_context_update_cursor(ec);
214 }
216 /**
217 * \brief Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
218 */
219 gint gobble_key_events(guint keyval, gint mask)
220 {
221 GdkEvent *event_next;
222 gint i = 0;
224 event_next = gdk_event_get();
225 // while the next event is also a key notify with the same keyval and mask,
226 while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
227 && event_next->key.keyval == keyval
228 && (!mask || (event_next->key.state & mask))) {
229 if (event_next->type == GDK_KEY_PRESS)
230 i ++;
231 // kill it
232 gdk_event_free(event_next);
233 // get next
234 event_next = gdk_event_get();
235 }
236 // otherwise, put it back onto the queue
237 if (event_next) gdk_event_put(event_next);
239 return i;
240 }
242 /**
243 * \brief Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
244 */
245 gint gobble_motion_events(gint mask)
246 {
247 GdkEvent *event_next;
248 gint i = 0;
250 event_next = gdk_event_get();
251 // while the next event is also a key notify with the same keyval and mask,
252 while (event_next && event_next->type == GDK_MOTION_NOTIFY
253 && (event_next->motion.state & mask)) {
254 // kill it
255 gdk_event_free(event_next);
256 // get next
257 event_next = gdk_event_get();
258 i ++;
259 }
260 // otherwise, put it back onto the queue
261 if (event_next) gdk_event_put(event_next);
263 return i;
264 }
266 /**
267 * Toggles current tool between active tool and selector tool.
268 * Subroutine of sp_event_context_private_root_handler().
269 */
270 static void
271 sp_toggle_selector(SPDesktop *dt)
272 {
273 if (!dt->event_context) return;
275 if (tools_isactive(dt, TOOLS_SELECT)) {
276 if (selector_toggled) {
277 if (switch_selector_to) tools_switch (dt, switch_selector_to);
278 selector_toggled = FALSE;
279 } else return;
280 } else {
281 selector_toggled = TRUE;
282 switch_selector_to = tools_active(dt);
283 tools_switch (dt, TOOLS_SELECT);
284 }
285 }
287 /**
288 * Toggles current tool between active tool and dropper tool.
289 * Subroutine of sp_event_context_private_root_handler().
290 */
291 static void
292 sp_toggle_dropper(SPDesktop *dt)
293 {
294 if (!dt->event_context) return;
296 if (tools_isactive(dt, TOOLS_DROPPER)) {
297 if (dropper_toggled) {
298 if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
299 dropper_toggled = FALSE;
300 } else return;
301 } else {
302 dropper_toggled = TRUE;
303 switch_dropper_to = tools_active(dt);
304 tools_switch (dt, TOOLS_DROPPER);
305 }
306 }
308 /**
309 * Calculates and keeps track of scroll acceleration.
310 * Subroutine of sp_event_context_private_root_handler().
311 */
312 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
313 {
314 guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
316 /* key pressed within 500ms ? (1/2 second) */
317 if (time_diff > 500 || event->key.keyval != scroll_keyval) {
318 scroll_multiply = 1; // abort acceleration
319 } else {
320 scroll_multiply += acceleration; // continue acceleration
321 }
323 scroll_event_time = ((GdkEventKey *) event)->time;
324 scroll_keyval = event->key.keyval;
326 return scroll_multiply;
327 }
329 /**
330 * Main event dispatch, gets called from Gdk.
331 */
332 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
333 {
334 static Geom::Point button_w;
335 static unsigned int panning = 0;
336 static unsigned int zoom_rb = 0;
338 SPDesktop *desktop = event_context->desktop;
339 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
341 /// @todo REmove redundant /value in preference keys
342 tolerance = prefs->getIntLimited(
343 "/options/dragtolerance/value", 0, 0, 100);
344 double const zoom_inc = prefs->getDoubleLimited(
345 "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
346 double const acceleration = prefs->getDoubleLimited(
347 "/options/scrollingacceleration/value", 0, 0, 6);
348 int const key_scroll = prefs->getIntLimited(
349 "/options/keyscroll/value", 10, 0, 1000);
350 int const wheel_scroll = prefs->getIntLimited(
351 "/options/wheelscroll/value", 40, 0, 1000);
353 gint ret = FALSE;
355 switch (event->type) {
356 case GDK_2BUTTON_PRESS:
357 if (panning) {
358 panning = 0;
359 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
360 event->button.time);
361 ret = TRUE;
362 } else {
363 /* sp_desktop_dialog(); */
364 }
365 break;
366 case GDK_BUTTON_PRESS:
368 // save drag origin
369 xp = (gint) event->button.x;
370 yp = (gint) event->button.y;
371 within_tolerance = true;
373 button_w = Geom::Point(event->button.x, event->button.y);
375 switch (event->button.button) {
376 case 1:
377 if (event_context->space_panning) {
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 panning = 2;
390 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
391 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
392 NULL, event->button.time-1);
393 }
394 ret = TRUE;
395 break;
396 case 3:
397 if (event->button.state & GDK_SHIFT_MASK
398 || event->button.state & GDK_CONTROL_MASK) {
399 panning = 3;
400 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
401 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
402 NULL, event->button.time);
403 ret = TRUE;
404 } else {
405 sp_event_root_menu_popup(desktop, NULL, event);
406 }
407 break;
408 default:
409 break;
410 }
411 break;
412 case GDK_MOTION_NOTIFY:
413 if (panning) {
414 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
415 || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
416 || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
417 ) {
418 /* Gdk seems to lose button release for us sometimes :-( */
419 panning = 0;
420 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
421 event->button.time);
422 ret = TRUE;
423 } else {
424 if ( within_tolerance
425 && ( abs( (gint) event->motion.x - xp ) < tolerance )
426 && ( abs( (gint) event->motion.y - yp ) < tolerance ))
427 {
428 // do not drag if we're within tolerance from origin
429 break;
430 }
431 // Once the user has moved farther than tolerance from
432 // the original location (indicating they intend to move
433 // the object, not click), then always process the motion
434 // notify coordinates as given (no snapping back to origin)
435 within_tolerance = false;
437 // gobble subsequent motion events to prevent "sticking"
438 // when scrolling is slow
439 gobble_motion_events(panning == 2 ?
440 GDK_BUTTON2_MASK :
441 (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
443 Geom::Point const motion_w(event->motion.x, event->motion.y);
444 Geom::Point const moved_w( motion_w - button_w );
445 event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
446 ret = TRUE;
447 }
448 } else if (zoom_rb) {
449 Geom::Point const motion_w(event->motion.x, event->motion.y);
450 Geom::Point const motion_dt(desktop->w2d(motion_w));
452 if ( within_tolerance
453 && ( abs( (gint) event->motion.x - xp ) < tolerance )
454 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
455 break; // do not drag if we're within tolerance from origin
456 }
457 // Once the user has moved farther than tolerance from the original location
458 // (indicating they intend to move the object, not click), then always process the
459 // motion notify coordinates as given (no snapping back to origin)
460 within_tolerance = false;
462 if (Inkscape::Rubberband::get(desktop)->is_started()) {
463 Inkscape::Rubberband::get(desktop)->move(motion_dt);
464 } else {
465 Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
466 }
467 if (zoom_rb == 2)
468 gobble_motion_events(GDK_BUTTON2_MASK);
469 }
470 break;
471 case GDK_BUTTON_RELEASE:
472 xp = yp = 0;
473 if (within_tolerance && (panning || zoom_rb)) {
474 zoom_rb = 0;
475 if (panning) {
476 panning = 0;
477 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
478 event->button.time);
479 }
480 Geom::Point const event_w(event->button.x, event->button.y);
481 Geom::Point const event_dt(desktop->w2d(event_w));
482 desktop->zoom_relative_keep_point(event_dt,
483 (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
484 desktop->updateNow();
485 ret = TRUE;
486 } else if (panning == event->button.button) {
487 panning = 0;
488 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
489 event->button.time);
491 // in slow complex drawings, some of the motion events are lost;
492 // to make up for this, we scroll it once again to the button-up event coordinates
493 // (i.e. canvas will always get scrolled all the way to the mouse release point,
494 // even if few intermediate steps were visible)
495 Geom::Point const motion_w(event->button.x, event->button.y);
496 Geom::Point const moved_w( motion_w - button_w );
497 event_context->desktop->scroll_world(moved_w);
498 desktop->updateNow();
499 ret = TRUE;
500 } else if (zoom_rb == event->button.button) {
501 zoom_rb = 0;
502 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
503 Inkscape::Rubberband::get(desktop)->stop();
504 if (b && !within_tolerance) {
505 desktop->set_display_area(*b, 10);
506 }
507 ret = TRUE;
508 }
509 break;
510 case GDK_KEY_PRESS:
511 switch (get_group0_keyval(&event->key)) {
512 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
513 // in the editing window). So we resteal them back and run our regular shortcut
514 // invoker on them.
515 unsigned int shortcut;
516 case GDK_Tab:
517 case GDK_ISO_Left_Tab:
518 case GDK_F1:
519 shortcut = get_group0_keyval(&event->key);
520 if (event->key.state & GDK_SHIFT_MASK)
521 shortcut |= SP_SHORTCUT_SHIFT_MASK;
522 if (event->key.state & GDK_CONTROL_MASK)
523 shortcut |= SP_SHORTCUT_CONTROL_MASK;
524 if (event->key.state & GDK_MOD1_MASK)
525 shortcut |= SP_SHORTCUT_ALT_MASK;
526 ret = sp_shortcut_invoke(shortcut, desktop);
527 break;
529 case GDK_D:
530 case GDK_d:
531 if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
532 sp_toggle_dropper(desktop);
533 ret = TRUE;
534 }
535 break;
536 case GDK_Q:
537 case GDK_q:
538 if (desktop->quick_zoomed()) {
539 ret = TRUE;
540 }
541 if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
542 desktop->zoom_quick(true);
543 ret = TRUE;
544 }
545 break;
546 case GDK_W:
547 case GDK_w:
548 case GDK_F4:
549 /* Close view */
550 if (MOD__CTRL_ONLY) {
551 sp_ui_close_view(NULL);
552 ret = TRUE;
553 }
554 break;
555 case GDK_Left: // Ctrl Left
556 case GDK_KP_Left:
557 case GDK_KP_4:
558 if (MOD__CTRL_ONLY) {
559 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
560 gobble_key_events(get_group0_keyval(&event->key),
561 GDK_CONTROL_MASK);
562 event_context->desktop->scroll_world(i, 0);
563 ret = TRUE;
564 }
565 break;
566 case GDK_Up: // Ctrl Up
567 case GDK_KP_Up:
568 case GDK_KP_8:
569 if (MOD__CTRL_ONLY) {
570 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
571 gobble_key_events(get_group0_keyval(&event->key),
572 GDK_CONTROL_MASK);
573 event_context->desktop->scroll_world(0, i);
574 ret = TRUE;
575 }
576 break;
577 case GDK_Right: // Ctrl Right
578 case GDK_KP_Right:
579 case GDK_KP_6:
580 if (MOD__CTRL_ONLY) {
581 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
582 gobble_key_events(get_group0_keyval(&event->key),
583 GDK_CONTROL_MASK);
584 event_context->desktop->scroll_world(-i, 0);
585 ret = TRUE;
586 }
587 break;
588 case GDK_Down: // Ctrl Down
589 case GDK_KP_Down:
590 case GDK_KP_2:
591 if (MOD__CTRL_ONLY) {
592 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
593 gobble_key_events(get_group0_keyval(&event->key),
594 GDK_CONTROL_MASK);
595 event_context->desktop->scroll_world(0, -i);
596 ret = TRUE;
597 }
598 break;
599 case GDK_F10:
600 if (MOD__SHIFT_ONLY) {
601 sp_event_root_menu_popup(desktop, NULL, event);
602 ret= TRUE;
603 }
604 break;
605 case GDK_space:
606 if (prefs->getBool("/options/spacepans/value")) {
607 event_context->space_panning = true;
608 event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
609 ret= TRUE;
610 } else {
611 sp_toggle_selector(desktop);
612 ret= TRUE;
613 }
614 break;
615 case GDK_z:
616 case GDK_Z:
617 if (MOD__ALT_ONLY) {
618 desktop->zoom_grab_focus();
619 ret = TRUE;
620 }
621 break;
622 default:
623 break;
624 }
625 break;
626 case GDK_KEY_RELEASE:
627 switch (get_group0_keyval(&event->key)) {
628 case GDK_space:
629 if (event_context->space_panning) {
630 event_context->space_panning = false;
631 event_context->_message_context->clear();
632 if (panning == 1) {
633 panning = 0;
634 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
635 event->key.time);
636 desktop->updateNow();
637 }
638 ret= TRUE;
639 }
640 break;
641 case GDK_Q:
642 case GDK_q:
643 if (desktop->quick_zoomed()) {
644 desktop->zoom_quick(false);
645 ret = TRUE;
646 }
647 break;
648 default:
649 break;
650 }
651 break;
652 case GDK_SCROLL:
653 {
654 bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
655 bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
656 /* shift + wheel, pan left--right */
657 if (event->scroll.state & GDK_SHIFT_MASK) {
658 switch (event->scroll.direction) {
659 case GDK_SCROLL_UP:
660 desktop->scroll_world(wheel_scroll, 0);
661 break;
662 case GDK_SCROLL_DOWN:
663 desktop->scroll_world(-wheel_scroll, 0);
664 break;
665 default:
666 break;
667 }
669 /* ctrl + wheel, zoom in--out */
670 } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
671 double rel_zoom;
672 switch (event->scroll.direction) {
673 case GDK_SCROLL_UP:
674 rel_zoom = zoom_inc;
675 break;
676 case GDK_SCROLL_DOWN:
677 rel_zoom = 1 / zoom_inc;
678 break;
679 default:
680 rel_zoom = 0.0;
681 break;
682 }
683 if (rel_zoom != 0.0) {
684 Geom::Point const scroll_dt = desktop->point();
685 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
686 }
688 /* no modifier, pan up--down (left--right on multiwheel mice?) */
689 } else {
690 switch (event->scroll.direction) {
691 case GDK_SCROLL_UP:
692 desktop->scroll_world(0, wheel_scroll);
693 break;
694 case GDK_SCROLL_DOWN:
695 desktop->scroll_world(0, -wheel_scroll);
696 break;
697 case GDK_SCROLL_LEFT:
698 desktop->scroll_world(wheel_scroll, 0);
699 break;
700 case GDK_SCROLL_RIGHT:
701 desktop->scroll_world(-wheel_scroll, 0);
702 break;
703 }
704 }
705 break;
706 }
707 default:
708 break;
709 }
711 return ret;
712 }
714 /**
715 * Handles item specific events. Gets called from Gdk.
716 *
717 * Only reacts to right mouse button at the moment.
718 * \todo Fixme: do context sensitive popup menu on items.
719 */
720 gint
721 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
722 {
723 int ret = FALSE;
725 switch (event->type) {
726 case GDK_BUTTON_PRESS:
727 if ((event->button.button == 3)
728 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
729 sp_event_root_menu_popup(ec->desktop, item, event);
730 ret = TRUE;
731 }
732 break;
733 default:
734 break;
735 }
737 return ret;
738 }
740 /**
741 * @brief An observer that relays pref changes to the derived classes
742 */
743 class ToolPrefObserver : public Inkscape::Preferences::Observer {
744 public:
745 ToolPrefObserver(Glib::ustring const &path, SPEventContext *ec) :
746 Inkscape::Preferences::Observer(path),
747 _ec(ec) {}
748 virtual void notify(Inkscape::Preferences::Entry const &val)
749 {
750 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set) {
751 ((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set(_ec,
752 const_cast<Inkscape::Preferences::Entry*>(&val));
753 }
754 }
755 private:
756 SPEventContext * const _ec;
757 };
759 /**
760 * Creates new SPEventContext object and calls its virtual setup() function.
761 * @todo This is bogus. pref_path should be a private property of the inheriting objects.
762 */
763 SPEventContext *
764 sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path, unsigned int key)
765 {
766 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
767 g_return_val_if_fail(desktop != NULL, NULL);
769 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
771 ec->desktop = desktop;
772 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
773 ec->key = key;
774 ec->pref_observer = NULL;
776 if (pref_path) {
777 ec->pref_observer = new ToolPrefObserver(pref_path, ec);
779 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
780 prefs->addObserver(*(ec->pref_observer));
781 }
783 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
784 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
786 return ec;
787 }
789 /**
790 * Finishes SPEventContext.
791 */
792 void
793 sp_event_context_finish(SPEventContext *ec)
794 {
795 g_return_if_fail(ec != NULL);
796 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
798 ec->enableSelectionCue(false);
800 if (ec->next) {
801 g_warning("Finishing event context with active link\n");
802 }
804 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
805 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
806 }
808 //-------------------------------member functions
810 /**
811 * Enables/disables the SPEventContext's SelCue.
812 */
813 void SPEventContext::enableSelectionCue(bool enable) {
814 if (enable) {
815 if (!_selcue) {
816 _selcue = new Inkscape::SelCue(desktop);
817 }
818 } else {
819 delete _selcue;
820 _selcue = NULL;
821 }
822 }
824 /**
825 * Enables/disables the SPEventContext's GrDrag.
826 */
827 void SPEventContext::enableGrDrag(bool enable) {
828 if (enable) {
829 if (!_grdrag) {
830 _grdrag = new GrDrag(desktop);
831 }
832 } else {
833 if (_grdrag) {
834 delete _grdrag;
835 _grdrag = NULL;
836 }
837 }
838 }
840 /**
841 * Calls virtual set() function of SPEventContext.
842 */
843 void
844 sp_event_context_read(SPEventContext *ec, gchar const *key)
845 {
846 g_return_if_fail(ec != NULL);
847 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
848 g_return_if_fail(key != NULL);
850 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
851 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
852 Inkscape::Preferences::Entry val = prefs->getEntry(
853 ec->pref_observer->observed_path + '/' + key );
854 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, &val);
855 }
856 }
858 /**
859 * Calls virtual activate() function of SPEventContext.
860 */
861 void
862 sp_event_context_activate(SPEventContext *ec)
863 {
864 g_return_if_fail(ec != NULL);
865 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
867 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
868 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
869 }
871 /**
872 * Calls virtual deactivate() function of SPEventContext.
873 */
874 void
875 sp_event_context_deactivate(SPEventContext *ec)
876 {
877 g_return_if_fail(ec != NULL);
878 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
880 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
881 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
882 }
884 /**
885 * Calls virtual root_handler(), the main event handling function.
886 */
887 gint
888 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
889 {
890 gint ret;
892 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
894 set_event_location(event_context->desktop, event);
896 return ret;
897 }
899 /**
900 * Calls virtual item_handler(), the item event handling function.
901 */
902 gint
903 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
904 {
905 gint ret;
907 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
909 if (! ret) {
910 ret = sp_event_context_root_handler(event_context, event);
911 } else {
912 set_event_location(event_context->desktop, event);
913 }
915 return ret;
916 }
918 /**
919 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
920 */
921 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
922 {
923 if (event->type != GDK_MOTION_NOTIFY) {
924 return;
925 }
927 Geom::Point const button_w(event->button.x, event->button.y);
928 Geom::Point const button_dt(desktop->w2d(button_w));
929 desktop-> setPosition (button_dt);
930 desktop->set_coordinate_status(button_dt);
931 }
933 //-------------------------------------------------------------------
934 /**
935 * Create popup menu and tell Gtk to show it.
936 */
937 void
938 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
939 {
940 GtkWidget *menu;
942 /* fixme: This is not what I want but works for now (Lauris) */
943 if (event->type == GDK_KEY_PRESS) {
944 item = sp_desktop_selection(desktop)->singleItem();
945 }
946 menu = sp_ui_context_menu(desktop, item);
947 gtk_widget_show(menu);
949 switch (event->type) {
950 case GDK_BUTTON_PRESS:
951 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
952 break;
953 case GDK_KEY_PRESS:
954 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
955 break;
956 default:
957 break;
958 }
959 }
961 /**
962 * Show tool context specific modifier tip.
963 */
964 void
965 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
966 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
967 gchar const *alt_tip)
968 {
969 guint keyval = get_group0_keyval(&event->key);
971 bool ctrl = ctrl_tip && (MOD__CTRL
972 || (keyval == GDK_Control_L)
973 || (keyval == GDK_Control_R));
974 bool shift = shift_tip
975 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
976 bool alt = alt_tip
977 && (MOD__ALT
978 || (keyval == GDK_Alt_L)
979 || (keyval == GDK_Alt_R)
980 || (keyval == GDK_Meta_L)
981 || (keyval == GDK_Meta_R));
983 gchar *tip = g_strdup_printf("%s%s%s%s%s",
984 ( ctrl ? ctrl_tip : "" ),
985 ( ctrl && (shift || alt) ? "; " : "" ),
986 ( shift ? shift_tip : "" ),
987 ( (ctrl || shift) && alt ? "; " : "" ),
988 ( alt ? alt_tip : "" ));
990 if (strlen(tip) > 0) {
991 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
992 }
994 g_free(tip);
995 }
997 /**
998 * Return the keyval corresponding to the key event in group 0, i.e.,
999 * in the main (English) layout.
1000 *
1001 * Use this instead of simply event->keyval, so that your keyboard shortcuts
1002 * work regardless of layouts (e.g., in Cyrillic).
1003 */
1004 guint
1005 get_group0_keyval(GdkEventKey *event)
1006 {
1007 guint keyval = 0;
1008 gdk_keymap_translate_keyboard_state(
1009 gdk_keymap_get_for_display(gdk_display_get_default()),
1010 event->hardware_keycode,
1011 (GdkModifierType) event->state,
1012 0 /*event->key.group*/,
1013 &keyval, NULL, NULL, NULL);
1014 return keyval;
1015 }
1017 /**
1018 * Returns item at point p in desktop.
1019 *
1020 * If state includes alt key mask, cyclically selects under; honors
1021 * into_groups.
1022 */
1023 SPItem *
1024 sp_event_context_find_item (SPDesktop *desktop, Geom::Point const &p,
1025 bool select_under, bool into_groups)
1026 {
1027 SPItem *item;
1029 if (select_under) {
1030 SPItem *selected_at_point =
1031 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1032 item = desktop->item_at_point(p, into_groups, selected_at_point);
1033 if (item == NULL) { // we may have reached bottom, flip over to the top
1034 item = desktop->item_at_point(p, into_groups, NULL);
1035 }
1036 } else
1037 item = desktop->item_at_point(p, into_groups, NULL);
1039 return item;
1040 }
1042 /**
1043 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1044 *
1045 * Honors into_groups.
1046 */
1047 SPItem *
1048 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, Geom::Point const &p)
1049 {
1050 GSList *temp = NULL;
1051 temp = g_slist_prepend (temp, item);
1052 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1053 g_slist_free (temp);
1055 return item_at_point;
1056 }
1058 ShapeEditor *
1059 sp_event_context_get_shape_editor (SPEventContext *ec)
1060 {
1061 if (SP_IS_NODE_CONTEXT(ec)) {
1062 return SP_NODE_CONTEXT(ec)->shape_editor;
1063 } else if (SP_IS_LPETOOL_CONTEXT(ec)) {
1064 return SP_LPETOOL_CONTEXT(ec)->shape_editor;
1065 } else {
1066 g_warning("ShapeEditor only exists in Node and Geometric Tool.");
1067 return NULL;
1068 }
1069 }
1071 /**
1072 * Called when SPEventContext subclass node attribute changed.
1073 */
1074 void
1075 ec_shape_event_attr_changed(Inkscape::XML::Node */*shape_repr*/, gchar const *name,
1076 gchar const */*old_value*/, gchar const */*new_value*/,
1077 bool const /*is_interactive*/, gpointer const data)
1078 {
1079 if (!name
1080 || !strcmp(name, "style")
1081 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1082 // no need to regenrate knotholder if only style changed
1083 return;
1084 }
1086 SPEventContext *ec = SP_EVENT_CONTEXT(data);
1088 if (ec->shape_knot_holder) {
1089 delete ec->shape_knot_holder;
1090 }
1091 ec->shape_knot_holder = NULL;
1093 SPDesktop *desktop = ec->desktop;
1095 SPItem *item = sp_desktop_selection(desktop)->singleItem();
1097 if (item) {
1098 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1099 }
1100 }
1103 void
1104 event_context_print_event_info(GdkEvent *event, bool print_return) {
1105 switch (event->type) {
1106 case GDK_BUTTON_PRESS:
1107 g_print ("GDK_BUTTON_PRESS");
1108 break;
1109 case GDK_2BUTTON_PRESS:
1110 g_print ("GDK_2BUTTON_PRESS");
1111 break;
1112 case GDK_3BUTTON_PRESS:
1113 g_print ("GDK_3BUTTON_PRESS");
1114 break;
1116 case GDK_MOTION_NOTIFY:
1117 g_print ("GDK_MOTION_NOTIFY");
1118 break;
1119 case GDK_ENTER_NOTIFY:
1120 g_print ("GDK_ENTER_NOTIFY");
1121 break;
1123 case GDK_LEAVE_NOTIFY:
1124 g_print ("GDK_LEAVE_NOTIFY");
1125 break;
1126 case GDK_BUTTON_RELEASE:
1127 g_print ("GDK_BUTTON_RELEASE");
1128 break;
1130 case GDK_KEY_PRESS:
1131 g_print ("GDK_KEY_PRESS: %d", get_group0_keyval(&event->key));
1132 break;
1133 case GDK_KEY_RELEASE:
1134 g_print ("GDK_KEY_RELEASE: %d", get_group0_keyval(&event->key));
1135 break;
1136 default:
1137 //g_print ("even type not recognized");
1138 break;
1139 }
1141 if (print_return) {
1142 g_print ("\n");
1143 }
1144 }
1146 /*
1147 Local Variables:
1148 mode:c++
1149 c-file-style:"stroustrup"
1150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1151 indent-tabs-mode:nil
1152 fill-column:99
1153 End:
1154 */
1155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :