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