Code

Cleanup of device internal handling and API.
[inkscape.git] / src / ui / dialog / transformation.cpp
1 /** @file
2  * \brief Transform dialog - implementation
3  */
4 /* Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   buliabyak@gmail.com
7  *
8  * Copyright (C) 2004, 2005 Authors
9  * Released under GNU GPL.  Read the file 'COPYING' for more information.
10  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <gtkmm/stock.h>
17 #include <gtkmm/dialog.h>
19 #include "document.h"
20 #include "desktop-handles.h"
21 #include "transformation.h"
22 #include "align-and-distribute.h"
23 #include "libnr/nr-matrix-ops.h"
24 #include "inkscape.h"
25 #include "selection.h"
26 #include "selection-chemistry.h"
27 #include "verbs.h"
28 #include "preferences.h"
29 #include "sp-item-transform.h"
30 #include "macros.h"
31 #include "sp-item.h"
32 #include "util/glib-list-iterators.h"
34 namespace Inkscape {
35 namespace UI {
36 namespace Dialog {
38 void on_selection_changed(Inkscape::Application */*inkscape*/, Inkscape::Selection *selection, Transformation *daad)
39 {
40     int page = daad->getCurrentPage();
41     daad->updateSelection((Inkscape::UI::Dialog::Transformation::PageType)page, selection);
42 }
44 void on_selection_modified( Inkscape::Application */*inkscape*/,
45                             Inkscape::Selection *selection,
46                             guint /*flags*/,
47                             Transformation *daad )
48 {
49     int page = daad->getCurrentPage();
50     daad->updateSelection((Inkscape::UI::Dialog::Transformation::PageType)page, selection);
51 }
53 /*########################################################################
54 # C O N S T R U C T O R
55 ########################################################################*/
57 /**
58  * Constructor for Transformation.  This does the initialization
59  * and layout of the dialog used for transforming SVG objects.  It
60  * consists of 5 pages for the 5 operations it handles:
61  * 'Move' allows x,y translation of SVG objects
62  * 'Scale' allows linear resizing of SVG objects
63  * 'Rotate' allows rotating SVG objects by a degree
64  * 'Skew' allows skewing SVG objects
65  * 'Matrix' allows applying a generic affine transform on SVG objects,
66  *     with the user specifying the 6 degrees of freedom manually.
67  *
68  * The dialog is implemented as a Gtk::Notebook with five pages.
69  * The pages are implemented using Inkscape's NotebookPage which
70  * is used to help make sure all of Inkscape's notebooks follow
71  * the same style.  We then populate the pages with our widgets,
72  * we use the ScalarUnit class for this.
73  *
74  */
75 Transformation::Transformation()
76     : UI::Widget::Panel ("", "/dialogs/transformation", SP_VERB_DIALOG_TRANSFORM),
77       _page_move              (4, 2),
78       _page_scale             (4, 2),
79       _page_rotate            (4, 2),
80       _page_skew              (4, 2),
81       _page_transform         (3, 3),
82       _scalar_move_horizontal (_("_Horizontal"), _("Horizontal displacement (relative) or position (absolute)"), UNIT_TYPE_LINEAR,
83                                "", "transform-move-horizontal", &_units_move),
84       _scalar_move_vertical   (_("_Vertical"),  _("Vertical displacement (relative) or position (absolute)"), UNIT_TYPE_LINEAR,
85                                "", "transform-move-vertical", &_units_move),
86       _scalar_scale_horizontal(_("_Width"), _("Horizontal size (absolute or percentage of current)"), UNIT_TYPE_DIMENSIONLESS,
87                                "", "transform-scale-horizontal", &_units_scale),
88       _scalar_scale_vertical  (_("_Height"),  _("Vertical size (absolute or percentage of current)"), UNIT_TYPE_DIMENSIONLESS,
89                                "", "transform-scale-vertical", &_units_scale),
90       _scalar_rotate          (_("A_ngle"), _("Rotation angle (positive = counterclockwise)"), UNIT_TYPE_RADIAL,
91                                "", "transform-rotate", &_units_rotate),
92       _scalar_skew_horizontal (_("_Horizontal"), _("Horizontal skew angle (positive = counterclockwise), or absolute displacement, or percentage displacement"), UNIT_TYPE_LINEAR,
93                                "", "transform-skew-horizontal", &_units_skew),
94       _scalar_skew_vertical   (_("_Vertical"),  _("Vertical skew angle (positive = counterclockwise), or absolute displacement, or percentage displacement"),  UNIT_TYPE_LINEAR,
95                                "", "transform-skew-vertical", &_units_skew),
97       _scalar_transform_a     ("_A", _("Transformation matrix element A")),
98       _scalar_transform_b     ("_B", _("Transformation matrix element B")),
99       _scalar_transform_c     ("_C", _("Transformation matrix element C")),
100       _scalar_transform_d     ("_D", _("Transformation matrix element D")),
101       _scalar_transform_e     ("_E", _("Transformation matrix element E")),
102       _scalar_transform_f     ("_F", _("Transformation matrix element F")),
104       _check_move_relative    (_("Rela_tive move"), _("Add the specified relative displacement to the current position; otherwise, edit the current absolute position directly")),
105       _check_scale_proportional (_("Scale proportionally"), _("Preserve the width/height ratio of the scaled objects")),
106       _check_apply_separately    (_("Apply to each _object separately"), _("Apply the scale/rotate/skew to each selected object separately; otherwise, transform the selection as a whole")),
107       _check_replace_matrix    (_("Edit c_urrent matrix"), _("Edit the current transform= matrix; otherwise, post-multiply transform= by this matrix"))
110     Gtk::Box *contents = _getContents();
112     contents->set_spacing(0);
114     // Notebook for individual transformations
115     contents->pack_start(_notebook, true, true);
117     _notebook.append_page(_page_move, _("_Move"), true);
118     layoutPageMove();
120     _notebook.append_page(_page_scale, _("_Scale"), true);
121     layoutPageScale();
123     _notebook.append_page(_page_rotate, _("_Rotate"), true);
124     layoutPageRotate();
126     _notebook.append_page(_page_skew, _("Ske_w"), true);
127     layoutPageSkew();
129     _notebook.append_page(_page_transform, _("Matri_x"), true);
130     layoutPageTransform();
132     _notebook.signal_switch_page().connect(sigc::mem_fun(*this, &Transformation::onSwitchPage));
134     // Apply separately
135     contents->pack_start(_check_apply_separately, true, true);
136     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
137     _check_apply_separately.set_active(prefs->getBool("/dialogs/transformation/applyseparately"));
138     _check_apply_separately.signal_toggled().connect(sigc::mem_fun(*this, &Transformation::onApplySeparatelyToggled));
140     // make sure all spinbuttons activate Apply on pressing Enter
141       ((Gtk::Entry *) (_scalar_move_horizontal.getWidget()))->set_activates_default(true);
142       ((Gtk::Entry *) (_scalar_move_vertical.getWidget()))->set_activates_default(true);
143       ((Gtk::Entry *) (_scalar_scale_horizontal.getWidget()))->set_activates_default(true);
144       ((Gtk::Entry *) (_scalar_scale_vertical.getWidget()))->set_activates_default(true);
145       ((Gtk::Entry *) (_scalar_rotate.getWidget()))->set_activates_default(true);
146       ((Gtk::Entry *) (_scalar_skew_horizontal.getWidget()))->set_activates_default(true);
147       ((Gtk::Entry *) (_scalar_skew_vertical.getWidget()))->set_activates_default(true);
149     updateSelection(PAGE_MOVE, _getSelection());
151     resetButton = addResponseButton(Gtk::Stock::CLEAR, 0);
152     if (resetButton) {
153         _tooltips.set_tip((*resetButton), _("Reset the values on the current tab to defaults"));
154         resetButton->set_sensitive(true);
155         resetButton->signal_clicked().connect(sigc::mem_fun(*this, &Transformation::onClear));
156     }
158     applyButton = addResponseButton(Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
159     if (applyButton) {
160         _tooltips.set_tip((*applyButton), _("Apply transformation to selection"));
161         applyButton->set_sensitive(false);
162     }
164     // Connect to the global selection changed & modified signals
165     g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
166     g_signal_connect (G_OBJECT (INKSCAPE), "modify_selection", G_CALLBACK (on_selection_modified), this);
168     show_all_children();
171 Transformation::~Transformation()
173     sp_signal_disconnect_by_data (G_OBJECT (INKSCAPE), this);
177 /*########################################################################
178 # U T I L I T Y
179 ########################################################################*/
181 void
182 Transformation::presentPage(Transformation::PageType page)
184     _notebook.set_current_page(page);
185     show();
186     present();
192 /*########################################################################
193 # S E T U P   L A Y O U T
194 ########################################################################*/
197 void
198 Transformation::layoutPageMove()
200     _units_move.setUnitType(UNIT_TYPE_LINEAR);
201     _scalar_move_horizontal.initScalar(-1e6, 1e6);
202     _scalar_move_horizontal.setDigits(3);
203     _scalar_move_horizontal.setIncrements(0.1, 1.0);
205     _scalar_move_vertical.initScalar(-1e6, 1e6);
206     _scalar_move_vertical.setDigits(3);
207     _scalar_move_vertical.setIncrements(0.1, 1.0);
209     //_scalar_move_vertical.set_label_image( INKSCAPE_STOCK_ARROWS_HOR );
210     _page_move.table()
211         .attach(_scalar_move_horizontal, 0, 2, 0, 1, Gtk::FILL, Gtk::SHRINK);
213     _page_move.table()
214         .attach(_units_move, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
216     _scalar_move_horizontal.signal_value_changed()
217         .connect(sigc::mem_fun(*this, &Transformation::onMoveValueChanged));
219     //_scalar_move_vertical.set_label_image( INKSCAPE_STOCK_ARROWS_VER );
220     _page_move.table()
221         .attach(_scalar_move_vertical, 0, 2, 1, 2, Gtk::FILL, Gtk::SHRINK);
223     _scalar_move_vertical.signal_value_changed()
224         .connect(sigc::mem_fun(*this, &Transformation::onMoveValueChanged));
226     // Relative moves
227     _page_move.table()
228         .attach(_check_move_relative, 0, 2, 2, 3, Gtk::FILL, Gtk::SHRINK);
229     _check_move_relative.set_active(true);
230     _check_move_relative.signal_toggled()
231         .connect(sigc::mem_fun(*this, &Transformation::onMoveRelativeToggled));
234 void
235 Transformation::layoutPageScale()
237     _units_scale.setUnitType(UNIT_TYPE_DIMENSIONLESS);
238     _units_scale.setUnitType(UNIT_TYPE_LINEAR);
240     _scalar_scale_horizontal.initScalar(-1e6, 1e6);
241     _scalar_scale_horizontal.setValue(100.0, "%");
242     _scalar_scale_horizontal.setDigits(3);
243     _scalar_scale_horizontal.setIncrements(0.1, 1.0);
244     _scalar_scale_horizontal.setAbsoluteIsIncrement(true);
245     _scalar_scale_horizontal.setPercentageIsIncrement(true);
247     _scalar_scale_vertical.initScalar(-1e6, 1e6);
248     _scalar_scale_vertical.setValue(100.0, "%");
249     _scalar_scale_vertical.setDigits(3);
250     _scalar_scale_vertical.setIncrements(0.1, 1.0);
251     _scalar_scale_vertical.setAbsoluteIsIncrement(true);
252     _scalar_scale_vertical.setPercentageIsIncrement(true);
254     _page_scale.table()
255         .attach(_scalar_scale_horizontal, 0, 2, 0, 1, Gtk::FILL, Gtk::SHRINK);
256     _scalar_scale_horizontal.signal_value_changed()
257         .connect(sigc::mem_fun(*this, &Transformation::onScaleXValueChanged));
259     _page_scale.table()
260         .attach(_units_scale, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
262     _page_scale.table()
263         .attach(_scalar_scale_vertical, 0, 2, 1, 2, Gtk::FILL, Gtk::SHRINK);
264     _scalar_scale_vertical.signal_value_changed()
265         .connect(sigc::mem_fun(*this, &Transformation::onScaleYValueChanged));
267     _page_scale.table()
268         .attach(_check_scale_proportional, 0, 2, 2, 3, Gtk::FILL, Gtk::SHRINK);
269     _check_scale_proportional.set_active(false);
270     _check_scale_proportional.signal_toggled()
271         .connect(sigc::mem_fun(*this, &Transformation::onScaleProportionalToggled));
273     //TODO: add a widget for selecting the fixed point in scaling, or honour rotation center?
276 void
277 Transformation::layoutPageRotate()
279     _units_rotate.setUnitType(UNIT_TYPE_RADIAL);
281     _scalar_rotate.initScalar(-360.0, 360.0);
282     _scalar_rotate.setDigits(3);
283     _scalar_rotate.setIncrements(0.1, 1.0);
285     _page_rotate.table()
286         .attach(_scalar_rotate, 0, 2, 0, 1, Gtk::FILL, Gtk::SHRINK);
288     _page_rotate.table()
289         .attach(_units_rotate, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
291     _scalar_rotate.signal_value_changed()
292         .connect(sigc::mem_fun(*this, &Transformation::onRotateValueChanged));
294     //TODO: honour rotation center?
297 void
298 Transformation::layoutPageSkew()
300     _units_skew.setUnitType(UNIT_TYPE_LINEAR);
301     _units_skew.setUnitType(UNIT_TYPE_DIMENSIONLESS);
302     _units_skew.setUnitType(UNIT_TYPE_RADIAL);
304     _scalar_skew_horizontal.initScalar(-1e6, 1e6);
305     _scalar_skew_horizontal.setDigits(3);
306     _scalar_skew_horizontal.setIncrements(0.1, 1.0);
308     _scalar_skew_vertical.initScalar(-1e6, 1e6);
309     _scalar_skew_vertical.setDigits(3);
310     _scalar_skew_vertical.setIncrements(0.1, 1.0);
312     _page_skew.table()
313         .attach(_scalar_skew_horizontal, 0, 2, 0, 1, Gtk::FILL, Gtk::SHRINK);
314     _scalar_skew_horizontal.signal_value_changed()
315         .connect(sigc::mem_fun(*this, &Transformation::onSkewValueChanged));
317     _page_skew.table()
318         .attach(_units_skew, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
320     _page_skew.table()
321         .attach(_scalar_skew_vertical, 0, 2, 1, 2, Gtk::FILL, Gtk::SHRINK);
322     _scalar_skew_vertical.signal_value_changed()
323         .connect(sigc::mem_fun(*this, &Transformation::onSkewValueChanged));
325     //TODO: honour rotation center?
330 void
331 Transformation::layoutPageTransform()
333     _scalar_transform_a.setWidgetSizeRequest(65, -1);
334     _scalar_transform_a.setRange(-1e10, 1e10);
335     _scalar_transform_a.setDigits(3);
336     _scalar_transform_a.setIncrements(0.1, 1.0);
337     _scalar_transform_a.setValue(1.0);
338     _page_transform.table()
339         .attach(_scalar_transform_a, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
340     _scalar_transform_a.signal_value_changed()
341         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
344     _scalar_transform_b.setWidgetSizeRequest(65, -1);
345     _scalar_transform_b.setRange(-1e10, 1e10);
346     _scalar_transform_b.setDigits(3);
347     _scalar_transform_b.setIncrements(0.1, 1.0);
348     _scalar_transform_b.setValue(0.0);
349     _page_transform.table()
350         .attach(_scalar_transform_b, 0, 1, 1, 2, Gtk::SHRINK, Gtk::SHRINK);
351     _scalar_transform_b.signal_value_changed()
352         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
355     _scalar_transform_c.setWidgetSizeRequest(65, -1);
356     _scalar_transform_c.setRange(-1e10, 1e10);
357     _scalar_transform_c.setDigits(3);
358     _scalar_transform_c.setIncrements(0.1, 1.0);
359     _scalar_transform_c.setValue(0.0);
360     _page_transform.table()
361         .attach(_scalar_transform_c, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
362     _scalar_transform_c.signal_value_changed()
363         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
366     _scalar_transform_d.setWidgetSizeRequest(65, -1);
367     _scalar_transform_d.setRange(-1e10, 1e10);
368     _scalar_transform_d.setDigits(3);
369     _scalar_transform_d.setIncrements(0.1, 1.0);
370     _scalar_transform_d.setValue(1.0);
371     _page_transform.table()
372         .attach(_scalar_transform_d, 1, 2, 1, 2, Gtk::SHRINK, Gtk::SHRINK);
373     _scalar_transform_d.signal_value_changed()
374         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
377     _scalar_transform_e.setWidgetSizeRequest(65, -1);
378     _scalar_transform_e.setRange(-1e10, 1e10);
379     _scalar_transform_e.setDigits(3);
380     _scalar_transform_e.setIncrements(0.1, 1.0);
381     _scalar_transform_e.setValue(0.0);
382     _page_transform.table()
383         .attach(_scalar_transform_e, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
384     _scalar_transform_e.signal_value_changed()
385         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
388     _scalar_transform_f.setWidgetSizeRequest(65, -1);
389     _scalar_transform_f.setRange(-1e10, 1e10);
390     _scalar_transform_f.setDigits(3);
391     _scalar_transform_f.setIncrements(0.1, 1.0);
392     _scalar_transform_f.setValue(0.0);
393     _page_transform.table()
394         .attach(_scalar_transform_f, 2, 3, 1, 2, Gtk::SHRINK, Gtk::SHRINK);
395     _scalar_transform_f.signal_value_changed()
396         .connect(sigc::mem_fun(*this, &Transformation::onTransformValueChanged));
398     // Edit existing matrix
399     _page_transform.table()
400         .attach(_check_replace_matrix, 0, 2, 2, 3, Gtk::FILL, Gtk::SHRINK);
401     _check_replace_matrix.set_active(false);
402     _check_replace_matrix.signal_toggled()
403         .connect(sigc::mem_fun(*this, &Transformation::onReplaceMatrixToggled));
407 /*########################################################################
408 # U P D A T E
409 ########################################################################*/
411 void
412 Transformation::updateSelection(PageType page, Inkscape::Selection *selection)
414     if (!selection || selection->isEmpty())
415         return;
417     switch (page) {
418         case PAGE_MOVE: {
419             updatePageMove(selection);
420             break;
421         }
422         case PAGE_SCALE: {
423             updatePageScale(selection);
424             break;
425         }
426         case PAGE_ROTATE: {
427             updatePageRotate(selection);
428             break;
429         }
430         case PAGE_SKEW: {
431             updatePageSkew(selection);
432             break;
433         }
434         case PAGE_TRANSFORM: {
435             updatePageTransform(selection);
436             break;
437         }
438         case PAGE_QTY: {
439             break;
440         }
441     }
443     setResponseSensitive(Gtk::RESPONSE_APPLY,
444                          selection && !selection->isEmpty());
447 void
448 Transformation::onSwitchPage(GtkNotebookPage */*page*/,
449                                    guint pagenum)
451     updateSelection((PageType)pagenum, sp_desktop_selection(getDesktop()));
455 void
456 Transformation::updatePageMove(Inkscape::Selection *selection)
458     if (selection && !selection->isEmpty()) {
459         if (!_check_move_relative.get_active()) {
460             Geom::OptRect bbox = selection->bounds();
461             if (bbox) {
462                 double x = bbox->min()[Geom::X];
463                 double y = bbox->min()[Geom::Y];
465                 _scalar_move_horizontal.setValue(x, "px");
466                 _scalar_move_vertical.setValue(y, "px");
467             }
468         } else {
469             // do nothing, so you can apply the same relative move to many objects in turn
470         }
471         _page_move.set_sensitive(true);
472     } else {
473         _page_move.set_sensitive(false);
474     }
477 void
478 Transformation::updatePageScale(Inkscape::Selection *selection)
480     if (selection && !selection->isEmpty()) {
481         Geom::OptRect bbox = selection->bounds();
482         if (bbox) {
483             double w = bbox->dimensions()[Geom::X];
484             double h = bbox->dimensions()[Geom::Y];
485             _scalar_scale_horizontal.setHundredPercent(w);
486             _scalar_scale_vertical.setHundredPercent(h);
487             onScaleXValueChanged(); // to update x/y proportionality if switch is on
488             _page_scale.set_sensitive(true);
489         } else {
490             _page_scale.set_sensitive(false);
491         }
492     } else {
493         _page_scale.set_sensitive(false);
494     }
497 void
498 Transformation::updatePageRotate(Inkscape::Selection *selection)
500     if (selection && !selection->isEmpty()) {
501         _page_rotate.set_sensitive(true);
502     } else {
503         _page_rotate.set_sensitive(false);
504     }
507 void
508 Transformation::updatePageSkew(Inkscape::Selection *selection)
510     if (selection && !selection->isEmpty()) {
511         Geom::OptRect bbox = selection->bounds();
512         if (bbox) {
513             double w = bbox->dimensions()[Geom::X];
514             double h = bbox->dimensions()[Geom::Y];
515             _scalar_skew_vertical.setHundredPercent(w);
516             _scalar_skew_horizontal.setHundredPercent(h);
517             _page_skew.set_sensitive(true);
518         } else {
519             _page_skew.set_sensitive(false);
520         }
521     } else {
522         _page_skew.set_sensitive(false);
523     }
526 void
527 Transformation::updatePageTransform(Inkscape::Selection *selection)
529     if (selection && !selection->isEmpty()) {
530         if (_check_replace_matrix.get_active()) {
531             Geom::Matrix current (SP_ITEM(selection->itemList()->data)->transform); // take from the first item in selection
533             Geom::Matrix new_displayed = current;
535             _scalar_transform_a.setValue(new_displayed[0]);
536             _scalar_transform_b.setValue(new_displayed[1]);
537             _scalar_transform_c.setValue(new_displayed[2]);
538             _scalar_transform_d.setValue(new_displayed[3]);
539             _scalar_transform_e.setValue(new_displayed[4]);
540             _scalar_transform_f.setValue(new_displayed[5]);
541         } else {
542             // do nothing, so you can apply the same matrix to many objects in turn
543         }
544         _page_transform.set_sensitive(true);
545     } else {
546         _page_transform.set_sensitive(false);
547     }
554 /*########################################################################
555 # A P P L Y
556 ########################################################################*/
560 void
561 Transformation::_apply()
563     Inkscape::Selection * const selection = _getSelection();
564     if (!selection || selection->isEmpty())
565         return;
567     int const page = _notebook.get_current_page();
569     switch (page) {
570         case PAGE_MOVE: {
571             applyPageMove(selection);
572             break;
573         }
574         case PAGE_ROTATE: {
575             applyPageRotate(selection);
576             break;
577         }
578         case PAGE_SCALE: {
579             applyPageScale(selection);
580             break;
581         }
582         case PAGE_SKEW: {
583             applyPageSkew(selection);
584             break;
585         }
586         case PAGE_TRANSFORM: {
587             applyPageTransform(selection);
588             break;
589         }
590     }
592     //Let's play with never turning this off
593     //setResponseSensitive(Gtk::RESPONSE_APPLY, false);
596 void
597 Transformation::applyPageMove(Inkscape::Selection *selection)
599     double x = _scalar_move_horizontal.getValue("px");
600     double y = _scalar_move_vertical.getValue("px");
602     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
603     if (!prefs->getBool("/dialogs/transformation/applyseparately")) {
604         // move selection as a whole
605         if (_check_move_relative.get_active()) {
606             sp_selection_move_relative(selection, x, y);
607         } else {
608             Geom::OptRect bbox = selection->bounds();
609             if (bbox) {
610                 sp_selection_move_relative(selection,
611                                            x - bbox->min()[Geom::X], y - bbox->min()[Geom::Y]);
612             }
613         }
614     } else {
616         if (_check_move_relative.get_active()) {
617             // shift each object relatively to the previous one
618             using Inkscape::Util::GSListConstIterator;
619             std::list<SPItem *> selected;
620             selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
621             if (selected.empty()) return;
623             if (fabs(x) > 1e-6) {
624                 std::vector< BBoxSort  > sorted;
625                 for (std::list<SPItem *>::iterator it(selected.begin());
626                      it != selected.end();
627                      ++it)
628                 {
629                     Geom::OptRect bbox = sp_item_bbox_desktop(*it);
630                     if (bbox) {
631                         sorted.push_back(BBoxSort(*it, *bbox, Geom::X, x > 0? 1. : 0., x > 0? 0. : 1.));
632                     }
633                 }
634                 //sort bbox by anchors
635                 std::sort(sorted.begin(), sorted.end());
637                 double move = x;
638                 for ( std::vector<BBoxSort> ::iterator it (sorted.begin());
639                       it < sorted.end();
640                       it ++ )
641                 {
642                     sp_item_move_rel(it->item, Geom::Translate(move, 0));
643                     // move each next object by x relative to previous
644                     move += x;
645                 }
646             }
647             if (fabs(y) > 1e-6) {
648                 std::vector< BBoxSort  > sorted;
649                 for (std::list<SPItem *>::iterator it(selected.begin());
650                      it != selected.end();
651                      ++it)
652                 {
653                     Geom::OptRect bbox = sp_item_bbox_desktop(*it);
654                     if (bbox) {
655                         sorted.push_back(BBoxSort(*it, *bbox, Geom::Y, y > 0? 1. : 0., y > 0? 0. : 1.));
656                     }
657                 }
658                 //sort bbox by anchors
659                 std::sort(sorted.begin(), sorted.end());
661                 double move = y;
662                 for ( std::vector<BBoxSort> ::iterator it (sorted.begin());
663                       it < sorted.end();
664                       it ++ )
665                 {
666                     sp_item_move_rel(it->item, Geom::Translate(0, move));
667                     // move each next object by x relative to previous
668                     move += y;
669                 }
670             }
671         } else {
672             Geom::OptRect bbox = selection->bounds();
673             if (bbox) {
674                 sp_selection_move_relative(selection,
675                                            x - bbox->min()[Geom::X], y - bbox->min()[Geom::Y]);
676             }
677         }
678     }
680     sp_document_done ( sp_desktop_document (selection->desktop()) , SP_VERB_DIALOG_TRANSFORM,
681                        _("Move"));
684 void
685 Transformation::applyPageScale(Inkscape::Selection *selection)
687     double scaleX = _scalar_scale_horizontal.getValue("px");
688     double scaleY = _scalar_scale_vertical.getValue("px");
690     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
691     if (prefs->getBool("/dialogs/transformation/applyseparately")) {
692         for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
693             SPItem *item = SP_ITEM(l->data);
694             Geom::Scale scale (0,0);
695             // the values are increments!
696             if (_units_scale.isAbsolute()) {
697                 Geom::OptRect bbox(sp_item_bbox_desktop(item));
698                 if (bbox) {
699                     double new_width = scaleX;
700                     if (fabs(new_width) < 1e-6) new_width = 1e-6; // not 0, as this would result in a nasty no-bbox object
701                     double new_height = scaleY;
702                     if (fabs(new_height) < 1e-6) new_height = 1e-6;
703                     scale = Geom::Scale(new_width / bbox->dimensions()[Geom::X], new_height / bbox->dimensions()[Geom::Y]);
704                 }
705             } else {
706                 double new_width = scaleX;
707                 if (fabs(new_width) < 1e-6) new_width = 1e-6;
708                 double new_height = scaleY;
709                 if (fabs(new_height) < 1e-6) new_height = 1e-6;
710                 scale = Geom::Scale(new_width / 100.0, new_height / 100.0);
711             }
712             sp_item_scale_rel (item, scale);
713         }
714     } else {
715         Geom::OptRect bbox(selection->bounds());
716         if (bbox) {
717             Geom::Point center(bbox->midpoint()); // use rotation center?
718             Geom::Scale scale (0,0);
719             // the values are increments!
720             if (_units_scale.isAbsolute()) {
721                 double new_width = scaleX;
722                 if (fabs(new_width) < 1e-6) new_width = 1e-6;
723                 double new_height = scaleY;
724                 if (fabs(new_height) < 1e-6) new_height = 1e-6;
725                 scale = Geom::Scale(new_width / bbox->dimensions()[Geom::X], new_height / bbox->dimensions()[Geom::Y]);
726             } else {
727                 double new_width = scaleX;
728                 if (fabs(new_width) < 1e-6) new_width = 1e-6;
729                 double new_height = scaleY;
730                 if (fabs(new_height) < 1e-6) new_height = 1e-6;
731                 scale = Geom::Scale(new_width / 100.0, new_height / 100.0);
732             }
733             sp_selection_scale_relative(selection, center, scale);
734         }
735     }
737     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_DIALOG_TRANSFORM,
738                      _("Scale"));
741 void
742 Transformation::applyPageRotate(Inkscape::Selection *selection)
744     double angle = _scalar_rotate.getValue("deg");
746     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
747     if (prefs->getBool("/dialogs/transformation/applyseparately")) {
748         for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
749             SPItem *item = SP_ITEM(l->data);
750             sp_item_rotate_rel(item, Geom::Rotate (angle*M_PI/180.0));
751         }
752     } else {
753         boost::optional<Geom::Point> center = selection->center();
754         if (center) {
755             sp_selection_rotate_relative(selection, *center, angle);
756         }
757     }
759     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_DIALOG_TRANSFORM,
760                      _("Rotate"));
763 void
764 Transformation::applyPageSkew(Inkscape::Selection *selection)
766     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
767     if (prefs->getBool("/dialogs/transformation/applyseparately")) {
768         for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
769             SPItem *item = SP_ITEM(l->data);
771             if (!_units_skew.isAbsolute()) { // percentage
772                 double skewX = _scalar_skew_horizontal.getValue("%");
773                 double skewY = _scalar_skew_vertical.getValue("%");
774                 sp_item_skew_rel (item, 0.01*skewX, 0.01*skewY);
775             } else if (_units_skew.isRadial()) { //deg or rad
776                 double angleX = _scalar_skew_horizontal.getValue("rad");
777                 double angleY = _scalar_skew_vertical.getValue("rad");
778                 double skewX = tan(-angleX);
779                 double skewY = tan(angleY);
780                 sp_item_skew_rel (item, skewX, skewY);
781             } else { // absolute displacement
782                 double skewX = _scalar_skew_horizontal.getValue("px");
783                 double skewY = _scalar_skew_vertical.getValue("px");
784                 Geom::OptRect bbox(sp_item_bbox_desktop(item));
785                 if (bbox) {
786                     double width = bbox->dimensions()[Geom::X];
787                     double height = bbox->dimensions()[Geom::Y];
788                     sp_item_skew_rel (item, skewX/height, skewY/width);
789                 }
790             }
791         }
792     } else { // transform whole selection
793         Geom::OptRect bbox = selection->bounds();
794         boost::optional<Geom::Point> center = selection->center();
796         if ( bbox && center ) {
797             double width  = bbox->dimensions()[Geom::X];
798             double height = bbox->dimensions()[Geom::Y];
800             if (!_units_skew.isAbsolute()) { // percentage
801                 double skewX = _scalar_skew_horizontal.getValue("%");
802                 double skewY = _scalar_skew_vertical.getValue("%");
803                 sp_selection_skew_relative(selection, *center, 0.01*skewX, 0.01*skewY);
804             } else if (_units_skew.isRadial()) { //deg or rad
805                 double angleX = _scalar_skew_horizontal.getValue("rad");
806                 double angleY = _scalar_skew_vertical.getValue("rad");
807                 double skewX = tan(-angleX);
808                 double skewY = tan(angleY);
809                 sp_selection_skew_relative(selection, *center, skewX, skewY);
810             } else { // absolute displacement
811                 double skewX = _scalar_skew_horizontal.getValue("px");
812                 double skewY = _scalar_skew_vertical.getValue("px");
813                 sp_selection_skew_relative(selection, *center, skewX/height, skewY/width);
814             }
815         }
816     }
818     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_DIALOG_TRANSFORM,
819                      _("Skew"));
823 void
824 Transformation::applyPageTransform(Inkscape::Selection *selection)
826     double a = _scalar_transform_a.getValue();
827     double b = _scalar_transform_b.getValue();
828     double c = _scalar_transform_c.getValue();
829     double d = _scalar_transform_d.getValue();
830     double e = _scalar_transform_e.getValue();
831     double f = _scalar_transform_f.getValue();
833     Geom::Matrix displayed(a, b, c, d, e, f);
835     if (_check_replace_matrix.get_active()) {
836         for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
837             SPItem *item = SP_ITEM(l->data);
838             sp_item_set_item_transform(item, displayed);
839             SP_OBJECT(item)->updateRepr();
840         }
841     } else {
842         sp_selection_apply_affine(selection, displayed); // post-multiply each object's transform
843     }
845     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_DIALOG_TRANSFORM,
846                      _("Edit transformation matrix"));
853 /*########################################################################
854 # V A L U E - C H A N G E D    C A L L B A C K S
855 ########################################################################*/
857 void
858 Transformation::onMoveValueChanged()
860     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
863 void
864 Transformation::onMoveRelativeToggled()
866     Inkscape::Selection *selection = _getSelection();
868     if (!selection || selection->isEmpty())
869         return;
871     double x = _scalar_move_horizontal.getValue("px");
872     double y = _scalar_move_vertical.getValue("px");
874     //g_message("onMoveRelativeToggled: %f, %f px\n", x, y);
876     Geom::OptRect bbox = selection->bounds();
878     if (bbox) {
879         if (_check_move_relative.get_active()) {
880             // From absolute to relative
881             _scalar_move_horizontal.setValue(x - bbox->min()[Geom::X], "px");
882             _scalar_move_vertical.setValue(  y - bbox->min()[Geom::Y], "px");
883         } else {
884             // From relative to absolute
885             _scalar_move_horizontal.setValue(bbox->min()[Geom::X] + x, "px");
886             _scalar_move_vertical.setValue(  bbox->min()[Geom::Y] + y, "px");
887         }
888     }
890     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
893 void
894 Transformation::onScaleXValueChanged()
896     if (_scalar_scale_horizontal.setProgrammatically) {
897         _scalar_scale_horizontal.setProgrammatically = false;
898         return;
899     }
901     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
903     if (_check_scale_proportional.get_active()) {
904         if (!_units_scale.isAbsolute()) { // percentage, just copy over
905             _scalar_scale_vertical.setValue(_scalar_scale_horizontal.getValue("%"));
906         } else {
907             double scaleXPercentage = _scalar_scale_horizontal.getAsPercentage();
908             _scalar_scale_vertical.setFromPercentage (scaleXPercentage);
909         }
910     }
913 void
914 Transformation::onScaleYValueChanged()
916     if (_scalar_scale_vertical.setProgrammatically) {
917         _scalar_scale_vertical.setProgrammatically = false;
918         return;
919     }
921     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
923     if (_check_scale_proportional.get_active()) {
924         if (!_units_scale.isAbsolute()) { // percentage, just copy over
925             _scalar_scale_horizontal.setValue(_scalar_scale_vertical.getValue("%"));
926         } else {
927             double scaleYPercentage = _scalar_scale_vertical.getAsPercentage();
928             _scalar_scale_horizontal.setFromPercentage (scaleYPercentage);
929         }
930     }
933 void
934 Transformation::onRotateValueChanged()
936     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
939 void
940 Transformation::onSkewValueChanged()
942     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
945 void
946 Transformation::onTransformValueChanged()
949     /*
950     double a = _scalar_transform_a.getValue();
951     double b = _scalar_transform_b.getValue();
952     double c = _scalar_transform_c.getValue();
953     double d = _scalar_transform_d.getValue();
954     double e = _scalar_transform_e.getValue();
955     double f = _scalar_transform_f.getValue();
957     //g_message("onTransformValueChanged: (%f, %f, %f, %f, %f, %f)\n",
958     //          a, b, c, d, e ,f);
959     */
961     setResponseSensitive(Gtk::RESPONSE_APPLY, true);
964 void
965 Transformation::onReplaceMatrixToggled()
967     Inkscape::Selection *selection = _getSelection();
969     if (!selection || selection->isEmpty())
970         return;
972     double a = _scalar_transform_a.getValue();
973     double b = _scalar_transform_b.getValue();
974     double c = _scalar_transform_c.getValue();
975     double d = _scalar_transform_d.getValue();
976     double e = _scalar_transform_e.getValue();
977     double f = _scalar_transform_f.getValue();
979     Geom::Matrix displayed (a, b, c, d, e, f);
980     Geom::Matrix current = SP_ITEM(selection->itemList()->data)->transform; // take from the first item in selection
982     Geom::Matrix new_displayed;
983     if (_check_replace_matrix.get_active()) {
984         new_displayed = current;
985     } else {
986         new_displayed = current.inverse() * displayed;
987     }
989     _scalar_transform_a.setValue(new_displayed[0]);
990     _scalar_transform_b.setValue(new_displayed[1]);
991     _scalar_transform_c.setValue(new_displayed[2]);
992     _scalar_transform_d.setValue(new_displayed[3]);
993     _scalar_transform_e.setValue(new_displayed[4]);
994     _scalar_transform_f.setValue(new_displayed[5]);
997 void
998 Transformation::onScaleProportionalToggled()
1000     onScaleXValueChanged();
1004 void
1005 Transformation::onClear()
1007     int const page = _notebook.get_current_page();
1009     switch (page) {
1010     case PAGE_MOVE: {
1011         Inkscape::Selection *selection = _getSelection();
1012         if (!selection || selection->isEmpty() || _check_move_relative.get_active()) {
1013             _scalar_move_horizontal.setValue(0);
1014             _scalar_move_vertical.setValue(0);
1015         } else {
1016             Geom::OptRect bbox = selection->bounds();
1017             if (bbox) {
1018                 _scalar_move_horizontal.setValue(bbox->min()[Geom::X], "px");
1019                 _scalar_move_vertical.setValue(bbox->min()[Geom::Y], "px");
1020             }
1021         }
1022         break;
1023     }
1024     case PAGE_ROTATE: {
1025         _scalar_rotate.setValue(0);
1026         break;
1027     }
1028     case PAGE_SCALE: {
1029         _scalar_scale_horizontal.setValue(100, "%");
1030         _scalar_scale_vertical.setValue(100, "%");
1031         break;
1032     }
1033     case PAGE_SKEW: {
1034         _scalar_skew_horizontal.setValue(0);
1035         _scalar_skew_vertical.setValue(0);
1036         break;
1037     }
1038     case PAGE_TRANSFORM: {
1039         _scalar_transform_a.setValue(1);
1040         _scalar_transform_b.setValue(0);
1041         _scalar_transform_c.setValue(0);
1042         _scalar_transform_d.setValue(1);
1043         _scalar_transform_e.setValue(0);
1044         _scalar_transform_f.setValue(0);
1045         break;
1046     }
1047     }
1050 void
1051 Transformation::onApplySeparatelyToggled()
1053     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1054     prefs->setBool("/dialogs/transformation/applyseparately", _check_apply_separately.get_active());
1058 } // namespace Dialog
1059 } // namespace UI
1060 } // namespace Inkscape
1062 /*
1063   Local Variables:
1064   mode:c++
1065   c-file-style:"stroustrup"
1066   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1067   indent-tabs-mode:nil
1068   fill-column:99
1069   End:
1070 */
1071 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :