Code

301d54ed008aa0ff1b9737c10e54bff4f496b71e
[inkscape.git] / src / extension / param / int.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"
17 #include "extension/extension.h"
18 #include "preferences.h"
19 #include "int.h"
21 namespace Inkscape {
22 namespace Extension {
25 /** \brief  Use the superclass' allocator and set the \c _value */
26 ParamInt::ParamInt (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) :
27         Parameter(name, guitext, desc, scope, gui_hidden, gui_tip, ext), _value(0), _min(0), _max(10)
28 {
29     const char * defaultval = NULL;
30     if (sp_repr_children(xml) != NULL)
31         defaultval = sp_repr_children(xml)->content();
32     if (defaultval != NULL) {
33         _value = atoi(defaultval);
34     }
36     const char * maxval = xml->attribute("max");
37     if (maxval != NULL)
38         _max = atoi(maxval);
40     const char * minval = xml->attribute("min");
41     if (minval != NULL)
42         _min = atoi(minval);
44     /* We're handling this by just killing both values */
45     if (_max < _min) {
46         _max = 10;
47         _min = 0;
48     }
50     gchar *pref_name = this->pref_name();
51     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
52     _value = prefs->getInt(extension_pref_root + pref_name, _value);
53     g_free(pref_name);
55     // std::cout << "New Int::  value: " << _value << "  max: " << _max << "  min: " << _min << std::endl;
57     if (_value > _max) _value = _max;
58     if (_value < _min) _value = _min;
60     return;
61 }
63 /** \brief  A function to set the \c _value
64     \param  in   The value to set to
65     \param  doc  A document that should be used to set the value.
66     \param  node The node where the value may be placed
68     This function sets the internal value, but it also sets the value
69     in the preferences structure.  To put it in the right place, \c PREF_DIR
70     and \c pref_name() are used.
71 */
72 int
73 ParamInt::set (int in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
74 {
75     _value = in;
76     if (_value > _max) _value = _max;
77     if (_value < _min) _value = _min;
79     gchar * prefname = this->pref_name();
80     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
81     prefs->setInt(extension_pref_root + prefname, _value);
82     g_free(prefname);
84     return _value;
85 }
87 /** \brief  A class to make an adjustment that uses Extension params */
88 class ParamIntAdjustment : public Gtk::Adjustment {
89     /** The parameter to adjust */
90     ParamInt * _pref;
91     SPDocument * _doc;
92     Inkscape::XML::Node * _node;
93     sigc::signal<void> * _changeSignal;
94 public:
95     /** \brief  Make the adjustment using an extension and the string
96                 describing the parameter. */
97     ParamIntAdjustment (ParamInt * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
98             Gtk::Adjustment(0.0, param->min(), param->max(), 1.0, 0), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
99         this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
100         this->signal_value_changed().connect(sigc::mem_fun(this, &ParamIntAdjustment::val_changed));
101         return;
102     };
104     void val_changed (void);
105 }; /* class ParamIntAdjustment */
107 /** \brief  A function to respond to the value_changed signal from the
108             adjustment.
110     This function just grabs the value from the adjustment and writes
111     it to the parameter.  Very simple, but yet beautiful.
112 */
113 void
114 ParamIntAdjustment::val_changed (void)
116     //std::cout << "Value Changed to: " << this->get_value() << std::endl;
117     _pref->set((int)this->get_value(), _doc, _node);
118     if (_changeSignal != NULL) {
119         _changeSignal->emit();
120     }
121     return;
124 /**
125     \brief  Creates a Int Adjustment for a int parameter
127     Builds a hbox with a label and a int adjustment in it.
128 */
129 Gtk::Widget *
130 ParamInt::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
132         if (_gui_hidden) return NULL;
134     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
136     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
137     label->show();
138     hbox->pack_start(*label, true, true);
140     ParamIntAdjustment * fadjust = Gtk::manage(new ParamIntAdjustment(this, doc, node, changeSignal));
141     Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 1.0, 0));
142     spin->show();
143     hbox->pack_start(*spin, false, false);
145     hbox->show();
147     return dynamic_cast<Gtk::Widget *>(hbox);
150 /** \brief  Return the value as a string */
151 void
152 ParamInt::string (std::string &string)
154     char startstring[32];
155     sprintf(startstring, "%d", _value);
156     string += startstring;
157     return;
160 }  /* namespace Extension */
161 }  /* namespace Inkscape */
163 /*
164   Local Variables:
165   mode:c++
166   c-file-style:"stroustrup"
167   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168   indent-tabs-mode:nil
169   fill-column:99
170   End:
171 */
172 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :