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