Blame gobject/gbinding.c

Packit ae235b
/* gbinding.c: Binding for object properties
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2010  Intel Corp.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Emmanuele Bassi <ebassi@linux.intel.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gbinding
Packit ae235b
 * @Title: GBinding
Packit ae235b
 * @Short_Description: Bind two object properties
Packit ae235b
 *
Packit ae235b
 * #GBinding is the representation of a binding between a property on a
Packit ae235b
 * #GObject instance (or source) and another property on another #GObject
Packit ae235b
 * instance (or target). Whenever the source property changes, the same
Packit ae235b
 * value is applied to the target property; for instance, the following
Packit ae235b
 * binding:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   g_object_bind_property (object1, "property-a",
Packit ae235b
 *                           object2, "property-b",
Packit ae235b
 *                           G_BINDING_DEFAULT);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * will cause the property named "property-b" of @object2 to be updated
Packit ae235b
 * every time g_object_set() or the specific accessor changes the value of
Packit ae235b
 * the property "property-a" of @object1.
Packit ae235b
 *
Packit ae235b
 * It is possible to create a bidirectional binding between two properties
Packit ae235b
 * of two #GObject instances, so that if either property changes, the
Packit ae235b
 * other is updated as well, for instance:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   g_object_bind_property (object1, "property-a",
Packit ae235b
 *                           object2, "property-b",
Packit ae235b
 *                           G_BINDING_BIDIRECTIONAL);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * will keep the two properties in sync.
Packit ae235b
 *
Packit ae235b
 * It is also possible to set a custom transformation function (in both
Packit ae235b
 * directions, in case of a bidirectional binding) to apply a custom
Packit ae235b
 * transformation from the source value to the target value before
Packit ae235b
 * applying it; for instance, the following binding:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   g_object_bind_property_full (adjustment1, "value",
Packit ae235b
 *                                adjustment2, "value",
Packit ae235b
 *                                G_BINDING_BIDIRECTIONAL,
Packit ae235b
 *                                celsius_to_fahrenheit,
Packit ae235b
 *                                fahrenheit_to_celsius,
Packit ae235b
 *                                NULL, NULL);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * will keep the "value" property of the two adjustments in sync; the
Packit ae235b
 * @celsius_to_fahrenheit function will be called whenever the "value"
Packit ae235b
 * property of @adjustment1 changes and will transform the current value
Packit ae235b
 * of the property before applying it to the "value" property of @adjustment2.
Packit ae235b
 *
Packit ae235b
 * Vice versa, the @fahrenheit_to_celsius function will be called whenever
Packit ae235b
 * the "value" property of @adjustment2 changes, and will transform the
Packit ae235b
 * current value of the property before applying it to the "value" property
Packit ae235b
 * of @adjustment1.
Packit ae235b
 *
Packit ae235b
 * Note that #GBinding does not resolve cycles by itself; a cycle like
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   object1:propertyA -> object2:propertyB
Packit ae235b
 *   object2:propertyB -> object3:propertyC
Packit ae235b
 *   object3:propertyC -> object1:propertyA
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * might lead to an infinite loop. The loop, in this particular case,
Packit ae235b
 * can be avoided if the objects emit the #GObject::notify signal only
Packit ae235b
 * if the value has effectively been changed. A binding is implemented
Packit ae235b
 * using the #GObject::notify signal, so it is susceptible to all the
Packit ae235b
 * various ways of blocking a signal emission, like g_signal_stop_emission()
Packit ae235b
 * or g_signal_handler_block().
Packit ae235b
 *
Packit ae235b
 * A binding will be severed, and the resources it allocates freed, whenever
Packit ae235b
 * either one of the #GObject instances it refers to are finalized, or when
Packit ae235b
 * the #GBinding instance loses its last reference.
Packit ae235b
 *
Packit ae235b
 * Bindings for languages with garbage collection can use
Packit ae235b
 * g_binding_unbind() to explicitly release a binding between the source
Packit ae235b
 * and target properties, instead of relying on the last reference on the
Packit ae235b
 * binding, source, and target instances to drop.
Packit ae235b
 *
Packit ae235b
 * #GBinding is available since GObject 2.26
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gbinding.h"
Packit ae235b
#include "genums.h"
Packit ae235b
#include "gmarshal.h"
Packit ae235b
#include "gobject.h"
Packit ae235b
#include "gsignal.h"
Packit ae235b
#include "gparamspecs.h"
Packit ae235b
#include "gvaluetypes.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
GType
Packit ae235b
g_binding_flags_get_type (void)
Packit ae235b
{
Packit ae235b
  static volatile gsize g_define_type_id__volatile = 0;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&g_define_type_id__volatile))
Packit ae235b
    {
Packit ae235b
      static const GFlagsValue values[] = {
Packit ae235b
        { G_BINDING_DEFAULT, "G_BINDING_DEFAULT", "default" },
Packit ae235b
        { G_BINDING_BIDIRECTIONAL, "G_BINDING_BIDIRECTIONAL", "bidirectional" },
Packit ae235b
        { G_BINDING_SYNC_CREATE, "G_BINDING_SYNC_CREATE", "sync-create" },
Packit ae235b
        { G_BINDING_INVERT_BOOLEAN, "G_BINDING_INVERT_BOOLEAN", "invert-boolean" },
Packit ae235b
        { 0, NULL, NULL }
Packit ae235b
      };
Packit ae235b
      GType g_define_type_id =
Packit ae235b
        g_flags_register_static (g_intern_static_string ("GBindingFlags"), values);
Packit ae235b
      g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_define_type_id__volatile;
Packit ae235b
}
Packit ae235b
Packit ae235b
#define G_BINDING_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_BINDING, GBindingClass))
Packit ae235b
#define G_IS_BINDING_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_BINDING))
Packit ae235b
#define G_BINDING_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_BINDING, GBindingClass))
Packit ae235b
Packit ae235b
typedef struct _GBindingClass           GBindingClass;
Packit ae235b
Packit ae235b
struct _GBinding
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  /* no reference is held on the objects, to avoid cycles */
Packit ae235b
  GObject *source;
