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