bool wxClipboard::GetData( wxDataObject& data ) { if ( IsUsingPrimarySelection() ) return false; wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); size_t formatcount = data.GetFormatCount() + 1; wxDataFormat *array = new wxDataFormat[ formatcount ]; array[0] = data.GetPreferredFormat(); data.GetAllFormats( &array[1] ); bool transferred = false; if ( m_data ) { for (size_t i = 0; !transferred && i < formatcount; i++) { wxDataFormat format = array[ i ]; if ( m_data->IsSupported( format ) ) { int dataSize = m_data->GetDataSize( format ); transferred = true; if (dataSize == 0) { data.SetData( format, 0, 0 ); } else { char *d = new char[ dataSize ]; m_data->GetDataHere( format, (void*)d ); data.SetData( format, dataSize, d ); delete [] d; } } } } // get formats from wxDataObjects if ( !transferred ) { transferred = data.GetFromPasteboard( m_pasteboard ) ; } delete [] array; return transferred; }
// get the data for a specific wxDataFormat that stored as a property in Root window void GetClipboardData(Display* disp, Window win, wxDataObject &data, wxDataFormat dfFormat) { unsigned char *clipbrdData = NULL; // some variables that used to get the data in window property unsigned long len; switch ( dfFormat ) { case wxDF_INVALID: { return; } case wxDF_BITMAP: { wxVector<Atom> atomVector; atomVector.push_back(XA_IMAGE_BMP); atomVector.push_back(XA_IMAGE_JPG); atomVector.push_back(XA_IMAGE_TIFF); atomVector.push_back(XA_IMAGE_PNG); // check the four atoms in clipboard, try to find whether there has data // stored in one of these atom. for ( unsigned i = 0; i < atomVector.size(); i++ ) { clipbrdData = GetClipboardDataByFormat(disp, win, XA_CLIPBOARD, atomVector.at(i), &len); if ( clipbrdData != NULL ) break; } // if we got any data, copy it. if ( clipbrdData ) { data.SetData(dfFormat, len, (char*)clipbrdData); } break; } default: { clipbrdData = GetClipboardDataByFormat(disp, win, XA_CLIPBOARD, XA_UTF8_STRING, &len); // if we got any data, copy it. if ( clipbrdData ) { data.SetData(dfFormat, len, (char*)clipbrdData); } } } }
void CIDataObject::SetData ( const wxDataFormat& rFormat , char* pzBuffer ) { ULONG ulSize = 0; switch (rFormat.GetType()) { case wxDF_TEXT: case wxDF_OEMTEXT: case wxDF_FILENAME: case wxDF_HTML: ulSize = strlen((const char *)pzBuffer); break; #if wxUSE_UNICODE case wxDF_UNICODETEXT: ulSize = ::wcslen((const wchar_t *)pzBuffer); break; #endif case wxDF_BITMAP: case wxDF_METAFILE: case wxDF_ENHMETAFILE: case wxDF_TIFF: case wxDF_DIB: ulSize = 0; // pass via a handle break; case wxDF_SYLK: case wxDF_DIF: case wxDF_PALETTE: case wxDF_PENDATA: case wxDF_RIFF: case wxDF_WAVE: case wxDF_LOCALE: //PUNT break; case wxDF_PRIVATE: size_t* p = (size_t *)pzBuffer; ulSize = *p++; pzBuffer = (char*)p; break; } m_pDataObject->SetData( rFormat ,ulSize ,(void*)pzBuffer ); } // end of CIDataObject::SetData
bool wxClipboard::GetData( wxDataObject& data ) { wxCHECK_MSG( m_open, false, "clipboard not open" ); Display* xdisplay = wxGlobalDisplay(); Window xwindow = XtWindow( (Widget)wxTheApp->GetTopLevelRealizedWidget() ); Time timestamp = XtLastTimestampProcessed( xdisplay ); wxDataFormat chosenFormat; int retval; /////////////////////////////////////////////////////////////////////////// // determine if the cliboard holds any format we like /////////////////////////////////////////////////////////////////////////// while( ( retval = XmClipboardStartRetrieve( xdisplay, xwindow, timestamp ) ) == XmClipboardLocked ); if( retval != XmClipboardSuccess ) return false; wxClipboardEndRetrieve endRetrieve( xdisplay, xwindow ); int count; unsigned long max_name_length; size_t dfcount = data.GetFormatCount( wxDataObject::Set ); wxDataFormatScopedArray dfarr(dfcount); data.GetAllFormats( dfarr.get(), wxDataObject::Set ); if( XmClipboardInquireCount( xdisplay, xwindow, &count, &max_name_length ) == XmClipboardSuccess ) { wxCharBuffer buf( max_name_length + 1 ); unsigned long copied; for( int i = 0; i < count; ++i ) { if( XmClipboardInquireFormat( xdisplay, xwindow, i + 1, (XtPointer)buf.data(), max_name_length, &copied ) != XmClipboardSuccess ) continue; buf.data()[copied] = '\0'; // try preferred format if( buf == data.GetPreferredFormat( wxDataObject::Set ).GetId() ) { chosenFormat = data.GetPreferredFormat( wxDataObject::Set ); break; } // try all other formats for( size_t i = 0; i < dfcount; ++i ) { if( buf == dfarr[i].GetId() ) chosenFormat = dfarr[i]; } } } if( chosenFormat == wxDF_INVALID ) return false; /////////////////////////////////////////////////////////////////////////// // now retrieve the data /////////////////////////////////////////////////////////////////////////// unsigned long length, dummy1; long dummy2; wxString id = chosenFormat.GetId(); while( ( retval = XmClipboardInquireLength( xdisplay, xwindow, id.char_str(), &length ) ) == XmClipboardLocked ); if( retval != XmClipboardSuccess ) return false; wxCharBuffer buf(length); while( ( retval = XmClipboardRetrieve( xdisplay, xwindow, id.char_str(), (XtPointer)buf.data(), length, &dummy1, &dummy2 ) ) == XmClipboardLocked ); if( retval != XmClipboardSuccess ) return false; if( !data.SetData( chosenFormat, length, buf.data() ) ) return false; return true; }
// set data functions STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease) { wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData")); switch ( pmedium->tymed ) { case TYMED_GDI: m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap); break; case TYMED_ENHMF: m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile); break; case TYMED_ISTREAM: // check if this format is supported if ( !m_pDataObject->IsSupported(pformatetc->cfFormat, wxDataObject::Set) ) { // As this is not a supported format (content data), assume it // is system data and save it. return SaveSystemData(pformatetc, pmedium, fRelease); } break; case TYMED_MFPICT: // fall through - we pass METAFILEPICT through HGLOBAL case TYMED_HGLOBAL: { wxDataFormat format = pformatetc->cfFormat; format = HtmlFormatFixup(format); // check if this format is supported if ( !m_pDataObject->IsSupported(format, wxDataObject::Set) ) { // As above, assume that unsupported format must be system // data and just save it. return SaveSystemData(pformatetc, pmedium, fRelease); } // copy data const void *pBuf = GlobalLock(pmedium->hGlobal); if ( pBuf == NULL ) { wxLogLastError(wxT("GlobalLock")); return E_OUTOFMEMORY; } // we've got a problem with SetData() here because the base // class version requires the size parameter which we don't // have anywhere in OLE data transfer - so we need to // synthetise it for known formats and we suppose that all data // in custom formats starts with a DWORD containing the size size_t size; switch ( format ) { case wxDF_HTML: case CF_TEXT: case CF_OEMTEXT: size = strlen((const char *)pBuf); break; #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500)) case CF_UNICODETEXT: #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t); #else size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t); #endif break; #endif case CF_BITMAP: #ifndef __WXWINCE__ case CF_HDROP: // these formats don't use size at all, anyhow (but // pass data by handle, which is always a single DWORD) size = 0; break; #endif case CF_DIB: // the handler will calculate size itself (it's too // complicated to do it here) size = 0; break; #ifndef __WXWINCE__ case CF_METAFILEPICT: size = sizeof(METAFILEPICT); break; #endif default: pBuf = m_pDataObject-> GetSizeFromBuffer(pBuf, &size, format); size -= m_pDataObject->GetBufferOffset(format); } bool ok = m_pDataObject->SetData(format, size, pBuf); GlobalUnlock(pmedium->hGlobal); if ( !ok ) { return E_UNEXPECTED; } } break; default: return DV_E_TYMED; } if ( fRelease ) { // we own the medium, so we must release it - but do *not* free any // data we pass by handle because we have copied it elsewhere switch ( pmedium->tymed ) { case TYMED_GDI: pmedium->hBitmap = 0; break; case TYMED_MFPICT: pmedium->hMetaFilePict = 0; break; case TYMED_ENHMF: pmedium->hEnhMetaFile = 0; break; } ReleaseStgMedium(pmedium); } return S_OK; }