b90895df8ec1802317968ca2fb006645b6eb8622
1 /**
2 * \brief Groups an HScale and a SpinButton together using the same Adjustment
3 *
4 * Author:
5 * Nicholas Bishop <nicholasbishop@gmail.com>
6 * Felipe C. da S. Sanches <felipe.sanches@gmail.com>
7 *
8 * Copyright (C) 2007 Author
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
13 #include "glib/gstrfuncs.h"
14 #include "glibmm/i18n.h"
16 #include "spin-slider.h"
18 namespace Inkscape {
19 namespace UI {
20 namespace Widget {
22 SpinSlider::SpinSlider(double value, double lower, double upper, double step_inc,
23 double climb_rate, int digits, const SPAttributeEnum a, char* tip_text)
24 : AttrWidget(a, value), _adjustment(value, lower, upper, step_inc),
25 _scale(_adjustment), _spin(_adjustment, climb_rate, digits)
26 {
27 signal_value_changed().connect(signal_attr_changed().make_slot());
29 pack_start(_scale);
30 pack_start(_spin, false, false);
31 if (tip_text){
32 _tt.set_tip(_scale, tip_text);
33 _tt.set_tip(_spin, tip_text);
34 }
36 _scale.set_draw_value(false);
38 show_all_children();
39 }
41 Glib::ustring SpinSlider::get_as_attribute() const
42 {
43 const double val = _adjustment.get_value();
45 if(_spin.get_digits() == 0)
46 return Glib::Ascii::dtostr((int)val);
47 else
48 return Glib::Ascii::dtostr(val);
49 }
51 void SpinSlider::set_from_attribute(SPObject* o)
52 {
53 const gchar* val = attribute_value(o);
54 if(val)
55 _adjustment.set_value(Glib::Ascii::strtod(val));
56 else
57 _adjustment.set_value(get_default()->as_double());
58 }
60 Glib::SignalProxy0<void> SpinSlider::signal_value_changed()
61 {
62 return _adjustment.signal_value_changed();
63 }
65 double SpinSlider::get_value() const
66 {
67 return _adjustment.get_value();
68 }
70 void SpinSlider::set_value(const double val)
71 {
72 _adjustment.set_value(val);
73 }
75 const Gtk::Adjustment& SpinSlider::get_adjustment() const
76 {
77 return _adjustment;
78 }
79 Gtk::Adjustment& SpinSlider::get_adjustment()
80 {
81 return _adjustment;
82 }
84 const Gtk::HScale& SpinSlider::get_scale() const
85 {
86 return _scale;
87 }
88 Gtk::HScale& SpinSlider::get_scale()
89 {
90 return _scale;
91 }
93 const Gtk::SpinButton& SpinSlider::get_spin_button() const
94 {
95 return _spin;
96 }
97 Gtk::SpinButton& SpinSlider::get_spin_button()
98 {
99 return _spin;
100 }
102 void SpinSlider::set_update_policy(const Gtk::UpdateType u)
103 {
104 _scale.set_update_policy(u);
105 }
107 void SpinSlider::remove_scale()
108 {
109 remove(_scale);
110 }
112 DualSpinSlider::DualSpinSlider(double value, double lower, double upper, double step_inc,
113 double climb_rate, int digits, const SPAttributeEnum a, char* tip_text1, char* tip_text2)
114 : AttrWidget(a),
115 _s1(value, lower, upper, step_inc, climb_rate, digits, SP_ATTR_INVALID, tip_text1),
116 _s2(value, lower, upper, step_inc, climb_rate, digits, SP_ATTR_INVALID, tip_text2),
117 //TRANSLATORS: Only translate the word "Link" - means to _link_ two sliders together
118 _link(Q_("sliders|Link"))
119 {
120 signal_value_changed().connect(signal_attr_changed().make_slot());
122 _s1.get_adjustment().signal_value_changed().connect(_signal_value_changed.make_slot());
123 _s2.get_adjustment().signal_value_changed().connect(_signal_value_changed.make_slot());
124 _s1.get_adjustment().signal_value_changed().connect(sigc::mem_fun(*this, &DualSpinSlider::update_linked));
125 _link.signal_toggled().connect(sigc::mem_fun(*this, &DualSpinSlider::link_toggled));
127 Gtk::VBox* vb = Gtk::manage(new Gtk::VBox);
128 vb->add(_s1);
129 vb->add(_s2);
130 pack_start(*vb);
131 pack_start(_link, false, false);
132 _link.set_active(true);
134 show_all();
135 }
137 Glib::ustring DualSpinSlider::get_as_attribute() const
138 {
139 if(_link.get_active())
140 return _s1.get_as_attribute();
141 else
142 return _s1.get_as_attribute() + " " + _s2.get_as_attribute();
143 }
145 void DualSpinSlider::set_from_attribute(SPObject* o)
146 {
147 const gchar* val = attribute_value(o);
148 if(val) {
149 // Split val into parts
150 gchar** toks = g_strsplit(val, " ", 2);
152 if(toks) {
153 double v1, v2;
154 if(toks[0])
155 v1 = v2 = Glib::Ascii::strtod(toks[0]);
156 if(toks[1])
157 v2 = Glib::Ascii::strtod(toks[1]);
159 _link.set_active(toks[1] == 0);
161 _s1.get_adjustment().set_value(v1);
162 _s2.get_adjustment().set_value(v2);
164 g_strfreev(toks);
165 }
166 }
167 }
169 sigc::signal<void>& DualSpinSlider::signal_value_changed()
170 {
171 return _signal_value_changed;
172 }
174 const SpinSlider& DualSpinSlider::get_spinslider1() const
175 {
176 return _s1;
177 }
179 SpinSlider& DualSpinSlider::get_spinslider1()
180 {
181 return _s1;
182 }
184 const SpinSlider& DualSpinSlider::get_spinslider2() const
185 {
186 return _s2;
187 }
189 SpinSlider& DualSpinSlider::get_spinslider2()
190 {
191 return _s2;
192 }
194 void DualSpinSlider::set_update_policy(const Gtk::UpdateType u)
195 {
196 _s1.set_update_policy(u);
197 _s2.set_update_policy(u);
198 }
200 void DualSpinSlider::remove_scale()
201 {
202 _s1.remove_scale();
203 _s2.remove_scale();
204 }
206 void DualSpinSlider::link_toggled()
207 {
208 _s2.set_sensitive(!_link.get_active());
209 update_linked();
210 }
212 void DualSpinSlider::update_linked()
213 {
214 if(_link.get_active())
215 _s2.set_value(_s1.get_value());
216 }
218 } // namespace Widget
219 } // namespace UI
220 } // namespace Inkscape
222 /*
223 Local Variables:
224 mode:c++
225 c-file-style:"stroustrup"
226 c-file-offsets:((innamespace . 0)(inline-open . 0))
227 indent-tabs-mode:nil
228 fill-column:99
229 End:
230 */
231 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :