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 _bbox(NR::Nothing()),
92 _approximate_bbox(NR::Nothing()),
93 _chandle(NULL),
94 _stamp_cache(NULL),
95 _message_context(desktop->messageStack())
96 {
97 gchar const *prefs_bbox = prefs_get_string_attribute("tools", "bounding_box");
98 _snap_bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
100 g_return_if_fail(desktop != NULL);
102 for (int i = 0; i < 8; i++) {
103 _shandle[i] = NULL;
104 _rhandle[i] = NULL;
105 }
107 _updateVolatileState();
108 _current.set_identity();
110 _center_is_set = false; // reread _center from items, or set to bbox midpoint
112 _updateHandles();
114 _selection = sp_desktop_selection(desktop);
116 _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
117 SP_TYPE_CTRL,
118 "anchor", GTK_ANCHOR_CENTER,
119 "mode", SP_CTRL_MODE_COLOR,
120 "shape", SP_CTRL_SHAPE_BITMAP,
121 "size", 13.0,
122 "filled", TRUE,
123 "fill_color", 0x00000000,
124 "stroked", TRUE,
125 "stroke_color", 0x000000a0,
126 "pixbuf", handles[12],
127 NULL);
129 _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
130 SP_TYPE_CTRL,
131 "anchor", GTK_ANCHOR_CENTER,
132 "mode", SP_CTRL_MODE_XOR,
133 "shape", SP_CTRL_SHAPE_CROSS,
134 "size", 7.0,
135 "filled", TRUE,
136 "fill_color", 0xffffff7f,
137 "stroked", TRUE,
138 "stroke_color", 0xffffffff,
139 "pixbuf", handles[12],
140 NULL);
142 sp_canvas_item_hide(_grip);
143 sp_canvas_item_hide(_norm);
145 for (int i = 0; i < 4; i++) {
146 _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
147 sp_canvas_item_hide(_l[i]);
148 }
150 _sel_changed_connection = _selection->connectChanged(
151 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
152 );
154 _sel_modified_connection = _selection->connectModified(
155 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
156 );
157 }
159 Inkscape::SelTrans::~SelTrans()
160 {
161 _sel_changed_connection.disconnect();
162 _sel_modified_connection.disconnect();
164 for (unsigned int i = 0; i < 8; i++) {
165 if (_shandle[i]) {
166 g_object_unref(G_OBJECT(_shandle[i]));
167 _shandle[i] = NULL;
168 }
169 if (_rhandle[i]) {
170 g_object_unref(G_OBJECT(_rhandle[i]));
171 _rhandle[i] = NULL;
172 }
173 }
174 if (_chandle) {
175 g_object_unref(G_OBJECT(_chandle));
176 _chandle = NULL;
177 }
179 if (_norm) {
180 gtk_object_destroy(GTK_OBJECT(_norm));
181 _norm = NULL;
182 }
183 if (_grip) {
184 gtk_object_destroy(GTK_OBJECT(_grip));
185 _grip = NULL;
186 }
187 for (int i = 0; i < 4; i++) {
188 if (_l[i]) {
189 gtk_object_destroy(GTK_OBJECT(_l[i]));
190 _l[i] = NULL;
191 }
192 }
194 for (unsigned i = 0; i < _items.size(); i++) {
195 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
196 }
198 _items.clear();
199 _items_centers.clear();
200 }
202 void Inkscape::SelTrans::resetState()
203 {
204 _state = STATE_SCALE;
205 }
207 void Inkscape::SelTrans::increaseState()
208 {
209 if (_state == STATE_SCALE) {
210 _state = STATE_ROTATE;
211 } else {
212 _state = STATE_SCALE;
213 }
215 _center_is_set = true; // no need to reread center
217 _updateHandles();
218 }
220 void Inkscape::SelTrans::setCenter(NR::Point const &p)
221 {
222 _center = p;
223 _center_is_set = true;
225 // Write the new center position into all selected items
226 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
227 SPItem *it = (SPItem*)SP_OBJECT(l->data);
228 it->setCenter(p);
229 // only set the value; updating repr and document_done will be done once, on ungrab
230 }
232 _updateHandles();
233 }
235 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
236 {
237 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
239 g_return_if_fail(!_grabbed);
241 _grabbed = true;
242 _show_handles = show_handles;
243 _updateVolatileState();
244 _current.set_identity();
246 _changed = false;
248 if (_empty) {
249 return;
250 }
252 for (GSList const *l = selection->itemList(); l; l = l->next) {
253 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
254 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
255 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
256 }
258 _current.set_identity();
260 _point = p;
262 // The selector tool should snap the bbox, special snappoints, and path nodes
263 // (The special points are the handles, center, rotation axis, font baseline, ends of spiral, etc.)
265 // First, determine the bounding box for snapping ...
266 _bbox = selection->bounds(_snap_bbox_type);
267 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX); // Used for correctly scaling the strokewidth
269 // Next, get all points to consider for snapping
270 SnapManager const &m = _desktop->namedview->snap_manager;
271 _snap_points = selection->getSnapPoints(m.getIncludeItemCenter());
272 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull();
273 if (_snap_points.size() > 100) {
274 /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
275 An average user would rarely ever try to snap such a large number of nodes anyway, because
276 (s)he could hardly discern which node would be snapping */
277 _snap_points = snap_points_hull;
278 // Unfortunately, by now we will have lost the font-baseline snappoints :-(
279 }
281 // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
282 // path nodes, for example because a rectangle which has been converted to a path doesn't have
283 // any other special points
284 NR::Rect snap_points_bbox;
285 if ( snap_points_hull.empty() == false ) {
286 std::vector<NR::Point>::iterator i = snap_points_hull.begin();
287 snap_points_bbox = NR::Rect(*i, *i);
288 i++;
289 while (i != snap_points_hull.end()) {
290 snap_points_bbox.expandTo(*i);
291 i++;
292 }
293 }
295 _bbox_points.clear();
296 if (_bbox) {
297 // ... and add the bbox corners to _bbox_points
298 for ( unsigned i = 0 ; i < 4 ; i++ ) {
299 _bbox_points.push_back(_bbox->corner(i));
300 }
301 // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
302 // - one for snapping the boundingbox, which can be either visual or geometric
303 // - one for snapping the special points
304 // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
305 // These distinct "opposites" are needed in the snapmanager to avoid bugs such as #1540195 (in which
306 // a box is caught between to guides)
307 _opposite_for_bboxpoints = _bbox->min() + _bbox->dimensions() * NR::scale(1-x, 1-y);
308 _opposite_for_specpoints = (snap_points_bbox.min() + (snap_points_bbox.dimensions() * NR::scale(1-x, 1-y) ) );
309 // Only a single "opposite" can be used in calculating transformations.
310 _opposite = _opposite_for_bboxpoints;
311 }
313 // The lines below are usefull for debugging any snapping issues, as they'll spit out all points that are considered for snapping
315 /*std::cout << "Number of snap points: " << _snap_points.size() << std::endl;
316 for (std::vector<NR::Point>::const_iterator i = _snap_points.begin(); i != _snap_points.end(); i++)
317 {
318 std::cout << " " << *i << std::endl;
319 }
321 std::cout << "Number of bbox points: " << _bbox_points.size() << std::endl;
322 for (std::vector<NR::Point>::const_iterator i = _bbox_points.begin(); i != _bbox_points.end(); i++)
323 {
324 std::cout << " " << *i << std::endl;
325 }*/
327 if ((x != -1) && (y != -1)) {
328 sp_canvas_item_show(_norm);
329 sp_canvas_item_show(_grip);
330 }
332 if (_show == SHOW_OUTLINE) {
333 for (int i = 0; i < 4; i++)
334 sp_canvas_item_show(_l[i]);
335 }
337 _updateHandles();
338 g_return_if_fail(_stamp_cache == NULL);
339 }
341 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
342 {
343 g_return_if_fail(_grabbed);
344 g_return_if_fail(!_empty);
346 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
348 if (_show == SHOW_CONTENT) {
349 // update the content
350 for (unsigned i = 0; i < _items.size(); i++) {
351 SPItem &item = *_items[i].first;
352 NR::Matrix const &prev_transform = _items[i].second;
353 sp_item_set_i2d_affine(&item, prev_transform * affine);
354 }
355 } else {
356 if (_bbox) {
357 NR::Point p[4];
358 /* update the outline */
359 for (unsigned i = 0 ; i < 4 ; i++) {
360 p[i] = _bbox->corner(i) * affine;
361 }
362 for (unsigned i = 0 ; i < 4 ; i++) {
363 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
364 }
365 }
366 }
368 _current = affine;
369 _changed = true;
370 _updateHandles();
371 }
373 void Inkscape::SelTrans::ungrab()
374 {
375 g_return_if_fail(_grabbed);
376 _grabbed = false;
377 _show_handles = true;
379 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
380 _updateVolatileState();
382 for (unsigned i = 0; i < _items.size(); i++) {
383 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
384 }
386 sp_canvas_item_hide(_norm);
387 sp_canvas_item_hide(_grip);
389 if (_show == SHOW_OUTLINE) {
390 for (int i = 0; i < 4; i++)
391 sp_canvas_item_hide(_l[i]);
392 }
394 if (_stamp_cache) {
395 g_slist_free(_stamp_cache);
396 _stamp_cache = NULL;
397 }
399 _message_context.clear();
401 if (!_empty && _changed) {
402 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
403 if (_center) {
404 *_center *= _current;
405 _center_is_set = true;
406 }
408 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
409 // appropriately - it does not know the original positions of the centers (all objects already have
410 // the new bboxes). So we need to reset the centers from our saved array.
411 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
412 for (unsigned i = 0; i < _items_centers.size(); i++) {
413 SPItem *currentItem = _items_centers[i].first;
414 if (currentItem->isCenterSet()) { // only if it's already set
415 currentItem->setCenter (_items_centers[i].second * _current);
416 SP_OBJECT(currentItem)->updateRepr();
417 }
418 }
419 }
421 _items.clear();
422 _items_centers.clear();
424 if (_current.is_translation()) {
425 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
426 _("Move"));
427 } else if (_current.is_scale()) {
428 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
429 _("Scale"));
430 } else if (_current.is_rotation()) {
431 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
432 _("Rotate"));
433 } else {
434 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
435 _("Skew"));
436 }
438 } else {
440 if (_center_is_set) {
441 // we were dragging center; update reprs and commit undoable action
442 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
443 SPItem *it = (SPItem*)SP_OBJECT(l->data);
444 SP_OBJECT(it)->updateRepr();
445 }
446 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
447 _("Set center"));
448 }
450 _items.clear();
451 _items_centers.clear();
452 _updateHandles();
453 }
454 }
456 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
457 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
459 void Inkscape::SelTrans::stamp()
460 {
461 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
463 bool fixup = !_grabbed;
464 if ( fixup && _stamp_cache ) {
465 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
466 g_slist_free(_stamp_cache);
467 _stamp_cache = NULL;
468 }
470 /* stamping mode */
471 if (!_empty) {
472 GSList *l;
473 if (_stamp_cache) {
474 l = _stamp_cache;
475 } else {
476 /* Build cache */
477 l = g_slist_copy((GSList *) selection->itemList());
478 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
479 _stamp_cache = l;
480 }
482 while (l) {
483 SPItem *original_item = SP_ITEM(l->data);
484 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
486 // remember the position of the item
487 gint pos = original_repr->position();
488 // remember parent
489 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
491 Inkscape::XML::Node *copy_repr = original_repr->duplicate(parent->document());
493 // add the new repr to the parent
494 parent->appendChild(copy_repr);
495 // move to the saved position
496 copy_repr->setPosition(pos > 0 ? pos : 0);
498 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
500 NR::Matrix const *new_affine;
501 if (_show == SHOW_OUTLINE) {
502 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
503 NR::Matrix const i2dnew( i2d * _current );
504 sp_item_set_i2d_affine(copy_item, i2dnew);
505 new_affine = ©_item->transform;
506 } else {
507 new_affine = &original_item->transform;
508 }
510 sp_item_write_transform(copy_item, copy_repr, *new_affine);
512 if ( copy_item->isCenterSet() && _center ) {
513 copy_item->setCenter(*_center * _current);
514 }
516 Inkscape::GC::release(copy_repr);
517 l = l->next;
518 }
519 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
520 _("Stamp"));
521 }
523 if ( fixup && _stamp_cache ) {
524 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
525 g_slist_free(_stamp_cache);
526 _stamp_cache = NULL;
527 }
528 }
530 void Inkscape::SelTrans::_updateHandles()
531 {
532 if ( !_show_handles || _empty )
533 {
534 sp_remove_handles(_shandle, 8);
535 sp_remove_handles(_rhandle, 8);
536 sp_remove_handles(&_chandle, 1);
537 return;
538 }
540 // center handle
541 if ( _chandle == NULL ) {
542 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
544 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
545 _chandle->setSize (13);
546 _chandle->setAnchor (handle_center.anchor);
547 _chandle->setMode (SP_CTRL_MODE_XOR);
548 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
549 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
550 _chandle->setPixbuf(handles[handle_center.control]);
551 sp_knot_update_ctrl(_chandle);
553 g_signal_connect(G_OBJECT(_chandle), "request",
554 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
555 g_signal_connect(G_OBJECT(_chandle), "moved",
556 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
557 g_signal_connect(G_OBJECT(_chandle), "grabbed",
558 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
559 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
560 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
561 g_signal_connect(G_OBJECT(_chandle), "clicked",
562 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
563 }
565 sp_remove_handles(&_chandle, 1);
566 if ( _state == STATE_SCALE ) {
567 sp_remove_handles(_rhandle, 8);
568 _showHandles(_shandle, handles_scale, 8,
569 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
570 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
571 } else {
572 sp_remove_handles(_shandle, 8);
573 _showHandles(_rhandle, handles_rotate, 8,
574 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
575 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
576 }
578 if (!_center_is_set) {
579 _center = _desktop->selection->center();
580 _center_is_set = true;
581 }
583 if ( _state == STATE_SCALE || !_center ) {
584 sp_knot_hide(_chandle);
585 } else {
586 sp_knot_show(_chandle);
587 sp_knot_moveto(_chandle, &*_center);
588 }
589 }
591 void Inkscape::SelTrans::_updateVolatileState()
592 {
593 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
594 _empty = selection->isEmpty();
596 if (_empty) {
597 return;
598 }
600 //Update the bboxes
601 _bbox = selection->bounds(_snap_bbox_type);
602 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX);
604 if (!_bbox) {
605 _empty = true;
606 return;
607 }
609 _strokewidth = stroke_average_width (selection->itemList());
610 }
612 static void sp_remove_handles(SPKnot *knot[], gint num)
613 {
614 for (int i = 0; i < num; i++) {
615 if (knot[i] != NULL) {
616 sp_knot_hide(knot[i]);
617 }
618 }
619 }
621 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
622 gchar const *even_tip, gchar const *odd_tip)
623 {
624 g_return_if_fail( !_empty );
626 for (int i = 0; i < num; i++) {
627 if (knot[i] == NULL) {
628 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
630 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
631 knot[i]->setSize (13);
632 knot[i]->setAnchor (handle[i].anchor);
633 knot[i]->setMode (SP_CTRL_MODE_XOR);
634 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
635 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
636 knot[i]->setPixbuf(handles[handle[i].control]);
637 sp_knot_update_ctrl(knot[i]);
639 g_signal_connect(G_OBJECT(knot[i]), "request",
640 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
641 g_signal_connect(G_OBJECT(knot[i]), "moved",
642 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
643 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
644 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
645 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
646 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
647 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
648 }
649 sp_knot_show(knot[i]);
651 NR::Point const handle_pt(handle[i].x, handle[i].y);
652 // shouldn't have nullary bbox, but knots
653 g_assert(_bbox);
654 NR::Point p( _bbox->min()
655 + ( _bbox->dimensions()
656 * NR::scale(handle_pt) ) );
658 sp_knot_moveto(knot[i], &p);
659 }
660 }
662 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
663 {
664 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
665 knot, state, *(SPSelTransHandle const *) data
666 );
667 }
669 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint /*state*/, gpointer /*data*/)
670 {
671 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
672 }
674 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
675 {
676 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
677 knot, position, state, *(SPSelTransHandle const *) data
678 );
679 }
681 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
682 {
683 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
684 knot, position, state, *(SPSelTransHandle const *) data
685 );
686 }
688 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
689 {
690 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
691 knot, state, *(SPSelTransHandle const *) data
692 );
693 }
695 void Inkscape::SelTrans::handleClick(SPKnot */*knot*/, guint state, SPSelTransHandle const &handle)
696 {
697 switch (handle.anchor) {
698 case GTK_ANCHOR_CENTER:
699 if (state & GDK_SHIFT_MASK) {
700 // Unset the center position for all selected items
701 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
702 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
703 it->unsetCenter();
704 SP_OBJECT(it)->updateRepr();
705 _center_is_set = false; // center has changed
706 _updateHandles();
707 }
708 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
709 _("Reset center"));
710 }
711 break;
712 default:
713 break;
714 }
715 }
717 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint /*state*/, SPSelTransHandle const &handle)
718 {
719 switch (handle.anchor) {
720 case GTK_ANCHOR_CENTER:
721 g_object_set(G_OBJECT(_grip),
722 "shape", SP_CTRL_SHAPE_BITMAP,
723 "size", 13.0,
724 NULL);
725 sp_canvas_item_show(_grip);
726 break;
727 default:
728 g_object_set(G_OBJECT(_grip),
729 "shape", SP_CTRL_SHAPE_CROSS,
730 "size", 7.0,
731 NULL);
732 sp_canvas_item_show(_norm);
733 sp_canvas_item_show(_grip);
735 break;
736 }
738 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
739 }
742 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
743 {
744 if (!SP_KNOT_IS_GRABBED(knot)) {
745 return;
746 }
748 // in case items have been unhooked from the document, don't
749 // try to continue processing events for them.
750 for (unsigned int i = 0; i < _items.size(); i++) {
751 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
752 return;
753 }
754 }
756 handle.action(this, handle, *position, state);
757 }
760 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
761 {
762 if (!SP_KNOT_IS_GRABBED(knot)) {
763 return TRUE;
764 }
766 knot->desktop->setPosition(*position);
768 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
769 _origin = _opposite;
770 _origin_for_bboxpoints = _opposite_for_bboxpoints;
771 _origin_for_specpoints = _opposite_for_specpoints;
772 } else if (_center) {
773 _origin = *_center;
774 _origin_for_bboxpoints = *_center;
775 _origin_for_specpoints = *_center;
776 } else {
777 // FIXME
778 return TRUE;
779 }
780 if (handle.request(this, handle, *position, state)) {
781 sp_knot_set_position(knot, position, state);
782 SP_CTRL(_grip)->moveto(*position);
783 SP_CTRL(_norm)->moveto(_origin);
784 }
786 return TRUE;
787 }
790 void Inkscape::SelTrans::_selChanged(Inkscape::Selection */*selection*/)
791 {
792 if (!_grabbed) {
793 // reread in case it changed on the fly:
794 gchar const *prefs_bbox = prefs_get_string_attribute("tools", "bounding_box");
795 _snap_bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
796 //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
798 _updateVolatileState();
799 _current.set_identity();
800 _center_is_set = false; // center(s) may have changed
801 _updateHandles();
802 }
803 }
805 void Inkscape::SelTrans::_selModified(Inkscape::Selection */*selection*/, guint /*flags*/)
806 {
807 if (!_grabbed) {
808 _updateVolatileState();
809 _current.set_identity();
811 // reset internal flag
812 _changed = false;
814 _center_is_set = false; // center(s) may have changed
816 _updateHandles();
817 }
818 }
820 /*
821 * handlers for handle move-request
822 */
824 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
825 static double sign(double const x)
826 {
827 return ( x < 0
828 ? -1
829 : 1 );
830 }
832 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
833 SPSelTransHandle const &, NR::Point &pt, guint state)
834 {
835 return seltrans->scaleRequest(pt, state);
836 }
838 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
839 SPSelTransHandle const &handle, NR::Point &pt, guint state)
840 {
841 return seltrans->stretchRequest(handle, pt, state);
842 }
844 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
845 SPSelTransHandle const &handle, NR::Point &pt, guint state)
846 {
847 return seltrans->skewRequest(handle, pt, state);
848 }
850 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
851 SPSelTransHandle const &, NR::Point &pt, guint state)
852 {
853 return seltrans->rotateRequest(pt, state);
854 }
856 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
857 SPSelTransHandle const &, NR::Point &pt, guint state)
858 {
859 return seltrans->centerRequest(pt, state);
860 }
862 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
863 {
864 using NR::X;
865 using NR::Y;
867 NR::Point d = _point - _origin;
868 NR::scale s(0, 0);
870 /* Work out the new scale factors `s' */
871 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
872 if ( fabs(d[i]) > 0.001 ) {
873 s[i] = ( pt[i] - _origin[i] ) / d[i];
874 if ( fabs(s[i]) < 1e-9 ) {
875 s[i] = 1e-9;
876 }
877 }
878 }
880 if (state & GDK_MOD1_MASK) { // scale by an integer multiplier/divider
881 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
882 if (fabs(s[i]) > 1)
883 s[i] = round(s[i]);
884 else
885 s[i] = 1/round(1/(MIN(s[i], 10)));
886 }
887 }
889 SnapManager const &m = _desktop->namedview->snap_manager;
891 /* Get a STL list of the selected items.
892 ** FIXME: this should probably be done by Inkscape::Selection.
893 */
894 std::list<SPItem const*> it;
895 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
896 it.push_back(reinterpret_cast<SPItem*>(i->data));
897 }
899 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
900 // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
901 //
902 // The aspect-ratio must be locked before snapping
903 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
904 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
905 } else {
906 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
907 }
909 // Snap along a suitable constraint vector from the origin.
910 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::SNAPPOINT_BBOX,
911 _bbox_points,
912 it,
913 s,
914 _origin_for_bboxpoints);
916 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAPPOINT_NODE,
917 _snap_points,
918 it,
919 s,
920 _origin_for_specpoints);
922 if (bb.second || sn.second) { // If we snapped to something
923 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
924 ** just compare difference in s[X].
925 */
926 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
927 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
928 s = (bd < sd) ? bb.first : sn.first;
929 }
931 } else {
932 /* Scale aspect ratio is unlocked */
934 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::SNAPPOINT_BBOX,
935 _bbox_points,
936 it,
937 s,
938 _origin_for_bboxpoints);
939 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAPPOINT_NODE,
940 _snap_points,
941 it,
942 s,
943 _origin_for_specpoints);
945 if (bb.second || sn.second) { // If we snapped to something
946 /* Pick the snap that puts us closest to the original scale */
947 NR::Coord bd = bb.second ?
948 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
949 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
950 : NR_HUGE;
951 NR::Coord sd = sn.second ?
952 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
953 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
954 : NR_HUGE;
955 s = (bd < sd) ? bb.first : sn.first;
956 }
957 }
959 /* Update the knot position */
960 pt = ( _point - _origin ) * s + _origin;
962 /* Status text */
963 _message_context.setF(Inkscape::NORMAL_MESSAGE,
964 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
965 100 * s[NR::X], 100 * s[NR::Y]);
967 return TRUE;
968 }
970 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
971 {
972 using NR::X;
973 using NR::Y;
975 NR::Dim2 axis, perp;
977 switch (handle.cursor) {
978 case GDK_TOP_SIDE:
979 case GDK_BOTTOM_SIDE:
980 axis = NR::Y;
981 perp = NR::X;
982 break;
983 case GDK_LEFT_SIDE:
984 case GDK_RIGHT_SIDE:
985 axis = NR::X;
986 perp = NR::Y;
987 break;
988 default:
989 g_assert_not_reached();
990 return TRUE;
991 };
993 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
994 return FALSE;
995 }
997 NR::scale s(1, 1);
998 s[axis] = ( ( pt[axis] - _origin[axis] )
999 / ( _point[axis] - _origin[axis] ) );
1000 if ( fabs(s[axis]) < 1e-15 ) {
1001 s[axis] = 1e-15;
1002 }
1004 if (state & GDK_MOD1_MASK) { // scale by an integer multiplier/divider
1005 if (fabs(s[axis]) > 1)
1006 s[axis] = round(s[axis]);
1007 else
1008 s[axis] = 1/round(1/(MIN(s[axis], 10)));
1009 }
1011 /* Get a STL list of the selected items.
1012 ** FIXME: this should probably be done by Inkscape::Selection.
1013 */
1014 std::list<SPItem const*> it;
1015 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1016 it.push_back(reinterpret_cast<SPItem*>(i->data));
1017 }
1019 SnapManager const &m = _desktop->namedview->snap_manager;
1021 if ( state & GDK_CONTROL_MASK ) {
1022 // on ctrl, apply symmetrical scaling instead of stretching
1023 s[perp] = fabs(s[axis]);
1025 std::pair<NR::Coord, bool> const bb = m.constrainedSnapStretch(
1026 Snapper::SNAPPOINT_BBOX,
1027 _bbox_points,
1028 it,
1029 s[axis],
1030 _origin_for_bboxpoints,
1031 axis,
1032 true);
1034 std::pair<NR::Coord, bool> const sn = m.constrainedSnapStretch(
1035 Snapper::SNAPPOINT_NODE,
1036 _snap_points,
1037 it,
1038 s[axis],
1039 _origin_for_specpoints,
1040 axis,
1041 true);
1043 if (bb.second || sn.second) { // If we snapped to something
1044 /* Choose the smaller difference in scale */
1045 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1046 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1047 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
1049 if (fabs(ratio) < NR_HUGE) {
1050 s[axis] = fabs(ratio) * sign(s[axis]);
1051 }
1052 s[perp] = fabs(s[axis]);
1053 }
1054 } else {
1056 std::pair<NR::Coord, bool> const bb = m.constrainedSnapStretch(
1057 Snapper::SNAPPOINT_BBOX,
1058 _bbox_points,
1059 it,
1060 s[axis],
1061 _origin_for_bboxpoints,
1062 axis,
1063 false);
1065 std::pair<NR::Coord, bool> const sn = m.constrainedSnapStretch(
1066 Snapper::SNAPPOINT_NODE,
1067 _snap_points,
1068 it,
1069 s[axis],
1070 _origin_for_specpoints,
1071 axis,
1072 false);
1074 if (bb.second || sn.second) { // If we snapped to something
1075 /* Choose the smaller difference in scale */
1076 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1077 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1078 NR::Coord const nw = (bd < sd) ? bb.first : sn.first; // new stretch scale
1079 if (fabs(nw) < NR_HUGE) {
1080 s[axis] = nw;
1081 }
1082 s[perp] = 1;
1083 }
1084 }
1086 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1087 if (isNaN(pt[X] + pt[Y])) {
1088 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1089 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1090 }
1092 // status text
1093 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1094 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1095 100 * s[NR::X], 100 * s[NR::Y]);
1097 return TRUE;
1098 }
1100 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1101 {
1102 using NR::X;
1103 using NR::Y;
1105 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1106 return FALSE;
1107 }
1109 NR::Dim2 dim_a;
1110 NR::Dim2 dim_b;
1111 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1112 dim_a = X;
1113 dim_b = Y;
1114 } else {
1115 dim_a = Y;
1116 dim_b = X;
1117 }
1119 double skew[2];
1120 double s[2] = { 1.0, 1.0 };
1122 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1123 return FALSE;
1124 }
1126 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1128 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1130 if ( fabs(s[dim_a]) < 1 ) {
1131 s[dim_a] = sign(s[dim_a]);
1132 } else {
1133 s[dim_a] = floor( s[dim_a] + 0.5 );
1134 }
1136 double radians = atan(skew[dim_a] / s[dim_a]);
1138 if (state & GDK_CONTROL_MASK) {
1140 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1142 if (snaps) {
1143 double sections = floor( radians * snaps / M_PI + .5 );
1144 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1145 radians = ( M_PI / snaps ) * sections;
1146 }
1147 skew[dim_a] = tan(radians) * s[dim_a];
1148 } else {
1149 /* Get a STL list of the selected items.
1150 ** FIXME: this should probably be done by Inkscape::Selection.
1151 */
1152 std::list<SPItem const*> it;
1153 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1154 it.push_back(reinterpret_cast<SPItem*>(i->data));
1155 }
1157 SnapManager const &m = _desktop->namedview->snap_manager;
1159 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_BBOX,
1160 _bbox_points,
1161 it,
1162 skew[dim_a],
1163 _origin_for_bboxpoints,
1164 dim_b);
1166 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE,
1167 _snap_points,
1168 it,
1169 skew[dim_a],
1170 _origin_for_specpoints,
1171 dim_b);
1173 if (bb.second || sn.second) {
1174 /* We snapped something, so change the skew to reflect it */
1175 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1176 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1177 skew[dim_a] = std::min(bd, sd);
1178 }
1179 }
1181 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1182 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1184 /* status text */
1185 double degrees = 180 / M_PI * radians;
1186 if (degrees > 180) degrees -= 360;
1187 if (degrees < -180) degrees += 360;
1189 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1190 // TRANSLATORS: don't modify the first ";"
1191 // (it will NOT be displayed as ";" - only the second one will be)
1192 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1193 degrees);
1195 return TRUE;
1196 }
1198 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1199 {
1200 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1202 // rotate affine in rotate
1203 NR::Point const d1 = _point - _origin;
1204 NR::Point const d2 = pt - _origin;
1206 NR::Coord const h1 = NR::L2(d1);
1207 if (h1 < 1e-15) return FALSE;
1208 NR::Point q1 = d1 / h1;
1209 NR::Coord const h2 = NR::L2(d2);
1210 if (fabs(h2) < 1e-15) return FALSE;
1211 NR::Point q2 = d2 / h2;
1213 double radians;
1214 if (state & GDK_CONTROL_MASK) {
1215 /* Have to restrict movement. */
1216 double cos_t = NR::dot(q1, q2);
1217 double sin_t = NR::dot(NR::rot90(q1), q2);
1218 radians = atan2(sin_t, cos_t);
1219 if (snaps) {
1220 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1221 }
1222 q1 = NR::Point(1, 0);
1223 q2 = NR::Point(cos(radians), sin(radians));
1224 } else {
1225 radians = atan2(NR::dot(NR::rot90(d1), d2),
1226 NR::dot(d1, d2));
1227 }
1229 NR::rotate const r1(q1);
1230 NR::rotate const r2(q2);
1231 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1233 /* status text */
1234 double degrees = 180 / M_PI * radians;
1235 if (degrees > 180) degrees -= 360;
1236 if (degrees < -180) degrees += 360;
1238 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1239 // TRANSLATORS: don't modify the first ";"
1240 // (it will NOT be displayed as ";" - only the second one will be)
1241 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1243 return TRUE;
1244 }
1246 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1247 {
1248 using NR::X;
1249 using NR::Y;
1251 SnapManager const &m = _desktop->namedview->snap_manager;
1252 pt = m.freeSnap(Snapper::SNAPPOINT_NODE, pt, NULL).getPoint();
1254 if (state & GDK_CONTROL_MASK) {
1255 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1256 pt[Y] = _point[Y];
1257 } else {
1258 pt[X] = _point[X];
1259 }
1260 }
1262 if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
1263 // screen pixels to snap center to bbox
1264 #define SNAP_DIST 5
1265 // FIXME: take from prefs
1266 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1268 for (int i = 0; i < 2; i++) {
1269 if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
1270 pt[i] = _bbox->min()[i];
1271 }
1272 if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
1273 pt[i] = _bbox->midpoint()[i];
1274 }
1275 if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
1276 pt[i] = _bbox->max()[i];
1277 }
1278 }
1279 }
1281 // status text
1282 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1283 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1284 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1285 g_string_free(xs, FALSE);
1286 g_string_free(ys, FALSE);
1288 return TRUE;
1289 }
1291 /*
1292 * handlers for handle movement
1293 *
1294 */
1296 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1297 {
1298 seltrans->stretch(handle, pt, state);
1299 }
1301 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1302 {
1303 seltrans->scale(pt, state);
1304 }
1306 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1307 {
1308 seltrans->skew(handle, pt, state);
1309 }
1311 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1312 {
1313 seltrans->rotate(pt, state);
1314 }
1316 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1317 {
1318 using NR::X;
1319 using NR::Y;
1321 NR::Dim2 dim;
1322 switch (handle.cursor) {
1323 case GDK_LEFT_SIDE:
1324 case GDK_RIGHT_SIDE:
1325 dim = X;
1326 break;
1327 case GDK_TOP_SIDE:
1328 case GDK_BOTTOM_SIDE:
1329 dim = Y;
1330 break;
1331 default:
1332 g_assert_not_reached();
1333 abort();
1334 break;
1335 }
1337 NR::Point const scale_origin(_origin);
1338 double const offset = _point[dim] - scale_origin[dim];
1339 if (!( fabs(offset) >= 1e-15 )) {
1340 return;
1341 }
1342 NR::scale s(1, 1);
1343 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1344 if (isNaN(s[dim])) {
1345 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1346 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1347 }
1348 if (!( fabs(s[dim]) >= 1e-15 )) {
1349 s[dim] = 1e-15;
1350 }
1351 if (state & GDK_CONTROL_MASK) {
1352 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1353 s[!dim] = fabs(s[dim]);
1354 }
1356 if (!_bbox) {
1357 return;
1358 }
1360 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1361 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1363 int transform_stroke = false;
1364 gdouble strokewidth = 0;
1366 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1367 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1368 strokewidth = _strokewidth;
1369 }
1371 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1372 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1374 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1375 }
1377 void Inkscape::SelTrans::scale(NR::Point &pt, guint /*state*/)
1378 {
1379 if (!_bbox) {
1380 return;
1381 }
1383 NR::Point const offset = _point - _origin;
1385 NR::scale s (1, 1);
1386 for (int i = NR::X; i <= NR::Y; i++) {
1387 if (fabs(offset[i]) > 1e-9)
1388 s[i] = (pt[i] - _origin[i]) / offset[i];
1389 if (fabs(s[i]) < 1e-9)
1390 s[i] = 1e-9;
1391 }
1393 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1394 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1396 int transform_stroke = false;
1397 gdouble strokewidth = 0;
1399 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1400 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1401 strokewidth = _strokewidth;
1402 }
1404 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1405 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1407 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1408 }
1410 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint /*state*/)
1411 {
1412 NR::Point const offset = _point - _origin;
1414 unsigned dim;
1415 switch (handle.cursor) {
1416 case GDK_SB_H_DOUBLE_ARROW:
1417 dim = NR::Y;
1418 break;
1419 case GDK_SB_V_DOUBLE_ARROW:
1420 dim = NR::X;
1421 break;
1422 default:
1423 g_assert_not_reached();
1424 abort();
1425 break;
1426 }
1427 if (fabs(offset[dim]) < 1e-15) {
1428 return;
1429 }
1430 NR::Matrix skew = NR::identity();
1431 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1432 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1433 skew[2*(1-dim) + (dim)] = 0;
1434 skew[2*(1-dim) + (1-dim)] = 1;
1436 for (int i = 0; i < 2; i++) {
1437 if (fabs(skew[3*i]) < 1e-15) {
1438 skew[3*i] = 1e-15;
1439 }
1440 }
1441 transform(skew, _origin);
1442 }
1444 void Inkscape::SelTrans::rotate(NR::Point &pt, guint /*state*/)
1445 {
1446 NR::Point const offset = _point - _origin;
1448 NR::Coord const h1 = NR::L2(offset);
1449 if (h1 < 1e-15) {
1450 return;
1451 }
1452 NR::Point const q1 = offset / h1;
1453 NR::Coord const h2 = NR::L2( pt - _origin );
1454 if (h2 < 1e-15) {
1455 return;
1456 }
1457 NR::Point const q2 = (pt - _origin) / h2;
1458 NR::rotate const r1(q1);
1459 NR::rotate const r2(q2);
1461 NR::Matrix rotate( r2 / r1 );
1462 transform(rotate, _origin);
1463 }
1465 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint /*state*/)
1466 {
1467 seltrans->setCenter(pt);
1468 }
1471 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1472 {
1473 SnapManager const &m = _desktop->namedview->snap_manager;
1475 /* The amount that we've moved by during this drag */
1476 NR::Point dxy = xy - _point;
1478 /* Get a STL list of the selected items.
1479 ** FIXME: this should probably be done by Inkscape::Selection.
1480 */
1481 std::list<SPItem const*> it;
1482 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1483 it.push_back(reinterpret_cast<SPItem*>(i->data));
1484 }
1486 bool const alt = (state & GDK_MOD1_MASK);
1487 bool const control = (state & GDK_CONTROL_MASK);
1488 bool const shift = (state & GDK_SHIFT_MASK);
1490 if (alt) {
1492 /* Alt pressed means keep offset: snap the moved distance to the grid.
1493 ** FIXME: this will snap to more than just the grid, nowadays.
1494 */
1496 dxy = m.freeSnap(Snapper::SNAPPOINT_NODE, dxy, NULL).getPoint();
1498 } else if (!shift) {
1500 /* We're snapping to things, possibly with a constraint to horizontal or
1501 ** vertical movement. Obtain a list of possible translations and then
1502 ** pick the smallest.
1503 */
1505 /* This will be our list of possible translations */
1506 std::list<std::pair<NR::Point, bool> > s;
1508 if (control) {
1510 /* Snap to things, and also constrain to horizontal or vertical movement */
1512 for (unsigned int dim = 0; dim < 2; dim++) {
1513 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX,
1514 _bbox_points,
1515 it,
1516 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1517 dxy));
1519 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE,
1520 _snap_points,
1521 it,
1522 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1523 dxy));
1524 }
1526 } else {
1528 // Let's leave this timer code here for a while. I'll probably need it in the near future (Diederik van Lierop)
1529 /* GTimeVal starttime;
1530 GTimeVal endtime;
1531 g_get_current_time(&starttime); */
1533 /* Snap to things with no constraint */
1534 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX,
1535 _bbox_points, it, dxy));
1536 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE,
1537 _snap_points, it, dxy));
1539 /*g_get_current_time(&endtime);
1540 double elapsed = ((((double)endtime.tv_sec - starttime.tv_sec) * G_USEC_PER_SEC + (endtime.tv_usec - starttime.tv_usec))) / 1000.0;
1541 std::cout << "Time spent snapping: " << elapsed << std::endl; */
1542 }
1544 /* Pick one */
1545 NR::Coord best = NR_HUGE;
1546 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1547 if (i->second) {
1548 NR::Coord const m = NR::L2(i->first);
1549 if (m < best) {
1550 best = m;
1551 dxy = i->first;
1552 }
1553 }
1554 }
1555 }
1557 if (control) {
1558 /* Ensure that the horizontal and vertical constraint has been applied */
1559 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1560 dxy[NR::Y] = 0;
1561 } else {
1562 dxy[NR::X] = 0;
1563 }
1564 }
1566 NR::Matrix const move((NR::translate(dxy)));
1567 NR::Point const norm(0, 0);
1568 transform(move, norm);
1570 // status text
1571 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1572 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1573 _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);
1574 g_string_free(xs, TRUE);
1575 g_string_free(ys, TRUE);
1576 }
1579 /*
1580 Local Variables:
1581 mode:c++
1582 c-file-style:"stroustrup"
1583 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1584 indent-tabs-mode:nil
1585 fill-column:99
1586 End:
1587 */
1588 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :