1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::TextBuffer;
use glib::object::IsA;
use glib::translate::*;
use std::mem;
use std::ptr;

glib::wrapper! {
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct SelectionData(Boxed<ffi::GtkSelectionData>);

    match fn {
        copy => |ptr| ffi::gtk_selection_data_copy(ptr),
        free => |ptr| ffi::gtk_selection_data_free(ptr),
        type_ => || ffi::gtk_selection_data_get_type(),
    }
}

impl SelectionData {
    /// Retrieves the data type of the selection.
    ///
    /// # Returns
    ///
    /// the data type of the selection.
    #[doc(alias = "gtk_selection_data_get_data_type")]
    #[doc(alias = "get_data_type")]
    pub fn data_type(&self) -> gdk::Atom {
        unsafe { from_glib_none(ffi::gtk_selection_data_get_data_type(self.to_glib_none().0)) }
    }

    /// Retrieves the display of the selection.
    ///
    /// # Returns
    ///
    /// the display of the selection.
    #[doc(alias = "gtk_selection_data_get_display")]
    #[doc(alias = "get_display")]
    pub fn display(&self) -> Option<gdk::Display> {
        unsafe { from_glib_none(ffi::gtk_selection_data_get_display(self.to_glib_none().0)) }
    }

    /// Retrieves the format of the selection.
    ///
    /// # Returns
    ///
    /// the format of the selection.
    #[doc(alias = "gtk_selection_data_get_format")]
    #[doc(alias = "get_format")]
    pub fn format(&self) -> i32 {
        unsafe { ffi::gtk_selection_data_get_format(self.to_glib_none().0) }
    }

    /// Retrieves the length of the raw data of the selection.
    ///
    /// # Returns
    ///
    /// the length of the data of the selection.
    #[doc(alias = "gtk_selection_data_get_length")]
    #[doc(alias = "get_length")]
    pub fn length(&self) -> i32 {
        unsafe { ffi::gtk_selection_data_get_length(self.to_glib_none().0) }
    }

    /// Gets the contents of the selection data as a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
    ///
    /// # Returns
    ///
    /// if the selection data
    ///  contained a recognized image type and it could be converted to a
    ///  [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf], a newly allocated pixbuf is returned, otherwise
    ///  [`None`]. If the result is non-[`None`] it must be freed with
    ///  `g_object_unref()`.
    #[doc(alias = "gtk_selection_data_get_pixbuf")]
    #[doc(alias = "get_pixbuf")]
    pub fn pixbuf(&self) -> Option<gdk_pixbuf::Pixbuf> {
        unsafe { from_glib_full(ffi::gtk_selection_data_get_pixbuf(self.to_glib_none().0)) }
    }

    /// Retrieves the selection [`gdk::Atom`][crate::gdk::Atom] of the selection data.
    ///
    /// # Returns
    ///
    /// the selection [`gdk::Atom`][crate::gdk::Atom] of the selection data.
    #[doc(alias = "gtk_selection_data_get_selection")]
    #[doc(alias = "get_selection")]
    pub fn selection(&self) -> gdk::Atom {
        unsafe { from_glib_none(ffi::gtk_selection_data_get_selection(self.to_glib_none().0)) }
    }

    /// Retrieves the target of the selection.
    ///
    /// # Returns
    ///
    /// the target of the selection.
    #[doc(alias = "gtk_selection_data_get_target")]
    #[doc(alias = "get_target")]
    pub fn target(&self) -> gdk::Atom {
        unsafe { from_glib_none(ffi::gtk_selection_data_get_target(self.to_glib_none().0)) }
    }

    /// Gets the contents of `self` as an array of targets.
    /// This can be used to interpret the results of getting
    /// the standard TARGETS target that is always supplied for
    /// any selection.
    ///
    /// # Returns
    ///
    /// [`true`] if `self` contains a valid
    ///  array of targets, otherwise [`false`].
    ///
    /// ## `targets`
    ///
    ///  location to store an array of targets. The result stored
    ///  here must be freed with `g_free()`.
    #[doc(alias = "gtk_selection_data_get_targets")]
    #[doc(alias = "get_targets")]
    pub fn targets(&self) -> Option<Vec<gdk::Atom>> {
        unsafe {
            let mut targets = ptr::null_mut();
            let mut n_atoms = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gtk_selection_data_get_targets(
                self.to_glib_none().0,
                &mut targets,
                n_atoms.as_mut_ptr(),
            ));
            if ret {
                Some(FromGlibContainer::from_glib_container_num(
                    targets,
                    n_atoms.assume_init() as usize,
                ))
            } else {
                None
            }
        }
    }

    /// Gets the contents of the selection data as a UTF-8 string.
    ///
    /// # Returns
    ///
    /// if the selection data contained a
    ///  recognized text type and it could be converted to UTF-8, a newly
    ///  allocated string containing the converted text, otherwise [`None`].
    ///  If the result is non-[`None`] it must be freed with `g_free()`.
    #[doc(alias = "gtk_selection_data_get_text")]
    #[doc(alias = "get_text")]
    pub fn text(&self) -> Option<glib::GString> {
        unsafe { from_glib_full(ffi::gtk_selection_data_get_text(self.to_glib_none().0)) }
    }

    /// Gets the contents of the selection data as array of URIs.
    ///
    /// # Returns
    ///
    /// if
    ///  the selection data contains a list of
    ///  URIs, a newly allocated [`None`]-terminated string array
    ///  containing the URIs, otherwise [`None`]. If the result is
    ///  non-[`None`] it must be freed with `g_strfreev()`.
    #[doc(alias = "gtk_selection_data_get_uris")]
    #[doc(alias = "get_uris")]
    pub fn uris(&self) -> Vec<glib::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gtk_selection_data_get_uris(
                self.to_glib_none().0,
            ))
        }
    }

    /// Stores new data into a [`SelectionData`][crate::SelectionData] object. Should
    /// only be called from a selection handler callback.
    /// Zero-terminates the stored data.
    /// ## `type_`
    /// the type of selection data
    /// ## `format`
    /// format (number of bits in a unit)
    /// ## `data`
    /// pointer to the data (will be copied)
    #[doc(alias = "gtk_selection_data_set")]
    pub fn set(&self, type_: &gdk::Atom, format: i32, data: &[u8]) {
        let length = data.len() as i32;
        unsafe {
            ffi::gtk_selection_data_set(
                mut_override(self.to_glib_none().0),
                type_.to_glib_none().0,
                format,
                data.to_glib_none().0,
                length,
            );
        }
    }

    /// Sets the contents of the selection from a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
    /// The pixbuf is converted to the form determined by
    /// `self`->target.
    /// ## `pixbuf`
    /// a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
    ///
    /// # Returns
    ///
    /// [`true`] if the selection was successfully set,
    ///  otherwise [`false`].
    #[doc(alias = "gtk_selection_data_set_pixbuf")]
    pub fn set_pixbuf(&self, pixbuf: &gdk_pixbuf::Pixbuf) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_set_pixbuf(
                mut_override(self.to_glib_none().0),
                pixbuf.to_glib_none().0,
            ))
        }
    }

    /// Sets the contents of the selection from a UTF-8 encoded string.
    /// The string is converted to the form determined by
    /// `self`->target.
    /// ## `str`
    /// a UTF-8 string
    /// ## `len`
    /// the length of `str`, or -1 if `str` is nul-terminated.
    ///
    /// # Returns
    ///
    /// [`true`] if the selection was successfully set,
    ///  otherwise [`false`].
    #[doc(alias = "gtk_selection_data_set_text")]
    pub fn set_text(&self, str: &str) -> bool {
        let len = str.len() as i32;
        unsafe {
            from_glib(ffi::gtk_selection_data_set_text(
                mut_override(self.to_glib_none().0),
                str.to_glib_none().0,
                len,
            ))
        }
    }

    /// Sets the contents of the selection from a list of URIs.
    /// The string is converted to the form determined by
    /// `self`->target.
    /// ## `uris`
    /// a [`None`]-terminated array of
    ///  strings holding URIs
    ///
    /// # Returns
    ///
    /// [`true`] if the selection was successfully set,
    ///  otherwise [`false`].
    #[doc(alias = "gtk_selection_data_set_uris")]
    pub fn set_uris(&self, uris: &[&str]) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_set_uris(
                mut_override(self.to_glib_none().0),
                uris.to_glib_none().0,
            ))
        }
    }

    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
    /// determines if any of the targets in `targets` can be used to
    /// provide a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
    /// ## `writable`
    /// whether to accept only targets for which GTK+ knows
    ///  how to convert a pixbuf into the format
    ///
    /// # Returns
    ///
    /// [`true`] if `self` holds a list of targets,
    ///  and a suitable target for images is included, otherwise [`false`].
    #[doc(alias = "gtk_selection_data_targets_include_image")]
    pub fn targets_include_image(&self, writable: bool) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_targets_include_image(
                self.to_glib_none().0,
                writable.into_glib(),
            ))
        }
    }

    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
    /// determines if any of the targets in `targets` can be used to
    /// provide rich text.
    /// ## `buffer`
    /// a [`TextBuffer`][crate::TextBuffer]
    ///
    /// # Returns
    ///
    /// [`true`] if `self` holds a list of targets,
    ///  and a suitable target for rich text is included,
    ///  otherwise [`false`].
    #[doc(alias = "gtk_selection_data_targets_include_rich_text")]
    pub fn targets_include_rich_text(&self, buffer: &impl IsA<TextBuffer>) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_targets_include_rich_text(
                self.to_glib_none().0,
                buffer.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
    /// determines if any of the targets in `targets` can be used to
    /// provide text.
    ///
    /// # Returns
    ///
    /// [`true`] if `self` holds a list of targets,
    ///  and a suitable target for text is included, otherwise [`false`].
    #[doc(alias = "gtk_selection_data_targets_include_text")]
    pub fn targets_include_text(&self) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_targets_include_text(
                self.to_glib_none().0,
            ))
        }
    }

    /// Given a [`SelectionData`][crate::SelectionData] object holding a list of targets,
    /// determines if any of the targets in `targets` can be used to
    /// provide a list or URIs.
    ///
    /// # Returns
    ///
    /// [`true`] if `self` holds a list of targets,
    ///  and a suitable target for URI lists is included, otherwise [`false`].
    #[doc(alias = "gtk_selection_data_targets_include_uri")]
    pub fn targets_include_uri(&self) -> bool {
        unsafe {
            from_glib(ffi::gtk_selection_data_targets_include_uri(
                self.to_glib_none().0,
            ))
        }
    }
}