Code

fix by dvlierop2 for snapping bugs 1579556 and 1579587
[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();
104     _current.set_identity();
106     _center_is_set = false; // reread _center from items, or set to bbox midpoint
108     _updateHandles();
110     _selection = sp_desktop_selection(desktop);
112     _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
113                                SP_TYPE_CTRL,
114                                "anchor", GTK_ANCHOR_CENTER,
115                                "mode", SP_CTRL_MODE_COLOR,
116                                "shape", SP_CTRL_SHAPE_BITMAP,
117                                "size", 13.0,
118                                "filled", TRUE,
119                                "fill_color", 0x00000000,
120                                "stroked", TRUE,
121                                "stroke_color", 0x000000a0,
122                                "pixbuf", handles[12],
123                                NULL);
125     _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
126                                SP_TYPE_CTRL,
127                                "anchor", GTK_ANCHOR_CENTER,
128                                "mode", SP_CTRL_MODE_XOR,
129                                "shape", SP_CTRL_SHAPE_CROSS,
130                                "size", 7.0,
131                                "filled", TRUE,
132                                "fill_color", 0xffffff7f,
133                                "stroked", TRUE,
134                                "stroke_color", 0xffffffff,
135                                "pixbuf", handles[12],
136                                NULL);
138     sp_canvas_item_hide(_grip);
139     sp_canvas_item_hide(_norm);
141     for (int i = 0; i < 4; i++) {
142         _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
143         sp_canvas_item_hide(_l[i]);
144     }
146     _sel_changed_connection = _selection->connectChanged(
147         sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
148         );
150     _sel_modified_connection = _selection->connectModified(
151         sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
152         );
155 Inkscape::SelTrans::~SelTrans()
157     _sel_changed_connection.disconnect();
158     _sel_modified_connection.disconnect();
160     for (unsigned int i = 0; i < 8; i++) {
161         if (_shandle[i]) {
162             g_object_unref(G_OBJECT(_shandle[i]));
163             _shandle[i] = NULL;
164         }
165         if (_rhandle[i]) {
166             g_object_unref(G_OBJECT(_rhandle[i]));
167             _rhandle[i] = NULL;
168         }
169     }
170     if (_chandle) {
171         g_object_unref(G_OBJECT(_chandle));
172         _chandle = NULL;
173     }
175     if (_norm) {
176         gtk_object_destroy(GTK_OBJECT(_norm));
177         _norm = NULL;
178     }
179     if (_grip) {
180         gtk_object_destroy(GTK_OBJECT(_grip));
181         _grip = NULL;
182     }
183     for (int i = 0; i < 4; i++) {
184         if (_l[i]) {
185             gtk_object_destroy(GTK_OBJECT(_l[i]));
186             _l[i] = NULL;
187         }
188     }
190     for (unsigned i = 0; i < _items.size(); i++) {
191         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
192     }
194     _items.clear();
195     _items_centers.clear();
198 void Inkscape::SelTrans::resetState()
200     _state = STATE_SCALE;
203 void Inkscape::SelTrans::increaseState()
205     if (_state == STATE_SCALE) {
206         _state = STATE_ROTATE;
207     } else {
208         _state = STATE_SCALE;
209     }
211     _center_is_set = true; // no need to reread center
213     _updateHandles();
216 void Inkscape::SelTrans::setCenter(NR::Point const &p)
218     _center = p;
219     _center_is_set = true;
221     // Write the new center position into all selected items
222     for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
223         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
224         it->setCenter(p);
225         SP_OBJECT(it)->updateRepr();
226     }
227     sp_document_maybe_done (sp_desktop_document(_desktop), "center::move", SP_VERB_CONTEXT_SELECT, 
228                             _("Set center"));
230     _updateHandles();
233 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
235     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
237     g_return_if_fail(!_grabbed);
239     _grabbed = true;
240     _show_handles = show_handles;
241     _updateVolatileState();
242     _current.set_identity();
244     _changed = false;
246     if (_empty) {
247         return;
248     }
250     for (GSList const *l = selection->itemList(); l; l = l->next) {
251         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
252         _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
253         _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
254     }
256     _current.set_identity();
258     _point = p;
260     _snap_points = selection->getSnapPointsConvexHull();
261     _bbox_points = selection->getBBoxPointsOuter();
262     _bbox_4points = _bbox_points;
263     _bbox_4points.push_back(NR::Point(_bbox_points[0][NR::X], _bbox_points[1][NR::Y]));
264     _bbox_4points.push_back(NR::Point(_bbox_points[1][NR::X], _bbox_points[0][NR::Y]));
266     gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
267     bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
268     NR::Rect op_box = _box;
269     if (origin_on_bbox == false && _snap_points.empty() == false) {
270         std::vector<NR::Point>::iterator i = _snap_points.begin();
271         op_box = NR::Rect(*i, *i);
272         i++;
273         while (i != _snap_points.end()) {
274             op_box.expandTo(*i);
275             i++;
276         }
277     }
279     _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
281     if ((x != -1) && (y != -1)) {
282         sp_canvas_item_show(_norm);
283         sp_canvas_item_show(_grip);
284     }
286     if (_show == SHOW_OUTLINE) {
287         for (int i = 0; i < 4; i++)
288             sp_canvas_item_show(_l[i]);
289     }
292     _updateHandles();
293     g_return_if_fail(_stamp_cache == NULL);
296 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
298     g_return_if_fail(_grabbed);
299     g_return_if_fail(!_empty);
301     NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
303     if (_show == SHOW_CONTENT) {
304         // update the content
305         for (unsigned i = 0; i < _items.size(); i++) {
306             SPItem &item = *_items[i].first;
307             NR::Matrix const &prev_transform = _items[i].second;
308             sp_item_set_i2d_affine(&item, prev_transform * affine);
309         }
310     } else {
311         NR::Point p[4];
312         /* update the outline */
313         for (unsigned i = 0 ; i < 4 ; i++) {
314             p[i] = _box.corner(i) * affine;
315         }
316         for (unsigned i = 0 ; i < 4 ; i++) {
317             sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
318         }
319     }
321     _current = affine;
322     _changed = true;
323     _updateHandles();
326 void Inkscape::SelTrans::ungrab()
328     g_return_if_fail(_grabbed);
329     _grabbed = false;
330     _show_handles = true;
332     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
333     _updateVolatileState();
335     for (unsigned i = 0; i < _items.size(); i++) {
336         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
337     }
339     sp_canvas_item_hide(_norm);
340     sp_canvas_item_hide(_grip);
342     if (_show == SHOW_OUTLINE) {
343         for (int i = 0; i < 4; i++)
344             sp_canvas_item_hide(_l[i]);
345     }
347     if (_stamp_cache) {
348         g_slist_free(_stamp_cache);
349         _stamp_cache = NULL;
350     }
352     _message_context.clear();
354     if (!_empty && _changed) {
355         sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
356         _center *= _current;
357         _center_is_set = true;
359 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
360 // appropriately - it does not know the original positions of the centers (all objects already have
361 // the new bboxes). So we need to reset the centers from our saved array.
362         if (_show != SHOW_OUTLINE && !_current.is_translation()) {
363             for (unsigned i = 0; i < _items_centers.size(); i++) {
364                 SPItem *currentItem = _items_centers[i].first;
365                 if (currentItem->isCenterSet()) { // only if it's already set
366                     currentItem->setCenter (_items_centers[i].second * _current);
367                     SP_OBJECT(currentItem)->updateRepr();
368                 }
369             }
370         }
372         _items.clear();
373         _items_centers.clear();
375         if (_current.is_translation()) {
376             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
377                              _("Move"));
378         } else if (_current.is_scale()) {
379             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
380                              _("Scale"));
381         } else if (_current.is_rotation()) {
382             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
383                              _("Rotate"));
384         } else {
385             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
386                              _("Skew"));
387         }
389     } else {
390         _items.clear();
391         _items_centers.clear();
392         _updateHandles();
393     }
396 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
397 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
399 void Inkscape::SelTrans::stamp()
401     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
403     bool fixup = !_grabbed;
404     if ( fixup && _stamp_cache ) {
405         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
406         g_slist_free(_stamp_cache);
407         _stamp_cache = NULL;
408     }
410     /* stamping mode */
411     if (!_empty) {
412         GSList *l;
413         if (_stamp_cache) {
414             l = _stamp_cache;
415         } else {
416             /* Build cache */
417             l  = g_slist_copy((GSList *) selection->itemList());
418             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
419             _stamp_cache = l;
420         }
422         while (l) {
423             SPItem *original_item = SP_ITEM(l->data);
424             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
426             // remember the position of the item
427             gint pos = original_repr->position();
428             // remember parent
429             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
431             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
433             // add the new repr to the parent
434             parent->appendChild(copy_repr);
435             // move to the saved position
436             copy_repr->setPosition(pos > 0 ? pos : 0);
438             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
440             NR::Matrix const *new_affine;
441             if (_show == SHOW_OUTLINE) {
442                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
443                 NR::Matrix const i2dnew( i2d * _current );
444                 sp_item_set_i2d_affine(copy_item, i2dnew);
445                 new_affine = &copy_item->transform;
446             } else {
447                 new_affine = &original_item->transform;
448             }
450             sp_item_write_transform(copy_item, copy_repr, *new_affine);
452             if (copy_item->isCenterSet()) {
453                 copy_item->setCenter(_center * _current);
454             }
456             Inkscape::GC::release(copy_repr);
457             l = l->next;
458         }
459         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
460                          _("Stamp"));
461     }
463     if ( fixup && _stamp_cache ) {
464         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
465         g_slist_free(_stamp_cache);
466         _stamp_cache = NULL;
467     }
470 void Inkscape::SelTrans::_updateHandles()
472     if ( !_show_handles || _empty )
473     {
474         sp_remove_handles(_shandle, 8);
475         sp_remove_handles(_rhandle, 8);
476         sp_remove_handles(&_chandle, 1);
477         return;
478     }
480     // center handle
481     if ( _chandle == NULL ) {
482         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
484         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
485         _chandle->setSize (13);
486         _chandle->setAnchor (handle_center.anchor);
487         _chandle->setMode (SP_CTRL_MODE_XOR);
488         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
489         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
490         _chandle->setPixbuf(handles[handle_center.control]);
491         sp_knot_update_ctrl(_chandle);
493         g_signal_connect(G_OBJECT(_chandle), "request",
494                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
495         g_signal_connect(G_OBJECT(_chandle), "moved",
496                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
497         g_signal_connect(G_OBJECT(_chandle), "grabbed",
498                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
499         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
500                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
501         g_signal_connect(G_OBJECT(_chandle), "clicked",
502                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
503     }
505     sp_remove_handles(&_chandle, 1);
506     if ( _state == STATE_SCALE ) {
507         sp_remove_handles(_rhandle, 8);
508         _showHandles(_shandle, handles_scale, 8,
509                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
510                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
511     } else {
512         sp_remove_handles(_shandle, 8);
513         _showHandles(_rhandle, handles_rotate, 8,
514                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
515                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
516     }
518     if (!_center_is_set) {
519         _center = _desktop->selection->center();
520         _center_is_set = true;
521     }
523     if ( _state == STATE_SCALE ) {
524         sp_knot_hide(_chandle);
525     } else {
526         sp_knot_show(_chandle);
527         sp_knot_moveto(_chandle, &_center);
528     }
531 void Inkscape::SelTrans::_updateVolatileState()
533     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
534     _empty = selection->isEmpty();
536     if (_empty) {
537         return;
538     }
540     _box = selection->bounds();
541     if (_box.isEmpty()) {
542         _empty = true;
543         return;
544     }
546     _strokewidth = stroke_average_width (selection->itemList());
549 static void sp_remove_handles(SPKnot *knot[], gint num)
551     for (int i = 0; i < num; i++) {
552         if (knot[i] != NULL) {
553             sp_knot_hide(knot[i]);
554         }
555     }
558 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
559                              gchar const *even_tip, gchar const *odd_tip)
561     g_return_if_fail( !_empty );
563     for (int i = 0; i < num; i++) {
564         if (knot[i] == NULL) {
565             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
567             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
568             knot[i]->setSize (13);
569             knot[i]->setAnchor (handle[i].anchor);
570             knot[i]->setMode (SP_CTRL_MODE_XOR);
571             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
572             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
573             knot[i]->setPixbuf(handles[handle[i].control]);
574             sp_knot_update_ctrl(knot[i]);
576             g_signal_connect(G_OBJECT(knot[i]), "request",
577                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
578             g_signal_connect(G_OBJECT(knot[i]), "moved",
579                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
580             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
581                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
582             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
583                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
584             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
585         }
586         sp_knot_show(knot[i]);
588         NR::Point const handle_pt(handle[i].x, handle[i].y);
589         NR::Point p( _box.min()
590                      + ( _box.dimensions()
591                          * NR::scale(handle_pt) ) );
593         sp_knot_moveto(knot[i], &p);
594     }
597 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
599     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
600         knot, state, *(SPSelTransHandle const *) data
601         );
604 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
606     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
609 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
611     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
612         knot, position, state, *(SPSelTransHandle const *) data
613         );
616 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
618     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
619         knot, position, state, *(SPSelTransHandle const *) data
620         );
623 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
625     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
626         knot, state, *(SPSelTransHandle const *) data
627         );
630 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
632     switch (handle.anchor) {
633         case GTK_ANCHOR_CENTER:
634             if (state & GDK_SHIFT_MASK) {
635                 // Unset the  center position for all selected items
636                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
637                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
638                     it->unsetCenter();
639                     SP_OBJECT(it)->updateRepr();
640                     _center_is_set = false;  // center has changed
641                     _updateHandles();
642                 }
643                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT, 
644                                         _("Reset center"));
645             }
646             break;
647         default:
648             break;
649     }
652 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
654     switch (handle.anchor) {
655         case GTK_ANCHOR_CENTER:
656             g_object_set(G_OBJECT(_grip),
657                          "shape", SP_CTRL_SHAPE_BITMAP,
658                          "size", 13.0,
659                          NULL);
660             sp_canvas_item_show(_grip);
661             break;
662         default:
663             g_object_set(G_OBJECT(_grip),
664                          "shape", SP_CTRL_SHAPE_CROSS,
665                          "size", 7.0,
666                          NULL);
667             sp_canvas_item_show(_norm);
668             sp_canvas_item_show(_grip);
670             break;
671     }
673     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
677 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
679     if (!SP_KNOT_IS_GRABBED(knot)) {
680         return;
681     }
683     // in case items have been unhooked from the document, don't
684     // try to continue processing events for them.
685     for (unsigned int i = 0; i < _items.size(); i++) {
686         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
687             return;
688         }
689     }
691     handle.action(this, handle, *position, state);
695 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
697     if (!SP_KNOT_IS_GRABBED(knot)) {
698         return TRUE;
699     }
701     knot->desktop->set_coordinate_status(*position);
702     knot->desktop->setPosition(*position);
705     if (state & GDK_MOD1_MASK) {
706         *position = _point + ( *position - _point ) / 10;
707     }
709     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
710         _origin = _opposite;
711     } else {
712         _origin = _center;
713     }
714     if (handle.request(this, handle, *position, state)) {
715         sp_knot_set_position(knot, position, state);
716         SP_CTRL(_grip)->moveto(*position);
717         SP_CTRL(_norm)->moveto(_origin);
718     }
720     return TRUE;
724 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
726     if (!_grabbed) {
727         _updateVolatileState();
728         _current.set_identity();
729         _center_is_set = false; // center(s) may have changed
730         _updateHandles();
731     }
734 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
736     if (!_grabbed) {
737         _updateVolatileState();
738         _current.set_identity();
740         // reset internal flag
741         _changed = false;
743         _center_is_set = false;  // center(s) may have changed
745         _updateHandles();
746     }
749 /*
750  * handlers for handle move-request
751  */
753 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
754 static double sign(double const x)
756     return ( x < 0
757              ? -1
758              : 1 );
761 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
762                                     SPSelTransHandle const &, NR::Point &pt, guint state)
764     return seltrans->scaleRequest(pt, state);
767 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
768                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
770     return seltrans->stretchRequest(handle, pt, state);
773 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
774                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
776     return seltrans->skewRequest(handle, pt, state);
779 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
780                                      SPSelTransHandle const &, NR::Point &pt, guint state)
782     return seltrans->rotateRequest(pt, state);
785 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
786                                      SPSelTransHandle const &, NR::Point &pt, guint state)
788     return seltrans->centerRequest(pt, state);
791 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
793     using NR::X;
794     using NR::Y;
796     NR::Point d = _point - _origin;
797     NR::scale s(0, 0);
799     /* Work out the new scale factors `s' */
800     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
801         if ( fabs(d[i]) > 0.001 ) {
802             s[i] = ( pt[i] - _origin[i] ) / d[i];
803             if ( fabs(s[i]) < 1e-9 ) {
804                 s[i] = 1e-9;
805             }
806         }
807     }
809     SnapManager const &m = _desktop->namedview->snap_manager;
811     /* Get a STL list of the selected items.
812     ** FIXME: this should probably be done by Inkscape::Selection.
813     */
814     std::list<SPItem const*> it;
815     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
816         it.push_back(reinterpret_cast<SPItem*>(i->data));
817     }
819     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
820         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
821         ** To do this, we snap along a suitable constraint vector from the origin.
822         */
824         NR::Point const cv = NR::Point(
825             pt[NR::X] > _origin[NR::X] ? 1 : -1,
826             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
827             );
829         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
830                                                                _bbox_points,
831                                                                it,
832                                                                Snapper::ConstraintLine(_origin, cv),
833                                                                s,
834                                                                _origin);
836         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
837                                                                _snap_points,
838                                                                it,
839                                                                Snapper::ConstraintLine(_origin, cv),
840                                                                s,
841                                                                _origin);
843         if (bb.second == false && sn.second == false) {
845             /* We didn't snap, so just lock aspect ratio */
846             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
847                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
848             } else {
849                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
850             }
852         } else {
854             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
855             ** just compare difference in s[X].
856             */
857             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
858             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
859             s = (bd < sd) ? bb.first : sn.first;
860         }
862     } else {
863         /* Scale aspect ratio is unlocked */
864         
865         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
866                                                         _bbox_4points,
867                                                         it,
868                                                         s,
869                                                         _origin);
870         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
871                                                         _snap_points,
872                                                         it,
873                                                         s,
874                                                         _origin);
876         /* Pick the snap that puts us closest to the original scale */
877         NR::Coord bd = bb.second ?
878             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
879                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
880             : NR_HUGE;
881         NR::Coord sd = sn.second ?
882             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
883                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
884             : NR_HUGE;
885         s = (bd < sd) ? bb.first : sn.first;
886     }
888     /* Update the knot position */
889     pt = ( _point - _origin ) * s + _origin;
891     /* Status text */
892     _message_context.setF(Inkscape::NORMAL_MESSAGE,
893                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
894                           100 * s[NR::X], 100 * s[NR::Y]);
896     return TRUE;
899 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
901     using NR::X;
902     using NR::Y;
904     NR::Dim2 axis, perp;
906     switch (handle.cursor) {
907         case GDK_TOP_SIDE:
908         case GDK_BOTTOM_SIDE:
909            axis = NR::Y;
910            perp = NR::X;
911            break;
912         case GDK_LEFT_SIDE:
913         case GDK_RIGHT_SIDE:
914            axis = NR::X;
915            perp = NR::Y;
916            break;
917         default:
918             g_assert_not_reached();
919             return TRUE;
920     };
922     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
923         return FALSE;
924     }
926     NR::scale s(1, 1);
927     s[axis] = ( ( pt[axis] - _origin[axis] )
928                 / ( _point[axis] - _origin[axis] ) );
929     if ( fabs(s[axis]) < 1e-15 ) {
930         s[axis] = 1e-15;
931     }
933     /* Get a STL list of the selected items.
934     ** FIXME: this should probably be done by Inkscape::Selection.
935     */
936     std::list<SPItem const*> it;
937     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
938         it.push_back(reinterpret_cast<SPItem*>(i->data));
939     }
941     SnapManager const &m = _desktop->namedview->snap_manager;
943     if ( state & GDK_CONTROL_MASK ) {
944         s[perp] = fabs(s[axis]);
946         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
947             Snapper::BBOX_POINT,
948             _bbox_points,
949             it,
950             s[axis],
951             _origin,
952             axis,
953             true);
955         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
956             Snapper::SNAP_POINT,
957             _snap_points,
958             it,
959             s[axis],
960             _origin,
961             axis,
962             true);
964         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
965         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
966         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
968         s[axis] = fabs(ratio) * sign(s[axis]);
969         s[perp] = fabs(s[axis]);
970     } else {
972         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
973             Snapper::BBOX_POINT,
974             _bbox_points,
975             it,
976             s[axis],
977             _origin,
978             axis,
979             false);
981         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
982             Snapper::SNAP_POINT,
983             _snap_points,
984             it,
985             s[axis],
986             _origin,
987             axis,
988             false);
990         /* Choose the smaller difference in scale */
991         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
992         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
993         s[axis] = (bd < sd) ? bb.first : sn.first;
994         s[perp] = 1;
995     }
997     pt = ( _point - _origin ) * NR::scale(s) + _origin;
998     if (isNaN(pt[X] + pt[Y])) {
999         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1000                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1001     }
1003     // status text
1004     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1005                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1006                           100 * s[NR::X], 100 * s[NR::Y]);
1008     return TRUE;
1011 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1013     using NR::X;
1014     using NR::Y;
1016     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1017         return FALSE;
1018     }
1020     NR::Dim2 dim_a;
1021     NR::Dim2 dim_b;
1022     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1023         dim_a = X;
1024         dim_b = Y;
1025     } else {
1026         dim_a = Y;
1027         dim_b = X;
1028     }
1030     double skew[2];
1031     double s[2] = { 1.0, 1.0 };
1033     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1034         return FALSE;
1035     }
1037     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1039     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1041     if ( fabs(s[dim_a]) < 1 ) {
1042         s[dim_a] = sign(s[dim_a]);
1043     } else {
1044         s[dim_a] = floor( s[dim_a] + 0.5 );
1045     }
1047     double radians = atan(skew[dim_a] / s[dim_a]);
1049     if (state & GDK_CONTROL_MASK) {
1051         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1053         if (snaps) {
1054             double sections = floor( radians * snaps / M_PI + .5 );
1055             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1056             radians = ( M_PI / snaps ) * sections;
1057         }
1058         skew[dim_a] = tan(radians) * s[dim_a];
1059     } else {
1060         SnapManager const &m = _desktop->namedview->snap_manager;
1062         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1063                                                        _bbox_points,
1064                                                        std::list<SPItem const *>(),
1065                                                        skew[dim_a],
1066                                                        _origin,
1067                                                        dim_a);
1069         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1070                                                        _snap_points,
1071                                                        std::list<SPItem const *>(),
1072                                                        skew[dim_a],
1073                                                        _origin,
1074                                                        dim_a);
1075         
1076         if (bb.second || sn.second) {
1077             /* We snapped something, so change the skew to reflect it */
1078             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1079             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1080             skew[dim_a] = std::min(bd, sd);
1081         }
1082     }
1084     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1085     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1087     /* status text */
1088     double degrees = 180 / M_PI * radians;
1089     if (degrees > 180) degrees -= 360;
1090     if (degrees < -180) degrees += 360;
1092     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1093                           // TRANSLATORS: don't modify the first ";"
1094                           // (it will NOT be displayed as ";" - only the second one will be)
1095                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1096                           degrees);
1098     return TRUE;
1101 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1103     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1105     // rotate affine in rotate
1106     NR::Point const d1 = _point - _origin;
1107     NR::Point const d2 = pt     - _origin;
1109     NR::Coord const h1 = NR::L2(d1);
1110     if (h1 < 1e-15) return FALSE;
1111     NR::Point q1 = d1 / h1;
1112     NR::Coord const h2 = NR::L2(d2);
1113     if (fabs(h2) < 1e-15) return FALSE;
1114     NR::Point q2 = d2 / h2;
1116     double radians;
1117     if (state & GDK_CONTROL_MASK) {
1118         /* Have to restrict movement. */
1119         double cos_t = NR::dot(q1, q2);
1120         double sin_t = NR::dot(NR::rot90(q1), q2);
1121         radians = atan2(sin_t, cos_t);
1122         if (snaps) {
1123             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1124         }
1125         q1 = NR::Point(1, 0);
1126         q2 = NR::Point(cos(radians), sin(radians));
1127     } else {
1128         radians = atan2(NR::dot(NR::rot90(d1), d2),
1129                         NR::dot(d1, d2));
1130     }
1132     NR::rotate const r1(q1);
1133     NR::rotate const r2(q2);
1134     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1136     /* status text */
1137     double degrees = 180 / M_PI * radians;
1138     if (degrees > 180) degrees -= 360;
1139     if (degrees < -180) degrees += 360;
1141     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1142                           // TRANSLATORS: don't modify the first ";"
1143                           // (it will NOT be displayed as ";" - only the second one will be)
1144                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1146     return TRUE;
1149 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1151     using NR::X;
1152     using NR::Y;
1154     SnapManager const &m = _desktop->namedview->snap_manager;
1155     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1157     if (state & GDK_CONTROL_MASK) {
1158         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1159             pt[Y] = _point[Y];
1160         } else {
1161             pt[X] = _point[X];
1162         }
1163     }
1165     if (!(state & GDK_SHIFT_MASK)) {
1166         // screen pixels to snap center to bbox
1167 #define SNAP_DIST 5
1168         // FIXME: take from prefs
1169         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1171         for (int i = 0; i < 2; i++) {
1173             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1174                 pt[i] = _box.min()[i];
1175             }
1176             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1177                 pt[i] = _box.midpoint()[i];
1178             }
1179             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1180                 pt[i] = _box.max()[i];
1181             }
1182         }
1183     }
1185     // status text
1186     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1187     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1188     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1189     g_string_free(xs, FALSE);
1190     g_string_free(ys, FALSE);
1192     return TRUE;
1195 /*
1196  * handlers for handle movement
1197  *
1198  */
1200 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1202     seltrans->stretch(handle, pt, state);
1205 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1207     seltrans->scale(pt, state);
1210 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1212     seltrans->skew(handle, pt, state);
1215 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1217     seltrans->rotate(pt, state);
1220 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1222     using NR::X;
1223     using NR::Y;
1225     NR::Dim2 dim;
1226     switch (handle.cursor) {
1227         case GDK_LEFT_SIDE:
1228         case GDK_RIGHT_SIDE:
1229             dim = X;
1230             break;
1231         case GDK_TOP_SIDE:
1232         case GDK_BOTTOM_SIDE:
1233             dim = Y;
1234             break;
1235         default:
1236             g_assert_not_reached();
1237             abort();
1238             break;
1239     }
1241     NR::Point const scale_origin(_origin);
1242     double const offset = _point[dim] - scale_origin[dim];
1243     if (!( fabs(offset) >= 1e-15 )) {
1244         return;
1245     }
1246     NR::scale s(1, 1);
1247     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1248     if (isNaN(s[dim])) {
1249         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1250                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1251     }
1252     if (!( fabs(s[dim]) >= 1e-15 )) {
1253         s[dim] = 1e-15;
1254     }
1255     if (state & GDK_CONTROL_MASK) {
1256         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1257         s[!dim] = fabs(s[dim]);
1258     }
1260     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1261     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1263     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1264     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1265                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1267     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1270 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1272     NR::Point const offset = _point - _origin;
1274     NR::scale s (1, 1);
1275     for (int i = NR::X; i <= NR::Y; i++) {
1276         if (fabs(offset[i]) > 1e-9)
1277             s[i] = (pt[i] - _origin[i]) / offset[i];
1278         if (fabs(s[i]) < 1e-9)
1279             s[i] = 1e-9;
1280     }
1281     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1282     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1284     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1285     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1286                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1288     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1291 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1293     NR::Point const offset = _point - _origin;
1295     unsigned dim;
1296     switch (handle.cursor) {
1297         case GDK_SB_H_DOUBLE_ARROW:
1298             dim = NR::Y;
1299             break;
1300         case GDK_SB_V_DOUBLE_ARROW:
1301             dim = NR::X;
1302             break;
1303         default:
1304             g_assert_not_reached();
1305             abort();
1306             break;
1307     }
1308     if (fabs(offset[dim]) < 1e-15) {
1309         return;
1310     }
1311     NR::Matrix skew = NR::identity();
1312     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1313     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1314     skew[2*(1-dim) + (dim)] = 0;
1315     skew[2*(1-dim) + (1-dim)] = 1;
1317     for (int i = 0; i < 2; i++) {
1318         if (fabs(skew[3*i]) < 1e-15) {
1319             skew[3*i] = 1e-15;
1320         }
1321     }
1322     transform(skew, _origin);
1325 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1327     NR::Point const offset = _point - _origin;
1329     NR::Coord const h1 = NR::L2(offset);
1330     if (h1 < 1e-15) {
1331         return;
1332     }
1333     NR::Point const q1 = offset / h1;
1334     NR::Coord const h2 = NR::L2( pt - _origin );
1335     if (h2 < 1e-15) {
1336         return;
1337     }
1338     NR::Point const q2 = (pt - _origin) / h2;
1339     NR::rotate const r1(q1);
1340     NR::rotate const r2(q2);
1342     NR::Matrix rotate( r2 / r1 );
1343     transform(rotate, _origin);
1346 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1348     seltrans->setCenter(pt);
1352 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1354     SnapManager const &m = _desktop->namedview->snap_manager;
1356     /* The amount that we've moved by during this drag */
1357     NR::Point dxy = xy - _point;
1359     /* Get a STL list of the selected items.
1360     ** FIXME: this should probably be done by Inkscape::Selection.
1361     */
1362     std::list<SPItem const*> it;
1363     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1364         it.push_back(reinterpret_cast<SPItem*>(i->data));
1365     }
1367     bool const alt = (state & GDK_MOD1_MASK);
1368     bool const control = (state & GDK_CONTROL_MASK);
1369     bool const shift = (state & GDK_SHIFT_MASK);
1371     if (alt) {
1373         /* Alt pressed means keep offset: snap the moved distance to the grid.
1374         ** FIXME: this will snap to more than just the grid, nowadays.
1375         */
1377         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1379     } else if (!shift) {
1381         /* We're snapping to things, possibly with a constraint to horizontal or
1382         ** vertical movement.  Obtain a list of possible translations and then
1383         ** pick the smallest.
1384         */
1386         /* This will be our list of possible translations */
1387         std::list<std::pair<NR::Point, bool> > s;
1389         if (control) {
1391             /* Snap to things, and also constrain to horizontal or vertical movement */
1393             for (unsigned int dim = 0; dim < 2; dim++) {
1394                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1395                                                          _bbox_points,
1396                                                          it,
1397                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1398                                                          dxy));
1399                             
1400                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1401                                                          _snap_points,
1402                                                          it,
1403                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1404                                                          dxy));
1405             }
1407         } else {
1409             /* Snap to things with no constraint */
1411             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1412                                               _bbox_4points, it, dxy));
1413             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1414                                               _snap_points, it, dxy));
1415         }
1417         /* Pick one */
1418         NR::Coord best = NR_HUGE;
1419         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1420             if (i->second) {
1421                 NR::Coord const m = NR::L2(i->first);
1422                 if (m < best) {
1423                     best = m;
1424                     dxy = i->first;
1425                 }
1426             }
1427         }
1428     }
1430     if (control) {
1431         /* Ensure that the horizontal and vertical constraint has been applied */
1432         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1433             dxy[NR::Y] = 0;
1434         } else {
1435             dxy[NR::X] = 0;
1436         }
1437     }
1439     NR::Matrix const move((NR::translate(dxy)));
1440     NR::Point const norm(0, 0);
1441     transform(move, norm);
1443     // status text
1444     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1445     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1446     _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);
1447     g_string_free(xs, TRUE);
1448     g_string_free(ys, TRUE);
1452 /*
1453   Local Variables:
1454   mode:c++
1455   c-file-style:"stroustrup"
1456   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1457   indent-tabs-mode:nil
1458   fill-column:99
1459   End:
1460 */
1461 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :