Code

remove rudimental general-purpose clipart, add some Inkscape-related graphics, update...
[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();
263     gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
264     bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
265     NR::Rect op_box = _box;
266     if (origin_on_bbox == false && _snap_points.empty() == false) {
267         std::vector<NR::Point>::iterator i = _snap_points.begin();
268         op_box = NR::Rect(*i, *i);
269         i++;
270         while (i != _snap_points.end()) {
271             op_box.expandTo(*i);
272             i++;
273         }
274     }
276     _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
278     if ((x != -1) && (y != -1)) {
279         sp_canvas_item_show(_norm);
280         sp_canvas_item_show(_grip);
281     }
283     if (_show == SHOW_OUTLINE) {
284         for (int i = 0; i < 4; i++)
285             sp_canvas_item_show(_l[i]);
286     }
289     _updateHandles();
290     g_return_if_fail(_stamp_cache == NULL);
293 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
295     g_return_if_fail(_grabbed);
296     g_return_if_fail(!_empty);
298     NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
300     if (_show == SHOW_CONTENT) {
301         // update the content
302         for (unsigned i = 0; i < _items.size(); i++) {
303             SPItem &item = *_items[i].first;
304             NR::Matrix const &prev_transform = _items[i].second;
305             sp_item_set_i2d_affine(&item, prev_transform * affine);
306         }
307     } else {
308         NR::Point p[4];
309         /* update the outline */
310         for (unsigned i = 0 ; i < 4 ; i++) {
311             p[i] = _box.corner(i) * affine;
312         }
313         for (unsigned i = 0 ; i < 4 ; i++) {
314             sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
315         }
316     }
318     _current = affine;
319     _changed = true;
320     _updateHandles();
323 void Inkscape::SelTrans::ungrab()
325     g_return_if_fail(_grabbed);
326     _grabbed = false;
327     _show_handles = true;
329     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
330     _updateVolatileState();
332     for (unsigned i = 0; i < _items.size(); i++) {
333         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
334     }
336     sp_canvas_item_hide(_norm);
337     sp_canvas_item_hide(_grip);
339     if (_show == SHOW_OUTLINE) {
340         for (int i = 0; i < 4; i++)
341             sp_canvas_item_hide(_l[i]);
342     }
344     if (_stamp_cache) {
345         g_slist_free(_stamp_cache);
346         _stamp_cache = NULL;
347     }
349     _message_context.clear();
351     if (!_empty && _changed) {
352         sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
353         _center *= _current;
354         _center_is_set = true;
356 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
357 // appropriately - it does not know the original positions of the centers (all objects already have
358 // the new bboxes). So we need to reset the centers from our saved array.
359         if (_show != SHOW_OUTLINE && !_current.is_translation()) {
360             for (unsigned i = 0; i < _items_centers.size(); i++) {
361                 SPItem *currentItem = _items_centers[i].first;
362                 if (currentItem->isCenterSet()) { // only if it's already set
363                     currentItem->setCenter (_items_centers[i].second * _current);
364                     SP_OBJECT(currentItem)->updateRepr();
365                 }
366             }
367         }
369         _items.clear();
370         _items_centers.clear();
372         if (_current.is_translation()) {
373             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
374                              _("Move"));
375         } else if (_current.is_scale()) {
376             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
377                              _("Scale"));
378         } else if (_current.is_rotation()) {
379             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
380                              _("Rotate"));
381         } else {
382             sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
383                              _("Skew"));
384         }
386     } else {
387         _items.clear();
388         _items_centers.clear();
389         _updateHandles();
390     }
393 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
394 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
396 void Inkscape::SelTrans::stamp()
398     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
400     bool fixup = !_grabbed;
401     if ( fixup && _stamp_cache ) {
402         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
403         g_slist_free(_stamp_cache);
404         _stamp_cache = NULL;
405     }
407     /* stamping mode */
408     if (!_empty) {
409         GSList *l;
410         if (_stamp_cache) {
411             l = _stamp_cache;
412         } else {
413             /* Build cache */
414             l  = g_slist_copy((GSList *) selection->itemList());
415             l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
416             _stamp_cache = l;
417         }
419         while (l) {
420             SPItem *original_item = SP_ITEM(l->data);
421             Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
423             // remember the position of the item
424             gint pos = original_repr->position();
425             // remember parent
426             Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
428             Inkscape::XML::Node *copy_repr = original_repr->duplicate();
430             // add the new repr to the parent
431             parent->appendChild(copy_repr);
432             // move to the saved position
433             copy_repr->setPosition(pos > 0 ? pos : 0);
435             SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
437             NR::Matrix const *new_affine;
438             if (_show == SHOW_OUTLINE) {
439                 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
440                 NR::Matrix const i2dnew( i2d * _current );
441                 sp_item_set_i2d_affine(copy_item, i2dnew);
442                 new_affine = &copy_item->transform;
443             } else {
444                 new_affine = &original_item->transform;
445             }
447             sp_item_write_transform(copy_item, copy_repr, *new_affine);
449             if (copy_item->isCenterSet()) {
450                 copy_item->setCenter(_center * _current);
451             }
453             Inkscape::GC::release(copy_repr);
454             l = l->next;
455         }
456         sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
457                          _("Stamp"));
458     }
460     if ( fixup && _stamp_cache ) {
461         // TODO - give a proper fix. Simple temproary work-around for the grab() issue
462         g_slist_free(_stamp_cache);
463         _stamp_cache = NULL;
464     }
467 void Inkscape::SelTrans::_updateHandles()
469     if ( !_show_handles || _empty )
470     {
471         sp_remove_handles(_shandle, 8);
472         sp_remove_handles(_rhandle, 8);
473         sp_remove_handles(&_chandle, 1);
474         return;
475     }
477     // center handle
478     if ( _chandle == NULL ) {
479         _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
481         _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
482         _chandle->setSize (13);
483         _chandle->setAnchor (handle_center.anchor);
484         _chandle->setMode (SP_CTRL_MODE_XOR);
485         _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
486         _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
487         _chandle->setPixbuf(handles[handle_center.control]);
488         sp_knot_update_ctrl(_chandle);
490         g_signal_connect(G_OBJECT(_chandle), "request",
491                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
492         g_signal_connect(G_OBJECT(_chandle), "moved",
493                          G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
494         g_signal_connect(G_OBJECT(_chandle), "grabbed",
495                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
496         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
497                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
498         g_signal_connect(G_OBJECT(_chandle), "clicked",
499                          G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
500     }
502     sp_remove_handles(&_chandle, 1);
503     if ( _state == STATE_SCALE ) {
504         sp_remove_handles(_rhandle, 8);
505         _showHandles(_shandle, handles_scale, 8,
506                     _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
507                     _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
508     } else {
509         sp_remove_handles(_shandle, 8);
510         _showHandles(_rhandle, handles_rotate, 8,
511                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
512                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
513     }
515     if (!_center_is_set) {
516         _center = _desktop->selection->center();
517         _center_is_set = true;
518     }
520     if ( _state == STATE_SCALE ) {
521         sp_knot_hide(_chandle);
522     } else {
523         sp_knot_show(_chandle);
524         sp_knot_moveto(_chandle, &_center);
525     }
528 void Inkscape::SelTrans::_updateVolatileState()
530     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
531     _empty = selection->isEmpty();
533     if (_empty) {
534         return;
535     }
537     _box = selection->bounds();
538     if (_box.isEmpty()) {
539         _empty = true;
540         return;
541     }
543     _strokewidth = stroke_average_width (selection->itemList());
546 static void sp_remove_handles(SPKnot *knot[], gint num)
548     for (int i = 0; i < num; i++) {
549         if (knot[i] != NULL) {
550             sp_knot_hide(knot[i]);
551         }
552     }
555 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
556                              gchar const *even_tip, gchar const *odd_tip)
558     g_return_if_fail( !_empty );
560     for (int i = 0; i < num; i++) {
561         if (knot[i] == NULL) {
562             knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
564             knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
565             knot[i]->setSize (13);
566             knot[i]->setAnchor (handle[i].anchor);
567             knot[i]->setMode (SP_CTRL_MODE_XOR);
568             knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
569             knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
570             knot[i]->setPixbuf(handles[handle[i].control]);
571             sp_knot_update_ctrl(knot[i]);
573             g_signal_connect(G_OBJECT(knot[i]), "request",
574                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
575             g_signal_connect(G_OBJECT(knot[i]), "moved",
576                              G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
577             g_signal_connect(G_OBJECT(knot[i]), "grabbed",
578                              G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
579             g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
580                              G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
581             g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
582         }
583         sp_knot_show(knot[i]);
585         NR::Point const handle_pt(handle[i].x, handle[i].y);
586         NR::Point p( _box.min()
587                      + ( _box.dimensions()
588                          * NR::scale(handle_pt) ) );
590         sp_knot_moveto(knot[i], &p);
591     }
594 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
596     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
597         knot, state, *(SPSelTransHandle const *) data
598         );
601 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
603     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
606 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
608     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
609         knot, position, state, *(SPSelTransHandle const *) data
610         );
613 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
615     return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
616         knot, position, state, *(SPSelTransHandle const *) data
617         );
620 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
622     SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
623         knot, state, *(SPSelTransHandle const *) data
624         );
627 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
629     switch (handle.anchor) {
630         case GTK_ANCHOR_CENTER:
631             if (state & GDK_SHIFT_MASK) {
632                 // Unset the  center position for all selected items
633                 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
634                     SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
635                     it->unsetCenter();
636                     SP_OBJECT(it)->updateRepr();
637                     _center_is_set = false;  // center has changed
638                     _updateHandles();
639                 }
640                 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT, 
641                                         _("Reset center"));
642             }
643             break;
644         default:
645             break;
646     }
649 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
651     switch (handle.anchor) {
652         case GTK_ANCHOR_CENTER:
653             g_object_set(G_OBJECT(_grip),
654                          "shape", SP_CTRL_SHAPE_BITMAP,
655                          "size", 13.0,
656                          NULL);
657             sp_canvas_item_show(_grip);
658             break;
659         default:
660             g_object_set(G_OBJECT(_grip),
661                          "shape", SP_CTRL_SHAPE_CROSS,
662                          "size", 7.0,
663                          NULL);
664             sp_canvas_item_show(_norm);
665             sp_canvas_item_show(_grip);
667             break;
668     }
670     grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
674 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
676     if (!SP_KNOT_IS_GRABBED(knot)) {
677         return;
678     }
680     // in case items have been unhooked from the document, don't
681     // try to continue processing events for them.
682     for (unsigned int i = 0; i < _items.size(); i++) {
683         if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
684             return;
685         }
686     }
688     handle.action(this, handle, *position, state);
692 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
694     if (!SP_KNOT_IS_GRABBED(knot)) {
695         return TRUE;
696     }
698     knot->desktop->set_coordinate_status(*position);
699     knot->desktop->setPosition(*position);
702     if (state & GDK_MOD1_MASK) {
703         *position = _point + ( *position - _point ) / 10;
704     }
706     if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
707         _origin = _opposite;
708     } else {
709         _origin = _center;
710     }
711     if (handle.request(this, handle, *position, state)) {
712         sp_knot_set_position(knot, position, state);
713         SP_CTRL(_grip)->moveto(*position);
714         SP_CTRL(_norm)->moveto(_origin);
715     }
717     return TRUE;
721 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
723     if (!_grabbed) {
724         _updateVolatileState();
725         _current.set_identity();
726         _center_is_set = false; // center(s) may have changed
727         _updateHandles();
728     }
731 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
733     if (!_grabbed) {
734         _updateVolatileState();
735         _current.set_identity();
737         // reset internal flag
738         _changed = false;
740         _center_is_set = false;  // center(s) may have changed
742         _updateHandles();
743     }
746 /*
747  * handlers for handle move-request
748  */
750 /** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
751 static double sign(double const x)
753     return ( x < 0
754              ? -1
755              : 1 );
758 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
759                                     SPSelTransHandle const &, NR::Point &pt, guint state)
761     return seltrans->scaleRequest(pt, state);
764 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
765                                       SPSelTransHandle const &handle, NR::Point &pt, guint state)
767     return seltrans->stretchRequest(handle, pt, state);
770 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
771                                    SPSelTransHandle const &handle, NR::Point &pt, guint state)
773     return seltrans->skewRequest(handle, pt, state);
776 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
777                                      SPSelTransHandle const &, NR::Point &pt, guint state)
779     return seltrans->rotateRequest(pt, state);
782 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
783                                      SPSelTransHandle const &, NR::Point &pt, guint state)
785     return seltrans->centerRequest(pt, state);
788 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
790     using NR::X;
791     using NR::Y;
793     NR::Point d = _point - _origin;
794     NR::scale s(0, 0);
796     /* Work out the new scale factors `s' */
797     for ( unsigned int i = 0 ; i < 2 ; i++ ) {
798         if ( fabs(d[i]) > 0.001 ) {
799             s[i] = ( pt[i] - _origin[i] ) / d[i];
800             if ( fabs(s[i]) < 1e-9 ) {
801                 s[i] = 1e-9;
802             }
803         }
804     }
806     SnapManager const &m = _desktop->namedview->snap_manager;
808     /* Get a STL list of the selected items.
809     ** FIXME: this should probably be done by Inkscape::Selection.
810     */
811     std::list<SPItem const*> it;
812     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
813         it.push_back(reinterpret_cast<SPItem*>(i->data));
814     }
816     if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
817         /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
818         ** To do this, we snap along a suitable constraint vector from the origin.
819         */
821         NR::Point const cv = NR::Point(
822             pt[NR::X] > _origin[NR::X] ? 1 : -1,
823             pt[NR::Y] > _origin[NR::Y] ? 1 : -1
824             );
826         std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
827                                                                _bbox_points,
828                                                                it,
829                                                                Snapper::ConstraintLine(_origin, cv),
830                                                                s,
831                                                                _origin);
833         std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
834                                                                _snap_points,
835                                                                it,
836                                                                Snapper::ConstraintLine(_origin, cv),
837                                                                s,
838                                                                _origin);
840         if (bb.second == false && sn.second == false) {
842             /* We didn't snap, so just lock aspect ratio */
843             if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
844                 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
845             } else {
846                 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
847             }
849         } else {
851             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
852             ** just compare difference in s[X].
853             */
854             double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
855             double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
856             s = (bd < sd) ? bb.first : sn.first;
857         }
859     } else {
860         /* Scale aspect ratio is unlocked */
861         
862         std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
863                                                         _bbox_points,
864                                                         it,
865                                                         s,
866                                                         _origin);
867         std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
868                                                         _snap_points,
869                                                         it,
870                                                         s,
871                                                         _origin);
873         /* Pick the snap that puts us closest to the original scale */
874         NR::Coord bd = bb.second ?
875             fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
876                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
877             : NR_HUGE;
878         NR::Coord sd = sn.second ?
879             fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
880                  NR::L2(NR::Point(s[NR::X], s[NR::Y])))
881             : NR_HUGE;
882         s = (bd < sd) ? bb.first : sn.first;
883     }
885     /* Update the knot position */
886     pt = ( _point - _origin ) * s + _origin;
888     /* Status text */
889     _message_context.setF(Inkscape::NORMAL_MESSAGE,
890                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
891                           100 * s[NR::X], 100 * s[NR::Y]);
893     return TRUE;
896 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
898     using NR::X;
899     using NR::Y;
901     NR::Dim2 axis, perp;
903     switch (handle.cursor) {
904         case GDK_TOP_SIDE:
905         case GDK_BOTTOM_SIDE:
906            axis = NR::Y;
907            perp = NR::X;
908            break;
909         case GDK_LEFT_SIDE:
910         case GDK_RIGHT_SIDE:
911            axis = NR::X;
912            perp = NR::Y;
913            break;
914         default:
915             g_assert_not_reached();
916             return TRUE;
917     };
919     if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
920         return FALSE;
921     }
923     NR::scale s(1, 1);
924     s[axis] = ( ( pt[axis] - _origin[axis] )
925                 / ( _point[axis] - _origin[axis] ) );
926     if ( fabs(s[axis]) < 1e-15 ) {
927         s[axis] = 1e-15;
928     }
930     /* Get a STL list of the selected items.
931     ** FIXME: this should probably be done by Inkscape::Selection.
932     */
933     std::list<SPItem const*> it;
934     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
935         it.push_back(reinterpret_cast<SPItem*>(i->data));
936     }
938     SnapManager const &m = _desktop->namedview->snap_manager;
940     if ( state & GDK_CONTROL_MASK ) {
941         s[perp] = fabs(s[axis]);
943         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
944             Snapper::BBOX_POINT,
945             _bbox_points,
946             it,
947             s[axis],
948             _origin,
949             axis,
950             true);
952         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
953             Snapper::SNAP_POINT,
954             _snap_points,
955             it,
956             s[axis],
957             _origin,
958             axis,
959             true);
961         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
962         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
963         NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
965         s[axis] = fabs(ratio) * sign(s[axis]);
966         s[perp] = fabs(s[axis]);
967     } else {
969         std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
970             Snapper::BBOX_POINT,
971             _bbox_points,
972             it,
973             s[axis],
974             _origin,
975             axis,
976             false);
978         std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
979             Snapper::SNAP_POINT,
980             _snap_points,
981             it,
982             s[axis],
983             _origin,
984             axis,
985             false);
987         /* Choose the smaller difference in scale */
988         NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
989         NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
990         s[axis] = (bd < sd) ? bb.first : sn.first;
991         s[perp] = 1;
992     }
994     pt = ( _point - _origin ) * NR::scale(s) + _origin;
995     if (isNaN(pt[X] + pt[Y])) {
996         g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
997                   _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
998     }
1000     // status text
1001     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1002                           _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1003                           100 * s[NR::X], 100 * s[NR::Y]);
1005     return TRUE;
1008 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1010     using NR::X;
1011     using NR::Y;
1013     if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1014         return FALSE;
1015     }
1017     NR::Dim2 dim_a;
1018     NR::Dim2 dim_b;
1019     if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1020         dim_a = X;
1021         dim_b = Y;
1022     } else {
1023         dim_a = Y;
1024         dim_b = X;
1025     }
1027     double skew[2];
1028     double s[2] = { 1.0, 1.0 };
1030     if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1031         return FALSE;
1032     }
1034     skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1036     s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1038     if ( fabs(s[dim_a]) < 1 ) {
1039         s[dim_a] = sign(s[dim_a]);
1040     } else {
1041         s[dim_a] = floor( s[dim_a] + 0.5 );
1042     }
1044     double radians = atan(skew[dim_a] / s[dim_a]);
1046     if (state & GDK_CONTROL_MASK) {
1048         int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1050         if (snaps) {
1051             double sections = floor( radians * snaps / M_PI + .5 );
1052             if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1053             radians = ( M_PI / snaps ) * sections;
1054         }
1055         skew[dim_a] = tan(radians) * s[dim_a];
1056     } else {
1057         SnapManager const &m = _desktop->namedview->snap_manager;
1059         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1060                                                        _bbox_points,
1061                                                        std::list<SPItem const *>(),
1062                                                        skew[dim_a],
1063                                                        _origin,
1064                                                        dim_a);
1066         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1067                                                        _snap_points,
1068                                                        std::list<SPItem const *>(),
1069                                                        skew[dim_a],
1070                                                        _origin,
1071                                                        dim_a);
1072         
1073         if (bb.second || sn.second) {
1074             /* We snapped something, so change the skew to reflect it */
1075             NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1076             NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1077             skew[dim_a] = std::min(bd, sd);
1078         }
1079     }
1081     pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1082     pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1084     /* status text */
1085     double degrees = 180 / M_PI * radians;
1086     if (degrees > 180) degrees -= 360;
1087     if (degrees < -180) degrees += 360;
1089     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1090                           // TRANSLATORS: don't modify the first ";"
1091                           // (it will NOT be displayed as ";" - only the second one will be)
1092                           _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
1093                           degrees);
1095     return TRUE;
1098 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1100     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1102     // rotate affine in rotate
1103     NR::Point const d1 = _point - _origin;
1104     NR::Point const d2 = pt     - _origin;
1106     NR::Coord const h1 = NR::L2(d1);
1107     if (h1 < 1e-15) return FALSE;
1108     NR::Point q1 = d1 / h1;
1109     NR::Coord const h2 = NR::L2(d2);
1110     if (fabs(h2) < 1e-15) return FALSE;
1111     NR::Point q2 = d2 / h2;
1113     double radians;
1114     if (state & GDK_CONTROL_MASK) {
1115         /* Have to restrict movement. */
1116         double cos_t = NR::dot(q1, q2);
1117         double sin_t = NR::dot(NR::rot90(q1), q2);
1118         radians = atan2(sin_t, cos_t);
1119         if (snaps) {
1120             radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1121         }
1122         q1 = NR::Point(1, 0);
1123         q2 = NR::Point(cos(radians), sin(radians));
1124     } else {
1125         radians = atan2(NR::dot(NR::rot90(d1), d2),
1126                         NR::dot(d1, d2));
1127     }
1129     NR::rotate const r1(q1);
1130     NR::rotate const r2(q2);
1131     pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1133     /* status text */
1134     double degrees = 180 / M_PI * radians;
1135     if (degrees > 180) degrees -= 360;
1136     if (degrees < -180) degrees += 360;
1138     _message_context.setF(Inkscape::NORMAL_MESSAGE,
1139                           // TRANSLATORS: don't modify the first ";"
1140                           // (it will NOT be displayed as ";" - only the second one will be)
1141                           _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
1143     return TRUE;
1146 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1148     using NR::X;
1149     using NR::Y;
1151     SnapManager const &m = _desktop->namedview->snap_manager;
1152     pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1154     if (state & GDK_CONTROL_MASK) {
1155         if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1156             pt[Y] = _point[Y];
1157         } else {
1158             pt[X] = _point[X];
1159         }
1160     }
1162     if (!(state & GDK_SHIFT_MASK)) {
1163         // screen pixels to snap center to bbox
1164 #define SNAP_DIST 5
1165         // FIXME: take from prefs
1166         double snap_dist = SNAP_DIST / _desktop->current_zoom();
1168         for (int i = 0; i < 2; i++) {
1170             if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1171                 pt[i] = _box.min()[i];
1172             }
1173             if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1174                 pt[i] = _box.midpoint()[i];
1175             }
1176             if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1177                 pt[i] = _box.max()[i];
1178             }
1179         }
1180     }
1182     // status text
1183     GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1184     GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1185     _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1186     g_string_free(xs, FALSE);
1187     g_string_free(ys, FALSE);
1189     return TRUE;
1192 /*
1193  * handlers for handle movement
1194  *
1195  */
1197 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1199     seltrans->stretch(handle, pt, state);
1202 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1204     seltrans->scale(pt, state);
1207 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1209     seltrans->skew(handle, pt, state);
1212 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1214     seltrans->rotate(pt, state);
1217 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1219     using NR::X;
1220     using NR::Y;
1222     NR::Dim2 dim;
1223     switch (handle.cursor) {
1224         case GDK_LEFT_SIDE:
1225         case GDK_RIGHT_SIDE:
1226             dim = X;
1227             break;
1228         case GDK_TOP_SIDE:
1229         case GDK_BOTTOM_SIDE:
1230             dim = Y;
1231             break;
1232         default:
1233             g_assert_not_reached();
1234             abort();
1235             break;
1236     }
1238     NR::Point const scale_origin(_origin);
1239     double const offset = _point[dim] - scale_origin[dim];
1240     if (!( fabs(offset) >= 1e-15 )) {
1241         return;
1242     }
1243     NR::scale s(1, 1);
1244     s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1245     if (isNaN(s[dim])) {
1246         g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1247                   s[dim], pt[dim], scale_origin[dim], _point[dim]);
1248     }
1249     if (!( fabs(s[dim]) >= 1e-15 )) {
1250         s[dim] = 1e-15;
1251     }
1252     if (state & GDK_CONTROL_MASK) {
1253         /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1254         s[!dim] = fabs(s[dim]);
1255     }
1257     NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1258     NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1260     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1261     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1262                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1264     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1267 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1269     NR::Point const offset = _point - _origin;
1271     NR::scale s (1, 1);
1272     for (int i = NR::X; i <= NR::Y; i++) {
1273         if (fabs(offset[i]) > 1e-9)
1274             s[i] = (pt[i] - _origin[i]) / offset[i];
1275         if (fabs(s[i]) < 1e-9)
1276             s[i] = 1e-9;
1277     }
1278     NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1279     NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1281     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1282     NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1283                    new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1285     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1288 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1290     NR::Point const offset = _point - _origin;
1292     unsigned dim;
1293     switch (handle.cursor) {
1294         case GDK_SB_H_DOUBLE_ARROW:
1295             dim = NR::Y;
1296             break;
1297         case GDK_SB_V_DOUBLE_ARROW:
1298             dim = NR::X;
1299             break;
1300         default:
1301             g_assert_not_reached();
1302             abort();
1303             break;
1304     }
1305     if (fabs(offset[dim]) < 1e-15) {
1306         return;
1307     }
1308     NR::Matrix skew = NR::identity();
1309     skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1310     skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1311     skew[2*(1-dim) + (dim)] = 0;
1312     skew[2*(1-dim) + (1-dim)] = 1;
1314     for (int i = 0; i < 2; i++) {
1315         if (fabs(skew[3*i]) < 1e-15) {
1316             skew[3*i] = 1e-15;
1317         }
1318     }
1319     transform(skew, _origin);
1322 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1324     NR::Point const offset = _point - _origin;
1326     NR::Coord const h1 = NR::L2(offset);
1327     if (h1 < 1e-15) {
1328         return;
1329     }
1330     NR::Point const q1 = offset / h1;
1331     NR::Coord const h2 = NR::L2( pt - _origin );
1332     if (h2 < 1e-15) {
1333         return;
1334     }
1335     NR::Point const q2 = (pt - _origin) / h2;
1336     NR::rotate const r1(q1);
1337     NR::rotate const r2(q2);
1339     NR::Matrix rotate( r2 / r1 );
1340     transform(rotate, _origin);
1343 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1345     seltrans->setCenter(pt);
1349 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1351     SnapManager const &m = _desktop->namedview->snap_manager;
1353     /* The amount that we've moved by during this drag */
1354     NR::Point dxy = xy - _point;
1356     /* Get a STL list of the selected items.
1357     ** FIXME: this should probably be done by Inkscape::Selection.
1358     */
1359     std::list<SPItem const*> it;
1360     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1361         it.push_back(reinterpret_cast<SPItem*>(i->data));
1362     }
1364     bool const alt = (state & GDK_MOD1_MASK);
1365     bool const control = (state & GDK_CONTROL_MASK);
1366     bool const shift = (state & GDK_SHIFT_MASK);
1368     if (alt) {
1370         /* Alt pressed means keep offset: snap the moved distance to the grid.
1371         ** FIXME: this will snap to more than just the grid, nowadays.
1372         */
1374         dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1376     } else if (!shift) {
1378         /* We're snapping to things, possibly with a constraint to horizontal or
1379         ** vertical movement.  Obtain a list of possible translations and then
1380         ** pick the smallest.
1381         */
1383         /* This will be our list of possible translations */
1384         std::list<std::pair<NR::Point, bool> > s;
1386         if (control) {
1388             /* Snap to things, and also constrain to horizontal or vertical movement */
1390             for (unsigned int dim = 0; dim < 2; dim++) {
1391                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1392                                                          _bbox_points,
1393                                                          it,
1394                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1395                                                          dxy));
1396                             
1397                 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1398                                                          _snap_points,
1399                                                          it,
1400                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1401                                                          dxy));
1402             }
1404         } else {
1406             /* Snap to things with no constraint */
1408             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1409                                               _bbox_points, it, dxy));
1410             s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1411                                               _snap_points, it, dxy));
1412         }
1414         /* Pick one */
1415         NR::Coord best = NR_HUGE;
1416         for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1417             if (i->second) {
1418                 NR::Coord const m = NR::L2(i->first);
1419                 if (m < best) {
1420                     best = m;
1421                     dxy = i->first;
1422                 }
1423             }
1424         }
1425     }
1427     if (control) {
1428         /* Ensure that the horizontal and vertical constraint has been applied */
1429         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1430             dxy[NR::Y] = 0;
1431         } else {
1432             dxy[NR::X] = 0;
1433         }
1434     }
1436     NR::Matrix const move((NR::translate(dxy)));
1437     NR::Point const norm(0, 0);
1438     transform(move, norm);
1440     // status text
1441     GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1442     GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1443     _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);
1444     g_string_free(xs, TRUE);
1445     g_string_free(ys, TRUE);
1449 /*
1450   Local Variables:
1451   mode:c++
1452   c-file-style:"stroustrup"
1453   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1454   indent-tabs-mode:nil
1455   fill-column:99
1456   End:
1457 */
1458 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :