Code

fix bug which caused erroneous zoom-in after shift-middle-clicking to zoom out from...
[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                 zoom_rb = 0;
456                 NR::Point const event_w(event->button.x, event->button.y);
457                 NR::Point const event_dt(desktop->w2d(event_w));
458                 double const zoom_power = ( (event->button.state & GDK_SHIFT_MASK)
459                                             ? -dontgrab : dontgrab );
460                 desktop->zoom_relative_keep_point(event_dt,
461                                                   pow(zoom_inc, zoom_power));
462                 gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
463                 desktop->updateNow();
464             }
465             if (panning == event->button.button) {
466                 panning = 0;
467                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
468                                       event->button.time);
469                 desktop->updateNow();
470                 ret = TRUE;
471             } else if (zoom_rb == event->button.button) {
472                 zoom_rb = 0;
473                 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
474                 if (b != NR::Nothing() && !within_tolerance) {
475                     desktop->set_display_area(b.assume(), 10);
476                 }
477                 Inkscape::Rubberband::get()->stop();
478             }
479             xp = yp = 0;
480             break;
481         case GDK_KEY_PRESS:
482             switch (get_group0_keyval(&event->key)) {
483                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
484                 // in the editing window). So we resteal them back and run our regular shortcut
485                 // invoker on them.
486                 unsigned int shortcut;
487                 case GDK_Tab: 
488                 case GDK_ISO_Left_Tab: 
489                 case GDK_F1:
490                     shortcut = get_group0_keyval(&event->key);
491                     if (event->key.state & GDK_SHIFT_MASK)
492                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
493                     if (event->key.state & GDK_CONTROL_MASK)
494                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
495                     if (event->key.state & GDK_MOD1_MASK)
496                         shortcut |= SP_SHORTCUT_ALT_MASK;
497                     ret = sp_shortcut_invoke(shortcut, desktop);
498                     break;
500                 case GDK_W:
501                 case GDK_w:
502                 case GDK_F4:
503                     /* Close view */
504                     if (MOD__CTRL_ONLY) {
505                         sp_ui_close_view(NULL);
506                         ret = TRUE;
507                     }
508                     break;
509                 case GDK_Left: // Ctrl Left
510                 case GDK_KP_Left:
511                 case GDK_KP_4:
512                     if (MOD__CTRL_ONLY) {
513                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
514                         gobble_key_events(get_group0_keyval(&event->key),
515                                 GDK_CONTROL_MASK);
516                         event_context->desktop->scroll_world(i, 0);
517                         ret = TRUE;
518                     }
519                     break;
520                 case GDK_Up: // Ctrl Up
521                 case GDK_KP_Up:
522                 case GDK_KP_8:
523                     if (MOD__CTRL_ONLY) {
524                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
525                         gobble_key_events(get_group0_keyval(&event->key),
526                                 GDK_CONTROL_MASK);
527                         event_context->desktop->scroll_world(0, i);
528                         ret = TRUE;
529                     }
530                     break;
531                 case GDK_Right: // Ctrl Right
532                 case GDK_KP_Right:
533                 case GDK_KP_6:
534                     if (MOD__CTRL_ONLY) {
535                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
536                         gobble_key_events(get_group0_keyval(&event->key),
537                                 GDK_CONTROL_MASK);
538                         event_context->desktop->scroll_world(-i, 0);
539                         ret = TRUE;
540                     }
541                     break;
542                 case GDK_Down: // Ctrl Down
543                 case GDK_KP_Down:
544                 case GDK_KP_2:
545                     if (MOD__CTRL_ONLY) {
546                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
547                         gobble_key_events(get_group0_keyval(&event->key),
548                                 GDK_CONTROL_MASK);
549                         event_context->desktop->scroll_world(0, -i);
550                         ret = TRUE;
551                     }
552                     break;
553                 case GDK_F10:
554                     if (MOD__SHIFT_ONLY) {
555                         sp_event_root_menu_popup(desktop, NULL, event);
556                         ret= TRUE;
557                     }
558                     break;
559                 case GDK_space:
560                     sp_toggle_selector(desktop);
561                     ret= TRUE;
562                     break;
563                 case GDK_z:
564                 case GDK_Z:
565                     if (MOD__ALT_ONLY) {
566                         desktop->zoom_grab_focus();
567                         ret = TRUE;
568                     }
569                     break;
570                 default:
571                     break;
572             }
573             break;
574         case GDK_SCROLL:
575             /* shift + wheel, pan left--right */
576             if (event->scroll.state & GDK_SHIFT_MASK) {
577                 switch (event->scroll.direction) {
578                     case GDK_SCROLL_UP:
579                         desktop->scroll_world(wheel_scroll, 0);
580                         break;
581                     case GDK_SCROLL_DOWN:
582                         desktop->scroll_world(-wheel_scroll, 0);
583                         break;
584                     default:
585                         break;
586                 }
588                 /* ctrl + wheel, zoom in--out */
589             } else if (event->scroll.state & GDK_CONTROL_MASK) {
590                 double rel_zoom;
591                 switch (event->scroll.direction) {
592                     case GDK_SCROLL_UP:
593                         rel_zoom = zoom_inc;
594                         break;
595                     case GDK_SCROLL_DOWN:
596                         rel_zoom = 1 / zoom_inc;
597                         break;
598                     default:
599                         rel_zoom = 0.0;
600                         break;
601                 }
602                 if (rel_zoom != 0.0) {
603                     NR::Point const scroll_dt = desktop->point();
604                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
605                 }
607                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
608             } else {
609                 switch (event->scroll.direction) {
610                     case GDK_SCROLL_UP:
611                         desktop->scroll_world(0, wheel_scroll);
612                         break;
613                     case GDK_SCROLL_DOWN:
614                         desktop->scroll_world(0, -wheel_scroll);
615                         break;
616                     case GDK_SCROLL_LEFT:
617                         desktop->scroll_world(wheel_scroll, 0);
618                         break;
619                     case GDK_SCROLL_RIGHT:
620                         desktop->scroll_world(-wheel_scroll, 0);
621                         break;
622                 }
623             }
624             break;
625         default:
626             break;
627     }
629     return ret;
632 /**
633  * Handles item specific events. Gets called from Gdk.
634  *
635  * Only reacts to right mouse button at the moment.
636  * \todo Fixme: do context sensitive popup menu on items.
637  */
638 gint
639 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
641     int ret = FALSE;
643     switch (event->type) {
644         case GDK_BUTTON_PRESS:
645             if ((event->button.button == 3)
646                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
647                 sp_event_root_menu_popup(ec->desktop, item, event);
648                 ret = TRUE;
649             }
650             break;
651         default:
652             break;
653     }
655     return ret;
658 /**
659  * Gets called when attribute changes value.
660  */
661 static void
662 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
663                         bool is_interactive, gpointer data)
665     SPEventContext *ec;
667     ec = SP_EVENT_CONTEXT(data);
669     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
670         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
671     }
674 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
675     NULL, /* Child added */
676     NULL, /* Child removed */
677     sp_ec_repr_attr_changed,
678     NULL, /* Content changed */
679     NULL /* Order changed */
680 };
682 /**
683  * Creates new SPEventContext object and calls its virtual setup() function.
684  */
685 SPEventContext *
686 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
688     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
689     g_return_val_if_fail(desktop != NULL, NULL);
691     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
693     ec->desktop = desktop;
694     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
695     ec->key = key;
696     ec->prefs_repr = prefs_repr;
697     if (ec->prefs_repr) {
698         Inkscape::GC::anchor(ec->prefs_repr);
699         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
700     }
702     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
703         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
705     return ec;
708 /**
709  * Finishes SPEventContext.
710  */
711 void
712 sp_event_context_finish(SPEventContext *ec)
714     g_return_if_fail(ec != NULL);
715     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
717     ec->enableSelectionCue(false);
719     if (ec->next) {
720         g_warning("Finishing event context with active link\n");
721     }
723     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
724         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
727 //-------------------------------member functions
729 /**
730  * Enables/disables the SPEventContext's SelCue.
731  */
732 void SPEventContext::enableSelectionCue(bool enable) {
733     if (enable) {
734         if (!_selcue) {
735             _selcue = new Inkscape::SelCue(desktop);
736         }
737     } else {
738         delete _selcue;
739         _selcue = NULL;
740     }
743 /**
744  * Enables/disables the SPEventContext's GrDrag.
745  */
746 void SPEventContext::enableGrDrag(bool enable) {
747     if (enable) {
748         if (!_grdrag) {
749             _grdrag = new GrDrag(desktop);
750         }
751     } else {
752         if (_grdrag) {
753             delete _grdrag;
754             _grdrag = NULL;
755         }
756     }
759 /**
760  * Calls virtual set() function of SPEventContext.
761  */
762 void
763 sp_event_context_read(SPEventContext *ec, gchar const *key)
765     g_return_if_fail(ec != NULL);
766     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
767     g_return_if_fail(key != NULL);
769     if (ec->prefs_repr) {
770         gchar const *val = ec->prefs_repr->attribute(key);
771         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
772             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
773     }
776 /**
777  * Calls virtual activate() function of SPEventContext.
778  */
779 void
780 sp_event_context_activate(SPEventContext *ec)
782     g_return_if_fail(ec != NULL);
783     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
785     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
786         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
789 /**
790  * Calls virtual deactivate() function of SPEventContext.
791  */
792 void
793 sp_event_context_deactivate(SPEventContext *ec)
795     g_return_if_fail(ec != NULL);
796     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
798     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
799         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
802 /**
803  * Calls virtual root_handler(), the main event handling function.
804  */
805 gint
806 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
808     gint ret;
810     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
812     set_event_location(event_context->desktop, event);
814     return ret;
817 /**
818  * Calls virtual item_handler(), the item event handling function.
819  */
820 gint
821 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
823     gint ret;
825     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
827     if (! ret) {
828         ret = sp_event_context_root_handler(event_context, event);
829     } else {
830         set_event_location(event_context->desktop, event);
831     }
833     return ret;
836 /**
837  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
838  */
839 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
841     if (event->type != GDK_MOTION_NOTIFY) {
842         return;
843     }
845     NR::Point const button_w(event->button.x, event->button.y);
846     NR::Point const button_dt(desktop->w2d(button_w));
847     desktop-> setPosition (button_dt);
848     desktop->set_coordinate_status(button_dt);
851 //-------------------------------------------------------------------
852 /**
853  * Create popup menu and tell Gtk to show it.
854  */
855 void
856 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
858     GtkWidget *menu;
860     /* fixme: This is not what I want but works for now (Lauris) */
861     if (event->type == GDK_KEY_PRESS) {
862         item = sp_desktop_selection(desktop)->singleItem();
863     }
864     menu = sp_ui_context_menu(desktop, item);
865     gtk_widget_show(menu);
867     switch (event->type) {
868         case GDK_BUTTON_PRESS:
869             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
870             break;
871         case GDK_KEY_PRESS:
872             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
873             break;
874         default:
875             break;
876     }
879 /**
880  * Show tool context specific modifier tip.
881  */
882 void
883 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
884         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
885         gchar const *alt_tip)
887     guint keyval = get_group0_keyval(&event->key);
889     bool ctrl = ctrl_tip && (MOD__CTRL
890             || (keyval == GDK_Control_L)
891             || (keyval == GDK_Control_R));
892     bool shift = shift_tip
893         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
894     bool alt = alt_tip
895         && (MOD__ALT
896                 || (keyval == GDK_Alt_L)
897                 || (keyval == GDK_Alt_R)
898                 || (keyval == GDK_Meta_L)
899                 || (keyval == GDK_Meta_R));
901     gchar *tip = g_strdup_printf("%s%s%s%s%s",
902                                  ( ctrl ? ctrl_tip : "" ),
903                                  ( ctrl && (shift || alt) ? "; " : "" ),
904                                  ( shift ? shift_tip : "" ),
905                                  ( (ctrl || shift) && alt ? "; " : "" ),
906                                  ( alt ? alt_tip : "" ));
908     if (strlen(tip) > 0) {
909         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
910     }
912     g_free(tip);
915 /**
916  * Return the keyval corresponding to the key event in group 0, i.e.,
917  * in the main (English) layout.
918  *
919  * Use this instead of simply event->keyval, so that your keyboard shortcuts
920  * work regardless of layouts (e.g., in Cyrillic).
921  */
922 guint
923 get_group0_keyval(GdkEventKey *event)
925     guint keyval = 0;
926     gdk_keymap_translate_keyboard_state(
927             gdk_keymap_get_for_display(gdk_display_get_default()),
928             event->hardware_keycode,
929             (GdkModifierType) event->state,
930             0   /*event->key.group*/,
931             &keyval, NULL, NULL, NULL);
932     return keyval;
935 /**
936  * Returns item at point p in desktop.
937  *
938  * If state includes alt key mask, cyclically selects under; honors
939  * into_groups.
940  */
941 SPItem *
942 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
943         bool select_under, bool into_groups)
945     SPItem *item;
947     if (select_under) {
948         SPItem *selected_at_point =
949             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
950         item = desktop->item_at_point(p, into_groups, selected_at_point);
951         if (item == NULL) { // we may have reached bottom, flip over to the top
952             item = desktop->item_at_point(p, into_groups, NULL);
953         }
954     } else
955         item = desktop->item_at_point(p, into_groups, NULL);
957     return item;
960 /**
961  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
962  *
963  * Honors into_groups.
964  */
965 SPItem *
966 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
968     GSList *temp = NULL;
969     temp = g_slist_prepend (temp, item);
970     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
971     g_slist_free (temp);
973     return item_at_point;
976 /**
977  * Called when SPEventContext subclass node attribute changed.
978  */
979 void
980 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
981         gchar const *old_value, gchar const *new_value,
982         bool const is_interactive, gpointer const data)
984     if (!name
985             || !strcmp(name, "style")
986             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
987         // no need to regenrate knotholder if only style changed
988         return;
989     }
991     SPEventContext *ec = SP_EVENT_CONTEXT(data);
993     if (ec->shape_knot_holder) {
994         sp_knot_holder_destroy(ec->shape_knot_holder);
995     }
996     ec->shape_knot_holder = NULL;
998     SPDesktop *desktop = ec->desktop;
1000     SPItem *item = sp_desktop_selection(desktop)->singleItem();
1002     if (item) {
1003         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1004     }
1008 /*
1009   Local Variables:
1010   mode:c++
1011   c-file-style:"stroustrup"
1012   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1013   indent-tabs-mode:nil
1014   fill-column:99
1015   End:
1016 */
1017 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :