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 /* fixme: */
176 if (ec->cursor_shape) {
177 GdkBitmap *bitmap = NULL;
178 GdkBitmap *mask = NULL;
179 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
180 if ((bitmap != NULL) && (mask != NULL)) {
181 if (ec->cursor)
182 gdk_cursor_unref (ec->cursor);
183 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
184 &w->style->black,
185 &w->style->white,
186 ec->hot_x, ec->hot_y);
187 g_object_unref (bitmap);
188 g_object_unref (mask);
189 }
190 }
191 gdk_window_set_cursor(w->window, ec->cursor);
192 }
193 ec->desktop->waiting_cursor = false;
194 }
196 /**
197 * Callback that gets called on initialization of SPEventContext object.
198 * Redraws mouse cursor, at the moment.
199 */
200 static void
201 sp_event_context_private_setup(SPEventContext *ec)
202 {
203 sp_event_context_update_cursor(ec);
204 }
206 /**
207 * \brief Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
208 */
209 gint gobble_key_events(guint keyval, gint mask)
210 {
211 GdkEvent *event_next;
212 gint i = 0;
214 event_next = gdk_event_get();
215 // while the next event is also a key notify with the same keyval and mask,
216 while (event_next && event_next->type == GDK_KEY_PRESS
217 && event_next->key.keyval == keyval
218 && (event_next->key.state & mask)) {
219 // kill it
220 gdk_event_free(event_next);
221 // get next
222 event_next = gdk_event_get();
223 i ++;
224 }
225 // otherwise, put it back onto the queue
226 if (event_next) gdk_event_put(event_next);
228 return i;
229 }
231 /**
232 * \brief Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
233 */
234 gint gobble_motion_events(gint mask)
235 {
236 GdkEvent *event_next;
237 gint i = 0;
239 event_next = gdk_event_get();
240 // while the next event is also a key notify with the same keyval and mask,
241 while (event_next && event_next->type == GDK_MOTION_NOTIFY
242 && (event_next->motion.state & mask)) {
243 // kill it
244 gdk_event_free(event_next);
245 // get next
246 event_next = gdk_event_get();
247 i ++;
248 }
249 // otherwise, put it back onto the queue
250 if (event_next) gdk_event_put(event_next);
252 return i;
253 }
255 /**
256 * Toggles current tool between active tool and selector tool.
257 * Subroutine of sp_event_context_private_root_handler().
258 */
259 static void
260 sp_toggle_selector(SPDesktop *dt)
261 {
262 if (!dt->event_context) return;
264 if (tools_isactive(dt, TOOLS_SELECT)) {
265 if (selector_toggled) {
266 if (switch_selector_to) tools_switch (dt, switch_selector_to);
267 selector_toggled = FALSE;
268 } else return;
269 } else {
270 selector_toggled = TRUE;
271 switch_selector_to = tools_active(dt);
272 tools_switch (dt, TOOLS_SELECT);
273 }
274 }
276 /**
277 * Calculates and keeps track of scroll acceleration.
278 * Subroutine of sp_event_context_private_root_handler().
279 */
280 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas *canvas)
281 {
282 guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
283 glong slowest_buffer = canvas->slowest_buffer / 1000; // the buffer time is in usec, but event time is in msec
285 // reduce time interval by the time it took to paint slowest buffer,
286 // so that acceleration does not hiccup on complex slow-rendering drawings
287 if ((guint32) slowest_buffer <= time_diff)
288 time_diff -= slowest_buffer;
289 else
290 time_diff = 0;
292 /* key pressed within 500ms ? (1/2 second) */
293 if (time_diff > 500 || event->key.keyval != scroll_keyval) {
294 scroll_multiply = 1; // abort acceleration
295 } else {
296 scroll_multiply += acceleration; // continue acceleration
297 }
299 scroll_event_time = ((GdkEventKey *) event)->time;
300 scroll_keyval = event->key.keyval;
302 return scroll_multiply;
303 }
305 /**
306 * Main event dispatch, gets called from Gdk.
307 */
308 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
309 {
310 static NR::Point button_w;
311 static unsigned int panning = 0;
312 static unsigned int zoom_rb = 0;
314 SPDesktop *desktop = event_context->desktop;
316 tolerance = prefs_get_int_attribute_limited(
317 "options.dragtolerance","value", 0, 0, 100);
318 double const zoom_inc = prefs_get_double_attribute_limited(
319 "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
320 double const acceleration = prefs_get_double_attribute_limited(
321 "options.scrollingacceleration", "value", 0, 0, 6);
322 int const key_scroll = prefs_get_int_attribute_limited(
323 "options.keyscroll", "value", 10, 0, 1000);
324 int const wheel_scroll = prefs_get_int_attribute_limited(
325 "options.wheelscroll", "value", 40, 0, 1000);
327 gint ret = FALSE;
329 switch (event->type) {
330 case GDK_2BUTTON_PRESS:
331 if (panning) {
332 panning = 0;
333 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
334 event->button.time);
335 ret = TRUE;
336 } else {
337 /* sp_desktop_dialog(); */
338 }
339 break;
340 case GDK_BUTTON_PRESS:
342 // save drag origin
343 xp = (gint) event->button.x;
344 yp = (gint) event->button.y;
345 within_tolerance = true;
347 switch (event->button.button) {
348 case 2:
349 button_w = NR::Point(event->button.x,
350 event->button.y);
351 if (event->button.state == GDK_SHIFT_MASK) {
352 zoom_rb = 2;
353 } else {
354 panning = 2;
355 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
356 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
357 NULL, event->button.time-1);
358 }
359 ret = TRUE;
360 break;
361 case 3:
362 if (event->button.state & GDK_SHIFT_MASK
363 || event->button.state & GDK_CONTROL_MASK) {
364 button_w = NR::Point(event->button.x,
365 event->button.y);
366 panning = 3;
367 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
368 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
369 NULL, event->button.time);
370 ret = TRUE;
371 } else {
372 sp_event_root_menu_popup(desktop, NULL, event);
373 }
374 break;
375 default:
376 break;
377 }
378 break;
379 case GDK_MOTION_NOTIFY:
380 if (panning) {
381 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
382 || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))) {
383 /* Gdk seems to lose button release for us sometimes :-( */
384 panning = 0;
385 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
386 event->button.time);
387 ret = TRUE;
388 } else {
389 if ( within_tolerance
390 && ( abs( (gint) event->motion.x - xp ) < tolerance )
391 && ( abs( (gint) event->motion.y - yp ) < tolerance ))
392 {
393 // do not drag if we're within tolerance from origin
394 break;
395 }
396 // Once the user has moved farther than tolerance from
397 // the original location (indicating they intend to move
398 // the object, not click), then always process the motion
399 // notify coordinates as given (no snapping back to origin)
400 within_tolerance = false;
402 // gobble subsequent motion events to prevent "sticking"
403 // when scrolling is slow
404 gobble_motion_events(panning == 2 ?
405 GDK_BUTTON2_MASK : GDK_BUTTON3_MASK);
407 NR::Point const motion_w(event->motion.x, event->motion.y);
408 NR::Point const moved_w( motion_w - button_w );
409 event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
410 ret = TRUE;
411 }
412 } else if (zoom_rb) {
413 NR::Point const motion_w(event->motion.x, event->motion.y);
414 NR::Point const motion_dt(desktop->w2d(motion_w));
416 if ( within_tolerance
417 && ( abs( (gint) event->motion.x - xp ) < tolerance )
418 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
419 break; // do not drag if we're within tolerance from origin
420 }
421 // Once the user has moved farther than tolerance from the original location
422 // (indicating they intend to move the object, not click), then always process the
423 // motion notify coordinates as given (no snapping back to origin)
424 within_tolerance = false;
426 if (Inkscape::Rubberband::get()->is_started()) {
427 Inkscape::Rubberband::get()->move(motion_dt);
428 } else {
429 Inkscape::Rubberband::get()->start(desktop, motion_dt);
430 }
431 }
432 break;
433 case GDK_BUTTON_RELEASE:
434 xp = yp = 0;
435 if (within_tolerance && (panning || zoom_rb)) {
436 zoom_rb = 0;
437 if (panning) {
438 panning = 0;
439 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
440 event->button.time);
441 }
442 NR::Point const event_w(event->button.x, event->button.y);
443 NR::Point const event_dt(desktop->w2d(event_w));
444 desktop->zoom_relative_keep_point(event_dt,
445 (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
446 desktop->updateNow();
447 ret = TRUE;
448 } else if (panning == event->button.button) {
449 panning = 0;
450 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
451 event->button.time);
453 // in slow complex drawings, some of the motion events are lost;
454 // to make up for this, we scroll it once again to the button-up event coordinates
455 // (i.e. canvas will always get scrolled all the way to the mouse release point,
456 // even if few intermediate steps were visible)
457 NR::Point const motion_w(event->button.x, event->button.y);
458 NR::Point const moved_w( motion_w - button_w );
459 event_context->desktop->scroll_world(moved_w);
460 desktop->updateNow();
461 ret = TRUE;
462 } else if (zoom_rb == event->button.button) {
463 zoom_rb = 0;
464 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
465 Inkscape::Rubberband::get()->stop();
466 if (b && !within_tolerance) {
467 desktop->set_display_area(*b, 10);
468 }
469 ret = TRUE;
470 }
471 break;
472 case GDK_KEY_PRESS:
473 switch (get_group0_keyval(&event->key)) {
474 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
475 // in the editing window). So we resteal them back and run our regular shortcut
476 // invoker on them.
477 unsigned int shortcut;
478 case GDK_Tab:
479 case GDK_ISO_Left_Tab:
480 case GDK_F1:
481 shortcut = get_group0_keyval(&event->key);
482 if (event->key.state & GDK_SHIFT_MASK)
483 shortcut |= SP_SHORTCUT_SHIFT_MASK;
484 if (event->key.state & GDK_CONTROL_MASK)
485 shortcut |= SP_SHORTCUT_CONTROL_MASK;
486 if (event->key.state & GDK_MOD1_MASK)
487 shortcut |= SP_SHORTCUT_ALT_MASK;
488 ret = sp_shortcut_invoke(shortcut, desktop);
489 break;
491 case GDK_W:
492 case GDK_w:
493 case GDK_F4:
494 /* Close view */
495 if (MOD__CTRL_ONLY) {
496 sp_ui_close_view(NULL);
497 ret = TRUE;
498 }
499 break;
500 case GDK_Left: // Ctrl Left
501 case GDK_KP_Left:
502 case GDK_KP_4:
503 if (MOD__CTRL_ONLY) {
504 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
505 gobble_key_events(get_group0_keyval(&event->key),
506 GDK_CONTROL_MASK);
507 event_context->desktop->scroll_world(i, 0);
508 ret = TRUE;
509 }
510 break;
511 case GDK_Up: // Ctrl Up
512 case GDK_KP_Up:
513 case GDK_KP_8:
514 if (MOD__CTRL_ONLY) {
515 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
516 gobble_key_events(get_group0_keyval(&event->key),
517 GDK_CONTROL_MASK);
518 event_context->desktop->scroll_world(0, i);
519 ret = TRUE;
520 }
521 break;
522 case GDK_Right: // Ctrl Right
523 case GDK_KP_Right:
524 case GDK_KP_6:
525 if (MOD__CTRL_ONLY) {
526 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
527 gobble_key_events(get_group0_keyval(&event->key),
528 GDK_CONTROL_MASK);
529 event_context->desktop->scroll_world(-i, 0);
530 ret = TRUE;
531 }
532 break;
533 case GDK_Down: // Ctrl Down
534 case GDK_KP_Down:
535 case GDK_KP_2:
536 if (MOD__CTRL_ONLY) {
537 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
538 gobble_key_events(get_group0_keyval(&event->key),
539 GDK_CONTROL_MASK);
540 event_context->desktop->scroll_world(0, -i);
541 ret = TRUE;
542 }
543 break;
544 case GDK_F10:
545 if (MOD__SHIFT_ONLY) {
546 sp_event_root_menu_popup(desktop, NULL, event);
547 ret= TRUE;
548 }
549 break;
550 case GDK_space:
551 sp_toggle_selector(desktop);
552 ret= TRUE;
553 break;
554 case GDK_z:
555 case GDK_Z:
556 if (MOD__ALT_ONLY) {
557 desktop->zoom_grab_focus();
558 ret = TRUE;
559 }
560 break;
561 default:
562 break;
563 }
564 break;
565 case GDK_SCROLL:
566 /* shift + wheel, pan left--right */
567 if (event->scroll.state & GDK_SHIFT_MASK) {
568 switch (event->scroll.direction) {
569 case GDK_SCROLL_UP:
570 desktop->scroll_world(wheel_scroll, 0);
571 break;
572 case GDK_SCROLL_DOWN:
573 desktop->scroll_world(-wheel_scroll, 0);
574 break;
575 default:
576 break;
577 }
579 /* ctrl + wheel, zoom in--out */
580 } else if (event->scroll.state & GDK_CONTROL_MASK) {
581 double rel_zoom;
582 switch (event->scroll.direction) {
583 case GDK_SCROLL_UP:
584 rel_zoom = zoom_inc;
585 break;
586 case GDK_SCROLL_DOWN:
587 rel_zoom = 1 / zoom_inc;
588 break;
589 default:
590 rel_zoom = 0.0;
591 break;
592 }
593 if (rel_zoom != 0.0) {
594 NR::Point const scroll_dt = desktop->point();
595 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
596 }
598 /* no modifier, pan up--down (left--right on multiwheel mice?) */
599 } else {
600 switch (event->scroll.direction) {
601 case GDK_SCROLL_UP:
602 desktop->scroll_world(0, wheel_scroll);
603 break;
604 case GDK_SCROLL_DOWN:
605 desktop->scroll_world(0, -wheel_scroll);
606 break;
607 case GDK_SCROLL_LEFT:
608 desktop->scroll_world(wheel_scroll, 0);
609 break;
610 case GDK_SCROLL_RIGHT:
611 desktop->scroll_world(-wheel_scroll, 0);
612 break;
613 }
614 }
615 break;
616 default:
617 break;
618 }
620 return ret;
621 }
623 /**
624 * Handles item specific events. Gets called from Gdk.
625 *
626 * Only reacts to right mouse button at the moment.
627 * \todo Fixme: do context sensitive popup menu on items.
628 */
629 gint
630 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
631 {
632 int ret = FALSE;
634 switch (event->type) {
635 case GDK_BUTTON_PRESS:
636 if ((event->button.button == 3)
637 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
638 sp_event_root_menu_popup(ec->desktop, item, event);
639 ret = TRUE;
640 }
641 break;
642 default:
643 break;
644 }
646 return ret;
647 }
649 /**
650 * Gets called when attribute changes value.
651 */
652 static void
653 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
654 bool is_interactive, gpointer data)
655 {
656 SPEventContext *ec;
658 ec = SP_EVENT_CONTEXT(data);
660 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
661 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
662 }
663 }
665 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
666 NULL, /* Child added */
667 NULL, /* Child removed */
668 sp_ec_repr_attr_changed,
669 NULL, /* Content changed */
670 NULL /* Order changed */
671 };
673 /**
674 * Creates new SPEventContext object and calls its virtual setup() function.
675 */
676 SPEventContext *
677 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
678 {
679 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
680 g_return_val_if_fail(desktop != NULL, NULL);
682 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
684 ec->desktop = desktop;
685 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
686 ec->key = key;
687 ec->prefs_repr = prefs_repr;
688 if (ec->prefs_repr) {
689 Inkscape::GC::anchor(ec->prefs_repr);
690 sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
691 }
693 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
694 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
696 return ec;
697 }
699 /**
700 * Finishes SPEventContext.
701 */
702 void
703 sp_event_context_finish(SPEventContext *ec)
704 {
705 g_return_if_fail(ec != NULL);
706 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
708 ec->enableSelectionCue(false);
710 if (ec->next) {
711 g_warning("Finishing event context with active link\n");
712 }
714 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
715 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
716 }
718 //-------------------------------member functions
720 /**
721 * Enables/disables the SPEventContext's SelCue.
722 */
723 void SPEventContext::enableSelectionCue(bool enable) {
724 if (enable) {
725 if (!_selcue) {
726 _selcue = new Inkscape::SelCue(desktop);
727 }
728 } else {
729 delete _selcue;
730 _selcue = NULL;
731 }
732 }
734 /**
735 * Enables/disables the SPEventContext's GrDrag.
736 */
737 void SPEventContext::enableGrDrag(bool enable) {
738 if (enable) {
739 if (!_grdrag) {
740 _grdrag = new GrDrag(desktop);
741 }
742 } else {
743 if (_grdrag) {
744 delete _grdrag;
745 _grdrag = NULL;
746 }
747 }
748 }
750 /**
751 * Calls virtual set() function of SPEventContext.
752 */
753 void
754 sp_event_context_read(SPEventContext *ec, gchar const *key)
755 {
756 g_return_if_fail(ec != NULL);
757 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
758 g_return_if_fail(key != NULL);
760 if (ec->prefs_repr) {
761 gchar const *val = ec->prefs_repr->attribute(key);
762 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
763 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
764 }
765 }
767 /**
768 * Calls virtual activate() function of SPEventContext.
769 */
770 void
771 sp_event_context_activate(SPEventContext *ec)
772 {
773 g_return_if_fail(ec != NULL);
774 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
776 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
777 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
778 }
780 /**
781 * Calls virtual deactivate() function of SPEventContext.
782 */
783 void
784 sp_event_context_deactivate(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))->deactivate)
790 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
791 }
793 /**
794 * Calls virtual root_handler(), the main event handling function.
795 */
796 gint
797 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
798 {
799 gint ret;
801 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
803 set_event_location(event_context->desktop, event);
805 return ret;
806 }
808 /**
809 * Calls virtual item_handler(), the item event handling function.
810 */
811 gint
812 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
813 {
814 gint ret;
816 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
818 if (! ret) {
819 ret = sp_event_context_root_handler(event_context, event);
820 } else {
821 set_event_location(event_context->desktop, event);
822 }
824 return ret;
825 }
827 /**
828 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
829 */
830 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
831 {
832 if (event->type != GDK_MOTION_NOTIFY) {
833 return;
834 }
836 NR::Point const button_w(event->button.x, event->button.y);
837 NR::Point const button_dt(desktop->w2d(button_w));
838 desktop-> setPosition (button_dt);
839 desktop->set_coordinate_status(button_dt);
840 }
842 //-------------------------------------------------------------------
843 /**
844 * Create popup menu and tell Gtk to show it.
845 */
846 void
847 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
848 {
849 GtkWidget *menu;
851 /* fixme: This is not what I want but works for now (Lauris) */
852 if (event->type == GDK_KEY_PRESS) {
853 item = sp_desktop_selection(desktop)->singleItem();
854 }
855 menu = sp_ui_context_menu(desktop, item);
856 gtk_widget_show(menu);
858 switch (event->type) {
859 case GDK_BUTTON_PRESS:
860 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
861 break;
862 case GDK_KEY_PRESS:
863 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
864 break;
865 default:
866 break;
867 }
868 }
870 /**
871 * Show tool context specific modifier tip.
872 */
873 void
874 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
875 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
876 gchar const *alt_tip)
877 {
878 guint keyval = get_group0_keyval(&event->key);
880 bool ctrl = ctrl_tip && (MOD__CTRL
881 || (keyval == GDK_Control_L)
882 || (keyval == GDK_Control_R));
883 bool shift = shift_tip
884 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
885 bool alt = alt_tip
886 && (MOD__ALT
887 || (keyval == GDK_Alt_L)
888 || (keyval == GDK_Alt_R)
889 || (keyval == GDK_Meta_L)
890 || (keyval == GDK_Meta_R));
892 gchar *tip = g_strdup_printf("%s%s%s%s%s",
893 ( ctrl ? ctrl_tip : "" ),
894 ( ctrl && (shift || alt) ? "; " : "" ),
895 ( shift ? shift_tip : "" ),
896 ( (ctrl || shift) && alt ? "; " : "" ),
897 ( alt ? alt_tip : "" ));
899 if (strlen(tip) > 0) {
900 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
901 }
903 g_free(tip);
904 }
906 /**
907 * Return the keyval corresponding to the key event in group 0, i.e.,
908 * in the main (English) layout.
909 *
910 * Use this instead of simply event->keyval, so that your keyboard shortcuts
911 * work regardless of layouts (e.g., in Cyrillic).
912 */
913 guint
914 get_group0_keyval(GdkEventKey *event)
915 {
916 guint keyval = 0;
917 gdk_keymap_translate_keyboard_state(
918 gdk_keymap_get_for_display(gdk_display_get_default()),
919 event->hardware_keycode,
920 (GdkModifierType) event->state,
921 0 /*event->key.group*/,
922 &keyval, NULL, NULL, NULL);
923 return keyval;
924 }
926 /**
927 * Returns item at point p in desktop.
928 *
929 * If state includes alt key mask, cyclically selects under; honors
930 * into_groups.
931 */
932 SPItem *
933 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
934 bool select_under, bool into_groups)
935 {
936 SPItem *item;
938 if (select_under) {
939 SPItem *selected_at_point =
940 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
941 item = desktop->item_at_point(p, into_groups, selected_at_point);
942 if (item == NULL) { // we may have reached bottom, flip over to the top
943 item = desktop->item_at_point(p, into_groups, NULL);
944 }
945 } else
946 item = desktop->item_at_point(p, into_groups, NULL);
948 return item;
949 }
951 /**
952 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
953 *
954 * Honors into_groups.
955 */
956 SPItem *
957 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
958 {
959 GSList *temp = NULL;
960 temp = g_slist_prepend (temp, item);
961 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
962 g_slist_free (temp);
964 return item_at_point;
965 }
967 /**
968 * Called when SPEventContext subclass node attribute changed.
969 */
970 void
971 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
972 gchar const *old_value, gchar const *new_value,
973 bool const is_interactive, gpointer const data)
974 {
975 if (!name
976 || !strcmp(name, "style")
977 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
978 // no need to regenrate knotholder if only style changed
979 return;
980 }
982 SPEventContext *ec = SP_EVENT_CONTEXT(data);
984 if (ec->shape_knot_holder) {
985 sp_knot_holder_destroy(ec->shape_knot_holder);
986 }
987 ec->shape_knot_holder = NULL;
989 SPDesktop *desktop = ec->desktop;
991 SPItem *item = sp_desktop_selection(desktop)->singleItem();
993 if (item) {
994 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
995 }
996 }
999 /*
1000 Local Variables:
1001 mode:c++
1002 c-file-style:"stroustrup"
1003 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1004 indent-tabs-mode:nil
1005 fill-column:99
1006 End:
1007 */
1008 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :