[gtk/wip/matthiasc/shortcut-4: 90/147] accelerators: Make gtk_accelerator_parse() return TRUE/FALSE



commit 970a301717e604b3351d826045bac5fcfaa824c6
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/gtkshortcutlabel.c     |  3 +--
 tests/testgtk.c            |  6 ++++--
 testsuite/gtk/accel.c      | 14 +++++++-------
 6 files changed, 26 insertions(+), 24 deletions(-)
---
diff --git a/gtk/gtkaccelgroup.c b/gtk/gtkaccelgroup.c
index be79d8bd30..24f14b96d0 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 e7a4f48948..f800cb4d7b 100644
--- a/gtk/gtkapplicationaccels.c
+++ b/gtk/gtkapplicationaccels.c
@@ -195,9 +195,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);
@@ -271,12 +269,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/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index 704324fa8c..90471af3ee 100644
--- a/gtk/gtkshortcutlabel.c
+++ b/gtk/gtkshortcutlabel.c
@@ -303,8 +303,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 acb82b1eab..47bd34d851 100644
--- a/tests/testgtk.c
+++ b/tests/testgtk.c
@@ -1612,8 +1612,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]