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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::{Action, Actor, ActorMeta};
use glib::{
    object as gobject,
    object::{Cast, IsA},
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;
use std::{fmt, mem::transmute};

glib_wrapper! {
    pub struct DropAction(Object<ffi::ClutterDropAction, ffi::ClutterDropActionClass, DropActionClass>) @extends Action, ActorMeta, gobject::InitiallyUnowned;

    match fn {
        get_type => || ffi::clutter_drop_action_get_type(),
    }
}

impl DropAction {
    /// Creates a new `DropAction`.
    ///
    /// Use `ActorExt::add_action` to add the action to a `Actor`.
    ///
    /// # Returns
    ///
    /// the newly created `DropAction`
    pub fn new() -> DropAction {
        unsafe { Action::from_glib_none(ffi::clutter_drop_action_new()).unsafe_cast() }
    }
}

impl Default for DropAction {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait containing all `DropAction` methods.
///
/// # Implementors
///
/// [`DropAction`](struct.DropAction.html)
pub trait DropActionExt: 'static {
    /// The ::can-drop signal is emitted when the dragged actor is dropped
    /// on `actor`. The return value of the ::can-drop signal will determine
    /// whether or not the `DropAction::drop` signal is going to be
    /// emitted on `action`.
    ///
    /// The default implementation of `DropAction` returns `true` for
    /// this signal.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    /// ## `event_x`
    /// the X coordinate (in stage space) of the drop event
    /// ## `event_y`
    /// the Y coordinate (in stage space) of the drop event
    ///
    /// # Returns
    ///
    /// `true` if the drop is accepted, and `false` otherwise
    fn connect_can_drop<F: Fn(&Self, &Actor, f32, f32) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::drop signal is emitted when the dragged actor is dropped
    /// on `actor`. This signal is only emitted if at least an handler of
    /// `DropAction::can-drop` returns `true`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    /// ## `event_x`
    /// the X coordinate (in stage space) of the drop event
    /// ## `event_y`
    /// the Y coordinate (in stage space) of the drop event
    fn connect_drop<F: Fn(&Self, &Actor, f32, f32) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The ::drop-cancel signal is emitted when the drop is refused
    /// by an emission of the `DropAction::can-drop` signal.
    ///
    /// After the ::drop-cancel signal is fired the active drag is
    /// terminated.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    /// ## `event_x`
    /// the X coordinate (in stage space) of the drop event
    /// ## `event_y`
    /// the Y coordinate (in stage space) of the drop event
    fn connect_drop_cancel<F: Fn(&Self, &Actor, f32, f32) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::over-in signal is emitted when the dragged actor crosses
    /// into `actor`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    fn connect_over_in<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The ::over-out signal is emitted when the dragged actor crosses
    /// outside `actor`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    fn connect_over_out<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<DropAction>> DropActionExt for O {
    fn connect_can_drop<F: Fn(&Self, &Actor, f32, f32) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn can_drop_trampoline<P, F: Fn(&P, &Actor, f32, f32) -> bool + 'static>(
            this: *mut ffi::ClutterDropAction,
            actor: *mut ffi::ClutterActor,
            event_x: libc::c_float,
            event_y: libc::c_float,
            f: glib_sys::gpointer,
        ) -> glib_sys::gboolean
        where
            P: IsA<DropAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
                event_x,
                event_y,
            )
            .to_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"can-drop\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    can_drop_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_drop<F: Fn(&Self, &Actor, f32, f32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn drop_trampoline<P, F: Fn(&P, &Actor, f32, f32) + 'static>(
            this: *mut ffi::ClutterDropAction,
            actor: *mut ffi::ClutterActor,
            event_x: libc::c_float,
            event_y: libc::c_float,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DropAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
                event_x,
                event_y,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drop\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drop_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_drop_cancel<F: Fn(&Self, &Actor, f32, f32) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn drop_cancel_trampoline<P, F: Fn(&P, &Actor, f32, f32) + 'static>(
            this: *mut ffi::ClutterDropAction,
            actor: *mut ffi::ClutterActor,
            event_x: libc::c_float,
            event_y: libc::c_float,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DropAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
                event_x,
                event_y,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drop-cancel\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drop_cancel_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_over_in<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn over_in_trampoline<P, F: Fn(&P, &Actor) + 'static>(
            this: *mut ffi::ClutterDropAction,
            actor: *mut ffi::ClutterActor,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DropAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"over-in\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    over_in_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_over_out<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn over_out_trampoline<P, F: Fn(&P, &Actor) + 'static>(
            this: *mut ffi::ClutterDropAction,
            actor: *mut ffi::ClutterActor,
            f: glib_sys::gpointer,
        ) where
            P: IsA<DropAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &DropAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"over-out\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    over_out_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for DropAction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "DropAction")
    }
}