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