enum {
PROP_0,
+ PROP_CURRENT_OBJECT,
PROP_TRANSLATION_DOMAIN,
LAST_PROP
};
gchar *filename;
gchar *resource_prefix;
GType template_type;
+ GObject *current_object;
GtkBuilderClosureFunc closure_func;
gpointer closure_data;
G_DEFINE_TYPE_WITH_PRIVATE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)
+static void
+gtk_builder_dispose (GObject *object)
+{
+ GtkBuilderPrivate *priv = gtk_builder_get_instance_private (GTK_BUILDER (object));
+
+ g_clear_object (&priv->current_object);
+
+ G_OBJECT_CLASS (gtk_builder_parent_class)->dispose (object);
+}
+
static void
gtk_builder_class_init (GtkBuilderClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ gobject_class->dispose = gtk_builder_dispose;
gobject_class->finalize = gtk_builder_finalize;
gobject_class->set_property = gtk_builder_set_property;
gobject_class->get_property = gtk_builder_get_property;
NULL,
GTK_PARAM_READWRITE);
+ /**
+ * GtkBuilder:current-object:
+ *
+ * The object the builder is evaluating for.
+ */
+ builder_props[PROP_CURRENT_OBJECT] =
+ g_param_spec_object ("current-object",
+ P_("Current object"),
+ P_("The object the builder is evaluating for"),
+ G_TYPE_OBJECT,
+ GTK_PARAM_READWRITE);
+
g_object_class_install_properties (gobject_class, LAST_PROP, builder_props);
}
switch (prop_id)
{
+ case PROP_CURRENT_OBJECT:
+ gtk_builder_set_current_object (builder, g_value_get_object (value));
+ break;
+
case PROP_TRANSLATION_DOMAIN:
gtk_builder_set_translation_domain (builder, g_value_get_string (value));
break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
switch (prop_id)
{
+ case PROP_CURRENT_OBJECT:
+ g_value_set_object (value, priv->current_object);
+ break;
+
case PROP_TRANSLATION_DOMAIN:
g_value_set_string (value, priv->domain);
break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
g_object_ref (object));
}
+/**
+ * gtk_builder_get_current_object:
+ * @self: a #GtkBuilder
+ *
+ * Gets the current object set via gtk_builder_set_current_object().
+ *
+ * Returns: (nullable) (transfer none): the current object
+ **/
+GObject *
+gtk_builder_get_current_object (GtkBuilder *self)
+{
+ GtkBuilderPrivate *priv = gtk_builder_get_instance_private (self);
+
+ g_return_val_if_fail (GTK_IS_BUILDER (self), NULL);
+
+ return priv->current_object;
+}
+
+/**
+ * gtk_builder_set_current_object:
+ * @self: a #GtkBuilder
+ * @current_object: (nullable) (transfer none): the new current object or
+ * %NULL for none
+ *
+ * Sets the current object for the @self. The current object can be
+ * tought of as the `this` object that the builder is working for and
+ * will often be used as the default object when an object is optional.
+ *
+ * gtk_widget_init_template() for example will set the current object to
+ * the widget the template is inited for.
+ * For functions like gtk_builder_new_from_resource(), the current object
+ * will be %NULL.
+ **/
+void
+gtk_builder_set_current_object (GtkBuilder *self,
+ GObject *current_object)
+{
+ GtkBuilderPrivate *priv = gtk_builder_get_instance_private (self);
+
+ g_return_if_fail (GTK_IS_BUILDER (self));
+ g_return_if_fail (current_object || G_IS_OBJECT (current_object));
+
+ if (!g_set_object (&priv->current_object, current_object))
+ return;
+
+ g_object_notify_by_pspec (G_OBJECT (self), builder_props[PROP_CURRENT_OBJECT]);
+}
+
/**
* GtkBuilderClosureFunc:
* @builder: a #GtkBuilder
gboolean swapped,
GObject *object)
{
+ GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
GClosure *closure;
+ if (object == NULL)
+ object = priv->current_object;
+
if (object)
{
if (swapped)
return TRUE;
}
-static GClosure *
-gtk_widget_template_closure_func (GtkBuilder *builder,
- const char *function_name,
- gboolean swapped,
- GObject *object,
- gpointer user_data,
- GError **error)
-{
- if (object == NULL)
- object = user_data;
-
- return gtk_builder_create_cclosure (builder,
- function_name,
- swapped,
- object,
- error);
-}
-
/**
* gtk_widget_init_template:
* @widget: a #GtkWidget
builder = gtk_builder_new ();
+ gtk_builder_set_current_object (builder, G_OBJECT (widget));
+
/* Setup closure handling. All signal data from a template receive the
* template instance as user data automatically.
*
*/
if (template->closure_func)
gtk_builder_set_closure_func (builder, template->closure_func, template->closure_data, NULL);
- else
- gtk_builder_set_closure_func (builder, gtk_widget_template_closure_func, widget, NULL);
/* Add any callback symbols declared for this GType to the GtkBuilder namespace */
for (l = template->callbacks; l; l = l->next)