Skip to main content

gtk/
container.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{Container, Widget, ffi};
4use glib::translate::*;
5use glib::{object::IsA, value::FromValue, value::ToValue};
6
7mod sealed {
8    pub trait Sealed {}
9    impl<T: glib::object::IsA<crate::Container>> Sealed for T {}
10}
11
12pub trait ContainerExtManual: IsA<Container> + sealed::Sealed + 'static {
13    #[doc(alias = "gtk_container_child_get_property")]
14    fn child_property_value(&self, child: &impl IsA<Widget>, property_name: &str) -> glib::Value {
15        unsafe {
16            let container_class = glib::Class::<Container>::from_type(Self::static_type()).unwrap();
17            let pspec: Option<glib::ParamSpec> =
18                from_glib_none(ffi::gtk_container_class_find_child_property(
19                    container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass,
20                    property_name.to_glib_none().0,
21                ));
22            let pspec = pspec.unwrap_or_else(|| {
23                panic!("The Container property '{property_name}' doesn't exists")
24            });
25
26            if !pspec.flags().contains(glib::ParamFlags::READABLE)
27                || !pspec.flags().contains(glib::ParamFlags::READWRITE)
28            {
29                panic!("The Container property '{property_name}' is not readable");
30            }
31
32            let mut value = glib::Value::from_type(pspec.value_type());
33            ffi::gtk_container_child_get_property(
34                self.as_ref().to_glib_none().0,
35                child.as_ref().to_glib_none().0,
36                property_name.to_glib_none().0,
37                value.to_glib_none_mut().0,
38            );
39            value
40        }
41    }
42
43    #[doc(alias = "gtk_container_child_get_property")]
44    fn child_property<V: for<'b> FromValue<'b> + 'static>(
45        &self,
46        child: &impl IsA<Widget>,
47        property_name: &str,
48    ) -> V {
49        let value = self.child_property_value(child, property_name);
50        value
51            .get_owned::<V>()
52            .expect("Failed to get value of container")
53    }
54
55    /// Sets a child property for `child` and `self`.
56    /// ## `child`
57    /// a widget which is a child of `self`
58    /// ## `property_name`
59    /// the name of the property to set
60    /// ## `value`
61    /// the value to set the property to
62    #[doc(alias = "gtk_container_child_set_property")]
63    fn child_set_property(
64        &self,
65        child: &impl IsA<Widget>,
66        property_name: &str,
67        value: &dyn ToValue,
68    ) {
69        unsafe {
70            let container_class = glib::Class::<Container>::from_type(Self::static_type()).unwrap();
71            let pspec: Option<glib::ParamSpec> =
72                from_glib_none(ffi::gtk_container_class_find_child_property(
73                    container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass,
74                    property_name.to_glib_none().0,
75                ));
76            let pspec = pspec.unwrap_or_else(|| {
77                panic!("The Container property '{property_name}' doesn't exists")
78            });
79
80            if !pspec.flags().contains(glib::ParamFlags::WRITABLE)
81                || !pspec.flags().contains(glib::ParamFlags::READWRITE)
82            {
83                panic!("The Container property '{property_name}' is not writable");
84            }
85
86            assert!(
87                pspec.value_type().is_a(value.value_type()),
88                "The Container property '{}' is value is of wrong type. Expected '{}' but got '{}'",
89                property_name,
90                pspec.value_type(),
91                value.value_type()
92            );
93
94            ffi::gtk_container_child_set_property(
95                self.as_ref().to_glib_none().0,
96                child.as_ref().to_glib_none().0,
97                property_name.to_glib_none().0,
98                value.to_value().to_glib_none().0,
99            );
100        }
101    }
102}
103
104impl<O: IsA<Container>> ContainerExtManual for O {}