Code

3dd2a23282bfd66c04612a6f90272ffda0de7f02
[inkscape.git] / src / extension / param / string.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/extension.h>
19 #include "string.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 void
62 ParamString::string (std::string &string)
63 {
64     if (_value == NULL)
65         return;
67     string += _value;
68     return;
69 }
71 /** \brief  Initialize the object, to do that, copy the data. */
72 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) :
73     Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext), _value(NULL)
74 {
75     const char * defaultval = NULL;
76     if (sp_repr_children(xml) != NULL)
77         defaultval = sp_repr_children(xml)->content();
79     gchar * pref_name = this->pref_name();
80     const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
81     g_free(pref_name);
83     if (paramval != NULL)
84         defaultval = paramval;
85     if (defaultval != NULL)
86         _value = g_strdup(defaultval);
87     
88     _max_length = 0;
90     return;
91 }
93 /** \brief  A special type of Gtk::Entry to handle string parameteres */
94 class ParamStringEntry : public Gtk::Entry {
95 private:
96     ParamString * _pref;
97     SPDocument * _doc;
98     Inkscape::XML::Node * _node;
99     sigc::signal<void> * _changeSignal;
100 public:
101     /** \brief  Build a string preference for the given parameter
102         \param  pref  Where to get the string from, and where to put it
103                       when it changes.
104     */
105     ParamStringEntry (ParamString * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
106         Gtk::Entry(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
107         if (_pref->get(NULL, NULL) != NULL)
108             this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
109         this->set_max_length(_pref->getMaxLength()); //Set the max lenght - default zero means no maximum
110         this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
111     };
112     void changed_text (void);
113 };
116 /** \brief  Respond to the text box changing
118     This function responds to the box changing by grabbing the value
119     from the text box and putting it in the parameter.
120 */
121 void
122 ParamStringEntry::changed_text (void)
124     Glib::ustring data = this->get_text();
125     _pref->set(data.c_str(), _doc, _node);
126     if (_changeSignal != NULL) {
127         _changeSignal->emit();
128     }
129     return;
132 /**
133     \brief  Creates a text box for the string parameter
135     Builds a hbox with a label and a text box in it.
136 */
137 Gtk::Widget *
138 ParamString::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
140         if (_gui_hidden) return NULL;
142     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
144     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
145     label->show();
146     hbox->pack_start(*label, false, false);
148     ParamStringEntry * textbox = new ParamStringEntry(this, doc, node, changeSignal);
149     textbox->show();
150     hbox->pack_start(*textbox, true, true);
152     hbox->show();
154     return dynamic_cast<Gtk::Widget *>(hbox);
157 }  /* namespace Extension */
158 }  /* namespace Inkscape */