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