Clipboard.cpp 3.6 KB

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