[go: up one dir, main page]

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};
4use glib::translate::*;
5use glib::{value::FromValue, IsA, ToValue};
6
7mod sealed {
8    pub trait Sealed {}
9    impl<T: glib::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    #[doc(alias = "gtk_container_child_set_property")]
56    fn child_set_property(
57        &self,
58        child: &impl IsA<Widget>,
59        property_name: &str,
60        value: &dyn ToValue,
61    ) {
62        unsafe {
63            let container_class = glib::Class::<Container>::from_type(Self::static_type()).unwrap();
64            let pspec: Option<glib::ParamSpec> =
65                from_glib_none(ffi::gtk_container_class_find_child_property(
66                    container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass,
67                    property_name.to_glib_none().0,
68                ));
69            let pspec = pspec.unwrap_or_else(|| {
70                panic!("The Container property '{property_name}' doesn't exists")
71            });
72
73            if !pspec.flags().contains(glib::ParamFlags::WRITABLE)
74                || !pspec.flags().contains(glib::ParamFlags::READWRITE)
75            {
76                panic!("The Container property '{property_name}' is not writeable");
77            }
78
79            assert!(
80                pspec.value_type().is_a(value.value_type()),
81                "The Container property '{}' is value is of wrong type. Expected '{}' but got '{}'",
82                property_name,
83                pspec.value_type(),
84                value.value_type()
85            );
86
87            ffi::gtk_container_child_set_property(
88                self.as_ref().to_glib_none().0,
89                child.as_ref().to_glib_none().0,
90                property_name.to_glib_none().0,
91                value.to_value().to_glib_none().0,
92            );
93        }
94    }
95}
96
97impl<O: IsA<Container>> ContainerExtManual for O {}