clipboard.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  9. #include <SDL3/SDL.h>
  10. #include <SDL3/SDL_main.h>
  11. /* We will use this renderer to draw into this window every frame. */
  12. static SDL_Window *window = NULL;
  13. static SDL_Renderer *renderer = NULL;
  14. static const char *copybuttonstr = "Click here to copy!";
  15. static const char *pastebuttonstr = "Click here to paste!";
  16. static SDL_FRect currenttimerect;
  17. static SDL_FRect copybuttonrect;
  18. static SDL_FRect pastetextrect;
  19. static SDL_FRect pastebuttonrect;
  20. static bool copy_pressed = false;
  21. static bool paste_pressed = false;
  22. static char current_time[64];
  23. static char *pasted_str = NULL;
  24. static void CalculateCurrentTimeString(void)
  25. {
  26. SDL_Time ticks = 0;
  27. SDL_DateTime dt;
  28. if (!SDL_GetCurrentTime(&ticks) || !SDL_TimeToDateTime(ticks, &dt, true)) {
  29. SDL_snprintf(current_time, sizeof (current_time), "(Don't know the current time, sorry.)");
  30. } else {
  31. static const char *month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  32. static const char *day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  33. 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);
  34. }
  35. }
  36. /* This function runs once at startup. */
  37. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  38. {
  39. SDL_SetAppMetadata("Example Misc Clipboard", "1.0", "com.example.misc-clipboard");
  40. if (!SDL_Init(SDL_INIT_VIDEO)) {
  41. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  42. return SDL_APP_FAILURE;
  43. }
  44. if (!SDL_CreateWindowAndRenderer("examples/misc/clipboard", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  45. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  46. return SDL_APP_FAILURE;
  47. }
  48. SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  49. CalculateCurrentTimeString();
  50. /* set up the locations where we'll draw stuff. */
  51. currenttimerect.x = 30;
  52. currenttimerect.y = 10;
  53. currenttimerect.w = 390;
  54. currenttimerect.h = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 10;
  55. copybuttonrect.x = currenttimerect.x + currenttimerect.w + 30;
  56. copybuttonrect.y = currenttimerect.y;
  57. copybuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(copybuttonstr)) + 10);
  58. copybuttonrect.h = currenttimerect.h;
  59. pastetextrect.x = 10;
  60. pastetextrect.y = currenttimerect.y + currenttimerect.h + 10;
  61. pastetextrect.w = 620;
  62. pastetextrect.h = ((480 - pastetextrect.y) - copybuttonrect.h) - 20;
  63. pastebuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(pastebuttonstr)) + 10);
  64. pastebuttonrect.x = (640 - pastebuttonrect.w) / 2.0f;
  65. pastebuttonrect.y = pastetextrect.y + pastetextrect.h + 10;
  66. pastebuttonrect.h = copybuttonrect.h;
  67. return SDL_APP_CONTINUE; /* carry on with the program! */
  68. }
  69. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  70. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  71. {
  72. SDL_ConvertEventToRenderCoordinates(renderer, event);
  73. if (event->type == SDL_EVENT_QUIT) {
  74. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  75. } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
  76. if (event->button.button == SDL_BUTTON_LEFT) {
  77. const SDL_FPoint p = { event->button.x, event->button.y };
  78. copy_pressed = SDL_PointInRectFloat(&p, &copybuttonrect);
  79. paste_pressed = SDL_PointInRectFloat(&p, &pastebuttonrect);
  80. }
  81. } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
  82. if (event->button.button == SDL_BUTTON_LEFT) {
  83. const SDL_FPoint p = { event->button.x, event->button.y };
  84. if (copy_pressed && SDL_PointInRectFloat(&p, &copybuttonrect)) {
  85. SDL_SetClipboardText(current_time);
  86. } else if (paste_pressed && SDL_PointInRectFloat(&p, &pastebuttonrect)) {
  87. SDL_free(pasted_str);
  88. pasted_str = SDL_GetClipboardText();
  89. }
  90. copy_pressed = paste_pressed = false;
  91. }
  92. }
  93. return SDL_APP_CONTINUE; /* carry on with the program! */
  94. }
  95. static void RenderPastedText(void)
  96. {
  97. char *str = pasted_str;
  98. if (str) {
  99. float x = pastetextrect.x + 5;
  100. float y = pastetextrect.y + 5;
  101. const float w = pastetextrect.w - 10;
  102. const float h = pastetextrect.h;
  103. const size_t max_chars_per_line = (size_t) (w / SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE);
  104. char *newline;
  105. size_t slen;
  106. char ch;
  107. /* this doesn't wordwrap, or deal with Unicode....this is just a simple example app! */
  108. while ((newline = SDL_strchr(str, '\n')) != NULL) {
  109. const bool ignore_cr = ((newline > str) && (newline[-1] == '\r'));
  110. if (ignore_cr) {
  111. newline[-1] = '\0';
  112. }
  113. *newline = '\0';
  114. slen = SDL_strlen(str); /* length to end of line. */
  115. slen = SDL_min(slen, max_chars_per_line);
  116. ch = str[slen];
  117. str[slen] = '\0';
  118. SDL_RenderDebugText(renderer, x, y, str);
  119. str[slen] = ch;
  120. if (ignore_cr) {
  121. newline[-1] = '\r';
  122. }
  123. *newline = '\n';
  124. str = newline + 1;
  125. y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2);
  126. if ((h - y) < SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
  127. break; // no space for another line, stop here.
  128. }
  129. }
  130. /* last text after newline, if there's room. */
  131. if ((h - y) >= SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
  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. }
  140. }
  141. /* This function runs once per frame, and is the heart of the program. */
  142. SDL_AppResult SDL_AppIterate(void *appstate)
  143. {
  144. float x, y;
  145. CalculateCurrentTimeString();
  146. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black */
  147. SDL_RenderClear(renderer);
  148. /* draw a frame around the current time. */
  149. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  150. SDL_RenderFillRect(renderer, &currenttimerect);
  151. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  152. SDL_RenderRect(renderer, &currenttimerect);
  153. /* draw the current time inside the frame. */
  154. x = currenttimerect.x + ((currenttimerect.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(current_time))) / 2.0f);
  155. y = currenttimerect.y + 5;
  156. SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
  157. SDL_RenderDebugText(renderer, x, y, current_time);
  158. /* draw a frame for the "copy the current time to the clipboard" button. */
  159. if (copy_pressed) {
  160. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  161. } else {
  162. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  163. }
  164. SDL_RenderFillRect(renderer, &copybuttonrect);
  165. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  166. SDL_RenderRect(renderer, &copybuttonrect);
  167. /* draw the "copy this text" button string. */
  168. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  169. SDL_RenderDebugText(renderer, copybuttonrect.x + 5, copybuttonrect.y + 5, copybuttonstr);
  170. /* draw a frame for the pasted text area. */
  171. SDL_SetRenderDrawColor(renderer, 0, 53, 25, 255);
  172. SDL_RenderFillRect(renderer, &pastetextrect);
  173. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  174. SDL_RenderRect(renderer, &pastetextrect);
  175. /* draw pasted text. */
  176. SDL_SetRenderDrawColor(renderer, 0, 219, 107, 255);
  177. RenderPastedText();
  178. /* draw a frame for the "paste from the clipboard" button. */
  179. if (paste_pressed) {
  180. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  181. } else {
  182. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  183. }
  184. SDL_RenderFillRect(renderer, &pastebuttonrect);
  185. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  186. SDL_RenderRect(renderer, &pastebuttonrect);
  187. /* draw the "paste some text" button string. */
  188. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  189. SDL_RenderDebugText(renderer, pastebuttonrect.x + 5, pastebuttonrect.y + 5, pastebuttonstr);
  190. /* put the new rendering on the screen. */
  191. SDL_RenderPresent(renderer);
  192. return SDL_APP_CONTINUE; /* carry on with the program! */
  193. }
  194. /* This function runs once at shutdown. */
  195. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  196. {
  197. SDL_free(pasted_str);
  198. /* SDL will clean up the window/renderer for us. */
  199. }