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 // The inclination of the constraint vector is calculated from the aspect ratio
848 NR::Point bbox_dim = _box->dimensions();
849 double const aspect_ratio = bbox_dim[1] / bbox_dim[0]; // = height / width
851 // Determine direction of the constraint vector
852 NR::Point const cv = NR::Point(
853 pt[NR::X] > _origin[NR::X] ? 1 : -1,
854 pt[NR::Y] > _origin[NR::Y] ? aspect_ratio : -aspect_ratio
855 );
857 std::pair<NR::scale, bool> bb = m.constrainedSnapScale(Snapper::BBOX_POINT,
858 _bbox_points,
859 it,
860 Snapper::ConstraintLine(_origin, cv),
861 s,
862 _origin);
864 std::pair<NR::scale, bool> sn = m.constrainedSnapScale(Snapper::SNAP_POINT,
865 _snap_points,
866 it,
867 Snapper::ConstraintLine(_origin, cv),
868 s,
869 _origin);
871 if (bb.second == false && sn.second == false) {
873 /* We didn't snap, so just lock aspect ratio */
874 if (fabs(s[NR::X]) > fabs(s[NR::Y])) {
875 s[NR::X] = fabs(s[NR::Y]) * sign(s[NR::X]);
876 } else {
877 s[NR::Y] = fabs(s[NR::X]) * sign(s[NR::Y]);
878 }
880 } else {
882 /* Choose the smaller difference in scale. Since s[X] == s[Y] we can
883 ** just compare difference in s[X].
884 */
885 double const bd = bb.second ? fabs(bb.first[NR::X] - s[NR::X]) : NR_HUGE;
886 double const sd = sn.second ? fabs(sn.first[NR::X] - s[NR::X]) : NR_HUGE;
887 s = (bd < sd) ? bb.first : sn.first;
888 }
890 } else {
891 /* Scale aspect ratio is unlocked */
893 std::pair<NR::scale, bool> bb = m.freeSnapScale(Snapper::BBOX_POINT,
894 _bbox_points,
895 it,
896 s,
897 _origin);
898 std::pair<NR::scale, bool> sn = m.freeSnapScale(Snapper::SNAP_POINT,
899 _snap_points,
900 it,
901 s,
902 _origin);
904 /* Pick the snap that puts us closest to the original scale */
905 NR::Coord bd = bb.second ?
906 fabs(NR::L2(NR::Point(bb.first[NR::X], bb.first[NR::Y])) -
907 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
908 : NR_HUGE;
909 NR::Coord sd = sn.second ?
910 fabs(NR::L2(NR::Point(sn.first[NR::X], sn.first[NR::Y])) -
911 NR::L2(NR::Point(s[NR::X], s[NR::Y])))
912 : NR_HUGE;
913 s = (bd < sd) ? bb.first : sn.first;
914 }
916 /* Update the knot position */
917 pt = ( _point - _origin ) * s + _origin;
919 /* Status text */
920 _message_context.setF(Inkscape::NORMAL_MESSAGE,
921 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
922 100 * s[NR::X], 100 * s[NR::Y]);
924 return TRUE;
925 }
927 gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
928 {
929 using NR::X;
930 using NR::Y;
932 NR::Dim2 axis, perp;
934 switch (handle.cursor) {
935 case GDK_TOP_SIDE:
936 case GDK_BOTTOM_SIDE:
937 axis = NR::Y;
938 perp = NR::X;
939 break;
940 case GDK_LEFT_SIDE:
941 case GDK_RIGHT_SIDE:
942 axis = NR::X;
943 perp = NR::Y;
944 break;
945 default:
946 g_assert_not_reached();
947 return TRUE;
948 };
950 if ( fabs( _point[axis] - _origin[axis] ) < 1e-15 ) {
951 return FALSE;
952 }
954 NR::scale s(1, 1);
955 s[axis] = ( ( pt[axis] - _origin[axis] )
956 / ( _point[axis] - _origin[axis] ) );
957 if ( fabs(s[axis]) < 1e-15 ) {
958 s[axis] = 1e-15;
959 }
961 /* Get a STL list of the selected items.
962 ** FIXME: this should probably be done by Inkscape::Selection.
963 */
964 std::list<SPItem const*> it;
965 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
966 it.push_back(reinterpret_cast<SPItem*>(i->data));
967 }
969 SnapManager const &m = _desktop->namedview->snap_manager;
971 if ( state & GDK_CONTROL_MASK ) {
972 s[perp] = fabs(s[axis]);
974 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
975 Snapper::BBOX_POINT,
976 _bbox_points,
977 it,
978 s[axis],
979 _origin,
980 axis,
981 true);
983 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
984 Snapper::SNAP_POINT,
985 _snap_points,
986 it,
987 s[axis],
988 _origin,
989 axis,
990 true);
992 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
993 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
994 NR::Coord const ratio = (bd < sd) ? bb.first : sn.first;
996 s[axis] = fabs(ratio) * sign(s[axis]);
997 s[perp] = fabs(s[axis]);
998 } else {
1000 std::pair<NR::Coord, bool> const bb = m.freeSnapStretch(
1001 Snapper::BBOX_POINT,
1002 _bbox_points,
1003 it,
1004 s[axis],
1005 _origin,
1006 axis,
1007 false);
1009 std::pair<NR::Coord, bool> const sn = m.freeSnapStretch(
1010 Snapper::SNAP_POINT,
1011 _snap_points,
1012 it,
1013 s[axis],
1014 _origin,
1015 axis,
1016 false);
1018 /* Choose the smaller difference in scale */
1019 NR::Coord const bd = bb.second ? fabs(bb.first - s[axis]) : NR_HUGE;
1020 NR::Coord const sd = sn.second ? fabs(sn.first - s[axis]) : NR_HUGE;
1021 s[axis] = (bd < sd) ? bb.first : sn.first;
1022 s[perp] = 1;
1023 }
1025 pt = ( _point - _origin ) * NR::scale(s) + _origin;
1026 if (isNaN(pt[X] + pt[Y])) {
1027 g_warning("point=(%g, %g), norm=(%g, %g), s=(%g, %g)\n",
1028 _point[X], _point[Y], _origin[X], _origin[Y], s[X], s[Y]);
1029 }
1031 // status text
1032 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1033 _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
1034 100 * s[NR::X], 100 * s[NR::Y]);
1036 return TRUE;
1037 }
1039 gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1040 {
1041 using NR::X;
1042 using NR::Y;
1044 if (handle.cursor != GDK_SB_V_DOUBLE_ARROW && handle.cursor != GDK_SB_H_DOUBLE_ARROW) {
1045 return FALSE;
1046 }
1048 NR::Dim2 dim_a;
1049 NR::Dim2 dim_b;
1050 if (handle.cursor == GDK_SB_V_DOUBLE_ARROW) {
1051 dim_a = X;
1052 dim_b = Y;
1053 } else {
1054 dim_a = Y;
1055 dim_b = X;
1056 }
1058 double skew[2];
1059 double s[2] = { 1.0, 1.0 };
1061 if (fabs(_point[dim_a] - _origin[dim_a]) < NR_EPSILON) {
1062 return FALSE;
1063 }
1065 skew[dim_a] = ( pt[dim_b] - _point[dim_b] ) / ( _point[dim_a] - _origin[dim_a] );
1067 s[dim_a] = ( pt[dim_a] - _origin[dim_a] ) / ( _point[dim_a] - _origin[dim_a] );
1069 if ( fabs(s[dim_a]) < 1 ) {
1070 s[dim_a] = sign(s[dim_a]);
1071 } else {
1072 s[dim_a] = floor( s[dim_a] + 0.5 );
1073 }
1075 double radians = atan(skew[dim_a] / s[dim_a]);
1077 if (state & GDK_CONTROL_MASK) {
1079 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1081 if (snaps) {
1082 double sections = floor( radians * snaps / M_PI + .5 );
1083 if (fabs(sections) >= snaps / 2) sections = sign(sections) * (snaps / 2 - 1);
1084 radians = ( M_PI / snaps ) * sections;
1085 }
1086 skew[dim_a] = tan(radians) * s[dim_a];
1087 } else {
1088 SnapManager const &m = _desktop->namedview->snap_manager;
1090 std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT,
1091 _bbox_points,
1092 std::list<SPItem const *>(),
1093 skew[dim_a],
1094 _origin,
1095 dim_b);
1097 std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT,
1098 _snap_points,
1099 std::list<SPItem const *>(),
1100 skew[dim_a],
1101 _origin,
1102 dim_b);
1104 if (bb.second || sn.second) {
1105 /* We snapped something, so change the skew to reflect it */
1106 NR::Coord const bd = bb.second ? bb.first : NR_HUGE;
1107 NR::Coord const sd = sn.second ? sn.first : NR_HUGE;
1108 skew[dim_a] = std::min(bd, sd);
1109 }
1110 }
1112 pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b];
1113 pt[dim_a] = ( _point[dim_a] - _origin[dim_a] ) * s[dim_a] + _origin[dim_a];
1115 /* status text */
1116 double degrees = 180 / M_PI * radians;
1117 if (degrees > 180) degrees -= 360;
1118 if (degrees < -180) degrees += 360;
1120 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1121 // TRANSLATORS: don't modify the first ";"
1122 // (it will NOT be displayed as ";" - only the second one will be)
1123 _("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
1124 degrees);
1126 return TRUE;
1127 }
1129 gboolean Inkscape::SelTrans::rotateRequest(NR::Point &pt, guint state)
1130 {
1131 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1133 // rotate affine in rotate
1134 NR::Point const d1 = _point - _origin;
1135 NR::Point const d2 = pt - _origin;
1137 NR::Coord const h1 = NR::L2(d1);
1138 if (h1 < 1e-15) return FALSE;
1139 NR::Point q1 = d1 / h1;
1140 NR::Coord const h2 = NR::L2(d2);
1141 if (fabs(h2) < 1e-15) return FALSE;
1142 NR::Point q2 = d2 / h2;
1144 double radians;
1145 if (state & GDK_CONTROL_MASK) {
1146 /* Have to restrict movement. */
1147 double cos_t = NR::dot(q1, q2);
1148 double sin_t = NR::dot(NR::rot90(q1), q2);
1149 radians = atan2(sin_t, cos_t);
1150 if (snaps) {
1151 radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
1152 }
1153 q1 = NR::Point(1, 0);
1154 q2 = NR::Point(cos(radians), sin(radians));
1155 } else {
1156 radians = atan2(NR::dot(NR::rot90(d1), d2),
1157 NR::dot(d1, d2));
1158 }
1160 NR::rotate const r1(q1);
1161 NR::rotate const r2(q2);
1162 pt = _point * NR::translate(-_origin) * ( r2 / r1 ) * NR::translate(_origin);
1164 /* status text */
1165 double degrees = 180 / M_PI * radians;
1166 if (degrees > 180) degrees -= 360;
1167 if (degrees < -180) degrees += 360;
1169 _message_context.setF(Inkscape::NORMAL_MESSAGE,
1170 // TRANSLATORS: don't modify the first ";"
1171 // (it will NOT be displayed as ";" - only the second one will be)
1172 _("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
1174 return TRUE;
1175 }
1177 gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
1178 {
1179 using NR::X;
1180 using NR::Y;
1182 SnapManager const &m = _desktop->namedview->snap_manager;
1183 pt = m.freeSnap(Snapper::SNAP_POINT, pt, NULL).getPoint();
1185 if (state & GDK_CONTROL_MASK) {
1186 if ( fabs(_point[X] - pt[X]) > fabs(_point[Y] - pt[Y]) ) {
1187 pt[Y] = _point[Y];
1188 } else {
1189 pt[X] = _point[X];
1190 }
1191 }
1193 if ( !(state & GDK_SHIFT_MASK) && _box ) {
1194 // screen pixels to snap center to bbox
1195 #define SNAP_DIST 5
1196 // FIXME: take from prefs
1197 double snap_dist = SNAP_DIST / _desktop->current_zoom();
1199 for (int i = 0; i < 2; i++) {
1200 if (fabs(pt[i] - _box->min()[i]) < snap_dist) {
1201 pt[i] = _box->min()[i];
1202 }
1203 if (fabs(pt[i] - _box->midpoint()[i]) < snap_dist) {
1204 pt[i] = _box->midpoint()[i];
1205 }
1206 if (fabs(pt[i] - _box->max()[i]) < snap_dist) {
1207 pt[i] = _box->max()[i];
1208 }
1209 }
1210 }
1212 // status text
1213 GString *xs = SP_PX_TO_METRIC_STRING(pt[X], _desktop->namedview->getDefaultMetric());
1214 GString *ys = SP_PX_TO_METRIC_STRING(pt[Y], _desktop->namedview->getDefaultMetric());
1215 _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
1216 g_string_free(xs, FALSE);
1217 g_string_free(ys, FALSE);
1219 return TRUE;
1220 }
1222 /*
1223 * handlers for handle movement
1224 *
1225 */
1227 void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1228 {
1229 seltrans->stretch(handle, pt, state);
1230 }
1232 void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1233 {
1234 seltrans->scale(pt, state);
1235 }
1237 void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
1238 {
1239 seltrans->skew(handle, pt, state);
1240 }
1242 void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1243 {
1244 seltrans->rotate(pt, state);
1245 }
1247 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1248 {
1249 using NR::X;
1250 using NR::Y;
1252 NR::Dim2 dim;
1253 switch (handle.cursor) {
1254 case GDK_LEFT_SIDE:
1255 case GDK_RIGHT_SIDE:
1256 dim = X;
1257 break;
1258 case GDK_TOP_SIDE:
1259 case GDK_BOTTOM_SIDE:
1260 dim = Y;
1261 break;
1262 default:
1263 g_assert_not_reached();
1264 abort();
1265 break;
1266 }
1268 NR::Point const scale_origin(_origin);
1269 double const offset = _point[dim] - scale_origin[dim];
1270 if (!( fabs(offset) >= 1e-15 )) {
1271 return;
1272 }
1273 NR::scale s(1, 1);
1274 s[dim] = ( pt[dim] - scale_origin[dim] ) / offset;
1275 if (isNaN(s[dim])) {
1276 g_warning("s[dim]=%g, pt[dim]=%g, scale_origin[dim]=%g, point[dim]=%g\n",
1277 s[dim], pt[dim], scale_origin[dim], _point[dim]);
1278 }
1279 if (!( fabs(s[dim]) >= 1e-15 )) {
1280 s[dim] = 1e-15;
1281 }
1282 if (state & GDK_CONTROL_MASK) {
1283 /* Preserve aspect ratio, but never flip in the dimension not being edited. */
1284 s[!dim] = fabs(s[dim]);
1285 }
1287 if (!_box) {
1288 return;
1289 }
1291 NR::Point new_bbox_min = _box->min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1292 NR::Point new_bbox_max = _box->max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
1294 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1295 NR::Matrix scaler = get_scale_transform_with_stroke (*_box, _strokewidth, transform_stroke,
1296 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1298 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1299 }
1301 void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
1302 {
1303 if (!_box) {
1304 return;
1305 }
1307 NR::Point const offset = _point - _origin;
1309 NR::scale s (1, 1);
1310 for (int i = NR::X; i <= NR::Y; i++) {
1311 if (fabs(offset[i]) > 1e-9)
1312 s[i] = (pt[i] - _origin[i]) / offset[i];
1313 if (fabs(s[i]) < 1e-9)
1314 s[i] = 1e-9;
1315 }
1316 NR::Point new_bbox_min = _box->min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1317 NR::Point new_bbox_max = _box->max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
1319 int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
1320 NR::Matrix scaler = get_scale_transform_with_stroke (*_box, _strokewidth, transform_stroke,
1321 new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
1323 transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
1324 }
1326 void Inkscape::SelTrans::skew(SPSelTransHandle const &handle, NR::Point &pt, guint state)
1327 {
1328 NR::Point const offset = _point - _origin;
1330 unsigned dim;
1331 switch (handle.cursor) {
1332 case GDK_SB_H_DOUBLE_ARROW:
1333 dim = NR::Y;
1334 break;
1335 case GDK_SB_V_DOUBLE_ARROW:
1336 dim = NR::X;
1337 break;
1338 default:
1339 g_assert_not_reached();
1340 abort();
1341 break;
1342 }
1343 if (fabs(offset[dim]) < 1e-15) {
1344 return;
1345 }
1346 NR::Matrix skew = NR::identity();
1347 skew[2*dim + dim] = (pt[dim] - _origin[dim]) / offset[dim];
1348 skew[2*dim + (1-dim)] = (pt[1-dim] - _point[1-dim]) / offset[dim];
1349 skew[2*(1-dim) + (dim)] = 0;
1350 skew[2*(1-dim) + (1-dim)] = 1;
1352 for (int i = 0; i < 2; i++) {
1353 if (fabs(skew[3*i]) < 1e-15) {
1354 skew[3*i] = 1e-15;
1355 }
1356 }
1357 transform(skew, _origin);
1358 }
1360 void Inkscape::SelTrans::rotate(NR::Point &pt, guint state)
1361 {
1362 NR::Point const offset = _point - _origin;
1364 NR::Coord const h1 = NR::L2(offset);
1365 if (h1 < 1e-15) {
1366 return;
1367 }
1368 NR::Point const q1 = offset / h1;
1369 NR::Coord const h2 = NR::L2( pt - _origin );
1370 if (h2 < 1e-15) {
1371 return;
1372 }
1373 NR::Point const q2 = (pt - _origin) / h2;
1374 NR::rotate const r1(q1);
1375 NR::rotate const r2(q2);
1377 NR::Matrix rotate( r2 / r1 );
1378 transform(rotate, _origin);
1379 }
1381 void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
1382 {
1383 seltrans->setCenter(pt);
1384 }
1387 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
1388 {
1389 SnapManager const &m = _desktop->namedview->snap_manager;
1391 /* The amount that we've moved by during this drag */
1392 NR::Point dxy = xy - _point;
1394 /* Get a STL list of the selected items.
1395 ** FIXME: this should probably be done by Inkscape::Selection.
1396 */
1397 std::list<SPItem const*> it;
1398 for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
1399 it.push_back(reinterpret_cast<SPItem*>(i->data));
1400 }
1402 bool const alt = (state & GDK_MOD1_MASK);
1403 bool const control = (state & GDK_CONTROL_MASK);
1404 bool const shift = (state & GDK_SHIFT_MASK);
1406 if (alt) {
1408 /* Alt pressed means keep offset: snap the moved distance to the grid.
1409 ** FIXME: this will snap to more than just the grid, nowadays.
1410 */
1412 dxy = m.freeSnap(Snapper::SNAP_POINT, dxy, NULL).getPoint();
1414 } else if (!shift) {
1416 /* We're snapping to things, possibly with a constraint to horizontal or
1417 ** vertical movement. Obtain a list of possible translations and then
1418 ** pick the smallest.
1419 */
1421 /* This will be our list of possible translations */
1422 std::list<std::pair<NR::Point, bool> > s;
1424 if (control) {
1426 /* Snap to things, and also constrain to horizontal or vertical movement */
1428 for (unsigned int dim = 0; dim < 2; dim++) {
1429 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1430 _bbox_points,
1431 it,
1432 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1433 dxy));
1435 s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1436 _snap_points,
1437 it,
1438 Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
1439 dxy));
1440 }
1442 } else {
1444 /* Snap to things with no constraint */
1446 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::BBOX_POINT,
1447 _bbox_points, it, dxy));
1448 s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAP_POINT,
1449 _snap_points, it, dxy));
1450 }
1452 /* Pick one */
1453 NR::Coord best = NR_HUGE;
1454 for (std::list<std::pair<NR::Point, bool> >::const_iterator i = s.begin(); i != s.end(); i++) {
1455 if (i->second) {
1456 NR::Coord const m = NR::L2(i->first);
1457 if (m < best) {
1458 best = m;
1459 dxy = i->first;
1460 }
1461 }
1462 }
1463 }
1465 if (control) {
1466 /* Ensure that the horizontal and vertical constraint has been applied */
1467 if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
1468 dxy[NR::Y] = 0;
1469 } else {
1470 dxy[NR::X] = 0;
1471 }
1472 }
1474 NR::Matrix const move((NR::translate(dxy)));
1475 NR::Point const norm(0, 0);
1476 transform(move, norm);
1478 // status text
1479 GString *xs = SP_PX_TO_METRIC_STRING(dxy[NR::X], _desktop->namedview->getDefaultMetric());
1480 GString *ys = SP_PX_TO_METRIC_STRING(dxy[NR::Y], _desktop->namedview->getDefaultMetric());
1481 _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);
1482 g_string_free(xs, TRUE);
1483 g_string_free(ys, TRUE);
1484 }
1487 /*
1488 Local Variables:
1489 mode:c++
1490 c-file-style:"stroustrup"
1491 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1492 indent-tabs-mode:nil
1493 fill-column:99
1494 End:
1495 */
1496 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :