Code

Changed preference to use file chooser button
[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("");
67     // FIXME: I think the string should NOT be escaped. Just put between "..."
68     // Otherwise \frac{1}{2} will become \\frac{1}{2} and then the LaTeX effect won't work....
69     //gchar * esc = g_strescape(_value, NULL);
70     Glib::ustring escaped(_value);
71     //g_free(esc);
72     
73 #ifdef ESCAPE_DOLLAR_COMMANDLINE // escape the dollar sign 
74     Glib::ustring::iterator i;
75     for (i = escaped.begin(); i != escaped.end(); ++i) {
76         if ( *i == '$') {
77             i = escaped.insert(i, '\\');
78             i++;
79         }
80     }
81 #endif
83     Glib::ustring * mystring = new Glib::ustring("");
84     *mystring += "\"";
85     *mystring += escaped;
86     *mystring += "\"";
87     
88     return mystring;
89 }
91 /** \brief  Initialize the object, to do that, copy the data. */
92 ParamString::ParamString (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
93     Parameter(name, guitext, desc, scope, ext), _value(NULL)
94 {
95     const char * defaultval = NULL;
96     if (sp_repr_children(xml) != NULL)
97         defaultval = sp_repr_children(xml)->content();
99     gchar * pref_name = this->pref_name();
100     const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
101     g_free(pref_name);
103     if (paramval != NULL)
104         defaultval = paramval;
105     if (defaultval != NULL)
106         _value = g_strdup(defaultval);
108     return;
111 /** \brief  A special category of Gtk::Entry to handle string parameteres */
112 class ParamStringEntry : public Gtk::Entry {
113 private:
114     ParamString * _pref;
115     SPDocument * _doc;
116     Inkscape::XML::Node * _node;
117     sigc::signal<void> * _changeSignal;
118 public:
119     /** \brief  Build a string preference for the given parameter
120         \param  pref  Where to get the string from, and where to put it
121                       when it changes.
122     */
123     ParamStringEntry (ParamString * pref, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
124         Gtk::Entry(), _pref(pref), _doc(doc), _node(node), _changeSignal(changeSignal) {
125         if (_pref->get(NULL, NULL) != NULL)
126             this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
127         this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
128     };
129     void changed_text (void);
130 };
133 /** \brief  Respond to the text box changing
135     This function responds to the box changing by grabbing the value
136     from the text box and putting it in the parameter.
137 */
138 void
139 ParamStringEntry::changed_text (void)
141     Glib::ustring data = this->get_text();
142     _pref->set(data.c_str(), _doc, _node);
143     if (_changeSignal != NULL) {
144         _changeSignal->emit();
145     }
146     return;
149 /**
150     \brief  Creates a text box for the string parameter
152     Builds a hbox with a label and a text box in it.
153 */
154 Gtk::Widget *
155 ParamString::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
157     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
159     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
160     label->show();
161     hbox->pack_start(*label, false, false);
163     ParamStringEntry * textbox = new ParamStringEntry(this, doc, node, changeSignal);
164     textbox->show();
165     hbox->pack_start(*textbox, true, true);
167     hbox->show();
169     return dynamic_cast<Gtk::Widget *>(hbox);
172 }  /* namespace Extension */
173 }  /* namespace Inkscape */