Code

Applying fixes for gcc 4.3 build issues (closes LP: #169115)
[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()->is_started()) {
235             Inkscape::Rubberband::get()->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,
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()->setMode(RUBBERBAND_MODE_TOUCHPATH);
445                 Inkscape::Rubberband::get()->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_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()->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));
508                             // if neither a group nor an item (possibly in a group) at point are selected, set selection to the item at point
509                             if ((!item_in_group || !selection->includes(item_in_group)) &&
510                                 (!group_at_point || !selection->includes(group_at_point))
511                                 && !sc->button_press_alt) {
512                                 // select what is under cursor
513                                 if (!seltrans->isEmpty()) {
514                                     seltrans->resetState();
515                                 }
516                                 // when simply ctrl-dragging, we don't want to go into groups
517                                 if (item_at_point && !selection->includes(item_at_point))
518                                     selection->set(item_at_point);
519                             } // otherwise, do not change selection so that dragging selected-within-group items, as well as alt-dragging, is possible
520                             seltrans->grab(p, -1, -1, FALSE);
521                             sc->moved = TRUE;
522                         }
523                         if (!seltrans->isEmpty())
524                             seltrans->moveTo(p, event->button.state);
525                         desktop->scroll_to_point(&p);
526                         gobble_motion_events(GDK_BUTTON1_MASK);
527                         ret = TRUE;
528                     } else {
529                         sc->dragging = FALSE;
531                         sp_canvas_end_forced_full_redraws(desktop->canvas);
532                     }
533                 } else {
534                     if (Inkscape::Rubberband::get()->is_started()) {
535                         Inkscape::Rubberband::get()->move(p);
536                         if (Inkscape::Rubberband::get()->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
537                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
538                         } else {
539                             event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
540                         }
541                         gobble_motion_events(GDK_BUTTON1_MASK);
542                     }
543                 }
544             }
545             break;
546         case GDK_BUTTON_RELEASE:
547             xp = yp = 0;
548             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
549                 if (sc->dragging) {
550                     if (sc->moved) {
551                         // item has been moved
552                         seltrans->ungrab();
553                         sc->moved = FALSE;
554                     } else if (sc->item && !drag_escaped) {
555                         // item has not been moved -> simply a click, do selecting
556                         if (!selection->isEmpty()) {
557                             if (event->button.state & GDK_SHIFT_MASK) {
558                                 // with shift, toggle selection
559                                 seltrans->resetState();
560                                 selection->toggle(sc->item);
561                             } else {
562                                 // without shift, increase state (i.e. toggle scale/rotation handles)
563                                 if (selection->includes(sc->item)) {
564                                     seltrans->increaseState();
565                                 } else {
566                                     seltrans->resetState();
567                                     selection->set(sc->item);
568                                 }
569                             }
570                         } else { // simple or shift click, no previous selection
571                             seltrans->resetState();
572                             selection->set(sc->item);
573                         }
574                     }
575                     sc->dragging = FALSE;
577                     sp_canvas_end_forced_full_redraws(desktop->canvas);
579                     if (sc->item) {
580                         sp_object_unref( SP_OBJECT(sc->item), NULL);
581                     }
582                     sc->item = NULL;
583                 } else {
584                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
585                     if (r->is_started() && !within_tolerance) {
586                         // this was a rubberband drag
587                         GSList *items = NULL;
588                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
589                             NR::Maybe<NR::Rect> const b = r->getRectangle();
590                             items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
591                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
592                             items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
593                         }
595                         seltrans->resetState();
596                         r->stop();
597                         SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
599                         if (event->button.state & GDK_SHIFT_MASK) {
600                             // with shift, add to selection
601                             selection->addList (items);
602                         } else {
603                             // without shift, simply select anew
604                             selection->setList (items);
605                         }
606                         g_slist_free (items);
607                     } else { // it was just a click, or a too small rubberband
608                         r->stop();
609                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
610                             // this was a shift+click or alt+shift+click, select what was clicked upon
612                             sc->button_press_shift = false;
614                             if (sc->button_press_ctrl) {
615                                 // go into groups, honoring Alt
616                                 item = sp_event_context_find_item (desktop,
617                                                    NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
618                                 sc->button_press_ctrl = FALSE;
619                             } else {
620                                 // don't go into groups, honoring Alt
621                                 item = sp_event_context_find_item (desktop,
622                                                    NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
623                             }
625                             if (item) {
626                                 selection->toggle(item);
627                                 item = NULL;
628                             }
630                         } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
632                             item = sp_event_context_find_item (desktop,
633                                          NR::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
635                             sc->button_press_ctrl = FALSE;
636                             sc->button_press_alt = FALSE;
638                             if (item) {
639                                 if (selection->includes(item)) {
640                                     seltrans->increaseState();
641                                 } else {
642                                     seltrans->resetState();
643                                     selection->set(item);
644                                 }
645                                 item = NULL;
646                             }
648                         } else { // click without shift, simply deselect, unless with Alt or something was cancelled
649                             if (!selection->isEmpty()) {
650                                 if (!(rb_escaped) && !(drag_escaped) && !(event->button.state & GDK_MOD1_MASK))
651                                     selection->clear();
652                                 rb_escaped = 0;
653                                 ret = TRUE;
654                             }
655                         }
656                     }
657                     ret = TRUE;
658                 }
659                 if (sc->grabbed) {
660                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
661                     sc->grabbed = NULL;
662                 }
663                 
664                 desktop->updateNow();
665             }
666             if (event->button.button == 1) {
667                 Inkscape::Rubberband::get()->stop(); // might have been started in another tool!
668             }
669             sc->button_press_shift = false;
670             sc->button_press_ctrl = false;
671             sc->button_press_alt = false;
672             break;
674         case GDK_KEY_PRESS: // keybindings for select context
676             {
677             guint keyval = get_group0_keyval(&event->key);
678             bool alt = ( MOD__ALT
679                                     || (keyval == GDK_Alt_L)
680                                     || (keyval == GDK_Alt_R)
681                                     || (keyval == GDK_Meta_L)
682                                     || (keyval == GDK_Meta_R));
684             if (!key_is_a_modifier (keyval)) {
685                     event_context->defaultMessageContext()->clear();
686             } else if (sc->grabbed || seltrans->isGrabbed()) {
687                 if (Inkscape::Rubberband::get()->is_started()) {
688                     // if Alt then change cursor to moving cursor:
689                     if (alt) {
690                         Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
691                     }
692                 } else {
693                 // do not change the statusbar text when mousekey is down to move or transform the object,
694                 // because the statusbar text is already updated somewhere else.
695                    break;
696                 }
697             } else {
698                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
699                                                 _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
700                                                 _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
701                                                 _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
702                     // if Alt and nonempty selection, show moving cursor ("move selected"):
703                     if (alt && !selection->isEmpty()) {
704                         GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
705                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
706                         gdk_cursor_destroy(cursor);
707                     }
708                     //*/
709                     break;
710             }
711             }
713             switch (get_group0_keyval (&event->key)) {
714                 case GDK_Left: // move selection left
715                 case GDK_KP_Left:
716                 case GDK_KP_4:
717                     if (!MOD__CTRL) { // not ctrl
718                         gint mul = 1 + gobble_key_events(
719                             get_group0_keyval(&event->key), 0); // with any mask
720                         if (MOD__ALT) { // alt
721                             if (MOD__SHIFT) sp_selection_move_screen(mul*-10, 0); // shift
722                             else sp_selection_move_screen(mul*-1, 0); // no shift
723                         }
724                         else { // no alt
725                             if (MOD__SHIFT) sp_selection_move(mul*-10*nudge, 0); // shift
726                             else sp_selection_move(mul*-nudge, 0); // no shift
727                         }
728                         ret = TRUE;
729                     }
730                     break;
731                 case GDK_Up: // move selection up
732                 case GDK_KP_Up:
733                 case GDK_KP_8:
734                     if (!MOD__CTRL) { // not ctrl
735                         gint mul = 1 + gobble_key_events(
736                             get_group0_keyval(&event->key), 0); // with any mask
737                         if (MOD__ALT) { // alt
738                             if (MOD__SHIFT) sp_selection_move_screen(0, mul*10); // shift
739                             else sp_selection_move_screen(0, mul*1); // no shift
740                         }
741                         else { // no alt
742                             if (MOD__SHIFT) sp_selection_move(0, mul*10*nudge); // shift
743                             else sp_selection_move(0, mul*nudge); // no shift
744                         }
745                         ret = TRUE;
746                     }
747                     break;
748                 case GDK_Right: // move selection right
749                 case GDK_KP_Right:
750                 case GDK_KP_6:
751                     if (!MOD__CTRL) { // not ctrl
752                         gint mul = 1 + gobble_key_events(
753                             get_group0_keyval(&event->key), 0); // with any mask
754                         if (MOD__ALT) { // alt
755                             if (MOD__SHIFT) sp_selection_move_screen(mul*10, 0); // shift
756                             else sp_selection_move_screen(mul*1, 0); // no shift
757                         }
758                         else { // no alt
759                             if (MOD__SHIFT) sp_selection_move(mul*10*nudge, 0); // shift
760                             else sp_selection_move(mul*nudge, 0); // no shift
761                         }
762                         ret = TRUE;
763                     }
764                     break;
765                 case GDK_Down: // move selection down
766                 case GDK_KP_Down:
767                 case GDK_KP_2:
768                     if (!MOD__CTRL) { // not ctrl
769                         gint mul = 1 + gobble_key_events(
770                             get_group0_keyval(&event->key), 0); // with any mask
771                         if (MOD__ALT) { // alt
772                             if (MOD__SHIFT) sp_selection_move_screen(0, mul*-10); // shift
773                             else sp_selection_move_screen(0, mul*-1); // no shift
774                         }
775                         else { // no alt
776                             if (MOD__SHIFT) sp_selection_move(0, mul*-10*nudge); // shift
777                             else sp_selection_move(0, mul*-nudge); // no shift
778                         }
779                         ret = TRUE;
780                     }
781                     break;
782                 case GDK_Escape:
783                     if (!sp_select_context_abort(event_context))
784                         selection->clear();
785                     ret = TRUE;
786                     break;
787                 case GDK_a:
788                 case GDK_A:
789                     if (MOD__CTRL_ONLY) {
790                         sp_edit_select_all();
791                         ret = TRUE;
792                     }
793                     break;
794                 case GDK_space:
795                     /* stamping mode: show outline mode moving */
796                     /* FIXME: Is next condition ok? (lauris) */
797                     if (sc->dragging && sc->grabbed) {
798                         seltrans->stamp();
799                         ret = TRUE;
800                     }
801                     break;
802                 case GDK_x:
803                 case GDK_X:
804                     if (MOD__ALT_ONLY) {
805                         desktop->setToolboxFocusTo ("altx");
806                         ret = TRUE;
807                     }
808                     break;
809                 case GDK_bracketleft:
810                     if (MOD__ALT) {
811                         gint mul = 1 + gobble_key_events(
812                             get_group0_keyval(&event->key), 0); // with any mask
813                         sp_selection_rotate_screen(selection, mul*1);
814                     } else if (MOD__CTRL) {
815                         sp_selection_rotate(selection, 90);
816                     } else if (snaps) {
817                         sp_selection_rotate(selection, 180/snaps);
818                     }
819                     ret = TRUE;
820                     break;
821                 case GDK_bracketright:
822                     if (MOD__ALT) {
823                         gint mul = 1 + gobble_key_events(
824                             get_group0_keyval(&event->key), 0); // with any mask
825                         sp_selection_rotate_screen(selection, -1*mul);
826                     } else if (MOD__CTRL) {
827                         sp_selection_rotate(selection, -90);
828                     } else if (snaps) {
829                         sp_selection_rotate(selection, -180/snaps);
830                     }
831                     ret = TRUE;
832                     break;
833                 case GDK_less:
834                 case GDK_comma:
835                     if (MOD__ALT) {
836                         gint mul = 1 + gobble_key_events(
837                             get_group0_keyval(&event->key), 0); // with any mask
838                         sp_selection_scale_screen(selection, -2*mul);
839                     } else if (MOD__CTRL) {
840                         sp_selection_scale_times(selection, 0.5);
841                     } else {
842                         gint mul = 1 + gobble_key_events(
843                             get_group0_keyval(&event->key), 0); // with any mask
844                         sp_selection_scale(selection, -offset*mul);
845                     }
846                     ret = TRUE;
847                     break;
848                 case GDK_greater:
849                 case GDK_period:
850                     if (MOD__ALT) {
851                         gint mul = 1 + gobble_key_events(
852                             get_group0_keyval(&event->key), 0); // with any mask
853                         sp_selection_scale_screen(selection, 2*mul);
854                     } else if (MOD__CTRL) {
855                         sp_selection_scale_times(selection, 2);
856                     } else {
857                         gint mul = 1 + gobble_key_events(
858                             get_group0_keyval(&event->key), 0); // with any mask
859                         sp_selection_scale(selection, offset*mul);
860                     }
861                     ret = TRUE;
862                     break;
863                 case GDK_Return:
864                     if (MOD__CTRL_ONLY) {
865                         if (selection->singleItem()) {
866                             SPItem *clicked_item = selection->singleItem();
867                             if ( SP_IS_GROUP(clicked_item) &&
868                                 !SP_IS_BOX3D(clicked_item)) { // enter group if it's not a 3D box
869                                 desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
870                                 sp_desktop_selection(desktop)->clear();
871                             } else {
872                                 SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selected object is not a group. Cannot enter."));
873                             }
874                         }
875                         ret = TRUE;
876                     }
877                     break;
878                 case GDK_BackSpace:
879                     if (MOD__CTRL_ONLY) {
880                         sp_select_context_up_one_layer(desktop);
881                         ret = TRUE;
882                     }
883                     break;
884                 case GDK_s:
885                 case GDK_S:
886                     if (MOD__SHIFT_ONLY) {
887                         if (!selection->isEmpty()) {
888                             seltrans->increaseState();
889                         }
890                         ret = TRUE;
891                     }
892                     break;                  
893                 case GDK_g:
894                 case GDK_G:
895                     if (MOD__SHIFT_ONLY) {
896                         sp_selection_to_guides();
897                         ret = true;
898                     }
899                     break;
900                 default:
901                     break;
902             }
903             break;
905         case GDK_KEY_RELEASE:
906             {
907             guint keyval = get_group0_keyval(&event->key);
908             if (key_is_a_modifier (keyval))
909                 event_context->defaultMessageContext()->clear();
911             bool alt = ( MOD__ALT
912                          || (keyval == GDK_Alt_L)
913                          || (keyval == GDK_Alt_R)
914                          || (keyval == GDK_Meta_L)
915                          || (keyval == GDK_Meta_R));
917             if (Inkscape::Rubberband::get()->is_started()) {
918                 // if Alt then change cursor to moving cursor:
919                 if (alt) {
920                     Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_RECT);
921                 }
922             }
923             }
924             // set cursor to default.
925             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
926             break;
927         default:
928             break;
929     }
931     if (!ret) {
932         if (((SPEventContextClass *) parent_class)->root_handler)
933             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
934     }
936     return ret;
940 /*
941   Local Variables:
942   mode:c++
943   c-file-style:"stroustrup"
944   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
945   indent-tabs-mode:nil
946   fill-column:99
947   End:
948 */
949 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :