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 );
153 }
155 Inkscape::SelTrans::~SelTrans()
156 {
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();
196 }
198 void Inkscape::SelTrans::resetState()
199 {
200 _state = STATE_SCALE;
201 }
203 void Inkscape::SelTrans::increaseState()
204 {
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();
214 }
216 void Inkscape::SelTrans::setCenter(NR::Point const &p)
217 {
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();
231 }
233 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
234 {
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);
291 }
293 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
294 {
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();
321 }
323 void Inkscape::SelTrans::ungrab()
324 {
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 bool updh = true;
352 if (!_empty && _changed) {
353 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
354 _center *= _current;
355 _center_is_set = true;
357 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
358 // appropriately - it does not know the original positions of the centers (all objects already have
359 // the new bboxes). So we need to reset the centers from our saved array.
360 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
361 for (unsigned i = 0; i < _items_centers.size(); i++) {
362 SPItem *currentItem = _items_centers[i].first;
363 if (currentItem->isCenterSet()) { // only if it's already set
364 currentItem->setCenter (_items_centers[i].second * _current);
365 SP_OBJECT(currentItem)->updateRepr();
366 }
367 }
368 }
370 if (_current.is_translation()) {
371 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
372 _("Move"));
373 } else if (_current.is_scale()) {
374 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
375 _("Scale"));
376 } else if (_current.is_rotation()) {
377 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
378 _("Rotate"));
379 } else {
380 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
381 _("Skew"));
382 }
384 updh = false;
385 }
387 if (updh) {
388 _updateHandles();
389 }
391 _items.clear();
392 _items_centers.clear();
393 }
395 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
396 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
398 void Inkscape::SelTrans::stamp()
399 {
400 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
402 bool fixup = !_grabbed;
403 if ( fixup && _stamp_cache ) {
404 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
405 g_slist_free(_stamp_cache);
406 _stamp_cache = NULL;
407 }
409 /* stamping mode */
410 if (!_empty) {
411 GSList *l;
412 if (_stamp_cache) {
413 l = _stamp_cache;
414 } else {
415 /* Build cache */
416 l = g_slist_copy((GSList *) selection->itemList());
417 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
418 _stamp_cache = l;
419 }
421 while (l) {
422 SPItem *original_item = SP_ITEM(l->data);
423 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
425 // remember the position of the item
426 gint pos = original_repr->position();
427 // remember parent
428 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
430 Inkscape::XML::Node *copy_repr = original_repr->duplicate();
432 // add the new repr to the parent
433 parent->appendChild(copy_repr);
434 // move to the saved position
435 copy_repr->setPosition(pos > 0 ? pos : 0);
437 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
439 NR::Matrix const *new_affine;
440 if (_show == SHOW_OUTLINE) {
441 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
442 NR::Matrix const i2dnew( i2d * _current );
443 sp_item_set_i2d_affine(copy_item, i2dnew);
444 new_affine = ©_item->transform;
445 } else {
446 new_affine = &original_item->transform;
447 }
449 sp_item_write_transform(copy_item, copy_repr, *new_affine);
451 if (copy_item->isCenterSet()) {
452 copy_item->setCenter(_center * _current);
453 }
455 Inkscape::GC::release(copy_repr);
456 l = l->next;
457 }
458 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
459 _("Stamp"));
460 }
462 if ( fixup && _stamp_cache ) {
463 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
464 g_slist_free(_stamp_cache);
465 _stamp_cache = NULL;
466 }
467 }
469 void Inkscape::SelTrans::_updateHandles()
470 {
471 if ( !_show_handles || _empty )
472 {
473 sp_remove_handles(_shandle, 8);
474 sp_remove_handles(_rhandle, 8);
475 sp_remove_handles(&_chandle, 1);
476 return;
477 }
479 // center handle
480 if ( _chandle == NULL ) {
481 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
483 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
484 _chandle->setSize (13);
485 _chandle->setAnchor (handle_center.anchor);
486 _chandle->setMode (SP_CTRL_MODE_XOR);
487 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
488 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
489 _chandle->setPixbuf(handles[handle_center.control]);
490 sp_knot_update_ctrl(_chandle);
492 g_signal_connect(G_OBJECT(_chandle), "request",
493 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
494 g_signal_connect(G_OBJECT(_chandle), "moved",
495 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
496 g_signal_connect(G_OBJECT(_chandle), "grabbed",
497 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
498 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
499 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
500 g_signal_connect(G_OBJECT(_chandle), "clicked",
501 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
502 }
504 sp_remove_handles(&_chandle, 1);
505 if ( _state == STATE_SCALE ) {
506 sp_remove_handles(_rhandle, 8);
507 _showHandles(_shandle, handles_scale, 8,
508 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
509 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
510 } else {
511 sp_remove_handles(_shandle, 8);
512 _showHandles(_rhandle, handles_rotate, 8,
513 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
514 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
515 }
517 if (!_center_is_set) {
518 _center = _desktop->selection->center();
519 _center_is_set = true;
520 }
522 if ( _state == STATE_SCALE ) {
523 sp_knot_hide(_chandle);
524 } else {
525 sp_knot_show(_chandle);
526 sp_knot_moveto(_chandle, &_center);
527 }
528 }
530 void Inkscape::SelTrans::_updateVolatileState()
531 {
532 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
533 _empty = selection->isEmpty();
535 if (_empty) {
536 return;
537 }
539 _box = selection->bounds();
540 if (_box.isEmpty()) {
541 _empty = true;
542 return;
543 }
545 _strokewidth = stroke_average_width (selection->itemList());
546 }
548 static void sp_remove_handles(SPKnot *knot[], gint num)
549 {
550 for (int i = 0; i < num; i++) {
551 if (knot[i] != NULL) {
552 sp_knot_hide(knot[i]);
553 }
554 }
555 }
557 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
558 gchar const *even_tip, gchar const *odd_tip)
559 {
560 g_return_if_fail( !_empty );
562 for (int i = 0; i < num; i++) {
563 if (knot[i] == NULL) {
564 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
566 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
567 knot[i]->setSize (13);
568 knot[i]->setAnchor (handle[i].anchor);
569 knot[i]->setMode (SP_CTRL_MODE_XOR);
570 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
571 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
572 knot[i]->setPixbuf(handles[handle[i].control]);
573 sp_knot_update_ctrl(knot[i]);
575 g_signal_connect(G_OBJECT(knot[i]), "request",
576 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
577 g_signal_connect(G_OBJECT(knot[i]), "moved",
578 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
579 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
580 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
581 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
582 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
583 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
584 }
585 sp_knot_show(knot[i]);
587 NR::Point const handle_pt(handle[i].x, handle[i].y);
588 NR::Point p( _box.min()
589 + ( _box.dimensions()
590 * NR::scale(handle_pt) ) );
592 sp_knot_moveto(knot[i], &p);
593 }
594 }
596 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
597 {
598 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
599 knot, state, *(SPSelTransHandle const *) data
600 );
601 }
603 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
604 {
605 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
606 }
608 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
609 {
610 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
611 knot, position, state, *(SPSelTransHandle const *) data
612 );
613 }
615 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
616 {
617 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
618 knot, position, state, *(SPSelTransHandle const *) data
619 );
620 }
622 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
623 {
624 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
625 knot, state, *(SPSelTransHandle const *) data
626 );
627 }
629 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
630 {
631 switch (handle.anchor) {
632 case GTK_ANCHOR_CENTER:
633 if (state & GDK_SHIFT_MASK) {
634 // Unset the center position for all selected items
635 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
636 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
637 it->unsetCenter();
638 SP_OBJECT(it)->updateRepr();
639 _center_is_set = false; // center has changed
640 _updateHandles();
641 }
642 sp_document_maybe_done (sp_desktop_document(_desktop), "center::unset", SP_VERB_CONTEXT_SELECT,
643 _("Reset center"));
644 }
645 break;
646 default:
647 break;
648 }
649 }
651 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
652 {
653 switch (handle.anchor) {
654 case GTK_ANCHOR_CENTER:
655 g_object_set(G_OBJECT(_grip),
656 "shape", SP_CTRL_SHAPE_BITMAP,
657 "size", 13.0,
658 NULL);
659 sp_canvas_item_show(_grip);
660 break;
661 default:
662 g_object_set(G_OBJECT(_grip),
663 "shape", SP_CTRL_SHAPE_CROSS,
664 "size", 7.0,
665 NULL);
666 sp_canvas_item_show(_norm);
667 sp_canvas_item_show(_grip);
669 break;
670 }
672 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
673 }
676 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
677 {
678 if (!SP_KNOT_IS_GRABBED(knot)) {
679 return;
680 }
682 // in case items have been unhooked from the document, don't
683 // try to continue processing events for them.
684 for (unsigned int i = 0; i < _items.size(); i++) {
685 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
686 return;
687 }
688 }
690 handle.action(this, handle, *position, state);
691 }
694 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
695 {
696 if (!SP_KNOT_IS_GRABBED(knot)) {
697 return TRUE;
698 }
700 knot->desktop->set_coordinate_status(*position);
701 knot->desktop->setPosition(*position);
704 if (state & GDK_MOD1_MASK) {
705 *position = _point + ( *position - _point ) / 10;
706 }
708 if (!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) {
709 _origin = _opposite;
710 } else {
711 _origin = _center;
712 }
713 if (handle.request(this, handle, *position, state)) {
714 sp_knot_set_position(knot, position, state);
715 SP_CTRL(_grip)->moveto(*position);
716 SP_CTRL(_norm)->moveto(_origin);
717 }
719 return TRUE;
720 }
723 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
724 {
725 if (!_grabbed) {
726 _updateVolatileState();
727 _current.set_identity();
728 _center_is_set = false; // center(s) may have changed
729 _updateHandles();
730 }
731 }
733 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
734 {
735 if (!_grabbed) {
736 _updateVolatileState();
737 _current.set_identity();
739 // reset internal flag
740 _changed = false;
742 _center_is_set = false; // center(s) may have changed
744 _updateHandles();
745 }
746 }
748 /*
749 * handlers for handle move-request
750 */
752 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
753 static double sign(double const x)
754 {
755 return ( x < 0
756 ? -1
757 : 1 );
758 }
760 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
761 SPSelTransHandle const &, NR::Point &pt, guint state)
762 {
763 return seltrans->scaleRequest(pt, state);
764 }
766 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
767 SPSelTransHandle const &handle, NR::Point &pt, guint state)
768 {
769 return seltrans->stretchRequest(handle, pt, state);
770 }
772 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
773 SPSelTransHandle const &handle, NR::Point &pt, guint state)
774 {
775 return seltrans->skewRequest(handle, pt, state);
776 }
778 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
779 SPSelTransHandle const &, NR::Point &pt, guint state)
780 {
781 return seltrans->rotateRequest(pt, state);
782 }
784 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
785 SPSelTransHandle const &, NR::Point &pt, guint state)
786 {
787 return seltrans->centerRequest(pt, state);
788 }
790 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
791 {
792 using NR::X;
793 using NR::Y;
795 NR::Point d = _point - _origin;
796 NR::scale s(0, 0);
798 /* Work out the new scale factors `s' */
799 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
800 if ( fabs(d[i]) > 0.001 ) {
801 s[i] = ( pt[i] - _origin[i] ) / d[i];
802 if ( fabs(s[i]) < 1e-9 ) {
803 s[i] = 1e-9;
804 }
805 }
806 }
808 SnapManager const &m = _desktop->namedview->snap_manager;
810 /* Get a STL list of the selected items.
811 ** FIXME: this should probably be done by Inkscape::Selection.
812 */
813 std::list<SPItem const*> it;
814 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
815 it.push_back(reinterpret_cast<SPItem*>(i->data));
816 }
818 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
819 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
820 ** To do this, we snap along a suitable constraint vector from the origin.
821 */
823 NR::Point const cv = NR::Point(
824 pt[NR::X] > _origin[NR::X] ? 1 : -1,
825 pt[NR::Y] > _origin[NR::Y] ? 1 : -1
826 );
828 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
829 _bbox_points,
830 it,
831 Snapper::ConstraintLine(_origin, cv),
832 s,
833 _origin);
835 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
836 _snap_points,
837 it,
838 Snapper::ConstraintLine(_origin, cv),
839 s,
840 _origin);
842 if (bb.second == false && sn.second == false) {
844 /* We didn't snap, so just lock aspect ratio */
845 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
846 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
847 } else {
848 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
849 }
851 } else {
853 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
854 ** just compare difference in s[X].
855 */
856 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
857 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
858 s = (bd < sd) ? bb.first : sn.first;
859 }
861 } else {
862 /* Scale aspect ratio is unlocked */
864 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
865 _bbox_points,
866 it,
867 s,
868 _origin);
869 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
870 _snap_points,
871 it,
872 s,
873 _origin);
875 /* Pick the snap that puts us closest to the original scale */
876 NR::Coord bd = bb.second ?
877 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
878 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
879 : NR_HUGE;
880 NR::Coord sd = sn.second ?
881 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
882 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
883 : NR_HUGE;
884 s = (bd < sd) ? bb.first : sn.first;
885 }
887 /* Update the knot position */
888 pt = ( _point - _origin ) * s + _origin;
890 /* Status text */
891 _message_context.setF(Inkscape::NORMAL_MESSAGE,
892 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
893 100 * s[NR::X], 100 * s[NR::Y]);
895 return TRUE;
896 }
898 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
899 {
900 using NR::X;
901 using NR::Y;
903 NR::Dim2 axis, perp;
905 switch (handle.cursor) {
906 case GDK_TOP_SIDE:
907 case GDK_BOTTOM_SIDE:
908 axis = NR::Y;
909 perp = NR::X;
910 break;
911 case GDK_LEFT_SIDE:
912 case GDK_RIGHT_SIDE:
913 axis = NR::X;
914 perp = NR::Y;
915 break;
916 default:
917 g_assert_not_reached();
918 return TRUE;
919 };
921 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
922 return FALSE;
923 }
925 NR::scale s(1, 1);
926 s[axis] = ( ( pt[axis] - _origin[axis] )
927 / ( _point[axis] - _origin[axis] ) );
928 if ( fabs(s[axis]) < 1e-15 ) {
929 s[axis] = 1e-15;
930 }
932 /* Get a STL list of the selected items.
933 ** FIXME: this should probably be done by Inkscape::Selection.
934 */
935 std::list<SPItem const*> it;
936 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
937 it.push_back(reinterpret_cast<SPItem*>(i->data));
938 }
940 SnapManager const &m = _desktop->namedview->snap_manager;
942 if ( state & GDK_CONTROL_MASK ) {
943 s[perp] = fabs(s[axis]);
945 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
946 Snapper::BBOX_POINT,
947 _bbox_points,
948 it,
949 s[axis],
950 _origin,
951 axis,
952 true);
954 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
955 Snapper::SNAP_POINT,
956 _snap_points,
957 it,
958 s[axis],
959 _origin,
960 axis,
961 true);
963 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
964 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
965 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
967 s[axis] = fabs(ratio) * sign(s[axis]);
968 s[perp] = fabs(s[axis]);
969 } else {
971 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
972 Snapper::BBOX_POINT,
973 _bbox_points,
974 it,
975 s[axis],
976 _origin,
977 axis,
978 false);
980 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
981 Snapper::SNAP_POINT,
982 _snap_points,
983 it,
984 s[axis],
985 _origin,
986 axis,
987 false);
989 /* Choose the smaller difference in scale */
990 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
991 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
992 s[axis] = (bd < sd) ? bb.first : sn.first;
993 s[perp] = 1;
994 }
996 pt = ( _point - _origin ) * NR::scale(s) + _origin;
997 if (isNaN(pt[X] + pt[Y])) {
998 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
999 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1000 }
1002 // status text
1003 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1004 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1005 100 * s[NR::X], 100 * s[NR::Y]);
1007 return TRUE;
1008 }
1010 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1011 {
1012 using NR::X;
1013 using NR::Y;
1015 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1016 return FALSE;
1017 }
1019 NR::Dim2 dim_a;
1020 NR::Dim2 dim_b;
1021 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1022 dim_a = X;
1023 dim_b = Y;
1024 } else {
1025 dim_a = Y;
1026 dim_b = X;
1027 }
1029 double skew[2];
1030 double s[2] = { 1.0, 1.0 };
1032 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1033 return FALSE;
1034 }
1036 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1038 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1040 if ( fabs(s[dim_a]) < 1 ) {
1041 s[dim_a] = sign(s[dim_a]);
1042 } else {
1043 s[dim_a] = floor( s[dim_a] + 0.5 );
1044 }
1046 double radians = atan(skew[dim_a] / s[dim_a]);
1048 if (state & GDK_CONTROL_MASK) {
1050 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1052 if (snaps) {
1053 double sections = floor( radians * snaps / M_PI + .5 );
1054 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1055 radians = ( M_PI / snaps ) * sections;
1056 }
1057 skew[dim_a] = tan(radians) * s[dim_a];
1058 } else {
1059 SnapManager const &m = _desktop->namedview->snap_manager;
1061 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1062 _bbox_points,
1063 std::list<SPItem const *>(),
1064 skew[dim_a],
1065 _origin,
1066 dim_a);
1068 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1069 _snap_points,
1070 std::list<SPItem const *>(),
1071 skew[dim_a],
1072 _origin,
1073 dim_a);
1075 if (bb.second || sn.second) {
1076 /* We snapped something, so change the skew to reflect it */
1077 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1078 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1079 skew[dim_a] = std::min(bd, sd);
1080 }
1081 }
1083 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1084 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1086 /* status text */
1087 double degrees = 180 / M_PI * radians;
1088 if (degrees > 180) degrees -= 360;
1089 if (degrees < -180) degrees += 360;
1091 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1092 // TRANSLATORS: don't modify the first ";"
1093 // (it will NOT be displayed as ";" - only the second one will be)
1094 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1095 degrees);
1097 return TRUE;
1098 }
1100 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1101 {
1102 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1104 // rotate affine in rotate
1105 NR::Point const d1 = _point - _origin;
1106 NR::Point const d2 = pt - _origin;
1108 NR::Coord const h1 = NR::L2(d1);
1109 if (h1 < 1e-15) return FALSE;
1110 NR::Point q1 = d1 / h1;
1111 NR::Coord const h2 = NR::L2(d2);
1112 if (fabs(h2) < 1e-15) return FALSE;
1113 NR::Point q2 = d2 / h2;
1115 double radians;
1116 if (state & GDK_CONTROL_MASK) {
1117 /* Have to restrict movement. */
1118 double cos_t = NR::dot(q1, q2);
1119 double sin_t = NR::dot(NR::rot90(q1), q2);
1120 radians = atan2(sin_t, cos_t);
1121 if (snaps) {
1122 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1123 }
1124 q1 = NR::Point(1, 0);
1125 q2 = NR::Point(cos(radians), sin(radians));
1126 } else {
1127 radians = atan2(NR::dot(NR::rot90(d1), d2),
1128 NR::dot(d1, d2));
1129 }
1131 NR::rotate const r1(q1);
1132 NR::rotate const r2(q2);
1133 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1135 /* status text */
1136 double degrees = 180 / M_PI * radians;
1137 if (degrees > 180) degrees -= 360;
1138 if (degrees < -180) degrees += 360;
1140 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1141 // TRANSLATORS: don't modify the first ";"
1142 // (it will NOT be displayed as ";" - only the second one will be)
1143 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1145 return TRUE;
1146 }
1148 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1149 {
1150 using NR::X;
1151 using NR::Y;
1153 SnapManager const &m = _desktop->namedview->snap_manager;
1154 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1156 if (state & GDK_CONTROL_MASK) {
1157 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1158 pt[Y] = _point[Y];
1159 } else {
1160 pt[X] = _point[X];
1161 }
1162 }
1164 if (!(state & GDK_SHIFT_MASK)) {
1165 // screen pixels to snap center to bbox
1166 #define SNAP_DIST 5
1167 // FIXME: take from prefs
1168 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1170 for (int i = 0; i < 2; i++) {
1172 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1173 pt[i] = _box.min()[i];
1174 }
1175 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1176 pt[i] = _box.midpoint()[i];
1177 }
1178 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1179 pt[i] = _box.max()[i];
1180 }
1181 }
1182 }
1184 // status text
1185 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1186 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1187 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1188 g_string_free(xs, FALSE);
1189 g_string_free(ys, FALSE);
1191 return TRUE;
1192 }
1194 /*
1195 * handlers for handle movement
1196 *
1197 */
1199 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1200 {
1201 seltrans->stretch(handle, pt, state);
1202 }
1204 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1205 {
1206 seltrans->scale(pt, state);
1207 }
1209 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1210 {
1211 seltrans->skew(handle, pt, state);
1212 }
1214 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1215 {
1216 seltrans->rotate(pt, state);
1217 }
1219 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1220 {
1221 using NR::X;
1222 using NR::Y;
1224 NR::Dim2 dim;
1225 switch (handle.cursor) {
1226 case GDK_LEFT_SIDE:
1227 case GDK_RIGHT_SIDE:
1228 dim = X;
1229 break;
1230 case GDK_TOP_SIDE:
1231 case GDK_BOTTOM_SIDE:
1232 dim = Y;
1233 break;
1234 default:
1235 g_assert_not_reached();
1236 abort();
1237 break;
1238 }
1240 NR::Point const scale_origin(_origin);
1241 double const offset = _point[dim] - scale_origin[dim];
1242 if (!( fabs(offset) >= 1e-15 )) {
1243 return;
1244 }
1245 NR::scale s(1, 1);
1246 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1247 if (isNaN(s[dim])) {
1248 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1249 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1250 }
1251 if (!( fabs(s[dim]) >= 1e-15 )) {
1252 s[dim] = 1e-15;
1253 }
1254 if (state & GDK_CONTROL_MASK) {
1255 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1256 s[!dim] = fabs(s[dim]);
1257 }
1259 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1260 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1262 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1263 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1264 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1266 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1267 }
1269 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1270 {
1271 NR::Point const offset = _point - _origin;
1273 NR::scale s (1, 1);
1274 for (int i = NR::X; i <= NR::Y; i++) {
1275 if (fabs(offset[i]) > 1e-9)
1276 s[i] = (pt[i] - _origin[i]) / offset[i];
1277 if (fabs(s[i]) < 1e-9)
1278 s[i] = 1e-9;
1279 }
1280 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1281 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1283 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1284 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1285 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1287 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1288 }
1290 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1291 {
1292 NR::Point const offset = _point - _origin;
1294 unsigned dim;
1295 switch (handle.cursor) {
1296 case GDK_SB_H_DOUBLE_ARROW:
1297 dim = NR::Y;
1298 break;
1299 case GDK_SB_V_DOUBLE_ARROW:
1300 dim = NR::X;
1301 break;
1302 default:
1303 g_assert_not_reached();
1304 abort();
1305 break;
1306 }
1307 if (fabs(offset[dim]) < 1e-15) {
1308 return;
1309 }
1310 NR::Matrix skew = NR::identity();
1311 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1312 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1313 skew[2*(1-dim) + (dim)] = 0;
1314 skew[2*(1-dim) + (1-dim)] = 1;
1316 for (int i = 0; i < 2; i++) {
1317 if (fabs(skew[3*i]) < 1e-15) {
1318 skew[3*i] = 1e-15;
1319 }
1320 }
1321 transform(skew, _origin);
1322 }
1324 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1325 {
1326 NR::Point const offset = _point - _origin;
1328 NR::Coord const h1 = NR::L2(offset);
1329 if (h1 < 1e-15) {
1330 return;
1331 }
1332 NR::Point const q1 = offset / h1;
1333 NR::Coord const h2 = NR::L2( pt - _origin );
1334 if (h2 < 1e-15) {
1335 return;
1336 }
1337 NR::Point const q2 = (pt - _origin) / h2;
1338 NR::rotate const r1(q1);
1339 NR::rotate const r2(q2);
1341 NR::Matrix rotate( r2 / r1 );
1342 transform(rotate, _origin);
1343 }
1345 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1346 {
1347 seltrans->setCenter(pt);
1348 }
1351 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1352 {
1353 SnapManager const &m = _desktop->namedview->snap_manager;
1355 /* The amount that we've moved by during this drag */
1356 NR::Point dxy = xy - _point;
1358 /* Get a STL list of the selected items.
1359 ** FIXME: this should probably be done by Inkscape::Selection.
1360 */
1361 std::list<SPItem const*> it;
1362 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1363 it.push_back(reinterpret_cast<SPItem*>(i->data));
1364 }
1366 bool const alt = (state & GDK_MOD1_MASK);
1367 bool const control = (state & GDK_CONTROL_MASK);
1368 bool const shift = (state & GDK_SHIFT_MASK);
1370 if (alt) {
1372 /* Alt pressed means keep offset: snap the moved distance to the grid.
1373 ** FIXME: this will snap to more than just the grid, nowadays.
1374 */
1376 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1378 } else if (!shift) {
1380 /* We're snapping to things, possibly with a constraint to horizontal or
1381 ** vertical movement. Obtain a list of possible translations and then
1382 ** pick the smallest.
1383 */
1385 /* This will be our list of possible translations */
1386 std::list<std::pair<NR::Point, bool> > s;
1388 if (control) {
1390 /* Snap to things, and also constrain to horizontal or vertical movement */
1392 for (unsigned int dim = 0; dim < 2; dim++) {
1393 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1394 _bbox_points,
1395 it,
1396 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1397 dxy));
1399 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1400 _snap_points,
1401 it,
1402 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1403 dxy));
1404 }
1406 } else {
1408 /* Snap to things with no constraint */
1410 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1411 _bbox_points, it, dxy));
1412 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1413 _snap_points, it, dxy));
1414 }
1416 /* Pick one */
1417 NR::Coord best = NR_HUGE;
1418 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1419 if (i->second) {
1420 NR::Coord const m = NR::L2(i->first);
1421 if (m < best) {
1422 best = m;
1423 dxy = i->first;
1424 }
1425 }
1426 }
1427 }
1429 if (control) {
1430 /* Ensure that the horizontal and vertical constraint has been applied */
1431 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1432 dxy[NR::Y] = 0;
1433 } else {
1434 dxy[NR::X] = 0;
1435 }
1436 }
1438 NR::Matrix const move((NR::translate(dxy)));
1439 NR::Point const norm(0, 0);
1440 transform(move, norm);
1442 // status text
1443 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1444 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1445 _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);
1446 g_string_free(xs, TRUE);
1447 g_string_free(ys, TRUE);
1448 }
1451 /*
1452 Local Variables:
1453 mode:c++
1454 c-file-style:"stroustrup"
1455 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1456 indent-tabs-mode:nil
1457 fill-column:99
1458 End:
1459 */
1460 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :