Code

Following this thread: http://www.nabble.com/Extension-parameters-td9064285.html...
[inkscape.git] / src / extension / param / bool.cpp
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 "bool.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     _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  Returns \c _value */
67 bool 
68 ParamBool::get (const SPDocument * doc, const Inkscape::XML::Node * node)
69 {
70         return _value; 
71 }
73 /** \brief  A check button which is Param aware.  It works with the
74             parameter to change it's value as the check button changes
75             value. */
76 class ParamBoolCheckButton : public Gtk::CheckButton {
77 private:
78     /** \brief  Param to change */
79     ParamBool * _pref;
80     SPDocument * _doc;
81     Inkscape::XML::Node * _node;
82     sigc::signal<void> * _changeSignal;
83 public:
84     /** \brief  Initialize the check button
85         \param  param  Which parameter to adjust on changing the check button
87         This function sets the value of the checkbox to be that of the
88         parameter, and then sets up a callback to \c on_toggle.
89     */
90     ParamBoolCheckButton (ParamBool * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
91             Gtk::CheckButton(), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
92         this->set_active(_pref->get(NULL, NULL) /**\todo fix */);
93         this->signal_toggled().connect(sigc::mem_fun(this, &ParamBoolCheckButton::on_toggle));
94         return;
95     }
96     void on_toggle (void);
97 };
99 /**
100     \brief  A function to respond to the check box changing
102     Adjusts the value of the preference to match that in the check box.
103 */
104 void
105 ParamBoolCheckButton::on_toggle (void)
107     _pref->set(this->get_active(), NULL /**\todo fix this */, NULL);
108     if (_changeSignal != NULL) {
109         _changeSignal->emit();
110     }
111     return;
114 /** \brief  Return 'true' or 'false' */
115 void
116 ParamBool::string (std::string &string)
118     if (_value) {
119         string += "true";
120     } else {
121         string += "false";
122     }
124     return;
127 /**
128     \brief  Creates a bool check button for a bool parameter
130     Builds a hbox with a label and a check button in it.
131 */
132 Gtk::Widget *
133 ParamBool::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
135         if (_gui_hidden) return NULL;
136     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
138     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
139     label->show();
140     hbox->pack_start(*label, true, true);
142     ParamBoolCheckButton * checkbox = new ParamBoolCheckButton(this, doc, node, changeSignal);
143     checkbox->show();
144     hbox->pack_start(*checkbox, false, false);
146     hbox->show();
148     return dynamic_cast<Gtk::Widget *>(hbox);
151 }  /* namespace Extension */
152 }  /* namespace Inkscape */