Code

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