1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont demo
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all standard wxWidgets headers
27 #include "wx/choicdlg.h"
28 #include "wx/fontdlg.h"
29 #include "wx/fontenum.h"
30 #include "wx/fontmap.h"
31 #include "wx/encconv.h"
32 #include "wx/splitter.h"
33 #include "wx/textfile.h"
35 #include "../sample.xpm"
39 #include "wx/mac/fontdlg.h"
42 // used as title for several dialog boxes
43 static const wxChar SAMPLE_TITLE
[] = _T("wxWidgets Font Sample");
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
62 // MyCanvas is a canvas on which we show the font sample
63 class MyCanvas
: public wxWindow
66 MyCanvas( wxWindow
*parent
);
67 virtual ~MyCanvas(){};
69 // accessors for the frame
70 const wxFont
& GetTextFont() const { return m_font
; }
71 const wxColour
& GetColour() const { return m_colour
; }
72 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
73 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
76 void OnPaint( wxPaintEvent
&event
);
85 // Define a new frame type: this is going to be our main frame
86 class MyFrame
: public wxFrame
90 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
93 MyCanvas
*GetCanvas() const { return m_canvas
; }
95 // event handlers (these functions should _not_ be virtual)
96 void OnQuit(wxCommandEvent
& event
);
97 void OnAbout(wxCommandEvent
& event
);
99 void OnIncFont(wxCommandEvent
& WXUNUSED(event
)) { DoResizeFont(+2); }
100 void OnDecFont(wxCommandEvent
& WXUNUSED(event
)) { DoResizeFont(-2); }
102 void OnBold(wxCommandEvent
& event
);
103 void OnItalic(wxCommandEvent
& event
);
104 void OnUnderline(wxCommandEvent
& event
);
106 void OnwxPointerFont(wxCommandEvent
& event
);
108 void OnTestTextValue(wxCommandEvent
& event
);
109 void OnViewMsg(wxCommandEvent
& event
);
110 void OnSelectFont(wxCommandEvent
& event
);
111 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
112 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
113 { DoEnumerateFamilies(false); }
114 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
115 { DoEnumerateFamilies(true); }
116 void OnEnumerateEncodings(wxCommandEvent
& event
);
118 void OnSetNativeDesc(wxCommandEvent
& event
);
119 void OnSetNativeUserDesc(wxCommandEvent
& event
);
120 void OnSetFaceName(wxCommandEvent
& event
);
121 void OnSetEncoding(wxCommandEvent
& event
);
124 bool DoEnumerateFamilies(bool fixedWidthOnly
,
125 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
126 bool silent
= false);
128 void DoResizeFont(int diff
);
129 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
131 // ask the user to choose an encoding and return it or
132 // wxFONTENCODING_SYSTEM if the dialog was cancelled
133 wxFontEncoding
GetEncodingFromUser();
136 size_t m_fontSize
; // in points
138 wxTextCtrl
*m_textctrl
;
142 // any class wishing to process wxWidgets events must use this macro
143 DECLARE_EVENT_TABLE()
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // IDs for the controls and the menu commands
171 Font_EnumFamiliesForEncoding
,
173 Font_EnumFixedFamilies
,
176 Font_SetNativeUserDesc
,
182 // ----------------------------------------------------------------------------
183 // event tables and other macros for wxWidgets
184 // ----------------------------------------------------------------------------
186 // the event tables connect the wxWidgets events with the functions (event
187 // handlers) which process them. It can be also done at run-time, but for the
188 // simple menu events like this the static method is much simpler.
189 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
190 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
191 EVT_MENU(Font_TestTextValue
, MyFrame::OnTestTextValue
)
192 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
193 EVT_MENU(Font_About
, MyFrame::OnAbout
)
195 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
196 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
197 EVT_MENU(Font_Bold
, MyFrame::OnBold
)
198 EVT_MENU(Font_Italic
, MyFrame::OnItalic
)
199 EVT_MENU(Font_Underlined
, MyFrame::OnUnderline
)
201 EVT_MENU(Font_wxNORMAL_FONT
, MyFrame::OnwxPointerFont
)
202 EVT_MENU(Font_wxSMALL_FONT
, MyFrame::OnwxPointerFont
)
203 EVT_MENU(Font_wxITALIC_FONT
, MyFrame::OnwxPointerFont
)
204 EVT_MENU(Font_wxSWISS_FONT
, MyFrame::OnwxPointerFont
)
207 EVT_MENU(Font_SetNativeDesc
, MyFrame::OnSetNativeDesc
)
208 EVT_MENU(Font_SetNativeUserDesc
, MyFrame::OnSetNativeUserDesc
)
209 EVT_MENU(Font_SetFaceName
, MyFrame::OnSetFaceName
)
210 EVT_MENU(Font_SetEncoding
, MyFrame::OnSetEncoding
)
212 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
213 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
214 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
215 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
216 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
219 // Create a new application object: this macro will allow wxWidgets to create
220 // the application object during program execution (it's better than using a
221 // static object for many reasons) and also declares the accessor function
222 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
226 // ============================================================================
228 // ============================================================================
230 // ----------------------------------------------------------------------------
231 // the application class
232 // ----------------------------------------------------------------------------
234 // `Main program' equivalent: the program execution "starts" here
237 // Create the main application window
238 MyFrame
*frame
= new MyFrame(wxT("Font wxWidgets demo"),
239 wxPoint(50, 50), wxSize(600, 400));
241 // Show it and tell the application that it's our main window
245 // success: wxApp::OnRun() will be called which will enter the main message
246 // loop and the application will run. If we returned 'false' here, the
247 // application would exit immediately.
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
256 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
257 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
), m_textctrl(NULL
)
259 m_fontSize
= wxNORMAL_FONT
->GetPointSize();
261 SetIcon(wxIcon(sample_xpm
));
264 wxMenu
*menuFile
= new wxMenu
;
266 menuFile
->Append(Font_TestTextValue
, wxT("&Test text value"),
267 wxT("Verify that getting and setting text value doesn't change it"));
268 menuFile
->Append(Font_ViewMsg
, wxT("&View...\tCtrl-V"),
269 wxT("View an email message file"));
270 menuFile
->AppendSeparator();
271 menuFile
->Append(Font_About
, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
272 menuFile
->AppendSeparator();
273 menuFile
->Append(Font_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
275 wxMenu
*menuFont
= new wxMenu
;
276 menuFont
->Append(Font_IncSize
, wxT("&Increase font size by 2 points\tCtrl-I"));
277 menuFont
->Append(Font_DecSize
, wxT("&Decrease font size by 2 points\tCtrl-D"));
278 menuFont
->AppendSeparator();
279 menuFont
->AppendCheckItem(Font_Bold
, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
280 menuFont
->AppendCheckItem(Font_Italic
, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
281 menuFont
->AppendCheckItem(Font_Underlined
, wxT("&Underlined\tCtrl-U"),
282 wxT("Toggle underlined state"));
284 menuFont
->AppendSeparator();
285 menuFont
->Append(Font_SetNativeDesc
,
286 wxT("Set native font &description\tShift-Ctrl-D"));
287 menuFont
->Append(Font_SetNativeUserDesc
,
288 wxT("Set &user font description\tShift-Ctrl-U"));
289 menuFont
->Append(Font_SetFaceName
, wxT("Check font face name"));
290 menuFont
->Append(Font_SetEncoding
, wxT("Set font &encoding\tShift-Ctrl-E"));
292 wxMenu
*menuSelect
= new wxMenu
;
293 menuSelect
->Append(Font_Choose
, wxT("&Select font...\tCtrl-S"),
294 wxT("Select a standard font"));
296 wxMenu
*menuStdFonts
= new wxMenu
;
297 menuStdFonts
->Append(Font_wxNORMAL_FONT
, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
298 menuStdFonts
->Append(Font_wxSMALL_FONT
, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
299 menuStdFonts
->Append(Font_wxITALIC_FONT
, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
300 menuStdFonts
->Append(Font_wxSWISS_FONT
, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
301 menuSelect
->Append(Font_Standard
, wxT("Standar&d fonts"), menuStdFonts
);
303 menuSelect
->AppendSeparator();
304 menuSelect
->Append(Font_EnumFamilies
, wxT("Enumerate font &families\tCtrl-F"));
305 menuSelect
->Append(Font_EnumFixedFamilies
,
306 wxT("Enumerate fi&xed font families\tCtrl-X"));
307 menuSelect
->Append(Font_EnumEncodings
,
308 wxT("Enumerate &encodings\tCtrl-E"));
309 menuSelect
->Append(Font_EnumFamiliesForEncoding
,
310 wxT("Find font for en&coding...\tCtrl-C"),
311 wxT("Find font families for given encoding"));
313 // now append the freshly created menu to the menu bar...
314 wxMenuBar
*menuBar
= new wxMenuBar
;
315 menuBar
->Append(menuFile
, wxT("&File"));
316 menuBar
->Append(menuFont
, wxT("F&ont"));
317 menuBar
->Append(menuSelect
, wxT("&Select"));
319 // ... and attach this menu bar to the frame
322 wxSplitterWindow
*splitter
= new wxSplitterWindow(this);
324 m_textctrl
= new wxTextCtrl(splitter
, wxID_ANY
,
325 wxT("Paste text here to see how it looks\nlike in the given font"),
326 wxDefaultPosition
, wxDefaultSize
,
329 m_canvas
= new MyCanvas(splitter
);
331 splitter
->SplitHorizontally(m_textctrl
, m_canvas
, 100);
334 // create a status bar just for fun (by default with 1 pane only)
336 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
337 #endif // wxUSE_STATUSBAR
340 // --------------------------------------------------------
342 class MyEncodingEnumerator
: public wxFontEnumerator
345 MyEncodingEnumerator()
348 const wxString
& GetText() const
352 virtual bool OnFontEncoding(const wxString
& facename
,
353 const wxString
& encoding
)
356 text
.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
357 (unsigned int) ++m_n
, encoding
.c_str(), facename
.c_str());
367 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
369 MyEncodingEnumerator fontEnumerator
;
371 fontEnumerator
.EnumerateEncodings();
373 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
374 fontEnumerator
.GetText().c_str());
377 // -------------------------------------------------------------
379 class MyFontEnumerator
: public wxFontEnumerator
383 { return !m_facenames
.IsEmpty(); }
385 const wxArrayString
& GetFacenames() const
386 { return m_facenames
; }
389 virtual bool OnFacename(const wxString
& facename
)
391 m_facenames
.Add(facename
);
396 wxArrayString m_facenames
;
399 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
400 wxFontEncoding encoding
,
403 MyFontEnumerator fontEnumerator
;
405 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
407 if ( fontEnumerator
.GotAny() )
409 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
412 wxLogStatus(this, wxT("Found %d %sfonts"),
413 nFacenames
, fixedWidthOnly
? wxT("fixed width ") : wxT(""));
421 facename
= fontEnumerator
.GetFacenames().Item(0);
425 // let the user choose
426 wxString
*facenames
= new wxString
[nFacenames
];
428 for ( n
= 0; n
< nFacenames
; n
++ )
429 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
431 n
= wxGetSingleChoiceIndex
433 wxT("Choose a facename"),
441 facename
= facenames
[n
];
446 if ( !facename
.empty() )
448 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
449 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
450 wxFONTWEIGHT_NORMAL
, false, facename
, encoding
);
459 wxLogWarning(wxT("No such fonts found."));
465 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
467 wxFontEncoding enc
= GetEncodingFromUser();
468 if ( enc
!= wxFONTENCODING_SYSTEM
)
470 DoEnumerateFamilies(false, enc
);
474 void MyFrame::OnSetNativeDesc(wxCommandEvent
& WXUNUSED(event
))
476 wxString fontInfo
= wxGetTextFromUser
478 wxT("Enter native font string"),
479 wxT("Input font description"),
480 m_canvas
->GetTextFont().GetNativeFontInfoDesc(),
483 if ( fontInfo
.empty() )
484 return; // user clicked "Cancel" - do nothing
487 font
.SetNativeFontInfo(fontInfo
);
490 wxLogError(wxT("Font info string \"%s\" is invalid."),
498 void MyFrame::OnSetFaceName(wxCommandEvent
& WXUNUSED(event
))
500 wxString facename
= GetCanvas()->GetTextFont().GetFaceName();
501 wxString newFaceName
= wxGetTextFromUser(
502 wxT("Here you can edit current font face name."),
503 wxT("Input font facename"), facename
,
505 if (newFaceName
.IsEmpty())
506 return; // user clicked "Cancel" - do nothing
508 wxFont
font(GetCanvas()->GetTextFont());
509 if (font
.SetFaceName(newFaceName
)) // change facename only
511 wxASSERT_MSG(font
.Ok(), wxT("The font should now be valid"));
516 wxASSERT_MSG(!font
.Ok(), wxT("The font should now be invalid"));
517 wxMessageBox(wxT("There is no font with such face name..."),
518 wxT("Invalid face name"), wxOK
|wxICON_ERROR
, this);
522 void MyFrame::OnSetNativeUserDesc(wxCommandEvent
& WXUNUSED(event
))
524 wxString fontdesc
= GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
525 wxString fontUserInfo
= wxGetTextFromUser(
526 wxT("Here you can edit current font description"),
527 wxT("Input font description"), fontdesc
,
529 if (fontUserInfo
.IsEmpty())
530 return; // user clicked "Cancel" - do nothing
533 if (font
.SetNativeFontInfoUserDesc(fontUserInfo
))
535 wxASSERT_MSG(font
.Ok(), wxT("The font should now be valid"));
540 wxASSERT_MSG(!font
.Ok(), wxT("The font should now be invalid"));
541 wxMessageBox(wxT("Error trying to create a font with such description..."));
545 void MyFrame::OnSetEncoding(wxCommandEvent
& WXUNUSED(event
))
547 wxFontEncoding enc
= GetEncodingFromUser();
548 if ( enc
== wxFONTENCODING_SYSTEM
)
551 wxFont font
= m_canvas
->GetTextFont();
552 font
.SetEncoding(enc
);
556 wxFontEncoding
MyFrame::GetEncodingFromUser()
559 wxArrayInt encodings
;
561 const size_t count
= wxFontMapper::GetSupportedEncodingsCount();
562 names
.reserve(count
);
563 encodings
.reserve(count
);
565 for ( size_t n
= 0; n
< count
; n
++ )
567 wxFontEncoding enc
= wxFontMapper::GetEncoding(n
);
568 encodings
.push_back(enc
);
569 names
.push_back(wxFontMapper::GetEncodingName(enc
));
572 int i
= wxGetSingleChoiceIndex
574 wxT("Choose the encoding"),
580 return i
== -1 ? wxFONTENCODING_SYSTEM
: (wxFontEncoding
)encodings
[i
];
583 void MyFrame::DoResizeFont(int diff
)
585 wxFont font
= m_canvas
->GetTextFont();
587 font
.SetPointSize(font
.GetPointSize() + diff
);
591 void MyFrame::OnBold(wxCommandEvent
& event
)
593 wxFont font
= m_canvas
->GetTextFont();
595 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_BOLD
: wxFONTWEIGHT_NORMAL
);
599 void MyFrame::OnItalic(wxCommandEvent
& event
)
601 wxFont font
= m_canvas
->GetTextFont();
603 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
);
607 void MyFrame::OnUnderline(wxCommandEvent
& event
)
609 wxFont font
= m_canvas
->GetTextFont();
611 font
.SetUnderlined(event
.IsChecked());
615 void MyFrame::OnwxPointerFont(wxCommandEvent
& event
)
619 switch ( event
.GetId() )
621 case Font_wxNORMAL_FONT
:
622 font
= *wxNORMAL_FONT
;
625 case Font_wxSMALL_FONT
:
626 font
= *wxSMALL_FONT
;
629 case Font_wxITALIC_FONT
:
630 font
= *wxITALIC_FONT
;
633 case Font_wxSWISS_FONT
:
634 font
= *wxSWISS_FONT
;
638 wxFAIL_MSG( wxT("unknown standard font") );
645 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
647 m_canvas
->SetTextFont(font
);
649 m_canvas
->SetColour(col
);
652 m_textctrl
->SetFont(font
);
654 m_textctrl
->SetForegroundColour(col
);
656 // update the state of the bold/italic/underlined menu items
657 wxMenuBar
*mbar
= GetMenuBar();
660 mbar
->Check(Font_Bold
, font
.GetWeight() == wxFONTWEIGHT_BOLD
);
661 mbar
->Check(Font_Italic
, font
.GetStyle() == wxFONTSTYLE_ITALIC
);
662 mbar
->Check(Font_Underlined
, font
.GetUnderlined());
666 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
669 data
.SetInitialFont(m_canvas
->GetTextFont());
670 data
.SetColour(m_canvas
->GetColour());
672 wxFontDialog
dialog(this, data
);
673 if ( dialog
.ShowModal() == wxID_OK
)
675 wxFontData retData
= dialog
.GetFontData();
676 wxFont font
= retData
.GetChosenFont();
677 wxColour colour
= retData
.GetColour();
679 DoChangeFont(font
, colour
);
683 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
685 // true is to force the frame to close
689 void MyFrame::OnTestTextValue(wxCommandEvent
& WXUNUSED(event
))
691 wxString value
= m_textctrl
->GetValue();
692 m_textctrl
->SetValue(value
);
693 if ( m_textctrl
->GetValue() != value
)
695 wxLogError(wxT("Text value changed after getting and setting it"));
699 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
702 // first, choose the file
703 static wxString s_dir
, s_file
;
704 wxFileDialog
dialog(this, wxT("Open an email message file"),
706 if ( dialog
.ShowModal() != wxID_OK
)
709 // save for the next time
710 s_dir
= dialog
.GetDirectory();
711 s_file
= dialog
.GetFilename();
713 wxString filename
= dialog
.GetPath();
715 // load it and search for Content-Type header
716 wxTextFile
file(filename
);
722 static const wxChar
*prefix
= wxT("Content-Type: text/plain; charset=");
723 const size_t len
= wxStrlen(prefix
);
725 size_t n
, count
= file
.GetLineCount();
726 for ( n
= 0; n
< count
; n
++ )
728 wxString line
= file
[n
];
732 // if it is an email message, headers are over, no need to parse
737 if ( line
.Left(len
) == prefix
)
740 const wxChar
*pc
= line
.c_str() + len
;
741 if ( *pc
== wxT('"') )
744 while ( *pc
&& *pc
!= wxT('"') )
755 wxLogError(wxT("The file '%s' doesn't contain charset information."),
761 // ok, now get the corresponding encoding
762 wxFontEncoding fontenc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
763 if ( fontenc
== wxFONTENCODING_SYSTEM
)
765 wxLogError(wxT("Charset '%s' is unsupported."), charset
.c_str());
769 m_textctrl
->LoadFile(filename
);
771 if ( fontenc
== wxFONTENCODING_UTF8
||
772 !wxFontMapper::Get()->IsEncodingAvailable(fontenc
) )
774 // try to find some similar encoding:
775 wxFontEncoding encAlt
;
776 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc
, &encAlt
) )
778 wxEncodingConverter conv
;
780 if (conv
.Init(fontenc
, encAlt
))
783 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
787 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
788 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
789 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
793 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
794 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
797 // and now create the correct font
798 if ( !DoEnumerateFamilies(false, fontenc
, true /* silent */) )
800 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
801 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
802 wxFONTWEIGHT_NORMAL
, false /* !underlined */,
803 wxEmptyString
/* facename */, fontenc
);
810 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
811 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
814 #endif // wxUSE_FILEDLG
817 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
819 wxMessageBox(wxT("wxWidgets font sample\n")
820 wxT("(c) 1999-2006 Vadim Zeitlin"),
821 wxString(wxT("About ")) + SAMPLE_TITLE
,
822 wxOK
| wxICON_INFORMATION
, this);
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
830 EVT_PAINT(MyCanvas::OnPaint
)
833 MyCanvas::MyCanvas( wxWindow
*parent
)
834 : wxWindow( parent
, wxID_ANY
),
835 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
839 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
845 dc
.SetBackground(wxBrush(wxT("white"), wxSOLID
));
848 // one text line height
849 wxCoord hLine
= dc
.GetCharHeight();
851 // the current text origin
855 // output the font name/info
857 fontInfo
.Printf(wxT("Font size is %d points, family: %s, encoding: %s"),
858 m_font
.GetPointSize(),
859 m_font
.GetFamilyString().c_str(),
861 GetEncodingDescription(m_font
.GetEncoding()).c_str());
863 dc
.DrawText(fontInfo
, x
, y
);
866 fontInfo
.Printf(wxT("Style: %s, weight: %s, fixed width: %s"),
867 m_font
.GetStyleString().c_str(),
868 m_font
.GetWeightString().c_str(),
869 m_font
.IsFixedWidth() ? _T("yes") : _T("no"));
871 dc
.DrawText(fontInfo
, x
, y
);
876 const wxNativeFontInfo
*info
= m_font
.GetNativeFontInfo();
879 wxString fontDesc
= m_font
.GetNativeFontInfoUserDesc();
880 fontInfo
.Printf(wxT("Native font info: %s"), fontDesc
.c_str());
882 dc
.DrawText(fontInfo
, x
, y
);
889 // prepare to draw the font
891 dc
.SetTextForeground(m_colour
);
893 // the size of one cell (Normally biggest char + small margin)
894 long maxCharWidth
, maxCharHeight
;
895 dc
.GetTextExtent(wxT("W"), &maxCharWidth
, &maxCharHeight
);
896 int w
= maxCharWidth
+ 5,
897 h
= maxCharHeight
+ 4;
900 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
901 for ( int i
= 0; i
< 7; i
++ )
903 for ( int j
= 0; j
< 32; j
++ )
905 wxChar c
= (wxChar
)(32 * (i
+ 1) + j
);
907 long charWidth
, charHeight
;
908 dc
.GetTextExtent(c
, &charWidth
, &charHeight
);
912 x
+ w
*j
+ (maxCharWidth
- charWidth
) / 2 + 1,
913 y
+ h
*i
+ (maxCharHeight
- charHeight
) / 2
918 // draw the lines between them
919 dc
.SetPen(wxPen(wxColour(_T("blue")), 1, wxSOLID
));
923 for ( l
= 0; l
< 8; l
++ )
925 int yl
= y
+ h
*l
- 2;
926 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 1, yl
);
930 for ( l
= 0; l
< 33; l
++ )
932 int xl
= x
+ w
*l
- 2;
933 dc
.DrawLine(xl
, y
- 2, xl
, y
+ 7*h
- 1);