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
This commit is contained in:
Vadim Zeitlin
2006-08-23 13:33:04 +00:00
parent 7581272214
commit 0d91b2342a
2 changed files with 42 additions and 3 deletions

View File

@@ -191,6 +191,13 @@ private:
// change the font for everything in this control // change the font for everything in this control
void ChangeFontGlobally(); 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; GtkWidget *m_text;
bool m_modified:1; bool m_modified:1;

View File

@@ -812,6 +812,24 @@ wxString wxTextCtrl::GetValue() const
return tmp; 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 ) void wxTextCtrl::SetValue( const wxString &value )
{ {
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
@@ -825,7 +843,7 @@ void wxTextCtrl::SetValue( const wxString &value )
if ( IsMultiLine() ) if ( IsMultiLine() )
{ {
const wxCharBuffer buffer(wxGTK_CONV(value)); const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding()));
if ( !buffer ) if ( !buffer )
{ {
// what else can we do? at least don't crash... // what else can we do? at least don't crash...
@@ -860,10 +878,24 @@ void wxTextCtrl::WriteText( const wxString &text )
if ( text.empty() ) if ( text.empty() )
return; 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 ) 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; return;
} }