GtkLevelBar: Use the new helpers

Instead of issuing g_warning, fill the provided GError.
This lets us test this error handling, and is the right
thing to do. Use the new GtkBuilder helpers and
g_markup_collect_attributes to do so.
This commit is contained in:
Matthias Clasen 2015-04-25 11:22:10 -04:00
parent 66ce0b6509
commit afd6621743

View File

@ -91,6 +91,7 @@
#include "config.h" #include "config.h"
#include "gtkbuildable.h" #include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtkintl.h" #include "gtkintl.h"
#include "gtkorientableprivate.h" #include "gtkorientableprivate.h"
#include "gtklevelbar.h" #include "gtklevelbar.h"
@ -674,6 +675,7 @@ gtk_level_bar_ensure_offsets_in_range (GtkLevelBar *self)
typedef struct { typedef struct {
GtkLevelBar *self; GtkLevelBar *self;
GtkBuilder *builder;
GList *offsets; GList *offsets;
} OffsetsParserData; } OffsetsParserData;
@ -685,60 +687,51 @@ offset_start_element (GMarkupParseContext *context,
gpointer user_data, gpointer user_data,
GError **error) GError **error)
{ {
OffsetsParserData *parser_data = user_data; OffsetsParserData *data = user_data;
const gchar *name = NULL;
const gchar *value_str = NULL;
GtkLevelBarOffset *offset;
gint line_number, char_number;
gint idx;
if (strcmp (element_name, "offsets") == 0) if (strcmp (element_name, "offsets") == 0)
; {
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_INVALID, NULL, NULL,
G_MARKUP_COLLECT_INVALID))
_gtk_builder_prefix_error (data->builder, context, error);
}
else if (strcmp (element_name, "offset") == 0) else if (strcmp (element_name, "offset") == 0)
{ {
for (idx = 0; names[idx] != NULL; idx++) const gchar *name;
{ const gchar *value;
if (strcmp (names[idx], "name") == 0) GValue gvalue = G_VALUE_INIT;
{ GtkLevelBarOffset *offset;
name = values[idx];
}
else if (strcmp (names[idx], "value") == 0)
{
value_str = values[idx];
}
else
{
g_markup_parse_context_get_position (context,
&line_number,
&char_number);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
"%s:%d:%d '%s' is not a valid attribute of <%s>",
"<input>",
line_number, char_number, names[idx], "offset");
if (!_gtk_builder_check_parent (data->builder, context, "offsets", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "name", &name,
G_MARKUP_COLLECT_STRING, "value", &value,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, context, error);
return; return;
} }
if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_DOUBLE, value, &gvalue, error))
{
_gtk_builder_prefix_error (data->builder, context, error);
return;
} }
if (name && value_str) offset = gtk_level_bar_offset_new (name, g_value_get_double (&gvalue));
{ data->offsets = g_list_prepend (data->offsets, offset);
offset = gtk_level_bar_offset_new (name, g_ascii_strtod (value_str, NULL));
parser_data->offsets = g_list_prepend (parser_data->offsets, offset);
}
} }
else else
{ {
g_markup_parse_context_get_position (context, _gtk_builder_error_unhandled_tag (data->builder, context,
&line_number, "GtkLevelBar", element_name,
&char_number); error);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_UNHANDLED_TAG,
"%s:%d:%d unsupported tag for GtkLevelBar: \"%s\"",
"<input>",
line_number, char_number, element_name);
} }
} }
@ -753,9 +746,9 @@ gtk_level_bar_buildable_custom_tag_start (GtkBuildable *buildable,
GObject *child, GObject *child,
const gchar *tagname, const gchar *tagname,
GMarkupParser *parser, GMarkupParser *parser,
gpointer *data) gpointer *parser_data)
{ {
OffsetsParserData *parser_data; OffsetsParserData *data;
if (child) if (child)
return FALSE; return FALSE;
@ -763,12 +756,13 @@ gtk_level_bar_buildable_custom_tag_start (GtkBuildable *buildable,
if (strcmp (tagname, "offsets") != 0) if (strcmp (tagname, "offsets") != 0)
return FALSE; return FALSE;
parser_data = g_slice_new0 (OffsetsParserData); data = g_slice_new0 (OffsetsParserData);
parser_data->self = GTK_LEVEL_BAR (buildable); data->self = GTK_LEVEL_BAR (buildable);
parser_data->offsets = NULL; data->builder = builder;
data->offsets = NULL;
*parser = offset_parser; *parser = offset_parser;
*data = parser_data; *parser_data = data;
return TRUE; return TRUE;
} }
@ -780,26 +774,25 @@ gtk_level_bar_buildable_custom_finished (GtkBuildable *buildable,
const gchar *tagname, const gchar *tagname,
gpointer user_data) gpointer user_data)
{ {
OffsetsParserData *parser_data; OffsetsParserData *data = user_data;
GtkLevelBar *self; GtkLevelBar *self;
GtkLevelBarOffset *offset; GtkLevelBarOffset *offset;
GList *l; GList *l;
parser_data = user_data; self = data->self;
self = parser_data->self;
if (strcmp (tagname, "offsets") != 0) if (strcmp (tagname, "offsets") != 0)
goto out; goto out;
for (l = parser_data->offsets; l != NULL; l = l->next) for (l = data->offsets; l != NULL; l = l->next)
{ {
offset = l->data; offset = l->data;
gtk_level_bar_add_offset_value (self, offset->name, offset->value); gtk_level_bar_add_offset_value (self, offset->name, offset->value);
} }
out: out:
g_list_free_full (parser_data->offsets, (GDestroyNotify) gtk_level_bar_offset_free); g_list_free_full (data->offsets, (GDestroyNotify) gtk_level_bar_offset_free);
g_slice_free (OffsetsParserData, parser_data); g_slice_free (OffsetsParserData, data);
} }
static void static void