1use gdk;
6use gdk_pixbuf;
7use glib;
8use glib::object::IsA;
9use glib::translate::*;
10use glib::GString;
11use gtk_sys;
12use std::boxed::Box as Box_;
13use std::fmt;
14use std::mem;
15use std::ptr;
16use SelectionData;
17use TextBuffer;
18
19glib_wrapper! {
20 pub struct Clipboard(Object<gtk_sys::GtkClipboard, ClipboardClass>);
21
22 match fn {
23 get_type => || gtk_sys::gtk_clipboard_get_type(),
24 }
25}
26
27impl Clipboard {
28 pub fn clear(&self) {
29 unsafe {
30 gtk_sys::gtk_clipboard_clear(self.to_glib_none().0);
31 }
32 }
33
34 pub fn get_display(&self) -> Option<gdk::Display> {
35 unsafe { from_glib_none(gtk_sys::gtk_clipboard_get_display(self.to_glib_none().0)) }
36 }
37
38 pub fn get_owner(&self) -> Option<glib::Object> {
39 unsafe { from_glib_none(gtk_sys::gtk_clipboard_get_owner(self.to_glib_none().0)) }
40 }
41
42 #[cfg(any(feature = "v3_22", feature = "dox"))]
43 pub fn get_selection(&self) -> Option<gdk::Atom> {
44 unsafe { from_glib_none(gtk_sys::gtk_clipboard_get_selection(self.to_glib_none().0)) }
45 }
46
47 pub fn request_contents<P: FnOnce(&Clipboard, &SelectionData) + 'static>(
48 &self,
49 target: &gdk::Atom,
50 callback: P,
51 ) {
52 let callback_data: Box_<P> = Box::new(callback);
53 unsafe extern "C" fn callback_func<P: FnOnce(&Clipboard, &SelectionData) + 'static>(
54 clipboard: *mut gtk_sys::GtkClipboard,
55 selection_data: *mut gtk_sys::GtkSelectionData,
56 data: glib_sys::gpointer,
57 ) {
58 let clipboard = from_glib_borrow(clipboard);
59 let selection_data = from_glib_borrow(selection_data);
60 let callback: Box_<P> = Box_::from_raw(data as *mut _);
61 (*callback)(&clipboard, &selection_data);
62 }
63 let callback = Some(callback_func::<P> as _);
64 let super_callback0: Box_<P> = callback_data;
65 unsafe {
66 gtk_sys::gtk_clipboard_request_contents(
67 self.to_glib_none().0,
68 target.to_glib_none().0,
69 callback,
70 Box::into_raw(super_callback0) as *mut _,
71 );
72 }
73 }
74
75 pub fn request_image<P: FnOnce(&Clipboard, &gdk_pixbuf::Pixbuf) + 'static>(&self, callback: P) {
76 let callback_data: Box_<P> = Box::new(callback);
77 unsafe extern "C" fn callback_func<P: FnOnce(&Clipboard, &gdk_pixbuf::Pixbuf) + 'static>(
78 clipboard: *mut gtk_sys::GtkClipboard,
79 pixbuf: *mut gdk_pixbuf_sys::GdkPixbuf,
80 data: glib_sys::gpointer,
81 ) {
82 let clipboard = from_glib_borrow(clipboard);
83 let pixbuf = from_glib_borrow(pixbuf);
84 let callback: Box_<P> = Box_::from_raw(data as *mut _);
85 (*callback)(&clipboard, &pixbuf);
86 }
87 let callback = Some(callback_func::<P> as _);
88 let super_callback0: Box_<P> = callback_data;
89 unsafe {
90 gtk_sys::gtk_clipboard_request_image(
91 self.to_glib_none().0,
92 callback,
93 Box::into_raw(super_callback0) as *mut _,
94 );
95 }
96 }
97
98 pub fn request_rich_text<
99 P: IsA<TextBuffer>,
100 Q: FnOnce(&Clipboard, &gdk::Atom, Option<&str>, usize) + 'static,
101 >(
102 &self,
103 buffer: &P,
104 callback: Q,
105 ) {
106 let callback_data: Box_<Q> = Box::new(callback);
107 unsafe extern "C" fn callback_func<
108 P: IsA<TextBuffer>,
109 Q: FnOnce(&Clipboard, &gdk::Atom, Option<&str>, usize) + 'static,
110 >(
111 clipboard: *mut gtk_sys::GtkClipboard,
112 format: gdk_sys::GdkAtom,
113 text: *const libc::c_char,
114 length: libc::size_t,
115 data: glib_sys::gpointer,
116 ) {
117 let clipboard = from_glib_borrow(clipboard);
118 let format = from_glib_borrow(format);
119 let text: Option<GString> = from_glib_borrow(text);
120 let text = text.as_ref();
121 let callback: Box_<Q> = Box_::from_raw(data as *mut _);
122 (*callback)(&clipboard, &format, text.map(|x| x.as_str()), length);
123 }
124 let callback = Some(callback_func::<P, Q> as _);
125 let super_callback0: Box_<Q> = callback_data;
126 unsafe {
127 gtk_sys::gtk_clipboard_request_rich_text(
128 self.to_glib_none().0,
129 buffer.as_ref().to_glib_none().0,
130 callback,
131 Box::into_raw(super_callback0) as *mut _,
132 );
133 }
134 }
135
136 pub fn request_text<P: FnOnce(&Clipboard, Option<&str>) + 'static>(&self, callback: P) {
137 let callback_data: Box_<P> = Box::new(callback);
138 unsafe extern "C" fn callback_func<P: FnOnce(&Clipboard, Option<&str>) + 'static>(
139 clipboard: *mut gtk_sys::GtkClipboard,
140 text: *const libc::c_char,
141 data: glib_sys::gpointer,
142 ) {
143 let clipboard = from_glib_borrow(clipboard);
144 let text: Option<GString> = from_glib_borrow(text);
145 let text = text.as_ref();
146 let callback: Box_<P> = Box_::from_raw(data as *mut _);
147 (*callback)(&clipboard, text.map(|x| x.as_str()));
148 }
149 let callback = Some(callback_func::<P> as _);
150 let super_callback0: Box_<P> = callback_data;
151 unsafe {
152 gtk_sys::gtk_clipboard_request_text(
153 self.to_glib_none().0,
154 callback,
155 Box::into_raw(super_callback0) as *mut _,
156 );
157 }
158 }
159
160 pub fn set_image(&self, pixbuf: &gdk_pixbuf::Pixbuf) {
161 unsafe {
162 gtk_sys::gtk_clipboard_set_image(self.to_glib_none().0, pixbuf.to_glib_none().0);
163 }
164 }
165
166 pub fn set_text(&self, text: &str) {
167 let len = text.len() as i32;
168 unsafe {
169 gtk_sys::gtk_clipboard_set_text(self.to_glib_none().0, text.to_glib_none().0, len);
170 }
171 }
172
173 pub fn store(&self) {
174 unsafe {
175 gtk_sys::gtk_clipboard_store(self.to_glib_none().0);
176 }
177 }
178
179 pub fn wait_for_contents(&self, target: &gdk::Atom) -> Option<SelectionData> {
180 unsafe {
181 from_glib_full(gtk_sys::gtk_clipboard_wait_for_contents(
182 self.to_glib_none().0,
183 target.to_glib_none().0,
184 ))
185 }
186 }
187
188 pub fn wait_for_image(&self) -> Option<gdk_pixbuf::Pixbuf> {
189 unsafe { from_glib_full(gtk_sys::gtk_clipboard_wait_for_image(self.to_glib_none().0)) }
190 }
191
192 pub fn wait_for_rich_text<P: IsA<TextBuffer>>(&self, buffer: &P) -> (Vec<u8>, gdk::Atom) {
193 unsafe {
194 let mut format = gdk::Atom::uninitialized();
195 let mut length = mem::uninitialized();
196 let ret = FromGlibContainer::from_glib_full_num(
197 gtk_sys::gtk_clipboard_wait_for_rich_text(
198 self.to_glib_none().0,
199 buffer.as_ref().to_glib_none().0,
200 format.to_glib_none_mut().0,
201 &mut length,
202 ),
203 length as usize,
204 );
205 (ret, format)
206 }
207 }
208
209 pub fn wait_for_targets(&self) -> Option<Vec<gdk::Atom>> {
210 unsafe {
211 let mut targets = ptr::null_mut();
212 let mut n_targets = mem::uninitialized();
213 let ret = from_glib(gtk_sys::gtk_clipboard_wait_for_targets(
214 self.to_glib_none().0,
215 &mut targets,
216 &mut n_targets,
217 ));
218 if ret {
219 Some(FromGlibContainer::from_glib_container_num(
220 targets,
221 n_targets as usize,
222 ))
223 } else {
224 None
225 }
226 }
227 }
228
229 pub fn wait_for_text(&self) -> Option<GString> {
230 unsafe { from_glib_full(gtk_sys::gtk_clipboard_wait_for_text(self.to_glib_none().0)) }
231 }
232
233 pub fn wait_for_uris(&self) -> Vec<GString> {
234 unsafe {
235 FromGlibPtrContainer::from_glib_full(gtk_sys::gtk_clipboard_wait_for_uris(
236 self.to_glib_none().0,
237 ))
238 }
239 }
240
241 pub fn wait_is_image_available(&self) -> bool {
242 unsafe {
243 from_glib(gtk_sys::gtk_clipboard_wait_is_image_available(
244 self.to_glib_none().0,
245 ))
246 }
247 }
248
249 pub fn wait_is_rich_text_available<P: IsA<TextBuffer>>(&self, buffer: &P) -> bool {
250 unsafe {
251 from_glib(gtk_sys::gtk_clipboard_wait_is_rich_text_available(
252 self.to_glib_none().0,
253 buffer.as_ref().to_glib_none().0,
254 ))
255 }
256 }
257
258 pub fn wait_is_target_available(&self, target: &gdk::Atom) -> bool {
259 unsafe {
260 from_glib(gtk_sys::gtk_clipboard_wait_is_target_available(
261 self.to_glib_none().0,
262 target.to_glib_none().0,
263 ))
264 }
265 }
266
267 pub fn wait_is_text_available(&self) -> bool {
268 unsafe {
269 from_glib(gtk_sys::gtk_clipboard_wait_is_text_available(
270 self.to_glib_none().0,
271 ))
272 }
273 }
274
275 pub fn wait_is_uris_available(&self) -> bool {
276 unsafe {
277 from_glib(gtk_sys::gtk_clipboard_wait_is_uris_available(
278 self.to_glib_none().0,
279 ))
280 }
281 }
282
283 pub fn get(selection: &gdk::Atom) -> Clipboard {
284 assert_initialized_main_thread!();
285 unsafe { from_glib_none(gtk_sys::gtk_clipboard_get(selection.to_glib_none().0)) }
286 }
287
288 #[cfg(any(feature = "v3_16", feature = "dox"))]
289 pub fn get_default(display: &gdk::Display) -> Option<Clipboard> {
290 assert_initialized_main_thread!();
291 unsafe { from_glib_none(gtk_sys::gtk_clipboard_get_default(display.to_glib_none().0)) }
292 }
293
294 pub fn get_for_display(display: &gdk::Display, selection: &gdk::Atom) -> Clipboard {
295 assert_initialized_main_thread!();
296 unsafe {
297 from_glib_none(gtk_sys::gtk_clipboard_get_for_display(
298 display.to_glib_none().0,
299 selection.to_glib_none().0,
300 ))
301 }
302 }
303
304 }
308
309impl fmt::Display for Clipboard {
310 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
311 write!(f, "Clipboard")
312 }
313}