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