MyGUI: MyGUI_WindowsClipboardHandler.cpp Source File

MyGUI 3.4.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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"
8#include "MyGUI_ClipboardManager.h"
9#include "MyGUI_Gui.h"
10#include "MyGUI_TextIterator.h"
11#include "MyGUI_WindowsClipboardHandler.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 // Нашли. hWnd - то что надо
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 // Нашли. hWnd - то что надо
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 += newDelegate(this, &WindowsClipboardHandler::handleClipboardChanged);
74 ClipboardManager::getInstance().eventClipboardRequested += newDelegate(this, &WindowsClipboardHandler::handleClipboardRequested);
75 }
76
77 void WindowsClipboardHandler::shutdown()
78 {
79 ClipboardManager::getInstance().eventClipboardChanged -= newDelegate(this, &WindowsClipboardHandler::handleClipboardChanged);
80 ClipboardManager::getInstance().eventClipboardRequested -= newDelegate(this, &WindowsClipboardHandler::handleClipboardRequested);
81 }
82
83 void WindowsClipboardHandler::handleClipboardChanged(const std::string& _type, const std::string& _data)
84 {
85 if (_type == "Text")
86 {
87 mPutTextInClipboard = TextIterator::getOnlyText(UString(_data));
88 size_t size = (mPutTextInClipboard.size() + 1) * 2;
89 //открываем буфер обмена
90 if (OpenClipboard((HWND)mHwnd))
91 {
92 EmptyClipboard(); //очищаем буфер
93 HGLOBAL hgBuffer = GlobalAlloc(GMEM_DDESHARE, size);//выделяем память
94 wchar_t* chBuffer = hgBuffer ? (wchar_t*)GlobalLock(hgBuffer) : nullptr;
95 if (chBuffer)
96 {
97 memcpy(chBuffer, mPutTextInClipboard.asWStr_c_str(), size);
98 GlobalUnlock(hgBuffer);//разблокируем память
99 SetClipboardData(CF_UNICODETEXT, hgBuffer);//помещаем текст в буфер обмена
100 }
101 CloseClipboard(); //закрываем буфер обмена
102 }
103 }
104 }
105
106 void WindowsClipboardHandler::handleClipboardRequested(const std::string& _type, std::string& _data)
107 {
108 if (_type == "Text")
109 {
110 UString buff;
111 //открываем буфер обмена
112 if (OpenClipboard((HWND)mHwnd))
113 {
114 HANDLE hData = GetClipboardData(CF_UNICODETEXT);//извлекаем текст из буфера обмена
115 wchar_t* chBuffer = hData ? (wchar_t*)GlobalLock(hData) : nullptr;
116 if (chBuffer)
117 {
118 buff = chBuffer;
119 GlobalUnlock(hData);//разблокируем память
120 }
121 CloseClipboard();//закрываем буфер обмена
122 }
123 // если в буфере не то что мы ложили, то берем из буфера
124 if (mPutTextInClipboard != buff)
125 {
126 // вставляем теги, если нуно
127 const UString& text = TextIterator::toTagsString(buff);
128 _data = text.asUTF8();
129 }
130 }
131 }
132
133} // namespace MyGUI
134
135#endif // MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
MyGUI_ClipboardManager.h
MyGUI_Gui.h
MyGUI_Precompiled.h
MyGUI_TextIterator.h
MyGUI_WindowsClipboardHandler.h
MyGUI::ClipboardManager::eventClipboardRequested
delegates::CMultiDelegate2< const std::string &, std::string & > eventClipboardRequested
Definition MyGUI_ClipboardManager.h:59
MyGUI::ClipboardManager::eventClipboardChanged
delegates::CMultiDelegate2< const std::string &, const std::string & > eventClipboardChanged
Definition MyGUI_ClipboardManager.h:51
MyGUI::ClipboardManager::getInstance
static ClipboardManager & getInstance()
MyGUI::TextIterator::getOnlyText
static UString getOnlyText(const UString &_text)
Definition MyGUI_TextIterator.cpp:205
MyGUI::TextIterator::toTagsString
static UString toTagsString(const UString &_text)
Definition MyGUI_TextIterator.cpp:358
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::EnumChildWindowProc
BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam)
Definition MyGUI_WindowsClipboardHandler.cpp:40
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:99
MyGUI::EnumWindowProc
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
Definition MyGUI_WindowsClipboardHandler.cpp:22
MyGUI::g_hWnd
HWND g_hWnd
Definition MyGUI_WindowsClipboardHandler.cpp:20