a11y: Reuse builder parsing for enums

No need to manually write all these parsing functions
when _gtk_builder_enum_from_string does exactly what
is needed here.
This commit is contained in:
Matthias Clasen 2020-10-25 12:38:52 -04:00
parent fce455ae0b
commit 677ea5f22d

View File

@ -21,6 +21,9 @@
#include "config.h"
#include "gtkaccessiblevalueprivate.h"
#include "gtkbuilderprivate.h"
#include "gtkenums.h"
#include "gtktypebuiltins.h"
/* {{{ Undefined value */
@ -287,21 +290,12 @@ gtk_invalid_accessible_value_parse (const char *str,
gsize len,
GError **error)
{
int value;
g_return_val_if_fail (str == NULL || len == 0, NULL);
if (strncmp (str, "false", 5) == 0)
return gtk_invalid_accessible_value_new (GTK_ACCESSIBLE_INVALID_FALSE);
else if (strncmp (str, "true", 4) == 0)
return gtk_invalid_accessible_value_new (GTK_ACCESSIBLE_INVALID_TRUE);
else if (strncmp (str, "grammar", 7) == 0)
return gtk_invalid_accessible_value_new (GTK_ACCESSIBLE_INVALID_GRAMMAR);
else if (strncmp (str, "spelling", 8) == 0)
return gtk_invalid_accessible_value_new (GTK_ACCESSIBLE_INVALID_SPELLING);
else
g_set_error (error, GTK_ACCESSIBLE_VALUE_ERROR,
GTK_ACCESSIBLE_VALUE_ERROR_INVALID_TOKEN,
"Unknown token “%s”",
str);
if (_gtk_builder_enum_from_string (GTK_TYPE_ACCESSIBLE_INVALID_STATE, str, &value, error))
return gtk_invalid_accessible_value_new (value);
return NULL;
}
@ -356,21 +350,12 @@ gtk_autocomplete_accessible_value_parse (const char *str,
gsize len,
GError **error)
{
int value;
g_return_val_if_fail (str == NULL || len == 0, NULL);
if (strncmp (str, "none", 4) == 0)
return gtk_autocomplete_accessible_value_new (GTK_ACCESSIBLE_AUTOCOMPLETE_NONE);
else if (strncmp (str, "inline", 6) == 0)
return gtk_autocomplete_accessible_value_new (GTK_ACCESSIBLE_AUTOCOMPLETE_INLINE);
else if (strncmp (str, "list", 4) == 0)
return gtk_autocomplete_accessible_value_new (GTK_ACCESSIBLE_AUTOCOMPLETE_LIST);
else if (strncmp (str, "both", 4) == 0)
return gtk_autocomplete_accessible_value_new (GTK_ACCESSIBLE_AUTOCOMPLETE_BOTH);
else
g_set_error (error, GTK_ACCESSIBLE_VALUE_ERROR,
GTK_ACCESSIBLE_VALUE_ERROR_INVALID_TOKEN,
"Unknown token “%s”",
str);
if (_gtk_builder_enum_from_string (GTK_TYPE_ACCESSIBLE_AUTOCOMPLETE, str, &value, error))
return gtk_autocomplete_accessible_value_new (value);
return NULL;
}
@ -419,17 +404,12 @@ gtk_orientation_accessible_value_parse (const char *str,
gsize len,
GError **error)
{
int value;
g_return_val_if_fail (str == NULL || len == 0, NULL);
if (strncmp (str, "horizontal", 10) == 0)
return gtk_orientation_accessible_value_new (GTK_ORIENTATION_HORIZONTAL);
else if (strncmp (str, "vertical", 8) == 0)
return gtk_orientation_accessible_value_new (GTK_ORIENTATION_VERTICAL);
else
g_set_error (error, GTK_ACCESSIBLE_VALUE_ERROR,
GTK_ACCESSIBLE_VALUE_ERROR_INVALID_TOKEN,
"Unknown token “%s”",
str);
if (_gtk_builder_enum_from_string (GTK_TYPE_ORIENTATION, str, &value, error))
return gtk_orientation_accessible_value_new (value);
return NULL;
}
@ -484,21 +464,12 @@ gtk_sort_accessible_value_parse (const char *str,
gsize len,
GError **error)
{
int value;
g_return_val_if_fail (str == NULL || len == 0, NULL);
if (strncmp (str, "none", 4) == 0)
return gtk_sort_accessible_value_new (GTK_ACCESSIBLE_SORT_NONE);
else if (strncmp (str, "ascending", 9) == 0)
return gtk_sort_accessible_value_new (GTK_ACCESSIBLE_SORT_ASCENDING);
else if (strncmp (str, "descending", 10) == 0)
return gtk_sort_accessible_value_new (GTK_ACCESSIBLE_SORT_DESCENDING);
else if (strncmp (str, "other", 5) == 0)
return gtk_sort_accessible_value_new (GTK_ACCESSIBLE_SORT_OTHER);
else
g_set_error (error, GTK_ACCESSIBLE_VALUE_ERROR,
GTK_ACCESSIBLE_VALUE_ERROR_INVALID_TOKEN,
"Unknown token “%s”",
str);
if (_gtk_builder_enum_from_string (GTK_TYPE_ACCESSIBLE_SORT, str, &value, error))
return gtk_sort_accessible_value_new (value);
return NULL;
}