Code

33c1534341c5a71cb147ff962a2014cb29768fe0
[inkscape.git] / src / extension / param / enum.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/extension.h>
31 #include <prefs-utils.h>
32 #include <document-private.h>
33 #include <sp-object.h>
35 #include "enum.h"
36 #include "preferences.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 enumentry {
48 public:
49     enumentry (Glib::ustring * val, Glib::ustring * text) {
50         value = val;
51         guitext = text;
52     }
53     ~enumentry() {
54         delete value;
55         delete guitext;
56     }
58     Glib::ustring * value;
59     Glib::ustring * guitext;
60 };
63 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) :
64     Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext)
65 {
66     choices = NULL;
67     _value = NULL;
69     // Read XML tree to add enumeration items:
70     // printf("Extension Constructor: ");
71     if (xml != NULL) {
72         Inkscape::XML::Node *child_repr = sp_repr_children(xml);
73         while (child_repr != NULL) {
74             char const * chname = child_repr->name();
75             if (!strcmp(chname, "extension:item") || !strcmp(chname, "extension:_item")) {
76                 Glib::ustring * newguitext = NULL;
77                 Glib::ustring * newvalue = NULL;
78                 const char * contents = sp_repr_children(child_repr)->content();
79                 if (contents != NULL)
80                     // don't translate when 'item' but do translate when '_item'
81                         // NOTE: internal extensions use build_from_mem and don't need _item but 
82                         //       still need to include if are to be localized                   
83                      newguitext = new Glib::ustring( !strcmp(chname, "extension:_item") ? _(contents) : contents );
84                 else
85                     continue;
87                 const char * val = child_repr->attribute("value");
88                 if (val != NULL)
89                     newvalue = new Glib::ustring(val);
90                 else
91                     newvalue = new Glib::ustring(contents);
93                 if ( (newguitext) && (newvalue) ) {   // logical error if this is not true here
94                     choices = g_slist_append( choices, new enumentry(newvalue, newguitext) );
95                 }
96             }
97             child_repr = sp_repr_next(child_repr);
98         }
99     }
101     // Initialize _value with the default value from xml
102     // for simplicity : default to the contents of the first xml-child
103     const char * defaultval = NULL;
104     if (sp_repr_children(sp_repr_children(xml)) != NULL)
105         defaultval = sp_repr_children(xml)->attribute("value");
107     gchar * pref_name = this->pref_name();
108     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
109     Glib::ustring paramval = prefs->getString(PREF_DIR, pref_name);
110     g_free(pref_name);
112     if (!paramval.empty())
113         defaultval = paramval.data();
114     if (defaultval != NULL)
115         _value = g_strdup(defaultval);
117     return;
120 ParamComboBox::~ParamComboBox (void)
122     //destroy choice strings
123     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
124         delete (reinterpret_cast<enumentry *>(list->data));
125     }
126     g_slist_free(choices);
128     g_free(_value);
132 /** \brief  A function to set the \c _value
133     \param  in   The value to set
134     \param  doc  A document that should be used to set the value.
135     \param  node The node where the value may be placed
137     This function sets ONLY the internal value, but it also sets the value
138     in the preferences structure.  To put it in the right place, \c PREF_DIR
139     and \c pref_name() are used.
141     To copy the data into _value the old memory must be free'd first.
142     It is important to note that \c g_free handles \c NULL just fine.  Then
143     the passed in value is duplicated using \c g_strdup().
144 */
145 const gchar *
146 ParamComboBox::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
148     if (in == NULL) return NULL; /* Can't have NULL string */
150     Glib::ustring * settext = NULL;
151     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
152         enumentry * entr = reinterpret_cast<enumentry *>(list->data);
153         if ( !entr->guitext->compare(in) ) {
154             settext = entr->value;
155             break;  // break out of for loop
156         }
157     }
158     if (settext) {
159         if (_value != NULL) g_free(_value);
160         _value = g_strdup(settext->c_str());
161         gchar * prefname = this->pref_name();
162         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
163         prefs->setString(PREF_DIR, prefname, _value);
164         g_free(prefname);
165     }
167     return _value;
170 void
171 ParamComboBox::changed (void) {
172     
176 /**
177     \brief  A function to get the value of the parameter in string form
178     \return A string with the 'value' as command line argument
179 */
180 void
181 ParamComboBox::string (std::string &string)
183     string += _value;
184     return;
190 /** \brief  A special category of Gtk::Entry to handle string parameteres */
191 class ParamComboBoxEntry : public Gtk::ComboBoxText {
192 private:
193     ParamComboBox * _pref;
194     SPDocument * _doc;
195     Inkscape::XML::Node * _node;
196     sigc::signal<void> * _changeSignal;
197 public:
198     /** \brief  Build a string preference for the given parameter
199         \param  pref  Where to get the string from, and where to put it
200                       when it changes.
201     */
202     ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
203         Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
204         this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
205     };
206     void changed (void);
207 };
209 /** \brief  Respond to the text box changing
211     This function responds to the box changing by grabbing the value
212     from the text box and putting it in the parameter.
213 */
214 void
215 ParamComboBoxEntry::changed (void)
217     Glib::ustring data = this->get_active_text();
218     _pref->set(data.c_str(), _doc, _node);
219     if (_changeSignal != NULL) {
220         _changeSignal->emit();
221     }
224 /**
225     \brief  Creates a combobox widget for an enumeration parameter
226 */
227 Gtk::Widget *
228 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
230         if (_gui_hidden) return NULL;
232     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
234     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
235     label->show();
236     hbox->pack_start(*label, false, false);
238     ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
239     // add choice strings:
240     Glib::ustring * settext = 0;
241     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
242         enumentry * entr = reinterpret_cast<enumentry *>(list->data);
243         Glib::ustring * text = entr->guitext;
244         combo->append_text(*text);
245         if ( !entr->value->compare(_value) ) {
246             settext = entr->guitext;
247         }
248     }
249     if (settext) combo->set_active_text(*settext);
251     combo->show();
252     hbox->pack_start(*combo, true, true);
254     hbox->show();
256     return dynamic_cast<Gtk::Widget *>(hbox);
260 }  /* namespace Extension */
261 }  /* namespace Inkscape */