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