Clipboard.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Controls/Clipboard.h"
  29. #include "../../Include/RmlUi/Core/Types.h"
  30. #include "../../Include/RmlUi/Core/WString.h"
  31. #if defined RMLUI_PLATFORM_WIN32
  32. #include <windows.h>
  33. #endif
  34. namespace Rml {
  35. namespace Controls {
  36. #if defined RMLUI_PLATFORM_WIN32
  37. static HWND application_hwnd = NULL;
  38. static BOOL CALLBACK FindApplicationWindow(HWND hwnd, LPARAM process_id)
  39. {
  40. DWORD hwnd_pid;
  41. GetWindowThreadProcessId(hwnd, &hwnd_pid);
  42. if (hwnd_pid == (DWORD) process_id)
  43. {
  44. application_hwnd = hwnd;
  45. return FALSE;
  46. }
  47. return TRUE;
  48. }
  49. static HWND GetHWND()
  50. {
  51. if (application_hwnd != NULL)
  52. return application_hwnd;
  53. EnumWindows(FindApplicationWindow, GetCurrentProcessId());
  54. return application_hwnd;
  55. }
  56. #endif
  57. static Core::WString content;
  58. // Get the current contents of the clipboard.
  59. Core::WString Clipboard::Get()
  60. {
  61. #if defined RMLUI_PLATFORM_WIN32
  62. if (GetHWND())
  63. {
  64. Core::WString clipboard_content;
  65. if (!OpenClipboard(GetHWND()))
  66. return clipboard_content;
  67. HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
  68. if (clipboard_data == NULL)
  69. {
  70. CloseClipboard();
  71. return clipboard_content;
  72. }
  73. const Rml::Core::word* clipboard_text = (const Rml::Core::word*) GlobalLock(clipboard_data);
  74. if (clipboard_text)
  75. clipboard_content.Assign(clipboard_text);
  76. GlobalUnlock(clipboard_data);
  77. CloseClipboard();
  78. return clipboard_content;
  79. }
  80. else
  81. return content;
  82. #else
  83. return content;
  84. #endif
  85. }
  86. // Set the contents of the clipboard.
  87. void Clipboard::Set(const Core::WString& _content)
  88. {
  89. #if defined RMLUI_PLATFORM_WIN32
  90. if (GetHWND())
  91. {
  92. if (!OpenClipboard(GetHWND()))
  93. return;
  94. EmptyClipboard();
  95. Rml::Core::String win32_content;
  96. _content.ToUTF8(win32_content);
  97. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, win32_content.Length() + 1);
  98. // Replaced strcpy_s with a simple strcpy, because we know for sure it's big enough.
  99. strcpy((char*) clipboard_data, win32_content.CString());
  100. if (SetClipboardData(CF_TEXT, clipboard_data) == NULL)
  101. {
  102. CloseClipboard();
  103. GlobalFree(clipboard_data);
  104. }
  105. else
  106. CloseClipboard();
  107. }
  108. else
  109. content = _content;
  110. #else
  111. content = _content;
  112. #endif
  113. }
  114. #if defined RMLUI_PLATFORM_WIN32
  115. // Set the window handle of the application.
  116. void Clipboard::SetHWND(void* hwnd)
  117. {
  118. application_hwnd = (HWND) hwnd;
  119. }
  120. #endif
  121. }
  122. }