Code

Changed preference to use file chooser button
[inkscape.git] / src / extension / paramenum.cpp
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.h"
31 #include "prefs-utils.h"
32 #include "document-private.h"
33 #include "sp-object.h"
35 #include "paramenum.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, 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, "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                      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 enumentry(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;
109 ParamComboBox::~ParamComboBox (void)
111     //destroy choice strings
112     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
113         delete (reinterpret_cast<enumentry *>(list->data));
114     }
115     g_slist_free(choices);
117     g_free(_value);
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 ParamComboBox::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * node)
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         enumentry * entr = reinterpret_cast<enumentry *>(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;
158 void
159 ParamComboBox::changed (void) {
160     
164 /**
165     \brief  A function to get the value of the parameter in string form
166     \return A string with the 'value' as command line argument
167 */
168 Glib::ustring *
169 ParamComboBox::string (void)
171     Glib::ustring * param_string = new Glib::ustring("");
172     *param_string += _value;
173     return param_string;
179 /** \brief  A special category of Gtk::Entry to handle string parameteres */
180 class ParamComboBoxEntry : public Gtk::ComboBoxText {
181 private:
182     ParamComboBox * _pref;
183     SPDocument * _doc;
184     Inkscape::XML::Node * _node;
185     sigc::signal<void> * _changeSignal;
186 public:
187     /** \brief  Build a string preference for the given parameter
188         \param  pref  Where to get the string from, and where to put it
189                       when it changes.
190     */
191     ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
192         Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
193         this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
194     };
195     void changed (void);
196 };
198 /** \brief  Respond to the text box changing
200     This function responds to the box changing by grabbing the value
201     from the text box and putting it in the parameter.
202 */
203 void
204 ParamComboBoxEntry::changed (void)
206     Glib::ustring data = this->get_active_text();
207     _pref->set(data.c_str(), _doc, _node);
208     if (_changeSignal != NULL) {
209         _changeSignal->emit();
210     }
213 /**
214     \brief  Creates a combobox widget for an enumeration parameter
215 */
216 Gtk::Widget *
217 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
219     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
221     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
222     label->show();
223     hbox->pack_start(*label, false, false);
225     ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
226     // add choice strings:
227     Glib::ustring * settext = 0;
228     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
229         enumentry * entr = reinterpret_cast<enumentry *>(list->data);
230         Glib::ustring * text = entr->guitext;
231         combo->append_text(*text);
232         if ( !entr->value->compare(_value) ) {
233             settext = entr->guitext;
234         }
235     }
236     if (settext) combo->set_active_text(*settext);
238     combo->show();
239     hbox->pack_start(*combo, true, true);
241     hbox->show();
243     return dynamic_cast<Gtk::Widget *>(hbox);
247 }  /* namespace Extension */
248 }  /* namespace Inkscape */