Code

Extensions. New context support in extensions (enum attribute only), should fix Bug...
[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>
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                     if (!strcmp(chname, INKSCAPE_EXTENSION_NS "_item")) {
71                         if (node->attribute("msgctxt") != NULL) {
72                             newguitext =  g_dpgettext2(NULL, node->attribute("msgctxt"), contents);
73                         } else {
74                             newguitext =  _(contents);
75                         }
76                     } else {
77                         newguitext =  contents;
78                     }
79                 } else
80                     continue;
82                 const char * val = node->attribute("value");
83                 if (val != NULL)
84                     newvalue = val;
85                 else
86                     newvalue = contents;
88                 if ( (!newguitext.empty()) && (!newvalue.empty()) ) {   // logical error if this is not true here
89                     choices = g_slist_append( choices, new enumentry(newvalue, newguitext) );
90                 }
91             }
92         }
93     }
95     // Initialize _value with the default value from xml
96     // for simplicity : default to the contents of the first xml-child
97     const char * defaultval = NULL;
98     if (xml->firstChild() && xml->firstChild()->firstChild())
99         defaultval = xml->firstChild()->attribute("value");
101     gchar * pref_name = this->pref_name();
102     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
103     Glib::ustring paramval = prefs->getString(extension_pref_root + pref_name);
104     g_free(pref_name);
106     if (!paramval.empty())
107         defaultval = paramval.data();
108     if (defaultval != NULL)
109         _value = g_strdup(defaultval);
111     return;
114 ParamComboBox::~ParamComboBox (void)
116     //destroy choice strings
117     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
118         delete (reinterpret_cast<enumentry *>(list->data));
119     }
120     g_slist_free(choices);
122     g_free(_value);
126 /** \brief  A function to set the \c _value
127     \param  in   The value to set
128     \param  doc  A document that should be used to set the value.
129     \param  node The node where the value may be placed
131     This function sets ONLY the internal value, but it also sets the value
132     in the preferences structure.  To put it in the right place, \c PREF_DIR
133     and \c pref_name() are used.
135     To copy the data into _value the old memory must be free'd first.
136     It is important to note that \c g_free handles \c NULL just fine.  Then
137     the passed in value is duplicated using \c g_strdup().
138 */
139 const gchar *
140 ParamComboBox::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
142     if (in == NULL) return NULL; /* Can't have NULL string */
144     Glib::ustring settext;
145     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
146         enumentry * entr = reinterpret_cast<enumentry *>(list->data);
147         if ( !entr->guitext.compare(in) ) {
148             settext = entr->value;
149             break;  // break out of for loop
150         }
151     }
152     if (!settext.empty()) {
153         if (_value != NULL) g_free(_value);
154         _value = g_strdup(settext.data());
155         gchar * prefname = this->pref_name();
156         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
157         prefs->setString(extension_pref_root + prefname, _value);
158         g_free(prefname);
159     }
161     return _value;
164 void
165 ParamComboBox::changed (void) {
170 /**
171     \brief  A function to get the value of the parameter in string form
172     \return A string with the 'value' as command line argument
173 */
174 void
175 ParamComboBox::string (std::string &string)
177     string += _value;
178     return;
184 /** \brief  A special category of Gtk::Entry to handle string parameteres */
185 class ParamComboBoxEntry : public Gtk::ComboBoxText {
186 private:
187     ParamComboBox * _pref;
188     SPDocument * _doc;
189     Inkscape::XML::Node * _node;
190     sigc::signal<void> * _changeSignal;
191 public:
192     /** \brief  Build a string preference for the given parameter
193         \param  pref  Where to get the string from, and where to put it
194                       when it changes.
195     */
196     ParamComboBoxEntry (ParamComboBox * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
197         Gtk::ComboBoxText(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
198         this->signal_changed().connect(sigc::mem_fun(this, &ParamComboBoxEntry::changed));
199     };
200     void changed (void);
201 };
203 /** \brief  Respond to the text box changing
205     This function responds to the box changing by grabbing the value
206     from the text box and putting it in the parameter.
207 */
208 void
209 ParamComboBoxEntry::changed (void)
211     Glib::ustring data = this->get_active_text();
212     _pref->set(data.c_str(), _doc, _node);
213     if (_changeSignal != NULL) {
214         _changeSignal->emit();
215     }
218 /**
219     \brief  Creates a combobox widget for an enumeration parameter
220 */
221 Gtk::Widget *
222 ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
224         if (_gui_hidden) return NULL;
226     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
228     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
229     label->show();
230     hbox->pack_start(*label, false, false);
232     ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node, changeSignal));
233     // add choice strings:
234     Glib::ustring settext;
235     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
236         enumentry * entr = reinterpret_cast<enumentry *>(list->data);
237         Glib::ustring text = entr->guitext;
238         combo->append_text(text);
239         if ( _value && !entr->value.compare(_value) ) {
240             settext = entr->guitext;
241         }
242     }
243     if (!settext.empty()) combo->set_active_text(settext);
245     combo->show();
246     hbox->pack_start(*combo, true, true);
248     hbox->show();
250     return dynamic_cast<Gtk::Widget *>(hbox);
254 }  /* namespace Extension */
255 }  /* namespace Inkscape */