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 _snap_points = selection->getSnapPoints(); // Excludes path nodes
272 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull(); // Includes path nodes
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_MOD1_MASK) {
769 *position = _point + ( *position - _point ) / 10;
770 }
772 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
773 _origin = _opposite;
774 _origin_for_bboxpoints = _opposite_for_bboxpoints;
775 _origin_for_specpoints = _opposite_for_specpoints;
776 } else if (_center) {
777 _origin = *_center;
778 _origin_for_bboxpoints = *_center;
779 _origin_for_specpoints = *_center;
780 } else {
781 // FIXME
782 return TRUE;
783 }
784 if (handle.request(this, handle, *position, state)) {
785 sp_knot_set_position(knot, position, state);
786 SP_CTRL(_grip)->moveto(*position);
787 SP_CTRL(_norm)->moveto(_origin);
788 }
790 return TRUE;
791 }
794 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
795 {
796 if (!_grabbed) {
797 // reread in case it changed on the fly:
798 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
799 _snap_bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
800 //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
802 _updateVolatileState();
803 _current.set_identity();
804 _center_is_set = false; // center(s) may have changed
805 _updateHandles();
806 }
807 }
809 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
810 {
811 if (!_grabbed) {
812 _updateVolatileState();
813 _current.set_identity();
815 // reset internal flag
816 _changed = false;
818 _center_is_set = false; // center(s) may have changed
820 _updateHandles();
821 }
822 }
824 /*
825 * handlers for handle move-request
826 */
828 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
829 static double sign(double const x)
830 {
831 return ( x < 0
832 ? -1
833 : 1 );
834 }
836 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
837 SPSelTransHandle const &, NR::Point &pt, guint state)
838 {
839 return seltrans->scaleRequest(pt, state);
840 }
842 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
843 SPSelTransHandle const &handle, NR::Point &pt, guint state)
844 {
845 return seltrans->stretchRequest(handle, pt, state);
846 }
848 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
849 SPSelTransHandle const &handle, NR::Point &pt, guint state)
850 {
851 return seltrans->skewRequest(handle, pt, state);
852 }
854 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
855 SPSelTransHandle const &, NR::Point &pt, guint state)
856 {
857 return seltrans->rotateRequest(pt, state);
858 }
860 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
861 SPSelTransHandle const &, NR::Point &pt, guint state)
862 {
863 return seltrans->centerRequest(pt, state);
864 }
866 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
867 {
868 using NR::X;
869 using NR::Y;
871 NR::Point d = _point - _origin;
872 NR::scale s(0, 0);
874 /* Work out the new scale factors `s' */
875 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
876 if ( fabs(d[i]) > 0.001 ) {
877 s[i] = ( pt[i] - _origin[i] ) / d[i];
878 if ( fabs(s[i]) < 1e-9 ) {
879 s[i] = 1e-9;
880 }
881 }
882 }
884 SnapManager const &m = _desktop->namedview->snap_manager;
886 /* Get a STL list of the selected items.
887 ** FIXME: this should probably be done by Inkscape::Selection.
888 */
889 std::list<SPItem const*> it;
890 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
891 it.push_back(reinterpret_cast<SPItem*>(i->data));
892 }
894 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
895 // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
896 //
897 // The aspect-ratio must be locked before snapping
898 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
899 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
900 } else {
901 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
902 }
904 // Snap along a suitable constraint vector from the origin.
906 // The inclination of the constraint vector is calculated from the aspect ratio
907 NR::Point bbox_dim = _bbox->dimensions();
908 double const aspect_ratio = bbox_dim[1] / bbox_dim[0]; // = height / width
910 // Determine direction of the constraint vector
911 NR::Point const cv = NR::Point(
912 pt[NR::X] > _origin[NR::X] ? 1 : -1,
913 pt[NR::Y] > _origin[NR::Y] ? aspect_ratio : -aspect_ratio
914 );
916 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
917 _bbox_points,
918 it,
919 Snapper::ConstraintLine(_origin_for_bboxpoints, cv),
920 s,
921 _origin_for_bboxpoints);
923 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
924 _snap_points,
925 it,
926 Snapper::ConstraintLine(_origin_for_specpoints, cv),
927 s,
928 _origin_for_specpoints);
930 if (bb.second == false && sn.second == false) {
931 /* We didn't snap, so just keep the locked aspect ratio */
932 } else {
933 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
934 ** just compare difference in s[X].
935 */
936 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
937 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
938 s = (bd < sd) ? bb.first : sn.first;
939 }
941 } else {
942 /* Scale aspect ratio is unlocked */
944 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
945 _bbox_points,
946 it,
947 s,
948 _origin_for_bboxpoints);
949 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
950 _snap_points,
951 it,
952 s,
953 _origin_for_specpoints);
955 /* Pick the snap that puts us closest to the original scale */
956 NR::Coord bd = bb.second ?
957 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
958 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
959 : NR_HUGE;
960 NR::Coord sd = sn.second ?
961 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
962 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
963 : NR_HUGE;
964 s = (bd < sd) ? bb.first : sn.first;
965 }
967 /* Update the knot position */
968 pt = ( _point - _origin ) * s + _origin;
970 /* Status text */
971 _message_context.setF(Inkscape::NORMAL_MESSAGE,
972 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
973 100 * s[NR::X], 100 * s[NR::Y]);
975 return TRUE;
976 }
978 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
979 {
980 using NR::X;
981 using NR::Y;
983 NR::Dim2 axis, perp;
985 switch (handle.cursor) {
986 case GDK_TOP_SIDE:
987 case GDK_BOTTOM_SIDE:
988 axis = NR::Y;
989 perp = NR::X;
990 break;
991 case GDK_LEFT_SIDE:
992 case GDK_RIGHT_SIDE:
993 axis = NR::X;
994 perp = NR::Y;
995 break;
996 default:
997 g_assert_not_reached();
998 return TRUE;
999 };
1001 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
1002 return FALSE;
1003 }
1005 NR::scale s(1, 1);
1006 s[axis] = ( ( pt[axis] - _origin[axis] )
1007 / ( _point[axis] - _origin[axis] ) );
1008 if ( fabs(s[axis]) < 1e-15 ) {
1009 s[axis] = 1e-15;
1010 }
1012 /* Get a STL list of the selected items.
1013 ** FIXME: this should probably be done by Inkscape::Selection.
1014 */
1015 std::list<SPItem const*> it;
1016 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1017 it.push_back(reinterpret_cast<SPItem*>(i->data));
1018 }
1020 SnapManager const &m = _desktop->namedview->snap_manager;
1022 if ( state & GDK_CONTROL_MASK ) {
1023 // on ctrl, apply symmetrical scaling instead of stretching
1024 s[perp] = fabs(s[axis]);
1026 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1027 Snapper::BBOX_POINT,
1028 _bbox_points,
1029 it,
1030 s[axis],
1031 _origin_for_bboxpoints,
1032 axis,
1033 true);
1035 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1036 Snapper::SNAP_POINT,
1037 _snap_points,
1038 it,
1039 s[axis],
1040 _origin_for_specpoints,
1041 axis,
1042 true);
1044 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1045 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1046 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
1048 s[axis] = fabs(ratio) * sign(s[axis]);
1049 s[perp] = fabs(s[axis]);
1050 } else {
1052 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1053 Snapper::BBOX_POINT,
1054 _bbox_points,
1055 it,
1056 s[axis],
1057 _origin_for_bboxpoints,
1058 axis,
1059 false);
1061 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1062 Snapper::SNAP_POINT,
1063 _snap_points,
1064 it,
1065 s[axis],
1066 _origin_for_specpoints,
1067 axis,
1068 false);
1070 /* Choose the smaller difference in scale */
1071 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1072 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1073 s[axis] = (bd < sd) ? bb.first : sn.first;
1074 s[perp] = 1;
1075 }
1077 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1078 if (isNaN(pt[X] + pt[Y])) {
1079 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1080 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1081 }
1083 // status text
1084 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1085 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1086 100 * s[NR::X], 100 * s[NR::Y]);
1088 return TRUE;
1089 }
1091 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1092 {
1093 using NR::X;
1094 using NR::Y;
1096 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1097 return FALSE;
1098 }
1100 NR::Dim2 dim_a;
1101 NR::Dim2 dim_b;
1102 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1103 dim_a = X;
1104 dim_b = Y;
1105 } else {
1106 dim_a = Y;
1107 dim_b = X;
1108 }
1110 double skew[2];
1111 double s[2] = { 1.0, 1.0 };
1113 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1114 return FALSE;
1115 }
1117 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1119 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1121 if ( fabs(s[dim_a]) < 1 ) {
1122 s[dim_a] = sign(s[dim_a]);
1123 } else {
1124 s[dim_a] = floor( s[dim_a] + 0.5 );
1125 }
1127 double radians = atan(skew[dim_a] / s[dim_a]);
1129 if (state & GDK_CONTROL_MASK) {
1131 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1133 if (snaps) {
1134 double sections = floor( radians * snaps / M_PI + .5 );
1135 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1136 radians = ( M_PI / snaps ) * sections;
1137 }
1138 skew[dim_a] = tan(radians) * s[dim_a];
1139 } else {
1140 SnapManager const &m = _desktop->namedview->snap_manager;
1142 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1143 _bbox_points,
1144 std::list<SPItem const *>(),
1145 skew[dim_a],
1146 _origin_for_bboxpoints,
1147 dim_b);
1149 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1150 _snap_points,
1151 std::list<SPItem const *>(),
1152 skew[dim_a],
1153 _origin_for_specpoints,
1154 dim_b);
1156 if (bb.second || sn.second) {
1157 /* We snapped something, so change the skew to reflect it */
1158 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1159 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1160 skew[dim_a] = std::min(bd, sd);
1161 }
1162 }
1164 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1165 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1167 /* status text */
1168 double degrees = 180 / M_PI * radians;
1169 if (degrees > 180) degrees -= 360;
1170 if (degrees < -180) degrees += 360;
1172 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1173 // TRANSLATORS: don't modify the first ";"
1174 // (it will NOT be displayed as ";" - only the second one will be)
1175 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1176 degrees);
1178 return TRUE;
1179 }
1181 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1182 {
1183 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1185 // rotate affine in rotate
1186 NR::Point const d1 = _point - _origin;
1187 NR::Point const d2 = pt - _origin;
1189 NR::Coord const h1 = NR::L2(d1);
1190 if (h1 < 1e-15) return FALSE;
1191 NR::Point q1 = d1 / h1;
1192 NR::Coord const h2 = NR::L2(d2);
1193 if (fabs(h2) < 1e-15) return FALSE;
1194 NR::Point q2 = d2 / h2;
1196 double radians;
1197 if (state & GDK_CONTROL_MASK) {
1198 /* Have to restrict movement. */
1199 double cos_t = NR::dot(q1, q2);
1200 double sin_t = NR::dot(NR::rot90(q1), q2);
1201 radians = atan2(sin_t, cos_t);
1202 if (snaps) {
1203 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1204 }
1205 q1 = NR::Point(1, 0);
1206 q2 = NR::Point(cos(radians), sin(radians));
1207 } else {
1208 radians = atan2(NR::dot(NR::rot90(d1), d2),
1209 NR::dot(d1, d2));
1210 }
1212 NR::rotate const r1(q1);
1213 NR::rotate const r2(q2);
1214 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1216 /* status text */
1217 double degrees = 180 / M_PI * radians;
1218 if (degrees > 180) degrees -= 360;
1219 if (degrees < -180) degrees += 360;
1221 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1222 // TRANSLATORS: don't modify the first ";"
1223 // (it will NOT be displayed as ";" - only the second one will be)
1224 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1226 return TRUE;
1227 }
1229 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1230 {
1231 using NR::X;
1232 using NR::Y;
1234 SnapManager const &m = _desktop->namedview->snap_manager;
1235 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1237 if (state & GDK_CONTROL_MASK) {
1238 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1239 pt[Y] = _point[Y];
1240 } else {
1241 pt[X] = _point[X];
1242 }
1243 }
1245 if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
1246 // screen pixels to snap center to bbox
1247 #define SNAP_DIST 5
1248 // FIXME: take from prefs
1249 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1251 for (int i = 0; i < 2; i++) {
1252 if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
1253 pt[i] = _bbox->min()[i];
1254 }
1255 if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
1256 pt[i] = _bbox->midpoint()[i];
1257 }
1258 if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
1259 pt[i] = _bbox->max()[i];
1260 }
1261 }
1262 }
1264 // status text
1265 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1266 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1267 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1268 g_string_free(xs, FALSE);
1269 g_string_free(ys, FALSE);
1271 return TRUE;
1272 }
1274 /*
1275 * handlers for handle movement
1276 *
1277 */
1279 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1280 {
1281 seltrans->stretch(handle, pt, state);
1282 }
1284 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1285 {
1286 seltrans->scale(pt, state);
1287 }
1289 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1290 {
1291 seltrans->skew(handle, pt, state);
1292 }
1294 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1295 {
1296 seltrans->rotate(pt, state);
1297 }
1299 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1300 {
1301 using NR::X;
1302 using NR::Y;
1304 NR::Dim2 dim;
1305 switch (handle.cursor) {
1306 case GDK_LEFT_SIDE:
1307 case GDK_RIGHT_SIDE:
1308 dim = X;
1309 break;
1310 case GDK_TOP_SIDE:
1311 case GDK_BOTTOM_SIDE:
1312 dim = Y;
1313 break;
1314 default:
1315 g_assert_not_reached();
1316 abort();
1317 break;
1318 }
1320 NR::Point const scale_origin(_origin);
1321 double const offset = _point[dim] - scale_origin[dim];
1322 if (!( fabs(offset) >= 1e-15 )) {
1323 return;
1324 }
1325 NR::scale s(1, 1);
1326 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1327 if (isNaN(s[dim])) {
1328 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1329 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1330 }
1331 if (!( fabs(s[dim]) >= 1e-15 )) {
1332 s[dim] = 1e-15;
1333 }
1334 if (state & GDK_CONTROL_MASK) {
1335 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1336 s[!dim] = fabs(s[dim]);
1337 }
1339 if (!_bbox) {
1340 return;
1341 }
1343 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1344 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1346 int transform_stroke = false;
1347 gdouble strokewidth = 0;
1349 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1350 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1351 strokewidth = _strokewidth;
1352 }
1354 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1355 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1357 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1358 }
1360 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1361 {
1362 if (!_bbox) {
1363 return;
1364 }
1366 NR::Point const offset = _point - _origin;
1368 NR::scale s (1, 1);
1369 for (int i = NR::X; i <= NR::Y; i++) {
1370 if (fabs(offset[i]) > 1e-9)
1371 s[i] = (pt[i] - _origin[i]) / offset[i];
1372 if (fabs(s[i]) < 1e-9)
1373 s[i] = 1e-9;
1374 }
1376 NR::Point new_bbox_min = _approximate_bbox->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1377 NR::Point new_bbox_max = _approximate_bbox->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1379 int transform_stroke = false;
1380 gdouble strokewidth = 0;
1382 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1383 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1384 strokewidth = _strokewidth;
1385 }
1387 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1388 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1390 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1391 }
1393 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1394 {
1395 NR::Point const offset = _point - _origin;
1397 unsigned dim;
1398 switch (handle.cursor) {
1399 case GDK_SB_H_DOUBLE_ARROW:
1400 dim = NR::Y;
1401 break;
1402 case GDK_SB_V_DOUBLE_ARROW:
1403 dim = NR::X;
1404 break;
1405 default:
1406 g_assert_not_reached();
1407 abort();
1408 break;
1409 }
1410 if (fabs(offset[dim]) < 1e-15) {
1411 return;
1412 }
1413 NR::Matrix skew = NR::identity();
1414 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1415 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1416 skew[2*(1-dim) + (dim)] = 0;
1417 skew[2*(1-dim) + (1-dim)] = 1;
1419 for (int i = 0; i < 2; i++) {
1420 if (fabs(skew[3*i]) < 1e-15) {
1421 skew[3*i] = 1e-15;
1422 }
1423 }
1424 transform(skew, _origin);
1425 }
1427 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1428 {
1429 NR::Point const offset = _point - _origin;
1431 NR::Coord const h1 = NR::L2(offset);
1432 if (h1 < 1e-15) {
1433 return;
1434 }
1435 NR::Point const q1 = offset / h1;
1436 NR::Coord const h2 = NR::L2( pt - _origin );
1437 if (h2 < 1e-15) {
1438 return;
1439 }
1440 NR::Point const q2 = (pt - _origin) / h2;
1441 NR::rotate const r1(q1);
1442 NR::rotate const r2(q2);
1444 NR::Matrix rotate( r2 / r1 );
1445 transform(rotate, _origin);
1446 }
1448 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1449 {
1450 seltrans->setCenter(pt);
1451 }
1454 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1455 {
1456 SnapManager const &m = _desktop->namedview->snap_manager;
1458 /* The amount that we've moved by during this drag */
1459 NR::Point dxy = xy - _point;
1461 /* Get a STL list of the selected items.
1462 ** FIXME: this should probably be done by Inkscape::Selection.
1463 */
1464 std::list<SPItem const*> it;
1465 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1466 it.push_back(reinterpret_cast<SPItem*>(i->data));
1467 }
1469 bool const alt = (state & GDK_MOD1_MASK);
1470 bool const control = (state & GDK_CONTROL_MASK);
1471 bool const shift = (state & GDK_SHIFT_MASK);
1473 if (alt) {
1475 /* Alt pressed means keep offset: snap the moved distance to the grid.
1476 ** FIXME: this will snap to more than just the grid, nowadays.
1477 */
1479 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1481 } else if (!shift) {
1483 /* We're snapping to things, possibly with a constraint to horizontal or
1484 ** vertical movement. Obtain a list of possible translations and then
1485 ** pick the smallest.
1486 */
1488 /* This will be our list of possible translations */
1489 std::list<std::pair<NR::Point, bool> > s;
1491 if (control) {
1493 /* Snap to things, and also constrain to horizontal or vertical movement */
1495 for (unsigned int dim = 0; dim < 2; dim++) {
1496 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1497 _bbox_points,
1498 it,
1499 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1500 dxy));
1502 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1503 _snap_points,
1504 it,
1505 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1506 dxy));
1507 }
1509 } else {
1511 /* Snap to things with no constraint */
1513 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1514 _bbox_points, it, dxy));
1515 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1516 _snap_points, it, dxy));
1517 }
1519 /* Pick one */
1520 NR::Coord best = NR_HUGE;
1521 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1522 if (i->second) {
1523 NR::Coord const m = NR::L2(i->first);
1524 if (m < best) {
1525 best = m;
1526 dxy = i->first;
1527 }
1528 }
1529 }
1530 }
1532 if (control) {
1533 /* Ensure that the horizontal and vertical constraint has been applied */
1534 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1535 dxy[NR::Y] = 0;
1536 } else {
1537 dxy[NR::X] = 0;
1538 }
1539 }
1541 NR::Matrix const move((NR::translate(dxy)));
1542 NR::Point const norm(0, 0);
1543 transform(move, norm);
1545 // status text
1546 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1547 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1548 _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);
1549 g_string_free(xs, TRUE);
1550 g_string_free(ys, TRUE);
1551 }
1554 /*
1555 Local Variables:
1556 mode:c++
1557 c-file-style:"stroustrup"
1558 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1559 indent-tabs-mode:nil
1560 fill-column:99
1561 End:
1562 */
1563 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :