allow testing more font properties: light weight, slant style and font families
[wxWidgets.git] / samples / font / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont demo
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 30.09.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all standard wxWidgets headers
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23
24 #include "wx/log.h"
25 #endif
26
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"
35
36 #include "../sample.xpm"
37
38 #ifdef __WXMAC__
39 #undef wxFontDialog
40 #include "wx/osx/fontdlg.h"
41 #endif
42
43 // used as title for several dialog boxes
44 static const wxChar SAMPLE_TITLE[] = _T("wxWidgets Font Sample");
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp : public wxApp
52 {
53 public:
54 // override base class virtuals
55 // ----------------------------
56
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();
61 };
62
63 // MyCanvas is a canvas on which we show the font sample
64 class MyCanvas: public wxWindow
65 {
66 public:
67 MyCanvas( wxWindow *parent );
68 virtual ~MyCanvas(){};
69
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; }
75
76 // event handlers
77 void OnPaint( wxPaintEvent &event );
78
79 private:
80 wxColour m_colour;
81 wxFont m_font;
82
83 DECLARE_EVENT_TABLE()
84 };
85
86 // Define a new frame type: this is going to be our main frame
87 class MyFrame : public wxFrame
88 {
89 public:
90 // ctor(s)
91 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
92
93 // accessors
94 MyCanvas *GetCanvas() const { return m_canvas; }
95
96 // event handlers (these functions should _not_ be virtual)
97 void OnQuit(wxCommandEvent& event);
98 void OnAbout(wxCommandEvent& event);
99
100 void OnIncFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(+2); }
101 void OnDecFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(-2); }
102
103 void OnBold(wxCommandEvent& event);
104 void OnLight(wxCommandEvent& event);
105
106 void OnItalic(wxCommandEvent& event);
107 void OnSlant(wxCommandEvent& event);
108
109 void OnUnderline(wxCommandEvent& event);
110
111 void OnwxPointerFont(wxCommandEvent& event);
112 void OnwxSystemSettingsFont(wxCommandEvent& event);
113
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);
123
124 void OnSetNativeDesc(wxCommandEvent& event);
125 void OnSetNativeUserDesc(wxCommandEvent& event);
126
127 void OnSetFamily(wxCommandEvent& event);
128 void OnSetFaceName(wxCommandEvent& event);
129 void OnSetEncoding(wxCommandEvent& event);
130
131 protected:
132 bool DoEnumerateFamilies(bool fixedWidthOnly,
133 wxFontEncoding encoding = wxFONTENCODING_SYSTEM,
134 bool silent = false);
135
136 void DoResizeFont(int diff);
137 void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour);
138
139 // ask the user to choose an encoding and return it or
140 // wxFONTENCODING_SYSTEM if the dialog was cancelled
141 wxFontEncoding GetEncodingFromUser();
142
143 // ask the user to choose a font family and return it or
144 // wxFONTFAMILY_DEFAULT if the dialog was cancelled
145 wxFontFamily GetFamilyFromUser();
146
147 size_t m_fontSize; // in points
148
149 wxTextCtrl *m_textctrl;
150 MyCanvas *m_canvas;
151
152 private:
153 // any class wishing to process wxWidgets events must use this macro
154 DECLARE_EVENT_TABLE()
155 };
156
157 // ----------------------------------------------------------------------------
158 // constants
159 // ----------------------------------------------------------------------------
160
161 // IDs for the controls and the menu commands
162 enum
163 {
164 // menu items
165 Font_Quit = wxID_EXIT,
166 Font_About = wxID_ABOUT,
167
168 Font_ViewMsg = wxID_HIGHEST+1,
169 Font_TestTextValue,
170
171 Font_IncSize,
172 Font_DecSize,
173
174 Font_Bold,
175 Font_Light,
176
177 Font_Italic,
178 Font_Slant,
179
180 Font_Underlined,
181
182 // standard global wxFont objects:
183 Font_wxNORMAL_FONT,
184 Font_wxSMALL_FONT,
185 Font_wxITALIC_FONT,
186 Font_wxSWISS_FONT,
187 Font_Standard,
188
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,
196 Font_SystemSettings,
197
198 Font_Choose = 100,
199 Font_EnumFamiliesForEncoding,
200 Font_EnumFamilies,
201 Font_EnumFixedFamilies,
202 Font_EnumEncodings,
203 Font_SetNativeDesc,
204 Font_SetNativeUserDesc,
205 Font_SetFamily,
206 Font_SetFaceName,
207 Font_SetEncoding,
208 Font_Max
209 };
210
211 // ----------------------------------------------------------------------------
212 // event tables and other macros for wxWidgets
213 // ----------------------------------------------------------------------------
214
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)
223
224 EVT_MENU(Font_IncSize, MyFrame::OnIncFont)
225 EVT_MENU(Font_DecSize, MyFrame::OnDecFont)
226
227 EVT_MENU(Font_Bold, MyFrame::OnBold)
228 EVT_MENU(Font_Light, MyFrame::OnLight)
229
230 EVT_MENU(Font_Italic, MyFrame::OnItalic)
231 EVT_MENU(Font_Slant, MyFrame::OnSlant)
232
233 EVT_MENU(Font_Underlined, MyFrame::OnUnderline)
234
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)
239
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)
246
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)
252
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)
258 END_EVENT_TABLE()
259
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
264 // not wxApp)
265 IMPLEMENT_APP(MyApp)
266
267 // ============================================================================
268 // implementation
269 // ============================================================================
270
271 // ----------------------------------------------------------------------------
272 // the application class
273 // ----------------------------------------------------------------------------
274
275 // `Main program' equivalent: the program execution "starts" here
276 bool MyApp::OnInit()
277 {
278 if ( !wxApp::OnInit() )
279 return false;
280
281 // Create the main application window
282 MyFrame *frame = new MyFrame(wxT("Font wxWidgets demo"),
283 wxPoint(50, 50), wxSize(600, 400));
284
285 // Show it and tell the application that it's our main window
286 frame->Show(true);
287 SetTopWindow(frame);
288
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.
292 return true;
293 }
294
295 // ----------------------------------------------------------------------------
296 // main frame
297 // ----------------------------------------------------------------------------
298
299 // frame constructor
300 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
301 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), m_textctrl(NULL)
302 {
303 m_fontSize = wxNORMAL_FONT->GetPointSize();
304
305 SetIcon(wxIcon(sample_xpm));
306
307 // create a menu bar
308 wxMenu *menuFile = new wxMenu;
309
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"));
318
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"));
327 menuFont->AppendCheckItem(Font_Slant, wxT("&Slant\tCtrl-S"), wxT("Toggle slant state"));
328 menuFont->AppendSeparator();
329 menuFont->AppendCheckItem(Font_Underlined, wxT("&Underlined\tCtrl-U"),
330 wxT("Toggle underlined state"));
331
332 menuFont->AppendSeparator();
333 menuFont->Append(Font_SetNativeDesc,
334 wxT("Set native font &description\tShift-Ctrl-D"));
335 menuFont->Append(Font_SetNativeUserDesc,
336 wxT("Set &user font description\tShift-Ctrl-U"));
337 menuFont->AppendSeparator();
338 menuFont->Append(Font_SetFamily, wxT("Set font family"));
339 menuFont->Append(Font_SetFaceName, wxT("Set font face name"));
340 menuFont->Append(Font_SetEncoding, wxT("Set font &encoding\tShift-Ctrl-E"));
341
342 wxMenu *menuSelect = new wxMenu;
343 menuSelect->Append(Font_Choose, wxT("&Select font...\tCtrl-S"),
344 wxT("Select a standard font"));
345
346 wxMenu *menuStdFonts = new wxMenu;
347 menuStdFonts->Append(Font_wxNORMAL_FONT, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
348 menuStdFonts->Append(Font_wxSMALL_FONT, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
349 menuStdFonts->Append(Font_wxITALIC_FONT, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
350 menuStdFonts->Append(Font_wxSWISS_FONT, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
351 menuSelect->Append(Font_Standard, wxT("Standar&d fonts"), menuStdFonts);
352
353 wxMenu *menuSettingFonts = new wxMenu;
354 menuSettingFonts->Append(Font_wxSYS_OEM_FIXED_FONT, wxT("wxSYS_OEM_FIXED_FONT"),
355 wxT("Original equipment manufacturer dependent fixed-pitch font."));
356 menuSettingFonts->Append(Font_wxSYS_ANSI_FIXED_FONT, wxT("wxSYS_ANSI_FIXED_FONT"),
357 wxT("Windows fixed-pitch (monospaced) font. "));
358 menuSettingFonts->Append(Font_wxSYS_ANSI_VAR_FONT, wxT("wxSYS_ANSI_VAR_FONT"),
359 wxT("Windows variable-pitch (proportional) font."));
360 menuSettingFonts->Append(Font_wxSYS_SYSTEM_FONT, wxT("wxSYS_SYSTEM_FONT"),
361 wxT("System font."));
362 menuSettingFonts->Append(Font_wxSYS_DEVICE_DEFAULT_FONT, wxT("wxSYS_DEVICE_DEFAULT_FONT"),
363 wxT("Device-dependent font."));
364 menuSettingFonts->Append(Font_wxSYS_DEFAULT_GUI_FONT, wxT("wxSYS_DEFAULT_GUI_FONT"),
365 wxT("Default font for user interface objects such as menus and dialog boxes. "));
366 menuSelect->Append(Font_SystemSettings, wxT("System fonts"), menuSettingFonts);
367
368
369 menuSelect->AppendSeparator();
370 menuSelect->Append(Font_EnumFamilies, wxT("Enumerate font &families\tCtrl-F"));
371 menuSelect->Append(Font_EnumFixedFamilies,
372 wxT("Enumerate fi&xed font families\tCtrl-X"));
373 menuSelect->Append(Font_EnumEncodings,
374 wxT("Enumerate &encodings\tCtrl-E"));
375 menuSelect->Append(Font_EnumFamiliesForEncoding,
376 wxT("Find font for en&coding...\tCtrl-C"),
377 wxT("Find font families for given encoding"));
378
379 // now append the freshly created menu to the menu bar...
380 wxMenuBar *menuBar = new wxMenuBar;
381 menuBar->Append(menuFile, wxT("&File"));
382 menuBar->Append(menuFont, wxT("F&ont"));
383 menuBar->Append(menuSelect, wxT("&Select"));
384
385 // ... and attach this menu bar to the frame
386 SetMenuBar(menuBar);
387
388 wxSplitterWindow *splitter = new wxSplitterWindow(this);
389
390 m_textctrl = new wxTextCtrl(splitter, wxID_ANY,
391 wxT("Paste text here to see how it looks\nlike in the given font"),
392 wxDefaultPosition, wxDefaultSize,
393 wxTE_MULTILINE);
394
395 m_canvas = new MyCanvas(splitter);
396
397 splitter->SplitHorizontally(m_textctrl, m_canvas, 100);
398
399 #if wxUSE_STATUSBAR
400 // create a status bar just for fun (by default with 1 pane only)
401 CreateStatusBar();
402 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
403 #endif // wxUSE_STATUSBAR
404 }
405
406 // --------------------------------------------------------
407
408 class MyEncodingEnumerator : public wxFontEnumerator
409 {
410 public:
411 MyEncodingEnumerator()
412 { m_n = 0; }
413
414 const wxString& GetText() const
415 { return m_text; }
416
417 protected:
418 virtual bool OnFontEncoding(const wxString& facename,
419 const wxString& encoding)
420 {
421 wxString text;
422 text.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
423 (unsigned int) ++m_n, encoding.c_str(), facename.c_str());
424 m_text += text;
425 return true;
426 }
427
428 private:
429 size_t m_n;
430 wxString m_text;
431 };
432
433 void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
434 {
435 MyEncodingEnumerator fontEnumerator;
436
437 fontEnumerator.EnumerateEncodings();
438
439 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
440 fontEnumerator.GetText().c_str());
441 }
442
443 // -------------------------------------------------------------
444
445 class MyFontEnumerator : public wxFontEnumerator
446 {
447 public:
448 bool GotAny() const
449 { return !m_facenames.IsEmpty(); }
450
451 const wxArrayString& GetFacenames() const
452 { return m_facenames; }
453
454 protected:
455 virtual bool OnFacename(const wxString& facename)
456 {
457 m_facenames.Add(facename);
458 return true;
459 }
460
461 private:
462 wxArrayString m_facenames;
463 } fontEnumerator;
464
465 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
466 wxFontEncoding encoding,
467 bool silent)
468 {
469 MyFontEnumerator fontEnumerator;
470
471 fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
472
473 if ( fontEnumerator.GotAny() )
474 {
475 int nFacenames = fontEnumerator.GetFacenames().GetCount();
476 if ( !silent )
477 {
478 wxLogStatus(this, wxT("Found %d %sfonts"),
479 nFacenames, fixedWidthOnly ? wxT("fixed width ") : wxT(""));
480 }
481
482 wxString facename;
483
484 if ( silent )
485 {
486 // choose the first
487 facename = fontEnumerator.GetFacenames().Item(0);
488 }
489 else
490 {
491 // let the user choose
492 wxString *facenames = new wxString[nFacenames];
493 int n;
494 for ( n = 0; n < nFacenames; n++ )
495 facenames[n] = fontEnumerator.GetFacenames().Item(n);
496
497 n = wxGetSingleChoiceIndex
498 (
499 wxT("Choose a facename"),
500 SAMPLE_TITLE,
501 nFacenames,
502 facenames,
503 this
504 );
505
506 if ( n != -1 )
507 facename = facenames[n];
508
509 delete [] facenames;
510 }
511
512 if ( !facename.empty() )
513 {
514 wxFont font(wxNORMAL_FONT->GetPointSize(),
515 wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
516 wxFONTWEIGHT_NORMAL, false, facename, encoding);
517
518 DoChangeFont(font);
519 }
520
521 return true;
522 }
523 else if ( !silent )
524 {
525 wxLogWarning(wxT("No such fonts found."));
526 }
527
528 return false;
529 }
530
531 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
532 {
533 wxFontEncoding enc = GetEncodingFromUser();
534 if ( enc != wxFONTENCODING_SYSTEM )
535 {
536 DoEnumerateFamilies(false, enc);
537 }
538 }
539
540 void MyFrame::OnSetNativeDesc(wxCommandEvent& WXUNUSED(event))
541 {
542 wxString fontInfo = wxGetTextFromUser
543 (
544 wxT("Enter native font string"),
545 wxT("Input font description"),
546 m_canvas->GetTextFont().GetNativeFontInfoDesc(),
547 this
548 );
549 if ( fontInfo.empty() )
550 return; // user clicked "Cancel" - do nothing
551
552 wxFont font;
553 font.SetNativeFontInfo(fontInfo);
554 if ( !font.Ok() )
555 {
556 wxLogError(wxT("Font info string \"%s\" is invalid."),
557 fontInfo.c_str());
558 return;
559 }
560
561 DoChangeFont(font);
562 }
563
564 void MyFrame::OnSetNativeUserDesc(wxCommandEvent& WXUNUSED(event))
565 {
566 wxString fontdesc = GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
567 wxString fontUserInfo = wxGetTextFromUser(
568 wxT("Here you can edit current font description"),
569 wxT("Input font description"), fontdesc,
570 this);
571 if (fontUserInfo.IsEmpty())
572 return; // user clicked "Cancel" - do nothing
573
574 wxFont font;
575 if (font.SetNativeFontInfoUserDesc(fontUserInfo))
576 {
577 wxASSERT_MSG(font.Ok(), wxT("The font should now be valid"));
578 DoChangeFont(font);
579 }
580 else
581 {
582 wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid"));
583 wxMessageBox(wxT("Error trying to create a font with such description..."));
584 }
585 }
586
587 void MyFrame::OnSetFamily(wxCommandEvent& WXUNUSED(event))
588 {
589 wxFontFamily f = GetFamilyFromUser();
590
591 wxFont font = m_canvas->GetTextFont();
592 font.SetFamily(f);
593 DoChangeFont(font);
594 }
595
596 void MyFrame::OnSetFaceName(wxCommandEvent& WXUNUSED(event))
597 {
598 wxString facename = GetCanvas()->GetTextFont().GetFaceName();
599 wxString newFaceName = wxGetTextFromUser(
600 wxT("Here you can edit current font face name."),
601 wxT("Input font facename"), facename,
602 this);
603 if (newFaceName.IsEmpty())
604 return; // user clicked "Cancel" - do nothing
605
606 wxFont font(GetCanvas()->GetTextFont());
607 if (font.SetFaceName(newFaceName)) // change facename only
608 {
609 wxASSERT_MSG(font.Ok(), wxT("The font should now be valid"));
610 DoChangeFont(font);
611 }
612 else
613 {
614 wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid"));
615 wxMessageBox(wxT("There is no font with such face name..."),
616 wxT("Invalid face name"), wxOK|wxICON_ERROR, this);
617 }
618 }
619
620 void MyFrame::OnSetEncoding(wxCommandEvent& WXUNUSED(event))
621 {
622 wxFontEncoding enc = GetEncodingFromUser();
623 if ( enc == wxFONTENCODING_SYSTEM )
624 return;
625
626 wxFont font = m_canvas->GetTextFont();
627 font.SetEncoding(enc);
628 DoChangeFont(font);
629 }
630
631 wxFontEncoding MyFrame::GetEncodingFromUser()
632 {
633 wxArrayString names;
634 wxArrayInt encodings;
635
636 const size_t count = wxFontMapper::GetSupportedEncodingsCount();
637 names.reserve(count);
638 encodings.reserve(count);
639
640 for ( size_t n = 0; n < count; n++ )
641 {
642 wxFontEncoding enc = wxFontMapper::GetEncoding(n);
643 encodings.push_back(enc);
644 names.push_back(wxFontMapper::GetEncodingName(enc));
645 }
646
647 int i = wxGetSingleChoiceIndex
648 (
649 wxT("Choose the encoding"),
650 SAMPLE_TITLE,
651 names,
652 this
653 );
654
655 return i == -1 ? wxFONTENCODING_SYSTEM : (wxFontEncoding)encodings[i];
656 }
657
658 wxFontFamily MyFrame::GetFamilyFromUser()
659 {
660 wxArrayString names;
661 wxArrayInt families;
662
663 families.push_back(wxFONTFAMILY_DECORATIVE);
664 families.push_back(wxFONTFAMILY_ROMAN);
665 families.push_back(wxFONTFAMILY_SCRIPT);
666 families.push_back(wxFONTFAMILY_SWISS);
667 families.push_back(wxFONTFAMILY_MODERN);
668 families.push_back(wxFONTFAMILY_TELETYPE);
669
670 names.push_back("DECORATIVE");
671 names.push_back("ROMAN");
672 names.push_back("SCRIPT");
673 names.push_back("SWISS");
674 names.push_back("MODERN");
675 names.push_back("TELETYPE");
676
677 int i = wxGetSingleChoiceIndex
678 (
679 wxT("Choose the family"),
680 SAMPLE_TITLE,
681 names,
682 this
683 );
684
685 return i == -1 ? wxFONTFAMILY_DEFAULT : (wxFontFamily)families[i];
686 }
687
688 void MyFrame::DoResizeFont(int diff)
689 {
690 wxFont font = m_canvas->GetTextFont();
691
692 font.SetPointSize(font.GetPointSize() + diff);
693 DoChangeFont(font);
694 }
695
696 void MyFrame::OnBold(wxCommandEvent& event)
697 {
698 wxFont font = m_canvas->GetTextFont();
699
700 font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
701 DoChangeFont(font);
702 }
703
704 void MyFrame::OnLight(wxCommandEvent& event)
705 {
706 wxFont font = m_canvas->GetTextFont();
707
708 font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_LIGHT : wxFONTWEIGHT_NORMAL);
709 DoChangeFont(font);
710 }
711
712 void MyFrame::OnItalic(wxCommandEvent& event)
713 {
714 wxFont font = m_canvas->GetTextFont();
715
716 font.SetStyle(event.IsChecked() ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
717 DoChangeFont(font);
718 }
719
720 void MyFrame::OnSlant(wxCommandEvent& event)
721 {
722 wxFont font = m_canvas->GetTextFont();
723
724 font.SetStyle(event.IsChecked() ? wxFONTSTYLE_SLANT : wxFONTSTYLE_NORMAL);
725 DoChangeFont(font);
726 }
727
728 void MyFrame::OnUnderline(wxCommandEvent& event)
729 {
730 wxFont font = m_canvas->GetTextFont();
731
732 font.SetUnderlined(event.IsChecked());
733 DoChangeFont(font);
734 }
735
736 void MyFrame::OnwxPointerFont(wxCommandEvent& event)
737 {
738 wxFont font;
739
740 switch ( event.GetId() )
741 {
742 case Font_wxNORMAL_FONT:
743 font = *wxNORMAL_FONT;
744 break;
745
746 case Font_wxSMALL_FONT:
747 font = *wxSMALL_FONT;
748 break;
749
750 case Font_wxITALIC_FONT:
751 font = *wxITALIC_FONT;
752 break;
753
754 case Font_wxSWISS_FONT:
755 font = *wxSWISS_FONT;
756 break;
757
758 default:
759 wxFAIL_MSG( wxT("unknown standard font") );
760 return;
761 }
762
763 DoChangeFont(font);
764 }
765
766 void MyFrame::OnwxSystemSettingsFont(wxCommandEvent& event)
767 {
768 wxFont font;
769
770 switch ( event.GetId() )
771 {
772 case Font_wxSYS_OEM_FIXED_FONT:
773 font = wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT);
774 break;
775
776 case Font_wxSYS_ANSI_FIXED_FONT:
777 font = wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT);
778 break;
779
780 case Font_wxSYS_ANSI_VAR_FONT:
781 font = wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT);
782 break;
783
784 case Font_wxSYS_SYSTEM_FONT:
785 font = wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT);
786 break;
787
788 case Font_wxSYS_DEVICE_DEFAULT_FONT:
789 font = wxSystemSettings::GetFont(wxSYS_DEVICE_DEFAULT_FONT);
790 break;
791
792 case Font_wxSYS_DEFAULT_GUI_FONT:
793 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
794 break;
795
796 default:
797 wxFAIL_MSG( wxT("unknown standard font") );
798 return;
799 }
800
801 DoChangeFont(font);
802 }
803
804 void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col)
805 {
806 m_canvas->SetTextFont(font);
807 if ( col.Ok() )
808 m_canvas->SetColour(col);
809 m_canvas->Refresh();
810
811 m_textctrl->SetFont(font);
812 if ( col.Ok() )
813 m_textctrl->SetForegroundColour(col);
814
815 // update the state of the bold/italic/underlined menu items
816 wxMenuBar *mbar = GetMenuBar();
817 if ( mbar )
818 {
819 mbar->Check(Font_Light, font.GetWeight() == wxFONTWEIGHT_LIGHT);
820 mbar->Check(Font_Bold, font.GetWeight() == wxFONTWEIGHT_BOLD);
821
822 mbar->Check(Font_Italic, font.GetStyle() == wxFONTSTYLE_ITALIC);
823 mbar->Check(Font_Slant, font.GetStyle() == wxFONTSTYLE_SLANT);
824
825 mbar->Check(Font_Underlined, font.GetUnderlined());
826 }
827 }
828
829 void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
830 {
831 wxFontData data;
832 data.SetInitialFont(m_canvas->GetTextFont());
833 data.SetColour(m_canvas->GetColour());
834
835 wxFontDialog dialog(this, data);
836 if ( dialog.ShowModal() == wxID_OK )
837 {
838 wxFontData retData = dialog.GetFontData();
839 wxFont font = retData.GetChosenFont();
840 wxColour colour = retData.GetColour();
841
842 DoChangeFont(font, colour);
843 }
844 }
845
846 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
847 {
848 // true is to force the frame to close
849 Close(true);
850 }
851
852 void MyFrame::OnTestTextValue(wxCommandEvent& WXUNUSED(event))
853 {
854 wxString value = m_textctrl->GetValue();
855 m_textctrl->SetValue(value);
856 if ( m_textctrl->GetValue() != value )
857 {
858 wxLogError(wxT("Text value changed after getting and setting it"));
859 }
860 }
861
862 void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
863 {
864 #if wxUSE_FILEDLG
865 // first, choose the file
866 static wxString s_dir, s_file;
867 wxFileDialog dialog(this, wxT("Open an email message file"),
868 s_dir, s_file);
869 if ( dialog.ShowModal() != wxID_OK )
870 return;
871
872 // save for the next time
873 s_dir = dialog.GetDirectory();
874 s_file = dialog.GetFilename();
875
876 wxString filename = dialog.GetPath();
877
878 // load it and search for Content-Type header
879 wxTextFile file(filename);
880 if ( !file.Open() )
881 return;
882
883 wxString charset;
884
885 static const wxChar *prefix = wxT("Content-Type: text/plain; charset=");
886 const size_t len = wxStrlen(prefix);
887
888 size_t n, count = file.GetLineCount();
889 for ( n = 0; n < count; n++ )
890 {
891 wxString line = file[n];
892
893 if ( !line )
894 {
895 // if it is an email message, headers are over, no need to parse
896 // all the file
897 break;
898 }
899
900 if ( line.Left(len) == prefix )
901 {
902 // found!
903 const wxChar *pc = line.c_str() + len;
904 if ( *pc == wxT('"') )
905 pc++;
906
907 while ( *pc && *pc != wxT('"') )
908 {
909 charset += *pc++;
910 }
911
912 break;
913 }
914 }
915
916 if ( !charset )
917 {
918 wxLogError(wxT("The file '%s' doesn't contain charset information."),
919 filename.c_str());
920
921 return;
922 }
923
924 // ok, now get the corresponding encoding
925 wxFontEncoding fontenc = wxFontMapper::Get()->CharsetToEncoding(charset);
926 if ( fontenc == wxFONTENCODING_SYSTEM )
927 {
928 wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str());
929 return;
930 }
931
932 m_textctrl->LoadFile(filename);
933
934 if ( fontenc == wxFONTENCODING_UTF8 ||
935 !wxFontMapper::Get()->IsEncodingAvailable(fontenc) )
936 {
937 // try to find some similar encoding:
938 wxFontEncoding encAlt;
939 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc, &encAlt) )
940 {
941 wxEncodingConverter conv;
942
943 if (conv.Init(fontenc, encAlt))
944 {
945 fontenc = encAlt;
946 m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue()));
947 }
948 else
949 {
950 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
951 wxFontMapper::GetEncodingDescription(fontenc).c_str(),
952 wxFontMapper::GetEncodingDescription(encAlt).c_str());
953 }
954 }
955 else
956 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
957 wxFontMapper::GetEncodingDescription(fontenc).c_str());
958 }
959
960 // and now create the correct font
961 if ( !DoEnumerateFamilies(false, fontenc, true /* silent */) )
962 {
963 wxFont font(wxNORMAL_FONT->GetPointSize(),
964 wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
965 wxFONTWEIGHT_NORMAL, false /* !underlined */,
966 wxEmptyString /* facename */, fontenc);
967 if ( font.Ok() )
968 {
969 DoChangeFont(font);
970 }
971 else
972 {
973 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
974 wxFontMapper::GetEncodingDescription(fontenc).c_str());
975 }
976 }
977 #endif // wxUSE_FILEDLG
978 }
979
980 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
981 {
982 wxMessageBox(wxT("wxWidgets font sample\n")
983 wxT("(c) 1999-2006 Vadim Zeitlin"),
984 wxString(wxT("About ")) + SAMPLE_TITLE,
985 wxOK | wxICON_INFORMATION, this);
986 }
987
988 // ----------------------------------------------------------------------------
989 // MyCanvas
990 // ----------------------------------------------------------------------------
991
992 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
993 EVT_PAINT(MyCanvas::OnPaint)
994 END_EVENT_TABLE()
995
996 MyCanvas::MyCanvas( wxWindow *parent )
997 : wxWindow( parent, wxID_ANY ),
998 m_colour(*wxRED), m_font(*wxNORMAL_FONT)
999 {
1000 }
1001
1002 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
1003 {
1004 wxPaintDC dc(this);
1005 PrepareDC(dc);
1006
1007 // set background
1008 dc.SetBackground(wxBrush(wxT("white"), wxSOLID));
1009 dc.Clear();
1010
1011 // one text line height
1012 wxCoord hLine = dc.GetCharHeight();
1013
1014 // the current text origin
1015 wxCoord x = 5,
1016 y = 5;
1017
1018 // output the font name/info
1019 wxString fontInfo;
1020 fontInfo.Printf(wxT("Font size is %d points, family: %s, encoding: %s"),
1021 m_font.GetPointSize(),
1022 m_font.GetFamilyString().c_str(),
1023 wxFontMapper::
1024 GetEncodingDescription(m_font.GetEncoding()).c_str());
1025
1026 dc.DrawText(fontInfo, x, y);
1027 y += hLine;
1028
1029 fontInfo.Printf(wxT("Style: %s, weight: %s, fixed width: %s"),
1030 m_font.GetStyleString().c_str(),
1031 m_font.GetWeightString().c_str(),
1032 m_font.IsFixedWidth() ? _T("yes") : _T("no"));
1033
1034 dc.DrawText(fontInfo, x, y);
1035 y += hLine;
1036
1037 if ( m_font.Ok() )
1038 {
1039 const wxNativeFontInfo *info = m_font.GetNativeFontInfo();
1040 if ( info )
1041 {
1042 wxString fontDesc = m_font.GetNativeFontInfoUserDesc();
1043 fontInfo.Printf(wxT("Native font info: %s"), fontDesc.c_str());
1044
1045 dc.DrawText(fontInfo, x, y);
1046 y += hLine;
1047 }
1048 }
1049
1050 y += hLine;
1051
1052 // prepare to draw the font
1053 dc.SetFont(m_font);
1054 dc.SetTextForeground(m_colour);
1055
1056 // the size of one cell (Normally biggest char + small margin)
1057 wxCoord maxCharWidth, maxCharHeight;
1058 dc.GetTextExtent(wxT("W"), &maxCharWidth, &maxCharHeight);
1059 int w = maxCharWidth + 5,
1060 h = maxCharHeight + 4;
1061
1062
1063 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
1064 for ( int i = 0; i < 7; i++ )
1065 {
1066 for ( int j = 0; j < 32; j++ )
1067 {
1068 wxChar c = (wxChar)(32 * (i + 1) + j);
1069
1070 wxCoord charWidth, charHeight;
1071 dc.GetTextExtent(c, &charWidth, &charHeight);
1072 dc.DrawText
1073 (
1074 c,
1075 x + w*j + (maxCharWidth - charWidth) / 2 + 1,
1076 y + h*i + (maxCharHeight - charHeight) / 2
1077 );
1078 }
1079 }
1080
1081 // draw the lines between them
1082 dc.SetPen(wxPen(wxColour(_T("blue")), 1, wxSOLID));
1083 int l;
1084
1085 // horizontal
1086 for ( l = 0; l < 8; l++ )
1087 {
1088 int yl = y + h*l - 2;
1089 dc.DrawLine(x - 2, yl, x + 32*w - 1, yl);
1090 }
1091
1092 // and vertical
1093 for ( l = 0; l < 33; l++ )
1094 {
1095 int xl = x + w*l - 2;
1096 dc.DrawLine(xl, y - 2, xl, y + 7*h - 1);
1097 }
1098 }