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