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