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 <gdk/gdkkeysyms.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkmenu.h>
34 #include "display/sp-canvas.h"
35 #include "xml/node-event-vector.h"
36 #include "sp-cursor.h"
37 #include "shortcuts.h"
38 #include "desktop.h"
39 #include "desktop-handles.h"
40 #include "selection.h"
41 #include "file.h"
42 #include "interface.h"
43 #include "macros.h"
44 #include "tools-switch.h"
45 #include "prefs-utils.h"
46 #include "message-context.h"
47 #include "gradient-drag.h"
48 #include "object-edit.h"
49 #include "attributes.h"
50 #include "rubberband.h"
51 #include "selcue.h"
53 #include "event-context.h"
55 static void sp_event_context_class_init(SPEventContextClass *klass);
56 static void sp_event_context_init(SPEventContext *event_context);
57 static void sp_event_context_dispose(GObject *object);
59 static void sp_event_context_private_setup(SPEventContext *ec);
60 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
61 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
63 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
65 static GObjectClass *parent_class;
67 // globals for temporary switching to selector by space
68 static bool selector_toggled = FALSE;
69 static int switch_selector_to = 0;
71 static gint xp = 0, yp = 0; // where drag started
72 static gint tolerance = 0;
73 static bool within_tolerance = false;
75 // globals for keeping track of keyboard scroll events in order to accelerate
76 static guint32 scroll_event_time = 0;
77 static gdouble scroll_multiply = 1;
78 static guint scroll_keyval = 0;
80 /**
81 * Registers the SPEventContext class with Glib and returns its type number.
82 */
83 GType
84 sp_event_context_get_type(void)
85 {
86 static GType type = 0;
87 if (!type) {
88 GTypeInfo info = {
89 sizeof(SPEventContextClass),
90 NULL, NULL,
91 (GClassInitFunc) sp_event_context_class_init,
92 NULL, NULL,
93 sizeof(SPEventContext),
94 4,
95 (GInstanceInitFunc) sp_event_context_init,
96 NULL, /* value_table */
97 };
98 type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
99 }
100 return type;
101 }
103 /**
104 * Callback to set up the SPEventContext vtable.
105 */
106 static void
107 sp_event_context_class_init(SPEventContextClass *klass)
108 {
109 GObjectClass *object_class;
111 object_class = (GObjectClass *) klass;
113 parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
115 object_class->dispose = sp_event_context_dispose;
117 klass->setup = sp_event_context_private_setup;
118 klass->root_handler = sp_event_context_private_root_handler;
119 klass->item_handler = sp_event_context_private_item_handler;
120 }
122 /**
123 * Clears all SPEventContext object members.
124 */
125 static void
126 sp_event_context_init(SPEventContext *event_context)
127 {
128 event_context->desktop = NULL;
129 event_context->cursor = NULL;
130 event_context->_message_context = NULL;
131 event_context->_selcue = NULL;
132 event_context->_grdrag = NULL;
133 }
135 /**
136 * Callback to free and null member variables of SPEventContext object.
137 */
138 static void
139 sp_event_context_dispose(GObject *object)
140 {
141 SPEventContext *ec;
143 ec = SP_EVENT_CONTEXT(object);
145 if (ec->_message_context) {
146 delete ec->_message_context;
147 }
149 if (ec->cursor != NULL) {
150 gdk_cursor_unref(ec->cursor);
151 ec->cursor = NULL;
152 }
154 if (ec->desktop) {
155 ec->desktop = NULL;
156 }
158 if (ec->prefs_repr) {
159 sp_repr_remove_listener_by_data(ec->prefs_repr, ec);
160 Inkscape::GC::release(ec->prefs_repr);
161 ec->prefs_repr = NULL;
162 }
164 G_OBJECT_CLASS(parent_class)->dispose(object);
165 }
167 /**
168 * Recreates and draws cursor on desktop related to SPEventContext.
169 */
170 void
171 sp_event_context_update_cursor(SPEventContext *ec)
172 {
173 GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
174 if (w->window) {
175 GdkDisplay *display=gdk_display_get_default();
177 if (ec->cursor)
178 gdk_cursor_unref(ec->cursor);
180 ec->cursor=sp_cursor_new(
181 display,
182 ec->cursor_pixbuf,
183 ec->cursor_shape,
184 ec->hot_x,
185 ec->hot_y);
186 gdk_window_set_cursor(w->window, ec->cursor);
187 }
188 }
190 /**
191 * Callback that gets called on initialization of SPEventContext object.
192 * Redraws mouse cursor, at the moment.
193 */
194 static void
195 sp_event_context_private_setup(SPEventContext *ec)
196 {
197 sp_event_context_update_cursor(ec);
198 }
200 /**
201 * \brief Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
202 */
203 gint gobble_key_events(guint keyval, gint mask)
204 {
205 GdkEvent *event_next;
206 gint i = 0;
208 event_next = gdk_event_get();
209 // while the next event is also a key notify with the same keyval and mask,
210 while (event_next && event_next->type == GDK_KEY_PRESS
211 && event_next->key.keyval == keyval
212 && (event_next->key.state & mask)) {
213 // kill it
214 gdk_event_free(event_next);
215 // get next
216 event_next = gdk_event_get();
217 i ++;
218 }
219 // otherwise, put it back onto the queue
220 if (event_next) gdk_event_put(event_next);
222 return i;
223 }
225 /**
226 * \brief Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
227 */
228 gint gobble_motion_events(gint mask)
229 {
230 GdkEvent *event_next;
231 gint i = 0;
233 event_next = gdk_event_get();
234 // while the next event is also a key notify with the same keyval and mask,
235 while (event_next && event_next->type == GDK_MOTION_NOTIFY
236 && (event_next->motion.state & mask)) {
237 // kill it
238 gdk_event_free(event_next);
239 // get next
240 event_next = gdk_event_get();
241 i ++;
242 }
243 // otherwise, put it back onto the queue
244 if (event_next) gdk_event_put(event_next);
246 return i;
247 }
249 /**
250 * Toggles current tool between active tool and selector tool.
251 * Subroutine of sp_event_context_private_root_handler().
252 */
253 static void
254 sp_toggle_selector(SPDesktop *dt)
255 {
256 if (!dt->event_context) return;
258 if (tools_isactive(dt, TOOLS_SELECT)) {
259 if (selector_toggled) {
260 if (switch_selector_to) tools_switch (dt, switch_selector_to);
261 selector_toggled = FALSE;
262 } else return;
263 } else {
264 selector_toggled = TRUE;
265 switch_selector_to = tools_active(dt);
266 tools_switch (dt, TOOLS_SELECT);
267 }
268 }
270 /**
271 * Calculates and keeps track of scroll acceleration.
272 * Subroutine of sp_event_context_private_root_handler().
273 */
274 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration)
275 {
276 guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
278 /* key pressed within 500ms ? (1/2 second) */
279 if (time_diff > 500 || event->key.keyval != scroll_keyval) {
280 scroll_multiply = 1;
281 } else {
282 scroll_multiply += acceleration;
283 }
285 scroll_event_time = ((GdkEventKey *) event)->time;
286 scroll_keyval = event->key.keyval;
288 return scroll_multiply;
289 }
291 // This is a hack that is necessary because when middle-clicking too fast,
292 // button_press events come for all clicks but there's button_release only
293 // for the first one. So after a release, we must prohibit the next grab for
294 // some time, or the grab will be stuck. Perhaps this is caused by some
295 // wrong handling of events among contexts and not by a GDK bug;
296 // if someone can fix this properly this would be great.
297 static gint dontgrab = 0;
298 static bool
299 grab_allow_again()
300 {
301 dontgrab--;
302 if (dontgrab < 0) dontgrab = 0;
303 return FALSE; // so that it is only called once
304 }
306 /**
307 * Main event dispatch, gets called from Gdk.
308 */
309 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
310 {
311 static NR::Point button_w;
312 static unsigned int panning = 0;
313 static unsigned int zoom_rb = 0;
315 SPDesktop *desktop = event_context->desktop;
317 tolerance = prefs_get_int_attribute_limited(
318 "options.dragtolerance","value", 0, 0, 100);
319 double const zoom_inc = prefs_get_double_attribute_limited(
320 "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
321 double const acceleration = prefs_get_double_attribute_limited(
322 "options.scrollingacceleration", "value", 0, 0, 6);
323 int const key_scroll = prefs_get_int_attribute_limited(
324 "options.keyscroll", "value", 10, 0, 1000);
325 int const wheel_scroll = prefs_get_int_attribute_limited(
326 "options.wheelscroll", "value", 40, 0, 1000);
328 gint ret = FALSE;
330 switch (event->type) {
331 case GDK_2BUTTON_PRESS:
332 if (panning) {
333 panning = 0;
334 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
335 event->button.time);
336 ret = TRUE;
337 } else {
338 /* sp_desktop_dialog(); */
339 }
340 break;
341 case GDK_BUTTON_PRESS:
343 // save drag origin
344 xp = (gint) event->button.x;
345 yp = (gint) event->button.y;
346 within_tolerance = true;
348 switch (event->button.button) {
349 case 2:
351 if (dontgrab)
352 // double-click, still not permitted to grab;
353 // increase the counter to guard against triple click
354 {
355 dontgrab ++;
356 gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
357 break;
358 }
360 button_w = NR::Point(event->button.x,
361 event->button.y);
362 if (event->button.state == GDK_SHIFT_MASK) {
363 zoom_rb = 2;
364 } else {
365 panning = 2;
366 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
367 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
368 NULL, event->button.time-1);
369 }
370 ret = TRUE;
371 break;
372 case 3:
373 if (event->button.state & GDK_SHIFT_MASK
374 || event->button.state & GDK_CONTROL_MASK) {
375 button_w = NR::Point(event->button.x,
376 event->button.y);
377 panning = 3;
378 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
379 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
380 NULL, event->button.time);
381 ret = TRUE;
382 } else {
383 sp_event_root_menu_popup(desktop, NULL, event);
384 }
385 break;
386 default:
387 break;
388 }
389 break;
390 case GDK_MOTION_NOTIFY:
391 if (panning) {
392 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
393 || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))) {
394 /* Gdk seems to lose button release for us sometimes :-( */
395 panning = 0;
396 dontgrab = 0;
397 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
398 event->button.time);
399 ret = TRUE;
400 } else {
401 if ( within_tolerance
402 && ( abs( (gint) event->motion.x - xp ) < tolerance )
403 && ( abs( (gint) event->motion.y - yp ) < tolerance ))
404 {
405 // do not drag if we're within tolerance from origin
406 break;
407 }
408 // Once the user has moved farther than tolerance from
409 // the original location (indicating they intend to move
410 // the object, not click), then always process the motion
411 // notify coordinates as given (no snapping back to origin)
412 within_tolerance = false;
414 // gobble subsequent motion events to prevent "sticking"
415 // when scrolling is slow
416 gobble_motion_events(panning == 2 ?
417 GDK_BUTTON2_MASK : GDK_BUTTON3_MASK);
419 NR::Point const motion_w(event->motion.x,
420 event->motion.y);
421 NR::Point const moved_w( motion_w - button_w );
422 event_context->desktop->scroll_world(moved_w);
423 ret = TRUE;
424 }
425 } else if (zoom_rb) {
426 NR::Point const motion_w(event->motion.x, event->motion.y);
427 NR::Point const motion_dt(desktop->w2d(motion_w));
429 if ( within_tolerance
430 && ( abs( (gint) event->motion.x - xp ) < tolerance )
431 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
432 break; // do not drag if we're within tolerance from origin
433 }
435 if (within_tolerance) {
436 Inkscape::Rubberband::get()->start(desktop, motion_dt);
437 } else {
438 Inkscape::Rubberband::get()->move(motion_dt);
439 }
441 // Once the user has moved farther than tolerance from the original location
442 // (indicating they intend to move the object, not click), then always process the
443 // motion notify coordinates as given (no snapping back to origin)
444 within_tolerance = false;
445 }
446 break;
447 case GDK_BUTTON_RELEASE:
448 if (within_tolerance && (panning || zoom_rb)) {
449 dontgrab ++;
450 NR::Point const event_w(event->button.x, event->button.y);
451 NR::Point const event_dt(desktop->w2d(event_w));
452 double const zoom_power = ( (event->button.state & GDK_SHIFT_MASK)
453 ? -dontgrab : dontgrab );
454 desktop->zoom_relative_keep_point(event_dt,
455 pow(zoom_inc, zoom_power));
456 gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
457 }
458 if (panning == event->button.button) {
459 panning = 0;
460 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
461 event->button.time);
462 ret = TRUE;
463 } else if (zoom_rb == event->button.button) {
464 zoom_rb = 0;
465 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
466 if (b != NR::Nothing() && !within_tolerance) {
467 desktop->set_display_area(b.assume(), 10);
468 }
469 Inkscape::Rubberband::get()->stop();
470 }
471 xp = yp = 0;
472 break;
473 case GDK_KEY_PRESS:
474 switch (get_group0_keyval(&event->key)) {
475 unsigned int shortcut;
476 case GDK_F1:
477 /* Grab it away from Gtk */
478 shortcut = get_group0_keyval(&event->key);
479 if (event->key.state & GDK_SHIFT_MASK)
480 shortcut |= SP_SHORTCUT_SHIFT_MASK;
481 if (event->key.state & GDK_CONTROL_MASK)
482 shortcut |= SP_SHORTCUT_CONTROL_MASK;
483 if (event->key.state & GDK_MOD1_MASK)
484 shortcut |= SP_SHORTCUT_ALT_MASK;
485 ret = sp_shortcut_invoke(shortcut, desktop);
486 break;
488 case GDK_Tab: // disable tab/shift-tab which cycle widget focus
489 case GDK_ISO_Left_Tab: // they will get different functions
490 if (!(MOD__CTRL_ONLY || (MOD__CTRL && MOD__SHIFT))) {
491 ret = TRUE;
492 } else {
493 /* Grab it away from Gtk */
494 shortcut = get_group0_keyval(&event->key);
495 if (event->key.state & GDK_SHIFT_MASK)
496 shortcut |= SP_SHORTCUT_SHIFT_MASK;
497 if (event->key.state & GDK_CONTROL_MASK)
498 shortcut |= SP_SHORTCUT_CONTROL_MASK;
499 if (event->key.state & GDK_MOD1_MASK)
500 shortcut |= SP_SHORTCUT_ALT_MASK;
501 ret = sp_shortcut_invoke(shortcut, desktop);
502 }
503 break;
504 case GDK_W:
505 case GDK_w:
506 case GDK_F4:
507 /* Close view */
508 if (MOD__CTRL_ONLY) {
509 sp_ui_close_view(NULL);
510 ret = TRUE;
511 }
512 break;
513 case GDK_Left: // Ctrl Left
514 case GDK_KP_Left:
515 case GDK_KP_4:
516 if (MOD__CTRL_ONLY) {
517 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
518 gobble_key_events(get_group0_keyval(&event->key),
519 GDK_CONTROL_MASK);
520 event_context->desktop->scroll_world(i, 0);
521 ret = TRUE;
522 }
523 break;
524 case GDK_Up: // Ctrl Up
525 case GDK_KP_Up:
526 case GDK_KP_8:
527 if (MOD__CTRL_ONLY) {
528 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
529 gobble_key_events(get_group0_keyval(&event->key),
530 GDK_CONTROL_MASK);
531 event_context->desktop->scroll_world(0, i);
532 ret = TRUE;
533 }
534 break;
535 case GDK_Right: // Ctrl Right
536 case GDK_KP_Right:
537 case GDK_KP_6:
538 if (MOD__CTRL_ONLY) {
539 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
540 gobble_key_events(get_group0_keyval(&event->key),
541 GDK_CONTROL_MASK);
542 event_context->desktop->scroll_world(-i, 0);
543 ret = TRUE;
544 }
545 break;
546 case GDK_Down: // Ctrl Down
547 case GDK_KP_Down:
548 case GDK_KP_2:
549 if (MOD__CTRL_ONLY) {
550 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
551 gobble_key_events(get_group0_keyval(&event->key),
552 GDK_CONTROL_MASK);
553 event_context->desktop->scroll_world(0, -i);
554 ret = TRUE;
555 }
556 break;
557 case GDK_F10:
558 if (MOD__SHIFT_ONLY) {
559 sp_event_root_menu_popup(desktop, NULL, event);
560 ret= TRUE;
561 }
562 break;
563 case GDK_space:
564 sp_toggle_selector(desktop);
565 ret= TRUE;
566 break;
567 case GDK_z:
568 case GDK_Z:
569 if (MOD__ALT_ONLY) {
570 desktop->zoom_grab_focus();
571 ret = TRUE;
572 }
573 break;
574 default:
575 break;
576 }
577 break;
578 case GDK_SCROLL:
579 /* shift + wheel, pan left--right */
580 if (event->scroll.state & GDK_SHIFT_MASK) {
581 switch (event->scroll.direction) {
582 case GDK_SCROLL_UP:
583 desktop->scroll_world(wheel_scroll, 0);
584 break;
585 case GDK_SCROLL_DOWN:
586 desktop->scroll_world(-wheel_scroll, 0);
587 break;
588 default:
589 break;
590 }
592 /* ctrl + wheel, zoom in--out */
593 } else if (event->scroll.state & GDK_CONTROL_MASK) {
594 double rel_zoom;
595 switch (event->scroll.direction) {
596 case GDK_SCROLL_UP:
597 rel_zoom = zoom_inc;
598 break;
599 case GDK_SCROLL_DOWN:
600 rel_zoom = 1 / zoom_inc;
601 break;
602 default:
603 rel_zoom = 0.0;
604 break;
605 }
606 if (rel_zoom != 0.0) {
607 NR::Point const scroll_dt = desktop->point();
608 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
609 }
611 /* no modifier, pan up--down (left--right on multiwheel mice?) */
612 } else {
613 switch (event->scroll.direction) {
614 case GDK_SCROLL_UP:
615 desktop->scroll_world(0, wheel_scroll);
616 break;
617 case GDK_SCROLL_DOWN:
618 desktop->scroll_world(0, -wheel_scroll);
619 break;
620 case GDK_SCROLL_LEFT:
621 desktop->scroll_world(wheel_scroll, 0);
622 break;
623 case GDK_SCROLL_RIGHT:
624 desktop->scroll_world(-wheel_scroll, 0);
625 break;
626 }
627 }
628 break;
629 default:
630 break;
631 }
633 return ret;
634 }
636 /**
637 * Handles item specific events. Gets called from Gdk.
638 *
639 * Only reacts to right mouse button at the moment.
640 * \todo Fixme: do context sensitive popup menu on items.
641 */
642 gint
643 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
644 {
645 int ret = FALSE;
647 switch (event->type) {
648 case GDK_BUTTON_PRESS:
649 if ((event->button.button == 3)
650 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
651 sp_event_root_menu_popup(ec->desktop, item, event);
652 ret = TRUE;
653 }
654 break;
655 default:
656 break;
657 }
659 return ret;
660 }
662 /**
663 * Gets called when attribute changes value.
664 */
665 static void
666 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
667 bool is_interactive, gpointer data)
668 {
669 SPEventContext *ec;
671 ec = SP_EVENT_CONTEXT(data);
673 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
674 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
675 }
676 }
678 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
679 NULL, /* Child added */
680 NULL, /* Child removed */
681 sp_ec_repr_attr_changed,
682 NULL, /* Content changed */
683 NULL /* Order changed */
684 };
686 /**
687 * Creates new SPEventContext object and calls its virtual setup() function.
688 */
689 SPEventContext *
690 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
691 {
692 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
693 g_return_val_if_fail(desktop != NULL, NULL);
695 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
697 ec->desktop = desktop;
698 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
699 ec->key = key;
700 ec->prefs_repr = prefs_repr;
701 if (ec->prefs_repr) {
702 Inkscape::GC::anchor(ec->prefs_repr);
703 sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
704 }
706 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
707 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
709 return ec;
710 }
712 /**
713 * Finishes SPEventContext.
714 */
715 void
716 sp_event_context_finish(SPEventContext *ec)
717 {
718 g_return_if_fail(ec != NULL);
719 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
721 ec->enableSelectionCue(false);
723 if (ec->next) {
724 g_warning("Finishing event context with active link\n");
725 }
727 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
728 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
729 }
731 //-------------------------------member functions
733 /**
734 * Enables/disables the SPEventContext's SelCue.
735 */
736 void SPEventContext::enableSelectionCue(bool enable) {
737 if (enable) {
738 if (!_selcue) {
739 _selcue = new Inkscape::SelCue(desktop);
740 }
741 } else {
742 delete _selcue;
743 _selcue = NULL;
744 }
745 }
747 /**
748 * Enables/disables the SPEventContext's GrDrag.
749 */
750 void SPEventContext::enableGrDrag(bool enable) {
751 if (enable) {
752 if (!_grdrag) {
753 _grdrag = new GrDrag(desktop);
754 }
755 } else {
756 if (_grdrag) {
757 delete _grdrag;
758 _grdrag = NULL;
759 }
760 }
761 }
763 /**
764 * Calls virtual set() function of SPEventContext.
765 */
766 void
767 sp_event_context_read(SPEventContext *ec, gchar const *key)
768 {
769 g_return_if_fail(ec != NULL);
770 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
771 g_return_if_fail(key != NULL);
773 if (ec->prefs_repr) {
774 gchar const *val = ec->prefs_repr->attribute(key);
775 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
776 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
777 }
778 }
780 /**
781 * Calls virtual activate() function of SPEventContext.
782 */
783 void
784 sp_event_context_activate(SPEventContext *ec)
785 {
786 g_return_if_fail(ec != NULL);
787 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
789 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
790 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
791 }
793 /**
794 * Calls virtual deactivate() function of SPEventContext.
795 */
796 void
797 sp_event_context_deactivate(SPEventContext *ec)
798 {
799 g_return_if_fail(ec != NULL);
800 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
802 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
803 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
804 }
806 /**
807 * Calls virtual root_handler(), the main event handling function.
808 */
809 gint
810 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
811 {
812 gint ret;
814 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
816 set_event_location(event_context->desktop, event);
818 return ret;
819 }
821 /**
822 * Calls virtual item_handler(), the item event handling function.
823 */
824 gint
825 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
826 {
827 gint ret;
829 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
831 if (! ret) {
832 ret = sp_event_context_root_handler(event_context, event);
833 } else {
834 set_event_location(event_context->desktop, event);
835 }
837 return ret;
838 }
840 /**
841 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
842 */
843 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
844 {
845 if (event->type != GDK_MOTION_NOTIFY) {
846 return;
847 }
849 NR::Point const button_w(event->button.x, event->button.y);
850 NR::Point const button_dt(desktop->w2d(button_w));
851 desktop-> setPosition (button_dt);
852 desktop->set_coordinate_status(button_dt);
853 }
855 //-------------------------------------------------------------------
856 /**
857 * Create popup menu and tell Gtk to show it.
858 */
859 void
860 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
861 {
862 GtkWidget *menu;
864 /* fixme: This is not what I want but works for now (Lauris) */
865 if (event->type == GDK_KEY_PRESS) {
866 item = sp_desktop_selection(desktop)->singleItem();
867 }
868 menu = sp_ui_context_menu(desktop, item);
869 gtk_widget_show(menu);
871 switch (event->type) {
872 case GDK_BUTTON_PRESS:
873 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
874 break;
875 case GDK_KEY_PRESS:
876 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
877 break;
878 default:
879 break;
880 }
881 }
883 /**
884 * Show tool context specific modifier tip.
885 */
886 void
887 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
888 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
889 gchar const *alt_tip)
890 {
891 guint keyval = get_group0_keyval(&event->key);
893 bool ctrl = ctrl_tip && (MOD__CTRL
894 || (keyval == GDK_Control_L)
895 || (keyval == GDK_Control_R));
896 bool shift = shift_tip
897 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
898 bool alt = alt_tip
899 && (MOD__ALT
900 || (keyval == GDK_Alt_L)
901 || (keyval == GDK_Alt_R)
902 || (keyval == GDK_Meta_L)
903 || (keyval == GDK_Meta_R));
905 gchar *tip = g_strdup_printf("%s%s%s%s%s",
906 ( ctrl ? ctrl_tip : "" ),
907 ( ctrl && (shift || alt) ? "; " : "" ),
908 ( shift ? shift_tip : "" ),
909 ( (ctrl || shift) && alt ? "; " : "" ),
910 ( alt ? alt_tip : "" ));
912 if (strlen(tip) > 0) {
913 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
914 }
916 g_free(tip);
917 }
919 /**
920 * Return the keyval corresponding to the key event in group 0, i.e.,
921 * in the main (English) layout.
922 *
923 * Use this instead of simply event->keyval, so that your keyboard shortcuts
924 * work regardless of layouts (e.g., in Cyrillic).
925 */
926 guint
927 get_group0_keyval(GdkEventKey *event)
928 {
929 guint keyval = 0;
930 gdk_keymap_translate_keyboard_state(
931 gdk_keymap_get_for_display(gdk_display_get_default()),
932 event->hardware_keycode,
933 (GdkModifierType) event->state,
934 0 /*event->key.group*/,
935 &keyval, NULL, NULL, NULL);
936 return keyval;
937 }
939 /**
940 * Returns item at point p in desktop.
941 *
942 * If state includes alt key mask, cyclically selects under; honors
943 * into_groups.
944 */
945 SPItem *
946 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
947 bool select_under, bool into_groups)
948 {
949 SPItem *item;
951 if (select_under) {
952 SPItem *selected_at_point =
953 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
954 item = desktop->item_at_point(p, into_groups, selected_at_point);
955 if (item == NULL) { // we may have reached bottom, flip over to the top
956 item = desktop->item_at_point(p, into_groups, NULL);
957 }
958 } else
959 item = desktop->item_at_point(p, into_groups, NULL);
961 return item;
962 }
964 /**
965 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
966 *
967 * Honors into_groups.
968 */
969 SPItem *
970 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
971 {
972 GSList *temp = NULL;
973 temp = g_slist_prepend (temp, item);
974 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
975 g_slist_free (temp);
977 return item_at_point;
978 }
980 /**
981 * Called when SPEventContext subclass node attribute changed.
982 */
983 void
984 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
985 gchar const *old_value, gchar const *new_value,
986 bool const is_interactive, gpointer const data)
987 {
988 if (!name
989 || !strcmp(name, "style")
990 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
991 // no need to regenrate knotholder if only style changed
992 return;
993 }
995 SPEventContext *ec = SP_EVENT_CONTEXT(data);
997 if (ec->shape_knot_holder) {
998 sp_knot_holder_destroy(ec->shape_knot_holder);
999 }
1000 ec->shape_knot_holder = NULL;
1002 SPDesktop *desktop = ec->desktop;
1004 SPItem *item = sp_desktop_selection(desktop)->singleItem();
1006 if (item) {
1007 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1008 }
1009 }
1012 /*
1013 Local Variables:
1014 mode:c++
1015 c-file-style:"stroustrup"
1016 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1017 indent-tabs-mode:nil
1018 fill-column:99
1019 End:
1020 */
1021 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :