2
0

core_clipboard_text.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - clipboard text
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Ananth S (@Ananth1839) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Ananth S (@Ananth1839)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "raygui.h"
  20. #define MAX_TEXT_SAMPLES 5
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text");
  31. // Define some sample texts
  32. const char *sampleTexts[MAX_TEXT_SAMPLES] = {
  33. "Hello from raylib!",
  34. "The quick brown fox jumps over the lazy dog",
  35. "Clipboard operations are useful!",
  36. "raylib is a simple and easy-to-use library",
  37. "Copy and paste me!"
  38. };
  39. const char *clipboardText = NULL;
  40. char inputBuffer[256] = "Hello from raylib!"; // Random initial string
  41. // UI required variables
  42. bool textBoxEditMode = false;
  43. bool btnCutPressed = false;
  44. bool btnCopyPressed = false;
  45. bool btnPastePressed = false;
  46. bool btnClearPressed = false;
  47. bool btnRandomPressed = false;
  48. // Set UI style
  49. GuiSetStyle(DEFAULT, TEXT_SIZE, 20);
  50. GuiSetIconScale(2);
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. // Handle button interactions
  59. if (btnCutPressed)
  60. {
  61. SetClipboardText(inputBuffer);
  62. clipboardText = GetClipboardText();
  63. inputBuffer[0] = '\0'; // Quick solution to clear text
  64. //memset(inputBuffer, 0, 256); // Clear full buffer properly
  65. }
  66. if (btnCopyPressed)
  67. {
  68. SetClipboardText(inputBuffer); // Copy text to clipboard
  69. clipboardText = GetClipboardText(); // Get text from clipboard
  70. }
  71. if (btnPastePressed)
  72. {
  73. // Paste text from clipboard
  74. clipboardText = GetClipboardText();
  75. if (clipboardText != NULL) TextCopy(inputBuffer, clipboardText);
  76. }
  77. if (btnClearPressed)
  78. {
  79. inputBuffer[0] = '\0'; // Quick solution to clear text
  80. //memset(inputBuffer, 0, 256); // Clear full buffer properly
  81. }
  82. if (btnRandomPressed)
  83. {
  84. // Get random text from sample list
  85. TextCopy(inputBuffer, sampleTexts[GetRandomValue(0, MAX_TEXT_SAMPLES - 1)]);
  86. }
  87. // Quick cut/copy/paste with keyboard shortcuts
  88. if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL))
  89. {
  90. if (IsKeyPressed(KEY_X))
  91. {
  92. SetClipboardText(inputBuffer);
  93. inputBuffer[0] = '\0'; // Quick solution to clear text
  94. }
  95. if (IsKeyPressed(KEY_C)) SetClipboardText(inputBuffer);
  96. if (IsKeyPressed(KEY_V))
  97. {
  98. clipboardText = GetClipboardText();
  99. if (clipboardText != NULL) TextCopy(inputBuffer, clipboardText);
  100. }
  101. }
  102. //----------------------------------------------------------------------------------
  103. // Draw
  104. //----------------------------------------------------------------------------------
  105. BeginDrawing();
  106. ClearBackground(RAYWHITE);
  107. // Draw instructions
  108. GuiLabel((Rectangle){ 50, 20, 700, 36 }, "Use the BUTTONS or KEY SHORTCUTS:");
  109. DrawText("[CTRL+X] - CUT | [CTRL+C] COPY | [CTRL+V] | PASTE", 50, 60, 20, MAROON);
  110. // Draw text box
  111. if (GuiTextBox((Rectangle){ 50, 120, 652, 40 }, inputBuffer, 256, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  112. // Random text button
  113. btnRandomPressed = GuiButton((Rectangle){ 50 + 652 + 8, 120, 40, 40 }, "#77#");
  114. // Draw buttons
  115. btnCutPressed = GuiButton((Rectangle){ 50, 180, 158, 40 }, "#17#CUT");
  116. btnCopyPressed = GuiButton((Rectangle){ 50 + 165, 180, 158, 40 }, "#16#COPY");
  117. btnPastePressed = GuiButton((Rectangle){ 50 + 165*2, 180, 158, 40 }, "#18#PASTE");
  118. btnClearPressed = GuiButton((Rectangle){ 50 + 165*3, 180, 158, 40 }, "#143#CLEAR");
  119. // Draw clipboard status
  120. GuiSetState(STATE_DISABLED);
  121. GuiLabel((Rectangle){ 50, 260, 700, 40 }, "Clipboard current text data:");
  122. GuiSetStyle(TEXTBOX, TEXT_READONLY, 1);
  123. GuiTextBox((Rectangle){ 50, 300, 700, 40 }, (char *)clipboardText, 256, false);
  124. GuiSetStyle(TEXTBOX, TEXT_READONLY, 0);
  125. GuiLabel((Rectangle){ 50, 360, 700, 40 }, "Try copying text from other applications and pasting here!");
  126. GuiSetState(STATE_NORMAL);
  127. EndDrawing();
  128. //----------------------------------------------------------------------------------
  129. }
  130. // De-Initialization
  131. //--------------------------------------------------------------------------------------
  132. CloseWindow(); // Close window and OpenGL context
  133. //--------------------------------------------------------------------------------------
  134. return 0;
  135. }