3 #include <glib-object.h>
5 typedef struct _BindingSource
7 GObject parent_instance;
13 typedef struct _BindingSourceClass
15 GObjectClass parent_class;
27 G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
30 binding_source_set_property (GObject *gobject,
35 BindingSource *source = (BindingSource *) gobject;
40 source->foo = g_value_get_int (value);
43 case PROP_SOURCE_VALUE:
44 source->value = g_value_get_double (value);
48 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
53 binding_source_get_property (GObject *gobject,
58 BindingSource *source = (BindingSource *) gobject;
63 g_value_set_int (value, source->foo);
66 case PROP_SOURCE_VALUE:
67 g_value_set_double (value, source->value);
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
76 binding_source_class_init (BindingSourceClass *klass)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
80 gobject_class->set_property = binding_source_set_property;
81 gobject_class->get_property = binding_source_get_property;
83 g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
84 g_param_spec_int ("foo", "Foo", "Foo",
88 g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
89 g_param_spec_double ("value", "Value", "Value",
96 binding_source_init (BindingSource *self)
100 typedef struct _BindingTarget
102 GObject parent_instance;
108 typedef struct _BindingTargetClass
110 GObjectClass parent_class;
111 } BindingTargetClass;
122 G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
125 binding_target_set_property (GObject *gobject,
130 BindingTarget *target = (BindingTarget *) gobject;
134 case PROP_TARGET_BAR:
135 target->bar = g_value_get_int (value);
138 case PROP_TARGET_VALUE:
139 target->value = g_value_get_double (value);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
148 binding_target_get_property (GObject *gobject,
153 BindingTarget *target = (BindingTarget *) gobject;
157 case PROP_TARGET_BAR:
158 g_value_set_int (value, target->bar);
161 case PROP_TARGET_VALUE:
162 g_value_set_double (value, target->value);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
171 binding_target_class_init (BindingTargetClass *klass)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
175 gobject_class->set_property = binding_target_set_property;
176 gobject_class->get_property = binding_target_get_property;
178 g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
179 g_param_spec_int ("bar", "Bar", "Bar",
183 g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
184 g_param_spec_double ("value", "Value", "Value",
191 binding_target_init (BindingTarget *self)
196 celsius_to_fahrenheit (GBinding *binding,
197 const GValue *source_value,
198 GValue *target_value,
199 gpointer user_data G_GNUC_UNUSED)
201 gdouble celsius, fahrenheit;
203 g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
204 g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
206 celsius = g_value_get_double (source_value);
207 fahrenheit = (9 * celsius / 5) + 32.0;
209 if (g_test_verbose ())
210 g_print ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
212 g_value_set_double (target_value, fahrenheit);
218 fahrenheit_to_celsius (GBinding *binding,
219 const GValue *source_value,
220 GValue *target_value,
221 gpointer user_data G_GNUC_UNUSED)
223 gdouble celsius, fahrenheit;
225 g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
226 g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
228 fahrenheit = g_value_get_double (source_value);
229 celsius = 5 * (fahrenheit - 32.0) / 9;
231 if (g_test_verbose ())
232 g_print ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
234 g_value_set_double (target_value, celsius);
240 binding_default (void)
242 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
243 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
246 binding = g_object_bind_property (source, "foo",
250 g_object_set (source, "foo", 42, NULL);
251 g_assert_cmpint (source->foo, ==, target->bar);
253 g_object_set (target, "bar", 47, NULL);
254 g_assert_cmpint (source->foo, !=, target->bar);
256 g_object_unref (binding);
258 g_object_set (source, "foo", 0, NULL);
259 g_assert_cmpint (source->foo, !=, target->bar);
261 g_object_unref (source);
262 g_object_unref (target);
266 binding_bidirectional (void)
268 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
269 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
272 binding = g_object_bind_property (source, "foo",
274 G_BINDING_BIDIRECTIONAL);
276 g_object_set (source, "foo", 42, NULL);
277 g_assert_cmpint (source->foo, ==, target->bar);
279 g_object_set (target, "bar", 47, NULL);
280 g_assert_cmpint (source->foo, ==, target->bar);
282 g_object_unref (binding);
284 g_object_set (source, "foo", 0, NULL);
285 g_assert_cmpint (source->foo, !=, target->bar);
287 g_object_unref (source);
288 g_object_unref (target);
292 binding_transform (void)
294 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
295 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
298 binding = g_object_bind_property_full (source, "value",
300 G_BINDING_BIDIRECTIONAL,
301 celsius_to_fahrenheit,
302 fahrenheit_to_celsius,
305 g_object_set (source, "value", 24.0, NULL);
306 g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
308 g_object_set (target, "value", 69.0, NULL);
309 g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
311 g_object_unref (source);
312 g_object_unref (target);
318 BindingSource *a = g_object_new (binding_source_get_type (), NULL);
319 BindingSource *b = g_object_new (binding_source_get_type (), NULL);
320 BindingSource *c = g_object_new (binding_source_get_type (), NULL);
321 GBinding *binding_1, *binding_2;
324 binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
325 binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
327 /* verify the chain */
328 g_object_set (a, "foo", 42, NULL);
329 g_assert_cmpint (a->foo, ==, b->foo);
330 g_assert_cmpint (b->foo, ==, c->foo);
332 /* unbind A -> B and B -> C */
333 g_object_unref (binding_1);
334 g_object_unref (binding_2);
336 /* bind A -> C directly */
337 binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
339 /* verify the chain is broken */
340 g_object_set (a, "foo", 47, NULL);
341 g_assert_cmpint (a->foo, !=, b->foo);
342 g_assert_cmpint (a->foo, ==, c->foo);
350 main (int argc, char *argv[])
353 g_test_init (&argc, &argv, NULL);
355 g_test_add_func ("/binding/default", binding_default);
356 g_test_add_func ("/binding/bidirectional", binding_bidirectional);
357 g_test_add_func ("/binding/transform", binding_transform);
358 g_test_add_func ("/binding/chain", binding_chain);
360 return g_test_run ();