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