gtk/
clipboard.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::Clipboard;
4use crate::SelectionData;
5use crate::TargetEntry;
6use glib::ffi::gpointer;
7use glib::translate::*;
8use libc::{c_char, c_uint};
9use std::boxed::Box as Box_;
10
11impl Clipboard {
12    #[doc(alias = "gtk_clipboard_set_with_data")]
13    pub fn set_with_data<F: Fn(&Clipboard, &SelectionData, u32) + 'static>(
14        &self,
15        targets: &[TargetEntry],
16        f: F,
17    ) -> bool {
18        unsafe extern "C" fn trampoline<F: Fn(&Clipboard, &SelectionData, u32) + 'static>(
19            clipboard: *mut ffi::GtkClipboard,
20            selection_data: *mut ffi::GtkSelectionData,
21            info: c_uint,
22            user_data: gpointer,
23        ) {
24            let f: &F = &*(user_data as *const F);
25            f(
26                &from_glib_borrow(clipboard),
27                &from_glib_borrow(selection_data),
28                info,
29            );
30        }
31        unsafe extern "C" fn cleanup<F: Fn(&Clipboard, &SelectionData, u32) + 'static>(
32            _clipboard: *mut ffi::GtkClipboard,
33            user_data: gpointer,
34        ) {
35            let _ = Box_::<F>::from_raw(user_data as *mut _);
36        }
37        let stashed_targets: Vec<_> = targets.iter().map(|e| e.to_glib_none()).collect();
38        let mut t = Vec::with_capacity(stashed_targets.len());
39        for stash in &stashed_targets {
40            unsafe {
41                t.push(ffi::GtkTargetEntry {
42                    target: (*stash.0).target,
43                    flags: (*stash.0).flags,
44                    info: (*stash.0).info,
45                });
46            }
47        }
48        let t_ptr: *mut ffi::GtkTargetEntry = t.as_mut_ptr();
49        let f: Box_<F> = Box_::new(f);
50        let user_data = Box_::into_raw(f) as *mut _;
51        let success: bool = unsafe {
52            from_glib(ffi::gtk_clipboard_set_with_data(
53                self.to_glib_none().0,
54                t_ptr,
55                t.len() as c_uint,
56                Some(trampoline::<F>),
57                Some(cleanup::<F>),
58                user_data,
59            ))
60        };
61        if !success {
62            // Cleanup function is not called in case of a failure.
63            unsafe {
64                let _ = Box_::<F>::from_raw(user_data as *mut _);
65            }
66        }
67        success
68    }
69
70    #[doc(alias = "gtk_clipboard_request_uris")]
71    pub fn request_uris<P: FnOnce(&Clipboard, &[glib::GString]) + 'static>(&self, callback: P) {
72        let callback_data: Box_<P> = Box_::new(callback);
73        unsafe extern "C" fn callback_func<P: FnOnce(&Clipboard, &[glib::GString]) + 'static>(
74            clipboard: *mut ffi::GtkClipboard,
75            uris: *mut *mut c_char,
76            data: glib::ffi::gpointer,
77        ) {
78            let clipboard = from_glib_borrow(clipboard);
79            let uris: Vec<glib::GString> = FromGlibPtrContainer::from_glib_none(uris);
80            let callback: Box_<P> = Box_::from_raw(data as *mut _);
81            (*callback)(&clipboard, &uris);
82        }
83        let callback = Some(callback_func::<P> as _);
84        let super_callback0: Box_<P> = callback_data;
85        unsafe {
86            ffi::gtk_clipboard_request_uris(
87                self.to_glib_none().0,
88                callback,
89                Box_::into_raw(super_callback0) as *mut _,
90            );
91        }
92    }
93}