1 /*
2 * Copyright (C) 2005-2007 Authors:
3 * Ted Gould <ted@gould.cx>
4 * Johan Engelen <johan@shouraizou.nl> *
5 * Released under GNU GPL, read the file 'COPYING' for more information
6 */
8 #ifdef HAVE_CONFIG_H
9 # include "config.h"
10 #endif
12 #include <gtkmm/adjustment.h>
13 #include <gtkmm/box.h>
14 #include <gtkmm/spinbutton.h>
16 #include "xml/node.h"
17 #include "../extension.h"
18 #include "bool.h"
19 #include "preferences.h"
21 namespace Inkscape {
22 namespace Extension {
24 /** \brief Use the superclass' allocator and set the \c _value */
25 ParamBool::ParamBool (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, bool gui_hidden, const gchar * gui_tip, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
26 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext), _value(false)
27 {
28 const char * defaultval = NULL;
29 if (sp_repr_children(xml) != NULL)
30 defaultval = sp_repr_children(xml)->content();
32 if (defaultval != NULL && (!strcmp(defaultval, "true") || !strcmp(defaultval, "true") || !strcmp(defaultval, "1"))) {
33 _value = true;
34 } else {
35 _value = false;
36 }
38 gchar * pref_name = this->pref_name();
39 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
40 _value = prefs->getBool(extension_pref_root + pref_name, _value);
41 g_free(pref_name);
43 return;
44 }
46 /** \brief A function to set the \c _value
47 \param in The value to set to
48 \param doc A document that should be used to set the value.
49 \param node The node where the value may be placed
51 This function sets the internal value, but it also sets the value
52 in the preferences structure. To put it in the right place, \c PREF_DIR
53 and \c pref_name() are used.
54 */
55 bool
56 ParamBool::set( bool in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/ )
57 {
58 _value = in;
60 gchar * prefname = this->pref_name();
61 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
62 prefs->setBool(extension_pref_root + prefname, _value);
63 g_free(prefname);
65 return _value;
66 }
68 /** \brief Returns \c _value */
69 bool
70 ParamBool::get (const SPDocument * doc, const Inkscape::XML::Node * node)
71 {
72 return _value;
73 }
75 /** \brief A check button which is Param aware. It works with the
76 parameter to change it's value as the check button changes
77 value. */
78 class ParamBoolCheckButton : public Gtk::CheckButton {
79 private:
80 /** \brief Param to change */
81 ParamBool * _pref;
82 SPDocument * _doc;
83 Inkscape::XML::Node * _node;
84 sigc::signal<void> * _changeSignal;
85 public:
86 /** \brief Initialize the check button
87 \param param Which parameter to adjust on changing the check button
89 This function sets the value of the checkbox to be that of the
90 parameter, and then sets up a callback to \c on_toggle.
91 */
92 ParamBoolCheckButton (ParamBool * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
93 Gtk::CheckButton(), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
94 this->set_active(_pref->get(NULL, NULL) /**\todo fix */);
95 this->signal_toggled().connect(sigc::mem_fun(this, &ParamBoolCheckButton::on_toggle));
96 return;
97 }
98 void on_toggle (void);
99 };
101 /**
102 \brief A function to respond to the check box changing
104 Adjusts the value of the preference to match that in the check box.
105 */
106 void
107 ParamBoolCheckButton::on_toggle (void)
108 {
109 _pref->set(this->get_active(), NULL /**\todo fix this */, NULL);
110 if (_changeSignal != NULL) {
111 _changeSignal->emit();
112 }
113 return;
114 }
116 /** \brief Return 'true' or 'false' */
117 void
118 ParamBool::string (std::string &string)
119 {
120 if (_value) {
121 string += "true";
122 } else {
123 string += "false";
124 }
126 return;
127 }
129 /**
130 \brief Creates a bool check button for a bool parameter
132 Builds a hbox with a label and a check button in it.
133 */
134 Gtk::Widget *
135 ParamBool::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
136 {
137 if (_gui_hidden) return NULL;
138 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
140 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
141 label->show();
142 hbox->pack_start(*label, true, true);
144 ParamBoolCheckButton * checkbox = new ParamBoolCheckButton(this, doc, node, changeSignal);
145 checkbox->show();
146 hbox->pack_start(*checkbox, false, false);
148 hbox->show();
150 return dynamic_cast<Gtk::Widget *>(hbox);
151 }
153 } /* namespace Extension */
154 } /* namespace Inkscape */
156 /*
157 Local Variables:
158 mode:c++
159 c-file-style:"stroustrup"
160 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
161 indent-tabs-mode:nil
162 fill-column:99
163 End:
164 */
165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :