Code

Applied patch #1503864
[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->getSnapPointsConvexHull();
257     _bbox_points = selection->getBBoxPointsOuter();
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     bool fixup = !_grabbed;
384     if ( fixup && _stamp_cache ) {
385         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
386         g_slist_free(_stamp_cache);
387         _stamp_cache = NULL;
388     }
390     /* stamping mode */
391     if (!_empty) {
392         GSList *l;
393         if (_stamp_cache) {
394             l = _stamp_cache;
395         } else {
396             /* Build cache */
397             l  = g_slist_copy((GSList *) selection->itemList());
398             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
399             _stamp_cache = l;
400         }
402         while (l) {
403             SPItem *original_item = SP_ITEM(l->data);
404             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
406             // remember the position of the item
407             gint pos = original_repr->position();
408             // remember parent
409             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
411             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
413             // add the new repr to the parent
414             parent->appendChild(copy_repr);
415             // move to the saved position
416             copy_repr->setPosition(pos > 0 ? pos : 0);
418             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
420             NR::Matrix const *new_affine;
421             if (_show == SHOW_OUTLINE) {
422                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
423                 NR::Matrix const i2dnew( i2d * _current );
424                 sp_item_set_i2d_affine(copy_item, i2dnew);
425                 new_affine = &copy_item->transform;
426             } else {
427                 new_affine = &original_item->transform;
428             }
430             sp_item_write_transform(copy_item, copy_repr, *new_affine);
432             if (copy_item->isCenterSet()) {
433                 copy_item->setCenter(_center * _current);
434             }
436             Inkscape::GC::release(copy_repr);
437             l = l->next;
438         }
439         sp_document_done(sp_desktop_document(_desktop));
440     }
442     if ( fixup && _stamp_cache ) {
443         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
444         g_slist_free(_stamp_cache);
445         _stamp_cache = NULL;
446     }
449 void Inkscape::SelTrans::_updateHandles()
451     if ( !_show_handles || _empty )
452     {
453         sp_remove_handles(_shandle, 8);
454         sp_remove_handles(_rhandle, 8);
455         sp_remove_handles(&_chandle, 1);
456         return;
457     }
459     // center handle
460     if ( _chandle == NULL ) {
461         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
463         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
464         _chandle->setSize (13);
465         _chandle->setAnchor (handle_center.anchor);
466         _chandle->setMode (SP_CTRL_MODE_XOR);
467         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
468         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
469         _chandle->setPixbuf(handles[handle_center.control]);
470         sp_knot_update_ctrl(_chandle);
472         g_signal_connect(G_OBJECT(_chandle), "request",
473                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
474         g_signal_connect(G_OBJECT(_chandle), "moved",
475                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
476         g_signal_connect(G_OBJECT(_chandle), "grabbed",
477                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
478         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
479                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
480         g_signal_connect(G_OBJECT(_chandle), "clicked",
481                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
482     }
484     sp_remove_handles(&_chandle, 1);
485     if ( _state == STATE_SCALE ) {
486         sp_remove_handles(_rhandle, 8);
487         _showHandles(_shandle, handles_scale, 8,
488                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
489                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
490     } else {
491         sp_remove_handles(_shandle, 8);
492         _showHandles(_rhandle, handles_rotate, 8,
493                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
494                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
495     }
497     if (!_center_is_set) {
498         _center = _desktop->selection->center();
499         _center_is_set = true;
500     }
502     if ( _state == STATE_SCALE ) {
503         sp_knot_hide(_chandle);
504     } else {
505         sp_knot_show(_chandle);
506         sp_knot_moveto(_chandle, &_center);
507     }
510 void Inkscape::SelTrans::_updateVolatileState()
512     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
513     _empty = selection->isEmpty();
515     if (_empty) {
516         return;
517     }
519     _box = selection->bounds();
520     if (_box.isEmpty()) {
521         _empty = true;
522         return;
523     }
525     _strokewidth = stroke_average_width (selection->itemList());
527     _current.set_identity();
530 static void sp_remove_handles(SPKnot *knot[], gint num)
532     for (int i = 0; i < num; i++) {
533         if (knot[i] != NULL) {
534             sp_knot_hide(knot[i]);
535         }
536     }
539 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
540                              gchar const *even_tip, gchar const *odd_tip)
542     g_return_if_fail( !_empty );
544     for (int i = 0; i < num; i++) {
545         if (knot[i] == NULL) {
546             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
548             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
549             knot[i]->setSize (13);
550             knot[i]->setAnchor (handle[i].anchor);
551             knot[i]->setMode (SP_CTRL_MODE_XOR);
552             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
553             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
554             knot[i]->setPixbuf(handles[handle[i].control]);
555             sp_knot_update_ctrl(knot[i]);
557             g_signal_connect(G_OBJECT(knot[i]), "request",
558                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
559             g_signal_connect(G_OBJECT(knot[i]), "moved",
560                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
561             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
562                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
563             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
564                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
565             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
566         }
567         sp_knot_show(knot[i]);
569         NR::Point const handle_pt(handle[i].x, handle[i].y);
570         NR::Point p( _box.min()
571                      + ( _box.dimensions()
572                          * NR::scale(handle_pt) ) );
574         sp_knot_moveto(knot[i], &p);
575     }
578 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
580     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
581         knot, state, *(SPSelTransHandle const *) data
582         );
585 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
587     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
590 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
592     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
593         knot, position, state, *(SPSelTransHandle const *) data
594         );
597 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
599     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
600         knot, position, state, *(SPSelTransHandle const *) data
601         );
604 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
606     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
607         knot, state, *(SPSelTransHandle const *) data
608         );
611 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
613     switch (handle.anchor) {
614         case GTK_ANCHOR_CENTER:
615             if (state & GDK_SHIFT_MASK) {
616                 // Unset the  center position for all selected items
617                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
618                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
619                     it->unsetCenter();
620                     SP_OBJECT(it)->updateRepr();
621                     _center_is_set = false;  // center has changed
622                     _updateHandles();
623                 }
624                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset");
625             }
626             break;
627         default:
628             break;
629     }
632 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
634     switch (handle.anchor) {
635         case GTK_ANCHOR_CENTER:
636             g_object_set(G_OBJECT(_grip),
637                          "shape", SP_CTRL_SHAPE_BITMAP,
638                          "size", 13.0,
639                          NULL);
640             sp_canvas_item_show(_grip);
641             break;
642         default:
643             g_object_set(G_OBJECT(_grip),
644                          "shape", SP_CTRL_SHAPE_CROSS,
645                          "size", 7.0,
646                          NULL);
647             sp_canvas_item_show(_norm);
648             sp_canvas_item_show(_grip);
650             break;
651     }
653     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
657 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
659     if (!SP_KNOT_IS_GRABBED(knot)) {
660         return;
661     }
663     // in case items have been unhooked from the document, don't
664     // try to continue processing events for them.
665     for (unsigned int i = 0; i < _items.size(); i++) {
666         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
667             return;
668         }
669     }
671     handle.action(this, handle, *position, state);
675 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
677     if (!SP_KNOT_IS_GRABBED(knot)) {
678         return TRUE;
679     }
681     knot->desktop->set_coordinate_status(*position);
682     knot->desktop->setPosition(*position);
685     if (state & GDK_MOD1_MASK) {
686         *position = _point + ( *position - _point ) / 10;
687     }
689     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
690         _origin = _opposite;
691     } else {
692         _origin = _center;
693     }
694     if (handle.request(this, handle, *position, state)) {
695         sp_knot_set_position(knot, position, state);
696         SP_CTRL(_grip)->moveto(*position);
697         SP_CTRL(_norm)->moveto(_origin);
698     }
700     return TRUE;
704 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
706     if (!_grabbed) {
707         _updateVolatileState();
708         _center_is_set = false; // center(s) may have changed
709         _updateHandles();
710     }
713 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
715     if (!_grabbed) {
716         _updateVolatileState();
718         // reset internal flag
719         _changed = false;
721         _center_is_set = false;  // center(s) may have changed
723         _updateHandles();
724     }
727 /*
728  * handlers for handle move-request
729  */
731 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
732 static double sign(double const x)
734     return ( x < 0
735              ? -1
736              : 1 );
739 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
740                                     SPSelTransHandle const &, NR::Point &pt, guint state)
742     return seltrans->scaleRequest(pt, state);
745 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
746                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
748     return seltrans->stretchRequest(handle, pt, state);
751 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
752                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
754     return seltrans->skewRequest(handle, pt, state);
757 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
758                                      SPSelTransHandle const &, NR::Point &pt, guint state)
760     return seltrans->rotateRequest(pt, state);
763 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
764                                      SPSelTransHandle const &, NR::Point &pt, guint state)
766     return seltrans->centerRequest(pt, state);
769 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
771     using NR::X;
772     using NR::Y;
774     NR::Point d = _point - _origin;
775     NR::scale s(0, 0);
777     /* Work out the new scale factors `s' */
778     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
779         if ( fabs(d[i]) > 0.001 ) {
780             s[i] = ( pt[i] - _origin[i] ) / d[i];
781             if ( fabs(s[i]) < 1e-9 ) {
782                 s[i] = 1e-9;
783             }
784         }
785     }
787     SnapManager const &m = _desktop->namedview->snap_manager;
789     /* Get a STL list of the selected items.
790     ** FIXME: this should probably be done by Inkscape::Selection.
791     */
792     std::list<SPItem const*> it;
793     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
794         it.push_back(reinterpret_cast<SPItem*>(i->data));
795     }
797     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
798         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
799         ** To do this, we snap along a suitable constraint vector from the origin.
800         */
802         NR::Point const cv = NR::Point(
803             pt[NR::X] > _origin[NR::X] ? 1 : -1,
804             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
805             );
807         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
808                                                                _bbox_points,
809                                                                it,
810                                                                Snapper::ConstraintLine(_origin, cv),
811                                                                s,
812                                                                _origin);
814         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
815                                                                _snap_points,
816                                                                it,
817                                                                Snapper::ConstraintLine(_origin, cv),
818                                                                s,
819                                                                _origin);
821         if (bb.second == false && sn.second == false) {
823             /* We didn't snap, so just lock aspect ratio */
824             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
825                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
826             } else {
827                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
828             }
830         } else {
832             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
833             ** just compare difference in s[X].
834             */
835             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
836             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
837             s = (bd < sd) ? bb.first : sn.first;
838         }
840     } else {
841         /* Scale aspect ratio is unlocked */
842         
843         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
844                                                         _bbox_points,
845                                                         it,
846                                                         s,
847                                                         _origin);
848         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
849                                                         _snap_points,
850                                                         it,
851                                                         s,
852                                                         _origin);
854         /* Pick the snap that puts us closest to the original scale */
855         NR::Coord bd = bb.second ?
856             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
857                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
858             : NR_HUGE;
859         NR::Coord sd = sn.second ?
860             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
861                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
862             : NR_HUGE;
863         s = (bd < sd) ? bb.first : sn.first;
864     }
866     /* Update the knot position */
867     pt = ( _point - _origin ) * s + _origin;
869     /* Status text */
870     _message_context.setF(Inkscape::NORMAL_MESSAGE,
871                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
872                           100 * s[NR::X], 100 * s[NR::Y]);
874     return TRUE;
877 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
879     using NR::X;
880     using NR::Y;
882     NR::Dim2 axis, perp;
884     switch (handle.cursor) {
885         case GDK_TOP_SIDE:
886         case GDK_BOTTOM_SIDE:
887            axis = NR::Y;
888            perp = NR::X;
889            break;
890         case GDK_LEFT_SIDE:
891         case GDK_RIGHT_SIDE:
892            axis = NR::X;
893            perp = NR::Y;
894            break;
895         default:
896             g_assert_not_reached();
897             return TRUE;
898     };
900     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
901         return FALSE;
902     }
904     NR::scale s(1, 1);
905     s[axis] = ( ( pt[axis] - _origin[axis] )
906                 / ( _point[axis] - _origin[axis] ) );
907     if ( fabs(s[axis]) < 1e-15 ) {
908         s[axis] = 1e-15;
909     }
911     /* Get a STL list of the selected items.
912     ** FIXME: this should probably be done by Inkscape::Selection.
913     */
914     std::list<SPItem const*> it;
915     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
916         it.push_back(reinterpret_cast<SPItem*>(i->data));
917     }
919     SnapManager const &m = _desktop->namedview->snap_manager;
921     if ( state & GDK_CONTROL_MASK ) {
922         s[perp] = fabs(s[axis]);
924         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
925             Snapper::BBOX_POINT,
926             _bbox_points,
927             it,
928             s[axis],
929             _origin,
930             axis,
931             true);
933         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
934             Snapper::SNAP_POINT,
935             _snap_points,
936             it,
937             s[axis],
938             _origin,
939             axis,
940             true);
942         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
943         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
944         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
946         s[axis] = fabs(ratio) * sign(s[axis]);
947         s[perp] = fabs(s[axis]);
948     } else {
950         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
951             Snapper::BBOX_POINT,
952             _bbox_points,
953             it,
954             s[axis],
955             _origin,
956             axis,
957             false);
959         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
960             Snapper::SNAP_POINT,
961             _snap_points,
962             it,
963             s[axis],
964             _origin,
965             axis,
966             false);
968         /* Choose the smaller difference in scale */
969         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
970         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
971         s[axis] = (bd < sd) ? bb.first : sn.first;
972         s[perp] = 1;
973     }
975     pt = ( _point - _origin ) * NR::scale(s) + _origin;
976     if (isNaN(pt[X] + pt[Y])) {
977         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
978                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
979     }
981     // status text
982     _message_context.setF(Inkscape::NORMAL_MESSAGE,
983                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
984                           100 * s[NR::X], 100 * s[NR::Y]);
986     return TRUE;
989 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
991     using NR::X;
992     using NR::Y;
994     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
995         return FALSE;
996     }
998     NR::Dim2 dim_a;
999     NR::Dim2 dim_b;
1000     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1001         dim_a = X;
1002         dim_b = Y;
1003     } else {
1004         dim_a = Y;
1005         dim_b = X;
1006     }
1008     double skew[2];
1009     double s[2] = { 1.0, 1.0 };
1011     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1012         return FALSE;
1013     }
1015     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1017     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1019     if ( fabs(s[dim_a]) < 1 ) {
1020         s[dim_a] = sign(s[dim_a]);
1021     } else {
1022         s[dim_a] = floor( s[dim_a] + 0.5 );
1023     }
1025     double radians = atan(skew[dim_a] / s[dim_a]);
1027     if (state & GDK_CONTROL_MASK) {
1029         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1031         if (snaps) {
1032             double sections = floor( radians * snaps / M_PI + .5 );
1033             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1034             radians = ( M_PI / snaps ) * sections;
1035         }
1036         skew[dim_a] = tan(radians) * s[dim_a];
1037     } else {
1038         SnapManager const &m = _desktop->namedview->snap_manager;
1040         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1041                                                        _bbox_points,
1042                                                        std::list<SPItem const *>(),
1043                                                        skew[dim_a],
1044                                                        _origin,
1045                                                        dim_a);
1047         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1048                                                        _snap_points,
1049                                                        std::list<SPItem const *>(),
1050                                                        skew[dim_a],
1051                                                        _origin,
1052                                                        dim_a);
1053         
1054         if (bb.second || sn.second) {
1055             /* We snapped something, so change the skew to reflect it */
1056             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1057             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1058             skew[dim_a] = std::min(bd, sd);
1059         }
1060     }
1062     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1063     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1065     /* status text */
1066     double degrees = 180 / M_PI * radians;
1067     if (degrees > 180) degrees -= 360;
1068     if (degrees < -180) degrees += 360;
1070     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1071                           // TRANSLATORS: don't modify the first ";"
1072                           // (it will NOT be displayed as ";" - only the second one will be)
1073                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1074                           degrees);
1076     return TRUE;
1079 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1081     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1083     // rotate affine in rotate
1084     NR::Point const d1 = _point - _origin;
1085     NR::Point const d2 = pt     - _origin;
1087     NR::Coord const h1 = NR::L2(d1);
1088     if (h1 < 1e-15) return FALSE;
1089     NR::Point q1 = d1 / h1;
1090     NR::Coord const h2 = NR::L2(d2);
1091     if (fabs(h2) < 1e-15) return FALSE;
1092     NR::Point q2 = d2 / h2;
1094     double radians;
1095     if (state & GDK_CONTROL_MASK) {
1096         /* Have to restrict movement. */
1097         double cos_t = NR::dot(q1, q2);
1098         double sin_t = NR::dot(NR::rot90(q1), q2);
1099         radians = atan2(sin_t, cos_t);
1100         if (snaps) {
1101             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1102         }
1103         q1 = NR::Point(1, 0);
1104         q2 = NR::Point(cos(radians), sin(radians));
1105     } else {
1106         radians = atan2(NR::dot(NR::rot90(d1), d2),
1107                         NR::dot(d1, d2));
1108     }
1110     NR::rotate const r1(q1);
1111     NR::rotate const r2(q2);
1112     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1114     /* status text */
1115     double degrees = 180 / M_PI * radians;
1116     if (degrees > 180) degrees -= 360;
1117     if (degrees < -180) degrees += 360;
1119     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1120                           // TRANSLATORS: don't modify the first ";"
1121                           // (it will NOT be displayed as ";" - only the second one will be)
1122                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1124     return TRUE;
1127 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1129     using NR::X;
1130     using NR::Y;
1132     SnapManager const &m = _desktop->namedview->snap_manager;
1133     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1135     if (state & GDK_CONTROL_MASK) {
1136         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1137             pt[Y] = _point[Y];
1138         } else {
1139             pt[X] = _point[X];
1140         }
1141     }
1143     if (!(state & GDK_SHIFT_MASK)) {
1144         // screen pixels to snap center to bbox
1145 #define SNAP_DIST 5
1146         // FIXME: take from prefs
1147         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1149         for (int i = 0; i < 2; i++) {
1151             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1152                 pt[i] = _box.min()[i];
1153             }
1154             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1155                 pt[i] = _box.midpoint()[i];
1156             }
1157             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1158                 pt[i] = _box.max()[i];
1159             }
1160         }
1161     }
1163     // status text
1164     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1165     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1166     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1167     g_string_free(xs, FALSE);
1168     g_string_free(ys, FALSE);
1170     return TRUE;
1173 /*
1174  * handlers for handle movement
1175  *
1176  */
1178 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1180     seltrans->stretch(handle, pt, state);
1183 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1185     seltrans->scale(pt, state);
1188 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1190     seltrans->skew(handle, pt, state);
1193 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1195     seltrans->rotate(pt, state);
1198 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1200     using NR::X;
1201     using NR::Y;
1203     NR::Dim2 dim;
1204     switch (handle.cursor) {
1205         case GDK_LEFT_SIDE:
1206         case GDK_RIGHT_SIDE:
1207             dim = X;
1208             break;
1209         case GDK_TOP_SIDE:
1210         case GDK_BOTTOM_SIDE:
1211             dim = Y;
1212             break;
1213         default:
1214             g_assert_not_reached();
1215             abort();
1216             break;
1217     }
1219     NR::Point const scale_origin(_origin);
1220     double const offset = _point[dim] - scale_origin[dim];
1221     if (!( fabs(offset) >= 1e-15 )) {
1222         return;
1223     }
1224     NR::scale s(1, 1);
1225     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1226     if (isNaN(s[dim])) {
1227         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1228                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1229     }
1230     if (!( fabs(s[dim]) >= 1e-15 )) {
1231         s[dim] = 1e-15;
1232     }
1233     if (state & GDK_CONTROL_MASK) {
1234         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1235         s[!dim] = fabs(s[dim]);
1236     }
1238     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1239     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1241     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1242     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1243                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1245     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1248 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1250     NR::Point const offset = _point - _origin;
1252     NR::scale s (1, 1);
1253     for (int i = NR::X; i <= NR::Y; i++) {
1254         if (fabs(offset[i]) > 1e-9)
1255             s[i] = (pt[i] - _origin[i]) / offset[i];
1256         if (fabs(s[i]) < 1e-9)
1257             s[i] = 1e-9;
1258     }
1259     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1260     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1262     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1263     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1264                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1266     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1269 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1271     NR::Point const offset = _point - _origin;
1273     unsigned dim;
1274     switch (handle.cursor) {
1275         case GDK_SB_H_DOUBLE_ARROW:
1276             dim = NR::Y;
1277             break;
1278         case GDK_SB_V_DOUBLE_ARROW:
1279             dim = NR::X;
1280             break;
1281         default:
1282             g_assert_not_reached();
1283             abort();
1284             break;
1285     }
1286     if (fabs(offset[dim]) < 1e-15) {
1287         return;
1288     }
1289     NR::Matrix skew = NR::identity();
1290     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1291     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1292     skew[2*(1-dim) + (dim)] = 0;
1293     skew[2*(1-dim) + (1-dim)] = 1;
1295     for (int i = 0; i < 2; i++) {
1296         if (fabs(skew[3*i]) < 1e-15) {
1297             skew[3*i] = 1e-15;
1298         }
1299     }
1300     transform(skew, _origin);
1303 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1305     NR::Point const offset = _point - _origin;
1307     NR::Coord const h1 = NR::L2(offset);
1308     if (h1 < 1e-15) {
1309         return;
1310     }
1311     NR::Point const q1 = offset / h1;
1312     NR::Coord const h2 = NR::L2( pt - _origin );
1313     if (h2 < 1e-15) {
1314         return;
1315     }
1316     NR::Point const q2 = (pt - _origin) / h2;
1317     NR::rotate const r1(q1);
1318     NR::rotate const r2(q2);
1320     NR::Matrix rotate( r2 / r1 );
1321     transform(rotate, _origin);
1324 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1326     seltrans->setCenter(pt);
1330 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1332     SnapManager const &m = _desktop->namedview->snap_manager;
1334     /* The amount that we've moved by during this drag */
1335     NR::Point dxy = xy - _point;
1337     /* Get a STL list of the selected items.
1338     ** FIXME: this should probably be done by Inkscape::Selection.
1339     */
1340     std::list<SPItem const*> it;
1341     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1342         it.push_back(reinterpret_cast<SPItem*>(i->data));
1343     }
1345     bool const alt = (state & GDK_MOD1_MASK);
1346     bool const control = (state & GDK_CONTROL_MASK);
1347     bool const shift = (state & GDK_SHIFT_MASK);
1349     if (alt) {
1351         /* Alt pressed means keep offset: snap the moved distance to the grid.
1352         ** FIXME: this will snap to more than just the grid, nowadays.
1353         */
1355         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1357     } else if (!shift) {
1359         /* We're snapping to things, possibly with a constraint to horizontal or
1360         ** vertical movement.  Obtain a list of possible translations and then
1361         ** pick the smallest.
1362         */
1364         /* This will be our list of possible translations */
1365         std::list<std::pair<NR::Point, bool> > s;
1367         if (control) {
1369             /* Snap to things, and also constrain to horizontal or vertical movement */
1371             for (unsigned int dim = 0; dim < 2; dim++) {
1372                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1373                                                          _bbox_points,
1374                                                          it,
1375                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1376                                                          dxy));
1377                             
1378                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1379                                                          _snap_points,
1380                                                          it,
1381                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1382                                                          dxy));
1383             }
1385         } else {
1387             /* Snap to things with no constraint */
1389             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1390                                               _bbox_points, it, dxy));
1391             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1392                                               _snap_points, it, dxy));
1393         }
1395         /* Pick one */
1396         NR::Coord best = NR_HUGE;
1397         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1398             if (i->second) {
1399                 NR::Coord const m = NR::L2(i->first);
1400                 if (m < best) {
1401                     best = m;
1402                     dxy = i->first;
1403                 }
1404             }
1405         }
1406     }
1408     if (control) {
1409         /* Ensure that the horizontal and vertical constraint has been applied */
1410         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1411             dxy[NR::Y] = 0;
1412         } else {
1413             dxy[NR::X] = 0;
1414         }
1415     }
1417     NR::Matrix const move((NR::translate(dxy)));
1418     NR::Point const norm(0, 0);
1419     transform(move, norm);
1421     // status text
1422     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1423     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1424     _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);
1425     g_string_free(xs, TRUE);
1426     g_string_free(ys, TRUE);
1430 /*
1431   Local Variables:
1432   mode:c++
1433   c-file-style:"stroustrup"
1434   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1435   indent-tabs-mode:nil
1436   fill-column:99
1437   End:
1438 */
1439 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :