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 {}
46 Glib::ustring value;
47 Glib::ustring guitext;
48 };
51 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) :
52 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext)
53 {
54 choices = NULL;
55 _value = NULL;
57 // Read XML tree to add enumeration items:
58 // printf("Extension Constructor: ");
59 if (xml != NULL) {
60 for (Inkscape::XML::Node *node = xml->firstChild(); node; node = node->next()) {
61 char const * chname = node->name();
62 if (!strcmp(chname, INKSCAPE_EXTENSION_NS "item") || !strcmp(chname, INKSCAPE_EXTENSION_NS "_item")) {
63 Glib::ustring newguitext, newvalue;
64 const char * contents = NULL;
65 if (node->firstChild()) contents = node->firstChild()->content();
66 if (contents != NULL)
67 // don't translate when 'item' but do translate when '_item'
68 // NOTE: internal extensions use build_from_mem and don't need _item but
69 // still need to include if are to be localized
70 newguitext = !strcmp(chname, INKSCAPE_EXTENSION_NS "_item") ? _(contents) : contents;
71 else
72 continue;
74 const char * val = node->attribute("value");
75 if (val != NULL)
76 newvalue = val;
77 else
78 newvalue = contents;
80 if ( (!newguitext.empty()) && (!newvalue.empty()) ) { // logical error if this is not true here
81 choices = g_slist_append( choices, new enumentry(newvalue, newguitext) );
82 }
83 }
84 }
85 }
87 // Initialize _value with the default value from xml
88 // for simplicity : default to the contents of the first xml-child
89 const char * defaultval = NULL;
90 if (xml->firstChild() && xml->firstChild()->firstChild())
91 defaultval = xml->firstChild()->attribute("value");
93 gchar * pref_name = this->pref_name();
94 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
95 Glib::ustring paramval = prefs->getString(extension_pref_root + pref_name);
96 g_free(pref_name);
98 if (!paramval.empty())
99 defaultval = paramval.data();
100 if (defaultval != NULL)
101 _value = g_strdup(defaultval);
103 return;
104 }
106 ParamComboBox::~ParamComboBox (void)
107 {
108 //destroy choice strings
109 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
110 delete (reinterpret_cast<enumentry *>(list->data));
111 }
112 g_slist_free(choices);
114 g_free(_value);
115 }
118 /** \brief A function to set the \c _value
119 \param in The value to set
120 \param doc A document that should be used to set the value.
121 \param node The node where the value may be placed
123 This function sets ONLY the internal value, but it also sets the value
124 in the preferences structure. To put it in the right place, \c PREF_DIR
125 and \c pref_name() are used.
127 To copy the data into _value the old memory must be free'd first.
128 It is important to note that \c g_free handles \c NULL just fine. Then
129 the passed in value is duplicated using \c g_strdup().
130 */
131 const gchar *
132 ParamComboBox::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
133 {
134 if (in == NULL) return NULL; /* Can't have NULL string */
136 Glib::ustring settext;
137 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
138 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
139 if ( !entr->guitext.compare(in) ) {
140 settext = entr->value;
141 break; // break out of for loop
142 }
143 }
144 if (!settext.empty()) {
145 if (_value != NULL) g_free(_value);
146 _value = g_strdup(settext.data());
147 gchar * prefname = this->pref_name();
148 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
149 prefs->setString(extension_pref_root + prefname, _value);
150 g_free(prefname);
151 }
153 return _value;
154 }
156 void
157 ParamComboBox::changed (void) {
159 }
162 /**
163 \brief A function to get the value of the parameter in string form
164 \return A string with the 'value' as command line argument
165 */
166 void
167 ParamComboBox::string (std::string &string)
168 {
169 string += _value;
170 return;
171 }
176 /** \brief A special category of Gtk::Entry to handle string parameteres */
177 class ParamComboBoxEntry : public Gtk::ComboBoxText {
178 private:
179 ParamComboBox * _pref;
180 SPDocument * _doc;
181 Inkscape::XML::Node * _node;
182 sigc::signal<void> * _changeSignal;
183 public:
184 /** \brief Build a string preference for the given parameter
185 \param pref Where to get the string from, and where to put it
186 when it changes.
187 */
188 ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
189 Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
190 this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
191 };
192 void changed (void);
193 };
195 /** \brief Respond to the text box changing
197 This function responds to the box changing by grabbing the value
198 from the text box and putting it in the parameter.
199 */
200 void
201 ParamComboBoxEntry::changed (void)
202 {
203 Glib::ustring data = this->get_active_text();
204 _pref->set(data.c_str(), _doc, _node);
205 if (_changeSignal != NULL) {
206 _changeSignal->emit();
207 }
208 }
210 /**
211 \brief Creates a combobox widget for an enumeration parameter
212 */
213 Gtk::Widget *
214 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
215 {
216 if (_gui_hidden) return NULL;
218 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
220 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
221 label->show();
222 hbox->pack_start(*label, false, false);
224 ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
225 // add choice strings:
226 Glib::ustring settext;
227 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
228 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
229 Glib::ustring text = entr->guitext;
230 combo->append_text(text);
231 if ( _value && !entr->value.compare(_value) ) {
232 settext = entr->guitext;
233 }
234 }
235 if (!settext.empty()) combo->set_active_text(settext);
237 combo->show();
238 hbox->pack_start(*combo, true, true);
240 hbox->show();
242 return dynamic_cast<Gtk::Widget *>(hbox);
243 }
246 } /* namespace Extension */
247 } /* namespace Inkscape */