[gtk/wip/otte/listview: 18/141] builder: Add gtk_builder_lookup_object()
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/listview: 18/141] builder: Add gtk_builder_lookup_object()
- Date: Tue, 26 Nov 2019 19:31:53 +0000 (UTC)
commit e9f3eed6f74967b11fdc0404b00f6aa380dfb0f8
Author: Benjamin Otte <otte redhat com>
Date: Mon Nov 25 07:40:28 2019 +0100
builder: Add gtk_builder_lookup_object()
... and use it. This function looks up an object like
gtk_builder_get_object() but generates an error on failure.
Unlike the evil function _gtk_builder_lookup_object() which also
generates an error but hides it for later lookup.
Use this to avoid continuing applying properties when an error was
encountered.
gtk/gtkbuilder.c | 74 +++++++++++++++++++++++++++++++++++++------------
gtk/gtkbuilderparser.c | 4 +--
gtk/gtkbuilderprivate.h | 5 ++++
3 files changed, 64 insertions(+), 19 deletions(-)
---
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c
index b605b22759..d7ba9f7084 100644
--- a/gtk/gtkbuilder.c
+++ b/gtk/gtkbuilder.c
@@ -1007,11 +1007,13 @@ _gtk_builder_add_signals (GtkBuilder *builder,
g_slist_copy (signals));
}
-static void
-gtk_builder_apply_delayed_properties (GtkBuilder *builder)
+static gboolean
+gtk_builder_apply_delayed_properties (GtkBuilder *builder,
+ GError **error)
{
GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
GSList *l, *props;
+ gboolean result = TRUE;
/* take the list over from the builder->priv.
*
@@ -1026,18 +1028,25 @@ gtk_builder_apply_delayed_properties (GtkBuilder *builder)
DelayedProperty *property = l->data;
GObject *object, *obj;
- object = g_hash_table_lookup (priv->objects, property->object);
- g_assert (object != NULL);
+ if (result)
+ {
+ object = g_hash_table_lookup (priv->objects, property->object);
+ g_assert (object != NULL);
- obj = _gtk_builder_lookup_object (builder, property->value, property->line, property->col);
- if (obj)
- g_object_set (object, property->pspec->name, obj, NULL);
+ obj = gtk_builder_lookup_object (builder, property->value, property->line, property->col, error);
+ if (obj)
+ g_object_set (object, property->pspec->name, obj, NULL);
+ else
+ result = FALSE;
+ }
g_free (property->value);
g_free (property->object);
g_slice_free (DelayedProperty, property);
}
g_slist_free (props);
+
+ return result;
}
static inline void
@@ -1051,28 +1060,37 @@ free_binding_info (gpointer data,
g_slice_free (BindingInfo, data);
}
-static inline void
-gtk_builder_create_bindings (GtkBuilder *builder)
+static inline gboolean
+gtk_builder_create_bindings (GtkBuilder *builder,
+ GError **error)
{
GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
GSList *l;
+ gboolean result = TRUE;
for (l = priv->bindings; l; l = l->next)
{
BindingInfo *info = l->data;
GObject *source;
- source = _gtk_builder_lookup_object (builder, info->source, info->line, info->col);
- if (source)
- g_object_bind_property (source, info->source_property,
- info->target, info->target_pspec->name,
- info->flags);
+ if (result)
+ {
+ source = gtk_builder_lookup_object (builder, info->source, info->line, info->col, error);
+ if (source)
+ g_object_bind_property (source, info->source_property,
+ info->target, info->target_pspec->name,
+ info->flags);
+ else
+ result = FALSE;
+ }
free_binding_info (info, NULL);
}
g_slist_free (priv->bindings);
priv->bindings = NULL;
+
+ return result;
}
/**
@@ -1766,9 +1784,9 @@ gboolean
_gtk_builder_finish (GtkBuilder *builder,
GError **error)
{
- gtk_builder_apply_delayed_properties (builder);
- gtk_builder_create_bindings (builder);
- return gtk_builder_connect_signals (builder, error);
+ return gtk_builder_apply_delayed_properties (builder, error)
+ && gtk_builder_create_bindings (builder, error)
+ && gtk_builder_connect_signals (builder, error);
}
/**
@@ -2994,6 +3012,28 @@ _gtk_builder_check_parent (GtkBuilder *builder,
return FALSE;
}
+GObject *
+gtk_builder_lookup_object (GtkBuilder *builder,
+ const gchar *name,
+ gint line,
+ gint col,
+ GError **error)
+{
+ GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
+ GObject *obj;
+
+ obj = g_hash_table_lookup (priv->objects, name);
+ if (obj == NULL)
+ {
+ g_set_error (error,
+ GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ID,
+ "%s:%d:%d Object with ID %s not found",
+ priv->filename, line, col, name);
+ }
+
+ return obj;
+}
+
/*< private >
* @builder: a #GtkBuilder
* @name: object name to look up
diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c
index fa492fd477..11d6f525e5 100644
--- a/gtk/gtkbuilderparser.c
+++ b/gtk/gtkbuilderparser.c
@@ -1982,10 +1982,10 @@ _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
if (!gtk_buildable_parse_context_parse (&data.ctx, buffer, length, error))
goto out;
- if (!_gtk_builder_finish (builder, error))
+ if (_gtk_builder_lookup_failed (builder, error))
goto out;
- if (_gtk_builder_lookup_failed (builder, error))
+ if (!_gtk_builder_finish (builder, error))
goto out;
/* Custom parser_finished */
diff --git a/gtk/gtkbuilderprivate.h b/gtk/gtkbuilderprivate.h
index 56df5f9ecb..0e9f73b3fd 100644
--- a/gtk/gtkbuilderprivate.h
+++ b/gtk/gtkbuilderprivate.h
@@ -255,6 +255,11 @@ gboolean _gtk_builder_check_parent (GtkBuilder *builder,
GtkBuildableParseContext *context,
const gchar *parent_name,
GError **error);
+GObject *gtk_builder_lookup_object (GtkBuilder *builder,
+ const gchar *name,
+ gint line,
+ gint col,
+ GError **error);
GObject *_gtk_builder_lookup_object (GtkBuilder *builder,
const gchar *name,
gint line,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]