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>
18 #include <extension/extension.h>
19 #include "string.h"
20 #include "preferences.h"
22 namespace Inkscape {
23 namespace Extension {
26 /** \brief Free the allocated data. */
27 ParamString::~ParamString(void)
28 {
29 g_free(_value);
30 }
32 /** \brief A function to set the \c _value
33 \param in The value to set to
34 \param doc A document that should be used to set the value.
35 \param node The node where the value may be placed
37 This function sets the internal value, but it also sets the value
38 in the preferences structure. To put it in the right place, \c PREF_DIR
39 and \c pref_name() are used.
41 To copy the data into _value the old memory must be free'd first.
42 It is important to note that \c g_free handles \c NULL just fine. Then
43 the passed in value is duplicated using \c g_strdup().
44 */
45 const gchar *
46 ParamString::set (const gchar * in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
47 {
48 if (in == NULL) return NULL; /* Can't have NULL string */
50 if (_value != NULL)
51 g_free(_value);
52 _value = g_strdup(in);
54 gchar * prefname = this->pref_name();
55 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
56 prefs->setString(PREF_DIR, prefname, _value);
57 g_free(prefname);
59 return _value;
60 }
62 /** \brief Return the value as a string */
63 void
64 ParamString::string (std::string &string)
65 {
66 if (_value == NULL)
67 return;
69 string += _value;
70 return;
71 }
73 /** \brief Initialize the object, to do that, copy the data. */
74 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) :
75 Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext), _value(NULL)
76 {
77 const char * defaultval = NULL;
78 if (sp_repr_children(xml) != NULL)
79 defaultval = sp_repr_children(xml)->content();
81 gchar * pref_name = this->pref_name();
82 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
83 Glib::ustring paramval = prefs->getString(PREF_DIR, pref_name);
84 g_free(pref_name);
86 if (!paramval.empty())
87 defaultval = paramval.data();
88 if (defaultval != NULL)
89 _value = g_strdup(defaultval);
91 _max_length = 0;
93 return;
94 }
96 /** \brief A special type of Gtk::Entry to handle string parameteres */
97 class ParamStringEntry : public Gtk::Entry {
98 private:
99 ParamString * _pref;
100 SPDocument * _doc;
101 Inkscape::XML::Node * _node;
102 sigc::signal<void> * _changeSignal;
103 public:
104 /** \brief Build a string preference for the given parameter
105 \param pref Where to get the string from, and where to put it
106 when it changes.
107 */
108 ParamStringEntry (ParamString * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
109 Gtk::Entry(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
110 if (_pref->get(NULL, NULL) != NULL)
111 this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
112 this->set_max_length(_pref->getMaxLength()); //Set the max lenght - default zero means no maximum
113 this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
114 };
115 void changed_text (void);
116 };
119 /** \brief Respond to the text box changing
121 This function responds to the box changing by grabbing the value
122 from the text box and putting it in the parameter.
123 */
124 void
125 ParamStringEntry::changed_text (void)
126 {
127 Glib::ustring data = this->get_text();
128 _pref->set(data.c_str(), _doc, _node);
129 if (_changeSignal != NULL) {
130 _changeSignal->emit();
131 }
132 return;
133 }
135 /**
136 \brief Creates a text box for the string parameter
138 Builds a hbox with a label and a text box in it.
139 */
140 Gtk::Widget *
141 ParamString::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
142 {
143 if (_gui_hidden) return NULL;
145 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
147 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
148 label->show();
149 hbox->pack_start(*label, false, false);
151 ParamStringEntry * textbox = new ParamStringEntry(this, doc, node, changeSignal);
152 textbox->show();
153 hbox->pack_start(*textbox, true, true);
155 hbox->show();
157 return dynamic_cast<Gtk::Widget *>(hbox);
158 }
160 } /* namespace Extension */
161 } /* namespace Inkscape */