]> Pileus Git - ~andy/gtk/commitdiff
Provide access to the string representation of accelerators used in
authorMatthias Clasen <mclasen@redhat.com>
Tue, 5 Oct 2004 05:17:22 +0000 (05:17 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Tue, 5 Oct 2004 05:17:22 +0000 (05:17 +0000)
2004-10-05  Matthias Clasen  <mclasen@redhat.com>

Provide access to the string representation of accelerators used
in GtkAccelLabel.  (#154068, John Spray)

* gtk/gtkaccelgroup.h:
* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
to return the accelerator label used in GtkAccelLabel.
(gtk_accelerator_name): Update docs to point to
gtk_accelerator_get_label().
* gtk/gtkaccellabel.h:
* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
New auxiliary function which creates the string representing the
accelerator.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-6
ChangeLog.pre-2-8
gtk/gtkaccelgroup.c
gtk/gtkaccelgroup.h
gtk/gtkaccellabel.c
gtk/gtkaccellabel.h

index f5a7846040a9ff99c8d96015872e243c181d3bde..459fbc2ac918837e9a260bc178720c12a0488ddc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2004-10-05  Matthias Clasen  <mclasen@redhat.com>
+
+       Provide access to the string representation of accelerators used
+       in GtkAccelLabel.  (#154068, John Spray)
+
+       * gtk/gtkaccelgroup.h:
+       * gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
+       to return the accelerator label used in GtkAccelLabel. 
+       (gtk_accelerator_name): Update docs to point to 
+       gtk_accelerator_get_label().
+       * gtk/gtkaccellabel.h:
+       * gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label): 
+       New auxiliary function which creates the string representing the
+       accelerator.
+       
 2004-10-05  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the
index f5a7846040a9ff99c8d96015872e243c181d3bde..459fbc2ac918837e9a260bc178720c12a0488ddc 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-05  Matthias Clasen  <mclasen@redhat.com>
+
+       Provide access to the string representation of accelerators used
+       in GtkAccelLabel.  (#154068, John Spray)
+
+       * gtk/gtkaccelgroup.h:
+       * gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
+       to return the accelerator label used in GtkAccelLabel. 
+       (gtk_accelerator_name): Update docs to point to 
+       gtk_accelerator_get_label().
+       * gtk/gtkaccellabel.h:
+       * gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label): 
+       New auxiliary function which creates the string representing the
+       accelerator.
+       
 2004-10-05  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the
index f5a7846040a9ff99c8d96015872e243c181d3bde..459fbc2ac918837e9a260bc178720c12a0488ddc 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-05  Matthias Clasen  <mclasen@redhat.com>
+
+       Provide access to the string representation of accelerators used
+       in GtkAccelLabel.  (#154068, John Spray)
+
+       * gtk/gtkaccelgroup.h:
+       * gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
+       to return the accelerator label used in GtkAccelLabel. 
+       (gtk_accelerator_name): Update docs to point to 
+       gtk_accelerator_get_label().
+       * gtk/gtkaccellabel.h:
+       * gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label): 
+       New auxiliary function which creates the string representing the
+       accelerator.
+       
 2004-10-05  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the
index f5a7846040a9ff99c8d96015872e243c181d3bde..459fbc2ac918837e9a260bc178720c12a0488ddc 100644 (file)
@@ -1,3 +1,18 @@
+2004-10-05  Matthias Clasen  <mclasen@redhat.com>
+
+       Provide access to the string representation of accelerators used
+       in GtkAccelLabel.  (#154068, John Spray)
+
+       * gtk/gtkaccelgroup.h:
+       * gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
+       to return the accelerator label used in GtkAccelLabel. 
+       (gtk_accelerator_name): Update docs to point to 
+       gtk_accelerator_get_label().
+       * gtk/gtkaccellabel.h:
+       * gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label): 
+       New auxiliary function which creates the string representing the
+       accelerator.
+       
 2004-10-05  Matthias Clasen  <mclasen@redhat.com>
 
        * gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the
index e9607b767097299ad49e7a50aba1e5acbe6932e6..038d73179ebaf458862336ad03f672d83f0d639b 100644 (file)
@@ -30,7 +30,9 @@
 
 #include "gtkalias.h"
 #include "gtkaccelgroup.h"
+#include "gtkaccellabel.h" /* For _gtk_accel_label_class_get_accelerator_label */
 #include "gtkaccelmap.h"
+#include "gtkintl.h"
 #include "gtkmain.h"           /* For _gtk_boolean_handled_accumulator */
 #include "gdk/gdkkeysyms.h"
 #include "gtkmarshalers.h"
@@ -1056,14 +1058,16 @@ gtk_accelerator_parse (const gchar     *accelerator,
  * gtk_accelerator_name:
  * @accelerator_key:  accelerator keyval
  * @accelerator_mods: accelerator modifier mask
- * @returns:          a newly-allocated accelerator name
  * 
  * Converts an accelerator keyval and modifier mask
  * into a string parseable by gtk_accelerator_parse().
  * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
  * this function returns "&lt;Control&gt;q". 
  *
- * The caller of this function must free the returned string.
+ * If you need to display accelerators in the user interface,
+ * see gtk_accelerator_get_label().
+ *
+ * Returns: a newly-allocated accelerator name
  */
 gchar*
 gtk_accelerator_name (guint           accelerator_key,
@@ -1155,6 +1159,47 @@ gtk_accelerator_name (guint           accelerator_key,
   return accelerator;
 }
 
+/* Underscores in key names are better displayed as spaces
+ * E.g., Page_Up should be "Page Up"
+ */
+static void
+substitute_underscores (char *str)
+{
+  char *p;
+
+  for (p = str; *p; p++)
+    if (*p == '_')
+      *p = ' ';
+}
+
+/**
+ * gtk_accelerator_get_label:
+ * @accelerator_key:  accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ * 
+ * Converts an accelerator keyval and modifier mask into a string 
+ * which can be used to represent the accelerator to the user. 
+ *
+ * Returns: a newly-allocated string representing the accelerator.
+ *
+ * Since: 2.6
+ */
+gchar*
+gtk_accelerator_get_label (guint           accelerator_key,
+                          GdkModifierType accelerator_mods)
+{
+  GtkAccelLabelClass *klass;
+  gchar *label;
+
+  klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
+  label = _gtk_accel_label_class_get_accelerator_label (klass, 
+                                                       accelerator_key, 
+                                                       accelerator_mods);
+  g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
+
+  return label;
+}  
+
 /**
  * gtk_accelerator_set_default_mod_mask:
  * @default_mod_mask: accelerator modifier mask
index b4f1b032e766fad68110deb0c127967ad5190e87..a9adf4af5b36fb551254964e58dfb8d89016a7c0 100644 (file)
@@ -148,6 +148,8 @@ void         gtk_accelerator_parse                (const gchar     *accelerator,
                                               GdkModifierType *accelerator_mods);
 gchar*  gtk_accelerator_name                 (guint            accelerator_key,
                                               GdkModifierType  accelerator_mods);
+gchar*  gtk_accelerator_display_name         (guint            accelerator_key,
+                                              GdkModifierType  accelerator_mods);
 void    gtk_accelerator_set_default_mod_mask (GdkModifierType  default_mod_mask);
 guint   gtk_accelerator_get_default_mod_mask (void);
 
index 8392552b73accd9f969c1c9c96682d06e623ccb6..73b27e52a5f08d605af3a5cf6ae88088d92d5014 100644 (file)
@@ -528,6 +528,71 @@ substitute_underscores (char *str)
       *p = ' ';
 }
 
+gchar *
+_gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
+                                             guint               accelerator_key,
+                                             GdkModifierType     accelerator_mods)
+{
+  GString *gstring;
+  gboolean seen_mod = FALSE;
+  gunichar ch;
+  
+  gstring = g_string_new ("");
+  
+  if (accelerator_mods & GDK_SHIFT_MASK)
+    {
+      g_string_append (gstring, klass->mod_name_shift);
+      seen_mod = TRUE;
+    }
+  if (accelerator_mods & GDK_CONTROL_MASK)
+    {
+      if (seen_mod)
+       g_string_append (gstring, klass->mod_separator);
+      g_string_append (gstring, klass->mod_name_control);
+      seen_mod = TRUE;
+    }
+  if (accelerator_mods & GDK_MOD1_MASK)
+    {
+      if (seen_mod)
+       g_string_append (gstring, klass->mod_separator);
+      g_string_append (gstring, klass->mod_name_alt);
+      seen_mod = TRUE;
+    }
+  if (seen_mod)
+    g_string_append (gstring, klass->mod_separator);
+  
+  ch = gdk_keyval_to_unicode (accelerator_key);
+  if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
+      (ch < 0x80 || klass->latin1_to_char))
+    {
+      switch (ch)
+       {
+       case ' ':
+         g_string_append (gstring, "Space");
+         break;
+       case '\\':
+         g_string_append (gstring, "Backslash");
+         break;
+       default:
+         g_string_append_unichar (gstring, g_unichar_toupper (ch));
+         break;
+       }
+    }
+  else
+    {
+      gchar *tmp;
+      
+      tmp = gtk_accelerator_name (accelerator_key, 0);
+      if (tmp[0] != 0 && tmp[1] == 0)
+       tmp[0] = g_ascii_toupper (tmp[0]);
+      substitute_underscores (tmp);
+      g_string_append (gstring, tmp);
+      g_free (tmp);
+    }
+
+  return g_string_free (gstring, FALSE);
+}
+
 gboolean
 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
 {
@@ -549,66 +614,15 @@ gtk_accel_label_refetch (GtkAccelLabel *accel_label)
 
       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
        {
-         GString *gstring;
-         gboolean seen_mod = FALSE;
-         gunichar ch;
-         
-         gstring = g_string_new (accel_label->accel_string);
-         g_string_append (gstring, gstring->len ? class->accel_seperator : "   ");
-         
-         if (key->accel_mods & GDK_SHIFT_MASK)
-           {
-             g_string_append (gstring, class->mod_name_shift);
-             seen_mod = TRUE;
-           }
-         if (key->accel_mods & GDK_CONTROL_MASK)
-           {
-             if (seen_mod)
-               g_string_append (gstring, class->mod_separator);
-             g_string_append (gstring, class->mod_name_control);
-             seen_mod = TRUE;
-           }
-         if (key->accel_mods & GDK_MOD1_MASK)
-           {
-             if (seen_mod)
-               g_string_append (gstring, class->mod_separator);
-             g_string_append (gstring, class->mod_name_alt);
-             seen_mod = TRUE;
-           }
-         if (seen_mod)
-           g_string_append (gstring, class->mod_separator);
-
-         ch = gdk_keyval_to_unicode (key->accel_key);
-         if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
-             (ch < 0x80 || class->latin1_to_char))
-           {
-             switch (ch)
-               {
-               case ' ':
-                 g_string_append (gstring, "Space");
-                 break;
-               case '\\':
-                 g_string_append (gstring, "Backslash");
-                 break;
-               default:
-                 g_string_append_unichar (gstring, g_unichar_toupper (ch));
-                 break;
-               }
-           }
-         else
-           {
-             gchar *tmp;
-             
-             tmp = gtk_accelerator_name (key->accel_key, 0);
-             if (tmp[0] != 0 && tmp[1] == 0)
-               tmp[0] = g_ascii_toupper (tmp[0]);
-             substitute_underscores (tmp);
-             g_string_append (gstring, tmp);
-             g_free (tmp);
-           }
-         g_free (accel_label->accel_string);
-         accel_label->accel_string = gstring->str;
-         g_string_free (gstring, FALSE);
+         GtkAccelLabelClass *klass;
+         gchar *tmp;
+
+         klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
+         tmp = _gtk_accel_label_class_get_accelerator_label (klass,
+                                                             key->accel_key,
+                                                             key->accel_mods);
+         accel_label->accel_string = g_strconcat ("   ", tmp, NULL);
+         g_free (tmp);
        }
       if (!accel_label->accel_string)
        accel_label->accel_string = g_strdup ("-/-");
index 9d15ba6a74116030b5d298edf1164142e2e68758..4eb842a610c1ef4190efb395b6bc47f9237155ca 100644 (file)
 #include <gtk/gtklabel.h>
 
 
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
+G_BEGIN_DECLS
 
 #define GTK_TYPE_ACCEL_LABEL           (gtk_accel_label_get_type ())
 #define GTK_ACCEL_LABEL(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ACCEL_LABEL, GtkAccelLabel))
@@ -97,9 +94,11 @@ void    gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
                                              GClosure      *accel_closure);
 gboolean   gtk_accel_label_refetch           (GtkAccelLabel *accel_label);
 
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
+/* private */
+gchar *    _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
+                                                        guint               accelerator_key,
+                                                        GdkModifierType     accelerator_mods);
 
+G_END_DECLS
 
 #endif /* __GTK_ACCEL_LABEL_H__ */