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>
33 #include <glibmm/i18n.h>
35 #include "display/sp-canvas.h"
36 #include "xml/node-event-vector.h"
37 #include "sp-cursor.h"
38 #include "shortcuts.h"
39 #include "desktop.h"
40 #include "desktop-handles.h"
41 #include "selection.h"
42 #include "file.h"
43 #include "interface.h"
44 #include "macros.h"
45 #include "tools-switch.h"
46 #include "prefs-utils.h"
47 #include "message-context.h"
48 #include "gradient-drag.h"
49 #include "object-edit.h"
50 #include "attributes.h"
51 #include "rubberband.h"
52 #include "selcue.h"
54 #include "event-context.h"
56 static void sp_event_context_class_init(SPEventContextClass *klass);
57 static void sp_event_context_init(SPEventContext *event_context);
58 static void sp_event_context_dispose(GObject *object);
60 static void sp_event_context_private_setup(SPEventContext *ec);
61 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
62 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
64 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
66 static GObjectClass *parent_class;
68 // globals for temporary switching to selector by space
69 static bool selector_toggled = FALSE;
70 static int switch_selector_to = 0;
72 // globals for temporary switching to dropper by 'D'
73 static bool dropper_toggled = FALSE;
74 static int switch_dropper_to = 0;
76 static gint xp = 0, yp = 0; // where drag started
77 static gint tolerance = 0;
78 static bool within_tolerance = false;
80 // globals for keeping track of keyboard scroll events in order to accelerate
81 static guint32 scroll_event_time = 0;
82 static gdouble scroll_multiply = 1;
83 static guint scroll_keyval = 0;
85 /**
86 * Registers the SPEventContext class with Glib and returns its type number.
87 */
88 GType
89 sp_event_context_get_type(void)
90 {
91 static GType type = 0;
92 if (!type) {
93 GTypeInfo info = {
94 sizeof(SPEventContextClass),
95 NULL, NULL,
96 (GClassInitFunc) sp_event_context_class_init,
97 NULL, NULL,
98 sizeof(SPEventContext),
99 4,
100 (GInstanceInitFunc) sp_event_context_init,
101 NULL, /* value_table */
102 };
103 type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
104 }
105 return type;
106 }
108 /**
109 * Callback to set up the SPEventContext vtable.
110 */
111 static void
112 sp_event_context_class_init(SPEventContextClass *klass)
113 {
114 GObjectClass *object_class;
116 object_class = (GObjectClass *) klass;
118 parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
120 object_class->dispose = sp_event_context_dispose;
122 klass->setup = sp_event_context_private_setup;
123 klass->root_handler = sp_event_context_private_root_handler;
124 klass->item_handler = sp_event_context_private_item_handler;
125 }
127 /**
128 * Clears all SPEventContext object members.
129 */
130 static void
131 sp_event_context_init(SPEventContext *event_context)
132 {
133 event_context->desktop = NULL;
134 event_context->cursor = NULL;
135 event_context->_message_context = NULL;
136 event_context->_selcue = NULL;
137 event_context->_grdrag = NULL;
138 event_context->space_panning = false;
139 }
141 /**
142 * Callback to free and null member variables of SPEventContext object.
143 */
144 static void
145 sp_event_context_dispose(GObject *object)
146 {
147 SPEventContext *ec;
149 ec = SP_EVENT_CONTEXT(object);
151 if (ec->_message_context) {
152 delete ec->_message_context;
153 }
155 if (ec->cursor != NULL) {
156 gdk_cursor_unref(ec->cursor);
157 ec->cursor = NULL;
158 }
160 if (ec->desktop) {
161 ec->desktop = NULL;
162 }
164 if (ec->prefs_repr) {
165 sp_repr_remove_listener_by_data(ec->prefs_repr, ec);
166 Inkscape::GC::release(ec->prefs_repr);
167 ec->prefs_repr = NULL;
168 }
170 G_OBJECT_CLASS(parent_class)->dispose(object);
171 }
173 /**
174 * Recreates and draws cursor on desktop related to SPEventContext.
175 */
176 void
177 sp_event_context_update_cursor(SPEventContext *ec)
178 {
179 GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
180 if (w->window) {
181 /* fixme: */
182 if (ec->cursor_shape) {
183 GdkBitmap *bitmap = NULL;
184 GdkBitmap *mask = NULL;
185 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
186 if ((bitmap != NULL) && (mask != NULL)) {
187 if (ec->cursor)
188 gdk_cursor_unref (ec->cursor);
189 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
190 &w->style->black,
191 &w->style->white,
192 ec->hot_x, ec->hot_y);
193 g_object_unref (bitmap);
194 g_object_unref (mask);
195 }
196 }
197 gdk_window_set_cursor(w->window, ec->cursor);
198 }
199 ec->desktop->waiting_cursor = false;
200 }
202 /**
203 * Callback that gets called on initialization of SPEventContext object.
204 * Redraws mouse cursor, at the moment.
205 */
206 static void
207 sp_event_context_private_setup(SPEventContext *ec)
208 {
209 sp_event_context_update_cursor(ec);
210 }
212 /**
213 * \brief Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
214 */
215 gint gobble_key_events(guint keyval, gint mask)
216 {
217 GdkEvent *event_next;
218 gint i = 0;
220 event_next = gdk_event_get();
221 // while the next event is also a key notify with the same keyval and mask,
222 while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
223 && event_next->key.keyval == keyval
224 && (!mask || (event_next->key.state & mask))) {
225 if (event_next->type == GDK_KEY_PRESS)
226 i ++;
227 // kill it
228 gdk_event_free(event_next);
229 // get next
230 event_next = gdk_event_get();
231 }
232 // otherwise, put it back onto the queue
233 if (event_next) gdk_event_put(event_next);
235 return i;
236 }
238 /**
239 * \brief Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
240 */
241 gint gobble_motion_events(gint mask)
242 {
243 GdkEvent *event_next;
244 gint i = 0;
246 event_next = gdk_event_get();
247 // while the next event is also a key notify with the same keyval and mask,
248 while (event_next && event_next->type == GDK_MOTION_NOTIFY
249 && (event_next->motion.state & mask)) {
250 // kill it
251 gdk_event_free(event_next);
252 // get next
253 event_next = gdk_event_get();
254 i ++;
255 }
256 // otherwise, put it back onto the queue
257 if (event_next) gdk_event_put(event_next);
259 return i;
260 }
262 /**
263 * Toggles current tool between active tool and selector tool.
264 * Subroutine of sp_event_context_private_root_handler().
265 */
266 static void
267 sp_toggle_selector(SPDesktop *dt)
268 {
269 if (!dt->event_context) return;
271 if (tools_isactive(dt, TOOLS_SELECT)) {
272 if (selector_toggled) {
273 if (switch_selector_to) tools_switch (dt, switch_selector_to);
274 selector_toggled = FALSE;
275 } else return;
276 } else {
277 selector_toggled = TRUE;
278 switch_selector_to = tools_active(dt);
279 tools_switch (dt, TOOLS_SELECT);
280 }
281 }
283 /**
284 * Toggles current tool between active tool and selector tool.
285 * Subroutine of sp_event_context_private_root_handler().
286 */
287 static void
288 sp_toggle_dropper(SPDesktop *dt)
289 {
290 if (!dt->event_context) return;
292 if (tools_isactive(dt, TOOLS_DROPPER)) {
293 if (dropper_toggled) {
294 if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
295 dropper_toggled = FALSE;
296 } else return;
297 } else {
298 dropper_toggled = TRUE;
299 switch_dropper_to = tools_active(dt);
300 tools_switch (dt, TOOLS_DROPPER);
301 }
302 }
304 /**
305 * Calculates and keeps track of scroll acceleration.
306 * Subroutine of sp_event_context_private_root_handler().
307 */
308 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
309 {
310 guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
312 /* key pressed within 500ms ? (1/2 second) */
313 if (time_diff > 500 || event->key.keyval != scroll_keyval) {
314 scroll_multiply = 1; // abort acceleration
315 } else {
316 scroll_multiply += acceleration; // continue acceleration
317 }
319 scroll_event_time = ((GdkEventKey *) event)->time;
320 scroll_keyval = event->key.keyval;
322 return scroll_multiply;
323 }
325 /**
326 * Main event dispatch, gets called from Gdk.
327 */
328 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
329 {
330 static NR::Point button_w;
331 static unsigned int panning = 0;
332 static unsigned int zoom_rb = 0;
334 SPDesktop *desktop = event_context->desktop;
336 tolerance = prefs_get_int_attribute_limited(
337 "options.dragtolerance","value", 0, 0, 100);
338 double const zoom_inc = prefs_get_double_attribute_limited(
339 "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
340 double const acceleration = prefs_get_double_attribute_limited(
341 "options.scrollingacceleration", "value", 0, 0, 6);
342 int const key_scroll = prefs_get_int_attribute_limited(
343 "options.keyscroll", "value", 10, 0, 1000);
344 int const wheel_scroll = prefs_get_int_attribute_limited(
345 "options.wheelscroll", "value", 40, 0, 1000);
347 gint ret = FALSE;
349 switch (event->type) {
350 case GDK_2BUTTON_PRESS:
351 if (panning) {
352 panning = 0;
353 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
354 event->button.time);
355 ret = TRUE;
356 } else {
357 /* sp_desktop_dialog(); */
358 }
359 break;
360 case GDK_BUTTON_PRESS:
362 // save drag origin
363 xp = (gint) event->button.x;
364 yp = (gint) event->button.y;
365 within_tolerance = true;
367 button_w = NR::Point(event->button.x, event->button.y);
369 switch (event->button.button) {
370 case 1:
371 if (event_context->space_panning) {
372 panning = 1;
373 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
374 GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
375 NULL, event->button.time-1);
376 ret = TRUE;
377 }
378 break;
379 case 2:
380 if (event->button.state == GDK_SHIFT_MASK) {
381 zoom_rb = 2;
382 } else {
383 panning = 2;
384 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
385 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
386 NULL, event->button.time-1);
387 }
388 ret = TRUE;
389 break;
390 case 3:
391 if (event->button.state & GDK_SHIFT_MASK
392 || event->button.state & GDK_CONTROL_MASK) {
393 panning = 3;
394 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
395 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
396 NULL, event->button.time);
397 ret = TRUE;
398 } else {
399 sp_event_root_menu_popup(desktop, NULL, event);
400 }
401 break;
402 default:
403 break;
404 }
405 break;
406 case GDK_MOTION_NOTIFY:
407 if (panning) {
408 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
409 || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
410 || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
411 ) {
412 /* Gdk seems to lose button release for us sometimes :-( */
413 panning = 0;
414 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
415 event->button.time);
416 ret = TRUE;
417 } else {
418 if ( within_tolerance
419 && ( abs( (gint) event->motion.x - xp ) < tolerance )
420 && ( abs( (gint) event->motion.y - yp ) < tolerance ))
421 {
422 // do not drag if we're within tolerance from origin
423 break;
424 }
425 // Once the user has moved farther than tolerance from
426 // the original location (indicating they intend to move
427 // the object, not click), then always process the motion
428 // notify coordinates as given (no snapping back to origin)
429 within_tolerance = false;
431 // gobble subsequent motion events to prevent "sticking"
432 // when scrolling is slow
433 gobble_motion_events(panning == 2 ?
434 GDK_BUTTON2_MASK :
435 (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
437 NR::Point const motion_w(event->motion.x, event->motion.y);
438 NR::Point const moved_w( motion_w - button_w );
439 event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
440 ret = TRUE;
441 }
442 } else if (zoom_rb) {
443 NR::Point const motion_w(event->motion.x, event->motion.y);
444 NR::Point const motion_dt(desktop->w2d(motion_w));
446 if ( within_tolerance
447 && ( abs( (gint) event->motion.x - xp ) < tolerance )
448 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
449 break; // do not drag if we're within tolerance from origin
450 }
451 // Once the user has moved farther than tolerance from the original location
452 // (indicating they intend to move the object, not click), then always process the
453 // motion notify coordinates as given (no snapping back to origin)
454 within_tolerance = false;
456 if (Inkscape::Rubberband::get()->is_started()) {
457 Inkscape::Rubberband::get()->move(motion_dt);
458 } else {
459 Inkscape::Rubberband::get()->start(desktop, motion_dt);
460 }
461 if (zoom_rb == 2)
462 gobble_motion_events(GDK_BUTTON2_MASK);
463 }
464 break;
465 case GDK_BUTTON_RELEASE:
466 xp = yp = 0;
467 if (within_tolerance && (panning || zoom_rb)) {
468 zoom_rb = 0;
469 if (panning) {
470 panning = 0;
471 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
472 event->button.time);
473 }
474 NR::Point const event_w(event->button.x, event->button.y);
475 NR::Point const event_dt(desktop->w2d(event_w));
476 desktop->zoom_relative_keep_point(event_dt,
477 (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
478 desktop->updateNow();
479 ret = TRUE;
480 } else if (panning == event->button.button) {
481 panning = 0;
482 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
483 event->button.time);
485 // in slow complex drawings, some of the motion events are lost;
486 // to make up for this, we scroll it once again to the button-up event coordinates
487 // (i.e. canvas will always get scrolled all the way to the mouse release point,
488 // even if few intermediate steps were visible)
489 NR::Point const motion_w(event->button.x, event->button.y);
490 NR::Point const moved_w( motion_w - button_w );
491 event_context->desktop->scroll_world(moved_w);
492 desktop->updateNow();
493 ret = TRUE;
494 } else if (zoom_rb == event->button.button) {
495 zoom_rb = 0;
496 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
497 Inkscape::Rubberband::get()->stop();
498 if (b && !within_tolerance) {
499 desktop->set_display_area(*b, 10);
500 }
501 ret = TRUE;
502 }
503 break;
504 case GDK_KEY_PRESS:
505 switch (get_group0_keyval(&event->key)) {
506 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
507 // in the editing window). So we resteal them back and run our regular shortcut
508 // invoker on them.
509 unsigned int shortcut;
510 case GDK_Tab:
511 case GDK_ISO_Left_Tab:
512 case GDK_F1:
513 shortcut = get_group0_keyval(&event->key);
514 if (event->key.state & GDK_SHIFT_MASK)
515 shortcut |= SP_SHORTCUT_SHIFT_MASK;
516 if (event->key.state & GDK_CONTROL_MASK)
517 shortcut |= SP_SHORTCUT_CONTROL_MASK;
518 if (event->key.state & GDK_MOD1_MASK)
519 shortcut |= SP_SHORTCUT_ALT_MASK;
520 ret = sp_shortcut_invoke(shortcut, desktop);
521 break;
523 case GDK_D:
524 case GDK_d:
525 sp_toggle_dropper(desktop);
526 ret = TRUE;
527 break;
528 case GDK_W:
529 case GDK_w:
530 case GDK_F4:
531 /* Close view */
532 if (MOD__CTRL_ONLY) {
533 sp_ui_close_view(NULL);
534 ret = TRUE;
535 }
536 break;
537 case GDK_Left: // Ctrl Left
538 case GDK_KP_Left:
539 case GDK_KP_4:
540 if (MOD__CTRL_ONLY) {
541 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
542 gobble_key_events(get_group0_keyval(&event->key),
543 GDK_CONTROL_MASK);
544 event_context->desktop->scroll_world(i, 0);
545 ret = TRUE;
546 }
547 break;
548 case GDK_Up: // Ctrl Up
549 case GDK_KP_Up:
550 case GDK_KP_8:
551 if (MOD__CTRL_ONLY) {
552 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
553 gobble_key_events(get_group0_keyval(&event->key),
554 GDK_CONTROL_MASK);
555 event_context->desktop->scroll_world(0, i);
556 ret = TRUE;
557 }
558 break;
559 case GDK_Right: // Ctrl Right
560 case GDK_KP_Right:
561 case GDK_KP_6:
562 if (MOD__CTRL_ONLY) {
563 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
564 gobble_key_events(get_group0_keyval(&event->key),
565 GDK_CONTROL_MASK);
566 event_context->desktop->scroll_world(-i, 0);
567 ret = TRUE;
568 }
569 break;
570 case GDK_Down: // Ctrl Down
571 case GDK_KP_Down:
572 case GDK_KP_2:
573 if (MOD__CTRL_ONLY) {
574 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
575 gobble_key_events(get_group0_keyval(&event->key),
576 GDK_CONTROL_MASK);
577 event_context->desktop->scroll_world(0, -i);
578 ret = TRUE;
579 }
580 break;
581 case GDK_F10:
582 if (MOD__SHIFT_ONLY) {
583 sp_event_root_menu_popup(desktop, NULL, event);
584 ret= TRUE;
585 }
586 break;
587 case GDK_space:
588 if (prefs_get_int_attribute("options.spacepans","value", 0) == 1) {
589 event_context->space_panning = true;
590 event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
591 ret= TRUE;
592 } else {
593 sp_toggle_selector(desktop);
594 ret= TRUE;
595 }
596 break;
597 case GDK_z:
598 case GDK_Z:
599 if (MOD__ALT_ONLY) {
600 desktop->zoom_grab_focus();
601 ret = TRUE;
602 }
603 break;
604 default:
605 break;
606 }
607 break;
608 case GDK_KEY_RELEASE:
609 switch (get_group0_keyval(&event->key)) {
610 case GDK_space:
611 if (event_context->space_panning) {
612 event_context->space_panning = false;
613 event_context->_message_context->clear();
614 if (panning == 1) {
615 panning = 0;
616 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
617 event->key.time);
618 desktop->updateNow();
619 }
620 ret= TRUE;
621 }
622 break;
623 default:
624 break;
625 }
626 break;
627 case GDK_SCROLL:
628 {
629 bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
630 bool wheelzooms = (prefs_get_int_attribute("options.wheelzooms","value", 0) == 1);
631 /* shift + wheel, pan left--right */
632 if (event->scroll.state & GDK_SHIFT_MASK) {
633 switch (event->scroll.direction) {
634 case GDK_SCROLL_UP:
635 desktop->scroll_world(wheel_scroll, 0);
636 break;
637 case GDK_SCROLL_DOWN:
638 desktop->scroll_world(-wheel_scroll, 0);
639 break;
640 default:
641 break;
642 }
644 /* ctrl + wheel, zoom in--out */
645 } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
646 double rel_zoom;
647 switch (event->scroll.direction) {
648 case GDK_SCROLL_UP:
649 rel_zoom = zoom_inc;
650 break;
651 case GDK_SCROLL_DOWN:
652 rel_zoom = 1 / zoom_inc;
653 break;
654 default:
655 rel_zoom = 0.0;
656 break;
657 }
658 if (rel_zoom != 0.0) {
659 NR::Point const scroll_dt = desktop->point();
660 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
661 }
663 /* no modifier, pan up--down (left--right on multiwheel mice?) */
664 } else {
665 switch (event->scroll.direction) {
666 case GDK_SCROLL_UP:
667 desktop->scroll_world(0, wheel_scroll);
668 break;
669 case GDK_SCROLL_DOWN:
670 desktop->scroll_world(0, -wheel_scroll);
671 break;
672 case GDK_SCROLL_LEFT:
673 desktop->scroll_world(wheel_scroll, 0);
674 break;
675 case GDK_SCROLL_RIGHT:
676 desktop->scroll_world(-wheel_scroll, 0);
677 break;
678 }
679 }
680 break;
681 }
682 default:
683 break;
684 }
686 return ret;
687 }
689 /**
690 * Handles item specific events. Gets called from Gdk.
691 *
692 * Only reacts to right mouse button at the moment.
693 * \todo Fixme: do context sensitive popup menu on items.
694 */
695 gint
696 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
697 {
698 int ret = FALSE;
700 switch (event->type) {
701 case GDK_BUTTON_PRESS:
702 if ((event->button.button == 3)
703 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
704 sp_event_root_menu_popup(ec->desktop, item, event);
705 ret = TRUE;
706 }
707 break;
708 default:
709 break;
710 }
712 return ret;
713 }
715 /**
716 * Gets called when attribute changes value.
717 */
718 static void
719 sp_ec_repr_attr_changed(Inkscape::XML::Node */*prefs_repr*/, gchar const *key, gchar const */*oldval*/, gchar const *newval,
720 bool /*is_interactive*/, gpointer data)
721 {
722 SPEventContext *ec;
724 ec = SP_EVENT_CONTEXT(data);
726 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
727 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
728 }
729 }
731 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
732 NULL, /* Child added */
733 NULL, /* Child removed */
734 sp_ec_repr_attr_changed,
735 NULL, /* Content changed */
736 NULL /* Order changed */
737 };
739 /**
740 * Creates new SPEventContext object and calls its virtual setup() function.
741 */
742 SPEventContext *
743 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
744 {
745 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
746 g_return_val_if_fail(desktop != NULL, NULL);
748 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
750 ec->desktop = desktop;
751 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
752 ec->key = key;
753 ec->prefs_repr = prefs_repr;
754 if (ec->prefs_repr) {
755 Inkscape::GC::anchor(ec->prefs_repr);
756 sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
757 }
759 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
760 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
762 return ec;
763 }
765 /**
766 * Finishes SPEventContext.
767 */
768 void
769 sp_event_context_finish(SPEventContext *ec)
770 {
771 g_return_if_fail(ec != NULL);
772 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
774 ec->enableSelectionCue(false);
776 if (ec->next) {
777 g_warning("Finishing event context with active link\n");
778 }
780 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
781 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
782 }
784 //-------------------------------member functions
786 /**
787 * Enables/disables the SPEventContext's SelCue.
788 */
789 void SPEventContext::enableSelectionCue(bool enable) {
790 if (enable) {
791 if (!_selcue) {
792 _selcue = new Inkscape::SelCue(desktop);
793 }
794 } else {
795 delete _selcue;
796 _selcue = NULL;
797 }
798 }
800 /**
801 * Enables/disables the SPEventContext's GrDrag.
802 */
803 void SPEventContext::enableGrDrag(bool enable) {
804 if (enable) {
805 if (!_grdrag) {
806 _grdrag = new GrDrag(desktop);
807 }
808 } else {
809 if (_grdrag) {
810 delete _grdrag;
811 _grdrag = NULL;
812 }
813 }
814 }
816 /**
817 * Calls virtual set() function of SPEventContext.
818 */
819 void
820 sp_event_context_read(SPEventContext *ec, gchar const *key)
821 {
822 g_return_if_fail(ec != NULL);
823 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
824 g_return_if_fail(key != NULL);
826 if (ec->prefs_repr) {
827 gchar const *val = ec->prefs_repr->attribute(key);
828 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
829 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
830 }
831 }
833 /**
834 * Calls virtual activate() function of SPEventContext.
835 */
836 void
837 sp_event_context_activate(SPEventContext *ec)
838 {
839 g_return_if_fail(ec != NULL);
840 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
842 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
843 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
844 }
846 /**
847 * Calls virtual deactivate() function of SPEventContext.
848 */
849 void
850 sp_event_context_deactivate(SPEventContext *ec)
851 {
852 g_return_if_fail(ec != NULL);
853 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
855 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
856 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
857 }
859 /**
860 * Calls virtual root_handler(), the main event handling function.
861 */
862 gint
863 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
864 {
865 gint ret;
867 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
869 set_event_location(event_context->desktop, event);
871 return ret;
872 }
874 /**
875 * Calls virtual item_handler(), the item event handling function.
876 */
877 gint
878 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
879 {
880 gint ret;
882 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
884 if (! ret) {
885 ret = sp_event_context_root_handler(event_context, event);
886 } else {
887 set_event_location(event_context->desktop, event);
888 }
890 return ret;
891 }
893 /**
894 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
895 */
896 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
897 {
898 if (event->type != GDK_MOTION_NOTIFY) {
899 return;
900 }
902 NR::Point const button_w(event->button.x, event->button.y);
903 NR::Point const button_dt(desktop->w2d(button_w));
904 desktop-> setPosition (button_dt);
905 desktop->set_coordinate_status(button_dt);
906 }
908 //-------------------------------------------------------------------
909 /**
910 * Create popup menu and tell Gtk to show it.
911 */
912 void
913 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
914 {
915 GtkWidget *menu;
917 /* fixme: This is not what I want but works for now (Lauris) */
918 if (event->type == GDK_KEY_PRESS) {
919 item = sp_desktop_selection(desktop)->singleItem();
920 }
921 menu = sp_ui_context_menu(desktop, item);
922 gtk_widget_show(menu);
924 switch (event->type) {
925 case GDK_BUTTON_PRESS:
926 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
927 break;
928 case GDK_KEY_PRESS:
929 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
930 break;
931 default:
932 break;
933 }
934 }
936 /**
937 * Show tool context specific modifier tip.
938 */
939 void
940 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
941 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
942 gchar const *alt_tip)
943 {
944 guint keyval = get_group0_keyval(&event->key);
946 bool ctrl = ctrl_tip && (MOD__CTRL
947 || (keyval == GDK_Control_L)
948 || (keyval == GDK_Control_R));
949 bool shift = shift_tip
950 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
951 bool alt = alt_tip
952 && (MOD__ALT
953 || (keyval == GDK_Alt_L)
954 || (keyval == GDK_Alt_R)
955 || (keyval == GDK_Meta_L)
956 || (keyval == GDK_Meta_R));
958 gchar *tip = g_strdup_printf("%s%s%s%s%s",
959 ( ctrl ? ctrl_tip : "" ),
960 ( ctrl && (shift || alt) ? "; " : "" ),
961 ( shift ? shift_tip : "" ),
962 ( (ctrl || shift) && alt ? "; " : "" ),
963 ( alt ? alt_tip : "" ));
965 if (strlen(tip) > 0) {
966 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
967 }
969 g_free(tip);
970 }
972 /**
973 * Return the keyval corresponding to the key event in group 0, i.e.,
974 * in the main (English) layout.
975 *
976 * Use this instead of simply event->keyval, so that your keyboard shortcuts
977 * work regardless of layouts (e.g., in Cyrillic).
978 */
979 guint
980 get_group0_keyval(GdkEventKey *event)
981 {
982 guint keyval = 0;
983 gdk_keymap_translate_keyboard_state(
984 gdk_keymap_get_for_display(gdk_display_get_default()),
985 event->hardware_keycode,
986 (GdkModifierType) event->state,
987 0 /*event->key.group*/,
988 &keyval, NULL, NULL, NULL);
989 return keyval;
990 }
992 /**
993 * Returns item at point p in desktop.
994 *
995 * If state includes alt key mask, cyclically selects under; honors
996 * into_groups.
997 */
998 SPItem *
999 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
1000 bool select_under, bool into_groups)
1001 {
1002 SPItem *item;
1004 if (select_under) {
1005 SPItem *selected_at_point =
1006 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1007 item = desktop->item_at_point(p, into_groups, selected_at_point);
1008 if (item == NULL) { // we may have reached bottom, flip over to the top
1009 item = desktop->item_at_point(p, into_groups, NULL);
1010 }
1011 } else
1012 item = desktop->item_at_point(p, into_groups, NULL);
1014 return item;
1015 }
1017 /**
1018 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1019 *
1020 * Honors into_groups.
1021 */
1022 SPItem *
1023 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
1024 {
1025 GSList *temp = NULL;
1026 temp = g_slist_prepend (temp, item);
1027 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1028 g_slist_free (temp);
1030 return item_at_point;
1031 }
1033 /**
1034 * Called when SPEventContext subclass node attribute changed.
1035 */
1036 void
1037 ec_shape_event_attr_changed(Inkscape::XML::Node */*shape_repr*/, gchar const *name,
1038 gchar const */*old_value*/, gchar const */*new_value*/,
1039 bool const /*is_interactive*/, gpointer const data)
1040 {
1041 if (!name
1042 || !strcmp(name, "style")
1043 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1044 // no need to regenrate knotholder if only style changed
1045 return;
1046 }
1048 SPEventContext *ec = SP_EVENT_CONTEXT(data);
1050 if (ec->shape_knot_holder) {
1051 sp_knot_holder_destroy(ec->shape_knot_holder);
1052 }
1053 ec->shape_knot_holder = NULL;
1055 SPDesktop *desktop = ec->desktop;
1057 SPItem *item = sp_desktop_selection(desktop)->singleItem();
1059 if (item) {
1060 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1061 }
1062 }
1065 /*
1066 Local Variables:
1067 mode:c++
1068 c-file-style:"stroustrup"
1069 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1070 indent-tabs-mode:nil
1071 fill-column:99
1072 End:
1073 */
1074 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :