wxMacUniCharBuffer::wxMacUniCharBuffer( const wxString &str ) { m_chars = str.length() ; m_ubuf = NULL ; #if SIZEOF_WCHAR_T == 4 wxMBConvUTF16 converter ; #if wxUSE_UNICODE size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ; m_ubuf = (UniChar*) malloc( unicharlen + 2 ) ; converter.WC2MB( (char*) m_ubuf , str.wc_str(), unicharlen + 2 ) ; #else const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ; m_ubuf = (UniChar*) malloc( unicharlen + 2 ) ; converter.WC2MB( (char*) m_ubuf , wchar.data() , unicharlen + 2 ) ; #endif m_chars = unicharlen / 2 ; #else // SIZEOF_WCHAR_T is then 2 #if wxUSE_UNICODE m_ubuf = malloc( m_chars * 2 + 2 ) ; memcpy( m_ubuf , (UniChar*) str.wc_str() , m_chars * 2 + 2 ) ; #else wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; m_chars = wxWcslen( wchar.data() ) ; m_ubuf = malloc( m_chars * 2 + 2 ) ; memcpy( m_ubuf , (UniChar*) wchar.data() , m_chars * 2 + 2 ) ; #endif #endif }
// tests the encoding and decoding capability of an wxMBConv object // // decodes the utf-8 bytes into wide characters // encodes the wide characters to compare against input multiBuffer // decodes the multiBuffer to compare against wide characters // decodes the multiBuffer into wide characters void MBConvTestCase::TestCoder( const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter" size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter" const char* utf8Buffer, // the same character sequence as multiBuffer, encoded as UTF-8 size_t utf8Bytes, // the byte length of the UTF-8 encoded character sequence wxMBConv* converter, // the wxMBConv object thta can decode multiBuffer into a wide character sequence int sizeofNull // the number of bytes occupied by a terminating null in the converter's encoding ) { // wide character size and endian-ess varies from platform to platform // compiler support for wide character literals varies from compiler to compiler // so we should store the wide character version as UTF-8 and depend on // the UTF-8 converter's ability to decode it to platform specific wide characters // this test is invalid if the UTF-8 converter can't decode wxWCharBuffer wideBuffer((size_t)0); wideBuffer = DecodeUTF8( utf8Buffer, utf8Bytes ); size_t wideChars = wxWcslen( wideBuffer.data() ); TestDecoder ( wideBuffer.data(), wideChars, (const char*)multiBuffer, multiBytes, converter, sizeofNull ); TestEncoder ( wideBuffer.data(), wideChars, (const char*)multiBuffer, multiBytes, converter, sizeofNull ); }
// 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; }