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