1 #define __SELTRANS_C__
3 /*
4 * Helper object for transforming selected items
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 * Carl Hetherington <inkscape@carlh.net>
10 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
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
20 #include <libnr/nr-matrix-ops.h>
21 #include <libnr/nr-matrix-translate-ops.h>
22 #include <libnr/nr-rotate-ops.h>
23 #include <libnr/nr-scale-ops.h>
24 #include <libnr/nr-translate-matrix-ops.h>
25 #include <libnr/nr-translate-ops.h>
26 #include <gdk/gdkkeysyms.h>
27 #include "document.h"
28 #include "sp-namedview.h"
29 #include "desktop.h"
30 #include "desktop-handles.h"
31 #include "desktop-style.h"
32 #include "knot.h"
33 #include "snap.h"
34 #include "selection.h"
35 #include "select-context.h"
36 #include "sp-item.h"
37 #include "sp-item-transform.h"
38 #include "seltrans-handles.h"
39 #include "seltrans.h"
40 #include "selection-chemistry.h"
41 #include "sp-metrics.h"
42 #include <glibmm/i18n.h>
43 #include "display/sp-ctrlline.h"
44 #include "prefs-utils.h"
45 #include "xml/repr.h"
47 #include "isnan.h" //temp fix. make sure included last
49 static void sp_remove_handles(SPKnot *knot[], gint num);
51 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data);
52 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data);
53 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data);
54 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint32 state, gpointer data);
55 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *p, guint state, gboolean *data);
57 extern GdkPixbuf *handles[];
59 static gboolean sp_seltrans_handle_event(SPKnot *knot, GdkEvent *event, gpointer)
60 {
61 switch (event->type) {
62 case GDK_MOTION_NOTIFY:
63 break;
64 case GDK_KEY_PRESS:
65 if (get_group0_keyval (&event->key) == GDK_space) {
66 /* stamping mode: both mode(show content and outline) operation with knot */
67 if (!SP_KNOT_IS_GRABBED(knot)) {
68 return FALSE;
69 }
70 SPDesktop *desktop = knot->desktop;
71 Inkscape::SelTrans *seltrans = SP_SELECT_CONTEXT(desktop->event_context)->_seltrans;
72 seltrans->stamp();
73 return TRUE;
74 }
75 break;
76 default:
77 break;
78 }
80 return FALSE;
81 }
83 Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
84 _desktop(desktop),
85 _selcue(desktop),
86 _state(STATE_SCALE),
87 _show(SHOW_CONTENT),
88 _grabbed(false),
89 _show_handles(true),
90 _box(NR::Point(0,0), NR::Point(0,0)),
91 _chandle(NULL),
92 _stamp_cache(NULL),
93 _message_context(desktop->messageStack())
94 {
95 g_return_if_fail(desktop != NULL);
97 for (int i = 0; i < 8; i++) {
98 _shandle[i] = NULL;
99 _rhandle[i] = NULL;
100 }
102 _updateVolatileState();
104 _updateHandles();
106 _selection = SP_DT_SELECTION(desktop);
108 _norm = sp_canvas_item_new(SP_DT_CONTROLS(desktop),
109 SP_TYPE_CTRL,
110 "anchor", GTK_ANCHOR_CENTER,
111 "mode", SP_CTRL_MODE_COLOR,
112 "shape", SP_CTRL_SHAPE_BITMAP,
113 "size", 13.0,
114 "filled", TRUE,
115 "fill_color", 0x00000000,
116 "stroked", TRUE,
117 "stroke_color", 0x000000a0,
118 "pixbuf", handles[12],
119 NULL);
121 _grip = sp_canvas_item_new(SP_DT_CONTROLS(desktop),
122 SP_TYPE_CTRL,
123 "anchor", GTK_ANCHOR_CENTER,
124 "mode", SP_CTRL_MODE_XOR,
125 "shape", SP_CTRL_SHAPE_CROSS,
126 "size", 7.0,
127 "filled", TRUE,
128 "fill_color", 0xffffff7f,
129 "stroked", TRUE,
130 "stroke_color", 0xffffffff,
131 "pixbuf", handles[12],
132 NULL);
134 sp_canvas_item_hide(_grip);
135 sp_canvas_item_hide(_norm);
137 for (int i = 0; i < 4; i++) {
138 _l[i] = sp_canvas_item_new(SP_DT_CONTROLS(desktop), SP_TYPE_CTRLLINE, NULL);
139 sp_canvas_item_hide(_l[i]);
140 }
142 _sel_changed_connection = _selection->connectChanged(
143 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
144 );
146 _sel_modified_connection = _selection->connectModified(
147 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
148 );
149 }
151 Inkscape::SelTrans::~SelTrans()
152 {
153 _sel_changed_connection.disconnect();
154 _sel_modified_connection.disconnect();
156 for (unsigned int i = 0; i < 8; i++) {
157 if (_shandle[i]) {
158 g_object_unref(G_OBJECT(_shandle[i]));
159 _shandle[i] = NULL;
160 }
161 if (_rhandle[i]) {
162 g_object_unref(G_OBJECT(_rhandle[i]));
163 _rhandle[i] = NULL;
164 }
165 }
166 if (_chandle) {
167 g_object_unref(G_OBJECT(_chandle));
168 _chandle = NULL;
169 }
171 if (_norm) {
172 gtk_object_destroy(GTK_OBJECT(_norm));
173 _norm = NULL;
174 }
175 if (_grip) {
176 gtk_object_destroy(GTK_OBJECT(_grip));
177 _grip = NULL;
178 }
179 for (int i = 0; i < 4; i++) {
180 if (_l[i]) {
181 gtk_object_destroy(GTK_OBJECT(_l[i]));
182 _l[i] = NULL;
183 }
184 }
186 for (unsigned i = 0; i < _items.size(); i++) {
187 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
188 }
190 _items.clear();
191 }
193 void Inkscape::SelTrans::resetState()
194 {
195 _state = STATE_SCALE;
196 }
198 void Inkscape::SelTrans::increaseState()
199 {
200 if (_state == STATE_SCALE) {
201 _state = STATE_ROTATE;
202 } else {
203 _state = STATE_SCALE;
204 }
206 _updateHandles();
207 }
209 void Inkscape::SelTrans::setCenter(NR::Point const &p)
210 {
211 _center = p;
213 // Write the new center position into all selected items
214 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
215 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
216 it->setCenter(p);
217 SP_OBJECT(it)->updateRepr();
218 }
219 sp_document_maybe_done (SP_DT_DOCUMENT(_desktop), "center::move");
221 _updateHandles();
222 }
224 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
225 {
226 Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
228 g_return_if_fail(!_grabbed);
230 _grabbed = true;
231 _show_handles = show_handles;
232 _updateVolatileState();
234 _changed = false;
236 if (_empty) {
237 return;
238 }
240 for (GSList const *l = selection->itemList(); l; l = l->next) {
241 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
242 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
243 }
245 _current.set_identity();
247 _point = p;
249 _snap_points = selection->getSnapPoints();
250 _bbox_points = selection->getBBoxPoints();
252 gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
253 bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
254 NR::Rect op_box = _box;
255 if (origin_on_bbox == false && _snap_points.empty() == false) {
256 std::vector<NR::Point>::iterator i = _snap_points.begin();
257 op_box = NR::Rect(*i, *i);
258 i++;
259 while (i != _snap_points.end()) {
260 op_box.expandTo(*i);
261 i++;
262 }
263 }
265 _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
267 if ((x != -1) && (y != -1)) {
268 sp_canvas_item_show(_norm);
269 sp_canvas_item_show(_grip);
270 }
272 if (_show == SHOW_OUTLINE) {
273 for (int i = 0; i < 4; i++)
274 sp_canvas_item_show(_l[i]);
275 }
278 _updateHandles();
279 g_return_if_fail(_stamp_cache == NULL);
280 }
282 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
283 {
284 g_return_if_fail(_grabbed);
285 g_return_if_fail(!_empty);
287 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
289 if (_show == SHOW_CONTENT) {
290 // update the content
291 for (unsigned i = 0; i < _items.size(); i++) {
292 SPItem &item = *_items[i].first;
293 NR::Matrix const &prev_transform = _items[i].second;
294 sp_item_set_i2d_affine(&item, prev_transform * affine);
295 }
296 } else {
297 NR::Point p[4];
298 /* update the outline */
299 for (unsigned i = 0 ; i < 4 ; i++) {
300 p[i] = _box.corner(i) * affine;
301 }
302 for (unsigned i = 0 ; i < 4 ; i++) {
303 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
304 }
305 }
307 _current = affine;
308 _changed = true;
309 _updateHandles();
310 }
312 void Inkscape::SelTrans::ungrab()
313 {
314 g_return_if_fail(_grabbed);
316 Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
317 bool updh = true;
318 if (!_empty && _changed) {
319 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
320 _center *= _current;
322 // Transform may have changed the objects' bboxes, so we need to write the _center into them again
323 for (unsigned i = 0; i < _items.size(); i++) {
324 SPItem *currentItem = _items[i].first;
325 if (currentItem->isCenterSet() || _current[1] != 0 || _current[2] != 0) { // only if it's already set, or if it's a rotation/skew
326 currentItem->setCenter (_center);
327 SP_OBJECT(currentItem)->updateRepr();
328 }
329 }
331 sp_document_done(SP_DT_DOCUMENT(_desktop));
332 updh = false;
333 }
335 for (unsigned i = 0; i < _items.size(); i++) {
336 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
337 }
338 _items.clear();
340 _grabbed = false;
341 _show_handles = true;
343 sp_canvas_item_hide(_norm);
344 sp_canvas_item_hide(_grip);
346 if (_show == SHOW_OUTLINE) {
347 for (int i = 0; i < 4; i++)
348 sp_canvas_item_hide(_l[i]);
349 }
351 _updateVolatileState();
352 if (updh) {
353 _updateHandles();
354 }
355 if (_stamp_cache) {
356 g_slist_free(_stamp_cache);
357 _stamp_cache = NULL;
358 }
360 _message_context.clear();
361 }
363 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
364 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
366 void Inkscape::SelTrans::stamp()
367 {
368 Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
370 /* stamping mode */
371 if (!_empty) {
372 GSList *l;
373 if (_stamp_cache) {
374 l = _stamp_cache;
375 } else {
376 /* Build cache */
377 l = g_slist_copy((GSList *) selection->itemList());
378 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
379 _stamp_cache = l;
380 }
382 while (l) {
383 SPItem *original_item = SP_ITEM(l->data);
384 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
386 // remember the position of the item
387 gint pos = original_repr->position();
388 // remember parent
389 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
391 Inkscape::XML::Node *copy_repr = original_repr->duplicate();
393 // add the new repr to the parent
394 parent->appendChild(copy_repr);
395 // move to the saved position
396 copy_repr->setPosition(pos > 0 ? pos : 0);
398 SPItem *copy_item = (SPItem *) SP_DT_DOCUMENT(_desktop)->getObjectByRepr(copy_repr);
400 NR::Matrix const *new_affine;
401 if (_show == SHOW_OUTLINE) {
402 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
403 NR::Matrix const i2dnew( i2d * _current );
404 sp_item_set_i2d_affine(copy_item, i2dnew);
405 new_affine = ©_item->transform;
406 } else {
407 new_affine = &original_item->transform;
408 }
410 sp_item_write_transform(copy_item, copy_repr, *new_affine);
412 Inkscape::GC::release(copy_repr);
413 l = l->next;
414 }
415 sp_document_done(SP_DT_DOCUMENT(_desktop));
416 }
417 }
419 void Inkscape::SelTrans::_updateHandles()
420 {
421 if ( !_show_handles || _empty )
422 {
423 sp_remove_handles(_shandle, 8);
424 sp_remove_handles(_rhandle, 8);
425 sp_remove_handles(&_chandle, 1);
426 return;
427 }
429 // center handle
430 if ( _chandle == NULL ) {
431 _chandle = sp_knot_new(_desktop);
432 g_object_set(G_OBJECT(_chandle),
433 "anchor", handle_center.anchor,
434 "shape", SP_CTRL_SHAPE_BITMAP,
435 "size", 13,
436 "mode", SP_CTRL_MODE_XOR,
437 "fill", 0x00000000,
438 "fill_mouseover", 0x00000000,
439 "stroke", 0x000000ff,
440 "stroke_mouseover", 0xff0000b0,
441 "pixbuf", handles[handle_center.control],
442 "tip", _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"),
443 NULL);
444 g_signal_connect(G_OBJECT(_chandle), "request",
445 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
446 g_signal_connect(G_OBJECT(_chandle), "moved",
447 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
448 g_signal_connect(G_OBJECT(_chandle), "grabbed",
449 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
450 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
451 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
452 g_signal_connect(G_OBJECT(_chandle), "clicked",
453 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
454 }
456 sp_remove_handles(&_chandle, 1);
457 if ( _state == STATE_SCALE ) {
458 sp_remove_handles(_rhandle, 8);
459 _showHandles(_shandle, handles_scale, 8,
460 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
461 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
462 } else {
463 sp_remove_handles(_shandle, 8);
464 _showHandles(_rhandle, handles_rotate, 8,
465 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
466 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
467 }
469 // Extract the position of the center from the first selected object
470 GSList *items = (GSList *) _desktop->selection->itemList();
471 if (items) {
472 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
473 if (first->isCenterSet()) { // only if set explicitly
474 _center = first->getCenter();
475 } else {
476 _center = _box.midpoint();
477 }
478 } else {
479 _center = _box.midpoint();
480 }
482 if ( _state == STATE_SCALE ) {
483 sp_knot_hide(_chandle);
484 } else {
485 sp_knot_show(_chandle);
486 sp_knot_moveto(_chandle, &_center);
487 }
488 }
490 void Inkscape::SelTrans::_updateVolatileState()
491 {
492 Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
493 _empty = selection->isEmpty();
495 if (_empty) {
496 return;
497 }
499 _box = selection->bounds();
500 if (_box.isEmpty()) {
501 _empty = true;
502 return;
503 }
505 _strokewidth = stroke_average_width (selection->itemList());
507 _current.set_identity();
508 }
510 static void sp_remove_handles(SPKnot *knot[], gint num)
511 {
512 for (int i = 0; i < num; i++) {
513 if (knot[i] != NULL) {
514 sp_knot_hide(knot[i]);
515 }
516 }
517 }
519 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
520 gchar const *even_tip, gchar const *odd_tip)
521 {
522 g_return_if_fail( !_empty );
524 for (int i = 0; i < num; i++) {
525 if (knot[i] == NULL) {
526 knot[i] = sp_knot_new(_desktop);
527 g_object_set(G_OBJECT(knot[i]),
528 "anchor", handle[i].anchor,
529 "shape", SP_CTRL_SHAPE_BITMAP,
530 "size", 13,
531 "mode", SP_KNOT_MODE_XOR,
532 "fill", 0x000000ff, // inversion
533 "fill_mouseover", 0x00ff6600, // green
534 "stroke", 0x000000ff, // inversion
535 "stroke_mouseover", 0x000000ff, // inversion
536 "pixbuf", handles[handle[i].control],
537 "tip", i % 2 ? even_tip : odd_tip,
538 NULL);
540 g_signal_connect(G_OBJECT(knot[i]), "request",
541 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
542 g_signal_connect(G_OBJECT(knot[i]), "moved",
543 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
544 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
545 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
546 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
547 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
548 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
549 }
550 sp_knot_show(knot[i]);
552 NR::Point const handle_pt(handle[i].x, handle[i].y);
553 NR::Point p( _box.min()
554 + ( _box.dimensions()
555 * NR::scale(handle_pt) ) );
557 sp_knot_moveto(knot[i], &p);
558 }
559 }
561 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
562 {
563 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
564 knot, state, *(SPSelTransHandle const *) data
565 );
566 }
568 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
569 {
570 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
571 }
573 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
574 {
575 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
576 knot, position, state, *(SPSelTransHandle const *) data
577 );
578 }
580 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
581 {
582 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
583 knot, position, state, *(SPSelTransHandle const *) data
584 );
585 }
587 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
588 {
589 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
590 knot, state, *(SPSelTransHandle const *) data
591 );
592 }
594 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
595 {
596 switch (handle.anchor) {
597 case GTK_ANCHOR_CENTER:
598 if (state & GDK_SHIFT_MASK) {
599 // Unset the center position for all selected items
600 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
601 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
602 it->unsetCenter();
603 SP_OBJECT(it)->updateRepr();
604 }
605 sp_document_maybe_done (SP_DT_DOCUMENT(_desktop), "center::unset");
606 }
607 break;
608 default:
609 break;
610 }
611 }
613 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
614 {
615 switch (handle.anchor) {
616 case GTK_ANCHOR_CENTER:
617 g_object_set(G_OBJECT(_grip),
618 "shape", SP_CTRL_SHAPE_BITMAP,
619 "size", 13.0,
620 NULL);
621 sp_canvas_item_show(_grip);
622 break;
623 default:
624 g_object_set(G_OBJECT(_grip),
625 "shape", SP_CTRL_SHAPE_CROSS,
626 "size", 7.0,
627 NULL);
628 sp_canvas_item_show(_norm);
629 sp_canvas_item_show(_grip);
631 break;
632 }
634 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
635 }
638 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
639 {
640 if (!SP_KNOT_IS_GRABBED(knot)) {
641 return;
642 }
644 // in case items have been unhooked from the document, don't
645 // try to continue processing events for them.
646 for (unsigned int i = 0; i < _items.size(); i++) {
647 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
648 return;
649 }
650 }
652 handle.action(this, handle, *position, state);
653 }
656 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
657 {
658 if (!SP_KNOT_IS_GRABBED(knot)) {
659 return TRUE;
660 }
662 knot->desktop->set_coordinate_status(*position);
663 knot->desktop->setPosition(*position);
666 if (state & GDK_MOD1_MASK) {
667 *position = _point + ( *position - _point ) / 10;
668 }
670 if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
671 _origin = _opposite;
672 } else {
673 _origin = _center;
674 }
675 if (handle.request(this, handle, *position, state)) {
676 sp_knot_set_position(knot, position, state);
677 SP_CTRL(_grip)->moveto(*position);
678 SP_CTRL(_norm)->moveto(_origin);
679 }
681 return TRUE;
682 }
685 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
686 {
687 if (!_grabbed) {
688 _updateVolatileState();
689 _updateHandles();
690 }
691 }
693 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
694 {
695 if (!_grabbed) {
696 _updateVolatileState();
698 // reset internal flag
699 _changed = false;
701 _updateHandles();
702 }
703 }
705 /*
706 * handlers for handle move-request
707 */
709 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
710 static double sign(double const x)
711 {
712 return ( x < 0
713 ? -1
714 : 1 );
715 }
717 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
718 SPSelTransHandle const &, NR::Point &pt, guint state)
719 {
720 return seltrans->scaleRequest(pt, state);
721 }
723 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
724 SPSelTransHandle const &handle, NR::Point &pt, guint state)
725 {
726 return seltrans->stretchRequest(handle, pt, state);
727 }
729 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
730 SPSelTransHandle const &handle, NR::Point &pt, guint state)
731 {
732 return seltrans->skewRequest(handle, pt, state);
733 }
735 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
736 SPSelTransHandle const &, NR::Point &pt, guint state)
737 {
738 return seltrans->rotateRequest(pt, state);
739 }
741 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
742 SPSelTransHandle const &, NR::Point &pt, guint state)
743 {
744 return seltrans->centerRequest(pt, state);
745 }
747 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
748 {
749 using NR::X;
750 using NR::Y;
752 NR::Point d = _point - _origin;
753 NR::scale s(0, 0);
755 /* Work out the new scale factors `s' */
756 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
757 if ( fabs(d[i]) > 0.001 ) {
758 s[i] = ( pt[i] - _origin[i] ) / d[i];
759 if ( fabs(s[i]) < 1e-9 ) {
760 s[i] = 1e-9;
761 }
762 }
763 }
765 /* Get a STL list of the selected items.
766 ** FIXME: this should probably be done by Inkscape::Selection.
767 */
768 std::list<SPItem const*> it;
769 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
770 it.push_back(reinterpret_cast<SPItem*>(i->data));
771 }
773 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
774 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y] */
776 NR::Dim2 locked_dim;
778 /* Lock aspect ratio, using the smaller of the x and y factors */
779 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
780 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
781 locked_dim = NR::X;
782 } else {
783 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
784 locked_dim = NR::Y;
785 }
787 /* Snap the scale factor */
788 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
789 Snapper::BBOX_POINT, _bbox_points,
790 _origin, s, it);
791 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
792 Snapper::SNAP_POINT, _snap_points,
793 _origin, s, it);
795 double bd = bb.second ? fabs(bb.first - s[locked_dim]) : NR_HUGE;
796 double sd = sn.second ? fabs(sn.first - s[locked_dim]) : NR_HUGE;
797 double r = (bd < sd) ? bb.first : sn.first;
799 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
800 s[i] = r * sign(s[i]);
801 }
803 } else {
804 /* Scale aspect ratio is unlocked */
805 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
806 std::pair<double, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview,
807 Snapper::BBOX_POINT, _bbox_points,
808 _origin, s[i], NR::Dim2(i), it);
809 std::pair<double, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview,
810 Snapper::SNAP_POINT, _snap_points,
811 _origin, s[i], NR::Dim2(i), it);
813 /* Pick the snap that puts us closest to the original scale */
814 NR::Coord bd = bb.second ? fabs(bb.first - s[i]) : NR_HUGE;
815 NR::Coord sd = sn.second ? fabs(sn.first - s[i]) : NR_HUGE;
816 s[i] = (bd < sd) ? bb.first : sn.first;
817 }
818 }
820 /* Update the knot position */
821 pt = ( _point - _origin ) * s + _origin;
823 /* Status text */
824 _message_context.setF(Inkscape::NORMAL_MESSAGE,
825 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
826 100 * s[NR::X], 100 * s[NR::Y]);
828 return TRUE;
829 }
831 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
832 {
833 using NR::X;
834 using NR::Y;
836 NR::Dim2 axis, perp;
838 switch (handle.cursor) {
839 case GDK_TOP_SIDE:
840 case GDK_BOTTOM_SIDE:
841 axis = NR::Y;
842 perp = NR::X;
843 break;
844 case GDK_LEFT_SIDE:
845 case GDK_RIGHT_SIDE:
846 axis = NR::X;
847 perp = NR::Y;
848 break;
849 default:
850 g_assert_not_reached();
851 return TRUE;
852 };
854 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
855 return FALSE;
856 }
858 NR::scale s(1, 1);
859 s[axis] = ( ( pt[axis] - _origin[axis] )
860 / ( _point[axis] - _origin[axis] ) );
861 if ( fabs(s[axis]) < 1e-15 ) {
862 s[axis] = 1e-15;
863 }
865 /* Get a STL list of the selected items.
866 ** FIXME: this should probably be done by Inkscape::Selection.
867 */
868 std::list<SPItem const*> it;
869 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
870 it.push_back(reinterpret_cast<SPItem*>(i->data));
871 }
873 if ( state & GDK_CONTROL_MASK ) {
874 s[perp] = fabs(s[axis]);
876 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
877 Snapper::BBOX_POINT,
878 _bbox_points, _origin, s, it);
879 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
880 Snapper::SNAP_POINT,
881 _snap_points, _origin, s, it);
883 double bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
884 double sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
885 double ratio = (bd < sd) ? bb.first : sn.first;
887 s[axis] = fabs(ratio) * sign(s[axis]);
888 s[perp] = fabs(s[axis]);
889 } else {
890 std::pair<NR::Coord, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::BBOX_POINT,
891 _bbox_points, _origin,
892 s[axis], axis, it);
893 std::pair<NR::Coord, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::SNAP_POINT,
894 _snap_points, _origin,
895 s[axis], axis, it);
897 /* Pick the snap that puts us closest to the original scale */
898 NR::Coord bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
899 NR::Coord sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
900 s[axis] = (bd < sd) ? bb.first : sn.first;
901 }
903 pt = ( _point - _origin ) * NR::scale(s) + _origin;
904 if (isNaN(pt[X] + pt[Y])) {
905 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
906 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
907 }
909 // status text
910 _message_context.setF(Inkscape::NORMAL_MESSAGE,
911 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
912 100 * s[NR::X], 100 * s[NR::Y]);
914 return TRUE;
915 }
917 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
918 {
919 using NR::X;
920 using NR::Y;
922 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
923 return FALSE;
924 }
926 NR::Dim2 dim_a;
927 NR::Dim2 dim_b;
928 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
929 dim_a = X;
930 dim_b = Y;
931 } else {
932 dim_a = Y;
933 dim_b = X;
934 }
936 double skew[2];
937 double s[2] = { 1.0, 1.0 };
939 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
940 return FALSE;
941 }
943 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
945 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
947 if ( fabs(s[dim_a]) < 1 ) {
948 s[dim_a] = sign(s[dim_a]);
949 } else {
950 s[dim_a] = floor( s[dim_a] + 0.5 );
951 }
953 double radians = atan(skew[dim_a] / s[dim_a]);
955 if (state & GDK_CONTROL_MASK) {
957 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
959 if (snaps) {
960 double sections = floor( radians * snaps / M_PI + .5 );
961 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
962 radians = ( M_PI / snaps ) * sections;
963 }
964 skew[dim_a] = tan(radians) * s[dim_a];
965 } else {
966 skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview,
967 Snapper::SNAP_POINT, _snap_points,
968 _origin, skew[dim_a], dim_b);
969 }
971 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
972 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
974 /* status text */
975 double degrees = 180 / M_PI * radians;
976 if (degrees > 180) degrees -= 360;
977 if (degrees < -180) degrees += 360;
979 _message_context.setF(Inkscape::NORMAL_MESSAGE,
980 // TRANSLATORS: don't modify the first ";"
981 // (it will NOT be displayed as ";" - only the second one will be)
982 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
983 degrees);
985 return TRUE;
986 }
988 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
989 {
990 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
992 // rotate affine in rotate
993 NR::Point const d1 = _point - _origin;
994 NR::Point const d2 = pt - _origin;
996 NR::Coord const h1 = NR::L2(d1);
997 if (h1 < 1e-15) return FALSE;
998 NR::Point q1 = d1 / h1;
999 NR::Coord const h2 = NR::L2(d2);
1000 if (fabs(h2) < 1e-15) return FALSE;
1001 NR::Point q2 = d2 / h2;
1003 double radians;
1004 if (state & GDK_CONTROL_MASK) {
1005 /* Have to restrict movement. */
1006 double cos_t = NR::dot(q1, q2);
1007 double sin_t = NR::dot(NR::rot90(q1), q2);
1008 radians = atan2(sin_t, cos_t);
1009 if (snaps) {
1010 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1011 }
1012 q1 = NR::Point(1, 0);
1013 q2 = NR::Point(cos(radians), sin(radians));
1014 } else {
1015 radians = atan2(NR::dot(NR::rot90(d1), d2),
1016 NR::dot(d1, d2));
1017 }
1019 NR::rotate const r1(q1);
1020 NR::rotate const r2(q2);
1021 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1023 /* status text */
1024 double degrees = 180 / M_PI * radians;
1025 if (degrees > 180) degrees -= 360;
1026 if (degrees < -180) degrees += 360;
1028 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1029 // TRANSLATORS: don't modify the first ";"
1030 // (it will NOT be displayed as ";" - only the second one will be)
1031 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1033 return TRUE;
1034 }
1036 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1037 {
1038 using NR::X;
1039 using NR::Y;
1041 SnapManager const m(_desktop->namedview);
1042 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1044 if (state & GDK_CONTROL_MASK) {
1045 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1046 pt[Y] = _point[Y];
1047 } else {
1048 pt[X] = _point[X];
1049 }
1050 }
1052 if (!(state & GDK_SHIFT_MASK)) {
1053 // screen pixels to snap center to bbox
1054 #define SNAP_DIST 5
1055 // FIXME: take from prefs
1056 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1058 for (int i = 0; i < 2; i++) {
1060 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1061 pt[i] = _box.min()[i];
1062 }
1063 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1064 pt[i] = _box.midpoint()[i];
1065 }
1066 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1067 pt[i] = _box.max()[i];
1068 }
1069 }
1070 }
1072 // status text
1073 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1074 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1075 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1076 g_string_free(xs, FALSE);
1077 g_string_free(ys, FALSE);
1079 return TRUE;
1080 }
1082 /*
1083 * handlers for handle movement
1084 *
1085 */
1087 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1088 {
1089 seltrans->stretch(handle, pt, state);
1090 }
1092 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1093 {
1094 seltrans->scale(pt, state);
1095 }
1097 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1098 {
1099 seltrans->skew(handle, pt, state);
1100 }
1102 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1103 {
1104 seltrans->rotate(pt, state);
1105 }
1107 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1108 {
1109 using NR::X;
1110 using NR::Y;
1112 NR::Dim2 dim;
1113 switch (handle.cursor) {
1114 case GDK_LEFT_SIDE:
1115 case GDK_RIGHT_SIDE:
1116 dim = X;
1117 break;
1118 case GDK_TOP_SIDE:
1119 case GDK_BOTTOM_SIDE:
1120 dim = Y;
1121 break;
1122 default:
1123 g_assert_not_reached();
1124 abort();
1125 break;
1126 }
1128 NR::Point const scale_origin(_origin);
1129 double const offset = _point[dim] - scale_origin[dim];
1130 if (!( fabs(offset) >= 1e-15 )) {
1131 return;
1132 }
1133 NR::scale s(1, 1);
1134 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1135 if (isNaN(s[dim])) {
1136 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1137 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1138 }
1139 if (!( fabs(s[dim]) >= 1e-15 )) {
1140 s[dim] = 1e-15;
1141 }
1142 if (state & GDK_CONTROL_MASK) {
1143 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1144 s[!dim] = fabs(s[dim]);
1145 }
1147 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1148 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1150 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1151 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1152 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1154 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1155 }
1157 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1158 {
1159 NR::Point const offset = _point - _origin;
1161 NR::scale s (1, 1);
1162 for (int i = NR::X; i <= NR::Y; i++) {
1163 if (fabs(offset[i]) > 1e-9)
1164 s[i] = (pt[i] - _origin[i]) / offset[i];
1165 if (fabs(s[i]) < 1e-9)
1166 s[i] = 1e-9;
1167 }
1168 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1169 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1171 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1172 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1173 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1175 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1176 }
1178 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1179 {
1180 NR::Point const offset = _point - _origin;
1182 unsigned dim;
1183 switch (handle.cursor) {
1184 case GDK_SB_H_DOUBLE_ARROW:
1185 dim = NR::Y;
1186 break;
1187 case GDK_SB_V_DOUBLE_ARROW:
1188 dim = NR::X;
1189 break;
1190 default:
1191 g_assert_not_reached();
1192 abort();
1193 break;
1194 }
1195 if (fabs(offset[dim]) < 1e-15) {
1196 return;
1197 }
1198 NR::Matrix skew = NR::identity();
1199 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1200 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1201 skew[2*(1-dim) + (dim)] = 0;
1202 skew[2*(1-dim) + (1-dim)] = 1;
1204 for (int i = 0; i < 2; i++) {
1205 if (fabs(skew[3*i]) < 1e-15) {
1206 skew[3*i] = 1e-15;
1207 }
1208 }
1209 transform(skew, _origin);
1210 }
1212 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1213 {
1214 NR::Point const offset = _point - _origin;
1216 NR::Coord const h1 = NR::L2(offset);
1217 if (h1 < 1e-15) {
1218 return;
1219 }
1220 NR::Point const q1 = offset / h1;
1221 NR::Coord const h2 = NR::L2( pt - _origin );
1222 if (h2 < 1e-15) {
1223 return;
1224 }
1225 NR::Point const q2 = (pt - _origin) / h2;
1226 NR::rotate const r1(q1);
1227 NR::rotate const r2(q2);
1229 NR::Matrix rotate( r2 / r1 );
1230 transform(rotate, _origin);
1231 }
1233 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1234 {
1235 seltrans->setCenter(pt);
1236 }
1239 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1240 {
1241 SnapManager const m(_desktop->namedview);
1243 /* The amount that we've moved by during this drag */
1244 NR::Point dxy = xy - _point;
1246 /* Get a STL list of the selected items.
1247 ** FIXME: this should probably be done by Inkscape::Selection.
1248 */
1249 std::list<SPItem const*> it;
1250 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1251 it.push_back(reinterpret_cast<SPItem*>(i->data));
1252 }
1254 bool const alt = (state & GDK_MOD1_MASK);
1255 bool const control = (state & GDK_CONTROL_MASK);
1256 bool const shift = (state & GDK_SHIFT_MASK);
1258 if (alt) {
1260 /* Alt pressed means keep offset: snap the moved distance to the grid.
1261 ** FIXME: this will snap to more than just the grid, nowadays.
1262 */
1264 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1266 } else if (!shift) {
1268 /* We're snapping to things, possibly with a constraint to horizontal or
1269 ** vertical movement. Obtain a list of possible translations and then
1270 ** pick the smallest.
1271 */
1273 /* This will be our list of possible translations */
1274 std::list<std::pair<NR::Point, bool> > s;
1276 if (control) {
1278 /* Snap to things, and also constrain to horizontal or vertical movement */
1280 for (unsigned int dim = 0; dim < 2; dim++) {
1281 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1282 _bbox_points,
1283 component_vectors[dim], it, dxy));
1284 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1285 _snap_points,
1286 component_vectors[dim], it, dxy));
1287 }
1289 } else {
1291 /* Snap to things with no constraint */
1293 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1294 _bbox_points, it, dxy));
1295 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1296 _snap_points, it, dxy));
1297 }
1299 /* Pick one */
1300 NR::Coord best = NR_HUGE;
1301 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1302 if (i->second) {
1303 NR::Coord const m = NR::L2(i->first);
1304 if (m < best) {
1305 best = m;
1306 dxy = i->first;
1307 }
1308 }
1309 }
1310 }
1312 if (control) {
1313 /* Ensure that the horizontal and vertical constraint has been applied */
1314 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1315 dxy[NR::Y] = 0;
1316 } else {
1317 dxy[NR::X] = 0;
1318 }
1319 }
1321 NR::Matrix const move((NR::translate(dxy)));
1322 NR::Point const norm(0, 0);
1323 transform(move, norm);
1325 // status text
1326 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1327 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1328 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("<b>Move</b> by %s, %s; with <b>Ctrl</b> to restrict to horizontal/vertical; with <b>Shift</b> to disable snapping"), xs->str, ys->str);
1329 g_string_free(xs, TRUE);
1330 g_string_free(ys, TRUE);
1331 }
1334 /*
1335 Local Variables:
1336 mode:c++
1337 c-file-style:"stroustrup"
1338 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1339 indent-tabs-mode:nil
1340 fill-column:99
1341 End:
1342 */
1343 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :