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