Code

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