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

From edaf447678700f88b7f26f8d5071006542d92d2e Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Thu, 27 Feb 2025 11:14:04 -0500
Subject: [PATCH] tests: Add showing/setting the primary selection text to
 testclipboard

---
 test/testclipboard.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/test/testclipboard.c b/test/testclipboard.c
index cb57bbf25d8ca..9dad02be79c52 100644
--- a/test/testclipboard.c
+++ b/test/testclipboard.c
@@ -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) {
             SDL_SetClipboardData(ClipboardDataCallback, NULL, NULL, mime_types, SDL_arraysize(mime_types));
             break;
+        } else if (event->key.key == SDLK_P && event->key.mod & SDL_KMOD_CTRL) {
+            SDL_SetPrimarySelectionText("SDL Primary Selection Text!");
         }
         break;
 
@@ -99,6 +101,15 @@ static float PrintClipboardText(float x, float y, const char *mime_type)
     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)
 {
     /* 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;
 }
 
-static void PrintClipboardContents(float x, float y)
+static float PrintClipboardContents(float x, float y)
 {
     char **clipboard_mime_types = SDL_GetClipboardMimeTypes(NULL);
     if (clipboard_mime_types) {
@@ -152,6 +163,8 @@ static void PrintClipboardContents(float x, float y)
         }
         SDL_free(clipboard_mime_types);
     }
+
+    return y;
 }
 
 SDL_AppResult SDL_AppIterate(void *appstate)
@@ -164,10 +177,18 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     float y = 4.0f;
     SDL_RenderDebugText(renderer, x, y, "Press Ctrl+C to copy content to the clipboard");
     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:");
     x += 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);