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
);
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,
182 // standard global wxFont objects:
189 // wxSystemSettings::GetFont possible objects:
190 Font_wxSYS_OEM_FIXED_FONT
,
191 Font_wxSYS_ANSI_FIXED_FONT
,
192 Font_wxSYS_ANSI_VAR_FONT
,
193 Font_wxSYS_SYSTEM_FONT
,
194 Font_wxSYS_DEVICE_DEFAULT_FONT
,
195 Font_wxSYS_DEFAULT_GUI_FONT
,
199 Font_EnumFamiliesForEncoding
,
201 Font_EnumFixedFamilies
,
204 Font_SetNativeUserDesc
,
211 // ----------------------------------------------------------------------------
212 // event tables and other macros for wxWidgets
213 // ----------------------------------------------------------------------------
215 // the event tables connect the wxWidgets events with the functions (event
216 // handlers) which process them. It can be also done at run-time, but for the
217 // simple menu events like this the static method is much simpler.
218 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
219 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
220 EVT_MENU(Font_TestTextValue
, MyFrame::OnTestTextValue
)
221 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
222 EVT_MENU(Font_About
, MyFrame::OnAbout
)
224 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
225 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
227 EVT_MENU(Font_Bold
, MyFrame::OnBold
)
228 EVT_MENU(Font_Light
, MyFrame::OnLight
)
230 EVT_MENU(Font_Italic
, MyFrame::OnItalic
)
231 EVT_MENU(Font_Slant
, MyFrame::OnSlant
)
233 EVT_MENU(Font_Underlined
, MyFrame::OnUnderline
)
235 EVT_MENU(Font_wxNORMAL_FONT
, MyFrame::OnwxPointerFont
)
236 EVT_MENU(Font_wxSMALL_FONT
, MyFrame::OnwxPointerFont
)
237 EVT_MENU(Font_wxITALIC_FONT
, MyFrame::OnwxPointerFont
)
238 EVT_MENU(Font_wxSWISS_FONT
, MyFrame::OnwxPointerFont
)
240 EVT_MENU(Font_wxSYS_OEM_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
241 EVT_MENU(Font_wxSYS_ANSI_FIXED_FONT
, MyFrame::OnwxSystemSettingsFont
)
242 EVT_MENU(Font_wxSYS_ANSI_VAR_FONT
, MyFrame::OnwxSystemSettingsFont
)
243 EVT_MENU(Font_wxSYS_SYSTEM_FONT
, MyFrame::OnwxSystemSettingsFont
)
244 EVT_MENU(Font_wxSYS_DEVICE_DEFAULT_FONT
, MyFrame::OnwxSystemSettingsFont
)
245 EVT_MENU(Font_wxSYS_DEFAULT_GUI_FONT
, MyFrame::OnwxSystemSettingsFont
)
247 EVT_MENU(Font_SetNativeDesc
, MyFrame::OnSetNativeDesc
)
248 EVT_MENU(Font_SetNativeUserDesc
, MyFrame::OnSetNativeUserDesc
)
249 EVT_MENU(Font_SetFamily
, MyFrame::OnSetFamily
)
250 EVT_MENU(Font_SetFaceName
, MyFrame::OnSetFaceName
)
251 EVT_MENU(Font_SetEncoding
, MyFrame::OnSetEncoding
)
253 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
254 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
255 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
256 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
257 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
260 // Create a new application object: this macro will allow wxWidgets to create
261 // the application object during program execution (it's better than using a
262 // static object for many reasons) and also declares the accessor function
263 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
267 // ============================================================================
269 // ============================================================================
271 // ----------------------------------------------------------------------------
272 // the application class
273 // ----------------------------------------------------------------------------
275 // `Main program' equivalent: the program execution "starts" here
278 if ( !wxApp::OnInit() )
281 // Create the main application window
282 MyFrame
*frame
= new MyFrame(wxT("Font wxWidgets demo"),
283 wxPoint(50, 50), wxSize(600, 400));
285 // Show it and tell the application that it's our main window
289 // success: wxApp::OnRun() will be called which will enter the main message
290 // loop and the application will run. If we returned 'false' here, the
291 // application would exit immediately.
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
300 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
301 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
), m_textctrl(NULL
)
303 m_fontSize
= wxNORMAL_FONT
->GetPointSize();
305 SetIcon(wxIcon(sample_xpm
));
308 wxMenu
*menuFile
= new wxMenu
;
310 menuFile
->Append(Font_TestTextValue
, wxT("&Test text value"),
311 wxT("Verify that getting and setting text value doesn't change it"));
312 menuFile
->Append(Font_ViewMsg
, wxT("&View...\tCtrl-V"),
313 wxT("View an email message file"));
314 menuFile
->AppendSeparator();
315 menuFile
->Append(Font_About
, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
316 menuFile
->AppendSeparator();
317 menuFile
->Append(Font_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
319 wxMenu
*menuFont
= new wxMenu
;
320 menuFont
->Append(Font_IncSize
, wxT("&Increase font size by 2 points\tCtrl-I"));
321 menuFont
->Append(Font_DecSize
, wxT("&Decrease font size by 2 points\tCtrl-D"));
322 menuFont
->AppendSeparator();
323 menuFont
->AppendCheckItem(Font_Bold
, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
324 menuFont
->AppendCheckItem(Font_Light
, wxT("&Light\tCtrl-L"), wxT("Toggle light state"));
325 menuFont
->AppendSeparator();
326 menuFont
->AppendCheckItem(Font_Italic
, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
328 // under wxMSW slant == italic so there's no reason to provide another menu item for the same thing
329 menuFont
->AppendCheckItem(Font_Slant
, wxT("&Slant\tCtrl-S"), wxT("Toggle slant state"));
331 menuFont
->AppendSeparator();
332 menuFont
->AppendCheckItem(Font_Underlined
, wxT("&Underlined\tCtrl-U"),
333 wxT("Toggle underlined state"));
335 menuFont
->AppendSeparator();
336 menuFont
->Append(Font_SetNativeDesc
,
337 wxT("Set native font &description\tShift-Ctrl-D"));
338 menuFont
->Append(Font_SetNativeUserDesc
,
339 wxT("Set &user font description\tShift-Ctrl-U"));
340 menuFont
->AppendSeparator();
341 menuFont
->Append(Font_SetFamily
, wxT("Set font family"));
342 menuFont
->Append(Font_SetFaceName
, wxT("Set font face name"));
343 menuFont
->Append(Font_SetEncoding
, wxT("Set font &encoding\tShift-Ctrl-E"));
345 wxMenu
*menuSelect
= new wxMenu
;
346 menuSelect
->Append(Font_Choose
, wxT("&Select font...\tCtrl-S"),
347 wxT("Select a standard font"));
349 wxMenu
*menuStdFonts
= new wxMenu
;
350 menuStdFonts
->Append(Font_wxNORMAL_FONT
, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
351 menuStdFonts
->Append(Font_wxSMALL_FONT
, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
352 menuStdFonts
->Append(Font_wxITALIC_FONT
, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
353 menuStdFonts
->Append(Font_wxSWISS_FONT
, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
354 menuSelect
->Append(Font_Standard
, wxT("Standar&d fonts"), menuStdFonts
);
356 wxMenu
*menuSettingFonts
= new wxMenu
;
357 menuSettingFonts
->Append(Font_wxSYS_OEM_FIXED_FONT
, wxT("wxSYS_OEM_FIXED_FONT"),
358 wxT("Original equipment manufacturer dependent fixed-pitch font."));
359 menuSettingFonts
->Append(Font_wxSYS_ANSI_FIXED_FONT
, wxT("wxSYS_ANSI_FIXED_FONT"),
360 wxT("Windows fixed-pitch (monospaced) font. "));
361 menuSettingFonts
->Append(Font_wxSYS_ANSI_VAR_FONT
, wxT("wxSYS_ANSI_VAR_FONT"),
362 wxT("Windows variable-pitch (proportional) font."));
363 menuSettingFonts
->Append(Font_wxSYS_SYSTEM_FONT
, wxT("wxSYS_SYSTEM_FONT"),
364 wxT("System font."));
365 menuSettingFonts
->Append(Font_wxSYS_DEVICE_DEFAULT_FONT
, wxT("wxSYS_DEVICE_DEFAULT_FONT"),
366 wxT("Device-dependent font."));
367 menuSettingFonts
->Append(Font_wxSYS_DEFAULT_GUI_FONT
, wxT("wxSYS_DEFAULT_GUI_FONT"),
368 wxT("Default font for user interface objects such as menus and dialog boxes. "));
369 menuSelect
->Append(Font_SystemSettings
, wxT("System fonts"), menuSettingFonts
);
372 menuSelect
->AppendSeparator();
373 menuSelect
->Append(Font_EnumFamilies
, wxT("Enumerate font &families\tCtrl-F"));
374 menuSelect
->Append(Font_EnumFixedFamilies
,
375 wxT("Enumerate fi&xed font families\tCtrl-X"));
376 menuSelect
->Append(Font_EnumEncodings
,
377 wxT("Enumerate &encodings\tCtrl-E"));
378 menuSelect
->Append(Font_EnumFamiliesForEncoding
,
379 wxT("Find font for en&coding...\tCtrl-C"),
380 wxT("Find font families for given encoding"));
382 // now append the freshly created menu to the menu bar...
383 wxMenuBar
*menuBar
= new wxMenuBar
;
384 menuBar
->Append(menuFile
, wxT("&File"));
385 menuBar
->Append(menuFont
, wxT("F&ont"));
386 menuBar
->Append(menuSelect
, wxT("&Select"));
388 // ... and attach this menu bar to the frame
391 wxSplitterWindow
*splitter
= new wxSplitterWindow(this);
393 m_textctrl
= new wxTextCtrl(splitter
, wxID_ANY
,
394 wxT("Paste text here to see how it looks\nlike in the given font"),
395 wxDefaultPosition
, wxDefaultSize
,
398 m_canvas
= new MyCanvas(splitter
);
400 splitter
->SplitHorizontally(m_textctrl
, m_canvas
, 100);
403 // create a status bar just for fun (by default with 1 pane only)
405 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
406 #endif // wxUSE_STATUSBAR
409 // --------------------------------------------------------
411 class MyEncodingEnumerator
: public wxFontEnumerator
414 MyEncodingEnumerator()
417 const wxString
& GetText() const
421 virtual bool OnFontEncoding(const wxString
& facename
,
422 const wxString
& encoding
)
425 text
.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
426 (unsigned int) ++m_n
, encoding
.c_str(), facename
.c_str());
436 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
438 MyEncodingEnumerator fontEnumerator
;
440 fontEnumerator
.EnumerateEncodings();
442 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
443 fontEnumerator
.GetText().c_str());
446 // -------------------------------------------------------------
448 class MyFontEnumerator
: public wxFontEnumerator
452 { return !m_facenames
.IsEmpty(); }
454 const wxArrayString
& GetFacenames() const
455 { return m_facenames
; }
458 virtual bool OnFacename(const wxString
& facename
)
460 m_facenames
.Add(facename
);
465 wxArrayString m_facenames
;
468 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
469 wxFontEncoding encoding
,
472 MyFontEnumerator fontEnumerator
;
474 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
476 if ( fontEnumerator
.GotAny() )
478 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
481 wxLogStatus(this, wxT("Found %d %sfonts"),
482 nFacenames
, fixedWidthOnly
? wxT("fixed width ") : wxT(""));
490 facename
= fontEnumerator
.GetFacenames().Item(0);
494 // let the user choose
495 wxString
*facenames
= new wxString
[nFacenames
];
497 for ( n
= 0; n
< nFacenames
; n
++ )
498 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
500 n
= wxGetSingleChoiceIndex
502 wxT("Choose a facename"),
510 facename
= facenames
[n
];
515 if ( !facename
.empty() )
517 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
518 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
519 wxFONTWEIGHT_NORMAL
, false, facename
, encoding
);
528 wxLogWarning(wxT("No such fonts found."));
534 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
536 wxFontEncoding enc
= GetEncodingFromUser();
537 if ( enc
!= wxFONTENCODING_SYSTEM
)
539 DoEnumerateFamilies(false, enc
);
543 void MyFrame::OnSetNativeDesc(wxCommandEvent
& WXUNUSED(event
))
545 wxString fontInfo
= wxGetTextFromUser
547 wxT("Enter native font string"),
548 wxT("Input font description"),
549 m_canvas
->GetTextFont().GetNativeFontInfoDesc(),
552 if ( fontInfo
.empty() )
553 return; // user clicked "Cancel" - do nothing
556 font
.SetNativeFontInfo(fontInfo
);
559 wxLogError(wxT("Font info string \"%s\" is invalid."),
567 void MyFrame::OnSetNativeUserDesc(wxCommandEvent
& WXUNUSED(event
))
569 wxString fontdesc
= GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
570 wxString fontUserInfo
= wxGetTextFromUser(
571 wxT("Here you can edit current font description"),
572 wxT("Input font description"), fontdesc
,
574 if (fontUserInfo
.IsEmpty())
575 return; // user clicked "Cancel" - do nothing
578 if (font
.SetNativeFontInfoUserDesc(fontUserInfo
))
580 wxASSERT_MSG(font
.Ok(), wxT("The font should now be valid"));
585 wxASSERT_MSG(!font
.Ok(), wxT("The font should now be invalid"));
586 wxMessageBox(wxT("Error trying to create a font with such description..."));
590 void MyFrame::OnSetFamily(wxCommandEvent
& WXUNUSED(event
))
592 wxFontFamily f
= GetFamilyFromUser();
594 wxFont font
= m_canvas
->GetTextFont();
599 void MyFrame::OnSetFaceName(wxCommandEvent
& WXUNUSED(event
))
601 wxString facename
= GetCanvas()->GetTextFont().GetFaceName();
602 wxString newFaceName
= wxGetTextFromUser(
603 wxT("Here you can edit current font face name."),
604 wxT("Input font facename"), facename
,
606 if (newFaceName
.IsEmpty())
607 return; // user clicked "Cancel" - do nothing
609 wxFont
font(GetCanvas()->GetTextFont());
610 if (font
.SetFaceName(newFaceName
)) // change facename only
612 wxASSERT_MSG(font
.Ok(), wxT("The font should now be valid"));
617 wxASSERT_MSG(!font
.Ok(), wxT("The font should now be invalid"));
618 wxMessageBox(wxT("There is no font with such face name..."),
619 wxT("Invalid face name"), wxOK
|wxICON_ERROR
, this);
623 void MyFrame::OnSetEncoding(wxCommandEvent
& WXUNUSED(event
))
625 wxFontEncoding enc
= GetEncodingFromUser();
626 if ( enc
== wxFONTENCODING_SYSTEM
)
629 wxFont font
= m_canvas
->GetTextFont();
630 font
.SetEncoding(enc
);
634 wxFontEncoding
MyFrame::GetEncodingFromUser()
637 wxArrayInt encodings
;
639 const size_t count
= wxFontMapper::GetSupportedEncodingsCount();
640 names
.reserve(count
);
641 encodings
.reserve(count
);
643 for ( size_t n
= 0; n
< count
; n
++ )
645 wxFontEncoding enc
= wxFontMapper::GetEncoding(n
);
646 encodings
.push_back(enc
);
647 names
.push_back(wxFontMapper::GetEncodingName(enc
));
650 int i
= wxGetSingleChoiceIndex
652 wxT("Choose the encoding"),
658 return i
== -1 ? wxFONTENCODING_SYSTEM
: (wxFontEncoding
)encodings
[i
];
661 wxFontFamily
MyFrame::GetFamilyFromUser()
666 families
.push_back(wxFONTFAMILY_DECORATIVE
);
667 families
.push_back(wxFONTFAMILY_ROMAN
);
668 families
.push_back(wxFONTFAMILY_SCRIPT
);
669 families
.push_back(wxFONTFAMILY_SWISS
);
670 families
.push_back(wxFONTFAMILY_MODERN
);
671 families
.push_back(wxFONTFAMILY_TELETYPE
);
673 names
.push_back("DECORATIVE");
674 names
.push_back("ROMAN");
675 names
.push_back("SCRIPT");
676 names
.push_back("SWISS");
677 names
.push_back("MODERN");
678 names
.push_back("TELETYPE");
680 int i
= wxGetSingleChoiceIndex
682 wxT("Choose the family"),
688 return i
== -1 ? wxFONTFAMILY_DEFAULT
: (wxFontFamily
)families
[i
];
691 void MyFrame::DoResizeFont(int diff
)
693 wxFont font
= m_canvas
->GetTextFont();
695 font
.SetPointSize(font
.GetPointSize() + diff
);
699 void MyFrame::OnBold(wxCommandEvent
& event
)
701 wxFont font
= m_canvas
->GetTextFont();
703 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_BOLD
: wxFONTWEIGHT_NORMAL
);
707 void MyFrame::OnLight(wxCommandEvent
& event
)
709 wxFont font
= m_canvas
->GetTextFont();
711 font
.SetWeight(event
.IsChecked() ? wxFONTWEIGHT_LIGHT
: wxFONTWEIGHT_NORMAL
);
715 void MyFrame::OnItalic(wxCommandEvent
& event
)
717 wxFont font
= m_canvas
->GetTextFont();
719 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
);
723 void MyFrame::OnSlant(wxCommandEvent
& event
)
725 wxFont font
= m_canvas
->GetTextFont();
727 font
.SetStyle(event
.IsChecked() ? wxFONTSTYLE_SLANT
: wxFONTSTYLE_NORMAL
);
731 void MyFrame::OnUnderline(wxCommandEvent
& event
)
733 wxFont font
= m_canvas
->GetTextFont();
735 font
.SetUnderlined(event
.IsChecked());
739 void MyFrame::OnwxPointerFont(wxCommandEvent
& event
)
743 switch ( event
.GetId() )
745 case Font_wxNORMAL_FONT
:
746 font
= *wxNORMAL_FONT
;
749 case Font_wxSMALL_FONT
:
750 font
= *wxSMALL_FONT
;
753 case Font_wxITALIC_FONT
:
754 font
= *wxITALIC_FONT
;
757 case Font_wxSWISS_FONT
:
758 font
= *wxSWISS_FONT
;
762 wxFAIL_MSG( wxT("unknown standard font") );
769 void MyFrame::OnwxSystemSettingsFont(wxCommandEvent
& event
)
773 switch ( event
.GetId() )
775 case Font_wxSYS_OEM_FIXED_FONT
:
776 font
= wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
);
779 case Font_wxSYS_ANSI_FIXED_FONT
:
780 font
= wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT
);
783 case Font_wxSYS_ANSI_VAR_FONT
:
784 font
= wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT
);
787 case Font_wxSYS_SYSTEM_FONT
:
788 font
= wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
);
791 case Font_wxSYS_DEVICE_DEFAULT_FONT
:
792 font
= wxSystemSettings::GetFont(wxSYS_DEVICE_DEFAULT_FONT
);
795 case Font_wxSYS_DEFAULT_GUI_FONT
:
796 font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
800 wxFAIL_MSG( wxT("unknown standard font") );
807 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
809 m_canvas
->SetTextFont(font
);
811 m_canvas
->SetColour(col
);
814 m_textctrl
->SetFont(font
);
816 m_textctrl
->SetForegroundColour(col
);
818 // update the state of the bold/italic/underlined menu items
819 wxMenuBar
*mbar
= GetMenuBar();
822 mbar
->Check(Font_Light
, font
.GetWeight() == wxFONTWEIGHT_LIGHT
);
823 mbar
->Check(Font_Bold
, font
.GetWeight() == wxFONTWEIGHT_BOLD
);
825 mbar
->Check(Font_Italic
, font
.GetStyle() == wxFONTSTYLE_ITALIC
);
827 mbar
->Check(Font_Slant
, font
.GetStyle() == wxFONTSTYLE_SLANT
);
830 mbar
->Check(Font_Underlined
, font
.GetUnderlined());
834 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
837 data
.SetInitialFont(m_canvas
->GetTextFont());
838 data
.SetColour(m_canvas
->GetColour());
840 wxFontDialog
dialog(this, data
);
841 if ( dialog
.ShowModal() == wxID_OK
)
843 wxFontData retData
= dialog
.GetFontData();
844 wxFont font
= retData
.GetChosenFont();
845 wxColour colour
= retData
.GetColour();
847 DoChangeFont(font
, colour
);
851 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
853 // true is to force the frame to close
857 void MyFrame::OnTestTextValue(wxCommandEvent
& WXUNUSED(event
))
859 wxString value
= m_textctrl
->GetValue();
860 m_textctrl
->SetValue(value
);
861 if ( m_textctrl
->GetValue() != value
)
863 wxLogError(wxT("Text value changed after getting and setting it"));
867 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
870 // first, choose the file
871 static wxString s_dir
, s_file
;
872 wxFileDialog
dialog(this, wxT("Open an email message file"),
874 if ( dialog
.ShowModal() != wxID_OK
)
877 // save for the next time
878 s_dir
= dialog
.GetDirectory();
879 s_file
= dialog
.GetFilename();
881 wxString filename
= dialog
.GetPath();
883 // load it and search for Content-Type header
884 wxTextFile
file(filename
);
890 static const wxChar
*prefix
= wxT("Content-Type: text/plain; charset=");
891 const size_t len
= wxStrlen(prefix
);
893 size_t n
, count
= file
.GetLineCount();
894 for ( n
= 0; n
< count
; n
++ )
896 wxString line
= file
[n
];
900 // if it is an email message, headers are over, no need to parse
905 if ( line
.Left(len
) == prefix
)
908 const wxChar
*pc
= line
.c_str() + len
;
909 if ( *pc
== wxT('"') )
912 while ( *pc
&& *pc
!= wxT('"') )
923 wxLogError(wxT("The file '%s' doesn't contain charset information."),
929 // ok, now get the corresponding encoding
930 wxFontEncoding fontenc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
931 if ( fontenc
== wxFONTENCODING_SYSTEM
)
933 wxLogError(wxT("Charset '%s' is unsupported."), charset
.c_str());
937 m_textctrl
->LoadFile(filename
);
939 if ( fontenc
== wxFONTENCODING_UTF8
||
940 !wxFontMapper::Get()->IsEncodingAvailable(fontenc
) )
942 // try to find some similar encoding:
943 wxFontEncoding encAlt
;
944 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc
, &encAlt
) )
946 wxEncodingConverter conv
;
948 if (conv
.Init(fontenc
, encAlt
))
951 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
955 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
956 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
957 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
961 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
962 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
965 // and now create the correct font
966 if ( !DoEnumerateFamilies(false, fontenc
, true /* silent */) )
968 wxFont
font(wxNORMAL_FONT
->GetPointSize(),
969 wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
970 wxFONTWEIGHT_NORMAL
, false /* !underlined */,
971 wxEmptyString
/* facename */, fontenc
);
978 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
979 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
982 #endif // wxUSE_FILEDLG
985 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
987 wxMessageBox(wxT("wxWidgets font sample\n")
988 wxT("(c) 1999-2006 Vadim Zeitlin"),
989 wxString(wxT("About ")) + SAMPLE_TITLE
,
990 wxOK
| wxICON_INFORMATION
, this);
993 // ----------------------------------------------------------------------------
995 // ----------------------------------------------------------------------------
997 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
998 EVT_PAINT(MyCanvas::OnPaint
)
1001 MyCanvas::MyCanvas( wxWindow
*parent
)
1002 : wxWindow( parent
, wxID_ANY
),
1003 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
1007 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1013 dc
.SetBackground(wxBrush(wxT("white"), wxSOLID
));
1016 // one text line height
1017 wxCoord hLine
= dc
.GetCharHeight();
1019 // the current text origin
1023 // output the font name/info
1026 fontInfo
.Printf(wxT("Face name: %s, family: %s"),
1027 m_font
.GetFaceName().c_str(),
1028 m_font
.GetFamilyString().c_str());
1030 dc
.DrawText(fontInfo
, x
, y
);
1033 fontInfo
.Printf(wxT("Size: %d points, encoding: %s"),
1034 m_font
.GetPointSize(),
1036 GetEncodingDescription(m_font
.GetEncoding()).c_str());
1038 dc
.DrawText(fontInfo
, x
, y
);
1041 fontInfo
.Printf(wxT("Style: %s, weight: %s, fixed width: %s"),
1042 m_font
.GetStyleString().c_str(),
1043 m_font
.GetWeightString().c_str(),
1044 m_font
.IsFixedWidth() ? wxT("yes") : wxT("no"));
1046 dc
.DrawText(fontInfo
, x
, y
);
1051 const wxNativeFontInfo
*info
= m_font
.GetNativeFontInfo();
1054 wxString fontDesc
= m_font
.GetNativeFontInfoUserDesc();
1055 fontInfo
.Printf(wxT("Native font info: %s"), fontDesc
.c_str());
1057 dc
.DrawText(fontInfo
, x
, y
);
1064 // prepare to draw the font
1066 dc
.SetTextForeground(m_colour
);
1068 // the size of one cell (Normally biggest char + small margin)
1069 wxCoord maxCharWidth
, maxCharHeight
;
1070 dc
.GetTextExtent(wxT("W"), &maxCharWidth
, &maxCharHeight
);
1071 int w
= maxCharWidth
+ 5,
1072 h
= maxCharHeight
+ 4;
1075 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
1076 for ( int i
= 0; i
< 7; i
++ )
1078 for ( int j
= 0; j
< 32; j
++ )
1080 wxChar c
= (wxChar
)(32 * (i
+ 1) + j
);
1082 wxCoord charWidth
, charHeight
;
1083 dc
.GetTextExtent(c
, &charWidth
, &charHeight
);
1087 x
+ w
*j
+ (maxCharWidth
- charWidth
) / 2 + 1,
1088 y
+ h
*i
+ (maxCharHeight
- charHeight
) / 2
1093 // draw the lines between them
1094 dc
.SetPen(wxPen(wxColour(wxT("blue")), 1, wxSOLID
));
1098 for ( l
= 0; l
< 8; l
++ )
1100 int yl
= y
+ h
*l
- 2;
1101 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 1, yl
);
1105 for ( l
= 0; l
< 33; l
++ )
1107 int xl
= x
+ w
*l
- 2;
1108 dc
.DrawLine(xl
, y
- 2, xl
, y
+ 7*h
- 1);