// RCS-ID: $Id$
// Copyright: (c) Stefan Csomor
// Licence: wxWindows licence
+// Usage: Darwin (base library)
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
+
#ifndef WX_PRECOMP
#include "wx/string.h"
#include "wx/intl.h"
+ #if wxUSE_GUI
+ #include "wx/font.h"
+ #endif
#endif
-#include "wx/mac/corefoundation/cfstring.h"
-#if wxUSE_GUI
- #include "wx/font.h"
-#endif
+#include "wx/mac/corefoundation/cfstring.h"
-#ifdef __DARWIN__
- #include <CoreServices/CoreServices.h>
-#else
- #include <TextCommon.h>
-#endif
+#include <CoreServices/CoreServices.h>
void wxMacConvertNewlines13To10( char * data )
{
}
}
+const wxString sCR((wxChar)13);
+const wxString sLF((wxChar)10);
+
void wxMacConvertNewlines13To10( wxString * data )
{
- size_t len = data->Length() ;
-
- if ( len == 0 || wxStrchr(data->c_str(),0x0d)==NULL)
- return ;
-
- wxString temp(*data) ;
- wxStringBuffer buf(*data,len ) ;
- memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
-
- wxMacConvertNewlines13To10( buf ) ;
+ data->Replace( sCR,sLF);
}
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 ) ;
-}
-
-
-#if wxUSE_UNICODE
-void wxMacConvertNewlines13To10( wxChar * data )
-{
- wxChar * buf = data ;
- while( (buf=wxStrchr(buf,0x0d)) != NULL )
- {
- *buf = 0x0a ;
- buf++ ;
- }
+ data->Replace( sLF,sCR);
}
-void wxMacConvertNewlines10To13( wxChar * data )
-{
- wxChar * buf = data ;
- while( (buf=wxStrchr(buf,0x0a)) != NULL )
- {
- *buf = 0x0d ;
- buf++ ;
- }
-}
-#endif
-
wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding)
{
TextEncodingBase enc = 0 ;
#if wxUSE_GUI
encoding = wxFont::GetDefaultEncoding() ;
#else
- encoding = wxLocale::GetSystemEncoding() ;
+ encoding = wxFONTENCODING_SYSTEM; // to be set below
#endif
}
+
if ( encoding == wxFONTENCODING_SYSTEM )
{
enc = CFStringGetSystemEncoding();
//
-// CFStringRefs (Carbon only)
+// CFStringRefs
//
-// converts this string into a carbon foundation string with optional pc 2 mac encoding
-void wxMacCFStringHolder::Assign( const wxString &st , wxFontEncoding encoding )
+// converts this string into a core foundation string with optional pc 2 mac encoding
+
+wxCFStringRef::wxCFStringRef( const wxString &st , wxFontEncoding WXUNUSED_IN_UNICODE(encoding) )
{
- Release() ;
if (st.IsEmpty())
{
- m_cfs = CFSTR("") ;
- CFRetain( m_cfs ) ;
+ reset( wxCFRetain( CFSTR("") ) );
}
else
{
wxMacConvertNewlines13To10( &str ) ;
#if wxUSE_UNICODE
#if SIZEOF_WCHAR_T == 2
- m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
- (UniChar*)str.wc_str() , str.Len() );
+ reset( CFStringCreateWithCharacters( kCFAllocatorDefault,
+ (UniChar*)str.wc_str() , str.Len() ) );
#else
wxMBConvUTF16 converter ;
- size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
- UniChar *unibuf = new UniChar[ unicharlen / sizeof(UniChar) + 1 ] ;
- converter.WC2MB( (char*)unibuf , str.wc_str() , unicharlen ) ;
- m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault ,
- unibuf , unicharlen / sizeof(UniChar) ) ;
- delete[] unibuf ;
+ size_t unicharbytes = converter.FromWChar( NULL , 0 , str.wc_str() , str.Length() ) ;
+ wxASSERT( unicharbytes != wxCONV_FAILED );
+ if ( unicharbytes == wxCONV_FAILED )
+ {
+ // create an empty string
+ reset( wxCFRetain( CFSTR("") ) );
+ }
+ else
+ {
+ // unicharbytes: number of bytes needed for UTF-16 encoded string (without terminating null)
+ // unichars: number of UTF-16 characters (without terminating null)
+ size_t unichars = unicharbytes / sizeof(UniChar) ;
+ UniChar *unibuf = new UniChar[ unichars ] ;
+ converter.FromWChar( (char*)unibuf , unicharbytes , str.wc_str() , str.Length() ) ;
+ reset( CFStringCreateWithCharacters( kCFAllocatorDefault , unibuf , unichars ) ) ;
+ delete[] unibuf ;
+ }
#endif
#else // not wxUSE_UNICODE
- m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
- wxMacGetSystemEncFromFontEnc( encoding ) ) ;
+ reset( CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
+ wxMacGetSystemEncFromFontEnc( encoding ) ) );
#endif
}
- m_release = true ;
}
-wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding)
+wxString wxCFStringRef::AsString(wxFontEncoding WXUNUSED_IN_UNICODE(encoding))
{
- Size cflen = CFStringGetLength( m_cfs ) ;
+ if ( !get() )
+ return wxEmptyString ;
+
+ Size cflen = CFStringGetLength( get() ) ;
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 ) ;
+ CFStringGetCharacters( get() , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ;
noChars = cflen ;
#else
UniChar* unibuf = new UniChar[ cflen + 1 ] ;
- CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ;
+ CFStringGetCharacters( get() , 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 ] ;
- converter.MB2WC( buf , (const char*)unibuf , noChars ) ;
+ 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 ) ,
+ CFStringGetBytes( get() , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
'?' , false , NULL , 0 , &cStrLen ) ;
buf = new wxChar[ cStrLen + 1 ] ;
- CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
+ CFStringGetBytes( get() , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
'?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ;
noChars = cStrLen ;
#endif
buf[noChars] = 0 ;
- wxMacConvertNewlines10To13( buf ) ;
wxString result(buf) ;
delete[] buf ;
+ wxMacConvertNewlines10To13( &result);
return result ;
}
+//
+// wxMacUniCharBuffer
+//
+
+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
+}
+
+wxMacUniCharBuffer::~wxMacUniCharBuffer()
+{
+ free( m_ubuf ) ;
+}
+
+UniCharArrayPtr wxMacUniCharBuffer::GetBuffer()
+{
+ return m_ubuf ;
+}
+
+UniCharCount wxMacUniCharBuffer::GetChars()
+{
+ return m_chars ;
+}