1 /**
2 * \brief Inkscape Preferences dialog
3 *
4 * Authors:
5 * Marco Scholten
6 * Bruno Dilly <bruno.dilly@gmail.com>
7 *
8 * Copyright (C) 2004, 2006, 2007 Authors
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <gtkmm/frame.h>
18 #include <gtkmm/alignment.h>
19 #include <gtkmm/box.h>
21 #include "prefs-utils.h"
22 #include "ui/widget/preferences-widget.h"
23 #include "verbs.h"
24 #include "selcue.h"
25 #include <iostream>
26 #include "enums.h"
27 #include "inkscape.h"
28 #include "desktop-handles.h"
29 #include "message-stack.h"
30 #include "style.h"
31 #include "selection.h"
32 #include "selection-chemistry.h"
33 #include "xml/repr.h"
35 using namespace Inkscape::UI::Widget;
37 namespace Inkscape {
38 namespace UI {
39 namespace Widget {
41 DialogPage::DialogPage()
42 {
43 this->set_border_width(12);
44 this->set_col_spacings(12);
45 this->set_row_spacings(6);
46 }
48 void DialogPage::add_line(bool indent, const Glib::ustring label, Gtk::Widget& widget, const Glib::ustring suffix, const Glib::ustring& tip, bool expand_widget)
49 {
50 int start_col;
51 int row = this->property_n_rows();
52 Gtk::Widget* w;
53 if (expand_widget)
54 {
55 w = &widget;
56 }
57 else
58 {
59 Gtk::HBox* hb = Gtk::manage(new Gtk::HBox());
60 hb->set_spacing(12);
61 hb->pack_start(widget,false,false);
62 w = (Gtk::Widget*) hb;
63 }
64 if (label != "")
65 {
66 Gtk::Label* label_widget;
67 label_widget = Gtk::manage(new Gtk::Label(label , Gtk::ALIGN_LEFT , Gtk::ALIGN_CENTER, true));
68 label_widget->set_mnemonic_widget(widget);
69 if (indent)
70 {
71 Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment());
72 alignment->set_padding(0, 0, 12, 0);
73 alignment->add(*label_widget);
74 this->attach(*alignment , 0, 1, row, row + 1, Gtk::FILL, Gtk::AttachOptions(), 0, 0);
75 }
76 else
77 this->attach(*label_widget , 0, 1, row, row + 1, Gtk::FILL, Gtk::AttachOptions(), 0, 0);
78 start_col = 1;
79 }
80 else
81 start_col = 0;
83 if (start_col == 0 && indent) //indent this widget
84 {
85 Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment());
86 alignment->set_padding(0, 0, 12, 0);
87 alignment->add(*w);
88 this->attach(*alignment, start_col, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::AttachOptions(), 0, 0);
89 }
90 else
91 {
92 this->attach(*w, start_col, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::AttachOptions(), 0, 0);
93 }
95 if (suffix != "")
96 {
97 Gtk::Label* suffix_widget = Gtk::manage(new Gtk::Label(suffix , Gtk::ALIGN_LEFT , Gtk::ALIGN_CENTER, true));
98 if (expand_widget)
99 this->attach(*suffix_widget, 2, 3, row, row + 1, Gtk::FILL, Gtk::AttachOptions(), 0, 0);
100 else
101 ((Gtk::HBox*)w)->pack_start(*suffix_widget,false,false);
102 }
104 if (tip != "")
105 {
106 _tooltips.set_tip (widget, tip);
107 }
109 }
111 void DialogPage::add_group_header(Glib::ustring name)
112 {
113 int row = this->property_n_rows();
114 if (name != "")
115 {
116 Gtk::Label* label_widget = Gtk::manage(new Gtk::Label(Glib::ustring(/*"<span size='large'>*/"<b>") + name +
117 Glib::ustring("</b>"/*</span>"*/) , Gtk::ALIGN_LEFT , Gtk::ALIGN_CENTER, true));
118 label_widget->set_use_markup(true);
119 this->attach(*label_widget , 0, 4, row, row + 1, Gtk::FILL, Gtk::AttachOptions(), 0, 0);
120 if (row != 1)
121 this->set_row_spacing(row - 1, 18);
122 }
123 }
125 void DialogPage::set_tip(Gtk::Widget& widget, const Glib::ustring& tip)
126 {
127 _tooltips.set_tip (widget, tip);
128 }
130 void PrefCheckButton::init(const Glib::ustring& label, const std::string& prefs_path, const std::string& attr,
131 bool default_value)
132 {
133 _prefs_path = prefs_path;
134 _attr = attr;
135 this->set_label(label);
136 this->set_active( prefs_get_int_attribute (_prefs_path.c_str(), _attr.c_str(), (int)default_value) );
137 }
139 void PrefCheckButton::on_toggled()
140 {
141 if (this->is_visible()) //only take action if the user toggled it
142 {
143 prefs_set_int_attribute (_prefs_path.c_str(), _attr.c_str(), (int) this->get_active());
144 }
145 }
147 void PrefRadioButton::init(const Glib::ustring& label, const std::string& prefs_path, const std::string& attr,
148 const std::string& string_value, bool default_value, PrefRadioButton* group_member)
149 {
150 (void)default_value;
151 _value_type = VAL_STRING;
152 _prefs_path = prefs_path;
153 _attr = attr;
154 _string_value = string_value;
155 this->set_label(label);
156 if (group_member)
157 {
158 Gtk::RadioButtonGroup rbg = group_member->get_group();
159 this->set_group(rbg);
160 }
161 const gchar* val = prefs_get_string_attribute( _prefs_path.c_str(), _attr.c_str() );
162 if ( val )
163 this->set_active( std::string( val ) == _string_value);
164 else
165 this->set_active( false );
166 }
168 void PrefRadioButton::init(const Glib::ustring& label, const std::string& prefs_path, const std::string& attr,
169 int int_value, bool default_value, PrefRadioButton* group_member)
170 {
171 _value_type = VAL_INT;
172 _prefs_path = prefs_path;
173 _attr = attr;
174 _int_value = int_value;
175 this->set_label(label);
176 if (group_member)
177 {
178 Gtk::RadioButtonGroup rbg = group_member->get_group();
179 this->set_group(rbg);
180 }
181 if (default_value)
182 this->set_active( prefs_get_int_attribute( _prefs_path.c_str(), _attr.c_str(), int_value ) == _int_value);
183 else
184 this->set_active( prefs_get_int_attribute( _prefs_path.c_str(), _attr.c_str(), int_value + 1 )== _int_value);
185 }
187 void PrefRadioButton::on_toggled()
188 {
189 this->changed_signal.emit(this->get_active());
190 if (this->is_visible() && this->get_active() ) //only take action if toggled by user (to active)
191 {
192 if ( _value_type == VAL_STRING )
193 prefs_set_string_attribute ( _prefs_path.c_str(), _attr.c_str(), _string_value.c_str());
194 else if ( _value_type == VAL_INT )
195 prefs_set_int_attribute ( _prefs_path.c_str(), _attr.c_str(), _int_value);
196 }
197 }
199 void PrefSpinButton::init(const std::string& prefs_path, const std::string& attr,
200 double lower, double upper, double step_increment, double page_increment,
201 double default_value, bool is_int, bool is_percent)
202 {
203 _prefs_path = prefs_path;
204 _attr = attr;
205 _is_int = is_int;
206 _is_percent = is_percent;
208 double value;
209 if (is_int)
210 if (is_percent)
211 value = 100 * prefs_get_double_attribute_limited (prefs_path.c_str(), attr.c_str(), default_value, lower/100.0, upper/100.0);
212 else
213 value = (double) prefs_get_int_attribute_limited (prefs_path.c_str(), attr.c_str(), (int) default_value, (int) lower, (int) upper);
214 else
215 value = prefs_get_double_attribute_limited (prefs_path.c_str(), attr.c_str(), default_value, lower, upper);
217 this->set_range (lower, upper);
218 this->set_increments (step_increment, page_increment);
219 this->set_numeric();
220 this->set_value (value);
221 this->set_width_chars(6);
222 if (is_int)
223 this->set_digits(0);
224 else if (step_increment < 0.1)
225 this->set_digits(4);
226 else
227 this->set_digits(2);
229 }
231 void PrefSpinButton::on_value_changed()
232 {
233 if (this->is_visible()) //only take action if user changed value
234 {
235 if (_is_int)
236 if (_is_percent)
237 prefs_set_double_attribute(_prefs_path.c_str(), _attr.c_str(), this->get_value()/100.0);
238 else
239 prefs_set_int_attribute(_prefs_path.c_str(), _attr.c_str(), (int) this->get_value());
240 else
241 prefs_set_double_attribute (_prefs_path.c_str(), _attr.c_str(), this->get_value());
242 }
243 }
245 void PrefCombo::init(const std::string& prefs_path, const std::string& attr,
246 Glib::ustring labels[], int values[], int num_items, int default_value)
247 {
248 _prefs_path = prefs_path;
249 _attr = attr;
250 int row = 0;
251 int value = prefs_get_int_attribute (_prefs_path.c_str(), _attr.c_str(), default_value);
253 for (int i = 0 ; i < num_items; ++i)
254 {
255 this->append_text(labels[i]);
256 _values.push_back(values[i]);
257 if (value == values[i])
258 row = i;
259 }
260 this->set_active(row);
261 }
263 void PrefCombo::on_changed()
264 {
265 if (this->is_visible()) //only take action if user changed value
266 {
267 prefs_set_int_attribute (_prefs_path.c_str(), _attr.c_str(), _values[this->get_active_row_number()]);
268 }
269 }
271 void PrefEntryButtonHBox::init(const std::string& prefs_path, const std::string& attr,
272 bool visibility, gchar* default_string)
273 {
274 _prefs_path = prefs_path;
275 _attr = attr;
276 _default_string = default_string;
277 relatedEntry = new Gtk::Entry();
278 relatedButton = new Gtk::Button(_("Reset"));
279 relatedEntry->set_invisible_char('*');
280 relatedEntry->set_visibility(visibility);
281 relatedEntry->set_text(prefs_get_string_attribute(_prefs_path.c_str(), _attr.c_str()));
282 this->pack_start(*relatedEntry);
283 this->pack_start(*relatedButton);
284 relatedButton->signal_clicked().connect(
285 sigc::mem_fun(*this, &PrefEntryButtonHBox::onRelatedButtonClickedCallback));
286 relatedEntry->signal_changed().connect(
287 sigc::mem_fun(*this, &PrefEntryButtonHBox::onRelatedEntryChangedCallback));
288 }
290 void PrefEntryButtonHBox::onRelatedEntryChangedCallback()
291 {
292 if (this->is_visible()) //only take action if user changed value
293 {
294 prefs_set_string_attribute(_prefs_path.c_str(), _attr.c_str(),
295 relatedEntry->get_text().c_str());
296 }
297 }
299 void PrefEntryButtonHBox::onRelatedButtonClickedCallback()
300 {
301 if (this->is_visible()) //only take action if user changed value
302 {
303 prefs_set_string_attribute(_prefs_path.c_str(), _attr.c_str(),
304 _default_string);
305 relatedEntry->set_text(_default_string);
306 }
307 }
310 void PrefFileButton::init(const std::string& prefs_path, const std::string& attr)
311 {
312 _prefs_path = prefs_path;
313 _attr = attr;
314 select_filename(Glib::filename_from_utf8(prefs_get_string_attribute(_prefs_path.c_str(), _attr.c_str())));
316 signal_selection_changed().connect(sigc::mem_fun(*this, &PrefFileButton::onFileChanged));
317 }
319 void PrefFileButton::onFileChanged()
320 {
321 prefs_set_string_attribute(_prefs_path.c_str(), _attr.c_str(), Glib::filename_to_utf8(get_filename()).c_str());
322 }
324 void PrefEntry::init(const std::string& prefs_path, const std::string& attr,
325 bool visibility)
326 {
327 _prefs_path = prefs_path;
328 _attr = attr;
329 this->set_invisible_char('*');
330 this->set_visibility(visibility);
331 this->set_text(prefs_get_string_attribute(_prefs_path.c_str(), _attr.c_str()));
332 }
334 void PrefEntry::on_changed()
335 {
336 if (this->is_visible()) //only take action if user changed value
337 {
338 prefs_set_string_attribute(_prefs_path.c_str(), _attr.c_str(), this->get_text().c_str());
339 }
340 }
342 void PrefColorPicker::init(const Glib::ustring& label, const std::string& prefs_path, const std::string& attr,
343 guint32 default_rgba)
344 {
345 _prefs_path = prefs_path;
346 _attr = attr;
347 _title = label;
348 this->setRgba32( prefs_get_int_attribute (_prefs_path.c_str(), _attr.c_str(), (int)default_rgba) );
349 }
351 void PrefColorPicker::on_changed (guint32 rgba)
352 {
353 if (this->is_visible()) //only take action if the user toggled it
354 {
355 prefs_set_int_attribute (_prefs_path.c_str(), _attr.c_str(), (int) rgba);
356 }
357 }
359 void PrefUnit::init(const std::string& prefs_path, const std::string& attr)
360 {
361 _prefs_path = prefs_path;
362 _attr = attr;
363 setUnitType(UNIT_TYPE_LINEAR);
364 gchar const * prefval = prefs_get_string_attribute(_prefs_path.c_str(), _attr.c_str());
365 setUnit(prefval);
366 }
368 void PrefUnit::on_changed()
369 {
370 if (this->is_visible()) //only take action if user changed value
371 {
372 prefs_set_string_attribute(_prefs_path.c_str(), _attr.c_str(), getUnitAbbr().c_str());
373 }
374 }
376 } // namespace Widget
377 } // namespace UI
378 } // namespace Inkscape
380 /*
381 Local Variables:
382 mode:c++
383 c-file-style:"stroustrup"
384 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
385 indent-tabs-mode:nil
386 fill-column:99
387 End:
388 */
389 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :