Code

Fix two "snap window not opened" warnings
[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             sp_event_context_snap_window_closed(event_context);
216             drag_escaped = 1;
218             if (sc->item) {
219                 // only undo if the item is still valid
220                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
221                     sp_document_undo(sp_desktop_document(desktop));
222                 }
224                 sp_object_unref( SP_OBJECT(sc->item), NULL);
225             } else if (sc->button_press_ctrl) {
226                 // NOTE:  This is a workaround to a bug.
227                 // When the ctrl key is held, sc->item is not defined
228                 // so in this case (only), we skip the object doc check
229                 sp_document_undo(sp_desktop_document(desktop));
230             }
231             sc->item = NULL;
233             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Move canceled."));
234             return true;
235         }
236     } else {
237         if (Inkscape::Rubberband::get(desktop)->is_started()) {
238             Inkscape::Rubberband::get(desktop)->stop();
239             rb_escaped = 1;
240             SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
241             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
242             return true;
243         }
244     }
245     return false;
248 bool
249 key_is_a_modifier (guint key) {
250     return (key == GDK_Alt_L ||
251                 key == GDK_Alt_R ||
252                 key == GDK_Control_L ||
253                 key == GDK_Control_R ||
254                 key == GDK_Shift_L ||
255                 key == GDK_Shift_R ||
256                 key == GDK_Meta_L ||  // Meta is when you press Shift+Alt (at least on my machine)
257             key == GDK_Meta_R);
260 void
261 sp_select_context_up_one_layer(SPDesktop *desktop)
263     /* Click in empty place, go up one level -- but don't leave a layer to root.
264      *
265      * (Rationale: we don't usually allow users to go to the root, since that
266      * detracts from the layer metaphor: objects at the root level can in front
267      * of or behind layers.  Whereas it's fine to go to the root if editing
268      * a document that has no layers (e.g. a non-Inkscape document).)
269      *
270      * Once we support editing SVG "islands" (e.g. <svg> embedded in an xhtml
271      * document), we might consider further restricting the below to disallow
272      * leaving a layer to go to a non-layer.
273      */
274     SPObject *const current_layer = desktop->currentLayer();
275     if (current_layer) {
276         SPObject *const parent = SP_OBJECT_PARENT(current_layer);
277         if ( parent
278              && ( SP_OBJECT_PARENT(parent)
279                   || !( SP_IS_GROUP(current_layer)
280                         && ( SPGroup::LAYER
281                              == SP_GROUP(current_layer)->layerMode() ) ) ) )
282         {
283             desktop->setCurrentLayer(parent);
284             if (SP_IS_GROUP(current_layer) && SPGroup::LAYER != SP_GROUP(current_layer)->layerMode())
285                 sp_desktop_selection(desktop)->set(current_layer);
286         }
287     }
290 static gint
291 sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
293     gint ret = FALSE;
295     SPDesktop *desktop = event_context->desktop;
296     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
297     Inkscape::SelTrans *seltrans = sc->_seltrans;
299     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
302     // make sure we still have valid objects to move around
303     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
304         sp_select_context_abort(event_context);
305     }
307     switch (event->type) {
308         case GDK_BUTTON_PRESS:
309             if (event->button.button == 1 && !event_context->space_panning) {
310                 /* Left mousebutton */
312                 // save drag origin
313                 xp = (gint) event->button.x;
314                 yp = (gint) event->button.y;
315                 within_tolerance = true;
317                 // remember what modifiers were on before button press
318                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
319                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
320                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
322                 if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
323                     // if shift or ctrl was pressed, do not move objects;
324                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
325                 } else {
326                     sc->dragging = TRUE;
327                     sp_event_context_snap_window_open(event_context);
328                     sc->moved = FALSE;
330                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
332                     // remember the clicked item in sc->item:
333                     if (sc->item) {
334                         sp_object_unref(sc->item, NULL);
335                         sc->item = NULL;
336                     }
337                     sc->item = sp_event_context_find_item (desktop,
338                                               Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
339                     sp_object_ref(sc->item, NULL);
341                     rb_escaped = drag_escaped = 0;
343                     if (sc->grabbed) {
344                         sp_canvas_item_ungrab(sc->grabbed, event->button.time);
345                         sc->grabbed = NULL;
346                     }
347                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
348                                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
349                                         GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
350                                         NULL, event->button.time);
351                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
353                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
355                     ret = TRUE;
356                 }
357             } else if (event->button.button == 3) {
358                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
359                 sp_select_context_abort(event_context);
360             }
361             break;
363         case GDK_ENTER_NOTIFY:
364         {
365             if (!desktop->isWaitingCursor()) {
366                 GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
367                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
368                 gdk_cursor_destroy(cursor);
369             }
370             break;
371         }
373         case GDK_LEAVE_NOTIFY:
374             if (!desktop->isWaitingCursor())
375                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
376             break;
378         case GDK_KEY_PRESS:
379             if (get_group0_keyval (&event->key) == GDK_space) {
380                 if (sc->dragging && sc->grabbed) {
381                     /* stamping mode: show content mode moving */
382                     seltrans->stamp();
383                     ret = TRUE;
384                 }
385             }
386             break;
388         default:
389             break;
390     }
392     if (!ret) {
393         if (((SPEventContextClass *) parent_class)->item_handler)
394             ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
395     }
397     return ret;
400 static gint
401 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
403     SPItem *item = NULL;
404     SPItem *item_at_point = NULL, *group_at_point = NULL, *item_in_group = NULL;
405     gint ret = FALSE;
407     SPDesktop *desktop = event_context->desktop;
408     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
409     Inkscape::SelTrans *seltrans = sc->_seltrans;
410     Inkscape::Selection *selection = sp_desktop_selection(desktop);
411     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
413     gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
414     gdouble const offset = prefs->getDoubleLimited("/options/defaultscale/value", 2, 0, 1000);
415     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
416     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
418     // make sure we still have valid objects to move around
419     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
420         sp_select_context_abort(event_context);
421     }
423     switch (event->type) {
424         case GDK_2BUTTON_PRESS:
425             if (event->button.button == 1) {
426                 if (!selection->isEmpty()) {
427                     SPItem *clicked_item = (SPItem *) selection->itemList()->data;
428                     if (SP_IS_GROUP(clicked_item) && !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
429                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
430                         sp_desktop_selection(desktop)->clear();
431                         sc->dragging = false;
432                         sp_event_context_snap_window_closed(event_context);
434                         sp_canvas_end_forced_full_redraws(desktop->canvas);
435                     } else { // switch tool
436                         Geom::Point const button_pt(event->button.x, event->button.y);
437                         Geom::Point const p(desktop->w2d(button_pt));
438                         tools_switch_by_item (desktop, clicked_item, p);
439                     }
440                 } else {
441                     sp_select_context_up_one_layer(desktop);
442                 }
443                 ret = TRUE;
444             }
445             break;
446         case GDK_BUTTON_PRESS:
447             if (event->button.button == 1 && !event_context->space_panning) {
449                 // save drag origin
450                 xp = (gint) event->button.x;
451                 yp = (gint) event->button.y;
452                 within_tolerance = true;
454                 Geom::Point const button_pt(event->button.x, event->button.y);
455                 Geom::Point const p(desktop->w2d(button_pt));
456                 if (event->button.state & GDK_MOD1_MASK)
457                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
458                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
459                 if (sc->grabbed) {
460                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
461                     sc->grabbed = NULL;
462                 }
463                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
464                                     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,
465                                     NULL, event->button.time);
466                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
468                 // remember what modifiers were on before button press
469                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
470                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
471                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
473                 sc->moved = FALSE;
475                 rb_escaped = drag_escaped = 0;
477                 ret = TRUE;
478             } else if (event->button.button == 3) {
479                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
480                 sp_select_context_abort(event_context);
481             }
482             break;
484         case GDK_MOTION_NOTIFY:
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                         if (sc->dragging == FALSE) {
503                                 sp_event_context_snap_window_open(event_context);
504                         }
505                         sc->dragging = TRUE;
507                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
508                 }
510                 if (sc->dragging) {
511                     /* User has dragged fast, so we get events on root (lauris)*/
512                     // not only that; we will end up here when ctrl-dragging as well
513                     // and also when we started within tolerance, but trespassed tolerance outside of item
514                     Inkscape::Rubberband::get(desktop)->stop();
515                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
516                     item_at_point = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), FALSE);
517                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
518                         item_at_point = desktop->item_at_point(Geom::Point(xp, yp), FALSE);
519                     if (item_at_point || sc->moved || sc->button_press_alt) {
520                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
521                         if (!sc->moved) {
522                             item_in_group = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), TRUE);
523                             group_at_point = desktop->group_at_point(Geom::Point(event->button.x, event->button.y));
524                             if (SP_IS_LAYER(selection->single()))
525                                 group_at_point = SP_GROUP(selection->single());
527                             // group-at-point is meant to be topmost item if it's a group,
528                             // not topmost group of all items at point
529                             if (group_at_point != item_in_group &&
530                                 !(group_at_point && item_at_point &&
531                                   group_at_point->isAncestorOf(item_at_point)))
532                                 group_at_point = NULL;
534                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
535                             if ((!item_in_group || !selection->includes(item_in_group)) &&
536                                 (!group_at_point || !selection->includes(group_at_point))
537                                 && !sc->button_press_alt) {
538                                 // select what is under cursor
539                                 if (!seltrans->isEmpty()) {
540                                     seltrans->resetState();
541                                 }
542                                 // when simply ctrl-dragging, we don't want to go into groups
543                                 if (item_at_point && !selection->includes(item_at_point))
544                                     selection->set(item_at_point);
545                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
546                             seltrans->grab(p, -1, -1, FALSE);
547                             sc->moved = TRUE;
548                         }
549                         if (!seltrans->isEmpty())
550                             seltrans->moveTo(p, event->button.state);
551                         desktop->scroll_to_point(p);
552                         gobble_motion_events(GDK_BUTTON1_MASK);
553                         ret = TRUE;
554                     } else {
555                         sc->dragging = FALSE;
556                         sp_event_context_snap_window_closed(event_context);
557                         sp_canvas_end_forced_full_redraws(desktop->canvas);
558                     }
559                 } else {
560                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
561                         Inkscape::Rubberband::get(desktop)->move(p);
562                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
563                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
564                         } else {
565                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
566                         }
567                         gobble_motion_events(GDK_BUTTON1_MASK);
568                     }
569                 }
570             }
571             break;
572         case GDK_BUTTON_RELEASE:
573             xp = yp = 0;
574             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
575                 if (sc->dragging) {
576                     if (sc->moved) {
577                         // item has been moved
578                         seltrans->ungrab();
579                         sc->moved = FALSE;
580                     } else if (sc->item && !drag_escaped) {
581                         // item has not been moved -> simply a click, do selecting
582                         if (!selection->isEmpty()) {
583                             if (event->button.state & GDK_SHIFT_MASK) {
584                                 // with shift, toggle selection
585                                 seltrans->resetState();
586                                 selection->toggle(sc->item);
587                             } else {
588                                 SPObject* single = selection->single();
589                                 // without shift, increase state (i.e. toggle scale/rotation handles)
590                                 if (selection->includes(sc->item)) {
591                                     seltrans->increaseState();
592                                 } else if (SP_IS_LAYER(single) && single->isAncestorOf(sc->item)) {
593                                     seltrans->increaseState();
594                                 } else {
595                                     seltrans->resetState();
596                                     selection->set(sc->item);
597                                 }
598                             }
599                         } else { // simple or shift click, no previous selection
600                             seltrans->resetState();
601                             selection->set(sc->item);
602                         }
603                     }
604                     sc->dragging = FALSE;
605                     sp_event_context_snap_window_closed(event_context);
606                     sp_canvas_end_forced_full_redraws(desktop->canvas);
608                     if (sc->item) {
609                         sp_object_unref( SP_OBJECT(sc->item), NULL);
610                     }
611                     sc->item = NULL;
612                 } else {
613                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
614                     if (r->is_started() && !within_tolerance) {
615                         // this was a rubberband drag
616                         GSList *items = NULL;
617                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
618                             Geom::OptRect const b = r->getRectangle();
619                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
620                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
621                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
622                         }
624                         seltrans->resetState();
625                         r->stop();
626                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
628                         if (event->button.state & GDK_SHIFT_MASK) {
629                             // with shift, add to selection
630                             selection->addList (items);
631                         } else {
632                             // without shift, simply select anew
633                             selection->setList (items);
634                         }
635                         g_slist_free (items);
636                     } else { // it was just a click, or a too small rubberband
637                         r->stop();
638                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
639                             // this was a shift+click or alt+shift+click, select what was clicked upon
641                             sc->button_press_shift = false;
643                             if (sc->button_press_ctrl) {
644                                 // go into groups, honoring Alt
645                                 item = sp_event_context_find_item (desktop,
646                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
647                                 sc->button_press_ctrl = FALSE;
648                             } else {
649                                 // don't go into groups, honoring Alt
650                                 item = sp_event_context_find_item (desktop,
651                                                    Geom::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
652                             }
654                             if (item) {
655                                 selection->toggle(item);
656                                 item = NULL;
657                             }
659                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
661                             item = sp_event_context_find_item (desktop,
662                                          Geom::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
664                             sc->button_press_ctrl = FALSE;
665                             sc->button_press_alt = FALSE;
667                             if (item) {
668                                 if (selection->includes(item)) {
669                                     seltrans->increaseState();
670                                 } else {
671                                     seltrans->resetState();
672                                     selection->set(item);
673                                 }
674                                 item = NULL;
675                             }
677                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
678                             if (!selection->isEmpty()) {
679                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
680                                     selection->clear();
681                                 rb_escaped = 0;
682                                 ret = TRUE;
683                             }
684                         }
685                     }
686                     ret = TRUE;
687                 }
688                 if (sc->grabbed) {
689                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
690                     sc->grabbed = NULL;
691                 }
693                 desktop->updateNow();
694             }
695             if (event->button.button == 1) {
696                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
697             }
698             sc->button_press_shift = false;
699             sc->button_press_ctrl = false;
700             sc->button_press_alt = false;
701             break;
703         case GDK_KEY_PRESS: // keybindings for select context
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                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
734                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
735                         gdk_cursor_destroy(cursor);
736                     }
737                     //*/
738                     break;
739             }
740             }
742             switch (get_group0_keyval (&event->key)) {
743                 case GDK_Left: // move selection left
744                 case GDK_KP_Left:
745                 case GDK_KP_4:
746                     if (!MOD__CTRL) { // not ctrl
747                         gint mul = 1 + gobble_key_events(
748                             get_group0_keyval(&event->key), 0); // with any mask
749                         if (MOD__ALT) { // alt
750                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
751                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
752                         }
753                         else { // no alt
754                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
755                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
756                         }
757                         ret = TRUE;
758                     }
759                     break;
760                 case GDK_Up: // move selection up
761                 case GDK_KP_Up:
762                 case GDK_KP_8:
763                     if (!MOD__CTRL) { // not ctrl
764                         gint mul = 1 + gobble_key_events(
765                             get_group0_keyval(&event->key), 0); // with any mask
766                         if (MOD__ALT) { // alt
767                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
768                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
769                         }
770                         else { // no alt
771                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
772                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
773                         }
774                         ret = TRUE;
775                     }
776                     break;
777                 case GDK_Right: // move selection right
778                 case GDK_KP_Right:
779                 case GDK_KP_6:
780                     if (!MOD__CTRL) { // not ctrl
781                         gint mul = 1 + gobble_key_events(
782                             get_group0_keyval(&event->key), 0); // with any mask
783                         if (MOD__ALT) { // alt
784                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
785                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
786                         }
787                         else { // no alt
788                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
789                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
790                         }
791                         ret = TRUE;
792                     }
793                     break;
794                 case GDK_Down: // move selection down
795                 case GDK_KP_Down:
796                 case GDK_KP_2:
797                     if (!MOD__CTRL) { // not ctrl
798                         gint mul = 1 + gobble_key_events(
799                             get_group0_keyval(&event->key), 0); // with any mask
800                         if (MOD__ALT) { // alt
801                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
802                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
803                         }
804                         else { // no alt
805                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
806                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
807                         }
808                         ret = TRUE;
809                     }
810                     break;
811                 case GDK_Escape:
812                     if (!sp_select_context_abort(event_context))
813                         selection->clear();
814                     ret = TRUE;
815                     break;
816                 case GDK_a:
817                 case GDK_A:
818                     if (MOD__CTRL_ONLY) {
819                         sp_edit_select_all(desktop);
820                         ret = TRUE;
821                     }
822                     break;
823                 case GDK_space:
824                     /* stamping mode: show outline mode moving */
825                     /* FIXME: Is next condition ok? (lauris) */
826                     if (sc->dragging && sc->grabbed) {
827                         seltrans->stamp();
828                         ret = TRUE;
829                     }
830                     break;
831                 case GDK_x:
832                 case GDK_X:
833                     if (MOD__ALT_ONLY) {
834                         desktop->setToolboxFocusTo ("altx");
835                         ret = TRUE;
836                     }
837                     break;
838                 case GDK_bracketleft:
839                     if (MOD__ALT) {
840                         gint mul = 1 + gobble_key_events(
841                             get_group0_keyval(&event->key), 0); // with any mask
842                         sp_selection_rotate_screen(selection, mul*1);
843                     } else if (MOD__CTRL) {
844                         sp_selection_rotate(selection, 90);
845                     } else if (snaps) {
846                         sp_selection_rotate(selection, 180/snaps);
847                     }
848                     ret = TRUE;
849                     break;
850                 case GDK_bracketright:
851                     if (MOD__ALT) {
852                         gint mul = 1 + gobble_key_events(
853                             get_group0_keyval(&event->key), 0); // with any mask
854                         sp_selection_rotate_screen(selection, -1*mul);
855                     } else if (MOD__CTRL) {
856                         sp_selection_rotate(selection, -90);
857                     } else if (snaps) {
858                         sp_selection_rotate(selection, -180/snaps);
859                     }
860                     ret = TRUE;
861                     break;
862                 case GDK_less:
863                 case GDK_comma:
864                     if (MOD__ALT) {
865                         gint mul = 1 + gobble_key_events(
866                             get_group0_keyval(&event->key), 0); // with any mask
867                         sp_selection_scale_screen(selection, -2*mul);
868                     } else if (MOD__CTRL) {
869                         sp_selection_scale_times(selection, 0.5);
870                     } else {
871                         gint mul = 1 + gobble_key_events(
872                             get_group0_keyval(&event->key), 0); // with any mask
873                         sp_selection_scale(selection, -offset*mul);
874                     }
875                     ret = TRUE;
876                     break;
877                 case GDK_greater:
878                 case GDK_period:
879                     if (MOD__ALT) {
880                         gint mul = 1 + gobble_key_events(
881                             get_group0_keyval(&event->key), 0); // with any mask
882                         sp_selection_scale_screen(selection, 2*mul);
883                     } else if (MOD__CTRL) {
884                         sp_selection_scale_times(selection, 2);
885                     } else {
886                         gint mul = 1 + gobble_key_events(
887                             get_group0_keyval(&event->key), 0); // with any mask
888                         sp_selection_scale(selection, offset*mul);
889                     }
890                     ret = TRUE;
891                     break;
892                 case GDK_Return:
893                     if (MOD__CTRL_ONLY) {
894                         if (selection->singleItem()) {
895                             SPItem *clicked_item = selection->singleItem();
896                             if ( SP_IS_GROUP(clicked_item) ||
897                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
898                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
899                                 sp_desktop_selection(desktop)->clear();
900                             } else {
901                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
902                             }
903                         }
904                         ret = TRUE;
905                     }
906                     break;
907                 case GDK_BackSpace:
908                     if (MOD__CTRL_ONLY) {
909                         sp_select_context_up_one_layer(desktop);
910                         ret = TRUE;
911                     }
912                     break;
913                 case GDK_s:
914                 case GDK_S:
915                     if (MOD__SHIFT_ONLY) {
916                         if (!selection->isEmpty()) {
917                             seltrans->increaseState();
918                         }
919                         ret = TRUE;
920                     }
921                     break;
922                 case GDK_g:
923                 case GDK_G:
924                     if (MOD__SHIFT_ONLY) {
925                         sp_selection_to_guides(desktop);
926                         ret = true;
927                     }
928                     break;
929                 default:
930                     break;
931             }
932             break;
934         case GDK_KEY_RELEASE:
935             {
936             guint keyval = get_group0_keyval(&event->key);
937             if (key_is_a_modifier (keyval))
938                 event_context->defaultMessageContext()->clear();
940             bool alt = ( MOD__ALT
941                          || (keyval == GDK_Alt_L)
942                          || (keyval == GDK_Alt_R)
943                          || (keyval == GDK_Meta_L)
944                          || (keyval == GDK_Meta_R));
946             if (Inkscape::Rubberband::get(desktop)->is_started()) {
947                 // if Alt then change cursor to moving cursor:
948                 if (alt) {
949                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
950                 }
951             }
952             }
953             // set cursor to default.
954             if (!desktop->isWaitingCursor())
955                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
956             break;
957         default:
958             break;
959     }
961     if (!ret) {
962         if (((SPEventContextClass *) parent_class)->root_handler)
963             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
964     }
966     return ret;
970 /*
971   Local Variables:
972   mode:c++
973   c-file-style:"stroustrup"
974   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
975   indent-tabs-mode:nil
976   fill-column:99
977   End:
978 */
979 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :