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"
34 #include "wx/settings.h"
36 #include "../sample.xpm"
40 #include "wx/osx/fontdlg.h"
43 // used as title for several dialog boxes
44 static const wxChar SAMPLE_TITLE
[] = wxT("wxWidgets Font Sample");
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // override base class virtuals
55 // ----------------------------
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
63 // MyCanvas is a canvas on which we show the font sample
64 class MyCanvas
: public wxWindow
67 MyCanvas( wxWindow
*parent
);
68 virtual ~MyCanvas(){};
70 // accessors for the frame
71 const wxFont
& GetTextFont() const { return m_font
; }
72 const wxColour
& GetColour() const { return m_colour
; }
73 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
74 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
77 void OnPaint( wxPaintEvent
&event
);
86 // Define a new frame type: this is going to be our main frame
87 class MyFrame
: public wxFrame
91 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
94 MyCanvas
*GetCanvas() const { return m_canvas
; }
96 // event handlers (these functions should _not_ be virtual)
97 void OnQuit(wxCommandEvent
& event
);
98 void OnAbout(wxCommandEvent
& event
);
100 void OnIncFont(wxCommandEvent
& WXUNUSED(event
)) { DoResizeFont(+2); }
101 void OnDecFont(wxCommandEvent
& WXUNUSED(event
)) { DoResizeFont(-2); }
103 void OnBold(wxCommandEvent
& event
);
104 void OnLight(wxCommandEvent
& event
);
106 void OnItalic(wxCommandEvent
& event
);
107 void OnSlant(wxCommandEvent
& event
);
109 void OnUnderline(wxCommandEvent
& event
);
110 void OnStrikethrough(wxCommandEvent
& event
);
112 void OnwxPointerFont(wxCommandEvent
& event
);
113 void OnwxSystemSettingsFont(wxCommandEvent
& event
);
115 void OnTestTextValue(wxCommandEvent
& event
);
116 void OnViewMsg(wxCommandEvent
& event
);
117 void OnSelectFont(wxCommandEvent
& event
);
118 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
119 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
120 { DoEnumerateFamilies(false); }
121 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
122 { DoEnumerateFamilies(true); }
123 void OnEnumerateEncodings(wxCommandEvent
& event
);
125 void OnSetNativeDesc(wxCommandEvent
& event
);
126 void OnSetNativeUserDesc(wxCommandEvent
& event
);
128 void OnSetFamily(wxCommandEvent
& event
);
129 void OnSetFaceName(wxCommandEvent
& event
);
130 void OnSetEncoding(wxCommandEvent
& event
);
133 bool DoEnumerateFamilies(bool fixedWidthOnly
,
134 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
135 bool silent
= false);
137 void DoResizeFont(int diff
);
138 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
140 // ask the user to choose an encoding and return it or
141 // wxFONTENCODING_SYSTEM if the dialog was cancelled
142 wxFontEncoding
GetEncodingFromUser();
144 // ask the user to choose a font family and return it or
145 // wxFONTFAMILY_DEFAULT if the dialog was cancelled
146 wxFontFamily
GetFamilyFromUser();
148 size_t m_fontSize
; // in points
150 wxTextCtrl
*m_textctrl
;
154 // any class wishing to process wxWidgets events must use this macro
155 DECLARE_EVENT_TABLE()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
166 Font_Quit
= wxID_EXIT
,
167 Font_About
= wxID_ABOUT
,
169 Font_ViewMsg
= wxID_HIGHEST
+1,
184 // standard global wxFont objects:
191 // wxSystemSettings::GetFont possible objects:
192 Font_wxSYS_OEM_FIXED_FONT
,
193 Font_wxSYS_ANSI_FIXED_FONT
,
194 Font_wxSYS_ANSI_VAR_FONT
,
195 Font_wxSYS_SYSTEM_FONT
,
196 Font_wxSYS_DEVICE_DEFAULT_FONT
,
197 Font_wxSYS_DEFAULT_GUI_FONT
,
201 Font_EnumFamiliesForEncoding
,
203 Font_EnumFixedFamilies
,
206 Font_SetNativeUserDesc
,
213 // ----------------------------------------------------------------------------
214 // event tables and other macros for wxWidgets
215 // ----------------------------------------------------------------------------
217 // the event tables connect the wxWidgets events with the functions (event
218 // handlers) which process them. It can be also done at run-time, but for the
219 // simple menu events like this the static method is much simpler.
220 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
221 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
222 EVT_MENU(Font_TestTextValue
, MyFrame::OnTestTextValue
)
223 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
224 EVT_MENU(Font_About
, MyFrame::OnAbout
)
226 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
227 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
229 EVT_MENU(Font_Bold
, MyFrame::OnBold
)
230 EVT_MENU(Font_Light
, MyFrame::OnLight
)
232 EVT_MENU(Font_Italic
, MyFrame::OnItalic
)
233 EVT_MENU(Font_Slant
, MyFrame::OnSlant
)
235 EVT_MENU(Font_Underlined
, MyFrame::OnUnderline
)
236 EVT_MENU(Font_Strikethrough
, MyFrame::OnStrikethrough
)
238 EVT_MENU(Font_wxNORMAL_FONT
, MyFrame::OnwxPointerFont
)
239 EVT_MENU(Font_wxSMALL_FONT
, MyFrame::OnwxPointerFont
)
240 EVT_MENU(Font_wxITALIC_FONT
, MyFrame::OnwxPointerFont
)
241 EVT_MENU(Font_wxSWISS_FONT
, MyFrame::OnwxPointerFont
)
243 EVT_MENU(Font_wxSYS_OEM_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
244 EVT_MENU(Font_wxSYS_ANSI_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
245 EVT_MENU(Font_wxSYS_ANSI_VAR_FONT
, MyFrame::OnwxSystemSettingsFont
)
246 EVT_MENU(Font_wxSYS_SYSTEM_FONT
, MyFrame::OnwxSystemSettingsFont
)
247 EVT_MENU(Font_wxSYS_DEVICE_DEFAULT_FONT
, MyFrame::OnwxSystemSettingsFont
)
248 EVT_MENU(Font_wxSYS_DEFAULT_GUI_FONT
, MyFrame::OnwxSystemSettingsFont
)
250 EVT_MENU(Font_SetNativeDesc
, MyFrame::OnSetNativeDesc
)
251 EVT_MENU(Font_SetNativeUserDesc
, MyFrame::OnSetNativeUserDesc
)
252 EVT_MENU(Font_SetFamily
, MyFrame::OnSetFamily
)
253 EVT_MENU(Font_SetFaceName
, MyFrame::OnSetFaceName
)
254 EVT_MENU(Font_SetEncoding
, MyFrame::OnSetEncoding
)
256 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
257 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
258 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
259 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
260 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
263 // Create a new application object: this macro will allow wxWidgets to create
264 // the application object during program execution (it's better than using a
265 // static object for many reasons) and also declares the accessor function
266 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
270 // ============================================================================
272 // ============================================================================
274 // ----------------------------------------------------------------------------
275 // the application class
276 // ----------------------------------------------------------------------------
278 // `Main program' equivalent: the program execution "starts" here
281 if ( !wxApp::OnInit() )
284 // Create the main application window
285 MyFrame
*frame
= new MyFrame(wxT("Font wxWidgets demo"),
286 wxPoint(50, 50), wxSize(600, 400));
291 // success: wxApp::OnRun() will be called which will enter the main message
292 // loop and the application will run. If we returned 'false' here, the
293 // application would exit immediately.
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
302 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
303 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
), m_textctrl(NULL
)
305 m_fontSize
= wxNORMAL_FONT
->GetPointSize();
307 SetIcon(wxICON(sample
));
310 wxMenu
*menuFile
= new wxMenu
;
312 menuFile
->Append(Font_TestTextValue
, wxT("&Test text value"),
313 wxT("Verify that getting and setting text value doesn't change it"));
314 menuFile
->Append(Font_ViewMsg
, wxT("&View...\tCtrl-V"),
315 wxT("View an email message file"));
316 menuFile
->AppendSeparator();
317 menuFile
->Append(Font_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
318 menuFile
->AppendSeparator();
319 menuFile
->Append(Font_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
321 wxMenu
*menuFont
= new wxMenu
;
322 menuFont
->Append(Font_IncSize
, wxT("&Increase font size by 2 points\tCtrl-I"));
323 menuFont
->Append(Font_DecSize
, wxT("&Decrease font size by 2 points\tCtrl-D"));
324 menuFont
->AppendSeparator();
325 menuFont
->AppendCheckItem(Font_Bold
, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
326 menuFont
->AppendCheckItem(Font_Light
, wxT("&Light\tCtrl-L"), wxT("Toggle light state"));
327 menuFont
->AppendSeparator();
328 menuFont
->AppendCheckItem(Font_Italic
, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
330 // under wxMSW slant == italic so there's no reason to provide another menu item for the same thing
331 menuFont
->AppendCheckItem(Font_Slant
, wxT("&Slant\tCtrl-S"), wxT("Toggle slant state"));
333 menuFont
->AppendSeparator();
334 menuFont
->AppendCheckItem(Font_Underlined
, wxT("&Underlined\tCtrl-U"),
335 wxT("Toggle underlined state"));
336 menuFont
->AppendCheckItem(Font_Strikethrough
, wxT("&Strikethrough"),
337 wxT("Toggle strikethrough state"));
339 menuFont
->AppendSeparator();
340 menuFont
->Append(Font_SetNativeDesc
,
341 wxT("Set native font &description\tShift-Ctrl-D"));
342 menuFont
->Append(Font_SetNativeUserDesc
,
343 wxT("Set &user font description\tShift-Ctrl-U"));
344 menuFont
->AppendSeparator();
345 menuFont
->Append(Font_SetFamily
, wxT("Set font family"));
346 menuFont
->Append(Font_SetFaceName
, wxT("Set font face name"));
347 menuFont
->Append(Font_SetEncoding
, wxT("Set font &encoding\tShift-Ctrl-E"));
349 wxMenu
*menuSelect
= new wxMenu
;
350 menuSelect
->Append(Font_Choose
, wxT("&Select font...\tCtrl-S"),
351 wxT("Select a standard font"));
353 wxMenu
*menuStdFonts
= new wxMenu
;
354 menuStdFonts
->Append(Font_wxNORMAL_FONT
, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
355 menuStdFonts
->Append(Font_wxSMALL_FONT
, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
356 menuStdFonts
->Append(Font_wxITALIC_FONT
, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
357 menuStdFonts
->Append(Font_wxSWISS_FONT
, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
358 menuSelect
->Append(Font_Standard
, wxT("Standar&d fonts"), menuStdFonts
);
360 wxMenu
*menuSettingFonts
= new wxMenu
;
361 menuSettingFonts
->Append(Font_wxSYS_OEM_FIXED_FONT
, wxT("wxSYS_OEM_FIXED_FONT"),
362 wxT("Original equipment manufacturer dependent fixed-pitch font."));
363 menuSettingFonts
->Append(Font_wxSYS_ANSI_FIXED_FONT
, wxT("wxSYS_ANSI_FIXED_FONT"),
364 wxT("Windows fixed-pitch (monospaced) font. "));
365 menuSettingFonts
->Append(Font_wxSYS_ANSI_VAR_FONT
, wxT("wxSYS_ANSI_VAR_FONT"),
366 wxT("Windows variable-pitch (proportional) font."));
367 menuSettingFonts
->Append(Font_wxSYS_SYSTEM_FONT
, wxT("wxSYS_SYSTEM_FONT"),
368 wxT("System font."));
369 menuSettingFonts
->Append(Font_wxSYS_DEVICE_DEFAULT_FONT
, wxT("wxSYS_DEVICE_DEFAULT_FONT"),
370 wxT("Device-dependent font."));
371 menuSettingFonts
->Append(Font_wxSYS_DEFAULT_GUI_FONT
, wxT("wxSYS_DEFAULT_GUI_FONT"),
372 wxT("Default font for user interface objects such as menus and dialog boxes. "));
373 menuSelect
->Append(Font_SystemSettings
, wxT("System fonts"), menuSettingFonts
);
376 menuSelect
->AppendSeparator();
377 menuSelect
->Append(Font_EnumFamilies
, wxT("Enumerate font &families\tCtrl-F"));
378 menuSelect
->Append(Font_EnumFixedFamilies
,
379 wxT("Enumerate fi&xed font families\tCtrl-X"));
380 menuSelect
->Append(Font_EnumEncodings
,
381 wxT("Enumerate &encodings\tCtrl-E"));
382 menuSelect
->Append(Font_EnumFamiliesForEncoding
,
383 wxT("Find font for en&coding...\tCtrl-C"),
384 wxT("Find font families for given encoding"));
386 // now append the freshly created menu to the menu bar...
387 wxMenuBar
*menuBar
= new wxMenuBar
;
388 menuBar
->Append(menuFile
, wxT("&File"));
389 menuBar
->Append(menuFont
, wxT("F&ont"));
390 menuBar
->Append(menuSelect
, wxT("&Select"));
392 // ... and attach this menu bar to the frame
395 wxSplitterWindow
*splitter
= new wxSplitterWindow(this);
397 m_textctrl
= new wxTextCtrl(splitter
, wxID_ANY
,
398 wxT("Paste text here to see how it looks\nlike in the given font"),
399 wxDefaultPosition
, wxDefaultSize
,
402 m_canvas
= new MyCanvas(splitter
);
404 splitter
->SplitHorizontally(m_textctrl
, m_canvas
, 100);
407 // create a status bar just for fun (by default with 1 pane only)
409 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
410 #endif // wxUSE_STATUSBAR
413 // --------------------------------------------------------
415 class MyEncodingEnumerator
: public wxFontEnumerator
418 MyEncodingEnumerator()
421 const wxString
& GetText() const
425 virtual bool OnFontEncoding(const wxString
& facename
,
426 const wxString
& encoding
)
429 text
.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
430 (unsigned int) ++m_n
, encoding
.c_str(), facename
.c_str());
440 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
442 MyEncodingEnumerator fontEnumerator
;
444 fontEnumerator
.EnumerateEncodings();
446 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
447 fontEnumerator
.GetText().c_str());
450 // -------------------------------------------------------------
452 class MyFontEnumerator
: public wxFontEnumerator
456 { return !m_facenames
.IsEmpty(); }
458 const wxArrayString
& GetFacenames() const
459 { return m_facenames
; }
462 virtual bool OnFacename(const wxString
& facename
)
464 m_facenames
.Add(facename
);
469 wxArrayString m_facenames
;
472 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
473 wxFontEncoding encoding
,
476 MyFontEnumerator fontEnumerator
;
478 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
480 if ( fontEnumerator
.GotAny() )
482 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
485 wxLogStatus(this, wxT("Found %d %sfonts"),
486 nFacenames
, fixedWidthOnly
? wxT("fixed width ") : wxT(""));
494 facename
= fontEnumerator
.GetFacenames().Item(0);
498 // let the user choose
499 wxString
*facenames
= new wxString
[nFacenames
];
501 for ( n
= 0; n
< nFacenames
; n
++ )
502 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
504 n
= wxGetSingleChoiceIndex
506 wxT("Choose a facename"),
514 facename
= facenames
[n
];
519 if ( !facename
.empty() )
521 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
522 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
523 wxFONTWEIGHT_NORMAL
, false, facename
, encoding
);
532 wxLogWarning(wxT("No such fonts found."));
538 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
540 wxFontEncoding enc
= GetEncodingFromUser();
541 if ( enc
!= wxFONTENCODING_SYSTEM
)
543 DoEnumerateFamilies(false, enc
);
547 void MyFrame::OnSetNativeDesc(wxCommandEvent
& WXUNUSED(event
))
549 wxString fontInfo
= wxGetTextFromUser
551 wxT("Enter native font string"),
552 wxT("Input font description"),
553 m_canvas
->GetTextFont().GetNativeFontInfoDesc(),
556 if ( fontInfo
.empty() )
557 return; // user clicked "Cancel" - do nothing
560 font
.SetNativeFontInfo(fontInfo
);
563 wxLogError(wxT("Font info string \"%s\" is invalid."),
571 void MyFrame::OnSetNativeUserDesc(wxCommandEvent
& WXUNUSED(event
))
573 wxString fontdesc
= GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
574 wxString fontUserInfo
= wxGetTextFromUser(
575 wxT("Here you can edit current font description"),
576 wxT("Input font description"), fontdesc
,
578 if (fontUserInfo
.IsEmpty())
579 return; // user clicked "Cancel" - do nothing
582 if (font
.SetNativeFontInfoUserDesc(fontUserInfo
))
584 wxASSERT_MSG(font
.IsOk(), wxT("The font should now be valid"));
589 wxASSERT_MSG(!font
.IsOk(), wxT("The font should now be invalid"));
590 wxMessageBox(wxT("Error trying to create a font with such description..."));
594 void MyFrame::OnSetFamily(wxCommandEvent
& WXUNUSED(event
))
596 wxFontFamily f
= GetFamilyFromUser();
598 wxFont font
= m_canvas
->GetTextFont();
603 void MyFrame::OnSetFaceName(wxCommandEvent
& WXUNUSED(event
))
605 wxString facename
= GetCanvas()->GetTextFont().GetFaceName();
606 wxString newFaceName
= wxGetTextFromUser(
607 wxT("Here you can edit current font face name."),
608 wxT("Input font facename"), facename
,
610 if (newFaceName
.IsEmpty())
611 return; // user clicked "Cancel" - do nothing
613 wxFont
font(GetCanvas()->GetTextFont());
614 if (font
.SetFaceName(newFaceName
)) // change facename only
616 wxASSERT_MSG(font
.IsOk(), wxT("The font should now be valid"));
621 wxASSERT_MSG(!font
.IsOk(), wxT("The font should now be invalid"));
622 wxMessageBox(wxT("There is no font with such face name..."),
623 wxT("Invalid face name"), wxOK
|wxICON_ERROR
, this);
627 void MyFrame::OnSetEncoding(wxCommandEvent
& WXUNUSED(event
))
629 wxFontEncoding enc
= GetEncodingFromUser();
630 if ( enc
== wxFONTENCODING_SYSTEM
)
633 wxFont font
= m_canvas
->GetTextFont();
634 font
.SetEncoding(enc
);
638 wxFontEncoding
MyFrame::GetEncodingFromUser()
641 wxArrayInt encodings
;
643 const size_t count
= wxFontMapper::GetSupportedEncodingsCount();
644 names
.reserve(count
);
645 encodings
.reserve(count
);
647 for ( size_t n
= 0; n
< count
; n
++ )
649 wxFontEncoding enc
= wxFontMapper::GetEncoding(n
);
650 encodings
.push_back(enc
);
651 names
.push_back(wxFontMapper::GetEncodingName(enc
));
654 int i
= wxGetSingleChoiceIndex
656 wxT("Choose the encoding"),
662 return i
== -1 ? wxFONTENCODING_SYSTEM
: (wxFontEncoding
)encodings
[i
];
665 wxFontFamily
MyFrame::GetFamilyFromUser()
670 families
.push_back(wxFONTFAMILY_DECORATIVE
);
671 families
.push_back(wxFONTFAMILY_ROMAN
);
672 families
.push_back(wxFONTFAMILY_SCRIPT
);
673 families
.push_back(wxFONTFAMILY_SWISS
);
674 families
.push_back(wxFONTFAMILY_MODERN
);
675 families
.push_back(wxFONTFAMILY_TELETYPE
);
677 names
.push_back("DECORATIVE");
678 names
.push_back("ROMAN");
679 names
.push_back("SCRIPT");
680 names
.push_back("SWISS");
681 names
.push_back("MODERN");
682 names
.push_back("TELETYPE");
684 int i
= wxGetSingleChoiceIndex
686 wxT("Choose the family"),
692 return i
== -1 ? wxFONTFAMILY_DEFAULT
: (wxFontFamily
)families
[i
];
695 void MyFrame::DoResizeFont(int diff
)
697 wxFont font
= m_canvas
->GetTextFont();
699 font
.SetPointSize(font
.GetPointSize() + diff
);
703 void MyFrame::OnBold(wxCommandEvent
& event
)
705 wxFont font
= m_canvas
->GetTextFont();
707 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_BOLD
: wxFONTWEIGHT_NORMAL
);
711 void MyFrame::OnLight(wxCommandEvent
& event
)
713 wxFont font
= m_canvas
->GetTextFont();
715 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_LIGHT
: wxFONTWEIGHT_NORMAL
);
719 void MyFrame::OnItalic(wxCommandEvent
& event
)
721 wxFont font
= m_canvas
->GetTextFont();
723 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
);
727 void MyFrame::OnSlant(wxCommandEvent
& event
)
729 wxFont font
= m_canvas
->GetTextFont();
731 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_SLANT
: wxFONTSTYLE_NORMAL
);
735 void MyFrame::OnUnderline(wxCommandEvent
& event
)
737 wxFont font
= m_canvas
->GetTextFont();
739 font
.SetUnderlined(event
.IsChecked());
743 void MyFrame::OnStrikethrough(wxCommandEvent
& event
)
745 wxFont font
= m_canvas
->GetTextFont();
746 font
.SetStrikethrough(event
.IsChecked());
750 void MyFrame::OnwxPointerFont(wxCommandEvent
& event
)
754 switch ( event
.GetId() )
756 case Font_wxNORMAL_FONT
:
757 font
= *wxNORMAL_FONT
;
760 case Font_wxSMALL_FONT
:
761 font
= *wxSMALL_FONT
;
764 case Font_wxITALIC_FONT
:
765 font
= *wxITALIC_FONT
;
768 case Font_wxSWISS_FONT
:
769 font
= *wxSWISS_FONT
;
773 wxFAIL_MSG( wxT("unknown standard font") );
780 void MyFrame::OnwxSystemSettingsFont(wxCommandEvent
& event
)
784 switch ( event
.GetId() )
786 case Font_wxSYS_OEM_FIXED_FONT
:
787 font
= wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
);
790 case Font_wxSYS_ANSI_FIXED_FONT
:
791 font
= wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT
);
794 case Font_wxSYS_ANSI_VAR_FONT
:
795 font
= wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT
);
798 case Font_wxSYS_SYSTEM_FONT
:
799 font
= wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
);
802 case Font_wxSYS_DEVICE_DEFAULT_FONT
:
803 font
= wxSystemSettings::GetFont(wxSYS_DEVICE_DEFAULT_FONT
);
806 case Font_wxSYS_DEFAULT_GUI_FONT
:
807 font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
811 wxFAIL_MSG( wxT("unknown standard font") );
818 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
820 m_canvas
->SetTextFont(font
);
822 m_canvas
->SetColour(col
);
825 m_textctrl
->SetFont(font
);
827 m_textctrl
->SetForegroundColour(col
);
828 m_textctrl
->Refresh();
830 // update the state of the bold/italic/underlined menu items
831 wxMenuBar
*mbar
= GetMenuBar();
834 mbar
->Check(Font_Light
, font
.GetWeight() == wxFONTWEIGHT_LIGHT
);
835 mbar
->Check(Font_Bold
, font
.GetWeight() == wxFONTWEIGHT_BOLD
);
837 mbar
->Check(Font_Italic
, font
.GetStyle() == wxFONTSTYLE_ITALIC
);
839 mbar
->Check(Font_Slant
, font
.GetStyle() == wxFONTSTYLE_SLANT
);
842 mbar
->Check(Font_Underlined
, font
.GetUnderlined());
843 mbar
->Check(Font_Strikethrough
, font
.GetStrikethrough());
847 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
850 data
.SetInitialFont(m_canvas
->GetTextFont());
851 data
.SetColour(m_canvas
->GetColour());
853 wxFontDialog
dialog(this, data
);
854 if ( dialog
.ShowModal() == wxID_OK
)
856 wxFontData retData
= dialog
.GetFontData();
857 wxFont font
= retData
.GetChosenFont();
858 wxColour colour
= retData
.GetColour();
860 DoChangeFont(font
, colour
);
864 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
866 // true is to force the frame to close
870 void MyFrame::OnTestTextValue(wxCommandEvent
& WXUNUSED(event
))
872 wxString value
= m_textctrl
->GetValue();
873 m_textctrl
->SetValue(value
);
874 if ( m_textctrl
->GetValue() != value
)
876 wxLogError(wxT("Text value changed after getting and setting it"));
880 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
883 // first, choose the file
884 static wxString s_dir
, s_file
;
885 wxFileDialog
dialog(this, wxT("Open an email message file"),
887 if ( dialog
.ShowModal() != wxID_OK
)
890 // save for the next time
891 s_dir
= dialog
.GetDirectory();
892 s_file
= dialog
.GetFilename();
894 wxString filename
= dialog
.GetPath();
896 // load it and search for Content-Type header
897 wxTextFile
file(filename
);
903 static const wxChar
*prefix
= wxT("Content-Type: text/plain; charset=");
904 const size_t len
= wxStrlen(prefix
);
906 size_t n
, count
= file
.GetLineCount();
907 for ( n
= 0; n
< count
; n
++ )
909 wxString line
= file
[n
];
913 // if it is an email message, headers are over, no need to parse
918 if ( line
.Left(len
) == prefix
)
921 const wxChar
*pc
= line
.c_str() + len
;
922 if ( *pc
== wxT('"') )
925 while ( *pc
&& *pc
!= wxT('"') )
936 wxLogError(wxT("The file '%s' doesn't contain charset information."),
942 // ok, now get the corresponding encoding
943 wxFontEncoding fontenc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
944 if ( fontenc
== wxFONTENCODING_SYSTEM
)
946 wxLogError(wxT("Charset '%s' is unsupported."), charset
.c_str());
950 m_textctrl
->LoadFile(filename
);
952 if ( fontenc
== wxFONTENCODING_UTF8
||
953 !wxFontMapper::Get()->IsEncodingAvailable(fontenc
) )
955 // try to find some similar encoding:
956 wxFontEncoding encAlt
;
957 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc
, &encAlt
) )
959 wxEncodingConverter conv
;
961 if (conv
.Init(fontenc
, encAlt
))
964 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
968 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
969 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
970 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
974 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
975 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
978 // and now create the correct font
979 if ( !DoEnumerateFamilies(false, fontenc
, true /* silent */) )
981 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
982 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
983 wxFONTWEIGHT_NORMAL
, false /* !underlined */,
984 wxEmptyString
/* facename */, fontenc
);
991 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
992 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
995 #endif // wxUSE_FILEDLG
998 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1000 wxMessageBox(wxT("wxWidgets font sample\n")
1001 wxT("(c) 1999-2006 Vadim Zeitlin"),
1002 wxString(wxT("About ")) + SAMPLE_TITLE
,
1003 wxOK
| wxICON_INFORMATION
, this);
1006 // ----------------------------------------------------------------------------
1008 // ----------------------------------------------------------------------------
1010 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
1011 EVT_PAINT(MyCanvas::OnPaint
)
1014 MyCanvas::MyCanvas( wxWindow
*parent
)
1015 : wxWindow( parent
, wxID_ANY
),
1016 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
1020 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1026 dc
.SetBackground(*wxWHITE_BRUSH
);
1030 // one text line height
1031 wxCoord hLine
= dc
.GetCharHeight();
1033 // the current text origin
1037 // output the font name/info
1040 fontInfo
.Printf(wxT("Face name: %s, family: %s"),
1041 m_font
.GetFaceName().c_str(),
1042 m_font
.GetFamilyString().c_str());
1044 dc
.DrawText(fontInfo
, x
, y
);
1047 fontInfo
.Printf(wxT("Size: %d points or %d pixels; %d*%d average char size"),
1048 m_font
.GetPointSize(),
1049 m_font
.GetPixelSize().y
,
1050 dc
.GetCharWidth(), dc
.GetCharHeight());
1052 dc
.DrawText(fontInfo
, x
, y
);
1055 fontInfo
.Printf(wxT("Style: %s, weight: %s, fixed width: %s, encoding: %s"),
1056 m_font
.GetStyleString().c_str(),
1057 m_font
.GetWeightString().c_str(),
1058 m_font
.IsFixedWidth() ? wxT("yes") : wxT("no"),
1059 wxFontMapper::GetEncodingDescription(m_font
.GetEncoding()));
1061 dc
.DrawText(fontInfo
, x
, y
);
1064 if ( m_font
.IsOk() )
1066 const wxNativeFontInfo
*info
= m_font
.GetNativeFontInfo();
1069 wxString fontDesc
= m_font
.GetNativeFontInfoUserDesc();
1070 fontInfo
.Printf(wxT("Native font info: %s"), fontDesc
.c_str());
1072 dc
.DrawText(fontInfo
, x
, y
);
1079 // prepare to draw the font
1080 dc
.SetTextForeground(m_colour
);
1082 // the size of one cell (Normally biggest char + small margin)
1083 wxCoord maxCharWidth
, maxCharHeight
;
1084 dc
.GetTextExtent(wxT("W"), &maxCharWidth
, &maxCharHeight
);
1085 int w
= maxCharWidth
+ 5,
1086 h
= maxCharHeight
+ 4;
1089 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
1090 for ( int i
= 0; i
< 7; i
++ )
1092 for ( int j
= 0; j
< 32; j
++ )
1094 wxChar c
= (wxChar
)(32 * (i
+ 1) + j
);
1096 wxCoord charWidth
, charHeight
;
1097 dc
.GetTextExtent(c
, &charWidth
, &charHeight
);
1101 x
+ w
*j
+ (maxCharWidth
- charWidth
) / 2 + 1,
1102 y
+ h
*i
+ (maxCharHeight
- charHeight
) / 2
1107 // draw the lines between them
1108 dc
.SetPen(*wxBLUE_PEN
);
1112 for ( l
= 0; l
< 8; l
++ )
1114 int yl
= y
+ h
*l
- 2;
1115 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 1, yl
);
1119 for ( l
= 0; l
< 33; l
++ )
1121 int xl
= x
+ w
*l
- 2;
1122 dc
.DrawLine(xl
, y
- 2, xl
, y
+ 7*h
- 1);