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_default (void)
334 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
335 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
338 gchar *src_prop, *trg_prop;
341 binding = g_object_bind_property (source, "foo",
343 G_BINDING_BIDIRECTIONAL);
345 g_object_get (binding,
347 "source-property", &src_prop,
349 "target-property", &trg_prop,
352 g_assert (src == source);
353 g_assert (trg == target);
354 g_assert_cmpstr (src_prop, ==, "foo");
355 g_assert_cmpstr (trg_prop, ==, "value");
356 g_assert_cmpint (flags, ==, G_BINDING_BIDIRECTIONAL);
357 g_object_unref (src);
358 g_object_unref (trg);
362 g_object_set (source, "foo", 24, NULL);
363 g_assert_cmpfloat (target->value, ==, 24.0);
365 g_object_set (target, "value", 69.0, NULL);
366 g_assert_cmpint (source->foo, ==, 69);
368 g_object_unref (source);
369 g_object_unref (target);
373 binding_transform (void)
375 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
376 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
377 GBinding *binding G_GNUC_UNUSED;
378 gboolean unused_data = FALSE;
380 binding = g_object_bind_property_full (source, "value",
382 G_BINDING_BIDIRECTIONAL,
383 celsius_to_fahrenheit,
384 fahrenheit_to_celsius,
385 &unused_data, data_free);
387 g_object_set (source, "value", 24.0, NULL);
388 g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
390 g_object_set (target, "value", 69.0, NULL);
391 g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
393 g_object_unref (source);
394 g_object_unref (target);
396 g_assert (unused_data);
400 binding_transform_closure (void)
402 BindingSource *source = g_object_new (binding_source_get_type (), NULL);
403 BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
404 GBinding *binding G_GNUC_UNUSED;
405 gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
406 GClosure *c2f_clos, *f2c_clos;
408 c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
410 f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
412 binding = g_object_bind_property_with_closures (source, "value",
414 G_BINDING_BIDIRECTIONAL,
418 g_object_set (source, "value", 24.0, NULL);
419 g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
421 g_object_set (target, "value", 69.0, NULL);
422 g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
424 g_object_unref (source);
425 g_object_unref (target);
427 g_assert (unused_data_1);
428 g_assert (unused_data_2);
434 BindingSource *a = g_object_new (binding_source_get_type (), NULL);
435 BindingSource *b = g_object_new (binding_source_get_type (), NULL);
436 BindingSource *c = g_object_new (binding_source_get_type (), NULL);
437 GBinding *binding_1, *binding_2;
439 g_test_bug ("621782");
442 binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
443 binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
445 /* verify the chain */
446 g_object_set (a, "foo", 42, NULL);
447 g_assert_cmpint (a->foo, ==, b->foo);
448 g_assert_cmpint (b->foo, ==, c->foo);
450 /* unbind A -> B and B -> C */
451 g_object_unref (binding_1);
452 g_object_unref (binding_2);
454 /* bind A -> C directly */
455 binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
457 /* verify the chain is broken */
458 g_object_set (a, "foo", 47, NULL);
459 g_assert_cmpint (a->foo, !=, b->foo);
460 g_assert_cmpint (a->foo, ==, c->foo);
468 binding_sync_create (void)
470 BindingSource *source = g_object_new (binding_source_get_type (),
473 BindingTarget *target = g_object_new (binding_target_get_type (),
478 binding = g_object_bind_property (source, "foo",
480 G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
482 g_assert_cmpint (source->foo, ==, 42);
483 g_assert_cmpint (target->bar, ==, 42);
485 g_object_set (source, "foo", 47, NULL);
486 g_assert_cmpint (source->foo, ==, target->bar);
488 g_object_unref (binding);
490 g_object_set (target, "bar", 49, NULL);
492 binding = g_object_bind_property (source, "foo",
494 G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
495 g_assert_cmpint (source->foo, ==, 47);
496 g_assert_cmpint (target->bar, ==, 47);
498 g_object_unref (source);
499 g_object_unref (target);
503 binding_invert_boolean (void)
505 BindingSource *source = g_object_new (binding_source_get_type (),
508 BindingTarget *target = g_object_new (binding_target_get_type (),
513 binding = g_object_bind_property (source, "toggle",
515 G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN);
517 g_assert (source->toggle);
518 g_assert (!target->toggle);
520 g_object_set (source, "toggle", FALSE, NULL);
521 g_assert (!source->toggle);
522 g_assert (target->toggle);
524 g_object_set (target, "toggle", FALSE, NULL);
525 g_assert (source->toggle);
526 g_assert (!target->toggle);
528 g_object_unref (binding);
529 g_object_unref (source);
530 g_object_unref (target);
534 main (int argc, char *argv[])
537 g_test_init (&argc, &argv, NULL);
539 g_test_bug_base ("http://bugzilla.gnome.org/");
541 g_test_add_func ("/binding/default", binding_default);
542 g_test_add_func ("/binding/bidirectional", binding_bidirectional);
543 g_test_add_func ("/binding/transform", binding_transform);
544 g_test_add_func ("/binding/transform-default", binding_transform_default);
545 g_test_add_func ("/binding/transform-closure", binding_transform_closure);
546 g_test_add_func ("/binding/chain", binding_chain);
547 g_test_add_func ("/binding/sync-create", binding_sync_create);
548 g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
550 return g_test_run ();