Code

Ok, committed msgloan's patch to convert gbooleans to bools thus completing
[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 bool sp_sel_trans_handle_request(SPKnot *knot, NR::Point *p, guint state, bool *data);
58 extern GdkPixbuf *handles[];
60 static bool sp_seltrans_handle_event(SPKnot *knot, GdkEvent *event, gpointer)
61 {
62     switch (event->type) {
63         case GDK_MOTION_NOTIFY:
64             break;
65         case GDK_KEY_PRESS:
66             if (get_group0_keyval (&event->key) == GDK_space) {
67                 /* stamping mode: both mode(show content and outline) operation with knot */
68                 if (!SP_KNOT_IS_GRABBED(knot)) {
69                     return FALSE;
70                 }
71                 SPDesktop *desktop = knot->desktop;
72                 Inkscape::SelTrans *seltrans = SP_SELECT_CONTEXT(desktop->event_context)->_seltrans;
73                 seltrans->stamp();
74                 return TRUE;
75             }
76             break;
77         default:
78             break;
79     }
81     return FALSE;
82 }
84 Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
85     _desktop(desktop),
86     _selcue(desktop),
87     _state(STATE_SCALE),
88     _show(SHOW_CONTENT),
89     _grabbed(false),
90     _show_handles(true),
91     _box(NR::Point(0,0), NR::Point(0,0)),
92     _chandle(NULL),
93     _stamp_cache(NULL),
94     _message_context(desktop->messageStack())
95 {
96     g_return_if_fail(desktop != NULL);
98     for (int i = 0; i < 8; i++) {
99         _shandle[i] = NULL;
100         _rhandle[i] = NULL;
101     }
103     _updateVolatileState();
105     _center_is_set = false; // reread _center from items, or set to bbox midpoint
107     _updateHandles();
109     _selection = sp_desktop_selection(desktop);
111     _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
112                                SP_TYPE_CTRL,
113                                "anchor", GTK_ANCHOR_CENTER,
114                                "mode", SP_CTRL_MODE_COLOR,
115                                "shape", SP_CTRL_SHAPE_BITMAP,
116                                "size", 13.0,
117                                "filled", TRUE,
118                                "fill_color", 0x00000000,
119                                "stroked", TRUE,
120                                "stroke_color", 0x000000a0,
121                                "pixbuf", handles[12],
122                                NULL);
124     _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
125                                SP_TYPE_CTRL,
126                                "anchor", GTK_ANCHOR_CENTER,
127                                "mode", SP_CTRL_MODE_XOR,
128                                "shape", SP_CTRL_SHAPE_CROSS,
129                                "size", 7.0,
130                                "filled", TRUE,
131                                "fill_color", 0xffffff7f,
132                                "stroked", TRUE,
133                                "stroke_color", 0xffffffff,
134                                "pixbuf", handles[12],
135                                NULL);
137     sp_canvas_item_hide(_grip);
138     sp_canvas_item_hide(_norm);
140     for (int i = 0; i < 4; i++) {
141         _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
142         sp_canvas_item_hide(_l[i]);
143     }
145     _sel_changed_connection = _selection->connectChanged(
146         sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
147         );
149     _sel_modified_connection = _selection->connectModified(
150         sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
151         );
154 Inkscape::SelTrans::~SelTrans()
156     _sel_changed_connection.disconnect();
157     _sel_modified_connection.disconnect();
159     for (unsigned int i = 0; i < 8; i++) {
160         if (_shandle[i]) {
161             g_object_unref(G_OBJECT(_shandle[i]));
162             _shandle[i] = NULL;
163         }
164         if (_rhandle[i]) {
165             g_object_unref(G_OBJECT(_rhandle[i]));
166             _rhandle[i] = NULL;
167         }
168     }
169     if (_chandle) {
170         g_object_unref(G_OBJECT(_chandle));
171         _chandle = NULL;
172     }
174     if (_norm) {
175         gtk_object_destroy(GTK_OBJECT(_norm));
176         _norm = NULL;
177     }
178     if (_grip) {
179         gtk_object_destroy(GTK_OBJECT(_grip));
180         _grip = NULL;
181     }
182     for (int i = 0; i < 4; i++) {
183         if (_l[i]) {
184             gtk_object_destroy(GTK_OBJECT(_l[i]));
185             _l[i] = NULL;
186         }
187     }
189     for (unsigned i = 0; i < _items.size(); i++) {
190         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
191     }
193     _items.clear();
194     _items_centers.clear();
197 void Inkscape::SelTrans::resetState()
199     _state = STATE_SCALE;
202 void Inkscape::SelTrans::increaseState()
204     if (_state == STATE_SCALE) {
205         _state = STATE_ROTATE;
206     } else {
207         _state = STATE_SCALE;
208     }
210     _center_is_set = true; // no need to reread center
212     _updateHandles();
215 void Inkscape::SelTrans::setCenter(NR::Point const &p)
217     _center = p;
218     _center_is_set = true;
220     // Write the new center position into all selected items
221     for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
222         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
223         it->setCenter(p);
224         SP_OBJECT(it)->updateRepr();
225     }
226     sp_document_maybe_done (sp_desktop_document(_desktop), "center::move", SP_VERB_CONTEXT_SELECT, 
227                             _("Set center"));
229     _updateHandles();
232 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
234     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
236     g_return_if_fail(!_grabbed);
238     _grabbed = true;
239     _show_handles = show_handles;
240     _updateVolatileState();
242     _changed = false;
244     if (_empty) {
245         return;
246     }
248     for (GSList const *l = selection->itemList(); l; l = l->next) {
249         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
250         _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
251         _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
252     }
254     _current.set_identity();
256     _point = p;
258     _snap_points = selection->getSnapPointsConvexHull();
259     _bbox_points = selection->getBBoxPointsOuter();
261     gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
262     bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
263     NR::Rect op_box = _box;
264     if (origin_on_bbox == false && _snap_points.empty() == false) {
265         std::vector<NR::Point>::iterator i = _snap_points.begin();
266         op_box = NR::Rect(*i, *i);
267         i++;
268         while (i != _snap_points.end()) {
269             op_box.expandTo(*i);
270             i++;
271         }
272     }
274     _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
276     if ((x != -1) && (y != -1)) {
277         sp_canvas_item_show(_norm);
278         sp_canvas_item_show(_grip);
279     }
281     if (_show == SHOW_OUTLINE) {
282         for (int i = 0; i < 4; i++)
283             sp_canvas_item_show(_l[i]);
284     }
287     _updateHandles();
288     g_return_if_fail(_stamp_cache == NULL);
291 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
293     g_return_if_fail(_grabbed);
294     g_return_if_fail(!_empty);
296     NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
298     if (_show == SHOW_CONTENT) {
299         // update the content
300         for (unsigned i = 0; i < _items.size(); i++) {
301             SPItem &item = *_items[i].first;
302             NR::Matrix const &prev_transform = _items[i].second;
303             sp_item_set_i2d_affine(&item, prev_transform * affine);
304         }
305     } else {
306         NR::Point p[4];
307         /* update the outline */
308         for (unsigned i = 0 ; i < 4 ; i++) {
309             p[i] = _box.corner(i) * affine;
310         }
311         for (unsigned i = 0 ; i < 4 ; i++) {
312             sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
313         }
314     }
316     _current = affine;
317     _changed = true;
318     _updateHandles();
321 void Inkscape::SelTrans::ungrab()
323     g_return_if_fail(_grabbed);
324     _grabbed = false;
325     _show_handles = true;
327     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
328     bool updh = true;
329     if (!_empty && _changed) {
330         sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
331         _center *= _current;
332         _center_is_set = true;
334 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
335 // appropriately - it does not know the original positions of the centers (all objects already have
336 // the new bboxes). So we need to reset the centers from our saved array.
337         if (_show != SHOW_OUTLINE && !_current.is_translation()) {
338             for (unsigned i = 0; i < _items_centers.size(); i++) {
339                 SPItem *currentItem = _items_centers[i].first;
340                 if (currentItem->isCenterSet()) { // only if it's already set
341                     currentItem->setCenter (_items_centers[i].second * _current);
342                     SP_OBJECT(currentItem)->updateRepr();
343                 }
344             }
345         }
347         if (_current.is_translation()) {
348             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
349                              _("Move"));
350         } else if (_current.is_scale()) {
351             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
352                              _("Scale"));
353         } else if (_current.is_rotation()) {
354             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
355                              _("Rotate"));
356         } else {
357             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
358                              _("Skew"));
359         }
361         updh = false;
362     }
364     for (unsigned i = 0; i < _items.size(); i++) {
365         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
366     }
367     _items.clear();
368     _items_centers.clear();
370     sp_canvas_item_hide(_norm);
371     sp_canvas_item_hide(_grip);
373     if (_show == SHOW_OUTLINE) {
374         for (int i = 0; i < 4; i++)
375             sp_canvas_item_hide(_l[i]);
376     }
378     _updateVolatileState();
379     if (updh) {
380         _updateHandles();
381     }
382     if (_stamp_cache) {
383         g_slist_free(_stamp_cache);
384         _stamp_cache = NULL;
385     }
387     _message_context.clear();
390 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
391 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
393 void Inkscape::SelTrans::stamp()
395     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
397     bool fixup = !_grabbed;
398     if ( fixup && _stamp_cache ) {
399         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
400         g_slist_free(_stamp_cache);
401         _stamp_cache = NULL;
402     }
404     /* stamping mode */
405     if (!_empty) {
406         GSList *l;
407         if (_stamp_cache) {
408             l = _stamp_cache;
409         } else {
410             /* Build cache */
411             l  = g_slist_copy((GSList *) selection->itemList());
412             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
413             _stamp_cache = l;
414         }
416         while (l) {
417             SPItem *original_item = SP_ITEM(l->data);
418             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
420             // remember the position of the item
421             gint pos = original_repr->position();
422             // remember parent
423             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
425             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
427             // add the new repr to the parent
428             parent->appendChild(copy_repr);
429             // move to the saved position
430             copy_repr->setPosition(pos > 0 ? pos : 0);
432             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
434             NR::Matrix const *new_affine;
435             if (_show == SHOW_OUTLINE) {
436                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
437                 NR::Matrix const i2dnew( i2d * _current );
438                 sp_item_set_i2d_affine(copy_item, i2dnew);
439                 new_affine = &copy_item->transform;
440             } else {
441                 new_affine = &original_item->transform;
442             }
444             sp_item_write_transform(copy_item, copy_repr, *new_affine);
446             if (copy_item->isCenterSet()) {
447                 copy_item->setCenter(_center * _current);
448             }
450             Inkscape::GC::release(copy_repr);
451             l = l->next;
452         }
453         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
454                          _("Stamp"));
455     }
457     if ( fixup && _stamp_cache ) {
458         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
459         g_slist_free(_stamp_cache);
460         _stamp_cache = NULL;
461     }
464 void Inkscape::SelTrans::_updateHandles()
466     if ( !_show_handles || _empty )
467     {
468         sp_remove_handles(_shandle, 8);
469         sp_remove_handles(_rhandle, 8);
470         sp_remove_handles(&_chandle, 1);
471         return;
472     }
474     // center handle
475     if ( _chandle == NULL ) {
476         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
478         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
479         _chandle->setSize (13);
480         _chandle->setAnchor (handle_center.anchor);
481         _chandle->setMode (SP_CTRL_MODE_XOR);
482         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
483         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
484         _chandle->setPixbuf(handles[handle_center.control]);
485         sp_knot_update_ctrl(_chandle);
487         g_signal_connect(G_OBJECT(_chandle), "request",
488                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
489         g_signal_connect(G_OBJECT(_chandle), "moved",
490                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
491         g_signal_connect(G_OBJECT(_chandle), "grabbed",
492                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
493         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
494                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
495         g_signal_connect(G_OBJECT(_chandle), "clicked",
496                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
497     }
499     sp_remove_handles(&_chandle, 1);
500     if ( _state == STATE_SCALE ) {
501         sp_remove_handles(_rhandle, 8);
502         _showHandles(_shandle, handles_scale, 8,
503                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
504                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
505     } else {
506         sp_remove_handles(_shandle, 8);
507         _showHandles(_rhandle, handles_rotate, 8,
508                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
509                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
510     }
512     if (!_center_is_set) {
513         _center = _desktop->selection->center();
514         _center_is_set = true;
515     }
517     if ( _state == STATE_SCALE ) {
518         sp_knot_hide(_chandle);
519     } else {
520         sp_knot_show(_chandle);
521         sp_knot_moveto(_chandle, &_center);
522     }
525 void Inkscape::SelTrans::_updateVolatileState()
527     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
528     _empty = selection->isEmpty();
530     if (_empty) {
531         return;
532     }
534     _box = selection->bounds();
535     if (_box.isEmpty()) {
536         _empty = true;
537         return;
538     }
540     _strokewidth = stroke_average_width (selection->itemList());
542     _current.set_identity();
545 static void sp_remove_handles(SPKnot *knot[], gint num)
547     for (int i = 0; i < num; i++) {
548         if (knot[i] != NULL) {
549             sp_knot_hide(knot[i]);
550         }
551     }
554 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
555                              gchar const *even_tip, gchar const *odd_tip)
557     g_return_if_fail( !_empty );
559     for (int i = 0; i < num; i++) {
560         if (knot[i] == NULL) {
561             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
563             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
564             knot[i]->setSize (13);
565             knot[i]->setAnchor (handle[i].anchor);
566             knot[i]->setMode (SP_CTRL_MODE_XOR);
567             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
568             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
569             knot[i]->setPixbuf(handles[handle[i].control]);
570             sp_knot_update_ctrl(knot[i]);
572             g_signal_connect(G_OBJECT(knot[i]), "request",
573                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
574             g_signal_connect(G_OBJECT(knot[i]), "moved",
575                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
576             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
577                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
578             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
579                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
580             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
581         }
582         sp_knot_show(knot[i]);
584         NR::Point const handle_pt(handle[i].x, handle[i].y);
585         NR::Point p( _box.min()
586                      + ( _box.dimensions()
587                          * NR::scale(handle_pt) ) );
589         sp_knot_moveto(knot[i], &p);
590     }
593 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
595     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
596         knot, state, *(SPSelTransHandle const *) data
597         );
600 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
602     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
605 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
607     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
608         knot, position, state, *(SPSelTransHandle const *) data
609         );
612 static bool sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, bool *data)
614     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
615         knot, position, state, *(SPSelTransHandle const *) data
616         );
619 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
621     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
622         knot, state, *(SPSelTransHandle const *) data
623         );
626 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
628     switch (handle.anchor) {
629         case GTK_ANCHOR_CENTER:
630             if (state & GDK_SHIFT_MASK) {
631                 // Unset the  center position for all selected items
632                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
633                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
634                     it->unsetCenter();
635                     SP_OBJECT(it)->updateRepr();
636                     _center_is_set = false;  // center has changed
637                     _updateHandles();
638                 }
639                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT, 
640                                         _("Reset center"));
641             }
642             break;
643         default:
644             break;
645     }
648 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
650     switch (handle.anchor) {
651         case GTK_ANCHOR_CENTER:
652             g_object_set(G_OBJECT(_grip),
653                          "shape", SP_CTRL_SHAPE_BITMAP,
654                          "size", 13.0,
655                          NULL);
656             sp_canvas_item_show(_grip);
657             break;
658         default:
659             g_object_set(G_OBJECT(_grip),
660                          "shape", SP_CTRL_SHAPE_CROSS,
661                          "size", 7.0,
662                          NULL);
663             sp_canvas_item_show(_norm);
664             sp_canvas_item_show(_grip);
666             break;
667     }
669     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
673 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
675     if (!SP_KNOT_IS_GRABBED(knot)) {
676         return;
677     }
679     // in case items have been unhooked from the document, don't
680     // try to continue processing events for them.
681     for (unsigned int i = 0; i < _items.size(); i++) {
682         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
683             return;
684         }
685     }
687     handle.action(this, handle, *position, state);
691 bool Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
693     if (!SP_KNOT_IS_GRABBED(knot)) {
694         return TRUE;
695     }
697     knot->desktop->set_coordinate_status(*position);
698     knot->desktop->setPosition(*position);
701     if (state & GDK_MOD1_MASK) {
702         *position = _point + ( *position - _point ) / 10;
703     }
705     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
706         _origin = _opposite;
707     } else {
708         _origin = _center;
709     }
710     if (handle.request(this, handle, *position, state)) {
711         sp_knot_set_position(knot, position, state);
712         SP_CTRL(_grip)->moveto(*position);
713         SP_CTRL(_norm)->moveto(_origin);
714     }
716     return TRUE;
720 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
722     if (!_grabbed) {
723         _updateVolatileState();
724         _center_is_set = false; // center(s) may have changed
725         _updateHandles();
726     }
729 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
731     if (!_grabbed) {
732         _updateVolatileState();
734         // reset internal flag
735         _changed = false;
737         _center_is_set = false;  // center(s) may have changed
739         _updateHandles();
740     }
743 /*
744  * handlers for handle move-request
745  */
747 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
748 static double sign(double const x)
750     return ( x < 0
751              ? -1
752              : 1 );
755 bool sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
756                                     SPSelTransHandle const &, NR::Point &pt, guint state)
758     return seltrans->scaleRequest(pt, state);
761 bool sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
762                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
764     return seltrans->stretchRequest(handle, pt, state);
767 bool sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
768                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
770     return seltrans->skewRequest(handle, pt, state);
773 bool sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
774                                      SPSelTransHandle const &, NR::Point &pt, guint state)
776     return seltrans->rotateRequest(pt, state);
779 bool sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
780                                      SPSelTransHandle const &, NR::Point &pt, guint state)
782     return seltrans->centerRequest(pt, state);
785 bool Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
787     using NR::X;
788     using NR::Y;
790     NR::Point d = _point - _origin;
791     NR::scale s(0, 0);
793     /* Work out the new scale factors `s' */
794     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
795         if ( fabs(d[i]) > 0.001 ) {
796             s[i] = ( pt[i] - _origin[i] ) / d[i];
797             if ( fabs(s[i]) < 1e-9 ) {
798                 s[i] = 1e-9;
799             }
800         }
801     }
803     SnapManager const &m = _desktop->namedview->snap_manager;
805     /* Get a STL list of the selected items.
806     ** FIXME: this should probably be done by Inkscape::Selection.
807     */
808     std::list<SPItem const*> it;
809     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
810         it.push_back(reinterpret_cast<SPItem*>(i->data));
811     }
813     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
814         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
815         ** To do this, we snap along a suitable constraint vector from the origin.
816         */
818         NR::Point const cv = NR::Point(
819             pt[NR::X] > _origin[NR::X] ? 1 : -1,
820             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
821             );
823         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
824                                                                _bbox_points,
825                                                                it,
826                                                                Snapper::ConstraintLine(_origin, cv),
827                                                                s,
828                                                                _origin);
830         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
831                                                                _snap_points,
832                                                                it,
833                                                                Snapper::ConstraintLine(_origin, cv),
834                                                                s,
835                                                                _origin);
837         if (bb.second == false && sn.second == false) {
839             /* We didn't snap, so just lock aspect ratio */
840             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
841                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
842             } else {
843                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
844             }
846         } else {
848             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
849             ** just compare difference in s[X].
850             */
851             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
852             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
853             s = (bd < sd) ? bb.first : sn.first;
854         }
856     } else {
857         /* Scale aspect ratio is unlocked */
858         
859         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
860                                                         _bbox_points,
861                                                         it,
862                                                         s,
863                                                         _origin);
864         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
865                                                         _snap_points,
866                                                         it,
867                                                         s,
868                                                         _origin);
870         /* Pick the snap that puts us closest to the original scale */
871         NR::Coord bd = bb.second ?
872             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
873                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
874             : NR_HUGE;
875         NR::Coord sd = sn.second ?
876             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
877                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
878             : NR_HUGE;
879         s = (bd < sd) ? bb.first : sn.first;
880     }
882     /* Update the knot position */
883     pt = ( _point - _origin ) * s + _origin;
885     /* Status text */
886     _message_context.setF(Inkscape::NORMAL_MESSAGE,
887                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
888                           100 * s[NR::X], 100 * s[NR::Y]);
890     return TRUE;
893 bool Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
895     using NR::X;
896     using NR::Y;
898     NR::Dim2 axis, perp;
900     switch (handle.cursor) {
901         case GDK_TOP_SIDE:
902         case GDK_BOTTOM_SIDE:
903            axis = NR::Y;
904            perp = NR::X;
905            break;
906         case GDK_LEFT_SIDE:
907         case GDK_RIGHT_SIDE:
908            axis = NR::X;
909            perp = NR::Y;
910            break;
911         default:
912             g_assert_not_reached();
913             return TRUE;
914     };
916     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
917         return FALSE;
918     }
920     NR::scale s(1, 1);
921     s[axis] = ( ( pt[axis] - _origin[axis] )
922                 / ( _point[axis] - _origin[axis] ) );
923     if ( fabs(s[axis]) < 1e-15 ) {
924         s[axis] = 1e-15;
925     }
927     /* Get a STL list of the selected items.
928     ** FIXME: this should probably be done by Inkscape::Selection.
929     */
930     std::list<SPItem const*> it;
931     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
932         it.push_back(reinterpret_cast<SPItem*>(i->data));
933     }
935     SnapManager const &m = _desktop->namedview->snap_manager;
937     if ( state & GDK_CONTROL_MASK ) {
938         s[perp] = fabs(s[axis]);
940         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
941             Snapper::BBOX_POINT,
942             _bbox_points,
943             it,
944             s[axis],
945             _origin,
946             axis,
947             true);
949         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
950             Snapper::SNAP_POINT,
951             _snap_points,
952             it,
953             s[axis],
954             _origin,
955             axis,
956             true);
958         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
959         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
960         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
962         s[axis] = fabs(ratio) * sign(s[axis]);
963         s[perp] = fabs(s[axis]);
964     } else {
966         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
967             Snapper::BBOX_POINT,
968             _bbox_points,
969             it,
970             s[axis],
971             _origin,
972             axis,
973             false);
975         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
976             Snapper::SNAP_POINT,
977             _snap_points,
978             it,
979             s[axis],
980             _origin,
981             axis,
982             false);
984         /* Choose the smaller difference in scale */
985         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
986         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
987         s[axis] = (bd < sd) ? bb.first : sn.first;
988         s[perp] = 1;
989     }
991     pt = ( _point - _origin ) * NR::scale(s) + _origin;
992     if (isNaN(pt[X] + pt[Y])) {
993         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
994                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
995     }
997     // status text
998     _message_context.setF(Inkscape::NORMAL_MESSAGE,
999                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1000                           100 * s[NR::X], 100 * s[NR::Y]);
1002     return TRUE;
1005 bool Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1007     using NR::X;
1008     using NR::Y;
1010     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1011         return FALSE;
1012     }
1014     NR::Dim2 dim_a;
1015     NR::Dim2 dim_b;
1016     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1017         dim_a = X;
1018         dim_b = Y;
1019     } else {
1020         dim_a = Y;
1021         dim_b = X;
1022     }
1024     double skew[2];
1025     double s[2] = { 1.0, 1.0 };
1027     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1028         return FALSE;
1029     }
1031     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1033     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1035     if ( fabs(s[dim_a]) < 1 ) {
1036         s[dim_a] = sign(s[dim_a]);
1037     } else {
1038         s[dim_a] = floor( s[dim_a] + 0.5 );
1039     }
1041     double radians = atan(skew[dim_a] / s[dim_a]);
1043     if (state & GDK_CONTROL_MASK) {
1045         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1047         if (snaps) {
1048             double sections = floor( radians * snaps / M_PI + .5 );
1049             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1050             radians = ( M_PI / snaps ) * sections;
1051         }
1052         skew[dim_a] = tan(radians) * s[dim_a];
1053     } else {
1054         SnapManager const &m = _desktop->namedview->snap_manager;
1056         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1057                                                        _bbox_points,
1058                                                        std::list<SPItem const *>(),
1059                                                        skew[dim_a],
1060                                                        _origin,
1061                                                        dim_a);
1063         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1064                                                        _snap_points,
1065                                                        std::list<SPItem const *>(),
1066                                                        skew[dim_a],
1067                                                        _origin,
1068                                                        dim_a);
1069         
1070         if (bb.second || sn.second) {
1071             /* We snapped something, so change the skew to reflect it */
1072             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1073             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1074             skew[dim_a] = std::min(bd, sd);
1075         }
1076     }
1078     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1079     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1081     /* status text */
1082     double degrees = 180 / M_PI * radians;
1083     if (degrees > 180) degrees -= 360;
1084     if (degrees < -180) degrees += 360;
1086     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1087                           // TRANSLATORS: don't modify the first ";"
1088                           // (it will NOT be displayed as ";" - only the second one will be)
1089                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1090                           degrees);
1092     return TRUE;
1095 bool Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1097     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1099     // rotate affine in rotate
1100     NR::Point const d1 = _point - _origin;
1101     NR::Point const d2 = pt     - _origin;
1103     NR::Coord const h1 = NR::L2(d1);
1104     if (h1 < 1e-15) return FALSE;
1105     NR::Point q1 = d1 / h1;
1106     NR::Coord const h2 = NR::L2(d2);
1107     if (fabs(h2) < 1e-15) return FALSE;
1108     NR::Point q2 = d2 / h2;
1110     double radians;
1111     if (state & GDK_CONTROL_MASK) {
1112         /* Have to restrict movement. */
1113         double cos_t = NR::dot(q1, q2);
1114         double sin_t = NR::dot(NR::rot90(q1), q2);
1115         radians = atan2(sin_t, cos_t);
1116         if (snaps) {
1117             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1118         }
1119         q1 = NR::Point(1, 0);
1120         q2 = NR::Point(cos(radians), sin(radians));
1121     } else {
1122         radians = atan2(NR::dot(NR::rot90(d1), d2),
1123                         NR::dot(d1, d2));
1124     }
1126     NR::rotate const r1(q1);
1127     NR::rotate const r2(q2);
1128     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1130     /* status text */
1131     double degrees = 180 / M_PI * radians;
1132     if (degrees > 180) degrees -= 360;
1133     if (degrees < -180) degrees += 360;
1135     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1136                           // TRANSLATORS: don't modify the first ";"
1137                           // (it will NOT be displayed as ";" - only the second one will be)
1138                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1140     return TRUE;
1143 bool Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1145     using NR::X;
1146     using NR::Y;
1148     SnapManager const &m = _desktop->namedview->snap_manager;
1149     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1151     if (state & GDK_CONTROL_MASK) {
1152         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1153             pt[Y] = _point[Y];
1154         } else {
1155             pt[X] = _point[X];
1156         }
1157     }
1159     if (!(state & GDK_SHIFT_MASK)) {
1160         // screen pixels to snap center to bbox
1161 #define SNAP_DIST 5
1162         // FIXME: take from prefs
1163         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1165         for (int i = 0; i < 2; i++) {
1167             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1168                 pt[i] = _box.min()[i];
1169             }
1170             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1171                 pt[i] = _box.midpoint()[i];
1172             }
1173             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1174                 pt[i] = _box.max()[i];
1175             }
1176         }
1177     }
1179     // status text
1180     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1181     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1182     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1183     g_string_free(xs, FALSE);
1184     g_string_free(ys, FALSE);
1186     return TRUE;
1189 /*
1190  * handlers for handle movement
1191  *
1192  */
1194 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1196     seltrans->stretch(handle, pt, state);
1199 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1201     seltrans->scale(pt, state);
1204 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1206     seltrans->skew(handle, pt, state);
1209 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1211     seltrans->rotate(pt, state);
1214 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1216     using NR::X;
1217     using NR::Y;
1219     NR::Dim2 dim;
1220     switch (handle.cursor) {
1221         case GDK_LEFT_SIDE:
1222         case GDK_RIGHT_SIDE:
1223             dim = X;
1224             break;
1225         case GDK_TOP_SIDE:
1226         case GDK_BOTTOM_SIDE:
1227             dim = Y;
1228             break;
1229         default:
1230             g_assert_not_reached();
1231             abort();
1232             break;
1233     }
1235     NR::Point const scale_origin(_origin);
1236     double const offset = _point[dim] - scale_origin[dim];
1237     if (!( fabs(offset) >= 1e-15 )) {
1238         return;
1239     }
1240     NR::scale s(1, 1);
1241     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1242     if (isNaN(s[dim])) {
1243         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1244                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1245     }
1246     if (!( fabs(s[dim]) >= 1e-15 )) {
1247         s[dim] = 1e-15;
1248     }
1249     if (state & GDK_CONTROL_MASK) {
1250         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1251         s[!dim] = fabs(s[dim]);
1252     }
1254     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1255     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1257     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1258     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1259                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1261     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1264 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1266     NR::Point const offset = _point - _origin;
1268     NR::scale s (1, 1);
1269     for (int i = NR::X; i <= NR::Y; i++) {
1270         if (fabs(offset[i]) > 1e-9)
1271             s[i] = (pt[i] - _origin[i]) / offset[i];
1272         if (fabs(s[i]) < 1e-9)
1273             s[i] = 1e-9;
1274     }
1275     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1276     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1278     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1279     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1280                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1282     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1285 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1287     NR::Point const offset = _point - _origin;
1289     unsigned dim;
1290     switch (handle.cursor) {
1291         case GDK_SB_H_DOUBLE_ARROW:
1292             dim = NR::Y;
1293             break;
1294         case GDK_SB_V_DOUBLE_ARROW:
1295             dim = NR::X;
1296             break;
1297         default:
1298             g_assert_not_reached();
1299             abort();
1300             break;
1301     }
1302     if (fabs(offset[dim]) < 1e-15) {
1303         return;
1304     }
1305     NR::Matrix skew = NR::identity();
1306     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1307     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1308     skew[2*(1-dim) + (dim)] = 0;
1309     skew[2*(1-dim) + (1-dim)] = 1;
1311     for (int i = 0; i < 2; i++) {
1312         if (fabs(skew[3*i]) < 1e-15) {
1313             skew[3*i] = 1e-15;
1314         }
1315     }
1316     transform(skew, _origin);
1319 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1321     NR::Point const offset = _point - _origin;
1323     NR::Coord const h1 = NR::L2(offset);
1324     if (h1 < 1e-15) {
1325         return;
1326     }
1327     NR::Point const q1 = offset / h1;
1328     NR::Coord const h2 = NR::L2( pt - _origin );
1329     if (h2 < 1e-15) {
1330         return;
1331     }
1332     NR::Point const q2 = (pt - _origin) / h2;
1333     NR::rotate const r1(q1);
1334     NR::rotate const r2(q2);
1336     NR::Matrix rotate( r2 / r1 );
1337     transform(rotate, _origin);
1340 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1342     seltrans->setCenter(pt);
1346 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1348     SnapManager const &m = _desktop->namedview->snap_manager;
1350     /* The amount that we've moved by during this drag */
1351     NR::Point dxy = xy - _point;
1353     /* Get a STL list of the selected items.
1354     ** FIXME: this should probably be done by Inkscape::Selection.
1355     */
1356     std::list<SPItem const*> it;
1357     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1358         it.push_back(reinterpret_cast<SPItem*>(i->data));
1359     }
1361     bool const alt = (state & GDK_MOD1_MASK);
1362     bool const control = (state & GDK_CONTROL_MASK);
1363     bool const shift = (state & GDK_SHIFT_MASK);
1365     if (alt) {
1367         /* Alt pressed means keep offset: snap the moved distance to the grid.
1368         ** FIXME: this will snap to more than just the grid, nowadays.
1369         */
1371         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1373     } else if (!shift) {
1375         /* We're snapping to things, possibly with a constraint to horizontal or
1376         ** vertical movement.  Obtain a list of possible translations and then
1377         ** pick the smallest.
1378         */
1380         /* This will be our list of possible translations */
1381         std::list<std::pair<NR::Point, bool> > s;
1383         if (control) {
1385             /* Snap to things, and also constrain to horizontal or vertical movement */
1387             for (unsigned int dim = 0; dim < 2; dim++) {
1388                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1389                                                          _bbox_points,
1390                                                          it,
1391                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1392                                                          dxy));
1393                             
1394                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1395                                                          _snap_points,
1396                                                          it,
1397                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1398                                                          dxy));
1399             }
1401         } else {
1403             /* Snap to things with no constraint */
1405             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1406                                               _bbox_points, it, dxy));
1407             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1408                                               _snap_points, it, dxy));
1409         }
1411         /* Pick one */
1412         NR::Coord best = NR_HUGE;
1413         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1414             if (i->second) {
1415                 NR::Coord const m = NR::L2(i->first);
1416                 if (m < best) {
1417                     best = m;
1418                     dxy = i->first;
1419                 }
1420             }
1421         }
1422     }
1424     if (control) {
1425         /* Ensure that the horizontal and vertical constraint has been applied */
1426         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1427             dxy[NR::Y] = 0;
1428         } else {
1429             dxy[NR::X] = 0;
1430         }
1431     }
1433     NR::Matrix const move((NR::translate(dxy)));
1434     NR::Point const norm(0, 0);
1435     transform(move, norm);
1437     // status text
1438     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1439     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1440     _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);
1441     g_string_free(xs, TRUE);
1442     g_string_free(ys, TRUE);
1446 /*
1447   Local Variables:
1448   mode:c++
1449   c-file-style:"stroustrup"
1450   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1451   indent-tabs-mode:nil
1452   fill-column:99
1453   End:
1454 */
1455 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :