[gtk+] GtkBuilder: Add new convenience API
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+] GtkBuilder: Add new convenience API
- Date: Tue, 28 Apr 2015 03:11:07 +0000 (UTC)
commit fc83c8ac76b2a9ec13430dab530fa262aaaa9337
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Apr 27 22:54:25 2015 -0400
GtkBuilder: Add new convenience API
Add a convenience function that is like gtk_builder_get_object()
but stashes away a GError if a lookup fails. To make the error
message informative, the function takes a line/column pair.
Doing things this way is necessary because the custom_tag_end,
custom_finished, and parser_finished vfuncs don't take a
GError parameter, despite being called from a place where
we can report a GError back.
gtk/gtkbuilder.c | 40 ++++++++++++++++++++++++++++++++++++++++
gtk/gtkbuilderprivate.h | 5 +++++
2 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c
index 6cd801c..60b3b9a 100644
--- a/gtk/gtkbuilder.c
+++ b/gtk/gtkbuilder.c
@@ -2706,3 +2706,43 @@ _gtk_builder_check_parent (GtkBuilder *builder,
return FALSE;
}
+
+/*< private >
+ * @builder: a #GtkBuilder
+ * @name: object name to look up
+ * @line: line number where @name was encountered
+ * @col: column number where @name was encountered
+ *
+ * Looks up an object by name. Similar to gtk_builder_get_object(),
+ * but sets an error if lookup fails during custom_tag_end,
+ * custom_finished or parser_finished vfuncs.
+ *
+ * The reason for doing things this way is that these vfuncs don't
+ * take a GError** parameter to return an error.
+ *
+ * Returns: the found object
+ */
+GObject *
+_gtk_builder_lookup_object (GtkBuilder *builder,
+ const gchar *name,
+ gint line,
+ gint col)
+{
+ GObject *obj;
+ GError *error = NULL;
+
+ obj = g_hash_table_lookup (builder->priv->objects, name);
+ error = (GError *) g_object_get_data (G_OBJECT (builder), "lookup-error");
+
+ if (!obj && !error)
+ {
+ g_set_error (&error,
+ GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ID,
+ "%s:%d:%d Object with ID %s not found",
+ builder->priv->filename, line, col, name);
+ g_object_set_data_full (G_OBJECT (builder), "lookup-error",
+ error, (GDestroyNotify)g_error_free);
+ }
+
+ return obj;
+}
diff --git a/gtk/gtkbuilderprivate.h b/gtk/gtkbuilderprivate.h
index d64fa10..ee457c1 100644
--- a/gtk/gtkbuilderprivate.h
+++ b/gtk/gtkbuilderprivate.h
@@ -197,5 +197,10 @@ gboolean _gtk_builder_check_parent (GtkBuilder *builder,
GMarkupParseContext *context,
const gchar *parent_name,
GError **error);
+GObject * _gtk_builder_lookup_object (GtkBuilder *builder,
+ const gchar *name,
+ gint line,
+ gint col);
+
#endif /* __GTK_BUILDER_PRIVATE_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]