Code

Initialize pointer.
[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->cycling_items = NULL;
117     sc->cycling_items_cmp = 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                 tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
531                 if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
532                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
533                 Geom::Point const p(desktop->w2d(motion_pt));
535                 if ( within_tolerance
536                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
537                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
538                     break; // do not drag if we're within tolerance from origin
539                 }
540                 // Once the user has moved farther than tolerance from the original location
541                 // (indicating they intend to move the object, not click), then always process the
542                 // motion notify coordinates as given (no snapping back to origin)
543                 within_tolerance = false;
545                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
546                     // if it's not click and ctrl or alt was pressed (the latter with some selection
547                     // but not with shift) we want to drag rather than rubberband
548                         sc->dragging = TRUE;
549                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
551                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
552                 }
554                 if (sc->dragging) {
555                     /* User has dragged fast, so we get events on root (lauris)*/
556                     // not only that; we will end up here when ctrl-dragging as well
557                     // and also when we started within tolerance, but trespassed tolerance outside of item
558                     Inkscape::Rubberband::get(desktop)->stop();
559                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
560                     item_at_point = desktop->getItemAtPoint(Geom::Point(event->button.x, event->button.y), FALSE);
561                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
562                         item_at_point = desktop->getItemAtPoint(Geom::Point(xp, yp), FALSE);
563                     if (item_at_point || sc->moved || sc->button_press_alt) {
564                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
565                         if (!sc->moved) {
566                             item_in_group = desktop->getItemAtPoint(Geom::Point(event->button.x, event->button.y), TRUE);
567                             group_at_point = desktop->getGroupAtPoint(Geom::Point(event->button.x, event->button.y));
568                             if (SP_IS_LAYER(selection->single()))
569                                 group_at_point = SP_GROUP(selection->single());
571                             // group-at-point is meant to be topmost item if it's a group,
572                             // not topmost group of all items at point
573                             if (group_at_point != item_in_group &&
574                                 !(group_at_point && item_at_point &&
575                                   group_at_point->isAncestorOf(item_at_point)))
576                                 group_at_point = NULL;
578                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
579                             if ((!item_in_group || !selection->includes(item_in_group)) &&
580                                 (!group_at_point || !selection->includes(group_at_point))
581                                 && !sc->button_press_alt) {
582                                 // select what is under cursor
583                                 if (!seltrans->isEmpty()) {
584                                     seltrans->resetState();
585                                 }
586                                 // when simply ctrl-dragging, we don't want to go into groups
587                                 if (item_at_point && !selection->includes(item_at_point))
588                                     selection->set(item_at_point);
589                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
590                             seltrans->grab(p, -1, -1, FALSE, TRUE);
591                             sc->moved = TRUE;
592                         }
593                         if (!seltrans->isEmpty())
594                             seltrans->moveTo(p, event->button.state);
595                         desktop->scroll_to_point(p);
596                         gobble_motion_events(GDK_BUTTON1_MASK);
597                         ret = TRUE;
598                     } else {
599                         sc->dragging = FALSE;
600                         sp_event_context_discard_delayed_snap_event(event_context);
601                         sp_canvas_end_forced_full_redraws(desktop->canvas);
602                     }
603                 } else {
604                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
605                         Inkscape::Rubberband::get(desktop)->move(p);
606                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
607                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
608                         } else {
609                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
610                         }
611                         gobble_motion_events(GDK_BUTTON1_MASK);
612                     }
613                 }
614             }
615             break;
616         }
617         case GDK_BUTTON_RELEASE:
618             xp = yp = 0;
619             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
620                 if (sc->dragging) {
621                     if (sc->moved) {
622                         // item has been moved
623                         seltrans->ungrab();
624                         sc->moved = FALSE;
625                     } else if (sc->item && !drag_escaped) {
626                         // item has not been moved -> simply a click, do selecting
627                         if (!selection->isEmpty()) {
628                             if (event->button.state & GDK_SHIFT_MASK) {
629                                 // with shift, toggle selection
630                                 seltrans->resetState();
631                                 selection->toggle(sc->item);
632                             } else {
633                                 SPObject* single = selection->single();
634                                 // without shift, increase state (i.e. toggle scale/rotation handles)
635                                 if (selection->includes(sc->item)) {
636                                     seltrans->increaseState();
637                                 } else if (SP_IS_LAYER(single) && single->isAncestorOf(sc->item)) {
638                                     seltrans->increaseState();
639                                 } else {
640                                     seltrans->resetState();
641                                     selection->set(sc->item);
642                                 }
643                             }
644                         } else { // simple or shift click, no previous selection
645                             seltrans->resetState();
646                             selection->set(sc->item);
647                         }
648                     }
649                     sc->dragging = FALSE;
650                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectMouseover);
651                     sp_event_context_discard_delayed_snap_event(event_context);
652                     sp_canvas_end_forced_full_redraws(desktop->canvas);
654                     if (sc->item) {
655                         sp_object_unref( SP_OBJECT(sc->item), NULL);
656                     }
657                     sc->item = NULL;
658                 } else {
659                     Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
660                     if (r->is_started() && !within_tolerance) {
661                         // this was a rubberband drag
662                         GSList *items = NULL;
663                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
664                             Geom::OptRect const b = r->getRectangle();
665                             items = sp_desktop_document(desktop)->getItemsInBox(desktop->dkey, *b);
666                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
667                             items = sp_desktop_document(desktop)->getItemsAtPoints(desktop->dkey, r->getPoints());
668                         }
670                         seltrans->resetState();
671                         r->stop();
672                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
674                         if (event->button.state & GDK_SHIFT_MASK) {
675                             // with shift, add to selection
676                             selection->addList (items);
677                         } else {
678                             // without shift, simply select anew
679                             selection->setList (items);
680                         }
681                         g_slist_free (items);
682                     } else { // it was just a click, or a too small rubberband
683                         r->stop();
684                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
685                             // this was a shift+click or alt+shift+click, select what was clicked upon
687                             sc->button_press_shift = false;
689                             if (sc->button_press_ctrl) {
690                                 // go into groups, honoring Alt
691                                 item = sp_event_context_find_item (desktop,
692                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
693                                 sc->button_press_ctrl = FALSE;
694                             } else {
695                                 // don't go into groups, honoring Alt
696                                 item = sp_event_context_find_item (desktop,
697                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
698                             }
700                             if (item) {
701                                 selection->toggle(item);
702                                 item = NULL;
703                             }
705                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
707                             item = sp_event_context_find_item (desktop,
708                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
710                             sc->button_press_ctrl = FALSE;
711                             sc->button_press_alt = FALSE;
713                             if (item) {
714                                 if (selection->includes(item)) {
715                                     seltrans->increaseState();
716                                 } else {
717                                     seltrans->resetState();
718                                     selection->set(item);
719                                 }
720                                 item = NULL;
721                             }
723                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
724                             if (!selection->isEmpty()) {
725                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
726                                     selection->clear();
727                                 rb_escaped = 0;
728                                 ret = TRUE;
729                             }
730                         }
731                     }
732                     ret = TRUE;
733                 }
734                 if (sc->grabbed) {
735                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
736                     sc->grabbed = NULL;
737                 }
739                 desktop->updateNow();
740             }
741             if (event->button.button == 1) {
742                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
743             }
744             sc->button_press_shift = false;
745             sc->button_press_ctrl = false;
746             sc->button_press_alt = false;
747             break;
749         case GDK_SCROLL:
750         {
751             SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
752             GdkEventScroll *scroll_event = (GdkEventScroll*) event;
754             if (scroll_event->state & GDK_MOD1_MASK) { // alt modified pressed
755                 bool shift_pressed = scroll_event->state & GDK_SHIFT_MASK;
757                 /* Rebuild list of items underneath the mouse pointer */
758                 Geom::Point p = desktop->d2w(desktop->point());
759                 SPItem *item = desktop->getItemAtPoint(p, true, NULL);
761                 // Save pointer to current cycle-item so that we can find it again later, in the freshly built list
762                 SPItem *tmp_cur_item = sc->cycling_cur_item ? SP_ITEM(sc->cycling_cur_item->data) : NULL;
763                 g_list_free(sc->cycling_items);
764                 sc->cycling_items = NULL;
765                 sc->cycling_cur_item = NULL;
767                 while(item != NULL) {
768                     sc->cycling_items = g_list_append(sc->cycling_items, item);
769                     item = desktop->getItemAtPoint(p, true, item);
770                 }
772                 /* Compare current item list with item list during previous scroll ... */
773                 GList *l1, *l2;
774                 bool item_lists_differ = false;
775                 // Note that we can do an 'or' comparison in the loop because it is safe to call g_list_next with a NULL pointer.
776                 for (l1 = sc->cycling_items, l2 = sc->cycling_items_cmp; l1 != NULL || l2 != NULL; l1 = g_list_next(l1), l2 = g_list_next(l2)) {
777                     if ((l1 !=NULL && l2 == NULL) || (l1 == NULL && l2 != NULL) || (l1->data != l2->data)) {
778                         item_lists_differ = true;
779                         break;
780                     }
781                 }
783                 /* If list of items under mouse pointer hasn't changed ... */
784                 if (!item_lists_differ) {
785                     // ... find current item in the freshly built list and continue cycling ...
786                     // TODO: This wouldn't be necessary if cycling_cur_item pointed to an element of cycling_items_cmp instead
787                     sc->cycling_cur_item = g_list_find(sc->cycling_items, tmp_cur_item);
788                     g_assert(sc->cycling_cur_item != NULL || sc->cycling_items == NULL);
789                 } else {
790                     // ... otherwise reset opacities for outdated items ...
791                     NRArenaItem *arenaitem;
792                     for(GList *l = sc->cycling_items_cmp; l != NULL; l = l->next) {
793                         arenaitem = SP_ITEM(l->data)->get_arenaitem(desktop->dkey);
794                         nr_arena_item_set_opacity (arenaitem, 1.0);
795                         //if (!shift_pressed && !g_list_find(sc->cycling_items_selected_before, SP_ITEM(l->data)) && selection->includes(SP_ITEM(l->data)))
796                         if (!g_list_find(sc->cycling_items_selected_before, SP_ITEM(l->data)) && selection->includes(SP_ITEM(l->data)))
797                             selection->remove(SP_ITEM(l->data));
798                     }
800                     // ... clear the lists ...
801                     g_list_free(sc->cycling_items_cmp);
802                     g_list_free(sc->cycling_items_selected_before);
803                     sc->cycling_items_cmp = NULL;
804                     sc->cycling_items_selected_before = NULL;
805                     sc->cycling_cur_item = NULL;
807                     // ... and rebuild them with the new items.
808                     sc->cycling_items_cmp = g_list_copy(sc->cycling_items);
809                     SPItem *item;
810                     for(GList *l = sc->cycling_items; l != NULL; l = l->next) {
811                         item = SP_ITEM(l->data);
812                         arenaitem = item->get_arenaitem(desktop->dkey);
813                         nr_arena_item_set_opacity (arenaitem, 0.3);
814                         if (selection->includes(item)) {
815                             // already selected items are stored separately, too
816                             sc->cycling_items_selected_before = g_list_append(sc->cycling_items_selected_before, item);
817                         }
818                     }
820                     // set the current item to the bottommost one so that the cycling step below re-starts at the top
821                     sc->cycling_cur_item = g_list_last(sc->cycling_items);
822                 }
824                 // Cycle through the items underneath the mouse pointer, one-by-one
825                 sp_select_context_cycle_through_items(sc, selection, scroll_event, shift_pressed);
827                 ret = TRUE;
828             }
829             break;
830         }
832         case GDK_KEY_PRESS: // keybindings for select context
834                         {
835                         {
836                 guint keyval = get_group0_keyval(&event->key);
837             bool alt = ( MOD__ALT
838                                     || (keyval == GDK_Alt_L)
839                                     || (keyval == GDK_Alt_R)
840                                     || (keyval == GDK_Meta_L)
841                                     || (keyval == GDK_Meta_R));
843             if (!key_is_a_modifier (keyval)) {
844                     event_context->defaultMessageContext()->clear();
845             } else if (sc->grabbed || seltrans->isGrabbed()) {
846                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
847                     // if Alt then change cursor to moving cursor:
848                     if (alt) {
849                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
850                     }
851                 } else {
852                 // do not change the statusbar text when mousekey is down to move or transform the object,
853                 // because the statusbar text is already updated somewhere else.
854                    break;
855                 }
856             } else {
857                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
858                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
859                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
860                                                 _("<b>Alt</b>: click to select under; scroll mouse-wheel to cycle-select; drag to move selected or select by touch"));
861                     // if Alt and nonempty selection, show moving cursor ("move selected"):
862                     if (alt && !selection->isEmpty() && !desktop->isWaitingCursor()) {
863                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
864                     }
865                     //*/
866                     break;
867             }
868                         }
870             gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
871                         gdouble const offset = prefs->getDoubleLimited("/options/defaultscale/value", 2, 0, 1000);
872                         int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
874                         switch (get_group0_keyval (&event->key)) {
875                 case GDK_Left: // move selection left
876                 case GDK_KP_Left:
877                 case GDK_KP_4:
878                     if (!MOD__CTRL) { // not ctrl
879                         gint mul = 1 + gobble_key_events(
880                             get_group0_keyval(&event->key), 0); // with any mask
881                         if (MOD__ALT) { // alt
882                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
883                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
884                         }
885                         else { // no alt
886                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
887                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
888                         }
889                         ret = TRUE;
890                     }
891                     break;
892                 case GDK_Up: // move selection up
893                 case GDK_KP_Up:
894                 case GDK_KP_8:
895                     if (!MOD__CTRL) { // not ctrl
896                         gint mul = 1 + gobble_key_events(
897                             get_group0_keyval(&event->key), 0); // with any mask
898                         if (MOD__ALT) { // alt
899                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
900                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
901                         }
902                         else { // no alt
903                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
904                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
905                         }
906                         ret = TRUE;
907                     }
908                     break;
909                 case GDK_Right: // move selection right
910                 case GDK_KP_Right:
911                 case GDK_KP_6:
912                     if (!MOD__CTRL) { // not ctrl
913                         gint mul = 1 + gobble_key_events(
914                             get_group0_keyval(&event->key), 0); // with any mask
915                         if (MOD__ALT) { // alt
916                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
917                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
918                         }
919                         else { // no alt
920                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
921                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
922                         }
923                         ret = TRUE;
924                     }
925                     break;
926                 case GDK_Down: // move selection down
927                 case GDK_KP_Down:
928                 case GDK_KP_2:
929                     if (!MOD__CTRL) { // not ctrl
930                         gint mul = 1 + gobble_key_events(
931                             get_group0_keyval(&event->key), 0); // with any mask
932                         if (MOD__ALT) { // alt
933                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
934                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
935                         }
936                         else { // no alt
937                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
938                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
939                         }
940                         ret = TRUE;
941                     }
942                     break;
943                 case GDK_Escape:
944                     if (!sp_select_context_abort(event_context))
945                         selection->clear();
946                     ret = TRUE;
947                     break;
949                 case GDK_a:
950                 case GDK_A:
951                     if (MOD__CTRL_ONLY) {
952                         sp_edit_select_all(desktop);
953                         ret = TRUE;
954                     }
955                     break;
956                 case GDK_space:
957                     /* stamping mode: show outline mode moving */
958                     /* FIXME: Is next condition ok? (lauris) */
959                     if (sc->dragging && sc->grabbed) {
960                         seltrans->stamp();
961                         ret = TRUE;
962                     }
963                     break;
964                 case GDK_x:
965                 case GDK_X:
966                     if (MOD__ALT_ONLY) {
967                         desktop->setToolboxFocusTo ("altx");
968                         ret = TRUE;
969                     }
970                     break;
971                 case GDK_bracketleft:
972                     if (MOD__ALT) {
973                         gint mul = 1 + gobble_key_events(
974                             get_group0_keyval(&event->key), 0); // with any mask
975                         sp_selection_rotate_screen(selection, mul*1);
976                     } else if (MOD__CTRL) {
977                         sp_selection_rotate(selection, 90);
978                     } else if (snaps) {
979                         sp_selection_rotate(selection, 180/snaps);
980                     }
981                     ret = TRUE;
982                     break;
983                 case GDK_bracketright:
984                     if (MOD__ALT) {
985                         gint mul = 1 + gobble_key_events(
986                             get_group0_keyval(&event->key), 0); // with any mask
987                         sp_selection_rotate_screen(selection, -1*mul);
988                     } else if (MOD__CTRL) {
989                         sp_selection_rotate(selection, -90);
990                     } else if (snaps) {
991                         sp_selection_rotate(selection, -180/snaps);
992                     }
993                     ret = TRUE;
994                     break;
995                 case GDK_less:
996                 case GDK_comma:
997                     if (MOD__ALT) {
998                         gint mul = 1 + gobble_key_events(
999                             get_group0_keyval(&event->key), 0); // with any mask
1000                         sp_selection_scale_screen(selection, -2*mul);
1001                     } else if (MOD__CTRL) {
1002                         sp_selection_scale_times(selection, 0.5);
1003                     } else {
1004                         gint mul = 1 + gobble_key_events(
1005                             get_group0_keyval(&event->key), 0); // with any mask
1006                         sp_selection_scale(selection, -offset*mul);
1007                     }
1008                     ret = TRUE;
1009                     break;
1010                 case GDK_greater:
1011                 case GDK_period:
1012                     if (MOD__ALT) {
1013                         gint mul = 1 + gobble_key_events(
1014                             get_group0_keyval(&event->key), 0); // with any mask
1015                         sp_selection_scale_screen(selection, 2*mul);
1016                     } else if (MOD__CTRL) {
1017                         sp_selection_scale_times(selection, 2);
1018                     } else {
1019                         gint mul = 1 + gobble_key_events(
1020                             get_group0_keyval(&event->key), 0); // with any mask
1021                         sp_selection_scale(selection, offset*mul);
1022                     }
1023                     ret = TRUE;
1024                     break;
1025                 case GDK_Return:
1026                     if (MOD__CTRL_ONLY) {
1027                         if (selection->singleItem()) {
1028                             SPItem *clicked_item = selection->singleItem();
1029                             if ( SP_IS_GROUP(clicked_item) ||
1030                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
1031                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
1032                                 sp_desktop_selection(desktop)->clear();
1033                             } else {
1034                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
1035                             }
1036                         }
1037                         ret = TRUE;
1038                     }
1039                     break;
1040                 case GDK_BackSpace:
1041                     if (MOD__CTRL_ONLY) {
1042                         sp_select_context_up_one_layer(desktop);
1043                         ret = TRUE;
1044                     }
1045                     break;
1046                 case GDK_s:
1047                 case GDK_S:
1048                     if (MOD__SHIFT_ONLY) {
1049                         if (!selection->isEmpty()) {
1050                             seltrans->increaseState();
1051                         }
1052                         ret = TRUE;
1053                     }
1054                     break;
1055                 case GDK_g:
1056                 case GDK_G:
1057                     if (MOD__SHIFT_ONLY) {
1058                         sp_selection_to_guides(desktop);
1059                         ret = true;
1060                     }
1061                     break;
1062                 default:
1063                     break;
1064             }
1065             break;
1066                         }
1067         case GDK_KEY_RELEASE:
1068             {
1069             guint keyval = get_group0_keyval(&event->key);
1070             if (key_is_a_modifier (keyval))
1071                 event_context->defaultMessageContext()->clear();
1073             bool alt = ( MOD__ALT
1074                          || (keyval == GDK_Alt_L)
1075                          || (keyval == GDK_Alt_R)
1076                          || (keyval == GDK_Meta_L)
1077                          || (keyval == GDK_Meta_R));
1079             if (Inkscape::Rubberband::get(desktop)->is_started()) {
1080                 // if Alt then change cursor to moving cursor:
1081                 if (alt) {
1082                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
1083                 }
1084             } else {
1085                 if (alt) { // TODO: Should we have a variable like is_cycling or is it harmless to run this piece of code each time?
1086                     // quit cycle-selection and reset opacities
1087                     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
1088                     NRArenaItem *arenaitem;
1089                     for (GList *l = sc->cycling_items; l != NULL; l = g_list_next(l)) {
1090                         arenaitem = SP_ITEM(l->data)->get_arenaitem(desktop->dkey);
1091                         nr_arena_item_set_opacity (arenaitem, 1.0);
1092                     }
1093                     g_list_free(sc->cycling_items);
1094                     g_list_free(sc->cycling_items_selected_before);
1095                     g_list_free(sc->cycling_items_cmp);
1096                     sc->cycling_items = NULL;
1097                     sc->cycling_items_selected_before = NULL;
1098                     sc->cycling_cur_item = NULL;
1099                     sc->cycling_items_cmp = NULL;
1100                 }
1101             }
1103             }
1104             // set cursor to default.
1105             if (!desktop->isWaitingCursor()) {
1106                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
1107             }
1108             break;
1109         default:
1110             break;
1111     }
1113     if (!ret) {
1114         if (((SPEventContextClass *) parent_class)->root_handler)
1115             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1116     }
1118     return ret;
1122 /*
1123   Local Variables:
1124   mode:c++
1125   c-file-style:"stroustrup"
1126   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1127   indent-tabs-mode:nil
1128   fill-column:99
1129   End:
1130 */
1131 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :