clipboard-win icon indicating copy to clipboard operation
clipboard-win copied to clipboard

How to figure out the correct format?

Open AsafMah opened this issue 1 year ago • 3 comments

There are a few formats to choose from, how can I know which ones are available before extracting?

I saw EnumFormats in the raw crate, but that seems too low level - it returns the underlying formats and not the ones defined in this crate.

AsafMah avatar Feb 20 '23 12:02 AsafMah

Actually, your approach normally is to be on look out for particular formats, that you need. But if you really want to enumerate all (currently on clipboard, not just all), then you have to use EnumFormats.

it is iterator that returns raw id of format and you can compare it with constants On side note, winapi guarantees that EnumFormats will yield formats in order of placement on clipboard. So if for example you'd want to fetch data in ordered manner you could start fetching from first value within this iterator would be format that you look for. For more details, please read winapi documentation on EnumClipboardFormats

Aside from some specific esoteric formats, most are pretty much self-explatory

So if you have list of formats that you want to look out for, you can use: is_format_avail to query every format on list But if you just want to enumerate all available then you use EnumFormats

DoumanAsh avatar Feb 20 '23 13:02 DoumanAsh

What I was looking for is some mapping from these lower level formats to the higher level ones, It says bitmap corresponds to CF_BITMAP, but it would be nice to have the same EnumFormats and is_format_avail also at the higher level module, that would work with FileList and such.

Maybe even a method that returns all of the data in each available format.

I can implement it myself for my project, but I think it's a bit of a hole in the API.

AsafMah avatar Feb 20 '23 15:02 AsafMah

I can create API like this:

pub trait OnEnumerator {
    fn on_string(text: String) -> {}
    fn on_file_list(...) -> {}
    //etc
}

But that's not exactly convenient for use, but anything else would be inherently wasteful or overly complicated, and there is honestly little reason to enumerate all possible formats. Outside of text, images or file lists, there is really not a lot of useful stuff in clipboard Not to memtion issue of custom formats that every application can create makes it inherently impossible to make API that would fit it all.

If you can come up with API feel free to propose, but I do not really see much use when you can just iterate over formats and match on format identifier.

DoumanAsh avatar Feb 20 '23 15:02 DoumanAsh