Code

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