00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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 }