Code

r11512@tres: ted | 2006-04-24 21:36:08 -0700
[inkscape.git] / src / extension / parameter.cpp
index 88da74a1dd689bc5fedc34b7727fc25d9faa7335..ce6c8c272634a19baadfe1558c19da1c3fd270d0 100644 (file)
@@ -40,17 +40,17 @@ private:
     /** \brief  Internal value. */
     bool _value;
 public:
-    ParamBool(const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
+    ParamBool(const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
     /** \brief  Returns \c _value */
-    bool get (const Inkscape::XML::Document * doc) { return _value; }
-    bool set (bool in, Inkscape::XML::Document * doc);
+    bool get (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node) { return _value; }
+    bool set (bool in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node);
     Gtk::Widget * get_widget(void);
     Glib::ustring * string (void);
 };
 
 /** \brief  Use the superclass' allocator and set the \c _value */
-ParamBool::ParamBool (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
-        Parameter(name, guitext, ext), _value(false)
+ParamBool::ParamBool (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
+        Parameter(name, guitext, desc, scope, ext), _value(false)
 {
     const char * defaultval = NULL;
     if (sp_repr_children(xml) != NULL)
@@ -76,10 +76,10 @@ private:
     int _min;
     int _max;
 public:
-    ParamInt (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
+    ParamInt (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
     /** \brief  Returns \c _value */
-    int get (const Inkscape::XML::Document * doc) { return _value; }
-    int set (int in, Inkscape::XML::Document * doc);
+    int get (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node) { return _value; }
+    int set (int in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node);
     int max (void) { return _max; }
     int min (void) { return _min; }
     Gtk::Widget * get_widget(void);
@@ -87,8 +87,8 @@ public:
 };
 
 /** \brief  Use the superclass' allocator and set the \c _value */
-ParamInt::ParamInt (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
-        Parameter(name, guitext, ext), _value(0), _min(0), _max(10)
+ParamInt::ParamInt (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
+        Parameter(name, guitext, desc, scope, ext), _value(0), _min(0), _max(10)
 {
     const char * defaultval = NULL;
     if (sp_repr_children(xml) != NULL)
@@ -130,10 +130,10 @@ private:
     float _min;
     float _max;
 public:
-    ParamFloat (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
+    ParamFloat (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
     /** \brief  Returns \c _value */
-    float get (const Inkscape::XML::Document * doc) { return _value; }
-    float set (float in, Inkscape::XML::Document * doc);
+    float get (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node) { return _value; }
+    float set (float in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node);
     float max (void) { return _max; }
     float min (void) { return _min; }
     Gtk::Widget * get_widget(void);
@@ -141,8 +141,8 @@ public:
 };
 
 /** \brief  Use the superclass' allocator and set the \c _value */
-ParamFloat::ParamFloat (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
-        Parameter(name, guitext, ext), _value(0.0), _min(0.0), _max(10.0)
+ParamFloat::ParamFloat (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
+        Parameter(name, guitext, desc, scope, ext), _value(0.0), _min(0.0), _max(10.0)
 {
     const char * defaultval = NULL;
     if (sp_repr_children(xml) != NULL)
@@ -183,11 +183,11 @@ private:
                 been allocated in memory.  And should be free'd. */
     gchar * _value;
 public:
-    ParamString(const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
+    ParamString(const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml);
     ~ParamString(void);
     /** \brief  Returns \c _value, with a \i const to protect it. */
-    const gchar * get (const Inkscape::XML::Document * doc) { return _value; }
-    const gchar * set (const gchar * in, Inkscape::XML::Document * doc);
+    const gchar * get (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node) { return _value; }
+    const gchar * set (const gchar * in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node);
     Gtk::Widget * get_widget(void);
     Glib::ustring * string (void);
 };
@@ -221,27 +221,44 @@ Parameter::make (Inkscape::XML::Node * in_repr, Inkscape::Extension::Extension *
     const char * name;
     const char * type;
     const char * guitext;
+    const char * desc;
+    const char * scope_str;
+    Parameter::_scope_t scope = Parameter::SCOPE_USER;
 
     name = in_repr->attribute("name");
     type = in_repr->attribute("type");
     guitext = in_repr->attribute("gui-text");
     if (guitext == NULL)
         guitext = in_repr->attribute("_gui-text");
+    desc = in_repr->attribute("gui-description");
+    if (desc == NULL)
+        desc = in_repr->attribute("_gui-description");
+    scope_str = in_repr->attribute("scope");
 
     /* In this case we just don't have enough information */
     if (name == NULL || type == NULL) {
         return NULL;
     }
 
+    if (scope_str != NULL) {
+        if (!strcmp(scope_str, "user")) {
+            scope = Parameter::SCOPE_USER;
+        } else if (!strcmp(scope_str, "document")) {
+            scope = Parameter::SCOPE_DOCUMENT;
+        } else if (!strcmp(scope_str, "node")) {
+            scope = Parameter::SCOPE_NODE;
+        }
+    }
+
     Parameter * param = NULL;
     if (!strcmp(type, "boolean")) {
-        param = new ParamBool(name, guitext, in_ext, in_repr);
+        param = new ParamBool(name, guitext, desc, scope, in_ext, in_repr);
     } else if (!strcmp(type, "int")) {
-        param = new ParamInt(name, guitext, in_ext, in_repr);
+        param = new ParamInt(name, guitext, desc, scope, in_ext, in_repr);
     } else if (!strcmp(type, "float")) {
-        param = new ParamFloat(name, guitext, in_ext, in_repr);
+        param = new ParamFloat(name, guitext, desc, scope, in_ext, in_repr);
     } else if (!strcmp(type, "string")) {
-        param = new ParamString(name, guitext, in_ext, in_repr);
+        param = new ParamString(name, guitext, desc, scope, in_ext, in_repr);
     }
 
     /* Note: param could equal NULL */
@@ -251,13 +268,14 @@ Parameter::make (Inkscape::XML::Node * in_repr, Inkscape::Extension::Extension *
 /** \brief  A function to set the \c _value
     \param  in   The value to set to
     \param  doc  A document that should be used to set the value.
+    \param  node The node where the value may be placed
 
     This function sets the internal value, but it also sets the value
     in the preferences structure.  To put it in the right place, \c PREF_DIR
     and \c pref_name() are used.
 */
 bool
-ParamBool::set (bool in, Inkscape::XML::Document * doc)
+ParamBool::set (bool in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     _value = in;
 
@@ -271,13 +289,14 @@ ParamBool::set (bool in, Inkscape::XML::Document * doc)
 /** \brief  A function to set the \c _value
     \param  in   The value to set to
     \param  doc  A document that should be used to set the value.
+    \param  node The node where the value may be placed
 
     This function sets the internal value, but it also sets the value
     in the preferences structure.  To put it in the right place, \c PREF_DIR
     and \c pref_name() are used.
 */
 int
-ParamInt::set (int in, Inkscape::XML::Document * doc)
+ParamInt::set (int in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     _value = in;
     if (_value > _max) _value = _max;
@@ -293,13 +312,14 @@ ParamInt::set (int in, Inkscape::XML::Document * doc)
 /** \brief  A function to set the \c _value
     \param  in   The value to set to
     \param  doc  A document that should be used to set the value.
+    \param  node The node where the value may be placed
 
     This function sets the internal value, but it also sets the value
     in the preferences structure.  To put it in the right place, \c PREF_DIR
     and \c pref_name() are used.
 */
 float
-ParamFloat::set (float in, Inkscape::XML::Document * doc)
+ParamFloat::set (float in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     _value = in;
     if (_value > _max) _value = _max;
@@ -315,6 +335,7 @@ ParamFloat::set (float in, Inkscape::XML::Document * doc)
 /** \brief  A function to set the \c _value
     \param  in   The value to set to
     \param  doc  A document that should be used to set the value.
+    \param  node The node where the value may be placed
 
     This function sets the internal value, but it also sets the value
     in the preferences structure.  To put it in the right place, \c PREF_DIR
@@ -325,7 +346,7 @@ ParamFloat::set (float in, Inkscape::XML::Document * doc)
     the passed in value is duplicated using \c g_strdup().
 */
 const gchar *
-ParamString::set (const gchar * in, Inkscape::XML::Document * doc)
+ParamString::set (const gchar * in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     if (in == NULL) return NULL; /* Can't have NULL string */
 
@@ -342,95 +363,95 @@ ParamString::set (const gchar * in, Inkscape::XML::Document * doc)
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 bool
-Parameter::get_bool (const Inkscape::XML::Document * doc)
+Parameter::get_bool (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node)
 {
     ParamBool * boolpntr;
     boolpntr = dynamic_cast<ParamBool *>(this);
     if (boolpntr == NULL)
         throw Extension::param_wrong_type();
-    return boolpntr->get(doc);
+    return boolpntr->get(doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 int
-Parameter::get_int (const Inkscape::XML::Document * doc)
+Parameter::get_int (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node)
 {
     ParamInt * intpntr;
     intpntr = dynamic_cast<ParamInt *>(this);
     if (intpntr == NULL)
         throw Extension::param_wrong_type();
-    return intpntr->get(doc);
+    return intpntr->get(doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 float
-Parameter::get_float (const Inkscape::XML::Document * doc)
+Parameter::get_float (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node)
 {
     ParamFloat * floatpntr;
     floatpntr = dynamic_cast<ParamFloat *>(this);
     if (floatpntr == NULL)
         throw Extension::param_wrong_type();
-    return floatpntr->get(doc);
+    return floatpntr->get(doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 const gchar *
-Parameter::get_string (const Inkscape::XML::Document * doc)
+Parameter::get_string (const Inkscape::XML::Document * doc, const Inkscape::XML::Node * node)
 {
     ParamString * stringpntr;
     stringpntr = dynamic_cast<ParamString *>(this);
     if (stringpntr == NULL)
         throw Extension::param_wrong_type();
-    return stringpntr->get(doc);
+    return stringpntr->get(doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 bool
-Parameter::set_bool (bool in, Inkscape::XML::Document * doc)
+Parameter::set_bool (bool in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     ParamBool * boolpntr;
     boolpntr = dynamic_cast<ParamBool *>(this);
     if (boolpntr == NULL)
         throw Extension::param_wrong_type();
-    return boolpntr->set(in, doc);
+    return boolpntr->set(in, doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 int
-Parameter::set_int (int in, Inkscape::XML::Document * doc)
+Parameter::set_int (int in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     ParamInt * intpntr;
     intpntr = dynamic_cast<ParamInt *>(this);
     if (intpntr == NULL)
         throw Extension::param_wrong_type();
-    return intpntr->set(in, doc);
+    return intpntr->set(in, doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 float
-Parameter::set_float (float in, Inkscape::XML::Document * doc)
+Parameter::set_float (float in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     ParamFloat * floatpntr;
     floatpntr = dynamic_cast<ParamFloat *>(this);
     if (floatpntr == NULL)
         throw Extension::param_wrong_type();
-    return floatpntr->set(in, doc);
+    return floatpntr->set(in, doc, node);
 }
 
 /** \brief  Wrapper to cast to the object and use it's function.  */
 const gchar *
-Parameter::set_string (const gchar * in, Inkscape::XML::Document * doc)
+Parameter::set_string (const gchar * in, Inkscape::XML::Document * doc, Inkscape::XML::Node * node)
 {
     ParamString * stringpntr;
     stringpntr = dynamic_cast<ParamString *>(this);
     if (stringpntr == NULL)
         throw Extension::param_wrong_type();
-    return stringpntr->set(in, doc);
+    return stringpntr->set(in, doc, node);
 }
 
 /** \brief  Initialize the object, to do that, copy the data. */
-ParamString::ParamString (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
-    Parameter(name, guitext, ext), _value(NULL)
+ParamString::ParamString (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
+    Parameter(name, guitext, desc, scope, ext), _value(NULL)
 {
     const char * defaultval = NULL;
     if (sp_repr_children(xml) != NULL)
@@ -455,14 +476,23 @@ ParamString::~ParamString(void)
 }
 
 /** \brief  Oop, now that we need a parameter, we need it's name.  */
-Parameter::Parameter (const gchar * name, const gchar * guitext, Inkscape::Extension::Extension * ext) :
-    extension(ext), _name(NULL), _text(NULL)
-{
-    _name = g_strdup(name);
+Parameter::Parameter (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext) :
+    extension(ext), _name(NULL), _desc(NULL), _scope(scope), _text(NULL)
+{
+    if (name != NULL)
+        _name = g_strdup(name);
+    if (desc != NULL) {
+        _desc = g_strdup(desc);
+        // printf("Adding description: '%s' on '%s'\n", _desc, _name);
+    }
+
+
     if (guitext != NULL)
         _text = g_strdup(guitext);
     else
         _text = g_strdup(name);
+
+    return;
 }
 
 /** \brief  Just free the allocated name. */
@@ -504,7 +534,7 @@ public:
                 describing the parameter. */
     ParamFloatAdjustment (ParamFloat * param) :
             Gtk::Adjustment(0.0, param->min(), param->max(), 0.1), _pref(param) {
-        this->set_value(_pref->get(NULL) /* \todo fix */);
+        this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
         this->signal_value_changed().connect(sigc::mem_fun(this, &ParamFloatAdjustment::val_changed));
         return;
     };
@@ -522,7 +552,7 @@ void
 ParamFloatAdjustment::val_changed (void)
 {
     // std::cout << "Value Changed to: " << this->get_value() << std::endl;
-    _pref->set(this->get_value(), NULL /* \todo fix */);
+    _pref->set(this->get_value(), NULL /* \todo fix */, NULL);
     return;
 }
 
@@ -535,7 +565,7 @@ public:
                 describing the parameter. */
     ParamIntAdjustment (ParamInt * param) :
             Gtk::Adjustment(0.0, param->min(), param->max(), 1.0), _pref(param) {
-        this->set_value(_pref->get(NULL) /* \todo fix */);
+        this->set_value(_pref->get(NULL, NULL) /* \todo fix */);
         this->signal_value_changed().connect(sigc::mem_fun(this, &ParamIntAdjustment::val_changed));
         return;
     };
@@ -553,7 +583,7 @@ void
 ParamIntAdjustment::val_changed (void)
 {
     // std::cout << "Value Changed to: " << this->get_value() << std::endl;
-    _pref->set((int)this->get_value(), NULL /* \todo fix */);
+    _pref->set((int)this->get_value(), NULL /* \todo fix */, NULL);
     return;
 }
 
@@ -565,14 +595,14 @@ ParamIntAdjustment::val_changed (void)
 Gtk::Widget *
 ParamFloat::get_widget (void)
 {
-    Gtk::HBox * hbox = new Gtk::HBox();
+    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
 
-    Gtk::Label * label = new Gtk::Label(_(_text), Gtk::ALIGN_LEFT);
+    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
     label->show();
     hbox->pack_start(*label, true, true);
 
     ParamFloatAdjustment * fadjust = new ParamFloatAdjustment(this);
-    Gtk::SpinButton * spin = new Gtk::SpinButton(*fadjust, 0.1, 1);
+    Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 0.1, 1));
     spin->show();
     hbox->pack_start(*spin, false, false);
 
@@ -589,14 +619,14 @@ ParamFloat::get_widget (void)
 Gtk::Widget *
 ParamInt::get_widget (void)
 {
-    Gtk::HBox * hbox = new Gtk::HBox();
+    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
 
-    Gtk::Label * label = new Gtk::Label(_(_text), Gtk::ALIGN_LEFT);
+    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
     label->show();
     hbox->pack_start(*label, true, true);
 
     ParamIntAdjustment * fadjust = new ParamIntAdjustment(this);
-    Gtk::SpinButton * spin = new Gtk::SpinButton(*fadjust, 1.0, 0);
+    Gtk::SpinButton * spin = Gtk::manage(new Gtk::SpinButton(*fadjust, 1.0, 0));
     spin->show();
     hbox->pack_start(*spin, false, false);
 
@@ -621,7 +651,7 @@ public:
     */
     ParamBoolCheckButton (ParamBool * param) :
             Gtk::CheckButton(), _pref(param) {
-        this->set_active(_pref->get(NULL) /**\todo fix */);
+        this->set_active(_pref->get(NULL, NULL) /**\todo fix */);
         this->signal_toggled().connect(sigc::mem_fun(this, &ParamBoolCheckButton::on_toggle));
         return;
     }
@@ -636,7 +666,7 @@ public:
 void
 ParamBoolCheckButton::on_toggle (void)
 {
-    _pref->set(this->get_active(), NULL /**\todo fix this */);
+    _pref->set(this->get_active(), NULL /**\todo fix this */, NULL);
     return;
 }
 
@@ -648,9 +678,9 @@ ParamBoolCheckButton::on_toggle (void)
 Gtk::Widget *
 ParamBool::get_widget (void)
 {
-    Gtk::HBox * hbox = new Gtk::HBox();
+    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
 
-    Gtk::Label * label = new Gtk::Label(_(_text), Gtk::ALIGN_LEFT);
+    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
     label->show();
     hbox->pack_start(*label, true, true);
 
@@ -674,8 +704,8 @@ public:
     */
     ParamStringEntry (ParamString * pref) :
         Gtk::Entry(), _pref(pref) {
-        if (_pref->get(NULL) != NULL)
-            this->set_text(Glib::ustring(_pref->get(NULL)));
+        if (_pref->get(NULL, NULL) != NULL)
+            this->set_text(Glib::ustring(_pref->get(NULL, NULL)));
         this->signal_changed().connect(sigc::mem_fun(this, &ParamStringEntry::changed_text));
     };
     void changed_text (void);
@@ -690,7 +720,7 @@ void
 ParamStringEntry::changed_text (void)
 {
     Glib::ustring data = this->get_text();
-    _pref->set(data.c_str(), NULL);
+    _pref->set(data.c_str(), NULL, NULL);
     return;
 }
 
@@ -702,9 +732,9 @@ ParamStringEntry::changed_text (void)
 Gtk::Widget *
 ParamString::get_widget (void)
 {
-    Gtk::HBox * hbox = new Gtk::HBox();
+    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
 
-    Gtk::Label * label = new Gtk::Label(_(_text), Gtk::ALIGN_LEFT);
+    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT));
     label->show();
     hbox->pack_start(*label, true, true);