Skip to main content

gdk/auto/
drag_context.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::{Atom, Device, DragAction, DragCancelReason, DragProtocol, Window};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::{boxed::Box as Box_, fmt, mem::transmute};
12
13glib::wrapper! {
14    ///
15    ///
16    /// ## Signals
17    ///
18    ///
19    /// #### `action-changed`
20    ///  A new action is being chosen for the drag and drop operation.
21    ///
22    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
23    /// the drag and drop operation. See [`DragContext::manage_dnd()`][crate::DragContext::manage_dnd()]
24    /// for more information.
25    ///
26    ///
27    ///
28    ///
29    /// #### `cancel`
30    ///  The drag and drop operation was cancelled.
31    ///
32    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
33    /// the drag and drop operation. See [`DragContext::manage_dnd()`][crate::DragContext::manage_dnd()]
34    /// for more information.
35    ///
36    ///
37    ///
38    ///
39    /// #### `dnd-finished`
40    ///  The drag and drop operation was finished, the drag destination
41    /// finished reading all data. The drag source can now free all
42    /// miscellaneous data.
43    ///
44    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
45    /// the drag and drop operation. See [`DragContext::manage_dnd()`][crate::DragContext::manage_dnd()]
46    /// for more information.
47    ///
48    ///
49    ///
50    ///
51    /// #### `drop-performed`
52    ///  The drag and drop operation was performed on an accepting client.
53    ///
54    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
55    /// the drag and drop operation. See [`DragContext::manage_dnd()`][crate::DragContext::manage_dnd()]
56    /// for more information.
57    ///
58    ///
59    #[doc(alias = "GdkDragContext")]
60    pub struct DragContext(Object<ffi::GdkDragContext>);
61
62    match fn {
63        type_ => || ffi::gdk_drag_context_get_type(),
64    }
65}
66
67impl DragContext {
68    /// Determines the bitmask of actions proposed by the source if
69    /// [`suggested_action()`][Self::suggested_action()] returns [`DragAction::ASK`][crate::DragAction::ASK].
70    ///
71    /// # Returns
72    ///
73    /// the [`DragAction`][crate::DragAction] flags
74    #[doc(alias = "gdk_drag_context_get_actions")]
75    #[doc(alias = "get_actions")]
76    pub fn actions(&self) -> DragAction {
77        unsafe { from_glib(ffi::gdk_drag_context_get_actions(self.to_glib_none().0)) }
78    }
79
80    /// Returns the destination window for the DND operation.
81    ///
82    /// # Returns
83    ///
84    /// a [`Window`][crate::Window]
85    #[doc(alias = "gdk_drag_context_get_dest_window")]
86    #[doc(alias = "get_dest_window")]
87    pub fn dest_window(&self) -> Window {
88        unsafe { from_glib_none(ffi::gdk_drag_context_get_dest_window(self.to_glib_none().0)) }
89    }
90
91    /// Returns the [`Device`][crate::Device] associated to the drag context.
92    ///
93    /// # Returns
94    ///
95    /// The [`Device`][crate::Device] associated to `self`.
96    #[doc(alias = "gdk_drag_context_get_device")]
97    #[doc(alias = "get_device")]
98    pub fn device(&self) -> Device {
99        unsafe { from_glib_none(ffi::gdk_drag_context_get_device(self.to_glib_none().0)) }
100    }
101
102    /// Returns the window on which the drag icon should be rendered
103    /// during the drag operation. Note that the window may not be
104    /// available until the drag operation has begun. GDK will move
105    /// the window in accordance with the ongoing drag operation.
106    /// The window is owned by `self` and will be destroyed when
107    /// the drag operation is over.
108    ///
109    /// # Returns
110    ///
111    /// the drag window, or [`None`]
112    #[doc(alias = "gdk_drag_context_get_drag_window")]
113    #[doc(alias = "get_drag_window")]
114    pub fn drag_window(&self) -> Option<Window> {
115        unsafe { from_glib_none(ffi::gdk_drag_context_get_drag_window(self.to_glib_none().0)) }
116    }
117
118    /// Returns the drag protocol that is used by this context.
119    ///
120    /// # Returns
121    ///
122    /// the drag protocol
123    #[doc(alias = "gdk_drag_context_get_protocol")]
124    #[doc(alias = "get_protocol")]
125    pub fn protocol(&self) -> DragProtocol {
126        unsafe { from_glib(ffi::gdk_drag_context_get_protocol(self.to_glib_none().0)) }
127    }
128
129    /// Determines the action chosen by the drag destination.
130    ///
131    /// # Returns
132    ///
133    /// a [`DragAction`][crate::DragAction] value
134    #[doc(alias = "gdk_drag_context_get_selected_action")]
135    #[doc(alias = "get_selected_action")]
136    pub fn selected_action(&self) -> DragAction {
137        unsafe {
138            from_glib(ffi::gdk_drag_context_get_selected_action(
139                self.to_glib_none().0,
140            ))
141        }
142    }
143
144    /// Returns the [`Window`][crate::Window] where the DND operation started.
145    ///
146    /// # Returns
147    ///
148    /// a [`Window`][crate::Window]
149    #[doc(alias = "gdk_drag_context_get_source_window")]
150    #[doc(alias = "get_source_window")]
151    pub fn source_window(&self) -> Window {
152        unsafe {
153            from_glib_none(ffi::gdk_drag_context_get_source_window(
154                self.to_glib_none().0,
155            ))
156        }
157    }
158
159    /// Determines the suggested drag action of the context.
160    ///
161    /// # Returns
162    ///
163    /// a [`DragAction`][crate::DragAction] value
164    #[doc(alias = "gdk_drag_context_get_suggested_action")]
165    #[doc(alias = "get_suggested_action")]
166    pub fn suggested_action(&self) -> DragAction {
167        unsafe {
168            from_glib(ffi::gdk_drag_context_get_suggested_action(
169                self.to_glib_none().0,
170            ))
171        }
172    }
173
174    /// Retrieves the list of targets of the context.
175    ///
176    /// # Returns
177    ///
178    /// a `GList` of targets
179    #[doc(alias = "gdk_drag_context_list_targets")]
180    pub fn list_targets(&self) -> Vec<Atom> {
181        unsafe {
182            FromGlibPtrContainer::from_glib_none(ffi::gdk_drag_context_list_targets(
183                self.to_glib_none().0,
184            ))
185        }
186    }
187
188    /// Requests the drag and drop operation to be managed by `self`.
189    /// When a drag and drop operation becomes managed, the [`DragContext`][crate::DragContext]
190    /// will internally handle all input and source-side [`EventDND`][crate::EventDND] events
191    /// as required by the windowing system.
192    ///
193    /// Once the drag and drop operation is managed, the drag context will
194    /// emit the following signals:
195    /// - The [`action-changed`][struct@crate::DragContext#action-changed] signal whenever the final action
196    ///  to be performed by the drag and drop operation changes.
197    /// - The [`drop-performed`][struct@crate::DragContext#drop-performed] signal after the user performs
198    ///  the drag and drop gesture (typically by releasing the mouse button).
199    /// - The [`dnd-finished`][struct@crate::DragContext#dnd-finished] signal after the drag and drop
200    ///  operation concludes (after all `GdkSelection` transfers happen).
201    /// - The [`cancel`][struct@crate::DragContext#cancel] signal if the drag and drop operation is
202    ///  finished but doesn't happen over an accepting destination, or is
203    ///  cancelled through other means.
204    /// ## `ipc_window`
205    /// Window to use for IPC messaging/events
206    /// ## `actions`
207    /// the actions supported by the drag source
208    ///
209    /// # Returns
210    ///
211    /// [`true`] if the drag and drop operation is managed.
212    #[doc(alias = "gdk_drag_context_manage_dnd")]
213    pub fn manage_dnd(&self, ipc_window: &Window, actions: DragAction) -> bool {
214        unsafe {
215            from_glib(ffi::gdk_drag_context_manage_dnd(
216                self.to_glib_none().0,
217                ipc_window.to_glib_none().0,
218                actions.into_glib(),
219            ))
220        }
221    }
222
223    /// Associates a [`Device`][crate::Device] to `self`, so all Drag and Drop events
224    /// for `self` are emitted as if they came from this device.
225    /// ## `device`
226    /// a [`Device`][crate::Device]
227    #[doc(alias = "gdk_drag_context_set_device")]
228    pub fn set_device(&self, device: &impl IsA<Device>) {
229        unsafe {
230            ffi::gdk_drag_context_set_device(
231                self.to_glib_none().0,
232                device.as_ref().to_glib_none().0,
233            );
234        }
235    }
236
237    /// Sets the position of the drag window that will be kept
238    /// under the cursor hotspot. Initially, the hotspot is at the
239    /// top left corner of the drag window.
240    /// ## `hot_x`
241    /// x coordinate of the drag window hotspot
242    /// ## `hot_y`
243    /// y coordinate of the drag window hotspot
244    #[doc(alias = "gdk_drag_context_set_hotspot")]
245    pub fn set_hotspot(&self, hot_x: i32, hot_y: i32) {
246        unsafe {
247            ffi::gdk_drag_context_set_hotspot(self.to_glib_none().0, hot_x, hot_y);
248        }
249    }
250
251    /// A new action is being chosen for the drag and drop operation.
252    ///
253    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
254    /// the drag and drop operation. See [`manage_dnd()`][Self::manage_dnd()]
255    /// for more information.
256    /// ## `action`
257    /// The action currently chosen
258    #[doc(alias = "action-changed")]
259    pub fn connect_action_changed<F: Fn(&Self, DragAction) + 'static>(
260        &self,
261        f: F,
262    ) -> SignalHandlerId {
263        unsafe extern "C" fn action_changed_trampoline<
264            F: Fn(&DragContext, DragAction) + 'static,
265        >(
266            this: *mut ffi::GdkDragContext,
267            action: ffi::GdkDragAction,
268            f: glib::ffi::gpointer,
269        ) {
270            let f: &F = &*(f as *const F);
271            f(&from_glib_borrow(this), from_glib(action))
272        }
273        unsafe {
274            let f: Box_<F> = Box_::new(f);
275            connect_raw(
276                self.as_ptr() as *mut _,
277                b"action-changed\0".as_ptr() as *const _,
278                Some(transmute::<_, unsafe extern "C" fn()>(
279                    action_changed_trampoline::<F> as *const (),
280                )),
281                Box_::into_raw(f),
282            )
283        }
284    }
285
286    /// The drag and drop operation was cancelled.
287    ///
288    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
289    /// the drag and drop operation. See [`manage_dnd()`][Self::manage_dnd()]
290    /// for more information.
291    /// ## `reason`
292    /// The reason the context was cancelled
293    #[doc(alias = "cancel")]
294    pub fn connect_cancel<F: Fn(&Self, DragCancelReason) + 'static>(
295        &self,
296        f: F,
297    ) -> SignalHandlerId {
298        unsafe extern "C" fn cancel_trampoline<F: Fn(&DragContext, DragCancelReason) + 'static>(
299            this: *mut ffi::GdkDragContext,
300            reason: ffi::GdkDragCancelReason,
301            f: glib::ffi::gpointer,
302        ) {
303            let f: &F = &*(f as *const F);
304            f(&from_glib_borrow(this), from_glib(reason))
305        }
306        unsafe {
307            let f: Box_<F> = Box_::new(f);
308            connect_raw(
309                self.as_ptr() as *mut _,
310                b"cancel\0".as_ptr() as *const _,
311                Some(transmute::<_, unsafe extern "C" fn()>(
312                    cancel_trampoline::<F> as *const (),
313                )),
314                Box_::into_raw(f),
315            )
316        }
317    }
318
319    /// The drag and drop operation was finished, the drag destination
320    /// finished reading all data. The drag source can now free all
321    /// miscellaneous data.
322    ///
323    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
324    /// the drag and drop operation. See [`manage_dnd()`][Self::manage_dnd()]
325    /// for more information.
326    #[doc(alias = "dnd-finished")]
327    pub fn connect_dnd_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
328        unsafe extern "C" fn dnd_finished_trampoline<F: Fn(&DragContext) + 'static>(
329            this: *mut ffi::GdkDragContext,
330            f: glib::ffi::gpointer,
331        ) {
332            let f: &F = &*(f as *const F);
333            f(&from_glib_borrow(this))
334        }
335        unsafe {
336            let f: Box_<F> = Box_::new(f);
337            connect_raw(
338                self.as_ptr() as *mut _,
339                b"dnd-finished\0".as_ptr() as *const _,
340                Some(transmute::<_, unsafe extern "C" fn()>(
341                    dnd_finished_trampoline::<F> as *const (),
342                )),
343                Box_::into_raw(f),
344            )
345        }
346    }
347
348    /// The drag and drop operation was performed on an accepting client.
349    ///
350    /// This signal will only be emitted if the [`DragContext`][crate::DragContext] manages
351    /// the drag and drop operation. See [`manage_dnd()`][Self::manage_dnd()]
352    /// for more information.
353    /// ## `time`
354    /// the time at which the drop happened.
355    #[doc(alias = "drop-performed")]
356    pub fn connect_drop_performed<F: Fn(&Self, i32) + 'static>(&self, f: F) -> SignalHandlerId {
357        unsafe extern "C" fn drop_performed_trampoline<F: Fn(&DragContext, i32) + 'static>(
358            this: *mut ffi::GdkDragContext,
359            time: libc::c_int,
360            f: glib::ffi::gpointer,
361        ) {
362            let f: &F = &*(f as *const F);
363            f(&from_glib_borrow(this), time)
364        }
365        unsafe {
366            let f: Box_<F> = Box_::new(f);
367            connect_raw(
368                self.as_ptr() as *mut _,
369                b"drop-performed\0".as_ptr() as *const _,
370                Some(transmute::<_, unsafe extern "C" fn()>(
371                    drop_performed_trampoline::<F> as *const (),
372                )),
373                Box_::into_raw(f),
374            )
375        }
376    }
377}
378
379impl fmt::Display for DragContext {
380    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
381        f.write_str("DragContext")
382    }
383}