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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{CellArea, CellAreaContext, CellRenderer, CellRendererState, Widget};
use gdk::Event;
use glib::translate::*;
use glib::{IsA, ToValue};

pub trait CellAreaExtManual {
    /// This is used by [`CellArea`][crate::CellArea] subclasses when handling events
    /// to activate cells, the base [`CellArea`][crate::CellArea] class activates cells
    /// for keyboard events for free in its own GtkCellArea->`activate()`
    /// implementation.
    /// ## `widget`
    /// the [`Widget`][crate::Widget] that `self` is rendering onto
    /// ## `renderer`
    /// the [`CellRenderer`][crate::CellRenderer] in `self` to activate
    /// ## `event`
    /// the [`gdk::Event`][crate::gdk::Event] for which cell activation should occur
    /// ## `cell_area`
    /// the [`gdk::Rectangle`][crate::gdk::Rectangle] in `widget` relative coordinates
    ///  of `renderer` for the current row.
    /// ## `flags`
    /// the [`CellRenderer`][crate::CellRenderer]State for `renderer`
    ///
    /// # Returns
    ///
    /// whether cell activation was successful
    #[doc(alias = "gtk_cell_area_activate_cell")]
    fn activate_cell<P: IsA<Widget>, Q: IsA<CellRenderer>, R: AsRef<Event>>(
        &self,
        widget: &P,
        renderer: &Q,
        event: &R,
        cell_area: &gdk::Rectangle,
        flags: CellRendererState,
    ) -> bool;

    /// Delegates event handling to a [`CellArea`][crate::CellArea].
    /// ## `context`
    /// the [`CellArea`][crate::CellArea]Context for this row of data.
    /// ## `widget`
    /// the [`Widget`][crate::Widget] that `self` is rendering to
    /// ## `event`
    /// the [`gdk::Event`][crate::gdk::Event] to handle
    /// ## `cell_area`
    /// the `widget` relative coordinates for `self`
    /// ## `flags`
    /// the [`CellRenderer`][crate::CellRenderer]State for `self` in this row.
    ///
    /// # Returns
    ///
    /// [`true`] if the event was handled by `self`.
    #[doc(alias = "gtk_cell_area_event")]
    fn event<P: IsA<CellAreaContext>, Q: IsA<Widget>, R: AsRef<Event>>(
        &self,
        context: &P,
        widget: &Q,
        event: &R,
        cell_area: &gdk::Rectangle,
        flags: CellRendererState,
    ) -> i32;

    /// Gets the values of one or more cell properties for `renderer` in `self`.
    /// ## `renderer`
    /// a [`CellRenderer`][crate::CellRenderer] which is inside `self`
    /// ## `first_prop_name`
    /// the name of the first cell property to get
    #[doc(alias = "gtk_cell_area_cell_get_valist")]
    #[doc(alias = "gtk_cell_area_cell_get_property")]
    fn cell_get<P: IsA<CellRenderer>>(&self, renderer: &P, property_name: &str) -> glib::Value;

    /// Sets one or more cell properties for `cell` in `self`.
    /// ## `renderer`
    /// a [`CellRenderer`][crate::CellRenderer] which is a cell inside `self`
    /// ## `first_prop_name`
    /// the name of the first cell property to set
    #[doc(alias = "gtk_cell_area_cell_set_valist")]
    #[doc(alias = "gtk_cell_area_cell_set_property")]
    fn cell_set<P: IsA<CellRenderer>>(
        &self,
        renderer: &P,
        property_name: &str,
        value: &dyn ToValue,
    );
}

impl<O: IsA<CellArea>> CellAreaExtManual for O {
    fn activate_cell<P: IsA<Widget>, Q: IsA<CellRenderer>, R: AsRef<Event>>(
        &self,
        widget: &P,
        renderer: &Q,
        event: &R,
        cell_area: &gdk::Rectangle,
        flags: CellRendererState,
    ) -> bool {
        unsafe {
            from_glib(ffi::gtk_cell_area_activate_cell(
                self.as_ref().to_glib_none().0,
                widget.as_ref().to_glib_none().0,
                renderer.as_ref().to_glib_none().0,
                event.as_ref().to_glib_none().0,
                cell_area.to_glib_none().0,
                flags.into_glib(),
            ))
        }
    }

    fn cell_get<P: IsA<CellRenderer>>(&self, renderer: &P, property_name: &str) -> glib::Value {
        unsafe {
            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.to_glib_none().0,
                ));
            let pspec = pspec.unwrap_or_else(|| {
                panic!("The CellArea property {} doesn't exists", property_name)
            });
            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.to_glib_none().0,
                value.to_glib_none_mut().0,
            );
            value
        }
    }

    fn cell_set<P: IsA<CellRenderer>>(
        &self,
        renderer: &P,
        property_name: &str,
        value: &dyn ToValue,
    ) {
        unsafe {
            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.to_glib_none().0,
                ));
            let pspec = pspec.unwrap_or_else(|| {
                panic!("The CellArea property {} doesn't exists", property_name)
            });
            if !pspec.value_type().is_a(value.value_type()) {
                panic!(
                    "The CellArea property's value is of wrong type. Expected '{}' but got '{}'",
                    pspec.value_type(),
                    value.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.to_glib_none().0,
                value.to_value().to_glib_none().0,
            );
        }
    }

    fn event<P: IsA<CellAreaContext>, Q: IsA<Widget>, R: AsRef<Event>>(
        &self,
        context: &P,
        widget: &Q,
        event: &R,
        cell_area: &gdk::Rectangle,
        flags: CellRendererState,
    ) -> i32 {
        unsafe {
            ffi::gtk_cell_area_event(
                self.as_ref().to_glib_none().0,
                context.as_ref().to_glib_none().0,
                widget.as_ref().to_glib_none().0,
                event.as_ref().to_glib_none().0,
                cell_area.to_glib_none().0,
                flags.into_glib(),
            )
        }
    }
}