Code

Removal of SP_ACTIVE_DESKTOP, next take
[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         gdk_flush();
202     }
203     ec->desktop->waiting_cursor = false;
206 /**
207  * Callback that gets called on initialization of SPEventContext object.
208  * Redraws mouse cursor, at the moment.
209  */
210 static void
211 sp_event_context_private_setup(SPEventContext *ec)
213     sp_event_context_update_cursor(ec);
216 /**
217  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
218  */
219 gint gobble_key_events(guint keyval, gint mask)
221     GdkEvent *event_next;
222     gint i = 0;
224     event_next = gdk_event_get();
225     // while the next event is also a key notify with the same keyval and mask,
226     while (event_next && (event_next->type == GDK_KEY_PRESS || event_next->type == GDK_KEY_RELEASE)
227            && event_next->key.keyval == keyval
228            && (!mask || (event_next->key.state & mask))) {
229         if (event_next->type == GDK_KEY_PRESS)
230             i ++; 
231         // kill it
232         gdk_event_free(event_next);
233         // get next
234         event_next = gdk_event_get();
235     }
236     // otherwise, put it back onto the queue
237     if (event_next) gdk_event_put(event_next);
239     return i;
242 /**
243  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
244 */
245 gint gobble_motion_events(gint mask)
247     GdkEvent *event_next;
248     gint i = 0;
250     event_next = gdk_event_get();
251     // while the next event is also a key notify with the same keyval and mask,
252     while (event_next && event_next->type == GDK_MOTION_NOTIFY
253            && (event_next->motion.state & mask)) {
254         // kill it
255         gdk_event_free(event_next);
256         // get next
257         event_next = gdk_event_get();
258         i ++;
259     }
260     // otherwise, put it back onto the queue
261     if (event_next) gdk_event_put(event_next);
263     return i;
266 /**
267  * Toggles current tool between active tool and selector tool.
268  * Subroutine of sp_event_context_private_root_handler().
269  */
270 static void
271 sp_toggle_selector(SPDesktop *dt)
273     if (!dt->event_context) return;
275     if (tools_isactive(dt, TOOLS_SELECT)) {
276         if (selector_toggled) {
277             if (switch_selector_to) tools_switch (dt, switch_selector_to);
278             selector_toggled = FALSE;
279         } else return;
280     } else {
281         selector_toggled = TRUE;
282         switch_selector_to = tools_active(dt);
283         tools_switch (dt, TOOLS_SELECT);
284     }
287 /**
288  * Toggles current tool between active tool and dropper tool.
289  * Subroutine of sp_event_context_private_root_handler().
290  */
291 static void
292 sp_toggle_dropper(SPDesktop *dt)
294     if (!dt->event_context) return;
296     if (tools_isactive(dt, TOOLS_DROPPER)) {
297         if (dropper_toggled) {
298             if (switch_dropper_to) tools_switch (dt, switch_dropper_to);
299             dropper_toggled = FALSE;
300         } else return;
301     } else {
302         dropper_toggled = TRUE;
303         switch_dropper_to = tools_active(dt);
304         tools_switch (dt, TOOLS_DROPPER);
305     }
308 /**
309  * Calculates and keeps track of scroll acceleration.
310  * Subroutine of sp_event_context_private_root_handler().
311  */
312 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration, SPCanvas */*canvas*/)
314     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
316     /* key pressed within 500ms ? (1/2 second) */
317     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
318         scroll_multiply = 1; // abort acceleration
319     } else {
320         scroll_multiply += acceleration; // continue acceleration
321     }
323     scroll_event_time = ((GdkEventKey *) event)->time;
324     scroll_keyval = event->key.keyval;
326     return scroll_multiply;
329 /**
330  * Main event dispatch, gets called from Gdk.
331  */
332 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event)
334     static NR::Point button_w;
335     static unsigned int panning = 0;
336     static unsigned int zoom_rb = 0;
338     SPDesktop *desktop = event_context->desktop;
340     tolerance = prefs_get_int_attribute_limited(
341             "options.dragtolerance","value", 0, 0, 100);
342     double const zoom_inc = prefs_get_double_attribute_limited(
343             "options.zoomincrement", "value", M_SQRT2, 1.01, 10);
344     double const acceleration = prefs_get_double_attribute_limited(
345             "options.scrollingacceleration", "value", 0, 0, 6);
346     int const key_scroll = prefs_get_int_attribute_limited(
347             "options.keyscroll", "value", 10, 0, 1000);
348     int const wheel_scroll = prefs_get_int_attribute_limited(
349             "options.wheelscroll", "value", 40, 0, 1000);
351     gint ret = FALSE;
353     switch (event->type) {
354         case GDK_2BUTTON_PRESS:
355             if (panning) {
356                 panning = 0;
357                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
358                         event->button.time);
359                 ret = TRUE;
360             } else {
361                 /* sp_desktop_dialog(); */
362             }
363             break;
364         case GDK_BUTTON_PRESS:
366             // save drag origin
367             xp = (gint) event->button.x;
368             yp = (gint) event->button.y;
369             within_tolerance = true;
371             button_w = NR::Point(event->button.x, event->button.y);
373             switch (event->button.button) {
374                 case 1:
375                     if (event_context->space_panning) {
376                         panning = 1;
377                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
378                             GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
379                             NULL, event->button.time-1);
380                         ret = TRUE;
381                     }
382                     break;
383                 case 2:
384                     if (event->button.state == GDK_SHIFT_MASK) {
385                         zoom_rb = 2;
386                     } else {
387                         panning = 2;
388                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
389                             GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
390                             NULL, event->button.time-1);
391                     }
392                     ret = TRUE;
393                     break;
394                 case 3:
395                     if (event->button.state & GDK_SHIFT_MASK
396                             || event->button.state & GDK_CONTROL_MASK) {
397                         panning = 3;
398                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
399                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
400                                 NULL, event->button.time);
401                         ret = TRUE;
402                     } else {
403                         sp_event_root_menu_popup(desktop, NULL, event);
404                     }
405                     break;
406                 default:
407                     break;
408             }
409             break;
410         case GDK_MOTION_NOTIFY:
411             if (panning) {
412                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK))
413                         || (panning == 1 && !(event->motion.state & GDK_BUTTON1_MASK))
414                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))
415                    ) {
416                     /* Gdk seems to lose button release for us sometimes :-( */
417                     panning = 0;
418                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
419                             event->button.time);
420                     ret = TRUE;
421                 } else {
422                     if ( within_tolerance
423                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
424                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
425                     {
426                         // do not drag if we're within tolerance from origin
427                         break;
428                     }
429                     // Once the user has moved farther than tolerance from
430                     // the original location (indicating they intend to move
431                     // the object, not click), then always process the motion
432                     // notify coordinates as given (no snapping back to origin)
433                     within_tolerance = false;
435                     // gobble subsequent motion events to prevent "sticking"
436                     // when scrolling is slow
437                     gobble_motion_events(panning == 2 ?
438                                          GDK_BUTTON2_MASK :
439                                          (panning == 1 ? GDK_BUTTON1_MASK : GDK_BUTTON3_MASK));
441                     NR::Point const motion_w(event->motion.x, event->motion.y);
442                     NR::Point const moved_w( motion_w - button_w );
443                     event_context->desktop->scroll_world(moved_w, true); // we're still scrolling, do not redraw
444                     ret = TRUE;
445                 }
446             } else if (zoom_rb) {
447                 NR::Point const motion_w(event->motion.x, event->motion.y);
448                 NR::Point const motion_dt(desktop->w2d(motion_w));
450                 if ( within_tolerance
451                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
452                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
453                     break; // do not drag if we're within tolerance from origin
454                 }
455                 // Once the user has moved farther than tolerance from the original location
456                 // (indicating they intend to move the object, not click), then always process the
457                 // motion notify coordinates as given (no snapping back to origin)
458                 within_tolerance = false;
460                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
461                     Inkscape::Rubberband::get(desktop)->move(motion_dt);
462                 } else {
463                     Inkscape::Rubberband::get(desktop)->start(desktop, motion_dt);
464                 } 
465                 if (zoom_rb == 2)
466                     gobble_motion_events(GDK_BUTTON2_MASK);
467             }
468             break;
469         case GDK_BUTTON_RELEASE:
470             xp = yp = 0;
471             if (within_tolerance && (panning || zoom_rb)) {
472                 zoom_rb = 0;
473                 if (panning) {
474                     panning = 0;
475                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
476                                       event->button.time);
477                 }
478                 NR::Point const event_w(event->button.x, event->button.y);
479                 NR::Point const event_dt(desktop->w2d(event_w));
480                 desktop->zoom_relative_keep_point(event_dt,
481                           (event->button.state & GDK_SHIFT_MASK) ? 1/zoom_inc : zoom_inc);
482                 desktop->updateNow();
483                 ret = TRUE;
484             } else if (panning == event->button.button) {
485                 panning = 0;
486                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
487                                       event->button.time);
489                 // in slow complex drawings, some of the motion events are lost;
490                 // to make up for this, we scroll it once again to the button-up event coordinates
491                 // (i.e. canvas will always get scrolled all the way to the mouse release point, 
492                 // even if few intermediate steps were visible)
493                 NR::Point const motion_w(event->button.x, event->button.y);
494                 NR::Point const moved_w( motion_w - button_w );
495                 event_context->desktop->scroll_world(moved_w);
496                 desktop->updateNow();
497                 ret = TRUE;
498             } else if (zoom_rb == event->button.button) {
499                 zoom_rb = 0;
500                 boost::optional<NR::Rect> const b = Inkscape::Rubberband::get(desktop)->getRectangle();
501                 Inkscape::Rubberband::get(desktop)->stop();
502                 if (b && !within_tolerance) {
503                     desktop->set_display_area(*b, 10);
504                 }
505                 ret = TRUE;
506             }
507             break;
508         case GDK_KEY_PRESS:
509             switch (get_group0_keyval(&event->key)) {
510                 // GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
511                 // in the editing window). So we resteal them back and run our regular shortcut
512                 // invoker on them.
513                 unsigned int shortcut;
514                 case GDK_Tab: 
515                 case GDK_ISO_Left_Tab: 
516                 case GDK_F1:
517                     shortcut = get_group0_keyval(&event->key);
518                     if (event->key.state & GDK_SHIFT_MASK)
519                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
520                     if (event->key.state & GDK_CONTROL_MASK)
521                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
522                     if (event->key.state & GDK_MOD1_MASK)
523                         shortcut |= SP_SHORTCUT_ALT_MASK;
524                     ret = sp_shortcut_invoke(shortcut, desktop);
525                     break;
527                 case GDK_D:
528                 case GDK_d:
529                     if (!MOD__SHIFT && !MOD__CTRL && !MOD__ALT) {
530                         sp_toggle_dropper(desktop);
531                         ret = TRUE;
532                     }
533                     break;
534                 case GDK_W:
535                 case GDK_w:
536                 case GDK_F4:
537                     /* Close view */
538                     if (MOD__CTRL_ONLY) {
539                         sp_ui_close_view(NULL);
540                         ret = TRUE;
541                     }
542                     break;
543                 case GDK_Left: // Ctrl Left
544                 case GDK_KP_Left:
545                 case GDK_KP_4:
546                     if (MOD__CTRL_ONLY) {
547                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
548                         gobble_key_events(get_group0_keyval(&event->key),
549                                 GDK_CONTROL_MASK);
550                         event_context->desktop->scroll_world(i, 0);
551                         ret = TRUE;
552                     }
553                     break;
554                 case GDK_Up: // Ctrl Up
555                 case GDK_KP_Up:
556                 case GDK_KP_8:
557                     if (MOD__CTRL_ONLY) {
558                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
559                         gobble_key_events(get_group0_keyval(&event->key),
560                                 GDK_CONTROL_MASK);
561                         event_context->desktop->scroll_world(0, i);
562                         ret = TRUE;
563                     }
564                     break;
565                 case GDK_Right: // Ctrl Right
566                 case GDK_KP_Right:
567                 case GDK_KP_6:
568                     if (MOD__CTRL_ONLY) {
569                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
570                         gobble_key_events(get_group0_keyval(&event->key),
571                                 GDK_CONTROL_MASK);
572                         event_context->desktop->scroll_world(-i, 0);
573                         ret = TRUE;
574                     }
575                     break;
576                 case GDK_Down: // Ctrl Down
577                 case GDK_KP_Down:
578                 case GDK_KP_2:
579                     if (MOD__CTRL_ONLY) {
580                         int i = (int) floor(key_scroll * accelerate_scroll(event, acceleration, sp_desktop_canvas(desktop)));
581                         gobble_key_events(get_group0_keyval(&event->key),
582                                 GDK_CONTROL_MASK);
583                         event_context->desktop->scroll_world(0, -i);
584                         ret = TRUE;
585                     }
586                     break;
587                 case GDK_F10:
588                     if (MOD__SHIFT_ONLY) {
589                         sp_event_root_menu_popup(desktop, NULL, event);
590                         ret= TRUE;
591                     }
592                     break;
593                 case GDK_space:
594                     if (prefs_get_int_attribute("options.spacepans","value", 0) == 1) {
595                         event_context->space_panning = true;
596                         event_context->_message_context->set(Inkscape::INFORMATION_MESSAGE, _("<b>Space+mouse drag</b> to pan canvas"));
597                         ret= TRUE;
598                     } else {
599                         sp_toggle_selector(desktop);
600                         ret= TRUE;
601                     }
602                     break;
603                 case GDK_z:
604                 case GDK_Z:
605                     if (MOD__ALT_ONLY) {
606                         desktop->zoom_grab_focus();
607                         ret = TRUE;
608                     }
609                     break;
610                 default:
611                     break;
612             }
613             break;
614         case GDK_KEY_RELEASE:
615             switch (get_group0_keyval(&event->key)) {
616                 case GDK_space:
617                     if (event_context->space_panning) {
618                         event_context->space_panning = false;
619                         event_context->_message_context->clear();
620                         if (panning == 1) {
621                             panning = 0;
622                             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
623                                   event->key.time);
624                             desktop->updateNow();
625                         }
626                         ret= TRUE;
627                     } 
628                     break;
629                 default:
630                     break;
631             }
632             break;
633         case GDK_SCROLL:
634         {
635             bool ctrl = (event->scroll.state & GDK_CONTROL_MASK);
636             bool wheelzooms = (prefs_get_int_attribute("options.wheelzooms","value", 0) == 1);
637             /* shift + wheel, pan left--right */
638             if (event->scroll.state & GDK_SHIFT_MASK) {
639                 switch (event->scroll.direction) {
640                     case GDK_SCROLL_UP:
641                         desktop->scroll_world(wheel_scroll, 0);
642                         break;
643                     case GDK_SCROLL_DOWN:
644                         desktop->scroll_world(-wheel_scroll, 0);
645                         break;
646                     default:
647                         break;
648                 }
650                 /* ctrl + wheel, zoom in--out */
651             } else if ((ctrl && !wheelzooms) || (!ctrl && wheelzooms)) {
652                 double rel_zoom;
653                 switch (event->scroll.direction) {
654                     case GDK_SCROLL_UP:
655                         rel_zoom = zoom_inc;
656                         break;
657                     case GDK_SCROLL_DOWN:
658                         rel_zoom = 1 / zoom_inc;
659                         break;
660                     default:
661                         rel_zoom = 0.0;
662                         break;
663                 }
664                 if (rel_zoom != 0.0) {
665                     NR::Point const scroll_dt = desktop->point();
666                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
667                 }
669                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
670             } else {
671                 switch (event->scroll.direction) {
672                     case GDK_SCROLL_UP:
673                         desktop->scroll_world(0, wheel_scroll);
674                         break;
675                     case GDK_SCROLL_DOWN:
676                         desktop->scroll_world(0, -wheel_scroll);
677                         break;
678                     case GDK_SCROLL_LEFT:
679                         desktop->scroll_world(wheel_scroll, 0);
680                         break;
681                     case GDK_SCROLL_RIGHT:
682                         desktop->scroll_world(-wheel_scroll, 0);
683                         break;
684                 }
685             }
686             break;
687         }
688         default:
689             break;
690     }
692     return ret;
695 /**
696  * Handles item specific events. Gets called from Gdk.
697  *
698  * Only reacts to right mouse button at the moment.
699  * \todo Fixme: do context sensitive popup menu on items.
700  */
701 gint
702 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
704     int ret = FALSE;
706     switch (event->type) {
707         case GDK_BUTTON_PRESS:
708             if ((event->button.button == 3)
709                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
710                 sp_event_root_menu_popup(ec->desktop, item, event);
711                 ret = TRUE;
712             }
713             break;
714         default:
715             break;
716     }
718     return ret;
721 /**
722  * Gets called when attribute changes value.
723  */
724 static void
725 sp_ec_repr_attr_changed(Inkscape::XML::Node */*prefs_repr*/, gchar const *key, gchar const */*oldval*/, gchar const *newval,
726                         bool /*is_interactive*/, gpointer data)
728     SPEventContext *ec;
730     ec = SP_EVENT_CONTEXT(data);
732     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
733         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
734     }
737 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
738     NULL, /* Child added */
739     NULL, /* Child removed */
740     sp_ec_repr_attr_changed,
741     NULL, /* Content changed */
742     NULL /* Order changed */
743 };
745 /**
746  * Creates new SPEventContext object and calls its virtual setup() function.
747  */
748 SPEventContext *
749 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
751     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
752     g_return_val_if_fail(desktop != NULL, NULL);
754     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
756     ec->desktop = desktop;
757     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
758     ec->key = key;
759     ec->prefs_repr = prefs_repr;
760     if (ec->prefs_repr) {
761         Inkscape::GC::anchor(ec->prefs_repr);
762         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
763     }
765     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
766         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
768     return ec;
771 /**
772  * Finishes SPEventContext.
773  */
774 void
775 sp_event_context_finish(SPEventContext *ec)
777     g_return_if_fail(ec != NULL);
778     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
780     ec->enableSelectionCue(false);
782     if (ec->next) {
783         g_warning("Finishing event context with active link\n");
784     }
786     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
787         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
790 //-------------------------------member functions
792 /**
793  * Enables/disables the SPEventContext's SelCue.
794  */
795 void SPEventContext::enableSelectionCue(bool enable) {
796     if (enable) {
797         if (!_selcue) {
798             _selcue = new Inkscape::SelCue(desktop);
799         }
800     } else {
801         delete _selcue;
802         _selcue = NULL;
803     }
806 /**
807  * Enables/disables the SPEventContext's GrDrag.
808  */
809 void SPEventContext::enableGrDrag(bool enable) {
810     if (enable) {
811         if (!_grdrag) {
812             _grdrag = new GrDrag(desktop);
813         }
814     } else {
815         if (_grdrag) {
816             delete _grdrag;
817             _grdrag = NULL;
818         }
819     }
822 /**
823  * Calls virtual set() function of SPEventContext.
824  */
825 void
826 sp_event_context_read(SPEventContext *ec, gchar const *key)
828     g_return_if_fail(ec != NULL);
829     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
830     g_return_if_fail(key != NULL);
832     if (ec->prefs_repr) {
833         gchar const *val = ec->prefs_repr->attribute(key);
834         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
835             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
836     }
839 /**
840  * Calls virtual activate() function of SPEventContext.
841  */
842 void
843 sp_event_context_activate(SPEventContext *ec)
845     g_return_if_fail(ec != NULL);
846     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
848     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
849         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
852 /**
853  * Calls virtual deactivate() function of SPEventContext.
854  */
855 void
856 sp_event_context_deactivate(SPEventContext *ec)
858     g_return_if_fail(ec != NULL);
859     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
861     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate)
862         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->deactivate(ec);
865 /**
866  * Calls virtual root_handler(), the main event handling function.
867  */
868 gint
869 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
871     gint ret;
873     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
875     set_event_location(event_context->desktop, event);
877     return ret;
880 /**
881  * Calls virtual item_handler(), the item event handling function.
882  */
883 gint
884 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
886     gint ret;
888     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
890     if (! ret) {
891         ret = sp_event_context_root_handler(event_context, event);
892     } else {
893         set_event_location(event_context->desktop, event);
894     }
896     return ret;
899 /**
900  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
901  */
902 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
904     if (event->type != GDK_MOTION_NOTIFY) {
905         return;
906     }
908     NR::Point const button_w(event->button.x, event->button.y);
909     NR::Point const button_dt(desktop->w2d(button_w));
910     desktop-> setPosition (button_dt);
911     desktop->set_coordinate_status(button_dt);
914 //-------------------------------------------------------------------
915 /**
916  * Create popup menu and tell Gtk to show it.
917  */
918 void
919 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
921     GtkWidget *menu;
923     /* fixme: This is not what I want but works for now (Lauris) */
924     if (event->type == GDK_KEY_PRESS) {
925         item = sp_desktop_selection(desktop)->singleItem();
926     }
927     menu = sp_ui_context_menu(desktop, item);
928     gtk_widget_show(menu);
930     switch (event->type) {
931         case GDK_BUTTON_PRESS:
932             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
933             break;
934         case GDK_KEY_PRESS:
935             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
936             break;
937         default:
938             break;
939     }
942 /**
943  * Show tool context specific modifier tip.
944  */
945 void
946 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context,
947         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip,
948         gchar const *alt_tip)
950     guint keyval = get_group0_keyval(&event->key);
952     bool ctrl = ctrl_tip && (MOD__CTRL
953             || (keyval == GDK_Control_L)
954             || (keyval == GDK_Control_R));
955     bool shift = shift_tip
956         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
957     bool alt = alt_tip
958         && (MOD__ALT
959                 || (keyval == GDK_Alt_L)
960                 || (keyval == GDK_Alt_R)
961                 || (keyval == GDK_Meta_L)
962                 || (keyval == GDK_Meta_R));
964     gchar *tip = g_strdup_printf("%s%s%s%s%s",
965                                  ( ctrl ? ctrl_tip : "" ),
966                                  ( ctrl && (shift || alt) ? "; " : "" ),
967                                  ( shift ? shift_tip : "" ),
968                                  ( (ctrl || shift) && alt ? "; " : "" ),
969                                  ( alt ? alt_tip : "" ));
971     if (strlen(tip) > 0) {
972         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
973     }
975     g_free(tip);
978 /**
979  * Return the keyval corresponding to the key event in group 0, i.e.,
980  * in the main (English) layout.
981  *
982  * Use this instead of simply event->keyval, so that your keyboard shortcuts
983  * work regardless of layouts (e.g., in Cyrillic).
984  */
985 guint
986 get_group0_keyval(GdkEventKey *event)
988     guint keyval = 0;
989     gdk_keymap_translate_keyboard_state(
990             gdk_keymap_get_for_display(gdk_display_get_default()),
991             event->hardware_keycode,
992             (GdkModifierType) event->state,
993             0   /*event->key.group*/,
994             &keyval, NULL, NULL, NULL);
995     return keyval;
998 /**
999  * Returns item at point p in desktop.
1000  *
1001  * If state includes alt key mask, cyclically selects under; honors
1002  * into_groups.
1003  */
1004 SPItem *
1005 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p,
1006         bool select_under, bool into_groups)
1008     SPItem *item;
1010     if (select_under) {
1011         SPItem *selected_at_point =
1012             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
1013         item = desktop->item_at_point(p, into_groups, selected_at_point);
1014         if (item == NULL) { // we may have reached bottom, flip over to the top
1015             item = desktop->item_at_point(p, into_groups, NULL);
1016         }
1017     } else
1018         item = desktop->item_at_point(p, into_groups, NULL);
1020     return item;
1023 /**
1024  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
1025  *
1026  * Honors into_groups.
1027  */
1028 SPItem *
1029 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
1031     GSList *temp = NULL;
1032     temp = g_slist_prepend (temp, item);
1033     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1034     g_slist_free (temp);
1036     return item_at_point;
1039 /**
1040  * Called when SPEventContext subclass node attribute changed.
1041  */
1042 void
1043 ec_shape_event_attr_changed(Inkscape::XML::Node */*shape_repr*/, gchar const *name,
1044                             gchar const */*old_value*/, gchar const */*new_value*/,
1045                             bool const /*is_interactive*/, gpointer const data)
1047     if (!name
1048             || !strcmp(name, "style")
1049             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1050         // no need to regenrate knotholder if only style changed
1051         return;
1052     }
1054     SPEventContext *ec = SP_EVENT_CONTEXT(data);
1056     if (ec->shape_knot_holder) {
1057         delete ec->shape_knot_holder;
1058     }
1059     ec->shape_knot_holder = NULL;
1061     SPDesktop *desktop = ec->desktop;
1063     SPItem *item = sp_desktop_selection(desktop)->singleItem();
1065     if (item) {
1066         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1067     }
1071 /*
1072   Local Variables:
1073   mode:c++
1074   c-file-style:"stroustrup"
1075   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1076   indent-tabs-mode:nil
1077   fill-column:99
1078   End:
1079 */
1080 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :