Code

r16676@shi: ted | 2007-10-12 21:09:50 -0700
[inkscape.git] / src / extension / paramstring.cpp
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.h"
19 #include "paramstring.h"
21 namespace Inkscape {
22 namespace Extension {
23     
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     prefs_set_string_attribute(PREF_DIR, prefname, _value);
55     g_free(prefname);
57     return _value;
58 }
60 /** \brief  Return the value as a string */
61 Glib::ustring *
62 ParamString::string (void)
63 {
64     if (_value == NULL)
65         return new Glib::ustring("");
66         return new Glib::ustring(_value);
67 }
69 /** \brief  Initialize the object, to do that, copy the data. */
70 ParamString::ParamString (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
71     Parameter(name, guitext, desc, scope, ext), _value(NULL)
72 {
73     const char * defaultval = NULL;
74     if (sp_repr_children(xml) != NULL)
75         defaultval = sp_repr_children(xml)->content();
77     gchar * pref_name = this->pref_name();
78     const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
79     g_free(pref_name);
81     if (paramval != NULL)
82         defaultval = paramval;
83     if (defaultval != NULL)
84         _value = g_strdup(defaultval);
86     return;
87 }
89 /** \brief  A special category of Gtk::Entry to handle string parameteres */
90 class ParamStringEntry : public Gtk::Entry {
91 private:
92     ParamString * _pref;
93     SPDocument * _doc;
94     Inkscape::XML::Node * _node;
95     sigc::signal<void> * _changeSignal;
96 public:
97     /** \brief  Build a string preference for the given parameter
98         \param  pref  Where to get the string from, and where to put it
99                       when it changes.
100     */
101     ParamStringEntry (ParamString * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
102         Gtk::Entry(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
103         if (_pref->get(NULL, NULL) != NULL)
104             this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
105         this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
106     };
107     void changed_text (void);
108 };
111 /** \brief  Respond to the text box changing
113     This function responds to the box changing by grabbing the value
114     from the text box and putting it in the parameter.
115 */
116 void
117 ParamStringEntry::changed_text (void)
119     Glib::ustring data = this->get_text();
120     _pref->set(data.c_str(), _doc, _node);
121     if (_changeSignal != NULL) {
122         _changeSignal->emit();
123     }
124     return;
127 /**
128     \brief  Creates a text box for the string parameter
130     Builds a hbox with a label and a text box in it.
131 */
132 Gtk::Widget *
133 ParamString::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
135     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
137     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
138     label->show();
139     hbox->pack_start(*label, false, false);
141     ParamStringEntry * textbox = new ParamStringEntry(this, doc, node, changeSignal);
142     textbox->show();
143     hbox->pack_start(*textbox, true, true);
145     hbox->show();
147     return dynamic_cast<Gtk::Widget *>(hbox);
150 }  /* namespace Extension */
151 }  /* namespace Inkscape */