GObject parent_instance;
gint foo;
+ gint bar;
gdouble value;
+ gboolean toggle;
} BindingSource;
typedef struct _BindingSourceClass
PROP_SOURCE_0,
PROP_SOURCE_FOO,
-
- PROP_SOURCE_VALUE
+ PROP_SOURCE_BAR,
+ PROP_SOURCE_VALUE,
+ PROP_SOURCE_TOGGLE
};
+static GType binding_source_get_type (void);
G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
static void
source->foo = g_value_get_int (value);
break;
+ case PROP_SOURCE_BAR:
+ source->bar = g_value_get_int (value);
+ break;
+
case PROP_SOURCE_VALUE:
source->value = g_value_get_double (value);
break;
+ case PROP_SOURCE_TOGGLE:
+ source->toggle = g_value_get_boolean (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
g_value_set_int (value, source->foo);
break;
+ case PROP_SOURCE_BAR:
+ g_value_set_int (value, source->bar);
+ break;
+
case PROP_SOURCE_VALUE:
g_value_set_double (value, source->value);
break;
+ case PROP_SOURCE_TOGGLE:
+ g_value_set_boolean (value, source->toggle);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
-1, 100,
0,
G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_SOURCE_BAR,
+ g_param_spec_int ("bar", "Bar", "Bar",
+ -1, 100,
+ 0,
+ G_PARAM_READWRITE));
g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
g_param_spec_double ("value", "Value", "Value",
-100.0, 200.0,
0.0,
G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
+ g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+ FALSE,
+ G_PARAM_READWRITE));
}
static void
gint bar;
gdouble value;
+ gboolean toggle;
} BindingTarget;
typedef struct _BindingTargetClass
PROP_TARGET_0,
PROP_TARGET_BAR,
-
- PROP_TARGET_VALUE
+ PROP_TARGET_VALUE,
+ PROP_TARGET_TOGGLE
};
+static GType binding_target_get_type (void);
G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
static void
target->value = g_value_get_double (value);
break;
+ case PROP_TARGET_TOGGLE:
+ target->toggle = g_value_get_boolean (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
g_value_set_double (value, target->value);
break;
+ case PROP_TARGET_TOGGLE:
+ g_value_set_boolean (value, target->toggle);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
-1, 100,
0,
G_PARAM_READWRITE));
- g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
+ g_object_class_install_property (gobject_class, PROP_TARGET_VALUE,
g_param_spec_double ("value", "Value", "Value",
-100.0, 200.0,
0.0,
G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
+ g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+ FALSE,
+ G_PARAM_READWRITE));
}
static void
static gboolean
celsius_to_fahrenheit (GBinding *binding,
- const GValue *source_value,
- GValue *target_value,
+ const GValue *from_value,
+ GValue *to_value,
gpointer user_data G_GNUC_UNUSED)
{
gdouble celsius, fahrenheit;
- g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
- g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
- celsius = g_value_get_double (source_value);
+ celsius = g_value_get_double (from_value);
fahrenheit = (9 * celsius / 5) + 32.0;
if (g_test_verbose ())
g_print ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
- g_value_set_double (target_value, fahrenheit);
+ g_value_set_double (to_value, fahrenheit);
return TRUE;
}
static gboolean
fahrenheit_to_celsius (GBinding *binding,
- const GValue *source_value,
- GValue *target_value,
+ const GValue *from_value,
+ GValue *to_value,
gpointer user_data G_GNUC_UNUSED)
{
gdouble celsius, fahrenheit;
- g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
- g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
- fahrenheit = g_value_get_double (source_value);
+ fahrenheit = g_value_get_double (from_value);
celsius = 5 * (fahrenheit - 32.0) / 9;
if (g_test_verbose ())
g_print ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
- g_value_set_double (target_value, celsius);
+ g_value_set_double (to_value, celsius);
return TRUE;
}
target, "bar",
G_BINDING_DEFAULT);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
g_assert ((BindingSource *) g_binding_get_source (binding) == source);
g_assert ((BindingTarget *) g_binding_get_target (binding) == target);
g_assert_cmpstr (g_binding_get_source_property (binding), ==, "foo");
g_object_unref (source);
g_object_unref (target);
+ g_assert (binding == NULL);
}
static void
binding = g_object_bind_property (source, "foo",
target, "bar",
G_BINDING_BIDIRECTIONAL);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
g_object_set (source, "foo", 42, NULL);
g_assert_cmpint (source->foo, ==, target->bar);
g_object_unref (source);
g_object_unref (target);
+ g_assert (binding == NULL);
}
static void
}
static void
-binding_transform (void)
+binding_transform_default (void)
{
BindingSource *source = g_object_new (binding_source_get_type (), NULL);
BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
GBinding *binding;
+ gpointer src, trg;
+ gchar *src_prop, *trg_prop;
+ GBindingFlags flags;
+
+ binding = g_object_bind_property (source, "foo",
+ target, "value",
+ G_BINDING_BIDIRECTIONAL);
+
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+
+ g_object_get (binding,
+ "source", &src,
+ "source-property", &src_prop,
+ "target", &trg,
+ "target-property", &trg_prop,
+ "flags", &flags,
+ NULL);
+ g_assert (src == source);
+ g_assert (trg == target);
+ g_assert_cmpstr (src_prop, ==, "foo");
+ g_assert_cmpstr (trg_prop, ==, "value");
+ g_assert_cmpint (flags, ==, G_BINDING_BIDIRECTIONAL);
+ g_object_unref (src);
+ g_object_unref (trg);
+ g_free (src_prop);
+ g_free (trg_prop);
+
+ g_object_set (source, "foo", 24, NULL);
+ g_assert_cmpfloat (target->value, ==, 24.0);
+
+ g_object_set (target, "value", 69.0, NULL);
+ g_assert_cmpint (source->foo, ==, 69);
+
+ g_object_unref (target);
+ g_object_unref (source);
+ g_assert (binding == NULL);
+}
+
+static void
+binding_transform (void)
+{
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+ GBinding *binding G_GNUC_UNUSED;
gboolean unused_data = FALSE;
binding = g_object_bind_property_full (source, "value",
}
static void
-binding_transform_marshal (GClosure *closure,
- GValue *return_value,
- guint n_param_values,
- const GValue *param_values,
- gpointer invocation_hint G_GNUC_UNUSED,
- gpointer marshal_data)
-{
- typedef gboolean (* GMarshalFunc_BOOLEAN__VALUE_VALUE) (gpointer data1,
- gpointer arg_2,
- gpointer arg_3,
- gpointer data2);
- register GMarshalFunc_BOOLEAN__VALUE_VALUE callback;
- register GCClosure *cc = (GCClosure *) closure;
- register gpointer data1, data2;
- gboolean v_return;
-
- if (G_CCLOSURE_SWAP_DATA (closure))
- {
- data1 = closure->data;
- data2 = g_value_peek_pointer (param_values + 0);
- }
- else
- {
- data1 = g_value_peek_pointer (param_values + 0);
- data2 = closure->data;
- }
-
- callback = (GMarshalFunc_BOOLEAN__VALUE_VALUE) (marshal_data ? marshal_data : cc->callback);
- v_return = callback (data1,
- g_value_get_boxed (param_values + 1),
- g_value_get_boxed (param_values + 2),
- data2);
-
- g_value_set_boolean (return_value, v_return);
-}
-
-static void
binding_transform_closure (void)
{
BindingSource *source = g_object_new (binding_source_get_type (), NULL);
BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
- GBinding *binding;
+ GBinding *binding G_GNUC_UNUSED;
gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
GClosure *c2f_clos, *f2c_clos;
c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
- g_closure_set_marshal (c2f_clos, binding_transform_marshal);
f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
- g_closure_set_marshal (f2c_clos, binding_transform_marshal);
binding = g_object_bind_property_with_closures (source, "value",
target, "value",
BindingSource *c = g_object_new (binding_source_get_type (), NULL);
GBinding *binding_1, *binding_2;
+ g_test_bug ("621782");
+
/* A -> B, B -> C */
binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
+ g_object_add_weak_pointer (G_OBJECT (binding_1), (gpointer *) &binding_1);
+
binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
+ g_object_add_weak_pointer (G_OBJECT (binding_2), (gpointer *) &binding_2);
/* verify the chain */
g_object_set (a, "foo", 42, NULL);
/* unbind A -> B and B -> C */
g_object_unref (binding_1);
+ g_assert (binding_1 == NULL);
g_object_unref (binding_2);
+ g_assert (binding_2 == NULL);
/* bind A -> C directly */
binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
g_object_unref (target);
}
+static void
+binding_invert_boolean (void)
+{
+ BindingSource *source = g_object_new (binding_source_get_type (),
+ "toggle", TRUE,
+ NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (),
+ "toggle", FALSE,
+ NULL);
+ GBinding *binding;
+
+ binding = g_object_bind_property (source, "toggle",
+ target, "toggle",
+ G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN);
+
+ g_assert (source->toggle);
+ g_assert (!target->toggle);
+
+ g_object_set (source, "toggle", FALSE, NULL);
+ g_assert (!source->toggle);
+ g_assert (target->toggle);
+
+ g_object_set (target, "toggle", FALSE, NULL);
+ g_assert (source->toggle);
+ g_assert (!target->toggle);
+
+ g_object_unref (binding);
+ g_object_unref (source);
+ g_object_unref (target);
+}
+
+static void
+binding_same_object (void)
+{
+ BindingSource *source = g_object_new (binding_source_get_type (),
+ "foo", 100,
+ "bar", 50,
+ NULL);
+ GBinding *binding;
+
+ binding = g_object_bind_property (source, "foo",
+ source, "bar",
+ G_BINDING_BIDIRECTIONAL);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+
+ g_object_set (source, "foo", 10, NULL);
+ g_assert_cmpint (source->foo, ==, 10);
+ g_assert_cmpint (source->bar, ==, 10);
+ g_object_set (source, "bar", 30, NULL);
+ g_assert_cmpint (source->foo, ==, 30);
+ g_assert_cmpint (source->bar, ==, 30);
+
+ g_object_unref (source);
+ g_assert (binding == NULL);
+}
+
+static void
+binding_unbind (void)
+{
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+ GBinding *binding;
+
+ binding = g_object_bind_property (source, "foo",
+ target, "bar",
+ G_BINDING_DEFAULT);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+
+ g_object_set (source, "foo", 42, NULL);
+ g_assert_cmpint (source->foo, ==, target->bar);
+
+ g_object_set (target, "bar", 47, NULL);
+ g_assert_cmpint (source->foo, !=, target->bar);
+
+ g_binding_unbind (binding);
+ g_assert (binding == NULL);
+
+ g_object_set (source, "foo", 0, NULL);
+ g_assert_cmpint (source->foo, !=, target->bar);
+
+ g_object_unref (source);
+ g_object_unref (target);
+}
+
int
main (int argc, char *argv[])
{
- g_type_init ();
g_test_init (&argc, &argv, NULL);
+ g_test_bug_base ("http://bugzilla.gnome.org/");
+
g_test_add_func ("/binding/default", binding_default);
g_test_add_func ("/binding/bidirectional", binding_bidirectional);
g_test_add_func ("/binding/transform", binding_transform);
+ g_test_add_func ("/binding/transform-default", binding_transform_default);
g_test_add_func ("/binding/transform-closure", binding_transform_closure);
g_test_add_func ("/binding/chain", binding_chain);
g_test_add_func ("/binding/sync-create", binding_sync_create);
+ g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
+ g_test_add_func ("/binding/same-object", binding_same_object);
+ g_test_add_func ("/binding/unbind", binding_unbind);
return g_test_run ();
}