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 _center_is_set = false; // reread _center from items, or set to bbox midpoint
106 _updateHandles();
108 _selection = sp_desktop_selection(desktop);
110 _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
111 SP_TYPE_CTRL,
112 "anchor", GTK_ANCHOR_CENTER,
113 "mode", SP_CTRL_MODE_COLOR,
114 "shape", SP_CTRL_SHAPE_BITMAP,
115 "size", 13.0,
116 "filled", TRUE,
117 "fill_color", 0x00000000,
118 "stroked", TRUE,
119 "stroke_color", 0x000000a0,
120 "pixbuf", handles[12],
121 NULL);
123 _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
124 SP_TYPE_CTRL,
125 "anchor", GTK_ANCHOR_CENTER,
126 "mode", SP_CTRL_MODE_XOR,
127 "shape", SP_CTRL_SHAPE_CROSS,
128 "size", 7.0,
129 "filled", TRUE,
130 "fill_color", 0xffffff7f,
131 "stroked", TRUE,
132 "stroke_color", 0xffffffff,
133 "pixbuf", handles[12],
134 NULL);
136 sp_canvas_item_hide(_grip);
137 sp_canvas_item_hide(_norm);
139 for (int i = 0; i < 4; i++) {
140 _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
141 sp_canvas_item_hide(_l[i]);
142 }
144 _sel_changed_connection = _selection->connectChanged(
145 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
146 );
148 _sel_modified_connection = _selection->connectModified(
149 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
150 );
151 }
153 Inkscape::SelTrans::~SelTrans()
154 {
155 _sel_changed_connection.disconnect();
156 _sel_modified_connection.disconnect();
158 for (unsigned int i = 0; i < 8; i++) {
159 if (_shandle[i]) {
160 g_object_unref(G_OBJECT(_shandle[i]));
161 _shandle[i] = NULL;
162 }
163 if (_rhandle[i]) {
164 g_object_unref(G_OBJECT(_rhandle[i]));
165 _rhandle[i] = NULL;
166 }
167 }
168 if (_chandle) {
169 g_object_unref(G_OBJECT(_chandle));
170 _chandle = NULL;
171 }
173 if (_norm) {
174 gtk_object_destroy(GTK_OBJECT(_norm));
175 _norm = NULL;
176 }
177 if (_grip) {
178 gtk_object_destroy(GTK_OBJECT(_grip));
179 _grip = NULL;
180 }
181 for (int i = 0; i < 4; i++) {
182 if (_l[i]) {
183 gtk_object_destroy(GTK_OBJECT(_l[i]));
184 _l[i] = NULL;
185 }
186 }
188 for (unsigned i = 0; i < _items.size(); i++) {
189 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
190 }
192 _items.clear();
193 _items_centers.clear();
194 }
196 void Inkscape::SelTrans::resetState()
197 {
198 _state = STATE_SCALE;
199 }
201 void Inkscape::SelTrans::increaseState()
202 {
203 if (_state == STATE_SCALE) {
204 _state = STATE_ROTATE;
205 } else {
206 _state = STATE_SCALE;
207 }
209 _center_is_set = true; // no need to reread center
211 _updateHandles();
212 }
214 void Inkscape::SelTrans::setCenter(NR::Point const &p)
215 {
216 _center = p;
217 _center_is_set = true;
219 // Write the new center position into all selected items
220 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
221 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
222 it->setCenter(p);
223 SP_OBJECT(it)->updateRepr();
224 }
225 sp_document_maybe_done (sp_desktop_document(_desktop), "center::move");
227 _updateHandles();
228 }
230 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
231 {
232 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
234 g_return_if_fail(!_grabbed);
236 _grabbed = true;
237 _show_handles = show_handles;
238 _updateVolatileState();
240 _changed = false;
242 if (_empty) {
243 return;
244 }
246 for (GSList const *l = selection->itemList(); l; l = l->next) {
247 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
248 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
249 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
250 }
252 _current.set_identity();
254 _point = p;
256 _snap_points = selection->getSnapPoints();
257 _bbox_points = selection->getBBoxPoints();
259 gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
260 bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
261 NR::Rect op_box = _box;
262 if (origin_on_bbox == false && _snap_points.empty() == false) {
263 std::vector<NR::Point>::iterator i = _snap_points.begin();
264 op_box = NR::Rect(*i, *i);
265 i++;
266 while (i != _snap_points.end()) {
267 op_box.expandTo(*i);
268 i++;
269 }
270 }
272 _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
274 if ((x != -1) && (y != -1)) {
275 sp_canvas_item_show(_norm);
276 sp_canvas_item_show(_grip);
277 }
279 if (_show == SHOW_OUTLINE) {
280 for (int i = 0; i < 4; i++)
281 sp_canvas_item_show(_l[i]);
282 }
285 _updateHandles();
286 g_return_if_fail(_stamp_cache == NULL);
287 }
289 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
290 {
291 g_return_if_fail(_grabbed);
292 g_return_if_fail(!_empty);
294 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
296 if (_show == SHOW_CONTENT) {
297 // update the content
298 for (unsigned i = 0; i < _items.size(); i++) {
299 SPItem &item = *_items[i].first;
300 NR::Matrix const &prev_transform = _items[i].second;
301 sp_item_set_i2d_affine(&item, prev_transform * affine);
302 }
303 } else {
304 NR::Point p[4];
305 /* update the outline */
306 for (unsigned i = 0 ; i < 4 ; i++) {
307 p[i] = _box.corner(i) * affine;
308 }
309 for (unsigned i = 0 ; i < 4 ; i++) {
310 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
311 }
312 }
314 _current = affine;
315 _changed = true;
316 _updateHandles();
317 }
319 void Inkscape::SelTrans::ungrab()
320 {
321 g_return_if_fail(_grabbed);
323 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
324 bool updh = true;
325 if (!_empty && _changed) {
326 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
327 _center *= _current;
328 _center_is_set = true;
330 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
331 // appropriately - it does not know the original positions of the centers (all objects already have
332 // the new bboxes). So we need to reset the centers from our saved array.
333 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
334 for (unsigned i = 0; i < _items_centers.size(); i++) {
335 SPItem *currentItem = _items_centers[i].first;
336 if (currentItem->isCenterSet()) { // only if it's already set
337 currentItem->setCenter (_items_centers[i].second * _current);
338 SP_OBJECT(currentItem)->updateRepr();
339 }
340 }
341 }
343 sp_document_done(sp_desktop_document(_desktop));
344 updh = false;
345 }
347 for (unsigned i = 0; i < _items.size(); i++) {
348 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
349 }
350 _items.clear();
351 _items_centers.clear();
353 _grabbed = false;
354 _show_handles = true;
356 sp_canvas_item_hide(_norm);
357 sp_canvas_item_hide(_grip);
359 if (_show == SHOW_OUTLINE) {
360 for (int i = 0; i < 4; i++)
361 sp_canvas_item_hide(_l[i]);
362 }
364 _updateVolatileState();
365 if (updh) {
366 _updateHandles();
367 }
368 if (_stamp_cache) {
369 g_slist_free(_stamp_cache);
370 _stamp_cache = NULL;
371 }
373 _message_context.clear();
374 }
376 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
377 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
379 void Inkscape::SelTrans::stamp()
380 {
381 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
383 /* stamping mode */
384 if (!_empty) {
385 GSList *l;
386 if (_stamp_cache) {
387 l = _stamp_cache;
388 } else {
389 /* Build cache */
390 l = g_slist_copy((GSList *) selection->itemList());
391 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
392 _stamp_cache = l;
393 }
395 while (l) {
396 SPItem *original_item = SP_ITEM(l->data);
397 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
399 // remember the position of the item
400 gint pos = original_repr->position();
401 // remember parent
402 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
404 Inkscape::XML::Node *copy_repr = original_repr->duplicate();
406 // add the new repr to the parent
407 parent->appendChild(copy_repr);
408 // move to the saved position
409 copy_repr->setPosition(pos > 0 ? pos : 0);
411 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
413 NR::Matrix const *new_affine;
414 if (_show == SHOW_OUTLINE) {
415 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
416 NR::Matrix const i2dnew( i2d * _current );
417 sp_item_set_i2d_affine(copy_item, i2dnew);
418 new_affine = ©_item->transform;
419 } else {
420 new_affine = &original_item->transform;
421 }
423 sp_item_write_transform(copy_item, copy_repr, *new_affine);
425 if (copy_item->isCenterSet()) {
426 copy_item->setCenter(_center * _current);
427 }
429 Inkscape::GC::release(copy_repr);
430 l = l->next;
431 }
432 sp_document_done(sp_desktop_document(_desktop));
433 }
434 }
436 void Inkscape::SelTrans::_updateHandles()
437 {
438 if ( !_show_handles || _empty )
439 {
440 sp_remove_handles(_shandle, 8);
441 sp_remove_handles(_rhandle, 8);
442 sp_remove_handles(&_chandle, 1);
443 return;
444 }
446 // center handle
447 if ( _chandle == NULL ) {
448 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
450 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
451 _chandle->setSize (13);
452 _chandle->setAnchor (handle_center.anchor);
453 _chandle->setMode (SP_CTRL_MODE_XOR);
454 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
455 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
456 _chandle->setPixbuf(handles[handle_center.control]);
457 sp_knot_update_ctrl(_chandle);
459 g_signal_connect(G_OBJECT(_chandle), "request",
460 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
461 g_signal_connect(G_OBJECT(_chandle), "moved",
462 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
463 g_signal_connect(G_OBJECT(_chandle), "grabbed",
464 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
465 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
466 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
467 g_signal_connect(G_OBJECT(_chandle), "clicked",
468 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
469 }
471 sp_remove_handles(&_chandle, 1);
472 if ( _state == STATE_SCALE ) {
473 sp_remove_handles(_rhandle, 8);
474 _showHandles(_shandle, handles_scale, 8,
475 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
476 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
477 } else {
478 sp_remove_handles(_shandle, 8);
479 _showHandles(_rhandle, handles_rotate, 8,
480 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
481 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
482 }
484 if (!_center_is_set) {
485 _center = _desktop->selection->center();
486 _center_is_set = true;
487 }
489 if ( _state == STATE_SCALE ) {
490 sp_knot_hide(_chandle);
491 } else {
492 sp_knot_show(_chandle);
493 sp_knot_moveto(_chandle, &_center);
494 }
495 }
497 void Inkscape::SelTrans::_updateVolatileState()
498 {
499 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
500 _empty = selection->isEmpty();
502 if (_empty) {
503 return;
504 }
506 _box = selection->bounds();
507 if (_box.isEmpty()) {
508 _empty = true;
509 return;
510 }
512 _strokewidth = stroke_average_width (selection->itemList());
514 _current.set_identity();
515 }
517 static void sp_remove_handles(SPKnot *knot[], gint num)
518 {
519 for (int i = 0; i < num; i++) {
520 if (knot[i] != NULL) {
521 sp_knot_hide(knot[i]);
522 }
523 }
524 }
526 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
527 gchar const *even_tip, gchar const *odd_tip)
528 {
529 g_return_if_fail( !_empty );
531 for (int i = 0; i < num; i++) {
532 if (knot[i] == NULL) {
533 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
535 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
536 knot[i]->setSize (13);
537 knot[i]->setAnchor (handle[i].anchor);
538 knot[i]->setMode (SP_CTRL_MODE_XOR);
539 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
540 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
541 knot[i]->setPixbuf(handles[handle[i].control]);
542 sp_knot_update_ctrl(knot[i]);
544 g_signal_connect(G_OBJECT(knot[i]), "request",
545 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
546 g_signal_connect(G_OBJECT(knot[i]), "moved",
547 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
548 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
549 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
550 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
551 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
552 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
553 }
554 sp_knot_show(knot[i]);
556 NR::Point const handle_pt(handle[i].x, handle[i].y);
557 NR::Point p( _box.min()
558 + ( _box.dimensions()
559 * NR::scale(handle_pt) ) );
561 sp_knot_moveto(knot[i], &p);
562 }
563 }
565 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
566 {
567 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
568 knot, state, *(SPSelTransHandle const *) data
569 );
570 }
572 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
573 {
574 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
575 }
577 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
578 {
579 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
580 knot, position, state, *(SPSelTransHandle const *) data
581 );
582 }
584 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
585 {
586 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
587 knot, position, state, *(SPSelTransHandle const *) data
588 );
589 }
591 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
592 {
593 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
594 knot, state, *(SPSelTransHandle const *) data
595 );
596 }
598 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
599 {
600 switch (handle.anchor) {
601 case GTK_ANCHOR_CENTER:
602 if (state & GDK_SHIFT_MASK) {
603 // Unset the center position for all selected items
604 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
605 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
606 it->unsetCenter();
607 SP_OBJECT(it)->updateRepr();
608 }
609 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset");
610 }
611 break;
612 default:
613 break;
614 }
615 }
617 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
618 {
619 switch (handle.anchor) {
620 case GTK_ANCHOR_CENTER:
621 g_object_set(G_OBJECT(_grip),
622 "shape", SP_CTRL_SHAPE_BITMAP,
623 "size", 13.0,
624 NULL);
625 sp_canvas_item_show(_grip);
626 break;
627 default:
628 g_object_set(G_OBJECT(_grip),
629 "shape", SP_CTRL_SHAPE_CROSS,
630 "size", 7.0,
631 NULL);
632 sp_canvas_item_show(_norm);
633 sp_canvas_item_show(_grip);
635 break;
636 }
638 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
639 }
642 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
643 {
644 if (!SP_KNOT_IS_GRABBED(knot)) {
645 return;
646 }
648 // in case items have been unhooked from the document, don't
649 // try to continue processing events for them.
650 for (unsigned int i = 0; i < _items.size(); i++) {
651 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
652 return;
653 }
654 }
656 handle.action(this, handle, *position, state);
657 }
660 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
661 {
662 if (!SP_KNOT_IS_GRABBED(knot)) {
663 return TRUE;
664 }
666 knot->desktop->set_coordinate_status(*position);
667 knot->desktop->setPosition(*position);
670 if (state & GDK_MOD1_MASK) {
671 *position = _point + ( *position - _point ) / 10;
672 }
674 if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
675 _origin = _opposite;
676 } else {
677 _origin = _center;
678 }
679 if (handle.request(this, handle, *position, state)) {
680 sp_knot_set_position(knot, position, state);
681 SP_CTRL(_grip)->moveto(*position);
682 SP_CTRL(_norm)->moveto(_origin);
683 }
685 return TRUE;
686 }
689 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
690 {
691 if (!_grabbed) {
692 _updateVolatileState();
693 _center_is_set = false; // center(s) may have changed
694 _updateHandles();
695 }
696 }
698 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
699 {
700 if (!_grabbed) {
701 _updateVolatileState();
703 // reset internal flag
704 _changed = false;
706 _center_is_set = false; // center(s) may have changed
708 _updateHandles();
709 }
710 }
712 /*
713 * handlers for handle move-request
714 */
716 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
717 static double sign(double const x)
718 {
719 return ( x < 0
720 ? -1
721 : 1 );
722 }
724 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
725 SPSelTransHandle const &, NR::Point &pt, guint state)
726 {
727 return seltrans->scaleRequest(pt, state);
728 }
730 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
731 SPSelTransHandle const &handle, NR::Point &pt, guint state)
732 {
733 return seltrans->stretchRequest(handle, pt, state);
734 }
736 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
737 SPSelTransHandle const &handle, NR::Point &pt, guint state)
738 {
739 return seltrans->skewRequest(handle, pt, state);
740 }
742 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
743 SPSelTransHandle const &, NR::Point &pt, guint state)
744 {
745 return seltrans->rotateRequest(pt, state);
746 }
748 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
749 SPSelTransHandle const &, NR::Point &pt, guint state)
750 {
751 return seltrans->centerRequest(pt, state);
752 }
754 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
755 {
756 using NR::X;
757 using NR::Y;
759 NR::Point d = _point - _origin;
760 NR::scale s(0, 0);
762 /* Work out the new scale factors `s' */
763 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
764 if ( fabs(d[i]) > 0.001 ) {
765 s[i] = ( pt[i] - _origin[i] ) / d[i];
766 if ( fabs(s[i]) < 1e-9 ) {
767 s[i] = 1e-9;
768 }
769 }
770 }
772 /* Get a STL list of the selected items.
773 ** FIXME: this should probably be done by Inkscape::Selection.
774 */
775 std::list<SPItem const*> it;
776 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
777 it.push_back(reinterpret_cast<SPItem*>(i->data));
778 }
780 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
781 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y] */
783 NR::Dim2 locked_dim;
785 /* Lock aspect ratio, using the smaller of the x and y factors */
786 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
787 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
788 locked_dim = NR::X;
789 } else {
790 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
791 locked_dim = NR::Y;
792 }
794 /* Snap the scale factor */
795 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
796 Snapper::BBOX_POINT, _bbox_points,
797 _origin, s, it);
798 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
799 Snapper::SNAP_POINT, _snap_points,
800 _origin, s, it);
802 double bd = bb.second ? fabs(bb.first - s[locked_dim]) : NR_HUGE;
803 double sd = sn.second ? fabs(sn.first - s[locked_dim]) : NR_HUGE;
804 double r = (bd < sd) ? bb.first : sn.first;
806 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
807 s[i] = r * sign(s[i]);
808 }
810 } else {
811 /* Scale aspect ratio is unlocked */
812 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
813 std::pair<double, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview,
814 Snapper::BBOX_POINT, _bbox_points,
815 _origin, s[i], NR::Dim2(i), it);
816 std::pair<double, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview,
817 Snapper::SNAP_POINT, _snap_points,
818 _origin, s[i], NR::Dim2(i), it);
820 /* Pick the snap that puts us closest to the original scale */
821 NR::Coord bd = bb.second ? fabs(bb.first - s[i]) : NR_HUGE;
822 NR::Coord sd = sn.second ? fabs(sn.first - s[i]) : NR_HUGE;
823 s[i] = (bd < sd) ? bb.first : sn.first;
824 }
825 }
827 /* Update the knot position */
828 pt = ( _point - _origin ) * s + _origin;
830 /* Status text */
831 _message_context.setF(Inkscape::NORMAL_MESSAGE,
832 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
833 100 * s[NR::X], 100 * s[NR::Y]);
835 return TRUE;
836 }
838 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
839 {
840 using NR::X;
841 using NR::Y;
843 NR::Dim2 axis, perp;
845 switch (handle.cursor) {
846 case GDK_TOP_SIDE:
847 case GDK_BOTTOM_SIDE:
848 axis = NR::Y;
849 perp = NR::X;
850 break;
851 case GDK_LEFT_SIDE:
852 case GDK_RIGHT_SIDE:
853 axis = NR::X;
854 perp = NR::Y;
855 break;
856 default:
857 g_assert_not_reached();
858 return TRUE;
859 };
861 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
862 return FALSE;
863 }
865 NR::scale s(1, 1);
866 s[axis] = ( ( pt[axis] - _origin[axis] )
867 / ( _point[axis] - _origin[axis] ) );
868 if ( fabs(s[axis]) < 1e-15 ) {
869 s[axis] = 1e-15;
870 }
872 /* Get a STL list of the selected items.
873 ** FIXME: this should probably be done by Inkscape::Selection.
874 */
875 std::list<SPItem const*> it;
876 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
877 it.push_back(reinterpret_cast<SPItem*>(i->data));
878 }
880 if ( state & GDK_CONTROL_MASK ) {
881 s[perp] = fabs(s[axis]);
883 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
884 Snapper::BBOX_POINT,
885 _bbox_points, _origin, s, it);
886 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
887 Snapper::SNAP_POINT,
888 _snap_points, _origin, s, it);
890 double bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
891 double sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
892 double ratio = (bd < sd) ? bb.first : sn.first;
894 s[axis] = fabs(ratio) * sign(s[axis]);
895 s[perp] = fabs(s[axis]);
896 } else {
897 std::pair<NR::Coord, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::BBOX_POINT,
898 _bbox_points, _origin,
899 s[axis], axis, it);
900 std::pair<NR::Coord, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::SNAP_POINT,
901 _snap_points, _origin,
902 s[axis], axis, it);
904 /* Pick the snap that puts us closest to the original scale */
905 NR::Coord bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
906 NR::Coord sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
907 s[axis] = (bd < sd) ? bb.first : sn.first;
908 }
910 pt = ( _point - _origin ) * NR::scale(s) + _origin;
911 if (isNaN(pt[X] + pt[Y])) {
912 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
913 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
914 }
916 // status text
917 _message_context.setF(Inkscape::NORMAL_MESSAGE,
918 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
919 100 * s[NR::X], 100 * s[NR::Y]);
921 return TRUE;
922 }
924 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
925 {
926 using NR::X;
927 using NR::Y;
929 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
930 return FALSE;
931 }
933 NR::Dim2 dim_a;
934 NR::Dim2 dim_b;
935 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
936 dim_a = X;
937 dim_b = Y;
938 } else {
939 dim_a = Y;
940 dim_b = X;
941 }
943 double skew[2];
944 double s[2] = { 1.0, 1.0 };
946 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
947 return FALSE;
948 }
950 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
952 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
954 if ( fabs(s[dim_a]) < 1 ) {
955 s[dim_a] = sign(s[dim_a]);
956 } else {
957 s[dim_a] = floor( s[dim_a] + 0.5 );
958 }
960 double radians = atan(skew[dim_a] / s[dim_a]);
962 if (state & GDK_CONTROL_MASK) {
964 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
966 if (snaps) {
967 double sections = floor( radians * snaps / M_PI + .5 );
968 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
969 radians = ( M_PI / snaps ) * sections;
970 }
971 skew[dim_a] = tan(radians) * s[dim_a];
972 } else {
973 skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview,
974 Snapper::SNAP_POINT, _snap_points,
975 _origin, skew[dim_a], dim_b);
976 }
978 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
979 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
981 /* status text */
982 double degrees = 180 / M_PI * radians;
983 if (degrees > 180) degrees -= 360;
984 if (degrees < -180) degrees += 360;
986 _message_context.setF(Inkscape::NORMAL_MESSAGE,
987 // TRANSLATORS: don't modify the first ";"
988 // (it will NOT be displayed as ";" - only the second one will be)
989 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
990 degrees);
992 return TRUE;
993 }
995 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
996 {
997 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
999 // rotate affine in rotate
1000 NR::Point const d1 = _point - _origin;
1001 NR::Point const d2 = pt - _origin;
1003 NR::Coord const h1 = NR::L2(d1);
1004 if (h1 < 1e-15) return FALSE;
1005 NR::Point q1 = d1 / h1;
1006 NR::Coord const h2 = NR::L2(d2);
1007 if (fabs(h2) < 1e-15) return FALSE;
1008 NR::Point q2 = d2 / h2;
1010 double radians;
1011 if (state & GDK_CONTROL_MASK) {
1012 /* Have to restrict movement. */
1013 double cos_t = NR::dot(q1, q2);
1014 double sin_t = NR::dot(NR::rot90(q1), q2);
1015 radians = atan2(sin_t, cos_t);
1016 if (snaps) {
1017 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1018 }
1019 q1 = NR::Point(1, 0);
1020 q2 = NR::Point(cos(radians), sin(radians));
1021 } else {
1022 radians = atan2(NR::dot(NR::rot90(d1), d2),
1023 NR::dot(d1, d2));
1024 }
1026 NR::rotate const r1(q1);
1027 NR::rotate const r2(q2);
1028 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1030 /* status text */
1031 double degrees = 180 / M_PI * radians;
1032 if (degrees > 180) degrees -= 360;
1033 if (degrees < -180) degrees += 360;
1035 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1036 // TRANSLATORS: don't modify the first ";"
1037 // (it will NOT be displayed as ";" - only the second one will be)
1038 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1040 return TRUE;
1041 }
1043 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1044 {
1045 using NR::X;
1046 using NR::Y;
1048 SnapManager const m(_desktop->namedview);
1049 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1051 if (state & GDK_CONTROL_MASK) {
1052 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1053 pt[Y] = _point[Y];
1054 } else {
1055 pt[X] = _point[X];
1056 }
1057 }
1059 if (!(state & GDK_SHIFT_MASK)) {
1060 // screen pixels to snap center to bbox
1061 #define SNAP_DIST 5
1062 // FIXME: take from prefs
1063 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1065 for (int i = 0; i < 2; i++) {
1067 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1068 pt[i] = _box.min()[i];
1069 }
1070 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1071 pt[i] = _box.midpoint()[i];
1072 }
1073 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1074 pt[i] = _box.max()[i];
1075 }
1076 }
1077 }
1079 // status text
1080 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1081 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1082 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1083 g_string_free(xs, FALSE);
1084 g_string_free(ys, FALSE);
1086 return TRUE;
1087 }
1089 /*
1090 * handlers for handle movement
1091 *
1092 */
1094 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1095 {
1096 seltrans->stretch(handle, pt, state);
1097 }
1099 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1100 {
1101 seltrans->scale(pt, state);
1102 }
1104 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1105 {
1106 seltrans->skew(handle, pt, state);
1107 }
1109 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1110 {
1111 seltrans->rotate(pt, state);
1112 }
1114 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1115 {
1116 using NR::X;
1117 using NR::Y;
1119 NR::Dim2 dim;
1120 switch (handle.cursor) {
1121 case GDK_LEFT_SIDE:
1122 case GDK_RIGHT_SIDE:
1123 dim = X;
1124 break;
1125 case GDK_TOP_SIDE:
1126 case GDK_BOTTOM_SIDE:
1127 dim = Y;
1128 break;
1129 default:
1130 g_assert_not_reached();
1131 abort();
1132 break;
1133 }
1135 NR::Point const scale_origin(_origin);
1136 double const offset = _point[dim] - scale_origin[dim];
1137 if (!( fabs(offset) >= 1e-15 )) {
1138 return;
1139 }
1140 NR::scale s(1, 1);
1141 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1142 if (isNaN(s[dim])) {
1143 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1144 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1145 }
1146 if (!( fabs(s[dim]) >= 1e-15 )) {
1147 s[dim] = 1e-15;
1148 }
1149 if (state & GDK_CONTROL_MASK) {
1150 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1151 s[!dim] = fabs(s[dim]);
1152 }
1154 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1155 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1157 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1158 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1159 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1161 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1162 }
1164 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1165 {
1166 NR::Point const offset = _point - _origin;
1168 NR::scale s (1, 1);
1169 for (int i = NR::X; i <= NR::Y; i++) {
1170 if (fabs(offset[i]) > 1e-9)
1171 s[i] = (pt[i] - _origin[i]) / offset[i];
1172 if (fabs(s[i]) < 1e-9)
1173 s[i] = 1e-9;
1174 }
1175 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1176 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1178 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1179 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1180 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1182 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1183 }
1185 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1186 {
1187 NR::Point const offset = _point - _origin;
1189 unsigned dim;
1190 switch (handle.cursor) {
1191 case GDK_SB_H_DOUBLE_ARROW:
1192 dim = NR::Y;
1193 break;
1194 case GDK_SB_V_DOUBLE_ARROW:
1195 dim = NR::X;
1196 break;
1197 default:
1198 g_assert_not_reached();
1199 abort();
1200 break;
1201 }
1202 if (fabs(offset[dim]) < 1e-15) {
1203 return;
1204 }
1205 NR::Matrix skew = NR::identity();
1206 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1207 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1208 skew[2*(1-dim) + (dim)] = 0;
1209 skew[2*(1-dim) + (1-dim)] = 1;
1211 for (int i = 0; i < 2; i++) {
1212 if (fabs(skew[3*i]) < 1e-15) {
1213 skew[3*i] = 1e-15;
1214 }
1215 }
1216 transform(skew, _origin);
1217 }
1219 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1220 {
1221 NR::Point const offset = _point - _origin;
1223 NR::Coord const h1 = NR::L2(offset);
1224 if (h1 < 1e-15) {
1225 return;
1226 }
1227 NR::Point const q1 = offset / h1;
1228 NR::Coord const h2 = NR::L2( pt - _origin );
1229 if (h2 < 1e-15) {
1230 return;
1231 }
1232 NR::Point const q2 = (pt - _origin) / h2;
1233 NR::rotate const r1(q1);
1234 NR::rotate const r2(q2);
1236 NR::Matrix rotate( r2 / r1 );
1237 transform(rotate, _origin);
1238 }
1240 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1241 {
1242 seltrans->setCenter(pt);
1243 }
1246 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1247 {
1248 SnapManager const m(_desktop->namedview);
1250 /* The amount that we've moved by during this drag */
1251 NR::Point dxy = xy - _point;
1253 /* Get a STL list of the selected items.
1254 ** FIXME: this should probably be done by Inkscape::Selection.
1255 */
1256 std::list<SPItem const*> it;
1257 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1258 it.push_back(reinterpret_cast<SPItem*>(i->data));
1259 }
1261 bool const alt = (state & GDK_MOD1_MASK);
1262 bool const control = (state & GDK_CONTROL_MASK);
1263 bool const shift = (state & GDK_SHIFT_MASK);
1265 if (alt) {
1267 /* Alt pressed means keep offset: snap the moved distance to the grid.
1268 ** FIXME: this will snap to more than just the grid, nowadays.
1269 */
1271 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1273 } else if (!shift) {
1275 /* We're snapping to things, possibly with a constraint to horizontal or
1276 ** vertical movement. Obtain a list of possible translations and then
1277 ** pick the smallest.
1278 */
1280 /* This will be our list of possible translations */
1281 std::list<std::pair<NR::Point, bool> > s;
1283 if (control) {
1285 /* Snap to things, and also constrain to horizontal or vertical movement */
1287 for (unsigned int dim = 0; dim < 2; dim++) {
1288 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1289 _bbox_points,
1290 component_vectors[dim], it, dxy));
1291 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1292 _snap_points,
1293 component_vectors[dim], it, dxy));
1294 }
1296 } else {
1298 /* Snap to things with no constraint */
1300 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1301 _bbox_points, it, dxy));
1302 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1303 _snap_points, it, dxy));
1304 }
1306 /* Pick one */
1307 NR::Coord best = NR_HUGE;
1308 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1309 if (i->second) {
1310 NR::Coord const m = NR::L2(i->first);
1311 if (m < best) {
1312 best = m;
1313 dxy = i->first;
1314 }
1315 }
1316 }
1317 }
1319 if (control) {
1320 /* Ensure that the horizontal and vertical constraint has been applied */
1321 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1322 dxy[NR::Y] = 0;
1323 } else {
1324 dxy[NR::X] = 0;
1325 }
1326 }
1328 NR::Matrix const move((NR::translate(dxy)));
1329 NR::Point const norm(0, 0);
1330 transform(move, norm);
1332 // status text
1333 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1334 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1335 _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);
1336 g_string_free(xs, TRUE);
1337 g_string_free(ys, TRUE);
1338 }
1341 /*
1342 Local Variables:
1343 mode:c++
1344 c-file-style:"stroustrup"
1345 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1346 indent-tabs-mode:nil
1347 fill-column:99
1348 End:
1349 */
1350 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :