[gimp] Add functions to get the ISO 639-1 language code from GimpLanguageEntry



commit 5febc2e417a7ad423e55d8ee427cfcddd3387b8c
Author: Sven Neumann <sven gimp org>
Date:   Tue Aug 4 22:31:52 2009 +0200

    Add functions to get the ISO 639-1 language code from GimpLanguageEntry
    and to set the language using this code.

 app/widgets/gimplanguageentry.c |   90 ++++++++++++++++++++++++++++++++++++++-
 app/widgets/gimplanguageentry.h |   13 ++---
 app/widgets/gimplanguagestore.c |   50 +++++++++++++++++++++
 app/widgets/gimplanguagestore.h |    3 +
 app/widgets/gimppropwidgets.c   |   13 ++---
 5 files changed, 152 insertions(+), 17 deletions(-)
---
diff --git a/app/widgets/gimplanguageentry.c b/app/widgets/gimplanguageentry.c
index a534703..01d859f 100644
--- a/app/widgets/gimplanguageentry.c
+++ b/app/widgets/gimplanguageentry.c
@@ -20,6 +20,8 @@
 
 #include "config.h"
 
+#include <string.h>
+
 #include <gtk/gtk.h>
 
 #include "widgets-types.h"
@@ -34,6 +36,14 @@ enum
   PROP_MODEL
 };
 
+struct _GimpLanguageEntry
+{
+  GtkEntry       parent_instance;
+
+  GtkListStore  *store;
+  gchar         *code;  /*  ISO 639-1 language code  */
+};
+
 
 static GObject * gimp_language_entry_constructor  (GType                  type,
                                                    guint                  n_params,
@@ -49,6 +59,11 @@ static void      gimp_language_entry_get_property (GObject      *object,
                                                    GValue       *value,
                                                    GParamSpec   *pspec);
 
+static gboolean  gimp_language_entry_language_selected (GtkEntryCompletion *completion,
+                                                        GtkTreeModel       *model,
+                                                        GtkTreeIter        *iter,
+                                                        GimpLanguageEntry  *entry);
+
 
 G_DEFINE_TYPE (GimpLanguageEntry, gimp_language_entry, GTK_TYPE_ENTRY)
 
@@ -98,13 +113,17 @@ gimp_language_entry_constructor (GType                  type,
                                  NULL);
 
       /* Note that we must use this function to set the text column,
-       * otherwise we won't get a cell renderer for free
+       * otherwise we won't get a cell renderer for free.
        */
       gtk_entry_completion_set_text_column (completion,
                                             GIMP_LANGUAGE_STORE_LANGUAGE);
 
       gtk_entry_set_completion (GTK_ENTRY (entry), completion);
       g_object_unref (completion);
+
+      g_signal_connect (completion, "match-selected",
+                        G_CALLBACK (gimp_language_entry_language_selected),
+                        entry);
     }
 
   return object;
@@ -121,6 +140,12 @@ gimp_language_entry_finalize (GObject *object)
       entry->store = NULL;
     }
 
+  if (entry->code)
+    {
+      g_free (entry->code);
+      entry->code = NULL;
+    }
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -166,6 +191,21 @@ gimp_language_entry_get_property (GObject      *object,
     }
 }
 
+static gboolean
+gimp_language_entry_language_selected (GtkEntryCompletion *completion,
+                                       GtkTreeModel       *model,
+                                       GtkTreeIter        *iter,
+                                       GimpLanguageEntry  *entry)
+{
+  g_free (entry->code);
+
+  gtk_tree_model_get (model, iter,
+                      GIMP_LANGUAGE_STORE_ISO_639_1, &entry->code,
+                      -1);
+
+  return FALSE;
+}
+
 GtkWidget *
 gimp_language_entry_new (void)
 {
@@ -182,3 +222,51 @@ gimp_language_entry_new (void)
 
   return entry;
 }
+
+const gchar *
+gimp_language_entry_get_iso_code (GimpLanguageEntry *entry)
+{
+  g_return_val_if_fail (GIMP_IS_LANGUAGE_ENTRY (entry), NULL);
+
+  return entry->code;
+}
+
+gboolean
+gimp_language_entry_set_iso_code (GimpLanguageEntry *entry,
+                                  const gchar       *code)
+{
+  GtkTreeIter  iter;
+
+  g_return_val_if_fail (GIMP_IS_LANGUAGE_ENTRY (entry), FALSE);
+
+  if (entry->code)
+    {
+      g_free (entry->code);
+      entry->code = NULL;
+    }
+
+  if (! code || ! strlen (code))
+    {
+      gtk_entry_set_text (GTK_ENTRY (entry), "");
+
+      return TRUE;
+    }
+
+  if (gimp_language_store_lookup (GIMP_LANGUAGE_STORE (entry->store),
+                                  code, &iter))
+    {
+      gchar *language;
+
+      gtk_tree_model_get (GTK_TREE_MODEL (entry->store), &iter,
+                          GIMP_LANGUAGE_STORE_LANGUAGE,  &language,
+                          GIMP_LANGUAGE_STORE_ISO_639_1, &entry->code,
+                          -1);
+
+      gtk_entry_set_text (GTK_ENTRY (entry), language);
+      g_free (language);
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
diff --git a/app/widgets/gimplanguageentry.h b/app/widgets/gimplanguageentry.h
index 9dfcfa9..68e4ff6 100644
--- a/app/widgets/gimplanguageentry.h
+++ b/app/widgets/gimplanguageentry.h
@@ -37,17 +37,14 @@ struct _GimpLanguageEntryClass
   GtkEntryClass  parent_class;
 };
 
-struct _GimpLanguageEntry
-{
-  GtkEntry       parent_instance;
-
-  GtkListStore  *store;
-};
 
+GType         gimp_language_entry_get_type     (void) G_GNUC_CONST;
 
-GType       gimp_language_entry_get_type (void) G_GNUC_CONST;
+GtkWidget   * gimp_language_entry_new          (void);
 
-GtkWidget * gimp_language_entry_new      (void);
+const gchar * gimp_language_entry_get_iso_code (GimpLanguageEntry *entry);
+gboolean      gimp_language_entry_set_iso_code (GimpLanguageEntry *entry,
+                                                const gchar       *code);
 
 
 #endif  /* __GIMP_LANGUAGE_ENTRY_H__ */
diff --git a/app/widgets/gimplanguagestore.c b/app/widgets/gimplanguagestore.c
index ffbbc86..cb90ca1 100644
--- a/app/widgets/gimplanguagestore.c
+++ b/app/widgets/gimplanguagestore.c
@@ -20,6 +20,8 @@
 
 #include "config.h"
 
+#include <string.h>
+
 #include <gtk/gtk.h>
 
 #include "widgets-types.h"
@@ -69,3 +71,51 @@ gimp_language_store_add (GimpLanguageStore *store,
                       GIMP_LANGUAGE_STORE_ISO_639_1, code,
                       -1);
 }
+
+gboolean
+gimp_language_store_lookup (GimpLanguageStore *store,
+                            const gchar       *code,
+                            GtkTreeIter       *iter)
+{
+  GtkTreeModel *model;
+  const gchar  *hyphen;
+  gint          len;
+  gboolean      iter_valid;
+
+  g_return_val_if_fail (GIMP_IS_LANGUAGE_STORE (store), FALSE);
+  g_return_val_if_fail (code != NULL, FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
+
+  /*  We accept the code in RFC-3066 format here and only look at what's
+   *  before the first hyphen.
+   */
+  hyphen = strchr (code, '-');
+
+  if (hyphen)
+    len = hyphen - code;
+  else
+    len = strlen (code);
+
+  model = GTK_TREE_MODEL (store);
+
+  for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
+       iter_valid;
+       iter_valid = gtk_tree_model_iter_next (model, iter))
+    {
+      gchar *value;
+
+      gtk_tree_model_get (model, iter,
+                          GIMP_LANGUAGE_STORE_ISO_639_1, &value,
+                          -1);
+
+      if (strncmp (code, value, len) == 0)
+        {
+          g_free (value);
+          break;
+        }
+
+      g_free (value);
+    }
+
+  return iter_valid;
+}
diff --git a/app/widgets/gimplanguagestore.h b/app/widgets/gimplanguagestore.h
index b11a983..e316466 100644
--- a/app/widgets/gimplanguagestore.h
+++ b/app/widgets/gimplanguagestore.h
@@ -56,6 +56,9 @@ GtkListStore * gimp_language_store_new      (void);
 void           gimp_language_store_add      (GimpLanguageStore *store,
                                              const gchar       *lang,
                                              const gchar       *code);
+gboolean       gimp_language_store_lookup   (GimpLanguageStore *store,
+                                             const gchar       *code,
+                                             GtkTreeIter       *iter);
 
 
 #endif  /* __GIMP_LANGUAGE_STORE_H__ */
diff --git a/app/widgets/gimppropwidgets.c b/app/widgets/gimppropwidgets.c
index 4645cd9..7724b15 100644
--- a/app/widgets/gimppropwidgets.c
+++ b/app/widgets/gimppropwidgets.c
@@ -850,8 +850,7 @@ gimp_prop_language_entry_new (GObject     *config,
                 property_name, &value,
                 NULL);
 
-  // FIXME
-  gtk_entry_set_text (GTK_ENTRY (entry), value);
+  gimp_language_entry_set_iso_code (GIMP_LANGUAGE_ENTRY (entry), value);
   g_free (value);
 
   set_param_spec (G_OBJECT (entry), entry, param_spec);
@@ -872,21 +871,20 @@ gimp_prop_language_entry_callback (GtkWidget *entry,
                                    GObject   *config)
 {
   GParamSpec  *param_spec;
-  const gchar *text;
+  const gchar *code;
 
   param_spec = get_param_spec (G_OBJECT (entry));
   if (! param_spec)
     return;
 
-  // FIXME
-  text = gtk_entry_get_text (GTK_ENTRY (entry));
+  code = gimp_language_entry_get_iso_code (GIMP_LANGUAGE_ENTRY (entry));
 
   g_signal_handlers_block_by_func (config,
                                    gimp_prop_language_entry_notify,
                                    entry);
 
   g_object_set (config,
-                param_spec->name, text,
+                param_spec->name, code,
                 NULL);
 
   g_signal_handlers_unblock_by_func (config,
@@ -909,8 +907,7 @@ gimp_prop_language_entry_notify (GObject    *config,
                                    gimp_prop_language_entry_callback,
                                    config);
 
-  // FIXME
-  gtk_entry_set_text (GTK_ENTRY (entry), value ? value : "");
+  gimp_language_entry_set_iso_code (GIMP_LANGUAGE_ENTRY (entry), value);
 
   g_signal_handlers_unblock_by_func (entry,
                                      gimp_prop_language_entry_callback,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]