bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) { wxDataFormat::NativeFormat cf = dataFormat.GetFormatId(); if ( ::IsClipboardFormatAvailable(cf) ) { // ok from the first try return true; } // for several standard formats, we can convert from some other ones too switch ( cf ) { // for bitmaps, DIBs will also do case CF_BITMAP: return ::IsClipboardFormatAvailable(CF_DIB) != 0; #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) case CF_METAFILEPICT: return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0; #endif // wxUSE_ENH_METAFILE default: return false; } }
size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const { if ( format.GetFormatId() == CF_DIB ) { // create the DIB ScreenHDC hdc; // shouldn't be selected into a DC or GetDIBits() would fail wxASSERT_MSG( !m_bitmap.GetSelectedInto(), wxT("can't copy bitmap selected into wxMemoryDC") ); // first get the info BITMAPINFO bi; if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0, NULL, &bi, DIB_RGB_COLORS) ) { wxLogLastError(wxT("GetDIBits(NULL)")); return 0; } return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage; } else // CF_BITMAP { // no data to copy - we don't pass HBITMAP via global memory return 0; } }
/////////////////////////////////////////////////////////////////////////////// // Provides support for multiple clipboard formats. // bool ClipboardDataObject::SetData( const wxDataFormat& format, size_t len, const void* buf ) { bool result = false; if ( format == GetFormat() ) { // Editor clipboard format result = wxCustomDataObject::SetData( format, len, buf ); } else if ( format.IsStandard() && format.GetFormatId() == wxDF_FILENAME ) { // File name list format. Convert to our own type of filename list. wxFileDataObject fileData; fileData.SetData( len, buf ); if ( fileData.GetFilenames().size() > 0 ) { ClipboardFileListPtr fileList = new ClipboardFileList(); wxArrayString::const_iterator fileItr = fileData.GetFilenames().begin(); wxArrayString::const_iterator fileEnd = fileData.GetFilenames().end(); for ( ; fileItr != fileEnd; ++fileItr ) { const wxChar* file = fileItr->c_str(); fileList->AddFilePath( file ); } result = ToBuffer( fileList ); } } return result; }
bool wxBitmapDataObject::SetData(const wxDataFormat& format, size_t size, const void *pBuf) { HBITMAP hbmp; if ( format.GetFormatId() == CF_DIB ) { // here we get BITMAPINFO struct followed by the actual bitmap bits and // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info ScreenHDC hdc; BITMAPINFO *pbmi = (BITMAPINFO *)pBuf; BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader; hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT, pbmi + 1, pbmi, DIB_RGB_COLORS); if ( !hbmp ) { wxLogLastError(wxT("CreateDIBitmap")); } m_bitmap.SetWidth(pbmih->biWidth); m_bitmap.SetHeight(pbmih->biHeight); } else // CF_BITMAP { // it's easy with bitmaps: we pass them by handle hbmp = *(HBITMAP *)pBuf; BITMAP bmp; if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) ) { wxLogLastError(wxT("GetObject(HBITMAP)")); } m_bitmap.SetWidth(bmp.bmWidth); m_bitmap.SetHeight(bmp.bmHeight); m_bitmap.SetDepth(bmp.bmPlanes); } m_bitmap.SetHBITMAP((WXHBITMAP)hbmp); wxASSERT_MSG( m_bitmap.IsOk(), wxT("pasting invalid bitmap") ); return true; }
bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, void *pBuf) const { wxASSERT_MSG( m_bitmap.IsOk(), wxT("copying invalid bitmap") ); HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP(); if ( format.GetFormatId() == CF_DIB ) { // create the DIB ScreenHDC hdc; // shouldn't be selected into a DC or GetDIBits() would fail wxASSERT_MSG( !m_bitmap.GetSelectedInto(), wxT("can't copy bitmap selected into wxMemoryDC") ); // first get the info BITMAPINFO *pbi = (BITMAPINFO *)pBuf; if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) ) { wxLogLastError(wxT("GetDIBits(NULL)")); return 0; } // and now copy the bits if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1, pbi, DIB_RGB_COLORS) ) { wxLogLastError(wxT("GetDIBits")); return false; } } else // CF_BITMAP { // we put a bitmap handle into pBuf *(HBITMAP *)pBuf = hbmp; } return true; }