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