Commit ec3bb377 authored by Johan Dahlin's avatar Johan Dahlin

Add GtkBuilder and GError arguments to gtk_builder_value_from_string and

    * gtk/gtkbuilder.c: (gtk_builder_get_parameters),
    (gtk_builder_value_from_string),
    (gtk_builder_value_from_string_type):
    * gtk/gtkbuilder.h:
    * gtk/gtkcontainer.c: (gtk_container_buildable_set_child_property),
    (attributes_text_element):
    * gtk/gtkliststore.c: (list_store_text):
    * tests/buildertest.c: (test_value_from_string):

    Add GtkBuilder and GError arguments to gtk_builder_value_from_string
    and gtk_builder_value_from_string_type. (#451428)


svn path=/trunk/; revision=18262
parent 2fc1ae81
2007-06-27 Johan Dahlin <jdahlin@async.com.br>
* gtk/gtkbuilder.c: (gtk_builder_get_parameters),
(gtk_builder_value_from_string),
(gtk_builder_value_from_string_type):
* gtk/gtkbuilder.h:
* gtk/gtkcontainer.c: (gtk_container_buildable_set_child_property),
(attributes_text_element):
* gtk/gtkliststore.c: (list_store_text):
* tests/buildertest.c: (test_value_from_string):
Add GtkBuilder and GError arguments to gtk_builder_value_from_string
and gtk_builder_value_from_string_type. (#451428)
2007-06-27 Xan Lopez <xan@gnome.org>
* gtk/gtkvbbox.c (gtk_vbutton_box_size_allocate):
......
......@@ -244,7 +244,8 @@ gtk_builder_get_parameters (GtkBuilder *builder,
GParamSpec *pspec;
GObjectClass *oclass;
DelayedProperty *property;
GError *error = NULL;
oclass = g_type_class_ref (object_type);
g_assert (oclass != NULL);
......@@ -300,10 +301,14 @@ gtk_builder_get_parameters (GtkBuilder *builder,
continue;
}
}
else if (!gtk_builder_value_from_string (pspec, prop->data, &parameter.value))
else if (!gtk_builder_value_from_string (builder, pspec,
prop->data, &parameter.value, &error))
{
g_warning ("failed to set property %s.%s to %s",
g_type_name (object_type), prop->name, prop->data);
g_warning ("failed to set property %s.%s to %s: %s",
g_type_name (object_type), prop->name, prop->data,
error->message);
g_error_free (error);
error = NULL;
continue;
}
......@@ -917,9 +922,11 @@ gtk_builder_connect_signals_full (GtkBuilder *builder,
/**
* gtk_builder_value_from_string
* @builder: a #GtkBuilder
* @pspec: the GParamSpec for the property
* @string: the string representation of the value.
* @value: the GValue to store the result in.
* @error: return location for an error
*
* This function demarshals a value from a string. This function
* calls g_value_init() on the @value argument, so it need not be
......@@ -935,9 +942,11 @@ gtk_builder_connect_signals_full (GtkBuilder *builder,
* Since: 2.12
*/
gboolean
gtk_builder_value_from_string (GParamSpec *pspec,
const gchar *string,
GValue *value)
gtk_builder_value_from_string (GtkBuilder *builder,
GParamSpec *pspec,
const gchar *string,
GValue *value,
GError **error)
{
/*
* GParamSpecUnichar has the internal type G_TYPE_UINT,
......@@ -953,15 +962,18 @@ gtk_builder_value_from_string (GParamSpec *pspec,
return TRUE;
}
return gtk_builder_value_from_string_type (G_PARAM_SPEC_VALUE_TYPE (pspec),
string, value);
return gtk_builder_value_from_string_type (builder,
G_PARAM_SPEC_VALUE_TYPE (pspec),
string, value, error);
}
/**
* gtk_builder_value_from_string_type
* @builder: a #GtkBuilder
* @type: the GType of the value
* @string: the string representation of the value.
* @value: the GValue to store the result in.
* @error: return location for an error
*
* Like gtk_builder_value_from_string(), but takes a #GType instead of #GParamSpec.
*
......@@ -970,9 +982,11 @@ gtk_builder_value_from_string (GParamSpec *pspec,
* Since: 2.12
*/
gboolean
gtk_builder_value_from_string_type (GType type,
const gchar *string,
GValue *value)
gtk_builder_value_from_string_type (GtkBuilder *builder,
GType type,
const gchar *string,
GValue *value,
GError **error)
{
gboolean ret = TRUE;
......@@ -1010,7 +1024,11 @@ gtk_builder_value_from_string_type (GType type,
b = strtol (string, &endptr, 0);
if (errno || endptr == string)
{
g_warning ("could not parse int `%s'", string);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not parse int `%s'",
string);
ret = FALSE;
break;
}
......@@ -1029,7 +1047,11 @@ gtk_builder_value_from_string_type (GType type,
l = strtol (string, &endptr, 0);
if (errno || endptr == string)
{
g_warning ("could not parse long `%s'", string);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not parse long `%s'",
string);
ret = FALSE;
break;
}
......@@ -1048,7 +1070,11 @@ gtk_builder_value_from_string_type (GType type,
ul = strtoul (string, &endptr, 0);
if (errno || endptr == string)
{
g_warning ("could not parse ulong `%s'", string);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not parse ulong `%s'",
string);
ret = FALSE;
break;
}
......@@ -1073,7 +1099,11 @@ gtk_builder_value_from_string_type (GType type,
d = g_ascii_strtod (string, &endptr);
if (errno || endptr == string)
{
g_warning ("could not parse double `%s'", string);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not parse double `%s'",
string);
ret = FALSE;
break;
}
......@@ -1097,7 +1127,11 @@ gtk_builder_value_from_string_type (GType type,
g_value_set_boxed (value, &colour);
else
{
g_warning ("could not parse colour name `%s'", string);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not parse color `%s'",
string);
ret = FALSE;
}
}
......@@ -1114,11 +1148,11 @@ gtk_builder_value_from_string_type (GType type,
if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
{
gchar *filename;
GError *error = NULL;
GError *tmp_error = NULL;
GdkPixbuf *pixbuf;
filename = gtk_xml_relative_file (xml, string);
pixbuf = gdk_pixbuf_new_from_file (filename, &error);
pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);
if (pixbuf)
{
g_value_set_object (value, pixbuf);
......@@ -1126,8 +1160,12 @@ gtk_builder_value_from_string_type (GType type,
}
else
{
g_warning ("Error loading image: %s", error->message);
g_error_free (error);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"could not load image `%s'",
tmp_error->message);
g_error_free (tmp_error);
ret = FALSE;
}
g_free (filename);
......@@ -1137,6 +1175,11 @@ gtk_builder_value_from_string_type (GType type,
ret = FALSE;
break;
default:
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"unsupported GType `%s'",
g_type_name (type));
ret = FALSE;
break;
}
......
......@@ -44,7 +44,8 @@ typedef enum
GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
GTK_BUILDER_ERROR_INVALID_TAG,
GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE
GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE,
GTK_BUILDER_ERROR_INVALID_VALUE
} GtkBuilderError;
GQuark gtk_builder_error_quark (void);
......@@ -106,12 +107,16 @@ const gchar* gtk_builder_get_translation_domain (GtkBuilder *builder);
GType gtk_builder_get_type_from_name (GtkBuilder *builder,
const char *type_name);
gboolean gtk_builder_value_from_string (GParamSpec *pspec,
gboolean gtk_builder_value_from_string (GtkBuilder *builder,
GParamSpec *pspec,
const gchar *string,
GValue *value);
gboolean gtk_builder_value_from_string_type (GType type,
GValue *value,
GError **error);
gboolean gtk_builder_value_from_string_type (GtkBuilder *builder,
GType type,
const gchar *string,
GValue *value);
GValue *value,
GError **error);
guint _gtk_builder_flags_from_string (GType type,
const char *string);
......
......@@ -314,13 +314,15 @@ gtk_container_buildable_add_child (GtkBuildable *buildable,
static void
gtk_container_buildable_set_child_property (GtkContainer *container,
GtkBuilder *builder,
GtkWidget *child,
gchar *name,
const gchar *value)
{
GParamSpec *pspec;
GValue gvalue = { 0, };
GError *error = NULL;
pspec = gtk_container_class_find_child_property
(G_OBJECT_GET_CLASS (container), name);
if (!pspec)
......@@ -330,13 +332,15 @@ gtk_container_buildable_set_child_property (GtkContainer *container,
return;
}
if (!gtk_builder_value_from_string (pspec, value, &gvalue))
if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
{
g_warning ("Could not read property %s:%s with value %s of type %s",
g_warning ("Could not read property %s:%s with value %s of type %s: %s",
g_type_name (G_OBJECT_TYPE (container)),
name,
value,
g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
error->message);
g_error_free (error);
return;
}
......@@ -385,6 +389,7 @@ attributes_text_element (GMarkupParseContext *context,
return;
gtk_container_buildable_set_child_property (parser_data->container,
parser_data->builder,
parser_data->child,
parser_data->child_prop_name,
text);
......
......@@ -2187,17 +2187,27 @@ list_store_text (GMarkupParseContext *context,
{
SubParserData *data = (SubParserData*)user_data;
gint i;
GError *tmp_error = NULL;
if (!data->is_data)
return;
i = data->row_column - 1;
if (!gtk_builder_value_from_string_type (data->column_types[i],
if (!gtk_builder_value_from_string_type (data->builder,
data->column_types[i],
text,
&data->values[i]))
g_error ("Could not convert '%s' to type %s\n",
text, g_type_name (data->column_types[i]));
&data->values[i],
&tmp_error))
{
g_set_error (error,
tmp_error->domain,
tmp_error->code,
"Could not convert '%s' to type %s: %s\n",
text, g_type_name (data->column_types[i]),
tmp_error->message);
g_error_free (tmp_error);
}
}
static const GMarkupParser list_store_parser =
......
......@@ -1416,71 +1416,84 @@ static gboolean
test_value_from_string (void)
{
GValue value = { 0 };
GError *error = NULL;
GtkBuilder *builder;
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_STRING, "test", &value), FALSE);
builder = gtk_builder_new ();
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_STRING, "test", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_STRING (&value), FALSE);
g_return_val_if_fail (strcmp (g_value_get_string (&value), "test") == 0, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "true", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "true", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == TRUE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "false", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "false", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == FALSE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "yes", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "yes", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == TRUE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "no", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "no", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == FALSE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "0", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "0", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == FALSE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "1", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "1", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (&value), FALSE);
g_return_val_if_fail (g_value_get_boolean (&value) == TRUE, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_BOOLEAN, "blaurgh", &value) == FALSE, FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "blaurgh", &value, &error) == FALSE, FALSE);
g_return_val_if_fail (error != NULL, FALSE);
g_value_unset (&value);
g_error_free (error);
error = NULL;
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_INT, "12345", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_INT, "12345", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_INT (&value), FALSE);
g_return_val_if_fail (g_value_get_int (&value) == 12345, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_LONG, "9912345", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_LONG, "9912345", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_LONG (&value), FALSE);
g_return_val_if_fail (g_value_get_long (&value) == 9912345, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_UINT, "2345", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_UINT, "2345", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_UINT (&value), FALSE);
g_return_val_if_fail (g_value_get_uint (&value) == 2345, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_FLOAT, "1.454", &value), FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_FLOAT, "1.454", &value, &error), FALSE);
g_return_val_if_fail (G_VALUE_HOLDS_FLOAT (&value), FALSE);
g_return_val_if_fail (fabs (g_value_get_float (&value) - 1.454) < 0.00001, FALSE);
g_value_unset (&value);
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_FLOAT, "abc", &value) == FALSE, FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_FLOAT, "abc", &value, &error) == FALSE, FALSE);
g_value_unset (&value);
g_error_free (error);
error = NULL;
g_return_val_if_fail (gtk_builder_value_from_string_type (G_TYPE_INT, "/-+,abc", &value) == FALSE, FALSE);
g_return_val_if_fail (gtk_builder_value_from_string_type (builder, G_TYPE_INT, "/-+,abc", &value, &error) == FALSE, FALSE);
g_value_unset (&value);
g_error_free (error);
error = NULL;
g_object_unref (builder);
return TRUE;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment