Code

Enable item-selection by cycling through them (using Alt+mouse wheel scroll).
[inkscape.git] / src / select-context.cpp
1 /*
2  * Selection and transformation context
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   bulia byak <buliabyak@users.sf.net>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
10  * Copyright (C) 1999-2005 Authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include <cstring>
19 #include <string>
20 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "rubberband.h"
23 #include "document.h"
24 #include "selection.h"
25 #include "seltrans-handles.h"
26 #include "sp-cursor.h"
27 #include "pixmaps/cursor-select-m.xpm"
28 #include "pixmaps/cursor-select-d.xpm"
29 #include "pixmaps/handles.xpm"
30 #include <glibmm/i18n.h>
32 #include "select-context.h"
33 #include "selection-chemistry.h"
34 #include "desktop.h"
35 #include "desktop-handles.h"
36 #include "sp-root.h"
37 #include "preferences.h"
38 #include "tools-switch.h"
39 #include "message-stack.h"
40 #include "selection-describer.h"
41 #include "seltrans.h"
42 #include "box3d.h"
43 #include "display/nr-arena-item.h"
45 using Inkscape::DocumentUndo;
47 static void sp_select_context_class_init(SPSelectContextClass *klass);
48 static void sp_select_context_init(SPSelectContext *select_context);
49 static void sp_select_context_dispose(GObject *object);
51 static void sp_select_context_setup(SPEventContext *ec);
52 static void sp_select_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
53 static gint sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event);
54 static gint sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static SPEventContextClass *parent_class;
58 static GdkCursor *CursorSelectMouseover = NULL;
59 static GdkCursor *CursorSelectDragging = NULL;
60 GdkPixbuf *handles[13];
62 static gint rb_escaped = 0; // if non-zero, rubberband was canceled by esc, so the next button release should not deselect
63 static gint drag_escaped = 0; // if non-zero, drag was canceled by esc
65 static gint xp = 0, yp = 0; // where drag started
66 static gint tolerance = 0;
67 static bool within_tolerance = false;
69 GtkType
70 sp_select_context_get_type(void)
71 {
72     static GType type = 0;
73     if (!type) {
74         GTypeInfo info = {
75             sizeof(SPSelectContextClass),
76             NULL, NULL,
77             (GClassInitFunc) sp_select_context_class_init,
78             NULL, NULL,
79             sizeof(SPSelectContext),
80             4,
81             (GInstanceInitFunc) sp_select_context_init,
82             NULL,   /* value_table */
83         };
84         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSelectContext", &info, (GTypeFlags)0);
85     }
86     return type;
87 }
89 static void
90 sp_select_context_class_init(SPSelectContextClass *klass)
91 {
92     GObjectClass *object_class = (GObjectClass *) klass;
93     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
95     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
97     object_class->dispose = sp_select_context_dispose;
99     event_context_class->setup = sp_select_context_setup;
100     event_context_class->set = sp_select_context_set;
101     event_context_class->root_handler = sp_select_context_root_handler;
102     event_context_class->item_handler = sp_select_context_item_handler;
105 static void
106 sp_select_context_init(SPSelectContext *sc)
108     sc->dragging = FALSE;
109     sc->moved = FALSE;
110     sc->button_press_shift = false;
111     sc->button_press_ctrl = false;
112     sc->button_press_alt = false;
113     sc->is_cycling = false;
114     sc->cycling_items = NULL;
115     sc->cycling_items_selected_before = NULL;
116     sc->cycling_cur_item = NULL;
117     sc->_seltrans = NULL;
118     sc->_describer = NULL;
120     // cursors in select context
121     CursorSelectMouseover = sp_cursor_new_from_xpm(cursor_select_m_xpm , 1, 1);
122     CursorSelectDragging = sp_cursor_new_from_xpm(cursor_select_d_xpm , 1, 1);
123     // selection handles
124     handles[0]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_nw_xpm);
125     handles[1]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_ne_xpm);
126     handles[2]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_h_xpm);
127     handles[3]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_v_xpm);
128     handles[4]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_nw_xpm);
129     handles[5]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_n_xpm);
130     handles[6]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_ne_xpm);
131     handles[7]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_e_xpm);
132     handles[8]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_se_xpm);
133     handles[9]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_s_xpm);
134     handles[10] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_sw_xpm);
135     handles[11] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_rotate_w_xpm);
136     handles[12] = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_center_xpm);
139 static void
140 sp_select_context_dispose(GObject *object)
142     SPSelectContext *sc = SP_SELECT_CONTEXT(object);
143     SPEventContext * ec = SP_EVENT_CONTEXT (object);
145     ec->enableGrDrag(false);
147     if (sc->grabbed) {
148         sp_canvas_item_ungrab(sc->grabbed, GDK_CURRENT_TIME);
149         sc->grabbed = NULL;
150     }
152     delete sc->_seltrans;
153     sc->_seltrans = NULL;
154     delete sc->_describer;
155     sc->_describer = NULL;
157     if (CursorSelectDragging) {
158         gdk_cursor_unref (CursorSelectDragging);
159         CursorSelectDragging = NULL;
160     }
161     if (CursorSelectMouseover) {
162         gdk_cursor_unref (CursorSelectMouseover);
163         CursorSelectMouseover = NULL;
164     }
166     G_OBJECT_CLASS(parent_class)->dispose(object);
169 static void
170 sp_select_context_setup(SPEventContext *ec)
172     SPSelectContext *select_context = SP_SELECT_CONTEXT(ec);
174     if (((SPEventContextClass *) parent_class)->setup) {
175         ((SPEventContextClass *) parent_class)->setup(ec);
176     }
178     SPDesktop *desktop = ec->desktop;
180     select_context->_describer = new Inkscape::SelectionDescriber(
181                 desktop->selection, 
182                 desktop->messageStack(),
183                 _("Click selection to toggle scale/rotation handles"),
184                 _("No objects selected. Click, Shift+click, Alt+scroll mouse on top of objects, or drag around objects to select.")
185         );
187     select_context->_seltrans = new Inkscape::SelTrans(desktop);
189     sp_event_context_read(ec, "show");
190     sp_event_context_read(ec, "transform");
192     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
193     if (prefs->getBool("/tools/select/gradientdrag")) {
194         ec->enableGrDrag();
195     }
198 static void
199 sp_select_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
201     SPSelectContext *sc = SP_SELECT_CONTEXT(ec);
202     Glib::ustring path = val->getEntryName();
204     if (path == "show") {
205         if (val->getString() == "outline") {
206             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_OUTLINE);
207         } else {
208             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_CONTENT);
209         }
210     }
213 static bool
214 sp_select_context_abort(SPEventContext *event_context)
216     SPDesktop *desktop = event_context->desktop;
217     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
218     Inkscape::SelTrans *seltrans = sc->_seltrans;
220     if (sc->dragging) {
221         if (sc->moved) { // cancel dragging an object
222             seltrans->ungrab();
223             sc->moved = FALSE;
224             sc->dragging = FALSE;
225             sp_event_context_discard_delayed_snap_event(event_context);
226             drag_escaped = 1;
228             if (sc->item) {
229                 // only undo if the item is still valid
230                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
231                     DocumentUndo::undo(sp_desktop_document(desktop));
232                 }
234                 sp_object_unref( SP_OBJECT(sc->item), NULL);
235             } else if (sc->button_press_ctrl) {
236                 // NOTE:  This is a workaround to a bug.
237                 // When the ctrl key is held, sc->item is not defined
238                 // so in this case (only), we skip the object doc check
239                 DocumentUndo::undo(sp_desktop_document(desktop));
240             }
241             sc->item = NULL;
243             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Move canceled."));
244             return true;
245         }
246     } else {
247         if (Inkscape::Rubberband::get(desktop)->is_started()) {
248             Inkscape::Rubberband::get(desktop)->stop();
249             rb_escaped = 1;
250             SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
251             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
252             return true;
253         }
254     }
255     return false;
258 bool
259 key_is_a_modifier (guint key) {
260     return (key == GDK_Alt_L ||
261                 key == GDK_Alt_R ||
262                 key == GDK_Control_L ||
263                 key == GDK_Control_R ||
264                 key == GDK_Shift_L ||
265                 key == GDK_Shift_R ||
266                 key == GDK_Meta_L ||  // Meta is when you press Shift+Alt (at least on my machine)
267             key == GDK_Meta_R);
270 void
271 sp_select_context_up_one_layer(SPDesktop *desktop)
273     /* Click in empty place, go up one level -- but don't leave a layer to root.
274      *
275      * (Rationale: we don't usually allow users to go to the root, since that
276      * detracts from the layer metaphor: objects at the root level can in front
277      * of or behind layers.  Whereas it's fine to go to the root if editing
278      * a document that has no layers (e.g. a non-Inkscape document).)
279      *
280      * Once we support editing SVG "islands" (e.g. <svg> embedded in an xhtml
281      * document), we might consider further restricting the below to disallow
282      * leaving a layer to go to a non-layer.
283      */
284     SPObject *const current_layer = desktop->currentLayer();
285     if (current_layer) {
286         SPObject *const parent = SP_OBJECT_PARENT(current_layer);
287         if ( parent
288              && ( SP_OBJECT_PARENT(parent)
289                   || !( SP_IS_GROUP(current_layer)
290                         && ( SPGroup::LAYER
291                              == SP_GROUP(current_layer)->layerMode() ) ) ) )
292         {
293             desktop->setCurrentLayer(parent);
294             if (SP_IS_GROUP(current_layer) && SPGroup::LAYER != SP_GROUP(current_layer)->layerMode())
295                 sp_desktop_selection(desktop)->set(current_layer);
296         }
297     }
300 static gint
301 sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
303     gint ret = FALSE;
305     SPDesktop *desktop = event_context->desktop;
306     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
307     Inkscape::SelTrans *seltrans = sc->_seltrans;
309     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
310     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
312     // make sure we still have valid objects to move around
313     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
314         sp_select_context_abort(event_context);
315     }
317     switch (event->type) {
318         case GDK_BUTTON_PRESS:
319             if (event->button.button == 1 && !event_context->space_panning) {
320                 /* Left mousebutton */
322                 // save drag origin
323                 xp = (gint) event->button.x;
324                 yp = (gint) event->button.y;
325                 within_tolerance = true;
327                 // remember what modifiers were on before button press
328                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
329                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
330                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
332                 if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
333                     // if shift or ctrl was pressed, do not move objects;
334                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
335                 } else {
336                     sc->dragging = TRUE;
337                     sc->moved = FALSE;
339                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
341                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
343                     // remember the clicked item in sc->item:
344                     if (sc->item) {
345                         sp_object_unref(sc->item, NULL);
346                         sc->item = NULL;
347                     }
348                     sc->item = sp_event_context_find_item (desktop,
349                                               Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
350                     sp_object_ref(sc->item, NULL);
352                     rb_escaped = drag_escaped = 0;
354                     if (sc->grabbed) {
355                         sp_canvas_item_ungrab(sc->grabbed, event->button.time);
356                         sc->grabbed = NULL;
357                     }
358                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
359                                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
360                                         GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
361                                         NULL, event->button.time);
362                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
364                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
366                     ret = TRUE;
367                 }
368             } else if (event->button.button == 3) {
369                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
370                 sp_select_context_abort(event_context);
371             }
372             break;
374         case GDK_ENTER_NOTIFY:
375         {
376             if (!desktop->isWaitingCursor() && !sc->dragging) {
377                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectMouseover);
378             }
379             break;
380         }
382         case GDK_LEAVE_NOTIFY:
383             if (!desktop->isWaitingCursor() && !sc->dragging)
384                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
385             break;
387         case GDK_KEY_PRESS:
388             if (get_group0_keyval (&event->key) == GDK_space) {
389                 if (sc->dragging && sc->grabbed) {
390                     /* stamping mode: show content mode moving */
391                     seltrans->stamp();
392                     ret = TRUE;
393                 }
394             }
395             break;
397         default:
398             break;
399     }
401     if (!ret) {
402         if (((SPEventContextClass *) parent_class)->item_handler)
403             ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
404     }
406     return ret;
409 static void
410 sp_select_context_cycle_through_items(SPSelectContext *sc, Inkscape::Selection *selection, GdkEventScroll *scroll_event, bool shift_pressed) {
411     if (!sc->cycling_cur_item)
412         return;
414     NRArenaItem *arenaitem;
415     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
416     SPItem *item = SP_ITEM(sc->cycling_cur_item->data);
418     // Deactivate current item
419     if (!g_list_find(sc->cycling_items_selected_before, item) && selection->includes(item))
420         selection->remove(item);
421     arenaitem = item->get_arenaitem(desktop->dkey);
422     nr_arena_item_set_opacity (arenaitem, 0.3);
424     // Find next item and activate it
425     GList *next;
426     if (scroll_event->direction == GDK_SCROLL_UP) {
427         next = sc->cycling_cur_item->next;
428         if (next == NULL)
429             next = sc->cycling_items;
430     } else {
431         next = sc->cycling_cur_item->prev;
432         if (next == NULL)
433             next = g_list_last(sc->cycling_items);
434     }
435     sc->cycling_cur_item = next;
436     item = SP_ITEM(sc->cycling_cur_item->data);
437     arenaitem = item->get_arenaitem(desktop->dkey);
438     nr_arena_item_set_opacity (arenaitem, 1.0);
440     if (shift_pressed)
441         selection->add(item);
442     else
443         selection->set(item);
446 static gint
447 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
449     SPItem *item = NULL;
450     SPItem *item_at_point = NULL, *group_at_point = NULL, *item_in_group = NULL;
451     gint ret = FALSE;
453     SPDesktop *desktop = event_context->desktop;
454     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
455     Inkscape::SelTrans *seltrans = sc->_seltrans;
456     Inkscape::Selection *selection = sp_desktop_selection(desktop);
457     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
459     // make sure we still have valid objects to move around
460     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
461         sp_select_context_abort(event_context);
462     }
464     switch (event->type) {
465         case GDK_2BUTTON_PRESS:
466             if (event->button.button == 1) {
467                 if (!selection->isEmpty()) {
468                     SPItem *clicked_item = (SPItem *) selection->itemList()->data;
469                     if (SP_IS_GROUP(clicked_item) && !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
470                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
471                         sp_desktop_selection(desktop)->clear();
472                         sc->dragging = false;
473                         sp_event_context_discard_delayed_snap_event(event_context);
475                         sp_canvas_end_forced_full_redraws(desktop->canvas);
476                     } else { // switch tool
477                         Geom::Point const button_pt(event->button.x, event->button.y);
478                         Geom::Point const p(desktop->w2d(button_pt));
479                         tools_switch_by_item (desktop, clicked_item, p);
480                     }
481                 } else {
482                     sp_select_context_up_one_layer(desktop);
483                 }
484                 ret = TRUE;
485             }
486             break;
487         case GDK_BUTTON_PRESS:
488             if (event->button.button == 1 && !event_context->space_panning) {
490                 // save drag origin
491                 xp = (gint) event->button.x;
492                 yp = (gint) event->button.y;
493                 within_tolerance = true;
495                 Geom::Point const button_pt(event->button.x, event->button.y);
496                 Geom::Point const p(desktop->w2d(button_pt));
497                 if (event->button.state & GDK_MOD1_MASK)
498                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
499                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
500                 if (sc->grabbed) {
501                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
502                     sc->grabbed = NULL;
503                 }
504                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
505                                     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,
506                                     NULL, event->button.time);
507                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
509                 // remember what modifiers were on before button press
510                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
511                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
512                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
514                 sc->moved = FALSE;
516                 rb_escaped = drag_escaped = 0;
518                 ret = TRUE;
519             } else if (event->button.button == 3) {
520                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
521                 sp_select_context_abort(event_context);
522             }
523             break;
525         case GDK_MOTION_NOTIFY:
526         {
527             SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
528             if (sc->is_cycling) {
529                 // Abort cycle-select
530                 NRArenaItem *arenaitem;
531                 for (GList *l = sc->cycling_items; l != NULL; l = g_list_next(l)) {
532                     arenaitem = item->get_arenaitem(desktop->dkey);
533                     nr_arena_item_set_opacity (arenaitem, 1.0);
534                 }
535                 g_list_free(sc->cycling_items);
536                 g_list_free(sc->cycling_items_selected_before);
537                 sc->cycling_items = NULL;
538                 sc->cycling_cur_item = NULL;
539                 sc->is_cycling = false;
540             }
542                 tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
543                 if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
544                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
545                 Geom::Point const p(desktop->w2d(motion_pt));
547                 if ( within_tolerance
548                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
549                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
550                     break; // do not drag if we're within tolerance from origin
551                 }
552                 // Once the user has moved farther than tolerance from the original location
553                 // (indicating they intend to move the object, not click), then always process the
554                 // motion notify coordinates as given (no snapping back to origin)
555                 within_tolerance = false;
557                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
558                     // if it's not click and ctrl or alt was pressed (the latter with some selection
559                     // but not with shift) we want to drag rather than rubberband
560                         sc->dragging = TRUE;
561                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
563                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
564                 }
566                 if (sc->dragging) {
567                     /* User has dragged fast, so we get events on root (lauris)*/
568                     // not only that; we will end up here when ctrl-dragging as well
569                     // and also when we started within tolerance, but trespassed tolerance outside of item
570                     Inkscape::Rubberband::get(desktop)->stop();
571                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
572                     item_at_point = desktop->getItemAtPoint(Geom::Point(event->button.x, event->button.y), FALSE);
573                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
574                         item_at_point = desktop->getItemAtPoint(Geom::Point(xp, yp), FALSE);
575                     if (item_at_point || sc->moved || sc->button_press_alt) {
576                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
577                         if (!sc->moved) {
578                             item_in_group = desktop->getItemAtPoint(Geom::Point(event->button.x, event->button.y), TRUE);
579                             group_at_point = desktop->getGroupAtPoint(Geom::Point(event->button.x, event->button.y));
580                             if (SP_IS_LAYER(selection->single()))
581                                 group_at_point = SP_GROUP(selection->single());
583                             // group-at-point is meant to be topmost item if it's a group,
584                             // not topmost group of all items at point
585                             if (group_at_point != item_in_group &&
586                                 !(group_at_point && item_at_point &&
587                                   group_at_point->isAncestorOf(item_at_point)))
588                                 group_at_point = NULL;
590                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
591                             if ((!item_in_group || !selection->includes(item_in_group)) &&
592                                 (!group_at_point || !selection->includes(group_at_point))
593                                 && !sc->button_press_alt) {
594                                 // select what is under cursor
595                                 if (!seltrans->isEmpty()) {
596                                     seltrans->resetState();
597                                 }
598                                 // when simply ctrl-dragging, we don't want to go into groups
599                                 if (item_at_point && !selection->includes(item_at_point))
600                                     selection->set(item_at_point);
601                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
602                             seltrans->grab(p, -1, -1, FALSE, TRUE);
603                             sc->moved = TRUE;
604                         }
605                         if (!seltrans->isEmpty())
606                             seltrans->moveTo(p, event->button.state);
607                         desktop->scroll_to_point(p);
608                         gobble_motion_events(GDK_BUTTON1_MASK);
609                         ret = TRUE;
610                     } else {
611                         sc->dragging = FALSE;
612                         sp_event_context_discard_delayed_snap_event(event_context);
613                         sp_canvas_end_forced_full_redraws(desktop->canvas);
614                     }
615                 } else {
616                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
617                         Inkscape::Rubberband::get(desktop)->move(p);
618                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
619                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
620                         } else {
621                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
622                         }
623                         gobble_motion_events(GDK_BUTTON1_MASK);
624                     }
625                 }
626             }
627             break;
628         }
629         case GDK_BUTTON_RELEASE:
630             xp = yp = 0;
631             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
632                 if (sc->dragging) {
633                     if (sc->moved) {
634                         // item has been moved
635                         seltrans->ungrab();
636                         sc->moved = FALSE;
637                     } else if (sc->item && !drag_escaped) {
638                         // item has not been moved -> simply a click, do selecting
639                         if (!selection->isEmpty()) {
640                             if (event->button.state & GDK_SHIFT_MASK) {
641                                 // with shift, toggle selection
642                                 seltrans->resetState();
643                                 selection->toggle(sc->item);
644                             } else {
645                                 SPObject* single = selection->single();
646                                 // without shift, increase state (i.e. toggle scale/rotation handles)
647                                 if (selection->includes(sc->item)) {
648                                     seltrans->increaseState();
649                                 } else if (SP_IS_LAYER(single) && single->isAncestorOf(sc->item)) {
650                                     seltrans->increaseState();
651                                 } else {
652                                     seltrans->resetState();
653                                     selection->set(sc->item);
654                                 }
655                             }
656                         } else { // simple or shift click, no previous selection
657                             seltrans->resetState();
658                             selection->set(sc->item);
659                         }
660                     }
661                     sc->dragging = FALSE;
662                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectMouseover);
663                     sp_event_context_discard_delayed_snap_event(event_context);
664                     sp_canvas_end_forced_full_redraws(desktop->canvas);
666                     if (sc->item) {
667                         sp_object_unref( SP_OBJECT(sc->item), NULL);
668                     }
669                     sc->item = NULL;
670                 } else {
671                     Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
672                     if (r->is_started() && !within_tolerance) {
673                         // this was a rubberband drag
674                         GSList *items = NULL;
675                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
676                             Geom::OptRect const b = r->getRectangle();
677                             items = sp_desktop_document(desktop)->getItemsInBox(desktop->dkey, *b);
678                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
679                             items = sp_desktop_document(desktop)->getItemsAtPoints(desktop->dkey, r->getPoints());
680                         }
682                         seltrans->resetState();
683                         r->stop();
684                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
686                         if (event->button.state & GDK_SHIFT_MASK) {
687                             // with shift, add to selection
688                             selection->addList (items);
689                         } else {
690                             // without shift, simply select anew
691                             selection->setList (items);
692                         }
693                         g_slist_free (items);
694                     } else { // it was just a click, or a too small rubberband
695                         r->stop();
696                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
697                             // this was a shift+click or alt+shift+click, select what was clicked upon
699                             sc->button_press_shift = false;
701                             if (sc->button_press_ctrl) {
702                                 // go into groups, honoring Alt
703                                 item = sp_event_context_find_item (desktop,
704                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
705                                 sc->button_press_ctrl = FALSE;
706                             } else {
707                                 // don't go into groups, honoring Alt
708                                 item = sp_event_context_find_item (desktop,
709                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
710                             }
712                             if (item) {
713                                 selection->toggle(item);
714                                 item = NULL;
715                             }
717                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
719                             item = sp_event_context_find_item (desktop,
720                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
722                             sc->button_press_ctrl = FALSE;
723                             sc->button_press_alt = FALSE;
725                             if (item) {
726                                 if (selection->includes(item)) {
727                                     seltrans->increaseState();
728                                 } else {
729                                     seltrans->resetState();
730                                     selection->set(item);
731                                 }
732                                 item = NULL;
733                             }
735                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
736                             if (!selection->isEmpty()) {
737                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
738                                     selection->clear();
739                                 rb_escaped = 0;
740                                 ret = TRUE;
741                             }
742                         }
743                     }
744                     ret = TRUE;
745                 }
746                 if (sc->grabbed) {
747                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
748                     sc->grabbed = NULL;
749                 }
751                 desktop->updateNow();
752             }
753             if (event->button.button == 1) {
754                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
755             }
756             sc->button_press_shift = false;
757             sc->button_press_ctrl = false;
758             sc->button_press_alt = false;
759             break;
761         case GDK_SCROLL:
762         {
763             SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
764             GdkEventScroll *scroll_event = (GdkEventScroll*) event;
766             if (scroll_event->state & GDK_MOD1_MASK) { // alt modified pressed
767                 bool shift_pressed = scroll_event->state & GDK_SHIFT_MASK;
769                 if(sc->is_cycling == false) {
770                     // Build a list of items underneath the mouse pointer (for cycle-selection) and reduce their opacity
771                     Geom::Point p = desktop->d2w(desktop->point());
772                     SPItem *item = desktop->getItemAtPoint(p, true, NULL);
773                     while(item != NULL) {
774                         sc->cycling_items = g_list_append(sc->cycling_items, item);
775                         if (selection->includes(item)) {
776                             // already selected items are stored separately, too
777                             sc->cycling_items_selected_before = g_list_append(sc->cycling_items_selected_before, item);
778                         }
779                         NRArenaItem *arenaitem = item->get_arenaitem(desktop->dkey);
780                         nr_arena_item_set_opacity (arenaitem, 0.3);
782                         item = desktop->getItemAtPoint(p, true, item);
783                     }
784                     // Here we set the current item to the bottommost one so that the cycling step below re-starts at the top
785                     sc->cycling_cur_item = g_list_last(sc->cycling_items);
786                     sc->is_cycling = true;
787                 }
789                 // Cycle through the items underneath the mouse pointer, one-by-one
790                 sp_select_context_cycle_through_items(sc, selection, scroll_event, shift_pressed);
792                 ret = TRUE;
793             }
794             break;
795         }
797         case GDK_KEY_PRESS: // keybindings for select context
799                         {
800                         {
801                 guint keyval = get_group0_keyval(&event->key);
802             bool alt = ( MOD__ALT
803                                     || (keyval == GDK_Alt_L)
804                                     || (keyval == GDK_Alt_R)
805                                     || (keyval == GDK_Meta_L)
806                                     || (keyval == GDK_Meta_R));
808             if (!key_is_a_modifier (keyval)) {
809                     event_context->defaultMessageContext()->clear();
810             } else if (sc->grabbed || seltrans->isGrabbed()) {
811                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
812                     // if Alt then change cursor to moving cursor:
813                     if (alt) {
814                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
815                     }
816                 } else {
817                 // do not change the statusbar text when mousekey is down to move or transform the object,
818                 // because the statusbar text is already updated somewhere else.
819                    break;
820                 }
821             } else {
822                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
823                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
824                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
825                                                 _("<b>Alt</b>: click to select under; scroll mouse-wheel to cycle-select; drag to move selected or select by touch"));
826                     // if Alt and nonempty selection, show moving cursor ("move selected"):
827                     if (alt && !selection->isEmpty() && !desktop->isWaitingCursor()) {
828                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
829                     }
830                     //*/
831                     break;
832             }
833                         }
835             gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
836                         gdouble const offset = prefs->getDoubleLimited("/options/defaultscale/value", 2, 0, 1000);
837                         int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
839                         switch (get_group0_keyval (&event->key)) {
840                 case GDK_Left: // move selection left
841                 case GDK_KP_Left:
842                 case GDK_KP_4:
843                     if (!MOD__CTRL) { // not ctrl
844                         gint mul = 1 + gobble_key_events(
845                             get_group0_keyval(&event->key), 0); // with any mask
846                         if (MOD__ALT) { // alt
847                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
848                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
849                         }
850                         else { // no alt
851                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
852                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
853                         }
854                         ret = TRUE;
855                     }
856                     break;
857                 case GDK_Up: // move selection up
858                 case GDK_KP_Up:
859                 case GDK_KP_8:
860                     if (!MOD__CTRL) { // not ctrl
861                         gint mul = 1 + gobble_key_events(
862                             get_group0_keyval(&event->key), 0); // with any mask
863                         if (MOD__ALT) { // alt
864                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
865                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
866                         }
867                         else { // no alt
868                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
869                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
870                         }
871                         ret = TRUE;
872                     }
873                     break;
874                 case GDK_Right: // move selection right
875                 case GDK_KP_Right:
876                 case GDK_KP_6:
877                     if (!MOD__CTRL) { // not ctrl
878                         gint mul = 1 + gobble_key_events(
879                             get_group0_keyval(&event->key), 0); // with any mask
880                         if (MOD__ALT) { // alt
881                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
882                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
883                         }
884                         else { // no alt
885                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
886                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
887                         }
888                         ret = TRUE;
889                     }
890                     break;
891                 case GDK_Down: // move selection down
892                 case GDK_KP_Down:
893                 case GDK_KP_2:
894                     if (!MOD__CTRL) { // not ctrl
895                         gint mul = 1 + gobble_key_events(
896                             get_group0_keyval(&event->key), 0); // with any mask
897                         if (MOD__ALT) { // alt
898                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
899                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
900                         }
901                         else { // no alt
902                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
903                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
904                         }
905                         ret = TRUE;
906                     }
907                     break;
908                 case GDK_Escape:
909                     if (!sp_select_context_abort(event_context))
910                         selection->clear();
911                     ret = TRUE;
912                     break;
914                 case GDK_a:
915                 case GDK_A:
916                     if (MOD__CTRL_ONLY) {
917                         sp_edit_select_all(desktop);
918                         ret = TRUE;
919                     }
920                     break;
921                 case GDK_space:
922                     /* stamping mode: show outline mode moving */
923                     /* FIXME: Is next condition ok? (lauris) */
924                     if (sc->dragging && sc->grabbed) {
925                         seltrans->stamp();
926                         ret = TRUE;
927                     }
928                     break;
929                 case GDK_x:
930                 case GDK_X:
931                     if (MOD__ALT_ONLY) {
932                         desktop->setToolboxFocusTo ("altx");
933                         ret = TRUE;
934                     }
935                     break;
936                 case GDK_bracketleft:
937                     if (MOD__ALT) {
938                         gint mul = 1 + gobble_key_events(
939                             get_group0_keyval(&event->key), 0); // with any mask
940                         sp_selection_rotate_screen(selection, mul*1);
941                     } else if (MOD__CTRL) {
942                         sp_selection_rotate(selection, 90);
943                     } else if (snaps) {
944                         sp_selection_rotate(selection, 180/snaps);
945                     }
946                     ret = TRUE;
947                     break;
948                 case GDK_bracketright:
949                     if (MOD__ALT) {
950                         gint mul = 1 + gobble_key_events(
951                             get_group0_keyval(&event->key), 0); // with any mask
952                         sp_selection_rotate_screen(selection, -1*mul);
953                     } else if (MOD__CTRL) {
954                         sp_selection_rotate(selection, -90);
955                     } else if (snaps) {
956                         sp_selection_rotate(selection, -180/snaps);
957                     }
958                     ret = TRUE;
959                     break;
960                 case GDK_less:
961                 case GDK_comma:
962                     if (MOD__ALT) {
963                         gint mul = 1 + gobble_key_events(
964                             get_group0_keyval(&event->key), 0); // with any mask
965                         sp_selection_scale_screen(selection, -2*mul);
966                     } else if (MOD__CTRL) {
967                         sp_selection_scale_times(selection, 0.5);
968                     } else {
969                         gint mul = 1 + gobble_key_events(
970                             get_group0_keyval(&event->key), 0); // with any mask
971                         sp_selection_scale(selection, -offset*mul);
972                     }
973                     ret = TRUE;
974                     break;
975                 case GDK_greater:
976                 case GDK_period:
977                     if (MOD__ALT) {
978                         gint mul = 1 + gobble_key_events(
979                             get_group0_keyval(&event->key), 0); // with any mask
980                         sp_selection_scale_screen(selection, 2*mul);
981                     } else if (MOD__CTRL) {
982                         sp_selection_scale_times(selection, 2);
983                     } else {
984                         gint mul = 1 + gobble_key_events(
985                             get_group0_keyval(&event->key), 0); // with any mask
986                         sp_selection_scale(selection, offset*mul);
987                     }
988                     ret = TRUE;
989                     break;
990                 case GDK_Return:
991                     if (MOD__CTRL_ONLY) {
992                         if (selection->singleItem()) {
993                             SPItem *clicked_item = selection->singleItem();
994                             if ( SP_IS_GROUP(clicked_item) ||
995                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
996                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
997                                 sp_desktop_selection(desktop)->clear();
998                             } else {
999                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
1000                             }
1001                         }
1002                         ret = TRUE;
1003                     }
1004                     break;
1005                 case GDK_BackSpace:
1006                     if (MOD__CTRL_ONLY) {
1007                         sp_select_context_up_one_layer(desktop);
1008                         ret = TRUE;
1009                     }
1010                     break;
1011                 case GDK_s:
1012                 case GDK_S:
1013                     if (MOD__SHIFT_ONLY) {
1014                         if (!selection->isEmpty()) {
1015                             seltrans->increaseState();
1016                         }
1017                         ret = TRUE;
1018                     }
1019                     break;
1020                 case GDK_g:
1021                 case GDK_G:
1022                     if (MOD__SHIFT_ONLY) {
1023                         sp_selection_to_guides(desktop);
1024                         ret = true;
1025                     }
1026                     break;
1027                 default:
1028                     break;
1029             }
1030             break;
1031                         }
1032         case GDK_KEY_RELEASE:
1033             {
1034             guint keyval = get_group0_keyval(&event->key);
1035             if (key_is_a_modifier (keyval))
1036                 event_context->defaultMessageContext()->clear();
1038             bool alt = ( MOD__ALT
1039                          || (keyval == GDK_Alt_L)
1040                          || (keyval == GDK_Alt_R)
1041                          || (keyval == GDK_Meta_L)
1042                          || (keyval == GDK_Meta_R));
1044             if (Inkscape::Rubberband::get(desktop)->is_started()) {
1045                 // if Alt then change cursor to moving cursor:
1046                 if (alt) {
1047                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
1048                 }
1049             } else {
1050                 if (alt && sc->is_cycling) {
1051                     // quit cycle-selection and reset opacities
1052                     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
1053                     NRArenaItem *arenaitem;
1054                     for (GList *l = sc->cycling_items; l != NULL; l = g_list_next(l)) {
1055                         arenaitem = SP_ITEM(l->data)->get_arenaitem(desktop->dkey);
1056                         nr_arena_item_set_opacity (arenaitem, 1.0);
1057                     }
1058                     g_list_free(sc->cycling_items);
1059                     g_list_free(sc->cycling_items_selected_before);
1060                     sc->cycling_items = NULL;
1061                     sc->cycling_items_selected_before = NULL;
1062                     sc->cycling_cur_item = NULL;
1063                     sc->is_cycling = false;
1064                 }
1065             }
1067             }
1068             // set cursor to default.
1069             if (!desktop->isWaitingCursor()) {
1070                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
1071             }
1072             break;
1073         default:
1074             break;
1075     }
1077     if (!ret) {
1078         if (((SPEventContextClass *) parent_class)->root_handler)
1079             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1080     }
1082     return ret;
1086 /*
1087   Local Variables:
1088   mode:c++
1089   c-file-style:"stroustrup"
1090   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1091   indent-tabs-mode:nil
1092   fill-column:99
1093   End:
1094 */
1095 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :