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