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