+ // true is to force the frame to close
+ Close(true);
+}
+
+void MyFrame::OnTestTextValue(wxCommandEvent& WXUNUSED(event))
+{
+ wxString value = m_textctrl->GetValue();
+ m_textctrl->SetValue(value);
+ if ( m_textctrl->GetValue() != value )
+ {
+ wxLogError(wxT("Text value changed after getting and setting it"));
+ }
+}
+
+void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
+{
+#if wxUSE_FILEDLG
+ // first, choose the file
+ static wxString s_dir, s_file;
+ wxFileDialog dialog(this, wxT("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 wxChar *prefix = wxT("Content-Type: text/plain; charset=");
+ const size_t len = wxStrlen(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 == wxT('"') )
+ pc++;
+
+ while ( *pc && *pc != wxT('"') )
+ {
+ 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 = wxFontMapper::Get()->CharsetToEncoding(charset);
+ if ( fontenc == wxFONTENCODING_SYSTEM )
+ {
+ wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str());
+ return;
+ }
+
+ m_textctrl->LoadFile(filename);
+
+ if ( fontenc == wxFONTENCODING_UTF8 ||
+ !wxFontMapper::Get()->IsEncodingAvailable(fontenc) )
+ {
+ // try to find some similar encoding:
+ wxFontEncoding encAlt;
+ if ( wxFontMapper::Get()->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(wxNORMAL_FONT->GetPointSize(),
+ 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());
+ }
+ }
+#endif // wxUSE_FILEDLG