## GBinding GBinding — Bind two object properties |

## Synopsis

    #include <glib-object.h>

                        GBinding;
    enum                GBindingFlags;
    GObject *           g_binding_get_source                (GBinding *binding);
    const gchar *       g_binding_get_source_property       (GBinding *binding);
    GObject *           g_binding_get_target                (GBinding *binding);
    const gchar *       g_binding_get_target_property       (GBinding *binding);
    GBindingFlags       g_binding_get_flags                 (GBinding *binding);
    void                g_binding_unbind                    (GBinding *binding);

    GBinding *          g_object_bind_property              (gpointer source,
                                                             const gchar *source_property,
                                                             gpointer target,
                                                             const gchar *target_property,
                                                             GBindingFlags flags);
    gboolean            (*GBindingTransformFunc)            (GBinding *binding,
                                                             const GValue *from_value,
                                                             GValue *to_value,
                                                             gpointer user_data);
    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);
    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);

## Object Hierarchy

      GObject
       +----GBinding

## Properties

      "flags"                    GBindingFlags         : Read / Write / Construct Only
      "source"                   GObject*              : Read / Write / Construct Only
      "source-property"          gchar*                : Read / Write / Construct Only
      "target"                   GObject*              : Read / Write / Construct Only
      "target-property"          gchar*                : Read / Write / Construct Only

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

    1
    2
    3
    g_object_bind_property (object1, "property-a",
                            object2, "property-b",
                            G_BINDING_DEFAULT);

will cause object2:property-b to be updated every time `g_object_set()` or the specific accessor changes the value of object1:property-a.

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:

    1
    2
    3
    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:

    1
    2
    3
    4
    5
    6
    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 adjustment1:value property changes and will transform the current value of the property before applying it to the adjustment2:value property; vice versa, the `fahrenheit_to_celsius` function will be called whenever the adjustment2:value property changes, and will transform the current value of the property before applying it to the adjustment1:value.

Note that GBinding does not resolve cycles by itself; a cycle like

    1
    2
    3
    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 "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.

### Note

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

## Details

### GBinding

    typedef struct _GBinding GBinding;

GBinding is an opaque structure whose members cannot be accessed directly.

Since 2.26

### enum GBindingFlags

    typedef enum {
     /*< prefix=G_BINDING >*/
      G_BINDING_DEFAULT        = 0,

      G_BINDING_BIDIRECTIONAL  = 1 << 0,
      G_BINDING_SYNC_CREATE    = 1 << 1,
      G_BINDING_INVERT_BOOLEAN = 1 << 2
    } GBindingFlags;

Flags to be passed to `g_object_bind_property()` or `g_object_bind_property_full()`.

This enumeration can be extended at later date.

`G_BINDING_DEFAULT` | The default binding; if the source property changes, the target property is updated with its value.
`G_BINDING_BIDIRECTIONAL` | Bidirectional binding; if either the property of the source or the property of the target changes, the other is updated.
`G_BINDING_SYNC_CREATE` | Synchronize the values of the source and target properties when creating the binding; the direction of the synchronization is always from the source to the target.
`G_BINDING_INVERT_BOOLEAN` | If the two properties being bound are booleans, setting one to `TRUE` will result in the other being set to `FALSE` and vice versa. This flag will only work for boolean properties, and cannot be used when passing custom transformation functions to `g_object_bind_property_full()`.

Since 2.26

### g_binding_get_source ()

    GObject *           g_binding_get_source                (GBinding *binding);

Retrieves the GObject instance used as the source of the binding

`binding` : | a GBinding
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

`binding` : | a GBinding
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

`binding` : | a GBinding
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

`binding` : | a GBinding
Returns : | the name of the target property

Since 2.26

### g_binding_get_flags ()

    GBindingFlags       g_binding_get_flags                 (GBinding *binding);

Retrieves the flags passed when constructing the GBinding

`binding` : | a GBinding
Returns : | the GBindingFlags used by the GBinding

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`.

### Note

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.

`binding` : | a GBinding

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:

    1
    g_object_bind_property (action, "active", widget, "sensitive", 0);

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.

`source` : | the source GObject. [type GObject.Object]
`source_property` : | the property on `source` to bind
`target` : | the target GObject. [type GObject.Object]
`target_property` : | the property on `target` to bind
`flags` : | flags to pass to GBinding
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.

`binding` : | a GBinding
`from_value` : | the GValue containing the value to transform
`to_value` : | the GValue in which to store the transformed value
`user_data` : | data passed to the transform function
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. 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.

### Note

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.

`source` : | the source GObject. [type GObject.Object]
`source_property` : | the property on `source` to bind
`target` : | the target GObject. [type GObject.Object]
`target_property` : | the property on `target` to bind
`flags` : | flags to pass to GBinding
`transform_to` : | the transformation function from the `source` to the `target`, or `NULL` to use the default. [scope notified][allow-none]
`transform_from` : | the transformation function from the `target` to the `source`, or `NULL` to use the default. [scope notified][allow-none]
`user_data` : | custom data to be passed to the transformation functions, or `NULL`
`notify` : | function to be called when disposing the binding, to free the resources used by the transformation functions
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

`source` : | the source GObject. [type GObject.Object]
`source_property` : | the property on `source` to bind
`target` : | the target GObject. [type GObject.Object]
`target_property` : | the property on `target` to bind
`flags` : | flags to pass to GBinding
`transform_to` : | a GClosure wrapping the transformation function from the `source` to the `target`, or `NULL` to use the default
`transform_from` : | a GClosure wrapping the transformation function from the `target` to the `source`, or `NULL` to use the default
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         : Read / Write / Construct Only

Flags to be used to control the GBinding

Since 2.26

### The `"source"` property

      "source"                   GObject*              : Read / Write / Construct Only

The GObject that should be used as the source of the binding

Since 2.26

### The `"source-property"` property

      "source-property"          gchar*                : Read / Write / Construct Only

The name of the property of "source" that should be used as the source of the binding

Default value: NULL

Since 2.26

### The `"target"` property

      "target"                   GObject*              : Read / Write / Construct Only

The GObject that should be used as the target of the binding

Since 2.26

### The `"target-property"` property

      "target-property"          gchar*                : Read / Write / Construct Only

The name of the property of "target" that should be used as the target of the binding

Default value: NULL

Since 2.26