SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_clipboard.h
1#ifndef SDL3PP_CLIPBOARD_H_
2#define SDL3PP_CLIPBOARD_H_
3
4#include <SDL3/SDL_clipboard.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
77{
78 CheckError(SDL_SetClipboardText(text));
79}
80
99{
100 return StringResult{CheckError(SDL_GetClipboardText())};
101}
102
115inline bool HasClipboardText() { return SDL_HasClipboardText(); }
116
131{
132 CheckError(SDL_SetPrimarySelectionText(text));
133}
134
153{
154 return StringResult{CheckError(SDL_GetPrimarySelectionText())};
155}
156
170inline bool HasPrimarySelectionText() { return SDL_HasPrimarySelectionText(); }
171
193using ClipboardDataCallback = const void*(SDLCALL*)(void* userdata,
194 const char* mime_type,
195 size_t* size);
196
218using ClipboardDataCB = std::function<SourceBytes(const char* mime_type)>;
219
230using ClipboardCleanupCallback = void(SDLCALL*)(void* userdata);
231
242using ClipboardCleanupCB = std::function<void()>;
243
275 void* userdata,
276 std::span<const char*> mime_types)
277{
278 CheckError(SDL_SetClipboardData(
279 callback, cleanup, userdata, mime_types.data(), mime_types.size()));
280}
281
311 ClipboardCleanupCB cleanup,
312 std::span<const char*> mime_types)
313{
314 static ClipboardDataCB s_callback;
315 static ClipboardCleanupCB s_cleanup;
316 CheckError(SDL_ClearClipboardData());
317 s_callback = callback;
318 s_cleanup = cleanup;
320 [](void*, const char* mime_type, size_t* size) -> const void* {
321 auto source = s_callback(mime_type);
322 *size = source.size_bytes();
323 return source.data();
324 },
325 [](void*) {
326 if (s_cleanup) s_cleanup();
327 },
328 nullptr,
329 mime_types);
330}
331
343inline void ClearClipboardData() { CheckError(SDL_ClearClipboardData()); }
344
363{
364 size_t count = 0;
365 auto data = SDL_GetClipboardData(mime_type, &count);
366 if (!data) return {};
367 return StringResult{static_cast<char*>(data), count};
368}
369
387template<class T>
389{
390 size_t count = 0;
391 auto data = SDL_GetClipboardData(mime_type, &count);
392 if (!data) return {};
393 return OwnArray<T>{static_cast<T*>(data), count / sizeof(T)};
394}
395
410inline bool HasClipboardData(StringParam mime_type)
411{
412 return SDL_HasClipboardData(mime_type);
413}
414
428{
429 size_t count = 0;
430 auto data = SDL_GetClipboardMimeTypes(&count);
431 if (!data) return {};
432 return OwnArray<char*>{data, count};
433}
434
436
437} // namespace SDL
438
439#endif /* SDL3PP_CLIPBOARD_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
Source byte stream.
Definition: SDL3pp_strings.h:239
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
StringResult GetClipboardText()
Get UTF-8 text from the clipboard.
Definition: SDL3pp_clipboard.h:98
OwnArray< char * > GetClipboardMimeTypes()
Retrieve the list of mime types available in the clipboard.
Definition: SDL3pp_clipboard.h:427
bool HasClipboardText()
Query whether the clipboard exists and contains a non-empty text string.
Definition: SDL3pp_clipboard.h:115
void(SDLCALL *)(void *userdata) ClipboardCleanupCallback
Callback function that will be called when the clipboard is cleared, or when new data is set.
Definition: SDL3pp_clipboard.h:230
OwnArray< T > GetClipboardDataAs(StringParam mime_type)
Get the data from the clipboard for a given mime type.
Definition: SDL3pp_clipboard.h:388
bool HasClipboardData(StringParam mime_type)
Query whether there is data in the clipboard for the provided mime type.
Definition: SDL3pp_clipboard.h:410
void ClearClipboardData()
Clear the clipboard data.
Definition: SDL3pp_clipboard.h:343
StringResult GetPrimarySelectionText()
Get UTF-8 text from the primary selection.
Definition: SDL3pp_clipboard.h:152
void SetClipboardText(StringParam text)
Put UTF-8 text into the clipboard.
Definition: SDL3pp_clipboard.h:76
const void *(SDLCALL *)(void *userdata, const char *mime_type, size_t *size) ClipboardDataCallback
Callback function that will be called when data for the specified mime-type is requested by the OS.
Definition: SDL3pp_clipboard.h:195
std::function< void()> ClipboardCleanupCB
Callback function that will be called when the clipboard is cleared, or when new data is set.
Definition: SDL3pp_clipboard.h:242
void SetClipboardData(ClipboardDataCallback callback, ClipboardCleanupCallback cleanup, void *userdata, std::span< const char * > mime_types)
Offer clipboard data to the OS.
Definition: SDL3pp_clipboard.h:273
bool HasPrimarySelectionText()
Query whether the primary selection exists and contains a non-empty text string.
Definition: SDL3pp_clipboard.h:170
std::function< SourceBytes(const char *mime_type)> ClipboardDataCB
Callback function that will be called when data for the specified mime-type is requested by the OS.
Definition: SDL3pp_clipboard.h:218
void SetPrimarySelectionText(StringParam text)
Put UTF-8 text into the primary selection.
Definition: SDL3pp_clipboard.h:130
StringResult GetClipboardData(StringParam mime_type)
Get the data from the clipboard for a given mime type.
Definition: SDL3pp_clipboard.h:362
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
Main include header for the SDL3pp library.
A simple std::string-like interface for SDL allocated strings.
Definition: SDL3pp_strings.h:153