1 /** \file
2 * extension parameter for radiobuttons.
3 *
4 * It uses a Gtk:ComboBoxText widget in the extension UI.
5 */
7 /*
8 * Author:
9 * Johan Engelen <johan@shouraizou.nl>
10 *
11 * Copyright (C) 2006-2007 Johan Engelen
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <gtkmm/box.h>
22 #include <gtkmm/radiobutton.h>
23 #include <gtkmm/radiobuttongroup.h>
24 #include <gtkmm/tooltips.h>
25 #include <gtkmm/label.h>
27 #include <glibmm/i18n.h>
29 #include <xml/node.h>
31 #include "extension.h"
32 #include "prefs-utils.h"
33 #include "document-private.h"
34 #include "sp-object.h"
36 #include "paramradiobutton.h"
38 /** \brief The root directory in the preferences database for extension
39 related parameters. */
40 #define PREF_DIR "extensions"
42 namespace Inkscape {
43 namespace Extension {
45 /* For internal use only.
46 Note that value and guitext MUST be non-NULL. This is ensured by newing only at one location in the code where non-NULL checks are made. */
47 class optionentry {
48 public:
49 optionentry (Glib::ustring * val, Glib::ustring * text) {
50 value = val;
51 guitext = text;
52 }
53 ~optionentry() {
54 delete value;
55 delete guitext;
56 }
58 Glib::ustring * value;
59 Glib::ustring * guitext;
60 };
62 ParamRadioButton::ParamRadioButton (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
63 Parameter(name, guitext, desc, scope, ext)
64 {
65 choices = NULL;
66 _value = NULL;
68 // Read XML tree to add enumeration items:
69 // printf("Extension Constructor: ");
70 if (xml != NULL) {
71 Inkscape::XML::Node *child_repr = sp_repr_children(xml);
72 while (child_repr != NULL) {
73 char const * chname = child_repr->name();
74 if (!strcmp(chname, "option")) {
75 Glib::ustring * newguitext = NULL;
76 Glib::ustring * newvalue = NULL;
77 const char * contents = sp_repr_children(child_repr)->content();
78 if (contents != NULL)
79 newguitext = new Glib::ustring(contents);
80 const char * val = child_repr->attribute("value");
81 if (val != NULL)
82 newvalue = new Glib::ustring(val);
83 if ( (newguitext) && (newvalue) ) {
84 choices = g_slist_append( choices, new optionentry(newvalue, newguitext) );
85 }
86 }
87 child_repr = sp_repr_next(child_repr);
88 }
89 }
91 // Initialize _value with the default value from xml
92 // for simplicity : default to the contents of the first xml-child
93 const char * defaultval = NULL;
94 if (sp_repr_children(sp_repr_children(xml)) != NULL)
95 defaultval = sp_repr_children(xml)->attribute("value");
97 gchar * pref_name = this->pref_name();
98 const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
99 g_free(pref_name);
101 if (paramval != NULL)
102 defaultval = paramval;
103 if (defaultval != NULL)
104 _value = g_strdup(defaultval); // allocate space for _value
106 return;
107 }
109 ParamRadioButton::~ParamRadioButton (void)
110 {
111 //destroy choice strings
112 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
113 delete (reinterpret_cast<optionentry *>(list->data));
114 }
115 g_slist_free(choices);
117 g_free(_value);
118 }
121 /** \brief A function to set the \c _value
122 \param in The value to set
123 \param doc A document that should be used to set the value.
124 \param node The node where the value may be placed
126 This function sets ONLY the internal value, but it also sets the value
127 in the preferences structure. To put it in the right place, \c PREF_DIR
128 and \c pref_name() are used.
130 To copy the data into _value the old memory must be free'd first.
131 It is important to note that \c g_free handles \c NULL just fine. Then
132 the passed in value is duplicated using \c g_strdup().
133 */
134 const gchar *
135 ParamRadioButton::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * node)
136 {
137 if (in == NULL) return NULL; /* Can't have NULL string */
139 Glib::ustring * settext = NULL;
140 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
141 optionentry * entr = reinterpret_cast<optionentry *>(list->data);
142 if ( !entr->guitext->compare(in) ) {
143 settext = entr->value;
144 break; // break out of for loop
145 }
146 }
147 if (settext) {
148 if (_value != NULL) g_free(_value);
149 _value = g_strdup(settext->c_str());
150 gchar * prefname = this->pref_name();
151 prefs_set_string_attribute(PREF_DIR, prefname, _value);
152 g_free(prefname);
153 }
155 return _value;
156 }
159 /**
160 \brief A function to get the current value of the parameter in a string form
161 \return A string with the 'value' as command line argument
162 */
163 Glib::ustring *
164 ParamRadioButton::string (void)
165 {
166 Glib::ustring * param_string = new Glib::ustring("");
168 *param_string += "\"";
169 *param_string += _value;
170 *param_string += "\"";
172 return param_string;
173 }
175 /** \brief A special radiobutton class to use in ParamRadioButton */
176 class ParamRadioButtonWdg : public Gtk::RadioButton {
177 private:
178 ParamRadioButton * _pref;
179 SPDocument * _doc;
180 Inkscape::XML::Node * _node;
181 public:
182 /** \brief Build a string preference for the given parameter
183 \param pref Where to put the radiobutton's string when it is selected.
184 */
185 ParamRadioButtonWdg ( Gtk::RadioButtonGroup& group, const Glib::ustring& label,
186 ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :
187 Gtk::RadioButton(group, label), _pref(pref), _doc(doc), _node(node) {
188 add_changesignal();
189 };
190 ParamRadioButtonWdg ( const Glib::ustring& label,
191 ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :
192 Gtk::RadioButton(label), _pref(pref), _doc(doc), _node(node) {
193 add_changesignal();
194 };
195 void add_changesignal() {
196 this->signal_toggled().connect(sigc::mem_fun(this, &ParamRadioButtonWdg::changed));
197 };
198 void changed (void);
199 };
201 /** \brief Respond to the selected radiobutton changing
203 This function responds to the radiobutton selection changing by grabbing the value
204 from the text box and putting it in the parameter.
205 */
206 void
207 ParamRadioButtonWdg::changed (void)
208 {
209 if (this->get_active()) {
210 Glib::ustring data = this->get_label();
211 _pref->set(data.c_str(), _doc, _node);
212 }
213 }
217 /**
218 \brief Creates a combobox widget for an enumeration parameter
219 */
220 Gtk::Widget *
221 ParamRadioButton::get_widget (SPDocument * doc, Inkscape::XML::Node * node)
222 {
223 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
224 Gtk::VBox * vbox = Gtk::manage(new Gtk::VBox(false, 0));
226 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP));
227 label->show();
228 hbox->pack_start(*label, false, false);
230 // add choice strings as radiobuttons
231 // and select last selected option (_value)
232 bool first = true;
233 ParamRadioButtonWdg * radio;
234 Gtk::RadioButtonGroup group;
235 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
236 optionentry * entr = reinterpret_cast<optionentry *>(list->data);
237 Glib::ustring * text = entr->guitext;
238 if (first) {
239 radio = Gtk::manage(new ParamRadioButtonWdg(*text, this, doc, node));
240 group = radio->get_group();
241 first = false;
242 } else {
243 radio = Gtk::manage(new ParamRadioButtonWdg(group, *text, this, doc, node));
244 }
245 radio->show();
246 vbox->pack_start(*radio, true, true);
247 if (!entr->value->compare(_value)) {
248 radio->set_active();
249 }
250 }
252 vbox->show();
253 hbox->pack_end(*vbox, false, false);
254 hbox->show();
257 return dynamic_cast<Gtk::Widget *>(hbox);
258 }
261 } /* namespace Extension */
262 } /* namespace Inkscape */