]> git.saurik.com Git - wxWidgets.git/blob - samples/font/font.cpp
final sweep over docs - replace & with \&
[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
35 #ifdef __WXMAC__
36 #undef wxFontDialog
37 #include "wx/mac/fontdlg.h"
38 #endif
39
40 // ----------------------------------------------------------------------------
41 // private classes
42 // ----------------------------------------------------------------------------
43
44 // Define a new application type, each program should derive a class from wxApp
45 class MyApp : public wxApp
46 {
47 public:
48 // override base class virtuals
49 // ----------------------------
50
51 // this one is called on application startup and is a good place for the app
52 // initialization (doing it here and not in the ctor allows to have an error
53 // return: if OnInit() returns false, the application terminates)
54 virtual bool OnInit();
55 };
56
57 // MyCanvas is a canvas on which we show the font sample
58 class MyCanvas: public wxWindow
59 {
60 public:
61 MyCanvas( wxWindow *parent );
62 virtual ~MyCanvas(){};
63
64 // accessors for the frame
65 const wxFont& GetTextFont() const { return m_font; }
66 const wxColour& GetColour() const { return m_colour; }
67 void SetTextFont(const wxFont& font) { m_font = font; }
68 void SetColour(const wxColour& colour) { m_colour = colour; }
69
70 // event handlers
71 void OnPaint( wxPaintEvent &event );
72
73 private:
74 wxColour m_colour;
75 wxFont m_font;
76
77 DECLARE_EVENT_TABLE()
78 };
79
80 // Define a new frame type: this is going to be our main frame
81 class MyFrame : public wxFrame
82 {
83 public:
84 // ctor(s)
85 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
86
87 // accessors
88 MyCanvas *GetCanvas() const { return m_canvas; }
89
90 // event handlers (these functions should _not_ be virtual)
91 void OnQuit(wxCommandEvent& event);
92 void OnAbout(wxCommandEvent& event);
93
94 void OnIncFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(+2); }
95 void OnDecFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(-2); }
96
97 void OnBold(wxCommandEvent& event);
98 void OnItalic(wxCommandEvent& event);
99 void OnUnderline(wxCommandEvent& event);
100
101 void OnwxPointerFont(wxCommandEvent& event);
102
103 void OnViewMsg(wxCommandEvent& event);
104 void OnSelectFont(wxCommandEvent& event);
105 void OnEnumerateFamiliesForEncoding(wxCommandEvent& event);
106 void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event))
107 { DoEnumerateFamilies(false); }
108 void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event))
109 { DoEnumerateFamilies(true); }
110 void OnEnumerateEncodings(wxCommandEvent& event);
111
112 void OnCheckNativeToFromString(wxCommandEvent& event);
113
114 protected:
115 bool DoEnumerateFamilies(bool fixedWidthOnly,
116 wxFontEncoding encoding = wxFONTENCODING_SYSTEM,
117 bool silent = false);
118
119 void DoResizeFont(int diff);
120 void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour);
121
122 size_t m_fontSize; // in points
123
124 wxTextCtrl *m_textctrl;
125 MyCanvas *m_canvas;
126
127 private:
128 // any class wishing to process wxWidgets events must use this macro
129 DECLARE_EVENT_TABLE()
130 };
131
132 // ----------------------------------------------------------------------------
133 // constants
134 // ----------------------------------------------------------------------------
135
136 // IDs for the controls and the menu commands
137 enum
138 {
139 // menu items
140 Font_Quit = 1,
141 Font_About,
142 Font_ViewMsg,
143 Font_IncSize,
144 Font_DecSize,
145 Font_Bold,
146 Font_Italic,
147 Font_Underlined,
148 Font_wxNORMAL_FONT,
149 Font_wxSMALL_FONT,
150 Font_wxITALIC_FONT,
151 Font_wxSWISS_FONT,
152 Font_Standard,
153
154 Font_Choose = 100,
155 Font_EnumFamiliesForEncoding,
156 Font_EnumFamilies,
157 Font_EnumFixedFamilies,
158 Font_EnumEncodings,
159 Font_CheckNativeToFromString,
160 Font_Max
161 };
162
163 // ----------------------------------------------------------------------------
164 // event tables and other macros for wxWidgets
165 // ----------------------------------------------------------------------------
166
167 // the event tables connect the wxWidgets events with the functions (event
168 // handlers) which process them. It can be also done at run-time, but for the
169 // simple menu events like this the static method is much simpler.
170 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
171 EVT_MENU(Font_Quit, MyFrame::OnQuit)
172 EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg)
173 EVT_MENU(Font_About, MyFrame::OnAbout)
174
175 EVT_MENU(Font_IncSize, MyFrame::OnIncFont)
176 EVT_MENU(Font_DecSize, MyFrame::OnDecFont)
177 EVT_MENU(Font_Bold, MyFrame::OnBold)
178 EVT_MENU(Font_Italic, MyFrame::OnItalic)
179 EVT_MENU(Font_Underlined, MyFrame::OnUnderline)
180
181 EVT_MENU(Font_wxNORMAL_FONT, MyFrame::OnwxPointerFont)
182 EVT_MENU(Font_wxSMALL_FONT, MyFrame::OnwxPointerFont)
183 EVT_MENU(Font_wxITALIC_FONT, MyFrame::OnwxPointerFont)
184 EVT_MENU(Font_wxSWISS_FONT, MyFrame::OnwxPointerFont)
185
186 EVT_MENU(Font_CheckNativeToFromString, MyFrame::OnCheckNativeToFromString)
187
188 EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
189 EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding)
190 EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
191 EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
192 EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
193 END_EVENT_TABLE()
194
195 // Create a new application object: this macro will allow wxWidgets to create
196 // the application object during program execution (it's better than using a
197 // static object for many reasons) and also declares the accessor function
198 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
199 // not wxApp)
200 IMPLEMENT_APP(MyApp)
201
202 // ============================================================================
203 // implementation
204 // ============================================================================
205
206 // ----------------------------------------------------------------------------
207 // the application class
208 // ----------------------------------------------------------------------------
209
210 // `Main program' equivalent: the program execution "starts" here
211 bool MyApp::OnInit()
212 {
213 // Create the main application window
214 MyFrame *frame = new MyFrame(wxT("Font wxWidgets demo"),
215 wxPoint(50, 50), wxSize(600, 400));
216
217 // Show it and tell the application that it's our main window
218 frame->Show(true);
219 SetTopWindow(frame);
220
221 // success: wxApp::OnRun() will be called which will enter the main message
222 // loop and the application will run. If we returned 'false' here, the
223 // application would exit immediately.
224 return true;
225 }
226
227 // ----------------------------------------------------------------------------
228 // main frame
229 // ----------------------------------------------------------------------------
230
231 // frame constructor
232 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
233 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), m_textctrl(NULL)
234 {
235 m_fontSize = 12;
236
237 // create a menu bar
238 wxMenu *menuFile = new wxMenu;
239
240 menuFile->Append(Font_ViewMsg, wxT("&View...\tCtrl-V"),
241 wxT("View an email message file"));
242 menuFile->AppendSeparator();
243 menuFile->Append(Font_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
244 menuFile->AppendSeparator();
245 menuFile->Append(Font_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
246
247 wxMenu *menuFont = new wxMenu;
248 menuFont->Append(Font_IncSize, wxT("&Increase font size by 2 points\tCtrl-I"));
249 menuFont->Append(Font_DecSize, wxT("&Decrease font size by 2 points\tCtrl-D"));
250 menuFont->AppendSeparator();
251 menuFont->AppendCheckItem(Font_Bold, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
252 menuFont->AppendCheckItem(Font_Italic, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
253 menuFont->AppendCheckItem(Font_Underlined, wxT("&Underlined\tCtrl-U"),
254 wxT("Toggle underlined state"));
255
256 menuFont->AppendSeparator();
257 menuFont->Append(Font_CheckNativeToFromString,
258 wxT("Check Native Font Info To/From String"));
259
260 wxMenu *menuSelect = new wxMenu;
261 menuSelect->Append(Font_Choose, wxT("&Select font...\tCtrl-S"),
262 wxT("Select a standard font"));
263
264 wxMenu *menuStdFonts = new wxMenu;
265 menuStdFonts->Append(Font_wxNORMAL_FONT, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
266 menuStdFonts->Append(Font_wxSMALL_FONT, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
267 menuStdFonts->Append(Font_wxITALIC_FONT, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
268 menuStdFonts->Append(Font_wxSWISS_FONT, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
269 menuSelect->Append(Font_Standard, wxT("Standar&d fonts"), menuStdFonts);
270
271 menuSelect->AppendSeparator();
272 menuSelect->Append(Font_EnumFamilies, wxT("Enumerate font &families\tCtrl-F"));
273 menuSelect->Append(Font_EnumFixedFamilies,
274 wxT("Enumerate fi&xed font families\tCtrl-X"));
275 menuSelect->Append(Font_EnumEncodings,
276 wxT("Enumerate &encodings\tCtrl-E"));
277 menuSelect->Append(Font_EnumFamiliesForEncoding,
278 wxT("Find font for en&coding...\tCtrl-C"),
279 wxT("Find font families for given encoding"));
280
281 // now append the freshly created menu to the menu bar...
282 wxMenuBar *menuBar = new wxMenuBar;
283 menuBar->Append(menuFile, wxT("&File"));
284 menuBar->Append(menuFont, wxT("F&ont"));
285 menuBar->Append(menuSelect, wxT("&Select"));
286
287 // ... and attach this menu bar to the frame
288 SetMenuBar(menuBar);
289
290 wxSplitterWindow *splitter = new wxSplitterWindow(this);
291
292 m_textctrl = new wxTextCtrl(splitter, wxID_ANY,
293 wxT("Paste text here to see how it looks\nlike in the given font"),
294 wxDefaultPosition, wxDefaultSize,
295 wxTE_MULTILINE);
296
297 m_canvas = new MyCanvas(splitter);
298
299 splitter->SplitHorizontally(m_textctrl, m_canvas, 100);
300
301 #if wxUSE_STATUSBAR
302 // create a status bar just for fun (by default with 1 pane only)
303 CreateStatusBar();
304 SetStatusText(wxT("Welcome to wxWidgets font demo!"));
305 #endif // wxUSE_STATUSBAR
306 }
307
308 // --------------------------------------------------------
309
310 class MyEncodingEnumerator : public wxFontEnumerator
311 {
312 public:
313 MyEncodingEnumerator()
314 { m_n = 0; }
315
316 const wxString& GetText() const
317 { return m_text; }
318
319 protected:
320 virtual bool OnFontEncoding(const wxString& facename,
321 const wxString& encoding)
322 {
323 wxString text;
324 text.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
325 (unsigned int) ++m_n, encoding.c_str(), facename.c_str());
326 m_text += text;
327 return true;
328 }
329
330 private:
331 size_t m_n;
332 wxString m_text;
333 };
334
335 void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
336 {
337 MyEncodingEnumerator fontEnumerator;
338
339 fontEnumerator.EnumerateEncodings();
340
341 wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
342 fontEnumerator.GetText().c_str());
343 }
344
345 // -------------------------------------------------------------
346
347 class MyFontEnumerator : public wxFontEnumerator
348 {
349 public:
350 bool GotAny() const
351 { return !m_facenames.IsEmpty(); }
352
353 const wxArrayString& GetFacenames() const
354 { return m_facenames; }
355
356 protected:
357 virtual bool OnFacename(const wxString& facename)
358 {
359 m_facenames.Add(facename);
360 return true;
361 }
362
363 private:
364 wxArrayString m_facenames;
365 } fontEnumerator;
366
367 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
368 wxFontEncoding encoding,
369 bool silent)
370 {
371 MyFontEnumerator fontEnumerator;
372
373 fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
374
375 if ( fontEnumerator.GotAny() )
376 {
377 int nFacenames = fontEnumerator.GetFacenames().GetCount();
378 if ( !silent )
379 {
380 wxLogStatus(this, wxT("Found %d %sfonts"),
381 nFacenames, fixedWidthOnly ? wxT("fixed width ") : wxT(""));
382 }
383
384 wxString facename;
385 if ( silent )
386 {
387 // choose the first
388 facename = fontEnumerator.GetFacenames().Item(0);
389 }
390 else
391 {
392 // let the user choose
393 wxString *facenames = new wxString[nFacenames];
394 int n;
395 for ( n = 0; n < nFacenames; n++ )
396 facenames[n] = fontEnumerator.GetFacenames().Item(n);
397
398 n = wxGetSingleChoiceIndex(wxT("Choose a facename"), wxT("Font demo"),
399 nFacenames, facenames, this);
400
401 if ( n != -1 )
402 facename = facenames[n];
403
404 delete [] facenames;
405 }
406
407 if ( !facename.IsEmpty() )
408 {
409 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
410 wxFONTWEIGHT_NORMAL, false, facename, encoding);
411
412 DoChangeFont(font);
413 }
414
415 return true;
416 }
417 else if ( !silent )
418 {
419 wxLogWarning(wxT("No such fonts found."));
420 }
421
422 return false;
423 }
424
425 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
426 {
427 static wxFontEncoding encodings[] =
428 {
429 wxFONTENCODING_ISO8859_1,
430 wxFONTENCODING_ISO8859_2,
431 wxFONTENCODING_ISO8859_5,
432 wxFONTENCODING_ISO8859_7,
433 wxFONTENCODING_ISO8859_15,
434 wxFONTENCODING_KOI8,
435 wxFONTENCODING_KOI8_U,
436 wxFONTENCODING_CP1250,
437 wxFONTENCODING_CP1251,
438 wxFONTENCODING_CP1252,
439 };
440
441 static const wxString encodingNames[] =
442 {
443 wxT("Western European (ISO-8859-1)"),
444 wxT("Central European (ISO-8859-2)"),
445 wxT("Cyrillic (ISO-8859-5)"),
446 wxT("Greek (ISO-8859-7)"),
447 wxT("Western European with Euro (ISO-8859-15)"),
448 wxT("KOI8-R"),
449 wxT("KOI8-U"),
450 wxT("Windows Central European (CP 1250)"),
451 wxT("Windows Cyrillic (CP 1251)"),
452 wxT("Windows Western European (CP 1252)"),
453 };
454
455 int n = wxGetSingleChoiceIndex(wxT("Choose an encoding"), wxT("Font demo"),
456 WXSIZEOF(encodingNames),
457 encodingNames,
458 this);
459
460 if ( n != -1 )
461 {
462 DoEnumerateFamilies(false, encodings[n]);
463 }
464 }
465
466 void MyFrame::OnCheckNativeToFromString(wxCommandEvent& WXUNUSED(event))
467 {
468 wxString fontInfo = m_canvas->GetTextFont().GetNativeFontInfoDesc();
469
470 if ( fontInfo.IsEmpty() )
471 {
472 wxLogError(wxT("Native font info string is empty!"));
473 }
474 else
475 {
476 wxFont *font = wxFont::New(fontInfo);
477 if ( fontInfo != font->GetNativeFontInfoDesc() )
478 wxLogError(wxT("wxNativeFontInfo ToString()/FromString() broken!"));
479 else
480 wxLogMessage(wxT("wxNativeFontInfo works: %s"), fontInfo.c_str());
481
482 delete font;
483 }
484 }
485
486 void MyFrame::DoResizeFont(int diff)
487 {
488 wxFont font = m_canvas->GetTextFont();
489
490 font.SetPointSize(font.GetPointSize() + diff);
491 DoChangeFont(font);
492 }
493
494 void MyFrame::OnBold(wxCommandEvent& event)
495 {
496 wxFont font = m_canvas->GetTextFont();
497
498 font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
499 DoChangeFont(font);
500 }
501
502 void MyFrame::OnItalic(wxCommandEvent& event)
503 {
504 wxFont font = m_canvas->GetTextFont();
505
506 font.SetStyle(event.IsChecked() ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
507 DoChangeFont(font);
508 }
509
510 void MyFrame::OnUnderline(wxCommandEvent& event)
511 {
512 wxFont font = m_canvas->GetTextFont();
513
514 font.SetUnderlined(event.IsChecked());
515 DoChangeFont(font);
516 }
517
518 void MyFrame::OnwxPointerFont(wxCommandEvent& event)
519 {
520 wxFont font;
521
522 switch (event.GetId())
523 {
524 case Font_wxNORMAL_FONT : font = wxFont(*wxNORMAL_FONT); break;
525 case Font_wxSMALL_FONT : font = wxFont(*wxSMALL_FONT); break;
526 case Font_wxITALIC_FONT : font = wxFont(*wxITALIC_FONT); break;
527 case Font_wxSWISS_FONT : font = wxFont(*wxSWISS_FONT); break;
528 default : font = wxFont(*wxNORMAL_FONT); break;
529 }
530
531 GetMenuBar()->Check(Font_Bold, false);
532 GetMenuBar()->Check(Font_Italic, false);
533 GetMenuBar()->Check(Font_Underlined, false);
534
535 DoChangeFont(font);
536 }
537
538 void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col)
539 {
540 m_canvas->SetTextFont(font);
541 if ( col.Ok() )
542 m_canvas->SetColour(col);
543 m_canvas->Refresh();
544
545 m_textctrl->SetFont(font);
546 if ( col.Ok() )
547 m_textctrl->SetForegroundColour(col);
548 }
549
550 void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
551 {
552 wxFontData data;
553 data.SetInitialFont(m_canvas->GetTextFont());
554 data.SetColour(m_canvas->GetColour());
555
556 wxFontDialog dialog(this, data);
557 if ( dialog.ShowModal() == wxID_OK )
558 {
559 wxFontData retData = dialog.GetFontData();
560 wxFont font = retData.GetChosenFont();
561 wxColour colour = retData.GetColour();
562
563 DoChangeFont(font, colour);
564
565 // update the state of the bold/italic/underlined menu items
566 wxMenuBar *mbar = GetMenuBar();
567 if ( mbar )
568 {
569 mbar->Check(Font_Bold, font.GetWeight() == wxFONTWEIGHT_BOLD);
570 mbar->Check(Font_Italic, font.GetStyle() == wxFONTSTYLE_ITALIC);
571 mbar->Check(Font_Underlined, font.GetUnderlined());
572 }
573 }
574 }
575
576 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
577 {
578 // true is to force the frame to close
579 Close(true);
580 }
581
582 void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
583 {
584 // first, choose the file
585 static wxString s_dir, s_file;
586 wxFileDialog dialog(this, wxT("Open an email message file"),
587 s_dir, s_file);
588 if ( dialog.ShowModal() != wxID_OK )
589 return;
590
591 // save for the next time
592 s_dir = dialog.GetDirectory();
593 s_file = dialog.GetFilename();
594
595 wxString filename = dialog.GetPath();
596
597 // load it and search for Content-Type header
598 wxTextFile file(filename);
599 if ( !file.Open() )
600 return;
601
602 wxString charset;
603
604 static const wxChar *prefix = wxT("Content-Type: text/plain; charset=");
605 const size_t len = wxStrlen(prefix);
606
607 size_t n, count = file.GetLineCount();
608 for ( n = 0; n < count; n++ )
609 {
610 wxString line = file[n];
611
612 if ( !line )
613 {
614 // if it is an email message, headers are over, no need to parse
615 // all the file
616 break;
617 }
618
619 if ( line.Left(len) == prefix )
620 {
621 // found!
622 const wxChar *pc = line.c_str() + len;
623 if ( *pc == wxT('"') )
624 pc++;
625
626 while ( *pc && *pc != wxT('"') )
627 {
628 charset += *pc++;
629 }
630
631 break;
632 }
633 }
634
635 if ( !charset )
636 {
637 wxLogError(wxT("The file '%s' doesn't contain charset information."),
638 filename.c_str());
639
640 return;
641 }
642
643 // ok, now get the corresponding encoding
644 wxFontEncoding fontenc = wxFontMapper::Get()->CharsetToEncoding(charset);
645 if ( fontenc == wxFONTENCODING_SYSTEM )
646 {
647 wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str());
648 return;
649 }
650
651 m_textctrl->LoadFile(filename);
652
653 if ( fontenc == wxFONTENCODING_UTF8 ||
654 !wxFontMapper::Get()->IsEncodingAvailable(fontenc) )
655 {
656 // try to find some similar encoding:
657 wxFontEncoding encAlt;
658 if ( wxFontMapper::Get()->GetAltForEncoding(fontenc, &encAlt) )
659 {
660 wxEncodingConverter conv;
661
662 if (conv.Init(fontenc, encAlt))
663 {
664 fontenc = encAlt;
665 m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue()));
666 }
667 else
668 {
669 wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
670 wxFontMapper::GetEncodingDescription(fontenc).c_str(),
671 wxFontMapper::GetEncodingDescription(encAlt).c_str());
672 }
673 }
674 else
675 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
676 wxFontMapper::GetEncodingDescription(fontenc).c_str());
677 }
678
679 // and now create the correct font
680 if ( !DoEnumerateFamilies(false, fontenc, true /* silent */) )
681 {
682 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
683 wxFONTWEIGHT_NORMAL, false /* !underlined */,
684 wxEmptyString /* facename */, fontenc);
685 if ( font.Ok() )
686 {
687 DoChangeFont(font);
688 }
689 else
690 {
691 wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
692 wxFontMapper::GetEncodingDescription(fontenc).c_str());
693 }
694 }
695 }
696
697 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
698 {
699 wxMessageBox(wxT("wxWidgets font demo\n")
700 wxT("(c) 1999 Vadim Zeitlin"),
701 wxT("About Font"),
702 wxOK | wxICON_INFORMATION, this);
703 }
704
705 // ----------------------------------------------------------------------------
706 // MyCanvas
707 // ----------------------------------------------------------------------------
708
709 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
710 EVT_PAINT(MyCanvas::OnPaint)
711 END_EVENT_TABLE()
712
713 MyCanvas::MyCanvas( wxWindow *parent )
714 : wxWindow( parent, wxID_ANY ),
715 m_colour(*wxRED), m_font(*wxNORMAL_FONT)
716 {
717 }
718
719 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
720 {
721 wxPaintDC dc(this);
722 PrepareDC(dc);
723
724 // set background
725 dc.SetBackground(wxBrush(wxT("white"), wxSOLID));
726 dc.Clear();
727
728 // one text line height
729 wxCoord hLine = dc.GetCharHeight();
730
731 // the current text origin
732 wxCoord x = 5,
733 y = 5;
734
735 // output the font name/info
736 wxString fontInfo;
737 fontInfo.Printf(wxT("Font size is %d points, family: %s, encoding: %s"),
738 m_font.GetPointSize(),
739 m_font.GetFamilyString().c_str(),
740 wxFontMapper::Get()->
741 GetEncodingDescription(m_font.GetEncoding()).c_str());
742
743 dc.DrawText(fontInfo, x, y);
744 y += hLine;
745
746 fontInfo.Printf(wxT("Style: %s, weight: %s, fixed width: %s"),
747 m_font.GetStyleString().c_str(),
748 m_font.GetWeightString().c_str(),
749 m_font.IsFixedWidth() ? _T("yes") : _T("no"));
750
751 dc.DrawText(fontInfo, x, y);
752 y += hLine;
753
754 if ( m_font.Ok() )
755 {
756 const wxNativeFontInfo *info = m_font.GetNativeFontInfo();
757 if ( info )
758 {
759 wxString fontDesc = m_font.GetNativeFontInfoUserDesc();
760 fontInfo.Printf(wxT("Native font info: %s"), fontDesc.c_str());
761
762 dc.DrawText(fontInfo, x, y);
763 y += hLine;
764 }
765 }
766
767 y += hLine;
768
769 // prepare to draw the font
770 dc.SetFont(m_font);
771 dc.SetTextForeground(m_colour);
772
773 // the size of one cell (Normally biggest char + small margin)
774 long maxCharWidth, maxCharHeight;
775 dc.GetTextExtent(wxT("W"), &maxCharWidth, &maxCharHeight);
776 int w = maxCharWidth + 5,
777 h = maxCharHeight + 4;
778
779
780 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
781 for ( int i = 0; i < 7; i++ )
782 {
783 for ( int j = 0; j < 32; j++ )
784 {
785 wxChar c = (wxChar)(32 * (i + 1) + j);
786
787 long charWidth, charHeight;
788 dc.GetTextExtent(c, &charWidth, &charHeight);
789 dc.DrawText
790 (
791 c,
792 x + w*j + (maxCharWidth - charWidth) / 2 + 1,
793 y + h*i + (maxCharHeight - charHeight) / 2
794 );
795 }
796 }
797
798 // draw the lines between them
799 dc.SetPen(wxPen(wxColour(_T("blue")), 1, wxSOLID));
800 int l;
801
802 // horizontal
803 for ( l = 0; l < 8; l++ )
804 {
805 int yl = y + h*l - 2;
806 dc.DrawLine(x - 2, yl, x + 32*w - 1, yl);
807 }
808
809 // and vertical
810 for ( l = 0; l < 33; l++ )
811 {
812 int xl = x + w*l - 2;
813 dc.DrawLine(xl, y - 2, xl, y + 7*h - 1);
814 }
815 }