Code

When snapping while translating, use the bounding box corners of each selected item...
[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             sp_event_context_discard_delayed_snap_event(event_context);
216             drag_escaped = 1;
218             if (sc->item) {
219                 // only undo if the item is still valid
220                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
221                     sp_document_undo(sp_desktop_document(desktop));
222                 }
224                 sp_object_unref( SP_OBJECT(sc->item), NULL);
225             } else if (sc->button_press_ctrl) {
226                 // NOTE:  This is a workaround to a bug.
227                 // When the ctrl key is held, sc->item is not defined
228                 // so in this case (only), we skip the object doc check
229                 sp_document_undo(sp_desktop_document(desktop));
230             }
231             sc->item = NULL;
233             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Move canceled."));
234             return true;
235         }
236     } else {
237         if (Inkscape::Rubberband::get(desktop)->is_started()) {
238             Inkscape::Rubberband::get(desktop)->stop();
239             rb_escaped = 1;
240             SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
241             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
242             return true;
243         }
244     }
245     return false;
248 bool
249 key_is_a_modifier (guint key) {
250     return (key == GDK_Alt_L ||
251                 key == GDK_Alt_R ||
252                 key == GDK_Control_L ||
253                 key == GDK_Control_R ||
254                 key == GDK_Shift_L ||
255                 key == GDK_Shift_R ||
256                 key == GDK_Meta_L ||  // Meta is when you press Shift+Alt (at least on my machine)
257             key == GDK_Meta_R);
260 void
261 sp_select_context_up_one_layer(SPDesktop *desktop)
263     /* Click in empty place, go up one level -- but don't leave a layer to root.
264      *
265      * (Rationale: we don't usually allow users to go to the root, since that
266      * detracts from the layer metaphor: objects at the root level can in front
267      * of or behind layers.  Whereas it's fine to go to the root if editing
268      * a document that has no layers (e.g. a non-Inkscape document).)
269      *
270      * Once we support editing SVG "islands" (e.g. <svg> embedded in an xhtml
271      * document), we might consider further restricting the below to disallow
272      * leaving a layer to go to a non-layer.
273      */
274     SPObject *const current_layer = desktop->currentLayer();
275     if (current_layer) {
276         SPObject *const parent = SP_OBJECT_PARENT(current_layer);
277         if ( parent
278              && ( SP_OBJECT_PARENT(parent)
279                   || !( SP_IS_GROUP(current_layer)
280                         && ( SPGroup::LAYER
281                              == SP_GROUP(current_layer)->layerMode() ) ) ) )
282         {
283             desktop->setCurrentLayer(parent);
284             if (SP_IS_GROUP(current_layer) && SPGroup::LAYER != SP_GROUP(current_layer)->layerMode())
285                 sp_desktop_selection(desktop)->set(current_layer);
286         }
287     }
290 static gint
291 sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
293     gint ret = FALSE;
295     SPDesktop *desktop = event_context->desktop;
296     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
297     Inkscape::SelTrans *seltrans = sc->_seltrans;
299     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
302     // make sure we still have valid objects to move around
303     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
304         sp_select_context_abort(event_context);
305     }
307     switch (event->type) {
308         case GDK_BUTTON_PRESS:
309             if (event->button.button == 1 && !event_context->space_panning) {
310                 /* Left mousebutton */
312                 // save drag origin
313                 xp = (gint) event->button.x;
314                 yp = (gint) event->button.y;
315                 within_tolerance = true;
317                 // remember what modifiers were on before button press
318                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
319                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
320                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
322                 if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
323                     // if shift or ctrl was pressed, do not move objects;
324                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
325                 } else {
326                     sc->dragging = TRUE;
327                     sc->moved = FALSE;
329                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
331                     // remember the clicked item in sc->item:
332                     if (sc->item) {
333                         sp_object_unref(sc->item, NULL);
334                         sc->item = NULL;
335                     }
336                     sc->item = sp_event_context_find_item (desktop,
337                                               Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
338                     sp_object_ref(sc->item, NULL);
340                     rb_escaped = drag_escaped = 0;
342                     if (sc->grabbed) {
343                         sp_canvas_item_ungrab(sc->grabbed, event->button.time);
344                         sc->grabbed = NULL;
345                     }
346                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
347                                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
348                                         GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
349                                         NULL, event->button.time);
350                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
352                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
354                     ret = TRUE;
355                 }
356             } else if (event->button.button == 3) {
357                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
358                 sp_select_context_abort(event_context);
359             }
360             break;
362         case GDK_ENTER_NOTIFY:
363         {
364             if (!desktop->isWaitingCursor()) {
365                 GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
366                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
367                 gdk_cursor_destroy(cursor);
368             }
369             break;
370         }
372         case GDK_LEAVE_NOTIFY:
373             if (!desktop->isWaitingCursor())
374                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
375             break;
377         case GDK_KEY_PRESS:
378             if (get_group0_keyval (&event->key) == GDK_space) {
379                 if (sc->dragging && sc->grabbed) {
380                     /* stamping mode: show content mode moving */
381                     seltrans->stamp();
382                     ret = TRUE;
383                 }
384             }
385             break;
387         default:
388             break;
389     }
391     if (!ret) {
392         if (((SPEventContextClass *) parent_class)->item_handler)
393             ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
394     }
396     return ret;
399 static gint
400 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
402     SPItem *item = NULL;
403     SPItem *item_at_point = NULL, *group_at_point = NULL, *item_in_group = NULL;
404     gint ret = FALSE;
406     SPDesktop *desktop = event_context->desktop;
407     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
408     Inkscape::SelTrans *seltrans = sc->_seltrans;
409     Inkscape::Selection *selection = sp_desktop_selection(desktop);
410     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
412     // make sure we still have valid objects to move around
413     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
414         sp_select_context_abort(event_context);
415     }
417     switch (event->type) {
418         case GDK_2BUTTON_PRESS:
419             if (event->button.button == 1) {
420                 if (!selection->isEmpty()) {
421                     SPItem *clicked_item = (SPItem *) selection->itemList()->data;
422                     if (SP_IS_GROUP(clicked_item) && !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
423                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
424                         sp_desktop_selection(desktop)->clear();
425                         sc->dragging = false;
426                         sp_event_context_discard_delayed_snap_event(event_context);
428                         sp_canvas_end_forced_full_redraws(desktop->canvas);
429                     } else { // switch tool
430                         Geom::Point const button_pt(event->button.x, event->button.y);
431                         Geom::Point const p(desktop->w2d(button_pt));
432                         tools_switch_by_item (desktop, clicked_item, p);
433                     }
434                 } else {
435                     sp_select_context_up_one_layer(desktop);
436                 }
437                 ret = TRUE;
438             }
439             break;
440         case GDK_BUTTON_PRESS:
441             if (event->button.button == 1 && !event_context->space_panning) {
443                 // save drag origin
444                 xp = (gint) event->button.x;
445                 yp = (gint) event->button.y;
446                 within_tolerance = true;
448                 Geom::Point const button_pt(event->button.x, event->button.y);
449                 Geom::Point const p(desktop->w2d(button_pt));
450                 if (event->button.state & GDK_MOD1_MASK)
451                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
452                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
453                 if (sc->grabbed) {
454                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
455                     sc->grabbed = NULL;
456                 }
457                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
458                                     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,
459                                     NULL, event->button.time);
460                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
462                 // remember what modifiers were on before button press
463                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
464                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
465                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
467                 sc->moved = FALSE;
469                 rb_escaped = drag_escaped = 0;
471                 ret = TRUE;
472             } else if (event->button.button == 3) {
473                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
474                 sp_select_context_abort(event_context);
475             }
476             break;
478         case GDK_MOTION_NOTIFY:
479                 tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
480                 if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
481                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
482                 Geom::Point const p(desktop->w2d(motion_pt));
484                 if ( within_tolerance
485                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
486                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
487                     break; // do not drag if we're within tolerance from origin
488                 }
489                 // Once the user has moved farther than tolerance from the original location
490                 // (indicating they intend to move the object, not click), then always process the
491                 // motion notify coordinates as given (no snapping back to origin)
492                 within_tolerance = false;
494                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
495                     // if it's not click and ctrl or alt was pressed (the latter with some selection
496                     // but not with shift) we want to drag rather than rubberband
497                         sc->dragging = TRUE;
499                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
500                 }
502                 if (sc->dragging) {
503                     /* User has dragged fast, so we get events on root (lauris)*/
504                     // not only that; we will end up here when ctrl-dragging as well
505                     // and also when we started within tolerance, but trespassed tolerance outside of item
506                     Inkscape::Rubberband::get(desktop)->stop();
507                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
508                     item_at_point = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), FALSE);
509                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
510                         item_at_point = desktop->item_at_point(Geom::Point(xp, yp), FALSE);
511                     if (item_at_point || sc->moved || sc->button_press_alt) {
512                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
513                         if (!sc->moved) {
514                             item_in_group = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), TRUE);
515                             group_at_point = desktop->group_at_point(Geom::Point(event->button.x, event->button.y));
516                             if (SP_IS_LAYER(selection->single()))
517                                 group_at_point = SP_GROUP(selection->single());
519                             // group-at-point is meant to be topmost item if it's a group,
520                             // not topmost group of all items at point
521                             if (group_at_point != item_in_group &&
522                                 !(group_at_point && item_at_point &&
523                                   group_at_point->isAncestorOf(item_at_point)))
524                                 group_at_point = NULL;
526                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
527                             if ((!item_in_group || !selection->includes(item_in_group)) &&
528                                 (!group_at_point || !selection->includes(group_at_point))
529                                 && !sc->button_press_alt) {
530                                 // select what is under cursor
531                                 if (!seltrans->isEmpty()) {
532                                     seltrans->resetState();
533                                 }
534                                 // when simply ctrl-dragging, we don't want to go into groups
535                                 if (item_at_point && !selection->includes(item_at_point))
536                                     selection->set(item_at_point);
537                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
538                             seltrans->grab(p, -1, -1, FALSE, TRUE);
539                             sc->moved = TRUE;
540                         }
541                         if (!seltrans->isEmpty())
542                             seltrans->moveTo(p, event->button.state);
543                         desktop->scroll_to_point(p);
544                         gobble_motion_events(GDK_BUTTON1_MASK);
545                         ret = TRUE;
546                     } else {
547                         sc->dragging = FALSE;
548                         sp_event_context_discard_delayed_snap_event(event_context);
549                         sp_canvas_end_forced_full_redraws(desktop->canvas);
550                     }
551                 } else {
552                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
553                         Inkscape::Rubberband::get(desktop)->move(p);
554                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
555                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
556                         } else {
557                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
558                         }
559                         gobble_motion_events(GDK_BUTTON1_MASK);
560                     }
561                 }
562             }
563             break;
564         case GDK_BUTTON_RELEASE:
565             xp = yp = 0;
566             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
567                 if (sc->dragging) {
568                     if (sc->moved) {
569                         // item has been moved
570                         seltrans->ungrab();
571                         sc->moved = FALSE;
572                     } else if (sc->item && !drag_escaped) {
573                         // item has not been moved -> simply a click, do selecting
574                         if (!selection->isEmpty()) {
575                             if (event->button.state & GDK_SHIFT_MASK) {
576                                 // with shift, toggle selection
577                                 seltrans->resetState();
578                                 selection->toggle(sc->item);
579                             } else {
580                                 SPObject* single = selection->single();
581                                 // without shift, increase state (i.e. toggle scale/rotation handles)
582                                 if (selection->includes(sc->item)) {
583                                     seltrans->increaseState();
584                                 } else if (SP_IS_LAYER(single) && single->isAncestorOf(sc->item)) {
585                                     seltrans->increaseState();
586                                 } else {
587                                     seltrans->resetState();
588                                     selection->set(sc->item);
589                                 }
590                             }
591                         } else { // simple or shift click, no previous selection
592                             seltrans->resetState();
593                             selection->set(sc->item);
594                         }
595                     }
596                     sc->dragging = FALSE;
597                     sp_event_context_discard_delayed_snap_event(event_context);
598                     sp_canvas_end_forced_full_redraws(desktop->canvas);
600                     if (sc->item) {
601                         sp_object_unref( SP_OBJECT(sc->item), NULL);
602                     }
603                     sc->item = NULL;
604                 } else {
605                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
606                     if (r->is_started() && !within_tolerance) {
607                         // this was a rubberband drag
608                         GSList *items = NULL;
609                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
610                             Geom::OptRect const b = r->getRectangle();
611                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
612                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
613                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
614                         }
616                         seltrans->resetState();
617                         r->stop();
618                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
620                         if (event->button.state & GDK_SHIFT_MASK) {
621                             // with shift, add to selection
622                             selection->addList (items);
623                         } else {
624                             // without shift, simply select anew
625                             selection->setList (items);
626                         }
627                         g_slist_free (items);
628                     } else { // it was just a click, or a too small rubberband
629                         r->stop();
630                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
631                             // this was a shift+click or alt+shift+click, select what was clicked upon
633                             sc->button_press_shift = false;
635                             if (sc->button_press_ctrl) {
636                                 // go into groups, honoring Alt
637                                 item = sp_event_context_find_item (desktop,
638                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
639                                 sc->button_press_ctrl = FALSE;
640                             } else {
641                                 // don't go into groups, honoring Alt
642                                 item = sp_event_context_find_item (desktop,
643                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
644                             }
646                             if (item) {
647                                 selection->toggle(item);
648                                 item = NULL;
649                             }
651                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
653                             item = sp_event_context_find_item (desktop,
654                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
656                             sc->button_press_ctrl = FALSE;
657                             sc->button_press_alt = FALSE;
659                             if (item) {
660                                 if (selection->includes(item)) {
661                                     seltrans->increaseState();
662                                 } else {
663                                     seltrans->resetState();
664                                     selection->set(item);
665                                 }
666                                 item = NULL;
667                             }
669                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
670                             if (!selection->isEmpty()) {
671                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
672                                     selection->clear();
673                                 rb_escaped = 0;
674                                 ret = TRUE;
675                             }
676                         }
677                     }
678                     ret = TRUE;
679                 }
680                 if (sc->grabbed) {
681                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
682                     sc->grabbed = NULL;
683                 }
685                 desktop->updateNow();
686             }
687             if (event->button.button == 1) {
688                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
689             }
690             sc->button_press_shift = false;
691             sc->button_press_ctrl = false;
692             sc->button_press_alt = false;
693             break;
695         case GDK_KEY_PRESS: // keybindings for select context
697                         {
698                         {
699                 guint keyval = get_group0_keyval(&event->key);
700             bool alt = ( MOD__ALT
701                                     || (keyval == GDK_Alt_L)
702                                     || (keyval == GDK_Alt_R)
703                                     || (keyval == GDK_Meta_L)
704                                     || (keyval == GDK_Meta_R));
706             if (!key_is_a_modifier (keyval)) {
707                     event_context->defaultMessageContext()->clear();
708             } else if (sc->grabbed || seltrans->isGrabbed()) {
709                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
710                     // if Alt then change cursor to moving cursor:
711                     if (alt) {
712                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
713                     }
714                 } else {
715                 // do not change the statusbar text when mousekey is down to move or transform the object,
716                 // because the statusbar text is already updated somewhere else.
717                    break;
718                 }
719             } else {
720                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
721                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
722                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
723                                                 _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
724                     // if Alt and nonempty selection, show moving cursor ("move selected"):
725                     if (alt && !selection->isEmpty() && !desktop->isWaitingCursor()) {
726                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
727                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
728                         gdk_cursor_destroy(cursor);
729                     }
730                     //*/
731                     break;
732             }
733                         }
735             gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
736                         gdouble const offset = prefs->getDoubleLimited("/options/defaultscale/value", 2, 0, 1000);
737                         int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
739                         switch (get_group0_keyval (&event->key)) {
740                 case GDK_Left: // move selection left
741                 case GDK_KP_Left:
742                 case GDK_KP_4:
743                     if (!MOD__CTRL) { // not ctrl
744                         gint mul = 1 + gobble_key_events(
745                             get_group0_keyval(&event->key), 0); // with any mask
746                         if (MOD__ALT) { // alt
747                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
748                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
749                         }
750                         else { // no alt
751                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
752                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
753                         }
754                         ret = TRUE;
755                     }
756                     break;
757                 case GDK_Up: // move selection up
758                 case GDK_KP_Up:
759                 case GDK_KP_8:
760                     if (!MOD__CTRL) { // not ctrl
761                         gint mul = 1 + gobble_key_events(
762                             get_group0_keyval(&event->key), 0); // with any mask
763                         if (MOD__ALT) { // alt
764                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
765                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
766                         }
767                         else { // no alt
768                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
769                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
770                         }
771                         ret = TRUE;
772                     }
773                     break;
774                 case GDK_Right: // move selection right
775                 case GDK_KP_Right:
776                 case GDK_KP_6:
777                     if (!MOD__CTRL) { // not ctrl
778                         gint mul = 1 + gobble_key_events(
779                             get_group0_keyval(&event->key), 0); // with any mask
780                         if (MOD__ALT) { // alt
781                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
782                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
783                         }
784                         else { // no alt
785                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
786                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
787                         }
788                         ret = TRUE;
789                     }
790                     break;
791                 case GDK_Down: // move selection down
792                 case GDK_KP_Down:
793                 case GDK_KP_2:
794                     if (!MOD__CTRL) { // not ctrl
795                         gint mul = 1 + gobble_key_events(
796                             get_group0_keyval(&event->key), 0); // with any mask
797                         if (MOD__ALT) { // alt
798                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
799                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
800                         }
801                         else { // no alt
802                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
803                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
804                         }
805                         ret = TRUE;
806                     }
807                     break;
808                 case GDK_Escape:
809                     if (!sp_select_context_abort(event_context))
810                         selection->clear();
811                     ret = TRUE;
812                     break;
813                 case GDK_a:
814                 case GDK_A:
815                     if (MOD__CTRL_ONLY) {
816                         sp_edit_select_all(desktop);
817                         ret = TRUE;
818                     }
819                     break;
820                 case GDK_space:
821                     /* stamping mode: show outline mode moving */
822                     /* FIXME: Is next condition ok? (lauris) */
823                     if (sc->dragging && sc->grabbed) {
824                         seltrans->stamp();
825                         ret = TRUE;
826                     }
827                     break;
828                 case GDK_x:
829                 case GDK_X:
830                     if (MOD__ALT_ONLY) {
831                         desktop->setToolboxFocusTo ("altx");
832                         ret = TRUE;
833                     }
834                     break;
835                 case GDK_bracketleft:
836                     if (MOD__ALT) {
837                         gint mul = 1 + gobble_key_events(
838                             get_group0_keyval(&event->key), 0); // with any mask
839                         sp_selection_rotate_screen(selection, mul*1);
840                     } else if (MOD__CTRL) {
841                         sp_selection_rotate(selection, 90);
842                     } else if (snaps) {
843                         sp_selection_rotate(selection, 180/snaps);
844                     }
845                     ret = TRUE;
846                     break;
847                 case GDK_bracketright:
848                     if (MOD__ALT) {
849                         gint mul = 1 + gobble_key_events(
850                             get_group0_keyval(&event->key), 0); // with any mask
851                         sp_selection_rotate_screen(selection, -1*mul);
852                     } else if (MOD__CTRL) {
853                         sp_selection_rotate(selection, -90);
854                     } else if (snaps) {
855                         sp_selection_rotate(selection, -180/snaps);
856                     }
857                     ret = TRUE;
858                     break;
859                 case GDK_less:
860                 case GDK_comma:
861                     if (MOD__ALT) {
862                         gint mul = 1 + gobble_key_events(
863                             get_group0_keyval(&event->key), 0); // with any mask
864                         sp_selection_scale_screen(selection, -2*mul);
865                     } else if (MOD__CTRL) {
866                         sp_selection_scale_times(selection, 0.5);
867                     } else {
868                         gint mul = 1 + gobble_key_events(
869                             get_group0_keyval(&event->key), 0); // with any mask
870                         sp_selection_scale(selection, -offset*mul);
871                     }
872                     ret = TRUE;
873                     break;
874                 case GDK_greater:
875                 case GDK_period:
876                     if (MOD__ALT) {
877                         gint mul = 1 + gobble_key_events(
878                             get_group0_keyval(&event->key), 0); // with any mask
879                         sp_selection_scale_screen(selection, 2*mul);
880                     } else if (MOD__CTRL) {
881                         sp_selection_scale_times(selection, 2);
882                     } else {
883                         gint mul = 1 + gobble_key_events(
884                             get_group0_keyval(&event->key), 0); // with any mask
885                         sp_selection_scale(selection, offset*mul);
886                     }
887                     ret = TRUE;
888                     break;
889                 case GDK_Return:
890                     if (MOD__CTRL_ONLY) {
891                         if (selection->singleItem()) {
892                             SPItem *clicked_item = selection->singleItem();
893                             if ( SP_IS_GROUP(clicked_item) ||
894                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
895                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
896                                 sp_desktop_selection(desktop)->clear();
897                             } else {
898                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
899                             }
900                         }
901                         ret = TRUE;
902                     }
903                     break;
904                 case GDK_BackSpace:
905                     if (MOD__CTRL_ONLY) {
906                         sp_select_context_up_one_layer(desktop);
907                         ret = TRUE;
908                     }
909                     break;
910                 case GDK_s:
911                 case GDK_S:
912                     if (MOD__SHIFT_ONLY) {
913                         if (!selection->isEmpty()) {
914                             seltrans->increaseState();
915                         }
916                         ret = TRUE;
917                     }
918                     break;
919                 case GDK_g:
920                 case GDK_G:
921                     if (MOD__SHIFT_ONLY) {
922                         sp_selection_to_guides(desktop);
923                         ret = true;
924                     }
925                     break;
926                 default:
927                     break;
928             }
929             break;
930                         }
931         case GDK_KEY_RELEASE:
932             {
933             guint keyval = get_group0_keyval(&event->key);
934             if (key_is_a_modifier (keyval))
935                 event_context->defaultMessageContext()->clear();
937             bool alt = ( MOD__ALT
938                          || (keyval == GDK_Alt_L)
939                          || (keyval == GDK_Alt_R)
940                          || (keyval == GDK_Meta_L)
941                          || (keyval == GDK_Meta_R));
943             if (Inkscape::Rubberband::get(desktop)->is_started()) {
944                 // if Alt then change cursor to moving cursor:
945                 if (alt) {
946                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
947                 }
948             }
949             }
950             // set cursor to default.
951             if (!desktop->isWaitingCursor()) {
952                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
953             }
954             break;
955         default:
956             break;
957     }
959     if (!ret) {
960         if (((SPEventContextClass *) parent_class)->root_handler)
961             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
962     }
964     return ret;
968 /*
969   Local Variables:
970   mode:c++
971   c-file-style:"stroustrup"
972   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
973   indent-tabs-mode:nil
974   fill-column:99
975   End:
976 */
977 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :