Clipboard.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #if defined ROCKET_PLATFORM_WIN32
  30. #include <windows.h>
  31. #endif
  32. namespace Rocket {
  33. namespace Controls {
  34. #if defined ROCKET_PLATFORM_WIN32
  35. static HWND application_hwnd = NULL;
  36. static BOOL CALLBACK FindApplicationWindow(HWND hwnd, LPARAM process_id)
  37. {
  38. DWORD hwnd_pid;
  39. GetWindowThreadProcessId(hwnd, &hwnd_pid);
  40. if (hwnd_pid == (DWORD) process_id)
  41. {
  42. application_hwnd = hwnd;
  43. return FALSE;
  44. }
  45. return TRUE;
  46. }
  47. static HWND GetHWND()
  48. {
  49. if (application_hwnd != NULL)
  50. return application_hwnd;
  51. EnumWindows(FindApplicationWindow, GetCurrentProcessId());
  52. return application_hwnd;
  53. }
  54. #endif
  55. static Core::WString content;
  56. // Get the current contents of the clipboard.
  57. Core::WString Clipboard::Get()
  58. {
  59. #if defined ROCKET_PLATFORM_WIN32
  60. if (GetHWND())
  61. {
  62. Core::WString clipboard_content;
  63. if (!OpenClipboard(GetHWND()))
  64. return clipboard_content;
  65. HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
  66. if (clipboard_data == NULL)
  67. {
  68. CloseClipboard();
  69. return clipboard_content;
  70. }
  71. const Rocket::Core::word* clipboard_text = (const Rocket::Core::word*) GlobalLock(clipboard_data);
  72. if (clipboard_text)
  73. clipboard_content = clipboard_text;
  74. GlobalUnlock(clipboard_data);
  75. CloseClipboard();
  76. return clipboard_content;
  77. }
  78. else
  79. return content;
  80. #else
  81. return content;
  82. #endif
  83. }
  84. // Set the contents of the clipboard.
  85. void Clipboard::Set(const Core::WString& _content)
  86. {
  87. #if defined ROCKET_PLATFORM_WIN32
  88. if (GetHWND())
  89. {
  90. if (!OpenClipboard(GetHWND()))
  91. return;
  92. EmptyClipboard();
  93. Rocket::Core::String win32_content;
  94. win32_content = Core::ToUTF8(_content);
  95. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, win32_content.size() + 1);
  96. // Replaced strcpy_s with a simple strcpy, because we know for sure it's big enough.
  97. strcpy((char*) clipboard_data, win32_content.c_str());
  98. if (SetClipboardData(CF_TEXT, clipboard_data) == NULL)
  99. {
  100. CloseClipboard();
  101. GlobalFree(clipboard_data);
  102. }
  103. else
  104. CloseClipboard();
  105. }
  106. else
  107. content = _content;
  108. #else
  109. content = _content;
  110. #endif
  111. }
  112. #if defined ROCKET_PLATFORM_WIN32
  113. // Set the window handle of the application.
  114. void Clipboard::SetHWND(void* hwnd)
  115. {
  116. application_hwnd = (HWND) hwnd;
  117. }
  118. #endif
  119. }
  120. }