From f66ecdc4fbfa14f18f95331ba859a85baee36ae6 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Tue, 18 Aug 2009 15:30:24 +0000 Subject: [PATCH] simplifying native string handling, see #11061 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61698 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/core/cfstring.h | 7 ++++++- src/osx/cocoa/dirdlg.mm | 4 +--- src/osx/cocoa/filedlg.mm | 7 ++----- src/osx/cocoa/listbox.mm | 2 +- src/osx/cocoa/textctrl.mm | 6 ++---- src/osx/cocoa/window.mm | 6 ++---- src/osx/core/cfstring.cpp | 23 ++++++++++++++++++----- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/wx/osx/core/cfstring.h b/include/wx/osx/core/cfstring.h index 30d4319493..e60bb91548 100644 --- a/include/wx/osx/core/cfstring.h +++ b/include/wx/osx/core/cfstring.h @@ -68,7 +68,12 @@ public: { } - wxString AsString( wxFontEncoding encoding = wxFONTENCODING_DEFAULT ) ; + wxString AsString( wxFontEncoding encoding = wxFONTENCODING_DEFAULT ) const; + + static wxString AsString( CFStringRef ref, wxFontEncoding encoding = wxFONTENCODING_DEFAULT ) ; +#if wxOSX_USE_COCOA_OR_IPHONE + static wxString AsString( NSString* ref, wxFontEncoding encoding = wxFONTENCODING_DEFAULT ) ; +#endif #if wxOSX_USE_COCOA_OR_IPHONE NSString* AsNSString() const { return (NSString*)(CFStringRef) *this; } diff --git a/src/osx/cocoa/dirdlg.mm b/src/osx/cocoa/dirdlg.mm index e68bf5020b..9ab49d86ec 100644 --- a/src/osx/cocoa/dirdlg.mm +++ b/src/osx/cocoa/dirdlg.mm @@ -99,9 +99,7 @@ int wxDirDialog::ShowModal() } if (returnCode == NSOKButton ) { - wxCFStringRef resultpath( [[[oPanel filenames] objectAtIndex:0] retain] ); - - SetPath( resultpath.AsString() ); + SetPath( wxCFStringRef::AsString([[oPanel filenames] objectAtIndex:0])); result = wxID_OK; } return result; diff --git a/src/osx/cocoa/filedlg.mm b/src/osx/cocoa/filedlg.mm index 0b34682a72..4a9d65fbc9 100644 --- a/src/osx/cocoa/filedlg.mm +++ b/src/osx/cocoa/filedlg.mm @@ -201,9 +201,7 @@ int wxFileDialog::ShowModal() panel = sPanel; result = wxID_OK; - wxCFStringRef filename( [[sPanel filename] retain] ); - - m_path = filename.AsString(); + m_path = wxCFStringRef::AsString([sPanel filename]); m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly( m_path ); } @@ -245,8 +243,7 @@ int wxFileDialog::ShowModal() NSArray* filenames = [oPanel filenames]; for ( size_t i = 0 ; i < [filenames count] ; ++ i ) { - wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] ); - wxString fnstr = filename.AsString(); + wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]); m_paths.Add( fnstr ); m_fileNames.Add( wxFileNameFromPath(fnstr) ); if ( i == 0 ) diff --git a/src/osx/cocoa/listbox.mm b/src/osx/cocoa/listbox.mm index dc6519ae23..ded09fb1e6 100644 --- a/src/osx/cocoa/listbox.mm +++ b/src/osx/cocoa/listbox.mm @@ -209,7 +209,7 @@ public : virtual wxString GetStringValue() const { if ( [value isKindOfClass:[NSString class]] ) - return wxCFStringRef( (CFStringRef) [value retain] ).AsString(); + return wxCFStringRef::AsString( (NSString*) value ); return wxEmptyString; } diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index c9dbe0b1ae..77a6ba4d02 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -333,8 +333,7 @@ wxString wxNSTextViewControl::GetStringValue() const { if (m_textView) { - wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] ); - wxString result = cf.AsString(m_wxPeer->GetFont().GetEncoding()); + wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); wxMacConvertNewlines13To10( &result ) ; return result; } @@ -446,8 +445,7 @@ wxNSTextFieldControl::~wxNSTextFieldControl() wxString wxNSTextFieldControl::GetStringValue() const { - wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] ); - return cf.AsString(m_wxPeer->GetFont().GetEncoding()); + return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding()); } void wxNSTextFieldControl::SetStringValue( const wxString &str) diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 69f702827f..6dd32c83ae 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -294,13 +294,11 @@ void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charStrin { // if charString is set, it did not come from key up / key down wxevent.SetEventType( wxEVT_CHAR ); - wxCFStringRef cfchars((CFStringRef)[charString retain]); - chars = cfchars.AsString(); + chars = wxCFStringRef::AsString(charString); } else if ( nschars ) { - wxCFStringRef cfchars((CFStringRef)[nschars retain]); - chars = cfchars.AsString(); + chars = wxCFStringRef::AsString(nschars); } } diff --git a/src/osx/core/cfstring.cpp b/src/osx/core/cfstring.cpp index 3d10e29b7e..b603e5b48d 100644 --- a/src/osx/core/cfstring.cpp +++ b/src/osx/core/cfstring.cpp @@ -632,12 +632,12 @@ wxCFStringRef::wxCFStringRef( const wxString &st , wxFontEncoding WXUNUSED_IN_UN } } -wxString wxCFStringRef::AsString(wxFontEncoding WXUNUSED_IN_UNICODE(encoding)) +wxString wxCFStringRef::AsString( CFStringRef ref, wxFontEncoding WXUNUSED_IN_UNICODE(encoding) ) { - if ( !get() ) + if ( !ref ) return wxEmptyString ; - Size cflen = CFStringGetLength( get() ) ; + Size cflen = CFStringGetLength( ref ) ; char* buf = NULL ; CFStringEncoding cfencoding = 0; @@ -655,10 +655,10 @@ wxString wxCFStringRef::AsString(wxFontEncoding WXUNUSED_IN_UNICODE(encoding)) #endif CFIndex cStrLen ; - CFStringGetBytes( get() , CFRangeMake(0, cflen) , cfencoding , + CFStringGetBytes( ref , CFRangeMake(0, cflen) , cfencoding , '?' , false , NULL , 0 , &cStrLen ) ; buf = new char[ cStrLen ] ; - CFStringGetBytes( get() , CFRangeMake(0, cflen) , cfencoding, + CFStringGetBytes( ref , CFRangeMake(0, cflen) , cfencoding, '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ; #if wxUSE_UNICODE @@ -678,6 +678,19 @@ wxString wxCFStringRef::AsString(wxFontEncoding WXUNUSED_IN_UNICODE(encoding)) return result ; } +wxString wxCFStringRef::AsString(wxFontEncoding encoding) const +{ + return AsString( get(), encoding ); +} + +#if wxOSX_USE_COCOA_OR_IPHONE +wxString wxCFStringRef::AsString( NSString* ref, wxFontEncoding encoding ) +{ + return AsString( (CFStringRef) ref, encoding ); +} +#endif + + // // wxMacUniCharBuffer // -- 2.45.2