Code

convert almost all libnrtype to Geom::
[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 "prefs-utils.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, gchar const *key, gchar const *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     if (prefs_get_int_attribute("tools.select", "gradientdrag", 0) != 0) {
183         ec->enableGrDrag();
184     }
187 static void
188 sp_select_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
190     SPSelectContext *sc = SP_SELECT_CONTEXT(ec);
192     if (!strcmp(key, "show")) {
193         if (val && !strcmp(val, "outline")) {
194             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_OUTLINE);
195         } else {
196             sc->_seltrans->setShow(Inkscape::SelTrans::SHOW_CONTENT);
197         }
198     }
201 static bool
202 sp_select_context_abort(SPEventContext *event_context)
204     SPDesktop *desktop = event_context->desktop;
205     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
206     Inkscape::SelTrans *seltrans = sc->_seltrans;
208     if (sc->dragging) {
209         if (sc->moved) { // cancel dragging an object
210             seltrans->ungrab();
211             sc->moved = FALSE;
212             sc->dragging = FALSE;
213             drag_escaped = 1;
215             if (sc->item) {
216                 // only undo if the item is still valid
217                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
218                     sp_document_undo(sp_desktop_document(desktop));
219                 }
221                 sp_object_unref( SP_OBJECT(sc->item), NULL);
222             } else if (sc->button_press_ctrl) {
223                 // NOTE:  This is a workaround to a bug.
224                 // When the ctrl key is held, sc->item is not defined
225                 // so in this case (only), we skip the object doc check
226                 sp_document_undo(sp_desktop_document(desktop));
227             }
228             sc->item = NULL;
230             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Move canceled."));
231             return true;
232         }
233     } else {
234         if (Inkscape::Rubberband::get(desktop)->is_started()) {
235             Inkscape::Rubberband::get(desktop)->stop();
236             rb_escaped = 1;
237             SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
238             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
239             return true;
240         }
241     }
242     return false;
245 bool
246 key_is_a_modifier (guint key) {
247     return (key == GDK_Alt_L ||
248                 key == GDK_Alt_R ||
249                 key == GDK_Control_L ||
250                 key == GDK_Control_R ||
251                 key == GDK_Shift_L ||
252                 key == GDK_Shift_R ||
253                 key == GDK_Meta_L ||  // Meta is when you press Shift+Alt (at least on my machine)
254             key == GDK_Meta_R);
257 void
258 sp_select_context_up_one_layer(SPDesktop *desktop)
260     /* Click in empty place, go up one level -- but don't leave a layer to root.
261      *
262      * (Rationale: we don't usually allow users to go to the root, since that
263      * detracts from the layer metaphor: objects at the root level can in front
264      * of or behind layers.  Whereas it's fine to go to the root if editing
265      * a document that has no layers (e.g. a non-Inkscape document).)
266      *
267      * Once we support editing SVG "islands" (e.g. <svg> embedded in an xhtml
268      * document), we might consider further restricting the below to disallow
269      * leaving a layer to go to a non-layer.
270      */
271     SPObject *const current_layer = desktop->currentLayer();
272     if (current_layer) {
273         SPObject *const parent = SP_OBJECT_PARENT(current_layer);
274         if ( parent
275              && ( SP_OBJECT_PARENT(parent)
276                   || !( SP_IS_GROUP(current_layer)
277                         && ( SPGroup::LAYER
278                              == SP_GROUP(current_layer)->layerMode() ) ) ) )
279         {
280             desktop->setCurrentLayer(parent);
281             if (SP_IS_GROUP(current_layer) && SPGroup::LAYER != SP_GROUP(current_layer)->layerMode())
282                 sp_desktop_selection(desktop)->set(current_layer);
283         }
284     }
287 static gint
288 sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
290     gint ret = FALSE;
292     SPDesktop *desktop = event_context->desktop;
293     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
294     Inkscape::SelTrans *seltrans = sc->_seltrans;
296     tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
298     // make sure we still have valid objects to move around
299     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
300         sp_select_context_abort(event_context);
301     }
303     switch (event->type) {
304         case GDK_BUTTON_PRESS:
305             if (event->button.button == 1 && !event_context->space_panning) {
306                 /* Left mousebutton */
308                 // save drag origin
309                 xp = (gint) event->button.x;
310                 yp = (gint) event->button.y;
311                 within_tolerance = true;
313                 // remember what modifiers were on before button press
314                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
315                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
316                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
318                 if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
319                     // if shift or ctrl was pressed, do not move objects;
320                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
321                 } else {
322                     sc->dragging = TRUE;
323                     sc->moved = FALSE;
325                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
326                     
327                     // remember the clicked item in sc->item:
328                     if (sc->item) {
329                         sp_object_unref(sc->item, NULL);
330                         sc->item = NULL;
331                     }
332                     sc->item = sp_event_context_find_item (desktop,
333                                               NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
334                     sp_object_ref(sc->item, NULL);
336                     rb_escaped = drag_escaped = 0;
338                     if (sc->grabbed) {
339                         sp_canvas_item_ungrab(sc->grabbed, event->button.time);
340                         sc->grabbed = NULL;
341                     }
342                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
343                                         GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
344                                         GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK,
345                                         NULL, event->button.time);
346                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
348                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
350                     ret = TRUE;
351                 }
352             } else if (event->button.button == 3) {
353                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
354                 sp_select_context_abort(event_context);
355             }
356             break;
358         case GDK_ENTER_NOTIFY:
359         {
360             GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
361             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
362             gdk_cursor_destroy(cursor);
363             break;
364         }
366         case GDK_LEAVE_NOTIFY:
367             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
368             break;
370         case GDK_KEY_PRESS:
371             if (get_group0_keyval (&event->key) == GDK_space) {
372                 if (sc->dragging && sc->grabbed) {
373                     /* stamping mode: show content mode moving */
374                     seltrans->stamp();
375                     ret = TRUE;
376                 }
377             }
378             break;
380         default:
381             break;
382     }
384     if (!ret) {
385         if (((SPEventContextClass *) parent_class)->item_handler)
386             ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
387     }
389     return ret;
392 static gint
393 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
395     SPItem *item = NULL;
396     SPItem *item_at_point = NULL, *group_at_point = NULL, *item_in_group = NULL;
397     gint ret = FALSE;
399     SPDesktop *desktop = event_context->desktop;
400     SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
401     Inkscape::SelTrans *seltrans = sc->_seltrans;
402     Inkscape::Selection *selection = sp_desktop_selection(desktop);
403     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
404     gdouble const offset = prefs_get_double_attribute_limited("options.defaultscale", "value", 2, 0, 1000);
405     tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
406     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
408     // make sure we still have valid objects to move around
409     if (sc->item && SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))==NULL) {
410         sp_select_context_abort(event_context);
411     }
413     switch (event->type) {
414         case GDK_2BUTTON_PRESS:
415             if (event->button.button == 1) {
416                 if (!selection->isEmpty()) {
417                     SPItem *clicked_item = (SPItem *) selection->itemList()->data;
418                     if (SP_IS_GROUP(clicked_item) && !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
419                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
420                         sp_desktop_selection(desktop)->clear();
421                         sc->dragging = false;
423                         sp_canvas_end_forced_full_redraws(desktop->canvas);
424                     } else { // switch tool
425                         tools_switch_by_item (desktop, clicked_item);
426                     }
427                 } else {
428                     sp_select_context_up_one_layer(desktop);
429                 }
430                 ret = TRUE;
431             }
432             break;
433         case GDK_BUTTON_PRESS:
434             if (event->button.button == 1 && !event_context->space_panning) {
436                 // save drag origin
437                 xp = (gint) event->button.x;
438                 yp = (gint) event->button.y;
439                 within_tolerance = true;
441                 NR::Point const button_pt(event->button.x, event->button.y);
442                 NR::Point const p(desktop->w2d(button_pt));
443                 if (event->button.state & GDK_MOD1_MASK)
444                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
445                 Inkscape::Rubberband::get(desktop)->start(desktop, p);
446                 if (sc->grabbed) {
447                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
448                     sc->grabbed = NULL;
449                 }
450                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
451                                     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,
452                                     NULL, event->button.time);
453                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
455                 // remember what modifiers were on before button press
456                 sc->button_press_shift = (event->button.state & GDK_SHIFT_MASK) ? true : false;
457                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
458                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
460                 sc->moved = FALSE;
462                 rb_escaped = drag_escaped = 0;
464                 ret = TRUE;
465             } else if (event->button.button == 3) {
466                 // right click; do not eat it so that right-click menu can appear, but cancel dragging & rubberband
467                 sp_select_context_abort(event_context);
468             }
469             break;
471         case GDK_MOTION_NOTIFY:
472             if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
473                 NR::Point const motion_pt(event->motion.x, event->motion.y);
474                 NR::Point const p(desktop->w2d(motion_pt));
476                 if ( within_tolerance
477                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
478                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
479                     break; // do not drag if we're within tolerance from origin
480                 }
481                 // Once the user has moved farther than tolerance from the original location
482                 // (indicating they intend to move the object, not click), then always process the
483                 // motion notify coordinates as given (no snapping back to origin)
484                 within_tolerance = false;
486                 if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
487                     // if it's not click and ctrl or alt was pressed (the latter with some selection
488                     // but not with shift) we want to drag rather than rubberband
489                     sc->dragging = TRUE;
491                     sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
492                 }
494                 if (sc->dragging) {
495                     /* User has dragged fast, so we get events on root (lauris)*/
496                     // not only that; we will end up here when ctrl-dragging as well
497                     // and also when we started within tolerance, but trespassed tolerance outside of item
498                     Inkscape::Rubberband::get(desktop)->stop();
499                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
500                     item_at_point = desktop->item_at_point(NR::Point(event->button.x, event->button.y), FALSE);
501                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
502                         item_at_point = desktop->item_at_point(NR::Point(xp, yp), FALSE);
503                     if (item_at_point || sc->moved || sc->button_press_alt) {
504                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
505                         if (!sc->moved) {
506                             item_in_group = desktop->item_at_point(NR::Point(event->button.x, event->button.y), TRUE);
507                             group_at_point = desktop->group_at_point(NR::Point(event->button.x, event->button.y));
509                             // group-at-point is meant to be topmost item if it's a group, 
510                             // not topmost group of all items at point
511                             if (group_at_point != item_in_group && 
512                                 !(group_at_point && item_at_point && 
513                                   group_at_point->isAncestorOf(item_at_point)))
514                                 group_at_point = NULL;
516                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
517                             if ((!item_in_group || !selection->includes(item_in_group)) &&
518                                 (!group_at_point || !selection->includes(group_at_point))
519                                 && !sc->button_press_alt) {
520                                 // select what is under cursor
521                                 if (!seltrans->isEmpty()) {
522                                     seltrans->resetState();
523                                 }
524                                 // when simply ctrl-dragging, we don't want to go into groups
525                                 if (item_at_point && !selection->includes(item_at_point))
526                                     selection->set(item_at_point);
527                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
528                             seltrans->grab(p, -1, -1, FALSE);
529                             sc->moved = TRUE;
530                         }
531                         if (!seltrans->isEmpty())
532                             seltrans->moveTo(p, event->button.state);
533                         desktop->scroll_to_point(p);
534                         gobble_motion_events(GDK_BUTTON1_MASK);
535                         ret = TRUE;
536                     } else {
537                         sc->dragging = FALSE;
539                         sp_canvas_end_forced_full_redraws(desktop->canvas);
540                     }
541                 } else {
542                     if (Inkscape::Rubberband::get(desktop)->is_started()) {
543                         Inkscape::Rubberband::get(desktop)->move(p);
544                         if (Inkscape::Rubberband::get(desktop)->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
545                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
546                         } else {
547                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
548                         }
549                         gobble_motion_events(GDK_BUTTON1_MASK);
550                     }
551                 }
552             }
553             break;
554         case GDK_BUTTON_RELEASE:
555             xp = yp = 0;
556             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
557                 if (sc->dragging) {
558                     if (sc->moved) {
559                         // item has been moved
560                         seltrans->ungrab();
561                         sc->moved = FALSE;
562                     } else if (sc->item && !drag_escaped) {
563                         // item has not been moved -> simply a click, do selecting
564                         if (!selection->isEmpty()) {
565                             if (event->button.state & GDK_SHIFT_MASK) {
566                                 // with shift, toggle selection
567                                 seltrans->resetState();
568                                 selection->toggle(sc->item);
569                             } else {
570                                 // without shift, increase state (i.e. toggle scale/rotation handles)
571                                 if (selection->includes(sc->item)) {
572                                     seltrans->increaseState();
573                                 } else {
574                                     seltrans->resetState();
575                                     selection->set(sc->item);
576                                 }
577                             }
578                         } else { // simple or shift click, no previous selection
579                             seltrans->resetState();
580                             selection->set(sc->item);
581                         }
582                     }
583                     sc->dragging = FALSE;
585                     sp_canvas_end_forced_full_redraws(desktop->canvas);
587                     if (sc->item) {
588                         sp_object_unref( SP_OBJECT(sc->item), NULL);
589                     }
590                     sc->item = NULL;
591                 } else {
592                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
593                     if (r->is_started() && !within_tolerance) {
594                         // this was a rubberband drag
595                         GSList *items = NULL;
596                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
597                             boost::optional<NR::Rect> const b = r->getRectangle();
598                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
599                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
600                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
601                         }
603                         seltrans->resetState();
604                         r->stop();
605                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
607                         if (event->button.state & GDK_SHIFT_MASK) {
608                             // with shift, add to selection
609                             selection->addList (items);
610                         } else {
611                             // without shift, simply select anew
612                             selection->setList (items);
613                         }
614                         g_slist_free (items);
615                     } else { // it was just a click, or a too small rubberband
616                         r->stop();
617                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
618                             // this was a shift+click or alt+shift+click, select what was clicked upon
620                             sc->button_press_shift = false;
622                             if (sc->button_press_ctrl) {
623                                 // go into groups, honoring Alt
624                                 item = sp_event_context_find_item (desktop,
625                                                    NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
626                                 sc->button_press_ctrl = FALSE;
627                             } else {
628                                 // don't go into groups, honoring Alt
629                                 item = sp_event_context_find_item (desktop,
630                                                    NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
631                             }
633                             if (item) {
634                                 selection->toggle(item);
635                                 item = NULL;
636                             }
638                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
640                             item = sp_event_context_find_item (desktop,
641                                          NR::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
643                             sc->button_press_ctrl = FALSE;
644                             sc->button_press_alt = FALSE;
646                             if (item) {
647                                 if (selection->includes(item)) {
648                                     seltrans->increaseState();
649                                 } else {
650                                     seltrans->resetState();
651                                     selection->set(item);
652                                 }
653                                 item = NULL;
654                             }
656                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
657                             if (!selection->isEmpty()) {
658                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
659                                     selection->clear();
660                                 rb_escaped = 0;
661                                 ret = TRUE;
662                             }
663                         }
664                     }
665                     ret = TRUE;
666                 }
667                 if (sc->grabbed) {
668                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
669                     sc->grabbed = NULL;
670                 }
671                 
672                 desktop->updateNow();
673             }
674             if (event->button.button == 1) {
675                 Inkscape::Rubberband::get(desktop)->stop(); // might have been started in another tool!
676             }
677             sc->button_press_shift = false;
678             sc->button_press_ctrl = false;
679             sc->button_press_alt = false;
680             break;
682         case GDK_KEY_PRESS: // keybindings for select context
684             {
685             guint keyval = get_group0_keyval(&event->key);
686             bool alt = ( MOD__ALT
687                                     || (keyval == GDK_Alt_L)
688                                     || (keyval == GDK_Alt_R)
689                                     || (keyval == GDK_Meta_L)
690                                     || (keyval == GDK_Meta_R));
692             if (!key_is_a_modifier (keyval)) {
693                     event_context->defaultMessageContext()->clear();
694             } else if (sc->grabbed || seltrans->isGrabbed()) {
695                 if (Inkscape::Rubberband::get(desktop)->is_started()) {
696                     // if Alt then change cursor to moving cursor:
697                     if (alt) {
698                         Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
699                     }
700                 } else {
701                 // do not change the statusbar text when mousekey is down to move or transform the object,
702                 // because the statusbar text is already updated somewhere else.
703                    break;
704                 }
705             } else {
706                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
707                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
708                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
709                                                 _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
710                     // if Alt and nonempty selection, show moving cursor ("move selected"):
711                     if (alt && !selection->isEmpty()) {
712                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
713                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
714                         gdk_cursor_destroy(cursor);
715                     }
716                     //*/
717                     break;
718             }
719             }
721             switch (get_group0_keyval (&event->key)) {
722                 case GDK_Left: // move selection left
723                 case GDK_KP_Left:
724                 case GDK_KP_4:
725                     if (!MOD__CTRL) { // not ctrl
726                         gint mul = 1 + gobble_key_events(
727                             get_group0_keyval(&event->key), 0); // with any mask
728                         if (MOD__ALT) { // alt
729                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*-10, 0); // shift
730                             else sp_selection_move_screen(desktop, mul*-1, 0); // no shift
731                         }
732                         else { // no alt
733                             if (MOD__SHIFT) sp_selection_move(desktop, mul*-10*nudge, 0); // shift
734                             else sp_selection_move(desktop, mul*-nudge, 0); // no shift
735                         }
736                         ret = TRUE;
737                     }
738                     break;
739                 case GDK_Up: // move selection up
740                 case GDK_KP_Up:
741                 case GDK_KP_8:
742                     if (!MOD__CTRL) { // not ctrl
743                         gint mul = 1 + gobble_key_events(
744                             get_group0_keyval(&event->key), 0); // with any mask
745                         if (MOD__ALT) { // alt
746                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*10); // shift
747                             else sp_selection_move_screen(desktop, 0, mul*1); // no shift
748                         }
749                         else { // no alt
750                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*10*nudge); // shift
751                             else sp_selection_move(desktop, 0, mul*nudge); // no shift
752                         }
753                         ret = TRUE;
754                     }
755                     break;
756                 case GDK_Right: // move selection right
757                 case GDK_KP_Right:
758                 case GDK_KP_6:
759                     if (!MOD__CTRL) { // not ctrl
760                         gint mul = 1 + gobble_key_events(
761                             get_group0_keyval(&event->key), 0); // with any mask
762                         if (MOD__ALT) { // alt
763                             if (MOD__SHIFT) sp_selection_move_screen(desktop, mul*10, 0); // shift
764                             else sp_selection_move_screen(desktop, mul*1, 0); // no shift
765                         }
766                         else { // no alt
767                             if (MOD__SHIFT) sp_selection_move(desktop, mul*10*nudge, 0); // shift
768                             else sp_selection_move(desktop, mul*nudge, 0); // no shift
769                         }
770                         ret = TRUE;
771                     }
772                     break;
773                 case GDK_Down: // move selection down
774                 case GDK_KP_Down:
775                 case GDK_KP_2:
776                     if (!MOD__CTRL) { // not ctrl
777                         gint mul = 1 + gobble_key_events(
778                             get_group0_keyval(&event->key), 0); // with any mask
779                         if (MOD__ALT) { // alt
780                             if (MOD__SHIFT) sp_selection_move_screen(desktop, 0, mul*-10); // shift
781                             else sp_selection_move_screen(desktop, 0, mul*-1); // no shift
782                         }
783                         else { // no alt
784                             if (MOD__SHIFT) sp_selection_move(desktop, 0, mul*-10*nudge); // shift
785                             else sp_selection_move(desktop, 0, mul*-nudge); // no shift
786                         }
787                         ret = TRUE;
788                     }
789                     break;
790                 case GDK_Escape:
791                     if (!sp_select_context_abort(event_context))
792                         selection->clear();
793                     ret = TRUE;
794                     break;
795                 case GDK_a:
796                 case GDK_A:
797                     if (MOD__CTRL_ONLY) {
798                         sp_edit_select_all(desktop);
799                         ret = TRUE;
800                     }
801                     break;
802                 case GDK_space:
803                     /* stamping mode: show outline mode moving */
804                     /* FIXME: Is next condition ok? (lauris) */
805                     if (sc->dragging && sc->grabbed) {
806                         seltrans->stamp();
807                         ret = TRUE;
808                     }
809                     break;
810                 case GDK_x:
811                 case GDK_X:
812                     if (MOD__ALT_ONLY) {
813                         desktop->setToolboxFocusTo ("altx");
814                         ret = TRUE;
815                     }
816                     break;
817                 case GDK_bracketleft:
818                     if (MOD__ALT) {
819                         gint mul = 1 + gobble_key_events(
820                             get_group0_keyval(&event->key), 0); // with any mask
821                         sp_selection_rotate_screen(selection, mul*1);
822                     } else if (MOD__CTRL) {
823                         sp_selection_rotate(selection, 90);
824                     } else if (snaps) {
825                         sp_selection_rotate(selection, 180/snaps);
826                     }
827                     ret = TRUE;
828                     break;
829                 case GDK_bracketright:
830                     if (MOD__ALT) {
831                         gint mul = 1 + gobble_key_events(
832                             get_group0_keyval(&event->key), 0); // with any mask
833                         sp_selection_rotate_screen(selection, -1*mul);
834                     } else if (MOD__CTRL) {
835                         sp_selection_rotate(selection, -90);
836                     } else if (snaps) {
837                         sp_selection_rotate(selection, -180/snaps);
838                     }
839                     ret = TRUE;
840                     break;
841                 case GDK_less:
842                 case GDK_comma:
843                     if (MOD__ALT) {
844                         gint mul = 1 + gobble_key_events(
845                             get_group0_keyval(&event->key), 0); // with any mask
846                         sp_selection_scale_screen(selection, -2*mul);
847                     } else if (MOD__CTRL) {
848                         sp_selection_scale_times(selection, 0.5);
849                     } else {
850                         gint mul = 1 + gobble_key_events(
851                             get_group0_keyval(&event->key), 0); // with any mask
852                         sp_selection_scale(selection, -offset*mul);
853                     }
854                     ret = TRUE;
855                     break;
856                 case GDK_greater:
857                 case GDK_period:
858                     if (MOD__ALT) {
859                         gint mul = 1 + gobble_key_events(
860                             get_group0_keyval(&event->key), 0); // with any mask
861                         sp_selection_scale_screen(selection, 2*mul);
862                     } else if (MOD__CTRL) {
863                         sp_selection_scale_times(selection, 2);
864                     } else {
865                         gint mul = 1 + gobble_key_events(
866                             get_group0_keyval(&event->key), 0); // with any mask
867                         sp_selection_scale(selection, offset*mul);
868                     }
869                     ret = TRUE;
870                     break;
871                 case GDK_Return:
872                     if (MOD__CTRL_ONLY) {
873                         if (selection->singleItem()) {
874                             SPItem *clicked_item = selection->singleItem();
875                             if ( SP_IS_GROUP(clicked_item) ||
876                                  SP_IS_BOX3D(clicked_item)) { // enter group or a 3D box
877                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
878                                 sp_desktop_selection(desktop)->clear();
879                             } else {
880                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
881                             }
882                         }
883                         ret = TRUE;
884                     }
885                     break;
886                 case GDK_BackSpace:
887                     if (MOD__CTRL_ONLY) {
888                         sp_select_context_up_one_layer(desktop);
889                         ret = TRUE;
890                     }
891                     break;
892                 case GDK_s:
893                 case GDK_S:
894                     if (MOD__SHIFT_ONLY) {
895                         if (!selection->isEmpty()) {
896                             seltrans->increaseState();
897                         }
898                         ret = TRUE;
899                     }
900                     break;                  
901                 case GDK_g:
902                 case GDK_G:
903                     if (MOD__SHIFT_ONLY) {
904                         sp_selection_to_guides(desktop);
905                         ret = true;
906                     }
907                     break;
908                 default:
909                     break;
910             }
911             break;
913         case GDK_KEY_RELEASE:
914             {
915             guint keyval = get_group0_keyval(&event->key);
916             if (key_is_a_modifier (keyval))
917                 event_context->defaultMessageContext()->clear();
919             bool alt = ( MOD__ALT
920                          || (keyval == GDK_Alt_L)
921                          || (keyval == GDK_Alt_R)
922                          || (keyval == GDK_Meta_L)
923                          || (keyval == GDK_Meta_R));
925             if (Inkscape::Rubberband::get(desktop)->is_started()) {
926                 // if Alt then change cursor to moving cursor:
927                 if (alt) {
928                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
929                 }
930             }
931             }
932             // set cursor to default.
933             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
934             break;
935         default:
936             break;
937     }
939     if (!ret) {
940         if (((SPEventContextClass *) parent_class)->root_handler)
941             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
942     }
944     return ret;
948 /*
949   Local Variables:
950   mode:c++
951   c-file-style:"stroustrup"
952   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
953   indent-tabs-mode:nil
954   fill-column:99
955   End:
956 */
957 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :