MyGUI 3.5.3
MyGUI_WindowsClipboardHandler.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
9#include "MyGUI_Gui.h"
10#include "MyGUI_TextIterator.h"
12
13#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
14
15 #include <windows.h>
16
17namespace MyGUI
18{
19
20 HWND g_hWnd = nullptr;
21
22 BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
23 {
24 DWORD dwProcessID = 0;
25 GetWindowThreadProcessId(hWnd, &dwProcessID);
26
27 if (dwProcessID != (DWORD)lParam)
28 return TRUE;
29
30 if (GetParent(hWnd) == nullptr)
31 {
32 // Found. hWnd is what we need
33 g_hWnd = hWnd;
34 return FALSE;
35 }
36
37 return TRUE;
38 }
39
40 BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam)
41 {
42 DWORD dwProcessID = 0;
43 GetWindowThreadProcessId(hWnd, &dwProcessID);
44
45 if (dwProcessID != GetCurrentProcessId())
46 return TRUE;
47
48 if (GetWindowLongPtr(hWnd, GWLP_HINSTANCE) == lParam)
49 {
50 // Found. hWnd is what we need
51 g_hWnd = hWnd;
52 return FALSE;
53 }
54
55 return TRUE;
56 }
57
58 WindowsClipboardHandler::WindowsClipboardHandler()
59 {
60 }
61
62 void WindowsClipboardHandler::initialise()
63 {
64 // get executable path
65 char buf[MAX_PATH];
66 GetModuleFileName(0, (LPCH)&buf, MAX_PATH);
67 // get module instance
68 HINSTANCE instance = GetModuleHandle(buf);
69
70 EnumChildWindows(GetDesktopWindow(), (WNDENUMPROC)EnumWindowProc, (LPARAM)instance);
71 mHwnd = (size_t)g_hWnd;
72
73 ClipboardManager::getInstance().eventClipboardChanged +=
74 newDelegate(this, &WindowsClipboardHandler::handleClipboardChanged);
75 ClipboardManager::getInstance().eventClipboardRequested +=
76 newDelegate(this, &WindowsClipboardHandler::handleClipboardRequested);
77 }
78
79 void WindowsClipboardHandler::shutdown()
80 {
81 ClipboardManager::getInstance().eventClipboardChanged -=
82 newDelegate(this, &WindowsClipboardHandler::handleClipboardChanged);
83 ClipboardManager::getInstance().eventClipboardRequested -=
84 newDelegate(this, &WindowsClipboardHandler::handleClipboardRequested);
85 }
86
87 void WindowsClipboardHandler::handleClipboardChanged(std::string_view _type, std::string_view _data)
88 {
89 if (_type == "Text")
90 {
91 mPutTextInClipboard = TextIterator::getOnlyText(UString(_data));
92 const auto wideText = mPutTextInClipboard.asWStr();
93 size_t size = (wideText.size() + 1) * sizeof(wchar_t);
94 // open clipboard
95 if (OpenClipboard((HWND)mHwnd))
96 {
97 EmptyClipboard();
98 HGLOBAL hgBuffer = GlobalAlloc(GMEM_DDESHARE, size);
99 wchar_t* chBuffer = hgBuffer ? (wchar_t*)GlobalLock(hgBuffer) : nullptr;
100 if (chBuffer)
101 {
102 memcpy(chBuffer, wideText.c_str(), size);
103 GlobalUnlock(hgBuffer);
104 // put text in clipboard
105 SetClipboardData(CF_UNICODETEXT, hgBuffer);
106 }
107 CloseClipboard();
108 }
109 }
110 }
111
112 void WindowsClipboardHandler::handleClipboardRequested(std::string_view _type, std::string& _data)
113 {
114 if (_type == "Text")
115 {
116 UString buff;
117 // open clipboard
118 if (OpenClipboard((HWND)mHwnd))
119 {
120 struct ClipboardCloser
121 {
122 ~ClipboardCloser()
123 {
124 CloseClipboard();
125 }
126 } clipboardCloser;
127
128 HANDLE hData = GetClipboardData(CF_UNICODETEXT); // extract text from clipboard
129 wchar_t* chBuffer = hData ? (wchar_t*)GlobalLock(hData) : nullptr;
130 if (chBuffer)
131 {
132 struct ClipboardUnlocker
133 {
134 HANDLE handle;
135 ~ClipboardUnlocker()
136 {
137 GlobalUnlock(handle);
138 }
139 } clipboardUnlocker{hData};
140
141 buff = chBuffer;
142 }
143 }
144 // if the buffer doesn't contain what we put, take from buffer
145 if (mPutTextInClipboard != buff)
146 {
147 // insert tags if needed
148 const UString& text = TextIterator::toTagsString(buff);
149 _data = text.asUTF8();
150 }
151 }
152 }
153
154} // namespace MyGUI
155
156#endif // MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam)
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)