+ CreateStatusBar();
+ SetStatusText(wxT("Welcome to wxWidgets font demo!"));
+#endif // wxUSE_STATUSBAR
+}
+
+// --------------------------------------------------------
+
+class MyEncodingEnumerator : public wxFontEnumerator
+{
+public:
+ MyEncodingEnumerator()
+ { m_n = 0; }
+
+ const wxString& GetText() const
+ { return m_text; }
+
+protected:
+ virtual bool OnFontEncoding(const wxString& facename,
+ const wxString& encoding)
+ {
+ wxString text;
+ text.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
+ (unsigned int) ++m_n, encoding.c_str(), facename.c_str());
+ m_text += text;
+ return true;
+ }
+
+private:
+ size_t m_n;
+ wxString m_text;
+};
+
+void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
+{
+ MyEncodingEnumerator fontEnumerator;
+
+ fontEnumerator.EnumerateEncodings();
+
+ wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
+ fontEnumerator.GetText().c_str());
+}
+
+// -------------------------------------------------------------
+
+class MyFontEnumerator : public wxFontEnumerator
+{
+public:
+ bool GotAny() const
+ { return !m_facenames.IsEmpty(); }
+
+ const wxArrayString& GetFacenames() const
+ { return m_facenames; }
+
+protected:
+ virtual bool OnFacename(const wxString& facename)
+ {
+ m_facenames.Add(facename);
+ return true;
+ }
+
+ private:
+ wxArrayString m_facenames;
+} fontEnumerator;
+
+bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
+ wxFontEncoding encoding,
+ bool silent)
+{
+ MyFontEnumerator fontEnumerator;
+
+ fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
+
+ if ( fontEnumerator.GotAny() )
+ {
+ int nFacenames = fontEnumerator.GetFacenames().GetCount();
+ if ( !silent )
+ {
+ wxLogStatus(this, wxT("Found %d %sfonts"),
+ nFacenames, fixedWidthOnly ? wxT("fixed width ") : wxT(""));
+ }
+
+ wxString facename;
+
+ if ( silent )
+ {
+ // choose the first
+ facename = fontEnumerator.GetFacenames().Item(0);
+ }
+ else
+ {
+ // let the user choose
+ wxString *facenames = new wxString[nFacenames];
+ int n;
+ for ( n = 0; n < nFacenames; n++ )
+ facenames[n] = fontEnumerator.GetFacenames().Item(n);
+
+ n = wxGetSingleChoiceIndex
+ (
+ wxT("Choose a facename"),
+ SAMPLE_TITLE,
+ nFacenames,
+ facenames,
+ this
+ );
+
+ if ( n != -1 )
+ facename = facenames[n];
+
+ delete [] facenames;
+ }
+
+ if ( !facename.empty() )
+ {
+ wxFont font(wxNORMAL_FONT->GetPointSize(),
+ wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
+ wxFONTWEIGHT_NORMAL, false, facename, encoding);
+
+ DoChangeFont(font);
+ }
+
+ return true;
+ }
+ else if ( !silent )
+ {
+ wxLogWarning(wxT("No such fonts found."));
+ }
+
+ return false;
+}
+
+void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
+{
+ wxFontEncoding enc = GetEncodingFromUser();
+ if ( enc != wxFONTENCODING_SYSTEM )
+ {
+ DoEnumerateFamilies(false, enc);
+ }
+}
+
+void MyFrame::OnSetNativeDesc(wxCommandEvent& WXUNUSED(event))
+{
+ wxString fontInfo = wxGetTextFromUser
+ (
+ wxT("Enter native font string"),
+ wxT("Input font description"),
+ m_canvas->GetTextFont().GetNativeFontInfoDesc(),
+ this
+ );
+ if ( fontInfo.empty() )
+ return; // user clicked "Cancel" - do nothing
+
+ wxFont font;
+ font.SetNativeFontInfo(fontInfo);
+ if ( !font.Ok() )
+ {
+ wxLogError(wxT("Font info string \"%s\" is invalid."),
+ fontInfo.c_str());
+ return;
+ }
+
+ DoChangeFont(font);
+}
+
+void MyFrame::OnSetFaceName(wxCommandEvent& WXUNUSED(event))
+{
+ wxString facename = GetCanvas()->GetTextFont().GetFaceName();
+ wxString newFaceName = wxGetTextFromUser(
+ wxT("Here you can edit current font face name."),
+ wxT("Input font facename"), facename,
+ this);
+ if (newFaceName.IsEmpty())
+ return; // user clicked "Cancel" - do nothing
+
+ wxFont font(GetCanvas()->GetTextFont());
+ if (font.SetFaceName(newFaceName)) // change facename only
+ {
+ wxASSERT_MSG(font.Ok(), wxT("The font should now be valid"));
+ DoChangeFont(font);
+ }
+ else
+ {
+ wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid"));
+ wxMessageBox(wxT("There is no font with such face name..."),
+ wxT("Invalid face name"), wxOK|wxICON_ERROR, this);
+ }
+}
+
+void MyFrame::OnSetNativeUserDesc(wxCommandEvent& WXUNUSED(event))
+{
+ wxString fontdesc = GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
+ wxString fontUserInfo = wxGetTextFromUser(
+ wxT("Here you can edit current font description"),
+ wxT("Input font description"), fontdesc,
+ this);
+ if (fontUserInfo.IsEmpty())
+ return; // user clicked "Cancel" - do nothing
+
+ wxFont font;
+ if (font.SetNativeFontInfoUserDesc(fontUserInfo))
+ {
+ wxASSERT_MSG(font.Ok(), wxT("The font should now be valid"));
+ DoChangeFont(font);
+ }
+ else
+ {
+ wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid"));
+ wxMessageBox(wxT("Error trying to create a font with such description..."));
+ }
+}
+
+void MyFrame::OnSetEncoding(wxCommandEvent& WXUNUSED(event))
+{
+ wxFontEncoding enc = GetEncodingFromUser();
+ if ( enc == wxFONTENCODING_SYSTEM )
+ return;
+
+ wxFont font = m_canvas->GetTextFont();
+ font.SetEncoding(enc);
+ DoChangeFont(font);
+}
+
+wxFontEncoding MyFrame::GetEncodingFromUser()
+{
+ wxArrayString names;
+ wxArrayInt encodings;
+
+ const size_t count = wxFontMapper::GetSupportedEncodingsCount();
+ names.reserve(count);
+ encodings.reserve(count);
+
+ for ( size_t n = 0; n < count; n++ )
+ {
+ wxFontEncoding enc = wxFontMapper::GetEncoding(n);
+ encodings.push_back(enc);
+ names.push_back(wxFontMapper::GetEncodingName(enc));
+ }
+
+ int i = wxGetSingleChoiceIndex
+ (
+ wxT("Choose the encoding"),
+ SAMPLE_TITLE,
+ names,
+ this
+ );
+
+ return i == -1 ? wxFONTENCODING_SYSTEM : (wxFontEncoding)encodings[i];
+}
+
+void MyFrame::DoResizeFont(int diff)
+{
+ wxFont font = m_canvas->GetTextFont();
+
+ font.SetPointSize(font.GetPointSize() + diff);
+ DoChangeFont(font);
+}
+
+void MyFrame::OnBold(wxCommandEvent& event)
+{
+ wxFont font = m_canvas->GetTextFont();
+
+ font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
+ DoChangeFont(font);