Skip to main content

gdk4/auto/
drag.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{
6    ContentFormats, ContentProvider, Device, Display, DragAction, DragCancelReason, Surface, ffi,
7};
8use glib::{
9    object::ObjectType as _,
10    prelude::*,
11    signal::{SignalHandlerId, connect_raw},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17    /// Represents the source of an ongoing DND operation.
18    ///
19    /// A [`Drag`][crate::Drag] is created when a drag is started, and stays alive for duration of
20    /// the DND operation. After a drag has been started with [`begin()`][Self::begin()],
21    /// the caller gets informed about the status of the ongoing drag operation
22    /// with signals on the [`Drag`][crate::Drag] object.
23    ///
24    /// GTK provides a higher level abstraction based on top of these functions,
25    /// and so they are not normally needed in GTK applications. See the
26    /// "Drag and Drop" section of the GTK documentation for more information.
27    ///
28    /// This is an Abstract Base Class, you cannot instantiate it.
29    ///
30    /// ## Properties
31    ///
32    ///
33    /// #### `actions`
34    ///  The possible actions of this drag.
35    ///
36    /// Readable | Writeable
37    ///
38    ///
39    /// #### `content`
40    ///  The [`ContentProvider`][crate::ContentProvider].
41    ///
42    /// Readable | Writeable | Construct Only
43    ///
44    ///
45    /// #### `device`
46    ///  The [`Device`][crate::Device] that is performing the drag.
47    ///
48    /// Readable | Writeable | Construct Only
49    ///
50    ///
51    /// #### `display`
52    ///  The [`Display`][crate::Display] that the drag belongs to.
53    ///
54    /// Readable
55    ///
56    ///
57    /// #### `formats`
58    ///  The possible formats that the drag can provide its data in.
59    ///
60    /// Readable | Writeable | Construct Only
61    ///
62    ///
63    /// #### `selected-action`
64    ///  The currently selected action of the drag.
65    ///
66    /// Readable | Writeable
67    ///
68    ///
69    /// #### `surface`
70    ///  The surface where the drag originates.
71    ///
72    /// Readable | Writeable | Construct Only
73    ///
74    /// ## Signals
75    ///
76    ///
77    /// #### `cancel`
78    ///  Emitted when the drag operation is cancelled.
79    ///
80    ///
81    ///
82    ///
83    /// #### `dnd-finished`
84    ///  Emitted when the destination side has finished reading all data.
85    ///
86    /// The drag object can now free all miscellaneous data.
87    ///
88    ///
89    ///
90    ///
91    /// #### `drop-performed`
92    ///  Emitted when the drop operation is performed on an accepting client.
93    ///
94    ///
95    ///
96    /// # Implements
97    ///
98    /// [`DragExt`][trait@crate::prelude::DragExt]
99    #[doc(alias = "GdkDrag")]
100    pub struct Drag(Object<ffi::GdkDrag>);
101
102    match fn {
103        type_ => || ffi::gdk_drag_get_type(),
104    }
105}
106
107impl Drag {
108    pub const NONE: Option<&'static Drag> = None;
109
110    /// Starts a drag and creates a new drag context for it.
111    ///
112    /// This function is called by the drag source. After this call, you
113    /// probably want to set up the drag icon using the surface returned
114    /// by [`DragExt::drag_surface()`][crate::prelude::DragExt::drag_surface()].
115    ///
116    /// This function returns a reference to the [`Drag`][crate::Drag] object,
117    /// but GTK keeps its own reference as well, as long as the DND operation
118    /// is going on.
119    ///
120    /// Note: if @actions include [`DragAction::MOVE`][crate::DragAction::MOVE], you need to listen for
121    /// the [`dnd-finished`][struct@crate::Drag#dnd-finished] signal and delete the data at
122    /// the source if [`DragExt::selected_action()`][crate::prelude::DragExt::selected_action()] returns
123    /// [`DragAction::MOVE`][crate::DragAction::MOVE].
124    /// ## `surface`
125    /// the source surface for this drag
126    /// ## `device`
127    /// the device that controls this drag
128    /// ## `content`
129    /// the offered content
130    /// ## `actions`
131    /// the actions supported by this drag
132    /// ## `dx`
133    /// the x offset to @device's position where the drag nominally started
134    /// ## `dy`
135    /// the y offset to @device's position where the drag nominally started
136    ///
137    /// # Returns
138    ///
139    /// a newly created [`Drag`][crate::Drag]
140    #[doc(alias = "gdk_drag_begin")]
141    pub fn begin(
142        surface: &impl IsA<Surface>,
143        device: &impl IsA<Device>,
144        content: &impl IsA<ContentProvider>,
145        actions: DragAction,
146        dx: f64,
147        dy: f64,
148    ) -> Option<Drag> {
149        skip_assert_initialized!();
150        unsafe {
151            from_glib_full(ffi::gdk_drag_begin(
152                surface.as_ref().to_glib_none().0,
153                device.as_ref().to_glib_none().0,
154                content.as_ref().to_glib_none().0,
155                actions.into_glib(),
156                dx,
157                dy,
158            ))
159        }
160    }
161}
162
163/// Trait containing all [`struct@Drag`] methods.
164///
165/// # Implementors
166///
167/// [`Drag`][struct@crate::Drag]
168pub trait DragExt: IsA<Drag> + 'static {
169    /// Informs GDK that the drop ended.
170    ///
171    /// Passing [`false`] for @success may trigger a drag cancellation
172    /// animation.
173    ///
174    /// This function is called by the drag source, and should be the
175    /// last call before dropping the reference to the @self.
176    ///
177    /// The [`Drag`][crate::Drag] will only take the first [`drop_done()`][Self::drop_done()]
178    /// call as effective, if this function is called multiple times,
179    /// all subsequent calls will be ignored.
180    /// ## `success`
181    /// whether the drag was ultimatively successful
182    #[doc(alias = "gdk_drag_drop_done")]
183    fn drop_done(&self, success: bool) {
184        unsafe {
185            ffi::gdk_drag_drop_done(self.as_ref().to_glib_none().0, success.into_glib());
186        }
187    }
188
189    /// Determines the bitmask of possible actions proposed by the source.
190    ///
191    /// # Returns
192    ///
193    /// the [`DragAction`][crate::DragAction] flags
194    #[doc(alias = "gdk_drag_get_actions")]
195    #[doc(alias = "get_actions")]
196    fn actions(&self) -> DragAction {
197        unsafe { from_glib(ffi::gdk_drag_get_actions(self.as_ref().to_glib_none().0)) }
198    }
199
200    /// Returns the [`ContentProvider`][crate::ContentProvider] associated to the [`Drag`][crate::Drag] object.
201    ///
202    /// # Returns
203    ///
204    /// The [`ContentProvider`][crate::ContentProvider] associated to @self.
205    #[doc(alias = "gdk_drag_get_content")]
206    #[doc(alias = "get_content")]
207    fn content(&self) -> ContentProvider {
208        unsafe { from_glib_none(ffi::gdk_drag_get_content(self.as_ref().to_glib_none().0)) }
209    }
210
211    /// Returns the [`Device`][crate::Device] associated to the [`Drag`][crate::Drag] object.
212    ///
213    /// # Returns
214    ///
215    /// The [`Device`][crate::Device] associated to @self.
216    #[doc(alias = "gdk_drag_get_device")]
217    #[doc(alias = "get_device")]
218    fn device(&self) -> Device {
219        unsafe { from_glib_none(ffi::gdk_drag_get_device(self.as_ref().to_glib_none().0)) }
220    }
221
222    /// Gets the [`Display`][crate::Display] that the drag object was created for.
223    ///
224    /// # Returns
225    ///
226    /// a [`Display`][crate::Display]
227    #[doc(alias = "gdk_drag_get_display")]
228    #[doc(alias = "get_display")]
229    fn display(&self) -> Display {
230        unsafe { from_glib_none(ffi::gdk_drag_get_display(self.as_ref().to_glib_none().0)) }
231    }
232
233    /// Returns the surface on which the drag icon should be rendered
234    /// during the drag operation.
235    ///
236    /// Note that the surface may not be available until the drag operation
237    /// has begun. GDK will move the surface in accordance with the ongoing
238    /// drag operation. The surface is owned by @self and will be destroyed
239    /// when the drag operation is over.
240    ///
241    /// # Returns
242    ///
243    /// the drag surface
244    #[doc(alias = "gdk_drag_get_drag_surface")]
245    #[doc(alias = "get_drag_surface")]
246    fn drag_surface(&self) -> Option<Surface> {
247        unsafe {
248            from_glib_none(ffi::gdk_drag_get_drag_surface(
249                self.as_ref().to_glib_none().0,
250            ))
251        }
252    }
253
254    /// Retrieves the formats supported by this [`Drag`][crate::Drag] object.
255    ///
256    /// # Returns
257    ///
258    /// a [`ContentFormats`][crate::ContentFormats]
259    #[doc(alias = "gdk_drag_get_formats")]
260    #[doc(alias = "get_formats")]
261    fn formats(&self) -> ContentFormats {
262        unsafe { from_glib_none(ffi::gdk_drag_get_formats(self.as_ref().to_glib_none().0)) }
263    }
264
265    /// Determines the action chosen by the drag destination.
266    ///
267    /// # Returns
268    ///
269    /// a [`DragAction`][crate::DragAction] value
270    #[doc(alias = "gdk_drag_get_selected_action")]
271    #[doc(alias = "get_selected_action")]
272    #[doc(alias = "selected-action")]
273    fn selected_action(&self) -> DragAction {
274        unsafe {
275            from_glib(ffi::gdk_drag_get_selected_action(
276                self.as_ref().to_glib_none().0,
277            ))
278        }
279    }
280
281    /// Returns the [`Surface`][crate::Surface] where the drag originates.
282    ///
283    /// # Returns
284    ///
285    /// The [`Surface`][crate::Surface] where the drag originates
286    #[doc(alias = "gdk_drag_get_surface")]
287    #[doc(alias = "get_surface")]
288    fn surface(&self) -> Surface {
289        unsafe { from_glib_none(ffi::gdk_drag_get_surface(self.as_ref().to_glib_none().0)) }
290    }
291
292    /// Sets the position of the drag surface that will be kept
293    /// under the cursor hotspot.
294    ///
295    /// Initially, the hotspot is at the top left corner of the drag surface.
296    /// ## `hot_x`
297    /// x coordinate of the drag surface hotspot
298    /// ## `hot_y`
299    /// y coordinate of the drag surface hotspot
300    #[doc(alias = "gdk_drag_set_hotspot")]
301    fn set_hotspot(&self, hot_x: i32, hot_y: i32) {
302        unsafe {
303            ffi::gdk_drag_set_hotspot(self.as_ref().to_glib_none().0, hot_x, hot_y);
304        }
305    }
306
307    /// The possible actions of this drag.
308    fn set_actions(&self, actions: DragAction) {
309        ObjectExt::set_property(self.as_ref(), "actions", actions)
310    }
311
312    /// The currently selected action of the drag.
313    #[doc(alias = "selected-action")]
314    fn set_selected_action(&self, selected_action: DragAction) {
315        ObjectExt::set_property(self.as_ref(), "selected-action", selected_action)
316    }
317
318    /// Emitted when the drag operation is cancelled.
319    /// ## `reason`
320    /// The reason the drag was cancelled
321    #[doc(alias = "cancel")]
322    fn connect_cancel<F: Fn(&Self, DragCancelReason) + 'static>(&self, f: F) -> SignalHandlerId {
323        unsafe extern "C" fn cancel_trampoline<
324            P: IsA<Drag>,
325            F: Fn(&P, DragCancelReason) + 'static,
326        >(
327            this: *mut ffi::GdkDrag,
328            reason: ffi::GdkDragCancelReason,
329            f: glib::ffi::gpointer,
330        ) {
331            unsafe {
332                let f: &F = &*(f as *const F);
333                f(
334                    Drag::from_glib_borrow(this).unsafe_cast_ref(),
335                    from_glib(reason),
336                )
337            }
338        }
339        unsafe {
340            let f: Box_<F> = Box_::new(f);
341            connect_raw(
342                self.as_ptr() as *mut _,
343                c"cancel".as_ptr() as *const _,
344                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
345                    cancel_trampoline::<Self, F> as *const (),
346                )),
347                Box_::into_raw(f),
348            )
349        }
350    }
351
352    /// Emitted when the destination side has finished reading all data.
353    ///
354    /// The drag object can now free all miscellaneous data.
355    #[doc(alias = "dnd-finished")]
356    fn connect_dnd_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
357        unsafe extern "C" fn dnd_finished_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
358            this: *mut ffi::GdkDrag,
359            f: glib::ffi::gpointer,
360        ) {
361            unsafe {
362                let f: &F = &*(f as *const F);
363                f(Drag::from_glib_borrow(this).unsafe_cast_ref())
364            }
365        }
366        unsafe {
367            let f: Box_<F> = Box_::new(f);
368            connect_raw(
369                self.as_ptr() as *mut _,
370                c"dnd-finished".as_ptr() as *const _,
371                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
372                    dnd_finished_trampoline::<Self, F> as *const (),
373                )),
374                Box_::into_raw(f),
375            )
376        }
377    }
378
379    /// Emitted when the drop operation is performed on an accepting client.
380    #[doc(alias = "drop-performed")]
381    fn connect_drop_performed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
382        unsafe extern "C" fn drop_performed_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
383            this: *mut ffi::GdkDrag,
384            f: glib::ffi::gpointer,
385        ) {
386            unsafe {
387                let f: &F = &*(f as *const F);
388                f(Drag::from_glib_borrow(this).unsafe_cast_ref())
389            }
390        }
391        unsafe {
392            let f: Box_<F> = Box_::new(f);
393            connect_raw(
394                self.as_ptr() as *mut _,
395                c"drop-performed".as_ptr() as *const _,
396                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
397                    drop_performed_trampoline::<Self, F> as *const (),
398                )),
399                Box_::into_raw(f),
400            )
401        }
402    }
403
404    #[doc(alias = "actions")]
405    fn connect_actions_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
406        unsafe extern "C" fn notify_actions_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
407            this: *mut ffi::GdkDrag,
408            _param_spec: glib::ffi::gpointer,
409            f: glib::ffi::gpointer,
410        ) {
411            unsafe {
412                let f: &F = &*(f as *const F);
413                f(Drag::from_glib_borrow(this).unsafe_cast_ref())
414            }
415        }
416        unsafe {
417            let f: Box_<F> = Box_::new(f);
418            connect_raw(
419                self.as_ptr() as *mut _,
420                c"notify::actions".as_ptr() as *const _,
421                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
422                    notify_actions_trampoline::<Self, F> as *const (),
423                )),
424                Box_::into_raw(f),
425            )
426        }
427    }
428
429    #[doc(alias = "display")]
430    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
431        unsafe extern "C" fn notify_display_trampoline<P: IsA<Drag>, F: Fn(&P) + 'static>(
432            this: *mut ffi::GdkDrag,
433            _param_spec: glib::ffi::gpointer,
434            f: glib::ffi::gpointer,
435        ) {
436            unsafe {
437                let f: &F = &*(f as *const F);
438                f(Drag::from_glib_borrow(this).unsafe_cast_ref())
439            }
440        }
441        unsafe {
442            let f: Box_<F> = Box_::new(f);
443            connect_raw(
444                self.as_ptr() as *mut _,
445                c"notify::display".as_ptr() as *const _,
446                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
447                    notify_display_trampoline::<Self, F> as *const (),
448                )),
449                Box_::into_raw(f),
450            )
451        }
452    }
453
454    #[doc(alias = "selected-action")]
455    fn connect_selected_action_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
456        unsafe extern "C" fn notify_selected_action_trampoline<
457            P: IsA<Drag>,
458            F: Fn(&P) + 'static,
459        >(
460            this: *mut ffi::GdkDrag,
461            _param_spec: glib::ffi::gpointer,
462            f: glib::ffi::gpointer,
463        ) {
464            unsafe {
465                let f: &F = &*(f as *const F);
466                f(Drag::from_glib_borrow(this).unsafe_cast_ref())
467            }
468        }
469        unsafe {
470            let f: Box_<F> = Box_::new(f);
471            connect_raw(
472                self.as_ptr() as *mut _,
473                c"notify::selected-action".as_ptr() as *const _,
474                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
475                    notify_selected_action_trampoline::<Self, F> as *const (),
476                )),
477                Box_::into_raw(f),
478            )
479        }
480    }
481}
482
483impl<O: IsA<Drag>> DragExt for O {}