Clipboard.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <Rocket/Controls/Clipboard.h>
  28. #include <Rocket/Core/Types.h>
  29. #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. return clipboard_content;
  69. const Rocket::Core::word* clipboard_text = (const Rocket::Core::word*) GlobalLock(clipboard_data);
  70. if (clipboard_text)
  71. clipboard_content.Assign(clipboard_text);
  72. GlobalUnlock(clipboard_data);
  73. return clipboard_content;
  74. }
  75. else
  76. return content;
  77. #else
  78. return content;
  79. #endif
  80. }
  81. // Set the contents of the clipboard.
  82. void Clipboard::Set(const Core::WString& _content)
  83. {
  84. #if defined ROCKET_PLATFORM_WIN32
  85. if (GetHWND())
  86. {
  87. if (!OpenClipboard(GetHWND()))
  88. return;
  89. EmptyClipboard();
  90. Rocket::Core::String win32_content;
  91. _content.ToUTF8(win32_content);
  92. HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, win32_content.Length() + 1);
  93. // Replaced strcpy_s with a simple strcpy, because we know for sure it's big enough.
  94. strcpy((char*) clipboard_data, win32_content.CString());
  95. if (SetClipboardData(CF_TEXT, clipboard_data) == NULL)
  96. {
  97. CloseClipboard();
  98. GlobalFree(clipboard_data);
  99. }
  100. else
  101. CloseClipboard();
  102. }
  103. else
  104. content = _content;
  105. #else
  106. content = _content;
  107. #endif
  108. }
  109. #if defined ROCKET_PLATFORM_WIN32
  110. // Set the window handle of the application.
  111. void Clipboard::SetHWND(void* hwnd)
  112. {
  113. application_hwnd = (HWND) hwnd;
  114. }
  115. #endif
  116. }
  117. }