Code

47447e6a0fa4263a6c10e3b09158156529a4f20d
[inkscape.git] / src / event-context.cpp
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;
103 /**
104  * Callback to set up the SPEventContext vtable.
105  */
106 static void
107 sp_event_context_class_init(SPEventContextClass *klass)
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;
122 /**
123  * Clears all SPEventContext object members.
124  */
125 static void
126 sp_event_context_init(SPEventContext *event_context)
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;
135 /**
136  * Callback to free and null member variables of SPEventContext object.
137  */
138 static void
139 sp_event_context_dispose(GObject *object)
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);
167 /**
168  * Recreates and draws cursor on desktop related to SPEventContext.
169  */
170 void
171 sp_event_context_update_cursor(SPEventContext *ec)
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     }
195 /**
196  * Callback that gets called on initialization of SPEventContext object.
197  * Redraws mouse cursor, at the moment.
198  */
199 static void
200 sp_event_context_private_setup(SPEventContext *ec)
202     sp_event_context_update_cursor(ec);
205 /**
206  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
207  */
208 gint gobble_key_events(guint keyval, gint mask)
210     GdkEvent *event_next;
211     gint i = 0;
213     event_next = gdk_event_get();
214     // while the next event is also a key notify with the same keyval and mask,
215     while (event_next && event_next->type == GDK_KEY_PRESS
216            && event_next->key.keyval == keyval
217            && (event_next->key.state & mask)) {
218         // kill it
219         gdk_event_free(event_next);
220         // get next
221         event_next = gdk_event_get();
222         i ++;
223     }
224     // otherwise, put it back onto the queue
225     if (event_next) gdk_event_put(event_next);
227     return i;
230 /**
231  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
232 */
233 gint gobble_motion_events(gint mask)
235     GdkEvent *event_next;
236     gint i = 0;
238     event_next = gdk_event_get();
239     // while the next event is also a key notify with the same keyval and mask,
240     while (event_next && event_next->type == GDK_MOTION_NOTIFY
241            && (event_next->motion.state & mask)) {
242         // kill it
243         gdk_event_free(event_next);
244         // get next
245         event_next = gdk_event_get();
246         i ++;
247     }
248     // otherwise, put it back onto the queue
249     if (event_next) gdk_event_put(event_next);
251     return i;
254 /**
255  * Toggles current tool between active tool and selector tool.
256  * Subroutine of sp_event_context_private_root_handler().
257  */
258 static void
259 sp_toggle_selector(SPDesktop *dt)
261     if (!dt->event_context) return;
263     if (tools_isactive(dt, TOOLS_SELECT)) {
264         if (selector_toggled) {
265             if (switch_selector_to) tools_switch (dt, switch_selector_to);
266             selector_toggled = FALSE;
267         } else return;
268     } else {
269         selector_toggled = TRUE;
270         switch_selector_to = tools_active(dt);
271         tools_switch (dt, TOOLS_SELECT);
272     }
275 /**
276  * Calculates and keeps track of scroll acceleration.
277  * Subroutine of sp_event_context_private_root_handler().
278  */
279 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration)
281     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
283     /* key pressed within 500ms ? (1/2 second) */
284     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
285         scroll_multiply = 1;
286     } else {
287         scroll_multiply += acceleration;
288     }
290     scroll_event_time = ((GdkEventKey *) event)->time;
291     scroll_keyval = event->key.keyval;
293     return scroll_multiply;
296 // This is a hack that is necessary because when middle-clicking too fast,
297 // button_press events come for all clicks but there's button_release only
298 // for the first one. So after a release, we must prohibit the next grab for
299 // some time, or the grab will be stuck.  Perhaps this is caused by some
300 // wrong handling of events among contexts and not by a GDK bug;
301 // if someone can fix this properly this would be great.
302 static gint dontgrab = 0;
303 static bool
304 grab_allow_again()
306     dontgrab--;
307     if (dontgrab < 0) dontgrab = 0;
308     return FALSE; // so that it is only called once
311 /**
312  * Main event dispatch, gets called from Gdk.
313  */
314 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
316     static NR::Point button_w;
317     static unsigned int panning = 0;
318     static unsigned int zoom_rb = 0;
320     SPDesktop *desktop = event_context->desktop;
322     tolerance = prefs_get_int_attribute_limited(
323             "options.dragtolerance","value", 0, 0, 100);
324     double const zoom_inc = prefs_get_double_attribute_limited(
325             "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
326     double const acceleration = prefs_get_double_attribute_limited(
327             "options.scrollingacceleration", "value", 0, 0, 6);
328     int const key_scroll = prefs_get_int_attribute_limited(
329             "options.keyscroll", "value", 10, 0, 1000);
330     int const wheel_scroll = prefs_get_int_attribute_limited(
331             "options.wheelscroll", "value", 40, 0, 1000);
333     gint ret = FALSE;
335     switch (event->type) {
336         case GDK_2BUTTON_PRESS:
337             if (panning) {
338                 panning = 0;
339                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
340                         event->button.time);
341                 ret = TRUE;
342             } else {
343                 /* sp_desktop_dialog(); */
344             }
345             break;
346         case GDK_BUTTON_PRESS:
348             // save drag origin
349             xp = (gint) event->button.x;
350             yp = (gint) event->button.y;
351             within_tolerance = true;
353             switch (event->button.button) {
354                 case 2:
356                     if (dontgrab)
357                         // double-click, still not permitted to grab;
358                         // increase the counter to guard against triple click
359                     {
360                         dontgrab ++;
361                         gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
362                         break;
363                     }
365                     button_w = NR::Point(event->button.x,
366                                          event->button.y);
367                     if (event->button.state == GDK_SHIFT_MASK) {
368                         zoom_rb = 2;
369                     } else {
370                         panning = 2;
371                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
372                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
373                             NULL, event->button.time-1);
374                     }
375                     ret = TRUE;
376                     break;
377                 case 3:
378                     if (event->button.state & GDK_SHIFT_MASK
379                             || event->button.state & GDK_CONTROL_MASK) {
380                         button_w = NR::Point(event->button.x,
381                                              event->button.y);
382                         panning = 3;
383                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
384                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
385                                 NULL, event->button.time);
386                         ret = TRUE;
387                     } else {
388                         sp_event_root_menu_popup(desktop, NULL, event);
389                     }
390                     break;
391                 default:
392                     break;
393             }
394             break;
395         case GDK_MOTION_NOTIFY:
396             if (panning) {
397                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
398                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))) {
399                     /* Gdk seems to lose button release for us sometimes :-( */
400                     panning = 0;
401                     dontgrab = 0;
402                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
403                             event->button.time);
404                     ret = TRUE;
405                 } else {
406                     if ( within_tolerance
407                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
408                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
409                     {
410                         // do not drag if we're within tolerance from origin
411                         break;
412                     }
413                     // Once the user has moved farther than tolerance from
414                     // the original location (indicating they intend to move
415                     // the object, not click), then always process the motion
416                     // notify coordinates as given (no snapping back to origin)
417                     within_tolerance = false;
419                     // gobble subsequent motion events to prevent "sticking"
420                     // when scrolling is slow
421                     gobble_motion_events(panning == 2 ?
422                             GDK_BUTTON2_MASK : GDK_BUTTON3_MASK);
424                     NR::Point const motion_w(event->motion.x,
425                                              event->motion.y);
426                     NR::Point const moved_w( motion_w - button_w );
427                     event_context->desktop->scroll_world(moved_w);
428                     ret = TRUE;
429                 }
430             } else if (zoom_rb) {
431                 NR::Point const motion_w(event->motion.x, event->motion.y);
432                 NR::Point const motion_dt(desktop->w2d(motion_w));
434                 if ( within_tolerance
435                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
436                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
437                     break; // do not drag if we're within tolerance from origin
438                 }
440                 if (within_tolerance) {
441                     Inkscape::Rubberband::get()->start(desktop, motion_dt);
442                 } else {
443                     Inkscape::Rubberband::get()->move(motion_dt);
444                 }
446                 // Once the user has moved farther than tolerance from the original location
447                 // (indicating they intend to move the object, not click), then always process the
448                 // motion notify coordinates as given (no snapping back to origin)
449                 within_tolerance = false;
450             }
451             break;
452         case GDK_BUTTON_RELEASE:
453             if (within_tolerance && (panning || zoom_rb)) {
454                 dontgrab ++;
455                 NR::Point const event_w(event->button.x, event->button.y);
456                 NR::Point const event_dt(desktop->w2d(event_w));
457                 double const zoom_power = ( (event->button.state & GDK_SHIFT_MASK)
458                                             ? -dontgrab : dontgrab );
459                 desktop->zoom_relative_keep_point(event_dt,
460                                                   pow(zoom_inc, zoom_power));
461                 gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
462                 desktop->updateNow();
463             }
464             if (panning == event->button.button) {
465                 panning = 0;
466                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
467                                       event->button.time);
468                 desktop->updateNow();
469                 ret = TRUE;
470             } else if (zoom_rb == event->button.button) {
471                 zoom_rb = 0;
472                 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
473                 if (b != NR::Nothing() && !within_tolerance) {
474                     desktop->set_display_area(b.assume(), 10);
475                 }
476                 Inkscape::Rubberband::get()->stop();
477             }
478             xp = yp = 0;
479             break;
480         case GDK_KEY_PRESS:
481             switch (get_group0_keyval(&event->key)) {
482                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
483                 // in the editing window). So we resteal them back and run our regular shortcut
484                 // invoker on them.
485                 unsigned int shortcut;
486                 case GDK_Tab: 
487                 case GDK_ISO_Left_Tab: 
488                 case GDK_F1:
489                     shortcut = get_group0_keyval(&event->key);
490                     if (event->key.state & GDK_SHIFT_MASK)
491                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
492                     if (event->key.state & GDK_CONTROL_MASK)
493                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
494                     if (event->key.state & GDK_MOD1_MASK)
495                         shortcut |= SP_SHORTCUT_ALT_MASK;
496                     ret = sp_shortcut_invoke(shortcut, desktop);
497                     break;
499                 case GDK_W:
500                 case GDK_w:
501                 case GDK_F4:
502                     /* Close view */
503                     if (MOD__CTRL_ONLY) {
504                         sp_ui_close_view(NULL);
505                         ret = TRUE;
506                     }
507                     break;
508                 case GDK_Left: // Ctrl Left
509                 case GDK_KP_Left:
510                 case GDK_KP_4:
511                     if (MOD__CTRL_ONLY) {
512                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
513                         gobble_key_events(get_group0_keyval(&event->key),
514                                 GDK_CONTROL_MASK);
515                         event_context->desktop->scroll_world(i, 0);
516                         ret = TRUE;
517                     }
518                     break;
519                 case GDK_Up: // Ctrl Up
520                 case GDK_KP_Up:
521                 case GDK_KP_8:
522                     if (MOD__CTRL_ONLY) {
523                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
524                         gobble_key_events(get_group0_keyval(&event->key),
525                                 GDK_CONTROL_MASK);
526                         event_context->desktop->scroll_world(0, i);
527                         ret = TRUE;
528                     }
529                     break;
530                 case GDK_Right: // Ctrl Right
531                 case GDK_KP_Right:
532                 case GDK_KP_6:
533                     if (MOD__CTRL_ONLY) {
534                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
535                         gobble_key_events(get_group0_keyval(&event->key),
536                                 GDK_CONTROL_MASK);
537                         event_context->desktop->scroll_world(-i, 0);
538                         ret = TRUE;
539                     }
540                     break;
541                 case GDK_Down: // Ctrl Down
542                 case GDK_KP_Down:
543                 case GDK_KP_2:
544                     if (MOD__CTRL_ONLY) {
545                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
546                         gobble_key_events(get_group0_keyval(&event->key),
547                                 GDK_CONTROL_MASK);
548                         event_context->desktop->scroll_world(0, -i);
549                         ret = TRUE;
550                     }
551                     break;
552                 case GDK_F10:
553                     if (MOD__SHIFT_ONLY) {
554                         sp_event_root_menu_popup(desktop, NULL, event);
555                         ret= TRUE;
556                     }
557                     break;
558                 case GDK_space:
559                     sp_toggle_selector(desktop);
560                     ret= TRUE;
561                     break;
562                 case GDK_z:
563                 case GDK_Z:
564                     if (MOD__ALT_ONLY) {
565                         desktop->zoom_grab_focus();
566                         ret = TRUE;
567                     }
568                     break;
569                 default:
570                     break;
571             }
572             break;
573         case GDK_SCROLL:
574             /* shift + wheel, pan left--right */
575             if (event->scroll.state & GDK_SHIFT_MASK) {
576                 switch (event->scroll.direction) {
577                     case GDK_SCROLL_UP:
578                         desktop->scroll_world(wheel_scroll, 0);
579                         break;
580                     case GDK_SCROLL_DOWN:
581                         desktop->scroll_world(-wheel_scroll, 0);
582                         break;
583                     default:
584                         break;
585                 }
587                 /* ctrl + wheel, zoom in--out */
588             } else if (event->scroll.state & GDK_CONTROL_MASK) {
589                 double rel_zoom;
590                 switch (event->scroll.direction) {
591                     case GDK_SCROLL_UP:
592                         rel_zoom = zoom_inc;
593                         break;
594                     case GDK_SCROLL_DOWN:
595                         rel_zoom = 1 / zoom_inc;
596                         break;
597                     default:
598                         rel_zoom = 0.0;
599                         break;
600                 }
601                 if (rel_zoom != 0.0) {
602                     NR::Point const scroll_dt = desktop->point();
603                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
604                 }
606                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
607             } else {
608                 switch (event->scroll.direction) {
609                     case GDK_SCROLL_UP:
610                         desktop->scroll_world(0, wheel_scroll);
611                         break;
612                     case GDK_SCROLL_DOWN:
613                         desktop->scroll_world(0, -wheel_scroll);
614                         break;
615                     case GDK_SCROLL_LEFT:
616                         desktop->scroll_world(wheel_scroll, 0);
617                         break;
618                     case GDK_SCROLL_RIGHT:
619                         desktop->scroll_world(-wheel_scroll, 0);
620                         break;
621                 }
622             }
623             break;
624         default:
625             break;
626     }
628     return ret;
631 /**
632  * Handles item specific events. Gets called from Gdk.
633  *
634  * Only reacts to right mouse button at the moment.
635  * \todo Fixme: do context sensitive popup menu on items.
636  */
637 gint
638 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
640     int ret = FALSE;
642     switch (event->type) {
643         case GDK_BUTTON_PRESS:
644             if ((event->button.button == 3)
645                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
646                 sp_event_root_menu_popup(ec->desktop, item, event);
647                 ret = TRUE;
648             }
649             break;
650         default:
651             break;
652     }
654     return ret;
657 /**
658  * Gets called when attribute changes value.
659  */
660 static void
661 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
662                         bool is_interactive, gpointer data)
664     SPEventContext *ec;
666     ec = SP_EVENT_CONTEXT(data);
668     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
669         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
670     }
673 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
674     NULL, /* Child added */
675     NULL, /* Child removed */
676     sp_ec_repr_attr_changed,
677     NULL, /* Content changed */
678     NULL /* Order changed */
679 };
681 /**
682  * Creates new SPEventContext object and calls its virtual setup() function.
683  */
684 SPEventContext *
685 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
687     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
688     g_return_val_if_fail(desktop != NULL, NULL);
690     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
692     ec->desktop = desktop;
693     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
694     ec->key = key;
695     ec->prefs_repr = prefs_repr;
696     if (ec->prefs_repr) {
697         Inkscape::GC::anchor(ec->prefs_repr);
698         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
699     }
701     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
702         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
704     return ec;
707 /**
708  * Finishes SPEventContext.
709  */
710 void
711 sp_event_context_finish(SPEventContext *ec)
713     g_return_if_fail(ec != NULL);
714     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
716     ec->enableSelectionCue(false);
718     if (ec->next) {
719         g_warning("Finishing event context with active link\n");
720     }
722     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
723         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
726 //-------------------------------member functions
728 /**
729  * Enables/disables the SPEventContext's SelCue.
730  */
731 void SPEventContext::enableSelectionCue(bool enable) {
732     if (enable) {
733         if (!_selcue) {
734             _selcue = new Inkscape::SelCue(desktop);
735         }
736     } else {
737         delete _selcue;
738         _selcue = NULL;
739     }
742 /**
743  * Enables/disables the SPEventContext's GrDrag.
744  */
745 void SPEventContext::enableGrDrag(bool enable) {
746     if (enable) {
747         if (!_grdrag) {
748             _grdrag = new GrDrag(desktop);
749         }
750     } else {
751         if (_grdrag) {
752             delete _grdrag;
753             _grdrag = NULL;
754         }
755     }
758 /**
759  * Calls virtual set() function of SPEventContext.
760  */
761 void
762 sp_event_context_read(SPEventContext *ec, gchar const *key)
764     g_return_if_fail(ec != NULL);
765     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
766     g_return_if_fail(key != NULL);
768     if (ec->prefs_repr) {
769         gchar const *val = ec->prefs_repr->attribute(key);
770         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
771             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
772     }
775 /**
776  * Calls virtual activate() function of SPEventContext.
777  */
778 void
779 sp_event_context_activate(SPEventContext *ec)
781     g_return_if_fail(ec != NULL);
782     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
784     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
785         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
788 /**
789  * Calls virtual deactivate() function of SPEventContext.
790  */
791 void
792 sp_event_context_deactivate(SPEventContext *ec)
794     g_return_if_fail(ec != NULL);
795     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
797     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
798         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
801 /**
802  * Calls virtual root_handler(), the main event handling function.
803  */
804 gint
805 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
807     gint ret;
809     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
811     set_event_location(event_context->desktop, event);
813     return ret;
816 /**
817  * Calls virtual item_handler(), the item event handling function.
818  */
819 gint
820 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
822     gint ret;
824     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
826     if (! ret) {
827         ret = sp_event_context_root_handler(event_context, event);
828     } else {
829         set_event_location(event_context->desktop, event);
830     }
832     return ret;
835 /**
836  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
837  */
838 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
840     if (event->type != GDK_MOTION_NOTIFY) {
841         return;
842     }
844     NR::Point const button_w(event->button.x, event->button.y);
845     NR::Point const button_dt(desktop->w2d(button_w));
846     desktop-> setPosition (button_dt);
847     desktop->set_coordinate_status(button_dt);
850 //-------------------------------------------------------------------
851 /**
852  * Create popup menu and tell Gtk to show it.
853  */
854 void
855 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
857     GtkWidget *menu;
859     /* fixme: This is not what I want but works for now (Lauris) */
860     if (event->type == GDK_KEY_PRESS) {
861         item = sp_desktop_selection(desktop)->singleItem();
862     }
863     menu = sp_ui_context_menu(desktop, item);
864     gtk_widget_show(menu);
866     switch (event->type) {
867         case GDK_BUTTON_PRESS:
868             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
869             break;
870         case GDK_KEY_PRESS:
871             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
872             break;
873         default:
874             break;
875     }
878 /**
879  * Show tool context specific modifier tip.
880  */
881 void
882 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
883         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
884         gchar const *alt_tip)
886     guint keyval = get_group0_keyval(&event->key);
888     bool ctrl = ctrl_tip && (MOD__CTRL
889             || (keyval == GDK_Control_L)
890             || (keyval == GDK_Control_R));
891     bool shift = shift_tip
892         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
893     bool alt = alt_tip
894         && (MOD__ALT
895                 || (keyval == GDK_Alt_L)
896                 || (keyval == GDK_Alt_R)
897                 || (keyval == GDK_Meta_L)
898                 || (keyval == GDK_Meta_R));
900     gchar *tip = g_strdup_printf("%s%s%s%s%s",
901                                  ( ctrl ? ctrl_tip : "" ),
902                                  ( ctrl && (shift || alt) ? "; " : "" ),
903                                  ( shift ? shift_tip : "" ),
904                                  ( (ctrl || shift) && alt ? "; " : "" ),
905                                  ( alt ? alt_tip : "" ));
907     if (strlen(tip) > 0) {
908         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
909     }
911     g_free(tip);
914 /**
915  * Return the keyval corresponding to the key event in group 0, i.e.,
916  * in the main (English) layout.
917  *
918  * Use this instead of simply event->keyval, so that your keyboard shortcuts
919  * work regardless of layouts (e.g., in Cyrillic).
920  */
921 guint
922 get_group0_keyval(GdkEventKey *event)
924     guint keyval = 0;
925     gdk_keymap_translate_keyboard_state(
926             gdk_keymap_get_for_display(gdk_display_get_default()),
927             event->hardware_keycode,
928             (GdkModifierType) event->state,
929             0   /*event->key.group*/,
930             &keyval, NULL, NULL, NULL);
931     return keyval;
934 /**
935  * Returns item at point p in desktop.
936  *
937  * If state includes alt key mask, cyclically selects under; honors
938  * into_groups.
939  */
940 SPItem *
941 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
942         bool select_under, bool into_groups)
944     SPItem *item;
946     if (select_under) {
947         SPItem *selected_at_point =
948             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
949         item = desktop->item_at_point(p, into_groups, selected_at_point);
950         if (item == NULL) { // we may have reached bottom, flip over to the top
951             item = desktop->item_at_point(p, into_groups, NULL);
952         }
953     } else
954         item = desktop->item_at_point(p, into_groups, NULL);
956     return item;
959 /**
960  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
961  *
962  * Honors into_groups.
963  */
964 SPItem *
965 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
967     GSList *temp = NULL;
968     temp = g_slist_prepend (temp, item);
969     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
970     g_slist_free (temp);
972     return item_at_point;
975 /**
976  * Called when SPEventContext subclass node attribute changed.
977  */
978 void
979 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
980         gchar const *old_value, gchar const *new_value,
981         bool const is_interactive, gpointer const data)
983     if (!name
984             || !strcmp(name, "style")
985             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
986         // no need to regenrate knotholder if only style changed
987         return;
988     }
990     SPEventContext *ec = SP_EVENT_CONTEXT(data);
992     if (ec->shape_knot_holder) {
993         sp_knot_holder_destroy(ec->shape_knot_holder);
994     }
995     ec->shape_knot_holder = NULL;
997     SPDesktop *desktop = ec->desktop;
999     SPItem *item = sp_desktop_selection(desktop)->singleItem();
1001     if (item) {
1002         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1003     }
1007 /*
1008   Local Variables:
1009   mode:c++
1010   c-file-style:"stroustrup"
1011   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1012   indent-tabs-mode:nil
1013   fill-column:99
1014   End:
1015 */
1016 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :