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>
18 #include "extension.h"
19 #include "parambool.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, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
26 Parameter(name, guitext, desc, scope, 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 _value = (bool)prefs_get_int_attribute(PREF_DIR, pref_name, _value);
40 g_free(pref_name);
42 return;
43 }
45 /** \brief A function to set the \c _value
46 \param in The value to set to
47 \param doc A document that should be used to set the value.
48 \param node The node where the value may be placed
50 This function sets the internal value, but it also sets the value
51 in the preferences structure. To put it in the right place, \c PREF_DIR
52 and \c pref_name() are used.
53 */
54 bool
55 ParamBool::set( bool in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/ )
56 {
57 _value = in;
59 gchar * prefname = this->pref_name();
60 prefs_set_int_attribute(PREF_DIR, prefname, _value == true ? 1 : 0);
61 g_free(prefname);
63 return _value;
64 }
66 /** \brief A check button which is Param aware. It works with the
67 parameter to change it's value as the check button changes
68 value. */
69 class ParamBoolCheckButton : public Gtk::CheckButton {
70 private:
71 /** \brief Param to change */
72 ParamBool * _pref;
73 SPDocument * _doc;
74 Inkscape::XML::Node * _node;
75 sigc::signal<void> * _changeSignal;
76 public:
77 /** \brief Initialize the check button
78 \param param Which parameter to adjust on changing the check button
80 This function sets the value of the checkbox to be that of the
81 parameter, and then sets up a callback to \c on_toggle.
82 */
83 ParamBoolCheckButton (ParamBool * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
84 Gtk::CheckButton(), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
85 this->set_active(_pref->get(NULL, NULL) /**\todo fix */);
86 this->signal_toggled().connect(sigc::mem_fun(this, &ParamBoolCheckButton::on_toggle));
87 return;
88 }
89 void on_toggle (void);
90 };
92 /**
93 \brief A function to respond to the check box changing
95 Adjusts the value of the preference to match that in the check box.
96 */
97 void
98 ParamBoolCheckButton::on_toggle (void)
99 {
100 _pref->set(this->get_active(), NULL /**\todo fix this */, NULL);
101 if (_changeSignal != NULL) {
102 _changeSignal->emit();
103 }
104 return;
105 }
107 /** \brief Return 'true' or 'false' */
108 Glib::ustring *
109 ParamBool::string (void)
110 {
111 Glib::ustring * mystring;
113 if (_value)
114 mystring = new Glib::ustring("true");
115 else
116 mystring = new Glib::ustring("false");
118 return mystring;
119 }
121 /**
122 \brief Creates a bool check button for a bool parameter
124 Builds a hbox with a label and a check button in it.
125 */
126 Gtk::Widget *
127 ParamBool::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
128 {
129 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
131 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
132 label->show();
133 hbox->pack_start(*label, true, true);
135 ParamBoolCheckButton * checkbox = new ParamBoolCheckButton(this, doc, node, changeSignal);
136 checkbox->show();
137 hbox->pack_start(*checkbox, false, false);
139 hbox->show();
141 return dynamic_cast<Gtk::Widget *>(hbox);
142 }
144 } /* namespace Extension */
145 } /* namespace Inkscape */