tests: Add showing/setting the primary selection text to testclipboard

This commit is contained in:
Frank Praznik 2025-02-27 11:14:04 -05:00
parent 7b9036bea6
commit edaf447678
No known key found for this signature in database
1 changed files with 23 additions and 2 deletions

View File

@ -63,6 +63,8 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
if (event->key.key == SDLK_C && event->key.mod & SDL_KMOD_CTRL) { if (event->key.key == SDLK_C && event->key.mod & SDL_KMOD_CTRL) {
SDL_SetClipboardData(ClipboardDataCallback, NULL, NULL, mime_types, SDL_arraysize(mime_types)); SDL_SetClipboardData(ClipboardDataCallback, NULL, NULL, mime_types, SDL_arraysize(mime_types));
break; break;
} else if (event->key.key == SDLK_P && event->key.mod & SDL_KMOD_CTRL) {
SDL_SetPrimarySelectionText("SDL Primary Selection Text!");
} }
break; break;
@ -99,6 +101,15 @@ static float PrintClipboardText(float x, float y, const char *mime_type)
return 0.0f; return 0.0f;
} }
static float PrintPrimarySelectionText(float x, float y)
{
if (SDL_HasPrimarySelectionText()) {
SDL_RenderDebugText(renderer, x, y, SDL_GetPrimarySelectionText());
return SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2.0f;
}
return 0.0f;
}
static float PrintClipboardImage(float x, float y, const char *mime_type) static float PrintClipboardImage(float x, float y, const char *mime_type)
{ {
/* We don't actually need to read this data each frame, but this is a simple example */ /* We don't actually need to read this data each frame, but this is a simple example */
@ -134,7 +145,7 @@ static float PrintClipboardImage(float x, float y, const char *mime_type)
return 0.0f; return 0.0f;
} }
static void PrintClipboardContents(float x, float y) static float PrintClipboardContents(float x, float y)
{ {
char **clipboard_mime_types = SDL_GetClipboardMimeTypes(NULL); char **clipboard_mime_types = SDL_GetClipboardMimeTypes(NULL);
if (clipboard_mime_types) { if (clipboard_mime_types) {
@ -152,6 +163,8 @@ static void PrintClipboardContents(float x, float y)
} }
SDL_free(clipboard_mime_types); SDL_free(clipboard_mime_types);
} }
return y;
} }
SDL_AppResult SDL_AppIterate(void *appstate) SDL_AppResult SDL_AppIterate(void *appstate)
@ -164,10 +177,18 @@ SDL_AppResult SDL_AppIterate(void *appstate)
float y = 4.0f; float y = 4.0f;
SDL_RenderDebugText(renderer, x, y, "Press Ctrl+C to copy content to the clipboard"); SDL_RenderDebugText(renderer, x, y, "Press Ctrl+C to copy content to the clipboard");
y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2; y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
SDL_RenderDebugText(renderer, x, y, "Press Ctrl+P to set the primary selection text");
y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
SDL_RenderDebugText(renderer, x, y, "Clipboard contents:"); SDL_RenderDebugText(renderer, x, y, "Clipboard contents:");
x += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2; x += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2; y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
PrintClipboardContents(x, y); y = PrintClipboardContents(x, y);
if (SDL_HasPrimarySelectionText()) {
x = 4.0f;
SDL_RenderDebugText(renderer, x, y, "Primary selection text contents:");
y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
PrintPrimarySelectionText(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y);
}
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);