1 /** \file
2 * extension parameter for enumerations.
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/comboboxtext.h>
23 #include <gtkmm/tooltips.h>
24 #include <gtkmm/label.h>
25 #include <glibmm/i18n.h>
27 #include "xml/node.h"
28 #include "extension/extension.h"
29 #include "document-private.h"
30 #include "sp-object.h"
31 #include "enum.h"
32 #include "preferences.h"
34 namespace Inkscape {
35 namespace Extension {
37 /* For internal use only.
38 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. */
39 class enumentry {
40 public:
41 enumentry (Glib::ustring * val, Glib::ustring * text) {
42 value = val;
43 guitext = text;
44 }
45 ~enumentry() {
46 delete value;
47 delete guitext;
48 }
50 Glib::ustring * value;
51 Glib::ustring * guitext;
52 };
55 ParamComboBox::ParamComboBox (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) :
56 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext)
57 {
58 choices = NULL;
59 _value = NULL;
61 // Read XML tree to add enumeration items:
62 // printf("Extension Constructor: ");
63 if (xml != NULL) {
64 Inkscape::XML::Node *child_repr = sp_repr_children(xml);
65 while (child_repr != NULL) {
66 char const * chname = child_repr->name();
67 if (!strcmp(chname, INKSCAPE_EXTENSION_NS "item") || !strcmp(chname, INKSCAPE_EXTENSION_NS "_item")) {
68 Glib::ustring * newguitext = NULL;
69 Glib::ustring * newvalue = NULL;
70 const char * contents = sp_repr_children(child_repr)->content();
71 if (contents != NULL)
72 // don't translate when 'item' but do translate when '_item'
73 // NOTE: internal extensions use build_from_mem and don't need _item but
74 // still need to include if are to be localized
75 newguitext = new Glib::ustring( !strcmp(chname, INKSCAPE_EXTENSION_NS "_item") ? _(contents): contents );
76 else
77 continue;
79 const char * val = child_repr->attribute("value");
80 if (val != NULL)
81 newvalue = new Glib::ustring(val);
82 else
83 newvalue = new Glib::ustring(contents);
85 if ( (newguitext) && (newvalue) ) { // logical error if this is not true here
86 choices = g_slist_append( choices, new enumentry(newvalue, newguitext) );
87 }
88 }
89 child_repr = sp_repr_next(child_repr);
90 }
91 }
93 // Initialize _value with the default value from xml
94 // for simplicity : default to the contents of the first xml-child
95 const char * defaultval = NULL;
96 if (sp_repr_children(sp_repr_children(xml)) != NULL)
97 defaultval = sp_repr_children(xml)->attribute("value");
99 gchar * pref_name = this->pref_name();
100 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
101 Glib::ustring paramval = prefs->getString(extension_pref_root + pref_name);
102 g_free(pref_name);
104 if (!paramval.empty())
105 defaultval = paramval.data();
106 if (defaultval != NULL)
107 _value = g_strdup(defaultval);
109 return;
110 }
112 ParamComboBox::~ParamComboBox (void)
113 {
114 //destroy choice strings
115 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
116 delete (reinterpret_cast<enumentry *>(list->data));
117 }
118 g_slist_free(choices);
120 g_free(_value);
121 }
124 /** \brief A function to set the \c _value
125 \param in The value to set
126 \param doc A document that should be used to set the value.
127 \param node The node where the value may be placed
129 This function sets ONLY the internal value, but it also sets the value
130 in the preferences structure. To put it in the right place, \c PREF_DIR
131 and \c pref_name() are used.
133 To copy the data into _value the old memory must be free'd first.
134 It is important to note that \c g_free handles \c NULL just fine. Then
135 the passed in value is duplicated using \c g_strdup().
136 */
137 const gchar *
138 ParamComboBox::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
139 {
140 if (in == NULL) return NULL; /* Can't have NULL string */
142 Glib::ustring * settext = NULL;
143 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
144 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
145 if ( !entr->guitext->compare(in) ) {
146 settext = entr->value;
147 break; // break out of for loop
148 }
149 }
150 if (settext) {
151 if (_value != NULL) g_free(_value);
152 _value = g_strdup(settext->c_str());
153 gchar * prefname = this->pref_name();
154 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
155 prefs->setString(extension_pref_root + prefname, _value);
156 g_free(prefname);
157 }
159 return _value;
160 }
162 void
163 ParamComboBox::changed (void) {
165 }
168 /**
169 \brief A function to get the value of the parameter in string form
170 \return A string with the 'value' as command line argument
171 */
172 void
173 ParamComboBox::string (std::string &string)
174 {
175 string += _value;
176 return;
177 }
182 /** \brief A special category of Gtk::Entry to handle string parameteres */
183 class ParamComboBoxEntry : public Gtk::ComboBoxText {
184 private:
185 ParamComboBox * _pref;
186 SPDocument * _doc;
187 Inkscape::XML::Node * _node;
188 sigc::signal<void> * _changeSignal;
189 public:
190 /** \brief Build a string preference for the given parameter
191 \param pref Where to get the string from, and where to put it
192 when it changes.
193 */
194 ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
195 Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
196 this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
197 };
198 void changed (void);
199 };
201 /** \brief Respond to the text box changing
203 This function responds to the box changing by grabbing the value
204 from the text box and putting it in the parameter.
205 */
206 void
207 ParamComboBoxEntry::changed (void)
208 {
209 Glib::ustring data = this->get_active_text();
210 _pref->set(data.c_str(), _doc, _node);
211 if (_changeSignal != NULL) {
212 _changeSignal->emit();
213 }
214 }
216 /**
217 \brief Creates a combobox widget for an enumeration parameter
218 */
219 Gtk::Widget *
220 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
221 {
222 if (_gui_hidden) return NULL;
224 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
226 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
227 label->show();
228 hbox->pack_start(*label, false, false);
230 ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
231 // add choice strings:
232 Glib::ustring * settext = 0;
233 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
234 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
235 Glib::ustring * text = entr->guitext;
236 combo->append_text(*text);
237 if ( !entr->value->compare(_value) ) {
238 settext = entr->guitext;
239 }
240 }
241 if (settext) combo->set_active_text(*settext);
243 combo->show();
244 hbox->pack_start(*combo, true, true);
246 hbox->show();
248 return dynamic_cast<Gtk::Widget *>(hbox);
249 }
252 } /* namespace Extension */
253 } /* namespace Inkscape */