wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl *wxPeer, const wxString& str, const wxPoint& pos, const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer ) { m_font = wxPeer->GetFont() ; m_windowStyle = style ; m_selection.selStart = m_selection.selEnd = 0; Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ; wxString st = str ; wxMacConvertNewlines10To13( &st ) ; wxCFStringRef cf(st , m_font.GetEncoding()) ; m_valueTag = kControlEditTextCFStringTag ; OptionBits attributes = kHISearchFieldAttributesSearchIcon; HIRect hibounds = { { bounds.left, bounds.top }, { bounds.right-bounds.left, bounds.bottom-bounds.top } }; verify_noerr( HISearchFieldCreate( &hibounds, attributes, 0, // MenuRef CFSTR(""), &m_controlRef ) ); HIViewSetVisible (m_controlRef, true); verify_noerr( SetData<CFStringRef>( 0, kControlEditTextCFStringTag , cf ) ) ; ::InstallControlEventHandler( m_controlRef, GetwxMacSearchControlEventHandlerUPP(), GetEventTypeCount(eventList), eventList, wxPeer, NULL); SetNeedsFrame(false); wxMacUnicodeTextControl::InstallEventHandlers(); }
void wxMacConvertNewlines10To13( wxString * data ) { size_t len = data->Length() ; if ( data->Length() == 0 || wxStrchr(data->c_str(),0x0a)==NULL) return ; wxString temp(*data) ; wxStringBuffer buf(*data,len ) ; memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ; wxMacConvertNewlines10To13( buf ) ; }
wxString wxCFStringRef::AsString(wxFontEncoding WXUNUSED_IN_UNICODE(encoding)) { if ( !get() ) return wxEmptyString ; Size cflen = CFStringGetLength( get() ) ; char* buf = NULL ; CFStringEncoding cfencoding = 0; wxString result; #if wxUSE_UNICODE #if wxUSE_UNICODE_WCHAR cfencoding = kCFStringEncodingUTF32Native; #elif wxUSE_UNICODE_UTF8 cfencoding = kCFStringEncodingUTF8; #else #error "unsupported unicode representation" #endif #else cfencoding = wxMacGetSystemEncFromFontEnc( encoding ); #endif CFIndex cStrLen ; CFStringGetBytes( get() , CFRangeMake(0, cflen) , cfencoding , '?' , false , NULL , 0 , &cStrLen ) ; buf = new char[ cStrLen ] ; CFStringGetBytes( get() , CFRangeMake(0, cflen) , cfencoding, '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ; #if wxUSE_UNICODE #if wxUSE_UNICODE_WCHAR result = wxString( (const wchar_t*) buf , cStrLen/4); #elif wxUSE_UNICODE_UTF8 result = wxString::FromUTF8( buf, cStrLen ); #else #error "unsupported unicode representation" #endif #else result = wxString(buf, cStrLen) ; #endif delete[] buf ; wxMacConvertNewlines10To13( &result); return result ; }
wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding) { if ( m_cfs == NULL ) return wxEmptyString ; Size cflen = CFStringGetLength( m_cfs ) ; size_t noChars ; wxChar* buf = NULL ; #if wxUSE_UNICODE #if SIZEOF_WCHAR_T == 2 buf = new wxChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ; noChars = cflen ; #else UniChar* unibuf = new UniChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ; unibuf[cflen] = 0 ; wxMBConvUTF16 converter ; noChars = converter.MB2WC( NULL , (const char*)unibuf , 0 ) ; wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") ); buf = new wxChar[ noChars + 1 ] ; noChars = converter.MB2WC( buf , (const char*)unibuf , noChars + 1 ) ; wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") ); delete[] unibuf ; #endif #else CFIndex cStrLen ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , NULL , 0 , &cStrLen ) ; buf = new wxChar[ cStrLen + 1 ] ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ; noChars = cStrLen ; #endif buf[noChars] = 0 ; wxMacConvertNewlines10To13( buf ) ; wxString result(buf) ; delete[] buf ; return result ; }
wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding) { Size cflen = CFStringGetLength( m_cfs ) ; size_t noChars ; wxChar* buf = NULL ; #if wxUSE_UNICODE #if SIZEOF_WCHAR_T == 2 buf = new wxChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ; noChars = cflen ; #else UniChar* unibuf = new UniChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ; unibuf[cflen] = 0 ; wxMBConvUTF16BE converter ; noChars = converter.MB2WC( NULL , (const char*)unibuf , 0 ) ; buf = new wxChar[ noChars + 1 ] ; converter.MB2WC( buf , (const char*)unibuf , noChars ) ; delete[] unibuf ; #endif #else CFIndex cStrLen ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , NULL , 0 , &cStrLen ) ; buf = new wxChar[ cStrLen + 1 ] ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ; noChars = cStrLen ; #endif buf[noChars] = 0 ; wxMacConvertNewlines10To13( buf ) ; wxString result(buf) ; delete[] buf ; return result ; }
bool wxDataObject::GetFromPasteboard( void * pb ) { PasteboardRef pasteboard = (PasteboardRef) pb; size_t formatcount = GetFormatCount() + 1; wxDataFormat *array = new wxDataFormat[ formatcount ]; array[0] = GetPreferredFormat(); GetAllFormats( &array[1] ); ItemCount itemCount = 0; wxString filenamesPassed; bool transferred = false; // we synchronize here once again, so we don't mind which flags get returned PasteboardSynchronize( pasteboard ); OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); if ( err == noErr ) { for (size_t i = 0; !transferred && i < formatcount; i++) { // go through the data in our order of preference wxDataFormat dataFormat = array[ i ]; for( UInt32 itemIndex = 1; itemIndex <= itemCount && transferred == false ; itemIndex++ ) { PasteboardItemID itemID = 0; CFArrayRef flavorTypeArray = NULL; CFIndex flavorCount = 0; err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); if ( err != noErr ) continue; err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); if ( err != noErr ) continue; flavorCount = CFArrayGetCount( flavorTypeArray ); for( CFIndex flavorIndex = 0; !transferred && flavorIndex < flavorCount ; flavorIndex++ ) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, flavorIndex ); // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion if ( UTTypeConformsTo(flavorType, CFSTR("public.utf8-plain-text") ) ) { flavorType = CFSTR("public.utf16-plain-text"); } wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); if ( dataFormat == flavorFormat ) { err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType , &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); if (dataFormat.GetType() == wxDF_FILENAME ) { // revert the translation and decomposition to arrive at a proper utf8 string again CFURLRef url = CFURLCreateWithBytes( kCFAllocatorDefault, CFDataGetBytePtr( flavorData ), flavorDataSize, kCFStringEncodingUTF8, NULL ); CFStringRef cfString = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ); CFRelease( url ); CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); CFRelease( cfString ); CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); wxString path = wxMacCFStringHolder(cfMutableString).AsString(); if (!path.empty()) filenamesPassed += path + wxT("\n"); } else { // because some data implementation expect trailing a trailing NUL, we add some headroom void *buf = malloc( flavorDataSize + 4 ); if ( buf ) { memset( buf, 0, flavorDataSize + 4 ); memcpy( buf, CFDataGetBytePtr( flavorData ), flavorDataSize ); if (dataFormat.GetType() == wxDF_TEXT) wxMacConvertNewlines10To13( (char*) buf ); SetData( flavorFormat, flavorDataSize, buf ); transferred = true; free( buf ); } } CFRelease (flavorData); } } else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) { err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType, &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); void *asciibuf = malloc( flavorDataSize + 1 ); if ( asciibuf ) { memset( asciibuf, 0, flavorDataSize + 1 ); memcpy( asciibuf, CFDataGetBytePtr( flavorData ), flavorDataSize ); CFRelease (flavorData); SetData( wxDF_TEXT, flavorDataSize, asciibuf ); transferred = true; free( asciibuf ); } else CFRelease (flavorData); } } } CFRelease( flavorTypeArray ); } if (filenamesPassed.length() > 0) { wxCharBuffer buf = filenamesPassed.fn_str(); SetData( wxDF_FILENAME, strlen( buf ), (const char*)buf ); transferred = true; } } } return transferred; }
bool wxDataObject::GetFromPasteboard( void * pb ) { PasteboardRef pasteboard = (PasteboardRef) pb; size_t formatcount = GetFormatCount(wxDataObject::Set); wxDataFormat *array = new wxDataFormat[ formatcount ]; GetAllFormats(array, wxDataObject::Set); ItemCount itemCount = 0; wxString filenamesPassed; bool transferred = false; bool pastelocationset = false; // we synchronize here once again, so we don't mind which flags get returned PasteboardSynchronize( pasteboard ); OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); if ( err == noErr ) { for (size_t i = 0; !transferred && i < formatcount; i++) { // go through the data in our order of preference wxDataFormat dataFormat = array[ i ]; for( UInt32 itemIndex = 1; itemIndex <= itemCount && transferred == false ; itemIndex++ ) { PasteboardItemID itemID = 0; CFArrayRef flavorTypeArray = NULL; CFIndex flavorCount = 0; err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); if ( err != noErr ) continue; err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); if ( err != noErr ) continue; flavorCount = CFArrayGetCount( flavorTypeArray ); for( CFIndex flavorIndex = 0; !transferred && flavorIndex < flavorCount ; flavorIndex++ ) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, flavorIndex ); wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); if ( dataFormat == flavorFormat ) { if ( UTTypeConformsTo( (CFStringRef)flavorType, kPasteboardTypeFileURLPromise) ) { if ( !pastelocationset ) { wxString tempdir = wxFileName::GetTempDir() + wxFILE_SEP_PATH + "wxtemp.XXXXXX"; char* result = mkdtemp((char*)tempdir.fn_str().data()); if (!result) continue; wxCFRef<CFURLRef> dest(CFURLCreateFromFileSystemRepresentation(NULL,(const UInt8*)result,strlen(result),true)); PasteboardSetPasteLocation(pasteboard, dest); pastelocationset = true; } } else if ( flavorFormat.GetType() != wxDF_PRIVATE ) { // indicate the expected format for the type, benefiting from native conversions eg utf8 -> utf16 flavorType = (CFStringRef) wxDataFormat( flavorFormat.GetType()).GetFormatId(); } err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType , &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); if (dataFormat.GetType() == wxDF_FILENAME ) { // revert the translation and decomposition to arrive at a proper utf8 string again CFURLRef url = CFURLCreateWithBytes( kCFAllocatorDefault, CFDataGetBytePtr( flavorData ), flavorDataSize, kCFStringEncodingUTF8, NULL ); CFStringRef cfString = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ); CFRelease( url ); CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); CFRelease( cfString ); CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); wxString path = wxCFStringRef(cfMutableString).AsString(); if (!path.empty()) filenamesPassed += path + wxT("\n"); } else { // because some data implementation expect trailing a trailing NUL, we add some headroom void *buf = malloc( flavorDataSize + 4 ); if ( buf ) { memset( buf, 0, flavorDataSize + 4 ); memcpy( buf, CFDataGetBytePtr( flavorData ), flavorDataSize ); if (dataFormat.GetType() == wxDF_TEXT) wxMacConvertNewlines10To13( (char*) buf ); SetData( flavorFormat, flavorDataSize, buf ); transferred = true; free( buf ); } } CFRelease (flavorData); } } else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) { err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType, &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); void *asciibuf = malloc( flavorDataSize + 1 ); if ( asciibuf ) { memset( asciibuf, 0, flavorDataSize + 1 ); memcpy( asciibuf, CFDataGetBytePtr( flavorData ), flavorDataSize ); CFRelease (flavorData); SetData( wxDF_TEXT, flavorDataSize, asciibuf ); transferred = true; free( asciibuf ); } else CFRelease (flavorData); } } } CFRelease( flavorTypeArray ); } if ( !filenamesPassed.empty() ) { wxCharBuffer buf = filenamesPassed.fn_str(); SetData( wxDF_FILENAME, strlen( buf ), (const char*)buf ); transferred = true; } } } return transferred; }