Description
GBinding is the representation of a binding between a property on a
GObject instance (or source) and another property on another GObject
instance (or target). Whenever the source property changes, the same
value is applied to the target property; for instance, the following
binding:
will cause the property named "property-b" of object2
to be updated
every time g_object_set() or the specific accessor changes the value of
the property "property-a" of object1
.
It is possible to create a bidirectional binding between two properties
of two GObject instances, so that if either property changes, the
other is updated as well, for instance:
will keep the two properties in sync.
It is also possible to set a custom transformation function (in both
directions, in case of a bidirectional binding) to apply a custom
transformation from the source value to the target value before
applying it; for instance, the following binding:
will keep the "value" property of the two adjustments in sync; the
celsius_to_fahrenheit
function will be called whenever the "value"
property of adjustment1
changes and will transform the current value
of the property before applying it to the "value" property of adjustment2
.
Vice versa, the fahrenheit_to_celsius
function will be called whenever
the "value" property of adjustment2
changes, and will transform the
current value of the property before applying it to the "value" property
of adjustment1
.
Note that GBinding does not resolve cycles by itself; a cycle like
might lead to an infinite loop. The loop, in this particular case,
can be avoided if the objects emit the “notify” signal only
if the value has effectively been changed. A binding is implemented
using the “notify” signal, so it is susceptible to all the
various ways of blocking a signal emission, like g_signal_stop_emission()
or g_signal_handler_block().
A binding will be severed, and the resources it allocates freed, whenever
either one of the GObject instances it refers to are finalized, or when
the GBinding instance loses its last reference.
Bindings for languages with garbage collection can use
g_binding_unbind() to explicitly release a binding between the source
and target properties, instead of relying on the last reference on the
binding, source, and target instances to drop.
GBinding is available since GObject 2.26
Functions
g_binding_get_source ()
GObject *
g_binding_get_source (GBinding *binding);
Retrieves the GObject instance used as the source of the binding.
Returns
the source GObject.
[transfer none]
Since: 2.26
g_binding_get_source_property ()
const gchar *
g_binding_get_source_property (GBinding *binding);
Retrieves the name of the property of “source” used as the source
of the binding.
Returns
the name of the source property
Since: 2.26
g_binding_get_target ()
GObject *
g_binding_get_target (GBinding *binding);
Retrieves the GObject instance used as the target of the binding.
Returns
the target GObject.
[transfer none]
Since: 2.26
g_binding_get_target_property ()
const gchar *
g_binding_get_target_property (GBinding *binding);
Retrieves the name of the property of “target” used as the target
of the binding.
Returns
the name of the target property
Since: 2.26
g_binding_unbind ()
void
g_binding_unbind (GBinding *binding);
Explicitly releases the binding between the source and the target
property expressed by binding
.
This function will release the reference that is being held on
the binding
instance; if you want to hold on to the GBinding instance
after calling g_binding_unbind(), you will need to hold a reference
to it.
Since: 2.38
g_object_bind_property ()
GBinding *
g_object_bind_property (gpointer source,
const gchar *source_property,
gpointer target,
const gchar *target_property,
GBindingFlags flags);
Creates a binding between source_property
on source
and target_property
on target
. Whenever the source_property
is changed the target_property
is
updated using the same value. For instance:
Will result in the "sensitive" property of the widget GObject instance to be
updated with the same value of the "active" property of the action GObject
instance.
If flags
contains G_BINDING_BIDIRECTIONAL then the binding will be mutual:
if target_property
on target
changes then the source_property
on source
will be updated as well.
The binding will automatically be removed when either the source
or the
target
instances are finalized. To remove the binding without affecting the
source
and the target
you can just call g_object_unref() on the returned
GBinding instance.
A GObject can have multiple bindings.
Returns
the GBinding instance representing the
binding between the two GObject instances. The binding is released
whenever the GBinding reference count reaches zero.
[transfer none]
Since: 2.26
GBindingTransformFunc ()
gboolean
(*GBindingTransformFunc) (GBinding *binding,
const GValue *from_value,
GValue *to_value,
gpointer user_data);
A function to be called to transform from_value
to to_value
. If
this is the transform_to
function of a binding, then from_value
is the source_property
on the source
object, and to_value
is the
target_property
on the target
object. If this is the
transform_from
function of a G_BINDING_BIDIRECTIONAL binding,
then those roles are reversed.
Returns
TRUE if the transformation was successful, and FALSE
otherwise
Since: 2.26
g_object_bind_property_full ()
GBinding *
g_object_bind_property_full (gpointer source,
const gchar *source_property,
gpointer target,
const gchar *target_property,
GBindingFlags flags,
GBindingTransformFunc transform_to,
GBindingTransformFunc transform_from,
gpointer user_data,
GDestroyNotify notify);
Complete version of g_object_bind_property().
Creates a binding between source_property
on source
and target_property
on target
, allowing you to set the transformation functions to be used by
the binding.
If flags
contains G_BINDING_BIDIRECTIONAL then the binding will be mutual:
if target_property
on target
changes then the source_property
on source
will be updated as well. The transform_from
function is only used in case
of bidirectional bindings, otherwise it will be ignored
The binding will automatically be removed when either the source
or the
target
instances are finalized. This will release the reference that is
being held on the GBinding instance; if you want to hold on to the
GBinding instance, you will need to hold a reference to it.
To remove the binding, call g_binding_unbind().
A GObject can have multiple bindings.
The same user_data
parameter will be used for both transform_to
and transform_from
transformation functions; the notify
function will
be called once, when the binding is removed. If you need different data
for each transformation function, please use
g_object_bind_property_with_closures() instead.
Returns
the GBinding instance representing the
binding between the two GObject instances. The binding is released
whenever the GBinding reference count reaches zero.
[transfer none]
Since: 2.26
g_object_bind_property_with_closures ()
GBinding *
g_object_bind_property_with_closures (gpointer source,
const gchar *source_property,
gpointer target,
const gchar *target_property,
GBindingFlags flags,
GClosure *transform_to,
GClosure *transform_from);
Creates a binding between source_property
on source
and target_property
on target
, allowing you to set the transformation functions to be used by
the binding.
This function is the language bindings friendly version of
g_object_bind_property_full(), using GClosures instead of
function pointers.
[rename-to g_object_bind_property_full]
Returns
the GBinding instance representing the
binding between the two GObject instances. The binding is released
whenever the GBinding reference count reaches zero.
[transfer none]
Since: 2.26
Property Details
The “flags” property
“flags” GBindingFlags
Flags to be used to control the GBinding
Owner: GBinding
Flags: Read / Write / Construct Only
Since: 2.26
The “source” property
“source” GObject *
The GObject that should be used as the source of the binding
Owner: GBinding
Flags: Read / Write / Construct Only
Since: 2.26
The “source-property” property
“source-property” gchar *
The name of the property of “source” that should be used
as the source of the binding.
This should be in canonical form to get the
best performance.
Owner: GBinding
Flags: Read / Write / Construct Only
Default value: NULL
Since: 2.26
The “target” property
“target” GObject *
The GObject that should be used as the target of the binding
Owner: GBinding
Flags: Read / Write / Construct Only
Since: 2.26
The “target-property” property
“target-property” gchar *
The name of the property of “target” that should be used
as the target of the binding.
This should be in canonical form to get the
best performance.
Owner: GBinding
Flags: Read / Write / Construct Only
Default value: NULL
Since: 2.26