clipbrd.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 939 行 · 第 1/2 页
CPP
939 行
retval = s;
break;
}
default:
{
HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
if ( !hGlobalMemory )
break;
DWORD size = ::GlobalSize(hGlobalMemory);
if ( len )
*len = size;
void *buf = malloc(size);
if ( !buf )
break;
LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
memcpy(buf, lpGlobalMemory, size);
GlobalUnlock(hGlobalMemory);
retval = buf;
break;
}
}
if ( !retval )
{
wxLogSysError(_("Failed to retrieve data from the clipboard."));
}
return retval;
}
wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
{
return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
}
int wxRegisterClipboardFormat(wxChar *formatName)
{
return ::RegisterClipboardFormat(formatName);
}
bool wxGetClipboardFormatName(wxDataFormat dataFormat,
wxChar *formatName,
int maxCount)
{
return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
}
// ---------------------------------------------------------------------------
// wxClipboard
// ---------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
// the last object which we put on the clipboard
static IDataObject *gs_LastDataObject = NULL;
wxClipboard::wxClipboard()
{
#if wxUSE_OLE_CLIPBOARD
wxOleInitialize();
#endif
m_clearOnExit = false;
m_isOpened = false;
}
wxClipboard::~wxClipboard()
{
if ( m_clearOnExit )
{
Clear();
}
#if wxUSE_OLE_CLIPBOARD
wxOleUninitialize();
#endif
}
void wxClipboard::Clear()
{
#if wxUSE_OLE_CLIPBOARD
if ( gs_LastDataObject )
{
if ( OleIsCurrentClipboard(gs_LastDataObject) == S_OK )
{
HRESULT hr = OleSetClipboard(NULL);
if ( FAILED(hr) )
{
wxLogApiError(wxT("OleSetClipboard(NULL)"), hr);
}
}
gs_LastDataObject = NULL;
}
#endif // wxUSE_OLE_CLIPBOARD
}
bool wxClipboard::Flush()
{
#if wxUSE_OLE_CLIPBOARD
if ( gs_LastDataObject && OleIsCurrentClipboard(gs_LastDataObject) == S_OK )
{
HRESULT hr = OleFlushClipboard();
if ( FAILED(hr) )
{
wxLogApiError(wxT("OleFlushClipboard"), hr);
return false;
}
}
gs_LastDataObject = NULL;
return true;
#else // !wxUSE_OLE_CLIPBOARD
return false;
#endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
}
bool wxClipboard::Open()
{
// OLE opens clipboard for us
m_isOpened = true;
#if wxUSE_OLE_CLIPBOARD
return true;
#else
return wxOpenClipboard();
#endif
}
bool wxClipboard::IsOpened() const
{
#if wxUSE_OLE_CLIPBOARD
return m_isOpened;
#else
return wxIsClipboardOpened();
#endif
}
bool wxClipboard::SetData( wxDataObject *data )
{
#if !wxUSE_OLE_CLIPBOARD
(void)wxEmptyClipboard();
#endif // wxUSE_OLE_CLIPBOARD
if ( data )
return AddData(data);
else
return true;
}
bool wxClipboard::AddData( wxDataObject *data )
{
wxCHECK_MSG( data, false, wxT("data is invalid") );
#if wxUSE_OLE_CLIPBOARD
HRESULT hr = OleSetClipboard(data->GetInterface());
if ( FAILED(hr) )
{
wxLogSysError(hr, _("Failed to put data on the clipboard"));
// don't free anything in this case
return false;
}
// used to track whether the data in the clipboard is still this data
// otherwise don't call either OleSetClipboard(NULL) nor
// OleFlushClipboard() as it will interfere with other applications
gs_LastDataObject = data->GetInterface();
// we have a problem here because we should delete wxDataObject, but we
// can't do it because IDataObject which we just gave to the clipboard
// would try to use it when it will need the data. IDataObject is ref
// counted and so doesn't suffer from such problem, so we release it now
// and tell it to delete wxDataObject when it is deleted itself.
data->SetAutoDelete();
// we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
// using OLE clipboard when the app terminates - by default, we call
// OleSetClipboard(NULL) which won't waste RAM, but the app can call
// wxClipboard::Flush() to chaneg this
m_clearOnExit = true;
return true;
#elif wxUSE_DATAOBJ
wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
wxDataFormat format = data->GetPreferredFormat();
switch ( format )
{
case wxDF_TEXT:
case wxDF_OEMTEXT:
{
wxTextDataObject* textDataObject = (wxTextDataObject*) data;
wxString str(textDataObject->GetText());
return wxSetClipboardData(format, str.c_str());
}
case wxDF_BITMAP:
case wxDF_DIB:
{
wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
wxBitmap bitmap(bitmapDataObject->GetBitmap());
return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
}
#if wxUSE_METAFILE
case wxDF_METAFILE:
{
#if 1
// TODO
wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
return false;
#else
wxMetafileDataObject* metaFileDataObject =
(wxMetafileDataObject*) data;
wxMetafile metaFile = metaFileDataObject->GetMetafile();
return wxSetClipboardData(wxDF_METAFILE, &metaFile,
metaFileDataObject->GetWidth(),
metaFileDataObject->GetHeight());
#endif
}
#endif // wxUSE_METAFILE
default:
{
// This didn't compile, of course
// return wxSetClipboardData(data);
// TODO
wxLogError(wxT("Not implemented."));
return false;
}
}
#else // !wxUSE_DATAOBJ
return false;
#endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
}
void wxClipboard::Close()
{
m_isOpened = false;
// OLE closes clipboard for us
#if !wxUSE_OLE_CLIPBOARD
wxCloseClipboard();
#endif
}
bool wxClipboard::IsSupported( const wxDataFormat& format )
{
return wxIsClipboardFormatAvailable(format);
}
bool wxClipboard::GetData( wxDataObject& data )
{
#if wxUSE_OLE_CLIPBOARD
IDataObject *pDataObject = NULL;
HRESULT hr = OleGetClipboard(&pDataObject);
if ( FAILED(hr) || !pDataObject )
{
wxLogSysError(hr, _("Failed to get data from the clipboard"));
return false;
}
// build the list of supported formats
size_t nFormats = data.GetFormatCount(wxDataObject::Set);
wxDataFormat format;
wxDataFormat *formats;
if ( nFormats == 1 )
{
// the most common case
formats = &format;
}
else
{
// bad luck, need to alloc mem
formats = new wxDataFormat[nFormats];
}
data.GetAllFormats(formats, wxDataObject::Set);
// get the data for the given formats
FORMATETC formatEtc;
CLIPFORMAT cf;
bool result = false;
// enumerate all explicit formats on the clipboard.
// note that this does not include implicit / synthetic (automatically
// converted) formats.
#ifdef __WXDEBUG__
// get the format enumerator
IEnumFORMATETC *pEnumFormatEtc = NULL;
hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
if ( FAILED(hr) || !pEnumFormatEtc )
{
wxLogSysError(hr,
_("Failed to retrieve the supported clipboard formats"));
}
else
{
// ask for the supported formats and see if there are any we support
for ( ;; )
{
ULONG nCount;
hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
// don't use FAILED() because S_FALSE would pass it
if ( hr != S_OK )
{
// no more formats
break;
}
cf = formatEtc.cfFormat;
wxLogTrace(wxTRACE_OleCalls,
wxT("Object on the clipboard supports format %s."),
wxDataObject::GetFormatName(cf));
}
pEnumFormatEtc->Release();
}
#endif // Debug
STGMEDIUM medium;
// stop at the first valid format found on the clipboard
for ( size_t n = 0; !result && (n < nFormats); n++ )
{
// convert to NativeFormat Id
cf = formats[n].GetFormatId();
// if the format is not available, try the next one
// this test includes implicit / sythetic formats
if ( !::IsClipboardFormatAvailable(cf) )
continue;
formatEtc.cfFormat = cf;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
// use the appropriate tymed
switch ( formatEtc.cfFormat )
{
case CF_BITMAP:
formatEtc.tymed = TYMED_GDI;
break;
#ifndef __WXWINCE__
case CF_METAFILEPICT:
formatEtc.tymed = TYMED_MFPICT;
break;
case CF_ENHMETAFILE:
formatEtc.tymed = TYMED_ENHMF;
break;
#endif
default:
formatEtc.tymed = TYMED_HGLOBAL;
}
// try to get data
hr = pDataObject->GetData(&formatEtc, &medium);
if ( FAILED(hr) )
{
// try other tymed for GDI objects
if ( formatEtc.cfFormat == CF_BITMAP )
{
formatEtc.tymed = TYMED_HGLOBAL;
hr = pDataObject->GetData(&formatEtc, &medium);
}
}
if ( SUCCEEDED(hr) )
{
// pass the data to the data object
hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
if ( FAILED(hr) )
{
wxLogDebug(wxT("Failed to set data in wxIDataObject"));
// IDataObject only takes the ownership of data if it
// successfully got it - which is not the case here
ReleaseStgMedium(&medium);
}
else
{
result = true;
}
}
//else: unsupported tymed?
}
if ( formats != &format )
{
delete [] formats;
}
//else: we didn't allocate any memory
// clean up and return
pDataObject->Release();
return result;
#elif wxUSE_DATAOBJ
wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
wxDataFormat format = data.GetPreferredFormat();
switch ( format )
{
case wxDF_TEXT:
case wxDF_OEMTEXT:
{
wxTextDataObject& textDataObject = (wxTextDataObject &)data;
char* s = (char*)wxGetClipboardData(format);
if ( !s )
return false;
textDataObject.SetText(wxString::FromAscii(s));
delete [] s;
return true;
}
case wxDF_BITMAP:
case wxDF_DIB:
{
wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
if ( !bitmap )
return false;
bitmapDataObject.SetBitmap(*bitmap);
delete bitmap;
return true;
}
#if wxUSE_METAFILE
case wxDF_METAFILE:
{
wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
if ( !metaFile )
return false;
metaFileDataObject.SetMetafile(*metaFile);
delete metaFile;
return true;
}
#endif // wxUSE_METAFILE
}
return false;
#else // !wxUSE_DATAOBJ
wxFAIL_MSG( wxT("no clipboard implementation") );
return false;
#endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
}
#endif // wxUSE_CLIPBOARD
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?