3 #include <glib-object.h>
5 typedef struct _BindingSource
7 GObject parent_instance;
14 typedef struct _BindingSourceClass
16 GObjectClass parent_class;
28 G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
31 binding_source_set_property (GObject *gobject,
36 BindingSource *source = (BindingSource *) gobject;
41 source->foo = g_value_get_int (value);
44 case PROP_SOURCE_VALUE:
45 source->value = g_value_get_double (value);
48 case PROP_SOURCE_TOGGLE:
49 source->toggle = g_value_get_boolean (value);
53 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
58 binding_source_get_property (GObject *gobject,
63 BindingSource *source = (BindingSource *) gobject;
68 g_value_set_int (value, source->foo);
71 case PROP_SOURCE_VALUE:
72 g_value_set_double (value, source->value);
75 case PROP_SOURCE_TOGGLE:
76 g_value_set_boolean (value, source->toggle);
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
85 binding_source_class_init (BindingSourceClass *klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 gobject_class->set_property = binding_source_set_property;
90 gobject_class->get_property = binding_source_get_property;
92 g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
93 g_param_spec_int ("foo", "Foo", "Foo",
97 g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
98 g_param_spec_double ("value", "Value", "Value",
102 g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
103 g_param_spec_boolean ("toggle", "Toggle", "Toggle",
109 binding_source_init (BindingSource *self)
113 typedef struct _BindingTarget
115 GObject parent_instance;
122 typedef struct _BindingTargetClass
124 GObjectClass parent_class;
125 } BindingTargetClass;
136 G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
139 binding_target_set_property (GObject *gobject,
144 BindingTarget *target = (BindingTarget *) gobject;
148 case PROP_TARGET_BAR:
149 target->bar = g_value_get_int (value);
152 case PROP_TARGET_VALUE:
153 target->value = g_value_get_double (value);
156 case PROP_TARGET_TOGGLE:
157 target->toggle = g_value_get_boolean (value);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
166 binding_target_get_property (GObject *gobject,
171 BindingTarget *target = (BindingTarget *) gobject;
175 case PROP_TARGET_BAR:
176 g_value_set_int (value, target->bar);
179 case PROP_TARGET_VALUE:
180 g_value_set_double (value, target->value);
183 case PROP_TARGET_TOGGLE:
184 g_value_set_boolean (value, target->toggle);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
193 binding_target_class_init (BindingTargetClass *klass)
195 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197 gobject_class->set_property = binding_target_set_property;
198 gobject_class->get_property = binding_target_get_property;
200 g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
201 g_param_spec_int ("bar", "Bar", "Bar",
205 g_object_class_install_property (gobject_class, PROP_TARGET_VALUE,
206 g_param_spec_double ("value", "Value", "Value",
210 g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
211 g_param_spec_boolean ("toggle", "Toggle", "Toggle",
217 binding_target_init (BindingTarget *self)
222 celsius_to_fahrenheit (GBinding *binding,
223 const GValue *source_value,
224 GValue *target_value,
225 gpointer user_data G_GNUC_UNUSED)
227 gdouble celsius, fahrenheit;
229 g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
230 g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
232 celsius = g_value_get_double (source_value);
233 fahrenheit = (9 * celsius / 5) + 32.0;
235 if (g_test_verbose ())
236 g_print ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
238 g_value_set_double (target_value, fahrenheit);
244 fahrenheit_to_celsius (GBinding *binding,
245 const GValue *source_value,
246 GValue *target_value,
247 gpointer user_data G_GNUC_UNUSED)
249 gdouble celsius, fahrenheit;
251 g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
252 g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
254 fahrenheit = g_value_get_double (source_value);
255 celsius = 5 * (fahrenheit - 32.0) / 9;
257 if (g_test_verbose ())
258 g_print ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
260 g_value_set_double (target_value, celsius);
266 binding_default (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",
276 g_assert ((BindingSource *) g_binding_get_source (binding) == source);
277 g_assert ((BindingTarget *) g_binding_get_target (binding) == target);
278 g_assert_cmpstr (g_binding_get_source_property (binding), ==, "foo");
279 g_assert_cmpstr (g_binding_get_target_property (binding), ==, "bar");
280 g_assert_cmpint (g_binding_get_flags (binding), ==, G_BINDING_DEFAULT);
282 g_object_set (source, "foo", 42, NULL);
283 g_assert_cmpint (source->foo, ==, target->bar);
285 g_object_set (target, "bar", 47, NULL);
286 g_assert_cmpint (source->foo, !=, target->bar);
288 g_object_unref (binding);
290 g_object_set (source, "foo", 0, NULL);
291 g_assert_cmpint (source->foo, !=, target->bar);
293 g_object_unref (source);
294 g_object_unref (target);
298 binding_bidirectional (void)
300 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
301 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
304 binding = g_object_bind_property (source, "foo",
306 G_BINDING_BIDIRECTIONAL);
308 g_object_set (source, "foo", 42, NULL);
309 g_assert_cmpint (source->foo, ==, target->bar);
311 g_object_set (target, "bar", 47, NULL);
312 g_assert_cmpint (source->foo, ==, target->bar);
314 g_object_unref (binding);
316 g_object_set (source, "foo", 0, NULL);
317 g_assert_cmpint (source->foo, !=, target->bar);
319 g_object_unref (source);
320 g_object_unref (target);
324 data_free (gpointer data)
332 binding_transform (void)
334 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
335 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
337 gboolean unused_data = FALSE;
339 binding = g_object_bind_property_full (source, "value",
341 G_BINDING_BIDIRECTIONAL,
342 celsius_to_fahrenheit,
343 fahrenheit_to_celsius,
344 &unused_data, data_free);
346 g_object_set (source, "value", 24.0, NULL);
347 g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
349 g_object_set (target, "value", 69.0, NULL);
350 g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
352 g_object_unref (source);
353 g_object_unref (target);
355 g_assert (unused_data);
359 binding_transform_closure (void)
361 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
362 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
364 gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
365 GClosure *c2f_clos, *f2c_clos;
367 c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
369 f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
371 binding = g_object_bind_property_with_closures (source, "value",
373 G_BINDING_BIDIRECTIONAL,
377 g_object_set (source, "value", 24.0, NULL);
378 g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
380 g_object_set (target, "value", 69.0, NULL);
381 g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
383 g_object_unref (source);
384 g_object_unref (target);
386 g_assert (unused_data_1);
387 g_assert (unused_data_2);
393 BindingSource *a = g_object_new (binding_source_get_type (), NULL);
394 BindingSource *b = g_object_new (binding_source_get_type (), NULL);
395 BindingSource *c = g_object_new (binding_source_get_type (), NULL);
396 GBinding *binding_1, *binding_2;
399 binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
400 binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
402 /* verify the chain */
403 g_object_set (a, "foo", 42, NULL);
404 g_assert_cmpint (a->foo, ==, b->foo);
405 g_assert_cmpint (b->foo, ==, c->foo);
407 /* unbind A -> B and B -> C */
408 g_object_unref (binding_1);
409 g_object_unref (binding_2);
411 /* bind A -> C directly */
412 binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
414 /* verify the chain is broken */
415 g_object_set (a, "foo", 47, NULL);
416 g_assert_cmpint (a->foo, !=, b->foo);
417 g_assert_cmpint (a->foo, ==, c->foo);
425 binding_sync_create (void)
427 BindingSource *source = g_object_new (binding_source_get_type (),
430 BindingTarget *target = g_object_new (binding_target_get_type (),
435 binding = g_object_bind_property (source, "foo",
437 G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
439 g_assert_cmpint (source->foo, ==, 42);
440 g_assert_cmpint (target->bar, ==, 42);
442 g_object_set (source, "foo", 47, NULL);
443 g_assert_cmpint (source->foo, ==, target->bar);
445 g_object_unref (binding);
447 g_object_set (target, "bar", 49, NULL);
449 binding = g_object_bind_property (source, "foo",
451 G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
452 g_assert_cmpint (source->foo, ==, 47);
453 g_assert_cmpint (target->bar, ==, 47);
455 g_object_unref (source);
456 g_object_unref (target);
460 binding_invert_boolean (void)
462 BindingSource *source = g_object_new (binding_source_get_type (),
465 BindingTarget *target = g_object_new (binding_target_get_type (),
470 binding = g_object_bind_property (source, "toggle",
472 G_BINDING_DEFAULT | G_BINDING_INVERT_BOOLEAN);
474 g_assert (source->toggle);
475 g_assert (!target->toggle);
477 g_object_set (source, "toggle", FALSE, NULL);
478 g_assert (!source->toggle);
479 g_assert (target->toggle);
481 g_object_unref (binding);
482 g_object_unref (source);
483 g_object_unref (target);
487 main (int argc, char *argv[])
490 g_test_init (&argc, &argv, NULL);
492 g_test_add_func ("/binding/default", binding_default);
493 g_test_add_func ("/binding/bidirectional", binding_bidirectional);
494 g_test_add_func ("/binding/transform", binding_transform);
495 g_test_add_func ("/binding/transform-closure", binding_transform_closure);
496 g_test_add_func ("/binding/chain", binding_chain);
497 g_test_add_func ("/binding/sync-create", binding_sync_create);
498 g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
500 return g_test_run ();