X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ea9144a3bcebbc04fbf2174b1ec9e31f92784c44..f1567cddead054974c4a079ed8ee2c798ef998dc:/samples/font/font.cpp diff --git a/samples/font/font.cpp b/samples/font/font.cpp index 56e5a565b6..8c1a0b630a 100644 --- a/samples/font/font.cpp +++ b/samples/font/font.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include // ---------------------------------------------------------------------------- @@ -234,34 +235,36 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) SetStatusText("Welcome to wxWindows font demo!"); } +// -------------------------------------------------------- -// event handlers -void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event)) +class MyEncodingEnumerator : public wxFontEnumerator { - class MyEncodingEnumerator : public wxFontEnumerator - { - public: - MyEncodingEnumerator() { m_n = 0; } +public: + MyEncodingEnumerator() + { m_n = 0; } - const wxString& GetText() const { return m_text; } + const wxString& GetText() const + { return m_text; } - protected: - virtual bool OnFontEncoding(const wxString& facename, - const wxString& encoding) - { - wxString text; - text.Printf("Encoding %d: %s (available in facename '%s')\n", - ++m_n, encoding.c_str(), facename.c_str()); - m_text += text; - - return TRUE; - } +protected: + virtual bool OnFontEncoding(const wxString& facename, + const wxString& encoding) + { + wxString text; + text.Printf("Encoding %d: %s (available in facename '%s')\n", + ++m_n, encoding.c_str(), facename.c_str()); + m_text += text; + return TRUE; + } - private: - size_t m_n; +private: + size_t m_n; + wxString m_text; +}; - wxString m_text; - } fontEnumerator; +void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event)) +{ + MyEncodingEnumerator fontEnumerator; fontEnumerator.EnumerateEncodings(); @@ -269,28 +272,33 @@ void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event)) fontEnumerator.GetText().c_str()); } -bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly, - wxFontEncoding encoding, - bool silent) -{ - class MyFontEnumerator : public wxFontEnumerator - { - public: - bool GotAny() const { return !m_facenames.IsEmpty(); } +// ------------------------------------------------------------- - const wxArrayString& GetFacenames() const { return m_facenames; } +class MyFontEnumerator : public wxFontEnumerator +{ +public: + bool GotAny() const + { return !m_facenames.IsEmpty(); } - protected: - virtual bool OnFacename(const wxString& facename) - { - m_facenames.Add(facename); + const wxArrayString& GetFacenames() const + { return m_facenames; } - return TRUE; - } +protected: + virtual bool OnFacename(const wxString& facename) + { + m_facenames.Add(facename); + return TRUE; + } private: wxArrayString m_facenames; - } fontEnumerator; +} fontEnumerator; + +bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly, + wxFontEncoding encoding, + bool silent) +{ + MyFontEnumerator fontEnumerator; fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly); @@ -489,6 +497,33 @@ void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event)) return; } + m_textctrl->LoadFile(filename); + + if (!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("Cannot convert from '%s' to '%s'.", + wxFontMapper::GetEncodingDescription(fontenc).c_str(), + wxFontMapper::GetEncodingDescription(encAlt).c_str()); + } + } + else + wxLogWarning("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 */) ) { @@ -505,8 +540,6 @@ void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event)) wxFontMapper::GetEncodingDescription(fontenc).c_str()); } } - - m_textctrl->LoadFile(filename); } void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))