[gtk/shortcuts-rebased-again: 94/171] accelerators: Make gtk_accelerator_parse() return TRUE/FALSE



commit d273fb63ecc27bfc06391fb132440697a275f62f
Author: Benjamin Otte <otte redhat com>
Date:   Mon Aug 20 19:45:51 2018 +0200

    accelerators: Make gtk_accelerator_parse() return TRUE/FALSE
    
    A parse function should return success or not. So do that.

 gtk/gtkaccelgroup.c        | 13 +++++++++----
 gtk/gtkaccelgroup.h        |  4 ++--
 gtk/gtkapplicationaccels.c | 10 +++-------
 gtk/gtkmodelmenuitem.c     |  7 ++++---
 gtk/gtkshortcutlabel.c     |  3 +--
 tests/testgtk.c            |  6 ++++--
 testsuite/gtk/accel.c      | 14 +++++++-------
 7 files changed, 30 insertions(+), 27 deletions(-)
---
diff --git a/gtk/gtkaccelgroup.c b/gtk/gtkaccelgroup.c
index 097c872986..08ab2e9241 100644
--- a/gtk/gtkaccelgroup.c
+++ b/gtk/gtkaccelgroup.c
@@ -932,8 +932,10 @@ is_keycode (const gchar *string)
  *
  * If the parse fails, @accelerator_key, @accelerator_mods and
  * @accelerator_codes will be set to 0 (zero).
+ *
+ * Returns: %TRUE if parsing succeeded
  */
-void
+gboolean
 gtk_accelerator_parse_with_keycode (const gchar     *accelerator,
                                     guint           *accelerator_key,
                                     guint          **accelerator_codes,
@@ -950,7 +952,8 @@ gtk_accelerator_parse_with_keycode (const gchar     *accelerator,
     *accelerator_mods = 0;
   if (accelerator_codes)
     *accelerator_codes = NULL;
-  g_return_if_fail (accelerator != NULL);
+
+  g_return_val_if_fail (accelerator != NULL, FALSE);
 
   error = FALSE;
   keyval = 0;
@@ -1160,6 +1163,8 @@ out:
     *accelerator_key = gdk_keyval_to_lower (keyval);
   if (accelerator_mods)
     *accelerator_mods = mods;
+
+  return !error;
 }
 
 /**
@@ -1183,12 +1188,12 @@ out:
  * If the parse fails, @accelerator_key and @accelerator_mods will
  * be set to 0 (zero).
  */
-void
+gboolean
 gtk_accelerator_parse (const gchar     *accelerator,
                        guint           *accelerator_key,
                        GdkModifierType *accelerator_mods)
 {
-  gtk_accelerator_parse_with_keycode (accelerator, accelerator_key, NULL, accelerator_mods);
+  return gtk_accelerator_parse_with_keycode (accelerator, accelerator_key, NULL, accelerator_mods);
 }
 
 /**
diff --git a/gtk/gtkaccelgroup.h b/gtk/gtkaccelgroup.h
index 73c4fc1d40..cc5d64c58a 100644
--- a/gtk/gtkaccelgroup.h
+++ b/gtk/gtkaccelgroup.h
@@ -184,11 +184,11 @@ GDK_AVAILABLE_IN_ALL
 gboolean gtk_accelerator_valid               (guint            keyval,
                                               GdkModifierType  modifiers) G_GNUC_CONST;
 GDK_AVAILABLE_IN_ALL
-void    gtk_accelerator_parse                (const gchar     *accelerator,
+gboolean gtk_accelerator_parse               (const gchar     *accelerator,
                                               guint           *accelerator_key,
                                               GdkModifierType *accelerator_mods);
 GDK_AVAILABLE_IN_ALL
-void gtk_accelerator_parse_with_keycode       (const gchar     *accelerator,
+gboolean gtk_accelerator_parse_with_keycode   (const gchar     *accelerator,
                                                guint           *accelerator_key,
                                                guint          **accelerator_codes,
                                                GdkModifierType *accelerator_mods);
diff --git a/gtk/gtkapplicationaccels.c b/gtk/gtkapplicationaccels.c
index 43e35d79e4..22e20c1e76 100644
--- a/gtk/gtkapplicationaccels.c
+++ b/gtk/gtkapplicationaccels.c
@@ -196,9 +196,7 @@ gtk_application_accels_set_accels_for_action (GtkApplicationAccels *accels,
 
       for (i = 0; i < n; i++)
         {
-          gtk_accelerator_parse (accelerators[i], &keys[i].key, &keys[i].modifier);
-
-          if (keys[i].key == 0)
+          if (!gtk_accelerator_parse (accelerators[i], &keys[i].key, &keys[i].modifier))
             {
               g_warning ("Unable to parse accelerator '%s': ignored request to install %d accelerators",
                          accelerators[i], n);
@@ -272,12 +270,10 @@ gtk_application_accels_get_actions_for_accel (GtkApplicationAccels *accels,
   AccelKey accel_key;
   guint i, n;
 
-  gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier);
-
-  if (accel_key.key == 0)
+  if (!gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier))
     {
       g_critical ("invalid accelerator string '%s'", accel);
-      g_return_val_if_fail (accel_key.key != 0, NULL);
+      return NULL;
     }
 
   actions_and_targets = g_hash_table_lookup (accels->accel_to_actions, &accel_key);
diff --git a/gtk/gtkmodelmenuitem.c b/gtk/gtkmodelmenuitem.c
index b52e126053..8b5fc17975 100644
--- a/gtk/gtkmodelmenuitem.c
+++ b/gtk/gtkmodelmenuitem.c
@@ -310,9 +310,10 @@ gtk_model_menu_item_set_accel (GtkModelMenuItem *item,
 
   if (accel)
     {
-      gtk_accelerator_parse (accel, &key, &modifiers);
-      if (!key)
-        modifiers = 0;
+      if (!gtk_accelerator_parse (accel, &key, &modifiers))
+        {
+          g_warning ("\"%s\" is not a valida accelerator, ignoring", accel);
+        }
     }
   else
     {
diff --git a/gtk/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index c61362e7b4..6f203fdcda 100644
--- a/gtk/gtkshortcutlabel.c
+++ b/gtk/gtkshortcutlabel.c
@@ -304,8 +304,7 @@ parse_combination (GtkShortcutLabel *self,
   accels = g_strsplit (str, "&", 0);
   for (k = 0; accels[k]; k++)
     {
-      gtk_accelerator_parse (accels[k], &key, &modifier);
-      if (key == 0 && modifier == 0)
+      if (!gtk_accelerator_parse (accels[k], &key, &modifier))
         {
           retval = FALSE;
           break;
diff --git a/tests/testgtk.c b/tests/testgtk.c
index 2ff85dd413..327d271350 100644
--- a/tests/testgtk.c
+++ b/tests/testgtk.c
@@ -1758,8 +1758,10 @@ accel_button_new (GtkAccelGroup *accel_group,
   GtkWidget *button;
   GtkWidget *label;
 
-  gtk_accelerator_parse (accel, &keyval, &modifiers);
-  g_assert (keyval);
+  if (!gtk_accelerator_parse (accel, &keyval, &modifiers))
+    {
+      g_assert_not_reached ();
+    }
 
   button = gtk_button_new ();
   gtk_widget_add_accelerator (button, "activate", accel_group,
diff --git a/testsuite/gtk/accel.c b/testsuite/gtk/accel.c
index da031da7be..51e6fc7bee 100644
--- a/testsuite/gtk/accel.c
+++ b/testsuite/gtk/accel.c
@@ -28,19 +28,19 @@ test_one_accel (const char *accel,
   char *label, *name;
 
   accel_key = 0;
-  gtk_accelerator_parse_with_keycode (accel,
-                                     &accel_key,
-                                     &keycodes,
-                                     &mods);
+  g_assert (gtk_accelerator_parse_with_keycode (accel,
+                                                &accel_key,
+                                                &keycodes,
+                                                &mods));
 
   if (has_keysym)
     {
       guint accel_key_2;
       GdkModifierType mods_2;
 
-      gtk_accelerator_parse (accel,
-                             &accel_key_2,
-                             &mods_2);
+      g_assert (gtk_accelerator_parse (accel,
+                                       &accel_key_2,
+                                       &mods_2));
       g_assert (accel_key == accel_key_2);
       g_assert (mods == mods_2);
     }


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