1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{prelude::*, CellArea, CellRenderer};
use glib::{translate::*, value::FromValue, IntoGStr, Value};

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of [`CellArea`](crate::CellArea).
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub trait CellAreaExtManual {
    /// Adds @renderer to @self, setting cell properties at the same time.
    /// See gtk_cell_area_add() and gtk_cell_area_cell_set() for more details.
    ///
    /// # Deprecated since 4.10
    ///
    /// ## `renderer`
    /// a [`CellRenderer`][crate::CellRenderer] to be placed inside @self
    /// ## `first_prop_name`
    /// the name of the first cell property to set
    #[doc(alias = "gtk_cell_area_add_with_properties")]
    fn add_with_properties(
        &self,
        renderer: &impl IsA<CellRenderer>,
        properties: &[(&str, &dyn ToValue)],
    );

    #[doc(alias = "gtk_cell_area_cell_get_valist")]
    #[doc(alias = "gtk_cell_area_cell_get_property")]
    fn cell_get_value(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
    ) -> glib::Value;

    // rustdoc-stripper-ignore-next
    /// Similar to [`Self::cell_get_value`] but panics if the value is of a different type.
    #[doc(alias = "gtk_cell_area_cell_get_valist")]
    #[doc(alias = "gtk_cell_area_cell_get_property")]
    fn cell_get<V: for<'b> FromValue<'b> + 'static>(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
    ) -> V;

    #[doc(alias = "gtk_cell_area_cell_set_valist")]
    #[doc(alias = "gtk_cell_area_cell_set_property")]
    fn cell_set(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
        value: impl Into<Value>,
    );
}

impl<O: IsA<CellArea>> CellAreaExtManual for O {
    fn add_with_properties(
        &self,
        renderer: &impl IsA<CellRenderer>,
        properties: &[(&str, &dyn ToValue)],
    ) {
        self.as_ref().add(renderer);
        properties.iter().for_each(|(property_name, value)| {
            self.as_ref().cell_set(renderer, *property_name, *value);
        });
    }

    fn cell_get_value(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
    ) -> glib::Value {
        unsafe {
            property_name.run_with_gstr(|property_name| {
                let cell_class = glib::Class::<CellArea>::from_type(O::static_type()).unwrap();
                let pspec: Option<glib::ParamSpec> =
                    from_glib_none(ffi::gtk_cell_area_class_find_cell_property(
                        cell_class.as_ref() as *const _ as *mut ffi::GtkCellAreaClass,
                        property_name.as_ptr(),
                    ));
                let pspec = pspec.unwrap_or_else(|| {
                    panic!("The CellArea property {property_name} doesn't exists")
                });
                let mut value = glib::Value::from_type(pspec.value_type());
                ffi::gtk_cell_area_cell_get_property(
                    self.as_ref().to_glib_none().0,
                    renderer.as_ref().to_glib_none().0,
                    property_name.as_ptr(),
                    value.to_glib_none_mut().0,
                );
                value
            })
        }
    }

    fn cell_get<V: for<'b> FromValue<'b> + 'static>(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
    ) -> V {
        let value = self.cell_get_value(renderer, property_name);
        value
            .get_owned::<V>()
            .expect("Failed to get value of renderer")
    }

    fn cell_set(
        &self,
        renderer: &impl IsA<CellRenderer>,
        property_name: impl IntoGStr,
        value: impl Into<Value>,
    ) {
        unsafe {
            property_name.run_with_gstr(|property_name| {
                let cell_class = glib::Class::<CellArea>::from_type(O::static_type()).unwrap();
                let pspec: Option<glib::ParamSpec> =
                    from_glib_none(ffi::gtk_cell_area_class_find_cell_property(
                        cell_class.as_ref() as *const _ as *mut ffi::GtkCellAreaClass,
                        property_name.as_ptr(),
                    ));
                let pspec = pspec.unwrap_or_else(|| {
                    panic!("The CellArea property {property_name} doesn't exists")
                });

                let value = value.into();
                assert!(
                    pspec.value_type().is_a(value.type_()),
                    "The CellArea property's value is of wrong type. Expected '{}' but got '{}'",
                    pspec.value_type(),
                    value.type_()
                );

                ffi::gtk_cell_area_cell_set_property(
                    self.as_ref().to_glib_none().0,
                    renderer.as_ref().to_glib_none().0,
                    property_name.as_ptr(),
                    value.to_glib_none().0,
                );
            })
        }
    }
}