1 /*
2 * Copyright (C) 2005-2007 Authors:
3 * Ted Gould <ted@gould.cx>
4 * Johan Engelen <johan@shouraizou.nl> *
5 * Released under GNU GPL, read the file 'COPYING' for more information
6 */
8 #ifdef HAVE_CONFIG_H
9 # include "config.h"
10 #endif
12 #include <gtkmm/adjustment.h>
13 #include <gtkmm/box.h>
14 #include <gtkmm/spinbutton.h>
16 #include "xml/node.h"
17 #include "extension/extension.h"
18 #include "string.h"
19 #include "preferences.h"
21 namespace Inkscape {
22 namespace Extension {
25 /** \brief Free the allocated data. */
26 ParamString::~ParamString(void)
27 {
28 g_free(_value);
29 }
31 /** \brief A function to set the \c _value
32 \param in The value to set to
33 \param doc A document that should be used to set the value.
34 \param node The node where the value may be placed
36 This function sets the internal value, but it also sets the value
37 in the preferences structure. To put it in the right place, \c PREF_DIR
38 and \c pref_name() are used.
40 To copy the data into _value the old memory must be free'd first.
41 It is important to note that \c g_free handles \c NULL just fine. Then
42 the passed in value is duplicated using \c g_strdup().
43 */
44 const gchar *
45 ParamString::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
46 {
47 if (in == NULL) return NULL; /* Can't have NULL string */
49 if (_value != NULL)
50 g_free(_value);
51 _value = g_strdup(in);
53 gchar * prefname = this->pref_name();
54 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
55 prefs->setString(extension_pref_root + prefname, _value);
56 g_free(prefname);
58 return _value;
59 }
61 /** \brief Return the value as a string */
62 void
63 ParamString::string (std::string &string)
64 {
65 if (_value == NULL)
66 return;
68 string += _value;
69 return;
70 }
72 /** \brief Initialize the object, to do that, copy the data. */
73 ParamString::ParamString (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) :
74 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext), _value(NULL)
75 {
76 const char * defaultval = NULL;
77 if (sp_repr_children(xml) != NULL)
78 defaultval = sp_repr_children(xml)->content();
80 gchar * pref_name = this->pref_name();
81 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
82 Glib::ustring paramval = prefs->getString(extension_pref_root + pref_name);
83 g_free(pref_name);
85 if (!paramval.empty())
86 defaultval = paramval.data();
87 if (defaultval != NULL)
88 _value = g_strdup(defaultval);
90 _max_length = 0;
92 return;
93 }
95 /** \brief A special type of Gtk::Entry to handle string parameteres */
96 class ParamStringEntry : public Gtk::Entry {
97 private:
98 ParamString * _pref;
99 SPDocument * _doc;
100 Inkscape::XML::Node * _node;
101 sigc::signal<void> * _changeSignal;
102 public:
103 /** \brief Build a string preference for the given parameter
104 \param pref Where to get the string from, and where to put it
105 when it changes.
106 */
107 ParamStringEntry (ParamString * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
108 Gtk::Entry(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
109 if (_pref->get(NULL, NULL) != NULL)
110 this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
111 this->set_max_length(_pref->getMaxLength()); //Set the max lenght - default zero means no maximum
112 this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
113 };
114 void changed_text (void);
115 };
118 /** \brief Respond to the text box changing
120 This function responds to the box changing by grabbing the value
121 from the text box and putting it in the parameter.
122 */
123 void
124 ParamStringEntry::changed_text (void)
125 {
126 Glib::ustring data = this->get_text();
127 _pref->set(data.c_str(), _doc, _node);
128 if (_changeSignal != NULL) {
129 _changeSignal->emit();
130 }
131 return;
132 }
134 /**
135 \brief Creates a text box for the string parameter
137 Builds a hbox with a label and a text box in it.
138 */
139 Gtk::Widget *
140 ParamString::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
141 {
142 if (_gui_hidden) return NULL;
144 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
146 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
147 label->show();
148 hbox->pack_start(*label, false, false);
150 ParamStringEntry * textbox = new ParamStringEntry(this, doc, node, changeSignal);
151 textbox->show();
152 hbox->pack_start(*textbox, true, true);
154 hbox->show();
156 return dynamic_cast<Gtk::Widget *>(hbox);
157 }
159 } /* namespace Extension */
160 } /* namespace Inkscape */