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 "float.h"
21 namespace Inkscape {
22 namespace Extension {
25 /** \brief Use the superclass' allocator and set the \c _value */
26 ParamFloat::ParamFloat (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.0), _min(0.0), _max(10.0)
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 = atof(defaultval);
34 }
36 const char * maxval = xml->attribute("max");
37 if (maxval != NULL)
38 _max = atof(maxval);
40 const char * minval = xml->attribute("min");
41 if (minval != NULL)
42 _min = atof(minval);
44 _precision = 1;
45 const char * precision = xml->attribute("precision");
46 if (precision != NULL)
47 _precision = atoi(precision);
49 /* We're handling this by just killing both values */
50 if (_max < _min) {
51 _max = 10.0;
52 _min = 0.0;
53 }
55 gchar * pref_name = this->pref_name();
56 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
57 _value = prefs->getDouble(extension_pref_root + pref_name, _value);
58 g_free(pref_name);
60 // std::cout << "New Float:: value: " << _value << " max: " << _max << " min: " << _min << std::endl;
62 if (_value > _max) _value = _max;
63 if (_value < _min) _value = _min;
65 return;
66 }
68 /** \brief A function to set the \c _value
69 \param in The value to set to
70 \param doc A document that should be used to set the value.
71 \param node The node where the value may be placed
73 This function sets the internal value, but it also sets the value
74 in the preferences structure. To put it in the right place, \c PREF_DIR
75 and \c pref_name() are used.
76 */
77 float
78 ParamFloat::set (float in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/)
79 {
80 _value = in;
81 if (_value > _max) _value = _max;
82 if (_value < _min) _value = _min;
84 gchar * prefname = this->pref_name();
85 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
86 prefs->setDouble(extension_pref_root + prefname, _value);
87 g_free(prefname);
89 return _value;
90 }
92 /** \brief Return the value as a string */
93 void
94 ParamFloat::string (std::string &string)
95 {
96 char startstring[G_ASCII_DTOSTR_BUF_SIZE];
97 g_ascii_dtostr(startstring, G_ASCII_DTOSTR_BUF_SIZE, _value);
98 string += startstring;
99 return;
100 }
102 /** \brief A class to make an adjustment that uses Extension params */
103 class ParamFloatAdjustment : public Gtk::Adjustment {
104 /** The parameter to adjust */
105 ParamFloat * _pref;
106 SPDocument * _doc;
107 Inkscape::XML::Node * _node;
108 sigc::signal<void> * _changeSignal;
109 public:
110 /** \brief Make the adjustment using an extension and the string
111 describing the parameter. */
112 ParamFloatAdjustment (ParamFloat * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
113 Gtk::Adjustment(0.0, param->min(), param->max(), 0.1, 0), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
114 this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
115 this->signal_value_changed().connect(sigc::mem_fun(this, &ParamFloatAdjustment::val_changed));
116 return;
117 };
119 void val_changed (void);
120 }; /* class ParamFloatAdjustment */
122 /** \brief A function to respond to the value_changed signal from the
123 adjustment.
125 This function just grabs the value from the adjustment and writes
126 it to the parameter. Very simple, but yet beautiful.
127 */
128 void
129 ParamFloatAdjustment::val_changed (void)
130 {
131 //std::cout << "Value Changed to: " << this->get_value() << std::endl;
132 _pref->set(this->get_value(), _doc, _node);
133 if (_changeSignal != NULL) {
134 _changeSignal->emit();
135 }
136 return;
137 }
139 /**
140 \brief Creates a Float Adjustment for a float parameter
142 Builds a hbox with a label and a float adjustment in it.
143 */
144 Gtk::Widget *
145 ParamFloat::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
146 {
147 if (_gui_hidden) return NULL;
149 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
151 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
152 label->show();
153 hbox->pack_start(*label, true, true);
155 ParamFloatAdjustment * fadjust = Gtk::manage(new ParamFloatAdjustment(this, doc, node, changeSignal));
156 Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 0.1, _precision));
157 spin->show();
158 hbox->pack_start(*spin, false, false);
160 hbox->show();
162 return dynamic_cast<Gtk::Widget *>(hbox);
163 }
166 } /* namespace Extension */
167 } /* namespace Inkscape */