Code

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