bbaaf2f9df1de0957925f6712865ef30f9ff2bd0
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 _center_is_set = false; // center has changed
609 _updateHandles();
610 }
611 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset");
612 }
613 break;
614 default:
615 break;
616 }
617 }
619 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
620 {
621 switch (handle.anchor) {
622 case GTK_ANCHOR_CENTER:
623 g_object_set(G_OBJECT(_grip),
624 "shape", SP_CTRL_SHAPE_BITMAP,
625 "size", 13.0,
626 NULL);
627 sp_canvas_item_show(_grip);
628 break;
629 default:
630 g_object_set(G_OBJECT(_grip),
631 "shape", SP_CTRL_SHAPE_CROSS,
632 "size", 7.0,
633 NULL);
634 sp_canvas_item_show(_norm);
635 sp_canvas_item_show(_grip);
637 break;
638 }
640 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
641 }
644 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
645 {
646 if (!SP_KNOT_IS_GRABBED(knot)) {
647 return;
648 }
650 // in case items have been unhooked from the document, don't
651 // try to continue processing events for them.
652 for (unsigned int i = 0; i < _items.size(); i++) {
653 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
654 return;
655 }
656 }
658 handle.action(this, handle, *position, state);
659 }
662 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
663 {
664 if (!SP_KNOT_IS_GRABBED(knot)) {
665 return TRUE;
666 }
668 knot->desktop->set_coordinate_status(*position);
669 knot->desktop->setPosition(*position);
672 if (state & GDK_MOD1_MASK) {
673 *position = _point + ( *position - _point ) / 10;
674 }
676 if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
677 _origin = _opposite;
678 } else {
679 _origin = _center;
680 }
681 if (handle.request(this, handle, *position, state)) {
682 sp_knot_set_position(knot, position, state);
683 SP_CTRL(_grip)->moveto(*position);
684 SP_CTRL(_norm)->moveto(_origin);
685 }
687 return TRUE;
688 }
691 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
692 {
693 if (!_grabbed) {
694 _updateVolatileState();
695 _center_is_set = false; // center(s) may have changed
696 _updateHandles();
697 }
698 }
700 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
701 {
702 if (!_grabbed) {
703 _updateVolatileState();
705 // reset internal flag
706 _changed = false;
708 _center_is_set = false; // center(s) may have changed
710 _updateHandles();
711 }
712 }
714 /*
715 * handlers for handle move-request
716 */
718 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
719 static double sign(double const x)
720 {
721 return ( x < 0
722 ? -1
723 : 1 );
724 }
726 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
727 SPSelTransHandle const &, NR::Point &pt, guint state)
728 {
729 return seltrans->scaleRequest(pt, state);
730 }
732 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
733 SPSelTransHandle const &handle, NR::Point &pt, guint state)
734 {
735 return seltrans->stretchRequest(handle, pt, state);
736 }
738 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
739 SPSelTransHandle const &handle, NR::Point &pt, guint state)
740 {
741 return seltrans->skewRequest(handle, pt, state);
742 }
744 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
745 SPSelTransHandle const &, NR::Point &pt, guint state)
746 {
747 return seltrans->rotateRequest(pt, state);
748 }
750 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
751 SPSelTransHandle const &, NR::Point &pt, guint state)
752 {
753 return seltrans->centerRequest(pt, state);
754 }
756 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
757 {
758 using NR::X;
759 using NR::Y;
761 NR::Point d = _point - _origin;
762 NR::scale s(0, 0);
764 /* Work out the new scale factors `s' */
765 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
766 if ( fabs(d[i]) > 0.001 ) {
767 s[i] = ( pt[i] - _origin[i] ) / d[i];
768 if ( fabs(s[i]) < 1e-9 ) {
769 s[i] = 1e-9;
770 }
771 }
772 }
774 SnapManager const &m = _desktop->namedview->snap_manager;
776 /* Get a STL list of the selected items.
777 ** FIXME: this should probably be done by Inkscape::Selection.
778 */
779 std::list<SPItem const*> it;
780 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
781 it.push_back(reinterpret_cast<SPItem*>(i->data));
782 }
784 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
785 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
786 ** To do this, we snap along a suitable constraint vector from the origin.
787 */
789 NR::Point const cv = NR::Point(
790 pt[NR::X] > _origin[NR::X] ? 1 : -1,
791 pt[NR::Y] > _origin[NR::Y] ? 1 : -1
792 );
794 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
795 _bbox_points,
796 it,
797 Snapper::ConstraintLine(_origin, cv),
798 s,
799 _origin);
801 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
802 _snap_points,
803 it,
804 Snapper::ConstraintLine(_origin, cv),
805 s,
806 _origin);
808 if (bb.second == false && sn.second == false) {
810 /* We didn't snap, so just lock aspect ratio */
811 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
812 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
813 } else {
814 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
815 }
817 } else {
819 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
820 ** just compare difference in s[X].
821 */
822 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
823 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
824 s = (bd < sd) ? bb.first : sn.first;
825 }
827 } else {
828 /* Scale aspect ratio is unlocked */
830 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
831 _bbox_points,
832 it,
833 s,
834 _origin);
835 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
836 _snap_points,
837 it,
838 s,
839 _origin);
841 /* Pick the snap that puts us closest to the original scale */
842 NR::Coord bd = bb.second ?
843 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
844 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
845 : NR_HUGE;
846 NR::Coord sd = sn.second ?
847 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
848 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
849 : NR_HUGE;
850 s = (bd < sd) ? bb.first : sn.first;
851 }
853 /* Update the knot position */
854 pt = ( _point - _origin ) * s + _origin;
856 /* Status text */
857 _message_context.setF(Inkscape::NORMAL_MESSAGE,
858 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
859 100 * s[NR::X], 100 * s[NR::Y]);
861 return TRUE;
862 }
864 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
865 {
866 using NR::X;
867 using NR::Y;
869 NR::Dim2 axis, perp;
871 switch (handle.cursor) {
872 case GDK_TOP_SIDE:
873 case GDK_BOTTOM_SIDE:
874 axis = NR::Y;
875 perp = NR::X;
876 break;
877 case GDK_LEFT_SIDE:
878 case GDK_RIGHT_SIDE:
879 axis = NR::X;
880 perp = NR::Y;
881 break;
882 default:
883 g_assert_not_reached();
884 return TRUE;
885 };
887 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
888 return FALSE;
889 }
891 NR::scale s(1, 1);
892 s[axis] = ( ( pt[axis] - _origin[axis] )
893 / ( _point[axis] - _origin[axis] ) );
894 if ( fabs(s[axis]) < 1e-15 ) {
895 s[axis] = 1e-15;
896 }
898 /* Get a STL list of the selected items.
899 ** FIXME: this should probably be done by Inkscape::Selection.
900 */
901 std::list<SPItem const*> it;
902 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
903 it.push_back(reinterpret_cast<SPItem*>(i->data));
904 }
906 SnapManager const &m = _desktop->namedview->snap_manager;
908 if ( state & GDK_CONTROL_MASK ) {
909 s[perp] = fabs(s[axis]);
911 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
912 Snapper::BBOX_POINT,
913 _bbox_points,
914 it,
915 s[axis],
916 _origin,
917 axis,
918 true);
920 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
921 Snapper::SNAP_POINT,
922 _snap_points,
923 it,
924 s[axis],
925 _origin,
926 axis,
927 true);
929 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
930 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
931 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
933 s[axis] = fabs(ratio) * sign(s[axis]);
934 s[perp] = fabs(s[axis]);
935 } else {
937 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
938 Snapper::BBOX_POINT,
939 _bbox_points,
940 it,
941 s[axis],
942 _origin,
943 axis,
944 false);
946 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
947 Snapper::SNAP_POINT,
948 _snap_points,
949 it,
950 s[axis],
951 _origin,
952 axis,
953 false);
955 /* Choose the smaller difference in scale */
956 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
957 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
958 s[axis] = (bd < sd) ? bb.first : sn.first;
959 s[perp] = 1;
960 }
962 pt = ( _point - _origin ) * NR::scale(s) + _origin;
963 if (isNaN(pt[X] + pt[Y])) {
964 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
965 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
966 }
968 // status text
969 _message_context.setF(Inkscape::NORMAL_MESSAGE,
970 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
971 100 * s[NR::X], 100 * s[NR::Y]);
973 return TRUE;
974 }
976 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
977 {
978 using NR::X;
979 using NR::Y;
981 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
982 return FALSE;
983 }
985 NR::Dim2 dim_a;
986 NR::Dim2 dim_b;
987 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
988 dim_a = X;
989 dim_b = Y;
990 } else {
991 dim_a = Y;
992 dim_b = X;
993 }
995 double skew[2];
996 double s[2] = { 1.0, 1.0 };
998 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
999 return FALSE;
1000 }
1002 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1004 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1006 if ( fabs(s[dim_a]) < 1 ) {
1007 s[dim_a] = sign(s[dim_a]);
1008 } else {
1009 s[dim_a] = floor( s[dim_a] + 0.5 );
1010 }
1012 double radians = atan(skew[dim_a] / s[dim_a]);
1014 if (state & GDK_CONTROL_MASK) {
1016 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1018 if (snaps) {
1019 double sections = floor( radians * snaps / M_PI + .5 );
1020 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1021 radians = ( M_PI / snaps ) * sections;
1022 }
1023 skew[dim_a] = tan(radians) * s[dim_a];
1024 } else {
1025 skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview,
1026 Snapper::SNAP_POINT, _snap_points,
1027 _origin, skew[dim_a], dim_b);
1028 }
1030 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1031 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1033 /* status text */
1034 double degrees = 180 / M_PI * radians;
1035 if (degrees > 180) degrees -= 360;
1036 if (degrees < -180) degrees += 360;
1038 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1039 // TRANSLATORS: don't modify the first ";"
1040 // (it will NOT be displayed as ";" - only the second one will be)
1041 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1042 degrees);
1044 return TRUE;
1045 }
1047 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1048 {
1049 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1051 // rotate affine in rotate
1052 NR::Point const d1 = _point - _origin;
1053 NR::Point const d2 = pt - _origin;
1055 NR::Coord const h1 = NR::L2(d1);
1056 if (h1 < 1e-15) return FALSE;
1057 NR::Point q1 = d1 / h1;
1058 NR::Coord const h2 = NR::L2(d2);
1059 if (fabs(h2) < 1e-15) return FALSE;
1060 NR::Point q2 = d2 / h2;
1062 double radians;
1063 if (state & GDK_CONTROL_MASK) {
1064 /* Have to restrict movement. */
1065 double cos_t = NR::dot(q1, q2);
1066 double sin_t = NR::dot(NR::rot90(q1), q2);
1067 radians = atan2(sin_t, cos_t);
1068 if (snaps) {
1069 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1070 }
1071 q1 = NR::Point(1, 0);
1072 q2 = NR::Point(cos(radians), sin(radians));
1073 } else {
1074 radians = atan2(NR::dot(NR::rot90(d1), d2),
1075 NR::dot(d1, d2));
1076 }
1078 NR::rotate const r1(q1);
1079 NR::rotate const r2(q2);
1080 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1082 /* status text */
1083 double degrees = 180 / M_PI * radians;
1084 if (degrees > 180) degrees -= 360;
1085 if (degrees < -180) degrees += 360;
1087 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1088 // TRANSLATORS: don't modify the first ";"
1089 // (it will NOT be displayed as ";" - only the second one will be)
1090 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1092 return TRUE;
1093 }
1095 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1096 {
1097 using NR::X;
1098 using NR::Y;
1100 SnapManager const &m = _desktop->namedview->snap_manager;
1101 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1103 if (state & GDK_CONTROL_MASK) {
1104 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1105 pt[Y] = _point[Y];
1106 } else {
1107 pt[X] = _point[X];
1108 }
1109 }
1111 if (!(state & GDK_SHIFT_MASK)) {
1112 // screen pixels to snap center to bbox
1113 #define SNAP_DIST 5
1114 // FIXME: take from prefs
1115 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1117 for (int i = 0; i < 2; i++) {
1119 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1120 pt[i] = _box.min()[i];
1121 }
1122 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1123 pt[i] = _box.midpoint()[i];
1124 }
1125 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1126 pt[i] = _box.max()[i];
1127 }
1128 }
1129 }
1131 // status text
1132 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1133 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1134 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1135 g_string_free(xs, FALSE);
1136 g_string_free(ys, FALSE);
1138 return TRUE;
1139 }
1141 /*
1142 * handlers for handle movement
1143 *
1144 */
1146 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1147 {
1148 seltrans->stretch(handle, pt, state);
1149 }
1151 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1152 {
1153 seltrans->scale(pt, state);
1154 }
1156 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1157 {
1158 seltrans->skew(handle, pt, state);
1159 }
1161 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1162 {
1163 seltrans->rotate(pt, state);
1164 }
1166 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1167 {
1168 using NR::X;
1169 using NR::Y;
1171 NR::Dim2 dim;
1172 switch (handle.cursor) {
1173 case GDK_LEFT_SIDE:
1174 case GDK_RIGHT_SIDE:
1175 dim = X;
1176 break;
1177 case GDK_TOP_SIDE:
1178 case GDK_BOTTOM_SIDE:
1179 dim = Y;
1180 break;
1181 default:
1182 g_assert_not_reached();
1183 abort();
1184 break;
1185 }
1187 NR::Point const scale_origin(_origin);
1188 double const offset = _point[dim] - scale_origin[dim];
1189 if (!( fabs(offset) >= 1e-15 )) {
1190 return;
1191 }
1192 NR::scale s(1, 1);
1193 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1194 if (isNaN(s[dim])) {
1195 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1196 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1197 }
1198 if (!( fabs(s[dim]) >= 1e-15 )) {
1199 s[dim] = 1e-15;
1200 }
1201 if (state & GDK_CONTROL_MASK) {
1202 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1203 s[!dim] = fabs(s[dim]);
1204 }
1206 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1207 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1209 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1210 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1211 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1213 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1214 }
1216 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1217 {
1218 NR::Point const offset = _point - _origin;
1220 NR::scale s (1, 1);
1221 for (int i = NR::X; i <= NR::Y; i++) {
1222 if (fabs(offset[i]) > 1e-9)
1223 s[i] = (pt[i] - _origin[i]) / offset[i];
1224 if (fabs(s[i]) < 1e-9)
1225 s[i] = 1e-9;
1226 }
1227 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1228 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1230 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1231 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1232 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1234 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1235 }
1237 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1238 {
1239 NR::Point const offset = _point - _origin;
1241 unsigned dim;
1242 switch (handle.cursor) {
1243 case GDK_SB_H_DOUBLE_ARROW:
1244 dim = NR::Y;
1245 break;
1246 case GDK_SB_V_DOUBLE_ARROW:
1247 dim = NR::X;
1248 break;
1249 default:
1250 g_assert_not_reached();
1251 abort();
1252 break;
1253 }
1254 if (fabs(offset[dim]) < 1e-15) {
1255 return;
1256 }
1257 NR::Matrix skew = NR::identity();
1258 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1259 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1260 skew[2*(1-dim) + (dim)] = 0;
1261 skew[2*(1-dim) + (1-dim)] = 1;
1263 for (int i = 0; i < 2; i++) {
1264 if (fabs(skew[3*i]) < 1e-15) {
1265 skew[3*i] = 1e-15;
1266 }
1267 }
1268 transform(skew, _origin);
1269 }
1271 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1272 {
1273 NR::Point const offset = _point - _origin;
1275 NR::Coord const h1 = NR::L2(offset);
1276 if (h1 < 1e-15) {
1277 return;
1278 }
1279 NR::Point const q1 = offset / h1;
1280 NR::Coord const h2 = NR::L2( pt - _origin );
1281 if (h2 < 1e-15) {
1282 return;
1283 }
1284 NR::Point const q2 = (pt - _origin) / h2;
1285 NR::rotate const r1(q1);
1286 NR::rotate const r2(q2);
1288 NR::Matrix rotate( r2 / r1 );
1289 transform(rotate, _origin);
1290 }
1292 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1293 {
1294 seltrans->setCenter(pt);
1295 }
1298 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1299 {
1300 SnapManager const &m = _desktop->namedview->snap_manager;
1302 /* The amount that we've moved by during this drag */
1303 NR::Point dxy = xy - _point;
1305 /* Get a STL list of the selected items.
1306 ** FIXME: this should probably be done by Inkscape::Selection.
1307 */
1308 std::list<SPItem const*> it;
1309 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1310 it.push_back(reinterpret_cast<SPItem*>(i->data));
1311 }
1313 bool const alt = (state & GDK_MOD1_MASK);
1314 bool const control = (state & GDK_CONTROL_MASK);
1315 bool const shift = (state & GDK_SHIFT_MASK);
1317 if (alt) {
1319 /* Alt pressed means keep offset: snap the moved distance to the grid.
1320 ** FIXME: this will snap to more than just the grid, nowadays.
1321 */
1323 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1325 } else if (!shift) {
1327 /* We're snapping to things, possibly with a constraint to horizontal or
1328 ** vertical movement. Obtain a list of possible translations and then
1329 ** pick the smallest.
1330 */
1332 /* This will be our list of possible translations */
1333 std::list<std::pair<NR::Point, bool> > s;
1335 if (control) {
1337 /* Snap to things, and also constrain to horizontal or vertical movement */
1339 for (unsigned int dim = 0; dim < 2; dim++) {
1340 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1341 _bbox_points,
1342 it,
1343 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1344 dxy));
1346 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1347 _snap_points,
1348 it,
1349 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1350 dxy));
1351 }
1353 } else {
1355 /* Snap to things with no constraint */
1357 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1358 _bbox_points, it, dxy));
1359 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1360 _snap_points, it, dxy));
1361 }
1363 /* Pick one */
1364 NR::Coord best = NR_HUGE;
1365 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1366 if (i->second) {
1367 NR::Coord const m = NR::L2(i->first);
1368 if (m < best) {
1369 best = m;
1370 dxy = i->first;
1371 }
1372 }
1373 }
1374 }
1376 if (control) {
1377 /* Ensure that the horizontal and vertical constraint has been applied */
1378 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1379 dxy[NR::Y] = 0;
1380 } else {
1381 dxy[NR::X] = 0;
1382 }
1383 }
1385 NR::Matrix const move((NR::translate(dxy)));
1386 NR::Point const norm(0, 0);
1387 transform(move, norm);
1389 // status text
1390 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1391 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1392 _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);
1393 g_string_free(xs, TRUE);
1394 g_string_free(ys, TRUE);
1395 }
1398 /*
1399 Local Variables:
1400 mode:c++
1401 c-file-style:"stroustrup"
1402 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1403 indent-tabs-mode:nil
1404 fill-column:99
1405 End:
1406 */
1407 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :