Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / ui / dialog / guides.cpp
1 /** @file
2  * @brief Simple guideline dialog
3  */
4 /* Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Andrius R. <knutux@gmail.com>
7  *   Johan Engelen
8  *
9  * Copyright (C) 1999-2007 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "display/guideline.h"
18 #include "helper/unit-menu.h"
19 #include "helper/units.h"
20 #include "desktop.h"
21 #include "document.h"
22 #include "sp-guide.h"
23 #include "sp-namedview.h"
24 #include "desktop-handles.h"
25 #include "event-context.h"
26 #include "widgets/desktop-widget.h"
27 #include "sp-metrics.h"
28 #include <glibmm/i18n.h>
29 #include "dialogs/dialog-events.h"
30 #include "message-context.h"
31 #include "xml/repr.h"
32 #include <2geom/point.h>
33 #include <2geom/angle.h>
34 #include "guides.h"
36 namespace Inkscape {
37 namespace UI {
38 namespace Dialogs {
40 GuidelinePropertiesDialog::GuidelinePropertiesDialog(SPGuide *guide, SPDesktop *desktop)
41 : _desktop(desktop), _guide(guide),
42   _label_units(_("Unit:")),
43   _label_X(_("X:")),
44   _label_Y(_("Y:")),
45   _label_degrees(_("Angle (degrees):")),
46   _relative_toggle(_("Rela_tive change"), _("Move and/or rotate the guide relative to current settings")),
47   _adjustment_x(0.0, -1e6, 1e6, 1.0, 10.0, 10.0),  
48   _adjustment_y(0.0, -1e6, 1e6, 1.0, 10.0, 10.0),  
49   _adj_angle(0.0, -360, 360, 1.0, 10.0, 10.0),  
50   _unit_selector(NULL), _mode(true), _oldpos(0.,0.), _oldangle(0.0)
51 {
52 }
54 GuidelinePropertiesDialog::~GuidelinePropertiesDialog() {
55 }
57 void GuidelinePropertiesDialog::showDialog(SPGuide *guide, SPDesktop *desktop) {
58     GuidelinePropertiesDialog dialog(guide, desktop);
59     dialog._setup();
60     dialog.run();
61 }
63 void GuidelinePropertiesDialog::_modeChanged()
64 {
65     _mode = !_relative_toggle.get_active();
66     if (!_mode) {
67         // relative
68         _spin_angle.set_value(0);
70         _spin_button_y.set_value(0);
71         _spin_button_x.set_value(0);
72     } else {
73         // absolute
74         _spin_angle.set_value(_oldangle);
76         SPUnit const &unit = *sp_unit_selector_get_unit(SP_UNIT_SELECTOR(_unit_selector->gobj()));
77         gdouble const val_y = sp_pixels_get_units(_oldpos[Geom::Y], unit);
78         _spin_button_y.set_value(val_y);
79         gdouble const val_x = sp_pixels_get_units(_oldpos[Geom::X], unit);
80         _spin_button_x.set_value(val_x);
81     }
82 }
84 void GuidelinePropertiesDialog::_onApply()
85 {
86     double deg_angle = _spin_angle.get_value();
87     if (!_mode)
88         deg_angle += _oldangle;
89     Geom::Point normal;
90     if ( deg_angle == 90. || deg_angle == 270. || deg_angle == -90. || deg_angle == -270.) {
91         normal = Geom::Point(1.,0.);
92     } else if ( deg_angle == 0. || deg_angle == 180. || deg_angle == -180.) {
93         normal = Geom::Point(0.,1.);
94     } else {
95         double rad_angle = Geom::deg_to_rad( deg_angle );
96         normal = Geom::rot90(Geom::Point::polar(rad_angle, 1.0));
97     }
98     sp_guide_set_normal(*_guide, normal, true);
100     SPUnit const &unit = *sp_unit_selector_get_unit(SP_UNIT_SELECTOR(_unit_selector->gobj()));
101     gdouble const raw_dist_x = _spin_button_x.get_value();
102     gdouble const points_x = sp_units_get_pixels(raw_dist_x, unit);
103     gdouble const raw_dist_y = _spin_button_y.get_value();
104     gdouble const points_y = sp_units_get_pixels(raw_dist_y, unit);
105     Geom::Point newpos(points_x, points_y);
106     if (!_mode)
107         newpos += _oldpos;
109     sp_guide_moveto(*_guide, newpos, true);
111     SPDocumentUndo::done(SP_OBJECT_DOCUMENT(_guide), SP_VERB_NONE, 
112                      _("Set guide properties"));
115 void GuidelinePropertiesDialog::_onOK()
117     _onApply();
120 void GuidelinePropertiesDialog::_onDelete()
122     SPDocument *doc = SP_OBJECT_DOCUMENT(_guide);
123     sp_guide_remove(_guide);
124     SPDocumentUndo::done(doc, SP_VERB_NONE, 
125                      _("Delete guide"));
128 void GuidelinePropertiesDialog::_response(gint response)
130     switch (response) {
131         case Gtk::RESPONSE_OK:
132             _onOK();
133             break;
134         case -12:
135             _onDelete();
136             break;
137         case Gtk::RESPONSE_CANCEL:
138             break;
139         case Gtk::RESPONSE_DELETE_EVENT:
140             break;
141 /*      case GTK_RESPONSE_APPLY:
142         _onApply();
143         break;
144 */
145         default:
146             g_assert_not_reached();
147     }
150 void GuidelinePropertiesDialog::_setup() {
151     set_title(_("Guideline"));
152     add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
153     add_button(Gtk::Stock::DELETE, -12);
154     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
156     Gtk::VBox *mainVBox = get_vbox();
158     _layout_table.set_spacings(4);
159     _layout_table.resize (3, 4);
161     mainVBox->pack_start(_layout_table, false, false, 0);
163     _label_name.set_label("foo0");
164     _layout_table.attach(_label_name,
165                          0, 3, 0, 1, Gtk::FILL, Gtk::FILL);
166     _label_name.set_alignment(0, 0.5);
168     _label_descr.set_label("foo1");
169     _layout_table.attach(_label_descr,
170                          0, 3, 1, 2, Gtk::FILL, Gtk::FILL);
171     _label_descr.set_alignment(0, 0.5);
173     // indent
174     _layout_table.attach(*manage(new Gtk::Label(" ")),
175                          0, 1, 2, 3, Gtk::FILL, Gtk::FILL, 10);
177     // mode radio button
178     _layout_table.attach(_relative_toggle,
179                          1, 3, 9, 10, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
180     _relative_toggle.signal_toggled().connect(sigc::mem_fun(*this, &GuidelinePropertiesDialog::_modeChanged));
182     // unitmenu
183     /* fixme: We should allow percents here too, as percents of the canvas size */
184     GtkWidget *unit_selector = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
185     sp_unit_selector_set_unit(SP_UNIT_SELECTOR(unit_selector), _desktop->namedview->doc_units);
186     _unit_selector = Gtk::manage(Glib::wrap(unit_selector));
188     // position spinbuttons
189     sp_unit_selector_add_adjustment(SP_UNIT_SELECTOR(unit_selector), GTK_ADJUSTMENT(_adjustment_x.gobj()));
190     sp_unit_selector_add_adjustment(SP_UNIT_SELECTOR(unit_selector), GTK_ADJUSTMENT(_adjustment_y.gobj()));
191     _spin_button_x.configure(_adjustment_x, 1.0 , 3);
192     _spin_button_x.set_numeric();
193     _spin_button_y.configure(_adjustment_y, 1.0 , 3);
194     _spin_button_y.set_numeric();
195     _layout_table.attach(_label_X,
196                          1, 2, 4, 5, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
197     _layout_table.attach(_spin_button_x,
198                          2, 3, 4, 5, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
199     _layout_table.attach(_label_Y,
200                          1, 2, 5, 6, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
201     _layout_table.attach(_spin_button_y,
202                          2, 3, 5, 6, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
203     gtk_signal_connect_object(GTK_OBJECT(_spin_button_x.gobj()), "activate",
204                               GTK_SIGNAL_FUNC(gtk_window_activate_default),
205                               gobj());
207     _layout_table.attach(_label_units,
208                          1, 2, 6, 7, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
209     _layout_table.attach(*_unit_selector,
210                          2, 3, 6, 7, Gtk::FILL, Gtk::FILL);
212     // angle spinbutton
213     _spin_angle.configure(_adj_angle, 5.0 , 3);
214     _spin_angle.set_numeric();
215     _spin_angle.show();
216     _layout_table.attach(_label_degrees,
217                          1, 2, 8, 9, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
218     _layout_table.attach(_spin_angle,
219                          2, 3, 8, 9, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
222     // dialog
223     set_default_response(Gtk::RESPONSE_OK);
224     signal_response().connect(sigc::mem_fun(*this, &GuidelinePropertiesDialog::_response));
226     // initialize dialog
227     _oldpos = _guide->point_on_line;
228     if (_guide->is_vertical()) {
229         _oldangle = 90;
230     } else if (_guide->is_horizontal()) {
231         _oldangle = 0;
232     } else {
233         _oldangle = Geom::rad_to_deg( std::atan2( - _guide->normal_to_line[Geom::X], _guide->normal_to_line[Geom::Y] ) );
234     }
236     {
237         Inkscape::XML::Node *repr = SP_OBJECT_REPR (_guide);
238         const gchar *guide_id = repr->attribute("id");
239         gchar *label = g_strdup_printf(_("Guideline ID: %s"), guide_id);
240         _label_name.set_label(label);
241         g_free(label);
242     }
243     {
244         gchar *guide_description = sp_guide_description(_guide, false);
245         gchar *label = g_strdup_printf(_("Current: %s"), guide_description);
246         g_free(guide_description);
247         _label_descr.set_markup(label);
248         g_free(label);
249     }
251     _modeChanged(); // sets values of spinboxes.
253     if ( _oldangle == 90. || _oldangle == 270. || _oldangle == -90. || _oldangle == -270.) {
254         _spin_button_x.grab_focus();
255         _spin_button_x.select_region(0, 20);
256     } else if ( _oldangle == 0. || _oldangle == 180. || _oldangle == -180.) {
257         _spin_button_y.grab_focus();
258         _spin_button_y.select_region(0, 20);
259     } else {
260         _spin_angle.grab_focus();
261         _spin_angle.select_region(0, 20);
262     }
264     set_position(Gtk::WIN_POS_MOUSE);
266     show_all_children();
267     set_modal(true);
268     _desktop->setWindowTransient (gobj());
269     property_destroy_with_parent() = true;
276 /*
277   Local Variables:
278   mode:c++
279   c-file-style:"stroustrup"
280   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
281   indent-tabs-mode:nil
282   fill-column:99
283   End:
284 */
285 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :