]> git.saurik.com Git - wxWidgets.git/commitdiff
respect the encoding of the text style and not only the global control font encoding...
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 23 Aug 2006 13:33:04 +0000 (13:33 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 23 Aug 2006 13:33:04 +0000 (13:33 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40771 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/gtk/textctrl.h
src/gtk/textctrl.cpp

index b9b793eb19c62941410a4d3056b8f8476445b2cf..19c3ca90102b3a8b8e0be7fed4058416eddb436a 100644 (file)
@@ -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;
index 07b729c9cddc27fe428a76db9cdb6b9fbac28fa9..1c599667a9ac6ece15be13356aa5735e60904aa3 100644 (file)
@@ -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;
     }