gtk4/
cell_area.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{translate::*, value::FromValue, Value};
4
5use crate::{ffi, prelude::*, CellArea, CellRenderer};
6
7mod sealed {
8    pub trait Sealed {}
9    impl<T: super::IsA<super::CellArea>> Sealed for T {}
10}
11
12// rustdoc-stripper-ignore-next
13/// Trait containing manually implemented methods of
14/// [`CellArea`](crate::CellArea).
15#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
16#[allow(deprecated)]
17pub trait CellAreaExtManual: sealed::Sealed + IsA<CellArea> {
18    /// Adds @renderer to @self, setting cell properties at the same time.
19    /// See gtk_cell_area_add() and gtk_cell_area_cell_set() for more details.
20    ///
21    /// # Deprecated since 4.10
22    ///
23    /// ## `renderer`
24    /// a [`CellRenderer`][crate::CellRenderer] to be placed inside @self
25    /// ## `first_prop_name`
26    /// the name of the first cell property to set
27    #[doc(alias = "gtk_cell_area_add_with_properties")]
28    fn add_with_properties(
29        &self,
30        renderer: &impl IsA<CellRenderer>,
31        properties: &[(&str, &dyn ToValue)],
32    ) {
33        self.as_ref().add(renderer);
34        properties.iter().for_each(|(property_name, value)| {
35            self.cell_set(renderer, *property_name, *value);
36        });
37    }
38
39    #[doc(alias = "gtk_cell_area_cell_get_valist")]
40    #[doc(alias = "gtk_cell_area_cell_get_property")]
41    fn cell_get_value(
42        &self,
43        renderer: &impl IsA<CellRenderer>,
44        property_name: impl IntoGStr,
45    ) -> glib::Value {
46        unsafe {
47            property_name.run_with_gstr(|property_name| {
48                let cell_class = glib::Class::<CellArea>::from_type(Self::static_type()).unwrap();
49                let pspec: Option<glib::ParamSpec> =
50                    from_glib_none(ffi::gtk_cell_area_class_find_cell_property(
51                        cell_class.as_ref() as *const _ as *mut ffi::GtkCellAreaClass,
52                        property_name.as_ptr(),
53                    ));
54                let pspec = pspec.unwrap_or_else(|| {
55                    panic!("The CellArea property {property_name} doesn't exists")
56                });
57                let mut value = glib::Value::from_type(pspec.value_type());
58                ffi::gtk_cell_area_cell_get_property(
59                    self.as_ref().to_glib_none().0,
60                    renderer.as_ref().to_glib_none().0,
61                    property_name.as_ptr(),
62                    value.to_glib_none_mut().0,
63                );
64                value
65            })
66        }
67    }
68
69    // rustdoc-stripper-ignore-next
70    /// Similar to [`Self::cell_get_value`] but panics if the value is of a
71    /// different type.
72    #[doc(alias = "gtk_cell_area_cell_get_valist")]
73    #[doc(alias = "gtk_cell_area_cell_get_property")]
74    fn cell_get<V: for<'b> FromValue<'b> + 'static>(
75        &self,
76        renderer: &impl IsA<CellRenderer>,
77        property_name: impl IntoGStr,
78    ) -> V {
79        let value = self.cell_get_value(renderer, property_name);
80        value
81            .get_owned::<V>()
82            .expect("Failed to get value of renderer")
83    }
84
85    #[doc(alias = "gtk_cell_area_cell_set_valist")]
86    #[doc(alias = "gtk_cell_area_cell_set_property")]
87    fn cell_set(
88        &self,
89        renderer: &impl IsA<CellRenderer>,
90        property_name: impl IntoGStr,
91        value: impl Into<Value>,
92    ) {
93        unsafe {
94            property_name.run_with_gstr(|property_name| {
95                let cell_class = glib::Class::<CellArea>::from_type(Self::static_type()).unwrap();
96                let pspec: Option<glib::ParamSpec> =
97                    from_glib_none(ffi::gtk_cell_area_class_find_cell_property(
98                        cell_class.as_ref() as *const _ as *mut ffi::GtkCellAreaClass,
99                        property_name.as_ptr(),
100                    ));
101                let pspec = pspec.unwrap_or_else(|| {
102                    panic!("The CellArea property {property_name} doesn't exists")
103                });
104
105                let value = value.into();
106                assert!(
107                    pspec.value_type().is_a(value.type_()),
108                    "The CellArea property's value is of wrong type. Expected '{}' but got '{}'",
109                    pspec.value_type(),
110                    value.type_()
111                );
112
113                ffi::gtk_cell_area_cell_set_property(
114                    self.as_ref().to_glib_none().0,
115                    renderer.as_ref().to_glib_none().0,
116                    property_name.as_ptr(),
117                    value.to_glib_none().0,
118                );
119            })
120        }
121    }
122}
123
124impl<O: IsA<CellArea>> CellAreaExtManual for O {}