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 if (MOD__SHIFT && MOD__CTRL) {
526 // don't catch Shift+Ctrl+D (Document Properties dialog)
527 break;
528 }
529 sp_toggle_dropper(desktop);
530 ret = TRUE;
531 break;
532 case GDK_W:
533 case GDK_w:
534 case GDK_F4:
535 /* Close view */
536 if (MOD__CTRL_ONLY) {
537 sp_ui_close_view(NULL);
538 ret = TRUE;
539 }
540 break;
541 case GDK_Left: // Ctrl Left
542 case GDK_KP_Left:
543 case GDK_KP_4:
544 if (MOD__CTRL_ONLY) {
545 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
546 gobble_key_events(get_group0_keyval(&event->key),
547 GDK_CONTROL_MASK);
548 event_context->desktop->scroll_world(i, 0);
549 ret = TRUE;
550 }
551 break;
552 case GDK_Up: // Ctrl Up
553 case GDK_KP_Up:
554 case GDK_KP_8:
555 if (MOD__CTRL_ONLY) {
556 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
557 gobble_key_events(get_group0_keyval(&event->key),
558 GDK_CONTROL_MASK);
559 event_context->desktop->scroll_world(0, i);
560 ret = TRUE;
561 }
562 break;
563 case GDK_Right: // Ctrl Right
564 case GDK_KP_Right:
565 case GDK_KP_6:
566 if (MOD__CTRL_ONLY) {
567 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
568 gobble_key_events(get_group0_keyval(&event->key),
569 GDK_CONTROL_MASK);
570 event_context->desktop->scroll_world(-i, 0);
571 ret = TRUE;
572 }
573 break;
574 case GDK_Down: // Ctrl Down
575 case GDK_KP_Down:
576 case GDK_KP_2:
577 if (MOD__CTRL_ONLY) {
578 int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
579 gobble_key_events(get_group0_keyval(&event->key),
580 GDK_CONTROL_MASK);
581 event_context->desktop->scroll_world(0, -i);
582 ret = TRUE;
583 }
584 break;
585 case GDK_F10:
586 if (MOD__SHIFT_ONLY) {
587 sp_event_root_menu_popup(desktop, NULL, event);
588 ret= TRUE;
589 }
590 break;
591 case GDK_space:
592 if (prefs_get_int_attribute("options.spacepans","value", 0) == 1) {
593 event_context->space_panning = true;
594 event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
595 ret= TRUE;
596 } else {
597 sp_toggle_selector(desktop);
598 ret= TRUE;
599 }
600 break;
601 case GDK_z:
602 case GDK_Z:
603 if (MOD__ALT_ONLY) {
604 desktop->zoom_grab_focus();
605 ret = TRUE;
606 }
607 break;
608 default:
609 break;
610 }
611 break;
612 case GDK_KEY_RELEASE:
613 switch (get_group0_keyval(&event->key)) {
614 case GDK_space:
615 if (event_context->space_panning) {
616 event_context->space_panning = false;
617 event_context->_message_context->clear();
618 if (panning == 1) {
619 panning = 0;
620 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
621 event->key.time);
622 desktop->updateNow();
623 }
624 ret= TRUE;
625 }
626 break;
627 default:
628 break;
629 }
630 break;
631 case GDK_SCROLL:
632 {
633 bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
634 bool wheelzooms = (prefs_get_int_attribute("options.wheelzooms","value", 0) == 1);
635 /* shift + wheel, pan left--right */
636 if (event->scroll.state & GDK_SHIFT_MASK) {
637 switch (event->scroll.direction) {
638 case GDK_SCROLL_UP:
639 desktop->scroll_world(wheel_scroll, 0);
640 break;
641 case GDK_SCROLL_DOWN:
642 desktop->scroll_world(-wheel_scroll, 0);
643 break;
644 default:
645 break;
646 }
648 /* ctrl + wheel, zoom in--out */
649 } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
650 double rel_zoom;
651 switch (event->scroll.direction) {
652 case GDK_SCROLL_UP:
653 rel_zoom = zoom_inc;
654 break;
655 case GDK_SCROLL_DOWN:
656 rel_zoom = 1 / zoom_inc;
657 break;
658 default:
659 rel_zoom = 0.0;
660 break;
661 }
662 if (rel_zoom != 0.0) {
663 NR::Point const scroll_dt = desktop->point();
664 desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
665 }
667 /* no modifier, pan up--down (left--right on multiwheel mice?) */
668 } else {
669 switch (event->scroll.direction) {
670 case GDK_SCROLL_UP:
671 desktop->scroll_world(0, wheel_scroll);
672 break;
673 case GDK_SCROLL_DOWN:
674 desktop->scroll_world(0, -wheel_scroll);
675 break;
676 case GDK_SCROLL_LEFT:
677 desktop->scroll_world(wheel_scroll, 0);
678 break;
679 case GDK_SCROLL_RIGHT:
680 desktop->scroll_world(-wheel_scroll, 0);
681 break;
682 }
683 }
684 break;
685 }
686 default:
687 break;
688 }
690 return ret;
691 }
693 /**
694 * Handles item specific events. Gets called from Gdk.
695 *
696 * Only reacts to right mouse button at the moment.
697 * \todo Fixme: do context sensitive popup menu on items.
698 */
699 gint
700 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
701 {
702 int ret = FALSE;
704 switch (event->type) {
705 case GDK_BUTTON_PRESS:
706 if ((event->button.button == 3)
707 && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
708 sp_event_root_menu_popup(ec->desktop, item, event);
709 ret = TRUE;
710 }
711 break;
712 default:
713 break;
714 }
716 return ret;
717 }
719 /**
720 * Gets called when attribute changes value.
721 */
722 static void
723 sp_ec_repr_attr_changed(Inkscape::XML::Node */*prefs_repr*/, gchar const *key, gchar const */*oldval*/, gchar const *newval,
724 bool /*is_interactive*/, gpointer data)
725 {
726 SPEventContext *ec;
728 ec = SP_EVENT_CONTEXT(data);
730 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
731 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
732 }
733 }
735 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
736 NULL, /* Child added */
737 NULL, /* Child removed */
738 sp_ec_repr_attr_changed,
739 NULL, /* Content changed */
740 NULL /* Order changed */
741 };
743 /**
744 * Creates new SPEventContext object and calls its virtual setup() function.
745 */
746 SPEventContext *
747 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
748 {
749 g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
750 g_return_val_if_fail(desktop != NULL, NULL);
752 SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
754 ec->desktop = desktop;
755 ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
756 ec->key = key;
757 ec->prefs_repr = prefs_repr;
758 if (ec->prefs_repr) {
759 Inkscape::GC::anchor(ec->prefs_repr);
760 sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
761 }
763 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
764 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
766 return ec;
767 }
769 /**
770 * Finishes SPEventContext.
771 */
772 void
773 sp_event_context_finish(SPEventContext *ec)
774 {
775 g_return_if_fail(ec != NULL);
776 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
778 ec->enableSelectionCue(false);
780 if (ec->next) {
781 g_warning("Finishing event context with active link\n");
782 }
784 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
785 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
786 }
788 //-------------------------------member functions
790 /**
791 * Enables/disables the SPEventContext's SelCue.
792 */
793 void SPEventContext::enableSelectionCue(bool enable) {
794 if (enable) {
795 if (!_selcue) {
796 _selcue = new Inkscape::SelCue(desktop);
797 }
798 } else {
799 delete _selcue;
800 _selcue = NULL;
801 }
802 }
804 /**
805 * Enables/disables the SPEventContext's GrDrag.
806 */
807 void SPEventContext::enableGrDrag(bool enable) {
808 if (enable) {
809 if (!_grdrag) {
810 _grdrag = new GrDrag(desktop);
811 }
812 } else {
813 if (_grdrag) {
814 delete _grdrag;
815 _grdrag = NULL;
816 }
817 }
818 }
820 /**
821 * Calls virtual set() function of SPEventContext.
822 */
823 void
824 sp_event_context_read(SPEventContext *ec, gchar const *key)
825 {
826 g_return_if_fail(ec != NULL);
827 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
828 g_return_if_fail(key != NULL);
830 if (ec->prefs_repr) {
831 gchar const *val = ec->prefs_repr->attribute(key);
832 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
833 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
834 }
835 }
837 /**
838 * Calls virtual activate() function of SPEventContext.
839 */
840 void
841 sp_event_context_activate(SPEventContext *ec)
842 {
843 g_return_if_fail(ec != NULL);
844 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
846 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
847 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
848 }
850 /**
851 * Calls virtual deactivate() function of SPEventContext.
852 */
853 void
854 sp_event_context_deactivate(SPEventContext *ec)
855 {
856 g_return_if_fail(ec != NULL);
857 g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
859 if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
860 ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
861 }
863 /**
864 * Calls virtual root_handler(), the main event handling function.
865 */
866 gint
867 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
868 {
869 gint ret;
871 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
873 set_event_location(event_context->desktop, event);
875 return ret;
876 }
878 /**
879 * Calls virtual item_handler(), the item event handling function.
880 */
881 gint
882 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
883 {
884 gint ret;
886 ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
888 if (! ret) {
889 ret = sp_event_context_root_handler(event_context, event);
890 } else {
891 set_event_location(event_context->desktop, event);
892 }
894 return ret;
895 }
897 /**
898 * Emits 'position_set' signal on desktop and shows coordinates on status bar.
899 */
900 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
901 {
902 if (event->type != GDK_MOTION_NOTIFY) {
903 return;
904 }
906 NR::Point const button_w(event->button.x, event->button.y);
907 NR::Point const button_dt(desktop->w2d(button_w));
908 desktop-> setPosition (button_dt);
909 desktop->set_coordinate_status(button_dt);
910 }
912 //-------------------------------------------------------------------
913 /**
914 * Create popup menu and tell Gtk to show it.
915 */
916 void
917 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
918 {
919 GtkWidget *menu;
921 /* fixme: This is not what I want but works for now (Lauris) */
922 if (event->type == GDK_KEY_PRESS) {
923 item = sp_desktop_selection(desktop)->singleItem();
924 }
925 menu = sp_ui_context_menu(desktop, item);
926 gtk_widget_show(menu);
928 switch (event->type) {
929 case GDK_BUTTON_PRESS:
930 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
931 break;
932 case GDK_KEY_PRESS:
933 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
934 break;
935 default:
936 break;
937 }
938 }
940 /**
941 * Show tool context specific modifier tip.
942 */
943 void
944 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
945 GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
946 gchar const *alt_tip)
947 {
948 guint keyval = get_group0_keyval(&event->key);
950 bool ctrl = ctrl_tip && (MOD__CTRL
951 || (keyval == GDK_Control_L)
952 || (keyval == GDK_Control_R));
953 bool shift = shift_tip
954 && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
955 bool alt = alt_tip
956 && (MOD__ALT
957 || (keyval == GDK_Alt_L)
958 || (keyval == GDK_Alt_R)
959 || (keyval == GDK_Meta_L)
960 || (keyval == GDK_Meta_R));
962 gchar *tip = g_strdup_printf("%s%s%s%s%s",
963 ( ctrl ? ctrl_tip : "" ),
964 ( ctrl && (shift || alt) ? "; " : "" ),
965 ( shift ? shift_tip : "" ),
966 ( (ctrl || shift) && alt ? "; " : "" ),
967 ( alt ? alt_tip : "" ));
969 if (strlen(tip) > 0) {
970 message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
971 }
973 g_free(tip);
974 }
976 /**
977 * Return the keyval corresponding to the key event in group 0, i.e.,
978 * in the main (English) layout.
979 *
980 * Use this instead of simply event->keyval, so that your keyboard shortcuts
981 * work regardless of layouts (e.g., in Cyrillic).
982 */
983 guint
984 get_group0_keyval(GdkEventKey *event)
985 {
986 guint keyval = 0;
987 gdk_keymap_translate_keyboard_state(
988 gdk_keymap_get_for_display(gdk_display_get_default()),
989 event->hardware_keycode,
990 (GdkModifierType) event->state,
991 0 /*event->key.group*/,
992 &keyval, NULL, NULL, NULL);
993 return keyval;
994 }
996 /**
997 * Returns item at point p in desktop.
998 *
999 * If state includes alt key mask, cyclically selects under; honors
1000 * into_groups.
1001 */
1002 SPItem *
1003 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
1004 bool select_under, bool into_groups)
1005 {
1006 SPItem *item;
1008 if (select_under) {
1009 SPItem *selected_at_point =
1010 desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1011 item = desktop->item_at_point(p, into_groups, selected_at_point);
1012 if (item == NULL) { // we may have reached bottom, flip over to the top
1013 item = desktop->item_at_point(p, into_groups, NULL);
1014 }
1015 } else
1016 item = desktop->item_at_point(p, into_groups, NULL);
1018 return item;
1019 }
1021 /**
1022 * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1023 *
1024 * Honors into_groups.
1025 */
1026 SPItem *
1027 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
1028 {
1029 GSList *temp = NULL;
1030 temp = g_slist_prepend (temp, item);
1031 SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1032 g_slist_free (temp);
1034 return item_at_point;
1035 }
1037 /**
1038 * Called when SPEventContext subclass node attribute changed.
1039 */
1040 void
1041 ec_shape_event_attr_changed(Inkscape::XML::Node */*shape_repr*/, gchar const *name,
1042 gchar const */*old_value*/, gchar const */*new_value*/,
1043 bool const /*is_interactive*/, gpointer const data)
1044 {
1045 if (!name
1046 || !strcmp(name, "style")
1047 || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1048 // no need to regenrate knotholder if only style changed
1049 return;
1050 }
1052 SPEventContext *ec = SP_EVENT_CONTEXT(data);
1054 if (ec->shape_knot_holder) {
1055 sp_knot_holder_destroy(ec->shape_knot_holder);
1056 }
1057 ec->shape_knot_holder = NULL;
1059 SPDesktop *desktop = ec->desktop;
1061 SPItem *item = sp_desktop_selection(desktop)->singleItem();
1063 if (item) {
1064 ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1065 }
1066 }
1069 /*
1070 Local Variables:
1071 mode:c++
1072 c-file-style:"stroustrup"
1073 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1074 indent-tabs-mode:nil
1075 fill-column:99
1076 End:
1077 */
1078 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :