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(l->data);
224 it->setCenter(p);
225 // only set the value; updating repr and document_done will be done once, on ungrab
226 }
228 _updateHandles();
229 }
231 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
232 {
233 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
235 g_return_if_fail(!_grabbed);
237 _grabbed = true;
238 _show_handles = show_handles;
239 _updateVolatileState();
240 _current.set_identity();
242 _changed = false;
244 if (_empty) {
245 return;
246 }
248 for (GSList const *l = selection->itemList(); l; l = l->next) {
249 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
250 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
251 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
252 }
254 _current.set_identity();
256 _point = p;
258 _snap_points = selection->getSnapPointsConvexHull();
259 _bbox_points = selection->getBBoxPointsOuter();
260 _bbox_4points = _bbox_points;
261 _bbox_4points.push_back(NR::Point(_bbox_points[0][NR::X], _bbox_points[1][NR::Y]));
262 _bbox_4points.push_back(NR::Point(_bbox_points[1][NR::X], _bbox_points[0][NR::Y]));
264 gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
265 bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
266 NR::Rect op_box = _box;
267 if (origin_on_bbox == false && _snap_points.empty() == false) {
268 std::vector<NR::Point>::iterator i = _snap_points.begin();
269 op_box = NR::Rect(*i, *i);
270 i++;
271 while (i != _snap_points.end()) {
272 op_box.expandTo(*i);
273 i++;
274 }
275 }
277 _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
279 if ((x != -1) && (y != -1)) {
280 sp_canvas_item_show(_norm);
281 sp_canvas_item_show(_grip);
282 }
284 if (_show == SHOW_OUTLINE) {
285 for (int i = 0; i < 4; i++)
286 sp_canvas_item_show(_l[i]);
287 }
290 _updateHandles();
291 g_return_if_fail(_stamp_cache == NULL);
292 }
294 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
295 {
296 g_return_if_fail(_grabbed);
297 g_return_if_fail(!_empty);
299 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
301 if (_show == SHOW_CONTENT) {
302 // update the content
303 for (unsigned i = 0; i < _items.size(); i++) {
304 SPItem &item = *_items[i].first;
305 NR::Matrix const &prev_transform = _items[i].second;
306 sp_item_set_i2d_affine(&item, prev_transform * affine);
307 }
308 } else {
309 NR::Point p[4];
310 /* update the outline */
311 for (unsigned i = 0 ; i < 4 ; i++) {
312 p[i] = _box.corner(i) * affine;
313 }
314 for (unsigned i = 0 ; i < 4 ; i++) {
315 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
316 }
317 }
319 _current = affine;
320 _changed = true;
321 _updateHandles();
322 }
324 void Inkscape::SelTrans::ungrab()
325 {
326 g_return_if_fail(_grabbed);
327 _grabbed = false;
328 _show_handles = true;
330 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
331 _updateVolatileState();
333 for (unsigned i = 0; i < _items.size(); i++) {
334 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
335 }
337 sp_canvas_item_hide(_norm);
338 sp_canvas_item_hide(_grip);
340 if (_show == SHOW_OUTLINE) {
341 for (int i = 0; i < 4; i++)
342 sp_canvas_item_hide(_l[i]);
343 }
345 if (_stamp_cache) {
346 g_slist_free(_stamp_cache);
347 _stamp_cache = NULL;
348 }
350 _message_context.clear();
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 _items.clear();
371 _items_centers.clear();
373 if (_current.is_translation()) {
374 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
375 _("Move"));
376 } else if (_current.is_scale()) {
377 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
378 _("Scale"));
379 } else if (_current.is_rotation()) {
380 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
381 _("Rotate"));
382 } else {
383 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
384 _("Skew"));
385 }
387 } else {
389 if (_center_is_set) {
390 // we were dragging center; update reprs and commit undoable action
391 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
392 SPItem *it = (SPItem*)SP_OBJECT(l->data);
393 SP_OBJECT(it)->updateRepr();
394 }
395 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
396 _("Set center"));
397 }
399 _items.clear();
400 _items_centers.clear();
401 _updateHandles();
402 }
403 }
405 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
406 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
408 void Inkscape::SelTrans::stamp()
409 {
410 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
412 bool fixup = !_grabbed;
413 if ( fixup && _stamp_cache ) {
414 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
415 g_slist_free(_stamp_cache);
416 _stamp_cache = NULL;
417 }
419 /* stamping mode */
420 if (!_empty) {
421 GSList *l;
422 if (_stamp_cache) {
423 l = _stamp_cache;
424 } else {
425 /* Build cache */
426 l = g_slist_copy((GSList *) selection->itemList());
427 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
428 _stamp_cache = l;
429 }
431 while (l) {
432 SPItem *original_item = SP_ITEM(l->data);
433 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
435 // remember the position of the item
436 gint pos = original_repr->position();
437 // remember parent
438 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
440 Inkscape::XML::Node *copy_repr = original_repr->duplicate();
442 // add the new repr to the parent
443 parent->appendChild(copy_repr);
444 // move to the saved position
445 copy_repr->setPosition(pos > 0 ? pos : 0);
447 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
449 NR::Matrix const *new_affine;
450 if (_show == SHOW_OUTLINE) {
451 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
452 NR::Matrix const i2dnew( i2d * _current );
453 sp_item_set_i2d_affine(copy_item, i2dnew);
454 new_affine = ©_item->transform;
455 } else {
456 new_affine = &original_item->transform;
457 }
459 sp_item_write_transform(copy_item, copy_repr, *new_affine);
461 if (copy_item->isCenterSet()) {
462 copy_item->setCenter(_center * _current);
463 }
465 Inkscape::GC::release(copy_repr);
466 l = l->next;
467 }
468 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
469 _("Stamp"));
470 }
472 if ( fixup && _stamp_cache ) {
473 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
474 g_slist_free(_stamp_cache);
475 _stamp_cache = NULL;
476 }
477 }
479 void Inkscape::SelTrans::_updateHandles()
480 {
481 if ( !_show_handles || _empty )
482 {
483 sp_remove_handles(_shandle, 8);
484 sp_remove_handles(_rhandle, 8);
485 sp_remove_handles(&_chandle, 1);
486 return;
487 }
489 // center handle
490 if ( _chandle == NULL ) {
491 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
493 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
494 _chandle->setSize (13);
495 _chandle->setAnchor (handle_center.anchor);
496 _chandle->setMode (SP_CTRL_MODE_XOR);
497 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
498 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
499 _chandle->setPixbuf(handles[handle_center.control]);
500 sp_knot_update_ctrl(_chandle);
502 g_signal_connect(G_OBJECT(_chandle), "request",
503 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
504 g_signal_connect(G_OBJECT(_chandle), "moved",
505 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
506 g_signal_connect(G_OBJECT(_chandle), "grabbed",
507 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
508 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
509 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
510 g_signal_connect(G_OBJECT(_chandle), "clicked",
511 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
512 }
514 sp_remove_handles(&_chandle, 1);
515 if ( _state == STATE_SCALE ) {
516 sp_remove_handles(_rhandle, 8);
517 _showHandles(_shandle, handles_scale, 8,
518 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
519 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
520 } else {
521 sp_remove_handles(_shandle, 8);
522 _showHandles(_rhandle, handles_rotate, 8,
523 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
524 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
525 }
527 if (!_center_is_set) {
528 _center = _desktop->selection->center();
529 _center_is_set = true;
530 }
532 if ( _state == STATE_SCALE ) {
533 sp_knot_hide(_chandle);
534 } else {
535 sp_knot_show(_chandle);
536 sp_knot_moveto(_chandle, &_center);
537 }
538 }
540 void Inkscape::SelTrans::_updateVolatileState()
541 {
542 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
543 _empty = selection->isEmpty();
545 if (_empty) {
546 return;
547 }
549 _box = selection->bounds();
550 if (_box.isEmpty()) {
551 _empty = true;
552 return;
553 }
555 _strokewidth = stroke_average_width (selection->itemList());
556 }
558 static void sp_remove_handles(SPKnot *knot[], gint num)
559 {
560 for (int i = 0; i < num; i++) {
561 if (knot[i] != NULL) {
562 sp_knot_hide(knot[i]);
563 }
564 }
565 }
567 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
568 gchar const *even_tip, gchar const *odd_tip)
569 {
570 g_return_if_fail( !_empty );
572 for (int i = 0; i < num; i++) {
573 if (knot[i] == NULL) {
574 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
576 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
577 knot[i]->setSize (13);
578 knot[i]->setAnchor (handle[i].anchor);
579 knot[i]->setMode (SP_CTRL_MODE_XOR);
580 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
581 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
582 knot[i]->setPixbuf(handles[handle[i].control]);
583 sp_knot_update_ctrl(knot[i]);
585 g_signal_connect(G_OBJECT(knot[i]), "request",
586 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
587 g_signal_connect(G_OBJECT(knot[i]), "moved",
588 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
589 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
590 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
591 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
592 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
593 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
594 }
595 sp_knot_show(knot[i]);
597 NR::Point const handle_pt(handle[i].x, handle[i].y);
598 NR::Point p( _box.min()
599 + ( _box.dimensions()
600 * NR::scale(handle_pt) ) );
602 sp_knot_moveto(knot[i], &p);
603 }
604 }
606 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
607 {
608 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
609 knot, state, *(SPSelTransHandle const *) data
610 );
611 }
613 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
614 {
615 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
616 }
618 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
619 {
620 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
621 knot, position, state, *(SPSelTransHandle const *) data
622 );
623 }
625 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
626 {
627 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
628 knot, position, state, *(SPSelTransHandle const *) data
629 );
630 }
632 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
633 {
634 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
635 knot, state, *(SPSelTransHandle const *) data
636 );
637 }
639 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
640 {
641 switch (handle.anchor) {
642 case GTK_ANCHOR_CENTER:
643 if (state & GDK_SHIFT_MASK) {
644 // Unset the center position for all selected items
645 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
646 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
647 it->unsetCenter();
648 SP_OBJECT(it)->updateRepr();
649 _center_is_set = false; // center has changed
650 _updateHandles();
651 }
652 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
653 _("Reset center"));
654 }
655 break;
656 default:
657 break;
658 }
659 }
661 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
662 {
663 switch (handle.anchor) {
664 case GTK_ANCHOR_CENTER:
665 g_object_set(G_OBJECT(_grip),
666 "shape", SP_CTRL_SHAPE_BITMAP,
667 "size", 13.0,
668 NULL);
669 sp_canvas_item_show(_grip);
670 break;
671 default:
672 g_object_set(G_OBJECT(_grip),
673 "shape", SP_CTRL_SHAPE_CROSS,
674 "size", 7.0,
675 NULL);
676 sp_canvas_item_show(_norm);
677 sp_canvas_item_show(_grip);
679 break;
680 }
682 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
683 }
686 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
687 {
688 if (!SP_KNOT_IS_GRABBED(knot)) {
689 return;
690 }
692 // in case items have been unhooked from the document, don't
693 // try to continue processing events for them.
694 for (unsigned int i = 0; i < _items.size(); i++) {
695 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
696 return;
697 }
698 }
700 handle.action(this, handle, *position, state);
701 }
704 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
705 {
706 if (!SP_KNOT_IS_GRABBED(knot)) {
707 return TRUE;
708 }
710 knot->desktop->set_coordinate_status(*position);
711 knot->desktop->setPosition(*position);
714 if (state & GDK_MOD1_MASK) {
715 *position = _point + ( *position - _point ) / 10;
716 }
718 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
719 _origin = _opposite;
720 } else {
721 _origin = _center;
722 }
723 if (handle.request(this, handle, *position, state)) {
724 sp_knot_set_position(knot, position, state);
725 SP_CTRL(_grip)->moveto(*position);
726 SP_CTRL(_norm)->moveto(_origin);
727 }
729 return TRUE;
730 }
733 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
734 {
735 if (!_grabbed) {
736 _updateVolatileState();
737 _current.set_identity();
738 _center_is_set = false; // center(s) may have changed
739 _updateHandles();
740 }
741 }
743 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
744 {
745 if (!_grabbed) {
746 _updateVolatileState();
747 _current.set_identity();
749 // reset internal flag
750 _changed = false;
752 _center_is_set = false; // center(s) may have changed
754 _updateHandles();
755 }
756 }
758 /*
759 * handlers for handle move-request
760 */
762 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
763 static double sign(double const x)
764 {
765 return ( x < 0
766 ? -1
767 : 1 );
768 }
770 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
771 SPSelTransHandle const &, NR::Point &pt, guint state)
772 {
773 return seltrans->scaleRequest(pt, state);
774 }
776 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
777 SPSelTransHandle const &handle, NR::Point &pt, guint state)
778 {
779 return seltrans->stretchRequest(handle, pt, state);
780 }
782 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
783 SPSelTransHandle const &handle, NR::Point &pt, guint state)
784 {
785 return seltrans->skewRequest(handle, pt, state);
786 }
788 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
789 SPSelTransHandle const &, NR::Point &pt, guint state)
790 {
791 return seltrans->rotateRequest(pt, state);
792 }
794 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
795 SPSelTransHandle const &, NR::Point &pt, guint state)
796 {
797 return seltrans->centerRequest(pt, state);
798 }
800 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
801 {
802 using NR::X;
803 using NR::Y;
805 NR::Point d = _point - _origin;
806 NR::scale s(0, 0);
808 /* Work out the new scale factors `s' */
809 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
810 if ( fabs(d[i]) > 0.001 ) {
811 s[i] = ( pt[i] - _origin[i] ) / d[i];
812 if ( fabs(s[i]) < 1e-9 ) {
813 s[i] = 1e-9;
814 }
815 }
816 }
818 SnapManager const &m = _desktop->namedview->snap_manager;
820 /* Get a STL list of the selected items.
821 ** FIXME: this should probably be done by Inkscape::Selection.
822 */
823 std::list<SPItem const*> it;
824 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
825 it.push_back(reinterpret_cast<SPItem*>(i->data));
826 }
828 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
829 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
830 ** To do this, we snap along a suitable constraint vector from the origin.
831 */
833 NR::Point const cv = NR::Point(
834 pt[NR::X] > _origin[NR::X] ? 1 : -1,
835 pt[NR::Y] > _origin[NR::Y] ? 1 : -1
836 );
838 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
839 _bbox_points,
840 it,
841 Snapper::ConstraintLine(_origin, cv),
842 s,
843 _origin);
845 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
846 _snap_points,
847 it,
848 Snapper::ConstraintLine(_origin, cv),
849 s,
850 _origin);
852 if (bb.second == false && sn.second == false) {
854 /* We didn't snap, so just lock aspect ratio */
855 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
856 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
857 } else {
858 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
859 }
861 } else {
863 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
864 ** just compare difference in s[X].
865 */
866 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
867 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
868 s = (bd < sd) ? bb.first : sn.first;
869 }
871 } else {
872 /* Scale aspect ratio is unlocked */
874 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
875 _bbox_4points,
876 it,
877 s,
878 _origin);
879 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
880 _snap_points,
881 it,
882 s,
883 _origin);
885 /* Pick the snap that puts us closest to the original scale */
886 NR::Coord bd = bb.second ?
887 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
888 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
889 : NR_HUGE;
890 NR::Coord sd = sn.second ?
891 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
892 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
893 : NR_HUGE;
894 s = (bd < sd) ? bb.first : sn.first;
895 }
897 /* Update the knot position */
898 pt = ( _point - _origin ) * s + _origin;
900 /* Status text */
901 _message_context.setF(Inkscape::NORMAL_MESSAGE,
902 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
903 100 * s[NR::X], 100 * s[NR::Y]);
905 return TRUE;
906 }
908 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
909 {
910 using NR::X;
911 using NR::Y;
913 NR::Dim2 axis, perp;
915 switch (handle.cursor) {
916 case GDK_TOP_SIDE:
917 case GDK_BOTTOM_SIDE:
918 axis = NR::Y;
919 perp = NR::X;
920 break;
921 case GDK_LEFT_SIDE:
922 case GDK_RIGHT_SIDE:
923 axis = NR::X;
924 perp = NR::Y;
925 break;
926 default:
927 g_assert_not_reached();
928 return TRUE;
929 };
931 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
932 return FALSE;
933 }
935 NR::scale s(1, 1);
936 s[axis] = ( ( pt[axis] - _origin[axis] )
937 / ( _point[axis] - _origin[axis] ) );
938 if ( fabs(s[axis]) < 1e-15 ) {
939 s[axis] = 1e-15;
940 }
942 /* Get a STL list of the selected items.
943 ** FIXME: this should probably be done by Inkscape::Selection.
944 */
945 std::list<SPItem const*> it;
946 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
947 it.push_back(reinterpret_cast<SPItem*>(i->data));
948 }
950 SnapManager const &m = _desktop->namedview->snap_manager;
952 if ( state & GDK_CONTROL_MASK ) {
953 s[perp] = fabs(s[axis]);
955 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
956 Snapper::BBOX_POINT,
957 _bbox_points,
958 it,
959 s[axis],
960 _origin,
961 axis,
962 true);
964 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
965 Snapper::SNAP_POINT,
966 _snap_points,
967 it,
968 s[axis],
969 _origin,
970 axis,
971 true);
973 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
974 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
975 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
977 s[axis] = fabs(ratio) * sign(s[axis]);
978 s[perp] = fabs(s[axis]);
979 } else {
981 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
982 Snapper::BBOX_POINT,
983 _bbox_points,
984 it,
985 s[axis],
986 _origin,
987 axis,
988 false);
990 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
991 Snapper::SNAP_POINT,
992 _snap_points,
993 it,
994 s[axis],
995 _origin,
996 axis,
997 false);
999 /* Choose the smaller difference in scale */
1000 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1001 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1002 s[axis] = (bd < sd) ? bb.first : sn.first;
1003 s[perp] = 1;
1004 }
1006 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1007 if (isNaN(pt[X] + pt[Y])) {
1008 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1009 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1010 }
1012 // status text
1013 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1014 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1015 100 * s[NR::X], 100 * s[NR::Y]);
1017 return TRUE;
1018 }
1020 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1021 {
1022 using NR::X;
1023 using NR::Y;
1025 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1026 return FALSE;
1027 }
1029 NR::Dim2 dim_a;
1030 NR::Dim2 dim_b;
1031 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1032 dim_a = X;
1033 dim_b = Y;
1034 } else {
1035 dim_a = Y;
1036 dim_b = X;
1037 }
1039 double skew[2];
1040 double s[2] = { 1.0, 1.0 };
1042 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1043 return FALSE;
1044 }
1046 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1048 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1050 if ( fabs(s[dim_a]) < 1 ) {
1051 s[dim_a] = sign(s[dim_a]);
1052 } else {
1053 s[dim_a] = floor( s[dim_a] + 0.5 );
1054 }
1056 double radians = atan(skew[dim_a] / s[dim_a]);
1058 if (state & GDK_CONTROL_MASK) {
1060 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1062 if (snaps) {
1063 double sections = floor( radians * snaps / M_PI + .5 );
1064 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1065 radians = ( M_PI / snaps ) * sections;
1066 }
1067 skew[dim_a] = tan(radians) * s[dim_a];
1068 } else {
1069 SnapManager const &m = _desktop->namedview->snap_manager;
1071 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1072 _bbox_points,
1073 std::list<SPItem const *>(),
1074 skew[dim_a],
1075 _origin,
1076 dim_a);
1078 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1079 _snap_points,
1080 std::list<SPItem const *>(),
1081 skew[dim_a],
1082 _origin,
1083 dim_a);
1085 if (bb.second || sn.second) {
1086 /* We snapped something, so change the skew to reflect it */
1087 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1088 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1089 skew[dim_a] = std::min(bd, sd);
1090 }
1091 }
1093 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1094 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1096 /* status text */
1097 double degrees = 180 / M_PI * radians;
1098 if (degrees > 180) degrees -= 360;
1099 if (degrees < -180) degrees += 360;
1101 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1102 // TRANSLATORS: don't modify the first ";"
1103 // (it will NOT be displayed as ";" - only the second one will be)
1104 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1105 degrees);
1107 return TRUE;
1108 }
1110 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1111 {
1112 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1114 // rotate affine in rotate
1115 NR::Point const d1 = _point - _origin;
1116 NR::Point const d2 = pt - _origin;
1118 NR::Coord const h1 = NR::L2(d1);
1119 if (h1 < 1e-15) return FALSE;
1120 NR::Point q1 = d1 / h1;
1121 NR::Coord const h2 = NR::L2(d2);
1122 if (fabs(h2) < 1e-15) return FALSE;
1123 NR::Point q2 = d2 / h2;
1125 double radians;
1126 if (state & GDK_CONTROL_MASK) {
1127 /* Have to restrict movement. */
1128 double cos_t = NR::dot(q1, q2);
1129 double sin_t = NR::dot(NR::rot90(q1), q2);
1130 radians = atan2(sin_t, cos_t);
1131 if (snaps) {
1132 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1133 }
1134 q1 = NR::Point(1, 0);
1135 q2 = NR::Point(cos(radians), sin(radians));
1136 } else {
1137 radians = atan2(NR::dot(NR::rot90(d1), d2),
1138 NR::dot(d1, d2));
1139 }
1141 NR::rotate const r1(q1);
1142 NR::rotate const r2(q2);
1143 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1145 /* status text */
1146 double degrees = 180 / M_PI * radians;
1147 if (degrees > 180) degrees -= 360;
1148 if (degrees < -180) degrees += 360;
1150 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1151 // TRANSLATORS: don't modify the first ";"
1152 // (it will NOT be displayed as ";" - only the second one will be)
1153 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1155 return TRUE;
1156 }
1158 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1159 {
1160 using NR::X;
1161 using NR::Y;
1163 SnapManager const &m = _desktop->namedview->snap_manager;
1164 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1166 if (state & GDK_CONTROL_MASK) {
1167 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1168 pt[Y] = _point[Y];
1169 } else {
1170 pt[X] = _point[X];
1171 }
1172 }
1174 if (!(state & GDK_SHIFT_MASK)) {
1175 // screen pixels to snap center to bbox
1176 #define SNAP_DIST 5
1177 // FIXME: take from prefs
1178 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1180 for (int i = 0; i < 2; i++) {
1182 if (fabs(pt[i] - _box.min()[i]) < snap_dist) {
1183 pt[i] = _box.min()[i];
1184 }
1185 if (fabs(pt[i] - _box.midpoint()[i]) < snap_dist) {
1186 pt[i] = _box.midpoint()[i];
1187 }
1188 if (fabs(pt[i] - _box.max()[i]) < snap_dist) {
1189 pt[i] = _box.max()[i];
1190 }
1191 }
1192 }
1194 // status text
1195 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1196 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1197 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1198 g_string_free(xs, FALSE);
1199 g_string_free(ys, FALSE);
1201 return TRUE;
1202 }
1204 /*
1205 * handlers for handle movement
1206 *
1207 */
1209 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1210 {
1211 seltrans->stretch(handle, pt, state);
1212 }
1214 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1215 {
1216 seltrans->scale(pt, state);
1217 }
1219 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1220 {
1221 seltrans->skew(handle, pt, state);
1222 }
1224 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1225 {
1226 seltrans->rotate(pt, state);
1227 }
1229 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1230 {
1231 using NR::X;
1232 using NR::Y;
1234 NR::Dim2 dim;
1235 switch (handle.cursor) {
1236 case GDK_LEFT_SIDE:
1237 case GDK_RIGHT_SIDE:
1238 dim = X;
1239 break;
1240 case GDK_TOP_SIDE:
1241 case GDK_BOTTOM_SIDE:
1242 dim = Y;
1243 break;
1244 default:
1245 g_assert_not_reached();
1246 abort();
1247 break;
1248 }
1250 NR::Point const scale_origin(_origin);
1251 double const offset = _point[dim] - scale_origin[dim];
1252 if (!( fabs(offset) >= 1e-15 )) {
1253 return;
1254 }
1255 NR::scale s(1, 1);
1256 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1257 if (isNaN(s[dim])) {
1258 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1259 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1260 }
1261 if (!( fabs(s[dim]) >= 1e-15 )) {
1262 s[dim] = 1e-15;
1263 }
1264 if (state & GDK_CONTROL_MASK) {
1265 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1266 s[!dim] = fabs(s[dim]);
1267 }
1269 NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1270 NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1272 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1273 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1274 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1276 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1277 }
1279 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1280 {
1281 NR::Point const offset = _point - _origin;
1283 NR::scale s (1, 1);
1284 for (int i = NR::X; i <= NR::Y; i++) {
1285 if (fabs(offset[i]) > 1e-9)
1286 s[i] = (pt[i] - _origin[i]) / offset[i];
1287 if (fabs(s[i]) < 1e-9)
1288 s[i] = 1e-9;
1289 }
1290 NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1291 NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1293 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1294 NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
1295 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1297 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1298 }
1300 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1301 {
1302 NR::Point const offset = _point - _origin;
1304 unsigned dim;
1305 switch (handle.cursor) {
1306 case GDK_SB_H_DOUBLE_ARROW:
1307 dim = NR::Y;
1308 break;
1309 case GDK_SB_V_DOUBLE_ARROW:
1310 dim = NR::X;
1311 break;
1312 default:
1313 g_assert_not_reached();
1314 abort();
1315 break;
1316 }
1317 if (fabs(offset[dim]) < 1e-15) {
1318 return;
1319 }
1320 NR::Matrix skew = NR::identity();
1321 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1322 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1323 skew[2*(1-dim) + (dim)] = 0;
1324 skew[2*(1-dim) + (1-dim)] = 1;
1326 for (int i = 0; i < 2; i++) {
1327 if (fabs(skew[3*i]) < 1e-15) {
1328 skew[3*i] = 1e-15;
1329 }
1330 }
1331 transform(skew, _origin);
1332 }
1334 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1335 {
1336 NR::Point const offset = _point - _origin;
1338 NR::Coord const h1 = NR::L2(offset);
1339 if (h1 < 1e-15) {
1340 return;
1341 }
1342 NR::Point const q1 = offset / h1;
1343 NR::Coord const h2 = NR::L2( pt - _origin );
1344 if (h2 < 1e-15) {
1345 return;
1346 }
1347 NR::Point const q2 = (pt - _origin) / h2;
1348 NR::rotate const r1(q1);
1349 NR::rotate const r2(q2);
1351 NR::Matrix rotate( r2 / r1 );
1352 transform(rotate, _origin);
1353 }
1355 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1356 {
1357 seltrans->setCenter(pt);
1358 }
1361 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1362 {
1363 SnapManager const &m = _desktop->namedview->snap_manager;
1365 /* The amount that we've moved by during this drag */
1366 NR::Point dxy = xy - _point;
1368 /* Get a STL list of the selected items.
1369 ** FIXME: this should probably be done by Inkscape::Selection.
1370 */
1371 std::list<SPItem const*> it;
1372 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1373 it.push_back(reinterpret_cast<SPItem*>(i->data));
1374 }
1376 bool const alt = (state & GDK_MOD1_MASK);
1377 bool const control = (state & GDK_CONTROL_MASK);
1378 bool const shift = (state & GDK_SHIFT_MASK);
1380 if (alt) {
1382 /* Alt pressed means keep offset: snap the moved distance to the grid.
1383 ** FIXME: this will snap to more than just the grid, nowadays.
1384 */
1386 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1388 } else if (!shift) {
1390 /* We're snapping to things, possibly with a constraint to horizontal or
1391 ** vertical movement. Obtain a list of possible translations and then
1392 ** pick the smallest.
1393 */
1395 /* This will be our list of possible translations */
1396 std::list<std::pair<NR::Point, bool> > s;
1398 if (control) {
1400 /* Snap to things, and also constrain to horizontal or vertical movement */
1402 for (unsigned int dim = 0; dim < 2; dim++) {
1403 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1404 _bbox_points,
1405 it,
1406 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1407 dxy));
1409 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1410 _snap_points,
1411 it,
1412 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1413 dxy));
1414 }
1416 } else {
1418 /* Snap to things with no constraint */
1420 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1421 _bbox_4points, it, dxy));
1422 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1423 _snap_points, it, dxy));
1424 }
1426 /* Pick one */
1427 NR::Coord best = NR_HUGE;
1428 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1429 if (i->second) {
1430 NR::Coord const m = NR::L2(i->first);
1431 if (m < best) {
1432 best = m;
1433 dxy = i->first;
1434 }
1435 }
1436 }
1437 }
1439 if (control) {
1440 /* Ensure that the horizontal and vertical constraint has been applied */
1441 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1442 dxy[NR::Y] = 0;
1443 } else {
1444 dxy[NR::X] = 0;
1445 }
1446 }
1448 NR::Matrix const move((NR::translate(dxy)));
1449 NR::Point const norm(0, 0);
1450 transform(move, norm);
1452 // status text
1453 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1454 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1455 _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);
1456 g_string_free(xs, TRUE);
1457 g_string_free(ys, TRUE);
1458 }
1461 /*
1462 Local Variables:
1463 mode:c++
1464 c-file-style:"stroustrup"
1465 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1466 indent-tabs-mode:nil
1467 fill-column:99
1468 End:
1469 */
1470 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :