Code

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