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 "paramfloat.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, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
27 Parameter(name, guitext, desc, scope, 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 /* We're handling this by just killing both values */
45 if (_max < _min) {
46 _max = 10.0;
47 _min = 0.0;
48 }
50 gchar * pref_name = this->pref_name();
51 _value = prefs_get_double_attribute(PREF_DIR, pref_name, _value);
52 g_free(pref_name);
54 // std::cout << "New Float:: value: " << _value << " max: " << _max << " min: " << _min << std::endl;
56 if (_value > _max) _value = _max;
57 if (_value < _min) _value = _min;
59 return;
60 }
62 /** \brief A function to set the \c _value
63 \param in The value to set to
64 \param doc A document that should be used to set the value.
65 \param node The node where the value may be placed
67 This function sets the internal value, but it also sets the value
68 in the preferences structure. To put it in the right place, \c PREF_DIR
69 and \c pref_name() are used.
70 */
71 float
72 ParamFloat::set (float in, SPDocument * doc, Inkscape::XML::Node * node)
73 {
74 _value = in;
75 if (_value > _max) _value = _max;
76 if (_value < _min) _value = _min;
78 gchar * prefname = this->pref_name();
79 prefs_set_double_attribute(PREF_DIR, prefname, _value);
80 g_free(prefname);
82 return _value;
83 }
85 /** \brief Return the value as a string */
86 Glib::ustring *
87 ParamFloat::string (void)
88 {
89 char startstring[G_ASCII_DTOSTR_BUF_SIZE];
90 g_ascii_dtostr(startstring, G_ASCII_DTOSTR_BUF_SIZE, _value);
91 Glib::ustring * mystring = new Glib::ustring(startstring);
92 return mystring;
93 }
95 /** \brief A class to make an adjustment that uses Extension params */
96 class ParamFloatAdjustment : public Gtk::Adjustment {
97 /** The parameter to adjust */
98 ParamFloat * _pref;
99 SPDocument * _doc;
100 Inkscape::XML::Node * _node;
101 sigc::signal<void> * _changeSignal;
102 public:
103 /** \brief Make the adjustment using an extension and the string
104 describing the parameter. */
105 ParamFloatAdjustment (ParamFloat * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
106 Gtk::Adjustment(0.0, param->min(), param->max(), 0.1), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
107 this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
108 this->signal_value_changed().connect(sigc::mem_fun(this, &ParamFloatAdjustment::val_changed));
109 return;
110 };
112 void val_changed (void);
113 }; /* class ParamFloatAdjustment */
115 /** \brief A function to respond to the value_changed signal from the
116 adjustment.
118 This function just grabs the value from the adjustment and writes
119 it to the parameter. Very simple, but yet beautiful.
120 */
121 void
122 ParamFloatAdjustment::val_changed (void)
123 {
124 //std::cout << "Value Changed to: " << this->get_value() << std::endl;
125 _pref->set(this->get_value(), _doc, _node);
126 if (_changeSignal != NULL) {
127 _changeSignal->emit();
128 }
129 return;
130 }
132 /**
133 \brief Creates a Float Adjustment for a float parameter
135 Builds a hbox with a label and a float adjustment in it.
136 */
137 Gtk::Widget *
138 ParamFloat::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
139 {
140 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
142 Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
143 label->show();
144 hbox->pack_start(*label, true, true);
146 ParamFloatAdjustment * fadjust = Gtk::manage(new ParamFloatAdjustment(this, doc, node, changeSignal));
147 Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 0.1, 1));
148 spin->show();
149 hbox->pack_start(*spin, false, false);
151 hbox->show();
153 return dynamic_cast<Gtk::Widget *>(hbox);
154 }
157 } /* namespace Extension */
158 } /* namespace Inkscape */