Code

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