MyGUI 3.2.0

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

- File List
- Globals

MyGUI_ClipboardManager.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_ClipboardManager.h"
24 #include "MyGUI_Gui.h"
25 #include "MyGUI_TextIterator.h"
26
27 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
28 #include <windows.h>
29 #endif
30
31 namespace MyGUI
32 {
33
34 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
35
36 HWND g_hWnd = NULL;
37
38 BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
39 {
40 DWORD dwProcessID = 0;
41 GetWindowThreadProcessId(hWnd, &dwProcessID);
42
43 if (dwProcessID != (DWORD)lParam)
44 return TRUE;
45
46 if (GetParent(hWnd) == NULL)
47 {
48 // Нашли. hWnd - то что надо
49 g_hWnd = hWnd;
50 return FALSE;
51 }
52
53 return TRUE;
54 }
55
56 BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam)
57 {
58 DWORD dwProcessID = 0;
59 GetWindowThreadProcessId(hWnd, &dwProcessID);
60
61 if (dwProcessID != GetCurrentProcessId())
62 return TRUE;
63
64 if (GetWindowLongPtr(hWnd, GWLP_HINSTANCE) == lParam)
65 {
66 // Нашли. hWnd - то что надо
67 g_hWnd = hWnd;
68 return FALSE;
69 }
70
71 return TRUE;
72 }
73
74 #endif
75
76 template <> ClipboardManager* Singleton<ClipboardManager>::msInstance = nullptr;
77 template <> const char* Singleton<ClipboardManager>::mClassTypeName("ClipboardManager");
78
79 ClipboardManager::ClipboardManager() :
80 mIsInitialise(false)
81 {
82 }
83
84 void ClipboardManager::initialise()
85 {
86 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
87 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
88
89 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
90 // берем имя нашего экзешника
91 char buf[MAX_PATH];
92 GetModuleFileName(0, (LPCH)&buf, MAX_PATH);
93 // берем инстанс нашего модуля
94 HINSTANCE instance = GetModuleHandle(buf);
95
96 EnumChildWindows(GetDesktopWindow(), (WNDENUMPROC)EnumWindowProc, (LPARAM)instance);
97 mHwnd = (size_t)g_hWnd;
98
99 #endif
100
101 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
102 mIsInitialise = true;
103 }
104
105 void ClipboardManager::shutdown()
106 {
107 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
108 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
109
110 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
111 mIsInitialise = false;
112 }
113
114 void ClipboardManager::setClipboardData(const std::string& _type, const std::string& _data)
115 {
116 mClipboardData[_type] = _data;
117
118 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
119 if (_type == "Text")
120 {
121 mPutTextInClipboard = TextIterator::getOnlyText(UString(_data));
122 size_t size = (mPutTextInClipboard.size() + 1) * 2;
123 //открываем буфер обмена
124 if (OpenClipboard((HWND)mHwnd))
125 {
126 EmptyClipboard(); //очищаем буфер
127 HGLOBAL hgBuffer = GlobalAlloc(GMEM_DDESHARE, size);//выделяем память
128 wchar_t* chBuffer = hgBuffer ? (wchar_t*)GlobalLock(hgBuffer) : NULL;
129 if (chBuffer)
130 {
131 memcpy(chBuffer, mPutTextInClipboard.asWStr_c_str(), size);
132 GlobalUnlock(hgBuffer);//разблокируем память
133 SetClipboardData(CF_UNICODETEXT, hgBuffer);//помещаем текст в буфер обмена
134 }
135 CloseClipboard(); //закрываем буфер обмена
136 }
137 }
138 #endif
139 }
140
141 void ClipboardManager::clearClipboardData(const std::string& _type)
142 {
143 MapString::iterator iter = mClipboardData.find(_type);
144 if (iter != mClipboardData.end()) mClipboardData.erase(iter);
145 }
146
147 std::string ClipboardManager::getClipboardData(const std::string& _type)
148 {
149 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
150 if (_type == "Text")
151 {
152 UString buff;
153 //открываем буфер обмена
154 if (OpenClipboard((HWND)mHwnd))
155 {
156 HANDLE hData = GetClipboardData(CF_UNICODETEXT);//извлекаем текст из буфера обмена
157 wchar_t* chBuffer = hData ? (wchar_t*)GlobalLock(hData) : NULL;
158 if (chBuffer)
159 {
160 buff = chBuffer;
161 GlobalUnlock(hData);//разблокируем память
162 }
163 CloseClipboard();//закрываем буфер обмена
164 }
165 // если в буфере не то что мы ложили, то берем из буфера
166 if (mPutTextInClipboard != buff)
167 {
168 // вставляем теги, если нуно
169 const UString& text = TextIterator::toTagsString(buff);
170 return text.asUTF8();
171 }
172
173 MapString::iterator iter = mClipboardData.find(_type);
174 if (iter != mClipboardData.end()) return (*iter).second;
175 return "";
176 }
177 #endif
178
179 MapString::iterator iter = mClipboardData.find(_type);
180 if (iter != mClipboardData.end()) return (*iter).second;
181 return "";
182 }
183
184 } // namespace MyGUI
MyGUI::ClipboardManager::initialise
void initialise()
Definition: MyGUI_ClipboardManager.cpp:84
MyGUI_ClipboardManager.h
MyGUI_Precompiled.h
MyGUI::TextIterator::getOnlyText
static UString getOnlyText(const UString &_text)
Definition: MyGUI_TextIterator.cpp:221
MyGUI::ClipboardManager::ClipboardManager
ClipboardManager()
Definition: MyGUI_ClipboardManager.cpp:79
MyGUI::ClipboardManager::clearClipboardData
void clearClipboardData(const std::string &_type)
Definition: MyGUI_ClipboardManager.cpp:141
MyGUI::Singleton< ClipboardManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI::ClipboardManager::getClipboardData
std::string getClipboardData(const std::string &_type)
Definition: MyGUI_ClipboardManager.cpp:147
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI::ClipboardManager::setClipboardData
void setClipboardData(const std::string &_type, const std::string &_data)
Definition: MyGUI_ClipboardManager.cpp:114
MyGUI::TextIterator::toTagsString
static UString toTagsString(const UString &_text)
Definition: MyGUI_TextIterator.cpp:360
MyGUI::g_hWnd
HWND g_hWnd
Definition: MyGUI_ClipboardManager.cpp:36
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::EnumWindowProc
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
Definition: MyGUI_ClipboardManager.cpp:38
MyGUI::EnumChildWindowProc
BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam)
Definition: MyGUI_ClipboardManager.cpp:56
MyGUI::UString::asUTF8
const std::string & asUTF8() const
returns the current string in UTF-8 form within a std::string
Definition: MyGUI_UString.cpp:712
MyGUI::UString
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
Definition: MyGUI_UString.h:169
MyGUI_Gui.h
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI_TextIterator.h
MyGUI::ClipboardManager::shutdown
void shutdown()
Definition: MyGUI_ClipboardManager.cpp:105