Code

r18381@shi: ted | 2008-03-07 20:11:34 -0800
[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>
18 #include <extension/extension.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     _value = prefs_get_int_attribute(PREF_DIR, pref_name, _value);
52     g_free(pref_name);
54     // std::cout << "New Int::  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 int
72 ParamInt::set (int 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_int_attribute(PREF_DIR, prefname, _value);
80     g_free(prefname);
82     return _value;
83 }
85 /** \brief  A class to make an adjustment that uses Extension params */
86 class ParamIntAdjustment : public Gtk::Adjustment {
87     /** The parameter to adjust */
88     ParamInt * _pref;
89     SPDocument * _doc;
90     Inkscape::XML::Node * _node;
91     sigc::signal<void> * _changeSignal;
92 public:
93     /** \brief  Make the adjustment using an extension and the string
94                 describing the parameter. */
95     ParamIntAdjustment (ParamInt * param, SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal) :
96             Gtk::Adjustment(0.0, param->min(), param->max(), 1.0), _pref(param), _doc(doc), _node(node), _changeSignal(changeSignal) {
97         this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
98         this->signal_value_changed().connect(sigc::mem_fun(this, &ParamIntAdjustment::val_changed));
99         return;
100     };
102     void val_changed (void);
103 }; /* class ParamIntAdjustment */
105 /** \brief  A function to respond to the value_changed signal from the
106             adjustment.
108     This function just grabs the value from the adjustment and writes
109     it to the parameter.  Very simple, but yet beautiful.
110 */
111 void
112 ParamIntAdjustment::val_changed (void)
114     //std::cout << "Value Changed to: " << this->get_value() << std::endl;
115     _pref->set((int)this->get_value(), _doc, _node);
116     if (_changeSignal != NULL) {
117         _changeSignal->emit();
118     }
119     return;
122 /**
123     \brief  Creates a Int Adjustment for a int parameter
125     Builds a hbox with a label and a int adjustment in it.
126 */
127 Gtk::Widget *
128 ParamInt::get_widget (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
130         if (_gui_hidden) return NULL;
132     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
134     Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
135     label->show();
136     hbox->pack_start(*label, true, true);
138     ParamIntAdjustment * fadjust = Gtk::manage(new ParamIntAdjustment(this, doc, node, changeSignal));
139     Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 1.0, 0));
140     spin->show();
141     hbox->pack_start(*spin, false, false);
143     hbox->show();
145     return dynamic_cast<Gtk::Widget *>(hbox);
148 /** \brief  Return the value as a string */
149 void
150 ParamInt::string (std::string &string)
152     char startstring[32];
153     sprintf(startstring, "%d", _value);
154     string += startstring;
155     return;
158 }  /* namespace Extension */
159 }  /* namespace Inkscape */