Code

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