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 _box(NR::Nothing()),
92 _chandle(NULL),
93 _stamp_cache(NULL),
94 _message_context(desktop->messageStack())
95 {
96 g_return_if_fail(desktop != NULL);
98 for (int i = 0; i < 8; i++) {
99 _shandle[i] = NULL;
100 _rhandle[i] = NULL;
101 }
103 _updateVolatileState();
104 _current.set_identity();
106 _center_is_set = false; // reread _center from items, or set to bbox midpoint
108 _updateHandles();
110 _selection = sp_desktop_selection(desktop);
112 _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
113 SP_TYPE_CTRL,
114 "anchor", GTK_ANCHOR_CENTER,
115 "mode", SP_CTRL_MODE_COLOR,
116 "shape", SP_CTRL_SHAPE_BITMAP,
117 "size", 13.0,
118 "filled", TRUE,
119 "fill_color", 0x00000000,
120 "stroked", TRUE,
121 "stroke_color", 0x000000a0,
122 "pixbuf", handles[12],
123 NULL);
125 _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
126 SP_TYPE_CTRL,
127 "anchor", GTK_ANCHOR_CENTER,
128 "mode", SP_CTRL_MODE_XOR,
129 "shape", SP_CTRL_SHAPE_CROSS,
130 "size", 7.0,
131 "filled", TRUE,
132 "fill_color", 0xffffff7f,
133 "stroked", TRUE,
134 "stroke_color", 0xffffffff,
135 "pixbuf", handles[12],
136 NULL);
138 sp_canvas_item_hide(_grip);
139 sp_canvas_item_hide(_norm);
141 for (int i = 0; i < 4; i++) {
142 _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
143 sp_canvas_item_hide(_l[i]);
144 }
146 _sel_changed_connection = _selection->connectChanged(
147 sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
148 );
150 _sel_modified_connection = _selection->connectModified(
151 sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
152 );
153 }
155 Inkscape::SelTrans::~SelTrans()
156 {
157 _sel_changed_connection.disconnect();
158 _sel_modified_connection.disconnect();
160 for (unsigned int i = 0; i < 8; i++) {
161 if (_shandle[i]) {
162 g_object_unref(G_OBJECT(_shandle[i]));
163 _shandle[i] = NULL;
164 }
165 if (_rhandle[i]) {
166 g_object_unref(G_OBJECT(_rhandle[i]));
167 _rhandle[i] = NULL;
168 }
169 }
170 if (_chandle) {
171 g_object_unref(G_OBJECT(_chandle));
172 _chandle = NULL;
173 }
175 if (_norm) {
176 gtk_object_destroy(GTK_OBJECT(_norm));
177 _norm = NULL;
178 }
179 if (_grip) {
180 gtk_object_destroy(GTK_OBJECT(_grip));
181 _grip = NULL;
182 }
183 for (int i = 0; i < 4; i++) {
184 if (_l[i]) {
185 gtk_object_destroy(GTK_OBJECT(_l[i]));
186 _l[i] = NULL;
187 }
188 }
190 for (unsigned i = 0; i < _items.size(); i++) {
191 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
192 }
194 _items.clear();
195 _items_centers.clear();
196 }
198 void Inkscape::SelTrans::resetState()
199 {
200 _state = STATE_SCALE;
201 }
203 void Inkscape::SelTrans::increaseState()
204 {
205 if (_state == STATE_SCALE) {
206 _state = STATE_ROTATE;
207 } else {
208 _state = STATE_SCALE;
209 }
211 _center_is_set = true; // no need to reread center
213 _updateHandles();
214 }
216 void Inkscape::SelTrans::setCenter(NR::Point const &p)
217 {
218 _center = p;
219 _center_is_set = true;
221 // Write the new center position into all selected items
222 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
223 SPItem *it = (SPItem*)SP_OBJECT(l->data);
224 it->setCenter(p);
225 // only set the value; updating repr and document_done will be done once, on ungrab
226 }
228 _updateHandles();
229 }
231 void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool show_handles)
232 {
233 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
235 g_return_if_fail(!_grabbed);
237 _grabbed = true;
238 _show_handles = show_handles;
239 _updateVolatileState();
240 _current.set_identity();
242 _changed = false;
244 if (_empty) {
245 return;
246 }
248 for (GSList const *l = selection->itemList(); l; l = l->next) {
249 SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
250 _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
251 _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
252 }
254 _current.set_identity();
256 _point = p;
258 _snap_points = selection->getSnapPointsConvexHull();
259 _box = selection->bounds();
260 _bbox_points.clear();
261 if (_box) {
262 for ( unsigned i = 0 ; i < 4 ; i++ ) {
263 _bbox_points.push_back(_box->corner(i));
264 }
265 }
267 gchar const *scale_origin = prefs_get_string_attribute("tools.select", "scale_origin");
268 bool const origin_on_bbox = (scale_origin == NULL || !strcmp(scale_origin, "bbox"));
270 if (_box) {
271 NR::Rect op_box = *_box;
272 // FIXME: should be using ConvexHull here
273 if (origin_on_bbox == false && _snap_points.empty() == false) {
274 std::vector<NR::Point>::iterator i = _snap_points.begin();
275 op_box = NR::Rect(*i, *i);
276 i++;
277 while (i != _snap_points.end()) {
278 op_box.expandTo(*i);
279 i++;
280 }
281 }
283 _opposite = ( op_box.min() + ( op_box.dimensions() * NR::scale(1-x, 1-y) ) );
284 }
287 if ((x != -1) && (y != -1)) {
288 sp_canvas_item_show(_norm);
289 sp_canvas_item_show(_grip);
290 }
292 if (_show == SHOW_OUTLINE) {
293 for (int i = 0; i < 4; i++)
294 sp_canvas_item_show(_l[i]);
295 }
297 _updateHandles();
298 g_return_if_fail(_stamp_cache == NULL);
299 }
301 void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const &norm)
302 {
303 g_return_if_fail(_grabbed);
304 g_return_if_fail(!_empty);
306 NR::Matrix const affine( NR::translate(-norm) * rel_affine * NR::translate(norm) );
308 if (_show == SHOW_CONTENT) {
309 // update the content
310 for (unsigned i = 0; i < _items.size(); i++) {
311 SPItem &item = *_items[i].first;
312 NR::Matrix const &prev_transform = _items[i].second;
313 sp_item_set_i2d_affine(&item, prev_transform * affine);
314 }
315 } else {
316 if (_box) {
317 NR::Point p[4];
318 /* update the outline */
319 for (unsigned i = 0 ; i < 4 ; i++) {
320 p[i] = _box->corner(i) * affine;
321 }
322 for (unsigned i = 0 ; i < 4 ; i++) {
323 sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
324 }
325 }
326 }
328 _current = affine;
329 _changed = true;
330 _updateHandles();
331 }
333 void Inkscape::SelTrans::ungrab()
334 {
335 g_return_if_fail(_grabbed);
336 _grabbed = false;
337 _show_handles = true;
339 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
340 _updateVolatileState();
342 for (unsigned i = 0; i < _items.size(); i++) {
343 sp_object_unref(SP_OBJECT(_items[i].first), NULL);
344 }
346 sp_canvas_item_hide(_norm);
347 sp_canvas_item_hide(_grip);
349 if (_show == SHOW_OUTLINE) {
350 for (int i = 0; i < 4; i++)
351 sp_canvas_item_hide(_l[i]);
352 }
354 if (_stamp_cache) {
355 g_slist_free(_stamp_cache);
356 _stamp_cache = NULL;
357 }
359 _message_context.clear();
361 if (!_empty && _changed) {
362 sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
363 if (_center) {
364 *_center *= _current;
365 _center_is_set = true;
366 }
368 // If dragging showed content live, sp_selection_apply_affine cannot change the centers
369 // appropriately - it does not know the original positions of the centers (all objects already have
370 // the new bboxes). So we need to reset the centers from our saved array.
371 if (_show != SHOW_OUTLINE && !_current.is_translation()) {
372 for (unsigned i = 0; i < _items_centers.size(); i++) {
373 SPItem *currentItem = _items_centers[i].first;
374 if (currentItem->isCenterSet()) { // only if it's already set
375 currentItem->setCenter (_items_centers[i].second * _current);
376 SP_OBJECT(currentItem)->updateRepr();
377 }
378 }
379 }
381 _items.clear();
382 _items_centers.clear();
384 if (_current.is_translation()) {
385 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
386 _("Move"));
387 } else if (_current.is_scale()) {
388 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
389 _("Scale"));
390 } else if (_current.is_rotation()) {
391 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
392 _("Rotate"));
393 } else {
394 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
395 _("Skew"));
396 }
398 } else {
400 if (_center_is_set) {
401 // we were dragging center; update reprs and commit undoable action
402 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
403 SPItem *it = (SPItem*)SP_OBJECT(l->data);
404 SP_OBJECT(it)->updateRepr();
405 }
406 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
407 _("Set center"));
408 }
410 _items.clear();
411 _items_centers.clear();
412 _updateHandles();
413 }
414 }
416 /* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
417 /* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
419 void Inkscape::SelTrans::stamp()
420 {
421 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
423 bool fixup = !_grabbed;
424 if ( fixup && _stamp_cache ) {
425 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
426 g_slist_free(_stamp_cache);
427 _stamp_cache = NULL;
428 }
430 /* stamping mode */
431 if (!_empty) {
432 GSList *l;
433 if (_stamp_cache) {
434 l = _stamp_cache;
435 } else {
436 /* Build cache */
437 l = g_slist_copy((GSList *) selection->itemList());
438 l = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
439 _stamp_cache = l;
440 }
442 while (l) {
443 SPItem *original_item = SP_ITEM(l->data);
444 Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
446 // remember the position of the item
447 gint pos = original_repr->position();
448 // remember parent
449 Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
451 Inkscape::XML::Node *copy_repr = original_repr->duplicate();
453 // add the new repr to the parent
454 parent->appendChild(copy_repr);
455 // move to the saved position
456 copy_repr->setPosition(pos > 0 ? pos : 0);
458 SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
460 NR::Matrix const *new_affine;
461 if (_show == SHOW_OUTLINE) {
462 NR::Matrix const i2d(sp_item_i2d_affine(original_item));
463 NR::Matrix const i2dnew( i2d * _current );
464 sp_item_set_i2d_affine(copy_item, i2dnew);
465 new_affine = ©_item->transform;
466 } else {
467 new_affine = &original_item->transform;
468 }
470 sp_item_write_transform(copy_item, copy_repr, *new_affine);
472 if ( copy_item->isCenterSet() && _center ) {
473 copy_item->setCenter(*_center * _current);
474 }
476 Inkscape::GC::release(copy_repr);
477 l = l->next;
478 }
479 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
480 _("Stamp"));
481 }
483 if ( fixup && _stamp_cache ) {
484 // TODO - give a proper fix. Simple temproary work-around for the grab() issue
485 g_slist_free(_stamp_cache);
486 _stamp_cache = NULL;
487 }
488 }
490 void Inkscape::SelTrans::_updateHandles()
491 {
492 if ( !_show_handles || _empty )
493 {
494 sp_remove_handles(_shandle, 8);
495 sp_remove_handles(_rhandle, 8);
496 sp_remove_handles(&_chandle, 1);
497 return;
498 }
500 // center handle
501 if ( _chandle == NULL ) {
502 _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
504 _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
505 _chandle->setSize (13);
506 _chandle->setAnchor (handle_center.anchor);
507 _chandle->setMode (SP_CTRL_MODE_XOR);
508 _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
509 _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
510 _chandle->setPixbuf(handles[handle_center.control]);
511 sp_knot_update_ctrl(_chandle);
513 g_signal_connect(G_OBJECT(_chandle), "request",
514 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
515 g_signal_connect(G_OBJECT(_chandle), "moved",
516 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
517 g_signal_connect(G_OBJECT(_chandle), "grabbed",
518 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
519 g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
520 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
521 g_signal_connect(G_OBJECT(_chandle), "clicked",
522 G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
523 }
525 sp_remove_handles(&_chandle, 1);
526 if ( _state == STATE_SCALE ) {
527 sp_remove_handles(_rhandle, 8);
528 _showHandles(_shandle, handles_scale, 8,
529 _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
530 _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
531 } else {
532 sp_remove_handles(_shandle, 8);
533 _showHandles(_rhandle, handles_rotate, 8,
534 _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
535 _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
536 }
538 if (!_center_is_set) {
539 _center = _desktop->selection->center();
540 _center_is_set = true;
541 }
543 if ( _state == STATE_SCALE || !_center ) {
544 sp_knot_hide(_chandle);
545 } else {
546 sp_knot_show(_chandle);
547 sp_knot_moveto(_chandle, &*_center);
548 }
549 }
551 void Inkscape::SelTrans::_updateVolatileState()
552 {
553 Inkscape::Selection *selection = sp_desktop_selection(_desktop);
554 _empty = selection->isEmpty();
556 if (_empty) {
557 return;
558 }
560 _box = selection->bounds();
561 if (!_box) {
562 _empty = true;
563 return;
564 }
566 _strokewidth = stroke_average_width (selection->itemList());
567 }
569 static void sp_remove_handles(SPKnot *knot[], gint num)
570 {
571 for (int i = 0; i < num; i++) {
572 if (knot[i] != NULL) {
573 sp_knot_hide(knot[i]);
574 }
575 }
576 }
578 void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
579 gchar const *even_tip, gchar const *odd_tip)
580 {
581 g_return_if_fail( !_empty );
583 for (int i = 0; i < num; i++) {
584 if (knot[i] == NULL) {
585 knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
587 knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
588 knot[i]->setSize (13);
589 knot[i]->setAnchor (handle[i].anchor);
590 knot[i]->setMode (SP_CTRL_MODE_XOR);
591 knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
592 knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
593 knot[i]->setPixbuf(handles[handle[i].control]);
594 sp_knot_update_ctrl(knot[i]);
596 g_signal_connect(G_OBJECT(knot[i]), "request",
597 G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
598 g_signal_connect(G_OBJECT(knot[i]), "moved",
599 G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
600 g_signal_connect(G_OBJECT(knot[i]), "grabbed",
601 G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
602 g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
603 G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
604 g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
605 }
606 sp_knot_show(knot[i]);
608 NR::Point const handle_pt(handle[i].x, handle[i].y);
609 // shouldn't have nullary bbox, but knots
610 g_assert(_box);
611 NR::Point p( _box->min()
612 + ( _box->dimensions()
613 * NR::scale(handle_pt) ) );
615 sp_knot_moveto(knot[i], &p);
616 }
617 }
619 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
620 {
621 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
622 knot, state, *(SPSelTransHandle const *) data
623 );
624 }
626 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data)
627 {
628 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
629 }
631 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
632 {
633 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
634 knot, position, state, *(SPSelTransHandle const *) data
635 );
636 }
638 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
639 {
640 return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
641 knot, position, state, *(SPSelTransHandle const *) data
642 );
643 }
645 static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
646 {
647 SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
648 knot, state, *(SPSelTransHandle const *) data
649 );
650 }
652 void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
653 {
654 switch (handle.anchor) {
655 case GTK_ANCHOR_CENTER:
656 if (state & GDK_SHIFT_MASK) {
657 // Unset the center position for all selected items
658 for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
659 SPItem *it = (SPItem*)(SP_OBJECT(l->data));
660 it->unsetCenter();
661 SP_OBJECT(it)->updateRepr();
662 _center_is_set = false; // center has changed
663 _updateHandles();
664 }
665 sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
666 _("Reset center"));
667 }
668 break;
669 default:
670 break;
671 }
672 }
674 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
675 {
676 switch (handle.anchor) {
677 case GTK_ANCHOR_CENTER:
678 g_object_set(G_OBJECT(_grip),
679 "shape", SP_CTRL_SHAPE_BITMAP,
680 "size", 13.0,
681 NULL);
682 sp_canvas_item_show(_grip);
683 break;
684 default:
685 g_object_set(G_OBJECT(_grip),
686 "shape", SP_CTRL_SHAPE_CROSS,
687 "size", 7.0,
688 NULL);
689 sp_canvas_item_show(_norm);
690 sp_canvas_item_show(_grip);
692 break;
693 }
695 grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
696 }
699 void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
700 {
701 if (!SP_KNOT_IS_GRABBED(knot)) {
702 return;
703 }
705 // in case items have been unhooked from the document, don't
706 // try to continue processing events for them.
707 for (unsigned int i = 0; i < _items.size(); i++) {
708 if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i].first)) ) {
709 return;
710 }
711 }
713 handle.action(this, handle, *position, state);
714 }
717 gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
718 {
719 if (!SP_KNOT_IS_GRABBED(knot)) {
720 return TRUE;
721 }
723 knot->desktop->setPosition(*position);
725 if (state & GDK_MOD1_MASK) {
726 *position = _point + ( *position - _point ) / 10;
727 }
729 if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
730 _origin = _opposite;
731 } else if (_center) {
732 _origin = *_center;
733 } else {
734 // FIXME
735 return TRUE;
736 }
737 if (handle.request(this, handle, *position, state)) {
738 sp_knot_set_position(knot, position, state);
739 SP_CTRL(_grip)->moveto(*position);
740 SP_CTRL(_norm)->moveto(_origin);
741 }
743 return TRUE;
744 }
747 void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
748 {
749 if (!_grabbed) {
750 _updateVolatileState();
751 _current.set_identity();
752 _center_is_set = false; // center(s) may have changed
753 _updateHandles();
754 }
755 }
757 void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flags)
758 {
759 if (!_grabbed) {
760 _updateVolatileState();
761 _current.set_identity();
763 // reset internal flag
764 _changed = false;
766 _center_is_set = false; // center(s) may have changed
768 _updateHandles();
769 }
770 }
772 /*
773 * handlers for handle move-request
774 */
776 /** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
777 static double sign(double const x)
778 {
779 return ( x < 0
780 ? -1
781 : 1 );
782 }
784 gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
785 SPSelTransHandle const &, NR::Point &pt, guint state)
786 {
787 return seltrans->scaleRequest(pt, state);
788 }
790 gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
791 SPSelTransHandle const &handle, NR::Point &pt, guint state)
792 {
793 return seltrans->stretchRequest(handle, pt, state);
794 }
796 gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
797 SPSelTransHandle const &handle, NR::Point &pt, guint state)
798 {
799 return seltrans->skewRequest(handle, pt, state);
800 }
802 gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
803 SPSelTransHandle const &, NR::Point &pt, guint state)
804 {
805 return seltrans->rotateRequest(pt, state);
806 }
808 gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
809 SPSelTransHandle const &, NR::Point &pt, guint state)
810 {
811 return seltrans->centerRequest(pt, state);
812 }
814 gboolean Inkscape::SelTrans::scaleRequest(NR::Point &pt, guint state)
815 {
816 using NR::X;
817 using NR::Y;
819 NR::Point d = _point - _origin;
820 NR::scale s(0, 0);
822 /* Work out the new scale factors `s' */
823 for ( unsigned int i = 0 ; i < 2 ; i++ ) {
824 if ( fabs(d[i]) > 0.001 ) {
825 s[i] = ( pt[i] - _origin[i] ) / d[i];
826 if ( fabs(s[i]) < 1e-9 ) {
827 s[i] = 1e-9;
828 }
829 }
830 }
832 SnapManager const &m = _desktop->namedview->snap_manager;
834 /* Get a STL list of the selected items.
835 ** FIXME: this should probably be done by Inkscape::Selection.
836 */
837 std::list<SPItem const*> it;
838 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
839 it.push_back(reinterpret_cast<SPItem*>(i->data));
840 }
842 if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
843 /* Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
844 ** To do this, we snap along a suitable constraint vector from the origin.
845 */
847 NR::Point const cv = NR::Point(
848 pt[NR::X] > _origin[NR::X] ? 1 : -1,
849 pt[NR::Y] > _origin[NR::Y] ? 1 : -1
850 );
852 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
853 _bbox_points,
854 it,
855 Snapper::ConstraintLine(_origin, cv),
856 s,
857 _origin);
859 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
860 _snap_points,
861 it,
862 Snapper::ConstraintLine(_origin, cv),
863 s,
864 _origin);
866 if (bb.second == false && sn.second == false) {
868 /* We didn't snap, so just lock aspect ratio */
869 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
870 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
871 } else {
872 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
873 }
875 } else {
877 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
878 ** just compare difference in s[X].
879 */
880 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
881 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
882 s = (bd < sd) ? bb.first : sn.first;
883 }
885 } else {
886 /* Scale aspect ratio is unlocked */
888 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
889 _bbox_points,
890 it,
891 s,
892 _origin);
893 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
894 _snap_points,
895 it,
896 s,
897 _origin);
899 /* Pick the snap that puts us closest to the original scale */
900 NR::Coord bd = bb.second ?
901 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
902 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
903 : NR_HUGE;
904 NR::Coord sd = sn.second ?
905 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
906 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
907 : NR_HUGE;
908 s = (bd < sd) ? bb.first : sn.first;
909 }
911 /* Update the knot position */
912 pt = ( _point - _origin ) * s + _origin;
914 /* Status text */
915 _message_context.setF(Inkscape::NORMAL_MESSAGE,
916 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
917 100 * s[NR::X], 100 * s[NR::Y]);
919 return TRUE;
920 }
922 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
923 {
924 using NR::X;
925 using NR::Y;
927 NR::Dim2 axis, perp;
929 switch (handle.cursor) {
930 case GDK_TOP_SIDE:
931 case GDK_BOTTOM_SIDE:
932 axis = NR::Y;
933 perp = NR::X;
934 break;
935 case GDK_LEFT_SIDE:
936 case GDK_RIGHT_SIDE:
937 axis = NR::X;
938 perp = NR::Y;
939 break;
940 default:
941 g_assert_not_reached();
942 return TRUE;
943 };
945 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
946 return FALSE;
947 }
949 NR::scale s(1, 1);
950 s[axis] = ( ( pt[axis] - _origin[axis] )
951 / ( _point[axis] - _origin[axis] ) );
952 if ( fabs(s[axis]) < 1e-15 ) {
953 s[axis] = 1e-15;
954 }
956 /* Get a STL list of the selected items.
957 ** FIXME: this should probably be done by Inkscape::Selection.
958 */
959 std::list<SPItem const*> it;
960 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
961 it.push_back(reinterpret_cast<SPItem*>(i->data));
962 }
964 SnapManager const &m = _desktop->namedview->snap_manager;
966 if ( state & GDK_CONTROL_MASK ) {
967 s[perp] = fabs(s[axis]);
969 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
970 Snapper::BBOX_POINT,
971 _bbox_points,
972 it,
973 s[axis],
974 _origin,
975 axis,
976 true);
978 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
979 Snapper::SNAP_POINT,
980 _snap_points,
981 it,
982 s[axis],
983 _origin,
984 axis,
985 true);
987 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
988 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
989 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
991 s[axis] = fabs(ratio) * sign(s[axis]);
992 s[perp] = fabs(s[axis]);
993 } else {
995 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
996 Snapper::BBOX_POINT,
997 _bbox_points,
998 it,
999 s[axis],
1000 _origin,
1001 axis,
1002 false);
1004 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1005 Snapper::SNAP_POINT,
1006 _snap_points,
1007 it,
1008 s[axis],
1009 _origin,
1010 axis,
1011 false);
1013 /* Choose the smaller difference in scale */
1014 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1015 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1016 s[axis] = (bd < sd) ? bb.first : sn.first;
1017 s[perp] = 1;
1018 }
1020 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1021 if (isNaN(pt[X] + pt[Y])) {
1022 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1023 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1024 }
1026 // status text
1027 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1028 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1029 100 * s[NR::X], 100 * s[NR::Y]);
1031 return TRUE;
1032 }
1034 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1035 {
1036 using NR::X;
1037 using NR::Y;
1039 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1040 return FALSE;
1041 }
1043 NR::Dim2 dim_a;
1044 NR::Dim2 dim_b;
1045 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1046 dim_a = X;
1047 dim_b = Y;
1048 } else {
1049 dim_a = Y;
1050 dim_b = X;
1051 }
1053 double skew[2];
1054 double s[2] = { 1.0, 1.0 };
1056 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1057 return FALSE;
1058 }
1060 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1062 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1064 if ( fabs(s[dim_a]) < 1 ) {
1065 s[dim_a] = sign(s[dim_a]);
1066 } else {
1067 s[dim_a] = floor( s[dim_a] + 0.5 );
1068 }
1070 double radians = atan(skew[dim_a] / s[dim_a]);
1072 if (state & GDK_CONTROL_MASK) {
1074 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1076 if (snaps) {
1077 double sections = floor( radians * snaps / M_PI + .5 );
1078 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1079 radians = ( M_PI / snaps ) * sections;
1080 }
1081 skew[dim_a] = tan(radians) * s[dim_a];
1082 } else {
1083 SnapManager const &m = _desktop->namedview->snap_manager;
1085 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1086 _bbox_points,
1087 std::list<SPItem const *>(),
1088 skew[dim_a],
1089 _origin,
1090 dim_a);
1092 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1093 _snap_points,
1094 std::list<SPItem const *>(),
1095 skew[dim_a],
1096 _origin,
1097 dim_a);
1099 if (bb.second || sn.second) {
1100 /* We snapped something, so change the skew to reflect it */
1101 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1102 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1103 skew[dim_a] = std::min(bd, sd);
1104 }
1105 }
1107 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1108 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1110 /* status text */
1111 double degrees = 180 / M_PI * radians;
1112 if (degrees > 180) degrees -= 360;
1113 if (degrees < -180) degrees += 360;
1115 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1116 // TRANSLATORS: don't modify the first ";"
1117 // (it will NOT be displayed as ";" - only the second one will be)
1118 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1119 degrees);
1121 return TRUE;
1122 }
1124 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1125 {
1126 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1128 // rotate affine in rotate
1129 NR::Point const d1 = _point - _origin;
1130 NR::Point const d2 = pt - _origin;
1132 NR::Coord const h1 = NR::L2(d1);
1133 if (h1 < 1e-15) return FALSE;
1134 NR::Point q1 = d1 / h1;
1135 NR::Coord const h2 = NR::L2(d2);
1136 if (fabs(h2) < 1e-15) return FALSE;
1137 NR::Point q2 = d2 / h2;
1139 double radians;
1140 if (state & GDK_CONTROL_MASK) {
1141 /* Have to restrict movement. */
1142 double cos_t = NR::dot(q1, q2);
1143 double sin_t = NR::dot(NR::rot90(q1), q2);
1144 radians = atan2(sin_t, cos_t);
1145 if (snaps) {
1146 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1147 }
1148 q1 = NR::Point(1, 0);
1149 q2 = NR::Point(cos(radians), sin(radians));
1150 } else {
1151 radians = atan2(NR::dot(NR::rot90(d1), d2),
1152 NR::dot(d1, d2));
1153 }
1155 NR::rotate const r1(q1);
1156 NR::rotate const r2(q2);
1157 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1159 /* status text */
1160 double degrees = 180 / M_PI * radians;
1161 if (degrees > 180) degrees -= 360;
1162 if (degrees < -180) degrees += 360;
1164 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1165 // TRANSLATORS: don't modify the first ";"
1166 // (it will NOT be displayed as ";" - only the second one will be)
1167 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1169 return TRUE;
1170 }
1172 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1173 {
1174 using NR::X;
1175 using NR::Y;
1177 SnapManager const &m = _desktop->namedview->snap_manager;
1178 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1180 if (state & GDK_CONTROL_MASK) {
1181 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1182 pt[Y] = _point[Y];
1183 } else {
1184 pt[X] = _point[X];
1185 }
1186 }
1188 if ( !(state & GDK_SHIFT_MASK) && _box ) {
1189 // screen pixels to snap center to bbox
1190 #define SNAP_DIST 5
1191 // FIXME: take from prefs
1192 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1194 for (int i = 0; i < 2; i++) {
1195 if (fabs(pt[i] - _box->min()[i]) < snap_dist) {
1196 pt[i] = _box->min()[i];
1197 }
1198 if (fabs(pt[i] - _box->midpoint()[i]) < snap_dist) {
1199 pt[i] = _box->midpoint()[i];
1200 }
1201 if (fabs(pt[i] - _box->max()[i]) < snap_dist) {
1202 pt[i] = _box->max()[i];
1203 }
1204 }
1205 }
1207 // status text
1208 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1209 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1210 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1211 g_string_free(xs, FALSE);
1212 g_string_free(ys, FALSE);
1214 return TRUE;
1215 }
1217 /*
1218 * handlers for handle movement
1219 *
1220 */
1222 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1223 {
1224 seltrans->stretch(handle, pt, state);
1225 }
1227 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1228 {
1229 seltrans->scale(pt, state);
1230 }
1232 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1233 {
1234 seltrans->skew(handle, pt, state);
1235 }
1237 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1238 {
1239 seltrans->rotate(pt, state);
1240 }
1242 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1243 {
1244 using NR::X;
1245 using NR::Y;
1247 NR::Dim2 dim;
1248 switch (handle.cursor) {
1249 case GDK_LEFT_SIDE:
1250 case GDK_RIGHT_SIDE:
1251 dim = X;
1252 break;
1253 case GDK_TOP_SIDE:
1254 case GDK_BOTTOM_SIDE:
1255 dim = Y;
1256 break;
1257 default:
1258 g_assert_not_reached();
1259 abort();
1260 break;
1261 }
1263 NR::Point const scale_origin(_origin);
1264 double const offset = _point[dim] - scale_origin[dim];
1265 if (!( fabs(offset) >= 1e-15 )) {
1266 return;
1267 }
1268 NR::scale s(1, 1);
1269 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1270 if (isNaN(s[dim])) {
1271 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1272 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1273 }
1274 if (!( fabs(s[dim]) >= 1e-15 )) {
1275 s[dim] = 1e-15;
1276 }
1277 if (state & GDK_CONTROL_MASK) {
1278 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1279 s[!dim] = fabs(s[dim]);
1280 }
1282 if (!_box) {
1283 return;
1284 }
1286 NR::Point new_bbox_min = _box->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1287 NR::Point new_bbox_max = _box->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1289 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1290 NR::Matrix scaler = get_scale_transform_with_stroke (*_box, _strokewidth, transform_stroke,
1291 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1293 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1294 }
1296 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1297 {
1298 if (!_box) {
1299 return;
1300 }
1302 NR::Point const offset = _point - _origin;
1304 NR::scale s (1, 1);
1305 for (int i = NR::X; i <= NR::Y; i++) {
1306 if (fabs(offset[i]) > 1e-9)
1307 s[i] = (pt[i] - _origin[i]) / offset[i];
1308 if (fabs(s[i]) < 1e-9)
1309 s[i] = 1e-9;
1310 }
1311 NR::Point new_bbox_min = _box->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1312 NR::Point new_bbox_max = _box->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1314 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1315 NR::Matrix scaler = get_scale_transform_with_stroke (*_box, _strokewidth, transform_stroke,
1316 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1318 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1319 }
1321 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1322 {
1323 NR::Point const offset = _point - _origin;
1325 unsigned dim;
1326 switch (handle.cursor) {
1327 case GDK_SB_H_DOUBLE_ARROW:
1328 dim = NR::Y;
1329 break;
1330 case GDK_SB_V_DOUBLE_ARROW:
1331 dim = NR::X;
1332 break;
1333 default:
1334 g_assert_not_reached();
1335 abort();
1336 break;
1337 }
1338 if (fabs(offset[dim]) < 1e-15) {
1339 return;
1340 }
1341 NR::Matrix skew = NR::identity();
1342 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1343 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1344 skew[2*(1-dim) + (dim)] = 0;
1345 skew[2*(1-dim) + (1-dim)] = 1;
1347 for (int i = 0; i < 2; i++) {
1348 if (fabs(skew[3*i]) < 1e-15) {
1349 skew[3*i] = 1e-15;
1350 }
1351 }
1352 transform(skew, _origin);
1353 }
1355 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1356 {
1357 NR::Point const offset = _point - _origin;
1359 NR::Coord const h1 = NR::L2(offset);
1360 if (h1 < 1e-15) {
1361 return;
1362 }
1363 NR::Point const q1 = offset / h1;
1364 NR::Coord const h2 = NR::L2( pt - _origin );
1365 if (h2 < 1e-15) {
1366 return;
1367 }
1368 NR::Point const q2 = (pt - _origin) / h2;
1369 NR::rotate const r1(q1);
1370 NR::rotate const r2(q2);
1372 NR::Matrix rotate( r2 / r1 );
1373 transform(rotate, _origin);
1374 }
1376 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1377 {
1378 seltrans->setCenter(pt);
1379 }
1382 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1383 {
1384 SnapManager const &m = _desktop->namedview->snap_manager;
1386 /* The amount that we've moved by during this drag */
1387 NR::Point dxy = xy - _point;
1389 /* Get a STL list of the selected items.
1390 ** FIXME: this should probably be done by Inkscape::Selection.
1391 */
1392 std::list<SPItem const*> it;
1393 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1394 it.push_back(reinterpret_cast<SPItem*>(i->data));
1395 }
1397 bool const alt = (state & GDK_MOD1_MASK);
1398 bool const control = (state & GDK_CONTROL_MASK);
1399 bool const shift = (state & GDK_SHIFT_MASK);
1401 if (alt) {
1403 /* Alt pressed means keep offset: snap the moved distance to the grid.
1404 ** FIXME: this will snap to more than just the grid, nowadays.
1405 */
1407 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1409 } else if (!shift) {
1411 /* We're snapping to things, possibly with a constraint to horizontal or
1412 ** vertical movement. Obtain a list of possible translations and then
1413 ** pick the smallest.
1414 */
1416 /* This will be our list of possible translations */
1417 std::list<std::pair<NR::Point, bool> > s;
1419 if (control) {
1421 /* Snap to things, and also constrain to horizontal or vertical movement */
1423 for (unsigned int dim = 0; dim < 2; dim++) {
1424 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1425 _bbox_points,
1426 it,
1427 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1428 dxy));
1430 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1431 _snap_points,
1432 it,
1433 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1434 dxy));
1435 }
1437 } else {
1439 /* Snap to things with no constraint */
1441 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1442 _bbox_points, it, dxy));
1443 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1444 _snap_points, it, dxy));
1445 }
1447 /* Pick one */
1448 NR::Coord best = NR_HUGE;
1449 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1450 if (i->second) {
1451 NR::Coord const m = NR::L2(i->first);
1452 if (m < best) {
1453 best = m;
1454 dxy = i->first;
1455 }
1456 }
1457 }
1458 }
1460 if (control) {
1461 /* Ensure that the horizontal and vertical constraint has been applied */
1462 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1463 dxy[NR::Y] = 0;
1464 } else {
1465 dxy[NR::X] = 0;
1466 }
1467 }
1469 NR::Matrix const move((NR::translate(dxy)));
1470 NR::Point const norm(0, 0);
1471 transform(move, norm);
1473 // status text
1474 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1475 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1476 _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);
1477 g_string_free(xs, TRUE);
1478 g_string_free(ys, TRUE);
1479 }
1482 /*
1483 Local Variables:
1484 mode:c++
1485 c-file-style:"stroustrup"
1486 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1487 indent-tabs-mode:nil
1488 fill-column:99
1489 End:
1490 */
1491 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :