clipboard.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, 0, &window, &renderer)) {
  45. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  46. return SDL_APP_FAILURE;
  47. }
  48. CalculateCurrentTimeString();
  49. /* set up the locations where we'll draw stuff. */
  50. currenttimerect.x = 30;
  51. currenttimerect.y = 10;
  52. currenttimerect.w = 390;
  53. currenttimerect.h = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 10;
  54. copybuttonrect.x = currenttimerect.x + currenttimerect.w + 30;
  55. copybuttonrect.y = currenttimerect.y;
  56. copybuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(copybuttonstr)) + 10);
  57. copybuttonrect.h = currenttimerect.h;
  58. pastetextrect.x = 10;
  59. pastetextrect.y = currenttimerect.y + currenttimerect.h + 10;
  60. pastetextrect.w = 620;
  61. pastetextrect.h = ((480 - pastetextrect.y) - copybuttonrect.h) - 20;
  62. pastebuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(pastebuttonstr)) + 10);
  63. pastebuttonrect.x = (640 - pastebuttonrect.w) / 2.0f;
  64. pastebuttonrect.y = pastetextrect.y + pastetextrect.h + 10;
  65. pastebuttonrect.h = copybuttonrect.h;
  66. return SDL_APP_CONTINUE; /* carry on with the program! */
  67. }
  68. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  69. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  70. {
  71. if (event->type == SDL_EVENT_QUIT) {
  72. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  73. } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
  74. if (event->button.button == SDL_BUTTON_LEFT) {
  75. const SDL_FPoint p = { event->button.x, event->button.y };
  76. copy_pressed = SDL_PointInRectFloat(&p, &copybuttonrect);
  77. paste_pressed = SDL_PointInRectFloat(&p, &pastebuttonrect);
  78. }
  79. } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
  80. if (event->button.button == SDL_BUTTON_LEFT) {
  81. const SDL_FPoint p = { event->button.x, event->button.y };
  82. if (copy_pressed && SDL_PointInRectFloat(&p, &copybuttonrect)) {
  83. SDL_SetClipboardText(current_time);
  84. } else if (paste_pressed && SDL_PointInRectFloat(&p, &pastebuttonrect)) {
  85. SDL_free(pasted_str);
  86. pasted_str = SDL_GetClipboardText();
  87. }
  88. copy_pressed = paste_pressed = false;
  89. }
  90. }
  91. return SDL_APP_CONTINUE; /* carry on with the program! */
  92. }
  93. static void RenderPastedText(void)
  94. {
  95. char *str = pasted_str;
  96. if (str) {
  97. float x = pastetextrect.x + 5;
  98. float y = pastetextrect.y + 5;
  99. const float w = pastetextrect.w - 10;
  100. const float h = pastetextrect.h;
  101. const size_t max_chars_per_line = (size_t) (w / SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE);
  102. char *newline;
  103. size_t slen;
  104. char ch;
  105. /* this doesn't wordwrap, or deal with Unicode....this is just a simple example app! */
  106. while ((newline = SDL_strchr(str, '\n')) != NULL) {
  107. const bool ignore_cr = ((newline > str) && (newline[-1] == '\r'));
  108. if (ignore_cr) {
  109. newline[-1] = '\0';
  110. }
  111. *newline = '\0';
  112. slen = SDL_strlen(str); /* length to end of line. */
  113. slen = SDL_min(slen, max_chars_per_line);
  114. ch = str[slen];
  115. str[slen] = '\0';
  116. SDL_RenderDebugText(renderer, x, y, str);
  117. str[slen] = ch;
  118. if (ignore_cr) {
  119. newline[-1] = '\r';
  120. }
  121. *newline = '\n';
  122. str = newline + 1;
  123. y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2);
  124. if ((h - y) < SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
  125. break; // no space for another line, stop here.
  126. }
  127. }
  128. /* last text after newline, if there's room. */
  129. if ((h - y) >= SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
  130. slen = SDL_strlen(str); /* length to end of line. */
  131. slen = SDL_min(slen, max_chars_per_line);
  132. ch = str[slen];
  133. str[slen] = '\0';
  134. SDL_RenderDebugText(renderer, x, y, str);
  135. str[slen] = ch;
  136. }
  137. }
  138. }
  139. /* This function runs once per frame, and is the heart of the program. */
  140. SDL_AppResult SDL_AppIterate(void *appstate)
  141. {
  142. float x, y;
  143. CalculateCurrentTimeString();
  144. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black */
  145. SDL_RenderClear(renderer);
  146. /* draw a frame around the current time. */
  147. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  148. SDL_RenderFillRect(renderer, &currenttimerect);
  149. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  150. SDL_RenderRect(renderer, &currenttimerect);
  151. /* draw the current time inside the frame. */
  152. x = currenttimerect.x + ((currenttimerect.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(current_time))) / 2.0f);
  153. y = currenttimerect.y + 5;
  154. SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
  155. SDL_RenderDebugText(renderer, x, y, current_time);
  156. /* draw a frame for the "copy the current time to the clipboard" button. */
  157. if (copy_pressed) {
  158. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  159. } else {
  160. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  161. }
  162. SDL_RenderFillRect(renderer, &copybuttonrect);
  163. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  164. SDL_RenderRect(renderer, &copybuttonrect);
  165. /* draw the "copy this text" button string. */
  166. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  167. SDL_RenderDebugText(renderer, copybuttonrect.x + 5, copybuttonrect.y + 5, copybuttonstr);
  168. /* draw a frame for the pasted text area. */
  169. SDL_SetRenderDrawColor(renderer, 0, 53, 25, 255);
  170. SDL_RenderFillRect(renderer, &pastetextrect);
  171. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  172. SDL_RenderRect(renderer, &pastetextrect);
  173. /* draw pasted text. */
  174. SDL_SetRenderDrawColor(renderer, 0, 219, 107, 255);
  175. RenderPastedText();
  176. /* draw a frame for the "paste from the clipboard" button. */
  177. if (paste_pressed) {
  178. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  179. } else {
  180. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  181. }
  182. SDL_RenderFillRect(renderer, &pastebuttonrect);
  183. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  184. SDL_RenderRect(renderer, &pastebuttonrect);
  185. /* draw the "paste some text" button string. */
  186. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  187. SDL_RenderDebugText(renderer, pastebuttonrect.x + 5, pastebuttonrect.y + 5, pastebuttonstr);
  188. /* put the new rendering on the screen. */
  189. SDL_RenderPresent(renderer);
  190. return SDL_APP_CONTINUE; /* carry on with the program! */
  191. }
  192. /* This function runs once at shutdown. */
  193. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  194. {
  195. SDL_free(pasted_str);
  196. /* SDL will clean up the window/renderer for us. */
  197. }