clipboard_win/
formats.rs

1#![allow(dead_code)]
2//! Standard clipboard formats.
3//!
4//! Header: Winuser.h
5//!
6//! Description is taken from [Standard Clipboard Formats](https://msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx)
7
8use crate::{SysResult, Getter, Setter};
9use crate::types::c_uint;
10
11use core::num::NonZeroU32;
12
13///Format trait
14pub trait Format {
15    ///Returns whether format is present on clipboard
16    fn is_format_avail(&self) -> bool;
17}
18
19macro_rules! impl_format {
20    ($($format:ident),+) => {
21        $(
22            impl From<$format> for u32 {
23                #[inline(always)]
24                fn from(value: $format) -> Self {
25                    (&value).into()
26                }
27            }
28
29            impl Format for $format {
30                #[inline(always)]
31                fn is_format_avail(&self) -> bool {
32                    crate::raw::is_format_avail(self.into())
33                }
34            }
35        )+
36
37    };
38}
39
40///A handle to a bitmap (HBITMAP).
41pub const CF_BITMAP: c_uint = 2;
42///A memory object containing a <b>BITMAPINFO</b> structure followed by the bitmap bits.
43pub const CF_DIB: c_uint = 8;
44///A memory object containing a <b>BITMAPV5HEADER</b> structure followed by the bitmap color space
45///information and the bitmap bits.
46pub const CF_DIBV5: c_uint = 17;
47///Software Arts' Data Interchange Format.
48pub const CF_DIF: c_uint = 5;
49///Bitmap display format associated with a private format. The hMem parameter must be a handle to
50///data that can be displayed in bitmap format in lieu of the privately formatted data.
51pub const CF_DSPBITMAP: c_uint = 0x0082;
52///Enhanced metafile display format associated with a private format. The *hMem* parameter must be a
53///handle to data that can be displayed in enhanced metafile format in lieu of the privately
54///formatted data.
55pub const CF_DSPENHMETAFILE: c_uint = 0x008E;
56///Metafile-picture display format associated with a private format. The hMem parameter must be a
57///handle to data that can be displayed in metafile-picture format in lieu of the privately
58///formatted data.
59pub const CF_DSPMETAFILEPICT: c_uint = 0x0083;
60///Text display format associated with a private format. The *hMem* parameter must be a handle to
61///data that can be displayed in text format in lieu of the privately formatted data.
62pub const CF_DSPTEXT: c_uint = 0x0081;
63///A handle to an enhanced metafile (<b>HENHMETAFILE</b>).
64pub const CF_ENHMETAFILE: c_uint = 14;
65///Start of a range of integer values for application-defined GDI object clipboard formats.
66pub const CF_GDIOBJFIRST: c_uint = 0x0300;
67///End of a range of integer values for application-defined GDI object clipboard formats.
68pub const CF_GDIOBJLAST: c_uint = 0x03FF;
69///A handle to type <b>HDROP</b> that identifies a list of files.
70pub const CF_HDROP: c_uint = 15;
71///The data is a handle to the locale identifier associated with text in the clipboard.
72///
73///For details see [Standart Clipboard Formats](https://msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx)
74pub const CF_LOCALE: c_uint = 16;
75///Handle to a metafile picture format as defined by the <b>METAFILEPICT</b> structure.
76pub const CF_METAFILEPICT: c_uint = 3;
77///Text format containing characters in the OEM character set.
78pub const CF_OEMTEXT: c_uint = 7;
79///Owner-display format.
80///
81///For details see [Standart Clipboard Formats](https://msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx)
82pub const CF_OWNERDISPLAY: c_uint = 0x0080;
83///Handle to a color palette.
84///
85///For details see [Standart Clipboard Formats](https://msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx)
86pub const CF_PALETTE: c_uint = 9;
87///Data for the pen extensions to the Microsoft Windows for Pen Computing.
88pub const CF_PENDATA: c_uint = 10;
89///Start of a range of integer values for private clipboard formats.
90pub const CF_PRIVATEFIRST: c_uint = 0x0200;
91///End of a range of integer values for private clipboard formats.
92pub const CF_PRIVATELAST: c_uint = 0x02FF;
93///Represents audio data more complex than can be represented in a ```CF_WAVE``` standard wave format.
94pub const CF_RIFF: c_uint = 11;
95///Microsoft Symbolic Link (SYLK) format.
96pub const CF_SYLK: c_uint = 4;
97///ANSI text format.
98pub const CF_TEXT: c_uint = 1;
99///Tagged-image file format.
100pub const CF_TIFF: c_uint = 6;
101///UTF16 text format.
102pub const CF_UNICODETEXT: c_uint = 13;
103///Represents audio data in one of the standard wave formats.
104pub const CF_WAVE: c_uint = 12;
105
106#[derive(Copy, Clone)]
107///Format to write/read from clipboard as raw bytes
108///
109///Has to be initialized with format `id`
110pub struct RawData(pub c_uint);
111
112impl<T: AsRef<[u8]>> Setter<T> for RawData {
113    #[inline(always)]
114    fn write_clipboard(&self, data: &T) -> SysResult<()> {
115        crate::raw::set(self.0, data.as_ref())
116    }
117}
118
119impl Getter<alloc::vec::Vec<u8>> for RawData {
120    #[inline(always)]
121    fn read_clipboard(&self, out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
122        crate::raw::get_vec(self.0, out)
123    }
124}
125
126impl From<&RawData> for u32 {
127    #[inline(always)]
128    fn from(value: &RawData) -> Self {
129        value.0 as _
130    }
131}
132
133#[derive(Copy, Clone)]
134///Format to read/write unicode string.
135///
136///Refer to `Getter` and `Setter`
137pub struct Unicode;
138
139impl Getter<alloc::vec::Vec<u8>> for Unicode {
140    #[inline(always)]
141    fn read_clipboard(&self, out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
142        crate::raw::get_string(out)
143    }
144}
145
146impl Getter<alloc::string::String> for Unicode {
147    #[inline(always)]
148    fn read_clipboard(&self, out: &mut alloc::string::String) -> SysResult<usize> {
149        self.read_clipboard(unsafe { out.as_mut_vec() })
150    }
151}
152
153impl<T: AsRef<str>> Setter<T> for Unicode {
154    #[inline(always)]
155    fn write_clipboard(&self, data: &T) -> SysResult<()> {
156        crate::raw::set_string(data.as_ref())
157    }
158}
159
160impl From<&Unicode> for u32 {
161    #[inline(always)]
162    fn from(_: &Unicode) -> Self {
163        CF_UNICODETEXT
164    }
165}
166
167#[derive(Copy, Clone)]
168///Format for file lists (generated by drag & drop).
169///
170///Corresponds to `CF_HDROP`
171///
172///`read_clipboard` returns number of file names
173pub struct FileList;
174
175impl Getter<alloc::vec::Vec<alloc::string::String>> for FileList {
176    #[inline(always)]
177    fn read_clipboard(&self, out: &mut alloc::vec::Vec<alloc::string::String>) -> SysResult<usize> {
178        crate::raw::get_file_list(out)
179    }
180}
181
182#[cfg(feature = "std")]
183impl Getter<alloc::vec::Vec<std::path::PathBuf>> for FileList {
184    #[inline(always)]
185    fn read_clipboard(&self, out: &mut alloc::vec::Vec<std::path::PathBuf>) -> SysResult<usize> {
186        crate::raw::get_file_list_path(out)
187    }
188}
189
190impl<T: AsRef<str>> Setter<[T]> for FileList {
191    #[inline(always)]
192    fn write_clipboard(&self, data: &[T]) -> SysResult<()> {
193        crate::raw::set_file_list(data)
194    }
195}
196
197impl From<&FileList> for u32 {
198    #[inline(always)]
199    fn from(_: &FileList) -> Self {
200        CF_HDROP
201    }
202}
203
204#[derive(Copy, Clone)]
205///Format for bitmap images i.e. `CF_BITMAP`.
206///
207///Both `Getter` and `Setter` expects image as header and rgb payload
208pub struct Bitmap;
209
210impl Getter<alloc::vec::Vec<u8>> for Bitmap {
211    #[inline(always)]
212    fn read_clipboard(&self, out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
213        crate::raw::get_bitmap(out)
214    }
215}
216
217impl<T: AsRef<[u8]>> Setter<T> for Bitmap {
218    #[inline(always)]
219    fn write_clipboard(&self, data: &T) -> SysResult<()> {
220        crate::raw::set_bitmap(data.as_ref())
221    }
222}
223
224impl From<&Bitmap> for u32 {
225    #[inline(always)]
226    fn from(_: &Bitmap) -> Self {
227        CF_BITMAP
228    }
229}
230
231#[derive(Copy, Clone)]
232///HTML Foramt
233///
234///Reference: https://learn.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format
235pub struct Html(NonZeroU32);
236
237impl Html {
238    #[inline(always)]
239    ///Creates new instance, if possible
240    pub fn new() -> Option<Self> {
241        //utf-16 "HTML Format"
242        const NAME: [u16; 12] = [72, 84, 77, 76, 32, 70, 111, 114, 109, 97, 116, 0];
243        unsafe {
244            crate::raw::register_raw_format(&NAME).map(Self)
245        }
246    }
247
248    #[inline(always)]
249    ///Gets raw format code
250    pub fn code(&self) -> u32 {
251        self.0.get()
252    }
253}
254
255impl Getter<alloc::vec::Vec<u8>> for Html {
256    #[inline(always)]
257    fn read_clipboard(&self, out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
258        crate::raw::get_html(self.0.get(), out)
259    }
260}
261
262impl Getter<alloc::string::String> for Html {
263    #[inline(always)]
264    fn read_clipboard(&self, out: &mut alloc::string::String) -> SysResult<usize> {
265        crate::raw::get_html(self.0.get(), unsafe { out.as_mut_vec() })
266    }
267}
268
269impl<T: AsRef<str>> Setter<T> for Html {
270    #[inline(always)]
271    fn write_clipboard(&self, data: &T) -> SysResult<()> {
272        crate::raw::set_html(self.code(), data.as_ref())
273    }
274}
275
276impl From<&Html> for u32 {
277    #[inline(always)]
278    fn from(value: &Html) -> Self {
279        value.code()
280    }
281}
282
283impl_format!(Html, Bitmap, RawData, Unicode, FileList);