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
#![cfg(windows)]
extern crate winapi;
extern crate user32;
extern crate kernel32;
extern crate windows_error;
use windows_error::WindowsError;
mod constants;
pub mod clipboard_formats;
pub mod wrapper;
macro_rules! try_none {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(_) => return None,
})
}
macro_rules! check_format {
($format:expr) => {
if !wrapper::is_format_avail($format) {
return Err(WindowsError::new(0))
}
}
}
#[inline]
pub fn set_clipboard<T: ?Sized + AsRef<std::ffi::OsStr>>(text: &T) -> Result<(), WindowsError> {
try!(wrapper::open_clipboard());
let result = wrapper::set_clipboard(text);
try!(wrapper::close_clipboard());
result
}
#[inline]
pub fn get_clipboard_string() -> Result<String, WindowsError> {
check_format!(clipboard_formats::CF_UNICODETEXT);
try!(wrapper::open_clipboard());
let result = wrapper::get_clipboard_string();
try!(wrapper::close_clipboard());
result
}
#[inline]
pub fn get_clipboard(format: u32) -> Result<Vec<u8>, WindowsError> {
check_format!(format);
try!(wrapper::open_clipboard());
let result = wrapper::get_clipboard(format);
try!(wrapper::close_clipboard());
result
}
#[inline]
pub fn get_clipboard_formats() -> Result<Vec<u32>, WindowsError> {
try!(wrapper::open_clipboard());
let result = wrapper::get_clipboard_formats();
try!(wrapper::close_clipboard());
result
}
pub use wrapper::{get_format_name,
count_formats,
register_format,
is_format_avail};