Code

Fixed: Non-translatable strings in "enum" type extensions
[inkscape.git] / src / extension / paramenum.cpp
index 849447427c5188bed439871dc32f5a807ee0baa0..bd8d0b14eed801e0be3fdfc1e4cba0d0739202bc 100644 (file)
@@ -1,5 +1,5 @@
 /** \file\r
- * extension parameter for enumerations. \r
+ * extension parameter for enumerations.\r
  *\r
  * It uses a Gtk:ComboBoxText widget in the extension UI.\r
  */\r
@@ -8,7 +8,7 @@
  * Author:\r
  *   Johan Engelen <johan@shouraizou.nl>\r
  *\r
- * Copyright (C) 2006 Author\r
+ * Copyright (C) 2006-2007 Johan Engelen\r
  *\r
  * Released under GNU GPL, read the file 'COPYING' for more information\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 enumentry {\r
+public:\r
+    enumentry (Glib::ustring * val, Glib::ustring * text) {\r
+        value = val;\r
+        guitext = text;\r
+    }\r
+    ~enumentry() {\r
+        delete value;\r
+        delete guitext;\r
+    }\r
+\r
+    Glib::ustring * value;\r
+    Glib::ustring * guitext;\r
+};\r
+\r
+\r
 ParamComboBox::ParamComboBox (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
+{\r
     choices = NULL;\r
     _value = NULL;\r
-    \r
+\r
     // Read XML tree to add enumeration items:\r
     // printf("Extension Constructor: ");\r
     if (xml != NULL) {\r
@@ -54,22 +72,28 @@ ParamComboBox::ParamComboBox (const gchar * name, const gchar * guitext, const g
         while (child_repr != NULL) {\r
             char const * chname = child_repr->name();\r
             if (!strcmp(chname, "item")) {\r
-                Glib::ustring * newitem = NULL;\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
-                     newitem = new Glib::ustring(contents);\r
-                if (newitem != NULL) choices = g_slist_append(choices, newitem);\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 enumentry(newvalue, newguitext) );\r
+                }\r
             }\r
             child_repr = sp_repr_next(child_repr);\r
         }\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(sp_repr_children(xml))->content();\r
-    \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
@@ -78,16 +102,15 @@ ParamComboBox::ParamComboBox (const gchar * name, const gchar * guitext, const g
         defaultval = paramval;\r
     if (defaultval != NULL)\r
         _value = g_strdup(defaultval);  // allocate space for _value\r
-        \r
+\r
     return;\r
 }\r
 \r
 ParamComboBox::~ParamComboBox (void)\r
-{                  \r
+{\r
     //destroy choice strings\r
     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
-        Glib::ustring * text = reinterpret_cast<Glib::ustring *>(list->data);\r
-        delete text;\r
+        delete (reinterpret_cast<enumentry *>(list->data));\r
     }\r
     g_slist_free(choices);\r
 \r
@@ -113,13 +136,21 @@ ParamComboBox::set (const gchar * in, SPDocument * doc, Inkscape::XML::Node * no
 {\r
     if (in == NULL) return NULL; /* Can't have NULL string */\r
 \r
-    if (_value != NULL)\r
-        g_free(_value);\r
-    _value = g_strdup(in);\r
-\r
-    gchar * prefname = this->pref_name();\r
-    prefs_set_string_attribute(PREF_DIR, prefname, _value);\r
-    g_free(prefname);\r
+    Glib::ustring * settext = NULL;\r
+    for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
+        enumentry * entr = reinterpret_cast<enumentry *>(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
@@ -187,12 +218,17 @@ ParamComboBox::get_widget (SPDocument * doc, Inkscape::XML::Node * node)
     hbox->pack_start(*label, false, false);\r
 \r
     ParamComboBoxEntry * combo = Gtk::manage(new ParamComboBoxEntry(this, doc, node));\r
-    // add choice strings:         \r
+    // add choice strings:\r
+    Glib::ustring * settext;\r
     for (GSList * list = choices; list != NULL; list = g_slist_next(list)) {\r
-        Glib::ustring * text = reinterpret_cast<Glib::ustring *>(list->data);\r
+        enumentry * entr = reinterpret_cast<enumentry *>(list->data);\r
+        Glib::ustring * text = entr->guitext;\r
         combo->append_text(*text);\r
+        if ( !entr->value->compare(_value) ) {\r
+            settext = entr->guitext;\r
+        }\r
     }\r
-    combo->set_active_text(Glib::ustring(_value));\r
+    if (settext) combo->set_active_text(*settext);\r
 \r
     combo->show();\r
     hbox->pack_start(*combo, true, true);\r