MyGUI
3.2.0
|
00001 00006 /* 00007 This file is part of MyGUI. 00008 00009 MyGUI is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU Lesser General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 MyGUI is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License 00020 along with MyGUI. If not, see <http://www.gnu.org/licenses/>. 00021 */ 00022 #include "MyGUI_Precompiled.h" 00023 #include "MyGUI_ClipboardManager.h" 00024 #include "MyGUI_Gui.h" 00025 #include "MyGUI_TextIterator.h" 00026 00027 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32 00028 #include <windows.h> 00029 #endif 00030 00031 namespace MyGUI 00032 { 00033 00034 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32 00035 00036 HWND g_hWnd = NULL; 00037 00038 BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam) 00039 { 00040 DWORD dwProcessID = 0; 00041 GetWindowThreadProcessId(hWnd, &dwProcessID); 00042 00043 if (dwProcessID != (DWORD)lParam) 00044 return TRUE; 00045 00046 if (GetParent(hWnd) == NULL) 00047 { 00048 // Нашли. hWnd - то что надо 00049 g_hWnd = hWnd; 00050 return FALSE; 00051 } 00052 00053 return TRUE; 00054 } 00055 00056 BOOL CALLBACK EnumChildWindowProc(HWND hWnd, LPARAM lParam) 00057 { 00058 DWORD dwProcessID = 0; 00059 GetWindowThreadProcessId(hWnd, &dwProcessID); 00060 00061 if (dwProcessID != GetCurrentProcessId()) 00062 return TRUE; 00063 00064 if (GetWindowLongPtr(hWnd, GWLP_HINSTANCE) == lParam) 00065 { 00066 // Нашли. hWnd - то что надо 00067 g_hWnd = hWnd; 00068 return FALSE; 00069 } 00070 00071 return TRUE; 00072 } 00073 00074 #endif 00075 00076 template <> ClipboardManager* Singleton<ClipboardManager>::msInstance = nullptr; 00077 template <> const char* Singleton<ClipboardManager>::mClassTypeName("ClipboardManager"); 00078 00079 ClipboardManager::ClipboardManager() : 00080 mIsInitialise(false) 00081 { 00082 } 00083 00084 void ClipboardManager::initialise() 00085 { 00086 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice"); 00087 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName()); 00088 00089 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32 00090 // берем имя нашего экзешника 00091 char buf[MAX_PATH]; 00092 GetModuleFileName(0, (LPCH)&buf, MAX_PATH); 00093 // берем инстанс нашего модуля 00094 HINSTANCE instance = GetModuleHandle(buf); 00095 00096 EnumChildWindows(GetDesktopWindow(), (WNDENUMPROC)EnumWindowProc, (LPARAM)instance); 00097 mHwnd = (size_t)g_hWnd; 00098 00099 #endif 00100 00101 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized"); 00102 mIsInitialise = true; 00103 } 00104 00105 void ClipboardManager::shutdown() 00106 { 00107 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised"); 00108 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName()); 00109 00110 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown"); 00111 mIsInitialise = false; 00112 } 00113 00114 void ClipboardManager::setClipboardData(const std::string& _type, const std::string& _data) 00115 { 00116 mClipboardData[_type] = _data; 00117 00118 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32 00119 if (_type == "Text") 00120 { 00121 mPutTextInClipboard = TextIterator::getOnlyText(UString(_data)); 00122 size_t size = (mPutTextInClipboard.size() + 1) * 2; 00123 //открываем буфер обмена 00124 if (OpenClipboard((HWND)mHwnd)) 00125 { 00126 EmptyClipboard(); //очищаем буфер 00127 HGLOBAL hgBuffer = GlobalAlloc(GMEM_DDESHARE, size);//выделяем память 00128 wchar_t* chBuffer = hgBuffer ? (wchar_t*)GlobalLock(hgBuffer) : NULL; 00129 if (chBuffer) 00130 { 00131 memcpy(chBuffer, mPutTextInClipboard.asWStr_c_str(), size); 00132 GlobalUnlock(hgBuffer);//разблокируем память 00133 SetClipboardData(CF_UNICODETEXT, hgBuffer);//помещаем текст в буфер обмена 00134 } 00135 CloseClipboard(); //закрываем буфер обмена 00136 } 00137 } 00138 #endif 00139 } 00140 00141 void ClipboardManager::clearClipboardData(const std::string& _type) 00142 { 00143 MapString::iterator iter = mClipboardData.find(_type); 00144 if (iter != mClipboardData.end()) mClipboardData.erase(iter); 00145 } 00146 00147 std::string ClipboardManager::getClipboardData(const std::string& _type) 00148 { 00149 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32 00150 if (_type == "Text") 00151 { 00152 UString buff; 00153 //открываем буфер обмена 00154 if (OpenClipboard((HWND)mHwnd)) 00155 { 00156 HANDLE hData = GetClipboardData(CF_UNICODETEXT);//извлекаем текст из буфера обмена 00157 wchar_t* chBuffer = hData ? (wchar_t*)GlobalLock(hData) : NULL; 00158 if (chBuffer) 00159 { 00160 buff = chBuffer; 00161 GlobalUnlock(hData);//разблокируем память 00162 } 00163 CloseClipboard();//закрываем буфер обмена 00164 } 00165 // если в буфере не то что мы ложили, то берем из буфера 00166 if (mPutTextInClipboard != buff) 00167 { 00168 // вставляем теги, если нуно 00169 const UString& text = TextIterator::toTagsString(buff); 00170 return text.asUTF8(); 00171 } 00172 00173 MapString::iterator iter = mClipboardData.find(_type); 00174 if (iter != mClipboardData.end()) return (*iter).second; 00175 return ""; 00176 } 00177 #endif 00178 00179 MapString::iterator iter = mClipboardData.find(_type); 00180 if (iter != mClipboardData.end()) return (*iter).second; 00181 return ""; 00182 } 00183 00184 } // namespace MyGUI