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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::ContentFormats;
use crate::ContentProvider;
use crate::Device;
use crate::Display;
use crate::DragAction;
use crate::DragCancelReason;
use crate::Surface;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// The [`Drag`][crate::Drag] object represents the source of an ongoing DND operation.
    ///
    /// A [`Drag`][crate::Drag] is created when a drag is started, and stays alive for duration of
    /// the DND operation. After a drag has been started with [`begin()`][Self::begin()],
    /// the caller gets informed about the status of the ongoing drag operation
    /// with signals on the [`Drag`][crate::Drag] object.
    ///
    /// GTK provides a higher level abstraction based on top of these functions,
    /// and so they are not normally needed in GTK applications. See the
    /// "Drag and Drop" section of the GTK documentation for more information.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`DragExt`][trait@crate::prelude::DragExt]
    #[doc(alias = "GdkDrag")]
    pub struct Drag(Object<ffi::GdkDrag>);

    match fn {
        type_ => || ffi::gdk_drag_get_type(),
    }
}

impl Drag {
    pub const NONE: Option<&'static Drag> = None;

    /// Starts a drag and creates a new drag context for it.
    ///
    /// This function is called by the drag source. After this call, you
    /// probably want to set up the drag icon using the surface returned
    /// by [`DragExt::drag_surface()`][crate::prelude::DragExt::drag_surface()].
    ///
    /// This function returns a reference to the [`Drag`][crate::Drag] object,
    /// but GTK keeps its own reference as well, as long as the DND operation
    /// is going on.
    ///
    /// Note: if @actions include [`DragAction::MOVE`][crate::DragAction::MOVE], you need to listen for
    /// the `signal::Drag::dnd-finished` signal and delete the data at
    /// the source if [`DragExt::selected_action()`][crate::prelude::DragExt::selected_action()] returns
    /// [`DragAction::MOVE`][crate::DragAction::MOVE].
    /// ## `surface`
    /// the source surface for this drag
    /// ## `device`
    /// the device that controls this drag
    /// ## `content`
    /// the offered content
    /// ## `actions`
    /// the actions supported by this drag
    /// ## `dx`
    /// the x offset to @device's position where the drag nominally started
    /// ## `dy`
    /// the y offset to @device's position where the drag nominally started
    ///
    /// # Returns
    ///
    /// a newly created [`Drag`][crate::Drag]
    #[doc(alias = "gdk_drag_begin")]
    pub fn begin(
        surface: &impl IsA<Surface>,
        device: &impl IsA<Device>,
        content: &impl IsA<ContentProvider>,
        actions: DragAction,
        dx: f64,
        dy: f64,
    ) -> Option<Drag> {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::gdk_drag_begin(
                surface.as_ref().to_glib_none().0,
                device.as_ref().to_glib_none().0,
                content.as_ref().to_glib_none().0,
                actions.into_glib(),
                dx,
                dy,
            ))
        }
    }
}

