Code

Cleaned up DOS line ends that had snuck in.
[inkscape.git] / src / extension / paramradiobutton.cpp
index 794abed94ac6e30157a4eb9f8d7696551e489fd2..3ca051f14733dd30974b8222f407385b3d4be5a7 100644 (file)
-/** \file\r
- * extension parameter for radiobuttons.\r
- *\r
- * It uses a Gtk:ComboBoxText widget in the extension UI.\r
- */\r
-\r
-/*\r
- * Author:\r
- *   Johan Engelen <johan@shouraizou.nl>\r
- *\r
- * Copyright (C) 2006-2007 Johan Engelen\r
- *\r
- * Released under GNU GPL, read the file 'COPYING' for more information\r
- */\r
-\r
-#ifdef HAVE_CONFIG_H\r
-# include "config.h"\r
-#endif\r
-\r
-\r
-#include <gtkmm/box.h>\r
-#include <gtkmm/radiobutton.h>\r
-#include <gtkmm/radiobuttongroup.h>\r
-#include <gtkmm/tooltips.h>\r
-#include <gtkmm/label.h>\r
-\r
-#include <glibmm/i18n.h>\r
-\r
-#include <xml/node.h>\r
-\r
-#include "extension.h"\r
-#include "prefs-utils.h"\r
-#include "document-private.h"\r
-#include "sp-object.h"\r
-\r
-#include "paramradiobutton.h"\r
-\r
-/** \brief  The root directory in the preferences database for extension\r
-            related parameters. */\r
-#define PREF_DIR "extensions"\r
-\r
-namespace Inkscape {\r
-namespace Extension {\r
-\r
-/* For internal use only.\r
-     Note that value and guitext MUST be non-NULL. This is ensured by newing only at one location in the code where non-NULL checks are made. */\r
-class optionentry {\r
-public:\r
-    optionentry (Glib::ustring * val, Glib::ustring * text) {\r
-        value = val;\r
-        guitext = text;\r
-    }\r
-    ~optionentry() {\r
-        delete value;\r
-        delete guitext;\r
-    }\r
-\r
-    Glib::ustring * value;\r
-    Glib::ustring * guitext;\r
-};\r
-\r
-ParamRadioButton::ParamRadioButton (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :\r
-    Parameter(name, guitext, desc, scope, ext)\r
-{\r
-    choices = NULL;\r
-    _value = NULL;\r
-\r
-    // Read XML tree to add enumeration items:\r
-    // printf("Extension Constructor: ");\r
-    if (xml != NULL) {\r
-        Inkscape::XML::Node *child_repr = sp_repr_children(xml);\r
-        while (child_repr != NULL) {\r
-            char const * chname = child_repr->name();\r
-            if (!strcmp(chname, "option")) {\r
-                Glib::ustring * newguitext = NULL;\r
-                Glib::ustring * newvalue = NULL;\r
-                const char * contents = sp_repr_children(child_repr)->content();\r
-                if (contents != NULL)\r
-                     newguitext = new Glib::ustring(contents);\r
-                const char * val = child_repr->attribute("value");\r
-                if (val != NULL)\r
-                    newvalue = new Glib::ustring(val);\r
-                if ( (newguitext) && (newvalue) ) {\r
-                    choices = g_slist_append( choices, new optionentry(newvalue, newguitext) );\r
-                }\r
-            }\r
-            child_repr = sp_repr_next(child_repr);\r
-        }\r
-    }\r
-\r
-    // Initialize _value with the default value from xml\r
-    // for simplicity : default to the contents of the first xml-child\r
-    const char * defaultval = NULL;\r
-    if (sp_repr_children(sp_repr_children(xml)) != NULL)\r
-        defaultval = sp_repr_children(xml)->attribute("value");\r
-\r
-        gchar * pref_name = this->pref_name();\r
-    const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);\r
-    g_free(pref_name);\r
-\r
-    if (paramval != NULL)\r
-        defaultval = paramval;\r
-    if (defaultval != NULL)\r
-        _value = g_strdup(defaultval);  // allocate space for _value\r
-\r
-    return;\r
-}\r
-\r
-ParamRadioButton::~ParamRadioButton (void)\r
-{\r
-    //destroy choice strings\r
-    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
-        delete (reinterpret_cast<optionentry *>(list->data));\r
-    }\r
-    g_slist_free(choices);\r
-\r
-    g_free(_value);\r
-}\r
-\r
-\r
-/** \brief  A function to set the \c _value\r
-    \param  in   The value to set\r
-    \param  doc  A document that should be used to set the value.\r
-    \param  node The node where the value may be placed\r
-\r
-    This function sets ONLY the internal value, but it also sets the value\r
-    in the preferences structure.  To put it in the right place, \c PREF_DIR\r
-    and \c pref_name() are used.\r
-\r
-    To copy the data into _value the old memory must be free'd first.\r
-    It is important to note that \c g_free handles \c NULL just fine.  Then\r
-    the passed in value is duplicated using \c g_strdup().\r
-*/\r
-const gchar *\r
-ParamRadioButton::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * node)\r
-{\r
-    if (in == NULL) return NULL; /* Can't have NULL string */\r
-\r
-    Glib::ustring * settext = NULL;\r
-    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
-        optionentry * entr = reinterpret_cast<optionentry *>(list->data);\r
-        if ( !entr->guitext->compare(in) ) {\r
-            settext = entr->value;\r
-            break;  // break out of for loop\r
-        }\r
-    }\r
-    if (settext) {\r
-        if (_value != NULL) g_free(_value);\r
-        _value = g_strdup(settext->c_str());\r
-        gchar * prefname = this->pref_name();\r
-        prefs_set_string_attribute(PREF_DIR, prefname, _value);\r
-        g_free(prefname);\r
-    }\r
-\r
-    return _value;\r
-}\r
-\r
-\r
-/**\r
-    \brief  A function to get the current value of the parameter in a string form\r
-    \return A string with the 'value' as command line argument\r
-*/\r
-Glib::ustring *\r
-ParamRadioButton::string (void)\r
-{\r
-    Glib::ustring * param_string = new Glib::ustring("");\r
-\r
-    *param_string += "\"";\r
-    *param_string += _value;\r
-    *param_string += "\"";\r
-\r
-    return param_string;\r
-}\r
-\r
-/** \brief  A special radiobutton class to use in ParamRadioButton */\r
-class ParamRadioButtonWdg : public Gtk::RadioButton {\r
-private:\r
-    ParamRadioButton * _pref;\r
-    SPDocument * _doc;\r
-    Inkscape::XML::Node * _node;\r
-public:\r
-    /** \brief  Build a string preference for the given parameter\r
-        \param  pref  Where to put the radiobutton's string when it is selected.\r
-    */\r
-    ParamRadioButtonWdg ( Gtk::RadioButtonGroup& group, const Glib::ustring& label,\r
-                          ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :\r
-        Gtk::RadioButton(group, label), _pref(pref), _doc(doc), _node(node) {\r
-        add_changesignal();\r
-    };\r
-    ParamRadioButtonWdg ( const Glib::ustring& label,\r
-                          ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :\r
-        Gtk::RadioButton(label), _pref(pref), _doc(doc), _node(node) {\r
-        add_changesignal();\r
-    };\r
-    void add_changesignal() {\r
-        this->signal_toggled().connect(sigc::mem_fun(this, &ParamRadioButtonWdg::changed));\r
-    };\r
-    void changed (void);\r
-};\r
-\r
-/** \brief  Respond to the selected radiobutton changing\r
-\r
-    This function responds to the radiobutton selection changing by grabbing the value\r
-    from the text box and putting it in the parameter.\r
-*/\r
-void\r
-ParamRadioButtonWdg::changed (void)\r
-{\r
-    if (this->get_active()) {\r
-        Glib::ustring data = this->get_label();\r
-        _pref->set(data.c_str(), _doc, _node);\r
-    }\r
-}\r
-\r
-\r
-\r
-/**\r
-    \brief  Creates a combobox widget for an enumeration parameter\r
-*/\r
-Gtk::Widget *\r
-ParamRadioButton::get_widget (SPDocument * doc, Inkscape::XML::Node * node)\r
-{\r
-    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));\r
-    Gtk::VBox * vbox = Gtk::manage(new Gtk::VBox(false, 0));\r
-\r
-    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP));\r
-    label->show();\r
-    hbox->pack_start(*label, false, false);\r
-\r
-    // add choice strings as radiobuttons\r
-    // and select last selected option (_value)\r
-    bool first = true;\r
-    ParamRadioButtonWdg * radio;\r
-    Gtk::RadioButtonGroup group;\r
-    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
-        optionentry * entr = reinterpret_cast<optionentry *>(list->data);\r
-        Glib::ustring * text = entr->guitext;\r
-        if (first) {\r
-            radio = Gtk::manage(new ParamRadioButtonWdg(*text, this, doc, node));\r
-            group = radio->get_group();\r
-            first = false;\r
-        } else {\r
-            radio = Gtk::manage(new ParamRadioButtonWdg(group, *text, this, doc, node));\r
-        }\r
-        radio->show();\r
-        vbox->pack_start(*radio, true, true);\r
-        if (!entr->value->compare(_value)) {\r
-            radio->set_active();\r
-        }\r
-    }\r
-\r
-    vbox->show();\r
-    hbox->pack_end(*vbox, false, false);\r
-    hbox->show();\r
-\r
-\r
-    return dynamic_cast<Gtk::Widget *>(hbox);\r
-}\r
-\r
-\r
-}  /* namespace Extension */\r
-}  /* namespace Inkscape */\r
-\r
+/** \file
+ * extension parameter for radiobuttons.
+ *
+ * It uses a Gtk:ComboBoxText widget in the extension UI.
+ */
+
+/*
+ * Author:
+ *   Johan Engelen <johan@shouraizou.nl>
+ *
+ * Copyright (C) 2006-2007 Johan Engelen
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+
+#include <gtkmm/box.h>
+#include <gtkmm/radiobutton.h>
+#include <gtkmm/radiobuttongroup.h>
+#include <gtkmm/tooltips.h>
+#include <gtkmm/label.h>
+
+#include <glibmm/i18n.h>
+
+#include <xml/node.h>
+
+#include "extension.h"
+#include "prefs-utils.h"
+#include "document-private.h"
+#include "sp-object.h"
+
+#include "paramradiobutton.h"
+
+/** \brief  The root directory in the preferences database for extension
+            related parameters. */
+#define PREF_DIR "extensions"
+
+namespace Inkscape {
+namespace Extension {
+
+/* For internal use only.
+     Note that value and guitext MUST be non-NULL. This is ensured by newing only at one location in the code where non-NULL checks are made. */
+class optionentry {
+public:
+    optionentry (Glib::ustring * val, Glib::ustring * text) {
+        value = val;
+        guitext = text;
+    }
+    ~optionentry() {
+        delete value;
+        delete guitext;
+    }
+
+    Glib::ustring * value;
+    Glib::ustring * guitext;
+};
+
+ParamRadioButton::ParamRadioButton (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)
+{
+    choices = NULL;
+    _value = NULL;
+
+    // Read XML tree to add enumeration items:
+    // printf("Extension Constructor: ");
+    if (xml != NULL) {
+        Inkscape::XML::Node *child_repr = sp_repr_children(xml);
+        while (child_repr != NULL) {
+            char const * chname = child_repr->name();
+            if (!strcmp(chname, "option")) {
+                Glib::ustring * newguitext = NULL;
+                Glib::ustring * newvalue = NULL;
+                const char * contents = sp_repr_children(child_repr)->content();
+                if (contents != NULL)
+                     newguitext = new Glib::ustring(contents);
+                const char * val = child_repr->attribute("value");
+                if (val != NULL)
+                    newvalue = new Glib::ustring(val);
+                if ( (newguitext) && (newvalue) ) {
+                    choices = g_slist_append( choices, new optionentry(newvalue, newguitext) );
+                }
+            }
+            child_repr = sp_repr_next(child_repr);
+        }
+    }
+
+    // Initialize _value with the default value from xml
+    // for simplicity : default to the contents of the first xml-child
+    const char * defaultval = NULL;
+    if (sp_repr_children(sp_repr_children(xml)) != NULL)
+        defaultval = sp_repr_children(xml)->attribute("value");
+
+        gchar * pref_name = this->pref_name();
+    const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
+    g_free(pref_name);
+
+    if (paramval != NULL)
+        defaultval = paramval;
+    if (defaultval != NULL)
+        _value = g_strdup(defaultval);  // allocate space for _value
+
+    return;
+}
+
+ParamRadioButton::~ParamRadioButton (void)
+{
+    //destroy choice strings
+    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
+        delete (reinterpret_cast<optionentry *>(list->data));
+    }
+    g_slist_free(choices);
+
+    g_free(_value);
+}
+
+
+/** \brief  A function to set the \c _value
+    \param  in   The value to set
+    \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 ONLY 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.
+
+    To copy the data into _value the old memory must be free'd first.
+    It is important to note that \c g_free handles \c NULL just fine.  Then
+    the passed in value is duplicated using \c g_strdup().
+*/
+const gchar *
+ParamRadioButton::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * node)
+{
+    if (in == NULL) return NULL; /* Can't have NULL string */
+
+    Glib::ustring * settext = NULL;
+    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
+        optionentry * entr = reinterpret_cast<optionentry *>(list->data);
+        if ( !entr->guitext->compare(in) ) {
+            settext = entr->value;
+            break;  // break out of for loop
+        }
+    }
+    if (settext) {
+        if (_value != NULL) g_free(_value);
+        _value = g_strdup(settext->c_str());
+        gchar * prefname = this->pref_name();
+        prefs_set_string_attribute(PREF_DIR, prefname, _value);
+        g_free(prefname);
+    }
+
+    return _value;
+}
+
+
+/**
+    \brief  A function to get the current value of the parameter in a string form
+    \return A string with the 'value' as command line argument
+*/
+Glib::ustring *
+ParamRadioButton::string (void)
+{
+    Glib::ustring * param_string = new Glib::ustring("");
+
+    *param_string += "\"";
+    *param_string += _value;
+    *param_string += "\"";
+
+    return param_string;
+}
+
+/** \brief  A special radiobutton class to use in ParamRadioButton */
+class ParamRadioButtonWdg : public Gtk::RadioButton {
+private:
+    ParamRadioButton * _pref;
+    SPDocument * _doc;
+    Inkscape::XML::Node * _node;
+public:
+    /** \brief  Build a string preference for the given parameter
+        \param  pref  Where to put the radiobutton's string when it is selected.
+    */
+    ParamRadioButtonWdg ( Gtk::RadioButtonGroup& group, const Glib::ustring& label,
+                          ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :
+        Gtk::RadioButton(group, label), _pref(pref), _doc(doc), _node(node) {
+        add_changesignal();
+    };
+    ParamRadioButtonWdg ( const Glib::ustring& label,
+                          ParamRadioButton * pref, SPDocument * doc, Inkscape::XML::Node * node ) :
+        Gtk::RadioButton(label), _pref(pref), _doc(doc), _node(node) {
+        add_changesignal();
+    };
+    void add_changesignal() {
+        this->signal_toggled().connect(sigc::mem_fun(this, &ParamRadioButtonWdg::changed));
+    };
+    void changed (void);
+};
+
+/** \brief  Respond to the selected radiobutton changing
+
+    This function responds to the radiobutton selection changing by grabbing the value
+    from the text box and putting it in the parameter.
+*/
+void
+ParamRadioButtonWdg::changed (void)
+{
+    if (this->get_active()) {
+        Glib::ustring data = this->get_label();
+        _pref->set(data.c_str(), _doc, _node);
+    }
+}
+
+
+
+/**
+    \brief  Creates a combobox widget for an enumeration parameter
+*/
+Gtk::Widget *
+ParamRadioButton::get_widget (SPDocument * doc, Inkscape::XML::Node * node)
+{
+    Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
+    Gtk::VBox * vbox = Gtk::manage(new Gtk::VBox(false, 0));
+
+    Gtk::Label * label = Gtk::manage(new Gtk::Label(_(_text), Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP));
+    label->show();
+    hbox->pack_start(*label, false, false);
+
+    // add choice strings as radiobuttons
+    // and select last selected option (_value)
+    bool first = true;
+    ParamRadioButtonWdg * radio;
+    Gtk::RadioButtonGroup group;
+    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {
+        optionentry * entr = reinterpret_cast<optionentry *>(list->data);
+        Glib::ustring * text = entr->guitext;
+        if (first) {
+            radio = Gtk::manage(new ParamRadioButtonWdg(*text, this, doc, node));
+            group = radio->get_group();
+            first = false;
+        } else {
+            radio = Gtk::manage(new ParamRadioButtonWdg(group, *text, this, doc, node));
+        }
+        radio->show();
+        vbox->pack_start(*radio, true, true);
+        if (!entr->value->compare(_value)) {
+            radio->set_active();
+        }
+    }
+
+    vbox->show();
+    hbox->pack_end(*vbox, false, false);
+    hbox->show();
+
+
+    return dynamic_cast<Gtk::Widget *>(hbox);
+}
+
+
+}  /* namespace Extension */
+}  /* namespace Inkscape */
+