Code

Applying patch #1415498 by James Kilfiger / zeimusu - Allow color & transparency...
[inkscape.git] / src / event-context.cpp
1 #define __SP_EVENT_CONTEXT_C__
3 /** \file
4  * Main event handling, and related helper functions.
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Frank Felfe <innerspace@iname.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 1999-2005 authors
12  * Copyright (C) 2001-2002 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 /** \class SPEventContext
18  * SPEventContext is an abstract base class of all tools. As the name
19  * indicates, event context implementations process UI events (mouse
20  * movements and keypresses) and take actions (like creating or modifying
21  * objects).  There is one event context implementation for each tool,
22  * plus few abstract base classes. Writing a new tool involves
23  * subclassing SPEventContext.
24  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <gdk/gdkkeysyms.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkmenu.h>
34 #include "display/sp-canvas.h"
35 #include "xml/node-event-vector.h"
36 #include "sp-cursor.h"
37 #include "shortcuts.h"
38 #include "desktop.h"
39 #include "desktop-handles.h"
40 #include "selection.h"
41 #include "file.h"
42 #include "interface.h"
43 #include "macros.h"
44 #include "tools-switch.h"
45 #include "prefs-utils.h"
46 #include "message-context.h"
47 #include "gradient-drag.h"
48 #include "object-edit.h"
49 #include "attributes.h"
50 #include "rubberband.h"
51 #include "selcue.h"
53 #include "event-context.h"
55 static void sp_event_context_class_init(SPEventContextClass *klass);
56 static void sp_event_context_init(SPEventContext *event_context);
57 static void sp_event_context_dispose(GObject *object);
59 static void sp_event_context_private_setup(SPEventContext *ec);
60 static gint sp_event_context_private_root_handler(SPEventContext *event_context, GdkEvent *event);
61 static gint sp_event_context_private_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
63 static void set_event_location(SPDesktop * desktop, GdkEvent * event);
65 static GObjectClass *parent_class;
67 // globals for temporary switching to selector by space
68 static bool selector_toggled = FALSE;
69 static int switch_selector_to = 0;
71 static gint xp = 0, yp = 0; // where drag started
72 static gint tolerance = 0;
73 static bool within_tolerance = false;
75 // globals for keeping track of keyboard scroll events in order to accelerate
76 static guint32 scroll_event_time = 0;
77 static gdouble scroll_multiply = 1;
78 static guint scroll_keyval = 0;
80 /**
81  * Registers the SPEventContext class with Glib and returns its type number.
82  */
83 GType
84 sp_event_context_get_type(void)
85 {
86     static GType type = 0;
87     if (!type) {
88         GTypeInfo info = {
89             sizeof(SPEventContextClass),
90             NULL, NULL,
91             (GClassInitFunc) sp_event_context_class_init,
92             NULL, NULL,
93             sizeof(SPEventContext),
94             4,
95             (GInstanceInitFunc) sp_event_context_init,
96             NULL,    /* value_table */
97         };
98         type = g_type_register_static(G_TYPE_OBJECT, "SPEventContext", &info, (GTypeFlags)0);
99     }
100     return type;
103 /**
104  * Callback to set up the SPEventContext vtable.
105  */
106 static void
107 sp_event_context_class_init(SPEventContextClass *klass)
109     GObjectClass *object_class;
111     object_class = (GObjectClass *) klass;
113     parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
115     object_class->dispose = sp_event_context_dispose;
117     klass->setup = sp_event_context_private_setup;
118     klass->root_handler = sp_event_context_private_root_handler;
119     klass->item_handler = sp_event_context_private_item_handler;
122 /**
123  * Clears all SPEventContext object members.
124  */
125 static void
126 sp_event_context_init(SPEventContext *event_context)
128     event_context->desktop = NULL;
129     event_context->cursor = NULL;
130     event_context->_message_context = NULL;
131     event_context->_selcue = NULL;
132     event_context->_grdrag = NULL;
135 /**
136  * Callback to free and null member variables of SPEventContext object.
137  */
138 static void
139 sp_event_context_dispose(GObject *object)
141     SPEventContext *ec;
143     ec = SP_EVENT_CONTEXT(object);
145     if (ec->_message_context) {
146         delete ec->_message_context;
147     }
149     if (ec->cursor != NULL) {
150         gdk_cursor_unref(ec->cursor);
151         ec->cursor = NULL;
152     }
154     if (ec->desktop) {
155         ec->desktop = NULL;
156     }
158     if (ec->prefs_repr) {
159         sp_repr_remove_listener_by_data(ec->prefs_repr, ec);
160         Inkscape::GC::release(ec->prefs_repr);
161         ec->prefs_repr = NULL;
162     }
164     G_OBJECT_CLASS(parent_class)->dispose(object);
167 /**
168  * Recreates and draws cursor on desktop related to SPEventContext.
169  */
170 void
171 sp_event_context_update_cursor(SPEventContext *ec)
173     GtkWidget *w = GTK_WIDGET(SP_DT_CANVAS(ec->desktop));
174     if (w->window) {
175         /* fixme: */
176         if (ec->cursor_shape) {
177             GdkDisplay *display=gdk_display_get_default();
178             if (
179                     gdk_display_supports_cursor_alpha(display) & 
180                     gdk_display_supports_cursor_color(display)
181                )
182             {
183                 GdkPixbuf *pixbuf =NULL;
184                 pixbuf=gdk_pixbuf_new_from_xpm_data((const char**)ec->cursor_shape);
185                 if (pixbuf !=NULL) { 
186                 ec->cursor = gdk_cursor_new_from_pixbuf(display,pixbuf,
187                         ec->hot_x,
188                         ec->hot_y); 
189                 }
190             }
191             else 
192             {
193                     GdkBitmap *bitmap = NULL;
194                 GdkBitmap *mask = NULL;
195                 sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, ec->cursor_shape);
196                 if ((bitmap != NULL) && (mask != NULL)) {
197                     if (ec->cursor)
198                         gdk_cursor_unref (ec->cursor);
199                     ec->cursor = gdk_cursor_new_from_pixmap(bitmap, mask,
200                             &w->style->black, 
201                             &w->style->white,
202                             ec->hot_x, ec->hot_y);
203                     g_object_unref (bitmap);
204                     g_object_unref (mask);
205                 }
206             }
207         }
208         gdk_window_set_cursor(w->window, ec->cursor);
209     }
212 /**
213  * Callback that gets called on initialization of SPEventContext object.
214  * Redraws mouse cursor, at the moment.
215  */
216 static void
217 sp_event_context_private_setup(SPEventContext *ec)
219     sp_event_context_update_cursor(ec);
222 /**
223  * \brief   Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
224  */
225 gint gobble_key_events(guint keyval, gint mask)
227     GdkEvent *event_next;
228     gint i = 0;
230     event_next = gdk_event_get();
231     // while the next event is also a key notify with the same keyval and mask,
232     while (event_next && event_next->type == GDK_KEY_PRESS
233            && event_next->key.keyval == keyval 
234            && (event_next->key.state & mask)) {
235         // kill it
236         gdk_event_free(event_next);
237         // get next
238         event_next = gdk_event_get();
239         i ++;
240     }
241     // otherwise, put it back onto the queue
242     if (event_next) gdk_event_put(event_next);
244     return i;
247 /**
248  * \brief   Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
249 */
250 gint gobble_motion_events(gint mask)
252     GdkEvent *event_next;
253     gint i = 0;
255     event_next = gdk_event_get();
256     // while the next event is also a key notify with the same keyval and mask,
257     while (event_next && event_next->type == GDK_MOTION_NOTIFY
258            && (event_next->motion.state & mask)) {
259         // kill it
260         gdk_event_free(event_next);
261         // get next
262         event_next = gdk_event_get();
263         i ++;
264     }
265     // otherwise, put it back onto the queue
266     if (event_next) gdk_event_put(event_next);
268     return i;
271 /**
272  * Toggles current tool between active tool and selector tool.
273  * Subroutine of sp_event_context_private_root_handler().
274  */
275 static void 
276 sp_toggle_selector(SPDesktop *dt)
278     if (!dt->event_context) return;
280     if (tools_isactive(dt, TOOLS_SELECT)) {
281         if (selector_toggled) {
282             if (switch_selector_to) tools_switch (dt, switch_selector_to);
283             selector_toggled = FALSE;
284         } else return;
285     } else {
286         selector_toggled = TRUE;
287         switch_selector_to = tools_active(dt);
288         tools_switch (dt, TOOLS_SELECT);
289     }
292 /**
293  * Calculates and keeps track of scroll acceleration.
294  * Subroutine of sp_event_context_private_root_handler().
295  */
296 static gdouble accelerate_scroll(GdkEvent *event, gdouble acceleration)
298     guint32 time_diff = ((GdkEventKey *) event)->time - scroll_event_time;
300     /* key pressed within 500ms ? (1/2 second) */
301     if (time_diff > 500 || event->key.keyval != scroll_keyval) {
302         scroll_multiply = 1;
303     } else {
304         scroll_multiply += acceleration;
305     }
307     scroll_event_time = ((GdkEventKey *) event)->time;
308     scroll_keyval = event->key.keyval;
310     return scroll_multiply;
313 // This is a hack that is necessary because when middle-clicking too fast,
314 // button_press events come for all clicks but there's button_release only 
315 // for the first one. So after a release, we must prohibit the next grab for 
316 // some time, or the grab will be stuck.  Perhaps this is caused by some 
317 // wrong handling of events among contexts and not by a GDK bug; 
318 // if someone can fix this properly this would be great.
319 static gint dontgrab = 0;
320 static bool 
321 grab_allow_again() 
323     dontgrab--; 
324     if (dontgrab < 0) dontgrab = 0;
325     return FALSE; // so that it is only called once
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             switch (event->button.button) {
371                 case 2:
373                     if (dontgrab) 
374                         // double-click, still not permitted to grab; 
375                         // increase the counter to guard against triple click
376                     {
377                         dontgrab ++; 
378                         gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
379                         break;
380                     }
382                     button_w = NR::Point(event->button.x,
383                                          event->button.y);
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                         button_w = NR::Point(event->button.x,
398                                              event->button.y);
399                         panning = 3;
400                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
401                                 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
402                                 NULL, event->button.time);
403                         ret = TRUE;
404                     } else {
405                         sp_event_root_menu_popup(desktop, NULL, event);
406                     }
407                     break;
408                 default:
409                     break;
410             }
411             break;
412         case GDK_MOTION_NOTIFY:
413             if (panning) {
414                 if ((panning == 2 && !(event->motion.state & GDK_BUTTON2_MASK)) 
415                         || (panning == 3 && !(event->motion.state & GDK_BUTTON3_MASK))) {
416                     /* Gdk seems to lose button release for us sometimes :-( */
417                     panning = 0;
418                     dontgrab = 0;
419                     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 
420                             event->button.time);
421                     ret = TRUE;
422                 } else {
423                     if ( within_tolerance
424                          && ( abs( (gint) event->motion.x - xp ) < tolerance )
425                          && ( abs( (gint) event->motion.y - yp ) < tolerance ))
426                     {
427                         // do not drag if we're within tolerance from origin
428                         break; 
429                     }
430                     // Once the user has moved farther than tolerance from 
431                     // the original location (indicating they intend to move 
432                     // the object, not click), then always process the motion 
433                     // notify coordinates as given (no snapping back to origin)
434                     within_tolerance = false; 
436                     // gobble subsequent motion events to prevent "sticking" 
437                     // when scrolling is slow
438                     gobble_motion_events(panning == 2 ? 
439                             GDK_BUTTON2_MASK : GDK_BUTTON3_MASK);
441                     NR::Point const motion_w(event->motion.x,
442                                              event->motion.y);
443                     NR::Point const moved_w( motion_w - button_w );
444                     event_context->desktop->scroll_world(moved_w);
445                     ret = TRUE;
446                 }
447             } else if (zoom_rb) {
448                 NR::Point const motion_w(event->motion.x, event->motion.y);
449                 NR::Point const motion_dt(desktop->w2d(motion_w));
451                 if ( within_tolerance
452                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
453                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
454                     break; // do not drag if we're within tolerance from origin
455                 }
457                 if (within_tolerance) {
458                     Inkscape::Rubberband::get()->start(desktop, motion_dt);
459                 } else {
460                     Inkscape::Rubberband::get()->move(motion_dt);
461                 }
463                 // Once the user has moved farther than tolerance from the original location 
464                 // (indicating they intend to move the object, not click), then always process the 
465                 // motion notify coordinates as given (no snapping back to origin)
466                 within_tolerance = false; 
467             }
468             break;
469         case GDK_BUTTON_RELEASE:
470             if (within_tolerance && (panning || zoom_rb)) {
471                 dontgrab ++;
472                 NR::Point const event_w(event->button.x, event->button.y);
473                 NR::Point const event_dt(desktop->w2d(event_w));
474                 double const zoom_power = ( (event->button.state & GDK_SHIFT_MASK)
475                                             ? -dontgrab : dontgrab );
476                 desktop->zoom_relative_keep_point(event_dt,
477                                                   pow(zoom_inc, zoom_power));
478                 gtk_timeout_add(250, (GtkFunction) grab_allow_again, NULL);
479             }
480             if (panning == event->button.button) {
481                 panning = 0;
482                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 
483                                       event->button.time);
484                 ret = TRUE;
485             } else if (zoom_rb == event->button.button) {
486                 zoom_rb = 0;
487                 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
488                 if (b != NR::Nothing() && !within_tolerance) {
489                     desktop->set_display_area(b.assume(), 10);
490                 }
491                 Inkscape::Rubberband::get()->stop();
492             }
493             xp = yp = 0; 
494             break;
495         case GDK_KEY_PRESS:
496             switch (get_group0_keyval(&event->key)) {
497                 unsigned int shortcut;
498                 case GDK_F1:
499                     /* Grab it away from Gtk */
500                     shortcut = get_group0_keyval(&event->key);
501                     if (event->key.state & GDK_SHIFT_MASK) 
502                         shortcut |= SP_SHORTCUT_SHIFT_MASK;
503                     if (event->key.state & GDK_CONTROL_MASK) 
504                         shortcut |= SP_SHORTCUT_CONTROL_MASK;
505                     if (event->key.state & GDK_MOD1_MASK) 
506                         shortcut |= SP_SHORTCUT_ALT_MASK;
507                     ret = sp_shortcut_invoke(shortcut, desktop);
508                     break;
510                 case GDK_Tab: // disable tab/shift-tab which cycle widget focus
511                 case GDK_ISO_Left_Tab: // they will get different functions
512                     if (!(MOD__CTRL_ONLY || (MOD__CTRL && MOD__SHIFT))) {
513                         ret = TRUE;
514                     } else {
515                         /* Grab it away from Gtk */
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                     }
525                     break;
526                 case GDK_W:
527                 case GDK_w:
528                 case GDK_F4:
529                     /* Close view */
530                     if (MOD__CTRL_ONLY) {
531                         sp_ui_close_view(NULL);
532                         ret = TRUE;
533                     }
534                     break;
535                     // FIXME: make import a verb
536                 case GDK_i: // Ctrl i - import file
537                     if (MOD__CTRL_ONLY) {
538                         sp_file_import(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));
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));
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));
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));
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                     sp_toggle_selector(desktop);
594                     ret= TRUE;
595                     break;
596                 case GDK_z:
597                 case GDK_Z:
598                     if (MOD__ALT_ONLY) {
599                         desktop->zoom_grab_focus();
600                         ret = TRUE;
601                     }
602                     break;
603                 default:
604                     break;
605             }
606             break;
607         case GDK_SCROLL:
608             /* shift + wheel, pan left--right */
609             if (event->scroll.state & GDK_SHIFT_MASK) {
610                 switch (event->scroll.direction) {
611                     case GDK_SCROLL_UP:
612                         desktop->scroll_world(wheel_scroll, 0);
613                         break;
614                     case GDK_SCROLL_DOWN:
615                         desktop->scroll_world(-wheel_scroll, 0);
616                         break;
617                     default:
618                         break;
619                 }
621                 /* ctrl + wheel, zoom in--out */
622             } else if (event->scroll.state & GDK_CONTROL_MASK) {
623                 double rel_zoom;
624                 switch (event->scroll.direction) {
625                     case GDK_SCROLL_UP:   
626                         rel_zoom = zoom_inc;
627                         break;
628                     case GDK_SCROLL_DOWN: 
629                         rel_zoom = 1 / zoom_inc;
630                         break;
631                     default:              
632                         rel_zoom = 0.0;
633                         break;
634                 }
635                 if (rel_zoom != 0.0) {
636                     NR::Point const scroll_dt = desktop->point();
637                     desktop->zoom_relative_keep_point(scroll_dt, rel_zoom);
638                 }
640                 /* no modifier, pan up--down (left--right on multiwheel mice?) */
641             } else {
642                 switch (event->scroll.direction) {
643                     case GDK_SCROLL_UP:
644                         desktop->scroll_world(0, wheel_scroll);
645                         break;
646                     case GDK_SCROLL_DOWN:
647                         desktop->scroll_world(0, -wheel_scroll);
648                         break;
649                     case GDK_SCROLL_LEFT:
650                         desktop->scroll_world(wheel_scroll, 0);
651                         break;
652                     case GDK_SCROLL_RIGHT:
653                         desktop->scroll_world(-wheel_scroll, 0);
654                         break;
655                 }
656             }
657             break;
658         default:
659             break;
660     }
662     return ret;
665 /**
666  * Handles item specific events. Gets called from Gdk. 
667  *
668  * Only reacts to right mouse button at the moment.
669  * \todo Fixme: do context sensitive popup menu on items.
670  */
671 gint
672 sp_event_context_private_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
674     int ret = FALSE;
676     switch (event->type) {
677         case GDK_BUTTON_PRESS:
678             if ((event->button.button == 3) 
679                     && !(event->button.state & GDK_SHIFT_MASK || event->button.state & GDK_CONTROL_MASK)) {
680                 sp_event_root_menu_popup(ec->desktop, item, event);
681                 ret = TRUE;
682             }
683             break;
684         default:
685             break;
686     }
688     return ret;
691 /**
692  * Gets called when attribute changes value.
693  */
694 static void
695 sp_ec_repr_attr_changed(Inkscape::XML::Node *prefs_repr, gchar const *key, gchar const *oldval, gchar const *newval,
696                         bool is_interactive, gpointer data)
698     SPEventContext *ec;
700     ec = SP_EVENT_CONTEXT(data);
702     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set) {
703         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, newval);
704     }
707 Inkscape::XML::NodeEventVector sp_ec_event_vector = {
708     NULL, /* Child added */
709     NULL, /* Child removed */
710     sp_ec_repr_attr_changed,
711     NULL, /* Content changed */
712     NULL /* Order changed */
713 };
715 /**
716  * Creates new SPEventContext object and calls its virtual setup() function.
717  */
718 SPEventContext *
719 sp_event_context_new(GType type, SPDesktop *desktop, Inkscape::XML::Node *prefs_repr, unsigned int key)
721     g_return_val_if_fail(g_type_is_a(type, SP_TYPE_EVENT_CONTEXT), NULL);
722     g_return_val_if_fail(desktop != NULL, NULL);
724     SPEventContext *const ec = (SPEventContext*)g_object_new(type, NULL);
726     ec->desktop = desktop;
727     ec->_message_context = new Inkscape::MessageContext(desktop->messageStack());
728     ec->key = key;
729     ec->prefs_repr = prefs_repr;
730     if (ec->prefs_repr) {
731         Inkscape::GC::anchor(ec->prefs_repr);
732         sp_repr_add_listener(ec->prefs_repr, &sp_ec_event_vector, ec);
733     }
735     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup)
736         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->setup(ec);
738     return ec;
741 /**
742  * Finishes SPEventContext.
743  */
744 void
745 sp_event_context_finish(SPEventContext *ec)
747     g_return_if_fail(ec != NULL);
748     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
750     ec->enableSelectionCue(false);
752     if (ec->next) {
753         g_warning("Finishing event context with active link\n");
754     }
756     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish)
757         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->finish(ec);
760 //-------------------------------member functions
762 /**
763  * Enables/disables the SPEventContext's SelCue.
764  */
765 void SPEventContext::enableSelectionCue(bool enable) {
766     if (enable) {
767         if (!_selcue) {
768             _selcue = new Inkscape::SelCue(desktop);
769         }
770     } else {
771         delete _selcue;
772         _selcue = NULL;
773     }
776 /**
777  * Enables/disables the SPEventContext's GrDrag.
778  */
779 void SPEventContext::enableGrDrag(bool enable) {
780     if (enable) {
781         if (!_grdrag) {
782             _grdrag = new GrDrag(desktop);
783         }
784     } else {
785         if (_grdrag) {
786             delete _grdrag;
787             _grdrag = NULL;
788         }
789     }
792 /**
793  * Calls virtual set() function of SPEventContext.
794  */
795 void
796 sp_event_context_read(SPEventContext *ec, gchar const *key)
798     g_return_if_fail(ec != NULL);
799     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
800     g_return_if_fail(key != NULL);
802     if (ec->prefs_repr) {
803         gchar const *val = ec->prefs_repr->attribute(key);
804         if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set)
805             ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->set(ec, key, val);
806     }
809 /**
810  * Calls virtual activate() function of SPEventContext.
811  */
812 void
813 sp_event_context_activate(SPEventContext *ec)
815     g_return_if_fail(ec != NULL);
816     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
818     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
819         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
822 /**
823  * Calls virtual deactivate() function of SPEventContext.
824  */
825 void
826 sp_event_context_deactivate(SPEventContext *ec)
828     g_return_if_fail(ec != NULL);
829     g_return_if_fail(SP_IS_EVENT_CONTEXT(ec));
831     if (((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate)
832         ((SPEventContextClass *) G_OBJECT_GET_CLASS(ec))->activate(ec);
835 /**
836  * Calls virtual root_handler(), the main event handling function.
837  */
838 gint
839 sp_event_context_root_handler(SPEventContext * event_context, GdkEvent * event)
841     gint ret;
843     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
845     set_event_location(event_context->desktop, event);
847     return ret;
850 /**
851  * Calls virtual item_handler(), the item event handling function.
852  */
853 gint
854 sp_event_context_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event)
856     gint ret;
858     ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
860     if (! ret) {
861         ret = sp_event_context_root_handler(event_context, event);
862     } else {
863         set_event_location(event_context->desktop, event);
864     }
866     return ret;
869 /**
870  * Emits 'position_set' signal on desktop and shows coordinates on status bar.
871  */
872 static void set_event_location(SPDesktop *desktop, GdkEvent *event)
874     if (event->type != GDK_MOTION_NOTIFY) {
875         return;
876     }
878     NR::Point const button_w(event->button.x, event->button.y);
879     NR::Point const button_dt(desktop->w2d(button_w));
880     desktop-> setPosition (button_dt);
881     desktop->set_coordinate_status(button_dt);
884 //-------------------------------------------------------------------
885 /**
886  * Create popup menu and tell Gtk to show it.
887  */
888 void
889 sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event)
891     GtkWidget *menu;
893     /* fixme: This is not what I want but works for now (Lauris) */
894     if (event->type == GDK_KEY_PRESS) {
895         item = SP_DT_SELECTION(desktop)->singleItem();
896     }
897     menu = sp_ui_context_menu(desktop, item);
898     gtk_widget_show(menu);
900     switch (event->type) {
901         case GDK_BUTTON_PRESS:
902             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, event->button.button, event->button.time);
903             break;
904         case GDK_KEY_PRESS:
905             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 0, NULL, 0, event->key.time);
906             break;
907         default:
908             break;
909     }
912 /**
913  * Show tool context specific modifier tip.
914  */
915 void
916 sp_event_show_modifier_tip(Inkscape::MessageContext *message_context, 
917         GdkEvent *event, gchar const *ctrl_tip, gchar const *shift_tip, 
918         gchar const *alt_tip)
920     guint keyval = get_group0_keyval(&event->key);
921     
922     bool ctrl = ctrl_tip && (MOD__CTRL 
923             || (keyval == GDK_Control_L) 
924             || (keyval == GDK_Control_R));
925     bool shift = shift_tip 
926         && (MOD__SHIFT || (keyval == GDK_Shift_L) || (keyval == GDK_Shift_R));
927     bool alt = alt_tip 
928         && (MOD__ALT 
929                 || (keyval == GDK_Alt_L) 
930                 || (keyval == GDK_Alt_R) 
931                 || (keyval == GDK_Meta_L) 
932                 || (keyval == GDK_Meta_R));
934     gchar *tip = g_strdup_printf("%s%s%s%s%s", 
935                                  ( ctrl ? ctrl_tip : "" ),
936                                  ( ctrl && (shift || alt) ? "; " : "" ),
937                                  ( shift ? shift_tip : "" ),
938                                  ( (ctrl || shift) && alt ? "; " : "" ),
939                                  ( alt ? alt_tip : "" ));
941     if (strlen(tip) > 0) {
942         message_context->flash(Inkscape::INFORMATION_MESSAGE, tip);
943     }
945     g_free(tip);
948 /**
949  * Return the keyval corresponding to the key event in group 0, i.e., 
950  * in the main (English) layout.  
951  *
952  * Use this instead of simply event->keyval, so that your keyboard shortcuts 
953  * work regardless of layouts (e.g., in Cyrillic).
954  */
955 guint
956 get_group0_keyval(GdkEventKey *event) 
958     guint keyval = 0;
959     gdk_keymap_translate_keyboard_state(
960             gdk_keymap_get_for_display(gdk_display_get_default()),
961             event->hardware_keycode, 
962             (GdkModifierType) event->state, 
963             0   /*event->key.group*/,
964             &keyval, NULL, NULL, NULL);
965     return keyval;
968 /**
969  * Returns item at point p in desktop.
970  *
971  * If state includes alt key mask, cyclically selects under; honors 
972  * into_groups.
973  */
974 SPItem *
975 sp_event_context_find_item (SPDesktop *desktop, NR::Point const p, 
976         bool select_under, bool into_groups)
978     SPItem *item;
980     if (select_under) {
981         SPItem *selected_at_point = 
982             desktop->item_from_list_at_point_bottom (desktop->selection->itemList(), p);
983         item = desktop->item_at_point(p, into_groups, selected_at_point);
984         if (item == NULL) { // we may have reached bottom, flip over to the top
985             item = desktop->item_at_point(p, into_groups, NULL);
986         }
987     } else 
988         item = desktop->item_at_point(p, into_groups, NULL);
990     return item;
993 /**
994  * Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
995  *
996  * Honors into_groups.
997  */
998 SPItem *
999 sp_event_context_over_item (SPDesktop *desktop, SPItem *item, NR::Point const p)
1001     GSList *temp = NULL;
1002     temp = g_slist_prepend (temp, item);
1003     SPItem *item_at_point = desktop->item_from_list_at_point_bottom (temp, p);
1004     g_slist_free (temp);
1006     return item_at_point;
1009 /**
1010  * Called when SPEventContext subclass node attribute changed.
1011  */
1012 void 
1013 ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr, gchar const *name, 
1014         gchar const *old_value, gchar const *new_value,
1015         bool const is_interactive, gpointer const data)
1017     if (!name 
1018             || !strcmp(name, "style") 
1019             || SP_ATTRIBUTE_IS_CSS(sp_attribute_lookup(name))) {
1020         // no need to regenrate knotholder if only style changed
1021         return;
1022     }
1024     SPEventContext *ec = SP_EVENT_CONTEXT(data);
1026     if (ec->shape_knot_holder) {
1027         sp_knot_holder_destroy(ec->shape_knot_holder);
1028     }
1029     ec->shape_knot_holder = NULL;
1031     SPDesktop *desktop = ec->desktop;
1033     SPItem *item = SP_DT_SELECTION(desktop)->singleItem();
1035     if (item) {
1036         ec->shape_knot_holder = sp_item_knot_holder(item, desktop);
1037     }
1041 /*
1042   Local Variables:
1043   mode:c++
1044   c-file-style:"stroustrup"
1045   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1046   indent-tabs-mode:nil
1047   fill-column:99
1048   End:
1049 */
1050 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :