Code

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