gtk/auto/gesture_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::{EventController, Gesture, GestureSingle, PropagationPhase, Widget, 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 /// [`GestureDrag`][crate::GestureDrag] is a [`Gesture`][crate::Gesture] implementation that recognizes drag
16 /// operations. The drag operation itself can be tracked throught the
17 /// [`drag-begin`][struct@crate::GestureDrag#drag-begin], [`drag-update`][struct@crate::GestureDrag#drag-update] and
18 /// [`drag-end`][struct@crate::GestureDrag#drag-end] signals, or the relevant coordinates be
19 /// extracted through [`GestureDragExt::offset()`][crate::prelude::GestureDragExt::offset()] and
20 /// [`GestureDragExt::start_point()`][crate::prelude::GestureDragExt::start_point()].
21 ///
22 /// ## Signals
23 ///
24 ///
25 /// #### `drag-begin`
26 /// This signal is emitted whenever dragging starts.
27 ///
28 ///
29 ///
30 ///
31 /// #### `drag-end`
32 /// This signal is emitted whenever the dragging is finished.
33 ///
34 ///
35 ///
36 ///
37 /// #### `drag-update`
38 /// This signal is emitted whenever the dragging point moves.
39 ///
40 ///
41 /// <details><summary><h4>Gesture</h4></summary>
42 ///
43 ///
44 /// #### `begin`
45 /// This signal is emitted when the gesture is recognized. This means the
46 /// number of touch sequences matches [`n-points`][struct@crate::Gesture#n-points], and the [`check`][struct@crate::Gesture#check]
47 /// handler(s) returned [`true`].
48 ///
49 /// Note: These conditions may also happen when an extra touch (eg. a third touch
50 /// on a 2-touches gesture) is lifted, in that situation `sequence` won't pertain
51 /// to the current set of active touches, so don't rely on this being true.
52 ///
53 ///
54 ///
55 ///
56 /// #### `cancel`
57 /// This signal is emitted whenever a sequence is cancelled. This usually
58 /// happens on active touches when [`EventControllerExt::reset()`][crate::prelude::EventControllerExt::reset()] is called
59 /// on `gesture` (manually, due to grabs...), or the individual `sequence`
60 /// was claimed by parent widgets' controllers (see [`GestureExt::set_sequence_state()`][crate::prelude::GestureExt::set_sequence_state()]).
61 ///
62 /// `gesture` must forget everything about `sequence` as a reaction to this signal.
63 ///
64 ///
65 ///
66 ///
67 /// #### `end`
68 /// This signal is emitted when `gesture` either stopped recognizing the event
69 /// sequences as something to be handled (the [`check`][struct@crate::Gesture#check] handler returned
70 /// [`false`]), or the number of touch sequences became higher or lower than
71 /// [`n-points`][struct@crate::Gesture#n-points].
72 ///
73 /// Note: `sequence` might not pertain to the group of sequences that were
74 /// previously triggering recognition on `gesture` (ie. a just pressed touch
75 /// sequence that exceeds [`n-points`][struct@crate::Gesture#n-points]). This situation may be detected
76 /// by checking through [`GestureExt::handles_sequence()`][crate::prelude::GestureExt::handles_sequence()].
77 ///
78 ///
79 ///
80 ///
81 /// #### `sequence-state-changed`
82 /// This signal is emitted whenever a sequence state changes. See
83 /// [`GestureExt::set_sequence_state()`][crate::prelude::GestureExt::set_sequence_state()] to know more about the expectable
84 /// sequence lifetimes.
85 ///
86 ///
87 ///
88 ///
89 /// #### `update`
90 /// This signal is emitted whenever an event is handled while the gesture is
91 /// recognized. `sequence` is guaranteed to pertain to the set of active touches.
92 ///
93 ///
94 /// </details>
95 ///
96 /// # Implements
97 ///
98 /// [`GestureDragExt`][trait@crate::prelude::GestureDragExt], [`GestureSingleExt`][trait@crate::prelude::GestureSingleExt], [`GestureExt`][trait@crate::prelude::GestureExt], [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`]
99 #[doc(alias = "GtkGestureDrag")]
100 pub struct GestureDrag(Object<ffi::GtkGestureDrag, ffi::GtkGestureDragClass>) @extends GestureSingle, Gesture, EventController;
101
102 match fn {
103 type_ => || ffi::gtk_gesture_drag_get_type(),
104 }
105}
106
107impl GestureDrag {
108 pub const NONE: Option<&'static GestureDrag> = None;
109
110 /// Returns a newly created [`Gesture`][crate::Gesture] that recognizes drags.
111 /// ## `widget`
112 /// a [`Widget`][crate::Widget]
113 ///
114 /// # Returns
115 ///
116 /// a newly created [`GestureDrag`][crate::GestureDrag]
117 #[doc(alias = "gtk_gesture_drag_new")]
118 pub fn new(widget: &impl IsA<Widget>) -> GestureDrag {
119 skip_assert_initialized!();
120 unsafe {
121 Gesture::from_glib_full(ffi::gtk_gesture_drag_new(widget.as_ref().to_glib_none().0))
122 .unsafe_cast()
123 }
124 }
125
126 // rustdoc-stripper-ignore-next
127 /// Creates a new builder-pattern struct instance to construct [`GestureDrag`] objects.
128 ///
129 /// This method returns an instance of [`GestureDragBuilder`](crate::builders::GestureDragBuilder) which can be used to create [`GestureDrag`] objects.
130 pub fn builder() -> GestureDragBuilder {
131 GestureDragBuilder::new()
132 }
133}
134
135impl Default for GestureDrag {
136 fn default() -> Self {
137 glib::object::Object::new::<Self>()
138 }
139}
140
141// rustdoc-stripper-ignore-next
142/// A [builder-pattern] type to construct [`GestureDrag`] objects.
143///
144/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
145#[must_use = "The builder must be built to be used"]
146pub struct GestureDragBuilder {
147 builder: glib::object::ObjectBuilder<'static, GestureDrag>,
148}
149
150impl GestureDragBuilder {
151 fn new() -> Self {
152 Self {
153 builder: glib::object::Object::builder(),
154 }
155 }
156
157 /// Mouse button number to listen to, or 0 to listen for any button.
158 pub fn button(self, button: u32) -> Self {
159 Self {
160 builder: self.builder.property("button", button),
161 }
162 }
163
164 /// Whether the gesture is exclusive. Exclusive gestures only listen to pointer
165 /// and pointer emulated events.
166 pub fn exclusive(self, exclusive: bool) -> Self {
167 Self {
168 builder: self.builder.property("exclusive", exclusive),
169 }
170 }
171
172 /// Whether the gesture handles only touch events.
173 pub fn touch_only(self, touch_only: bool) -> Self {
174 Self {
175 builder: self.builder.property("touch-only", touch_only),
176 }
177 }
178
179 /// The number of touch points that trigger recognition on this gesture,
180 pub fn n_points(self, n_points: u32) -> Self {
181 Self {
182 builder: self.builder.property("n-points", n_points),
183 }
184 }
185
186 /// If non-[`None`], the gesture will only listen for events that happen on
187 /// this [`gdk::Window`][crate::gdk::Window], or a child of it.
188 pub fn window(self, window: &gdk::Window) -> Self {
189 Self {
190 builder: self.builder.property("window", window.clone()),
191 }
192 }
193
194 /// The propagation phase at which this controller will handle events.
195 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
196 Self {
197 builder: self
198 .builder
199 .property("propagation-phase", propagation_phase),
200 }
201 }
202
203 /// The widget receiving the `GdkEvents` that the controller will handle.
204 pub fn widget(self, widget: &impl IsA<Widget>) -> Self {
205 Self {
206 builder: self.builder.property("widget", widget.clone().upcast()),
207 }
208 }
209
210 // rustdoc-stripper-ignore-next
211 /// Build the [`GestureDrag`].
212 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
213 pub fn build(self) -> GestureDrag {
214 assert_initialized_main_thread!();
215 self.builder.build()
216 }
217}
218
219/// Trait containing all [`struct@GestureDrag`] methods.
220///
221/// # Implementors
222///
223/// [`GestureDrag`][struct@crate::GestureDrag], [`GesturePan`][struct@crate::GesturePan]
224pub trait GestureDragExt: IsA<GestureDrag> + 'static {
225 /// If the `self` is active, this function returns [`true`] and
226 /// fills in `x` and `y` with the coordinates of the current point,
227 /// as an offset to the starting drag point.
228 ///
229 /// # Returns
230 ///
231 /// [`true`] if the gesture is active
232 ///
233 /// ## `x`
234 /// X offset for the current point
235 ///
236 /// ## `y`
237 /// Y offset for the current point
238 #[doc(alias = "gtk_gesture_drag_get_offset")]
239 #[doc(alias = "get_offset")]
240 fn offset(&self) -> Option<(f64, f64)> {
241 unsafe {
242 let mut x = std::mem::MaybeUninit::uninit();
243 let mut y = std::mem::MaybeUninit::uninit();
244 let ret = from_glib(ffi::gtk_gesture_drag_get_offset(
245 self.as_ref().to_glib_none().0,
246 x.as_mut_ptr(),
247 y.as_mut_ptr(),
248 ));
249 if ret {
250 Some((x.assume_init(), y.assume_init()))
251 } else {
252 None
253 }
254 }
255 }
256
257 /// If the `self` is active, this function returns [`true`]
258 /// and fills in `x` and `y` with the drag start coordinates,
259 /// in window-relative coordinates.
260 ///
261 /// # Returns
262 ///
263 /// [`true`] if the gesture is active
264 ///
265 /// ## `x`
266 /// X coordinate for the drag start point
267 ///
268 /// ## `y`
269 /// Y coordinate for the drag start point
270 #[doc(alias = "gtk_gesture_drag_get_start_point")]
271 #[doc(alias = "get_start_point")]
272 fn start_point(&self) -> Option<(f64, f64)> {
273 unsafe {
274 let mut x = std::mem::MaybeUninit::uninit();
275 let mut y = std::mem::MaybeUninit::uninit();
276 let ret = from_glib(ffi::gtk_gesture_drag_get_start_point(
277 self.as_ref().to_glib_none().0,
278 x.as_mut_ptr(),
279 y.as_mut_ptr(),
280 ));
281 if ret {
282 Some((x.assume_init(), y.assume_init()))
283 } else {
284 None
285 }
286 }
287 }
288
289 /// This signal is emitted whenever dragging starts.
290 /// ## `start_x`
291 /// X coordinate, relative to the widget allocation
292 /// ## `start_y`
293 /// Y coordinate, relative to the widget allocation
294 #[doc(alias = "drag-begin")]
295 fn connect_drag_begin<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
296 unsafe extern "C" fn drag_begin_trampoline<
297 P: IsA<GestureDrag>,
298 F: Fn(&P, f64, f64) + 'static,
299 >(
300 this: *mut ffi::GtkGestureDrag,
301 start_x: std::ffi::c_double,
302 start_y: std::ffi::c_double,
303 f: glib::ffi::gpointer,
304 ) {
305 unsafe {
306 let f: &F = &*(f as *const F);
307 f(
308 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
309 start_x,
310 start_y,
311 )
312 }
313 }
314 unsafe {
315 let f: Box_<F> = Box_::new(f);
316 connect_raw(
317 self.as_ptr() as *mut _,
318 c"drag-begin".as_ptr(),
319 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
320 drag_begin_trampoline::<Self, F> as *const (),
321 )),
322 Box_::into_raw(f),
323 )
324 }
325 }
326
327 /// This signal is emitted whenever the dragging is finished.
328 /// ## `offset_x`
329 /// X offset, relative to the start point
330 /// ## `offset_y`
331 /// Y offset, relative to the start point
332 #[doc(alias = "drag-end")]
333 fn connect_drag_end<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
334 unsafe extern "C" fn drag_end_trampoline<
335 P: IsA<GestureDrag>,
336 F: Fn(&P, f64, f64) + 'static,
337 >(
338 this: *mut ffi::GtkGestureDrag,
339 offset_x: std::ffi::c_double,
340 offset_y: std::ffi::c_double,
341 f: glib::ffi::gpointer,
342 ) {
343 unsafe {
344 let f: &F = &*(f as *const F);
345 f(
346 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
347 offset_x,
348 offset_y,
349 )
350 }
351 }
352 unsafe {
353 let f: Box_<F> = Box_::new(f);
354 connect_raw(
355 self.as_ptr() as *mut _,
356 c"drag-end".as_ptr(),
357 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
358 drag_end_trampoline::<Self, F> as *const (),
359 )),
360 Box_::into_raw(f),
361 )
362 }
363 }
364
365 /// This signal is emitted whenever the dragging point moves.
366 /// ## `offset_x`
367 /// X offset, relative to the start point
368 /// ## `offset_y`
369 /// Y offset, relative to the start point
370 #[doc(alias = "drag-update")]
371 fn connect_drag_update<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
372 unsafe extern "C" fn drag_update_trampoline<
373 P: IsA<GestureDrag>,
374 F: Fn(&P, f64, f64) + 'static,
375 >(
376 this: *mut ffi::GtkGestureDrag,
377 offset_x: std::ffi::c_double,
378 offset_y: std::ffi::c_double,
379 f: glib::ffi::gpointer,
380 ) {
381 unsafe {
382 let f: &F = &*(f as *const F);
383 f(
384 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
385 offset_x,
386 offset_y,
387 )
388 }
389 }
390 unsafe {
391 let f: Box_<F> = Box_::new(f);
392 connect_raw(
393 self.as_ptr() as *mut _,
394 c"drag-update".as_ptr(),
395 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
396 drag_update_trampoline::<Self, F> as *const (),
397 )),
398 Box_::into_raw(f),
399 )
400 }
401 }
402}
403
404impl<O: IsA<GestureDrag>> GestureDragExt for O {}