From 988a3e2bf4809359ef65134bc1fb985e7c468c9d Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Sat, 30 Jun 2007 07:03:34 +0000 Subject: [PATCH] Move type-func to instead of , add a test to make sure 2007-06-30 Johan Dahlin * gtk/gtkbuilderparser.c: (_get_type_by_symbol), (parse_object), (parse_child): * tests/buildertest.c: (test_types): Move type-func to instead of , add a test to make sure that it works as desired, #452463 svn path=/trunk/; revision=18306 --- ChangeLog | 6 ++++ gtk/gtkbuilderparser.c | 68 ++++++++++++++++++++++-------------------- tests/buildertest.c | 26 ++++++++++++++++ 3 files changed, 68 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 504334ed8f..7d5437d29e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2007-06-30 Johan Dahlin + * gtk/gtkbuilderparser.c: (_get_type_by_symbol), (parse_object), + (parse_child): + * tests/buildertest.c: (test_types): + Move type-func to instead of , add a test to + make sure that it works as desired, #452463 + * gtk/gtkbuilder.c: (gtk_builder_value_from_string_type): * gtk/gtkbuilderparser.c: (_gtk_builder_parse_boolean), (parse_property), (parse_signal): diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c index a18d5ec7c4..dd1305b970 100644 --- a/gtk/gtkbuilderparser.c +++ b/gtk/gtkbuilderparser.c @@ -227,6 +227,26 @@ builder_construct (ParserData *data, return object; } +static gchar * +_get_type_by_symbol (const gchar* symbol) +{ + static GModule *module = NULL; + GTypeGetFunc func; + GType type; + + if (!module) + module = g_module_open (NULL, 0); + + if (!g_module_symbol (module, symbol, (gpointer)&func)) + return NULL; + + type = func (); + if (type == G_TYPE_INVALID) + return NULL; + + return g_strdup (g_type_name (type)); +} + static void parse_object (ParserData *data, const gchar *element_name, @@ -258,6 +278,22 @@ parse_object (ParserData *data, object_id = g_strdup (values[i]); else if (strcmp (names[i], "constructor") == 0) constructor = g_strdup (values[i]); + else if (strcmp (names[i], "type-func") == 0) + { + /* Call the GType function, and return the name of the GType, + * it's guaranteed afterwards that g_type_from_name on the name + * will return our GType + */ + object_class = _get_type_by_symbol (values[i]); + if (!object_class) + { + g_set_error (error, GTK_BUILDER_ERROR, + GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION, + _("Invalid type function: `%s'"), + values[i]); + return; + } + } else { error_invalid_attribute (data, element_name, names[i], error); @@ -303,26 +339,6 @@ free_object_info (ObjectInfo *info) g_slice_free (ObjectInfo, info); } -static gchar * -_get_type_by_symbol (const gchar* symbol) -{ - static GModule *module = NULL; - GTypeGetFunc func; - GType type; - - if (!module) - module = g_module_open (NULL, 0); - - if (!g_module_symbol (module, symbol, (gpointer)&func)) - return NULL; - - type = func (); - if (type == G_TYPE_INVALID) - return NULL; - - return g_strdup (g_type_name (type)); -} - static void parse_child (ParserData *data, const gchar *element_name, @@ -354,18 +370,6 @@ parse_child (ParserData *data, child_info->type = g_strdup (values[i]); else if (strcmp (names[i], "internal-child") == 0) child_info->internal_child = g_strdup (values[i]); - else if (strcmp (names[i], "type-func") == 0) - { - child_info->type = _get_type_by_symbol (values[i]); - if (!child_info->type) - { - g_set_error (error, GTK_BUILDER_ERROR, - GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION, - _("Invalid type function: `%s'"), - values[i]); - return; - } - } else error_invalid_attribute (data, element_name, names[i], error); } diff --git a/tests/buildertest.c b/tests/buildertest.c index 48fbd88cee..fb443a74e9 100644 --- a/tests/buildertest.c +++ b/tests/buildertest.c @@ -638,13 +638,39 @@ gboolean test_types (void) " " " " ""; + const gchar buffer2[] = + "" + " " + ""; + const gchar buffer3[] = + "" + " " + ""; GtkBuilder *builder; + GObject *window; + GError *error; builder = builder_new_from_string (buffer, -1, NULL); gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "dialog"))); gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "window"))); g_object_unref (builder); + + builder = builder_new_from_string (buffer2, -1, NULL); + window = gtk_builder_get_object (builder, "window"); + g_assert (window != NULL); + g_assert (GTK_IS_WINDOW (window)); + gtk_widget_destroy (GTK_WIDGET (window)); + g_object_unref (builder); + error = NULL; + builder = gtk_builder_new (); + gtk_builder_add_from_string (builder, buffer3, -1, &error); + g_assert (error != NULL); + g_return_val_if_fail (error->domain == GTK_BUILDER_ERROR, FALSE); + g_return_val_if_fail (error->code == GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION, FALSE); + g_error_free (error); + g_object_unref (builder); + return TRUE; }