Code

fix setting and reading rotation centers
[inkscape.git] / src / seltrans.cpp
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);
122     
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);
135     
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         );
153 Inkscape::SelTrans::~SelTrans()
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();
196 void Inkscape::SelTrans::resetState()
198     _state = STATE_SCALE;
201 void Inkscape::SelTrans::increaseState()
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();
214 void Inkscape::SelTrans::setCenter(NR::Point const &p)
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();
230 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
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);
289 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
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();
319 void Inkscape::SelTrans::ungrab()
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();
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()
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 = &copy_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     }
432 void Inkscape::SelTrans::_updateHandles()
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);
445         g_object_set(G_OBJECT(_chandle),
446                      "anchor", handle_center.anchor,
447                      "shape", SP_CTRL_SHAPE_BITMAP,
448                      "size", 13,
449                      "mode", SP_CTRL_MODE_XOR,
450                      "fill", 0x00000000,
451                      "fill_mouseover", 0x00000000,
452                      "stroke", 0x000000ff,
453                      "stroke_mouseover", 0xff0000b0,
454                      "pixbuf", handles[handle_center.control],
455                      "tip", _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"),
456                      NULL);
457         g_signal_connect(G_OBJECT(_chandle), "request",
458                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
459         g_signal_connect(G_OBJECT(_chandle), "moved",
460                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
461         g_signal_connect(G_OBJECT(_chandle), "grabbed",
462                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
463         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
464                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
465         g_signal_connect(G_OBJECT(_chandle), "clicked",
466                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
467     }
469     sp_remove_handles(&_chandle, 1);
470     if ( _state == STATE_SCALE ) {
471         sp_remove_handles(_rhandle, 8);
472         _showHandles(_shandle, handles_scale, 8,
473                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
474                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
475     } else {
476         sp_remove_handles(_shandle, 8);
477         _showHandles(_rhandle, handles_rotate, 8,
478                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
479                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
480     }
482     if (!_center_is_set) {
483         // Extract the position of the center from the first selected object
484         GSList *items = (GSList *) _desktop->selection->itemList();
485         if (items) {
486             SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
487             if (first->isCenterSet()) { // only if set explicitly
488                 _center =  first->getCenter(); 
489             } else {
490                 _center = _box.midpoint();
491             }
492         } else {
493             _center = _box.midpoint();
494         }
495         _center_is_set = true;
496     }
498     if ( _state == STATE_SCALE ) {
499         sp_knot_hide(_chandle);
500     } else {
501         sp_knot_show(_chandle);
502         sp_knot_moveto(_chandle, &_center);
503     }
506 void Inkscape::SelTrans::_updateVolatileState()
508     Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
509     _empty = selection->isEmpty();
511     if (_empty) {
512         return;
513     }
515     _box = selection->bounds();
516     if (_box.isEmpty()) {
517         _empty = true;
518         return;
519     }
521     _strokewidth = stroke_average_width (selection->itemList());
523     _current.set_identity();
526 static void sp_remove_handles(SPKnot *knot[], gint num)
528     for (int i = 0; i < num; i++) {
529         if (knot[i] != NULL) {
530             sp_knot_hide(knot[i]);
531         }
532     }
535 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
536                              gchar const *even_tip, gchar const *odd_tip)
538     g_return_if_fail( !_empty );
540     for (int i = 0; i < num; i++) {
541         if (knot[i] == NULL) {
542             knot[i] = sp_knot_new(_desktop);
543             g_object_set(G_OBJECT(knot[i]),
544                          "anchor", handle[i].anchor,
545                          "shape", SP_CTRL_SHAPE_BITMAP,
546                          "size", 13,
547                          "mode", SP_KNOT_MODE_XOR,
548                          "fill", 0x000000ff, // inversion
549                          "fill_mouseover", 0x00ff6600, // green
550                          "stroke", 0x000000ff, // inversion
551                          "stroke_mouseover", 0x000000ff, // inversion
552                          "pixbuf", handles[handle[i].control],
553                          "tip", i % 2 ? even_tip : odd_tip,
554                          NULL);
556             g_signal_connect(G_OBJECT(knot[i]), "request",
557                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
558             g_signal_connect(G_OBJECT(knot[i]), "moved",
559                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
560             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
561                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
562             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
563                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
564             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
565         }
566         sp_knot_show(knot[i]);
568         NR::Point const handle_pt(handle[i].x, handle[i].y);
569         NR::Point p( _box.min()
570                      + ( _box.dimensions()
571                          * NR::scale(handle_pt) ) );
573         sp_knot_moveto(knot[i], &p);
574     }
577 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
579     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
580         knot, state, *(SPSelTransHandle const *) data
581         );
584 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
586     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
589 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
591     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
592         knot, position, state, *(SPSelTransHandle const *) data
593         );
596 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
598     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
599         knot, position, state, *(SPSelTransHandle const *) data
600         );
603 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
605     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
606         knot, state, *(SPSelTransHandle const *) data
607         );
610 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
612     switch (handle.anchor) {
613         case GTK_ANCHOR_CENTER:
614             if (state & GDK_SHIFT_MASK) {
615                 // Unset the  center position for all selected items
616                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
617                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
618                     it->unsetCenter();
619                     SP_OBJECT(it)->updateRepr();
620                 }
621                 sp_document_maybe_done (SP_DT_DOCUMENT(_desktop), "center::unset");
622             }
623             break;
624         default:
625             break;
626     }
629 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
631     switch (handle.anchor) {
632         case GTK_ANCHOR_CENTER:
633             g_object_set(G_OBJECT(_grip),
634                          "shape", SP_CTRL_SHAPE_BITMAP,
635                          "size", 13.0,
636                          NULL);
637             sp_canvas_item_show(_grip);
638             break;
639         default:
640             g_object_set(G_OBJECT(_grip),
641                          "shape", SP_CTRL_SHAPE_CROSS,
642                          "size", 7.0,
643                          NULL);
644             sp_canvas_item_show(_norm);
645             sp_canvas_item_show(_grip);
647             break;
648     }
650     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
654 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
656     if (!SP_KNOT_IS_GRABBED(knot)) {
657         return;
658     }
660     // in case items have been unhooked from the document, don't
661     // try to continue processing events for them.
662     for (unsigned int i = 0; i < _items.size(); i++) {
663         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
664             return;
665         }
666     }
668     handle.action(this, handle, *position, state);
672 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
674     if (!SP_KNOT_IS_GRABBED(knot)) {
675         return TRUE;
676     }
678     knot->desktop->set_coordinate_status(*position);
679     knot->desktop->setPosition(*position);
681     
682     if (state & GDK_MOD1_MASK) {
683         *position = _point + ( *position - _point ) / 10;
684     }
686     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
687         _origin = _opposite;
688     } else {
689         _origin = _center;
690     }
691     if (handle.request(this, handle, *position, state)) {
692         sp_knot_set_position(knot, position, state);
693         SP_CTRL(_grip)->moveto(*position);
694         SP_CTRL(_norm)->moveto(_origin);
695     }
697     return TRUE;
701 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
703     if (!_grabbed) {
704         _updateVolatileState();
705         _center_is_set = false; // center(s) may have changed
706         _updateHandles();
707     }
710 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
712     if (!_grabbed) {
713         _updateVolatileState();
715         // reset internal flag
716         _changed = false;
718         _center_is_set = false;  // center(s) may have changed
720         _updateHandles();
721     }
724 /*
725  * handlers for handle move-request
726  */
728 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
729 static double sign(double const x)
731     return ( x < 0
732              ? -1
733              : 1 );
736 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
737                                     SPSelTransHandle const &, NR::Point &pt, guint state)
739     return seltrans->scaleRequest(pt, state);
742 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
743                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
745     return seltrans->stretchRequest(handle, pt, state);
748 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
749                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
751     return seltrans->skewRequest(handle, pt, state);
754 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
755                                      SPSelTransHandle const &, NR::Point &pt, guint state)
757     return seltrans->rotateRequest(pt, state);
760 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
761                                      SPSelTransHandle const &, NR::Point &pt, guint state)
763     return seltrans->centerRequest(pt, state);
766 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
768     using NR::X;
769     using NR::Y;
771     NR::Point d = _point - _origin;
772     NR::scale s(0, 0);
774     /* Work out the new scale factors `s' */
775     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
776         if ( fabs(d[i]) > 0.001 ) {
777             s[i] = ( pt[i] - _origin[i] ) / d[i];
778             if ( fabs(s[i]) < 1e-9 ) {
779                 s[i] = 1e-9;
780             }
781         }
782     }
784     /* Get a STL list of the selected items.
785     ** FIXME: this should probably be done by Inkscape::Selection.
786     */
787     std::list<SPItem const*> it;
788     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
789         it.push_back(reinterpret_cast<SPItem*>(i->data));
790     }
792     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
793         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y] */
795         NR::Dim2 locked_dim;
797         /* Lock aspect ratio, using the smaller of the x and y factors */
798         if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
799             s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
800             locked_dim = NR::X;
801         } else {
802             s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
803             locked_dim = NR::Y;
804         }
806         /* Snap the scale factor */
807         std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
808                                                                 Snapper::BBOX_POINT, _bbox_points,
809                                                                 _origin, s, it);
810         std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
811                                                                 Snapper::SNAP_POINT, _snap_points,
812                                                                 _origin, s, it);
814         double bd = bb.second ? fabs(bb.first - s[locked_dim]) : NR_HUGE;
815         double sd = sn.second ? fabs(sn.first - s[locked_dim]) : NR_HUGE;
816         double r = (bd < sd) ? bb.first : sn.first;
818         for ( unsigned int i = 0 ; i < 2 ; i++ ) {
819             s[i] = r * sign(s[i]);
820         }
822     } else {
823         /* Scale aspect ratio is unlocked */
824         for ( unsigned int i = 0 ; i < 2 ; i++ ) {
825             std::pair<double, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview,
826                                                                        Snapper::BBOX_POINT, _bbox_points,
827                                                                        _origin, s[i], NR::Dim2(i), it);
828             std::pair<double, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview,
829                                                                        Snapper::SNAP_POINT, _snap_points,
830                                                                        _origin, s[i], NR::Dim2(i), it);
832             /* Pick the snap that puts us closest to the original scale */
833             NR::Coord bd = bb.second ? fabs(bb.first - s[i]) : NR_HUGE;
834             NR::Coord sd = sn.second ? fabs(sn.first - s[i]) : NR_HUGE;
835             s[i] = (bd < sd) ? bb.first : sn.first;
836         }
837     }
839     /* Update the knot position */
840     pt = ( _point - _origin ) * s + _origin;
842     /* Status text */
843     _message_context.setF(Inkscape::NORMAL_MESSAGE,
844                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
845                           100 * s[NR::X], 100 * s[NR::Y]);
847     return TRUE;
850 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
852     using NR::X;
853     using NR::Y;
855     NR::Dim2 axis, perp;
857     switch (handle.cursor) {
858         case GDK_TOP_SIDE:
859         case GDK_BOTTOM_SIDE:
860            axis = NR::Y;
861            perp = NR::X;
862            break;
863         case GDK_LEFT_SIDE:
864         case GDK_RIGHT_SIDE:
865            axis = NR::X;
866            perp = NR::Y;
867            break;
868         default:
869             g_assert_not_reached();
870             return TRUE;
871     };
873     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
874         return FALSE;
875     }
877     NR::scale s(1, 1);
878     s[axis] = ( ( pt[axis] - _origin[axis] )
879                 / ( _point[axis] - _origin[axis] ) );
880     if ( fabs(s[axis]) < 1e-15 ) {
881         s[axis] = 1e-15;
882     }
884     /* Get a STL list of the selected items.
885     ** FIXME: this should probably be done by Inkscape::Selection.
886     */
887     std::list<SPItem const*> it;
888     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
889         it.push_back(reinterpret_cast<SPItem*>(i->data));
890     }
891     
892     if ( state & GDK_CONTROL_MASK ) {
893         s[perp] = fabs(s[axis]);
895         std::pair<double, bool> sn = namedview_vector_snap_list(_desktop->namedview,
896                                                                 Snapper::BBOX_POINT,
897                                                                 _bbox_points, _origin, s, it);
898         std::pair<double, bool> bb = namedview_vector_snap_list(_desktop->namedview,
899                                                                 Snapper::SNAP_POINT,
900                                                                 _snap_points, _origin, s, it);
902         double bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
903         double sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
904         double ratio = (bd < sd) ? bb.first : sn.first;
906         s[axis] = fabs(ratio) * sign(s[axis]);
907         s[perp] = fabs(s[axis]);
908     } else {
909         std::pair<NR::Coord, bool> bb = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::BBOX_POINT,
910                                                                       _bbox_points, _origin,
911                                                                       s[axis], axis, it);
912         std::pair<NR::Coord, bool> sn = namedview_dim_snap_list_scale(_desktop->namedview, Snapper::SNAP_POINT,
913                                                                       _snap_points, _origin,
914                                                                       s[axis], axis, it);
916         /* Pick the snap that puts us closest to the original scale */
917         NR::Coord bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
918         NR::Coord sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
919         s[axis] = (bd < sd) ? bb.first : sn.first;
920     }
922     pt = ( _point - _origin ) * NR::scale(s) + _origin;
923     if (isNaN(pt[X] + pt[Y])) {
924         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
925                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
926     }
928     // status text
929     _message_context.setF(Inkscape::NORMAL_MESSAGE,
930                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
931                           100 * s[NR::X], 100 * s[NR::Y]);
933     return TRUE;
936 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
938     using NR::X;
939     using NR::Y;
941     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
942         return FALSE;
943     }
945     NR::Dim2 dim_a;
946     NR::Dim2 dim_b;
947     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
948         dim_a = X;
949         dim_b = Y;
950     } else {
951         dim_a = Y;
952         dim_b = X;
953     }
955     double skew[2];
956     double s[2] = { 1.0, 1.0 };
958     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
959         return FALSE;
960     }
962     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
964     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
966     if ( fabs(s[dim_a]) < 1 ) {
967         s[dim_a] = sign(s[dim_a]);
968     } else {
969         s[dim_a] = floor( s[dim_a] + 0.5 );
970     }
972     double radians = atan(skew[dim_a] / s[dim_a]);
974     if (state & GDK_CONTROL_MASK) {
976         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
978         if (snaps) {
979             double sections = floor( radians * snaps / M_PI + .5 );
980             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
981             radians = ( M_PI / snaps ) * sections;
982         }
983         skew[dim_a] = tan(radians) * s[dim_a];
984     } else {
985         skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview,
986                 Snapper::SNAP_POINT, _snap_points,
987                 _origin, skew[dim_a], dim_b);
988     }
990     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
991     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
993     /* status text */
994     double degrees = 180 / M_PI * radians;
995     if (degrees > 180) degrees -= 360;
996     if (degrees < -180) degrees += 360;
998     _message_context.setF(Inkscape::NORMAL_MESSAGE,
999                           // TRANSLATORS: don't modify the first ";"
1000                           // (it will NOT be displayed as ";" - only the second one will be)
1001                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1002                           degrees);
1004     return TRUE;
1007 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1009     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1011     // rotate affine in rotate
1012     NR::Point const d1 = _point - _origin;
1013     NR::Point const d2 = pt     - _origin;
1015     NR::Coord const h1 = NR::L2(d1);
1016     if (h1 < 1e-15) return FALSE;
1017     NR::Point q1 = d1 / h1;
1018     NR::Coord const h2 = NR::L2(d2);
1019     if (fabs(h2) < 1e-15) return FALSE;
1020     NR::Point q2 = d2 / h2;
1022     double radians;
1023     if (state & GDK_CONTROL_MASK) {
1024         /* Have to restrict movement. */
1025         double cos_t = NR::dot(q1, q2);
1026         double sin_t = NR::dot(NR::rot90(q1), q2);
1027         radians = atan2(sin_t, cos_t);
1028         if (snaps) {
1029             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1030         }
1031         q1 = NR::Point(1, 0);
1032         q2 = NR::Point(cos(radians), sin(radians));
1033     } else {
1034         radians = atan2(NR::dot(NR::rot90(d1), d2),
1035                         NR::dot(d1, d2));
1036     }
1038     NR::rotate const r1(q1);
1039     NR::rotate const r2(q2);
1040     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1042     /* status text */
1043     double degrees = 180 / M_PI * radians;
1044     if (degrees > 180) degrees -= 360;
1045     if (degrees < -180) degrees += 360;
1047     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1048                           // TRANSLATORS: don't modify the first ";"
1049                           // (it will NOT be displayed as ";" - only the second one will be)
1050                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1052     return TRUE;
1055 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1057     using NR::X;
1058     using NR::Y;
1060     SnapManager const m(_desktop->namedview);
1061     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1063     if (state & GDK_CONTROL_MASK) {
1064         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1065             pt[Y] = _point[Y];
1066         } else {
1067             pt[X] = _point[X];
1068         }
1069     }
1071     if (!(state & GDK_SHIFT_MASK)) {
1072         // screen pixels to snap center to bbox
1073 #define SNAP_DIST 5
1074         // FIXME: take from prefs
1075         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1077         for (int i = 0; i < 2; i++) {
1079             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1080                 pt[i] = _box.min()[i];
1081             }
1082             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1083                 pt[i] = _box.midpoint()[i];
1084             }
1085             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1086                 pt[i] = _box.max()[i];
1087             }
1088         }
1089     }
1091     // status text
1092     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1093     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1094     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1095     g_string_free(xs, FALSE);
1096     g_string_free(ys, FALSE);
1098     return TRUE;
1101 /*
1102  * handlers for handle movement
1103  *
1104  */
1106 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1108     seltrans->stretch(handle, pt, state);
1111 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1113     seltrans->scale(pt, state);
1116 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1118     seltrans->skew(handle, pt, state);
1121 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1123     seltrans->rotate(pt, state);
1125     
1126 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1128     using NR::X;
1129     using NR::Y;
1131     NR::Dim2 dim;
1132     switch (handle.cursor) {
1133         case GDK_LEFT_SIDE:
1134         case GDK_RIGHT_SIDE:
1135             dim = X;
1136             break;
1137         case GDK_TOP_SIDE:
1138         case GDK_BOTTOM_SIDE:
1139             dim = Y;
1140             break;
1141         default:
1142             g_assert_not_reached();
1143             abort();
1144             break;
1145     }
1147     NR::Point const scale_origin(_origin);
1148     double const offset = _point[dim] - scale_origin[dim];
1149     if (!( fabs(offset) >= 1e-15 )) {
1150         return;
1151     }
1152     NR::scale s(1, 1);
1153     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1154     if (isNaN(s[dim])) {
1155         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1156                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1157     }
1158     if (!( fabs(s[dim]) >= 1e-15 )) {
1159         s[dim] = 1e-15;
1160     }
1161     if (state & GDK_CONTROL_MASK) {
1162         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1163         s[!dim] = fabs(s[dim]);
1164     }
1166     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1167     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1169     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1170     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke, 
1171                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1173     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1176 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1178     NR::Point const offset = _point - _origin;
1180     NR::scale s (1, 1);
1181     for (int i = NR::X; i <= NR::Y; i++) {
1182         if (fabs(offset[i]) > 1e-9)
1183             s[i] = (pt[i] - _origin[i]) / offset[i];
1184         if (fabs(s[i]) < 1e-9)
1185             s[i] = 1e-9;
1186     }
1187     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1188     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1190     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1191     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke, 
1192                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1194     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1197 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1199     NR::Point const offset = _point - _origin;
1201     unsigned dim;
1202     switch (handle.cursor) {
1203         case GDK_SB_H_DOUBLE_ARROW:
1204             dim = NR::Y;
1205             break;
1206         case GDK_SB_V_DOUBLE_ARROW:
1207             dim = NR::X;
1208             break;
1209         default:
1210             g_assert_not_reached();
1211             abort();
1212             break;
1213     }
1214     if (fabs(offset[dim]) < 1e-15) {
1215         return;
1216     }
1217     NR::Matrix skew = NR::identity();
1218     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1219     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1220     skew[2*(1-dim) + (dim)] = 0;
1221     skew[2*(1-dim) + (1-dim)] = 1;
1223     for (int i = 0; i < 2; i++) {
1224         if (fabs(skew[3*i]) < 1e-15) {
1225             skew[3*i] = 1e-15;
1226         }
1227     }
1228     transform(skew, _origin);
1231 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1233     NR::Point const offset = _point - _origin;
1235     NR::Coord const h1 = NR::L2(offset);
1236     if (h1 < 1e-15) {
1237         return;
1238     }
1239     NR::Point const q1 = offset / h1;
1240     NR::Coord const h2 = NR::L2( pt - _origin );
1241     if (h2 < 1e-15) {
1242         return;
1243     }
1244     NR::Point const q2 = (pt - _origin) / h2;
1245     NR::rotate const r1(q1);
1246     NR::rotate const r2(q2);
1248     NR::Matrix rotate( r2 / r1 );
1249     transform(rotate, _origin);
1252 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1254     seltrans->setCenter(pt);
1258 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1260     SnapManager const m(_desktop->namedview);
1261     
1262     /* The amount that we've moved by during this drag */
1263     NR::Point dxy = xy - _point;
1265     /* Get a STL list of the selected items.
1266     ** FIXME: this should probably be done by Inkscape::Selection.
1267     */
1268     std::list<SPItem const*> it;
1269     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1270         it.push_back(reinterpret_cast<SPItem*>(i->data));
1271     }
1273     bool const alt = (state & GDK_MOD1_MASK);
1274     bool const control = (state & GDK_CONTROL_MASK);
1275     bool const shift = (state & GDK_SHIFT_MASK);
1277     if (alt) {
1279         /* Alt pressed means keep offset: snap the moved distance to the grid.
1280         ** FIXME: this will snap to more than just the grid, nowadays.
1281         */
1283         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1285     } else if (!shift) {
1287         /* We're snapping to things, possibly with a constraint to horizontal or
1288         ** vertical movement.  Obtain a list of possible translations and then
1289         ** pick the smallest.
1290         */
1292         /* This will be our list of possible translations */
1293         std::list<std::pair<NR::Point, bool> > s;
1294         
1295         if (control) {
1296             
1297             /* Snap to things, and also constrain to horizontal or vertical movement */
1299             for (unsigned int dim = 0; dim < 2; dim++) {
1300                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1301                                                          _bbox_points,
1302                                                          component_vectors[dim], it, dxy));
1303                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1304                                                          _snap_points,
1305                                                          component_vectors[dim], it, dxy));
1306             }
1307             
1308         } else {
1310             /* Snap to things with no constraint */
1312             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1313                                               _bbox_points, it, dxy));
1314             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1315                                               _snap_points, it, dxy));
1316         }
1318         /* Pick one */
1319         NR::Coord best = NR_HUGE;
1320         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1321             if (i->second) {
1322                 NR::Coord const m = NR::L2(i->first);
1323                 if (m < best) {
1324                     best = m;
1325                     dxy = i->first;
1326                 }
1327             }
1328         }
1329     }
1330     
1331     if (control) {
1332         /* Ensure that the horizontal and vertical constraint has been applied */
1333         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1334             dxy[NR::Y] = 0;
1335         } else {
1336             dxy[NR::X] = 0;
1337         }
1338     }
1339     
1340     NR::Matrix const move((NR::translate(dxy)));
1341     NR::Point const norm(0, 0);
1342     transform(move, norm);
1344     // status text
1345     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1346     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1347     _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);
1348     g_string_free(xs, TRUE);
1349     g_string_free(ys, TRUE);
1353 /*
1354   Local Variables:
1355   mode:c++
1356   c-file-style:"stroustrup"
1357   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1358   indent-tabs-mode:nil
1359   fill-column:99
1360   End:
1361 */
1362 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :