[gimp/metadata-browser] app: implement gimp_get_mod_string() using gtk_accelerator_get_label()



commit c6c45d46f9fa37661ff5f1d63721a17505ce8b97
Author: Michael Natterer <mitch gimp org>
Date:   Wed Oct 5 01:28:45 2011 +0200

    app: implement gimp_get_mod_string() using gtk_accelerator_get_label()
    
    so it can handle all modifiers (also Mac ones).
    Remove gimp_get_mod_name_shift,control,alt().

 app/tools/gimpsourcetool.c      |    2 +-
 app/widgets/gimpwidgets-utils.c |  136 ++++++++++-----------------------------
 app/widgets/gimpwidgets-utils.h |    3 -
 3 files changed, 34 insertions(+), 107 deletions(-)
---
diff --git a/app/tools/gimpsourcetool.c b/app/tools/gimpsourcetool.c
index ddaa923..70b7ad4 100644
--- a/app/tools/gimpsourcetool.c
+++ b/app/tools/gimpsourcetool.c
@@ -320,7 +320,7 @@ gimp_source_tool_oper_update (GimpTool         *tool,
           else
             {
               gimp_tool_replace_status (tool, display, "%s%s%s",
-                                        gimp_get_mod_name_control (),
+                                        gimp_get_mod_string (GDK_CONTROL_MASK),
                                         gimp_get_mod_separator (),
                                         source_tool->status_set_source);
             }
diff --git a/app/widgets/gimpwidgets-utils.c b/app/widgets/gimpwidgets-utils.c
index 647ec5a..81db2b9 100644
--- a/app/widgets/gimpwidgets-utils.c
+++ b/app/widgets/gimpwidgets-utils.c
@@ -456,57 +456,6 @@ gimp_preview_tab_style_to_icon (GimpTabStyle tab_style)
 }
 
 const gchar *
-gimp_get_mod_name_shift (void)
-{
-  static gchar *mod_name_shift = NULL;
-
-  if (! mod_name_shift)
-    {
-      GtkAccelLabelClass *accel_label_class;
-
-      accel_label_class = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
-      mod_name_shift = g_strdup (accel_label_class->mod_name_shift);
-      g_type_class_unref (accel_label_class);
-    }
-
-  return (const gchar *) mod_name_shift;
-}
-
-const gchar *
-gimp_get_mod_name_control (void)
-{
-  static gchar *mod_name_control = NULL;
-
-  if (! mod_name_control)
-    {
-      GtkAccelLabelClass *accel_label_class;
-
-      accel_label_class = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
-      mod_name_control = g_strdup (accel_label_class->mod_name_control);
-      g_type_class_unref (accel_label_class);
-    }
-
-  return (const gchar *) mod_name_control;
-}
-
-const gchar *
-gimp_get_mod_name_alt (void)
-{
-  static gchar *mod_name_alt = NULL;
-
-  if (! mod_name_alt)
-    {
-      GtkAccelLabelClass *accel_label_class;
-
-      accel_label_class = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
-      mod_name_alt = g_strdup (accel_label_class->mod_name_alt);
-      g_type_class_unref (accel_label_class);
-    }
-
-  return (const gchar *) mod_name_alt;
-}
-
-const gchar *
 gimp_get_mod_separator (void)
 {
   static gchar *mod_separator = NULL;
@@ -526,61 +475,42 @@ gimp_get_mod_separator (void)
 const gchar *
 gimp_get_mod_string (GdkModifierType modifiers)
 {
-  static struct
-  {
-    GdkModifierType  modifiers;
-    gchar           *name;
-  }
-  modifier_strings[] =
-  {
-    { GDK_SHIFT_MASK,                                      NULL },
-    { GDK_CONTROL_MASK,                                    NULL },
-    { GDK_MOD1_MASK,                                       NULL },
-    { GDK_SHIFT_MASK   | GDK_CONTROL_MASK,                 NULL },
-    { GDK_SHIFT_MASK   | GDK_MOD1_MASK,                    NULL },
-    { GDK_CONTROL_MASK | GDK_MOD1_MASK,                    NULL },
-    { GDK_SHIFT_MASK   | GDK_CONTROL_MASK | GDK_MOD1_MASK, NULL }
-  };
-
-  gint i;
-
-  for (i = 0; i < G_N_ELEMENTS (modifier_strings); i++)
-    {
-      if (modifiers == modifier_strings[i].modifiers)
-        {
-          if (! modifier_strings[i].name)
-            {
-              GString *str = g_string_new (NULL);
+  static GHashTable *mod_labels;
 
-              if (modifiers & GDK_SHIFT_MASK)
-                {
-                  g_string_append (str, gimp_get_mod_name_shift ());
-                }
+  GtkAccelLabelClass *accel_label_class;
+  gchar              *label;
 
-              if (modifiers & GDK_CONTROL_MASK)
-                {
-                  if (str->len)
-                    g_string_append (str, gimp_get_mod_separator ());
+  if (! modifiers)
+    return NULL;
 
-                  g_string_append (str, gimp_get_mod_name_control ());
-                }
+  if (G_UNLIKELY (! mod_labels))
+    mod_labels = g_hash_table_new (g_int_hash, g_int_equal);
 
-              if (modifiers & GDK_MOD1_MASK)
-                {
-                  if (str->len)
-                    g_string_append (str, gimp_get_mod_separator ());
+  label = g_hash_table_lookup (mod_labels, &modifiers);
 
-                  g_string_append (str, gimp_get_mod_name_alt ());
-                }
+  if (label)
+    return label;
 
-              modifier_strings[i].name = g_string_free (str, FALSE);
-            }
+  label = gtk_accelerator_get_label (0, modifiers);
 
-          return modifier_strings[i].name;
-        }
+  accel_label_class = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
+
+  if (accel_label_class->mod_separator &&
+      strlen (accel_label_class->mod_separator))
+    {
+      gchar *sep = g_strrstr (label, accel_label_class->mod_separator);
+
+      if (sep - label == strlen (label) - strlen (accel_label_class->mod_separator))
+        *sep = '\0';
     }
 
-  return NULL;
+  g_type_class_unref (accel_label_class);
+
+  g_hash_table_insert (mod_labels,
+                       g_memdup (&modifiers, sizeof (GdkModifierType)),
+                       label);
+
+  return label;
 }
 
 #define BUF_SIZE 100
@@ -619,11 +549,11 @@ gimp_suggest_modifiers (const gchar     *message,
       if (shift_format && *shift_format)
         {
           g_snprintf (msg_buf[num_msgs], BUF_SIZE, shift_format,
-                      gimp_get_mod_name_shift ());
+                      gimp_get_mod_string (GDK_SHIFT_MASK));
         }
       else
         {
-          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_name_shift (), BUF_SIZE);
+          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_string (GDK_SHIFT_MASK), BUF_SIZE);
           try = TRUE;
         }
 
@@ -635,11 +565,11 @@ gimp_suggest_modifiers (const gchar     *message,
       if (control_format && *control_format)
         {
           g_snprintf (msg_buf[num_msgs], BUF_SIZE, control_format,
-                      gimp_get_mod_name_control ());
+                      gimp_get_mod_string (GDK_CONTROL_MASK));
         }
       else
         {
-          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_name_control (), BUF_SIZE);
+          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_string (GDK_CONTROL_MASK), BUF_SIZE);
           try = TRUE;
         }
 
@@ -651,11 +581,11 @@ gimp_suggest_modifiers (const gchar     *message,
       if (alt_format && *alt_format)
         {
           g_snprintf (msg_buf[num_msgs], BUF_SIZE, alt_format,
-                      gimp_get_mod_name_alt ());
+                      gimp_get_mod_string (GDK_MOD1_MASK));
         }
       else
         {
-          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_name_alt (), BUF_SIZE);
+          g_strlcpy (msg_buf[num_msgs], gimp_get_mod_string (GDK_MOD1_MASK), BUF_SIZE);
           try = TRUE;
         }
 
diff --git a/app/widgets/gimpwidgets-utils.h b/app/widgets/gimpwidgets-utils.h
index 8a2769c..fcfdbe2 100644
--- a/app/widgets/gimpwidgets-utils.h
+++ b/app/widgets/gimpwidgets-utils.h
@@ -50,9 +50,6 @@ GtkIconSize       gimp_get_icon_size               (GtkWidget            *widget
                                                     gint                  width,
                                                     gint                  height);
 GimpTabStyle      gimp_preview_tab_style_to_icon   (GimpTabStyle          tab_style);
-const gchar     * gimp_get_mod_name_shift          (void);
-const gchar     * gimp_get_mod_name_control        (void);
-const gchar     * gimp_get_mod_name_alt            (void);
 const gchar     * gimp_get_mod_separator           (void);
 const gchar     * gimp_get_mod_string              (GdkModifierType       modifiers);
 gchar           * gimp_suggest_modifiers           (const gchar          *message,



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