Code

eliminate GDK_POINTER_MOTION_HINT_MASK that broke dragging with tablet pen with newer...
[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             }
463             if (panning == event->button.button) {
464                 panning = 0;
465                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
466                                       event->button.time);
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                 if (b != NR::Nothing() && !within_tolerance) {
472                     desktop->set_display_area(b.assume(), 10);
473                 }
474                 Inkscape::Rubberband::get()->stop();
475             }
476             xp = yp = 0;
477             break;
478         case GDK_KEY_PRESS:
479             switch (get_group0_keyval(&event->key)) {
480                 unsigned int shortcut;
481                 case GDK_F1:
482                     /* Grab it away from Gtk */
483                     shortcut = get_group0_keyval(&event->key);
484                     if (event->key.state & GDK_SHIFT_MASK)
485                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
486                     if (event->key.state & GDK_CONTROL_MASK)
487                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
488                     if (event->key.state & GDK_MOD1_MASK)
489                         shortcut |= SP_SHORTCUT_ALT_MASK;
490                     ret = sp_shortcut_invoke(shortcut, desktop);
491                     break;
493                 case GDK_Tab: // disable tab/shift-tab which cycle widget focus
494                 case GDK_ISO_Left_Tab: // they will get different functions
495                     if (!(MOD__CTRL_ONLY || (MOD__CTRL && MOD__SHIFT))) {
496                         ret = TRUE;
497                     } else {
498                         /* Grab it away from Gtk */
499                         shortcut = get_group0_keyval(&event->key);
500                         if (event->key.state & GDK_SHIFT_MASK)
501                             shortcut |= SP_SHORTCUT_SHIFT_MASK;
502                         if (event->key.state & GDK_CONTROL_MASK)
503                             shortcut |= SP_SHORTCUT_CONTROL_MASK;
504                         if (event->key.state & GDK_MOD1_MASK)
505                             shortcut |= SP_SHORTCUT_ALT_MASK;
506                         ret = sp_shortcut_invoke(shortcut, desktop);
507                     }
508                     break;
509                 case GDK_W:
510                 case GDK_w:
511                 case GDK_F4:
512                     /* Close view */
513                     if (MOD__CTRL_ONLY) {
514                         sp_ui_close_view(NULL);
515                         ret = TRUE;
516                     }
517                     break;
518                 case GDK_Left: // Ctrl Left
519                 case GDK_KP_Left:
520                 case GDK_KP_4:
521                     if (MOD__CTRL_ONLY) {
522                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
523                         gobble_key_events(get_group0_keyval(&event->key),
524                                 GDK_CONTROL_MASK);
525                         event_context->desktop->scroll_world(i, 0);
526                         ret = TRUE;
527                     }
528                     break;
529                 case GDK_Up: // Ctrl Up
530                 case GDK_KP_Up:
531                 case GDK_KP_8:
532                     if (MOD__CTRL_ONLY) {
533                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
534                         gobble_key_events(get_group0_keyval(&event->key),
535                                 GDK_CONTROL_MASK);
536                         event_context->desktop->scroll_world(0, i);
537                         ret = TRUE;
538                     }
539                     break;
540                 case GDK_Right: // Ctrl Right
541                 case GDK_KP_Right:
542                 case GDK_KP_6:
543                     if (MOD__CTRL_ONLY) {
544                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
545                         gobble_key_events(get_group0_keyval(&event->key),
546                                 GDK_CONTROL_MASK);
547                         event_context->desktop->scroll_world(-i, 0);
548                         ret = TRUE;
549                     }
550                     break;
551                 case GDK_Down: // Ctrl Down
552                 case GDK_KP_Down:
553                 case GDK_KP_2:
554                     if (MOD__CTRL_ONLY) {
555                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration));
556                         gobble_key_events(get_group0_keyval(&event->key),
557                                 GDK_CONTROL_MASK);
558                         event_context->desktop->scroll_world(0, -i);
559                         ret = TRUE;
560                     }
561                     break;
562                 case GDK_F10:
563                     if (MOD__SHIFT_ONLY) {
564                         sp_event_root_menu_popup(desktop, NULL, event);
565                         ret= TRUE;
566                     }
567                     break;
568                 case GDK_space:
569                     sp_toggle_selector(desktop);
570                     ret= TRUE;
571                     break;
572                 case GDK_z:
573                 case GDK_Z:
574                     if (MOD__ALT_ONLY) {
575                         desktop->zoom_grab_focus();
576                         ret = TRUE;
577                     }
578                     break;
579                 default:
580                     break;
581             }
582             break;
583         case GDK_SCROLL:
584             /* shift + wheel, pan left--right */
585             if (event->scroll.state & GDK_SHIFT_MASK) {
586                 switch (event->scroll.direction) {
587                     case GDK_SCROLL_UP:
588                         desktop->scroll_world(wheel_scroll, 0);
589                         break;
590                     case GDK_SCROLL_DOWN:
591                         desktop->scroll_world(-wheel_scroll, 0);
592                         break;
593                     default:
594                         break;
595                 }
597                 /* ctrl + wheel, zoom in--out */
598             } else if (event->scroll.state & GDK_CONTROL_MASK) {
599                 double rel_zoom;
600                 switch (event->scroll.direction) {
601                     case GDK_SCROLL_UP:
602                         rel_zoom = zoom_inc;
603                         break;
604                     case GDK_SCROLL_DOWN:
605                         rel_zoom = 1 / zoom_inc;
606                         break;
607                     default:
608                         rel_zoom = 0.0;
609                         break;
610                 }
611                 if (rel_zoom != 0.0) {
612                     NR::Point const scroll_dt = desktop->point();
613                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
614                 }
616                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
617             } else {
618                 switch (event->scroll.direction) {
619                     case GDK_SCROLL_UP:
620                         desktop->scroll_world(0, wheel_scroll);
621                         break;
622                     case GDK_SCROLL_DOWN:
623                         desktop->scroll_world(0, -wheel_scroll);
624                         break;
625                     case GDK_SCROLL_LEFT:
626                         desktop->scroll_world(wheel_scroll, 0);
627                         break;
628                     case GDK_SCROLL_RIGHT:
629                         desktop->scroll_world(-wheel_scroll, 0);
630                         break;
631                 }
632             }
633             break;
634         default:
635             break;
636     }
638     return ret;
641 /**
642  * Handles item specific events. Gets called from Gdk.
643  *
644  * Only reacts to right mouse button at the moment.
645  * \todo Fixme: do context sensitive popup menu on items.
646  */
647 gint
648 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
650     int ret = FALSE;
652     switch (event->type) {
653         case GDK_BUTTON_PRESS:
654             if ((event->button.button == 3)
655                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
656                 sp_event_root_menu_popup(ec->desktop, item, event);
657                 ret = TRUE;
658             }
659             break;
660         default:
661             break;
662     }
664     return ret;
667 /**
668  * Gets called when attribute changes value.
669  */
670 static void
671 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
672                         bool is_interactive, gpointer data)
674     SPEventContext *ec;
676     ec = SP_EVENT_CONTEXT(data);
678     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
679         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
680     }
683 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
684     NULL, /* Child added */
685     NULL, /* Child removed */
686     sp_ec_repr_attr_changed,
687     NULL, /* Content changed */
688     NULL /* Order changed */
689 };
691 /**
692  * Creates new SPEventContext object and calls its virtual setup() function.
693  */
694 SPEventContext *
695 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
697     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
698     g_return_val_if_fail(desktop != NULL, NULL);
700     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
702     ec->desktop = desktop;
703     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
704     ec->key = key;
705     ec->prefs_repr = prefs_repr;
706     if (ec->prefs_repr) {
707         Inkscape::GC::anchor(ec->prefs_repr);
708         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
709     }
711     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
712         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
714     return ec;
717 /**
718  * Finishes SPEventContext.
719  */
720 void
721 sp_event_context_finish(SPEventContext *ec)
723     g_return_if_fail(ec != NULL);
724     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
726     ec->enableSelectionCue(false);
728     if (ec->next) {
729         g_warning("Finishing event context with active link\n");
730     }
732     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
733         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
736 //-------------------------------member functions
738 /**
739  * Enables/disables the SPEventContext's SelCue.
740  */
741 void SPEventContext::enableSelectionCue(bool enable) {
742     if (enable) {
743         if (!_selcue) {
744             _selcue = new Inkscape::SelCue(desktop);
745         }
746     } else {
747         delete _selcue;
748         _selcue = NULL;
749     }
752 /**
753  * Enables/disables the SPEventContext's GrDrag.
754  */
755 void SPEventContext::enableGrDrag(bool enable) {
756     if (enable) {
757         if (!_grdrag) {
758             _grdrag = new GrDrag(desktop);
759         }
760     } else {
761         if (_grdrag) {
762             delete _grdrag;
763             _grdrag = NULL;
764         }
765     }
768 /**
769  * Calls virtual set() function of SPEventContext.
770  */
771 void
772 sp_event_context_read(SPEventContext *ec, gchar const *key)
774     g_return_if_fail(ec != NULL);
775     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
776     g_return_if_fail(key != NULL);
778     if (ec->prefs_repr) {
779         gchar const *val = ec->prefs_repr->attribute(key);
780         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
781             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
782     }
785 /**
786  * Calls virtual activate() function of SPEventContext.
787  */
788 void
789 sp_event_context_activate(SPEventContext *ec)
791     g_return_if_fail(ec != NULL);
792     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
794     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
795         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
798 /**
799  * Calls virtual deactivate() function of SPEventContext.
800  */
801 void
802 sp_event_context_deactivate(SPEventContext *ec)
804     g_return_if_fail(ec != NULL);
805     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
807     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
808         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
811 /**
812  * Calls virtual root_handler(), the main event handling function.
813  */
814 gint
815 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
817     gint ret;
819     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
821     set_event_location(event_context->desktop, event);
823     return ret;
826 /**
827  * Calls virtual item_handler(), the item event handling function.
828  */
829 gint
830 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
832     gint ret;
834     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
836     if (! ret) {
837         ret = sp_event_context_root_handler(event_context, event);
838     } else {
839         set_event_location(event_context->desktop, event);
840     }
842     return ret;
845 /**
846  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
847  */
848 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
850     if (event->type != GDK_MOTION_NOTIFY) {
851         return;
852     }
854     NR::Point const button_w(event->button.x, event->button.y);
855     NR::Point const button_dt(desktop->w2d(button_w));
856     desktop-> setPosition (button_dt);
857     desktop->set_coordinate_status(button_dt);
860 //-------------------------------------------------------------------
861 /**
862  * Create popup menu and tell Gtk to show it.
863  */
864 void
865 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
867     GtkWidget *menu;
869     /* fixme: This is not what I want but works for now (Lauris) */
870     if (event->type == GDK_KEY_PRESS) {
871         item = sp_desktop_selection(desktop)->singleItem();
872     }
873     menu = sp_ui_context_menu(desktop, item);
874     gtk_widget_show(menu);
876     switch (event->type) {
877         case GDK_BUTTON_PRESS:
878             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
879             break;
880         case GDK_KEY_PRESS:
881             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
882             break;
883         default:
884             break;
885     }
888 /**
889  * Show tool context specific modifier tip.
890  */
891 void
892 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
893         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
894         gchar const *alt_tip)
896     guint keyval = get_group0_keyval(&event->key);
898     bool ctrl = ctrl_tip && (MOD__CTRL
899             || (keyval == GDK_Control_L)
900             || (keyval == GDK_Control_R));
901     bool shift = shift_tip
902         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
903     bool alt = alt_tip
904         && (MOD__ALT
905                 || (keyval == GDK_Alt_L)
906                 || (keyval == GDK_Alt_R)
907                 || (keyval == GDK_Meta_L)
908                 || (keyval == GDK_Meta_R));
910     gchar *tip = g_strdup_printf("%s%s%s%s%s",
911                                  ( ctrl ? ctrl_tip : "" ),
912                                  ( ctrl && (shift || alt) ? "; " : "" ),
913                                  ( shift ? shift_tip : "" ),
914                                  ( (ctrl || shift) && alt ? "; " : "" ),
915                                  ( alt ? alt_tip : "" ));
917     if (strlen(tip) > 0) {
918         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
919     }
921     g_free(tip);
924 /**
925  * Return the keyval corresponding to the key event in group 0, i.e.,
926  * in the main (English) layout.
927  *
928  * Use this instead of simply event->keyval, so that your keyboard shortcuts
929  * work regardless of layouts (e.g., in Cyrillic).
930  */
931 guint
932 get_group0_keyval(GdkEventKey *event)
934     guint keyval = 0;
935     gdk_keymap_translate_keyboard_state(
936             gdk_keymap_get_for_display(gdk_display_get_default()),
937             event->hardware_keycode,
938             (GdkModifierType) event->state,
939             0   /*event->key.group*/,
940             &keyval, NULL, NULL, NULL);
941     return keyval;
944 /**
945  * Returns item at point p in desktop.
946  *
947  * If state includes alt key mask, cyclically selects under; honors
948  * into_groups.
949  */
950 SPItem *
951 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
952         bool select_under, bool into_groups)
954     SPItem *item;
956     if (select_under) {
957         SPItem *selected_at_point =
958             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
959         item = desktop->item_at_point(p, into_groups, selected_at_point);
960         if (item == NULL) { // we may have reached bottom, flip over to the top
961             item = desktop->item_at_point(p, into_groups, NULL);
962         }
963     } else
964         item = desktop->item_at_point(p, into_groups, NULL);
966     return item;
969 /**
970  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
971  *
972  * Honors into_groups.
973  */
974 SPItem *
975 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
977     GSList *temp = NULL;
978     temp = g_slist_prepend (temp, item);
979     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
980     g_slist_free (temp);
982     return item_at_point;
985 /**
986  * Called when SPEventContext subclass node attribute changed.
987  */
988 void
989 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name,
990         gchar const *old_value, gchar const *new_value,
991         bool const is_interactive, gpointer const data)
993     if (!name
994             || !strcmp(name, "style")
995             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
996         // no need to regenrate knotholder if only style changed
997         return;
998     }
1000     SPEventContext *ec = SP_EVENT_CONTEXT(data);
1002     if (ec->shape_knot_holder) {
1003         sp_knot_holder_destroy(ec->shape_knot_holder);
1004     }
1005     ec->shape_knot_holder = NULL;
1007     SPDesktop *desktop = ec->desktop;
1009     SPItem *item = sp_desktop_selection(desktop)->singleItem();
1011     if (item) {
1012         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1013     }
1017 /*
1018   Local Variables:
1019   mode:c++
1020   c-file-style:"stroustrup"
1021   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1022   indent-tabs-mode:nil
1023   fill-column:99
1024   End:
1025 */
1026 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :