Code

patch from Gustav Broberg: undo annotations and history dialog
[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                             /* TODO: annotate */ "seltrans.cpp:227");
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         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
346                          /* TODO: annotate */ "seltrans.cpp:346");
347         updh = false;
348     }
350     for (unsigned i = 0; i < _items.size(); i++) {
351         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
352     }
353     _items.clear();
354     _items_centers.clear();
356     _grabbed = false;
357     _show_handles = true;
359     sp_canvas_item_hide(_norm);
360     sp_canvas_item_hide(_grip);
362     if (_show == SHOW_OUTLINE) {
363         for (int i = 0; i < 4; i++)
364             sp_canvas_item_hide(_l[i]);
365     }
367     _updateVolatileState();
368     if (updh) {
369         _updateHandles();
370     }
371     if (_stamp_cache) {
372         g_slist_free(_stamp_cache);
373         _stamp_cache = NULL;
374     }
376     _message_context.clear();
379 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
380 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
382 void Inkscape::SelTrans::stamp()
384     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
386     bool fixup = !_grabbed;
387     if ( fixup && _stamp_cache ) {
388         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
389         g_slist_free(_stamp_cache);
390         _stamp_cache = NULL;
391     }
393     /* stamping mode */
394     if (!_empty) {
395         GSList *l;
396         if (_stamp_cache) {
397             l = _stamp_cache;
398         } else {
399             /* Build cache */
400             l  = g_slist_copy((GSList *) selection->itemList());
401             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
402             _stamp_cache = l;
403         }
405         while (l) {
406             SPItem *original_item = SP_ITEM(l->data);
407             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
409             // remember the position of the item
410             gint pos = original_repr->position();
411             // remember parent
412             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
414             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
416             // add the new repr to the parent
417             parent->appendChild(copy_repr);
418             // move to the saved position
419             copy_repr->setPosition(pos > 0 ? pos : 0);
421             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
423             NR::Matrix const *new_affine;
424             if (_show == SHOW_OUTLINE) {
425                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
426                 NR::Matrix const i2dnew( i2d * _current );
427                 sp_item_set_i2d_affine(copy_item, i2dnew);
428                 new_affine = &copy_item->transform;
429             } else {
430                 new_affine = &original_item->transform;
431             }
433             sp_item_write_transform(copy_item, copy_repr, *new_affine);
435             if (copy_item->isCenterSet()) {
436                 copy_item->setCenter(_center * _current);
437             }
439             Inkscape::GC::release(copy_repr);
440             l = l->next;
441         }
442         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
443                          /* TODO: annotate */ "seltrans.cpp:443");
444     }
446     if ( fixup && _stamp_cache ) {
447         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
448         g_slist_free(_stamp_cache);
449         _stamp_cache = NULL;
450     }
453 void Inkscape::SelTrans::_updateHandles()
455     if ( !_show_handles || _empty )
456     {
457         sp_remove_handles(_shandle, 8);
458         sp_remove_handles(_rhandle, 8);
459         sp_remove_handles(&_chandle, 1);
460         return;
461     }
463     // center handle
464     if ( _chandle == NULL ) {
465         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
467         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
468         _chandle->setSize (13);
469         _chandle->setAnchor (handle_center.anchor);
470         _chandle->setMode (SP_CTRL_MODE_XOR);
471         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
472         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
473         _chandle->setPixbuf(handles[handle_center.control]);
474         sp_knot_update_ctrl(_chandle);
476         g_signal_connect(G_OBJECT(_chandle), "request",
477                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
478         g_signal_connect(G_OBJECT(_chandle), "moved",
479                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
480         g_signal_connect(G_OBJECT(_chandle), "grabbed",
481                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
482         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
483                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
484         g_signal_connect(G_OBJECT(_chandle), "clicked",
485                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
486     }
488     sp_remove_handles(&_chandle, 1);
489     if ( _state == STATE_SCALE ) {
490         sp_remove_handles(_rhandle, 8);
491         _showHandles(_shandle, handles_scale, 8,
492                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
493                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
494     } else {
495         sp_remove_handles(_shandle, 8);
496         _showHandles(_rhandle, handles_rotate, 8,
497                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
498                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
499     }
501     if (!_center_is_set) {
502         _center = _desktop->selection->center();
503         _center_is_set = true;
504     }
506     if ( _state == STATE_SCALE ) {
507         sp_knot_hide(_chandle);
508     } else {
509         sp_knot_show(_chandle);
510         sp_knot_moveto(_chandle, &_center);
511     }
514 void Inkscape::SelTrans::_updateVolatileState()
516     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
517     _empty = selection->isEmpty();
519     if (_empty) {
520         return;
521     }
523     _box = selection->bounds();
524     if (_box.isEmpty()) {
525         _empty = true;
526         return;
527     }
529     _strokewidth = stroke_average_width (selection->itemList());
531     _current.set_identity();
534 static void sp_remove_handles(SPKnot *knot[], gint num)
536     for (int i = 0; i < num; i++) {
537         if (knot[i] != NULL) {
538             sp_knot_hide(knot[i]);
539         }
540     }
543 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
544                              gchar const *even_tip, gchar const *odd_tip)
546     g_return_if_fail( !_empty );
548     for (int i = 0; i < num; i++) {
549         if (knot[i] == NULL) {
550             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
552             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
553             knot[i]->setSize (13);
554             knot[i]->setAnchor (handle[i].anchor);
555             knot[i]->setMode (SP_CTRL_MODE_XOR);
556             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
557             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
558             knot[i]->setPixbuf(handles[handle[i].control]);
559             sp_knot_update_ctrl(knot[i]);
561             g_signal_connect(G_OBJECT(knot[i]), "request",
562                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
563             g_signal_connect(G_OBJECT(knot[i]), "moved",
564                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
565             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
566                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
567             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
568                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
569             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
570         }
571         sp_knot_show(knot[i]);
573         NR::Point const handle_pt(handle[i].x, handle[i].y);
574         NR::Point p( _box.min()
575                      + ( _box.dimensions()
576                          * NR::scale(handle_pt) ) );
578         sp_knot_moveto(knot[i], &p);
579     }
582 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
584     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
585         knot, state, *(SPSelTransHandle const *) data
586         );
589 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
591     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
594 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
596     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
597         knot, position, state, *(SPSelTransHandle const *) data
598         );
601 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
603     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
604         knot, position, state, *(SPSelTransHandle const *) data
605         );
608 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
610     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
611         knot, state, *(SPSelTransHandle const *) data
612         );
615 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
617     switch (handle.anchor) {
618         case GTK_ANCHOR_CENTER:
619             if (state & GDK_SHIFT_MASK) {
620                 // Unset the  center position for all selected items
621                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
622                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
623                     it->unsetCenter();
624                     SP_OBJECT(it)->updateRepr();
625                     _center_is_set = false;  // center has changed
626                     _updateHandles();
627                 }
628                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT, 
629                                         /* TODO: annotate */ "seltrans.cpp:629");
630             }
631             break;
632         default:
633             break;
634     }
637 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
639     switch (handle.anchor) {
640         case GTK_ANCHOR_CENTER:
641             g_object_set(G_OBJECT(_grip),
642                          "shape", SP_CTRL_SHAPE_BITMAP,
643                          "size", 13.0,
644                          NULL);
645             sp_canvas_item_show(_grip);
646             break;
647         default:
648             g_object_set(G_OBJECT(_grip),
649                          "shape", SP_CTRL_SHAPE_CROSS,
650                          "size", 7.0,
651                          NULL);
652             sp_canvas_item_show(_norm);
653             sp_canvas_item_show(_grip);
655             break;
656     }
658     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
662 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
664     if (!SP_KNOT_IS_GRABBED(knot)) {
665         return;
666     }
668     // in case items have been unhooked from the document, don't
669     // try to continue processing events for them.
670     for (unsigned int i = 0; i < _items.size(); i++) {
671         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
672             return;
673         }
674     }
676     handle.action(this, handle, *position, state);
680 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
682     if (!SP_KNOT_IS_GRABBED(knot)) {
683         return TRUE;
684     }
686     knot->desktop->set_coordinate_status(*position);
687     knot->desktop->setPosition(*position);
690     if (state & GDK_MOD1_MASK) {
691         *position = _point + ( *position - _point ) / 10;
692     }
694     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
695         _origin = _opposite;
696     } else {
697         _origin = _center;
698     }
699     if (handle.request(this, handle, *position, state)) {
700         sp_knot_set_position(knot, position, state);
701         SP_CTRL(_grip)->moveto(*position);
702         SP_CTRL(_norm)->moveto(_origin);
703     }
705     return TRUE;
709 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
711     if (!_grabbed) {
712         _updateVolatileState();
713         _center_is_set = false; // center(s) may have changed
714         _updateHandles();
715     }
718 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
720     if (!_grabbed) {
721         _updateVolatileState();
723         // reset internal flag
724         _changed = false;
726         _center_is_set = false;  // center(s) may have changed
728         _updateHandles();
729     }
732 /*
733  * handlers for handle move-request
734  */
736 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
737 static double sign(double const x)
739     return ( x < 0
740              ? -1
741              : 1 );
744 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
745                                     SPSelTransHandle const &, NR::Point &pt, guint state)
747     return seltrans->scaleRequest(pt, state);
750 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
751                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
753     return seltrans->stretchRequest(handle, pt, state);
756 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
757                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
759     return seltrans->skewRequest(handle, pt, state);
762 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
763                                      SPSelTransHandle const &, NR::Point &pt, guint state)
765     return seltrans->rotateRequest(pt, state);
768 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
769                                      SPSelTransHandle const &, NR::Point &pt, guint state)
771     return seltrans->centerRequest(pt, state);
774 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
776     using NR::X;
777     using NR::Y;
779     NR::Point d = _point - _origin;
780     NR::scale s(0, 0);
782     /* Work out the new scale factors `s' */
783     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
784         if ( fabs(d[i]) > 0.001 ) {
785             s[i] = ( pt[i] - _origin[i] ) / d[i];
786             if ( fabs(s[i]) < 1e-9 ) {
787                 s[i] = 1e-9;
788             }
789         }
790     }
792     SnapManager const &m = _desktop->namedview->snap_manager;
794     /* Get a STL list of the selected items.
795     ** FIXME: this should probably be done by Inkscape::Selection.
796     */
797     std::list<SPItem const*> it;
798     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
799         it.push_back(reinterpret_cast<SPItem*>(i->data));
800     }
802     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
803         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
804         ** To do this, we snap along a suitable constraint vector from the origin.
805         */
807         NR::Point const cv = NR::Point(
808             pt[NR::X] > _origin[NR::X] ? 1 : -1,
809             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
810             );
812         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
813                                                                _bbox_points,
814                                                                it,
815                                                                Snapper::ConstraintLine(_origin, cv),
816                                                                s,
817                                                                _origin);
819         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
820                                                                _snap_points,
821                                                                it,
822                                                                Snapper::ConstraintLine(_origin, cv),
823                                                                s,
824                                                                _origin);
826         if (bb.second == false && sn.second == false) {
828             /* We didn't snap, so just lock aspect ratio */
829             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
830                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
831             } else {
832                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
833             }
835         } else {
837             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
838             ** just compare difference in s[X].
839             */
840             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
841             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
842             s = (bd < sd) ? bb.first : sn.first;
843         }
845     } else {
846         /* Scale aspect ratio is unlocked */
847         
848         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
849                                                         _bbox_points,
850                                                         it,
851                                                         s,
852                                                         _origin);
853         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
854                                                         _snap_points,
855                                                         it,
856                                                         s,
857                                                         _origin);
859         /* Pick the snap that puts us closest to the original scale */
860         NR::Coord bd = bb.second ?
861             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
862                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
863             : NR_HUGE;
864         NR::Coord sd = sn.second ?
865             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
866                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
867             : NR_HUGE;
868         s = (bd < sd) ? bb.first : sn.first;
869     }
871     /* Update the knot position */
872     pt = ( _point - _origin ) * s + _origin;
874     /* Status text */
875     _message_context.setF(Inkscape::NORMAL_MESSAGE,
876                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
877                           100 * s[NR::X], 100 * s[NR::Y]);
879     return TRUE;
882 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
884     using NR::X;
885     using NR::Y;
887     NR::Dim2 axis, perp;
889     switch (handle.cursor) {
890         case GDK_TOP_SIDE:
891         case GDK_BOTTOM_SIDE:
892            axis = NR::Y;
893            perp = NR::X;
894            break;
895         case GDK_LEFT_SIDE:
896         case GDK_RIGHT_SIDE:
897            axis = NR::X;
898            perp = NR::Y;
899            break;
900         default:
901             g_assert_not_reached();
902             return TRUE;
903     };
905     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
906         return FALSE;
907     }
909     NR::scale s(1, 1);
910     s[axis] = ( ( pt[axis] - _origin[axis] )
911                 / ( _point[axis] - _origin[axis] ) );
912     if ( fabs(s[axis]) < 1e-15 ) {
913         s[axis] = 1e-15;
914     }
916     /* Get a STL list of the selected items.
917     ** FIXME: this should probably be done by Inkscape::Selection.
918     */
919     std::list<SPItem const*> it;
920     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
921         it.push_back(reinterpret_cast<SPItem*>(i->data));
922     }
924     SnapManager const &m = _desktop->namedview->snap_manager;
926     if ( state & GDK_CONTROL_MASK ) {
927         s[perp] = fabs(s[axis]);
929         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
930             Snapper::BBOX_POINT,
931             _bbox_points,
932             it,
933             s[axis],
934             _origin,
935             axis,
936             true);
938         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
939             Snapper::SNAP_POINT,
940             _snap_points,
941             it,
942             s[axis],
943             _origin,
944             axis,
945             true);
947         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
948         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
949         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
951         s[axis] = fabs(ratio) * sign(s[axis]);
952         s[perp] = fabs(s[axis]);
953     } else {
955         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
956             Snapper::BBOX_POINT,
957             _bbox_points,
958             it,
959             s[axis],
960             _origin,
961             axis,
962             false);
964         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
965             Snapper::SNAP_POINT,
966             _snap_points,
967             it,
968             s[axis],
969             _origin,
970             axis,
971             false);
973         /* Choose the smaller difference in scale */
974         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
975         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
976         s[axis] = (bd < sd) ? bb.first : sn.first;
977         s[perp] = 1;
978     }
980     pt = ( _point - _origin ) * NR::scale(s) + _origin;
981     if (isNaN(pt[X] + pt[Y])) {
982         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
983                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
984     }
986     // status text
987     _message_context.setF(Inkscape::NORMAL_MESSAGE,
988                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
989                           100 * s[NR::X], 100 * s[NR::Y]);
991     return TRUE;
994 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
996     using NR::X;
997     using NR::Y;
999     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1000         return FALSE;
1001     }
1003     NR::Dim2 dim_a;
1004     NR::Dim2 dim_b;
1005     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1006         dim_a = X;
1007         dim_b = Y;
1008     } else {
1009         dim_a = Y;
1010         dim_b = X;
1011     }
1013     double skew[2];
1014     double s[2] = { 1.0, 1.0 };
1016     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1017         return FALSE;
1018     }
1020     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1022     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1024     if ( fabs(s[dim_a]) < 1 ) {
1025         s[dim_a] = sign(s[dim_a]);
1026     } else {
1027         s[dim_a] = floor( s[dim_a] + 0.5 );
1028     }
1030     double radians = atan(skew[dim_a] / s[dim_a]);
1032     if (state & GDK_CONTROL_MASK) {
1034         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1036         if (snaps) {
1037             double sections = floor( radians * snaps / M_PI + .5 );
1038             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1039             radians = ( M_PI / snaps ) * sections;
1040         }
1041         skew[dim_a] = tan(radians) * s[dim_a];
1042     } else {
1043         SnapManager const &m = _desktop->namedview->snap_manager;
1045         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1046                                                        _bbox_points,
1047                                                        std::list<SPItem const *>(),
1048                                                        skew[dim_a],
1049                                                        _origin,
1050                                                        dim_a);
1052         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1053                                                        _snap_points,
1054                                                        std::list<SPItem const *>(),
1055                                                        skew[dim_a],
1056                                                        _origin,
1057                                                        dim_a);
1058         
1059         if (bb.second || sn.second) {
1060             /* We snapped something, so change the skew to reflect it */
1061             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1062             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1063             skew[dim_a] = std::min(bd, sd);
1064         }
1065     }
1067     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1068     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1070     /* status text */
1071     double degrees = 180 / M_PI * radians;
1072     if (degrees > 180) degrees -= 360;
1073     if (degrees < -180) degrees += 360;
1075     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1076                           // TRANSLATORS: don't modify the first ";"
1077                           // (it will NOT be displayed as ";" - only the second one will be)
1078                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1079                           degrees);
1081     return TRUE;
1084 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1086     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1088     // rotate affine in rotate
1089     NR::Point const d1 = _point - _origin;
1090     NR::Point const d2 = pt     - _origin;
1092     NR::Coord const h1 = NR::L2(d1);
1093     if (h1 < 1e-15) return FALSE;
1094     NR::Point q1 = d1 / h1;
1095     NR::Coord const h2 = NR::L2(d2);
1096     if (fabs(h2) < 1e-15) return FALSE;
1097     NR::Point q2 = d2 / h2;
1099     double radians;
1100     if (state & GDK_CONTROL_MASK) {
1101         /* Have to restrict movement. */
1102         double cos_t = NR::dot(q1, q2);
1103         double sin_t = NR::dot(NR::rot90(q1), q2);
1104         radians = atan2(sin_t, cos_t);
1105         if (snaps) {
1106             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1107         }
1108         q1 = NR::Point(1, 0);
1109         q2 = NR::Point(cos(radians), sin(radians));
1110     } else {
1111         radians = atan2(NR::dot(NR::rot90(d1), d2),
1112                         NR::dot(d1, d2));
1113     }
1115     NR::rotate const r1(q1);
1116     NR::rotate const r2(q2);
1117     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1119     /* status text */
1120     double degrees = 180 / M_PI * radians;
1121     if (degrees > 180) degrees -= 360;
1122     if (degrees < -180) degrees += 360;
1124     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1125                           // TRANSLATORS: don't modify the first ";"
1126                           // (it will NOT be displayed as ";" - only the second one will be)
1127                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1129     return TRUE;
1132 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1134     using NR::X;
1135     using NR::Y;
1137     SnapManager const &m = _desktop->namedview->snap_manager;
1138     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1140     if (state & GDK_CONTROL_MASK) {
1141         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1142             pt[Y] = _point[Y];
1143         } else {
1144             pt[X] = _point[X];
1145         }
1146     }
1148     if (!(state & GDK_SHIFT_MASK)) {
1149         // screen pixels to snap center to bbox
1150 #define SNAP_DIST 5
1151         // FIXME: take from prefs
1152         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1154         for (int i = 0; i < 2; i++) {
1156             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1157                 pt[i] = _box.min()[i];
1158             }
1159             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1160                 pt[i] = _box.midpoint()[i];
1161             }
1162             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1163                 pt[i] = _box.max()[i];
1164             }
1165         }
1166     }
1168     // status text
1169     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1170     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1171     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1172     g_string_free(xs, FALSE);
1173     g_string_free(ys, FALSE);
1175     return TRUE;
1178 /*
1179  * handlers for handle movement
1180  *
1181  */
1183 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1185     seltrans->stretch(handle, pt, state);
1188 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1190     seltrans->scale(pt, state);
1193 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1195     seltrans->skew(handle, pt, state);
1198 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1200     seltrans->rotate(pt, state);
1203 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1205     using NR::X;
1206     using NR::Y;
1208     NR::Dim2 dim;
1209     switch (handle.cursor) {
1210         case GDK_LEFT_SIDE:
1211         case GDK_RIGHT_SIDE:
1212             dim = X;
1213             break;
1214         case GDK_TOP_SIDE:
1215         case GDK_BOTTOM_SIDE:
1216             dim = Y;
1217             break;
1218         default:
1219             g_assert_not_reached();
1220             abort();
1221             break;
1222     }
1224     NR::Point const scale_origin(_origin);
1225     double const offset = _point[dim] - scale_origin[dim];
1226     if (!( fabs(offset) >= 1e-15 )) {
1227         return;
1228     }
1229     NR::scale s(1, 1);
1230     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1231     if (isNaN(s[dim])) {
1232         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1233                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1234     }
1235     if (!( fabs(s[dim]) >= 1e-15 )) {
1236         s[dim] = 1e-15;
1237     }
1238     if (state & GDK_CONTROL_MASK) {
1239         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1240         s[!dim] = fabs(s[dim]);
1241     }
1243     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1244     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1246     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1247     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1248                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1250     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1253 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1255     NR::Point const offset = _point - _origin;
1257     NR::scale s (1, 1);
1258     for (int i = NR::X; i <= NR::Y; i++) {
1259         if (fabs(offset[i]) > 1e-9)
1260             s[i] = (pt[i] - _origin[i]) / offset[i];
1261         if (fabs(s[i]) < 1e-9)
1262             s[i] = 1e-9;
1263     }
1264     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1265     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1267     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1268     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1269                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1271     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1274 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1276     NR::Point const offset = _point - _origin;
1278     unsigned dim;
1279     switch (handle.cursor) {
1280         case GDK_SB_H_DOUBLE_ARROW:
1281             dim = NR::Y;
1282             break;
1283         case GDK_SB_V_DOUBLE_ARROW:
1284             dim = NR::X;
1285             break;
1286         default:
1287             g_assert_not_reached();
1288             abort();
1289             break;
1290     }
1291     if (fabs(offset[dim]) < 1e-15) {
1292         return;
1293     }
1294     NR::Matrix skew = NR::identity();
1295     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1296     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1297     skew[2*(1-dim) + (dim)] = 0;
1298     skew[2*(1-dim) + (1-dim)] = 1;
1300     for (int i = 0; i < 2; i++) {
1301         if (fabs(skew[3*i]) < 1e-15) {
1302             skew[3*i] = 1e-15;
1303         }
1304     }
1305     transform(skew, _origin);
1308 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1310     NR::Point const offset = _point - _origin;
1312     NR::Coord const h1 = NR::L2(offset);
1313     if (h1 < 1e-15) {
1314         return;
1315     }
1316     NR::Point const q1 = offset / h1;
1317     NR::Coord const h2 = NR::L2( pt - _origin );
1318     if (h2 < 1e-15) {
1319         return;
1320     }
1321     NR::Point const q2 = (pt - _origin) / h2;
1322     NR::rotate const r1(q1);
1323     NR::rotate const r2(q2);
1325     NR::Matrix rotate( r2 / r1 );
1326     transform(rotate, _origin);
1329 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1331     seltrans->setCenter(pt);
1335 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1337     SnapManager const &m = _desktop->namedview->snap_manager;
1339     /* The amount that we've moved by during this drag */
1340     NR::Point dxy = xy - _point;
1342     /* Get a STL list of the selected items.
1343     ** FIXME: this should probably be done by Inkscape::Selection.
1344     */
1345     std::list<SPItem const*> it;
1346     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1347         it.push_back(reinterpret_cast<SPItem*>(i->data));
1348     }
1350     bool const alt = (state & GDK_MOD1_MASK);
1351     bool const control = (state & GDK_CONTROL_MASK);
1352     bool const shift = (state & GDK_SHIFT_MASK);
1354     if (alt) {
1356         /* Alt pressed means keep offset: snap the moved distance to the grid.
1357         ** FIXME: this will snap to more than just the grid, nowadays.
1358         */
1360         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1362     } else if (!shift) {
1364         /* We're snapping to things, possibly with a constraint to horizontal or
1365         ** vertical movement.  Obtain a list of possible translations and then
1366         ** pick the smallest.
1367         */
1369         /* This will be our list of possible translations */
1370         std::list<std::pair<NR::Point, bool> > s;
1372         if (control) {
1374             /* Snap to things, and also constrain to horizontal or vertical movement */
1376             for (unsigned int dim = 0; dim < 2; dim++) {
1377                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1378                                                          _bbox_points,
1379                                                          it,
1380                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1381                                                          dxy));
1382                             
1383                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1384                                                          _snap_points,
1385                                                          it,
1386                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1387                                                          dxy));
1388             }
1390         } else {
1392             /* Snap to things with no constraint */
1394             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1395                                               _bbox_points, it, dxy));
1396             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1397                                               _snap_points, it, dxy));
1398         }
1400         /* Pick one */
1401         NR::Coord best = NR_HUGE;
1402         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1403             if (i->second) {
1404                 NR::Coord const m = NR::L2(i->first);
1405                 if (m < best) {
1406                     best = m;
1407                     dxy = i->first;
1408                 }
1409             }
1410         }
1411     }
1413     if (control) {
1414         /* Ensure that the horizontal and vertical constraint has been applied */
1415         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1416             dxy[NR::Y] = 0;
1417         } else {
1418             dxy[NR::X] = 0;
1419         }
1420     }
1422     NR::Matrix const move((NR::translate(dxy)));
1423     NR::Point const norm(0, 0);
1424     transform(move, norm);
1426     // status text
1427     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1428     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1429     _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);
1430     g_string_free(xs, TRUE);
1431     g_string_free(ys, TRUE);
1435 /*
1436   Local Variables:
1437   mode:c++
1438   c-file-style:"stroustrup"
1439   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1440   indent-tabs-mode:nil
1441   fill-column:99
1442   End:
1443 */
1444 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :