gdk4/auto/cursor.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::{ffi, Texture};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9 /// Used to create and destroy cursors.
10 ///
11 /// Cursors are immutable objects, so once you created them, there is no way
12 /// to modify them later. You should create a new cursor when you want to change
13 /// something about it.
14 ///
15 /// Cursors by themselves are not very interesting: they must be bound to a
16 /// window for users to see them. This is done with [`SurfaceExt::set_cursor()`][crate::prelude::SurfaceExt::set_cursor()]
17 /// or [`SurfaceExt::set_device_cursor()`][crate::prelude::SurfaceExt::set_device_cursor()]. Applications will typically
18 /// use higher-level GTK functions such as [gtk_widget_set_cursor()](../gtk4/method.Widget.set_cursor.html)
19 /// instead.
20 ///
21 /// Cursors are not bound to a given [`Display`][crate::Display], so they can be shared.
22 /// However, the appearance of cursors may vary when used on different
23 /// platforms.
24 ///
25 /// ## Named and texture cursors
26 ///
27 /// There are multiple ways to create cursors. The platform's own cursors
28 /// can be created with [`from_name()`][Self::from_name()]. That function lists
29 /// the commonly available names that are shared with the CSS specification.
30 /// Other names may be available, depending on the platform in use. On some
31 /// platforms, what images are used for named cursors may be influenced by
32 /// the cursor theme.
33 ///
34 /// Another option to create a cursor is to use [`from_texture()`][Self::from_texture()]
35 /// and provide an image to use for the cursor.
36 ///
37 /// To ease work with unsupported cursors, a fallback cursor can be provided.
38 /// If a [`Surface`][crate::Surface] cannot use a cursor because of the reasons mentioned
39 /// above, it will try the fallback cursor. Fallback cursors can themselves have
40 /// fallback cursors again, so it is possible to provide a chain of progressively
41 /// easier to support cursors. If none of the provided cursors can be supported,
42 /// the default cursor will be the ultimate fallback.
43 ///
44 /// ## Properties
45 ///
46 ///
47 /// #### `fallback`
48 /// Cursor to fall back to if this cursor cannot be displayed.
49 ///
50 /// Readable | Writeable | Construct Only
51 ///
52 ///
53 /// #### `hotspot-x`
54 /// X position of the cursor hotspot in the cursor image.
55 ///
56 /// Readable | Writeable | Construct Only
57 ///
58 ///
59 /// #### `hotspot-y`
60 /// Y position of the cursor hotspot in the cursor image.
61 ///
62 /// Readable | Writeable | Construct Only
63 ///
64 ///
65 /// #### `name`
66 /// Name of this this cursor.
67 ///
68 /// The name will be [`None`] if the cursor was created from a texture.
69 ///
70 /// Readable | Writeable | Construct Only
71 ///
72 ///
73 /// #### `texture`
74 /// The texture displayed by this cursor.
75 ///
76 /// The texture will be [`None`] if the cursor was created from a name.
77 ///
78 /// Readable | Writeable | Construct Only
79 #[doc(alias = "GdkCursor")]
80 pub struct Cursor(Object<ffi::GdkCursor>);
81
82 match fn {
83 type_ => || ffi::gdk_cursor_get_type(),
84 }
85}
86
87impl Cursor {
88 /// Creates a new cursor by looking up @name in the current cursor
89 /// theme.
90 ///
91 /// A recommended set of cursor names that will work across different
92 /// platforms can be found in the CSS specification:
93 ///
94 /// | | | |
95 /// | --- | --- | --- |
96 /// | | "none" | No cursor |
97 /// |  | "default" | The default cursor |
98 /// |  | "help" | Help is available |
99 /// |  | "pointer" | Indicates a link or interactive element |
100 /// |  |"context-menu" | A context menu is available |
101 /// |  | "progress" | Progress indicator |
102 /// |  | "wait" | Busy cursor |
103 /// |  | "cell" | Cell(s) may be selected |
104 /// |  | "crosshair" | Simple crosshair |
105 /// |  | "text" | Text may be selected |
106 /// |  | "vertical-text" | Vertical text may be selected |
107 /// |  | "alias" | DND: Something will be linked |
108 /// |  | "copy" | DND: Something will be copied |
109 /// |  | "move" | DND: Something will be moved |
110 /// |  | "dnd-ask" | DND: User can choose action to be carried out |
111 /// |  | "no-drop" | DND: Can't drop here |
112 /// |  | "not-allowed" | DND: Action will not be carried out |
113 /// |  | "grab" | DND: Something can be grabbed |
114 /// |  | "grabbing" | DND: Something is being grabbed |
115 /// |  | "n-resize" | Resizing: Move north border |
116 /// |  | "e-resize" | Resizing: Move east border |
117 /// |  | "s-resize" | Resizing: Move south border |
118 /// |  | "w-resize" | Resizing: Move west border |
119 /// |  | "ne-resize" | Resizing: Move north-east corner |
120 /// |  | "nw-resize" | Resizing: Move north-west corner |
121 /// |  | "sw-resize" | Resizing: Move south-west corner |
122 /// |  | "se-resize" | Resizing: Move south-east corner |
123 /// |  | "col-resize" | Resizing: Move an item or border horizontally |
124 /// |  | "row-resize" | Resizing: Move an item or border vertically |
125 /// |  | "ew-resize" | Moving: Something can be moved horizontally |
126 /// |  | "ns-resize" | Moving: Something can be moved vertically |
127 /// |  | "nesw-resize" | Moving: Something can be moved diagonally, north-east to south-west |
128 /// |  | "nwse-resize" | Moving: something can be moved diagonally, north-west to south-east |
129 /// |  | "all-resize" | Moving: Something can be moved in any direction |
130 /// |  | "all-scroll" | Can scroll in any direction |
131 /// |  | "zoom-in" | Zoom in |
132 /// |  | "zoom-out" | Zoom out |
133 /// ## `name`
134 /// the name of the cursor
135 /// ## `fallback`
136 /// [`None`] or the [`Cursor`][crate::Cursor] to fall back to when
137 /// this one cannot be supported
138 ///
139 /// # Returns
140 ///
141 /// a new [`Cursor`][crate::Cursor], or [`None`] if there is no
142 /// cursor with the given name
143 #[doc(alias = "gdk_cursor_new_from_name")]
144 #[doc(alias = "new_from_name")]
145 pub fn from_name(name: &str, fallback: Option<&Cursor>) -> Option<Cursor> {
146 assert_initialized_main_thread!();
147 unsafe {
148 from_glib_full(ffi::gdk_cursor_new_from_name(
149 name.to_glib_none().0,
150 fallback.to_glib_none().0,
151 ))
152 }
153 }
154
155 /// Creates a new cursor from a [`Texture`][crate::Texture].
156 /// ## `texture`
157 /// the texture providing the pixel data
158 /// ## `hotspot_x`
159 /// the horizontal offset of the “hotspot” of the cursor
160 /// ## `hotspot_y`
161 /// the vertical offset of the “hotspot” of the cursor
162 /// ## `fallback`
163 /// the [`Cursor`][crate::Cursor] to fall back to when
164 /// this one cannot be supported
165 ///
166 /// # Returns
167 ///
168 /// a new [`Cursor`][crate::Cursor]
169 #[doc(alias = "gdk_cursor_new_from_texture")]
170 #[doc(alias = "new_from_texture")]
171 pub fn from_texture(
172 texture: &impl IsA<Texture>,
173 hotspot_x: i32,
174 hotspot_y: i32,
175 fallback: Option<&Cursor>,
176 ) -> Cursor {
177 skip_assert_initialized!();
178 unsafe {
179 from_glib_full(ffi::gdk_cursor_new_from_texture(
180 texture.as_ref().to_glib_none().0,
181 hotspot_x,
182 hotspot_y,
183 fallback.to_glib_none().0,
184 ))
185 }
186 }
187
188 // rustdoc-stripper-ignore-next
189 /// Creates a new builder-pattern struct instance to construct [`Cursor`] objects.
190 ///
191 /// This method returns an instance of [`CursorBuilder`](crate::builders::CursorBuilder) which can be used to create [`Cursor`] objects.
192 pub fn builder() -> CursorBuilder {
193 CursorBuilder::new()
194 }
195
196 /// Returns the fallback for this @self.
197 ///
198 /// The fallback will be used if this cursor is not available on a given
199 /// [`Display`][crate::Display]. For named cursors, this can happen when using nonstandard
200 /// names or when using an incomplete cursor theme. For textured cursors,
201 /// this can happen when the texture is too large or when the [`Display`][crate::Display]
202 /// it is used on does not support textured cursors.
203 ///
204 /// # Returns
205 ///
206 /// the fallback of the cursor or [`None`]
207 /// to use the default cursor as fallback
208 #[doc(alias = "gdk_cursor_get_fallback")]
209 #[doc(alias = "get_fallback")]
210 #[must_use]
211 pub fn fallback(&self) -> Option<Cursor> {
212 unsafe { from_glib_none(ffi::gdk_cursor_get_fallback(self.to_glib_none().0)) }
213 }
214
215 /// Returns the horizontal offset of the hotspot.
216 ///
217 /// The hotspot indicates the pixel that will be directly above the cursor.
218 ///
219 /// Note that named cursors may have a nonzero hotspot, but this function
220 /// will only return the hotspot position for cursors created with
221 /// [`from_texture()`][Self::from_texture()].
222 ///
223 /// # Returns
224 ///
225 /// the horizontal offset of the hotspot or 0 for named cursors
226 #[doc(alias = "gdk_cursor_get_hotspot_x")]
227 #[doc(alias = "get_hotspot_x")]
228 #[doc(alias = "hotspot-x")]
229 pub fn hotspot_x(&self) -> i32 {
230 unsafe { ffi::gdk_cursor_get_hotspot_x(self.to_glib_none().0) }
231 }
232
233 /// Returns the vertical offset of the hotspot.
234 ///
235 /// The hotspot indicates the pixel that will be directly above the cursor.
236 ///
237 /// Note that named cursors may have a nonzero hotspot, but this function
238 /// will only return the hotspot position for cursors created with
239 /// [`from_texture()`][Self::from_texture()].
240 ///
241 /// # Returns
242 ///
243 /// the vertical offset of the hotspot or 0 for named cursors
244 #[doc(alias = "gdk_cursor_get_hotspot_y")]
245 #[doc(alias = "get_hotspot_y")]
246 #[doc(alias = "hotspot-y")]
247 pub fn hotspot_y(&self) -> i32 {
248 unsafe { ffi::gdk_cursor_get_hotspot_y(self.to_glib_none().0) }
249 }
250
251 /// Returns the name of the cursor.
252 ///
253 /// If the cursor is not a named cursor, [`None`] will be returned.
254 ///
255 /// # Returns
256 ///
257 /// the name of the cursor or [`None`]
258 /// if it is not a named cursor
259 #[doc(alias = "gdk_cursor_get_name")]
260 #[doc(alias = "get_name")]
261 pub fn name(&self) -> Option<glib::GString> {
262 unsafe { from_glib_none(ffi::gdk_cursor_get_name(self.to_glib_none().0)) }
263 }
264
265 /// Returns the texture for the cursor.
266 ///
267 /// If the cursor is a named cursor, [`None`] will be returned.
268 ///
269 /// # Returns
270 ///
271 /// the texture for cursor or [`None`]
272 /// if it is a named cursor
273 #[doc(alias = "gdk_cursor_get_texture")]
274 #[doc(alias = "get_texture")]
275 pub fn texture(&self) -> Option<Texture> {
276 unsafe { from_glib_none(ffi::gdk_cursor_get_texture(self.to_glib_none().0)) }
277 }
278}
279
280// rustdoc-stripper-ignore-next
281/// A [builder-pattern] type to construct [`Cursor`] objects.
282///
283/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
284#[must_use = "The builder must be built to be used"]
285pub struct CursorBuilder {
286 builder: glib::object::ObjectBuilder<'static, Cursor>,
287}
288
289impl CursorBuilder {
290 fn new() -> Self {
291 Self {
292 builder: glib::object::Object::builder(),
293 }
294 }
295
296 /// Cursor to fall back to if this cursor cannot be displayed.
297 pub fn fallback(self, fallback: &Cursor) -> Self {
298 Self {
299 builder: self.builder.property("fallback", fallback.clone()),
300 }
301 }
302
303 /// X position of the cursor hotspot in the cursor image.
304 pub fn hotspot_x(self, hotspot_x: i32) -> Self {
305 Self {
306 builder: self.builder.property("hotspot-x", hotspot_x),
307 }
308 }
309
310 /// Y position of the cursor hotspot in the cursor image.
311 pub fn hotspot_y(self, hotspot_y: i32) -> Self {
312 Self {
313 builder: self.builder.property("hotspot-y", hotspot_y),
314 }
315 }
316
317 /// Name of this this cursor.
318 ///
319 /// The name will be [`None`] if the cursor was created from a texture.
320 pub fn name(self, name: impl Into<glib::GString>) -> Self {
321 Self {
322 builder: self.builder.property("name", name.into()),
323 }
324 }
325
326 /// The texture displayed by this cursor.
327 ///
328 /// The texture will be [`None`] if the cursor was created from a name.
329 pub fn texture(self, texture: &impl IsA<Texture>) -> Self {
330 Self {
331 builder: self.builder.property("texture", texture.clone().upcast()),
332 }
333 }
334
335 // rustdoc-stripper-ignore-next
336 /// Build the [`Cursor`].
337 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
338 pub fn build(self) -> Cursor {
339 assert_initialized_main_thread!();
340 self.builder.build()
341 }
342}