string encoding in wxStreamToTextRedirector
string encoding in wxStreamToTextRedirector
I met a problem to display UTF8 non-ASCII string with wxStreamToTextRedirector. The string is displayed OK with other wxWidgets text control after being converted by mb_str(wxConvUTF8). As below
Any suggestions?
I tried to change wxConvCurrent to wxConvUTF8 but it did not work.Any suggestions?
Re: string encoding in wxStreamToTextRedirector
Can you post a minimal, compilable code sample?
Which wxWidgets version are you using and what's the locale of your OS?
Which wxWidgets version are you using and what's the locale of your OS?
Use the source, Luke!
Re: string encoding in wxStreamToTextRedirector
I'll try to make a minimal, compilable code sample.
I'm using wxWidgets 3.1.3. The encoding of the string in the example was UTF8(I verified). The locale of my OS is GBK.
I'm using wxWidgets 3.1.3. The encoding of the string in the example was UTF8(I verified). The locale of my OS is GBK.
Re: string encoding in wxStreamToTextRedirector
I made a minimal example with a dialog which has a text control and two buttons. Below is the code snippet for demo the issue.
Result
Code: Select all
void testDialog::OnAbout(wxCommandEvent& event)
{
std::string s("\xE6\xB5\x8B\xE8\xAF\x95"); // UTF8
TextCtrl1->Clear();
wxStreamToTextRedirector redirctor(TextCtrl1);
std::cout << s << std::endl;
wxMBConv* current = wxConvCurrent;
wxConvCurrent = dynamic_cast<wxMBConv*>(&wxConvUTF8);
wxASSERT(wxConvCurrent);
std::cout << s << std::endl; // try again
wxConvCurrent = current;
wxString str(s.c_str(), wxConvUTF8);
TextCtrl1->AppendText(str);
}
Re: string encoding in wxStreamToTextRedirector
After a little bit of tracing through the code, i don't think it's possible.
Every single char of the 8bit string passes through this method individually, no decoding takes place:(src/common/textcmn.cpp)
Every single char of the 8bit string passes through this method individually, no decoding takes place:
Code: Select all
int wxTextCtrlBase::overflow(int c)
{
AppendText((wxChar)c);
// return something different from EOF
return 0;
}
Use the source, Luke!
Re: string encoding in wxStreamToTextRedirector
Thanks, doublemax. Now I understand the root cause.
BTW: do you think it should be fixed/supported by wxWidgets?
BTW: do you think it should be fixed/supported by wxWidgets?
Re: string encoding in wxStreamToTextRedirector
I'm not sure. But you could try to open a ticket for this and see what the response is.
Use the source, Luke!
Re: string encoding in wxStreamToTextRedirector
If you copy the wxStreamToTextRedirector code and convert it to wide chars, you can redirect std::wcout.
Code: Select all
#include <iostream>
#include <fstream>
class wxStreamToTextRedirectorUnicode : public std::wstreambuf
{
private:
void Init(wxTextCtrl *text)
{
m_text = text;
m_sbufOld = m_ostr.rdbuf();
m_ostr.rdbuf(this);
}
public:
wxStreamToTextRedirectorUnicode(wxTextCtrl *text)
: m_ostr(std::wcout)
{
Init(text);
}
wxStreamToTextRedirectorUnicode(wxTextCtrl *text, std::wostream *ostr)
: m_ostr(*ostr)
{
Init(text);
}
~wxStreamToTextRedirectorUnicode()
{
m_ostr.rdbuf(m_sbufOld);
}
// override streambuf method
int_type overflow(int_type i) wxOVERRIDE
{
m_text->AppendText( (wxChar)i );
return 0;
}
private:
wxTextCtrl *m_text;
// the stream we're redirecting
std::wostream &m_ostr;
// the old streambuf (before we changed it)
std::wstreambuf *m_sbufOld;
};
Use the source, Luke!
Re: string encoding in wxStreamToTextRedirector
Thanks, doublemax. Will try it and let you know the result.
Re: string encoding in wxStreamToTextRedirector
It works in a weird way I think it is because MS windows uses UTF16.
I used a customized redirector function for my app and convert it with wxString("...", wxConvUtf8). Now I can output as expected.
Thanks anyway.
I used a customized redirector function for my app and convert it with wxString("...", wxConvUtf8). Now I can output as expected.
Thanks anyway.