89db25d0381d6d8e5e96b143e6e06b960cc6ca49
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>
26 #include <glibmm/i18n.h>
28 #include <xml/node.h>
30 #include <extension/extension.h>
31 #include <prefs-utils.h>
32 #include <document-private.h>
33 #include <sp-object.h>
35 #include "enum.h"
37 /** \brief The root directory in the preferences database for extension
38 related parameters. */
39 #define PREF_DIR "extensions"
41 namespace Inkscape {
42 namespace Extension {
44 /* For internal use only.
45 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. */
46 class enumentry {
47 public:
48 enumentry (Glib::ustring * val, Glib::ustring * text) {
49 value = val;
50 guitext = text;
51 }
52 ~enumentry() {
53 delete value;
54 delete guitext;
55 }
57 Glib::ustring * value;
58 Glib::ustring * guitext;
59 };
62 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) :
63 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, 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, "extension:item") || !strcmp(chname, "extension:_item")) {
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 // don't translate when 'item' but do translate when '_item'
80 // NOTE: internal extensions use build_from_mem and don't need _item but
81 // still need to include if are to be localized
82 newguitext = new Glib::ustring( !strcmp(chname, "extension:_item") ? _(contents) : contents );
83 else
84 continue;
86 const char * val = child_repr->attribute("value");
87 if (val != NULL)
88 newvalue = new Glib::ustring(val);
89 else
90 newvalue = new Glib::ustring(contents);
92 if ( (newguitext) && (newvalue) ) { // logical error if this is not true here
93 choices = g_slist_append( choices, new enumentry(newvalue, newguitext) );
94 }
95 }
96 child_repr = sp_repr_next(child_repr);
97 }
98 }
100 // Initialize _value with the default value from xml
101 // for simplicity : default to the contents of the first xml-child
102 const char * defaultval = NULL;
103 if (sp_repr_children(sp_repr_children(xml)) != NULL)
104 defaultval = sp_repr_children(xml)->attribute("value");
106 gchar * pref_name = this->pref_name();
107 const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
108 g_free(pref_name);
110 if (paramval != NULL)
111 defaultval = paramval;
112 if (defaultval != NULL)
113 _value = g_strdup(defaultval); // allocate space for _value
115 return;
116 }
118 ParamComboBox::~ParamComboBox (void)
119 {
120 //destroy choice strings
121 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
122 delete (reinterpret_cast<enumentry *>(list->data));
123 }
124 g_slist_free(choices);
126 g_free(_value);
127 }
130 /** \brief A function to set the \c _value
131 \param in The value to set
132 \param doc A document that should be used to set the value.
133 \param node The node where the value may be placed
135 This function sets ONLY the internal value, but it also sets the value
136 in the preferences structure. To put it in the right place, \c PREF_DIR
137 and \c pref_name() are used.
139 To copy the data into _value the old memory must be free'd first.
140 It is important to note that \c g_free handles \c NULL just fine. Then
141 the passed in value is duplicated using \c g_strdup().
142 */
143 const gchar *
144 ParamComboBox::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
145 {
146 if (in == NULL) return NULL; /* Can't have NULL string */
148 Glib::ustring * settext = NULL;
149 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
150 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
151 if ( !entr->guitext->compare(in) ) {
152 settext = entr->value;
153 break; // break out of for loop
154 }
155 }
156 if (settext) {
157 if (_value != NULL) g_free(_value);
158 _value = g_strdup(settext->c_str());
159 gchar * prefname = this->pref_name();
160 prefs_set_string_attribute(PREF_DIR, prefname, _value);
161 g_free(prefname);
162 }
164 return _value;
165 }
167 void
168 ParamComboBox::changed (void) {
170 }
173 /**
174 \brief A function to get the value of the parameter in string form
175 \return A string with the 'value' as command line argument
176 */
177 void
178 ParamComboBox::string (std::string &string)
179 {
180 string += _value;
181 return;
182 }
187 /** \brief A special category of Gtk::Entry to handle string parameteres */
188 class ParamComboBoxEntry : public Gtk::ComboBoxText {
189 private:
190 ParamComboBox * _pref;
191 SPDocument * _doc;
192 Inkscape::XML::Node * _node;
193 sigc::signal<void> * _changeSignal;
194 public:
195 /** \brief Build a string preference for the given parameter
196 \param pref Where to get the string from, and where to put it
197 when it changes.
198 */
199 ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
200 Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
201 this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
202 };
203 void changed (void);
204 };
206 /** \brief Respond to the text box changing
208 This function responds to the box changing by grabbing the value
209 from the text box and putting it in the parameter.
210 */
211 void
212 ParamComboBoxEntry::changed (void)
213 {
214 Glib::ustring data = this->get_active_text();
215 _pref->set(data.c_str(), _doc, _node);
216 if (_changeSignal != NULL) {
217 _changeSignal->emit();
218 }
219 }
221 /**
222 \brief Creates a combobox widget for an enumeration parameter
223 */
224 Gtk::Widget *
225 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
226 {
227 if (_gui_hidden) return NULL;
229 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
231 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
232 label->show();
233 hbox->pack_start(*label, false, false);
235 ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
236 // add choice strings:
237 Glib::ustring * settext = 0;
238 for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
239 enumentry * entr = reinterpret_cast<enumentry *>(list->data);
240 Glib::ustring * text = entr->guitext;
241 combo->append_text(*text);
242 if ( !entr->value->compare(_value) ) {
243 settext = entr->guitext;
244 }
245 }
246 if (settext) combo->set_active_text(*settext);
248 combo->show();
249 hbox->pack_start(*combo, true, true);
251 hbox->show();
253 return dynamic_cast<Gtk::Widget *>(hbox);
254 }
257 } /* namespace Extension */
258 } /* namespace Inkscape */