Code

updated spanish.nsh and inkscape.nsi to reflect latest file-changes
[inkscape.git] / trunk / 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                         tools_switch_by_item (desktop, clicked_item);
434                     }
435                 } else {
436                     sp_select_context_up_one_layer(desktop);
437                 }
438                 ret = TRUE;
439             }
440             break;
441         case GDK_BUTTON_PRESS:
442             if (event->button.button == 1 && !event_context->space_panning) {
444                 // save drag origin
445                 xp = (gint) event->button.x;
446                 yp = (gint) event->button.y;
447                 within_tolerance = true;
449                 Geom::Point const button_pt(event->button.x, event->button.y);
450                 Geom::Point const p(desktop->w2d(button_pt));
451                 if (event->button.state & GDK_MOD1_MASK)
452                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
453                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
454                 if (sc->grabbed) {
455                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
456                     sc->grabbed = NULL;
457                 }
458                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
459                                     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,
460                                     NULL, event->button.time);
461                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
463                 // remember what modifiers were on before button press
464                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
465                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
466                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
468                 sc->moved = FALSE;
470                 rb_escaped = drag_escaped = 0;
472                 ret = TRUE;
473             } else if (event->button.button == 3) {
474                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
475                 sp_select_context_abort(event_context);
476             }
477             break;
479         case GDK_MOTION_NOTIFY:
480             if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
481                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
482                 Geom::Point const p(desktop->w2d(motion_pt));
484                 if ( within_tolerance
485                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
486                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
487                     break; // do not drag if we're within tolerance from origin
488                 }
489                 // Once the user has moved farther than tolerance from the original location
490                 // (indicating they intend to move the object, not click), then always process the
491                 // motion notify coordinates as given (no snapping back to origin)
492                 within_tolerance = false;
494                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
495                     // if it's not click and ctrl or alt was pressed (the latter with some selection
496                     // but not with shift) we want to drag rather than rubberband
497                     sc->dragging = TRUE;
499                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
500                 }
502                 if (sc->dragging) {
503                     /* User has dragged fast, so we get events on root (lauris)*/
504                     // not only that; we will end up here when ctrl-dragging as well
505                     // and also when we started within tolerance, but trespassed tolerance outside of item
506                     Inkscape::Rubberband::get(desktop)->stop();
507                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
508                     item_at_point = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), FALSE);
509                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
510                         item_at_point = desktop->item_at_point(Geom::Point(xp, yp), FALSE);
511                     if (item_at_point || sc->moved || sc->button_press_alt) {
512                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
513                         if (!sc->moved) {
514                             item_in_group = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), TRUE);
515                             group_at_point = desktop->group_at_point(Geom::Point(event->button.x, event->button.y));
517                             // group-at-point is meant to be topmost item if it's a group,
518                             // not topmost group of all items at point
519                             if (group_at_point != item_in_group &&
520                                 !(group_at_point && item_at_point &&
521                                   group_at_point->isAncestorOf(item_at_point)))
522                                 group_at_point = NULL;
524                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
525                             if ((!item_in_group || !selection->includes(item_in_group)) &&
526                                 (!group_at_point || !selection->includes(group_at_point))
527                                 && !sc->button_press_alt) {
528                                 // select what is under cursor
529                                 if (!seltrans->isEmpty()) {
530                                     seltrans->resetState();
531                                 }
532                                 // when simply ctrl-dragging, we don't want to go into groups
533                                 if (item_at_point && !selection->includes(item_at_point))
534                                     selection->set(item_at_point);
535                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
536                             seltrans->grab(p, -1, -1, FALSE);
537                             sc->moved = TRUE;
538                         }
539                         if (!seltrans->isEmpty())
540                             seltrans->moveTo(p, event->button.state);
541                         desktop->scroll_to_point(p);
542                         gobble_motion_events(GDK_BUTTON1_MASK);
543                         ret = TRUE;
544                     } else {
545                         sc->dragging = FALSE;
547                         sp_canvas_end_forced_full_redraws(desktop->canvas);
548                     }
549                 } else {
550                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
551                         Inkscape::Rubberband::get(desktop)->move(p);
552                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
553                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
554                         } else {
555                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
556                         }
557                         gobble_motion_events(GDK_BUTTON1_MASK);
558                     }
559                 }
560             }
561             break;
562         case GDK_BUTTON_RELEASE:
563             xp = yp = 0;
564             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
565                 if (sc->dragging) {
566                     if (sc->moved) {
567                         // item has been moved
568                         seltrans->ungrab();
569                         sc->moved = FALSE;
570                     } else if (sc->item && !drag_escaped) {
571                         // item has not been moved -> simply a click, do selecting
572                         if (!selection->isEmpty()) {
573                             if (event->button.state & GDK_SHIFT_MASK) {
574                                 // with shift, toggle selection
575                                 seltrans->resetState();
576                                 selection->toggle(sc->item);
577                             } else {
578                                 // without shift, increase state (i.e. toggle scale/rotation handles)
579                                 if (selection->includes(sc->item)) {
580                                     seltrans->increaseState();
581                                 } else {
582                                     seltrans->resetState();
583                                     selection->set(sc->item);
584                                 }
585                             }
586                         } else { // simple or shift click, no previous selection
587                             seltrans->resetState();
588                             selection->set(sc->item);
589                         }
590                     }
591                     sc->dragging = FALSE;
593                     sp_canvas_end_forced_full_redraws(desktop->canvas);
595                     if (sc->item) {
596                         sp_object_unref( SP_OBJECT(sc->item), NULL);
597                     }
598                     sc->item = NULL;
599                 } else {
600                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
601                     if (r->is_started() && !within_tolerance) {
602                         // this was a rubberband drag
603                         GSList *items = NULL;
604                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
605                             Geom::OptRect const b = r->getRectangle();
606                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
607                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
608                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
609                         }
611                         seltrans->resetState();
612                         r->stop();
613                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
615                         if (event->button.state & GDK_SHIFT_MASK) {
616                             // with shift, add to selection
617                             selection->addList (items);
618                         } else {
619                             // without shift, simply select anew
620                             selection->setList (items);
621                         }
622                         g_slist_free (items);
623                     } else { // it was just a click, or a too small rubberband
624                         r->stop();
625                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
626                             // this was a shift+click or alt+shift+click, select what was clicked upon
628                             sc->button_press_shift = false;
630                             if (sc->button_press_ctrl) {
631                                 // go into groups, honoring Alt
632                                 item = sp_event_context_find_item (desktop,
633                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
634                                 sc->button_press_ctrl = FALSE;
635                             } else {
636                                 // don't go into groups, honoring Alt
637                                 item = sp_event_context_find_item (desktop,
638                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
639                             }
641                             if (item) {
642                                 selection->toggle(item);
643                                 item = NULL;
644                             }
646                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
648                             item = sp_event_context_find_item (desktop,
649                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
651                             sc->button_press_ctrl = FALSE;
652                             sc->button_press_alt = FALSE;
654                             if (item) {
655                                 if (selection->includes(item)) {
656                                     seltrans->increaseState();
657                                 } else {
658                                     seltrans->resetState();
659                                     selection->set(item);
660                                 }
661                                 item = NULL;
662                             }
664                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
665                             if (!selection->isEmpty()) {
666                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
667                                     selection->clear();
668                                 rb_escaped = 0;
669                                 ret = TRUE;
670                             }
671                         }
672                     }
673                     ret = TRUE;
674                 }
675                 if (sc->grabbed) {
676                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
677                     sc->grabbed = NULL;
678                 }
680                 desktop->updateNow();
681             }
682             if (event->button.button == 1) {
683                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
684             }
685             sc->button_press_shift = false;
686             sc->button_press_ctrl = false;
687             sc->button_press_alt = false;
688             break;
690         case GDK_KEY_PRESS: // keybindings for select context
692             {
693             guint keyval = get_group0_keyval(&event->key);
694             bool alt = ( MOD__ALT
695                                     || (keyval == GDK_Alt_L)
696                                     || (keyval == GDK_Alt_R)
697                                     || (keyval == GDK_Meta_L)
698                                     || (keyval == GDK_Meta_R));
700             if (!key_is_a_modifier (keyval)) {
701                     event_context->defaultMessageContext()->clear();
702             } else if (sc->grabbed || seltrans->isGrabbed()) {
703                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
704                     // if Alt then change cursor to moving cursor:
705                     if (alt) {
706                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
707                     }
708                 } else {
709                 // do not change the statusbar text when mousekey is down to move or transform the object,
710                 // because the statusbar text is already updated somewhere else.
711                    break;
712                 }
713             } else {
714                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
715                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
716                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
717                                                 _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
718                     // if Alt and nonempty selection, show moving cursor ("move selected"):
719                     if (alt && !selection->isEmpty() && !desktop->isWaitingCursor()) {
720                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
721                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
722                         gdk_cursor_destroy(cursor);
723                     }
724                     //*/
725                     break;
726             }
727             }
729             switch (get_group0_keyval (&event->key)) {
730                 case GDK_Left: // move selection left
731                 case GDK_KP_Left:
732                 case GDK_KP_4:
733                     if (!MOD__CTRL) { // not ctrl
734                         gint mul = 1 + gobble_key_events(
735                             get_group0_keyval(&event->key), 0); // with any mask
736                         if (MOD__ALT) { // alt
737                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
738                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
739                         }
740                         else { // no alt
741                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
742                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
743                         }
744                         ret = TRUE;
745                     }
746                     break;
747                 case GDK_Up: // move selection up
748                 case GDK_KP_Up:
749                 case GDK_KP_8:
750                     if (!MOD__CTRL) { // not ctrl
751                         gint mul = 1 + gobble_key_events(
752                             get_group0_keyval(&event->key), 0); // with any mask
753                         if (MOD__ALT) { // alt
754                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
755                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
756                         }
757                         else { // no alt
758                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
759                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
760                         }
761                         ret = TRUE;
762                     }
763                     break;
764                 case GDK_Right: // move selection right
765                 case GDK_KP_Right:
766                 case GDK_KP_6:
767                     if (!MOD__CTRL) { // not ctrl
768                         gint mul = 1 + gobble_key_events(
769                             get_group0_keyval(&event->key), 0); // with any mask
770                         if (MOD__ALT) { // alt
771                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
772                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
773                         }
774                         else { // no alt
775                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
776                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
777                         }
778                         ret = TRUE;
779                     }
780                     break;
781                 case GDK_Down: // move selection down
782                 case GDK_KP_Down:
783                 case GDK_KP_2:
784                     if (!MOD__CTRL) { // not ctrl
785                         gint mul = 1 + gobble_key_events(
786                             get_group0_keyval(&event->key), 0); // with any mask
787                         if (MOD__ALT) { // alt
788                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
789                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
790                         }
791                         else { // no alt
792                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
793                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
794                         }
795                         ret = TRUE;
796                     }
797                     break;
798                 case GDK_Escape:
799                     if (!sp_select_context_abort(event_context))
800                         selection->clear();
801                     ret = TRUE;
802                     break;
803                 case GDK_a:
804                 case GDK_A:
805                     if (MOD__CTRL_ONLY) {
806                         sp_edit_select_all(desktop);
807                         ret = TRUE;
808                     }
809                     break;
810                 case GDK_space:
811                     /* stamping mode: show outline mode moving */
812                     /* FIXME: Is next condition ok? (lauris) */
813                     if (sc->dragging && sc->grabbed) {
814                         seltrans->stamp();
815                         ret = TRUE;
816                     }
817                     break;
818                 case GDK_x:
819                 case GDK_X:
820                     if (MOD__ALT_ONLY) {
821                         desktop->setToolboxFocusTo ("altx");
822                         ret = TRUE;
823                     }
824                     break;
825                 case GDK_bracketleft:
826                     if (MOD__ALT) {
827                         gint mul = 1 + gobble_key_events(
828                             get_group0_keyval(&event->key), 0); // with any mask
829                         sp_selection_rotate_screen(selection, mul*1);
830                     } else if (MOD__CTRL) {
831                         sp_selection_rotate(selection, 90);
832                     } else if (snaps) {
833                         sp_selection_rotate(selection, 180/snaps);
834                     }
835                     ret = TRUE;
836                     break;
837                 case GDK_bracketright:
838                     if (MOD__ALT) {
839                         gint mul = 1 + gobble_key_events(
840                             get_group0_keyval(&event->key), 0); // with any mask
841                         sp_selection_rotate_screen(selection, -1*mul);
842                     } else if (MOD__CTRL) {
843                         sp_selection_rotate(selection, -90);
844                     } else if (snaps) {
845                         sp_selection_rotate(selection, -180/snaps);
846                     }
847                     ret = TRUE;
848                     break;
849                 case GDK_less:
850                 case GDK_comma:
851                     if (MOD__ALT) {
852                         gint mul = 1 + gobble_key_events(
853                             get_group0_keyval(&event->key), 0); // with any mask
854                         sp_selection_scale_screen(selection, -2*mul);
855                     } else if (MOD__CTRL) {
856                         sp_selection_scale_times(selection, 0.5);
857                     } else {
858                         gint mul = 1 + gobble_key_events(
859                             get_group0_keyval(&event->key), 0); // with any mask
860                         sp_selection_scale(selection, -offset*mul);
861                     }
862                     ret = TRUE;
863                     break;
864                 case GDK_greater:
865                 case GDK_period:
866                     if (MOD__ALT) {
867                         gint mul = 1 + gobble_key_events(
868                             get_group0_keyval(&event->key), 0); // with any mask
869                         sp_selection_scale_screen(selection, 2*mul);
870                     } else if (MOD__CTRL) {
871                         sp_selection_scale_times(selection, 2);
872                     } else {
873                         gint mul = 1 + gobble_key_events(
874                             get_group0_keyval(&event->key), 0); // with any mask
875                         sp_selection_scale(selection, offset*mul);
876                     }
877                     ret = TRUE;
878                     break;
879                 case GDK_Return:
880                     if (MOD__CTRL_ONLY) {
881                         if (selection->singleItem()) {
882                             SPItem *clicked_item = selection->singleItem();
883                             if ( SP_IS_GROUP(clicked_item) ||
884                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
885                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
886                                 sp_desktop_selection(desktop)->clear();
887                             } else {
888                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
889                             }
890                         }
891                         ret = TRUE;
892                     }
893                     break;
894                 case GDK_BackSpace:
895                     if (MOD__CTRL_ONLY) {
896                         sp_select_context_up_one_layer(desktop);
897                         ret = TRUE;
898                     }
899                     break;
900                 case GDK_s:
901                 case GDK_S:
902                     if (MOD__SHIFT_ONLY) {
903                         if (!selection->isEmpty()) {
904                             seltrans->increaseState();
905                         }
906                         ret = TRUE;
907                     }
908                     break;
909                 case GDK_g:
910                 case GDK_G:
911                     if (MOD__SHIFT_ONLY) {
912                         sp_selection_to_guides(desktop);
913                         ret = true;
914                     }
915                     break;
916                 default:
917                     break;
918             }
919             break;
921         case GDK_KEY_RELEASE:
922             {
923             guint keyval = get_group0_keyval(&event->key);
924             if (key_is_a_modifier (keyval))
925                 event_context->defaultMessageContext()->clear();
927             bool alt = ( MOD__ALT
928                          || (keyval == GDK_Alt_L)
929                          || (keyval == GDK_Alt_R)
930                          || (keyval == GDK_Meta_L)
931                          || (keyval == GDK_Meta_R));
933             if (Inkscape::Rubberband::get(desktop)->is_started()) {
934                 // if Alt then change cursor to moving cursor:
935                 if (alt) {
936                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
937                 }
938             }
939             }
940             // set cursor to default.
941             if (!desktop->isWaitingCursor())
942                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
943             break;
944         default:
945             break;
946     }
948     if (!ret) {
949         if (((SPEventContextClass *) parent_class)->root_handler)
950             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
951     }
953     return ret;
957 /*
958   Local Variables:
959   mode:c++
960   c-file-style:"stroustrup"
961   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
962   indent-tabs-mode:nil
963   fill-column:99
964   End:
965 */
966 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :