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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::ContentFormats;
use crate::ContentProvider;
use crate::Display;
use crate::Texture;
use glib::object::Cast;
use glib::object::IsA;
use glib::object::ObjectType as ObjectType_;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::StaticType;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// The [`Clipboard`][crate::Clipboard] object represents data shared between applications or
    /// inside an application.
    ///
    /// To get a [`Clipboard`][crate::Clipboard] object, use [``DisplayExt::clipboard()``][crate::prelude::`DisplayExt::clipboard()`] or
    /// [``DisplayExt::primary_clipboard()``][crate::prelude::`DisplayExt::primary_clipboard()`]. You can find out about the data
    /// that is currently available in a clipboard using
    /// [``formats()``][`Self::formats()`].
    ///
    /// To make text or image data available in a clipboard, use
    /// [``set_text()``][`Self::set_text()`] or [``set_texture()``][`Self::set_texture()`].
    /// For other data, you can use [``set_content()``][`Self::set_content()`], which
    /// takes a [`ContentProvider`][crate::ContentProvider] object.
    ///
    /// To read textual or image data from a clipboard, use
    /// [``read_text_async()``][`Self::read_text_async()`] or
    /// [``read_texture_async()``][`Self::read_texture_async()`]. For other data, use
    /// [``read_async()``][`Self::read_async()`], which provides a `GInputStream` object.
    #[doc(alias = "GdkClipboard")]
    pub struct Clipboard(Object<ffi::GdkClipboard>);

    match fn {
        type_ => || ffi::gdk_clipboard_get_type(),
    }
}

impl Clipboard {
    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`Clipboard`] objects.
    ///
    /// This method returns an instance of [`ClipboardBuilder`] which can be used to create [`Clipboard`] objects.
    pub fn builder() -> ClipboardBuilder {
        ClipboardBuilder::default()
    }

    /// Returns the [`ContentProvider`][crate::ContentProvider] currently set on `self`.
    ///
    /// If the `self` is empty or its contents are not owned by the
    /// current process, [`None`] will be returned.
    ///
    /// # Returns
    ///
    /// The content of a clipboard
    ///  if the clipboard does not maintain any content
    #[doc(alias = "gdk_clipboard_get_content")]
    #[doc(alias = "get_content")]
    pub fn content(&self) -> Option<ContentProvider> {
        unsafe { from_glib_none(ffi::gdk_clipboard_get_content(self.to_glib_none().0)) }
    }

    /// Gets the [`Display`][crate::Display] that the clipboard was created for.
    ///
    /// # Returns
    ///
    /// a [`Display`][crate::Display]
    #[doc(alias = "gdk_clipboard_get_display")]
    #[doc(alias = "get_display")]
    pub fn display(&self) -> Option<Display> {
        unsafe { from_glib_none(ffi::gdk_clipboard_get_display(self.to_glib_none().0)) }
    }

    /// Gets the formats that the clipboard can provide its current contents in.
    ///
    /// # Returns
    ///
    /// The formats of the clipboard
    #[doc(alias = "gdk_clipboard_get_formats")]
    #[doc(alias = "get_formats")]
    pub fn formats(&self) -> Option<ContentFormats> {
        unsafe { from_glib_none(ffi::gdk_clipboard_get_formats(self.to_glib_none().0)) }
    }

    /// Returns if the clipboard is local.
    ///
    /// A clipboard is considered local if it was last claimed
    /// by the running application.
    ///
    /// Note that [``content()``][`Self::content()`] may return [`None`]
    /// even on a local clipboard. In this case the clipboard is empty.
    ///
    /// # Returns
    ///
    /// [`true`] if the clipboard is local
    #[doc(alias = "gdk_clipboard_is_local")]
    pub fn is_local(&self) -> bool {
        unsafe { from_glib(ffi::gdk_clipboard_is_local(self.to_glib_none().0)) }
    }

    /// Sets a new content provider on `self`.
    ///
    /// The clipboard will claim the [`Display`][crate::Display]'s resources and advertise
    /// these new contents to other applications.
    ///
    /// In the rare case of a failure, this function will return [`false`]. The
    /// clipboard will then continue reporting its old contents and ignore
    /// `provider`.
    ///
    /// If the contents are read by either an external application or the
    /// `self`'s read functions, `self` will select the best format to
    /// transfer the contents and then request that format from `provider`.
    /// ## `provider`
    /// the new contents of `self`
    ///  or [`None`] to clear the clipboard
    ///
    /// # Returns
    ///
    /// [`true`] if setting the clipboard succeeded
    #[doc(alias = "gdk_clipboard_set_content")]
    pub fn set_content<P: IsA<ContentProvider>>(
        &self,
        provider: Option<&P>,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gdk_clipboard_set_content(
                    self.to_glib_none().0,
                    provider.map(|p| p.as_ref()).to_glib_none().0
                ),
                "Can't set new clipboard content"
            )
        }
    }

    /// Puts the given `text` into the clipboard.
    /// ## `text`
    /// Text to put into the clipboard
    #[doc(alias = "gdk_clipboard_set_text")]
    pub fn set_text(&self, text: &str) {
        unsafe {
            ffi::gdk_clipboard_set_text(self.to_glib_none().0, text.to_glib_none().0);
        }
    }

    /// Puts the given `texture` into the clipboard.
    /// ## `texture`
    /// a [`Texture`][crate::Texture] to put into the clipboard
    #[doc(alias = "gdk_clipboard_set_texture")]
    pub fn set_texture<P: IsA<Texture>>(&self, texture: &P) {
        unsafe {
            ffi::gdk_clipboard_set_texture(
                self.to_glib_none().0,
                texture.as_ref().to_glib_none().0,
            );
        }
    }

    /// Emitted when the clipboard changes ownership.
    #[doc(alias = "changed")]
    pub fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn changed_trampoline<F: Fn(&Clipboard) + 'static>(
            this: *mut ffi::GdkClipboard,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    changed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "content")]
    pub fn connect_content_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_content_trampoline<F: Fn(&Clipboard) + 'static>(
            this: *mut ffi::GdkClipboard,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::content\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_content_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "formats")]
    pub fn connect_formats_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_formats_trampoline<F: Fn(&Clipboard) + 'static>(
            this: *mut ffi::GdkClipboard,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::formats\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_formats_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "local")]
    pub fn connect_local_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_local_trampoline<F: Fn(&Clipboard) + 'static>(
            this: *mut ffi::GdkClipboard,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::local\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_local_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

#[derive(Clone, Default)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`Clipboard`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
pub struct ClipboardBuilder {
    display: Option<Display>,
}

impl ClipboardBuilder {
    // rustdoc-stripper-ignore-next
    /// Create a new [`ClipboardBuilder`].
    pub fn new() -> Self {
        Self::default()
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`Clipboard`].
    pub fn build(self) -> Clipboard {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref display) = self.display {
            properties.push(("display", display));
        }
        glib::Object::new::<Clipboard>(&properties)
            .expect("Failed to create an instance of Clipboard")
    }

    /// The [`Display`][crate::Display] that the clipboard belongs to.
    pub fn display<P: IsA<Display>>(mut self, display: &P) -> Self {
        self.display = Some(display.clone().upcast());
        self
    }
}

impl fmt::Display for Clipboard {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Clipboard")
    }
}