[gtk+] help overlay: Allow key sequences



commit 705d371362c005a4ea699d0b617cdf9d8a336877
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Nov 14 21:34:43 2015 -0500

    help overlay: Allow key sequences
    
    Extend the syntax to allow sequences of keys or key combinations,
    e.g. t+t or <ctl>c+<ctl>x.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=758051

 gtk/gtkshortcutlabel.c     |   94 +++++++++++++++++++++++++-------------------
 gtk/gtkshortcutsshortcut.c |   12 +++--
 2 files changed, 60 insertions(+), 46 deletions(-)
---
diff --git a/gtk/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index 12f1467..24c4f1b 100644
--- a/gtk/gtkshortcutlabel.c
+++ b/gtk/gtkshortcutlabel.c
@@ -190,15 +190,58 @@ display_shortcut (GtkContainer    *self,
   g_strfreev (keys);
 }
 
-static void
-gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
+static gboolean
+parse_sequence (GtkShortcutLabel *self,
+                const gchar      *str)
 {
-  gchar **accels = NULL;
+  gchar **accels;
+  gint k;
   GdkModifierType modifier = 0;
-  GdkModifierType modifier2 = 0;
   guint key = 0;
-  guint key2 = 0;
-  guint k;
+  gboolean retval = TRUE;
+
+  accels = g_strsplit (str, "+", 0);
+  for (k = 0; accels[k]; k++)
+    {
+      gtk_accelerator_parse (accels[k], &key, &modifier);
+      if (key == 0 && modifier == 0)
+        {
+          retval = FALSE;
+          break;
+        }
+      display_shortcut (GTK_CONTAINER (self), key, modifier);
+    }
+  g_strfreev (accels);
+
+  return retval;
+}
+
+static gboolean
+parse_range (GtkShortcutLabel *self,
+             const gchar      *str)
+{
+  gchar *dots;
+
+  dots = strstr (str, "...");
+  if (!dots)
+    return parse_sequence (self, str);
+
+  dots[0] = '\0';
+  if (!parse_sequence (self, str))
+    return FALSE;
+
+  gtk_container_add (GTK_CONTAINER (self), dim_label ("⋯"));
+  if (!parse_sequence (self, dots + 3))
+    return FALSE;
+
+  return TRUE;
+}
+
+static void
+gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
+{
+  gchar **accels;
+  gint k;
 
   gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback)gtk_widget_destroy, NULL);
 
@@ -208,46 +251,15 @@ gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
   accels = g_strsplit (self->accelerator, " ", 0);
   for (k = 0; accels[k]; k++)
     {
-      gchar *dots;
-
-      dots = strstr (accels[k], "...");
-      if (dots)
-        {
-          dots[0] = '\0';
-          gtk_accelerator_parse (dots + 3, &key2, &modifier2);
-          if ((key2 == 0) && (modifier2 == 0))
-            {
-              g_warning ("Failed to parse %s, part of accelerator '%s'", dots + 3, self->accelerator);
-              goto out;
-            }
-        }
-      else
-        key2 = 0;
+      if (k > 0)
+        gtk_container_add (GTK_CONTAINER (self), dim_label ("/"));
 
-      gtk_accelerator_parse (accels[k], &key, &modifier);
-      if ((key == 0) && (modifier == 0))
+      if (!parse_range (self, accels[k]))
         {
           g_warning ("Failed to parse %s, part of accelerator '%s'", accels[k], self->accelerator);
-          goto out;
+          break;
         }
-
-      if (k > 0)
-        gtk_container_add (GTK_CONTAINER (self), dim_label ("/"));
-
-      display_shortcut (GTK_CONTAINER (self), key, modifier);
-
-       if (key2 == 0)
-         continue;
-
-       if (modifier2 == modifier)
-         modifier2 = 0;
-
-      gtk_container_add (GTK_CONTAINER (self), dim_label ("⋯"));
-
-      display_shortcut (GTK_CONTAINER (self), key2, modifier2);
     }
-
-out:
   g_strfreev (accels);
 }
 
diff --git a/gtk/gtkshortcutsshortcut.c b/gtk/gtkshortcutsshortcut.c
index 63a342d..b4888a5 100644
--- a/gtk/gtkshortcutsshortcut.c
+++ b/gtk/gtkshortcutsshortcut.c
@@ -182,17 +182,19 @@ gtk_shortcuts_shortcut_class_init (GtkShortcutsShortcutClass *klass)
   /**
    * GtkShortcutsShortcut:accelerator:
    *
-   * The accelerator(s) represented by this object, in the syntax
-   * understood by gtk_accelerator_parse(). Multiple accelerators
+   * The accelerator(s) represented by this object in (an extension of)
+   * the syntax understood by gtk_accelerator_parse(). Multiple accelerators
    * can be specified by separating them with a space, but keep in
    * mind that the available width is limited. It is also possible
-   * to specify ranges of shortcuts, using ... between the keys.
+   * to specify ranges of shortcuts, using ... between the keys. Sequences
+   * of keys can be specified using a + between the keys.
    *
    * Examples:
    *
-   * - A single shortcut: <ctrl><alt>delete
+   * - A single shortcut: <ctl><alt>delete
    * - Two alternative shortcuts: <shift>a Home
-   * - A range: <alt>1...9
+   * - A range of shortcuts: <alt>1...<alt>9
+   * - A sequence of key combinations: <ctl>c+<ctl>x
    *
    * Note that < and > need to be escaped as &lt; and &gt; when used
    * in .ui files.


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