+void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
+{
+ // first, choose the file
+ static wxString s_dir, s_file;
+ wxFileDialog dialog(this, "Open an email message file",
+ s_dir, s_file);
+ if ( dialog.ShowModal() != wxID_OK )
+ return;
+
+ // save for the next time
+ s_dir = dialog.GetDirectory();
+ s_file = dialog.GetFilename();
+
+ wxString filename = dialog.GetPath();
+
+ // load it and search for Content-Type header
+ wxTextFile file(filename);
+ if ( !file.Open() )
+ return;
+
+ wxString charset;
+
+ static const char *prefix = "Content-Type: text/plain; charset=";
+ const size_t len = strlen(prefix);
+
+ size_t n, count = file.GetLineCount();
+ for ( n = 0; n < count; n++ )
+ {
+ wxString line = file[n];
+
+ if ( !line )
+ {
+ // if it is an email message, headers are over, no need to parse
+ // all the file
+ break;
+ }
+
+ if ( line.Left(len) == prefix )
+ {
+ // found!
+ const wxChar *pc = line.c_str() + len;
+ if ( *pc == '"' )
+ pc++;
+
+ while ( *pc && *pc != '"' )
+ {
+ charset += *pc++;
+ }
+
+ break;
+ }
+ }
+
+ if ( !charset )
+ {
+ wxLogError(wxT("The file '%s' doesn't contain charset information."),
+ filename.c_str());
+
+ return;
+ }
+
+ // ok, now get the corresponding encoding
+ wxFontEncoding fontenc = wxTheFontMapper->CharsetToEncoding(charset);
+ if ( fontenc == wxFONTENCODING_SYSTEM )
+ {
+ wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str());
+ return;
+ }
+
+ m_textctrl->LoadFile(filename);
+
+ if ( fontenc == wxFONTENCODING_UTF8 ||
+ !wxTheFontMapper->IsEncodingAvailable(fontenc) )
+ {
+ // try to find some similar encoding:
+ wxFontEncoding encAlt;
+ if ( wxTheFontMapper->GetAltForEncoding(fontenc, &encAlt) )
+ {
+ wxEncodingConverter conv;
+
+ if (conv.Init(fontenc, encAlt))
+ {
+ fontenc = encAlt;
+ m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue()));
+ }
+ else
+ {
+ wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
+ wxFontMapper::GetEncodingDescription(fontenc).c_str(),
+ wxFontMapper::GetEncodingDescription(encAlt).c_str());
+ }
+ }
+ else
+ wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
+ wxFontMapper::GetEncodingDescription(fontenc).c_str());
+ }
+
+ // and now create the correct font
+ if ( !DoEnumerateFamilies(FALSE, fontenc, TRUE /* silent */) )
+ {
+ wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
+ wxFONTWEIGHT_NORMAL, FALSE /* !underlined */,
+ wxEmptyString /* facename */, fontenc);
+ if ( font.Ok() )
+ {
+ DoChangeFont(font);
+ }
+ else
+ {
+ wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
+ wxFontMapper::GetEncodingDescription(fontenc).c_str());
+ }
+ }
+}
+