Code

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