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_DT_SELECTION(desktop);
110 _norm = sp_canvas_item_new(SP_DT_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_DT_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_DT_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_DT_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_DT_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_DT_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_DT_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_DT_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_DT_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 Inkscape::GC::release(copy_repr);
426 l = l->next;
427 }
428 sp_document_done(SP_DT_DOCUMENT(_desktop));
429 }
430 }
432 void Inkscape::SelTrans::_updateHandles()
433 {
434 if ( !_show_handles || _empty )
435 {
436 sp_remove_handles(_shandle, 8);
437 sp_remove_handles(_rhandle, 8);
438 sp_remove_handles(&_chandle, 1);
439 return;
440 }
442 // center handle
443 if ( _chandle == NULL ) {
444 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
446 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
447 _chandle->setSize (13);
448 _chandle->setAnchor (handle_center.anchor);
449 _chandle->setMode (SP_CTRL_MODE_XOR);
450 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
451 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
452 _chandle->setPixbuf(handles[handle_center.control]);
453 sp_knot_update_ctrl(_chandle);
455 g_signal_connect(G_OBJECT(_chandle), "request",
456 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
457 g_signal_connect(G_OBJECT(_chandle), "moved",
458 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
459 g_signal_connect(G_OBJECT(_chandle), "grabbed",
460 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
461 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
462 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
463 g_signal_connect(G_OBJECT(_chandle), "clicked",
464 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
465 }
467 sp_remove_handles(&_chandle, 1);
468 if ( _state == STATE_SCALE ) {
469 sp_remove_handles(_rhandle, 8);
470 _showHandles(_shandle, handles_scale, 8,
471 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
472 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
473 } else {
474 sp_remove_handles(_shandle, 8);
475 _showHandles(_rhandle, handles_rotate, 8,
476 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
477 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
478 }
480 if (!_center_is_set) {
481 _center = _desktop->selection->center();
482 _center_is_set = true;
483 }
485 if ( _state == STATE_SCALE ) {
486 sp_knot_hide(_chandle);
487 } else {
488 sp_knot_show(_chandle);
489 sp_knot_moveto(_chandle, &_center);
490 }
491 }
493 void Inkscape::SelTrans::_updateVolatileState()
494 {
495 Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
496 _empty = selection->isEmpty();
498 if (_empty) {
499 return;
500 }
502 _box = selection->bounds();
503 if (_box.isEmpty()) {
504 _empty = true;
505 return;
506 }
508 _strokewidth = stroke_average_width (selection->itemList());
510 _current.set_identity();
511 }
513 static void sp_remove_handles(SPKnot *knot[], gint num)
514 {
515 for (int i = 0; i < num; i++) {
516 if (knot[i] != NULL) {
517 sp_knot_hide(knot[i]);
518 }
519 }
520 }
522 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
523 gchar const *even_tip, gchar const *odd_tip)
524 {
525 g_return_if_fail( !_empty );
527 for (int i = 0; i < num; i++) {
528 if (knot[i] == NULL) {
529 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
531 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
532 knot[i]->setSize (13);
533 knot[i]->setAnchor (handle[i].anchor);
534 knot[i]->setMode (SP_CTRL_MODE_XOR);
535 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
536 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
537 knot[i]->setPixbuf(handles[handle[i].control]);
538 sp_knot_update_ctrl(knot[i]);
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 _center_is_set = false; // center(s) may have changed
690 _updateHandles();
691 }
692 }
694 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
695 {
696 if (!_grabbed) {
697 _updateVolatileState();
699 // reset internal flag
700 _changed = false;
702 _center_is_set = false; // center(s) may have changed
704 _updateHandles();
705 }
706 }
708 /*
709 * handlers for handle move-request
710 */
712 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
713 static double sign(double const x)
714 {
715 return ( x < 0
716 ? -1
717 : 1 );
718 }
720 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
721 SPSelTransHandle const &, NR::Point &pt, guint state)
722 {
723 return seltrans->scaleRequest(pt, state);
724 }
726 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
727 SPSelTransHandle const &handle, NR::Point &pt, guint state)
728 {
729 return seltrans->stretchRequest(handle, pt, state);
730 }
732 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
733 SPSelTransHandle const &handle, NR::Point &pt, guint state)
734 {
735 return seltrans->skewRequest(handle, pt, state);
736 }
738 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
739 SPSelTransHandle const &, NR::Point &pt, guint state)
740 {
741 return seltrans->rotateRequest(pt, state);
742 }
744 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
745 SPSelTransHandle const &, NR::Point &pt, guint state)
746 {
747 return seltrans->centerRequest(pt, state);
748 }
750 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
751 {
752 using NR::X;
753 using NR::Y;
755 NR::Point d = _point - _origin;
756 NR::scale s(0, 0);
758 /* Work out the new scale factors `s' */
759 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
760 if ( fabs(d[i]) > 0.001 ) {
761 s[i] = ( pt[i] - _origin[i] ) / d[i];
762 if ( fabs(s[i]) < 1e-9 ) {
763 s[i] = 1e-9;
764 }
765 }
766 }
768 /* Get a STL list of the selected items.
769 ** FIXME: this should probably be done by Inkscape::Selection.
770 */
771 std::list<SPItem const*> it;
772 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
773 it.push_back(reinterpret_cast<SPItem*>(i->data));
774 }
776 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
777 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y] */
779 NR::Dim2 locked_dim;
781 /* Lock aspect ratio, using the smaller of the x and y factors */
782 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
783 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
784 locked_dim = NR::X;
785 } else {
786 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
787 locked_dim = NR::Y;
788 }
790 /* Snap the scale factor */
791 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
792 Snapper::BBOX_POINT, _bbox_points,
793 _origin, s, it);
794 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
795 Snapper::SNAP_POINT, _snap_points,
796 _origin, s, it);
798 double bd = bb.second ? fabs(bb.first - s[locked_dim]) : NR_HUGE;
799 double sd = sn.second ? fabs(sn.first - s[locked_dim]) : NR_HUGE;
800 double r = (bd < sd) ? bb.first : sn.first;
802 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
803 s[i] = r * sign(s[i]);
804 }
806 } else {
807 /* Scale aspect ratio is unlocked */
808 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
809 std::pair<double, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview,
810 Snapper::BBOX_POINT, _bbox_points,
811 _origin, s[i], NR::Dim2(i), it);
812 std::pair<double, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview,
813 Snapper::SNAP_POINT, _snap_points,
814 _origin, s[i], NR::Dim2(i), it);
816 /* Pick the snap that puts us closest to the original scale */
817 NR::Coord bd = bb.second ? fabs(bb.first - s[i]) : NR_HUGE;
818 NR::Coord sd = sn.second ? fabs(sn.first - s[i]) : NR_HUGE;
819 s[i] = (bd < sd) ? bb.first : sn.first;
820 }
821 }
823 /* Update the knot position */
824 pt = ( _point - _origin ) * s + _origin;
826 /* Status text */
827 _message_context.setF(Inkscape::NORMAL_MESSAGE,
828 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
829 100 * s[NR::X], 100 * s[NR::Y]);
831 return TRUE;
832 }
834 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
835 {
836 using NR::X;
837 using NR::Y;
839 NR::Dim2 axis, perp;
841 switch (handle.cursor) {
842 case GDK_TOP_SIDE:
843 case GDK_BOTTOM_SIDE:
844 axis = NR::Y;
845 perp = NR::X;
846 break;
847 case GDK_LEFT_SIDE:
848 case GDK_RIGHT_SIDE:
849 axis = NR::X;
850 perp = NR::Y;
851 break;
852 default:
853 g_assert_not_reached();
854 return TRUE;
855 };
857 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
858 return FALSE;
859 }
861 NR::scale s(1, 1);
862 s[axis] = ( ( pt[axis] - _origin[axis] )
863 / ( _point[axis] - _origin[axis] ) );
864 if ( fabs(s[axis]) < 1e-15 ) {
865 s[axis] = 1e-15;
866 }
868 /* Get a STL list of the selected items.
869 ** FIXME: this should probably be done by Inkscape::Selection.
870 */
871 std::list<SPItem const*> it;
872 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
873 it.push_back(reinterpret_cast<SPItem*>(i->data));
874 }
876 if ( state & GDK_CONTROL_MASK ) {
877 s[perp] = fabs(s[axis]);
879 std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
880 Snapper::BBOX_POINT,
881 _bbox_points, _origin, s, it);
882 std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
883 Snapper::SNAP_POINT,
884 _snap_points, _origin, s, it);
886 double bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
887 double sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
888 double ratio = (bd < sd) ? bb.first : sn.first;
890 s[axis] = fabs(ratio) * sign(s[axis]);
891 s[perp] = fabs(s[axis]);
892 } else {
893 std::pair<NR::Coord, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::BBOX_POINT,
894 _bbox_points, _origin,
895 s[axis], axis, it);
896 std::pair<NR::Coord, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::SNAP_POINT,
897 _snap_points, _origin,
898 s[axis], axis, it);
900 /* Pick the snap that puts us closest to the original scale */
901 NR::Coord bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
902 NR::Coord sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
903 s[axis] = (bd < sd) ? bb.first : sn.first;
904 }
906 pt = ( _point - _origin ) * NR::scale(s) + _origin;
907 if (isNaN(pt[X] + pt[Y])) {
908 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
909 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
910 }
912 // status text
913 _message_context.setF(Inkscape::NORMAL_MESSAGE,
914 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
915 100 * s[NR::X], 100 * s[NR::Y]);
917 return TRUE;
918 }
920 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
921 {
922 using NR::X;
923 using NR::Y;
925 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
926 return FALSE;
927 }
929 NR::Dim2 dim_a;
930 NR::Dim2 dim_b;
931 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
932 dim_a = X;
933 dim_b = Y;
934 } else {
935 dim_a = Y;
936 dim_b = X;
937 }
939 double skew[2];
940 double s[2] = { 1.0, 1.0 };
942 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
943 return FALSE;
944 }
946 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
948 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
950 if ( fabs(s[dim_a]) < 1 ) {
951 s[dim_a] = sign(s[dim_a]);
952 } else {
953 s[dim_a] = floor( s[dim_a] + 0.5 );
954 }
956 double radians = atan(skew[dim_a] / s[dim_a]);
958 if (state & GDK_CONTROL_MASK) {
960 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
962 if (snaps) {
963 double sections = floor( radians * snaps / M_PI + .5 );
964 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
965 radians = ( M_PI / snaps ) * sections;
966 }
967 skew[dim_a] = tan(radians) * s[dim_a];
968 } else {
969 skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview,
970 Snapper::SNAP_POINT, _snap_points,
971 _origin, skew[dim_a], dim_b);
972 }
974 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
975 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
977 /* status text */
978 double degrees = 180 / M_PI * radians;
979 if (degrees > 180) degrees -= 360;
980 if (degrees < -180) degrees += 360;
982 _message_context.setF(Inkscape::NORMAL_MESSAGE,
983 // TRANSLATORS: don't modify the first ";"
984 // (it will NOT be displayed as ";" - only the second one will be)
985 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
986 degrees);
988 return TRUE;
989 }
991 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
992 {
993 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
995 // rotate affine in rotate
996 NR::Point const d1 = _point - _origin;
997 NR::Point const d2 = pt - _origin;
999 NR::Coord const h1 = NR::L2(d1);
1000 if (h1 < 1e-15) return FALSE;
1001 NR::Point q1 = d1 / h1;
1002 NR::Coord const h2 = NR::L2(d2);
1003 if (fabs(h2) < 1e-15) return FALSE;
1004 NR::Point q2 = d2 / h2;
1006 double radians;
1007 if (state & GDK_CONTROL_MASK) {
1008 /* Have to restrict movement. */
1009 double cos_t = NR::dot(q1, q2);
1010 double sin_t = NR::dot(NR::rot90(q1), q2);
1011 radians = atan2(sin_t, cos_t);
1012 if (snaps) {
1013 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1014 }
1015 q1 = NR::Point(1, 0);
1016 q2 = NR::Point(cos(radians), sin(radians));
1017 } else {
1018 radians = atan2(NR::dot(NR::rot90(d1), d2),
1019 NR::dot(d1, d2));
1020 }
1022 NR::rotate const r1(q1);
1023 NR::rotate const r2(q2);
1024 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1026 /* status text */
1027 double degrees = 180 / M_PI * radians;
1028 if (degrees > 180) degrees -= 360;
1029 if (degrees < -180) degrees += 360;
1031 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1032 // TRANSLATORS: don't modify the first ";"
1033 // (it will NOT be displayed as ";" - only the second one will be)
1034 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1036 return TRUE;
1037 }
1039 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1040 {
1041 using NR::X;
1042 using NR::Y;
1044 SnapManager const m(_desktop->namedview);
1045 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1047 if (state & GDK_CONTROL_MASK) {
1048 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1049 pt[Y] = _point[Y];
1050 } else {
1051 pt[X] = _point[X];
1052 }
1053 }
1055 if (!(state & GDK_SHIFT_MASK)) {
1056 // screen pixels to snap center to bbox
1057 #define SNAP_DIST 5
1058 // FIXME: take from prefs
1059 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1061 for (int i = 0; i < 2; i++) {
1063 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1064 pt[i] = _box.min()[i];
1065 }
1066 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1067 pt[i] = _box.midpoint()[i];
1068 }
1069 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1070 pt[i] = _box.max()[i];
1071 }
1072 }
1073 }
1075 // status text
1076 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1077 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1078 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1079 g_string_free(xs, FALSE);
1080 g_string_free(ys, FALSE);
1082 return TRUE;
1083 }
1085 /*
1086 * handlers for handle movement
1087 *
1088 */
1090 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1091 {
1092 seltrans->stretch(handle, pt, state);
1093 }
1095 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1096 {
1097 seltrans->scale(pt, state);
1098 }
1100 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1101 {
1102 seltrans->skew(handle, pt, state);
1103 }
1105 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1106 {
1107 seltrans->rotate(pt, state);
1108 }
1110 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1111 {
1112 using NR::X;
1113 using NR::Y;
1115 NR::Dim2 dim;
1116 switch (handle.cursor) {
1117 case GDK_LEFT_SIDE:
1118 case GDK_RIGHT_SIDE:
1119 dim = X;
1120 break;
1121 case GDK_TOP_SIDE:
1122 case GDK_BOTTOM_SIDE:
1123 dim = Y;
1124 break;
1125 default:
1126 g_assert_not_reached();
1127 abort();
1128 break;
1129 }
1131 NR::Point const scale_origin(_origin);
1132 double const offset = _point[dim] - scale_origin[dim];
1133 if (!( fabs(offset) >= 1e-15 )) {
1134 return;
1135 }
1136 NR::scale s(1, 1);
1137 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1138 if (isNaN(s[dim])) {
1139 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1140 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1141 }
1142 if (!( fabs(s[dim]) >= 1e-15 )) {
1143 s[dim] = 1e-15;
1144 }
1145 if (state & GDK_CONTROL_MASK) {
1146 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1147 s[!dim] = fabs(s[dim]);
1148 }
1150 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1151 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1153 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1154 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1155 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1157 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1158 }
1160 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1161 {
1162 NR::Point const offset = _point - _origin;
1164 NR::scale s (1, 1);
1165 for (int i = NR::X; i <= NR::Y; i++) {
1166 if (fabs(offset[i]) > 1e-9)
1167 s[i] = (pt[i] - _origin[i]) / offset[i];
1168 if (fabs(s[i]) < 1e-9)
1169 s[i] = 1e-9;
1170 }
1171 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1172 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1174 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1175 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1176 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1178 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1179 }
1181 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1182 {
1183 NR::Point const offset = _point - _origin;
1185 unsigned dim;
1186 switch (handle.cursor) {
1187 case GDK_SB_H_DOUBLE_ARROW:
1188 dim = NR::Y;
1189 break;
1190 case GDK_SB_V_DOUBLE_ARROW:
1191 dim = NR::X;
1192 break;
1193 default:
1194 g_assert_not_reached();
1195 abort();
1196 break;
1197 }
1198 if (fabs(offset[dim]) < 1e-15) {
1199 return;
1200 }
1201 NR::Matrix skew = NR::identity();
1202 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1203 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1204 skew[2*(1-dim) + (dim)] = 0;
1205 skew[2*(1-dim) + (1-dim)] = 1;
1207 for (int i = 0; i < 2; i++) {
1208 if (fabs(skew[3*i]) < 1e-15) {
1209 skew[3*i] = 1e-15;
1210 }
1211 }
1212 transform(skew, _origin);
1213 }
1215 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1216 {
1217 NR::Point const offset = _point - _origin;
1219 NR::Coord const h1 = NR::L2(offset);
1220 if (h1 < 1e-15) {
1221 return;
1222 }
1223 NR::Point const q1 = offset / h1;
1224 NR::Coord const h2 = NR::L2( pt - _origin );
1225 if (h2 < 1e-15) {
1226 return;
1227 }
1228 NR::Point const q2 = (pt - _origin) / h2;
1229 NR::rotate const r1(q1);
1230 NR::rotate const r2(q2);
1232 NR::Matrix rotate( r2 / r1 );
1233 transform(rotate, _origin);
1234 }
1236 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1237 {
1238 seltrans->setCenter(pt);
1239 }
1242 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1243 {
1244 SnapManager const m(_desktop->namedview);
1246 /* The amount that we've moved by during this drag */
1247 NR::Point dxy = xy - _point;
1249 /* Get a STL list of the selected items.
1250 ** FIXME: this should probably be done by Inkscape::Selection.
1251 */
1252 std::list<SPItem const*> it;
1253 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1254 it.push_back(reinterpret_cast<SPItem*>(i->data));
1255 }
1257 bool const alt = (state & GDK_MOD1_MASK);
1258 bool const control = (state & GDK_CONTROL_MASK);
1259 bool const shift = (state & GDK_SHIFT_MASK);
1261 if (alt) {
1263 /* Alt pressed means keep offset: snap the moved distance to the grid.
1264 ** FIXME: this will snap to more than just the grid, nowadays.
1265 */
1267 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1269 } else if (!shift) {
1271 /* We're snapping to things, possibly with a constraint to horizontal or
1272 ** vertical movement. Obtain a list of possible translations and then
1273 ** pick the smallest.
1274 */
1276 /* This will be our list of possible translations */
1277 std::list<std::pair<NR::Point, bool> > s;
1279 if (control) {
1281 /* Snap to things, and also constrain to horizontal or vertical movement */
1283 for (unsigned int dim = 0; dim < 2; dim++) {
1284 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1285 _bbox_points,
1286 component_vectors[dim], it, dxy));
1287 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1288 _snap_points,
1289 component_vectors[dim], it, dxy));
1290 }
1292 } else {
1294 /* Snap to things with no constraint */
1296 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1297 _bbox_points, it, dxy));
1298 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1299 _snap_points, it, dxy));
1300 }
1302 /* Pick one */
1303 NR::Coord best = NR_HUGE;
1304 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1305 if (i->second) {
1306 NR::Coord const m = NR::L2(i->first);
1307 if (m < best) {
1308 best = m;
1309 dxy = i->first;
1310 }
1311 }
1312 }
1313 }
1315 if (control) {
1316 /* Ensure that the horizontal and vertical constraint has been applied */
1317 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1318 dxy[NR::Y] = 0;
1319 } else {
1320 dxy[NR::X] = 0;
1321 }
1322 }
1324 NR::Matrix const move((NR::translate(dxy)));
1325 NR::Point const norm(0, 0);
1326 transform(move, norm);
1328 // status text
1329 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1330 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1331 _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);
1332 g_string_free(xs, TRUE);
1333 g_string_free(ys, TRUE);
1334 }
1337 /*
1338 Local Variables:
1339 mode:c++
1340 c-file-style:"stroustrup"
1341 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1342 indent-tabs-mode:nil
1343 fill-column:99
1344 End:
1345 */
1346 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :