Skip to main content

gtk/auto/
selection_data.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::{TextBuffer, ffi};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10    pub struct SelectionData(Boxed<ffi::GtkSelectionData>);
11
12    match fn {
13        copy => |ptr| ffi::gtk_selection_data_copy(ptr),
14        free => |ptr| ffi::gtk_selection_data_free(ptr),
15        type_ => || ffi::gtk_selection_data_get_type(),
16    }
17}
18
19impl SelectionData {
20    /// Retrieves the data type of the selection.
21    ///
22    /// # Returns
23    ///
24    /// the data type of the selection.
25    #[doc(alias = "gtk_selection_data_get_data_type")]
26    #[doc(alias = "get_data_type")]
27    pub fn data_type(&self) -> gdk::Atom {
28        unsafe { from_glib_none(ffi::gtk_selection_data_get_data_type(self.to_glib_none().0)) }
29    }
30
31    /// Retrieves the display of the selection.
32    ///
33    /// # Returns
34    ///
35    /// the display of the selection.
36    #[doc(alias = "gtk_selection_data_get_display")]
37    #[doc(alias = "get_display")]
38    pub fn display(&self) -> Option<gdk::Display> {
39        unsafe { from_glib_none(ffi::gtk_selection_data_get_display(self.to_glib_none().0)) }
40    }
41
42    /// Retrieves the format of the selection.
43    ///
44    /// # Returns
45    ///
46    /// the format of the selection.
47    #[doc(alias = "gtk_selection_data_get_format")]
48    #[doc(alias = "get_format")]
49    pub fn format(&self) -> i32 {
50        unsafe { ffi::gtk_selection_data_get_format(self.to_glib_none().0) }
51    }
52
53    /// Retrieves the length of the raw data of the selection.
54    ///
55    /// # Returns
56    ///
57    /// the length of the data of the selection.
58    #[doc(alias = "gtk_selection_data_get_length")]
59    #[doc(alias = "get_length")]
60    pub fn length(&self) -> i32 {
61        unsafe { ffi::gtk_selection_data_get_length(self.to_glib_none().0) }
62    }
63
64    /// Gets the contents of the selection data as a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
65    ///
66    /// # Returns
67    ///
68    /// if the selection data
69    ///  contained a recognized image type and it could be converted to a
70    ///  [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf], a newly allocated pixbuf is returned, otherwise
71    ///  [`None`]. If the result is non-[`None`] it must be freed with
72    ///  `g_object_unref()`.
73    #[doc(alias = "gtk_selection_data_get_pixbuf")]
74    #[doc(alias = "get_pixbuf")]
75    pub fn pixbuf(&self) -> Option<gdk_pixbuf::Pixbuf> {
76        unsafe { from_glib_full(ffi::gtk_selection_data_get_pixbuf(self.to_glib_none().0)) }
77    }
78
79    /// Retrieves the selection [`gdk::Atom`][crate::gdk::Atom] of the selection data.
80    ///
81    /// # Returns
82    ///
83    /// the selection [`gdk::Atom`][crate::gdk::Atom] of the selection data.
84    #[doc(alias = "gtk_selection_data_get_selection")]
85    #[doc(alias = "get_selection")]
86    pub fn selection(&self) -> gdk::Atom {
87        unsafe { from_glib_none(ffi::gtk_selection_data_get_selection(self.to_glib_none().0)) }
88    }
89
90    /// Retrieves the target of the selection.
91    ///
92    /// # Returns
93    ///
94    /// the target of the selection.
95    #[doc(alias = "gtk_selection_data_get_target")]
96    #[doc(alias = "get_target")]
97    pub fn target(&self) -> gdk::Atom {
98        unsafe { from_glib_none(ffi::gtk_selection_data_get_target(self.to_glib_none().0)) }
99    }
100
101    /// Gets the contents of `self` as an array of targets.
102    /// This can be used to interpret the results of getting
103    /// the standard TARGETS target that is always supplied for
104    /// any selection.
105    ///
106    /// # Returns
107    ///
108    /// [`true`] if `self` contains a valid
109    ///  array of targets, otherwise [`false`].
110    ///
111    /// ## `targets`
112    ///
113    ///  location to store an array of targets. The result stored
114    ///  here must be freed with `g_free()`.
115    #[doc(alias = "gtk_selection_data_get_targets")]
116    #[doc(alias = "get_targets")]
117    pub fn targets(&self) -> Option<Vec<gdk::Atom>> {
118        unsafe {
119            let mut targets = std::ptr::null_mut();
120            let mut n_atoms = std::mem::MaybeUninit::uninit();
121            let ret = from_glib(ffi::gtk_selection_data_get_targets(
122                self.to_glib_none().0,
123                &mut targets,
124                n_atoms.as_mut_ptr(),
125            ));
126            if ret {
127                Some(FromGlibContainer::from_glib_container_num(
128                    targets,
129                    n_atoms.assume_init() as _,
130                ))
131            } else {
132                None
133            }
134        }
135    }
136
137    /// Gets the contents of the selection data as a UTF-8 string.
138    ///
139    /// # Returns
140    ///
141    /// if the selection data contained a
142    ///  recognized text type and it could be converted to UTF-8, a newly
143    ///  allocated string containing the converted text, otherwise [`None`].
144    ///  If the result is non-[`None`] it must be freed with `g_free()`.
145    #[doc(alias = "gtk_selection_data_get_text")]
146    #[doc(alias = "get_text")]
147    pub fn text(&self) -> Option<glib::GString> {
148        unsafe { from_glib_full(ffi::gtk_selection_data_get_text(self.to_glib_none().0)) }
149    }
150
151    /// Gets the contents of the selection data as array of URIs.
152    ///
153    /// Since 3.24.37, this may involve using the FileTransfer
154    /// portal to send files between sandboxed apps.
155    ///
156    /// # Returns
157    ///
158    /// if
159    ///  the selection data contains a list of
160    ///  URIs, a newly allocated [`None`]-terminated string array
161    ///  containing the URIs, otherwise [`None`]. If the result is
162    ///  non-[`None`] it must be freed with `g_strfreev()`.
163    #[doc(alias = "gtk_selection_data_get_uris")]
164    #[doc(alias = "get_uris")]
165    pub fn uris(&self) -> Vec<glib::GString> {
166        unsafe {
167            FromGlibPtrContainer::from_glib_full(ffi::gtk_selection_data_get_uris(
168                self.to_glib_none().0,
169            ))
170        }
171    }
172
173    /// Stores new data into a [`SelectionData`][crate::SelectionData] object. Should
174    /// only be called from a selection handler callback.
175    /// Zero-terminates the stored data.
176    /// ## `type_`
177    /// the type of selection data
178    /// ## `format`
179    /// format (number of bits in a unit)
180    /// ## `data`
181    /// pointer to the data (will be copied)
182    #[doc(alias = "gtk_selection_data_set")]
183    pub fn set(&self, type_: &gdk::Atom, format: i32, data: &[u8]) {
184        let length = data.len() as _;
185        unsafe {
186            ffi::gtk_selection_data_set(
187                mut_override(self.to_glib_none().0),
188                type_.to_glib_none().0,
189                format,
190                data.to_glib_none().0,
191                length,
192            );
193        }
194    }
195
196    /// Sets the contents of the selection from a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
197    /// The pixbuf is converted to the form determined by
198    /// `self`->target.
199    /// ## `pixbuf`
200    /// a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
201    ///
202    /// # Returns
203    ///
204    /// [`true`] if the selection was successfully set,
205    ///  otherwise [`false`].
206    #[doc(alias = "gtk_selection_data_set_pixbuf")]
207    pub fn set_pixbuf(&self, pixbuf: &gdk_pixbuf::Pixbuf) -> bool {
208        unsafe {
209            from_glib(ffi::gtk_selection_data_set_pixbuf(
210                mut_override(self.to_glib_none().0),
211                pixbuf.to_glib_none().0,
212            ))
213        }
214    }
215
216    /// Sets the contents of the selection from a UTF-8 encoded string.
217    /// The string is converted to the form determined by
218    /// `self`->target.
219    /// ## `str`
220    /// a UTF-8 string
221    /// ## `len`
222    /// the length of `str`, or -1 if `str` is nul-terminated.
223    ///
224    /// # Returns
225    ///
226    /// [`true`] if the selection was successfully set,
227    ///  otherwise [`false`].
228    #[doc(alias = "gtk_selection_data_set_text")]
229    pub fn set_text(&self, str: &str) -> bool {
230        let len = str.len() as _;
231        unsafe {
232            from_glib(ffi::gtk_selection_data_set_text(
233                mut_override(self.to_glib_none().0),
234                str.to_glib_none().0,
235                len,
236            ))
237        }
238    }
239
240    /// Sets the contents of the selection from a list of URIs.
241    /// The string is converted to the form determined by
242    /// `self`->target.
243    ///
244    /// Since 3.24.37, this may involve using the FileTransfer
245    /// portal to send files between sandboxed apps.
246    /// ## `uris`
247    /// a [`None`]-terminated array of
248    ///  strings holding URIs
249    ///
250    /// # Returns
251    ///
252    /// [`true`] if the selection was successfully set,
253    ///  otherwise [`false`].
254    #[doc(alias = "gtk_selection_data_set_uris")]
255    pub fn set_uris(&self, uris: &[&str]) -> bool {
256        unsafe {
257            from_glib(ffi::gtk_selection_data_set_uris(
258                mut_override(self.to_glib_none().0),
259                uris.to_glib_none().0,
260            ))
261        }
262    }
263
264    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
265    /// determines if any of the targets in `targets` can be used to
266    /// provide a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
267    /// ## `writable`
268    /// whether to accept only targets for which GTK+ knows
269    ///  how to convert a pixbuf into the format
270    ///
271    /// # Returns
272    ///
273    /// [`true`] if `self` holds a list of targets,
274    ///  and a suitable target for images is included, otherwise [`false`].
275    #[doc(alias = "gtk_selection_data_targets_include_image")]
276    pub fn targets_include_image(&self, writable: bool) -> bool {
277        unsafe {
278            from_glib(ffi::gtk_selection_data_targets_include_image(
279                self.to_glib_none().0,
280                writable.into_glib(),
281            ))
282        }
283    }
284
285    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
286    /// determines if any of the targets in `targets` can be used to
287    /// provide rich text.
288    /// ## `buffer`
289    /// a [`TextBuffer`][crate::TextBuffer]
290    ///
291    /// # Returns
292    ///
293    /// [`true`] if `self` holds a list of targets,
294    ///  and a suitable target for rich text is included,
295    ///  otherwise [`false`].
296    #[doc(alias = "gtk_selection_data_targets_include_rich_text")]
297    pub fn targets_include_rich_text(&self, buffer: &impl IsA<TextBuffer>) -> bool {
298        unsafe {
299            from_glib(ffi::gtk_selection_data_targets_include_rich_text(
300                self.to_glib_none().0,
301                buffer.as_ref().to_glib_none().0,
302            ))
303        }
304    }
305
306    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
307    /// determines if any of the targets in `targets` can be used to
308    /// provide text.
309    ///
310    /// # Returns
311    ///
312    /// [`true`] if `self` holds a list of targets,
313    ///  and a suitable target for text is included, otherwise [`false`].
314    #[doc(alias = "gtk_selection_data_targets_include_text")]
315    pub fn targets_include_text(&self) -> bool {
316        unsafe {
317            from_glib(ffi::gtk_selection_data_targets_include_text(
318                self.to_glib_none().0,
319            ))
320        }
321    }
322
323    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
324    /// determines if any of the targets in `targets` can be used to
325    /// provide a list or URIs.
326    ///
327    /// # Returns
328    ///
329    /// [`true`] if `self` holds a list of targets,
330    ///  and a suitable target for URI lists is included, otherwise [`false`].
331    #[doc(alias = "gtk_selection_data_targets_include_uri")]
332    pub fn targets_include_uri(&self) -> bool {
333        unsafe {
334            from_glib(ffi::gtk_selection_data_targets_include_uri(
335                self.to_glib_none().0,
336            ))
337        }
338    }
339}