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 }
422 if (within_tolerance) {
423 Inkscape::Rubberband::get()->start(desktop, motion_dt);
424 } else {
425 Inkscape::Rubberband::get()->move(motion_dt);
426 }
428 // Once the user has moved farther than tolerance from the original location
429 // (indicating they intend to move the object, not click), then always process the
430 // motion notify coordinates as given (no snapping back to origin)
431 within_tolerance = false;
432 }
433 break;
434 case GDK_BUTTON_RELEASE:
435 xp = yp = 0;
436 if (within_tolerance && (panning || zoom_rb)) {
437 zoom_rb = 0;
438 if (panning) {
439 panning = 0;
440 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
441 event->button.time);
442 }
443 NR::Point const event_w(event->button.x, event->button.y);
444 NR::Point const event_dt(desktop->w2d(event_w));
445 desktop->zoom_relative_keep_point(event_dt,
446 (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
447 desktop->updateNow();
448 ret = TRUE;
449 } else if (panning == event->button.button) {
450 panning = 0;
451 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
452 event->button.time);
454 // in slow complex drawings, some of the motion events are lost;
455 // to make up for this, we scroll it once again to the button-up event coordinates
456 // (i.e. canvas will always get scrolled all the way to the mouse release point,
457 // even if few intermediate steps were visible)
458 NR::Point const motion_w(event->button.x, event->button.y);
459 NR::Point const moved_w( motion_w - button_w );
460 event_context->desktop->scroll_world(moved_w);
461 desktop->updateNow();
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 Inkscape::Rubberband::get()->stop();
467 if (b && !within_tolerance) {
468 desktop->set_display_area(*b, 10);
469 }
470 ret = TRUE;
471 }
472 break;
473 case GDK_KEY_PRESS:
474 switch (get_group0_keyval(&event->key)) {
475 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
476 // in the editing window). So we resteal them back and run our regular shortcut
477 // invoker on them.
478 unsigned int shortcut;
479 case GDK_Tab:
480 case GDK_ISO_Left_Tab:
481 case GDK_F1:
482 shortcut = get_group0_keyval(&event->key);
483 if (event->key.state & GDK_SHIFT_MASK)
484 shortcut |= SP_SHORTCUT_SHIFT_MASK;
485 if (event->key.state & GDK_CONTROL_MASK)
486 shortcut |= SP_SHORTCUT_CONTROL_MASK;
487 if (event->key.state & GDK_MOD1_MASK)
488 shortcut |= SP_SHORTCUT_ALT_MASK;
489 ret = sp_shortcut_invoke(shortcut, desktop);
490 break;
492 case GDK_W:
493 case GDK_w:
494 case GDK_F4:
495 /* Close view */
496 if (MOD__CTRL_ONLY) {
497 sp_ui_close_view(NULL);
498 ret = TRUE;
499 }
500 break;
501 case GDK_Left: // Ctrl Left
502 case GDK_KP_Left:
503 case GDK_KP_4:
504 if (MOD__CTRL_ONLY) {
505 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
506 gobble_key_events(get_group0_keyval(&event->key),
507 GDK_CONTROL_MASK);
508 event_context->desktop->scroll_world(i, 0);
509 ret = TRUE;
510 }
511 break;
512 case GDK_Up: // Ctrl Up
513 case GDK_KP_Up:
514 case GDK_KP_8:
515 if (MOD__CTRL_ONLY) {
516 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
517 gobble_key_events(get_group0_keyval(&event->key),
518 GDK_CONTROL_MASK);
519 event_context->desktop->scroll_world(0, i);
520 ret = TRUE;
521 }
522 break;
523 case GDK_Right: // Ctrl Right
524 case GDK_KP_Right:
525 case GDK_KP_6:
526 if (MOD__CTRL_ONLY) {
527 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
528 gobble_key_events(get_group0_keyval(&event->key),
529 GDK_CONTROL_MASK);
530 event_context->desktop->scroll_world(-i, 0);
531 ret = TRUE;
532 }
533 break;
534 case GDK_Down: // Ctrl Down
535 case GDK_KP_Down:
536 case GDK_KP_2:
537 if (MOD__CTRL_ONLY) {
538 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
539 gobble_key_events(get_group0_keyval(&event->key),
540 GDK_CONTROL_MASK);
541 event_context->desktop->scroll_world(0, -i);
542 ret = TRUE;
543 }
544 break;
545 case GDK_F10:
546 if (MOD__SHIFT_ONLY) {
547 sp_event_root_menu_popup(desktop, NULL, event);
548 ret= TRUE;
549 }
550 break;
551 case GDK_space:
552 sp_toggle_selector(desktop);
553 ret= TRUE;
554 break;
555 case GDK_z:
556 case GDK_Z:
557 if (MOD__ALT_ONLY) {
558 desktop->zoom_grab_focus();
559 ret = TRUE;
560 }
561 break;
562 default:
563 break;
564 }
565 break;
566 case GDK_SCROLL:
567 /* shift + wheel, pan left--right */
568 if (event->scroll.state & GDK_SHIFT_MASK) {
569 switch (event->scroll.direction) {
570 case GDK_SCROLL_UP:
571 desktop->scroll_world(wheel_scroll, 0);
572 break;
573 case GDK_SCROLL_DOWN:
574 desktop->scroll_world(-wheel_scroll, 0);
575 break;
576 default:
577 break;
578 }
580 /* ctrl + wheel, zoom in--out */
581 } else if (event->scroll.state & GDK_CONTROL_MASK) {
582 double rel_zoom;
583 switch (event->scroll.direction) {
584 case GDK_SCROLL_UP:
585 rel_zoom = zoom_inc;
586 break;
587 case GDK_SCROLL_DOWN:
588 rel_zoom = 1 / zoom_inc;
589 break;
590 default:
591 rel_zoom = 0.0;
592 break;
593 }
594 if (rel_zoom != 0.0) {
595 NR::Point const scroll_dt = desktop->point();
596 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
597 }
599 /* no modifier, pan up--down (left--right on multiwheel mice?) */
600 } else {
601 switch (event->scroll.direction) {
602 case GDK_SCROLL_UP:
603 desktop->scroll_world(0, wheel_scroll);
604 break;
605 case GDK_SCROLL_DOWN:
606 desktop->scroll_world(0, -wheel_scroll);
607 break;
608 case GDK_SCROLL_LEFT:
609 desktop->scroll_world(wheel_scroll, 0);
610 break;
611 case GDK_SCROLL_RIGHT:
612 desktop->scroll_world(-wheel_scroll, 0);
613 break;
614 }
615 }
616 break;
617 default:
618 break;
619 }
621 return ret;
622 }
624 /**
625 * Handles item specific events. Gets called from Gdk.
626 *
627 * Only reacts to right mouse button at the moment.
628 * \todo Fixme: do context sensitive popup menu on items.
629 */
630 gint
631 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
632 {
633 int ret = FALSE;
635 switch (event->type) {
636 case GDK_BUTTON_PRESS:
637 if ((event->button.button == 3)
638 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
639 sp_event_root_menu_popup(ec->desktop, item, event);
640 ret = TRUE;
641 }
642 break;
643 default:
644 break;
645 }
647 return ret;
648 }
650 /**
651 * Gets called when attribute changes value.
652 */
653 static void
654 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
655 bool is_interactive, gpointer data)
656 {
657 SPEventContext *ec;
659 ec = SP_EVENT_CONTEXT(data);
661 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
662 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
663 }
664 }
666 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
667 NULL, /* Child added */
668 NULL, /* Child removed */
669 sp_ec_repr_attr_changed,
670 NULL, /* Content changed */
671 NULL /* Order changed */
672 };
674 /**
675 * Creates new SPEventContext object and calls its virtual setup() function.
676 */
677 SPEventContext *
678 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
679 {
680 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
681 g_return_val_if_fail(desktop != NULL, NULL);
683 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
685 ec->desktop = desktop;
686 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
687 ec->key = key;
688 ec->prefs_repr = prefs_repr;
689 if (ec->prefs_repr) {
690 Inkscape::GC::anchor(ec->prefs_repr);
691 sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
692 }
694 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
695 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
697 return ec;
698 }
700 /**
701 * Finishes SPEventContext.
702 */
703 void
704 sp_event_context_finish(SPEventContext *ec)
705 {
706 g_return_if_fail(ec != NULL);
707 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
709 ec->enableSelectionCue(false);
711 if (ec->next) {
712 g_warning("Finishing event context with active link\n");
713 }
715 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
716 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
717 }
719 //-------------------------------member functions
721 /**
722 * Enables/disables the SPEventContext's SelCue.
723 */
724 void SPEventContext::enableSelectionCue(bool enable) {
725 if (enable) {
726 if (!_selcue) {
727 _selcue = new Inkscape::SelCue(desktop);
728 }
729 } else {
730 delete _selcue;
731 _selcue = NULL;
732 }
733 }
735 /**
736 * Enables/disables the SPEventContext's GrDrag.
737 */
738 void SPEventContext::enableGrDrag(bool enable) {
739 if (enable) {
740 if (!_grdrag) {
741 _grdrag = new GrDrag(desktop);
742 }
743 } else {
744 if (_grdrag) {
745 delete _grdrag;
746 _grdrag = NULL;
747 }
748 }
749 }
751 /**
752 * Calls virtual set() function of SPEventContext.
753 */
754 void
755 sp_event_context_read(SPEventContext *ec, gchar const *key)
756 {
757 g_return_if_fail(ec != NULL);
758 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
759 g_return_if_fail(key != NULL);
761 if (ec->prefs_repr) {
762 gchar const *val = ec->prefs_repr->attribute(key);
763 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
764 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
765 }
766 }
768 /**
769 * Calls virtual activate() function of SPEventContext.
770 */
771 void
772 sp_event_context_activate(SPEventContext *ec)
773 {
774 g_return_if_fail(ec != NULL);
775 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
777 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
778 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
779 }
781 /**
782 * Calls virtual deactivate() function of SPEventContext.
783 */
784 void
785 sp_event_context_deactivate(SPEventContext *ec)
786 {
787 g_return_if_fail(ec != NULL);
788 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
790 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
791 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
792 }
794 /**
795 * Calls virtual root_handler(), the main event handling function.
796 */
797 gint
798 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
799 {
800 gint ret;
802 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
804 set_event_location(event_context->desktop, event);
806 return ret;
807 }
809 /**
810 * Calls virtual item_handler(), the item event handling function.
811 */
812 gint
813 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
814 {
815 gint ret;
817 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
819 if (! ret) {
820 ret = sp_event_context_root_handler(event_context, event);
821 } else {
822 set_event_location(event_context->desktop, event);
823 }
825 return ret;
826 }
828 /**
829 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
830 */
831 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
832 {
833 if (event->type != GDK_MOTION_NOTIFY) {
834 return;
835 }
837 NR::Point const button_w(event->button.x, event->button.y);
838 NR::Point const button_dt(desktop->w2d(button_w));
839 desktop-> setPosition (button_dt);
840 desktop->set_coordinate_status(button_dt);
841 }
843 //-------------------------------------------------------------------
844 /**
845 * Create popup menu and tell Gtk to show it.
846 */
847 void
848 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
849 {
850 GtkWidget *menu;
852 /* fixme: This is not what I want but works for now (Lauris) */
853 if (event->type == GDK_KEY_PRESS) {
854 item = sp_desktop_selection(desktop)->singleItem();
855 }
856 menu = sp_ui_context_menu(desktop, item);
857 gtk_widget_show(menu);
859 switch (event->type) {
860 case GDK_BUTTON_PRESS:
861 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
862 break;
863 case GDK_KEY_PRESS:
864 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
865 break;
866 default:
867 break;
868 }
869 }
871 /**
872 * Show tool context specific modifier tip.
873 */
874 void
875 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
876 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
877 gchar const *alt_tip)
878 {
879 guint keyval = get_group0_keyval(&event->key);
881 bool ctrl = ctrl_tip && (MOD__CTRL
882 || (keyval == GDK_Control_L)
883 || (keyval == GDK_Control_R));
884 bool shift = shift_tip
885 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
886 bool alt = alt_tip
887 && (MOD__ALT
888 || (keyval == GDK_Alt_L)
889 || (keyval == GDK_Alt_R)
890 || (keyval == GDK_Meta_L)
891 || (keyval == GDK_Meta_R));
893 gchar *tip = g_strdup_printf("%s%s%s%s%s",
894 ( ctrl ? ctrl_tip : "" ),
895 ( ctrl && (shift || alt) ? "; " : "" ),
896 ( shift ? shift_tip : "" ),
897 ( (ctrl || shift) && alt ? "; " : "" ),
898 ( alt ? alt_tip : "" ));
900 if (strlen(tip) > 0) {
901 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
902 }
904 g_free(tip);
905 }
907 /**
908 * Return the keyval corresponding to the key event in group 0, i.e.,
909 * in the main (English) layout.
910 *
911 * Use this instead of simply event->keyval, so that your keyboard shortcuts
912 * work regardless of layouts (e.g., in Cyrillic).
913 */
914 guint
915 get_group0_keyval(GdkEventKey *event)
916 {
917 guint keyval = 0;
918 gdk_keymap_translate_keyboard_state(
919 gdk_keymap_get_for_display(gdk_display_get_default()),
920 event->hardware_keycode,
921 (GdkModifierType) event->state,
922 0 /*event->key.group*/,
923 &keyval, NULL, NULL, NULL);
924 return keyval;
925 }
927 /**
928 * Returns item at point p in desktop.
929 *
930 * If state includes alt key mask, cyclically selects under; honors
931 * into_groups.
932 */
933 SPItem *
934 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
935 bool select_under, bool into_groups)
936 {
937 SPItem *item;
939 if (select_under) {
940 SPItem *selected_at_point =
941 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
942 item = desktop->item_at_point(p, into_groups, selected_at_point);
943 if (item == NULL) { // we may have reached bottom, flip over to the top
944 item = desktop->item_at_point(p, into_groups, NULL);
945 }
946 } else
947 item = desktop->item_at_point(p, into_groups, NULL);
949 return item;
950 }
952 /**
953 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
954 *
955 * Honors into_groups.
956 */
957 SPItem *
958 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
959 {
960 GSList *temp = NULL;
961 temp = g_slist_prepend (temp, item);
962 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
963 g_slist_free (temp);
965 return item_at_point;
966 }
968 /**
969 * Called when SPEventContext subclass node attribute changed.
970 */
971 void
972 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
973 gchar const *old_value, gchar const *new_value,
974 bool const is_interactive, gpointer const data)
975 {
976 if (!name
977 || !strcmp(name, "style")
978 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
979 // no need to regenrate knotholder if only style changed
980 return;
981 }
983 SPEventContext *ec = SP_EVENT_CONTEXT(data);
985 if (ec->shape_knot_holder) {
986 sp_knot_holder_destroy(ec->shape_knot_holder);
987 }
988 ec->shape_knot_holder = NULL;
990 SPDesktop *desktop = ec->desktop;
992 SPItem *item = sp_desktop_selection(desktop)->singleItem();
994 if (item) {
995 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
996 }
997 }
1000 /*
1001 Local Variables:
1002 mode:c++
1003 c-file-style:"stroustrup"
1004 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1005 indent-tabs-mode:nil
1006 fill-column:99
1007 End:
1008 */
1009 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :