3de87ae28e2d343dc7ba18ff0ca34f924fcc633d
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 _snap_bbox_type(SPItem::GEOMETRIC_BBOX),
92 _bbox(NR::Nothing()),
93 _approximate_bbox(NR::Nothing()),
94 _chandle(NULL),
95 _stamp_cache(NULL),
96 _message_context(desktop->messageStack())
97 {
99 g_return_if_fail(desktop != NULL);
101 for (int i = 0; i < 8; i++) {
102 _shandle[i] = NULL;
103 _rhandle[i] = NULL;
104 }
106 _updateVolatileState();
107 _current.set_identity();
109 _center_is_set = false; // reread _center from items, or set to bbox midpoint
111 _updateHandles();
113 _selection = sp_desktop_selection(desktop);
115 _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
116 SP_TYPE_CTRL,
117 "anchor", GTK_ANCHOR_CENTER,
118 "mode", SP_CTRL_MODE_COLOR,
119 "shape", SP_CTRL_SHAPE_BITMAP,
120 "size", 13.0,
121 "filled", TRUE,
122 "fill_color", 0x00000000,
123 "stroked", TRUE,
124 "stroke_color", 0x000000a0,
125 "pixbuf", handles[12],
126 NULL);
128 _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
129 SP_TYPE_CTRL,
130 "anchor", GTK_ANCHOR_CENTER,
131 "mode", SP_CTRL_MODE_XOR,
132 "shape", SP_CTRL_SHAPE_CROSS,
133 "size", 7.0,
134 "filled", TRUE,
135 "fill_color", 0xffffff7f,
136 "stroked", TRUE,
137 "stroke_color", 0xffffffff,
138 "pixbuf", handles[12],
139 NULL);
141 sp_canvas_item_hide(_grip);
142 sp_canvas_item_hide(_norm);
144 for (int i = 0; i < 4; i++) {
145 _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
146 sp_canvas_item_hide(_l[i]);
147 }
149 _sel_changed_connection = _selection->connectChanged(
150 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
151 );
153 _sel_modified_connection = _selection->connectModified(
154 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
155 );
156 }
158 Inkscape::SelTrans::~SelTrans()
159 {
160 _sel_changed_connection.disconnect();
161 _sel_modified_connection.disconnect();
163 for (unsigned int i = 0; i < 8; i++) {
164 if (_shandle[i]) {
165 g_object_unref(G_OBJECT(_shandle[i]));
166 _shandle[i] = NULL;
167 }
168 if (_rhandle[i]) {
169 g_object_unref(G_OBJECT(_rhandle[i]));
170 _rhandle[i] = NULL;
171 }
172 }
173 if (_chandle) {
174 g_object_unref(G_OBJECT(_chandle));
175 _chandle = NULL;
176 }
178 if (_norm) {
179 gtk_object_destroy(GTK_OBJECT(_norm));
180 _norm = NULL;
181 }
182 if (_grip) {
183 gtk_object_destroy(GTK_OBJECT(_grip));
184 _grip = NULL;
185 }
186 for (int i = 0; i < 4; i++) {
187 if (_l[i]) {
188 gtk_object_destroy(GTK_OBJECT(_l[i]));
189 _l[i] = NULL;
190 }
191 }
193 for (unsigned i = 0; i < _items.size(); i++) {
194 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
195 }
197 _items.clear();
198 _items_centers.clear();
199 }
201 void Inkscape::SelTrans::resetState()
202 {
203 _state = STATE_SCALE;
204 }
206 void Inkscape::SelTrans::increaseState()
207 {
208 if (_state == STATE_SCALE) {
209 _state = STATE_ROTATE;
210 } else {
211 _state = STATE_SCALE;
212 }
214 _center_is_set = true; // no need to reread center
216 _updateHandles();
217 }
219 void Inkscape::SelTrans::setCenter(NR::Point const &p)
220 {
221 _center = p;
222 _center_is_set = true;
224 // Write the new center position into all selected items
225 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
226 SPItem *it = (SPItem*)SP_OBJECT(l->data);
227 it->setCenter(p);
228 // only set the value; updating repr and document_done will be done once, on ungrab
229 }
231 _updateHandles();
232 }
234 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
235 {
236 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
238 g_return_if_fail(!_grabbed);
240 _grabbed = true;
241 _show_handles = show_handles;
242 _updateVolatileState();
243 _current.set_identity();
245 _changed = false;
247 if (_empty) {
248 return;
249 }
251 for (GSList const *l = selection->itemList(); l; l = l->next) {
252 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
253 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
254 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
255 }
257 _current.set_identity();
259 _point = p;
261 // The selector tool should snap the bbox and the special snappoints, but not path nodes
262 // (The special points are the handles, center, rotation axis, font baseline, ends of spiral, etc.)
264 // First, determine the bounding box for snapping ...
265 _bbox = selection->bounds(_snap_bbox_type);
266 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX); // Used for correctly scaling the strokewidth
269 // Next, get all special points for snapping
270 _snap_points = selection->getSnapPoints(); // Excludes path nodes
271 std::vector<NR::Point> snap_points_hull = selection->getSnapPointsConvexHull(); // Includes path nodes
272 if (_snap_points.size() > 100) {
273 /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
274 An average user would rarely ever try to snap such a large number of nodes anyway, because
275 (s)he could hardly discern which node would be snapping */
276 _snap_points = snap_points_hull;
277 // Unfortunately, by now we will have lost the font-baseline snappoints :-(
278 }
280 // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
281 // path nodes, for example because a rectangle which has been converted to a path doesn't have
282 // any other special points
283 NR::Rect snap_points_bbox;
284 if ( snap_points_hull.empty() == false ) {
285 std::vector<NR::Point>::iterator i = snap_points_hull.begin();
286 snap_points_bbox = NR::Rect(*i, *i);
287 i++;
288 while (i != snap_points_hull.end()) {
289 snap_points_bbox.expandTo(*i);
290 i++;
291 }
292 }
294 _bbox_points.clear();
295 if (_bbox) {
296 // ... and add the bbox corners to _bbox_points
297 for ( unsigned i = 0 ; i < 4 ; i++ ) {
298 _bbox_points.push_back(_bbox->corner(i));
299 }
300 // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
301 // - one for snapping the boundingbox, which can be either visual or geometric
302 // - one for snapping the special points
303 // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
304 // These distinct "opposites" are needed in the snapmanager to avoid bugs such as #1540195 (in which
305 // a box is caught between to guides)
306 _opposite_for_bboxpoints = _bbox->min() + _bbox->dimensions() * NR::scale(1-x, 1-y);
307 _opposite_for_specpoints = (snap_points_bbox.min() + (snap_points_bbox.dimensions() * NR::scale(1-x, 1-y) ) );
308 // Only a single "opposite" can be used in calculating transformations.
309 _opposite = _opposite_for_bboxpoints;
310 }
312 /*std::cout << "Number of snap points: " << _snap_points.size() << std::endl;
313 for (std::vector<NR::Point>::const_iterator i = _snap_points.begin(); i != _snap_points.end(); i++)
314 {
315 std::cout << " " << *i << std::endl;
316 }
318 std::cout << "Number of bbox points: " << _bbox_points.size() << std::endl;
319 for (std::vector<NR::Point>::const_iterator i = _bbox_points.begin(); i != _bbox_points.end(); i++)
320 {
321 std::cout << " " << *i << std::endl;
322 }*/
324 if ((x != -1) && (y != -1)) {
325 sp_canvas_item_show(_norm);
326 sp_canvas_item_show(_grip);
327 }
329 if (_show == SHOW_OUTLINE) {
330 for (int i = 0; i < 4; i++)
331 sp_canvas_item_show(_l[i]);
332 }
334 _updateHandles();
335 g_return_if_fail(_stamp_cache == NULL);
336 }
338 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
339 {
340 g_return_if_fail(_grabbed);
341 g_return_if_fail(!_empty);
343 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
345 if (_show == SHOW_CONTENT) {
346 // update the content
347 for (unsigned i = 0; i < _items.size(); i++) {
348 SPItem &item = *_items[i].first;
349 NR::Matrix const &prev_transform = _items[i].second;
350 sp_item_set_i2d_affine(&item, prev_transform * affine);
351 }
352 } else {
353 if (_bbox) {
354 NR::Point p[4];
355 /* update the outline */
356 for (unsigned i = 0 ; i < 4 ; i++) {
357 p[i] = _bbox->corner(i) * affine;
358 }
359 for (unsigned i = 0 ; i < 4 ; i++) {
360 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
361 }
362 }
363 }
365 _current = affine;
366 _changed = true;
367 _updateHandles();
368 }
370 void Inkscape::SelTrans::ungrab()
371 {
372 g_return_if_fail(_grabbed);
373 _grabbed = false;
374 _show_handles = true;
376 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
377 _updateVolatileState();
379 for (unsigned i = 0; i < _items.size(); i++) {
380 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
381 }
383 sp_canvas_item_hide(_norm);
384 sp_canvas_item_hide(_grip);
386 if (_show == SHOW_OUTLINE) {
387 for (int i = 0; i < 4; i++)
388 sp_canvas_item_hide(_l[i]);
389 }
391 if (_stamp_cache) {
392 g_slist_free(_stamp_cache);
393 _stamp_cache = NULL;
394 }
396 _message_context.clear();
398 if (!_empty && _changed) {
399 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
400 if (_center) {
401 *_center *= _current;
402 _center_is_set = true;
403 }
405 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
406 // appropriately - it does not know the original positions of the centers (all objects already have
407 // the new bboxes). So we need to reset the centers from our saved array.
408 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
409 for (unsigned i = 0; i < _items_centers.size(); i++) {
410 SPItem *currentItem = _items_centers[i].first;
411 if (currentItem->isCenterSet()) { // only if it's already set
412 currentItem->setCenter (_items_centers[i].second * _current);
413 SP_OBJECT(currentItem)->updateRepr();
414 }
415 }
416 }
418 _items.clear();
419 _items_centers.clear();
421 if (_current.is_translation()) {
422 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
423 _("Move"));
424 } else if (_current.is_scale()) {
425 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
426 _("Scale"));
427 } else if (_current.is_rotation()) {
428 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
429 _("Rotate"));
430 } else {
431 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
432 _("Skew"));
433 }
435 } else {
437 if (_center_is_set) {
438 // we were dragging center; update reprs and commit undoable action
439 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
440 SPItem *it = (SPItem*)SP_OBJECT(l->data);
441 SP_OBJECT(it)->updateRepr();
442 }
443 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
444 _("Set center"));
445 }
447 _items.clear();
448 _items_centers.clear();
449 _updateHandles();
450 }
451 }
453 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
454 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
456 void Inkscape::SelTrans::stamp()
457 {
458 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
460 bool fixup = !_grabbed;
461 if ( fixup && _stamp_cache ) {
462 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
463 g_slist_free(_stamp_cache);
464 _stamp_cache = NULL;
465 }
467 /* stamping mode */
468 if (!_empty) {
469 GSList *l;
470 if (_stamp_cache) {
471 l = _stamp_cache;
472 } else {
473 /* Build cache */
474 l = g_slist_copy((GSList *) selection->itemList());
475 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
476 _stamp_cache = l;
477 }
479 while (l) {
480 SPItem *original_item = SP_ITEM(l->data);
481 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
483 // remember the position of the item
484 gint pos = original_repr->position();
485 // remember parent
486 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
488 Inkscape::XML::Node *copy_repr = original_repr->duplicate(parent->document());
490 // add the new repr to the parent
491 parent->appendChild(copy_repr);
492 // move to the saved position
493 copy_repr->setPosition(pos > 0 ? pos : 0);
495 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
497 NR::Matrix const *new_affine;
498 if (_show == SHOW_OUTLINE) {
499 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
500 NR::Matrix const i2dnew( i2d * _current );
501 sp_item_set_i2d_affine(copy_item, i2dnew);
502 new_affine = ©_item->transform;
503 } else {
504 new_affine = &original_item->transform;
505 }
507 sp_item_write_transform(copy_item, copy_repr, *new_affine);
509 if ( copy_item->isCenterSet() && _center ) {
510 copy_item->setCenter(*_center * _current);
511 }
513 Inkscape::GC::release(copy_repr);
514 l = l->next;
515 }
516 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
517 _("Stamp"));
518 }
520 if ( fixup && _stamp_cache ) {
521 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
522 g_slist_free(_stamp_cache);
523 _stamp_cache = NULL;
524 }
525 }
527 void Inkscape::SelTrans::_updateHandles()
528 {
529 if ( !_show_handles || _empty )
530 {
531 sp_remove_handles(_shandle, 8);
532 sp_remove_handles(_rhandle, 8);
533 sp_remove_handles(&_chandle, 1);
534 return;
535 }
537 // center handle
538 if ( _chandle == NULL ) {
539 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
541 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
542 _chandle->setSize (13);
543 _chandle->setAnchor (handle_center.anchor);
544 _chandle->setMode (SP_CTRL_MODE_XOR);
545 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
546 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
547 _chandle->setPixbuf(handles[handle_center.control]);
548 sp_knot_update_ctrl(_chandle);
550 g_signal_connect(G_OBJECT(_chandle), "request",
551 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
552 g_signal_connect(G_OBJECT(_chandle), "moved",
553 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
554 g_signal_connect(G_OBJECT(_chandle), "grabbed",
555 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
556 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
557 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
558 g_signal_connect(G_OBJECT(_chandle), "clicked",
559 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
560 }
562 sp_remove_handles(&_chandle, 1);
563 if ( _state == STATE_SCALE ) {
564 sp_remove_handles(_rhandle, 8);
565 _showHandles(_shandle, handles_scale, 8,
566 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
567 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
568 } else {
569 sp_remove_handles(_shandle, 8);
570 _showHandles(_rhandle, handles_rotate, 8,
571 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
572 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
573 }
575 if (!_center_is_set) {
576 _center = _desktop->selection->center();
577 _center_is_set = true;
578 }
580 if ( _state == STATE_SCALE || !_center ) {
581 sp_knot_hide(_chandle);
582 } else {
583 sp_knot_show(_chandle);
584 sp_knot_moveto(_chandle, &*_center);
585 }
586 }
588 void Inkscape::SelTrans::_updateVolatileState()
589 {
590 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
591 _empty = selection->isEmpty();
593 if (_empty) {
594 return;
595 }
597 //Update the bboxes
598 _bbox = selection->bounds(_snap_bbox_type);
599 _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX);
601 if (!_bbox) {
602 _empty = true;
603 return;
604 }
606 _strokewidth = stroke_average_width (selection->itemList());
607 }
609 static void sp_remove_handles(SPKnot *knot[], gint num)
610 {
611 for (int i = 0; i < num; i++) {
612 if (knot[i] != NULL) {
613 sp_knot_hide(knot[i]);
614 }
615 }
616 }
618 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
619 gchar const *even_tip, gchar const *odd_tip)
620 {
621 g_return_if_fail( !_empty );
623 for (int i = 0; i < num; i++) {
624 if (knot[i] == NULL) {
625 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
627 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
628 knot[i]->setSize (13);
629 knot[i]->setAnchor (handle[i].anchor);
630 knot[i]->setMode (SP_CTRL_MODE_XOR);
631 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
632 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
633 knot[i]->setPixbuf(handles[handle[i].control]);
634 sp_knot_update_ctrl(knot[i]);
636 g_signal_connect(G_OBJECT(knot[i]), "request",
637 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
638 g_signal_connect(G_OBJECT(knot[i]), "moved",
639 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
640 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
641 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
642 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
643 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
644 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
645 }
646 sp_knot_show(knot[i]);
648 NR::Point const handle_pt(handle[i].x, handle[i].y);
649 // shouldn't have nullary bbox, but knots
650 g_assert(_bbox);
651 NR::Point p( _bbox->min()
652 + ( _bbox->dimensions()
653 * NR::scale(handle_pt) ) );
655 sp_knot_moveto(knot[i], &p);
656 }
657 }
659 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
660 {
661 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
662 knot, state, *(SPSelTransHandle const *) data
663 );
664 }
666 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
667 {
668 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
669 }
671 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
672 {
673 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
674 knot, position, state, *(SPSelTransHandle const *) data
675 );
676 }
678 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
679 {
680 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
681 knot, position, state, *(SPSelTransHandle const *) data
682 );
683 }
685 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
686 {
687 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
688 knot, state, *(SPSelTransHandle const *) data
689 );
690 }
692 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
693 {
694 switch (handle.anchor) {
695 case GTK_ANCHOR_CENTER:
696 if (state & GDK_SHIFT_MASK) {
697 // Unset the center position for all selected items
698 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
699 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
700 it->unsetCenter();
701 SP_OBJECT(it)->updateRepr();
702 _center_is_set = false; // center has changed
703 _updateHandles();
704 }
705 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
706 _("Reset center"));
707 }
708 break;
709 default:
710 break;
711 }
712 }
714 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
715 {
716 switch (handle.anchor) {
717 case GTK_ANCHOR_CENTER:
718 g_object_set(G_OBJECT(_grip),
719 "shape", SP_CTRL_SHAPE_BITMAP,
720 "size", 13.0,
721 NULL);
722 sp_canvas_item_show(_grip);
723 break;
724 default:
725 g_object_set(G_OBJECT(_grip),
726 "shape", SP_CTRL_SHAPE_CROSS,
727 "size", 7.0,
728 NULL);
729 sp_canvas_item_show(_norm);
730 sp_canvas_item_show(_grip);
732 break;
733 }
735 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
736 }
739 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
740 {
741 if (!SP_KNOT_IS_GRABBED(knot)) {
742 return;
743 }
745 // in case items have been unhooked from the document, don't
746 // try to continue processing events for them.
747 for (unsigned int i = 0; i < _items.size(); i++) {
748 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
749 return;
750 }
751 }
753 handle.action(this, handle, *position, state);
754 }
757 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
758 {
759 if (!SP_KNOT_IS_GRABBED(knot)) {
760 return TRUE;
761 }
763 knot->desktop->setPosition(*position);
765 if (state & GDK_MOD1_MASK) {
766 *position = _point + ( *position - _point ) / 10;
767 }
769 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
770 _origin = _opposite;
771 _origin_for_bboxpoints = _opposite_for_bboxpoints;
772 _origin_for_specpoints = _opposite_for_specpoints;
773 } else if (_center) {
774 _origin = *_center;
775 _origin_for_bboxpoints = *_center;
776 _origin_for_specpoints = *_center;
777 } else {
778 // FIXME
779 return TRUE;
780 }
781 if (handle.request(this, handle, *position, state)) {
782 sp_knot_set_position(knot, position, state);
783 SP_CTRL(_grip)->moveto(*position);
784 SP_CTRL(_norm)->moveto(_origin);
785 }
787 return TRUE;
788 }
791 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
792 {
793 if (!_grabbed) {
794 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
795 _snap_bbox_type = (prefs_bbox == NULL || strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
796 //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
798 _updateVolatileState();
799 _current.set_identity();
800 _center_is_set = false; // center(s) may have changed
801 _updateHandles();
802 }
803 }
805 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
806 {
807 if (!_grabbed) {
808 _updateVolatileState();
809 _current.set_identity();
811 // reset internal flag
812 _changed = false;
814 _center_is_set = false; // center(s) may have changed
816 _updateHandles();
817 }
818 }
820 /*
821 * handlers for handle move-request
822 */
824 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
825 static double sign(double const x)
826 {
827 return ( x < 0
828 ? -1
829 : 1 );
830 }
832 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
833 SPSelTransHandle const &, NR::Point &pt, guint state)
834 {
835 return seltrans->scaleRequest(pt, state);
836 }
838 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
839 SPSelTransHandle const &handle, NR::Point &pt, guint state)
840 {
841 return seltrans->stretchRequest(handle, pt, state);
842 }
844 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
845 SPSelTransHandle const &handle, NR::Point &pt, guint state)
846 {
847 return seltrans->skewRequest(handle, pt, state);
848 }
850 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
851 SPSelTransHandle const &, NR::Point &pt, guint state)
852 {
853 return seltrans->rotateRequest(pt, state);
854 }
856 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
857 SPSelTransHandle const &, NR::Point &pt, guint state)
858 {
859 return seltrans->centerRequest(pt, state);
860 }
862 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
863 {
864 using NR::X;
865 using NR::Y;
867 NR::Point d = _point - _origin;
868 NR::scale s(0, 0);
870 /* Work out the new scale factors `s' */
871 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
872 if ( fabs(d[i]) > 0.001 ) {
873 s[i] = ( pt[i] - _origin[i] ) / d[i];
874 if ( fabs(s[i]) < 1e-9 ) {
875 s[i] = 1e-9;
876 }
877 }
878 }
880 SnapManager const &m = _desktop->namedview->snap_manager;
882 /* Get a STL list of the selected items.
883 ** FIXME: this should probably be done by Inkscape::Selection.
884 */
885 std::list<SPItem const*> it;
886 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
887 it.push_back(reinterpret_cast<SPItem*>(i->data));
888 }
890 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
891 // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
892 //
893 // The aspect-ratio must be locked before snapping
894 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
895 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
896 } else {
897 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
898 }
900 // Snap along a suitable constraint vector from the origin.
902 // The inclination of the constraint vector is calculated from the aspect ratio
903 NR::Point bbox_dim = _bbox->dimensions();
904 double const aspect_ratio = bbox_dim[1] / bbox_dim[0]; // = height / width
906 // Determine direction of the constraint vector
907 NR::Point const cv = NR::Point(
908 pt[NR::X] > _origin[NR::X] ? 1 : -1,
909 pt[NR::Y] > _origin[NR::Y] ? aspect_ratio : -aspect_ratio
910 );
912 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
913 _bbox_points,
914 it,
915 Snapper::ConstraintLine(_origin_for_bboxpoints, cv),
916 s,
917 _origin_for_bboxpoints);
919 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
920 _snap_points,
921 it,
922 Snapper::ConstraintLine(_origin_for_specpoints, cv),
923 s,
924 _origin_for_specpoints);
926 if (bb.second == false && sn.second == false) {
927 /* We didn't snap, so just keep the locked aspect ratio */
928 } else {
929 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
930 ** just compare difference in s[X].
931 */
932 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
933 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
934 s = (bd < sd) ? bb.first : sn.first;
935 }
937 } else {
938 /* Scale aspect ratio is unlocked */
940 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
941 _bbox_points,
942 it,
943 s,
944 _origin_for_bboxpoints);
945 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
946 _snap_points,
947 it,
948 s,
949 _origin_for_specpoints);
951 /* Pick the snap that puts us closest to the original scale */
952 NR::Coord bd = bb.second ?
953 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
954 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
955 : NR_HUGE;
956 NR::Coord sd = sn.second ?
957 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
958 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
959 : NR_HUGE;
960 s = (bd < sd) ? bb.first : sn.first;
961 }
963 /* Update the knot position */
964 pt = ( _point - _origin ) * s + _origin;
966 /* Status text */
967 _message_context.setF(Inkscape::NORMAL_MESSAGE,
968 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
969 100 * s[NR::X], 100 * s[NR::Y]);
971 return TRUE;
972 }
974 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
975 {
976 using NR::X;
977 using NR::Y;
979 NR::Dim2 axis, perp;
981 switch (handle.cursor) {
982 case GDK_TOP_SIDE:
983 case GDK_BOTTOM_SIDE:
984 axis = NR::Y;
985 perp = NR::X;
986 break;
987 case GDK_LEFT_SIDE:
988 case GDK_RIGHT_SIDE:
989 axis = NR::X;
990 perp = NR::Y;
991 break;
992 default:
993 g_assert_not_reached();
994 return TRUE;
995 };
997 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
998 return FALSE;
999 }
1001 NR::scale s(1, 1);
1002 s[axis] = ( ( pt[axis] - _origin[axis] )
1003 / ( _point[axis] - _origin[axis] ) );
1004 if ( fabs(s[axis]) < 1e-15 ) {
1005 s[axis] = 1e-15;
1006 }
1008 /* Get a STL list of the selected items.
1009 ** FIXME: this should probably be done by Inkscape::Selection.
1010 */
1011 std::list<SPItem const*> it;
1012 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1013 it.push_back(reinterpret_cast<SPItem*>(i->data));
1014 }
1016 SnapManager const &m = _desktop->namedview->snap_manager;
1018 if ( state & GDK_CONTROL_MASK ) {
1019 // on ctrl, apply symmetrical scaling instead of stretching
1020 s[perp] = fabs(s[axis]);
1022 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1023 Snapper::BBOX_POINT,
1024 _bbox_points,
1025 it,
1026 s[axis],
1027 _origin_for_bboxpoints,
1028 axis,
1029 true);
1031 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1032 Snapper::SNAP_POINT,
1033 _snap_points,
1034 it,
1035 s[axis],
1036 _origin_for_specpoints,
1037 axis,
1038 true);
1040 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1041 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1042 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
1044 s[axis] = fabs(ratio) * sign(s[axis]);
1045 s[perp] = fabs(s[axis]);
1046 } else {
1048 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1049 Snapper::BBOX_POINT,
1050 _bbox_points,
1051 it,
1052 s[axis],
1053 _origin_for_bboxpoints,
1054 axis,
1055 false);
1057 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1058 Snapper::SNAP_POINT,
1059 _snap_points,
1060 it,
1061 s[axis],
1062 _origin_for_specpoints,
1063 axis,
1064 false);
1066 /* Choose the smaller difference in scale */
1067 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1068 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1069 s[axis] = (bd < sd) ? bb.first : sn.first;
1070 s[perp] = 1;
1071 }
1073 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1074 if (isNaN(pt[X] + pt[Y])) {
1075 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1076 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1077 }
1079 // status text
1080 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1081 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1082 100 * s[NR::X], 100 * s[NR::Y]);
1084 return TRUE;
1085 }
1087 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1088 {
1089 using NR::X;
1090 using NR::Y;
1092 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1093 return FALSE;
1094 }
1096 NR::Dim2 dim_a;
1097 NR::Dim2 dim_b;
1098 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1099 dim_a = X;
1100 dim_b = Y;
1101 } else {
1102 dim_a = Y;
1103 dim_b = X;
1104 }
1106 double skew[2];
1107 double s[2] = { 1.0, 1.0 };
1109 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1110 return FALSE;
1111 }
1113 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1115 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1117 if ( fabs(s[dim_a]) < 1 ) {
1118 s[dim_a] = sign(s[dim_a]);
1119 } else {
1120 s[dim_a] = floor( s[dim_a] + 0.5 );
1121 }
1123 double radians = atan(skew[dim_a] / s[dim_a]);
1125 if (state & GDK_CONTROL_MASK) {
1127 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1129 if (snaps) {
1130 double sections = floor( radians * snaps / M_PI + .5 );
1131 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1132 radians = ( M_PI / snaps ) * sections;
1133 }
1134 skew[dim_a] = tan(radians) * s[dim_a];
1135 } else {
1136 SnapManager const &m = _desktop->namedview->snap_manager;
1138 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1139 _bbox_points,
1140 std::list<SPItem const *>(),
1141 skew[dim_a],
1142 _origin_for_bboxpoints,
1143 dim_b);
1145 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1146 _snap_points,
1147 std::list<SPItem const *>(),
1148 skew[dim_a],
1149 _origin_for_specpoints,
1150 dim_b);
1152 if (bb.second || sn.second) {
1153 /* We snapped something, so change the skew to reflect it */
1154 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1155 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1156 skew[dim_a] = std::min(bd, sd);
1157 }
1158 }
1160 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1161 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1163 /* status text */
1164 double degrees = 180 / M_PI * radians;
1165 if (degrees > 180) degrees -= 360;
1166 if (degrees < -180) degrees += 360;
1168 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1169 // TRANSLATORS: don't modify the first ";"
1170 // (it will NOT be displayed as ";" - only the second one will be)
1171 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1172 degrees);
1174 return TRUE;
1175 }
1177 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1178 {
1179 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1181 // rotate affine in rotate
1182 NR::Point const d1 = _point - _origin;
1183 NR::Point const d2 = pt - _origin;
1185 NR::Coord const h1 = NR::L2(d1);
1186 if (h1 < 1e-15) return FALSE;
1187 NR::Point q1 = d1 / h1;
1188 NR::Coord const h2 = NR::L2(d2);
1189 if (fabs(h2) < 1e-15) return FALSE;
1190 NR::Point q2 = d2 / h2;
1192 double radians;
1193 if (state & GDK_CONTROL_MASK) {
1194 /* Have to restrict movement. */
1195 double cos_t = NR::dot(q1, q2);
1196 double sin_t = NR::dot(NR::rot90(q1), q2);
1197 radians = atan2(sin_t, cos_t);
1198 if (snaps) {
1199 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1200 }
1201 q1 = NR::Point(1, 0);
1202 q2 = NR::Point(cos(radians), sin(radians));
1203 } else {
1204 radians = atan2(NR::dot(NR::rot90(d1), d2),
1205 NR::dot(d1, d2));
1206 }
1208 NR::rotate const r1(q1);
1209 NR::rotate const r2(q2);
1210 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1212 /* status text */
1213 double degrees = 180 / M_PI * radians;
1214 if (degrees > 180) degrees -= 360;
1215 if (degrees < -180) degrees += 360;
1217 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1218 // TRANSLATORS: don't modify the first ";"
1219 // (it will NOT be displayed as ";" - only the second one will be)
1220 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1222 return TRUE;
1223 }
1225 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1226 {
1227 using NR::X;
1228 using NR::Y;
1230 SnapManager const &m = _desktop->namedview->snap_manager;
1231 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1233 if (state & GDK_CONTROL_MASK) {
1234 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1235 pt[Y] = _point[Y];
1236 } else {
1237 pt[X] = _point[X];
1238 }
1239 }
1241 if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
1242 // screen pixels to snap center to bbox
1243 #define SNAP_DIST 5
1244 // FIXME: take from prefs
1245 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1247 for (int i = 0; i < 2; i++) {
1248 if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
1249 pt[i] = _bbox->min()[i];
1250 }
1251 if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
1252 pt[i] = _bbox->midpoint()[i];
1253 }
1254 if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
1255 pt[i] = _bbox->max()[i];
1256 }
1257 }
1258 }
1260 // status text
1261 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1262 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1263 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1264 g_string_free(xs, FALSE);
1265 g_string_free(ys, FALSE);
1267 return TRUE;
1268 }
1270 /*
1271 * handlers for handle movement
1272 *
1273 */
1275 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1276 {
1277 seltrans->stretch(handle, pt, state);
1278 }
1280 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1281 {
1282 seltrans->scale(pt, state);
1283 }
1285 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1286 {
1287 seltrans->skew(handle, pt, state);
1288 }
1290 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1291 {
1292 seltrans->rotate(pt, state);
1293 }
1295 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1296 {
1297 using NR::X;
1298 using NR::Y;
1300 NR::Dim2 dim;
1301 switch (handle.cursor) {
1302 case GDK_LEFT_SIDE:
1303 case GDK_RIGHT_SIDE:
1304 dim = X;
1305 break;
1306 case GDK_TOP_SIDE:
1307 case GDK_BOTTOM_SIDE:
1308 dim = Y;
1309 break;
1310 default:
1311 g_assert_not_reached();
1312 abort();
1313 break;
1314 }
1316 NR::Point const scale_origin(_origin);
1317 double const offset = _point[dim] - scale_origin[dim];
1318 if (!( fabs(offset) >= 1e-15 )) {
1319 return;
1320 }
1321 NR::scale s(1, 1);
1322 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1323 if (isNaN(s[dim])) {
1324 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1325 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1326 }
1327 if (!( fabs(s[dim]) >= 1e-15 )) {
1328 s[dim] = 1e-15;
1329 }
1330 if (state & GDK_CONTROL_MASK) {
1331 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1332 s[!dim] = fabs(s[dim]);
1333 }
1335 if (!_bbox) {
1336 return;
1337 }
1339 //Get two corners of the new bbox
1340 NR::Point p1 = _approximate_bbox->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1341 NR::Point p2 = _approximate_bbox->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1343 //Find the ones at the lower-left and upper-right, as required by get_scale_transform_with_stroke
1344 NR::Point new_bbox_min = NR::Point(std::min(p1[NR::X], p2[NR::X]), std::min(p1[NR::Y], p2[NR::Y]));
1345 NR::Point new_bbox_max = NR::Point(std::max(p1[NR::X], p2[NR::X]), std::max(p1[NR::Y], p2[NR::Y]));
1347 int transform_stroke = false;
1348 gdouble strokewidth = 0;
1350 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1351 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1352 strokewidth = _strokewidth;
1353 }
1355 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1356 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1358 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1359 }
1361 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1362 {
1363 if (!_bbox) {
1364 return;
1365 }
1367 NR::Point const offset = _point - _origin;
1369 NR::scale s (1, 1);
1370 for (int i = NR::X; i <= NR::Y; i++) {
1371 if (fabs(offset[i]) > 1e-9)
1372 s[i] = (pt[i] - _origin[i]) / offset[i];
1373 if (fabs(s[i]) < 1e-9)
1374 s[i] = 1e-9;
1375 }
1377 //Get two corners of the new bbox
1378 NR::Point p1 = _approximate_bbox->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1379 NR::Point p2 = _approximate_bbox->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1381 //Find the ones at the lower-left and upper-right, as required by get_scale_transform_with_stroke
1382 NR::Point new_bbox_min = NR::Point(std::min(p1[NR::X], p2[NR::X]), std::min(p1[NR::Y], p2[NR::Y]));
1383 NR::Point new_bbox_max = NR::Point(std::max(p1[NR::X], p2[NR::X]), std::max(p1[NR::Y], p2[NR::Y]));
1385 int transform_stroke = false;
1386 gdouble strokewidth = 0;
1388 if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
1389 transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1390 strokewidth = _strokewidth;
1391 }
1393 NR::Matrix scaler = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
1394 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1396 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1397 }
1399 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1400 {
1401 NR::Point const offset = _point - _origin;
1403 unsigned dim;
1404 switch (handle.cursor) {
1405 case GDK_SB_H_DOUBLE_ARROW:
1406 dim = NR::Y;
1407 break;
1408 case GDK_SB_V_DOUBLE_ARROW:
1409 dim = NR::X;
1410 break;
1411 default:
1412 g_assert_not_reached();
1413 abort();
1414 break;
1415 }
1416 if (fabs(offset[dim]) < 1e-15) {
1417 return;
1418 }
1419 NR::Matrix skew = NR::identity();
1420 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1421 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1422 skew[2*(1-dim) + (dim)] = 0;
1423 skew[2*(1-dim) + (1-dim)] = 1;
1425 for (int i = 0; i < 2; i++) {
1426 if (fabs(skew[3*i]) < 1e-15) {
1427 skew[3*i] = 1e-15;
1428 }
1429 }
1430 transform(skew, _origin);
1431 }
1433 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1434 {
1435 NR::Point const offset = _point - _origin;
1437 NR::Coord const h1 = NR::L2(offset);
1438 if (h1 < 1e-15) {
1439 return;
1440 }
1441 NR::Point const q1 = offset / h1;
1442 NR::Coord const h2 = NR::L2( pt - _origin );
1443 if (h2 < 1e-15) {
1444 return;
1445 }
1446 NR::Point const q2 = (pt - _origin) / h2;
1447 NR::rotate const r1(q1);
1448 NR::rotate const r2(q2);
1450 NR::Matrix rotate( r2 / r1 );
1451 transform(rotate, _origin);
1452 }
1454 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1455 {
1456 seltrans->setCenter(pt);
1457 }
1460 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1461 {
1462 SnapManager const &m = _desktop->namedview->snap_manager;
1464 /* The amount that we've moved by during this drag */
1465 NR::Point dxy = xy - _point;
1467 /* Get a STL list of the selected items.
1468 ** FIXME: this should probably be done by Inkscape::Selection.
1469 */
1470 std::list<SPItem const*> it;
1471 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1472 it.push_back(reinterpret_cast<SPItem*>(i->data));
1473 }
1475 bool const alt = (state & GDK_MOD1_MASK);
1476 bool const control = (state & GDK_CONTROL_MASK);
1477 bool const shift = (state & GDK_SHIFT_MASK);
1479 if (alt) {
1481 /* Alt pressed means keep offset: snap the moved distance to the grid.
1482 ** FIXME: this will snap to more than just the grid, nowadays.
1483 */
1485 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1487 } else if (!shift) {
1489 /* We're snapping to things, possibly with a constraint to horizontal or
1490 ** vertical movement. Obtain a list of possible translations and then
1491 ** pick the smallest.
1492 */
1494 /* This will be our list of possible translations */
1495 std::list<std::pair<NR::Point, bool> > s;
1497 if (control) {
1499 /* Snap to things, and also constrain to horizontal or vertical movement */
1501 for (unsigned int dim = 0; dim < 2; dim++) {
1502 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1503 _bbox_points,
1504 it,
1505 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1506 dxy));
1508 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1509 _snap_points,
1510 it,
1511 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1512 dxy));
1513 }
1515 } else {
1517 /* Snap to things with no constraint */
1519 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1520 _bbox_points, it, dxy));
1521 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1522 _snap_points, it, dxy));
1523 }
1525 /* Pick one */
1526 NR::Coord best = NR_HUGE;
1527 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1528 if (i->second) {
1529 NR::Coord const m = NR::L2(i->first);
1530 if (m < best) {
1531 best = m;
1532 dxy = i->first;
1533 }
1534 }
1535 }
1536 }
1538 if (control) {
1539 /* Ensure that the horizontal and vertical constraint has been applied */
1540 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1541 dxy[NR::Y] = 0;
1542 } else {
1543 dxy[NR::X] = 0;
1544 }
1545 }
1547 NR::Matrix const move((NR::translate(dxy)));
1548 NR::Point const norm(0, 0);
1549 transform(move, norm);
1551 // status text
1552 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1553 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1554 _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);
1555 g_string_free(xs, TRUE);
1556 g_string_free(ys, TRUE);
1557 }
1560 /*
1561 Local Variables:
1562 mode:c++
1563 c-file-style:"stroustrup"
1564 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1565 indent-tabs-mode:nil
1566 fill-column:99
1567 End:
1568 */
1569 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :