Code

Fixed messed up transformations because of a missing cairo_restore() and removed...
[inkscape.git] / src / extension / paramfloat.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 "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     _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     _value = prefs_get_double_attribute(PREF_DIR, pref_name, _value);
57     g_free(pref_name);
59     // std::cout << "New Float::  value: " << _value << "  max: " << _max << "  min: " << _min << std::endl;
61     if (_value > _max) _value = _max;
62     if (_value < _min) _value = _min;
64     return;
65 }
67 /** \brief  A function to set the \c _value
68     \param  in   The value to set to
69     \param  doc  A document that should be used to set the value.
70     \param  node The node where the value may be placed
72     This function sets the internal value, but it also sets the value
73     in the preferences structure.  To put it in the right place, \c PREF_DIR
74     and \c pref_name() are used.
75 */
76 float
77 ParamFloat::set (float in, SPDocument * doc, Inkscape::XML::Node * node)
78 {
79     _value = in;
80     if (_value > _max) _value = _max;
81     if (_value < _min) _value = _min;
83     gchar * prefname = this->pref_name();
84     prefs_set_double_attribute(PREF_DIR, prefname, _value);
85     g_free(prefname);
87     return _value;
88 }
90 /** \brief  Return the value as a string */
91 Glib::ustring *
92 ParamFloat::string (void)
93 {
94     char startstring[G_ASCII_DTOSTR_BUF_SIZE];
95     g_ascii_dtostr(startstring, G_ASCII_DTOSTR_BUF_SIZE, _value);
96     Glib::ustring * mystring = new Glib::ustring(startstring);
97     return mystring;
98 }
100 /** \brief  A class to make an adjustment that uses Extension params */
101 class ParamFloatAdjustment : public Gtk::Adjustment {
102     /** The parameter to adjust */
103     ParamFloat * _pref;
104     SPDocument * _doc;
105     Inkscape::XML::Node * _node;
106     sigc::signal<void> * _changeSignal;
107 public:
108     /** \brief  Make the adjustment using an extension and the string
109                 describing the parameter. */
110     ParamFloatAdjustment (ParamFloat * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
111             Gtk::Adjustment(0.0, param->min(), param->max(), 0.1), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
112         this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
113         this->signal_value_changed().connect(sigc::mem_fun(this, &ParamFloatAdjustment::val_changed));
114         return;
115     };
117     void val_changed (void);
118 }; /* class ParamFloatAdjustment */
120 /** \brief  A function to respond to the value_changed signal from the
121             adjustment.
123     This function just grabs the value from the adjustment and writes
124     it to the parameter.  Very simple, but yet beautiful.
125 */
126 void
127 ParamFloatAdjustment::val_changed (void)
129     //std::cout << "Value Changed to: " << this->get_value() << std::endl;
130     _pref->set(this->get_value(), _doc, _node);
131     if (_changeSignal != NULL) {
132         _changeSignal->emit();
133     }
134     return;
137 /**
138     \brief  Creates a Float Adjustment for a float parameter
140     Builds a hbox with a label and a float adjustment in it.
141 */
142 Gtk::Widget *
143 ParamFloat::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
145     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
147     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
148     label->show();
149     hbox->pack_start(*label, true, true);
151     ParamFloatAdjustment * fadjust = Gtk::manage(new ParamFloatAdjustment(this, doc, node, changeSignal));
152     Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 0.1, _precision));
153     spin->show();
154     hbox->pack_start(*spin, false, false);
156     hbox->show();
158     return dynamic_cast<Gtk::Widget *>(hbox);
162 }  /* namespace Extension */
163 }  /* namespace Inkscape */