Atlas - clipboard.c

Home / ext / SDL / examples / misc / 02-clipboard Lines: 3 | Size: 9125 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example code lets the user copy and paste with the system clipboard. 3 * 4 * This only handles text, but SDL supports other data types, too. 5 * 6 * This code is public domain. Feel free to use it for any purpose! 7 */ 8 9#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 10#include <SDL3/SDL.h> 11#include <SDL3/SDL_main.h> 12 13/* We will use this renderer to draw into this window every frame. */ 14static SDL_Window *window = NULL; 15static SDL_Renderer *renderer = NULL; 16static const char *copybuttonstr = "Click here to copy!"; 17static const char *pastebuttonstr = "Click here to paste!"; 18static SDL_FRect currenttimerect; 19static SDL_FRect copybuttonrect; 20static SDL_FRect pastetextrect; 21static SDL_FRect pastebuttonrect; 22static bool copy_pressed = false; 23static bool paste_pressed = false; 24static char current_time[64]; 25static char *pasted_str = NULL; 26 27static void CalculateCurrentTimeString(void) 28{ 29 SDL_Time ticks = 0; 30 SDL_DateTime dt; 31 if (!SDL_GetCurrentTime(&ticks) || !SDL_TimeToDateTime(ticks, &dt, true)) { 32 SDL_snprintf(current_time, sizeof (current_time), "(Don't know the current time, sorry.)"); 33 } else { 34 static const char *month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; 35 static const char *day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; 36 SDL_snprintf(current_time, sizeof (current_time), "%s, %s %d, %d %02d:%02d:%02d", day[dt.day_of_week], month[dt.month-1], dt.day, dt.year, dt.hour, dt.minute, dt.second); 37 } 38} 39 40/* This function runs once at startup. */ 41SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 42{ 43 SDL_SetAppMetadata("Example Misc Clipboard", "1.0", "com.example.misc-clipboard"); 44 45 if (!SDL_Init(SDL_INIT_VIDEO)) { 46 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 47 return SDL_APP_FAILURE; 48 } 49 50 if (!SDL_CreateWindowAndRenderer("examples/misc/clipboard", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 51 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 52 return SDL_APP_FAILURE; 53 } 54 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 55 56 CalculateCurrentTimeString(); 57 58 /* set up the locations where we'll draw stuff. */ 59 currenttimerect.x = 30; 60 currenttimerect.y = 10; 61 currenttimerect.w = 390; 62 currenttimerect.h = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 10; 63 64 copybuttonrect.x = currenttimerect.x + currenttimerect.w + 30; 65 copybuttonrect.y = currenttimerect.y; 66 copybuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(copybuttonstr)) + 10); 67 copybuttonrect.h = currenttimerect.h; 68 69 pastetextrect.x = 10; 70 pastetextrect.y = currenttimerect.y + currenttimerect.h + 10; 71 pastetextrect.w = 620; 72 pastetextrect.h = ((480 - pastetextrect.y) - copybuttonrect.h) - 20; 73 74 pastebuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(pastebuttonstr)) + 10); 75 pastebuttonrect.x = (640 - pastebuttonrect.w) / 2.0f; 76 pastebuttonrect.y = pastetextrect.y + pastetextrect.h + 10; 77 pastebuttonrect.h = copybuttonrect.h; 78 79 return SDL_APP_CONTINUE; /* carry on with the program! */ 80} 81 82/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 83SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 84{ 85 SDL_ConvertEventToRenderCoordinates(renderer, event); 86 if (event->type == SDL_EVENT_QUIT) { 87 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 88 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) { 89 if (event->button.button == SDL_BUTTON_LEFT) { 90 const SDL_FPoint p = { event->button.x, event->button.y }; 91 copy_pressed = SDL_PointInRectFloat(&p, &copybuttonrect); 92 paste_pressed = SDL_PointInRectFloat(&p, &pastebuttonrect); 93 } 94 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) { 95 if (event->button.button == SDL_BUTTON_LEFT) { 96 const SDL_FPoint p = { event->button.x, event->button.y }; 97 if (copy_pressed && SDL_PointInRectFloat(&p, &copybuttonrect)) { 98 SDL_SetClipboardText(current_time); 99 } else if (paste_pressed && SDL_PointInRectFloat(&p, &pastebuttonrect)) { 100 SDL_free(pasted_str); 101 pasted_str = SDL_GetClipboardText(); 102 } 103 copy_pressed = paste_pressed = false; 104 } 105 } 106 107 return SDL_APP_CONTINUE; /* carry on with the program! */ 108} 109 110static void RenderPastedText(void) 111{ 112 char *str = pasted_str; 113 if (str) { 114 float x = pastetextrect.x + 5; 115 float y = pastetextrect.y + 5; 116 const float w = pastetextrect.w - 10; 117 const float h = pastetextrect.h; 118 const size_t max_chars_per_line = (size_t) (w / SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE); 119 char *newline; 120 size_t slen; 121 char ch; 122 123 /* this doesn't wordwrap, or deal with Unicode....this is just a simple example app! */ 124 while ((newline = SDL_strchr(str, '\n')) != NULL) { 125 const bool ignore_cr = ((newline > str) && (newline[-1] == '\r')); 126 127 if (ignore_cr) { 128 newline[-1] = '\0'; 129 } 130 *newline = '\0'; 131 132 slen = SDL_strlen(str); /* length to end of line. */ 133 slen = SDL_min(slen, max_chars_per_line); 134 ch = str[slen]; 135 str[slen] = '\0'; 136 SDL_RenderDebugText(renderer, x, y, str); 137 str[slen] = ch; 138 139 if (ignore_cr) { 140 newline[-1] = '\r'; 141 } 142 *newline = '\n'; 143 144 str = newline + 1; 145 y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2); 146 if ((h - y) < SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) { 147 break; // no space for another line, stop here. 148 } 149 } 150 151 /* last text after newline, if there's room. */ 152 if ((h - y) >= SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) { 153 slen = SDL_strlen(str); /* length to end of line. */ 154 slen = SDL_min(slen, max_chars_per_line); 155 ch = str[slen]; 156 str[slen] = '\0'; 157 SDL_RenderDebugText(renderer, x, y, str); 158 str[slen] = ch; 159 } 160 } 161} 162 163/* This function runs once per frame, and is the heart of the program. */ 164SDL_AppResult SDL_AppIterate(void *appstate) 165{ 166 float x, y; 167 168 CalculateCurrentTimeString(); 169 170 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black */ 171 SDL_RenderClear(renderer); 172 173 /* draw a frame around the current time. */ 174 SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); 175 SDL_RenderFillRect(renderer, &currenttimerect); 176 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 177 SDL_RenderRect(renderer, &currenttimerect); 178 179 /* draw the current time inside the frame. */ 180 x = currenttimerect.x + ((currenttimerect.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(current_time))) / 2.0f); 181 y = currenttimerect.y + 5; 182 SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); 183 SDL_RenderDebugText(renderer, x, y, current_time); 184 185 /* draw a frame for the "copy the current time to the clipboard" button. */ 186 if (copy_pressed) { 187 SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); 188 } else { 189 SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); 190 } 191 SDL_RenderFillRect(renderer, &copybuttonrect); 192 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 193 SDL_RenderRect(renderer, &copybuttonrect); 194 195 /* draw the "copy this text" button string. */ 196 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 197 SDL_RenderDebugText(renderer, copybuttonrect.x + 5, copybuttonrect.y + 5, copybuttonstr); 198 199 /* draw a frame for the pasted text area. */ 200 SDL_SetRenderDrawColor(renderer, 0, 53, 25, 255); 201 SDL_RenderFillRect(renderer, &pastetextrect); 202 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 203 SDL_RenderRect(renderer, &pastetextrect); 204 205 /* draw pasted text. */ 206 SDL_SetRenderDrawColor(renderer, 0, 219, 107, 255); 207 RenderPastedText(); 208 209 /* draw a frame for the "paste from the clipboard" button. */ 210 if (paste_pressed) { 211 SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); 212 } else { 213 SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); 214 } 215 SDL_RenderFillRect(renderer, &pastebuttonrect); 216 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 217 SDL_RenderRect(renderer, &pastebuttonrect); 218 219 /* draw the "paste some text" button string. */ 220 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 221 SDL_RenderDebugText(renderer, pastebuttonrect.x + 5, pastebuttonrect.y + 5, pastebuttonstr); 222 223 /* put the new rendering on the screen. */ 224 SDL_RenderPresent(renderer); 225 226 return SDL_APP_CONTINUE; /* carry on with the program! */ 227} 228 229/* This function runs once at shutdown. */ 230void SDL_AppQuit(void *appstate, SDL_AppResult result) 231{ 232 SDL_free(pasted_str); 233 /* SDL will clean up the window/renderer for us. */ 234} 235 236
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.