Code

- The snap-delay mechanism should now be more robust. From now on, it must be turned...
[inkscape.git] / src / select-context.cpp
1 #define __SP_SELECT_CONTEXT_C__
3 /*
4  * Selection and transformation context
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
11  * Copyright (C) 1999-2005 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19 #include <cstring>
20 #include <string>
21 #include <gdk/gdkkeysyms.h>
22 #include "macros.h"
23 #include "rubberband.h"
24 #include "document.h"
25 #include "selection.h"
26 #include "seltrans-handles.h"
27 #include "sp-cursor.h"
28 #include "pixmaps/cursor-select-m.xpm"
29 #include "pixmaps/cursor-select-d.xpm"
30 #include "pixmaps/handles.xpm"
31 #include <glibmm/i18n.h>
33 #include "select-context.h"
34 #include "selection-chemistry.h"
35 #include "desktop.h"
36 #include "desktop-handles.h"
37 #include "sp-root.h"
38 #include "preferences.h"
39 #include "tools-switch.h"
40 #include "message-stack.h"
41 #include "selection-describer.h"
42 #include "seltrans.h"
43 #include "box3d.h"
45 static void sp_select_context_class_init(SPSelectContextClass *klass);
46 static void sp_select_context_init(SPSelectContext *select_context);
47 static void sp_select_context_dispose(GObject *object);
49 static void sp_select_context_setup(SPEventContext *ec);
50 static void sp_select_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
51 static gint sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event);
52 static gint sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
54 static SPEventContextClass *parent_class;
56 static GdkCursor *CursorSelectMouseover = NULL;
57 static GdkCursor *CursorSelectDragging = NULL;
58 GdkPixbuf *handles[13];
60 static gint rb_escaped = 0; // if non-zero, rubberband was canceled by esc, so the next button release should not deselect
61 static gint drag_escaped = 0; // if non-zero, drag was canceled by esc
63 static gint xp = 0, yp = 0; // where drag started
64 static gint tolerance = 0;
65 static bool within_tolerance = false;
67 GtkType
68 sp_select_context_get_type(void)
69 {
70     static GType type = 0;
71     if (!type) {
72         GTypeInfo info = {
73             sizeof(SPSelectContextClass),
74             NULL, NULL,
75             (GClassInitFunc) sp_select_context_class_init,
76             NULL, NULL,
77             sizeof(SPSelectContext),
78             4,
79             (GInstanceInitFunc) sp_select_context_init,
80             NULL,   /* value_table */
81         };
82         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSelectContext", &info, (GTypeFlags)0);
83     }
84     return type;
85 }
87 static void
88 sp_select_context_class_init(SPSelectContextClass *klass)
89 {
90     GObjectClass *object_class = (GObjectClass *) klass;
91     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
93     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
95     object_class->dispose = sp_select_context_dispose;
97     event_context_class->setup = sp_select_context_setup;
98     event_context_class->set = sp_select_context_set;
99     event_context_class->root_handler = sp_select_context_root_handler;
100     event_context_class->item_handler = sp_select_context_item_handler;
102     // cursors in select context
103     CursorSelectMouseover = sp_cursor_new_from_xpm(cursor_select_m_xpm , 1, 1);
104     CursorSelectDragging = sp_cursor_new_from_xpm(cursor_select_d_xpm , 1, 1);
105     // selection handles
106     handles[0]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_nw_xpm);
107     handles[1]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_ne_xpm);
108     handles[2]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_h_xpm);
109     handles[3]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_v_xpm);
110     handles[4]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_nw_xpm);
111     handles[5]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_n_xpm);
112     handles[6]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_ne_xpm);
113     handles[7]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_e_xpm);
114     handles[8]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_se_xpm);
115     handles[9]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_s_xpm);
116     handles[10] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_sw_xpm);
117     handles[11] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_w_xpm);
118     handles[12] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_center_xpm);
122 static void
123 sp_select_context_init(SPSelectContext *sc)
125     sc->dragging = FALSE;
126     sc->moved = FALSE;
127     sc->button_press_shift = false;
128     sc->button_press_ctrl = false;
129     sc->button_press_alt = false;
130     sc->_seltrans = NULL;
131     sc->_describer = NULL;
134 static void
135 sp_select_context_dispose(GObject *object)
137     SPSelectContext *sc = SP_SELECT_CONTEXT(object);
138     SPEventContext * ec = SP_EVENT_CONTEXT (object);
140     ec->enableGrDrag(false);
142     if (sc->grabbed) {
143         sp_canvas_item_ungrab(sc->grabbed, GDK_CURRENT_TIME);
144         sc->grabbed = NULL;
145     }
147     delete sc->_seltrans;
148     sc->_seltrans = NULL;
149     delete sc->_describer;
150     sc->_describer = NULL;
152     if (CursorSelectDragging) {
153         gdk_cursor_unref (CursorSelectDragging);
154         CursorSelectDragging = NULL;
155     }
156     if (CursorSelectMouseover) {
157         gdk_cursor_unref (CursorSelectMouseover);
158         CursorSelectMouseover = NULL;
159     }
161     G_OBJECT_CLASS(parent_class)->dispose(object);
164 static void
165 sp_select_context_setup(SPEventContext *ec)
167     SPSelectContext *select_context = SP_SELECT_CONTEXT(ec);
169     if (((SPEventContextClass *) parent_class)->setup) {
170         ((SPEventContextClass *) parent_class)->setup(ec);
171     }
173     SPDesktop *desktop = ec->desktop;
175     select_context->_describer = new Inkscape::SelectionDescriber(desktop->selection, desktop->messageStack());
177     select_context->_seltrans = new Inkscape::SelTrans(desktop);
179     sp_event_context_read(ec, "show");
180     sp_event_context_read(ec, "transform");
182     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
183     if (prefs->getBool("/tools/select/gradientdrag")) {
184         ec->enableGrDrag();
185     }
188 static void
189 sp_select_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
191     SPSelectContext *sc = SP_SELECT_CONTEXT(ec);
192     Glib::ustring path = val->getEntryName();
194     if (path == "show") {
195         if (val->getString() == "outline") {
196             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_OUTLINE);
197         } else {
198             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_CONTENT);
199         }
200     }
203 static bool
204 sp_select_context_abort(SPEventContext *event_context)
206     SPDesktop *desktop = event_context->desktop;
207     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
208     Inkscape::SelTrans *seltrans = sc->_seltrans;
210     if (sc->dragging) {
211         if (sc->moved) { // cancel dragging an object
212             seltrans->ungrab();
213             sc->moved = FALSE;
214             sc->dragging = FALSE;
215             drag_escaped = 1;
217             if (sc->item) {
218                 // only undo if the item is still valid
219                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
220                     sp_document_undo(sp_desktop_document(desktop));
221                 }
223                 sp_object_unref( SP_OBJECT(sc->item), NULL);
224             } else if (sc->button_press_ctrl) {
225                 // NOTE:  This is a workaround to a bug.
226                 // When the ctrl key is held, sc->item is not defined
227                 // so in this case (only), we skip the object doc check
228                 sp_document_undo(sp_desktop_document(desktop));
229             }
230             sc->item = NULL;
232             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Move canceled."));
233             return true;
234         }
235     } else {
236         if (Inkscape::Rubberband::get(desktop)->is_started()) {
237             Inkscape::Rubberband::get(desktop)->stop();
238             rb_escaped = 1;
239             SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
240             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
241             return true;
242         }
243     }
244     return false;
247 bool
248 key_is_a_modifier (guint key) {
249     return (key == GDK_Alt_L ||
250                 key == GDK_Alt_R ||
251                 key == GDK_Control_L ||
252                 key == GDK_Control_R ||
253                 key == GDK_Shift_L ||
254                 key == GDK_Shift_R ||
255                 key == GDK_Meta_L ||  // Meta is when you press Shift+Alt (at least on my machine)
256             key == GDK_Meta_R);
259 void
260 sp_select_context_up_one_layer(SPDesktop *desktop)
262     /* Click in empty place, go up one level -- but don't leave a layer to root.
263      *
264      * (Rationale: we don't usually allow users to go to the root, since that
265      * detracts from the layer metaphor: objects at the root level can in front
266      * of or behind layers.  Whereas it's fine to go to the root if editing
267      * a document that has no layers (e.g. a non-Inkscape document).)
268      *
269      * Once we support editing SVG "islands" (e.g. <svg> embedded in an xhtml
270      * document), we might consider further restricting the below to disallow
271      * leaving a layer to go to a non-layer.
272      */
273     SPObject *const current_layer = desktop->currentLayer();
274     if (current_layer) {
275         SPObject *const parent = SP_OBJECT_PARENT(current_layer);
276         if ( parent
277              && ( SP_OBJECT_PARENT(parent)
278                   || !( SP_IS_GROUP(current_layer)
279                         && ( SPGroup::LAYER
280                              == SP_GROUP(current_layer)->layerMode() ) ) ) )
281         {
282             desktop->setCurrentLayer(parent);
283             if (SP_IS_GROUP(current_layer) && SPGroup::LAYER != SP_GROUP(current_layer)->layerMode())
284                 sp_desktop_selection(desktop)->set(current_layer);
285         }
286     }
289 static gint
290 sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
292     gint ret = FALSE;
294     SPDesktop *desktop = event_context->desktop;
295     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
296     Inkscape::SelTrans *seltrans = sc->_seltrans;
298     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
299     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
301     // make sure we still have valid objects to move around
302     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
303         sp_select_context_abort(event_context);
304     }
306     switch (event->type) {
307         case GDK_BUTTON_PRESS:
308             if (event->button.button == 1 && !event_context->space_panning) {
309                 /* Left mousebutton */
311                 // save drag origin
312                 xp = (gint) event->button.x;
313                 yp = (gint) event->button.y;
314                 within_tolerance = true;
316                 // remember what modifiers were on before button press
317                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
318                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
319                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
321                 if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
322                     // if shift or ctrl was pressed, do not move objects;
323                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
324                 } else {
325                     sc->dragging = TRUE;
326                     sc->moved = FALSE;
328                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
330                     // remember the clicked item in sc->item:
331                     if (sc->item) {
332                         sp_object_unref(sc->item, NULL);
333                         sc->item = NULL;
334                     }
335                     sc->item = sp_event_context_find_item (desktop,
336                                               Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
337                     sp_object_ref(sc->item, NULL);
339                     rb_escaped = drag_escaped = 0;
341                     if (sc->grabbed) {
342                         sp_canvas_item_ungrab(sc->grabbed, event->button.time);
343                         sc->grabbed = NULL;
344                     }
345                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
346                                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
347                                         GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
348                                         NULL, event->button.time);
349                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
351                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
353                     ret = TRUE;
354                 }
355             } else if (event->button.button == 3) {
356                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
357                 sp_select_context_abort(event_context);
358             }
359             break;
361         case GDK_ENTER_NOTIFY:
362         {
363             GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
364             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
365             gdk_cursor_destroy(cursor);
366             break;
367         }
369         case GDK_LEAVE_NOTIFY:
370             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
371             break;
373         case GDK_KEY_PRESS:
374             if (get_group0_keyval (&event->key) == GDK_space) {
375                 if (sc->dragging && sc->grabbed) {
376                     /* stamping mode: show content mode moving */
377                     seltrans->stamp();
378                     ret = TRUE;
379                 }
380             }
381             break;
383         default:
384             break;
385     }
387     if (!ret) {
388         if (((SPEventContextClass *) parent_class)->item_handler)
389             ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
390     }
392     return ret;
395 static gint
396 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
398     SPItem *item = NULL;
399     SPItem *item_at_point = NULL, *group_at_point = NULL, *item_in_group = NULL;
400     gint ret = FALSE;
402     SPDesktop *desktop = event_context->desktop;
403     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
404     Inkscape::SelTrans *seltrans = sc->_seltrans;
405     Inkscape::Selection *selection = sp_desktop_selection(desktop);
406     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
408     gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
409     gdouble const offset = prefs->getDoubleLimited("/options/defaultscale/value", 2, 0, 1000);
410     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
411     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
413     // make sure we still have valid objects to move around
414     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
415         sp_select_context_abort(event_context);
416     }
418     switch (event->type) {
419         case GDK_2BUTTON_PRESS:
420             if (event->button.button == 1) {
421                 if (!selection->isEmpty()) {
422                     SPItem *clicked_item = (SPItem *) selection->itemList()->data;
423                     if (SP_IS_GROUP(clicked_item) && !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
424                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
425                         sp_desktop_selection(desktop)->clear();
426                         sc->dragging = false;
428                         sp_canvas_end_forced_full_redraws(desktop->canvas);
429                     } else { // switch tool
430                         tools_switch_by_item (desktop, clicked_item);
431                     }
432                 } else {
433                     sp_select_context_up_one_layer(desktop);
434                 }
435                 ret = TRUE;
436             }
437             break;
438         case GDK_BUTTON_PRESS:
439             if (event->button.button == 1 && !event_context->space_panning) {
441                 // save drag origin
442                 xp = (gint) event->button.x;
443                 yp = (gint) event->button.y;
444                 within_tolerance = true;
446                 Geom::Point const button_pt(event->button.x, event->button.y);
447                 Geom::Point const p(desktop->w2d(button_pt));
448                 if (event->button.state & GDK_MOD1_MASK)
449                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
450                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
451                 if (sc->grabbed) {
452                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
453                     sc->grabbed = NULL;
454                 }
455                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
456                                     GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
457                                     NULL, event->button.time);
458                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
460                 // remember what modifiers were on before button press
461                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
462                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
463                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
465                 sc->moved = FALSE;
467                 rb_escaped = drag_escaped = 0;
469                 ret = TRUE;
470             } else if (event->button.button == 3) {
471                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
472                 sp_select_context_abort(event_context);
473             }
474             break;
476         case GDK_MOTION_NOTIFY:
477             if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
478                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
479                 Geom::Point const p(desktop->w2d(motion_pt));
481                 if ( within_tolerance
482                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
483                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
484                     break; // do not drag if we're within tolerance from origin
485                 }
486                 // Once the user has moved farther than tolerance from the original location
487                 // (indicating they intend to move the object, not click), then always process the
488                 // motion notify coordinates as given (no snapping back to origin)
489                 within_tolerance = false;
491                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
492                     // if it's not click and ctrl or alt was pressed (the latter with some selection
493                     // but not with shift) we want to drag rather than rubberband
494                     sc->dragging = TRUE;
496                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
497                 }
499                 if (sc->dragging) {
500                     /* User has dragged fast, so we get events on root (lauris)*/
501                     // not only that; we will end up here when ctrl-dragging as well
502                     // and also when we started within tolerance, but trespassed tolerance outside of item
503                     Inkscape::Rubberband::get(desktop)->stop();
504                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
505                     item_at_point = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), FALSE);
506                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
507                         item_at_point = desktop->item_at_point(Geom::Point(xp, yp), FALSE);
508                     if (item_at_point || sc->moved || sc->button_press_alt) {
509                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
510                         if (!sc->moved) {
511                             item_in_group = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), TRUE);
512                             group_at_point = desktop->group_at_point(Geom::Point(event->button.x, event->button.y));
514                             // group-at-point is meant to be topmost item if it's a group,
515                             // not topmost group of all items at point
516                             if (group_at_point != item_in_group &&
517                                 !(group_at_point && item_at_point &&
518                                   group_at_point->isAncestorOf(item_at_point)))
519                                 group_at_point = NULL;
521                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
522                             if ((!item_in_group || !selection->includes(item_in_group)) &&
523                                 (!group_at_point || !selection->includes(group_at_point))
524                                 && !sc->button_press_alt) {
525                                 // select what is under cursor
526                                 if (!seltrans->isEmpty()) {
527                                     seltrans->resetState();
528                                 }
529                                 // when simply ctrl-dragging, we don't want to go into groups
530                                 if (item_at_point && !selection->includes(item_at_point))
531                                     selection->set(item_at_point);
532                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
533                             seltrans->grab(p, -1, -1, FALSE);
534                             sc->moved = TRUE;
535                         }
536                         if (!seltrans->isEmpty())
537                             seltrans->moveTo(p, event->button.state);
538                         desktop->scroll_to_point(p);
539                         gobble_motion_events(GDK_BUTTON1_MASK);
540                         ret = TRUE;
541                     } else {
542                         sc->dragging = FALSE;
544                         sp_canvas_end_forced_full_redraws(desktop->canvas);
545                     }
546                 } else {
547                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
548                         Inkscape::Rubberband::get(desktop)->move(p);
549                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
550                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
551                         } else {
552                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
553                         }
554                         gobble_motion_events(GDK_BUTTON1_MASK);
555                     }
556                 }
557             }
558             break;
559         case GDK_BUTTON_RELEASE:
560             xp = yp = 0;
561             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
562                 if (sc->dragging) {
563                     if (sc->moved) {
564                         // item has been moved
565                         seltrans->ungrab();
566                         sc->moved = FALSE;
567                     } else if (sc->item && !drag_escaped) {
568                         // item has not been moved -> simply a click, do selecting
569                         if (!selection->isEmpty()) {
570                             if (event->button.state & GDK_SHIFT_MASK) {
571                                 // with shift, toggle selection
572                                 seltrans->resetState();
573                                 selection->toggle(sc->item);
574                             } else {
575                                 // without shift, increase state (i.e. toggle scale/rotation handles)
576                                 if (selection->includes(sc->item)) {
577                                     seltrans->increaseState();
578                                 } else {
579                                     seltrans->resetState();
580                                     selection->set(sc->item);
581                                 }
582                             }
583                         } else { // simple or shift click, no previous selection
584                             seltrans->resetState();
585                             selection->set(sc->item);
586                         }
587                     }
588                     sc->dragging = FALSE;
590                     sp_canvas_end_forced_full_redraws(desktop->canvas);
592                     if (sc->item) {
593                         sp_object_unref( SP_OBJECT(sc->item), NULL);
594                     }
595                     sc->item = NULL;
596                 } else {
597                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
598                     if (r->is_started() && !within_tolerance) {
599                         // this was a rubberband drag
600                         GSList *items = NULL;
601                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
602                             Geom::OptRect const b = r->getRectangle();
603                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
604                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
605                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
606                         }
608                         seltrans->resetState();
609                         r->stop();
610                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
612                         if (event->button.state & GDK_SHIFT_MASK) {
613                             // with shift, add to selection
614                             selection->addList (items);
615                         } else {
616                             // without shift, simply select anew
617                             selection->setList (items);
618                         }
619                         g_slist_free (items);
620                     } else { // it was just a click, or a too small rubberband
621                         r->stop();
622                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
623                             // this was a shift+click or alt+shift+click, select what was clicked upon
625                             sc->button_press_shift = false;
627                             if (sc->button_press_ctrl) {
628                                 // go into groups, honoring Alt
629                                 item = sp_event_context_find_item (desktop,
630                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
631                                 sc->button_press_ctrl = FALSE;
632                             } else {
633                                 // don't go into groups, honoring Alt
634                                 item = sp_event_context_find_item (desktop,
635                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
636                             }
638                             if (item) {
639                                 selection->toggle(item);
640                                 item = NULL;
641                             }
643                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
645                             item = sp_event_context_find_item (desktop,
646                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
648                             sc->button_press_ctrl = FALSE;
649                             sc->button_press_alt = FALSE;
651                             if (item) {
652                                 if (selection->includes(item)) {
653                                     seltrans->increaseState();
654                                 } else {
655                                     seltrans->resetState();
656                                     selection->set(item);
657                                 }
658                                 item = NULL;
659                             }
661                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
662                             if (!selection->isEmpty()) {
663                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
664                                     selection->clear();
665                                 rb_escaped = 0;
666                                 ret = TRUE;
667                             }
668                         }
669                     }
670                     ret = TRUE;
671                 }
672                 if (sc->grabbed) {
673                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
674                     sc->grabbed = NULL;
675                 }
677                 desktop->updateNow();
678             }
679             if (event->button.button == 1) {
680                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
681             }
682             sc->button_press_shift = false;
683             sc->button_press_ctrl = false;
684             sc->button_press_alt = false;
685             break;
687         case GDK_KEY_PRESS: // keybindings for select context
689             {
690             guint keyval = get_group0_keyval(&event->key);
691             bool alt = ( MOD__ALT
692                                     || (keyval == GDK_Alt_L)
693                                     || (keyval == GDK_Alt_R)
694                                     || (keyval == GDK_Meta_L)
695                                     || (keyval == GDK_Meta_R));
697             if (!key_is_a_modifier (keyval)) {
698                     event_context->defaultMessageContext()->clear();
699             } else if (sc->grabbed || seltrans->isGrabbed()) {
700                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
701                     // if Alt then change cursor to moving cursor:
702                     if (alt) {
703                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
704                     }
705                 } else {
706                 // do not change the statusbar text when mousekey is down to move or transform the object,
707                 // because the statusbar text is already updated somewhere else.
708                    break;
709                 }
710             } else {
711                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
712                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
713                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
714                                                 _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
715                     // if Alt and nonempty selection, show moving cursor ("move selected"):
716                     if (alt && !selection->isEmpty()) {
717                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
718                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
719                         gdk_cursor_destroy(cursor);
720                     }
721                     //*/
722                     break;
723             }
724             }
726             switch (get_group0_keyval (&event->key)) {
727                 case GDK_Left: // move selection left
728                 case GDK_KP_Left:
729                 case GDK_KP_4:
730                     if (!MOD__CTRL) { // not ctrl
731                         gint mul = 1 + gobble_key_events(
732                             get_group0_keyval(&event->key), 0); // with any mask
733                         if (MOD__ALT) { // alt
734                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
735                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
736                         }
737                         else { // no alt
738                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
739                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
740                         }
741                         ret = TRUE;
742                     }
743                     break;
744                 case GDK_Up: // move selection up
745                 case GDK_KP_Up:
746                 case GDK_KP_8:
747                     if (!MOD__CTRL) { // not ctrl
748                         gint mul = 1 + gobble_key_events(
749                             get_group0_keyval(&event->key), 0); // with any mask
750                         if (MOD__ALT) { // alt
751                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
752                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
753                         }
754                         else { // no alt
755                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
756                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
757                         }
758                         ret = TRUE;
759                     }
760                     break;
761                 case GDK_Right: // move selection right
762                 case GDK_KP_Right:
763                 case GDK_KP_6:
764                     if (!MOD__CTRL) { // not ctrl
765                         gint mul = 1 + gobble_key_events(
766                             get_group0_keyval(&event->key), 0); // with any mask
767                         if (MOD__ALT) { // alt
768                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
769                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
770                         }
771                         else { // no alt
772                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
773                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
774                         }
775                         ret = TRUE;
776                     }
777                     break;
778                 case GDK_Down: // move selection down
779                 case GDK_KP_Down:
780                 case GDK_KP_2:
781                     if (!MOD__CTRL) { // not ctrl
782                         gint mul = 1 + gobble_key_events(
783                             get_group0_keyval(&event->key), 0); // with any mask
784                         if (MOD__ALT) { // alt
785                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
786                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
787                         }
788                         else { // no alt
789                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
790                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
791                         }
792                         ret = TRUE;
793                     }
794                     break;
795                 case GDK_Escape:
796                     if (!sp_select_context_abort(event_context))
797                         selection->clear();
798                     ret = TRUE;
799                     break;
800                 case GDK_a:
801                 case GDK_A:
802                     if (MOD__CTRL_ONLY) {
803                         sp_edit_select_all(desktop);
804                         ret = TRUE;
805                     }
806                     break;
807                 case GDK_space:
808                     /* stamping mode: show outline mode moving */
809                     /* FIXME: Is next condition ok? (lauris) */
810                     if (sc->dragging && sc->grabbed) {
811                         seltrans->stamp();
812                         ret = TRUE;
813                     }
814                     break;
815                 case GDK_x:
816                 case GDK_X:
817                     if (MOD__ALT_ONLY) {
818                         desktop->setToolboxFocusTo ("altx");
819                         ret = TRUE;
820                     }
821                     break;
822                 case GDK_bracketleft:
823                     if (MOD__ALT) {
824                         gint mul = 1 + gobble_key_events(
825                             get_group0_keyval(&event->key), 0); // with any mask
826                         sp_selection_rotate_screen(selection, mul*1);
827                     } else if (MOD__CTRL) {
828                         sp_selection_rotate(selection, 90);
829                     } else if (snaps) {
830                         sp_selection_rotate(selection, 180/snaps);
831                     }
832                     ret = TRUE;
833                     break;
834                 case GDK_bracketright:
835                     if (MOD__ALT) {
836                         gint mul = 1 + gobble_key_events(
837                             get_group0_keyval(&event->key), 0); // with any mask
838                         sp_selection_rotate_screen(selection, -1*mul);
839                     } else if (MOD__CTRL) {
840                         sp_selection_rotate(selection, -90);
841                     } else if (snaps) {
842                         sp_selection_rotate(selection, -180/snaps);
843                     }
844                     ret = TRUE;
845                     break;
846                 case GDK_less:
847                 case GDK_comma:
848                     if (MOD__ALT) {
849                         gint mul = 1 + gobble_key_events(
850                             get_group0_keyval(&event->key), 0); // with any mask
851                         sp_selection_scale_screen(selection, -2*mul);
852                     } else if (MOD__CTRL) {
853                         sp_selection_scale_times(selection, 0.5);
854                     } else {
855                         gint mul = 1 + gobble_key_events(
856                             get_group0_keyval(&event->key), 0); // with any mask
857                         sp_selection_scale(selection, -offset*mul);
858                     }
859                     ret = TRUE;
860                     break;
861                 case GDK_greater:
862                 case GDK_period:
863                     if (MOD__ALT) {
864                         gint mul = 1 + gobble_key_events(
865                             get_group0_keyval(&event->key), 0); // with any mask
866                         sp_selection_scale_screen(selection, 2*mul);
867                     } else if (MOD__CTRL) {
868                         sp_selection_scale_times(selection, 2);
869                     } else {
870                         gint mul = 1 + gobble_key_events(
871                             get_group0_keyval(&event->key), 0); // with any mask
872                         sp_selection_scale(selection, offset*mul);
873                     }
874                     ret = TRUE;
875                     break;
876                 case GDK_Return:
877                     if (MOD__CTRL_ONLY) {
878                         if (selection->singleItem()) {
879                             SPItem *clicked_item = selection->singleItem();
880                             if ( SP_IS_GROUP(clicked_item) ||
881                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
882                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
883                                 sp_desktop_selection(desktop)->clear();
884                             } else {
885                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
886                             }
887                         }
888                         ret = TRUE;
889                     }
890                     break;
891                 case GDK_BackSpace:
892                     if (MOD__CTRL_ONLY) {
893                         sp_select_context_up_one_layer(desktop);
894                         ret = TRUE;
895                     }
896                     break;
897                 case GDK_s:
898                 case GDK_S:
899                     if (MOD__SHIFT_ONLY) {
900                         if (!selection->isEmpty()) {
901                             seltrans->increaseState();
902                         }
903                         ret = TRUE;
904                     }
905                     break;
906                 case GDK_g:
907                 case GDK_G:
908                     if (MOD__SHIFT_ONLY) {
909                         sp_selection_to_guides(desktop);
910                         ret = true;
911                     }
912                     break;
913                 default:
914                     break;
915             }
916             break;
918         case GDK_KEY_RELEASE:
919             {
920             guint keyval = get_group0_keyval(&event->key);
921             if (key_is_a_modifier (keyval))
922                 event_context->defaultMessageContext()->clear();
924             bool alt = ( MOD__ALT
925                          || (keyval == GDK_Alt_L)
926                          || (keyval == GDK_Alt_R)
927                          || (keyval == GDK_Meta_L)
928                          || (keyval == GDK_Meta_R));
930             if (Inkscape::Rubberband::get(desktop)->is_started()) {
931                 // if Alt then change cursor to moving cursor:
932                 if (alt) {
933                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
934                 }
935             }
936             }
937             // set cursor to default.
938             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
939             break;
940         default:
941             break;
942     }
944     if (!ret) {
945         if (((SPEventContextClass *) parent_class)->root_handler)
946             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
947     }
949     return ret;
953 /*
954   Local Variables:
955   mode:c++
956   c-file-style:"stroustrup"
957   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
958   indent-tabs-mode:nil
959   fill-column:99
960   End:
961 */
962 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :