1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{Container, Widget};
use glib::translate::*;
use glib::{value::FromValue, IsA, ToValue};

pub trait ContainerExtManual: 'static {
    #[doc(alias = "gtk_container_child_get_property")]
    fn child_property_value(&self, child: &impl IsA<Widget>, property_name: &str) -> glib::Value;

    #[doc(alias = "gtk_container_child_get_property")]
    fn child_property<V: for<'b> FromValue<'b> + 'static>(
        &self,
        child: &impl IsA<Widget>,
        property_name: &str,
    ) -> V;

    /// Sets a child property for `child` and `self`.
    /// ## `child`
    /// a widget which is a child of `self`
    /// ## `property_name`
    /// the name of the property to set
    /// ## `value`
    /// the value to set the property to
    #[doc(alias = "gtk_container_child_set_property")]
    fn child_set_property(
        &self,
        child: &impl IsA<Widget>,
        property_name: &str,
        value: &dyn ToValue,
    );
}

impl<O: IsA<Container>> ContainerExtManual for O {
    fn child_property_value(&self, child: &impl IsA<Widget>, property_name: &str) -> glib::Value {
        unsafe {
            let container_class = glib::Class::<Container>::from_type(O::static_type()).unwrap();
            let pspec: Option<glib::ParamSpec> =
                from_glib_none(ffi::gtk_container_class_find_child_property(
                    container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass,
                    property_name.to_glib_none().0,
                ));
            let pspec = pspec.unwrap_or_else(|| {
                panic!("The Container property '{}' doesn't exists", property_name)
            });

            if !pspec.flags().contains(glib::ParamFlags::READABLE)
                || !pspec.flags().contains(glib::ParamFlags::READWRITE)
            {
                panic!("The Container property '{}' is not readable", property_name);
            }

            let mut value = glib::Value::from_type(pspec.value_type());
            ffi::gtk_container_child_get_property(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                property_name.to_glib_none().0,
                value.to_glib_none_mut().0,
            );
            value
        }
    }

    fn child_property<V: for<'b> FromValue<'b> + 'static>(
        &self,
        child: &impl IsA<Widget>,
        property_name: &str,
    ) -> V {
        let value = self.child_property_value(child, property_name);
        value
            .get_owned::<V>()
            .expect("Failed to get value of container")
    }

    fn child_set_property(
        &self,
        child: &impl IsA<Widget>,
        property_name: &str,
        value: &dyn ToValue,
    ) {
        unsafe {
            let container_class = glib::Class::<Container>::from_type(O::static_type()).unwrap();
            let pspec: Option<glib::ParamSpec> =
                from_glib_none(ffi::gtk_container_class_find_child_property(
                    container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass,
                    property_name.to_glib_none().0,
                ));
            let pspec = pspec.unwrap_or_else(|| {
                panic!("The Container property '{}' doesn't exists", property_name)
            });

            if !pspec.flags().contains(glib::ParamFlags::WRITABLE)
                || !pspec.flags().contains(glib::ParamFlags::READWRITE)
            {
                panic!(
                    "The Container property '{}' is not writeable",
                    property_name
                );
            }

            assert!(
                pspec.value_type().is_a(value.value_type()),
                "The Container property '{}' is value is of wrong type. Expected '{}' but got '{}'",
                property_name,
                pspec.value_type(),
                value.value_type()
            );

            ffi::gtk_container_child_set_property(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                property_name.to_glib_none().0,
                value.to_value().to_glib_none().0,
            );
        }
    }
}