Code

move shape_editor from node context to the parent class, event context
[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 "event-context.h"
32 #include <string.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkmain.h>
35 #include <gtk/gtkmenu.h>
36 #include <glibmm/i18n.h>
37 #include <cstring>
38 #include <string>
40 #include "display/sp-canvas.h"
41 #include "xml/node-event-vector.h"
42 #include "sp-cursor.h"
43 #include "shortcuts.h"
44 #include "desktop.h"
45 #include "desktop-handles.h"
46 #include "selection.h"
47 #include "file.h"
48 #include "interface.h"
49 #include "macros.h"
50 #include "tools-switch.h"
51 #include "preferences.h"
52 #include "message-context.h"
53 #include "gradient-drag.h"
54 #include "object-edit.h"
55 #include "attributes.h"
56 #include "rubberband.h"
57 #include "selcue.h"
58 #include "node-context.h"
59 #include "lpe-tool-context.h"
61 static void sp_event_context_class_init(SPEventContextClass *klass);
62 static void sp_event_context_init(SPEventContext *event_context);
63 static void sp_event_context_dispose(GObject *object);
65 static void sp_event_context_private_setup(SPEventContext *ec);
66 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
67 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
69 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
71 static GObjectClass *parent_class;
73 // globals for temporary switching to selector by space
74 static bool selector_toggled = FALSE;
75 static int switch_selector_to = 0;
77 // globals for temporary switching to dropper by 'D'
78 static bool dropper_toggled = FALSE;
79 static int switch_dropper_to = 0;
81 static gint xp = 0, yp = 0; // where drag started
82 static gint tolerance = 0;
83 static bool within_tolerance = false;
85 // globals for keeping track of keyboard scroll events in order to accelerate
86 static guint32 scroll_event_time = 0;
87 static gdouble scroll_multiply = 1;
88 static guint scroll_keyval = 0;
90 /**
91  * Registers the SPEventContext class with Glib and returns its type number.
92  */
93 GType
94 sp_event_context_get_type(void)
95 {
96     static GType type = 0;
97     if (!type) {
98         GTypeInfo info = {
99             sizeof(SPEventContextClass),
100             NULL, NULL,
101             (GClassInitFunc) sp_event_context_class_init,
102             NULL, NULL,
103             sizeof(SPEventContext),
104             4,
105             (GInstanceInitFunc) sp_event_context_init,
106             NULL,    /* value_table */
107         };
108         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
109     }
110     return type;
113 /**
114  * Callback to set up the SPEventContext vtable.
115  */
116 static void
117 sp_event_context_class_init(SPEventContextClass *klass)
119     GObjectClass *object_class;
121     object_class = (GObjectClass *) klass;
123     parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
125     object_class->dispose = sp_event_context_dispose;
127     klass->setup = sp_event_context_private_setup;
128     klass->root_handler = sp_event_context_private_root_handler;
129     klass->item_handler = sp_event_context_private_item_handler;
132 /**
133  * Clears all SPEventContext object members.
134  */
135 static void
136 sp_event_context_init(SPEventContext *event_context)
138     event_context->desktop = NULL;
139     event_context->cursor = NULL;
140     event_context->_message_context = NULL;
141     event_context->_selcue = NULL;
142     event_context->_grdrag = NULL;
143     event_context->space_panning = false;
144     event_context->shape_editor = NULL;
147 /**
148  * Callback to free and null member variables of SPEventContext object.
149  */
150 static void
151 sp_event_context_dispose(GObject *object)
153     SPEventContext *ec;
155     ec = SP_EVENT_CONTEXT(object);
157     if (ec->_message_context) {
158         delete ec->_message_context;
159     }
161     if (ec->cursor != NULL) {
162         gdk_cursor_unref(ec->cursor);
163         ec->cursor = NULL;
164     }
166     if (ec->desktop) {
167         ec->desktop = NULL;
168     }
170     if (ec->pref_observer) {
171         delete ec->pref_observer;
172     }
174     G_OBJECT_CLASS(parent_class)->dispose(object);
177 /**
178  * Recreates and draws cursor on desktop related to SPEventContext.
179  */
180 void
181 sp_event_context_update_cursor(SPEventContext *ec)
183     GtkWidget *w = GTK_WIDGET(sp_desktop_canvas(ec->desktop));
184     if (w->window) {
185         /* fixme: */
186         if (ec->cursor_shape) {
187             GdkBitmap *bitmap = NULL;
188             GdkBitmap *mask = NULL;
189             sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
190             if ((bitmap != NULL) && (mask != NULL)) {
191                 if (ec->cursor)
192                     gdk_cursor_unref (ec->cursor);
193                 ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
194                                                         &w->style->black,
195                                                         &w->style->white,
196                                                         ec->hot_x, ec->hot_y);
197                 g_object_unref (bitmap);
198                 g_object_unref (mask);
199             }
200         }
201         gdk_window_set_cursor(w->window, ec->cursor);
202         gdk_flush();
203     }
204     ec->desktop->waiting_cursor = false;
207 /**
208  * Callback that gets called on initialization of SPEventContext object.
209  * Redraws mouse cursor, at the moment.
210  */
211 static void
212 sp_event_context_private_setup(SPEventContext *ec)
214     sp_event_context_update_cursor(ec);
217 /**
218  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
219  */
220 gint gobble_key_events(guint keyval, gint mask)
222     GdkEvent *event_next;
223     gint i = 0;
225     event_next = gdk_event_get();
226     // while the next event is also a key notify with the same keyval and mask,
227     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
228            && event_next->key.keyval == keyval
229            && (!mask || (event_next->key.state & mask))) {
230         if (event_next->type == GDK_KEY_PRESS)
231             i ++; 
232         // kill it
233         gdk_event_free(event_next);
234         // get next
235         event_next = gdk_event_get();
236     }
237     // otherwise, put it back onto the queue
238     if (event_next) gdk_event_put(event_next);
240     return i;
243 /**
244  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
245 */
246 gint gobble_motion_events(gint mask)
248     GdkEvent *event_next;
249     gint i = 0;
251     event_next = gdk_event_get();
252     // while the next event is also a key notify with the same keyval and mask,
253     while (event_next && event_next->type == GDK_MOTION_NOTIFY
254            && (event_next->motion.state & mask)) {
255         // kill it
256         gdk_event_free(event_next);
257         // get next
258         event_next = gdk_event_get();
259         i ++;
260     }
261     // otherwise, put it back onto the queue
262     if (event_next) gdk_event_put(event_next);
264     return i;
267 /**
268  * Toggles current tool between active tool and selector tool.
269  * Subroutine of sp_event_context_private_root_handler().
270  */
271 static void
272 sp_toggle_selector(SPDesktop *dt)
274     if (!dt->event_context) return;
276     if (tools_isactive(dt, TOOLS_SELECT)) {
277         if (selector_toggled) {
278             if (switch_selector_to) tools_switch (dt, switch_selector_to);
279             selector_toggled = FALSE;
280         } else return;
281     } else {
282         selector_toggled = TRUE;
283         switch_selector_to = tools_active(dt);
284         tools_switch (dt, TOOLS_SELECT);
285     }
288 /**
289  * Toggles current tool between active tool and dropper tool.
290  * Subroutine of sp_event_context_private_root_handler().
291  */
292 static void
293 sp_toggle_dropper(SPDesktop *dt)
295     if (!dt->event_context) return;
297     if (tools_isactive(dt, TOOLS_DROPPER)) {
298         if (dropper_toggled) {
299             if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
300             dropper_toggled = FALSE;
301         } else return;
302     } else {
303         dropper_toggled = TRUE;
304         switch_dropper_to = tools_active(dt);
305         tools_switch (dt, TOOLS_DROPPER);
306     }
309 /**
310  * Calculates and keeps track of scroll acceleration.
311  * Subroutine of sp_event_context_private_root_handler().
312  */
313 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
315     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
317     /* key pressed within 500ms ? (1/2 second) */
318     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
319         scroll_multiply = 1; // abort acceleration
320     } else {
321         scroll_multiply += acceleration; // continue acceleration
322     }
324     scroll_event_time = ((GdkEventKey *) event)->time;
325     scroll_keyval = event->key.keyval;
327     return scroll_multiply;
330 /**
331  * Main event dispatch, gets called from Gdk.
332  */
333 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
335     static Geom::Point button_w;
336     static unsigned int panning = 0;
337     static unsigned int zoom_rb = 0;
339     SPDesktop *desktop = event_context->desktop;
340     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
342     /// @todo REmove redundant /value in preference keys
343     tolerance = prefs->getIntLimited(
344             "/options/dragtolerance/value", 0, 0, 100);
345     double const zoom_inc = prefs->getDoubleLimited(
346             "/options/zoomincrement/value", M_SQRT2, 1.01, 10);
347     double const acceleration = prefs->getDoubleLimited(
348             "/options/scrollingacceleration/value", 0, 0, 6);
349     int const key_scroll = prefs->getIntLimited(
350             "/options/keyscroll/value", 10, 0, 1000);
351     int const wheel_scroll = prefs->getIntLimited(
352             "/options/wheelscroll/value", 40, 0, 1000);
354     gint ret = FALSE;
356     switch (event->type) {
357         case GDK_2BUTTON_PRESS:
358             if (panning) {
359                 panning = 0;
360                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
361                         event->button.time);
362                 ret = TRUE;
363             } else {
364                 /* sp_desktop_dialog(); */
365             }
366             break;
367         case GDK_BUTTON_PRESS:
369             // save drag origin
370             xp = (gint) event->button.x;
371             yp = (gint) event->button.y;
372             within_tolerance = true;
374             button_w = Geom::Point(event->button.x, event->button.y);
376             switch (event->button.button) {
377                 case 1:
378                     if (event_context->space_panning) {
379                         panning = 1;
380                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
381                             GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
382                             NULL, event->button.time-1);
383                         ret = TRUE;
384                     }
385                     break;
386                 case 2:
387                     if (event->button.state == GDK_SHIFT_MASK) {
388                         zoom_rb = 2;
389                     } else {
390                         panning = 2;
391                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
392                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
393                             NULL, event->button.time-1);
394                     }
395                     ret = TRUE;
396                     break;
397                 case 3:
398                     if (event->button.state & GDK_SHIFT_MASK
399                             || event->button.state & GDK_CONTROL_MASK) {
400                         panning = 3;
401                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
402                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
403                                 NULL, event->button.time);
404                         ret = TRUE;
405                     } else {
406                         sp_event_root_menu_popup(desktop, NULL, event);
407                     }
408                     break;
409                 default:
410                     break;
411             }
412             break;
413         case GDK_MOTION_NOTIFY:
414             if (panning) {
415                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
416                         || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
417                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
418                    ) {
419                     /* Gdk seems to lose button release for us sometimes :-( */
420                     panning = 0;
421                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
422                             event->button.time);
423                     ret = TRUE;
424                 } else {
425                     if ( within_tolerance
426                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
427                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
428                     {
429                         // do not drag if we're within tolerance from origin
430                         break;
431                     }
432                     // Once the user has moved farther than tolerance from
433                     // the original location (indicating they intend to move
434                     // the object, not click), then always process the motion
435                     // notify coordinates as given (no snapping back to origin)
436                     within_tolerance = false;
438                     // gobble subsequent motion events to prevent "sticking"
439                     // when scrolling is slow
440                     gobble_motion_events(panning == 2 ?
441                                          GDK_BUTTON2_MASK :
442                                          (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
444                     Geom::Point const motion_w(event->motion.x, event->motion.y);
445                     Geom::Point const moved_w( motion_w - button_w );
446                     event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
447                     ret = TRUE;
448                 }
449             } else if (zoom_rb) {
450                 Geom::Point const motion_w(event->motion.x, event->motion.y);
451                 Geom::Point const motion_dt(desktop->w2d(motion_w));
453                 if ( within_tolerance
454                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
455                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
456                     break; // do not drag if we're within tolerance from origin
457                 }
458                 // Once the user has moved farther than tolerance from the original location
459                 // (indicating they intend to move the object, not click), then always process the
460                 // motion notify coordinates as given (no snapping back to origin)
461                 within_tolerance = false;
463                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
464                     Inkscape::Rubberband::get(desktop)->move(motion_dt);
465                 } else {
466                     Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
467                 } 
468                 if (zoom_rb == 2)
469                     gobble_motion_events(GDK_BUTTON2_MASK);
470             }
471             break;
472         case GDK_BUTTON_RELEASE:
473             xp = yp = 0;
474             if (within_tolerance && (panning || zoom_rb)) {
475                 zoom_rb = 0;
476                 if (panning) {
477                     panning = 0;
478                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
479                                       event->button.time);
480                 }
481                 Geom::Point const event_w(event->button.x, event->button.y);
482                 Geom::Point const event_dt(desktop->w2d(event_w));
483                 desktop->zoom_relative_keep_point(event_dt,
484                           (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
485                 desktop->updateNow();
486                 ret = TRUE;
487             } else if (panning == event->button.button) {
488                 panning = 0;
489                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
490                                       event->button.time);
492                 // in slow complex drawings, some of the motion events are lost;
493                 // to make up for this, we scroll it once again to the button-up event coordinates
494                 // (i.e. canvas will always get scrolled all the way to the mouse release point, 
495                 // even if few intermediate steps were visible)
496                 Geom::Point const motion_w(event->button.x, event->button.y);
497                 Geom::Point const moved_w( motion_w - button_w );
498                 event_context->desktop->scroll_world(moved_w);
499                 desktop->updateNow();
500                 ret = TRUE;
501             } else if (zoom_rb == event->button.button) {
502                 zoom_rb = 0;
503                 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
504                 Inkscape::Rubberband::get(desktop)->stop();
505                 if (b && !within_tolerance) {
506                     desktop->set_display_area(*b, 10);
507                 }
508                 ret = TRUE;
509             }
510             break;
511         case GDK_KEY_PRESS:
512             switch (get_group0_keyval(&event->key)) {
513                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
514                 // in the editing window). So we resteal them back and run our regular shortcut
515                 // invoker on them.
516                 unsigned int shortcut;
517                 case GDK_Tab: 
518                 case GDK_ISO_Left_Tab: 
519                 case GDK_F1:
520                     shortcut = get_group0_keyval(&event->key);
521                     if (event->key.state & GDK_SHIFT_MASK)
522                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
523                     if (event->key.state & GDK_CONTROL_MASK)
524                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
525                     if (event->key.state & GDK_MOD1_MASK)
526                         shortcut |= SP_SHORTCUT_ALT_MASK;
527                     ret = sp_shortcut_invoke(shortcut, desktop);
528                     break;
530                 case GDK_D:
531                 case GDK_d:
532                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
533                         sp_toggle_dropper(desktop);
534                         ret = TRUE;
535                     }
536                     break;
537                 case GDK_Q:
538                 case GDK_q:
539                                         if (desktop->quick_zoomed()) {
540                                                 ret = TRUE;
541                                         }
542                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
543                                                 desktop->zoom_quick(true);
544                         ret = TRUE;
545                     }
546                     break;
547                 case GDK_W:
548                 case GDK_w:
549                 case GDK_F4:
550                     /* Close view */
551                     if (MOD__CTRL_ONLY) {
552                         sp_ui_close_view(NULL);
553                         ret = TRUE;
554                     }
555                     break;
556                 case GDK_Left: // Ctrl Left
557                 case GDK_KP_Left:
558                 case GDK_KP_4:
559                     if (MOD__CTRL_ONLY) {
560                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
561                         gobble_key_events(get_group0_keyval(&event->key),
562                                 GDK_CONTROL_MASK);
563                         event_context->desktop->scroll_world(i, 0);
564                         ret = TRUE;
565                     }
566                     break;
567                 case GDK_Up: // Ctrl Up
568                 case GDK_KP_Up:
569                 case GDK_KP_8:
570                     if (MOD__CTRL_ONLY) {
571                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
572                         gobble_key_events(get_group0_keyval(&event->key),
573                                 GDK_CONTROL_MASK);
574                         event_context->desktop->scroll_world(0, i);
575                         ret = TRUE;
576                     }
577                     break;
578                 case GDK_Right: // Ctrl Right
579                 case GDK_KP_Right:
580                 case GDK_KP_6:
581                     if (MOD__CTRL_ONLY) {
582                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
583                         gobble_key_events(get_group0_keyval(&event->key),
584                                 GDK_CONTROL_MASK);
585                         event_context->desktop->scroll_world(-i, 0);
586                         ret = TRUE;
587                     }
588                     break;
589                 case GDK_Down: // Ctrl Down
590                 case GDK_KP_Down:
591                 case GDK_KP_2:
592                     if (MOD__CTRL_ONLY) {
593                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
594                         gobble_key_events(get_group0_keyval(&event->key),
595                                 GDK_CONTROL_MASK);
596                         event_context->desktop->scroll_world(0, -i);
597                         ret = TRUE;
598                     }
599                     break;
600                 case GDK_F10:
601                     if (MOD__SHIFT_ONLY) {
602                         sp_event_root_menu_popup(desktop, NULL, event);
603                         ret= TRUE;
604                     }
605                     break;
606                 case GDK_space:
607                     if (prefs->getBool("/options/spacepans/value")) {
608                         event_context->space_panning = true;
609                         event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
610                         ret= TRUE;
611                     } else {
612                         sp_toggle_selector(desktop);
613                         ret= TRUE;
614                     }
615                     break;
616                 case GDK_z:
617                 case GDK_Z:
618                     if (MOD__ALT_ONLY) {
619                         desktop->zoom_grab_focus();
620                         ret = TRUE;
621                     }
622                     break;
623                 default:
624                     break;
625             }
626             break;
627         case GDK_KEY_RELEASE:
628             switch (get_group0_keyval(&event->key)) {
629                 case GDK_space:
630                     if (event_context->space_panning) {
631                         event_context->space_panning = false;
632                         event_context->_message_context->clear();
633                         if (panning == 1) {
634                             panning = 0;
635                             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
636                                   event->key.time);
637                             desktop->updateNow();
638                         }
639                         ret= TRUE;
640                     } 
641                     break;
642                 case GDK_Q:
643                 case GDK_q:
644                                         if (desktop->quick_zoomed()) {
645                                                 desktop->zoom_quick(false);
646                         ret = TRUE;
647                     }
648                     break;
649                 default:
650                     break;
651             }
652             break;
653         case GDK_SCROLL:
654         {
655             bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
656             bool wheelzooms = prefs->getBool("/options/wheelzooms/value");
657             /* shift + wheel, pan left--right */
658             if (event->scroll.state & GDK_SHIFT_MASK) {
659                 switch (event->scroll.direction) {
660                     case GDK_SCROLL_UP:
661                         desktop->scroll_world(wheel_scroll, 0);
662                         break;
663                     case GDK_SCROLL_DOWN:
664                         desktop->scroll_world(-wheel_scroll, 0);
665                         break;
666                     default:
667                         break;
668                 }
670                 /* ctrl + wheel, zoom in--out */
671             } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
672                 double rel_zoom;
673                 switch (event->scroll.direction) {
674                     case GDK_SCROLL_UP:
675                         rel_zoom = zoom_inc;
676                         break;
677                     case GDK_SCROLL_DOWN:
678                         rel_zoom = 1 / zoom_inc;
679                         break;
680                     default:
681                         rel_zoom = 0.0;
682                         break;
683                 }
684                 if (rel_zoom != 0.0) {
685                     Geom::Point const scroll_dt = desktop->point();
686                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
687                 }
689                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
690             } else {
691                 switch (event->scroll.direction) {
692                     case GDK_SCROLL_UP:
693                         desktop->scroll_world(0, wheel_scroll);
694                         break;
695                     case GDK_SCROLL_DOWN:
696                         desktop->scroll_world(0, -wheel_scroll);
697                         break;
698                     case GDK_SCROLL_LEFT:
699                         desktop->scroll_world(wheel_scroll, 0);
700                         break;
701                     case GDK_SCROLL_RIGHT:
702                         desktop->scroll_world(-wheel_scroll, 0);
703                         break;
704                 }
705             }
706             break;
707         }
708         default:
709             break;
710     }
712     return ret;
715 /**
716  * Handles item specific events. Gets called from Gdk.
717  *
718  * Only reacts to right mouse button at the moment.
719  * \todo Fixme: do context sensitive popup menu on items.
720  */
721 gint
722 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
724     int ret = FALSE;
726     switch (event->type) {
727         case GDK_BUTTON_PRESS:
728             if ((event->button.button == 3)
729                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
730                 sp_event_root_menu_popup(ec->desktop, item, event);
731                 ret = TRUE;
732             }
733             break;
734         default:
735             break;
736     }
738     return ret;
741 /**
742  * @brief An observer that relays pref changes to the derived classes
743  */
744 class ToolPrefObserver : public Inkscape::Preferences::Observer {
745 public:
746     ToolPrefObserver(Glib::ustring const &path, SPEventContext *ec) :
747         Inkscape::Preferences::Observer(path),
748         _ec(ec) {}
749     virtual void notify(Inkscape::Preferences::Entry const &val)
750     {   
751         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set) {
752             ((SPEventContextClass *) G_OBJECT_GET_CLASS(_ec))->set(_ec,
753                 const_cast<Inkscape::Preferences::Entry*>(&val));
754         }
755     }
756 private:
757     SPEventContext * const _ec;
758 };
760 /**
761  * Creates new SPEventContext object and calls its virtual setup() function.
762  * @todo This is bogus. pref_path should be a private property of the inheriting objects.
763  */
764 SPEventContext *
765 sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path, unsigned int key)
767     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
768     g_return_val_if_fail(desktop != NULL, NULL);
770     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
772     ec->desktop = desktop;
773     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
774     ec->key = key;
775     ec->pref_observer = NULL;
776     
777     if (pref_path) {
778         ec->pref_observer = new ToolPrefObserver(pref_path, ec);
779         
780         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
781         prefs->addObserver(*(ec->pref_observer));
782     }
784     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
785         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
787     return ec;
790 /**
791  * Finishes SPEventContext.
792  */
793 void
794 sp_event_context_finish(SPEventContext *ec)
796     g_return_if_fail(ec != NULL);
797     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
799     ec->enableSelectionCue(false);
801     if (ec->next) {
802         g_warning("Finishing event context with active link\n");
803     }
805     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
806         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
809 //-------------------------------member functions
811 /**
812  * Enables/disables the SPEventContext's SelCue.
813  */
814 void SPEventContext::enableSelectionCue(bool enable) {
815     if (enable) {
816         if (!_selcue) {
817             _selcue = new Inkscape::SelCue(desktop);
818         }
819     } else {
820         delete _selcue;
821         _selcue = NULL;
822     }
825 /**
826  * Enables/disables the SPEventContext's GrDrag.
827  */
828 void SPEventContext::enableGrDrag(bool enable) {
829     if (enable) {
830         if (!_grdrag) {
831             _grdrag = new GrDrag(desktop);
832         }
833     } else {
834         if (_grdrag) {
835             delete _grdrag;
836             _grdrag = NULL;
837         }
838     }
841 /**
842  * Calls virtual set() function of SPEventContext.
843  */
844 void
845 sp_event_context_read(SPEventContext *ec, gchar const *key)
847     g_return_if_fail(ec != NULL);
848     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
849     g_return_if_fail(key != NULL);
851     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
852         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
853         Inkscape::Preferences::Entry val = prefs->getEntry(
854             ec->pref_observer->observed_path + '/' + key );
855         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, &val);
856     }
859 /**
860  * Calls virtual activate() function of SPEventContext.
861  */
862 void
863 sp_event_context_activate(SPEventContext *ec)
865     g_return_if_fail(ec != NULL);
866     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
868     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
869         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
872 /**
873  * Calls virtual deactivate() function of SPEventContext.
874  */
875 void
876 sp_event_context_deactivate(SPEventContext *ec)
878     g_return_if_fail(ec != NULL);
879     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
881     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
882         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
885 /**
886  * Calls virtual root_handler(), the main event handling function.
887  */
888 gint
889 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
891     gint ret;
893     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
895     set_event_location(event_context->desktop, event);
897     return ret;
900 /**
901  * Calls virtual item_handler(), the item event handling function.
902  */
903 gint
904 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
906     gint ret;
908     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
910     if (! ret) {
911         ret = sp_event_context_root_handler(event_context, event);
912     } else {
913         set_event_location(event_context->desktop, event);
914     }
916     return ret;
919 /**
920  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
921  */
922 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
924     if (event->type != GDK_MOTION_NOTIFY) {
925         return;
926     }
928     Geom::Point const button_w(event->button.x, event->button.y);
929     Geom::Point const button_dt(desktop->w2d(button_w));
930     desktop-> setPosition (button_dt);
931     desktop->set_coordinate_status(button_dt);
934 //-------------------------------------------------------------------
935 /**
936  * Create popup menu and tell Gtk to show it.
937  */
938 void
939 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
941     GtkWidget *menu;
943     /* fixme: This is not what I want but works for now (Lauris) */
944     if (event->type == GDK_KEY_PRESS) {
945         item = sp_desktop_selection(desktop)->singleItem();
946     }
947     menu = sp_ui_context_menu(desktop, item);
948     gtk_widget_show(menu);
950     switch (event->type) {
951         case GDK_BUTTON_PRESS:
952             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
953             break;
954         case GDK_KEY_PRESS:
955             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
956             break;
957         default:
958             break;
959     }
962 /**
963  * Show tool context specific modifier tip.
964  */
965 void
966 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
967         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
968         gchar const *alt_tip)
970     guint keyval = get_group0_keyval(&event->key);
972     bool ctrl = ctrl_tip && (MOD__CTRL
973             || (keyval == GDK_Control_L)
974             || (keyval == GDK_Control_R));
975     bool shift = shift_tip
976         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
977     bool alt = alt_tip
978         && (MOD__ALT
979                 || (keyval == GDK_Alt_L)
980                 || (keyval == GDK_Alt_R)
981                 || (keyval == GDK_Meta_L)
982                 || (keyval == GDK_Meta_R));
984     gchar *tip = g_strdup_printf("%s%s%s%s%s",
985                                  ( ctrl ? ctrl_tip : "" ),
986                                  ( ctrl && (shift || alt) ? "; " : "" ),
987                                  ( shift ? shift_tip : "" ),
988                                  ( (ctrl || shift) && alt ? "; " : "" ),
989                                  ( alt ? alt_tip : "" ));
991     if (strlen(tip) > 0) {
992         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
993     }
995     g_free(tip);
998 /**
999  * Return the keyval corresponding to the key event in group 0, i.e.,
1000  * in the main (English) layout.
1001  *
1002  * Use this instead of simply event->keyval, so that your keyboard shortcuts
1003  * work regardless of layouts (e.g., in Cyrillic).
1004  */
1005 guint
1006 get_group0_keyval(GdkEventKey *event)
1008     guint keyval = 0;
1009     gdk_keymap_translate_keyboard_state(
1010             gdk_keymap_get_for_display(gdk_display_get_default()),
1011             event->hardware_keycode,
1012             (GdkModifierType) event->state,
1013             0   /*event->key.group*/,
1014             &keyval, NULL, NULL, NULL);
1015     return keyval;
1018 /**
1019  * Returns item at point p in desktop.
1020  *
1021  * If state includes alt key mask, cyclically selects under; honors
1022  * into_groups.
1023  */
1024 SPItem *
1025 sp_event_context_find_item (SPDesktop *desktop, Geom::Point const &p,
1026         bool select_under, bool into_groups)
1028     SPItem *item;
1030     if (select_under) {
1031         SPItem *selected_at_point =
1032             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1033         item = desktop->item_at_point(p, into_groups, selected_at_point);
1034         if (item == NULL) { // we may have reached bottom, flip over to the top
1035             item = desktop->item_at_point(p, into_groups, NULL);
1036         }
1037     } else
1038         item = desktop->item_at_point(p, into_groups, NULL);
1040     return item;
1043 /**
1044  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1045  *
1046  * Honors into_groups.
1047  */
1048 SPItem *
1049 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, Geom::Point const &p)
1051     GSList *temp = NULL;
1052     temp = g_slist_prepend (temp, item);
1053     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1054     g_slist_free (temp);
1056     return item_at_point;
1059 ShapeEditor *
1060 sp_event_context_get_shape_editor (SPEventContext *ec)
1062     return ec->shape_editor;
1065 void
1066 event_context_print_event_info(GdkEvent *event, bool print_return) {
1067     switch (event->type) {
1068         case GDK_BUTTON_PRESS:
1069             g_print ("GDK_BUTTON_PRESS");
1070             break;
1071         case GDK_2BUTTON_PRESS:
1072             g_print ("GDK_2BUTTON_PRESS");
1073             break;
1074         case GDK_3BUTTON_PRESS:
1075             g_print ("GDK_3BUTTON_PRESS");
1076             break;
1078         case GDK_MOTION_NOTIFY:
1079             g_print ("GDK_MOTION_NOTIFY");
1080             break;
1081         case GDK_ENTER_NOTIFY:
1082             g_print ("GDK_ENTER_NOTIFY");
1083             break;
1085         case GDK_LEAVE_NOTIFY:
1086             g_print ("GDK_LEAVE_NOTIFY");
1087             break;
1088         case GDK_BUTTON_RELEASE:
1089             g_print ("GDK_BUTTON_RELEASE");
1090             break;
1092         case GDK_KEY_PRESS:
1093             g_print ("GDK_KEY_PRESS: %d", get_group0_keyval(&event->key));
1094             break;
1095         case GDK_KEY_RELEASE:
1096             g_print ("GDK_KEY_RELEASE: %d", get_group0_keyval(&event->key));
1097             break;
1098         default:
1099             //g_print ("even type not recognized");
1100             break;
1101     }
1103     if (print_return) {
1104         g_print ("\n");
1105     }
1108 /*
1109   Local Variables:
1110   mode:c++
1111   c-file-style:"stroustrup"
1112   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1113   indent-tabs-mode:nil
1114   fill-column:99
1115   End:
1116 */
1117 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :