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.select", "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 and the special snappoints, but not 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
270 // Next, get all special points for snapping
271 SnapManager const &m = _desktop->namedview->snap_manager;
272 _snap_points = selection->getSnapPoints(m.getIncludeItemCenter()); // Excludes path nodes
273 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull(); // Includes path nodes
274 if (_snap_points.size() > 100) {
275 /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
276 An average user would rarely ever try to snap such a large number of nodes anyway, because
277 (s)he could hardly discern which node would be snapping */
278 _snap_points = snap_points_hull;
279 // Unfortunately, by now we will have lost the font-baseline snappoints :-(
280 }
282 // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
283 // path nodes, for example because a rectangle which has been converted to a path doesn't have
284 // any other special points
285 NR::Rect snap_points_bbox;
286 if ( snap_points_hull.empty() == false ) {
287 std::vector<NR::Point>::iterator i = snap_points_hull.begin();
288 snap_points_bbox = NR::Rect(*i, *i);
289 i++;
290 while (i != snap_points_hull.end()) {
291 snap_points_bbox.expandTo(*i);
292 i++;
293 }
294 }
296 _bbox_points.clear();
297 if (_bbox) {
298 // ... and add the bbox corners to _bbox_points
299 for ( unsigned i = 0 ; i < 4 ; i++ ) {
300 _bbox_points.push_back(_bbox->corner(i));
301 }
302 // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
303 // - one for snapping the boundingbox, which can be either visual or geometric
304 // - one for snapping the special points
305 // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
306 // These distinct "opposites" are needed in the snapmanager to avoid bugs such as #1540195 (in which
307 // a box is caught between to guides)
308 _opposite_for_bboxpoints = _bbox->min() + _bbox->dimensions() * NR::scale(1-x, 1-y);
309 _opposite_for_specpoints = (snap_points_bbox.min() + (snap_points_bbox.dimensions() * NR::scale(1-x, 1-y) ) );
310 // Only a single "opposite" can be used in calculating transformations.
311 _opposite = _opposite_for_bboxpoints;
312 }
314 // The lines below are usefull for debugging any snapping issues, as they'll spit out all points that are considered for snapping
316 /*std::cout << "Number of snap points: " << _snap_points.size() << std::endl;
317 for (std::vector<NR::Point>::const_iterator i = _snap_points.begin(); i != _snap_points.end(); i++)
318 {
319 std::cout << " " << *i << std::endl;
320 }
322 std::cout << "Number of bbox points: " << _bbox_points.size() << std::endl;
323 for (std::vector<NR::Point>::const_iterator i = _bbox_points.begin(); i != _bbox_points.end(); i++)
324 {
325 std::cout << " " << *i << std::endl;
326 }*/
328 if ((x != -1) && (y != -1)) {
329 sp_canvas_item_show(_norm);
330 sp_canvas_item_show(_grip);
331 }
333 if (_show == SHOW_OUTLINE) {
334 for (int i = 0; i < 4; i++)
335 sp_canvas_item_show(_l[i]);
336 }
338 _updateHandles();
339 g_return_if_fail(_stamp_cache == NULL);
340 }
342 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
343 {
344 g_return_if_fail(_grabbed);
345 g_return_if_fail(!_empty);
347 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
349 if (_show == SHOW_CONTENT) {
350 // update the content
351 for (unsigned i = 0; i < _items.size(); i++) {
352 SPItem &item = *_items[i].first;
353 NR::Matrix const &prev_transform = _items[i].second;
354 sp_item_set_i2d_affine(&item, prev_transform * affine);
355 }
356 } else {
357 if (_bbox) {
358 NR::Point p[4];
359 /* update the outline */
360 for (unsigned i = 0 ; i < 4 ; i++) {
361 p[i] = _bbox->corner(i) * affine;
362 }
363 for (unsigned i = 0 ; i < 4 ; i++) {
364 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
365 }
366 }
367 }
369 _current = affine;
370 _changed = true;
371 _updateHandles();
372 }
374 void Inkscape::SelTrans::ungrab()
375 {
376 g_return_if_fail(_grabbed);
377 _grabbed = false;
378 _show_handles = true;
380 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
381 _updateVolatileState();
383 for (unsigned i = 0; i < _items.size(); i++) {
384 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
385 }
387 sp_canvas_item_hide(_norm);
388 sp_canvas_item_hide(_grip);
390 if (_show == SHOW_OUTLINE) {
391 for (int i = 0; i < 4; i++)
392 sp_canvas_item_hide(_l[i]);
393 }
395 if (_stamp_cache) {
396 g_slist_free(_stamp_cache);
397 _stamp_cache = NULL;
398 }
400 _message_context.clear();
402 if (!_empty && _changed) {
403 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
404 if (_center) {
405 *_center *= _current;
406 _center_is_set = true;
407 }
409 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
410 // appropriately - it does not know the original positions of the centers (all objects already have
411 // the new bboxes). So we need to reset the centers from our saved array.
412 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
413 for (unsigned i = 0; i < _items_centers.size(); i++) {
414 SPItem *currentItem = _items_centers[i].first;
415 if (currentItem->isCenterSet()) { // only if it's already set
416 currentItem->setCenter (_items_centers[i].second * _current);
417 SP_OBJECT(currentItem)->updateRepr();
418 }
419 }
420 }
422 _items.clear();
423 _items_centers.clear();
425 if (_current.is_translation()) {
426 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
427 _("Move"));
428 } else if (_current.is_scale()) {
429 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
430 _("Scale"));
431 } else if (_current.is_rotation()) {
432 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
433 _("Rotate"));
434 } else {
435 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
436 _("Skew"));
437 }
439 } else {
441 if (_center_is_set) {
442 // we were dragging center; update reprs and commit undoable action
443 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
444 SPItem *it = (SPItem*)SP_OBJECT(l->data);
445 SP_OBJECT(it)->updateRepr();
446 }
447 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
448 _("Set center"));
449 }
451 _items.clear();
452 _items_centers.clear();
453 _updateHandles();
454 }
455 }
457 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
458 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
460 void Inkscape::SelTrans::stamp()
461 {
462 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
464 bool fixup = !_grabbed;
465 if ( fixup && _stamp_cache ) {
466 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
467 g_slist_free(_stamp_cache);
468 _stamp_cache = NULL;
469 }
471 /* stamping mode */
472 if (!_empty) {
473 GSList *l;
474 if (_stamp_cache) {
475 l = _stamp_cache;
476 } else {
477 /* Build cache */
478 l = g_slist_copy((GSList *) selection->itemList());
479 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
480 _stamp_cache = l;
481 }
483 while (l) {
484 SPItem *original_item = SP_ITEM(l->data);
485 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
487 // remember the position of the item
488 gint pos = original_repr->position();
489 // remember parent
490 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
492 Inkscape::XML::Node *copy_repr = original_repr->duplicate(parent->document());
494 // add the new repr to the parent
495 parent->appendChild(copy_repr);
496 // move to the saved position
497 copy_repr->setPosition(pos > 0 ? pos : 0);
499 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
501 NR::Matrix const *new_affine;
502 if (_show == SHOW_OUTLINE) {
503 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
504 NR::Matrix const i2dnew( i2d * _current );
505 sp_item_set_i2d_affine(copy_item, i2dnew);
506 new_affine = ©_item->transform;
507 } else {
508 new_affine = &original_item->transform;
509 }
511 sp_item_write_transform(copy_item, copy_repr, *new_affine);
513 if ( copy_item->isCenterSet() && _center ) {
514 copy_item->setCenter(*_center * _current);
515 }
517 Inkscape::GC::release(copy_repr);
518 l = l->next;
519 }
520 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
521 _("Stamp"));
522 }
524 if ( fixup && _stamp_cache ) {
525 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
526 g_slist_free(_stamp_cache);
527 _stamp_cache = NULL;
528 }
529 }
531 void Inkscape::SelTrans::_updateHandles()
532 {
533 if ( !_show_handles || _empty )
534 {
535 sp_remove_handles(_shandle, 8);
536 sp_remove_handles(_rhandle, 8);
537 sp_remove_handles(&_chandle, 1);
538 return;
539 }
541 // center handle
542 if ( _chandle == NULL ) {
543 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
545 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
546 _chandle->setSize (13);
547 _chandle->setAnchor (handle_center.anchor);
548 _chandle->setMode (SP_CTRL_MODE_XOR);
549 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
550 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
551 _chandle->setPixbuf(handles[handle_center.control]);
552 sp_knot_update_ctrl(_chandle);
554 g_signal_connect(G_OBJECT(_chandle), "request",
555 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
556 g_signal_connect(G_OBJECT(_chandle), "moved",
557 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
558 g_signal_connect(G_OBJECT(_chandle), "grabbed",
559 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
560 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
561 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
562 g_signal_connect(G_OBJECT(_chandle), "clicked",
563 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
564 }
566 sp_remove_handles(&_chandle, 1);
567 if ( _state == STATE_SCALE ) {
568 sp_remove_handles(_rhandle, 8);
569 _showHandles(_shandle, handles_scale, 8,
570 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
571 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
572 } else {
573 sp_remove_handles(_shandle, 8);
574 _showHandles(_rhandle, handles_rotate, 8,
575 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
576 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
577 }
579 if (!_center_is_set) {
580 _center = _desktop->selection->center();
581 _center_is_set = true;
582 }
584 if ( _state == STATE_SCALE || !_center ) {
585 sp_knot_hide(_chandle);
586 } else {
587 sp_knot_show(_chandle);
588 sp_knot_moveto(_chandle, &*_center);
589 }
590 }
592 void Inkscape::SelTrans::_updateVolatileState()
593 {
594 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
595 _empty = selection->isEmpty();
597 if (_empty) {
598 return;
599 }
601 //Update the bboxes
602 _bbox = selection->bounds(_snap_bbox_type);
603 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX);
605 if (!_bbox) {
606 _empty = true;
607 return;
608 }
610 _strokewidth = stroke_average_width (selection->itemList());
611 }
613 static void sp_remove_handles(SPKnot *knot[], gint num)
614 {
615 for (int i = 0; i < num; i++) {
616 if (knot[i] != NULL) {
617 sp_knot_hide(knot[i]);
618 }
619 }
620 }
622 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
623 gchar const *even_tip, gchar const *odd_tip)
624 {
625 g_return_if_fail( !_empty );
627 for (int i = 0; i < num; i++) {
628 if (knot[i] == NULL) {
629 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
631 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
632 knot[i]->setSize (13);
633 knot[i]->setAnchor (handle[i].anchor);
634 knot[i]->setMode (SP_CTRL_MODE_XOR);
635 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
636 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
637 knot[i]->setPixbuf(handles[handle[i].control]);
638 sp_knot_update_ctrl(knot[i]);
640 g_signal_connect(G_OBJECT(knot[i]), "request",
641 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
642 g_signal_connect(G_OBJECT(knot[i]), "moved",
643 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
644 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
645 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
646 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
647 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
648 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
649 }
650 sp_knot_show(knot[i]);
652 NR::Point const handle_pt(handle[i].x, handle[i].y);
653 // shouldn't have nullary bbox, but knots
654 g_assert(_bbox);
655 NR::Point p( _bbox->min()
656 + ( _bbox->dimensions()
657 * NR::scale(handle_pt) ) );
659 sp_knot_moveto(knot[i], &p);
660 }
661 }
663 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
664 {
665 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
666 knot, state, *(SPSelTransHandle const *) data
667 );
668 }
670 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
671 {
672 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
673 }
675 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
676 {
677 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
678 knot, position, state, *(SPSelTransHandle const *) data
679 );
680 }
682 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
683 {
684 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
685 knot, position, state, *(SPSelTransHandle const *) data
686 );
687 }
689 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
690 {
691 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
692 knot, state, *(SPSelTransHandle const *) data
693 );
694 }
696 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
697 {
698 switch (handle.anchor) {
699 case GTK_ANCHOR_CENTER:
700 if (state & GDK_SHIFT_MASK) {
701 // Unset the center position for all selected items
702 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
703 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
704 it->unsetCenter();
705 SP_OBJECT(it)->updateRepr();
706 _center_is_set = false; // center has changed
707 _updateHandles();
708 }
709 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
710 _("Reset center"));
711 }
712 break;
713 default:
714 break;
715 }
716 }
718 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
719 {
720 switch (handle.anchor) {
721 case GTK_ANCHOR_CENTER:
722 g_object_set(G_OBJECT(_grip),
723 "shape", SP_CTRL_SHAPE_BITMAP,
724 "size", 13.0,
725 NULL);
726 sp_canvas_item_show(_grip);
727 break;
728 default:
729 g_object_set(G_OBJECT(_grip),
730 "shape", SP_CTRL_SHAPE_CROSS,
731 "size", 7.0,
732 NULL);
733 sp_canvas_item_show(_norm);
734 sp_canvas_item_show(_grip);
736 break;
737 }
739 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
740 }
743 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
744 {
745 if (!SP_KNOT_IS_GRABBED(knot)) {
746 return;
747 }
749 // in case items have been unhooked from the document, don't
750 // try to continue processing events for them.
751 for (unsigned int i = 0; i < _items.size(); i++) {
752 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
753 return;
754 }
755 }
757 handle.action(this, handle, *position, state);
758 }
761 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
762 {
763 if (!SP_KNOT_IS_GRABBED(knot)) {
764 return TRUE;
765 }
767 knot->desktop->setPosition(*position);
769 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
770 _origin = _opposite;
771 _origin_for_bboxpoints = _opposite_for_bboxpoints;
772 _origin_for_specpoints = _opposite_for_specpoints;
773 } else if (_center) {
774 _origin = *_center;
775 _origin_for_bboxpoints = *_center;
776 _origin_for_specpoints = *_center;
777 } else {
778 // FIXME
779 return TRUE;
780 }
781 if (handle.request(this, handle, *position, state)) {
782 sp_knot_set_position(knot, position, state);
783 SP_CTRL(_grip)->moveto(*position);
784 SP_CTRL(_norm)->moveto(_origin);
785 }
787 return TRUE;
788 }
791 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
792 {
793 if (!_grabbed) {
794 // reread in case it changed on the fly:
795 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
796 _snap_bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
797 //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
799 _updateVolatileState();
800 _current.set_identity();
801 _center_is_set = false; // center(s) may have changed
802 _updateHandles();
803 }
804 }
806 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
807 {
808 if (!_grabbed) {
809 _updateVolatileState();
810 _current.set_identity();
812 // reset internal flag
813 _changed = false;
815 _center_is_set = false; // center(s) may have changed
817 _updateHandles();
818 }
819 }
821 /*
822 * handlers for handle move-request
823 */
825 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
826 static double sign(double const x)
827 {
828 return ( x < 0
829 ? -1
830 : 1 );
831 }
833 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
834 SPSelTransHandle const &, NR::Point &pt, guint state)
835 {
836 return seltrans->scaleRequest(pt, state);
837 }
839 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
840 SPSelTransHandle const &handle, NR::Point &pt, guint state)
841 {
842 return seltrans->stretchRequest(handle, pt, state);
843 }
845 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
846 SPSelTransHandle const &handle, NR::Point &pt, guint state)
847 {
848 return seltrans->skewRequest(handle, pt, state);
849 }
851 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
852 SPSelTransHandle const &, NR::Point &pt, guint state)
853 {
854 return seltrans->rotateRequest(pt, state);
855 }
857 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
858 SPSelTransHandle const &, NR::Point &pt, guint state)
859 {
860 return seltrans->centerRequest(pt, state);
861 }
863 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
864 {
865 using NR::X;
866 using NR::Y;
868 NR::Point d = _point - _origin;
869 NR::scale s(0, 0);
871 /* Work out the new scale factors `s' */
872 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
873 if ( fabs(d[i]) > 0.001 ) {
874 s[i] = ( pt[i] - _origin[i] ) / d[i];
875 if ( fabs(s[i]) < 1e-9 ) {
876 s[i] = 1e-9;
877 }
878 }
879 }
881 if (state & GDK_MOD1_MASK) { // scale by an integer multiplier/divider
882 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
883 if (fabs(s[i]) > 1)
884 s[i] = round(s[i]);
885 else
886 s[i] = 1/round(1/(MIN(s[i], 10)));
887 }
888 }
890 SnapManager const &m = _desktop->namedview->snap_manager;
892 /* Get a STL list of the selected items.
893 ** FIXME: this should probably be done by Inkscape::Selection.
894 */
895 std::list<SPItem const*> it;
896 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
897 it.push_back(reinterpret_cast<SPItem*>(i->data));
898 }
900 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
901 // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
902 //
903 // The aspect-ratio must be locked before snapping
904 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
905 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
906 } else {
907 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
908 }
910 // Snap along a suitable constraint vector from the origin.
912 // The inclination of the constraint vector is calculated from the aspect ratio
913 NR::Point bbox_dim = _bbox->dimensions();
914 double const aspect_ratio = bbox_dim[1] / bbox_dim[0]; // = height / width
916 // Determine direction of the constraint vector
917 NR::Point const cv = NR::Point(
918 pt[NR::X] > _origin[NR::X] ? 1 : -1,
919 pt[NR::Y] > _origin[NR::Y] ? aspect_ratio : -aspect_ratio
920 );
922 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::SNAPPOINT_BBOX,
923 _bbox_points,
924 it,
925 Snapper::ConstraintLine(_origin_for_bboxpoints, cv),
926 s,
927 _origin_for_bboxpoints);
929 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAPPOINT_NODE,
930 _snap_points,
931 it,
932 Snapper::ConstraintLine(_origin_for_specpoints, cv),
933 s,
934 _origin_for_specpoints);
936 if (bb.second == false && sn.second == false) {
937 /* We didn't snap, so just keep the locked aspect ratio */
938 } else {
939 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
940 ** just compare difference in s[X].
941 */
942 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
943 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
944 s = (bd < sd) ? bb.first : sn.first;
945 }
947 } else {
948 /* Scale aspect ratio is unlocked */
950 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::SNAPPOINT_BBOX,
951 _bbox_points,
952 it,
953 s,
954 _origin_for_bboxpoints);
955 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAPPOINT_NODE,
956 _snap_points,
957 it,
958 s,
959 _origin_for_specpoints);
961 /* Pick the snap that puts us closest to the original scale */
962 NR::Coord bd = bb.second ?
963 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
964 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
965 : NR_HUGE;
966 NR::Coord sd = sn.second ?
967 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
968 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
969 : NR_HUGE;
970 s = (bd < sd) ? bb.first : sn.first;
971 }
973 /* Update the knot position */
974 pt = ( _point - _origin ) * s + _origin;
976 /* Status text */
977 _message_context.setF(Inkscape::NORMAL_MESSAGE,
978 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
979 100 * s[NR::X], 100 * s[NR::Y]);
981 return TRUE;
982 }
984 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
985 {
986 using NR::X;
987 using NR::Y;
989 NR::Dim2 axis, perp;
991 switch (handle.cursor) {
992 case GDK_TOP_SIDE:
993 case GDK_BOTTOM_SIDE:
994 axis = NR::Y;
995 perp = NR::X;
996 break;
997 case GDK_LEFT_SIDE:
998 case GDK_RIGHT_SIDE:
999 axis = NR::X;
1000 perp = NR::Y;
1001 break;
1002 default:
1003 g_assert_not_reached();
1004 return TRUE;
1005 };
1007 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
1008 return FALSE;
1009 }
1011 NR::scale s(1, 1);
1012 s[axis] = ( ( pt[axis] - _origin[axis] )
1013 / ( _point[axis] - _origin[axis] ) );
1014 if ( fabs(s[axis]) < 1e-15 ) {
1015 s[axis] = 1e-15;
1016 }
1018 if (state & GDK_MOD1_MASK) { // scale by an integer multiplier/divider
1019 if (fabs(s[axis]) > 1)
1020 s[axis] = round(s[axis]);
1021 else
1022 s[axis] = 1/round(1/(MIN(s[axis], 10)));
1023 }
1025 /* Get a STL list of the selected items.
1026 ** FIXME: this should probably be done by Inkscape::Selection.
1027 */
1028 std::list<SPItem const*> it;
1029 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1030 it.push_back(reinterpret_cast<SPItem*>(i->data));
1031 }
1033 SnapManager const &m = _desktop->namedview->snap_manager;
1035 if ( state & GDK_CONTROL_MASK ) {
1036 // on ctrl, apply symmetrical scaling instead of stretching
1037 s[perp] = fabs(s[axis]);
1039 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1040 Snapper::SNAPPOINT_BBOX,
1041 _bbox_points,
1042 it,
1043 s[axis],
1044 _origin_for_bboxpoints,
1045 axis,
1046 true);
1048 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1049 Snapper::SNAPPOINT_NODE,
1050 _snap_points,
1051 it,
1052 s[axis],
1053 _origin_for_specpoints,
1054 axis,
1055 true);
1057 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1058 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1059 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
1061 s[axis] = fabs(ratio) * sign(s[axis]);
1062 s[perp] = fabs(s[axis]);
1063 } else {
1065 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1066 Snapper::SNAPPOINT_BBOX,
1067 _bbox_points,
1068 it,
1069 s[axis],
1070 _origin_for_bboxpoints,
1071 axis,
1072 false);
1074 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1075 Snapper::SNAPPOINT_NODE,
1076 _snap_points,
1077 it,
1078 s[axis],
1079 _origin_for_specpoints,
1080 axis,
1081 false);
1083 /* Choose the smaller difference in scale */
1084 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1085 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1086 s[axis] = (bd < sd) ? bb.first : sn.first;
1087 s[perp] = 1;
1088 }
1090 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1091 if (isNaN(pt[X] + pt[Y])) {
1092 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1093 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1094 }
1096 // status text
1097 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1098 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1099 100 * s[NR::X], 100 * s[NR::Y]);
1101 return TRUE;
1102 }
1104 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1105 {
1106 using NR::X;
1107 using NR::Y;
1109 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1110 return FALSE;
1111 }
1113 NR::Dim2 dim_a;
1114 NR::Dim2 dim_b;
1115 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1116 dim_a = X;
1117 dim_b = Y;
1118 } else {
1119 dim_a = Y;
1120 dim_b = X;
1121 }
1123 double skew[2];
1124 double s[2] = { 1.0, 1.0 };
1126 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1127 return FALSE;
1128 }
1130 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1132 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1134 if ( fabs(s[dim_a]) < 1 ) {
1135 s[dim_a] = sign(s[dim_a]);
1136 } else {
1137 s[dim_a] = floor( s[dim_a] + 0.5 );
1138 }
1140 double radians = atan(skew[dim_a] / s[dim_a]);
1142 if (state & GDK_CONTROL_MASK) {
1144 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1146 if (snaps) {
1147 double sections = floor( radians * snaps / M_PI + .5 );
1148 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1149 radians = ( M_PI / snaps ) * sections;
1150 }
1151 skew[dim_a] = tan(radians) * s[dim_a];
1152 } else {
1153 SnapManager const &m = _desktop->namedview->snap_manager;
1155 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_BBOX,
1156 _bbox_points,
1157 std::list<SPItem const *>(),
1158 skew[dim_a],
1159 _origin_for_bboxpoints,
1160 dim_b);
1162 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE,
1163 _snap_points,
1164 std::list<SPItem const *>(),
1165 skew[dim_a],
1166 _origin_for_specpoints,
1167 dim_b);
1169 if (bb.second || sn.second) {
1170 /* We snapped something, so change the skew to reflect it */
1171 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1172 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1173 skew[dim_a] = std::min(bd, sd);
1174 }
1175 }
1177 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1178 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1180 /* status text */
1181 double degrees = 180 / M_PI * radians;
1182 if (degrees > 180) degrees -= 360;
1183 if (degrees < -180) degrees += 360;
1185 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1186 // TRANSLATORS: don't modify the first ";"
1187 // (it will NOT be displayed as ";" - only the second one will be)
1188 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1189 degrees);
1191 return TRUE;
1192 }
1194 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1195 {
1196 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1198 // rotate affine in rotate
1199 NR::Point const d1 = _point - _origin;
1200 NR::Point const d2 = pt - _origin;
1202 NR::Coord const h1 = NR::L2(d1);
1203 if (h1 < 1e-15) return FALSE;
1204 NR::Point q1 = d1 / h1;
1205 NR::Coord const h2 = NR::L2(d2);
1206 if (fabs(h2) < 1e-15) return FALSE;
1207 NR::Point q2 = d2 / h2;
1209 double radians;
1210 if (state & GDK_CONTROL_MASK) {
1211 /* Have to restrict movement. */
1212 double cos_t = NR::dot(q1, q2);
1213 double sin_t = NR::dot(NR::rot90(q1), q2);
1214 radians = atan2(sin_t, cos_t);
1215 if (snaps) {
1216 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1217 }
1218 q1 = NR::Point(1, 0);
1219 q2 = NR::Point(cos(radians), sin(radians));
1220 } else {
1221 radians = atan2(NR::dot(NR::rot90(d1), d2),
1222 NR::dot(d1, d2));
1223 }
1225 NR::rotate const r1(q1);
1226 NR::rotate const r2(q2);
1227 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1229 /* status text */
1230 double degrees = 180 / M_PI * radians;
1231 if (degrees > 180) degrees -= 360;
1232 if (degrees < -180) degrees += 360;
1234 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1235 // TRANSLATORS: don't modify the first ";"
1236 // (it will NOT be displayed as ";" - only the second one will be)
1237 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1239 return TRUE;
1240 }
1242 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1243 {
1244 using NR::X;
1245 using NR::Y;
1247 SnapManager const &m = _desktop->namedview->snap_manager;
1248 pt = m.freeSnap(Snapper::SNAPPOINT_NODE, pt, NULL).getPoint();
1250 if (state & GDK_CONTROL_MASK) {
1251 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1252 pt[Y] = _point[Y];
1253 } else {
1254 pt[X] = _point[X];
1255 }
1256 }
1258 if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
1259 // screen pixels to snap center to bbox
1260 #define SNAP_DIST 5
1261 // FIXME: take from prefs
1262 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1264 for (int i = 0; i < 2; i++) {
1265 if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
1266 pt[i] = _bbox->min()[i];
1267 }
1268 if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
1269 pt[i] = _bbox->midpoint()[i];
1270 }
1271 if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
1272 pt[i] = _bbox->max()[i];
1273 }
1274 }
1275 }
1277 // status text
1278 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1279 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1280 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1281 g_string_free(xs, FALSE);
1282 g_string_free(ys, FALSE);
1284 return TRUE;
1285 }
1287 /*
1288 * handlers for handle movement
1289 *
1290 */
1292 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1293 {
1294 seltrans->stretch(handle, pt, state);
1295 }
1297 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1298 {
1299 seltrans->scale(pt, state);
1300 }
1302 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1303 {
1304 seltrans->skew(handle, pt, state);
1305 }
1307 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1308 {
1309 seltrans->rotate(pt, state);
1310 }
1312 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1313 {
1314 using NR::X;
1315 using NR::Y;
1317 NR::Dim2 dim;
1318 switch (handle.cursor) {
1319 case GDK_LEFT_SIDE:
1320 case GDK_RIGHT_SIDE:
1321 dim = X;
1322 break;
1323 case GDK_TOP_SIDE:
1324 case GDK_BOTTOM_SIDE:
1325 dim = Y;
1326 break;
1327 default:
1328 g_assert_not_reached();
1329 abort();
1330 break;
1331 }
1333 NR::Point const scale_origin(_origin);
1334 double const offset = _point[dim] - scale_origin[dim];
1335 if (!( fabs(offset) >= 1e-15 )) {
1336 return;
1337 }
1338 NR::scale s(1, 1);
1339 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1340 if (isNaN(s[dim])) {
1341 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1342 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1343 }
1344 if (!( fabs(s[dim]) >= 1e-15 )) {
1345 s[dim] = 1e-15;
1346 }
1347 if (state & GDK_CONTROL_MASK) {
1348 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1349 s[!dim] = fabs(s[dim]);
1350 }
1352 if (!_bbox) {
1353 return;
1354 }
1356 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1357 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1359 int transform_stroke = false;
1360 gdouble strokewidth = 0;
1362 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1363 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1364 strokewidth = _strokewidth;
1365 }
1367 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1368 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1370 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1371 }
1373 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1374 {
1375 if (!_bbox) {
1376 return;
1377 }
1379 NR::Point const offset = _point - _origin;
1381 NR::scale s (1, 1);
1382 for (int i = NR::X; i <= NR::Y; i++) {
1383 if (fabs(offset[i]) > 1e-9)
1384 s[i] = (pt[i] - _origin[i]) / offset[i];
1385 if (fabs(s[i]) < 1e-9)
1386 s[i] = 1e-9;
1387 }
1389 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1390 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1392 int transform_stroke = false;
1393 gdouble strokewidth = 0;
1395 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1396 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1397 strokewidth = _strokewidth;
1398 }
1400 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1401 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1403 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1404 }
1406 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1407 {
1408 NR::Point const offset = _point - _origin;
1410 unsigned dim;
1411 switch (handle.cursor) {
1412 case GDK_SB_H_DOUBLE_ARROW:
1413 dim = NR::Y;
1414 break;
1415 case GDK_SB_V_DOUBLE_ARROW:
1416 dim = NR::X;
1417 break;
1418 default:
1419 g_assert_not_reached();
1420 abort();
1421 break;
1422 }
1423 if (fabs(offset[dim]) < 1e-15) {
1424 return;
1425 }
1426 NR::Matrix skew = NR::identity();
1427 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1428 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1429 skew[2*(1-dim) + (dim)] = 0;
1430 skew[2*(1-dim) + (1-dim)] = 1;
1432 for (int i = 0; i < 2; i++) {
1433 if (fabs(skew[3*i]) < 1e-15) {
1434 skew[3*i] = 1e-15;
1435 }
1436 }
1437 transform(skew, _origin);
1438 }
1440 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1441 {
1442 NR::Point const offset = _point - _origin;
1444 NR::Coord const h1 = NR::L2(offset);
1445 if (h1 < 1e-15) {
1446 return;
1447 }
1448 NR::Point const q1 = offset / h1;
1449 NR::Coord const h2 = NR::L2( pt - _origin );
1450 if (h2 < 1e-15) {
1451 return;
1452 }
1453 NR::Point const q2 = (pt - _origin) / h2;
1454 NR::rotate const r1(q1);
1455 NR::rotate const r2(q2);
1457 NR::Matrix rotate( r2 / r1 );
1458 transform(rotate, _origin);
1459 }
1461 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1462 {
1463 seltrans->setCenter(pt);
1464 }
1467 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1468 {
1469 SnapManager const &m = _desktop->namedview->snap_manager;
1471 /* The amount that we've moved by during this drag */
1472 NR::Point dxy = xy - _point;
1474 /* Get a STL list of the selected items.
1475 ** FIXME: this should probably be done by Inkscape::Selection.
1476 */
1477 std::list<SPItem const*> it;
1478 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1479 it.push_back(reinterpret_cast<SPItem*>(i->data));
1480 }
1482 bool const alt = (state & GDK_MOD1_MASK);
1483 bool const control = (state & GDK_CONTROL_MASK);
1484 bool const shift = (state & GDK_SHIFT_MASK);
1486 if (alt) {
1488 /* Alt pressed means keep offset: snap the moved distance to the grid.
1489 ** FIXME: this will snap to more than just the grid, nowadays.
1490 */
1492 dxy = m.freeSnap(Snapper::SNAPPOINT_NODE, dxy, NULL).getPoint();
1494 } else if (!shift) {
1496 /* We're snapping to things, possibly with a constraint to horizontal or
1497 ** vertical movement. Obtain a list of possible translations and then
1498 ** pick the smallest.
1499 */
1501 /* This will be our list of possible translations */
1502 std::list<std::pair<NR::Point, bool> > s;
1504 if (control) {
1506 /* Snap to things, and also constrain to horizontal or vertical movement */
1508 for (unsigned int dim = 0; dim < 2; dim++) {
1509 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX,
1510 _bbox_points,
1511 it,
1512 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1513 dxy));
1515 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE,
1516 _snap_points,
1517 it,
1518 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1519 dxy));
1520 }
1522 } else {
1524 // Let's leave this timer code here for a while. I'll probably need it in the near future (Diederik van Lierop)
1525 /* GTimeVal starttime;
1526 GTimeVal endtime;
1527 g_get_current_time(&starttime); */
1529 /* Snap to things with no constraint */
1530 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX,
1531 _bbox_points, it, dxy));
1532 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE,
1533 _snap_points, it, dxy));
1535 /*g_get_current_time(&endtime);
1536 double elapsed = ((((double)endtime.tv_sec - starttime.tv_sec) * G_USEC_PER_SEC + (endtime.tv_usec - starttime.tv_usec))) / 1000.0;
1537 std::cout << "Time spent snapping: " << elapsed << std::endl; */
1538 }
1540 /* Pick one */
1541 NR::Coord best = NR_HUGE;
1542 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1543 if (i->second) {
1544 NR::Coord const m = NR::L2(i->first);
1545 if (m < best) {
1546 best = m;
1547 dxy = i->first;
1548 }
1549 }
1550 }
1551 }
1553 if (control) {
1554 /* Ensure that the horizontal and vertical constraint has been applied */
1555 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1556 dxy[NR::Y] = 0;
1557 } else {
1558 dxy[NR::X] = 0;
1559 }
1560 }
1562 NR::Matrix const move((NR::translate(dxy)));
1563 NR::Point const norm(0, 0);
1564 transform(move, norm);
1566 // status text
1567 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1568 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1569 _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);
1570 g_string_free(xs, TRUE);
1571 g_string_free(ys, TRUE);
1572 }
1575 /*
1576 Local Variables:
1577 mode:c++
1578 c-file-style:"stroustrup"
1579 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1580 indent-tabs-mode:nil
1581 fill-column:99
1582 End:
1583 */
1584 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :