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