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 //_snap_bbox_type = SPItem::GEOMETRIC_BBOX; //TODO: Get this parameter from UI; hardcoded for the time being
98 _snap_bbox_type = 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);
118 _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
119 SP_TYPE_CTRL,
120 "anchor", GTK_ANCHOR_CENTER,
121 "mode", SP_CTRL_MODE_COLOR,
122 "shape", SP_CTRL_SHAPE_BITMAP,
123 "size", 13.0,
124 "filled", TRUE,
125 "fill_color", 0x00000000,
126 "stroked", TRUE,
127 "stroke_color", 0x000000a0,
128 "pixbuf", handles[12],
129 NULL);
131 _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
132 SP_TYPE_CTRL,
133 "anchor", GTK_ANCHOR_CENTER,
134 "mode", SP_CTRL_MODE_XOR,
135 "shape", SP_CTRL_SHAPE_CROSS,
136 "size", 7.0,
137 "filled", TRUE,
138 "fill_color", 0xffffff7f,
139 "stroked", TRUE,
140 "stroke_color", 0xffffffff,
141 "pixbuf", handles[12],
142 NULL);
144 sp_canvas_item_hide(_grip);
145 sp_canvas_item_hide(_norm);
147 for (int i = 0; i < 4; i++) {
148 _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
149 sp_canvas_item_hide(_l[i]);
150 }
152 _sel_changed_connection = _selection->connectChanged(
153 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
154 );
156 _sel_modified_connection = _selection->connectModified(
157 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
158 );
159 }
161 Inkscape::SelTrans::~SelTrans()
162 {
163 _sel_changed_connection.disconnect();
164 _sel_modified_connection.disconnect();
166 for (unsigned int i = 0; i < 8; i++) {
167 if (_shandle[i]) {
168 g_object_unref(G_OBJECT(_shandle[i]));
169 _shandle[i] = NULL;
170 }
171 if (_rhandle[i]) {
172 g_object_unref(G_OBJECT(_rhandle[i]));
173 _rhandle[i] = NULL;
174 }
175 }
176 if (_chandle) {
177 g_object_unref(G_OBJECT(_chandle));
178 _chandle = NULL;
179 }
181 if (_norm) {
182 gtk_object_destroy(GTK_OBJECT(_norm));
183 _norm = NULL;
184 }
185 if (_grip) {
186 gtk_object_destroy(GTK_OBJECT(_grip));
187 _grip = NULL;
188 }
189 for (int i = 0; i < 4; i++) {
190 if (_l[i]) {
191 gtk_object_destroy(GTK_OBJECT(_l[i]));
192 _l[i] = NULL;
193 }
194 }
196 for (unsigned i = 0; i < _items.size(); i++) {
197 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
198 }
200 _items.clear();
201 _items_centers.clear();
202 }
204 void Inkscape::SelTrans::resetState()
205 {
206 _state = STATE_SCALE;
207 }
209 void Inkscape::SelTrans::increaseState()
210 {
211 if (_state == STATE_SCALE) {
212 _state = STATE_ROTATE;
213 } else {
214 _state = STATE_SCALE;
215 }
217 _center_is_set = true; // no need to reread center
219 _updateHandles();
220 }
222 void Inkscape::SelTrans::setCenter(NR::Point const &p)
223 {
224 _center = p;
225 _center_is_set = true;
227 // Write the new center position into all selected items
228 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
229 SPItem *it = (SPItem*)SP_OBJECT(l->data);
230 it->setCenter(p);
231 // only set the value; updating repr and document_done will be done once, on ungrab
232 }
234 _updateHandles();
235 }
237 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
238 {
239 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
241 g_return_if_fail(!_grabbed);
243 _grabbed = true;
244 _show_handles = show_handles;
245 _updateVolatileState();
246 _current.set_identity();
248 _changed = false;
250 if (_empty) {
251 return;
252 }
254 for (GSList const *l = selection->itemList(); l; l = l->next) {
255 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
256 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
257 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
258 }
260 _current.set_identity();
262 _point = p;
264 // The selector tool should snap the bbox and the special snappoints, but not path nodes
265 // (The special points are the handles, center, rotation axis, font baseline, ends of spiral, etc.)
267 // First, get all special points for snapping
268 _snap_points = selection->getSnapPoints(); // Excludes path nodes
269 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull(); // Includes path nodes
270 if (_snap_points.size() > 100) {
271 /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
272 An average user would rarely ever try to snap such a large number of nodes anyway, because
273 (s)he could hardly discern which node would be snapping */
274 _snap_points = snap_points_hull;
275 // Unfortunately, by now we will have lost the font-baseline snappoints :-(
276 }
278 // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
279 // path nodes, for example because a rectangle which has been converted to a path doesn't have
280 // any other special points
281 NR::Rect snap_points_bbox;
282 if ( snap_points_hull.empty() == false ) {
283 std::vector<NR::Point>::iterator i = snap_points_hull.begin();
284 snap_points_bbox = NR::Rect(*i, *i);
285 i++;
286 while (i != snap_points_hull.end()) {
287 snap_points_bbox.expandTo(*i);
288 i++;
289 }
290 }
292 // Next, determine the bounding box for snapping ...
293 _bbox = selection->bounds(_snap_bbox_type);
295 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX); // Used for correctly scaling the strokewidth
297 _bbox_points.clear();
298 if (_bbox) {
299 // ... and add the bbox corners to _bbox_points
300 for ( unsigned i = 0 ; i < 4 ; i++ ) {
301 _bbox_points.push_back(_bbox->corner(i));
302 }
303 // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
304 // - one for snapping the boundingbox, which can be either visual or geometric
305 // - one for snapping the special points
306 // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
307 // These distinct "opposites" are needed in the snapmanager to avoid bugs such as #1540195 (in which
308 // a box is caught between to guides)
309 _opposite_for_bboxpoints = _bbox->min() + _bbox->dimensions() * NR::scale(1-x, 1-y);
310 _opposite_for_specpoints = (snap_points_bbox.min() + (snap_points_bbox.dimensions() * NR::scale(1-x, 1-y) ) );
311 // Only a single "opposite" can be used in calculating transformations.
312 _opposite = _opposite_for_bboxpoints;
313 }
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 // First, get all special points for snapping
601 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull(); // Includes path nodes
602 // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
603 // path nodes, for example because a rectangle which has been converted to a path doesn't have
604 // any other special points
605 NR::Rect snap_points_bbox;
606 if ( snap_points_hull.empty() == false ) {
607 std::vector<NR::Point>::iterator i = snap_points_hull.begin();
608 snap_points_bbox = NR::Rect(*i, *i);
609 i++;
610 while (i != snap_points_hull.end()) {
611 snap_points_bbox.expandTo(*i);
612 i++;
613 }
614 }
616 // Next, determine the bounding box for snapping ...
617 _bbox = selection->bounds(_snap_bbox_type);
618 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX);
620 /*std::cout << "Approximate BBox: " << _approximate_bbox->min() << " - " << _approximate_bbox->max() << std::endl;
621 std::cout << "Geometric BBox: " << selection->bounds(SPItem::GEOMETRIC_BBOX)->min() << " - " << selection->bounds(SPItem::GEOMETRIC_BBOX)->max() << std::endl;
622 */
624 if (!_bbox) {
625 _empty = true;
626 return;
627 }
629 _strokewidth = stroke_average_width (selection->itemList());
630 }
632 static void sp_remove_handles(SPKnot *knot[], gint num)
633 {
634 for (int i = 0; i < num; i++) {
635 if (knot[i] != NULL) {
636 sp_knot_hide(knot[i]);
637 }
638 }
639 }
641 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
642 gchar const *even_tip, gchar const *odd_tip)
643 {
644 g_return_if_fail( !_empty );
646 for (int i = 0; i < num; i++) {
647 if (knot[i] == NULL) {
648 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
650 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
651 knot[i]->setSize (13);
652 knot[i]->setAnchor (handle[i].anchor);
653 knot[i]->setMode (SP_CTRL_MODE_XOR);
654 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
655 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
656 knot[i]->setPixbuf(handles[handle[i].control]);
657 sp_knot_update_ctrl(knot[i]);
659 g_signal_connect(G_OBJECT(knot[i]), "request",
660 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
661 g_signal_connect(G_OBJECT(knot[i]), "moved",
662 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
663 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
664 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
665 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
666 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
667 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
668 }
669 sp_knot_show(knot[i]);
671 NR::Point const handle_pt(handle[i].x, handle[i].y);
672 // shouldn't have nullary bbox, but knots
673 g_assert(_bbox);
674 NR::Point p( _bbox->min()
675 + ( _bbox->dimensions()
676 * NR::scale(handle_pt) ) );
678 sp_knot_moveto(knot[i], &p);
679 }
680 }
682 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
683 {
684 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
685 knot, state, *(SPSelTransHandle const *) data
686 );
687 }
689 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
690 {
691 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
692 }
694 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
695 {
696 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
697 knot, position, state, *(SPSelTransHandle const *) data
698 );
699 }
701 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
702 {
703 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
704 knot, position, state, *(SPSelTransHandle const *) data
705 );
706 }
708 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
709 {
710 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
711 knot, state, *(SPSelTransHandle const *) data
712 );
713 }
715 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
716 {
717 switch (handle.anchor) {
718 case GTK_ANCHOR_CENTER:
719 if (state & GDK_SHIFT_MASK) {
720 // Unset the center position for all selected items
721 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
722 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
723 it->unsetCenter();
724 SP_OBJECT(it)->updateRepr();
725 _center_is_set = false; // center has changed
726 _updateHandles();
727 }
728 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
729 _("Reset center"));
730 }
731 break;
732 default:
733 break;
734 }
735 }
737 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
738 {
739 switch (handle.anchor) {
740 case GTK_ANCHOR_CENTER:
741 g_object_set(G_OBJECT(_grip),
742 "shape", SP_CTRL_SHAPE_BITMAP,
743 "size", 13.0,
744 NULL);
745 sp_canvas_item_show(_grip);
746 break;
747 default:
748 g_object_set(G_OBJECT(_grip),
749 "shape", SP_CTRL_SHAPE_CROSS,
750 "size", 7.0,
751 NULL);
752 sp_canvas_item_show(_norm);
753 sp_canvas_item_show(_grip);
755 break;
756 }
758 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
759 }
762 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
763 {
764 if (!SP_KNOT_IS_GRABBED(knot)) {
765 return;
766 }
768 // in case items have been unhooked from the document, don't
769 // try to continue processing events for them.
770 for (unsigned int i = 0; i < _items.size(); i++) {
771 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
772 return;
773 }
774 }
776 handle.action(this, handle, *position, state);
777 }
780 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
781 {
782 if (!SP_KNOT_IS_GRABBED(knot)) {
783 return TRUE;
784 }
786 knot->desktop->setPosition(*position);
788 if (state & GDK_MOD1_MASK) {
789 *position = _point + ( *position - _point ) / 10;
790 }
792 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
793 _origin = _opposite;
794 _origin_for_bboxpoints = _opposite_for_bboxpoints;
795 _origin_for_specpoints = _opposite_for_specpoints;
796 } else if (_center) {
797 _origin = *_center;
798 _origin_for_bboxpoints = *_center;
799 _origin_for_specpoints = *_center;
800 } else {
801 // FIXME
802 return TRUE;
803 }
804 if (handle.request(this, handle, *position, state)) {
805 sp_knot_set_position(knot, position, state);
806 SP_CTRL(_grip)->moveto(*position);
807 SP_CTRL(_norm)->moveto(_origin);
808 }
810 return TRUE;
811 }
814 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
815 {
816 if (!_grabbed) {
817 _updateVolatileState();
818 _current.set_identity();
819 _center_is_set = false; // center(s) may have changed
820 _updateHandles();
821 }
822 }
824 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
825 {
826 if (!_grabbed) {
827 _updateVolatileState();
828 _current.set_identity();
830 // reset internal flag
831 _changed = false;
833 _center_is_set = false; // center(s) may have changed
835 _updateHandles();
836 }
837 }
839 /*
840 * handlers for handle move-request
841 */
843 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
844 static double sign(double const x)
845 {
846 return ( x < 0
847 ? -1
848 : 1 );
849 }
851 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
852 SPSelTransHandle const &, NR::Point &pt, guint state)
853 {
854 return seltrans->scaleRequest(pt, state);
855 }
857 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
858 SPSelTransHandle const &handle, NR::Point &pt, guint state)
859 {
860 return seltrans->stretchRequest(handle, pt, state);
861 }
863 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
864 SPSelTransHandle const &handle, NR::Point &pt, guint state)
865 {
866 return seltrans->skewRequest(handle, pt, state);
867 }
869 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
870 SPSelTransHandle const &, NR::Point &pt, guint state)
871 {
872 return seltrans->rotateRequest(pt, state);
873 }
875 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
876 SPSelTransHandle const &, NR::Point &pt, guint state)
877 {
878 return seltrans->centerRequest(pt, state);
879 }
881 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
882 {
883 using NR::X;
884 using NR::Y;
886 NR::Point d = _point - _origin;
887 NR::scale s(0, 0);
889 /* Work out the new scale factors `s' */
890 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
891 if ( fabs(d[i]) > 0.001 ) {
892 s[i] = ( pt[i] - _origin[i] ) / d[i];
893 if ( fabs(s[i]) < 1e-9 ) {
894 s[i] = 1e-9;
895 }
896 }
897 }
899 SnapManager const &m = _desktop->namedview->snap_manager;
901 /* Get a STL list of the selected items.
902 ** FIXME: this should probably be done by Inkscape::Selection.
903 */
904 std::list<SPItem const*> it;
905 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
906 it.push_back(reinterpret_cast<SPItem*>(i->data));
907 }
909 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
910 // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
911 //
912 // The aspect-ratio must be locked before snapping
913 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
914 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
915 } else {
916 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
917 }
919 // Snap along a suitable constraint vector from the origin.
921 // The inclination of the constraint vector is calculated from the aspect ratio
922 NR::Point bbox_dim = _bbox->dimensions();
923 double const aspect_ratio = bbox_dim[1] / bbox_dim[0]; // = height / width
925 // Determine direction of the constraint vector
926 NR::Point const cv = NR::Point(
927 pt[NR::X] > _origin[NR::X] ? 1 : -1,
928 pt[NR::Y] > _origin[NR::Y] ? aspect_ratio : -aspect_ratio
929 );
931 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
932 _bbox_points,
933 it,
934 Snapper::ConstraintLine(_origin_for_bboxpoints, cv),
935 s,
936 _origin_for_bboxpoints);
938 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
939 _snap_points,
940 it,
941 Snapper::ConstraintLine(_origin_for_specpoints, cv),
942 s,
943 _origin_for_specpoints);
945 if (bb.second == false && sn.second == false) {
946 /* We didn't snap, so just keep the locked aspect ratio */
947 } else {
948 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
949 ** just compare difference in s[X].
950 */
951 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
952 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
953 s = (bd < sd) ? bb.first : sn.first;
954 }
956 } else {
957 /* Scale aspect ratio is unlocked */
959 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
960 _bbox_points,
961 it,
962 s,
963 _origin_for_bboxpoints);
964 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
965 _snap_points,
966 it,
967 s,
968 _origin_for_specpoints);
970 /* Pick the snap that puts us closest to the original scale */
971 NR::Coord bd = bb.second ?
972 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
973 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
974 : NR_HUGE;
975 NR::Coord sd = sn.second ?
976 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
977 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
978 : NR_HUGE;
979 s = (bd < sd) ? bb.first : sn.first;
980 }
982 /* Update the knot position */
983 pt = ( _point - _origin ) * s + _origin;
985 /* Status text */
986 _message_context.setF(Inkscape::NORMAL_MESSAGE,
987 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
988 100 * s[NR::X], 100 * s[NR::Y]);
990 return TRUE;
991 }
993 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
994 {
995 using NR::X;
996 using NR::Y;
998 NR::Dim2 axis, perp;
1000 switch (handle.cursor) {
1001 case GDK_TOP_SIDE:
1002 case GDK_BOTTOM_SIDE:
1003 axis = NR::Y;
1004 perp = NR::X;
1005 break;
1006 case GDK_LEFT_SIDE:
1007 case GDK_RIGHT_SIDE:
1008 axis = NR::X;
1009 perp = NR::Y;
1010 break;
1011 default:
1012 g_assert_not_reached();
1013 return TRUE;
1014 };
1016 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
1017 return FALSE;
1018 }
1020 NR::scale s(1, 1);
1021 s[axis] = ( ( pt[axis] - _origin[axis] )
1022 / ( _point[axis] - _origin[axis] ) );
1023 if ( fabs(s[axis]) < 1e-15 ) {
1024 s[axis] = 1e-15;
1025 }
1027 /* Get a STL list of the selected items.
1028 ** FIXME: this should probably be done by Inkscape::Selection.
1029 */
1030 std::list<SPItem const*> it;
1031 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1032 it.push_back(reinterpret_cast<SPItem*>(i->data));
1033 }
1035 SnapManager const &m = _desktop->namedview->snap_manager;
1037 if ( state & GDK_CONTROL_MASK ) {
1038 // on ctrl, apply symmetrical scaling instead of stretching
1039 s[perp] = fabs(s[axis]);
1041 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1042 Snapper::BBOX_POINT,
1043 _bbox_points,
1044 it,
1045 s[axis],
1046 _origin_for_bboxpoints,
1047 axis,
1048 true);
1050 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1051 Snapper::SNAP_POINT,
1052 _snap_points,
1053 it,
1054 s[axis],
1055 _origin_for_specpoints,
1056 axis,
1057 true);
1059 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1060 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1061 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
1063 s[axis] = fabs(ratio) * sign(s[axis]);
1064 s[perp] = fabs(s[axis]);
1065 } else {
1067 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1068 Snapper::BBOX_POINT,
1069 _bbox_points,
1070 it,
1071 s[axis],
1072 _origin_for_bboxpoints,
1073 axis,
1074 false);
1076 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1077 Snapper::SNAP_POINT,
1078 _snap_points,
1079 it,
1080 s[axis],
1081 _origin_for_specpoints,
1082 axis,
1083 false);
1085 /* Choose the smaller difference in scale */
1086 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1087 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1088 s[axis] = (bd < sd) ? bb.first : sn.first;
1089 s[perp] = 1;
1090 }
1092 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1093 if (isNaN(pt[X] + pt[Y])) {
1094 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1095 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1096 }
1098 // status text
1099 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1100 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1101 100 * s[NR::X], 100 * s[NR::Y]);
1103 return TRUE;
1104 }
1106 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1107 {
1108 using NR::X;
1109 using NR::Y;
1111 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1112 return FALSE;
1113 }
1115 NR::Dim2 dim_a;
1116 NR::Dim2 dim_b;
1117 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1118 dim_a = X;
1119 dim_b = Y;
1120 } else {
1121 dim_a = Y;
1122 dim_b = X;
1123 }
1125 double skew[2];
1126 double s[2] = { 1.0, 1.0 };
1128 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1129 return FALSE;
1130 }
1132 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1134 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1136 if ( fabs(s[dim_a]) < 1 ) {
1137 s[dim_a] = sign(s[dim_a]);
1138 } else {
1139 s[dim_a] = floor( s[dim_a] + 0.5 );
1140 }
1142 double radians = atan(skew[dim_a] / s[dim_a]);
1144 if (state & GDK_CONTROL_MASK) {
1146 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1148 if (snaps) {
1149 double sections = floor( radians * snaps / M_PI + .5 );
1150 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1151 radians = ( M_PI / snaps ) * sections;
1152 }
1153 skew[dim_a] = tan(radians) * s[dim_a];
1154 } else {
1155 SnapManager const &m = _desktop->namedview->snap_manager;
1157 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1158 _bbox_points,
1159 std::list<SPItem const *>(),
1160 skew[dim_a],
1161 _origin_for_bboxpoints,
1162 dim_b);
1164 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1165 _snap_points,
1166 std::list<SPItem const *>(),
1167 skew[dim_a],
1168 _origin_for_specpoints,
1169 dim_b);
1171 if (bb.second || sn.second) {
1172 /* We snapped something, so change the skew to reflect it */
1173 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1174 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1175 skew[dim_a] = std::min(bd, sd);
1176 }
1177 }
1179 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1180 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1182 /* status text */
1183 double degrees = 180 / M_PI * radians;
1184 if (degrees > 180) degrees -= 360;
1185 if (degrees < -180) degrees += 360;
1187 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1188 // TRANSLATORS: don't modify the first ";"
1189 // (it will NOT be displayed as ";" - only the second one will be)
1190 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1191 degrees);
1193 return TRUE;
1194 }
1196 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1197 {
1198 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1200 // rotate affine in rotate
1201 NR::Point const d1 = _point - _origin;
1202 NR::Point const d2 = pt - _origin;
1204 NR::Coord const h1 = NR::L2(d1);
1205 if (h1 < 1e-15) return FALSE;
1206 NR::Point q1 = d1 / h1;
1207 NR::Coord const h2 = NR::L2(d2);
1208 if (fabs(h2) < 1e-15) return FALSE;
1209 NR::Point q2 = d2 / h2;
1211 double radians;
1212 if (state & GDK_CONTROL_MASK) {
1213 /* Have to restrict movement. */
1214 double cos_t = NR::dot(q1, q2);
1215 double sin_t = NR::dot(NR::rot90(q1), q2);
1216 radians = atan2(sin_t, cos_t);
1217 if (snaps) {
1218 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1219 }
1220 q1 = NR::Point(1, 0);
1221 q2 = NR::Point(cos(radians), sin(radians));
1222 } else {
1223 radians = atan2(NR::dot(NR::rot90(d1), d2),
1224 NR::dot(d1, d2));
1225 }
1227 NR::rotate const r1(q1);
1228 NR::rotate const r2(q2);
1229 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1231 /* status text */
1232 double degrees = 180 / M_PI * radians;
1233 if (degrees > 180) degrees -= 360;
1234 if (degrees < -180) degrees += 360;
1236 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1237 // TRANSLATORS: don't modify the first ";"
1238 // (it will NOT be displayed as ";" - only the second one will be)
1239 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1241 return TRUE;
1242 }
1244 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1245 {
1246 using NR::X;
1247 using NR::Y;
1249 SnapManager const &m = _desktop->namedview->snap_manager;
1250 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1252 if (state & GDK_CONTROL_MASK) {
1253 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1254 pt[Y] = _point[Y];
1255 } else {
1256 pt[X] = _point[X];
1257 }
1258 }
1260 if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
1261 // screen pixels to snap center to bbox
1262 #define SNAP_DIST 5
1263 // FIXME: take from prefs
1264 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1266 for (int i = 0; i < 2; i++) {
1267 if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
1268 pt[i] = _bbox->min()[i];
1269 }
1270 if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
1271 pt[i] = _bbox->midpoint()[i];
1272 }
1273 if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
1274 pt[i] = _bbox->max()[i];
1275 }
1276 }
1277 }
1279 // status text
1280 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1281 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1282 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1283 g_string_free(xs, FALSE);
1284 g_string_free(ys, FALSE);
1286 return TRUE;
1287 }
1289 /*
1290 * handlers for handle movement
1291 *
1292 */
1294 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1295 {
1296 seltrans->stretch(handle, pt, state);
1297 }
1299 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1300 {
1301 seltrans->scale(pt, state);
1302 }
1304 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1305 {
1306 seltrans->skew(handle, pt, state);
1307 }
1309 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1310 {
1311 seltrans->rotate(pt, state);
1312 }
1314 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1315 {
1316 using NR::X;
1317 using NR::Y;
1319 NR::Dim2 dim;
1320 switch (handle.cursor) {
1321 case GDK_LEFT_SIDE:
1322 case GDK_RIGHT_SIDE:
1323 dim = X;
1324 break;
1325 case GDK_TOP_SIDE:
1326 case GDK_BOTTOM_SIDE:
1327 dim = Y;
1328 break;
1329 default:
1330 g_assert_not_reached();
1331 abort();
1332 break;
1333 }
1335 NR::Point const scale_origin(_origin);
1336 double const offset = _point[dim] - scale_origin[dim];
1337 if (!( fabs(offset) >= 1e-15 )) {
1338 return;
1339 }
1340 NR::scale s(1, 1);
1341 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1342 if (isNaN(s[dim])) {
1343 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1344 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1345 }
1346 if (!( fabs(s[dim]) >= 1e-15 )) {
1347 s[dim] = 1e-15;
1348 }
1349 if (state & GDK_CONTROL_MASK) {
1350 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1351 s[!dim] = fabs(s[dim]);
1352 }
1354 if (!_bbox) {
1355 return;
1356 }
1358 //Get two corners of the new bbox
1359 NR::Point p1 = _approximate_bbox->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1360 NR::Point p2 = _approximate_bbox->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1362 //Find the ones at the lower-left and upper-right, as required by get_scale_transform_with_stroke
1363 NR::Point new_bbox_min = NR::Point(std::min(p1[NR::X], p2[NR::X]), std::min(p1[NR::Y], p2[NR::Y]));
1364 NR::Point new_bbox_max = NR::Point(std::max(p1[NR::X], p2[NR::X]), std::max(p1[NR::Y], p2[NR::Y]));
1366 int transform_stroke = false;
1367 gdouble strokewidth = 0;
1369 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1370 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1371 strokewidth = _strokewidth;
1372 }
1374 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1375 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1377 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1378 }
1380 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1381 {
1382 if (!_bbox) {
1383 return;
1384 }
1386 NR::Point const offset = _point - _origin;
1388 NR::scale s (1, 1);
1389 for (int i = NR::X; i <= NR::Y; i++) {
1390 if (fabs(offset[i]) > 1e-9)
1391 s[i] = (pt[i] - _origin[i]) / offset[i];
1392 if (fabs(s[i]) < 1e-9)
1393 s[i] = 1e-9;
1394 }
1396 //Get two corners of the new bbox
1397 NR::Point p1 = _approximate_bbox->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1398 NR::Point p2 = _approximate_bbox->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1400 //Find the ones at the lower-left and upper-right, as required by get_scale_transform_with_stroke
1401 NR::Point new_bbox_min = NR::Point(std::min(p1[NR::X], p2[NR::X]), std::min(p1[NR::Y], p2[NR::Y]));
1402 NR::Point new_bbox_max = NR::Point(std::max(p1[NR::X], p2[NR::X]), std::max(p1[NR::Y], p2[NR::Y]));
1404 int transform_stroke = false;
1405 gdouble strokewidth = 0;
1407 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1408 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1409 strokewidth = _strokewidth;
1410 }
1412 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1413 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1415 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1416 }
1418 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1419 {
1420 NR::Point const offset = _point - _origin;
1422 unsigned dim;
1423 switch (handle.cursor) {
1424 case GDK_SB_H_DOUBLE_ARROW:
1425 dim = NR::Y;
1426 break;
1427 case GDK_SB_V_DOUBLE_ARROW:
1428 dim = NR::X;
1429 break;
1430 default:
1431 g_assert_not_reached();
1432 abort();
1433 break;
1434 }
1435 if (fabs(offset[dim]) < 1e-15) {
1436 return;
1437 }
1438 NR::Matrix skew = NR::identity();
1439 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1440 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1441 skew[2*(1-dim) + (dim)] = 0;
1442 skew[2*(1-dim) + (1-dim)] = 1;
1444 for (int i = 0; i < 2; i++) {
1445 if (fabs(skew[3*i]) < 1e-15) {
1446 skew[3*i] = 1e-15;
1447 }
1448 }
1449 transform(skew, _origin);
1450 }
1452 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1453 {
1454 NR::Point const offset = _point - _origin;
1456 NR::Coord const h1 = NR::L2(offset);
1457 if (h1 < 1e-15) {
1458 return;
1459 }
1460 NR::Point const q1 = offset / h1;
1461 NR::Coord const h2 = NR::L2( pt - _origin );
1462 if (h2 < 1e-15) {
1463 return;
1464 }
1465 NR::Point const q2 = (pt - _origin) / h2;
1466 NR::rotate const r1(q1);
1467 NR::rotate const r2(q2);
1469 NR::Matrix rotate( r2 / r1 );
1470 transform(rotate, _origin);
1471 }
1473 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1474 {
1475 seltrans->setCenter(pt);
1476 }
1479 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1480 {
1481 SnapManager const &m = _desktop->namedview->snap_manager;
1483 /* The amount that we've moved by during this drag */
1484 NR::Point dxy = xy - _point;
1486 /* Get a STL list of the selected items.
1487 ** FIXME: this should probably be done by Inkscape::Selection.
1488 */
1489 std::list<SPItem const*> it;
1490 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1491 it.push_back(reinterpret_cast<SPItem*>(i->data));
1492 }
1494 bool const alt = (state & GDK_MOD1_MASK);
1495 bool const control = (state & GDK_CONTROL_MASK);
1496 bool const shift = (state & GDK_SHIFT_MASK);
1498 if (alt) {
1500 /* Alt pressed means keep offset: snap the moved distance to the grid.
1501 ** FIXME: this will snap to more than just the grid, nowadays.
1502 */
1504 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1506 } else if (!shift) {
1508 /* We're snapping to things, possibly with a constraint to horizontal or
1509 ** vertical movement. Obtain a list of possible translations and then
1510 ** pick the smallest.
1511 */
1513 /* This will be our list of possible translations */
1514 std::list<std::pair<NR::Point, bool> > s;
1516 if (control) {
1518 /* Snap to things, and also constrain to horizontal or vertical movement */
1520 for (unsigned int dim = 0; dim < 2; dim++) {
1521 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1522 _bbox_points,
1523 it,
1524 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1525 dxy));
1527 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1528 _snap_points,
1529 it,
1530 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1531 dxy));
1532 }
1534 } else {
1536 /* Snap to things with no constraint */
1538 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1539 _bbox_points, it, dxy));
1540 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1541 _snap_points, it, dxy));
1542 }
1544 /* Pick one */
1545 NR::Coord best = NR_HUGE;
1546 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1547 if (i->second) {
1548 NR::Coord const m = NR::L2(i->first);
1549 if (m < best) {
1550 best = m;
1551 dxy = i->first;
1552 }
1553 }
1554 }
1555 }
1557 if (control) {
1558 /* Ensure that the horizontal and vertical constraint has been applied */
1559 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1560 dxy[NR::Y] = 0;
1561 } else {
1562 dxy[NR::X] = 0;
1563 }
1564 }
1566 NR::Matrix const move((NR::translate(dxy)));
1567 NR::Point const norm(0, 0);
1568 transform(move, norm);
1570 // status text
1571 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1572 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1573 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("<b>Move</b> by %s, %s; with <b>Ctrl</b> to restrict to horizontal/vertical; with <b>Shift</b> to disable snapping"), xs->str, ys->str);
1574 g_string_free(xs, TRUE);
1575 g_string_free(ys, TRUE);
1576 }
1579 /*
1580 Local Variables:
1581 mode:c++
1582 c-file-style:"stroustrup"
1583 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1584 indent-tabs-mode:nil
1585 fill-column:99
1586 End:
1587 */
1588 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :