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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::EventController;
use crate::Gesture;
use crate::GestureSingle;
use crate::PropagationLimit;
use crate::PropagationPhase;
use glib::object::Cast;
use glib::object::IsA;
use glib::object::ObjectType as ObjectType_;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::StaticType;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// [`DragSource`][crate::DragSource] is an event controller to initiate Drag-And-Drop operations.
    ///
    /// [`DragSource`][crate::DragSource] can be set up with the necessary
    /// ingredients for a DND operation ahead of time. This includes
    /// the source for the data that is being transferred, in the form
    /// of a [`gdk::ContentProvider`][crate::gdk::ContentProvider], the desired action, and the icon to
    /// use during the drag operation. After setting it up, the drag
    /// source must be added to a widget as an event controller, using
    /// [`WidgetExt::add_controller()`][crate::prelude::WidgetExt::add_controller()].
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// static void
    /// my_widget_init (MyWidget *self)
    /// {
    ///   GtkDragSource *drag_source = gtk_drag_source_new ();
    ///
    ///   g_signal_connect (drag_source, "prepare", G_CALLBACK (on_drag_prepare), self);
    ///   g_signal_connect (drag_source, "drag-begin", G_CALLBACK (on_drag_begin), self);
    ///
    ///   gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source));
    /// }
    /// ```
    ///
    /// Setting up the content provider and icon ahead of time only makes
    /// sense when the data does not change. More commonly, you will want
    /// to set them up just in time. To do so, [`DragSource`][crate::DragSource] has
    /// `signal::DragSource::prepare` and `signal::DragSource::drag-begin`
    /// signals.
    ///
    /// The ::prepare signal is emitted before a drag is started, and
    /// can be used to set the content provider and actions that the
    /// drag should be started with.
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// static GdkContentProvider *
    /// on_drag_prepare (GtkDragSource *source,
    ///                  double         x,
    ///                  double         y,
    ///                  MyWidget      *self)
    /// {
    ///   // This widget supports two types of content: GFile objects
    ///   // and GdkPixbuf objects; GTK will handle the serialization
    ///   // of these types automatically
    ///   GFile *file = my_widget_get_file (self);
    ///   GdkPixbuf *pixbuf = my_widget_get_pixbuf (self);
    ///
    ///   return gdk_content_provider_new_union ((GdkContentProvider *[2]) {
    ///       gdk_content_provider_new_typed (G_TYPE_FILE, file),
    ///       gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf),
    ///     }, 2);
    /// }
    /// ```
    ///
    /// The ::drag-begin signal is emitted after the [`gdk::Drag`][crate::gdk::Drag] object has
    /// been created, and can be used to set up the drag icon.
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// static void
    /// on_drag_begin (GtkDragSource *source,
    ///                GdkDrag       *drag,
    ///                MyWidget      *self)
    /// {
    ///   // Set the widget as the drag icon
    ///   GdkPaintable *paintable = gtk_widget_paintable_new (GTK_WIDGET (self));
    ///   gtk_drag_source_set_icon (source, paintable, 0, 0);
    ///   g_object_unref (paintable);
    /// }
    /// ```
    ///
    /// During the DND operation, [`DragSource`][crate::DragSource] emits signals that
    /// can be used to obtain updates about the status of the operation,
    /// but it is not normally necessary to connect to any signals,
    /// except for one case: when the supported actions include
    /// [`gdk::DragAction::MOVE`][crate::gdk::DragAction::MOVE], you need to listen for the
    /// `signal::DragSource::drag-end` signal and delete the
    /// data after it has been transferred.
    ///
    /// # Implements
    ///
    /// [`GestureSingleExt`][trait@crate::prelude::GestureSingleExt], [`GestureExt`][trait@crate::prelude::GestureExt], [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkDragSource")]
    pub struct DragSource(Object<ffi::GtkDragSource, ffi::GtkDragSourceClass>) @extends GestureSingle, Gesture, EventController;

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

impl DragSource {
    /// Creates a new [`DragSource`][crate::DragSource] object.
    ///
    /// # Returns
    ///
    /// the new [`DragSource`][crate::DragSource]
    #[doc(alias = "gtk_drag_source_new")]
    pub fn new() -> DragSource {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gtk_drag_source_new()) }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`DragSource`] objects.
    ///
    /// This method returns an instance of [`DragSourceBuilder`](crate::builders::DragSourceBuilder) which can be used to create [`DragSource`] objects.
    pub fn builder() -> DragSourceBuilder {
        DragSourceBuilder::default()
    }

    /// Cancels a currently ongoing drag operation.
    #[doc(alias = "gtk_drag_source_drag_cancel")]
    pub fn drag_cancel(&self) {
        unsafe {
            ffi::gtk_drag_source_drag_cancel(self.to_glib_none().0);
        }
    }

    /// Gets the actions that are currently set on the [`DragSource`][crate::DragSource].
    ///
    /// # Returns
    ///
    /// the actions set on @self
    #[doc(alias = "gtk_drag_source_get_actions")]
    #[doc(alias = "get_actions")]
    pub fn actions(&self) -> gdk::DragAction {
        unsafe { from_glib(ffi::gtk_drag_source_get_actions(self.to_glib_none().0)) }
    }

    /// Gets the current content provider of a [`DragSource`][crate::DragSource].
    ///
    /// # Returns
    ///
    /// the [`gdk::ContentProvider`][crate::gdk::ContentProvider] of @self
    #[doc(alias = "gtk_drag_source_get_content")]
    #[doc(alias = "get_content")]
    pub fn content(&self) -> Option<gdk::ContentProvider> {
        unsafe { from_glib_none(ffi::gtk_drag_source_get_content(self.to_glib_none().0)) }
    }

    /// Returns the underlying [`gdk::Drag`][crate::gdk::Drag] object for an ongoing drag.
    ///
    /// # Returns
    ///
    /// the [`gdk::Drag`][crate::gdk::Drag] of the current
    ///   drag operation
    #[doc(alias = "gtk_drag_source_get_drag")]
    #[doc(alias = "get_drag")]
    pub fn drag(&self) -> Option<gdk::Drag> {
        unsafe { from_glib_none(ffi::gtk_drag_source_get_drag(self.to_glib_none().0)) }
    }

    /// Sets the actions on the [`DragSource`][crate::DragSource].
    ///
    /// During a DND operation, the actions are offered to potential
    /// drop targets. If @actions include [`gdk::DragAction::MOVE`][crate::gdk::DragAction::MOVE], you need
    /// to listen to the `signal::DragSource::drag-end` signal and
    /// handle @delete_data being [`true`].
    ///
    /// This function can be called before a drag is started,
    /// or in a handler for the `signal::DragSource::prepare` signal.
    /// ## `actions`
    /// the actions to offer
    #[doc(alias = "gtk_drag_source_set_actions")]
    pub fn set_actions(&self, actions: gdk::DragAction) {
        unsafe {
            ffi::gtk_drag_source_set_actions(self.to_glib_none().0, actions.into_glib());
        }
    }

    /// Sets a content provider on a [`DragSource`][crate::DragSource].
    ///
    /// When the data is requested in the cause of a DND operation,
    /// it will be obtained from the content provider.
    ///
    /// This function can be called before a drag is started,
    /// or in a handler for the `signal::DragSource::prepare` signal.
    ///
    /// You may consider setting the content provider back to
    /// [`None`] in a `signal::DragSource::drag-end` signal handler.
    /// ## `content`
    /// a [`gdk::ContentProvider`][crate::gdk::ContentProvider]
    #[doc(alias = "gtk_drag_source_set_content")]
    pub fn set_content(&self, content: Option<&impl IsA<gdk::ContentProvider>>) {
        unsafe {
            ffi::gtk_drag_source_set_content(
                self.to_glib_none().0,
                content.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    /// Sets a paintable to use as icon during DND operations.
    ///
    /// The hotspot coordinates determine the point on the icon
    /// that gets aligned with the hotspot of the cursor.
    ///
    /// If @paintable is [`None`], a default icon is used.
    ///
    /// This function can be called before a drag is started, or in
    /// a `signal::DragSource::prepare` or
    /// `signal::DragSource::drag-begin` signal handler.
    /// ## `paintable`
    /// the [`gdk::Paintable`][crate::gdk::Paintable] to use as icon
    /// ## `hot_x`
    /// the hotspot X coordinate on the icon
    /// ## `hot_y`
    /// the hotspot Y coordinate on the icon
    #[doc(alias = "gtk_drag_source_set_icon")]
    pub fn set_icon(&self, paintable: Option<&impl IsA<gdk::Paintable>>, hot_x: i32, hot_y: i32) {
        unsafe {
            ffi::gtk_drag_source_set_icon(
                self.to_glib_none().0,
                paintable.map(|p| p.as_ref()).to_glib_none().0,
                hot_x,
                hot_y,
            );
        }
    }

    /// Emitted on the drag source when a drag is started.
    ///
    /// It can be used to e.g. set a custom drag icon with
    /// [`set_icon()`][Self::set_icon()].
    /// ## `drag`
    /// the [`gdk::Drag`][crate::gdk::Drag] object
    #[doc(alias = "drag-begin")]
    pub fn connect_drag_begin<F: Fn(&Self, &gdk::Drag) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn drag_begin_trampoline<F: Fn(&DragSource, &gdk::Drag) + 'static>(
            this: *mut ffi::GtkDragSource,
            drag: *mut gdk::ffi::GdkDrag,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(drag))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drag-begin\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drag_begin_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted on the drag source when a drag has failed.
    ///
    /// The signal handler may handle a failed drag operation based on
    /// the type of error. It should return [`true`] if the failure has been handled
    /// and the default "drag operation failed" animation should not be shown.
    /// ## `drag`
    /// the [`gdk::Drag`][crate::gdk::Drag] object
    /// ## `reason`
    /// information on why the drag failed
    ///
    /// # Returns
    ///
    /// [`true`] if the failed drag operation has been already handled
    #[doc(alias = "drag-cancel")]
    pub fn connect_drag_cancel<
        F: Fn(&Self, &gdk::Drag, gdk::DragCancelReason) -> bool + 'static,
    >(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn drag_cancel_trampoline<
            F: Fn(&DragSource, &gdk::Drag, gdk::DragCancelReason) -> bool + 'static,
        >(
            this: *mut ffi::GtkDragSource,
            drag: *mut gdk::ffi::GdkDrag,
            reason: gdk::ffi::GdkDragCancelReason,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(drag),
                from_glib(reason),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drag-cancel\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drag_cancel_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted on the drag source when a drag is finished.
    ///
    /// A typical reason to connect to this signal is to undo
    /// things done in `signal::DragSource::prepare` or
    /// `signal::DragSource::drag-begin` handlers.
    /// ## `drag`
    /// the [`gdk::Drag`][crate::gdk::Drag] object
    /// ## `delete_data`
    /// [`true`] if the drag was performing [`gdk::DragAction::MOVE`][crate::gdk::DragAction::MOVE],
    ///    and the data should be deleted
    #[doc(alias = "drag-end")]
    pub fn connect_drag_end<F: Fn(&Self, &gdk::Drag, bool) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn drag_end_trampoline<F: Fn(&DragSource, &gdk::Drag, bool) + 'static>(
            this: *mut ffi::GtkDragSource,
            drag: *mut gdk::ffi::GdkDrag,
            delete_data: glib::ffi::gboolean,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(drag),
                from_glib(delete_data),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"drag-end\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    drag_end_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted when a drag is about to be initiated.
    ///
    /// It returns the [`gdk::ContentProvider`][crate::gdk::ContentProvider] to use for the drag that is about
    /// to start. The default handler for this signal returns the value of
    /// the `property::DragSource::content` property, so if you set up that
    /// property ahead of time, you don't need to connect to this signal.
    /// ## `x`
    /// the X coordinate of the drag starting point
    /// ## `y`
    /// the Y coordinate of the drag starting point
    ///
    /// # Returns
    ///
    /// a [`gdk::ContentProvider`][crate::gdk::ContentProvider]
    #[doc(alias = "prepare")]
    pub fn connect_prepare<F: Fn(&Self, f64, f64) -> Option<gdk::ContentProvider> + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn prepare_trampoline<
            F: Fn(&DragSource, f64, f64) -> Option<gdk::ContentProvider> + 'static,
        >(
            this: *mut ffi::GtkDragSource,
            x: libc::c_double,
            y: libc::c_double,
            f: glib::ffi::gpointer,
        ) -> *mut gdk::ffi::GdkContentProvider {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), x, y).to_glib_full()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"prepare\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    prepare_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "actions")]
    pub fn connect_actions_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_actions_trampoline<F: Fn(&DragSource) + 'static>(
            this: *mut ffi::GtkDragSource,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        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::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "content")]
    pub fn connect_content_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_content_trampoline<F: Fn(&DragSource) + 'static>(
            this: *mut ffi::GtkDragSource,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::content\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_content_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

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

#[derive(Clone, Default)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`DragSource`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct DragSourceBuilder {
    actions: Option<gdk::DragAction>,
    content: Option<gdk::ContentProvider>,
    button: Option<u32>,
    exclusive: Option<bool>,
    touch_only: Option<bool>,
    n_points: Option<u32>,
    name: Option<String>,
    propagation_limit: Option<PropagationLimit>,
    propagation_phase: Option<PropagationPhase>,
}

impl DragSourceBuilder {
    // rustdoc-stripper-ignore-next
    /// Create a new [`DragSourceBuilder`].
    pub fn new() -> Self {
        Self::default()
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`DragSource`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> DragSource {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref actions) = self.actions {
            properties.push(("actions", actions));
        }
        if let Some(ref content) = self.content {
            properties.push(("content", content));
        }
        if let Some(ref button) = self.button {
            properties.push(("button", button));
        }
        if let Some(ref exclusive) = self.exclusive {
            properties.push(("exclusive", exclusive));
        }
        if let Some(ref touch_only) = self.touch_only {
            properties.push(("touch-only", touch_only));
        }
        if let Some(ref n_points) = self.n_points {
            properties.push(("n-points", n_points));
        }
        if let Some(ref name) = self.name {
            properties.push(("name", name));
        }
        if let Some(ref propagation_limit) = self.propagation_limit {
            properties.push(("propagation-limit", propagation_limit));
        }
        if let Some(ref propagation_phase) = self.propagation_phase {
            properties.push(("propagation-phase", propagation_phase));
        }
        glib::Object::new::<DragSource>(&properties)
    }

    /// The actions that are supported by drag operations from the source.
    ///
    /// Note that you must handle the `signal::DragSource::drag-end` signal
    /// if the actions include [`gdk::DragAction::MOVE`][crate::gdk::DragAction::MOVE].
    pub fn actions(mut self, actions: gdk::DragAction) -> Self {
        self.actions = Some(actions);
        self
    }

    /// The data that is offered by drag operations from this source.
    pub fn content(mut self, content: &impl IsA<gdk::ContentProvider>) -> Self {
        self.content = Some(content.clone().upcast());
        self
    }

    /// Mouse button number to listen to, or 0 to listen for any button.
    pub fn button(mut self, button: u32) -> Self {
        self.button = Some(button);
        self
    }

    /// Whether the gesture is exclusive.
    ///
    /// Exclusive gestures only listen to pointer and pointer emulated events.
    pub fn exclusive(mut self, exclusive: bool) -> Self {
        self.exclusive = Some(exclusive);
        self
    }

    /// Whether the gesture handles only touch events.
    pub fn touch_only(mut self, touch_only: bool) -> Self {
        self.touch_only = Some(touch_only);
        self
    }

    /// The number of touch points that trigger
    /// recognition on this gesture.
    pub fn n_points(mut self, n_points: u32) -> Self {
        self.n_points = Some(n_points);
        self
    }

    /// The name for this controller, typically used for debugging purposes.
    pub fn name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    /// The limit for which events this controller will handle.
    pub fn propagation_limit(mut self, propagation_limit: PropagationLimit) -> Self {
        self.propagation_limit = Some(propagation_limit);
        self
    }

    /// The propagation phase at which this controller will handle events.
    pub fn propagation_phase(mut self, propagation_phase: PropagationPhase) -> Self {
        self.propagation_phase = Some(propagation_phase);
        self
    }
}

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