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