Packit ae235b
  GObject *target;
Packit ae235b
Packit ae235b
  /* the property names are interned, so they should not be freed */
Packit ae235b
  const gchar *source_property;
Packit ae235b
  const gchar *target_property;
Packit ae235b
Packit ae235b
  GParamSpec *source_pspec;
Packit ae235b
  GParamSpec *target_pspec;
Packit ae235b
Packit ae235b
  GBindingTransformFunc transform_s2t;
Packit ae235b
  GBindingTransformFunc transform_t2s;
Packit ae235b
Packit ae235b
  GBindingFlags flags;
Packit ae235b
Packit ae235b
  guint source_notify;
Packit ae235b
  guint target_notify;
Packit ae235b
Packit ae235b
  gpointer transform_data;
Packit ae235b
  GDestroyNotify notify;
Packit ae235b
Packit ae235b
  /* a guard, to avoid loops */
Packit ae235b
  guint is_frozen : 1;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GBindingClass
Packit ae235b
{
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
Packit ae235b
  PROP_SOURCE,
Packit ae235b
  PROP_TARGET,
Packit ae235b
  PROP_SOURCE_PROPERTY,
Packit ae235b
  PROP_TARGET_PROPERTY,
Packit ae235b
  PROP_FLAGS
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint gobject_notify_signal_id;
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GBinding, g_binding, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
/* the basic assumption is that if either the source or the target
Packit ae235b
 * goes away then the binding does not exist any more and it should
Packit ae235b
 * be reaped as well
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
weak_unbind (gpointer  user_data,
Packit ae235b
             GObject  *where_the_object_was)
Packit ae235b
{
Packit ae235b
  GBinding *binding = user_data;
Packit ae235b
Packit ae235b
  /* if what went away was the source, unset it so that GBinding::finalize
Packit ae235b
   * does not try to access it; otherwise, disconnect everything and remove
Packit ae235b
   * the GBinding instance from the object's qdata
Packit ae235b
   */
Packit ae235b
  if (binding->source == where_the_object_was)
Packit ae235b
    binding->source = NULL;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (binding->source_notify != 0)
Packit ae235b
        g_signal_handler_disconnect (binding->source, binding->source_notify);
Packit ae235b
Packit ae235b
      g_object_weak_unref (binding->source, weak_unbind, user_data);
Packit ae235b
Packit ae235b
      binding->source_notify = 0;
Packit ae235b
      binding->source = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* as above, but with the target */
Packit ae235b
  if (binding->target == where_the_object_was)
Packit ae235b
    binding->target = NULL;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (binding->target_notify != 0)
Packit ae235b
        g_signal_handler_disconnect (binding->target, binding->target_notify);
Packit ae235b
Packit ae235b
      g_object_weak_unref (binding->target, weak_unbind, user_data);
Packit ae235b
Packit ae235b
      binding->target_notify = 0;
Packit ae235b
      binding->target = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* this will take care of the binding itself */
Packit ae235b
  g_object_unref (binding);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
default_transform (GBinding     *binding,
Packit ae235b
                   const GValue *value_a,
Packit ae235b
                   GValue       *value_b,
Packit ae235b
                   gpointer      user_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  /* if it's not the same type, try to convert it using the GValue
Packit ae235b
   * transformation API; otherwise just copy it
Packit ae235b
   */
Packit ae235b
  if (!g_type_is_a (G_VALUE_TYPE (value_a), G_VALUE_TYPE (value_b)))
Packit ae235b
    {
Packit ae235b
      /* are these two types compatible (can be directly copied)? */
Packit ae235b
      if (g_value_type_compatible (G_VALUE_TYPE (value_a),
Packit ae235b
                                   G_VALUE_TYPE (value_b)))
Packit ae235b
        {
Packit ae235b
          g_value_copy (value_a, value_b);
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_value_type_transformable (G_VALUE_TYPE (value_a),
Packit ae235b
                                      G_VALUE_TYPE (value_b)))
Packit ae235b
        {
Packit ae235b
          if (g_value_transform (value_a, value_b))
Packit ae235b
            return TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_warning ("%s: Unable to convert a value of type %s to a "
Packit ae235b
                 "value of type %s",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 g_type_name (G_VALUE_TYPE (value_a)),
Packit ae235b
                 g_type_name (G_VALUE_TYPE (value_b)));
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_value_copy (value_a, value_b);
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
default_invert_boolean_transform (GBinding     *binding,
Packit ae235b
                                  const GValue *value_a,
Packit ae235b
                                  GValue       *value_b,
Packit ae235b
                                  gpointer      user_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  gboolean value;
Packit ae235b
Packit ae235b
  g_assert (G_VALUE_HOLDS_BOOLEAN (value_a));
Packit ae235b
  g_assert (G_VALUE_HOLDS_BOOLEAN (value_b));
Packit ae235b
Packit ae235b
  value = g_value_get_boolean (value_a);
Packit ae235b
  value = !value;
Packit ae235b
Packit ae235b
  g_value_set_boolean (value_b, value);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_source_notify (GObject    *gobject,
Packit ae235b
                  GParamSpec *pspec,
Packit ae235b
                  GBinding   *binding)
Packit ae235b
{
Packit ae235b
  GValue from_value = G_VALUE_INIT;
Packit ae235b
  GValue to_value = G_VALUE_INIT;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  if (binding->is_frozen)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_value_init (&from_value, G_PARAM_SPEC_VALUE_TYPE (binding->source_pspec));
Packit ae235b
  g_value_init (&to_value, G_PARAM_SPEC_VALUE_TYPE (binding->target_pspec));
Packit ae235b
Packit ae235b
  g_object_get_property (binding->source, binding->source_pspec->name, &from_value);
Packit ae235b
Packit ae235b
  res = binding->transform_s2t (binding,
Packit ae235b
                                &from_value,
Packit ae235b
                                &to_value,
Packit ae235b
                                binding->transform_data);
Packit ae235b
  if (res)
Packit ae235b
    {
Packit ae235b
      binding->is_frozen = TRUE;
Packit ae235b
Packit ae235b
      g_param_value_validate (binding->target_pspec, &to_value);
Packit ae235b
      g_object_set_property (binding->target, binding->target_pspec->name, &to_value);
Packit ae235b
Packit ae235b
      binding->is_frozen = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_value_unset (&from_value);
Packit ae235b
  g_value_unset (&to_value);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_target_notify (GObject    *gobject,
Packit ae235b
                  GParamSpec *pspec,
Packit ae235b
                  GBinding   *binding)
Packit ae235b
{
Packit ae235b
  GValue from_value = G_VALUE_INIT;
Packit ae235b
  GValue to_value = G_VALUE_INIT;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  if (binding->is_frozen)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_value_init (&from_value, G_PARAM_SPEC_VALUE_TYPE (binding->target_pspec));
Packit ae235b
  g_value_init (&to_value, G_PARAM_SPEC_VALUE_TYPE (binding->source_pspec));
Packit ae235b
Packit ae235b
  g_object_get_property (binding->target, binding->target_pspec->name, &from_value);
Packit ae235b
Packit ae235b
  res = binding->transform_t2s (binding,
Packit ae235b
                                &from_value,
Packit ae235b
                                &to_value,
Packit ae235b
                                binding->transform_data);
Packit ae235b
  if (res)
Packit ae235b
    {
Packit ae235b
      binding->is_frozen = TRUE;
Packit ae235b
Packit ae235b
      g_param_value_validate (binding->source_pspec, &to_value);
Packit ae235b
      g_object_set_property (binding->source, binding->source_pspec->name, &to_value);
Packit ae235b
Packit ae235b
      binding->is_frozen = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_value_unset (&from_value);
Packit ae235b
  g_value_unset (&to_value);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_binding_unbind_internal (GBinding *binding,
Packit ae235b
                           gboolean  unref_binding)
Packit ae235b
{
Packit ae235b
  gboolean source_is_target = binding->source == binding->target;
Packit ae235b
Packit ae235b
  /* dispose of the transformation data */
Packit ae235b
  if (binding->notify != NULL)
Packit ae235b
    {
Packit ae235b
      binding->notify (binding->transform_data);
Packit ae235b
Packit ae235b
      binding->transform_data = NULL;
Packit ae235b
      binding->notify = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (binding->source != NULL)
Packit ae235b
    {
Packit ae235b
      if (binding->source_notify != 0)
Packit ae235b
        g_signal_handler_disconnect (binding->source, binding->source_notify);
Packit ae235b
Packit ae235b
      g_object_weak_unref (binding->source, weak_unbind, binding);
Packit ae235b
Packit ae235b
      binding->source_notify = 0;
Packit ae235b
      binding->source = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (binding->target != NULL)
Packit ae235b
    {
Packit ae235b
      if (binding->target_notify != 0)
Packit ae235b
        g_signal_handler_disconnect (binding->target, binding->target_notify);
Packit ae235b
Packit ae235b
      if (!source_is_target)
Packit ae235b
        g_object_weak_unref (binding->target, weak_unbind, binding);
Packit ae235b
Packit ae235b
      binding->target_notify = 0;
Packit ae235b
      binding->target = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (unref_binding)
Packit ae235b
    g_object_unref (binding);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_finalize (GObject *gobject)
Packit ae235b
{
Packit ae235b
  GBinding *binding = G_BINDING (gobject);
Packit ae235b
Packit ae235b
  g_binding_unbind_internal (binding, FALSE);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_binding_parent_class)->finalize (gobject);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_set_property (GObject      *gobject,
Packit ae235b
                        guint         prop_id,
Packit ae235b
                        const GValue *value,
Packit ae235b
                        GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GBinding *binding = G_BINDING (gobject);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_SOURCE:
Packit ae235b
      binding->source = g_value_get_object (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_SOURCE_PROPERTY:
Packit ae235b
      binding->source_property = g_intern_string (g_value_get_string (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TARGET:
Packit ae235b
      binding->target = g_value_get_object (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TARGET_PROPERTY:
Packit ae235b
      binding->target_property = g_intern_string (g_value_get_string (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_FLAGS:
Packit ae235b
      binding->flags = g_value_get_flags (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_get_property (GObject    *gobject,
Packit ae235b
                        guint       prop_id,
Packit ae235b
                        GValue     *value,
Packit ae235b
                        GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GBinding *binding = G_BINDING (gobject);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_SOURCE:
Packit ae235b
      g_value_set_object (value, binding->source);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_SOURCE_PROPERTY:
Packit ae235b
      g_value_set_string (value, binding->source_property);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TARGET:
Packit ae235b
      g_value_set_object (value, binding->target);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TARGET_PROPERTY:
Packit ae235b
      g_value_set_string (value, binding->target_property);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_FLAGS:
Packit ae235b
      g_value_set_flags (value, binding->flags);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_constructed (GObject *gobject)
Packit ae235b
{
Packit ae235b
  GBinding *binding = G_BINDING (gobject);
Packit ae235b
  GBindingTransformFunc transform_func = default_transform;
Packit ae235b
  GQuark source_property_detail;
Packit ae235b
  GClosure *source_notify_closure;
Packit ae235b
Packit ae235b
  /* assert that we were constructed correctly */
Packit ae235b
  g_assert (binding->source != NULL);
Packit ae235b
  g_assert (binding->target != NULL);
Packit ae235b
  g_assert (binding->source_property != NULL);
Packit ae235b
  g_assert (binding->target_property != NULL);
Packit ae235b
Packit ae235b
  /* we assume a check was performed prior to construction - since
Packit ae235b
   * g_object_bind_property_full() does it; we cannot fail construction
Packit ae235b
   * anyway, so it would be hard for use to properly warn here
Packit ae235b
   */
Packit ae235b
  binding->source_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (binding->source), binding->source_property);
Packit ae235b
  binding->target_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (binding->target), binding->target_property);
Packit ae235b
  g_assert (binding->source_pspec != NULL);
Packit ae235b
  g_assert (binding->target_pspec != NULL);
Packit ae235b
Packit ae235b
  /* switch to the invert boolean transform if needed */
Packit ae235b
  if (binding->flags & G_BINDING_INVERT_BOOLEAN)
Packit ae235b
    transform_func = default_invert_boolean_transform;
Packit ae235b
Packit ae235b
  /* set the default transformation functions here */
Packit ae235b
  binding->transform_s2t = transform_func;
Packit ae235b
  binding->transform_t2s = transform_func;
Packit ae235b
Packit ae235b
  binding->transform_data = NULL;
Packit ae235b
  binding->notify = NULL;
Packit ae235b
Packit ae235b
  source_property_detail = g_quark_from_string (binding->source_property);
Packit ae235b
  source_notify_closure = g_cclosure_new (G_CALLBACK (on_source_notify),
Packit ae235b
                                          binding, NULL);
Packit ae235b
  binding->source_notify = g_signal_connect_closure_by_id (binding->source,
Packit ae235b
                                                           gobject_notify_signal_id,
Packit ae235b
                                                           source_property_detail,
Packit ae235b
                                                           source_notify_closure,
Packit ae235b
                                                           FALSE);
Packit ae235b
Packit ae235b
  g_object_weak_ref (binding->source, weak_unbind, binding);
Packit ae235b
Packit ae235b
  if (binding->flags & G_BINDING_BIDIRECTIONAL)
Packit ae235b
    {
Packit ae235b
      GQuark target_property_detail;
Packit ae235b
      GClosure *target_notify_closure;
Packit ae235b
Packit ae235b
      target_property_detail = g_quark_from_string (binding->target_property);
Packit ae235b
      target_notify_closure = g_cclosure_new (G_CALLBACK (on_target_notify),
Packit ae235b
                                              binding, NULL);
Packit ae235b
      binding->target_notify = g_signal_connect_closure_by_id (binding->target,
Packit ae235b
                                                               gobject_notify_signal_id,
Packit ae235b
                                                               target_property_detail,
Packit ae235b
                                                               target_notify_closure,
Packit ae235b
                                                               FALSE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (binding->target != binding->source)
Packit ae235b
    g_object_weak_ref (binding->target, weak_unbind, binding);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_class_init (GBindingClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_notify_signal_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
Packit ae235b
  g_assert (gobject_notify_signal_id != 0);
Packit ae235b
Packit ae235b
  gobject_class->constructed = g_binding_constructed;
Packit ae235b
  gobject_class->set_property = g_binding_set_property;
Packit ae235b
  gobject_class->get_property = g_binding_get_property;
Packit ae235b
  gobject_class->finalize = g_binding_finalize;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GBinding:source:
Packit ae235b
   *
Packit ae235b
   * The #GObject that should be used as the source of the binding
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_SOURCE,
Packit ae235b
                                   g_param_spec_object ("source",
Packit ae235b
                                                        P_("Source"),
Packit ae235b
                                                        P_("The source of the binding"),
Packit ae235b
                                                        G_TYPE_OBJECT,
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GBinding:target:
Packit ae235b
   *
Packit ae235b
   * The #GObject that should be used as the target of the binding
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TARGET,
Packit ae235b
                                   g_param_spec_object ("target",
Packit ae235b
                                                        P_("Target"),
Packit ae235b
                                                        P_("The target of the binding"),
Packit ae235b
                                                        G_TYPE_OBJECT,
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GBinding:source-property:
Packit ae235b
   *
Packit ae235b
   * The name of the property of #GBinding:source that should be used
Packit ae235b
   * as the source of the binding
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_SOURCE_PROPERTY,
Packit ae235b
                                   g_param_spec_string ("source-property",
Packit ae235b
                                                        P_("Source Property"),
Packit ae235b
                                                        P_("The property on the source to bind"),
Packit ae235b
                                                        NULL,
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GBinding:target-property:
Packit ae235b
   *
Packit ae235b
   * The name of the property of #GBinding:target that should be used
Packit ae235b
   * as the target of the binding
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TARGET_PROPERTY,
Packit ae235b
                                   g_param_spec_string ("target-property",
Packit ae235b
                                                        P_("Target Property"),
Packit ae235b
                                                        P_("The property on the target to bind"),
Packit ae235b
                                                        NULL,
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GBinding:flags:
Packit ae235b
   *
Packit ae235b
   * Flags to be used to control the #GBinding
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_FLAGS,
Packit ae235b
                                   g_param_spec_flags ("flags",
Packit ae235b
                                                       P_("Flags"),
Packit ae235b
                                                       P_("The binding flags"),
Packit ae235b
                                                       G_TYPE_BINDING_FLAGS,
Packit ae235b
                                                       G_BINDING_DEFAULT,
Packit ae235b
                                                       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                       G_PARAM_READWRITE |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_binding_init (GBinding *binding)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_get_flags:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Retrieves the flags passed when constructing the #GBinding.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GBindingFlags used by the #GBinding
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GBindingFlags
Packit ae235b
g_binding_get_flags (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_BINDING (binding), G_BINDING_DEFAULT);
Packit ae235b
Packit ae235b
  return binding->flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_get_source:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Retrieves the #GObject instance used as the source of the binding.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the source #GObject
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GObject *
Packit ae235b
g_binding_get_source (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
Packit ae235b
Packit ae235b
  return binding->source;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_get_target:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Retrieves the #GObject instance used as the target of the binding.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the target #GObject
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GObject *
Packit ae235b
g_binding_get_target (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
Packit ae235b
Packit ae235b
  return binding->target;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_get_source_property:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Retrieves the name of the property of #GBinding:source used as the source
Packit ae235b
 * of the binding.
Packit ae235b
 *
Packit ae235b
 * Returns: the name of the source property
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_binding_get_source_property (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
Packit ae235b
Packit ae235b
  return binding->source_property;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_get_target_property:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Retrieves the name of the property of #GBinding:target used as the target
Packit ae235b
 * of the binding.
Packit ae235b
 *
Packit ae235b
 * Returns: the name of the target property
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_binding_get_target_property (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
Packit ae235b
Packit ae235b
  return binding->target_property;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_binding_unbind:
Packit ae235b
 * @binding: a #GBinding
Packit ae235b
 *
Packit ae235b
 * Explicitly releases the binding between the source and the target
Packit ae235b
 * property expressed by @binding.
Packit ae235b
 *
Packit ae235b
 * This function will release the reference that is being held on
Packit ae235b
 * the @binding instance; if you want to hold on to the #GBinding instance
Packit ae235b
 * after calling g_binding_unbind(), you will need to hold a reference
Packit ae235b
 * to it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_binding_unbind (GBinding *binding)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_BINDING (binding));
Packit ae235b
Packit ae235b
  g_binding_unbind_internal (binding, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_object_bind_property_full:
Packit ae235b
 * @source: (type GObject.Object): the source #GObject
Packit ae235b
 * @source_property: the property on @source to bind
Packit ae235b
 * @target: (type GObject.Object): the target #GObject
Packit ae235b
 * @target_property: the property on @target to bind
Packit ae235b
 * @flags: flags to pass to #GBinding
Packit ae235b
 * @transform_to: (scope notified) (nullable): the transformation function
Packit ae235b
 *     from the @source to the @target, or %NULL to use the default
Packit ae235b
 * @transform_from: (scope notified) (nullable): the transformation function
Packit ae235b
 *     from the @target to the @source, or %NULL to use the default
Packit ae235b
 * @user_data: custom data to be passed to the transformation functions,
Packit ae235b
 *     or %NULL
Packit ae235b
 * @notify: (nullable): a function to call when disposing the binding, to free
Packit ae235b
 *     resources used by the transformation functions, or %NULL if not required
Packit ae235b
 *
Packit ae235b
 * Complete version of g_object_bind_property().
Packit ae235b
 *
Packit ae235b
 * Creates a binding between @source_property on @source and @target_property
Packit ae235b
 * on @target, allowing you to set the transformation functions to be used by
Packit ae235b
 * the binding.
Packit ae235b
 *
Packit ae235b
 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual:
Packit ae235b
 * if @target_property on @target changes then the @source_property on @source
Packit ae235b
 * will be updated as well. The @transform_from function is only used in case
Packit ae235b
 * of bidirectional bindings, otherwise it will be ignored
Packit ae235b
 *
Packit ae235b
 * The binding will automatically be removed when either the @source or the
Packit ae235b
 * @target instances are finalized. To remove the binding without affecting the
Packit ae235b
 * @source and the @target you can just call g_object_unref() on the returned
Packit ae235b
 * #GBinding instance.
Packit ae235b
 *
Packit ae235b
 * A #GObject can have multiple bindings.
Packit ae235b
 *
Packit ae235b
 * The same @user_data parameter will be used for both @transform_to
Packit ae235b
 * and @transform_from transformation functions; the @notify function will
Packit ae235b
 * be called once, when the binding is removed. If you need different data
Packit ae235b
 * for each transformation function, please use
Packit ae235b
 * g_object_bind_property_with_closures() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GBinding instance representing the
Packit ae235b
 *     binding between the two #GObject instances. The binding is released
Packit ae235b
 *     whenever the #GBinding reference count reaches zero.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GBinding *
Packit ae235b
g_object_bind_property_full (gpointer               source,
Packit ae235b
                             const gchar           *source_property,
Packit ae235b
                             gpointer               target,
Packit ae235b
                             const gchar           *target_property,
Packit ae235b
                             GBindingFlags          flags,
Packit ae235b
                             GBindingTransformFunc  transform_to,
Packit ae235b
                             GBindingTransformFunc  transform_from,
Packit ae235b
                             gpointer               user_data,
Packit ae235b
                             GDestroyNotify         notify)
Packit ae235b
{
Packit ae235b
  GParamSpec *pspec;
Packit ae235b
  GBinding *binding;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OBJECT (source), NULL);
Packit ae235b
  g_return_val_if_fail (source_property != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_OBJECT (target), NULL);
Packit ae235b
  g_return_val_if_fail (target_property != NULL, NULL);
Packit ae235b
Packit ae235b
  if (source == target && g_strcmp0 (source_property, target_property) == 0)
Packit ae235b
    {
Packit ae235b
      g_warning ("Unable to bind the same property on the same instance");
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* remove the G_BINDING_INVERT_BOOLEAN flag in case we have
Packit ae235b
   * custom transformation functions
Packit ae235b
   */
Packit ae235b
  if ((flags & G_BINDING_INVERT_BOOLEAN) &&
Packit ae235b
      (transform_to != NULL || transform_from != NULL))
Packit ae235b
    {
Packit ae235b
      flags &= ~G_BINDING_INVERT_BOOLEAN;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (source), source_property);
Packit ae235b
  if (pspec == NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The source object of type %s has no property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (source),
Packit ae235b
                 source_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!(pspec->flags & G_PARAM_READABLE))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The source object of type %s has no readable property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (source),
Packit ae235b
                 source_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((flags & G_BINDING_BIDIRECTIONAL) &&
Packit ae235b
      ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) || !(pspec->flags & G_PARAM_WRITABLE)))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The source object of type %s has no writable property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (source),
Packit ae235b
                 source_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((flags & G_BINDING_INVERT_BOOLEAN) &&
Packit ae235b
      !(G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The G_BINDING_INVERT_BOOLEAN flag can only be used "
Packit ae235b
                 "when binding boolean properties; the source property '%s' "
Packit ae235b
                 "is of type '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 source_property,
Packit ae235b
                 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (target), target_property);
Packit ae235b
  if (pspec == NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The target object of type %s has no property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (target),
Packit ae235b
                 target_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) || !(pspec->flags & G_PARAM_WRITABLE))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The target object of type %s has no writable property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (target),
Packit ae235b
                 target_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((flags & G_BINDING_BIDIRECTIONAL) &&
Packit ae235b
      !(pspec->flags & G_PARAM_READABLE))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The target object of type %s has no readable property called '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 G_OBJECT_TYPE_NAME (target),
Packit ae235b
                 target_property);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((flags & G_BINDING_INVERT_BOOLEAN) &&
Packit ae235b
      !(G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: The G_BINDING_INVERT_BOOLEAN flag can only be used "
Packit ae235b
                 "when binding boolean properties; the target property '%s' "
Packit ae235b
                 "is of type '%s'",
Packit ae235b
                 G_STRLOC,
Packit ae235b
                 target_property,
Packit ae235b
                 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  binding = g_object_new (G_TYPE_BINDING,
Packit ae235b
                          "source", source,
Packit ae235b
                          "source-property", source_property,
Packit ae235b
                          "target", target,
Packit ae235b
                          "target-property", target_property,
Packit ae235b
                          "flags", flags,
Packit ae235b
                          NULL);
Packit ae235b
Packit ae235b
  if (transform_to != NULL)
Packit ae235b
    binding->transform_s2t = transform_to;
Packit ae235b
Packit ae235b
  if (transform_from != NULL)
Packit ae235b
    binding->transform_t2s = transform_from;
Packit ae235b
Packit ae235b
  binding->transform_data = user_data;
Packit ae235b
  binding->notify = notify;
Packit ae235b
Packit ae235b
  /* synchronize the target with the source by faking an emission of
Packit ae235b
   * the ::notify signal for the source property; this will also take
Packit ae235b
   * care of the bidirectional binding case because the eventual change
Packit ae235b
   * will emit a notification on the target
Packit ae235b
   */
Packit ae235b
  if (flags & G_BINDING_SYNC_CREATE)
Packit ae235b
    on_source_notify (binding->source, binding->source_pspec, binding);
Packit ae235b
Packit ae235b
  return binding;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_object_bind_property:
Packit ae235b
 * @source: (type GObject.Object): the source #GObject
Packit ae235b
 * @source_property: the property on @source to bind
Packit ae235b
 * @target: (type GObject.Object): the target #GObject
Packit ae235b
 * @target_property: the property on @target to bind
Packit ae235b
 * @flags: flags to pass to #GBinding
Packit ae235b
 *
Packit ae235b
 * Creates a binding between @source_property on @source and @target_property
Packit ae235b
 * on @target. Whenever the @source_property is changed the @target_property is
Packit ae235b
 * updated using the same value. For instance:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   g_object_bind_property (action, "active", widget, "sensitive", 0);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Will result in the "sensitive" property of the widget #GObject instance to be
Packit ae235b
 * updated with the same value of the "active" property of the action #GObject
Packit ae235b
 * instance.
Packit ae235b
 *
Packit ae235b
 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual:
Packit ae235b
 * if @target_property on @target changes then the @source_property on @source
Packit ae235b
 * will be updated as well.
Packit ae235b
 *
Packit ae235b
 * The binding will automatically be removed when either the @source or the
Packit ae235b
 * @target instances are finalized. To remove the binding without affecting the
Packit ae235b
 * @source and the @target you can just call g_object_unref() on the returned
Packit ae235b
 * #GBinding instance.
Packit ae235b
 *
Packit ae235b
 * A #GObject can have multiple bindings.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GBinding instance representing the
Packit ae235b
 *     binding between the two #GObject instances. The binding is released
Packit ae235b
 *     whenever the #GBinding reference count reaches zero.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GBinding *
Packit ae235b
g_object_bind_property (gpointer       source,
Packit ae235b
                        const gchar   *source_property,
Packit ae235b
                        gpointer       target,
Packit ae235b
                        const gchar   *target_property,
Packit ae235b
                        GBindingFlags  flags)
Packit ae235b
{
Packit ae235b
  /* type checking is done in g_object_bind_property_full() */
Packit ae235b
Packit ae235b
  return g_object_bind_property_full (source, source_property,
Packit ae235b
                                      target, target_property,
Packit ae235b
                                      flags,
Packit ae235b
                                      NULL,
Packit ae235b
                                      NULL,
Packit ae235b
                                      NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct _TransformData
Packit ae235b
{
Packit ae235b
  GClosure *transform_to_closure;
Packit ae235b
  GClosure *transform_from_closure;
Packit ae235b
} TransformData;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
bind_with_closures_transform_to (GBinding     *binding,
Packit ae235b
                                 const GValue *source,
Packit ae235b
                                 GValue       *target,
Packit ae235b
                                 gpointer      data)
Packit ae235b
{
Packit ae235b
  TransformData *t_data = data;
Packit ae235b
  GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
  GValue retval = G_VALUE_INIT;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_BINDING);
Packit ae235b
  g_value_set_object (&params[0], binding);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_VALUE);
Packit ae235b
  g_value_set_boxed (&params[1], source);
Packit ae235b
Packit ae235b
  g_value_init (&params[2], G_TYPE_VALUE);
Packit ae235b
  g_value_set_boxed (&params[2], target);
Packit ae235b
Packit ae235b
  g_value_init (&retval, G_TYPE_BOOLEAN);
Packit ae235b
  g_value_set_boolean (&retval, FALSE);
Packit ae235b
Packit ae235b
  g_closure_invoke (t_data->transform_to_closure, &retval, 3, params, NULL);
Packit ae235b
Packit ae235b
  res = g_value_get_boolean (&retval);
Packit ae235b
  if (res)
Packit ae235b
    {
Packit ae235b
      const GValue *out_value = g_value_get_boxed (&params[2]);
Packit ae235b
Packit ae235b
      g_assert (out_value != NULL);
Packit ae235b
Packit ae235b
      g_value_copy (out_value, target);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_value_unset (&params[0]);
Packit ae235b
  g_value_unset (&params[1]);
Packit ae235b
  g_value_unset (&params[2]);
Packit ae235b
  g_value_unset (&retval);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
bind_with_closures_transform_from (GBinding     *binding,
Packit ae235b
                                   const GValue *source,
Packit ae235b
                                   GValue       *target,
Packit ae235b
                                   gpointer      data)
Packit ae235b
{
Packit ae235b
  TransformData *t_data = data;
Packit ae235b
  GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
  GValue retval = G_VALUE_INIT;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_BINDING);
Packit ae235b
  g_value_set_object (&params[0], binding);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_VALUE);
Packit ae235b
  g_value_set_boxed (&params[1], source);
Packit ae235b
Packit ae235b
  g_value_init (&params[2], G_TYPE_VALUE);
Packit ae235b
  g_value_set_boxed (&params[2], target);
Packit ae235b
Packit ae235b
  g_value_init (&retval, G_TYPE_BOOLEAN);
Packit ae235b
  g_value_set_boolean (&retval, FALSE);
Packit ae235b
Packit ae235b
  g_closure_invoke (t_data->transform_from_closure, &retval, 3, params, NULL);
Packit ae235b
Packit ae235b
  res = g_value_get_boolean (&retval);
Packit ae235b
  if (res)
Packit ae235b
    {
Packit ae235b
      const GValue *out_value = g_value_get_boxed (&params[2]);
Packit ae235b
Packit ae235b
      g_assert (out_value != NULL);
Packit ae235b
Packit ae235b
      g_value_copy (out_value, target);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_value_unset (&params[0]);
Packit ae235b
  g_value_unset (&params[1]);
Packit ae235b
  g_value_unset (&params[2]);
Packit ae235b
  g_value_unset (&retval);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
bind_with_closures_free_func (gpointer data)
Packit ae235b
{
Packit ae235b
  TransformData *t_data = data;
Packit ae235b
Packit ae235b
  if (t_data->transform_to_closure != NULL)
Packit ae235b
    g_closure_unref (t_data->transform_to_closure);
Packit ae235b
Packit ae235b
  if (t_data->transform_from_closure != NULL)
Packit ae235b
    g_closure_unref (t_data->transform_from_closure);
Packit ae235b
Packit ae235b
  g_slice_free (TransformData, t_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_object_bind_property_with_closures: (rename-to g_object_bind_property_full)
Packit ae235b
 * @source: (type GObject.Object): the source #GObject
Packit ae235b
 * @source_property: the property on @source to bind
Packit ae235b
 * @target: (type GObject.Object): the target #GObject
Packit ae235b
 * @target_property: the property on @target to bind
Packit ae235b
 * @flags: flags to pass to #GBinding
Packit ae235b
 * @transform_to: a #GClosure wrapping the transformation function
Packit ae235b
 *     from the @source to the @target, or %NULL to use the default
Packit ae235b
 * @transform_from: a #GClosure wrapping the transformation function
Packit ae235b
 *     from the @target to the @source, or %NULL to use the default
Packit ae235b
 *
Packit ae235b
 * Creates a binding between @source_property on @source and @target_property
Packit ae235b
 * on @target, allowing you to set the transformation functions to be used by
Packit ae235b
 * the binding.
Packit ae235b
 *
Packit ae235b
 * This function is the language bindings friendly version of
Packit ae235b
 * g_object_bind_property_full(), using #GClosures instead of
Packit ae235b
 * function pointers.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GBinding instance representing the
Packit ae235b
 *     binding between the two #GObject instances. The binding is released
Packit ae235b
 *     whenever the #GBinding reference count reaches zero.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GBinding *
Packit ae235b
g_object_bind_property_with_closures (gpointer       source,
Packit ae235b
                                      const gchar   *source_property,
Packit ae235b
                                      gpointer       target,
Packit ae235b
                                      const gchar   *target_property,
Packit ae235b
                                      GBindingFlags  flags,
Packit ae235b
                                      GClosure      *transform_to,
Packit ae235b
                                      GClosure      *transform_from)
Packit ae235b
{
Packit ae235b
  TransformData *data;
Packit ae235b
Packit ae235b
  data = g_slice_new0 (TransformData);
Packit ae235b
Packit ae235b
  if (transform_to != NULL)
Packit ae235b
    {
Packit ae235b
      if (G_CLOSURE_NEEDS_MARSHAL (transform_to))
Packit ae235b
        g_closure_set_marshal (transform_to, g_cclosure_marshal_BOOLEAN__BOXED_BOXED);
Packit ae235b
Packit ae235b
      data->transform_to_closure = g_closure_ref (transform_to);
Packit ae235b
      g_closure_sink (data->transform_to_closure);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (transform_from != NULL)
Packit ae235b
    {
Packit ae235b
      if (G_CLOSURE_NEEDS_MARSHAL (transform_from))
Packit ae235b
        g_closure_set_marshal (transform_from, g_cclosure_marshal_BOOLEAN__BOXED_BOXED);
Packit ae235b
Packit ae235b
      data->transform_from_closure = g_closure_ref (transform_from);
Packit ae235b
      g_closure_sink (data->transform_from_closure);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_object_bind_property_full (source, source_property,
Packit ae235b
                                      target, target_property,
Packit ae235b
                                      flags,
Packit ae235b
                                      transform_to != NULL ? bind_with_closures_transform_to : NULL,
Packit ae235b
                                      transform_from != NULL ? bind_with_closures_transform_from : NULL,
Packit ae235b
                                      data,
Packit ae235b
                                      bind_with_closures_free_func);
Packit ae235b
}