Code

fix undo bug in nodepath_preserve; fix crash caused by nodepath_cleanup deleting...
[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_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         );
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_desktop_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_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);
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_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();
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_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 = &copy_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     }
436 void Inkscape::SelTrans::_updateHandles()
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     }
497 void Inkscape::SelTrans::_updateVolatileState()
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();
517 static void sp_remove_handles(SPKnot *knot[], gint num)
519     for (int i = 0; i < num; i++) {
520         if (knot[i] != NULL) {
521             sp_knot_hide(knot[i]);
522         }
523     }
526 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
527                              gchar const *even_tip, gchar const *odd_tip)
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     }
565 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
567     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
568         knot, state, *(SPSelTransHandle const *) data
569         );
572 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
574     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
577 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
579     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
580         knot, position, state, *(SPSelTransHandle const *) data
581         );
584 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
586     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
587         knot, position, state, *(SPSelTransHandle const *) data
588         );
591 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
593     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
594         knot, state, *(SPSelTransHandle const *) data
595         );
598 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
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     }
619 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
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);
644 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
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);
662 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
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;
691 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
693     if (!_grabbed) {
694         _updateVolatileState();
695         _center_is_set = false; // center(s) may have changed
696         _updateHandles();
697     }
700 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
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     }
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)
721     return ( x < 0
722              ? -1
723              : 1 );
726 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
727                                     SPSelTransHandle const &, NR::Point &pt, guint state)
729     return seltrans->scaleRequest(pt, state);
732 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
733                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
735     return seltrans->stretchRequest(handle, pt, state);
738 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
739                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
741     return seltrans->skewRequest(handle, pt, state);
744 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
745                                      SPSelTransHandle const &, NR::Point &pt, guint state)
747     return seltrans->rotateRequest(pt, state);
750 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
751                                      SPSelTransHandle const &, NR::Point &pt, guint state)
753     return seltrans->centerRequest(pt, state);
756 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
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 */
829         
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;
864 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
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;
976 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
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         SnapManager const &m = _desktop->namedview->snap_manager;
1027         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1028                                                        _bbox_points,
1029                                                        std::list<SPItem const *>(),
1030                                                        skew[dim_a],
1031                                                        _origin,
1032                                                        dim_a);
1034         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1035                                                        _snap_points,
1036                                                        std::list<SPItem const *>(),
1037                                                        skew[dim_a],
1038                                                        _origin,
1039                                                        dim_a);
1040         
1041         if (bb.second || sn.second) {
1042             /* We snapped something, so change the skew to reflect it */
1043             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1044             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1045             skew[dim_a] = std::min(bd, sd);
1046         }
1047     }
1049     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1050     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1052     /* status text */
1053     double degrees = 180 / M_PI * radians;
1054     if (degrees > 180) degrees -= 360;
1055     if (degrees < -180) degrees += 360;
1057     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1058                           // TRANSLATORS: don't modify the first ";"
1059                           // (it will NOT be displayed as ";" - only the second one will be)
1060                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1061                           degrees);
1063     return TRUE;
1066 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1068     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1070     // rotate affine in rotate
1071     NR::Point const d1 = _point - _origin;
1072     NR::Point const d2 = pt     - _origin;
1074     NR::Coord const h1 = NR::L2(d1);
1075     if (h1 < 1e-15) return FALSE;
1076     NR::Point q1 = d1 / h1;
1077     NR::Coord const h2 = NR::L2(d2);
1078     if (fabs(h2) < 1e-15) return FALSE;
1079     NR::Point q2 = d2 / h2;
1081     double radians;
1082     if (state & GDK_CONTROL_MASK) {
1083         /* Have to restrict movement. */
1084         double cos_t = NR::dot(q1, q2);
1085         double sin_t = NR::dot(NR::rot90(q1), q2);
1086         radians = atan2(sin_t, cos_t);
1087         if (snaps) {
1088             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1089         }
1090         q1 = NR::Point(1, 0);
1091         q2 = NR::Point(cos(radians), sin(radians));
1092     } else {
1093         radians = atan2(NR::dot(NR::rot90(d1), d2),
1094                         NR::dot(d1, d2));
1095     }
1097     NR::rotate const r1(q1);
1098     NR::rotate const r2(q2);
1099     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1101     /* status text */
1102     double degrees = 180 / M_PI * radians;
1103     if (degrees > 180) degrees -= 360;
1104     if (degrees < -180) degrees += 360;
1106     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1107                           // TRANSLATORS: don't modify the first ";"
1108                           // (it will NOT be displayed as ";" - only the second one will be)
1109                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1111     return TRUE;
1114 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1116     using NR::X;
1117     using NR::Y;
1119     SnapManager const &m = _desktop->namedview->snap_manager;
1120     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1122     if (state & GDK_CONTROL_MASK) {
1123         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1124             pt[Y] = _point[Y];
1125         } else {
1126             pt[X] = _point[X];
1127         }
1128     }
1130     if (!(state & GDK_SHIFT_MASK)) {
1131         // screen pixels to snap center to bbox
1132 #define SNAP_DIST 5
1133         // FIXME: take from prefs
1134         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1136         for (int i = 0; i < 2; i++) {
1138             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1139                 pt[i] = _box.min()[i];
1140             }
1141             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1142                 pt[i] = _box.midpoint()[i];
1143             }
1144             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1145                 pt[i] = _box.max()[i];
1146             }
1147         }
1148     }
1150     // status text
1151     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1152     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1153     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1154     g_string_free(xs, FALSE);
1155     g_string_free(ys, FALSE);
1157     return TRUE;
1160 /*
1161  * handlers for handle movement
1162  *
1163  */
1165 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1167     seltrans->stretch(handle, pt, state);
1170 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1172     seltrans->scale(pt, state);
1175 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1177     seltrans->skew(handle, pt, state);
1180 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1182     seltrans->rotate(pt, state);
1185 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1187     using NR::X;
1188     using NR::Y;
1190     NR::Dim2 dim;
1191     switch (handle.cursor) {
1192         case GDK_LEFT_SIDE:
1193         case GDK_RIGHT_SIDE:
1194             dim = X;
1195             break;
1196         case GDK_TOP_SIDE:
1197         case GDK_BOTTOM_SIDE:
1198             dim = Y;
1199             break;
1200         default:
1201             g_assert_not_reached();
1202             abort();
1203             break;
1204     }
1206     NR::Point const scale_origin(_origin);
1207     double const offset = _point[dim] - scale_origin[dim];
1208     if (!( fabs(offset) >= 1e-15 )) {
1209         return;
1210     }
1211     NR::scale s(1, 1);
1212     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1213     if (isNaN(s[dim])) {
1214         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1215                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1216     }
1217     if (!( fabs(s[dim]) >= 1e-15 )) {
1218         s[dim] = 1e-15;
1219     }
1220     if (state & GDK_CONTROL_MASK) {
1221         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1222         s[!dim] = fabs(s[dim]);
1223     }
1225     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1226     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1228     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1229     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1230                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1232     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1235 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1237     NR::Point const offset = _point - _origin;
1239     NR::scale s (1, 1);
1240     for (int i = NR::X; i <= NR::Y; i++) {
1241         if (fabs(offset[i]) > 1e-9)
1242             s[i] = (pt[i] - _origin[i]) / offset[i];
1243         if (fabs(s[i]) < 1e-9)
1244             s[i] = 1e-9;
1245     }
1246     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1247     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1249     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1250     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1251                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1253     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1256 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1258     NR::Point const offset = _point - _origin;
1260     unsigned dim;
1261     switch (handle.cursor) {
1262         case GDK_SB_H_DOUBLE_ARROW:
1263             dim = NR::Y;
1264             break;
1265         case GDK_SB_V_DOUBLE_ARROW:
1266             dim = NR::X;
1267             break;
1268         default:
1269             g_assert_not_reached();
1270             abort();
1271             break;
1272     }
1273     if (fabs(offset[dim]) < 1e-15) {
1274         return;
1275     }
1276     NR::Matrix skew = NR::identity();
1277     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1278     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1279     skew[2*(1-dim) + (dim)] = 0;
1280     skew[2*(1-dim) + (1-dim)] = 1;
1282     for (int i = 0; i < 2; i++) {
1283         if (fabs(skew[3*i]) < 1e-15) {
1284             skew[3*i] = 1e-15;
1285         }
1286     }
1287     transform(skew, _origin);
1290 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1292     NR::Point const offset = _point - _origin;
1294     NR::Coord const h1 = NR::L2(offset);
1295     if (h1 < 1e-15) {
1296         return;
1297     }
1298     NR::Point const q1 = offset / h1;
1299     NR::Coord const h2 = NR::L2( pt - _origin );
1300     if (h2 < 1e-15) {
1301         return;
1302     }
1303     NR::Point const q2 = (pt - _origin) / h2;
1304     NR::rotate const r1(q1);
1305     NR::rotate const r2(q2);
1307     NR::Matrix rotate( r2 / r1 );
1308     transform(rotate, _origin);
1311 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1313     seltrans->setCenter(pt);
1317 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1319     SnapManager const &m = _desktop->namedview->snap_manager;
1321     /* The amount that we've moved by during this drag */
1322     NR::Point dxy = xy - _point;
1324     /* Get a STL list of the selected items.
1325     ** FIXME: this should probably be done by Inkscape::Selection.
1326     */
1327     std::list<SPItem const*> it;
1328     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1329         it.push_back(reinterpret_cast<SPItem*>(i->data));
1330     }
1332     bool const alt = (state & GDK_MOD1_MASK);
1333     bool const control = (state & GDK_CONTROL_MASK);
1334     bool const shift = (state & GDK_SHIFT_MASK);
1336     if (alt) {
1338         /* Alt pressed means keep offset: snap the moved distance to the grid.
1339         ** FIXME: this will snap to more than just the grid, nowadays.
1340         */
1342         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1344     } else if (!shift) {
1346         /* We're snapping to things, possibly with a constraint to horizontal or
1347         ** vertical movement.  Obtain a list of possible translations and then
1348         ** pick the smallest.
1349         */
1351         /* This will be our list of possible translations */
1352         std::list<std::pair<NR::Point, bool> > s;
1354         if (control) {
1356             /* Snap to things, and also constrain to horizontal or vertical movement */
1358             for (unsigned int dim = 0; dim < 2; dim++) {
1359                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1360                                                          _bbox_points,
1361                                                          it,
1362                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1363                                                          dxy));
1364                             
1365                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1366                                                          _snap_points,
1367                                                          it,
1368                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1369                                                          dxy));
1370             }
1372         } else {
1374             /* Snap to things with no constraint */
1376             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1377                                               _bbox_points, it, dxy));
1378             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1379                                               _snap_points, it, dxy));
1380         }
1382         /* Pick one */
1383         NR::Coord best = NR_HUGE;
1384         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1385             if (i->second) {
1386                 NR::Coord const m = NR::L2(i->first);
1387                 if (m < best) {
1388                     best = m;
1389                     dxy = i->first;
1390                 }
1391             }
1392         }
1393     }
1395     if (control) {
1396         /* Ensure that the horizontal and vertical constraint has been applied */
1397         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1398             dxy[NR::Y] = 0;
1399         } else {
1400             dxy[NR::X] = 0;
1401         }
1402     }
1404     NR::Matrix const move((NR::translate(dxy)));
1405     NR::Point const norm(0, 0);
1406     transform(move, norm);
1408     // status text
1409     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1410     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1411     _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);
1412     g_string_free(xs, TRUE);
1413     g_string_free(ys, TRUE);
1417 /*
1418   Local Variables:
1419   mode:c++
1420   c-file-style:"stroustrup"
1421   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1422   indent-tabs-mode:nil
1423   fill-column:99
1424   End:
1425 */
1426 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :