class GObject::Binding

Overview

#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:

|[ g_object_bind_property (object1, "property-a", object2, "property-b", G_BINDING_DEFAULT); ]|

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:

|[ g_object_bind_property (object1, "property-a", object2, "property-b", G_BINDING_BIDIRECTIONAL); ]|

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:

|[ g_object_bind_property_full (adjustment1, "value", adjustment2, "value", G_BINDING_BIDIRECTIONAL, celsius_to_fahrenheit, fahrenheit_to_celsius, NULL, NULL); ]|

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

|[ object1:propertyA -> object2:propertyB object2:propertyB -> object3:propertyC object3:propertyC -> object1:propertyA ]|

might lead to an infinite loop. The loop, in this particular case, can be avoided if the objects emit the #GObject::notify signal only if the value has effectively been changed. A binding is implemented using the #GObject::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

Defined in:

auto/g_object-2.0/binding.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void)? qdata, ref_count ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, cast?(obj : GObject::Object) : self? cast?, new new, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Constructor Detail

def self.new #

Initialize a new Binding.


def self.new(*, flags : GObject::BindingFlags? = nil, source : GObject::Object? = nil, source_property : String? = nil, target : GObject::Object? = nil, target_property : String? = nil) #

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


Instance Method Detail

def dup_source : GObject::Object? #

Retrieves the #GObject instance used as the source of the binding.

A #GBinding can outlive the source #GObject as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will return %NULL.


def dup_target : GObject::Object? #

Retrieves the #GObject instance used as the target of the binding.

A #GBinding can outlive the target #GObject as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will return %NULL.


def flags : GObject::BindingFlags #

Retrieves the flags passed when constructing the #GBinding.



def source : GObject::Object? #

Retrieves the #GObject instance used as the source of the binding.

A #GBinding can outlive the source #GObject as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will return %NULL.

Use g_binding_dup_source() if the source or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the source is finalized from another thread in the meantime.


def source=(value : GObject::Object?) : GObject::Object? #

def source_property : String #

Retrieves the name of the property of #GBinding:source used as the source of the binding.


def source_property=(value : String) : String #

def target : GObject::Object? #

Retrieves the #GObject instance used as the target of the binding.

A #GBinding can outlive the target #GObject as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will return %NULL.

Use g_binding_dup_target() if the target or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the target is finalized from another thread in the meantime.


def target=(value : GObject::Object?) : GObject::Object? #

def target_property : String #

Retrieves the name of the property of #GBinding:target used as the target of the binding.


def target_property=(value : String) : String #

def unbind : Nil #

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 the binding is still bound; if you want to hold on to the #GBinding instance after calling g_binding_unbind(), you will need to hold a reference to it.

Note however that this function does not take ownership of @binding, it only unrefs the reference that was initially created by g_object_bind_property() and is owned by the binding.