1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont demo
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all standard wxWidgets headers
26 #include "wx/choicdlg.h"
27 #include "wx/fontdlg.h"
28 #include "wx/fontenum.h"
29 #include "wx/fontmap.h"
30 #include "wx/encconv.h"
31 #include "wx/splitter.h"
32 #include "wx/textfile.h"
33 #include "wx/settings.h"
35 #include "../sample.xpm"
39 #include "wx/osx/fontdlg.h"
42 // used as title for several dialog boxes
43 static const wxChar SAMPLE_TITLE
[] = wxT("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 OnLight(wxCommandEvent
& event
);
105 void OnItalic(wxCommandEvent
& event
);
106 void OnSlant(wxCommandEvent
& event
);
108 void OnUnderline(wxCommandEvent
& event
);
109 void OnStrikethrough(wxCommandEvent
& event
);
111 void OnwxPointerFont(wxCommandEvent
& event
);
112 void OnwxSystemSettingsFont(wxCommandEvent
& event
);
114 void OnTestTextValue(wxCommandEvent
& event
);
115 void OnViewMsg(wxCommandEvent
& event
);
116 void OnSelectFont(wxCommandEvent
& event
);
117 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
118 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
119 { DoEnumerateFamilies(false); }
120 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
121 { DoEnumerateFamilies(true); }
122 void OnEnumerateEncodings(wxCommandEvent
& event
);
124 void OnSetNativeDesc(wxCommandEvent
& event
);
125 void OnSetNativeUserDesc(wxCommandEvent
& event
);
127 void OnSetFamily(wxCommandEvent
& event
);
128 void OnSetFaceName(wxCommandEvent
& event
);
129 void OnSetEncoding(wxCommandEvent
& event
);
132 bool DoEnumerateFamilies(bool fixedWidthOnly
,
133 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
134 bool silent
= false);
136 void DoResizeFont(int diff
);
137 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
139 // ask the user to choose an encoding and return it or
140 // wxFONTENCODING_SYSTEM if the dialog was cancelled
141 wxFontEncoding
GetEncodingFromUser();
143 // ask the user to choose a font family and return it or
144 // wxFONTFAMILY_DEFAULT if the dialog was cancelled
145 wxFontFamily
GetFamilyFromUser();
147 size_t m_fontSize
; // in points
149 wxTextCtrl
*m_textctrl
;
153 // any class wishing to process wxWidgets events must use this macro
154 DECLARE_EVENT_TABLE()
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // IDs for the controls and the menu commands
165 Font_Quit
= wxID_EXIT
,
166 Font_About
= wxID_ABOUT
,
168 Font_ViewMsg
= wxID_HIGHEST
+1,
183 // standard global wxFont objects:
190 // wxSystemSettings::GetFont possible objects:
191 Font_wxSYS_OEM_FIXED_FONT
,
192 Font_wxSYS_ANSI_FIXED_FONT
,
193 Font_wxSYS_ANSI_VAR_FONT
,
194 Font_wxSYS_SYSTEM_FONT
,
195 Font_wxSYS_DEVICE_DEFAULT_FONT
,
196 Font_wxSYS_DEFAULT_GUI_FONT
,
200 Font_EnumFamiliesForEncoding
,
202 Font_EnumFixedFamilies
,
205 Font_SetNativeUserDesc
,
212 // ----------------------------------------------------------------------------
213 // event tables and other macros for wxWidgets
214 // ----------------------------------------------------------------------------
216 // the event tables connect the wxWidgets events with the functions (event
217 // handlers) which process them. It can be also done at run-time, but for the
218 // simple menu events like this the static method is much simpler.
219 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
220 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
221 EVT_MENU(Font_TestTextValue
, MyFrame::OnTestTextValue
)
222 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
223 EVT_MENU(Font_About
, MyFrame::OnAbout
)
225 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
226 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
228 EVT_MENU(Font_Bold
, MyFrame::OnBold
)
229 EVT_MENU(Font_Light
, MyFrame::OnLight
)
231 EVT_MENU(Font_Italic
, MyFrame::OnItalic
)
232 EVT_MENU(Font_Slant
, MyFrame::OnSlant
)
234 EVT_MENU(Font_Underlined
, MyFrame::OnUnderline
)
235 EVT_MENU(Font_Strikethrough
, MyFrame::OnStrikethrough
)
237 EVT_MENU(Font_wxNORMAL_FONT
, MyFrame::OnwxPointerFont
)
238 EVT_MENU(Font_wxSMALL_FONT
, MyFrame::OnwxPointerFont
)
239 EVT_MENU(Font_wxITALIC_FONT
, MyFrame::OnwxPointerFont
)
240 EVT_MENU(Font_wxSWISS_FONT
, MyFrame::OnwxPointerFont
)
242 EVT_MENU(Font_wxSYS_OEM_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
243 EVT_MENU(Font_wxSYS_ANSI_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
244 EVT_MENU(Font_wxSYS_ANSI_VAR_FONT
, MyFrame::OnwxSystemSettingsFont
)
245 EVT_MENU(Font_wxSYS_SYSTEM_FONT
, MyFrame::OnwxSystemSettingsFont
)
246 EVT_MENU(Font_wxSYS_DEVICE_DEFAULT_FONT
, MyFrame::OnwxSystemSettingsFont
)
247 EVT_MENU(Font_wxSYS_DEFAULT_GUI_FONT
, MyFrame::OnwxSystemSettingsFont
)
249 EVT_MENU(Font_SetNativeDesc
, MyFrame::OnSetNativeDesc
)
250 EVT_MENU(Font_SetNativeUserDesc
, MyFrame::OnSetNativeUserDesc
)
251 EVT_MENU(Font_SetFamily
, MyFrame::OnSetFamily
)
252 EVT_MENU(Font_SetFaceName
, MyFrame::OnSetFaceName
)
253 EVT_MENU(Font_SetEncoding
, MyFrame::OnSetEncoding
)
255 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
256 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
257 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
258 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
259 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
262 // Create a new application object: this macro will allow wxWidgets to create
263 // the application object during program execution (it's better than using a
264 // static object for many reasons) and also declares the accessor function
265 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
269 // ============================================================================
271 // ============================================================================
273 // ----------------------------------------------------------------------------
274 // the application class
275 // ----------------------------------------------------------------------------
277 // `Main program' equivalent: the program execution "starts" here
280 if ( !wxApp::OnInit() )
283 // Create the main application window
284 MyFrame
*frame
= new MyFrame(wxT("Font wxWidgets demo"),
285 wxPoint(50, 50), wxSize(600, 400));
290 // success: wxApp::OnRun() will be called which will enter the main message
291 // loop and the application will run. If we returned 'false' here, the
292 // application would exit immediately.
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
301 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
302 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
), m_textctrl(NULL
)
304 m_fontSize
= wxNORMAL_FONT
->GetPointSize();
306 SetIcon(wxICON(sample
));
309 wxMenu
*menuFile
= new wxMenu
;
311 menuFile
->Append(Font_TestTextValue
, wxT("&Test text value"),
312 wxT("Verify that getting and setting text value doesn't change it"));
313 menuFile
->Append(Font_ViewMsg
, wxT("&View...\tCtrl-V"),
314 wxT("View an email message file"));
315 menuFile
->AppendSeparator();
316 menuFile
->Append(Font_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
317 menuFile
->AppendSeparator();
318 menuFile
->Append(Font_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
320 wxMenu
*menuFont
= new wxMenu
;
321 menuFont
->Append(Font_IncSize
, wxT("&Increase font size by 2 points\tCtrl-I"));
322 menuFont
->Append(Font_DecSize
, wxT("&Decrease font size by 2 points\tCtrl-D"));
323 menuFont
->AppendSeparator();
324 menuFont
->AppendCheckItem(Font_Bold
, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
325 menuFont
->AppendCheckItem(Font_Light
, wxT("&Light\tCtrl-L"), wxT("Toggle light state"));
326 menuFont
->AppendSeparator();
327 menuFont
->AppendCheckItem(Font_Italic
, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
329 // under wxMSW slant == italic so there's no reason to provide another menu item for the same thing
330 menuFont
->AppendCheckItem(Font_Slant
, wxT("&Slant\tCtrl-S"), wxT("Toggle slant state"));
332 menuFont
->AppendSeparator();
333 menuFont
->AppendCheckItem(Font_Underlined
, wxT("&Underlined\tCtrl-U"),
334 wxT("Toggle underlined state"));
335 menuFont
->AppendCheckItem(Font_Strikethrough
, wxT("&Strikethrough"),
336 wxT("Toggle strikethrough state"));
338 menuFont
->AppendSeparator();
339 menuFont
->Append(Font_SetNativeDesc
,
340 wxT("Set native font &description\tShift-Ctrl-D"));
341 menuFont
->Append(Font_SetNativeUserDesc
,
342 wxT("Set &user font description\tShift-Ctrl-U"));
343 menuFont
->AppendSeparator();
344 menuFont
->Append(Font_SetFamily
, wxT("Set font family"));
345 menuFont
->Append(Font_SetFaceName
, wxT("Set font face name"));
346 menuFont
->Append(Font_SetEncoding
, wxT("Set font &encoding\tShift-Ctrl-E"));
348 wxMenu
*menuSelect
= new wxMenu
;
349 menuSelect
->Append(Font_Choose
, wxT("&Select font...\tCtrl-S"),
350 wxT("Select a standard font"));
352 wxMenu
*menuStdFonts
= new wxMenu
;
353 menuStdFonts
->Append(Font_wxNORMAL_FONT
, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
354 menuStdFonts
->Append(Font_wxSMALL_FONT
, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
355 menuStdFonts
->Append(Font_wxITALIC_FONT
, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
356 menuStdFonts
->Append(Font_wxSWISS_FONT
, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
357 menuSelect
->Append(Font_Standard
, wxT("Standar&d fonts"), menuStdFonts
);
359 wxMenu
*menuSettingFonts
= new wxMenu
;
360 menuSettingFonts
->Append(Font_wxSYS_OEM_FIXED_FONT
, wxT("wxSYS_OEM_FIXED_FONT"),
361 wxT("Original equipment manufacturer dependent fixed-pitch font."));
362 menuSettingFonts
->Append(Font_wxSYS_ANSI_FIXED_FONT
, wxT("wxSYS_ANSI_FIXED_FONT"),
363 wxT("Windows fixed-pitch (monospaced) font. "));
364 menuSettingFonts
->Append(Font_wxSYS_ANSI_VAR_FONT
, wxT("wxSYS_ANSI_VAR_FONT"),
365 wxT("Windows variable-pitch (proportional) font."));
366 menuSettingFonts
->Append(Font_wxSYS_SYSTEM_FONT
, wxT("wxSYS_SYSTEM_FONT"),
367 wxT("System font."));
368 menuSettingFonts
->Append(Font_wxSYS_DEVICE_DEFAULT_FONT
, wxT("wxSYS_DEVICE_DEFAULT_FONT"),
369 wxT("Device-dependent font."));
370 menuSettingFonts
->Append(Font_wxSYS_DEFAULT_GUI_FONT
, wxT("wxSYS_DEFAULT_GUI_FONT"),
371 wxT("Default font for user interface objects such as menus and dialog boxes. "));
372 menuSelect
->Append(Font_SystemSettings
, wxT("System fonts"), menuSettingFonts
);
375 menuSelect
->AppendSeparator();
376 menuSelect
->Append(Font_EnumFamilies
, wxT("Enumerate font &families\tCtrl-F"));
377 menuSelect
->Append(Font_EnumFixedFamilies
,
378 wxT("Enumerate fi&xed font families\tCtrl-X"));
379 menuSelect
->Append(Font_EnumEncodings
,
380 wxT("Enumerate &encodings\tCtrl-E"));
381 menuSelect
->Append(Font_EnumFamiliesForEncoding
,
382 wxT("Find font for en&coding...\tCtrl-C"),
383 wxT("Find font families for given encoding"));
385 // now append the freshly created menu to the menu bar...
386 wxMenuBar
*menuBar
= new wxMenuBar
;
387 menuBar
->Append(menuFile
, wxT("&File"));
388 menuBar
->Append(menuFont
, wxT("F&ont"));
389 menuBar
->Append(menuSelect
, wxT("&Select"));
391 // ... and attach this menu bar to the frame
394 wxSplitterWindow
*splitter
= new wxSplitterWindow(this);
396 m_textctrl
= new wxTextCtrl(splitter
, wxID_ANY
,
397 wxT("Paste text here to see how it looks\nlike in the given font"),
398 wxDefaultPosition
, wxDefaultSize
,
401 m_canvas
= new MyCanvas(splitter
);
403 splitter
->SplitHorizontally(m_textctrl
, m_canvas
, 100);
406 // create a status bar just for fun (by default with 1 pane only)
408 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
409 #endif // wxUSE_STATUSBAR
412 // --------------------------------------------------------
414 class MyEncodingEnumerator
: public wxFontEnumerator
417 MyEncodingEnumerator()
420 const wxString
& GetText() const
424 virtual bool OnFontEncoding(const wxString
& facename
,
425 const wxString
& encoding
)
428 text
.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
429 (unsigned int) ++m_n
, encoding
.c_str(), facename
.c_str());
439 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
441 MyEncodingEnumerator fontEnumerator
;
443 fontEnumerator
.EnumerateEncodings();
445 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
446 fontEnumerator
.GetText().c_str());
449 // -------------------------------------------------------------
451 class MyFontEnumerator
: public wxFontEnumerator
455 { return !m_facenames
.IsEmpty(); }
457 const wxArrayString
& GetFacenames() const
458 { return m_facenames
; }
461 virtual bool OnFacename(const wxString
& facename
)
463 m_facenames
.Add(facename
);
468 wxArrayString m_facenames
;
471 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
472 wxFontEncoding encoding
,
475 MyFontEnumerator fontEnumerator
;
477 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
479 if ( fontEnumerator
.GotAny() )
481 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
484 wxLogStatus(this, wxT("Found %d %sfonts"),
485 nFacenames
, fixedWidthOnly
? wxT("fixed width ") : wxT(""));
493 facename
= fontEnumerator
.GetFacenames().Item(0);
497 // let the user choose
498 wxString
*facenames
= new wxString
[nFacenames
];
500 for ( n
= 0; n
< nFacenames
; n
++ )
501 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
503 n
= wxGetSingleChoiceIndex
505 wxT("Choose a facename"),
513 facename
= facenames
[n
];
518 if ( !facename
.empty() )
520 wxFont
font(wxFontInfo().FaceName(facename
).Encoding(encoding
));
529 wxLogWarning(wxT("No such fonts found."));
535 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
537 wxFontEncoding enc
= GetEncodingFromUser();
538 if ( enc
!= wxFONTENCODING_SYSTEM
)
540 DoEnumerateFamilies(false, enc
);
544 void MyFrame::OnSetNativeDesc(wxCommandEvent
& WXUNUSED(event
))
546 wxString fontInfo
= wxGetTextFromUser
548 wxT("Enter native font string"),
549 wxT("Input font description"),
550 m_canvas
->GetTextFont().GetNativeFontInfoDesc(),
553 if ( fontInfo
.empty() )
554 return; // user clicked "Cancel" - do nothing
557 font
.SetNativeFontInfo(fontInfo
);
560 wxLogError(wxT("Font info string \"%s\" is invalid."),
568 void MyFrame::OnSetNativeUserDesc(wxCommandEvent
& WXUNUSED(event
))
570 wxString fontdesc
= GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
571 wxString fontUserInfo
= wxGetTextFromUser(
572 wxT("Here you can edit current font description"),
573 wxT("Input font description"), fontdesc
,
575 if (fontUserInfo
.IsEmpty())
576 return; // user clicked "Cancel" - do nothing
579 if (font
.SetNativeFontInfoUserDesc(fontUserInfo
))
581 wxASSERT_MSG(font
.IsOk(), wxT("The font should now be valid"));
586 wxASSERT_MSG(!font
.IsOk(), wxT("The font should now be invalid"));
587 wxMessageBox(wxT("Error trying to create a font with such description..."));
591 void MyFrame::OnSetFamily(wxCommandEvent
& WXUNUSED(event
))
593 wxFontFamily f
= GetFamilyFromUser();
595 wxFont font
= m_canvas
->GetTextFont();
600 void MyFrame::OnSetFaceName(wxCommandEvent
& WXUNUSED(event
))
602 wxString facename
= GetCanvas()->GetTextFont().GetFaceName();
603 wxString newFaceName
= wxGetTextFromUser(
604 wxT("Here you can edit current font face name."),
605 wxT("Input font facename"), facename
,
607 if (newFaceName
.IsEmpty())
608 return; // user clicked "Cancel" - do nothing
610 wxFont
font(GetCanvas()->GetTextFont());
611 if (font
.SetFaceName(newFaceName
)) // change facename only
613 wxASSERT_MSG(font
.IsOk(), wxT("The font should now be valid"));
618 wxASSERT_MSG(!font
.IsOk(), wxT("The font should now be invalid"));
619 wxMessageBox(wxT("There is no font with such face name..."),
620 wxT("Invalid face name"), wxOK
|wxICON_ERROR
, this);
624 void MyFrame::OnSetEncoding(wxCommandEvent
& WXUNUSED(event
))
626 wxFontEncoding enc
= GetEncodingFromUser();
627 if ( enc
== wxFONTENCODING_SYSTEM
)
630 wxFont font
= m_canvas
->GetTextFont();
631 font
.SetEncoding(enc
);
635 wxFontEncoding
MyFrame::GetEncodingFromUser()
638 wxArrayInt encodings
;
640 const size_t count
= wxFontMapper::GetSupportedEncodingsCount();
641 names
.reserve(count
);
642 encodings
.reserve(count
);
644 for ( size_t n
= 0; n
< count
; n
++ )
646 wxFontEncoding enc
= wxFontMapper::GetEncoding(n
);
647 encodings
.push_back(enc
);
648 names
.push_back(wxFontMapper::GetEncodingName(enc
));
651 int i
= wxGetSingleChoiceIndex
653 wxT("Choose the encoding"),
659 return i
== -1 ? wxFONTENCODING_SYSTEM
: (wxFontEncoding
)encodings
[i
];
662 wxFontFamily
MyFrame::GetFamilyFromUser()
667 families
.push_back(wxFONTFAMILY_DECORATIVE
);
668 families
.push_back(wxFONTFAMILY_ROMAN
);
669 families
.push_back(wxFONTFAMILY_SCRIPT
);
670 families
.push_back(wxFONTFAMILY_SWISS
);
671 families
.push_back(wxFONTFAMILY_MODERN
);
672 families
.push_back(wxFONTFAMILY_TELETYPE
);
674 names
.push_back("DECORATIVE");
675 names
.push_back("ROMAN");
676 names
.push_back("SCRIPT");
677 names
.push_back("SWISS");
678 names
.push_back("MODERN");
679 names
.push_back("TELETYPE");
681 int i
= wxGetSingleChoiceIndex
683 wxT("Choose the family"),
689 return i
== -1 ? wxFONTFAMILY_DEFAULT
: (wxFontFamily
)families
[i
];
692 void MyFrame::DoResizeFont(int diff
)
694 wxFont font
= m_canvas
->GetTextFont();
696 font
.SetPointSize(font
.GetPointSize() + diff
);
700 void MyFrame::OnBold(wxCommandEvent
& event
)
702 wxFont font
= m_canvas
->GetTextFont();
704 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_BOLD
: wxFONTWEIGHT_NORMAL
);
708 void MyFrame::OnLight(wxCommandEvent
& event
)
710 wxFont font
= m_canvas
->GetTextFont();
712 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_LIGHT
: wxFONTWEIGHT_NORMAL
);
716 void MyFrame::OnItalic(wxCommandEvent
& event
)
718 wxFont font
= m_canvas
->GetTextFont();
720 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
);
724 void MyFrame::OnSlant(wxCommandEvent
& event
)
726 wxFont font
= m_canvas
->GetTextFont();
728 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_SLANT
: wxFONTSTYLE_NORMAL
);
732 void MyFrame::OnUnderline(wxCommandEvent
& event
)
734 wxFont font
= m_canvas
->GetTextFont();
736 font
.SetUnderlined(event
.IsChecked());
740 void MyFrame::OnStrikethrough(wxCommandEvent
& event
)
742 wxFont font
= m_canvas
->GetTextFont();
743 font
.SetStrikethrough(event
.IsChecked());
747 void MyFrame::OnwxPointerFont(wxCommandEvent
& event
)
751 switch ( event
.GetId() )
753 case Font_wxNORMAL_FONT
:
754 font
= *wxNORMAL_FONT
;
757 case Font_wxSMALL_FONT
:
758 font
= *wxSMALL_FONT
;
761 case Font_wxITALIC_FONT
:
762 font
= *wxITALIC_FONT
;
765 case Font_wxSWISS_FONT
:
766 font
= *wxSWISS_FONT
;
770 wxFAIL_MSG( wxT("unknown standard font") );
777 void MyFrame::OnwxSystemSettingsFont(wxCommandEvent
& event
)
781 switch ( event
.GetId() )
783 case Font_wxSYS_OEM_FIXED_FONT
:
784 font
= wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
);
787 case Font_wxSYS_ANSI_FIXED_FONT
:
788 font
= wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT
);
791 case Font_wxSYS_ANSI_VAR_FONT
:
792 font
= wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT
);
795 case Font_wxSYS_SYSTEM_FONT
:
796 font
= wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
);
799 case Font_wxSYS_DEVICE_DEFAULT_FONT
:
800 font
= wxSystemSettings::GetFont(wxSYS_DEVICE_DEFAULT_FONT
);
803 case Font_wxSYS_DEFAULT_GUI_FONT
:
804 font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
808 wxFAIL_MSG( wxT("unknown standard font") );
815 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
817 m_canvas
->SetTextFont(font
);
819 m_canvas
->SetColour(col
);
822 m_textctrl
->SetFont(font
);
824 m_textctrl
->SetForegroundColour(col
);
825 m_textctrl
->Refresh();
827 // update the state of the bold/italic/underlined menu items
828 wxMenuBar
*mbar
= GetMenuBar();
831 mbar
->Check(Font_Light
, font
.GetWeight() == wxFONTWEIGHT_LIGHT
);
832 mbar
->Check(Font_Bold
, font
.GetWeight() == wxFONTWEIGHT_BOLD
);
834 mbar
->Check(Font_Italic
, font
.GetStyle() == wxFONTSTYLE_ITALIC
);
836 mbar
->Check(Font_Slant
, font
.GetStyle() == wxFONTSTYLE_SLANT
);
839 mbar
->Check(Font_Underlined
, font
.GetUnderlined());
840 mbar
->Check(Font_Strikethrough
, font
.GetStrikethrough());
844 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
847 data
.SetInitialFont(m_canvas
->GetTextFont());
848 data
.SetColour(m_canvas
->GetColour());
850 wxFontDialog
dialog(this, data
);
851 if ( dialog
.ShowModal() == wxID_OK
)
853 wxFontData retData
= dialog
.GetFontData();
854 wxFont font
= retData
.GetChosenFont();
855 wxColour colour
= retData
.GetColour();
857 DoChangeFont(font
, colour
);
861 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
863 // true is to force the frame to close
867 void MyFrame::OnTestTextValue(wxCommandEvent
& WXUNUSED(event
))
869 wxString value
= m_textctrl
->GetValue();
870 m_textctrl
->SetValue(value
);
871 if ( m_textctrl
->GetValue() != value
)
873 wxLogError(wxT("Text value changed after getting and setting it"));
877 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
880 // first, choose the file
881 static wxString s_dir
, s_file
;
882 wxFileDialog
dialog(this, wxT("Open an email message file"),
884 if ( dialog
.ShowModal() != wxID_OK
)
887 // save for the next time
888 s_dir
= dialog
.GetDirectory();
889 s_file
= dialog
.GetFilename();
891 wxString filename
= dialog
.GetPath();
893 // load it and search for Content-Type header
894 wxTextFile
file(filename
);
900 static const wxChar
*prefix
= wxT("Content-Type: text/plain; charset=");
901 const size_t len
= wxStrlen(prefix
);
903 size_t n
, count
= file
.GetLineCount();
904 for ( n
= 0; n
< count
; n
++ )
906 wxString line
= file
[n
];
910 // if it is an email message, headers are over, no need to parse
915 if ( line
.Left(len
) == prefix
)
918 const wxChar
*pc
= line
.c_str() + len
;
919 if ( *pc
== wxT('"') )
922 while ( *pc
&& *pc
!= wxT('"') )
933 wxLogError(wxT("The file '%s' doesn't contain charset information."),
939 // ok, now get the corresponding encoding
940 wxFontEncoding fontenc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
941 if ( fontenc
== wxFONTENCODING_SYSTEM
)
943 wxLogError(wxT("Charset '%s' is unsupported."), charset
.c_str());
947 m_textctrl
->LoadFile(filename
);
949 if ( fontenc
== wxFONTENCODING_UTF8
||
950 !wxFontMapper::Get()->IsEncodingAvailable(fontenc
) )
952 // try to find some similar encoding:
953 wxFontEncoding encAlt
;
954 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc
, &encAlt
) )
956 wxEncodingConverter conv
;
958 if (conv
.Init(fontenc
, encAlt
))
961 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
965 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
966 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
967 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
971 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
972 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
975 // and now create the correct font
976 if ( !DoEnumerateFamilies(false, fontenc
, true /* silent */) )
978 wxFont
font(wxFontInfo(wxNORMAL_FONT
->GetPointSize()).Encoding(fontenc
));
985 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
986 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
989 #endif // wxUSE_FILEDLG
992 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
994 wxMessageBox(wxT("wxWidgets font sample\n")
995 wxT("(c) 1999-2006 Vadim Zeitlin"),
996 wxString(wxT("About ")) + SAMPLE_TITLE
,
997 wxOK
| wxICON_INFORMATION
, this);
1000 // ----------------------------------------------------------------------------
1002 // ----------------------------------------------------------------------------
1004 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
1005 EVT_PAINT(MyCanvas::OnPaint
)
1008 MyCanvas::MyCanvas( wxWindow
*parent
)
1009 : wxWindow( parent
, wxID_ANY
),
1010 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
1014 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1020 dc
.SetBackground(*wxWHITE_BRUSH
);
1024 // one text line height
1025 wxCoord hLine
= dc
.GetCharHeight();
1027 // the current text origin
1031 // output the font name/info
1034 fontInfo
.Printf(wxT("Face name: %s, family: %s"),
1035 m_font
.GetFaceName().c_str(),
1036 m_font
.GetFamilyString().c_str());
1038 dc
.DrawText(fontInfo
, x
, y
);
1041 fontInfo
.Printf(wxT("Size: %d points or %d pixels; %d*%d average char size"),
1042 m_font
.GetPointSize(),
1043 m_font
.GetPixelSize().y
,
1044 dc
.GetCharWidth(), dc
.GetCharHeight());
1046 dc
.DrawText(fontInfo
, x
, y
);
1049 fontInfo
.Printf(wxT("Style: %s, weight: %s, fixed width: %s, encoding: %s"),
1050 m_font
.GetStyleString().c_str(),
1051 m_font
.GetWeightString().c_str(),
1052 m_font
.IsFixedWidth() ? wxT("yes") : wxT("no"),
1053 wxFontMapper::GetEncodingDescription(m_font
.GetEncoding()));
1055 dc
.DrawText(fontInfo
, x
, y
);
1058 if ( m_font
.IsOk() )
1060 const wxNativeFontInfo
*info
= m_font
.GetNativeFontInfo();
1063 wxString fontDesc
= m_font
.GetNativeFontInfoUserDesc();
1064 fontInfo
.Printf(wxT("Native font info: %s"), fontDesc
.c_str());
1066 dc
.DrawText(fontInfo
, x
, y
);
1073 // prepare to draw the font
1074 dc
.SetTextForeground(m_colour
);
1076 // the size of one cell (Normally biggest char + small margin)
1077 wxCoord maxCharWidth
, maxCharHeight
;
1078 dc
.GetTextExtent(wxT("W"), &maxCharWidth
, &maxCharHeight
);
1079 int w
= maxCharWidth
+ 5,
1080 h
= maxCharHeight
+ 4;
1083 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
1084 for ( int i
= 0; i
< 7; i
++ )
1086 for ( int j
= 0; j
< 32; j
++ )
1088 wxChar c
= (wxChar
)(32 * (i
+ 1) + j
);
1090 wxCoord charWidth
, charHeight
;
1091 dc
.GetTextExtent(c
, &charWidth
, &charHeight
);
1095 x
+ w
*j
+ (maxCharWidth
- charWidth
) / 2 + 1,
1096 y
+ h
*i
+ (maxCharHeight
- charHeight
) / 2
1101 // draw the lines between them
1102 dc
.SetPen(*wxBLUE_PEN
);
1106 for ( l
= 0; l
< 8; l
++ )
1108 int yl
= y
+ h
*l
- 2;
1109 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 1, yl
);
1113 for ( l
= 0; l
< 33; l
++ )
1115 int xl
= x
+ w
*l
- 2;
1116 dc
.DrawLine(xl
, y
- 2, xl
, y
+ 7*h
- 1);