Code

918742e751f4e2d953e103cce70d28db93576c56
[inkscape.git] / src / ui / widget / registered-widget.cpp
1 /** \file
2  *
3  *
4  * Authors:
5  *   bulia byak <buliabyak@users.sf.net>
6  *   Bryce W. Harrington <bryce@bryceharrington.org>
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Jon Phillips <jon@rejon.org>
9  *   Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
10  *
11  * Copyright (C) 2000 - 2005 Authors
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
21 #include "ui/widget/color-picker.h"
22 #include "ui/widget/registry.h"
23 #include "ui/widget/scalar-unit.h"
25 #include "helper/units.h"
26 #include "xml/repr.h"
27 #include "svg/svg.h"
28 #include "svg/stringstream.h"
30 #include "inkscape.h"
31 #include "document.h"
32 #include "desktop-handles.h"
33 #include "sp-namedview.h"
35 #include "registered-widget.h"
37 namespace Inkscape {
38 namespace UI {
39 namespace Widget {
41 //===================================================
43 //---------------------------------------------------
47 //====================================================
49 RegisteredCheckButton::RegisteredCheckButton()
50 : _button(0)
51 {
52 }
54 RegisteredCheckButton::~RegisteredCheckButton()
55 {
56     _toggled_connection.disconnect();
57     if (_button) delete _button;
58 }
60 void
61 RegisteredCheckButton::init (const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, Registry& wr, bool right)
62 {
63     _button = new Gtk::CheckButton;
64     _tt.set_tip (*_button, tip);
65     _button->add (*manage (new Gtk::Label (label)));
66     _button->set_alignment (right? 1.0 : 0.0, 0.5);
67     _key = key;
68     _wr = &wr;
69     _toggled_connection = _button->signal_toggled().connect (sigc::mem_fun (*this, &RegisteredCheckButton::on_toggled));
70 }
72 void
73 RegisteredCheckButton::setActive (bool b)
74 {
75     _button->set_active (b);
76 }
78 void
79 RegisteredCheckButton::on_toggled()
80 {
81     if (_wr->isUpdating())
82         return;
84     SPDesktop *dt = SP_ACTIVE_DESKTOP;
85     if (!dt) {
86         return;
87     }
89     SPDocument *doc = SP_DT_DOCUMENT(dt);
91     Inkscape::XML::Node *repr = SP_OBJECT_REPR (SP_DT_NAMEDVIEW(dt));
92     _wr->setUpdating (true);
94     gboolean saved = sp_document_get_undo_sensitive (doc);
95     sp_document_set_undo_sensitive (doc, FALSE);
96     sp_repr_set_boolean(repr, _key.c_str(), _button->get_active());
97     doc->rroot->setAttribute("sodipodi:modified", "true");
98     sp_document_set_undo_sensitive (doc, saved);
99     sp_document_done (doc);
100     
101     _wr->setUpdating (false);
104 RegisteredUnitMenu::RegisteredUnitMenu()
105 : _label(0), _sel(0)
109 RegisteredUnitMenu::~RegisteredUnitMenu()
111     _changed_connection.disconnect();
112     if (_label) delete _label;
113     if (_sel) delete _sel;
116 void
117 RegisteredUnitMenu::init (const Glib::ustring& label, const Glib::ustring& key, Registry& wr)
119     _label = new Gtk::Label (label, 1.0, 0.5);
120     _sel = new UnitMenu ();
121     _sel->setUnitType (UNIT_TYPE_LINEAR);
122     _wr = &wr;
123     _key = key;
124     _changed_connection = _sel->signal_changed().connect (sigc::mem_fun (*this, &RegisteredUnitMenu::on_changed));
127 void 
128 RegisteredUnitMenu::setUnit (const SPUnit* unit)
130     _sel->setUnit (sp_unit_get_abbreviation (unit));
133 void
134 RegisteredUnitMenu::on_changed()
136     if (_wr->isUpdating())
137         return;
139     SPDesktop *dt = SP_ACTIVE_DESKTOP;
140     if (!dt) 
141         return;
143     Inkscape::SVGOStringStream os;
144     os << _sel->getUnitAbbr();
146     _wr->setUpdating (true);
148     SPDocument *doc = SP_DT_DOCUMENT(dt);
149     gboolean saved = sp_document_get_undo_sensitive (doc);
150     sp_document_set_undo_sensitive (doc, FALSE);
151     Inkscape::XML::Node *repr = SP_OBJECT_REPR (SP_DT_NAMEDVIEW(dt));
152     repr->setAttribute(_key.c_str(), os.str().c_str());
153     doc->rroot->setAttribute("sodipodi:modified", "true");
154     sp_document_set_undo_sensitive (doc, saved);
155     sp_document_done (doc);
156     
157     _wr->setUpdating (false);
161 RegisteredScalarUnit::RegisteredScalarUnit()
162 : _widget(0), _um(0)
166 RegisteredScalarUnit::~RegisteredScalarUnit()
168     if (_widget) delete _widget;
169     _value_changed_connection.disconnect();
172 void
173 RegisteredScalarUnit::init (const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, const RegisteredUnitMenu &rum, Registry& wr)
175     _widget = new ScalarUnit (label, tip, UNIT_TYPE_LINEAR, "", "", rum._sel);
176     _widget->initScalar (-1e6, 1e6);
177     _widget->setUnit (rum._sel->getUnitAbbr());
178     _widget->setDigits (2);
179     _key = key;
180     _um = rum._sel;
181     _value_changed_connection = _widget->signal_value_changed().connect (sigc::mem_fun (*this, &RegisteredScalarUnit::on_value_changed));
182     _wr = &wr;
185 ScalarUnit*
186 RegisteredScalarUnit::getSU()
188     return _widget;
191 void 
192 RegisteredScalarUnit::setValue (double val)
194     _widget->setValue (val);
195     on_value_changed();
198 void
199 RegisteredScalarUnit::on_value_changed()
201     if (_wr->isUpdating())
202         return;
204     SPDesktop *dt = SP_ACTIVE_DESKTOP;
205     if (!dt) 
206         return;
208     Inkscape::SVGOStringStream os;
209     os << _widget->getValue("");
210     if (_um)
211         os << _um->getUnitAbbr();
213     _wr->setUpdating (true);
215     SPDocument *doc = SP_DT_DOCUMENT(dt);
216     gboolean saved = sp_document_get_undo_sensitive (doc);
217     sp_document_set_undo_sensitive (doc, FALSE);
218     Inkscape::XML::Node *repr = SP_OBJECT_REPR (SP_DT_NAMEDVIEW(dt));
219     repr->setAttribute(_key.c_str(), os.str().c_str());
220     doc->rroot->setAttribute("sodipodi:modified", "true");
221     sp_document_set_undo_sensitive (doc, saved);
222     sp_document_done (doc);
223     
224     _wr->setUpdating (false);
227 RegisteredColorPicker::RegisteredColorPicker()
228 : _label(0), _cp(0)
232 RegisteredColorPicker::~RegisteredColorPicker()
234     _changed_connection.disconnect();
235     if (_cp) delete _cp;
236     if (_label) delete _label;
239 void
240 RegisteredColorPicker::init (const Glib::ustring& label, const Glib::ustring& title, const Glib::ustring& tip, const Glib::ustring& ckey, const Glib::ustring& akey, Registry& wr)
242     _label = new Gtk::Label (label, 1.0, 0.5);
243     _cp = new ColorPicker (title,tip,0,true);
244     _ckey = ckey;
245     _akey = akey;
246     _wr = &wr;
247     _changed_connection = _cp->connectChanged (sigc::mem_fun (*this, &RegisteredColorPicker::on_changed));
250 void 
251 RegisteredColorPicker::setRgba32 (guint32 rgba)
253     _cp->setRgba32 (rgba);
256 void
257 RegisteredColorPicker::closeWindow()
259     _cp->closeWindow();
262 void
263 RegisteredColorPicker::on_changed (guint32 rgba)
265     if (_wr->isUpdating() || !SP_ACTIVE_DESKTOP)
266         return;
268     _wr->setUpdating (true);
269     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP));
270     gchar c[32];
271     sp_svg_write_color(c, 32, rgba);
272     repr->setAttribute(_ckey.c_str(), c);
273     sp_repr_set_css_double(repr, _akey.c_str(), (rgba & 0xff) / 255.0);
274     _wr->setUpdating (false);
277 RegisteredSuffixedInteger::RegisteredSuffixedInteger()
278 : _label(0), _sb(0),
279   _adj(0.0,0.0,100.0,1.0,1.0,1.0), 
280   _suffix(0)
284 RegisteredSuffixedInteger::~RegisteredSuffixedInteger()
286     _changed_connection.disconnect();
287     if (_label) delete _label;
288     if (_suffix) delete _suffix;
289     if (_sb) delete _sb;
292 void
293 RegisteredSuffixedInteger::init (const Glib::ustring& label, const Glib::ustring& suffix, const Glib::ustring& key, Registry& wr)
295     _key = key;
296     _label = new Gtk::Label (label);
297     _label->set_alignment (1.0, 0.5);
298     _sb = new Gtk::SpinButton (_adj, 1.0, 0);
299     _suffix = new Gtk::Label (suffix);
300     _hbox.pack_start (*_sb, true, true, 0);
301     _hbox.pack_start (*_suffix, false, false, 0);
303     _changed_connection = _adj.signal_value_changed().connect (sigc::mem_fun(*this, &RegisteredSuffixedInteger::on_value_changed));
304     _wr = &wr;
307 void 
308 RegisteredSuffixedInteger::setValue (int i)
310     _adj.set_value (i);
313 void
314 RegisteredSuffixedInteger::on_value_changed()
316     if (_wr->isUpdating() || !SP_ACTIVE_DESKTOP)
317         return;
319     _wr->setUpdating (true);
320     
321     SPDesktop* dt = SP_ACTIVE_DESKTOP;
322     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(dt));
323     Inkscape::SVGOStringStream os;
324     int value = int(_adj.get_value());
325     os << value;
327     repr->setAttribute(_key.c_str(), os.str().c_str());
328     sp_document_done(SP_DT_DOCUMENT(dt));
329     
330     _wr->setUpdating (false);
333 RegisteredRadioButtonPair::RegisteredRadioButtonPair()
334 : _hbox(0)
338 RegisteredRadioButtonPair::~RegisteredRadioButtonPair()
340     _changed_connection.disconnect();
343 void
344 RegisteredRadioButtonPair::init (const Glib::ustring& label, 
345 const Glib::ustring& label1, const Glib::ustring& label2, 
346 const Glib::ustring& tip1, const Glib::ustring& tip2, 
347 const Glib::ustring& key, Registry& wr)
349     _hbox = new Gtk::HBox;
350     _hbox->add (*manage (new Gtk::Label (label)));
351     _rb1 = manage (new Gtk::RadioButton (label1));
352     _hbox->add (*_rb1);
353     Gtk::RadioButtonGroup group = _rb1->get_group();
354     _rb2 = manage (new Gtk::RadioButton (group, label2, false));
355     _hbox->add (*_rb2);
356     _rb2->set_active();
357     _tt.set_tip (*_rb1, tip1);
358     _tt.set_tip (*_rb2, tip2);
359     _key = key;
360     _wr = &wr;
361     _changed_connection = _rb1->signal_toggled().connect (sigc::mem_fun (*this, &RegisteredRadioButtonPair::on_value_changed));
364 void 
365 RegisteredRadioButtonPair::setValue (bool second)
367     if (second) _rb2->set_active();
368     else        _rb1->set_active();
371 void
372 RegisteredRadioButtonPair::on_value_changed()
374     if (_wr->isUpdating())
375         return;
377     SPDesktop *dt = SP_ACTIVE_DESKTOP;
378     if (!dt) 
379         return;
381     _wr->setUpdating (true);
382     
383     bool second = _rb2->get_active();
384     SPDocument *doc = SP_DT_DOCUMENT(dt);
385     gboolean saved = sp_document_get_undo_sensitive (doc);
386     sp_document_set_undo_sensitive (doc, FALSE);
387     Inkscape::XML::Node *repr = SP_OBJECT_REPR (SP_DT_NAMEDVIEW(dt));
388     repr->setAttribute(_key.c_str(), second ? "true" : "false");
389     doc->rroot->setAttribute("sodipodi:modified", "true");
390     sp_document_set_undo_sensitive (doc, saved);
391     sp_document_done (doc);
392     
393     _wr->setUpdating (false);
396 } // namespace Dialog
397 } // namespace UI
398 } // namespace Inkscape
400 /*
401   Local Variables:
402   mode:c++
403   c-file-style:"stroustrup"
404   c-file-offsets:((innamespace . 0)(inline-open . 0))
405   indent-tabs-mode:nil
406   fill-column:99
407   End:
408 */
409 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :