accelerators: Make gtk_accelerator_parse() return TRUE/FALSE

A parse function should return success or not. So do that.
This commit is contained in:
Benjamin Otte 2018-08-20 19:45:51 +02:00 committed by Matthias Clasen
parent fb6a8f5fc1
commit 580863b112
6 changed files with 26 additions and 24 deletions

View File

@ -932,8 +932,10 @@ is_keycode (const gchar *string)
* *
* If the parse fails, @accelerator_key, @accelerator_mods and * If the parse fails, @accelerator_key, @accelerator_mods and
* @accelerator_codes will be set to 0 (zero). * @accelerator_codes will be set to 0 (zero).
*
* Returns: %TRUE if parsing succeeded
*/ */
void gboolean
gtk_accelerator_parse_with_keycode (const gchar *accelerator, gtk_accelerator_parse_with_keycode (const gchar *accelerator,
guint *accelerator_key, guint *accelerator_key,
guint **accelerator_codes, guint **accelerator_codes,
@ -950,7 +952,8 @@ gtk_accelerator_parse_with_keycode (const gchar *accelerator,
*accelerator_mods = 0; *accelerator_mods = 0;
if (accelerator_codes) if (accelerator_codes)
*accelerator_codes = NULL; *accelerator_codes = NULL;
g_return_if_fail (accelerator != NULL);
g_return_val_if_fail (accelerator != NULL, FALSE);
error = FALSE; error = FALSE;
keyval = 0; keyval = 0;
@ -1160,6 +1163,8 @@ out:
*accelerator_key = gdk_keyval_to_lower (keyval); *accelerator_key = gdk_keyval_to_lower (keyval);
if (accelerator_mods) if (accelerator_mods)
*accelerator_mods = mods; *accelerator_mods = mods;
return !error;
} }
/** /**
@ -1183,12 +1188,12 @@ out:
* If the parse fails, @accelerator_key and @accelerator_mods will * If the parse fails, @accelerator_key and @accelerator_mods will
* be set to 0 (zero). * be set to 0 (zero).
*/ */
void gboolean
gtk_accelerator_parse (const gchar *accelerator, gtk_accelerator_parse (const gchar *accelerator,
guint *accelerator_key, guint *accelerator_key,
GdkModifierType *accelerator_mods) 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);
} }
/** /**

View File

@ -184,11 +184,11 @@ GDK_AVAILABLE_IN_ALL
gboolean gtk_accelerator_valid (guint keyval, gboolean gtk_accelerator_valid (guint keyval,
GdkModifierType modifiers) G_GNUC_CONST; GdkModifierType modifiers) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_accelerator_parse (const gchar *accelerator, gboolean gtk_accelerator_parse (const gchar *accelerator,
guint *accelerator_key, guint *accelerator_key,
GdkModifierType *accelerator_mods); GdkModifierType *accelerator_mods);
GDK_AVAILABLE_IN_ALL 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_key,
guint **accelerator_codes, guint **accelerator_codes,
GdkModifierType *accelerator_mods); GdkModifierType *accelerator_mods);

View File

@ -195,9 +195,7 @@ gtk_application_accels_set_accels_for_action (GtkApplicationAccels *accels,
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
gtk_accelerator_parse (accelerators[i], &keys[i].key, &keys[i].modifier); if (!gtk_accelerator_parse (accelerators[i], &keys[i].key, &keys[i].modifier))
if (keys[i].key == 0)
{ {
g_warning ("Unable to parse accelerator '%s': ignored request to install %d accelerators", g_warning ("Unable to parse accelerator '%s': ignored request to install %d accelerators",
accelerators[i], n); accelerators[i], n);
@ -271,12 +269,10 @@ gtk_application_accels_get_actions_for_accel (GtkApplicationAccels *accels,
AccelKey accel_key; AccelKey accel_key;
guint i, n; guint i, n;
gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier); if (!gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier))
if (accel_key.key == 0)
{ {
g_critical ("invalid accelerator string '%s'", accel); 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); actions_and_targets = g_hash_table_lookup (accels->accel_to_actions, &accel_key);

View File

@ -303,8 +303,7 @@ parse_combination (GtkShortcutLabel *self,
accels = g_strsplit (str, "&", 0); accels = g_strsplit (str, "&", 0);
for (k = 0; accels[k]; k++) for (k = 0; accels[k]; k++)
{ {
gtk_accelerator_parse (accels[k], &key, &modifier); if (!gtk_accelerator_parse (accels[k], &key, &modifier))
if (key == 0 && modifier == 0)
{ {
retval = FALSE; retval = FALSE;
break; break;

View File

@ -1612,8 +1612,10 @@ accel_button_new (GtkAccelGroup *accel_group,
GtkWidget *button; GtkWidget *button;
GtkWidget *label; GtkWidget *label;
gtk_accelerator_parse (accel, &keyval, &modifiers); if (!gtk_accelerator_parse (accel, &keyval, &modifiers))
g_assert (keyval); {
g_assert_not_reached ();
}
button = gtk_button_new (); button = gtk_button_new ();
gtk_widget_add_accelerator (button, "activate", accel_group, gtk_widget_add_accelerator (button, "activate", accel_group,

View File

@ -28,19 +28,19 @@ test_one_accel (const char *accel,
char *label, *name; char *label, *name;
accel_key = 0; accel_key = 0;
gtk_accelerator_parse_with_keycode (accel, g_assert (gtk_accelerator_parse_with_keycode (accel,
&accel_key, &accel_key,
&keycodes, &keycodes,
&mods); &mods));
if (has_keysym) if (has_keysym)
{ {
guint accel_key_2; guint accel_key_2;
GdkModifierType mods_2; GdkModifierType mods_2;
gtk_accelerator_parse (accel, g_assert (gtk_accelerator_parse (accel,
&accel_key_2, &accel_key_2,
&mods_2); &mods_2));
g_assert (accel_key == accel_key_2); g_assert (accel_key == accel_key_2);
g_assert (mods == mods_2); g_assert (mods == mods_2);
} }