/// Trait containing all [`struct@Drag`] methods.
///
/// # Implementors
///
/// [`Drag`][struct@crate::Drag]
pub trait DragExt: 'static {
    /// Informs GDK that the drop ended.
    ///
    /// Passing [`false`] for @success may trigger a drag cancellation
    /// animation.
    ///
    /// This function is called by the drag source, and should be the
    /// last call before dropping the reference to the @self.
    ///
    /// The [`Drag`][crate::Drag] will only take the first [`drop_done()`][Self::drop_done()]
    /// call as effective, if this function is called multiple times,
    /// all subsequent calls will be ignored.
    /// ## `success`
    /// whether the drag was ultimatively successful
    #[doc(alias = "gdk_drag_drop_done")]
    fn drop_done(&self, success: bool);

    /// Determines the bitmask of possible actions proposed by the source.
    ///
    /// # Returns
    ///
    /// the [`DragAction`][crate::DragAction] flags
    #[doc(alias = "gdk_drag_get_actions")]
    #[doc(alias = "get_actions")]
    fn actions(&self) -> DragAction;

    /// Returns the [`ContentProvider`][crate::ContentProvider] associated to the [`Drag`][crate::Drag] object.
    ///
    /// # Returns
    ///
    /// The [`ContentProvider`][crate::ContentProvider] associated to @self.
    #[doc(alias = "gdk_drag_get_content")]
    #[doc(alias = "get_content")]
    fn content(&self) -> ContentProvider;

    /// Returns the [`Device`][crate::Device] associated to the [`Drag`][crate::Drag] object.
    ///
    /// # Returns
    ///
    /// The [`Device`][crate::Device] associated to @self.
    #[doc(alias = "gdk_drag_get_device")]
    #[doc(alias = "get_device")]
    fn device(&self) -> Device;

    /// Gets the [`Display`][crate::Display] that the drag object was created for.
    ///
    /// # Returns
    ///
    /// a [`Display`][crate::Display]
    #[doc(alias = "gdk_drag_get_display")]
    #[doc(alias = "get_display")]
    fn display(&self) -> Display;

    /// Returns the surface on which the drag icon should be rendered
    /// during the drag operation.
    ///
    /// Note that the surface may not be available until the drag operation
    /// has begun. GDK will move the surface in accordance with the ongoing
    /// drag operation. The surface is owned by @self and will be destroyed
    /// when the drag operation is over.
    ///
    /// # Returns
    ///
    /// the drag surface
    #[doc(alias = "gdk_drag_get_drag_surface")]
    #[doc(alias = "get_drag_surface")]
    fn drag_surface(&self) -> Option<Surface>;

    /// Retrieves the formats supported by this [`Drag`][crate::Drag] object.
    ///
    /// # Returns
    ///
    /// a [`ContentFormats`][crate::ContentFormats]
    #[doc(alias = "gdk_drag_get_formats")]
    #[doc(alias = "get_formats")]
    fn formats(&self) -> ContentFormats;

    /// Determines the action chosen by the drag destination.
    ///
    /// # Returns
    ///
    /// a [`DragAction`][crate::DragAction] value
    #[doc(alias = "gdk_drag_get_selected_action")]
    #[doc(alias = "get_selected_action")]
    fn selected_action(&self) -> DragAction;

    /// Returns the [`Surface`][crate::Surface] where the drag originates.
    ///
    /// # Returns
    ///
    /// The [`Surface`][crate::Surface] where the drag originates
    #[doc(alias = "gdk_drag_get_surface")]
    #[doc(alias = "get_surface")]
    fn surface(&self) -> Surface;

    /// Sets the position of the drag surface that will be kept
    /// under the cursor hotspot.
    ///
    /// Initially, the hotspot is at the top left corner of the drag surface.
    /// ## `hot_x`
    /// x coordinate of the drag surface hotspot
    /// ## `hot_y`
    /// y coordinate of the drag surface hotspot
    #[doc(alias = "gdk_drag_set_hotspot")]
    fn set_hotspot(&self, hot_x: i32, hot_y: i32);

    /// The possible actions of this drag.
    fn set_actions(&self, actions: DragAction);

    /// The currently selected action of the drag.
    #[doc(alias = "selected-action")]
    fn set_selected_action(&self, selected_action: DragAction);

    /// Emitted when the drag operation is cancelled.
    /// ## `reason`
    /// The reason the drag was cancelled
    #[doc(alias = "cancel")]
    fn connect_cancel<F: Fn(&Self, DragCancelReason) + 'static>(&self, f: F) -> SignalHandlerId;

    /// Emitted when the destination side has finished reading all data.
    ///
    /// The drag object can now free all miscellaneous data.
    #[doc(alias = "dnd-finished")]
    fn connect_dnd_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    /// Emitted when the drop operation is performed on an accepting client.
    #[doc(alias = "drop-performed")]
    fn connect_drop_performed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "actions")]
    fn connect_actions_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "display")]
    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "selected-action")]
    fn connect_selected_action_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Drag>> DragExt for O {
    fn drop_done(&self, success: bool) {
        unsafe {
            ffi::gdk_drag_drop_done(self.as_ref().to_glib_none().0, success.into_glib());
        }
    }

    fn actions(&self) -> DragAction {
        unsafe { from_glib(ffi::gdk_drag_get_actions(self.as_ref().to_glib_none().0)) }
    }

    fn content(&self) -> ContentProvider {
        unsafe { from_glib_none(ffi::gdk_drag_get_content(self.as_ref().to_glib_none().0)) }
    }

    fn device(&self) -> Device {
        unsafe { from_glib_none(ffi::gdk_drag_get_device(self.as_ref().to_glib_none().0)) }
    }

    fn display(&self) -> Display {
        unsafe { from_glib_none(ffi::gdk_drag_get_display(self.as_ref().to_glib_none().0)) }
    }

    fn drag_surface(&self) -> Option<Surface> {
        unsafe {
            from_glib_none(ffi::gdk_drag_get_drag_surface(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn formats(&self) -> ContentFormats {
        unsafe { from_glib_none(ffi::gdk_drag_get_formats(self.as_ref().to_glib_none().0)) }
    }

    fn selected_action(&self) -> DragAction {
        unsafe {
            from_glib(ffi::gdk_drag_get_selected_action(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn surface(&self) -> Surface {
        unsafe { from_glib_none(ffi::gdk_drag_get_surface(self.as_ref().to_glib_none().0)) }
    }

    fn set_hotspot(&self, hot_x: i32, hot_y: i32) {
        unsafe {
            ffi::gdk_drag_set_hotspot(self.as_ref().to_glib_none().0, hot_x, hot_y);
        }
    }

    fn set_actions(&self, actions: DragAction) {
        glib::ObjectExt::set_property(self.as_ref(), "actions", &actions)
    }

    fn set_selected_action(&self, selected_action: DragAction) {
        glib::ObjectExt::set_property(self.as_ref(), "selected-action", &selected_action)
    }

    fn connect_cancel<F: Fn(&Self, DragCancelReason) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn cancel_trampoline<
            P: IsA<Drag>,
            F: Fn(&P, DragCancelReason) + 'static,
        >(
            this: *mut ffi::GdkDrag,
            reason: ffi::GdkDragCancelReason,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Drag::from_glib_borrow(this).unsafe_cast_ref(),
                from_glib(reason),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"cancel\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    cancel_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_dnd_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn dnd_finished_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
            this: *mut ffi::GdkDrag,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Drag::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"dnd-finished\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    dnd_finished_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_drop_performed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn drop_performed_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
            this: *mut ffi::GdkDrag,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Drag::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drop-performed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drop_performed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_actions_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_actions_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
            this: *mut ffi::GdkDrag,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Drag::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::actions\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_actions_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_display_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
            this: *mut ffi::GdkDrag,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Drag::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::display\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_display_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_selected_action_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_selected_action_trampoline<
            P: IsA<Drag>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GdkDrag,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Drag::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::selected-action\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_selected_action_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Drag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Drag")
    }
}