Code

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