animate/legacy/
drop_action.rs

1use crate::{Action, Actor, ActorMeta};
2use glib::{
3    object as gobject,
4    object::{Cast, IsA},
5    signal::{connect_raw, SignalHandlerId},
6    translate::*,
7};
8use std::boxed::Box as Box_;
9use std::{fmt, mem::transmute};
10
11glib_wrapper! {
12    pub struct DropAction(Object<ffi::ClutterDropAction, ffi::ClutterDropActionClass, DropActionClass>) @extends Action, ActorMeta, gobject::InitiallyUnowned;
13
14    match fn {
15        get_type => || ffi::clutter_drop_action_get_type(),
16    }
17}
18
19impl DropAction {
20    /// Creates a new `DropAction`.
21    ///
22    /// Use `ActorExt::add_action` to add the action to a `Actor`.
23    ///
24    /// # Returns
25    ///
26    /// the newly created `DropAction`
27    pub fn new() -> DropAction {
28        unsafe { Action::from_glib_none(ffi::clutter_drop_action_new()).unsafe_cast() }
29    }
30}
31
32impl Default for DropAction {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38/// Trait containing all `DropAction` methods.
39///
40/// # Implementors
41///
42/// [`DropAction`](struct.DropAction.html)
43pub trait DropActionExt: 'static {
44    /// The ::can-drop signal is emitted when the dragged actor is dropped
45    /// on `actor`. The return value of the ::can-drop signal will determine
46    /// whether or not the `DropAction::drop` signal is going to be
47    /// emitted on `action`.
48    ///
49    /// The default implementation of `DropAction` returns `true` for
50    /// this signal.
51    /// ## `actor`
52    /// the `Actor` attached to the `action`
53    /// ## `event_x`
54    /// the X coordinate (in stage space) of the drop event
55    /// ## `event_y`
56    /// the Y coordinate (in stage space) of the drop event
57    ///
58    /// # Returns
59    ///
60    /// `true` if the drop is accepted, and `false` otherwise
61    fn connect_can_drop<F: Fn(&Self, &Actor, f32, f32) -> bool + 'static>(
62        &self,
63        f: F,
64    ) -> SignalHandlerId;
65
66    /// The ::drop signal is emitted when the dragged actor is dropped
67    /// on `actor`. This signal is only emitted if at least an handler of
68    /// `DropAction::can-drop` returns `true`.
69    /// ## `actor`
70    /// the `Actor` attached to the `action`
71    /// ## `event_x`
72    /// the X coordinate (in stage space) of the drop event
73    /// ## `event_y`
74    /// the Y coordinate (in stage space) of the drop event
75    fn connect_drop<F: Fn(&Self, &Actor, f32, f32) + 'static>(&self, f: F) -> SignalHandlerId;
76
77    /// The ::drop-cancel signal is emitted when the drop is refused
78    /// by an emission of the `DropAction::can-drop` signal.
79    ///
80    /// After the ::drop-cancel signal is fired the active drag is
81    /// terminated.
82    /// ## `actor`
83    /// the `Actor` attached to the `action`
84    /// ## `event_x`
85    /// the X coordinate (in stage space) of the drop event
86    /// ## `event_y`
87    /// the Y coordinate (in stage space) of the drop event
88    fn connect_drop_cancel<F: Fn(&Self, &Actor, f32, f32) + 'static>(
89        &self,
90        f: F,
91    ) -> SignalHandlerId;
92
93    /// The ::over-in signal is emitted when the dragged actor crosses
94    /// into `actor`.
95    /// ## `actor`
96    /// the `Actor` attached to the `action`
97    fn connect_over_in<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;
98
99    /// The ::over-out signal is emitted when the dragged actor crosses
100    /// outside `actor`.
101    /// ## `actor`
102    /// the `Actor` attached to the `action`
103    fn connect_over_out<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;
104}
105
106impl<O: IsA<DropAction>> DropActionExt for O {
107    fn connect_can_drop<F: Fn(&Self, &Actor, f32, f32) -> bool + 'static>(
108        &self,
109        f: F,
110    ) -> SignalHandlerId {
111        unsafe extern "C" fn can_drop_trampoline<P, F: Fn(&P, &Actor, f32, f32) -> bool + 'static>(
112            this: *mut ffi::ClutterDropAction,
113            actor: *mut ffi::ClutterActor,
114            event_x: libc::c_float,
115            event_y: libc::c_float,
116            f: glib_sys::gpointer,
117        ) -> glib_sys::gboolean
118        where
119            P: IsA<DropAction>,
120        {
121            let f: &F = &*(f as *const F);
122            f(
123                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
124                &from_glib_borrow(actor),
125                event_x,
126                event_y,
127            )
128            .to_glib()
129        }
130        unsafe {
131            let f: Box_<F> = Box_::new(f);
132            connect_raw(
133                self.as_ptr() as *mut _,
134                b"can-drop\0".as_ptr() as *const _,
135                Some(transmute::<_, unsafe extern "C" fn()>(
136                    can_drop_trampoline::<Self, F> as *const (),
137                )),
138                Box_::into_raw(f),
139            )
140        }
141    }
142
143    fn connect_drop<F: Fn(&Self, &Actor, f32, f32) + 'static>(&self, f: F) -> SignalHandlerId {
144        unsafe extern "C" fn drop_trampoline<P, F: Fn(&P, &Actor, f32, f32) + 'static>(
145            this: *mut ffi::ClutterDropAction,
146            actor: *mut ffi::ClutterActor,
147            event_x: libc::c_float,
148            event_y: libc::c_float,
149            f: glib_sys::gpointer,
150        ) where
151            P: IsA<DropAction>,
152        {
153            let f: &F = &*(f as *const F);
154            f(
155                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
156                &from_glib_borrow(actor),
157                event_x,
158                event_y,
159            )
160        }
161        unsafe {
162            let f: Box_<F> = Box_::new(f);
163            connect_raw(
164                self.as_ptr() as *mut _,
165                b"drop\0".as_ptr() as *const _,
166                Some(transmute::<_, unsafe extern "C" fn()>(
167                    drop_trampoline::<Self, F> as *const (),
168                )),
169                Box_::into_raw(f),
170            )
171        }
172    }
173
174    fn connect_drop_cancel<F: Fn(&Self, &Actor, f32, f32) + 'static>(
175        &self,
176        f: F,
177    ) -> SignalHandlerId {
178        unsafe extern "C" fn drop_cancel_trampoline<P, F: Fn(&P, &Actor, f32, f32) + 'static>(
179            this: *mut ffi::ClutterDropAction,
180            actor: *mut ffi::ClutterActor,
181            event_x: libc::c_float,
182            event_y: libc::c_float,
183            f: glib_sys::gpointer,
184        ) where
185            P: IsA<DropAction>,
186        {
187            let f: &F = &*(f as *const F);
188            f(
189                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
190                &from_glib_borrow(actor),
191                event_x,
192                event_y,
193            )
194        }
195        unsafe {
196            let f: Box_<F> = Box_::new(f);
197            connect_raw(
198                self.as_ptr() as *mut _,
199                b"drop-cancel\0".as_ptr() as *const _,
200                Some(transmute::<_, unsafe extern "C" fn()>(
201                    drop_cancel_trampoline::<Self, F> as *const (),
202                )),
203                Box_::into_raw(f),
204            )
205        }
206    }
207
208    fn connect_over_in<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
209        unsafe extern "C" fn over_in_trampoline<P, F: Fn(&P, &Actor) + 'static>(
210            this: *mut ffi::ClutterDropAction,
211            actor: *mut ffi::ClutterActor,
212            f: glib_sys::gpointer,
213        ) where
214            P: IsA<DropAction>,
215        {
216            let f: &F = &*(f as *const F);
217            f(
218                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
219                &from_glib_borrow(actor),
220            )
221        }
222        unsafe {
223            let f: Box_<F> = Box_::new(f);
224            connect_raw(
225                self.as_ptr() as *mut _,
226                b"over-in\0".as_ptr() as *const _,
227                Some(transmute::<_, unsafe extern "C" fn()>(
228                    over_in_trampoline::<Self, F> as *const (),
229                )),
230                Box_::into_raw(f),
231            )
232        }
233    }
234
235    fn connect_over_out<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
236        unsafe extern "C" fn over_out_trampoline<P, F: Fn(&P, &Actor) + 'static>(
237            this: *mut ffi::ClutterDropAction,
238            actor: *mut ffi::ClutterActor,
239            f: glib_sys::gpointer,
240        ) where
241            P: IsA<DropAction>,
242        {
243            let f: &F = &*(f as *const F);
244            f(
245                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
246                &from_glib_borrow(actor),
247            )
248        }
249        unsafe {
250            let f: Box_<F> = Box_::new(f);
251            connect_raw(
252                self.as_ptr() as *mut _,
253                b"over-out\0".as_ptr() as *const _,
254                Some(transmute::<_, unsafe extern "C" fn()>(
255                    over_out_trampoline::<Self, F> as *const (),
256                )),
257                Box_::into_raw(f),
258            )
259        }
260    }
261}
262
263impl fmt::Display for DropAction {
264    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
265        write!(f, "DropAction")
266    }
267}