Code

I'm an idiot who forgot that MOTION_HINT_MASK still needs MOTION_MASK
[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 <string.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtkmain.h>
33 #include <gtk/gtkmenu.h>
34 #include <glibmm/i18n.h>
35 #include <cstring>
36 #include <string>
38 #include "display/sp-canvas.h"
39 #include "xml/node-event-vector.h"
40 #include "sp-cursor.h"
41 #include "shortcuts.h"
42 #include "desktop.h"
43 #include "desktop-handles.h"
44 #include "selection.h"
45 #include "file.h"
46 #include "interface.h"
47 #include "macros.h"
48 #include "tools-switch.h"
49 #include "prefs-utils.h"
50 #include "message-context.h"
51 #include "gradient-drag.h"
52 #include "object-edit.h"
53 #include "attributes.h"
54 #include "rubberband.h"
55 #include "selcue.h"
57 #include "event-context.h"
59 static void sp_event_context_class_init(SPEventContextClass *klass);
60 static void sp_event_context_init(SPEventContext *event_context);
61 static void sp_event_context_dispose(GObject *object);
63 static void sp_event_context_private_setup(SPEventContext *ec);
64 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
65 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
67 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
69 static GObjectClass *parent_class;
71 // globals for temporary switching to selector by space
72 static bool selector_toggled = FALSE;
73 static int switch_selector_to = 0;
75 // globals for temporary switching to dropper by 'D'
76 static bool dropper_toggled = FALSE;
77 static int switch_dropper_to = 0;
79 static gint xp = 0, yp = 0; // where drag started
80 static gint tolerance = 0;
81 static bool within_tolerance = false;
83 // globals for keeping track of keyboard scroll events in order to accelerate
84 static guint32 scroll_event_time = 0;
85 static gdouble scroll_multiply = 1;
86 static guint scroll_keyval = 0;
88 /**
89  * Registers the SPEventContext class with Glib and returns its type number.
90  */
91 GType
92 sp_event_context_get_type(void)
93 {
94     static GType type = 0;
95     if (!type) {
96         GTypeInfo info = {
97             sizeof(SPEventContextClass),
98             NULL, NULL,
99             (GClassInitFunc) sp_event_context_class_init,
100             NULL, NULL,
101             sizeof(SPEventContext),
102             4,
103             (GInstanceInitFunc) sp_event_context_init,
104             NULL,    /* value_table */
105         };
106         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
107     }
108     return type;
111 /**
112  * Callback to set up the SPEventContext vtable.
113  */
114 static void
115 sp_event_context_class_init(SPEventContextClass *klass)
117     GObjectClass *object_class;
119     object_class = (GObjectClass *) klass;
121     parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
123     object_class->dispose = sp_event_context_dispose;
125     klass->setup = sp_event_context_private_setup;
126     klass->root_handler = sp_event_context_private_root_handler;
127     klass->item_handler = sp_event_context_private_item_handler;
130 /**
131  * Clears all SPEventContext object members.
132  */
133 static void
134 sp_event_context_init(SPEventContext *event_context)
136     event_context->desktop = NULL;
137     event_context->cursor = NULL;
138     event_context->_message_context = NULL;
139     event_context->_selcue = NULL;
140     event_context->_grdrag = NULL;
141     event_context->space_panning = false;
144 /**
145  * Callback to free and null member variables of SPEventContext object.
146  */
147 static void
148 sp_event_context_dispose(GObject *object)
150     SPEventContext *ec;
152     ec = SP_EVENT_CONTEXT(object);
154     if (ec->_message_context) {
155         delete ec->_message_context;
156     }
158     if (ec->cursor != NULL) {
159         gdk_cursor_unref(ec->cursor);
160         ec->cursor = NULL;
161     }
163     if (ec->desktop) {
164         ec->desktop = NULL;
165     }
167     if (ec->prefs_repr) {
168         sp_repr_remove_listener_by_data(ec->prefs_repr, ec);
169         Inkscape::GC::release(ec->prefs_repr);
170         ec->prefs_repr = NULL;
171     }
173     G_OBJECT_CLASS(parent_class)->dispose(object);
176 /**
177  * Recreates and draws cursor on desktop related to SPEventContext.
178  */
179 void
180 sp_event_context_update_cursor(SPEventContext *ec)
182     GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
183     if (w->window) {
184         /* fixme: */
185         if (ec->cursor_shape) {
186             GdkBitmap *bitmap = NULL;
187             GdkBitmap *mask = NULL;
188             sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
189             if ((bitmap != NULL) && (mask != NULL)) {
190                 if (ec->cursor)
191                     gdk_cursor_unref (ec->cursor);
192                 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
193                                                         &w->style->black,
194                                                         &w->style->white,
195                                                         ec->hot_x, ec->hot_y);
196                 g_object_unref (bitmap);
197                 g_object_unref (mask);
198             }
199         }
200         gdk_window_set_cursor(w->window, ec->cursor);
201     }
202     ec->desktop->waiting_cursor = false;
205 /**
206  * Callback that gets called on initialization of SPEventContext object.
207  * Redraws mouse cursor, at the moment.
208  */
209 static void
210 sp_event_context_private_setup(SPEventContext *ec)
212     sp_event_context_update_cursor(ec);
215 /**
216  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
217  */
218 gint gobble_key_events(guint keyval, gint mask)
220     GdkEvent *event_next;
221     gint i = 0;
223     event_next = gdk_event_get();
224     // while the next event is also a key notify with the same keyval and mask,
225     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
226            && event_next->key.keyval == keyval
227            && (!mask || (event_next->key.state & mask))) {
228         if (event_next->type == GDK_KEY_PRESS)
229             i ++; 
230         // kill it
231         gdk_event_free(event_next);
232         // get next
233         event_next = gdk_event_get();
234     }
235     // otherwise, put it back onto the queue
236     if (event_next) gdk_event_put(event_next);
238     return i;
241 /**
242  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
243 */
244 gint gobble_motion_events(gint mask)
246     GdkEvent *event_next;
247     gint i = 0;
249     event_next = gdk_event_get();
250     // while the next event is also a key notify with the same keyval and mask,
251     while (event_next && event_next->type == GDK_MOTION_NOTIFY
252            && (event_next->motion.state & mask)) {
253         // kill it
254         gdk_event_free(event_next);
255         // get next
256         event_next = gdk_event_get();
257         i ++;
258     }
259     // otherwise, put it back onto the queue
260     if (event_next) gdk_event_put(event_next);
262     return i;
265 /**
266  * Toggles current tool between active tool and selector tool.
267  * Subroutine of sp_event_context_private_root_handler().
268  */
269 static void
270 sp_toggle_selector(SPDesktop *dt)
272     if (!dt->event_context) return;
274     if (tools_isactive(dt, TOOLS_SELECT)) {
275         if (selector_toggled) {
276             if (switch_selector_to) tools_switch (dt, switch_selector_to);
277             selector_toggled = FALSE;
278         } else return;
279     } else {
280         selector_toggled = TRUE;
281         switch_selector_to = tools_active(dt);
282         tools_switch (dt, TOOLS_SELECT);
283     }
286 /**
287  * Toggles current tool between active tool and dropper tool.
288  * Subroutine of sp_event_context_private_root_handler().
289  */
290 static void
291 sp_toggle_dropper(SPDesktop *dt)
293     if (!dt->event_context) return;
295     if (tools_isactive(dt, TOOLS_DROPPER)) {
296         if (dropper_toggled) {
297             if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
298             dropper_toggled = FALSE;
299         } else return;
300     } else {
301         dropper_toggled = TRUE;
302         switch_dropper_to = tools_active(dt);
303         tools_switch (dt, TOOLS_DROPPER);
304     }
307 /**
308  * Calculates and keeps track of scroll acceleration.
309  * Subroutine of sp_event_context_private_root_handler().
310  */
311 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
313     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
315     /* key pressed within 500ms ? (1/2 second) */
316     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
317         scroll_multiply = 1; // abort acceleration
318     } else {
319         scroll_multiply += acceleration; // continue acceleration
320     }
322     scroll_event_time = ((GdkEventKey *) event)->time;
323     scroll_keyval = event->key.keyval;
325     return scroll_multiply;
328 /**
329  * Main event dispatch, gets called from Gdk.
330  */
331 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
333     static NR::Point button_w;
334     static unsigned int panning = 0;
335     static unsigned int zoom_rb = 0;
337     SPDesktop *desktop = event_context->desktop;
339     tolerance = prefs_get_int_attribute_limited(
340             "options.dragtolerance","value", 0, 0, 100);
341     double const zoom_inc = prefs_get_double_attribute_limited(
342             "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
343     double const acceleration = prefs_get_double_attribute_limited(
344             "options.scrollingacceleration", "value", 0, 0, 6);
345     int const key_scroll = prefs_get_int_attribute_limited(
346             "options.keyscroll", "value", 10, 0, 1000);
347     int const wheel_scroll = prefs_get_int_attribute_limited(
348             "options.wheelscroll", "value", 40, 0, 1000);
350     gint ret = FALSE;
352     switch (event->type) {
353         case GDK_2BUTTON_PRESS:
354             if (panning) {
355                 panning = 0;
356                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
357                         event->button.time);
358                 ret = TRUE;
359             } else {
360                 /* sp_desktop_dialog(); */
361             }
362             break;
363         case GDK_BUTTON_PRESS:
365             // save drag origin
366             xp = (gint) event->button.x;
367             yp = (gint) event->button.y;
368             within_tolerance = true;
370             button_w = NR::Point(event->button.x, event->button.y);
372             switch (event->button.button) {
373                 case 1:
374                     if (event_context->space_panning) {
375                         panning = 1;
376                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
377                             GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
378                             NULL, event->button.time-1);
379                         ret = TRUE;
380                     }
381                     break;
382                 case 2:
383                     if (event->button.state == GDK_SHIFT_MASK) {
384                         zoom_rb = 2;
385                     } else {
386                         panning = 2;
387                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
388                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
389                             NULL, event->button.time-1);
390                     }
391                     ret = TRUE;
392                     break;
393                 case 3:
394                     if (event->button.state & GDK_SHIFT_MASK
395                             || event->button.state & GDK_CONTROL_MASK) {
396                         panning = 3;
397                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
398                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
399                                 NULL, event->button.time);
400                         ret = TRUE;
401                     } else {
402                         sp_event_root_menu_popup(desktop, NULL, event);
403                     }
404                     break;
405                 default:
406                     break;
407             }
408             break;
409         case GDK_MOTION_NOTIFY:
410             if (panning) {
411                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
412                         || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
413                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
414                    ) {
415                     /* Gdk seems to lose button release for us sometimes :-( */
416                     panning = 0;
417                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
418                             event->button.time);
419                     ret = TRUE;
420                 } else {
421                     if ( within_tolerance
422                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
423                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
424                     {
425                         // do not drag if we're within tolerance from origin
426                         break;
427                     }
428                     // Once the user has moved farther than tolerance from
429                     // the original location (indicating they intend to move
430                     // the object, not click), then always process the motion
431                     // notify coordinates as given (no snapping back to origin)
432                     within_tolerance = false;
434                     // gobble subsequent motion events to prevent "sticking"
435                     // when scrolling is slow
436                     gobble_motion_events(panning == 2 ?
437                                          GDK_BUTTON2_MASK :
438                                          (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
440                     NR::Point const motion_w(event->motion.x, event->motion.y);
441                     NR::Point const moved_w( motion_w - button_w );
442                     event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
443                     ret = TRUE;
444                 }
445             } else if (zoom_rb) {
446                 NR::Point const motion_w(event->motion.x, event->motion.y);
447                 NR::Point const motion_dt(desktop->w2d(motion_w));
449                 if ( within_tolerance
450                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
451                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
452                     break; // do not drag if we're within tolerance from origin
453                 }
454                 // Once the user has moved farther than tolerance from the original location
455                 // (indicating they intend to move the object, not click), then always process the
456                 // motion notify coordinates as given (no snapping back to origin)
457                 within_tolerance = false;
459                 if (Inkscape::Rubberband::get()->is_started()) {
460                     Inkscape::Rubberband::get()->move(motion_dt);
461                 } else {
462                     Inkscape::Rubberband::get()->start(desktop, motion_dt);
463                 } 
464                 if (zoom_rb == 2)
465                     gobble_motion_events(GDK_BUTTON2_MASK);
466             }
467             break;
468         case GDK_BUTTON_RELEASE:
469             xp = yp = 0;
470             if (within_tolerance && (panning || zoom_rb)) {
471                 zoom_rb = 0;
472                 if (panning) {
473                     panning = 0;
474                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
475                                       event->button.time);
476                 }
477                 NR::Point const event_w(event->button.x, event->button.y);
478                 NR::Point const event_dt(desktop->w2d(event_w));
479                 desktop->zoom_relative_keep_point(event_dt,
480                           (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
481                 desktop->updateNow();
482                 ret = TRUE;
483             } else if (panning == event->button.button) {
484                 panning = 0;
485                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
486                                       event->button.time);
488                 // in slow complex drawings, some of the motion events are lost;
489                 // to make up for this, we scroll it once again to the button-up event coordinates
490                 // (i.e. canvas will always get scrolled all the way to the mouse release point, 
491                 // even if few intermediate steps were visible)
492                 NR::Point const motion_w(event->button.x, event->button.y);
493                 NR::Point const moved_w( motion_w - button_w );
494                 event_context->desktop->scroll_world(moved_w);
495                 desktop->updateNow();
496                 ret = TRUE;
497             } else if (zoom_rb == event->button.button) {
498                 zoom_rb = 0;
499                 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
500                 Inkscape::Rubberband::get()->stop();
501                 if (b && !within_tolerance) {
502                     desktop->set_display_area(*b, 10);
503                 }
504                 ret = TRUE;
505             }
506             break;
507         case GDK_KEY_PRESS:
508             switch (get_group0_keyval(&event->key)) {
509                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
510                 // in the editing window). So we resteal them back and run our regular shortcut
511                 // invoker on them.
512                 unsigned int shortcut;
513                 case GDK_Tab: 
514                 case GDK_ISO_Left_Tab: 
515                 case GDK_F1:
516                     shortcut = get_group0_keyval(&event->key);
517                     if (event->key.state & GDK_SHIFT_MASK)
518                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
519                     if (event->key.state & GDK_CONTROL_MASK)
520                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
521                     if (event->key.state & GDK_MOD1_MASK)
522                         shortcut |= SP_SHORTCUT_ALT_MASK;
523                     ret = sp_shortcut_invoke(shortcut, desktop);
524                     break;
526                 case GDK_D:
527                 case GDK_d:
528                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
529                         sp_toggle_dropper(desktop);
530                         ret = TRUE;
531                     }
532                     break;
533                 case GDK_W:
534                 case GDK_w:
535                 case GDK_F4:
536                     /* Close view */
537                     if (MOD__CTRL_ONLY) {
538                         sp_ui_close_view(NULL);
539                         ret = TRUE;
540                     }
541                     break;
542                 case GDK_Left: // Ctrl Left
543                 case GDK_KP_Left:
544                 case GDK_KP_4:
545                     if (MOD__CTRL_ONLY) {
546                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
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_Up: // Ctrl Up
554                 case GDK_KP_Up:
555                 case GDK_KP_8:
556                     if (MOD__CTRL_ONLY) {
557                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
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_Right: // Ctrl Right
565                 case GDK_KP_Right:
566                 case GDK_KP_6:
567                     if (MOD__CTRL_ONLY) {
568                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
569                         gobble_key_events(get_group0_keyval(&event->key),
570                                 GDK_CONTROL_MASK);
571                         event_context->desktop->scroll_world(-i, 0);
572                         ret = TRUE;
573                     }
574                     break;
575                 case GDK_Down: // Ctrl Down
576                 case GDK_KP_Down:
577                 case GDK_KP_2:
578                     if (MOD__CTRL_ONLY) {
579                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
580                         gobble_key_events(get_group0_keyval(&event->key),
581                                 GDK_CONTROL_MASK);
582                         event_context->desktop->scroll_world(0, -i);
583                         ret = TRUE;
584                     }
585                     break;
586                 case GDK_F10:
587                     if (MOD__SHIFT_ONLY) {
588                         sp_event_root_menu_popup(desktop, NULL, event);
589                         ret= TRUE;
590                     }
591                     break;
592                 case GDK_space:
593                     if (prefs_get_int_attribute("options.spacepans","value", 0) == 1) {
594                         event_context->space_panning = true;
595                         event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
596                         ret= TRUE;
597                     } else {
598                         sp_toggle_selector(desktop);
599                         ret= TRUE;
600                     }
601                     break;
602                 case GDK_z:
603                 case GDK_Z:
604                     if (MOD__ALT_ONLY) {
605                         desktop->zoom_grab_focus();
606                         ret = TRUE;
607                     }
608                     break;
609                 default:
610                     break;
611             }
612             break;
613         case GDK_KEY_RELEASE:
614             switch (get_group0_keyval(&event->key)) {
615                 case GDK_space:
616                     if (event_context->space_panning) {
617                         event_context->space_panning = false;
618                         event_context->_message_context->clear();
619                         if (panning == 1) {
620                             panning = 0;
621                             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
622                                   event->key.time);
623                             desktop->updateNow();
624                         }
625                         ret= TRUE;
626                     } 
627                     break;
628                 default:
629                     break;
630             }
631             break;
632         case GDK_SCROLL:
633         {
634             bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
635             bool wheelzooms = (prefs_get_int_attribute("options.wheelzooms","value", 0) == 1);
636             /* shift + wheel, pan left--right */
637             if (event->scroll.state & GDK_SHIFT_MASK) {
638                 switch (event->scroll.direction) {
639                     case GDK_SCROLL_UP:
640                         desktop->scroll_world(wheel_scroll, 0);
641                         break;
642                     case GDK_SCROLL_DOWN:
643                         desktop->scroll_world(-wheel_scroll, 0);
644                         break;
645                     default:
646                         break;
647                 }
649                 /* ctrl + wheel, zoom in--out */
650             } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
651                 double rel_zoom;
652                 switch (event->scroll.direction) {
653                     case GDK_SCROLL_UP:
654                         rel_zoom = zoom_inc;
655                         break;
656                     case GDK_SCROLL_DOWN:
657                         rel_zoom = 1 / zoom_inc;
658                         break;
659                     default:
660                         rel_zoom = 0.0;
661                         break;
662                 }
663                 if (rel_zoom != 0.0) {
664                     NR::Point const scroll_dt = desktop->point();
665                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
666                 }
668                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
669             } else {
670                 switch (event->scroll.direction) {
671                     case GDK_SCROLL_UP:
672                         desktop->scroll_world(0, wheel_scroll);
673                         break;
674                     case GDK_SCROLL_DOWN:
675                         desktop->scroll_world(0, -wheel_scroll);
676                         break;
677                     case GDK_SCROLL_LEFT:
678                         desktop->scroll_world(wheel_scroll, 0);
679                         break;
680                     case GDK_SCROLL_RIGHT:
681                         desktop->scroll_world(-wheel_scroll, 0);
682                         break;
683                 }
684             }
685             break;
686         }
687         default:
688             break;
689     }
691     return ret;
694 /**
695  * Handles item specific events. Gets called from Gdk.
696  *
697  * Only reacts to right mouse button at the moment.
698  * \todo Fixme: do context sensitive popup menu on items.
699  */
700 gint
701 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
703     int ret = FALSE;
705     switch (event->type) {
706         case GDK_BUTTON_PRESS:
707             if ((event->button.button == 3)
708                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
709                 sp_event_root_menu_popup(ec->desktop, item, event);
710                 ret = TRUE;
711             }
712             break;
713         default:
714             break;
715     }
717     return ret;
720 /**
721  * Gets called when attribute changes value.
722  */
723 static void
724 sp_ec_repr_attr_changed(Inkscape::XML::Node */*prefs_repr*/, gchar const *key, gchar const */*oldval*/, gchar const *newval,
725                         bool /*is_interactive*/, gpointer data)
727     SPEventContext *ec;
729     ec = SP_EVENT_CONTEXT(data);
731     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
732         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
733     }
736 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
737     NULL, /* Child added */
738     NULL, /* Child removed */
739     sp_ec_repr_attr_changed,
740     NULL, /* Content changed */
741     NULL /* Order changed */
742 };
744 /**
745  * Creates new SPEventContext object and calls its virtual setup() function.
746  */
747 SPEventContext *
748 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
750     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
751     g_return_val_if_fail(desktop != NULL, NULL);
753     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
755     ec->desktop = desktop;
756     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
757     ec->key = key;
758     ec->prefs_repr = prefs_repr;
759     if (ec->prefs_repr) {
760         Inkscape::GC::anchor(ec->prefs_repr);
761         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
762     }
764     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
765         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
767     return ec;
770 /**
771  * Finishes SPEventContext.
772  */
773 void
774 sp_event_context_finish(SPEventContext *ec)
776     g_return_if_fail(ec != NULL);
777     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
779     ec->enableSelectionCue(false);
781     if (ec->next) {
782         g_warning("Finishing event context with active link\n");
783     }
785     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
786         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
789 //-------------------------------member functions
791 /**
792  * Enables/disables the SPEventContext's SelCue.
793  */
794 void SPEventContext::enableSelectionCue(bool enable) {
795     if (enable) {
796         if (!_selcue) {
797             _selcue = new Inkscape::SelCue(desktop);
798         }
799     } else {
800         delete _selcue;
801         _selcue = NULL;
802     }
805 /**
806  * Enables/disables the SPEventContext's GrDrag.
807  */
808 void SPEventContext::enableGrDrag(bool enable) {
809     if (enable) {
810         if (!_grdrag) {
811             _grdrag = new GrDrag(desktop);
812         }
813     } else {
814         if (_grdrag) {
815             delete _grdrag;
816             _grdrag = NULL;
817         }
818     }
821 /**
822  * Calls virtual set() function of SPEventContext.
823  */
824 void
825 sp_event_context_read(SPEventContext *ec, gchar const *key)
827     g_return_if_fail(ec != NULL);
828     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
829     g_return_if_fail(key != NULL);
831     if (ec->prefs_repr) {
832         gchar const *val = ec->prefs_repr->attribute(key);
833         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
834             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
835     }
838 /**
839  * Calls virtual activate() function of SPEventContext.
840  */
841 void
842 sp_event_context_activate(SPEventContext *ec)
844     g_return_if_fail(ec != NULL);
845     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
847     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
848         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
851 /**
852  * Calls virtual deactivate() function of SPEventContext.
853  */
854 void
855 sp_event_context_deactivate(SPEventContext *ec)
857     g_return_if_fail(ec != NULL);
858     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
860     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
861         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
864 /**
865  * Calls virtual root_handler(), the main event handling function.
866  */
867 gint
868 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
870     gint ret;
872     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
874     set_event_location(event_context->desktop, event);
876     return ret;
879 /**
880  * Calls virtual item_handler(), the item event handling function.
881  */
882 gint
883 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
885     gint ret;
887     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
889     if (! ret) {
890         ret = sp_event_context_root_handler(event_context, event);
891     } else {
892         set_event_location(event_context->desktop, event);
893     }
895     return ret;
898 /**
899  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
900  */
901 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
903     if (event->type != GDK_MOTION_NOTIFY) {
904         return;
905     }
907     NR::Point const button_w(event->button.x, event->button.y);
908     NR::Point const button_dt(desktop->w2d(button_w));
909     desktop-> setPosition (button_dt);
910     desktop->set_coordinate_status(button_dt);
913 //-------------------------------------------------------------------
914 /**
915  * Create popup menu and tell Gtk to show it.
916  */
917 void
918 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
920     GtkWidget *menu;
922     /* fixme: This is not what I want but works for now (Lauris) */
923     if (event->type == GDK_KEY_PRESS) {
924         item = sp_desktop_selection(desktop)->singleItem();
925     }
926     menu = sp_ui_context_menu(desktop, item);
927     gtk_widget_show(menu);
929     switch (event->type) {
930         case GDK_BUTTON_PRESS:
931             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
932             break;
933         case GDK_KEY_PRESS:
934             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
935             break;
936         default:
937             break;
938     }
941 /**
942  * Show tool context specific modifier tip.
943  */
944 void
945 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
946         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
947         gchar const *alt_tip)
949     guint keyval = get_group0_keyval(&event->key);
951     bool ctrl = ctrl_tip && (MOD__CTRL
952             || (keyval == GDK_Control_L)
953             || (keyval == GDK_Control_R));
954     bool shift = shift_tip
955         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
956     bool alt = alt_tip
957         && (MOD__ALT
958                 || (keyval == GDK_Alt_L)
959                 || (keyval == GDK_Alt_R)
960                 || (keyval == GDK_Meta_L)
961                 || (keyval == GDK_Meta_R));
963     gchar *tip = g_strdup_printf("%s%s%s%s%s",
964                                  ( ctrl ? ctrl_tip : "" ),
965                                  ( ctrl && (shift || alt) ? "; " : "" ),
966                                  ( shift ? shift_tip : "" ),
967                                  ( (ctrl || shift) && alt ? "; " : "" ),
968                                  ( alt ? alt_tip : "" ));
970     if (strlen(tip) > 0) {
971         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
972     }
974     g_free(tip);
977 /**
978  * Return the keyval corresponding to the key event in group 0, i.e.,
979  * in the main (English) layout.
980  *
981  * Use this instead of simply event->keyval, so that your keyboard shortcuts
982  * work regardless of layouts (e.g., in Cyrillic).
983  */
984 guint
985 get_group0_keyval(GdkEventKey *event)
987     guint keyval = 0;
988     gdk_keymap_translate_keyboard_state(
989             gdk_keymap_get_for_display(gdk_display_get_default()),
990             event->hardware_keycode,
991             (GdkModifierType) event->state,
992             0   /*event->key.group*/,
993             &keyval, NULL, NULL, NULL);
994     return keyval;
997 /**
998  * Returns item at point p in desktop.
999  *
1000  * If state includes alt key mask, cyclically selects under; honors
1001  * into_groups.
1002  */
1003 SPItem *
1004 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
1005         bool select_under, bool into_groups)
1007     SPItem *item;
1009     if (select_under) {
1010         SPItem *selected_at_point =
1011             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1012         item = desktop->item_at_point(p, into_groups, selected_at_point);
1013         if (item == NULL) { // we may have reached bottom, flip over to the top
1014             item = desktop->item_at_point(p, into_groups, NULL);
1015         }
1016     } else
1017         item = desktop->item_at_point(p, into_groups, NULL);
1019     return item;
1022 /**
1023  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1024  *
1025  * Honors into_groups.
1026  */
1027 SPItem *
1028 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
1030     GSList *temp = NULL;
1031     temp = g_slist_prepend (temp, item);
1032     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1033     g_slist_free (temp);
1035     return item_at_point;
1038 /**
1039  * Called when SPEventContext subclass node attribute changed.
1040  */
1041 void
1042 ec_shape_event_attr_changed(Inkscape::XML::Node */*shape_repr*/, gchar const *name,
1043                             gchar const */*old_value*/, gchar const */*new_value*/,
1044                             bool const /*is_interactive*/, gpointer const data)
1046     if (!name
1047             || !strcmp(name, "style")
1048             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1049         // no need to regenrate knotholder if only style changed
1050         return;
1051     }
1053     SPEventContext *ec = SP_EVENT_CONTEXT(data);
1055     if (ec->shape_knot_holder) {
1056         delete ec->shape_knot_holder;
1057     }
1058     ec->shape_knot_holder = NULL;
1060     SPDesktop *desktop = ec->desktop;
1062     SPItem *item = sp_desktop_selection(desktop)->singleItem();
1064     if (item) {
1065         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1066     }
1070 /*
1071   Local Variables:
1072   mode:c++
1073   c-file-style:"stroustrup"
1074   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1075   indent-tabs-mode:nil
1076   fill-column:99
1077   End:
1078 */
1079 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :