SDL_windowsclipboard.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINDOWS
  20. #include "SDL_windowsvideo.h"
  21. #include "SDL_windowswindow.h"
  22. #include "../../events/SDL_clipboardevents_c.h"
  23. #ifdef UNICODE
  24. #define TEXT_FORMAT CF_UNICODETEXT
  25. #else
  26. #define TEXT_FORMAT CF_TEXT
  27. #endif
  28. /* Get any application owned window handle for clipboard association */
  29. static HWND
  30. GetWindowHandle(_THIS)
  31. {
  32. SDL_Window *window;
  33. window = _this->windows;
  34. if (window) {
  35. return ((SDL_WindowData *) window->driverdata)->hwnd;
  36. }
  37. return NULL;
  38. }
  39. int
  40. WIN_SetClipboardText(_THIS, const char *text)
  41. {
  42. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  43. int result = 0;
  44. if (OpenClipboard(GetWindowHandle(_this))) {
  45. HANDLE hMem;
  46. LPTSTR tstr;
  47. SIZE_T i, size;
  48. /* Convert the text from UTF-8 to Windows Unicode */
  49. tstr = WIN_UTF8ToString(text);
  50. if (!tstr) {
  51. return -1;
  52. }
  53. /* Find out the size of the data */
  54. for (size = 0, i = 0; tstr[i]; ++i, ++size) {
  55. if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
  56. /* We're going to insert a carriage return */
  57. ++size;
  58. }
  59. }
  60. size = (size+1)*sizeof(*tstr);
  61. /* Save the data to the clipboard */
  62. hMem = GlobalAlloc(GMEM_MOVEABLE, size);
  63. if (hMem) {
  64. LPTSTR dst = (LPTSTR)GlobalLock(hMem);
  65. if (dst) {
  66. /* Copy the text over, adding carriage returns as necessary */
  67. for (i = 0; tstr[i]; ++i) {
  68. if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
  69. *dst++ = '\r';
  70. }
  71. *dst++ = tstr[i];
  72. }
  73. *dst = 0;
  74. GlobalUnlock(hMem);
  75. }
  76. EmptyClipboard();
  77. if (!SetClipboardData(TEXT_FORMAT, hMem)) {
  78. result = WIN_SetError("Couldn't set clipboard data");
  79. }
  80. data->clipboard_count = GetClipboardSequenceNumber();
  81. }
  82. SDL_free(tstr);
  83. CloseClipboard();
  84. } else {
  85. result = WIN_SetError("Couldn't open clipboard");
  86. }
  87. return result;
  88. }
  89. char *
  90. WIN_GetClipboardText(_THIS)
  91. {
  92. char *text;
  93. text = NULL;
  94. if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
  95. OpenClipboard(GetWindowHandle(_this))) {
  96. HANDLE hMem;
  97. LPTSTR tstr;
  98. hMem = GetClipboardData(TEXT_FORMAT);
  99. if (hMem) {
  100. tstr = (LPTSTR)GlobalLock(hMem);
  101. text = WIN_StringToUTF8(tstr);
  102. GlobalUnlock(hMem);
  103. } else {
  104. WIN_SetError("Couldn't get clipboard data");
  105. }
  106. CloseClipboard();
  107. }
  108. if (!text) {
  109. text = SDL_strdup("");
  110. }
  111. return text;
  112. }
  113. SDL_bool
  114. WIN_HasClipboardText(_THIS)
  115. {
  116. SDL_bool result = SDL_FALSE;
  117. char *text = WIN_GetClipboardText(_this);
  118. if (text) {
  119. result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
  120. SDL_free(text);
  121. }
  122. return result;
  123. }
  124. void
  125. WIN_CheckClipboardUpdate(struct SDL_VideoData * data)
  126. {
  127. const DWORD count = GetClipboardSequenceNumber();
  128. if (count != data->clipboard_count) {
  129. if (data->clipboard_count) {
  130. SDL_SendClipboardUpdate();
  131. }
  132. data->clipboard_count = count;
  133. }
  134. }
  135. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  136. /* vi: set ts=4 sw=4 expandtab: */