Code

remove svglsimpl
[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 "verbs.h"
43 #include <glibmm/i18n.h>
44 #include "display/sp-ctrlline.h"
45 #include "prefs-utils.h"
46 #include "xml/repr.h"
48 #include "isnan.h" //temp fix.  make sure included last
50 static void sp_remove_handles(SPKnot *knot[], gint num);
52 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data);
53 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data);
54 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data);
55 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint32 state, gpointer data);
56 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *p, guint state, gboolean *data);
58 extern GdkPixbuf *handles[];
60 static gboolean sp_seltrans_handle_event(SPKnot *knot, GdkEvent *event, gpointer)
61 {
62     switch (event->type) {
63         case GDK_MOTION_NOTIFY:
64             break;
65         case GDK_KEY_PRESS:
66             if (get_group0_keyval (&event->key) == GDK_space) {
67                 /* stamping mode: both mode(show content and outline) operation with knot */
68                 if (!SP_KNOT_IS_GRABBED(knot)) {
69                     return FALSE;
70                 }
71                 SPDesktop *desktop = knot->desktop;
72                 Inkscape::SelTrans *seltrans = SP_SELECT_CONTEXT(desktop->event_context)->_seltrans;
73                 seltrans->stamp();
74                 return TRUE;
75             }
76             break;
77         default:
78             break;
79     }
81     return FALSE;
82 }
84 Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
85     _desktop(desktop),
86     _selcue(desktop),
87     _state(STATE_SCALE),
88     _show(SHOW_CONTENT),
89     _grabbed(false),
90     _show_handles(true),
91     _box(NR::Point(0,0), NR::Point(0,0)),
92     _chandle(NULL),
93     _stamp_cache(NULL),
94     _message_context(desktop->messageStack())
95 {
96     g_return_if_fail(desktop != NULL);
98     for (int i = 0; i < 8; i++) {
99         _shandle[i] = NULL;
100         _rhandle[i] = NULL;
101     }
103     _updateVolatileState();
105     _center_is_set = false; // reread _center from items, or set to bbox midpoint
107     _updateHandles();
109     _selection = sp_desktop_selection(desktop);
111     _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
112                                SP_TYPE_CTRL,
113                                "anchor", GTK_ANCHOR_CENTER,
114                                "mode", SP_CTRL_MODE_COLOR,
115                                "shape", SP_CTRL_SHAPE_BITMAP,
116                                "size", 13.0,
117                                "filled", TRUE,
118                                "fill_color", 0x00000000,
119                                "stroked", TRUE,
120                                "stroke_color", 0x000000a0,
121                                "pixbuf", handles[12],
122                                NULL);
124     _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
125                                SP_TYPE_CTRL,
126                                "anchor", GTK_ANCHOR_CENTER,
127                                "mode", SP_CTRL_MODE_XOR,
128                                "shape", SP_CTRL_SHAPE_CROSS,
129                                "size", 7.0,
130                                "filled", TRUE,
131                                "fill_color", 0xffffff7f,
132                                "stroked", TRUE,
133                                "stroke_color", 0xffffffff,
134                                "pixbuf", handles[12],
135                                NULL);
137     sp_canvas_item_hide(_grip);
138     sp_canvas_item_hide(_norm);
140     for (int i = 0; i < 4; i++) {
141         _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
142         sp_canvas_item_hide(_l[i]);
143     }
145     _sel_changed_connection = _selection->connectChanged(
146         sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
147         );
149     _sel_modified_connection = _selection->connectModified(
150         sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
151         );
154 Inkscape::SelTrans::~SelTrans()
156     _sel_changed_connection.disconnect();
157     _sel_modified_connection.disconnect();
159     for (unsigned int i = 0; i < 8; i++) {
160         if (_shandle[i]) {
161             g_object_unref(G_OBJECT(_shandle[i]));
162             _shandle[i] = NULL;
163         }
164         if (_rhandle[i]) {
165             g_object_unref(G_OBJECT(_rhandle[i]));
166             _rhandle[i] = NULL;
167         }
168     }
169     if (_chandle) {
170         g_object_unref(G_OBJECT(_chandle));
171         _chandle = NULL;
172     }
174     if (_norm) {
175         gtk_object_destroy(GTK_OBJECT(_norm));
176         _norm = NULL;
177     }
178     if (_grip) {
179         gtk_object_destroy(GTK_OBJECT(_grip));
180         _grip = NULL;
181     }
182     for (int i = 0; i < 4; i++) {
183         if (_l[i]) {
184             gtk_object_destroy(GTK_OBJECT(_l[i]));
185             _l[i] = NULL;
186         }
187     }
189     for (unsigned i = 0; i < _items.size(); i++) {
190         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
191     }
193     _items.clear();
194     _items_centers.clear();
197 void Inkscape::SelTrans::resetState()
199     _state = STATE_SCALE;
202 void Inkscape::SelTrans::increaseState()
204     if (_state == STATE_SCALE) {
205         _state = STATE_ROTATE;
206     } else {
207         _state = STATE_SCALE;
208     }
210     _center_is_set = true; // no need to reread center
212     _updateHandles();
215 void Inkscape::SelTrans::setCenter(NR::Point const &p)
217     _center = p;
218     _center_is_set = true;
220     // Write the new center position into all selected items
221     for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
222         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
223         it->setCenter(p);
224         SP_OBJECT(it)->updateRepr();
225     }
226     sp_document_maybe_done (sp_desktop_document(_desktop), "center::move", SP_VERB_CONTEXT_SELECT, 
227                             _("Set center"));
229     _updateHandles();
232 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
234     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
236     g_return_if_fail(!_grabbed);
238     _grabbed = true;
239     _show_handles = show_handles;
240     _updateVolatileState();
242     _changed = false;
244     if (_empty) {
245         return;
246     }
248     for (GSList const *l = selection->itemList(); l; l = l->next) {
249         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
250         _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
251         _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
252     }
254     _current.set_identity();
256     _point = p;
258     _snap_points = selection->getSnapPointsConvexHull();
259     _bbox_points = selection->getBBoxPointsOuter();
261     gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
262     bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
263     NR::Rect op_box = _box;
264     if (origin_on_bbox == false && _snap_points.empty() == false) {
265         std::vector<NR::Point>::iterator i = _snap_points.begin();
266         op_box = NR::Rect(*i, *i);
267         i++;
268         while (i != _snap_points.end()) {
269             op_box.expandTo(*i);
270             i++;
271         }
272     }
274     _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
276     if ((x != -1) && (y != -1)) {
277         sp_canvas_item_show(_norm);
278         sp_canvas_item_show(_grip);
279     }
281     if (_show == SHOW_OUTLINE) {
282         for (int i = 0; i < 4; i++)
283             sp_canvas_item_show(_l[i]);
284     }
287     _updateHandles();
288     g_return_if_fail(_stamp_cache == NULL);
291 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
293     g_return_if_fail(_grabbed);
294     g_return_if_fail(!_empty);
296     NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
298     if (_show == SHOW_CONTENT) {
299         // update the content
300         for (unsigned i = 0; i < _items.size(); i++) {
301             SPItem &item = *_items[i].first;
302             NR::Matrix const &prev_transform = _items[i].second;
303             sp_item_set_i2d_affine(&item, prev_transform * affine);
304         }
305     } else {
306         NR::Point p[4];
307         /* update the outline */
308         for (unsigned i = 0 ; i < 4 ; i++) {
309             p[i] = _box.corner(i) * affine;
310         }
311         for (unsigned i = 0 ; i < 4 ; i++) {
312             sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
313         }
314     }
316     _current = affine;
317     _changed = true;
318     _updateHandles();
321 void Inkscape::SelTrans::ungrab()
323     g_return_if_fail(_grabbed);
325     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
326     bool updh = true;
327     if (!_empty && _changed) {
328         sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
329         _center *= _current;
330         _center_is_set = true;
332 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
333 // appropriately - it does not know the original positions of the centers (all objects already have
334 // the new bboxes). So we need to reset the centers from our saved array.
335         if (_show != SHOW_OUTLINE && !_current.is_translation()) {
336             for (unsigned i = 0; i < _items_centers.size(); i++) {
337                 SPItem *currentItem = _items_centers[i].first;
338                 if (currentItem->isCenterSet()) { // only if it's already set
339                     currentItem->setCenter (_items_centers[i].second * _current);
340                     SP_OBJECT(currentItem)->updateRepr();
341                 }
342             }
343         }
345         if (_current.is_translation()) {
346             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
347                              _("Move"));
348         } else if (_current.is_scale()) {
349             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
350                              _("Scale"));
351         } else if (_current.is_rotation()) {
352             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
353                              _("Rotate"));
354         } else {
355             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
356                              _("Skew"));
357         }
359         updh = false;
360     }
362     for (unsigned i = 0; i < _items.size(); i++) {
363         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
364     }
365     _items.clear();
366     _items_centers.clear();
368     _grabbed = false;
369     _show_handles = true;
371     sp_canvas_item_hide(_norm);
372     sp_canvas_item_hide(_grip);
374     if (_show == SHOW_OUTLINE) {
375         for (int i = 0; i < 4; i++)
376             sp_canvas_item_hide(_l[i]);
377     }
379     _updateVolatileState();
380     if (updh) {
381         _updateHandles();
382     }
383     if (_stamp_cache) {
384         g_slist_free(_stamp_cache);
385         _stamp_cache = NULL;
386     }
388     _message_context.clear();
391 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
392 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
394 void Inkscape::SelTrans::stamp()
396     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
398     bool fixup = !_grabbed;
399     if ( fixup && _stamp_cache ) {
400         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
401         g_slist_free(_stamp_cache);
402         _stamp_cache = NULL;
403     }
405     /* stamping mode */
406     if (!_empty) {
407         GSList *l;
408         if (_stamp_cache) {
409             l = _stamp_cache;
410         } else {
411             /* Build cache */
412             l  = g_slist_copy((GSList *) selection->itemList());
413             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
414             _stamp_cache = l;
415         }
417         while (l) {
418             SPItem *original_item = SP_ITEM(l->data);
419             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
421             // remember the position of the item
422             gint pos = original_repr->position();
423             // remember parent
424             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
426             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
428             // add the new repr to the parent
429             parent->appendChild(copy_repr);
430             // move to the saved position
431             copy_repr->setPosition(pos > 0 ? pos : 0);
433             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
435             NR::Matrix const *new_affine;
436             if (_show == SHOW_OUTLINE) {
437                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
438                 NR::Matrix const i2dnew( i2d * _current );
439                 sp_item_set_i2d_affine(copy_item, i2dnew);
440                 new_affine = &copy_item->transform;
441             } else {
442                 new_affine = &original_item->transform;
443             }
445             sp_item_write_transform(copy_item, copy_repr, *new_affine);
447             if (copy_item->isCenterSet()) {
448                 copy_item->setCenter(_center * _current);
449             }
451             Inkscape::GC::release(copy_repr);
452             l = l->next;
453         }
454         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
455                          _("Stamp"));
456     }
458     if ( fixup && _stamp_cache ) {
459         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
460         g_slist_free(_stamp_cache);
461         _stamp_cache = NULL;
462     }
465 void Inkscape::SelTrans::_updateHandles()
467     if ( !_show_handles || _empty )
468     {
469         sp_remove_handles(_shandle, 8);
470         sp_remove_handles(_rhandle, 8);
471         sp_remove_handles(&_chandle, 1);
472         return;
473     }
475     // center handle
476     if ( _chandle == NULL ) {
477         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
479         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
480         _chandle->setSize (13);
481         _chandle->setAnchor (handle_center.anchor);
482         _chandle->setMode (SP_CTRL_MODE_XOR);
483         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
484         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
485         _chandle->setPixbuf(handles[handle_center.control]);
486         sp_knot_update_ctrl(_chandle);
488         g_signal_connect(G_OBJECT(_chandle), "request",
489                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
490         g_signal_connect(G_OBJECT(_chandle), "moved",
491                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
492         g_signal_connect(G_OBJECT(_chandle), "grabbed",
493                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
494         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
495                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
496         g_signal_connect(G_OBJECT(_chandle), "clicked",
497                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
498     }
500     sp_remove_handles(&_chandle, 1);
501     if ( _state == STATE_SCALE ) {
502         sp_remove_handles(_rhandle, 8);
503         _showHandles(_shandle, handles_scale, 8,
504                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
505                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
506     } else {
507         sp_remove_handles(_shandle, 8);
508         _showHandles(_rhandle, handles_rotate, 8,
509                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
510                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
511     }
513     if (!_center_is_set) {
514         _center = _desktop->selection->center();
515         _center_is_set = true;
516     }
518     if ( _state == STATE_SCALE ) {
519         sp_knot_hide(_chandle);
520     } else {
521         sp_knot_show(_chandle);
522         sp_knot_moveto(_chandle, &_center);
523     }
526 void Inkscape::SelTrans::_updateVolatileState()
528     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
529     _empty = selection->isEmpty();
531     if (_empty) {
532         return;
533     }
535     _box = selection->bounds();
536     if (_box.isEmpty()) {
537         _empty = true;
538         return;
539     }
541     _strokewidth = stroke_average_width (selection->itemList());
543     _current.set_identity();
546 static void sp_remove_handles(SPKnot *knot[], gint num)
548     for (int i = 0; i < num; i++) {
549         if (knot[i] != NULL) {
550             sp_knot_hide(knot[i]);
551         }
552     }
555 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
556                              gchar const *even_tip, gchar const *odd_tip)
558     g_return_if_fail( !_empty );
560     for (int i = 0; i < num; i++) {
561         if (knot[i] == NULL) {
562             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
564             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
565             knot[i]->setSize (13);
566             knot[i]->setAnchor (handle[i].anchor);
567             knot[i]->setMode (SP_CTRL_MODE_XOR);
568             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
569             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
570             knot[i]->setPixbuf(handles[handle[i].control]);
571             sp_knot_update_ctrl(knot[i]);
573             g_signal_connect(G_OBJECT(knot[i]), "request",
574                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
575             g_signal_connect(G_OBJECT(knot[i]), "moved",
576                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
577             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
578                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
579             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
580                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
581             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
582         }
583         sp_knot_show(knot[i]);
585         NR::Point const handle_pt(handle[i].x, handle[i].y);
586         NR::Point p( _box.min()
587                      + ( _box.dimensions()
588                          * NR::scale(handle_pt) ) );
590         sp_knot_moveto(knot[i], &p);
591     }
594 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
596     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
597         knot, state, *(SPSelTransHandle const *) data
598         );
601 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
603     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
606 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
608     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
609         knot, position, state, *(SPSelTransHandle const *) data
610         );
613 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
615     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
616         knot, position, state, *(SPSelTransHandle const *) data
617         );
620 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
622     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
623         knot, state, *(SPSelTransHandle const *) data
624         );
627 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
629     switch (handle.anchor) {
630         case GTK_ANCHOR_CENTER:
631             if (state & GDK_SHIFT_MASK) {
632                 // Unset the  center position for all selected items
633                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
634                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
635                     it->unsetCenter();
636                     SP_OBJECT(it)->updateRepr();
637                     _center_is_set = false;  // center has changed
638                     _updateHandles();
639                 }
640                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT, 
641                                         _("Reset center"));
642             }
643             break;
644         default:
645             break;
646     }
649 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
651     switch (handle.anchor) {
652         case GTK_ANCHOR_CENTER:
653             g_object_set(G_OBJECT(_grip),
654                          "shape", SP_CTRL_SHAPE_BITMAP,
655                          "size", 13.0,
656                          NULL);
657             sp_canvas_item_show(_grip);
658             break;
659         default:
660             g_object_set(G_OBJECT(_grip),
661                          "shape", SP_CTRL_SHAPE_CROSS,
662                          "size", 7.0,
663                          NULL);
664             sp_canvas_item_show(_norm);
665             sp_canvas_item_show(_grip);
667             break;
668     }
670     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
674 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
676     if (!SP_KNOT_IS_GRABBED(knot)) {
677         return;
678     }
680     // in case items have been unhooked from the document, don't
681     // try to continue processing events for them.
682     for (unsigned int i = 0; i < _items.size(); i++) {
683         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
684             return;
685         }
686     }
688     handle.action(this, handle, *position, state);
692 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
694     if (!SP_KNOT_IS_GRABBED(knot)) {
695         return TRUE;
696     }
698     knot->desktop->set_coordinate_status(*position);
699     knot->desktop->setPosition(*position);
702     if (state & GDK_MOD1_MASK) {
703         *position = _point + ( *position - _point ) / 10;
704     }
706     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
707         _origin = _opposite;
708     } else {
709         _origin = _center;
710     }
711     if (handle.request(this, handle, *position, state)) {
712         sp_knot_set_position(knot, position, state);
713         SP_CTRL(_grip)->moveto(*position);
714         SP_CTRL(_norm)->moveto(_origin);
715     }
717     return TRUE;
721 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
723     if (!_grabbed) {
724         _updateVolatileState();
725         _center_is_set = false; // center(s) may have changed
726         _updateHandles();
727     }
730 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
732     if (!_grabbed) {
733         _updateVolatileState();
735         // reset internal flag
736         _changed = false;
738         _center_is_set = false;  // center(s) may have changed
740         _updateHandles();
741     }
744 /*
745  * handlers for handle move-request
746  */
748 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
749 static double sign(double const x)
751     return ( x < 0
752              ? -1
753              : 1 );
756 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
757                                     SPSelTransHandle const &, NR::Point &pt, guint state)
759     return seltrans->scaleRequest(pt, state);
762 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
763                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
765     return seltrans->stretchRequest(handle, pt, state);
768 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
769                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
771     return seltrans->skewRequest(handle, pt, state);
774 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
775                                      SPSelTransHandle const &, NR::Point &pt, guint state)
777     return seltrans->rotateRequest(pt, state);
780 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
781                                      SPSelTransHandle const &, NR::Point &pt, guint state)
783     return seltrans->centerRequest(pt, state);
786 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
788     using NR::X;
789     using NR::Y;
791     NR::Point d = _point - _origin;
792     NR::scale s(0, 0);
794     /* Work out the new scale factors `s' */
795     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
796         if ( fabs(d[i]) > 0.001 ) {
797             s[i] = ( pt[i] - _origin[i] ) / d[i];
798             if ( fabs(s[i]) < 1e-9 ) {
799                 s[i] = 1e-9;
800             }
801         }
802     }
804     SnapManager const &m = _desktop->namedview->snap_manager;
806     /* Get a STL list of the selected items.
807     ** FIXME: this should probably be done by Inkscape::Selection.
808     */
809     std::list<SPItem const*> it;
810     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
811         it.push_back(reinterpret_cast<SPItem*>(i->data));
812     }
814     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
815         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
816         ** To do this, we snap along a suitable constraint vector from the origin.
817         */
819         NR::Point const cv = NR::Point(
820             pt[NR::X] > _origin[NR::X] ? 1 : -1,
821             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
822             );
824         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
825                                                                _bbox_points,
826                                                                it,
827                                                                Snapper::ConstraintLine(_origin, cv),
828                                                                s,
829                                                                _origin);
831         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
832                                                                _snap_points,
833                                                                it,
834                                                                Snapper::ConstraintLine(_origin, cv),
835                                                                s,
836                                                                _origin);
838         if (bb.second == false && sn.second == false) {
840             /* We didn't snap, so just lock aspect ratio */
841             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
842                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
843             } else {
844                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
845             }
847         } else {
849             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
850             ** just compare difference in s[X].
851             */
852             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
853             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
854             s = (bd < sd) ? bb.first : sn.first;
855         }
857     } else {
858         /* Scale aspect ratio is unlocked */
859         
860         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
861                                                         _bbox_points,
862                                                         it,
863                                                         s,
864                                                         _origin);
865         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
866                                                         _snap_points,
867                                                         it,
868                                                         s,
869                                                         _origin);
871         /* Pick the snap that puts us closest to the original scale */
872         NR::Coord bd = bb.second ?
873             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
874                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
875             : NR_HUGE;
876         NR::Coord sd = sn.second ?
877             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
878                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
879             : NR_HUGE;
880         s = (bd < sd) ? bb.first : sn.first;
881     }
883     /* Update the knot position */
884     pt = ( _point - _origin ) * s + _origin;
886     /* Status text */
887     _message_context.setF(Inkscape::NORMAL_MESSAGE,
888                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
889                           100 * s[NR::X], 100 * s[NR::Y]);
891     return TRUE;
894 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
896     using NR::X;
897     using NR::Y;
899     NR::Dim2 axis, perp;
901     switch (handle.cursor) {
902         case GDK_TOP_SIDE:
903         case GDK_BOTTOM_SIDE:
904            axis = NR::Y;
905            perp = NR::X;
906            break;
907         case GDK_LEFT_SIDE:
908         case GDK_RIGHT_SIDE:
909            axis = NR::X;
910            perp = NR::Y;
911            break;
912         default:
913             g_assert_not_reached();
914             return TRUE;
915     };
917     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
918         return FALSE;
919     }
921     NR::scale s(1, 1);
922     s[axis] = ( ( pt[axis] - _origin[axis] )
923                 / ( _point[axis] - _origin[axis] ) );
924     if ( fabs(s[axis]) < 1e-15 ) {
925         s[axis] = 1e-15;
926     }
928     /* Get a STL list of the selected items.
929     ** FIXME: this should probably be done by Inkscape::Selection.
930     */
931     std::list<SPItem const*> it;
932     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
933         it.push_back(reinterpret_cast<SPItem*>(i->data));
934     }
936     SnapManager const &m = _desktop->namedview->snap_manager;
938     if ( state & GDK_CONTROL_MASK ) {
939         s[perp] = fabs(s[axis]);
941         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
942             Snapper::BBOX_POINT,
943             _bbox_points,
944             it,
945             s[axis],
946             _origin,
947             axis,
948             true);
950         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
951             Snapper::SNAP_POINT,
952             _snap_points,
953             it,
954             s[axis],
955             _origin,
956             axis,
957             true);
959         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
960         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
961         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
963         s[axis] = fabs(ratio) * sign(s[axis]);
964         s[perp] = fabs(s[axis]);
965     } else {
967         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
968             Snapper::BBOX_POINT,
969             _bbox_points,
970             it,
971             s[axis],
972             _origin,
973             axis,
974             false);
976         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
977             Snapper::SNAP_POINT,
978             _snap_points,
979             it,
980             s[axis],
981             _origin,
982             axis,
983             false);
985         /* Choose the smaller difference in scale */
986         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
987         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
988         s[axis] = (bd < sd) ? bb.first : sn.first;
989         s[perp] = 1;
990     }
992     pt = ( _point - _origin ) * NR::scale(s) + _origin;
993     if (isNaN(pt[X] + pt[Y])) {
994         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
995                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
996     }
998     // status text
999     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1000                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1001                           100 * s[NR::X], 100 * s[NR::Y]);
1003     return TRUE;
1006 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1008     using NR::X;
1009     using NR::Y;
1011     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1012         return FALSE;
1013     }
1015     NR::Dim2 dim_a;
1016     NR::Dim2 dim_b;
1017     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1018         dim_a = X;
1019         dim_b = Y;
1020     } else {
1021         dim_a = Y;
1022         dim_b = X;
1023     }
1025     double skew[2];
1026     double s[2] = { 1.0, 1.0 };
1028     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1029         return FALSE;
1030     }
1032     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1034     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1036     if ( fabs(s[dim_a]) < 1 ) {
1037         s[dim_a] = sign(s[dim_a]);
1038     } else {
1039         s[dim_a] = floor( s[dim_a] + 0.5 );
1040     }
1042     double radians = atan(skew[dim_a] / s[dim_a]);
1044     if (state & GDK_CONTROL_MASK) {
1046         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1048         if (snaps) {
1049             double sections = floor( radians * snaps / M_PI + .5 );
1050             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1051             radians = ( M_PI / snaps ) * sections;
1052         }
1053         skew[dim_a] = tan(radians) * s[dim_a];
1054     } else {
1055         SnapManager const &m = _desktop->namedview->snap_manager;
1057         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1058                                                        _bbox_points,
1059                                                        std::list<SPItem const *>(),
1060                                                        skew[dim_a],
1061                                                        _origin,
1062                                                        dim_a);
1064         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1065                                                        _snap_points,
1066                                                        std::list<SPItem const *>(),
1067                                                        skew[dim_a],
1068                                                        _origin,
1069                                                        dim_a);
1070         
1071         if (bb.second || sn.second) {
1072             /* We snapped something, so change the skew to reflect it */
1073             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1074             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1075             skew[dim_a] = std::min(bd, sd);
1076         }
1077     }
1079     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1080     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1082     /* status text */
1083     double degrees = 180 / M_PI * radians;
1084     if (degrees > 180) degrees -= 360;
1085     if (degrees < -180) degrees += 360;
1087     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1088                           // TRANSLATORS: don't modify the first ";"
1089                           // (it will NOT be displayed as ";" - only the second one will be)
1090                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1091                           degrees);
1093     return TRUE;
1096 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1098     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1100     // rotate affine in rotate
1101     NR::Point const d1 = _point - _origin;
1102     NR::Point const d2 = pt     - _origin;
1104     NR::Coord const h1 = NR::L2(d1);
1105     if (h1 < 1e-15) return FALSE;
1106     NR::Point q1 = d1 / h1;
1107     NR::Coord const h2 = NR::L2(d2);
1108     if (fabs(h2) < 1e-15) return FALSE;
1109     NR::Point q2 = d2 / h2;
1111     double radians;
1112     if (state & GDK_CONTROL_MASK) {
1113         /* Have to restrict movement. */
1114         double cos_t = NR::dot(q1, q2);
1115         double sin_t = NR::dot(NR::rot90(q1), q2);
1116         radians = atan2(sin_t, cos_t);
1117         if (snaps) {
1118             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1119         }
1120         q1 = NR::Point(1, 0);
1121         q2 = NR::Point(cos(radians), sin(radians));
1122     } else {
1123         radians = atan2(NR::dot(NR::rot90(d1), d2),
1124                         NR::dot(d1, d2));
1125     }
1127     NR::rotate const r1(q1);
1128     NR::rotate const r2(q2);
1129     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1131     /* status text */
1132     double degrees = 180 / M_PI * radians;
1133     if (degrees > 180) degrees -= 360;
1134     if (degrees < -180) degrees += 360;
1136     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1137                           // TRANSLATORS: don't modify the first ";"
1138                           // (it will NOT be displayed as ";" - only the second one will be)
1139                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1141     return TRUE;
1144 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1146     using NR::X;
1147     using NR::Y;
1149     SnapManager const &m = _desktop->namedview->snap_manager;
1150     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1152     if (state & GDK_CONTROL_MASK) {
1153         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1154             pt[Y] = _point[Y];
1155         } else {
1156             pt[X] = _point[X];
1157         }
1158     }
1160     if (!(state & GDK_SHIFT_MASK)) {
1161         // screen pixels to snap center to bbox
1162 #define SNAP_DIST 5
1163         // FIXME: take from prefs
1164         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1166         for (int i = 0; i < 2; i++) {
1168             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1169                 pt[i] = _box.min()[i];
1170             }
1171             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1172                 pt[i] = _box.midpoint()[i];
1173             }
1174             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1175                 pt[i] = _box.max()[i];
1176             }
1177         }
1178     }
1180     // status text
1181     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1182     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1183     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1184     g_string_free(xs, FALSE);
1185     g_string_free(ys, FALSE);
1187     return TRUE;
1190 /*
1191  * handlers for handle movement
1192  *
1193  */
1195 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1197     seltrans->stretch(handle, pt, state);
1200 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1202     seltrans->scale(pt, state);
1205 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1207     seltrans->skew(handle, pt, state);
1210 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1212     seltrans->rotate(pt, state);
1215 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1217     using NR::X;
1218     using NR::Y;
1220     NR::Dim2 dim;
1221     switch (handle.cursor) {
1222         case GDK_LEFT_SIDE:
1223         case GDK_RIGHT_SIDE:
1224             dim = X;
1225             break;
1226         case GDK_TOP_SIDE:
1227         case GDK_BOTTOM_SIDE:
1228             dim = Y;
1229             break;
1230         default:
1231             g_assert_not_reached();
1232             abort();
1233             break;
1234     }
1236     NR::Point const scale_origin(_origin);
1237     double const offset = _point[dim] - scale_origin[dim];
1238     if (!( fabs(offset) >= 1e-15 )) {
1239         return;
1240     }
1241     NR::scale s(1, 1);
1242     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1243     if (isNaN(s[dim])) {
1244         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1245                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1246     }
1247     if (!( fabs(s[dim]) >= 1e-15 )) {
1248         s[dim] = 1e-15;
1249     }
1250     if (state & GDK_CONTROL_MASK) {
1251         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1252         s[!dim] = fabs(s[dim]);
1253     }
1255     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1256     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1258     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1259     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1260                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1262     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1265 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1267     NR::Point const offset = _point - _origin;
1269     NR::scale s (1, 1);
1270     for (int i = NR::X; i <= NR::Y; i++) {
1271         if (fabs(offset[i]) > 1e-9)
1272             s[i] = (pt[i] - _origin[i]) / offset[i];
1273         if (fabs(s[i]) < 1e-9)
1274             s[i] = 1e-9;
1275     }
1276     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1277     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1279     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1280     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1281                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1283     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1286 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1288     NR::Point const offset = _point - _origin;
1290     unsigned dim;
1291     switch (handle.cursor) {
1292         case GDK_SB_H_DOUBLE_ARROW:
1293             dim = NR::Y;
1294             break;
1295         case GDK_SB_V_DOUBLE_ARROW:
1296             dim = NR::X;
1297             break;
1298         default:
1299             g_assert_not_reached();
1300             abort();
1301             break;
1302     }
1303     if (fabs(offset[dim]) < 1e-15) {
1304         return;
1305     }
1306     NR::Matrix skew = NR::identity();
1307     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1308     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1309     skew[2*(1-dim) + (dim)] = 0;
1310     skew[2*(1-dim) + (1-dim)] = 1;
1312     for (int i = 0; i < 2; i++) {
1313         if (fabs(skew[3*i]) < 1e-15) {
1314             skew[3*i] = 1e-15;
1315         }
1316     }
1317     transform(skew, _origin);
1320 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1322     NR::Point const offset = _point - _origin;
1324     NR::Coord const h1 = NR::L2(offset);
1325     if (h1 < 1e-15) {
1326         return;
1327     }
1328     NR::Point const q1 = offset / h1;
1329     NR::Coord const h2 = NR::L2( pt - _origin );
1330     if (h2 < 1e-15) {
1331         return;
1332     }
1333     NR::Point const q2 = (pt - _origin) / h2;
1334     NR::rotate const r1(q1);
1335     NR::rotate const r2(q2);
1337     NR::Matrix rotate( r2 / r1 );
1338     transform(rotate, _origin);
1341 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1343     seltrans->setCenter(pt);
1347 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1349     SnapManager const &m = _desktop->namedview->snap_manager;
1351     /* The amount that we've moved by during this drag */
1352     NR::Point dxy = xy - _point;
1354     /* Get a STL list of the selected items.
1355     ** FIXME: this should probably be done by Inkscape::Selection.
1356     */
1357     std::list<SPItem const*> it;
1358     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1359         it.push_back(reinterpret_cast<SPItem*>(i->data));
1360     }
1362     bool const alt = (state & GDK_MOD1_MASK);
1363     bool const control = (state & GDK_CONTROL_MASK);
1364     bool const shift = (state & GDK_SHIFT_MASK);
1366     if (alt) {
1368         /* Alt pressed means keep offset: snap the moved distance to the grid.
1369         ** FIXME: this will snap to more than just the grid, nowadays.
1370         */
1372         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1374     } else if (!shift) {
1376         /* We're snapping to things, possibly with a constraint to horizontal or
1377         ** vertical movement.  Obtain a list of possible translations and then
1378         ** pick the smallest.
1379         */
1381         /* This will be our list of possible translations */
1382         std::list<std::pair<NR::Point, bool> > s;
1384         if (control) {
1386             /* Snap to things, and also constrain to horizontal or vertical movement */
1388             for (unsigned int dim = 0; dim < 2; dim++) {
1389                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1390                                                          _bbox_points,
1391                                                          it,
1392                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1393                                                          dxy));
1394                             
1395                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1396                                                          _snap_points,
1397                                                          it,
1398                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1399                                                          dxy));
1400             }
1402         } else {
1404             /* Snap to things with no constraint */
1406             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1407                                               _bbox_points, it, dxy));
1408             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1409                                               _snap_points, it, dxy));
1410         }
1412         /* Pick one */
1413         NR::Coord best = NR_HUGE;
1414         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1415             if (i->second) {
1416                 NR::Coord const m = NR::L2(i->first);
1417                 if (m < best) {
1418                     best = m;
1419                     dxy = i->first;
1420                 }
1421             }
1422         }
1423     }
1425     if (control) {
1426         /* Ensure that the horizontal and vertical constraint has been applied */
1427         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1428             dxy[NR::Y] = 0;
1429         } else {
1430             dxy[NR::X] = 0;
1431         }
1432     }
1434     NR::Matrix const move((NR::translate(dxy)));
1435     NR::Point const norm(0, 0);
1436     transform(move, norm);
1438     // status text
1439     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1440     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1441     _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);
1442     g_string_free(xs, TRUE);
1443     g_string_free(ys, TRUE);
1447 /*
1448   Local Variables:
1449   mode:c++
1450   c-file-style:"stroustrup"
1451   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1452   indent-tabs-mode:nil
1453   fill-column:99
1454   End:
1455 */
1456 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :