00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CMSWindowsClipboard.h"
00016 #include "CMSWindowsClipboardTextConverter.h"
00017 #include "CMSWindowsClipboardUTF16Converter.h"
00018 #include "CMSWindowsClipboardBitmapConverter.h"
00019 #include "CMSWindowsClipboardHTMLConverter.h"
00020 #include "CLog.h"
00021 #include "CArchMiscWindows.h"
00022
00023
00024
00025
00026
00027 UINT CMSWindowsClipboard::s_ownershipFormat = 0;
00028
00029 CMSWindowsClipboard::CMSWindowsClipboard(HWND window) :
00030 m_window(window),
00031 m_time(0)
00032 {
00033
00034 m_converters.push_back(new CMSWindowsClipboardUTF16Converter);
00035 if (CArchMiscWindows::isWindows95Family()) {
00036
00037
00038 m_converters.push_back(new CMSWindowsClipboardTextConverter);
00039 }
00040 m_converters.push_back(new CMSWindowsClipboardBitmapConverter);
00041 m_converters.push_back(new CMSWindowsClipboardHTMLConverter);
00042 }
00043
00044 CMSWindowsClipboard::~CMSWindowsClipboard()
00045 {
00046 clearConverters();
00047 }
00048
00049 bool
00050 CMSWindowsClipboard::emptyUnowned()
00051 {
00052 LOG((CLOG_DEBUG "empty clipboard"));
00053
00054
00055 if (!EmptyClipboard()) {
00056 LOG((CLOG_DEBUG "failed to grab clipboard"));
00057 return false;
00058 }
00059
00060 return true;
00061 }
00062
00063 bool
00064 CMSWindowsClipboard::empty()
00065 {
00066 if (!emptyUnowned()) {
00067 return false;
00068 }
00069
00070
00071 HGLOBAL data = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 1);
00072 SetClipboardData(getOwnershipFormat(), data);
00073
00074 return true;
00075 }
00076
00077 void
00078 CMSWindowsClipboard::add(EFormat format, const CString& data)
00079 {
00080 LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));
00081
00082
00083 for (ConverterList::const_iterator index = m_converters.begin();
00084 index != m_converters.end(); ++index) {
00085 IMSWindowsClipboardConverter* converter = *index;
00086
00087
00088 if (converter->getFormat() == format) {
00089 HANDLE win32Data = converter->fromIClipboard(data);
00090 if (win32Data != NULL) {
00091 UINT win32Format = converter->getWin32Format();
00092 if (SetClipboardData(win32Format, win32Data) == NULL) {
00093
00094
00095 GlobalFree(win32Data);
00096 }
00097 }
00098 }
00099 }
00100 }
00101
00102 bool
00103 CMSWindowsClipboard::open(Time time) const
00104 {
00105 LOG((CLOG_DEBUG "open clipboard"));
00106
00107 if (!OpenClipboard(m_window)) {
00108 LOG((CLOG_WARN "failed to open clipboard"));
00109 return false;
00110 }
00111
00112 m_time = time;
00113
00114 return true;
00115 }
00116
00117 void
00118 CMSWindowsClipboard::close() const
00119 {
00120 LOG((CLOG_DEBUG "close clipboard"));
00121 CloseClipboard();
00122 }
00123
00124 IClipboard::Time
00125 CMSWindowsClipboard::getTime() const
00126 {
00127 return m_time;
00128 }
00129
00130 bool
00131 CMSWindowsClipboard::has(EFormat format) const
00132 {
00133 for (ConverterList::const_iterator index = m_converters.begin();
00134 index != m_converters.end(); ++index) {
00135 IMSWindowsClipboardConverter* converter = *index;
00136 if (converter->getFormat() == format) {
00137 if (IsClipboardFormatAvailable(converter->getWin32Format())) {
00138 return true;
00139 }
00140 }
00141 }
00142 return false;
00143 }
00144
00145 CString
00146 CMSWindowsClipboard::get(EFormat format) const
00147 {
00148
00149 IMSWindowsClipboardConverter* converter = NULL;
00150 UINT win32Format = EnumClipboardFormats(0);
00151 while (converter == NULL && win32Format != 0) {
00152 for (ConverterList::const_iterator index = m_converters.begin();
00153 index != m_converters.end(); ++index) {
00154 converter = *index;
00155 if (converter->getWin32Format() == win32Format &&
00156 converter->getFormat() == format) {
00157 break;
00158 }
00159 converter = NULL;
00160 }
00161 win32Format = EnumClipboardFormats(win32Format);
00162 }
00163
00164
00165 if (converter == NULL) {
00166 return CString();
00167 }
00168
00169
00170 HANDLE win32Data = GetClipboardData(converter->getWin32Format());
00171 if (win32Data == NULL) {
00172 return CString();
00173 }
00174
00175
00176 return converter->toIClipboard(win32Data);
00177 }
00178
00179 void
00180 CMSWindowsClipboard::clearConverters()
00181 {
00182 for (ConverterList::iterator index = m_converters.begin();
00183 index != m_converters.end(); ++index) {
00184 delete *index;
00185 }
00186 m_converters.clear();
00187 }
00188
00189 bool
00190 CMSWindowsClipboard::isOwnedBySynergy()
00191 {
00192
00193 if (s_ownershipFormat == 0) {
00194 s_ownershipFormat = RegisterClipboardFormat(TEXT("SynergyOwnership"));
00195 }
00196 return (IsClipboardFormatAvailable(getOwnershipFormat()) != 0);
00197 }
00198
00199 UINT
00200 CMSWindowsClipboard::getOwnershipFormat()
00201 {
00202
00203 if (s_ownershipFormat == 0) {
00204 s_ownershipFormat = RegisterClipboardFormat(TEXT("SynergyOwnership"));
00205 }
00206
00207
00208 return s_ownershipFormat;
00209 }