From: Vadim Zeitlin Date: Wed, 23 Aug 2006 13:33:04 +0000 (+0000) Subject: respect the encoding of the text style and not only the global control font encoding... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0d91b2342a1f83b3b9db145c7210efd5d9547cc8?ds=inline respect the encoding of the text style and not only the global control font encoding when inserting the text in the control git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40771 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/gtk/textctrl.h b/include/wx/gtk/textctrl.h index b9b793eb19..19c3ca9010 100644 --- a/include/wx/gtk/textctrl.h +++ b/include/wx/gtk/textctrl.h @@ -191,6 +191,13 @@ private: // change the font for everything in this control void ChangeFontGlobally(); + // get the encoding which is used in this control: this looks at our font + // and default style but not the current style (i.e. the style for the + // current position); returns wxFONTENCODING_SYSTEM if we have no specific + // encoding + wxFontEncoding GetTextEncoding() const; + + GtkWidget *m_text; bool m_modified:1; diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 07b729c9cd..1c599667a9 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -812,6 +812,24 @@ wxString wxTextCtrl::GetValue() const return tmp; } +wxFontEncoding wxTextCtrl::GetTextEncoding() const +{ + // GTK+ uses UTF-8 internally, we need to convert to it but from which + // encoding? + + // first check the default text style (we intentionally don't check the + // style for the current position as it doesn't make sense for SetValue()) + const wxTextAttr& style = GetDefaultStyle(); + wxFontEncoding enc = style.HasFont() ? style.GetFont().GetEncoding() + : wxFONTENCODING_SYSTEM; + + // fall back to the controls font if no style + if ( enc == wxFONTENCODING_SYSTEM && m_hasFont ) + enc = GetFont().GetEncoding(); + + return enc; +} + void wxTextCtrl::SetValue( const wxString &value ) { wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); @@ -825,7 +843,7 @@ void wxTextCtrl::SetValue( const wxString &value ) if ( IsMultiLine() ) { - const wxCharBuffer buffer(wxGTK_CONV(value)); + const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding())); if ( !buffer ) { // what else can we do? at least don't crash... @@ -860,10 +878,24 @@ void wxTextCtrl::WriteText( const wxString &text ) if ( text.empty() ) return; - const wxCharBuffer buffer(wxGTK_CONV(text)); + // check if we have a specific style for the current position + wxFontEncoding enc = wxFONTENCODING_SYSTEM; + wxTextAttr style; + if ( GetStyle(GetInsertionPoint(), style) && style.HasFont() ) + { + enc = style.GetFont().GetEncoding(); + } + + if ( enc == wxFONTENCODING_SYSTEM ) + enc = GetTextEncoding(); + + const wxCharBuffer buffer(wxGTK_CONV_ENC(text, enc)); if ( !buffer ) { - // what else can we do? at least don't crash... + // we must log an error here as losing the text like this can be a + // serious problem (e.g. imagine the document edited by user being + // empty instead of containing the correct text) + wxLogWarning(_("Failed to insert text in the control.")); return; }