]> git.saurik.com Git - wxWidgets.git/blame - samples/font/font.cpp
New SWIG generated sources
[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".
13#include <wx/wxprec.h>
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
22 #include <wx/wx.h>
23
24 #include <wx/log.h>
25#endif
26
36f210c8 27#include <wx/choicdlg.h>
1ccd74da 28#include <wx/fontdlg.h>
ddc8c2e3 29#include <wx/fontenum.h>
3c1866e8
VZ
30#include <wx/fontmap.h>
31#include <wx/textfile.h>
1ccd74da
VZ
32
33// ----------------------------------------------------------------------------
34// private classes
35// ----------------------------------------------------------------------------
36
37// Define a new application type, each program should derive a class from wxApp
38class MyApp : public wxApp
39{
40public:
41 // override base class virtuals
42 // ----------------------------
43
44 // this one is called on application startup and is a good place for the app
45 // initialization (doing it here and not in the ctor allows to have an error
46 // return: if OnInit() returns false, the application terminates)
47 virtual bool OnInit();
48};
49
50// MyCanvas is a canvas on which we show the font sample
51class MyCanvas: public wxWindow
52{
53public:
54 MyCanvas( wxWindow *parent );
55 ~MyCanvas();
56
57 // accessors for the frame
58 const wxFont& GetTextFont() const { return m_font; }
59 const wxColour& GetColour() const { return m_colour; }
60 void SetTextFont(const wxFont& font) { m_font = font; }
61 void SetColour(const wxColour& colour) { m_colour = colour; }
62
63 // event handlers
64 void OnPaint( wxPaintEvent &event );
65
66private:
67 wxColour m_colour;
68 wxFont m_font;
69
70 DECLARE_EVENT_TABLE()
71};
72
73// Define a new frame type: this is going to be our main frame
74class MyFrame : public wxFrame
75{
76public:
77 // ctor(s)
78 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
79
80 // accessors
81 MyCanvas *GetCanvas() const { return m_canvas; }
82
83 // event handlers (these functions should _not_ be virtual)
84 void OnQuit(wxCommandEvent& event);
85 void OnAbout(wxCommandEvent& event);
3c1866e8 86 void OnViewMsg(wxCommandEvent& event);
1ccd74da 87 void OnSelectFont(wxCommandEvent& event);
36f210c8 88 void OnEnumerateFamiliesForEncoding(wxCommandEvent& event);
ddc8c2e3
VZ
89 void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event))
90 { DoEnumerateFamilies(FALSE); }
91 void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event))
92 { DoEnumerateFamilies(TRUE); }
d111a89a 93 void OnEnumerateEncodings(wxCommandEvent& event);
1ccd74da 94
36f210c8
VZ
95 void OnSize(wxSizeEvent& event);
96
1ccd74da 97protected:
7beba2fc
VZ
98 bool DoEnumerateFamilies(bool fixedWidthOnly,
99 wxFontEncoding encoding = wxFONTENCODING_SYSTEM,
100 bool silent = FALSE);
36f210c8 101
a1d58ddc
VZ
102 void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour);
103
36f210c8 104 void Resize(const wxSize& size, const wxFont& font = wxNullFont);
ddc8c2e3 105
36f210c8
VZ
106 wxTextCtrl *m_textctrl;
107 MyCanvas *m_canvas;
1ccd74da
VZ
108
109private:
110 // any class wishing to process wxWindows events must use this macro
111 DECLARE_EVENT_TABLE()
112};
113
1ccd74da
VZ
114// ----------------------------------------------------------------------------
115// constants
116// ----------------------------------------------------------------------------
117
118// IDs for the controls and the menu commands
119enum
120{
121 // menu items
122 Font_Quit = 1,
123 Font_About,
3c1866e8 124 Font_ViewMsg,
1ccd74da 125 Font_Choose = 100,
36f210c8 126 Font_EnumFamiliesForEncoding,
ddc8c2e3
VZ
127 Font_EnumFamilies,
128 Font_EnumFixedFamilies,
d111a89a 129 Font_EnumEncodings,
ddc8c2e3 130 Font_Max
1ccd74da
VZ
131};
132
133// ----------------------------------------------------------------------------
134// event tables and other macros for wxWindows
135// ----------------------------------------------------------------------------
136
137// the event tables connect the wxWindows events with the functions (event
138// handlers) which process them. It can be also done at run-time, but for the
139// simple menu events like this the static method is much simpler.
140BEGIN_EVENT_TABLE(MyFrame, wxFrame)
141 EVT_MENU(Font_Quit, MyFrame::OnQuit)
142 EVT_MENU(Font_About, MyFrame::OnAbout)
3c1866e8 143 EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg)
1ccd74da 144 EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
36f210c8 145 EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding)
ddc8c2e3
VZ
146 EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
147 EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
d111a89a 148 EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
36f210c8
VZ
149
150 EVT_SIZE(MyFrame::OnSize)
1ccd74da
VZ
151END_EVENT_TABLE()
152
153// Create a new application object: this macro will allow wxWindows to create
154// the application object during program execution (it's better than using a
155// static object for many reasons) and also declares the accessor function
156// wxGetApp() which will return the reference of the right type (i.e. MyApp and
157// not wxApp)
158IMPLEMENT_APP(MyApp)
159
160// ============================================================================
161// implementation
162// ============================================================================
163
164// ----------------------------------------------------------------------------
165// the application class
166// ----------------------------------------------------------------------------
167
168// `Main program' equivalent: the program execution "starts" here
169bool MyApp::OnInit()
170{
171 // Create the main application window
ddc8c2e3 172 MyFrame *frame = new MyFrame("Font wxWindows demo",
1ccd74da
VZ
173 wxPoint(50, 50), wxSize(450, 340));
174
175 // Show it and tell the application that it's our main window
176 frame->Show(TRUE);
177 SetTopWindow(frame);
178
179 // success: wxApp::OnRun() will be called which will enter the main message
180 // loop and the application will run. If we returned FALSE here, the
181 // application would exit immediately.
182 return TRUE;
183}
184
185// ----------------------------------------------------------------------------
186// main frame
187// ----------------------------------------------------------------------------
188
189// frame constructor
190MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
a1d58ddc 191 : wxFrame((wxFrame *)NULL, -1, title, pos, size), m_textctrl(NULL)
1ccd74da 192{
1ccd74da
VZ
193 // create a menu bar
194 wxMenu *menuFile = new wxMenu;
195
3c1866e8
VZ
196 menuFile->Append(Font_ViewMsg, "&View...\tCtrl-V",
197 "View an email message file");
198 menuFile->AppendSeparator();
1ccd74da
VZ
199 menuFile->Append(Font_About, "&About...\tCtrl-A", "Show about dialog");
200 menuFile->AppendSeparator();
201 menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program");
202
203 wxMenu *menuFont = new wxMenu;
ddc8c2e3 204 menuFont->Append(Font_Choose, "&Select font...\tCtrl-S",
1ccd74da 205 "Select a standard font");
ddc8c2e3 206 menuFont->AppendSeparator();
d111a89a 207 menuFont->Append(Font_EnumFamilies, "Enumerate font &families\tCtrl-F");
ddc8c2e3 208 menuFont->Append(Font_EnumFixedFamilies,
d111a89a
VZ
209 "Enumerate f&ixed font families\tCtrl-I");
210 menuFont->Append(Font_EnumEncodings,
211 "Enumerate &encodings\tCtrl-E");
36f210c8
VZ
212 menuFont->Append(Font_EnumFamiliesForEncoding,
213 "Find font for en&coding...\tCtrl-C",
214 "Find font families for given encoding");
1ccd74da
VZ
215
216 // now append the freshly created menu to the menu bar...
217 wxMenuBar *menuBar = new wxMenuBar;
218 menuBar->Append(menuFile, "&File");
219 menuBar->Append(menuFont, "F&ont");
220
221 // ... and attach this menu bar to the frame
222 SetMenuBar(menuBar);
223
36f210c8
VZ
224 m_textctrl = new wxTextCtrl(this, -1,
225 "Paste text here to see how it looks\n"
226 "like in the given font",
227 wxDefaultPosition, wxDefaultSize,
228 wxTE_MULTILINE);
229
1ccd74da
VZ
230 m_canvas = new MyCanvas(this);
231
232 // create a status bar just for fun (by default with 1 pane only)
a1d58ddc
VZ
233 CreateStatusBar();
234 SetStatusText("Welcome to wxWindows font demo!");
1ccd74da
VZ
235}
236
237
238// event handlers
d111a89a
VZ
239void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
240{
241 class MyEncodingEnumerator : public wxFontEnumerator
242 {
243 public:
244 MyEncodingEnumerator() { m_n = 0; }
245
246 const wxString& GetText() const { return m_text; }
247
248 protected:
3c1866e8 249 virtual bool OnFontEncoding(const wxString& facename,
d111a89a
VZ
250 const wxString& encoding)
251 {
252 wxString text;
3c1866e8
VZ
253 text.Printf("Encoding %d: %s (available in facename '%s')\n",
254 ++m_n, encoding.c_str(), facename.c_str());
d111a89a
VZ
255 m_text += text;
256
257 return TRUE;
258 }
259
260 private:
261 size_t m_n;
262
263 wxString m_text;
264 } fontEnumerator;
265
266 fontEnumerator.EnumerateEncodings();
267
268 wxLogMessage("Enumerating all available encodings:\n%s",
269 fontEnumerator.GetText().c_str());
270}
1ccd74da 271
7beba2fc
VZ
272bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
273 wxFontEncoding encoding,
274 bool silent)
ddc8c2e3
VZ
275{
276 class MyFontEnumerator : public wxFontEnumerator
277 {
278 public:
a1d58ddc 279 bool GotAny() const { return !m_facenames.IsEmpty(); }
36f210c8 280
a1d58ddc 281 const wxArrayString& GetFacenames() const { return m_facenames; }
d111a89a 282
ddc8c2e3 283 protected:
3c1866e8 284 virtual bool OnFacename(const wxString& facename)
ddc8c2e3 285 {
3c1866e8 286 m_facenames.Add(facename);
ddc8c2e3
VZ
287
288 return TRUE;
289 }
290
291 private:
a1d58ddc 292 wxArrayString m_facenames;
ddc8c2e3
VZ
293 } fontEnumerator;
294
3c1866e8 295 fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
d111a89a 296
36f210c8
VZ
297 if ( fontEnumerator.GotAny() )
298 {
ea9144a3
VZ
299 int nFacenames = fontEnumerator.GetFacenames().GetCount();
300 if ( !silent )
301 {
302 wxLogStatus(this, "Found %d %sfonts",
303 nFacenames, fixedWidthOnly ? "fixed width " : "");
304 }
a1d58ddc 305
ea9144a3 306 wxString facename;
7beba2fc 307 if ( silent )
ea9144a3
VZ
308 {
309 // choose the first
310 facename = fontEnumerator.GetFacenames().Item(0);
311 }
7beba2fc 312 else
ea9144a3
VZ
313 {
314 // let the user choose
315 wxString *facenames = new wxString[nFacenames];
316 int n;
317 for ( n = 0; n < nFacenames; n++ )
318 facenames[n] = fontEnumerator.GetFacenames().Item(n);
319
7beba2fc
VZ
320 n = wxGetSingleChoiceIndex("Choose a facename", "Font demo",
321 nFacenames, facenames, this);
ea9144a3
VZ
322
323 if ( n != -1 )
324 facename = facenames[n];
325
326 delete [] facenames;
327 }
328
329 if ( !facename.IsEmpty() )
a1d58ddc 330 {
ea9144a3
VZ
331 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
332 wxFONTWEIGHT_NORMAL, FALSE, facename, encoding);
a1d58ddc
VZ
333
334 DoChangeFont(font);
335 }
336
7beba2fc 337 return TRUE;
36f210c8 338 }
7beba2fc 339 else if ( !silent )
36f210c8
VZ
340 {
341 wxLogWarning("No such fonts found.");
342 }
7beba2fc
VZ
343
344 return FALSE;
ddc8c2e3
VZ
345}
346
36f210c8 347void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
1ccd74da 348{
36f210c8
VZ
349 static wxFontEncoding encodings[] =
350 {
351 wxFONTENCODING_ISO8859_1,
352 wxFONTENCODING_ISO8859_2,
353 wxFONTENCODING_ISO8859_5,
354 wxFONTENCODING_ISO8859_7,
355 wxFONTENCODING_ISO8859_15,
356 wxFONTENCODING_KOI8,
357 wxFONTENCODING_CP1250,
358 wxFONTENCODING_CP1251,
359 wxFONTENCODING_CP1252,
360 };
361
362 static const char *encodingNames[] =
363 {
364 "West European (Latin 1)",
365 "Central European (Latin 2)",
366 "Cyrillic (Latin 5)",
367 "Greek (Latin 7)",
368 "West European new (Latin 0)",
369 "KOI8-R",
370 "Windows Latin 2",
371 "Windows Cyrillic",
372 "Windows Latin 1",
373 };
374
375 int n = wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
a1d58ddc
VZ
376 WXSIZEOF(encodingNames),
377 (char **)encodingNames,
36f210c8
VZ
378 this);
379
380 if ( n != -1 )
381 {
382 DoEnumerateFamilies(FALSE, encodings[n]);
383 }
1ccd74da
VZ
384}
385
a1d58ddc
VZ
386void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col)
387{
388 Resize(GetSize(), font);
389
390 m_canvas->SetTextFont(font);
391 if ( col.Ok() )
392 m_canvas->SetColour(col);
393 m_canvas->Refresh();
394
395 m_textctrl->SetFont(font);
396 if ( col.Ok() )
397 m_textctrl->SetForegroundColour(col);
398}
399
1ccd74da
VZ
400void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
401{
402 wxFontData data;
403 data.SetInitialFont(m_canvas->GetTextFont());
404 data.SetColour(m_canvas->GetColour());
405
406 wxFontDialog dialog(this, &data);
407 if ( dialog.ShowModal() == wxID_OK )
408 {
409 wxFontData retData = dialog.GetFontData();
36f210c8
VZ
410 wxFont font = retData.GetChosenFont();
411 wxColour colour = retData.GetColour();
412
a1d58ddc 413 DoChangeFont(font, colour);
1ccd74da
VZ
414 }
415}
416
417void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
418{
419 // TRUE is to force the frame to close
420 Close(TRUE);
421}
422
3c1866e8
VZ
423void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
424{
425 // first, choose the file
426 static wxString s_dir, s_file;
427 wxFileDialog dialog(this, "Open an email message file",
428 s_dir, s_file);
429 if ( dialog.ShowModal() != wxID_OK )
430 return;
431
432 // save for the next time
433 s_dir = dialog.GetDirectory();
434 s_file = dialog.GetFilename();
435
436 wxString filename = dialog.GetPath();
437
438 // load it and search for Content-Type header
439 wxTextFile file(filename);
440 if ( !file.Open() )
441 return;
442
443 wxString charset;
444
445 static const char *prefix = "Content-Type: text/plain; charset=";
446 const size_t len = strlen(prefix);
447
448 size_t n, count = file.GetLineCount();
449 for ( n = 0; n < count; n++ )
450 {
451 wxString line = file[n];
452
453 if ( !line )
454 {
455 // if it is an email message, headers are over, no need to parse
456 // all the file
457 break;
458 }
459
460 if ( line.Left(len) == prefix )
461 {
462 // found!
463 const char *pc = line.c_str() + len;
464 if ( *pc == '"' )
465 pc++;
466
467 while ( *pc && *pc != '"' )
468 {
469 charset += *pc++;
470 }
471
472 break;
473 }
474 }
475
476 if ( !charset )
477 {
478 wxLogError("The file '%s' doesn't contain charset information.",
479 filename.c_str());
480
481 return;
482 }
483
484 // ok, now get the corresponding encoding
7beba2fc 485 wxFontEncoding fontenc = wxTheFontMapper->CharsetToEncoding(charset);
3c1866e8
VZ
486 if ( fontenc == wxFONTENCODING_SYSTEM )
487 {
488 wxLogError("Charset '%s' is unsupported.", charset.c_str());
489 return;
490 }
491
492 // and now create the correct font
7beba2fc
VZ
493 if ( !DoEnumerateFamilies(FALSE, fontenc, TRUE /* silent */) )
494 {
ea9144a3 495 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
7beba2fc
VZ
496 wxFONTWEIGHT_NORMAL, FALSE /* !underlined */,
497 wxEmptyString /* facename */, fontenc);
498 if ( font.Ok() )
499 {
500 DoChangeFont(font);
501 }
502 else
503 {
504 wxLogWarning("No fonts for encoding '%s' on this system.",
505 wxFontMapper::GetEncodingDescription(fontenc).c_str());
506 }
507 }
3c1866e8 508
7beba2fc 509 m_textctrl->LoadFile(filename);
3c1866e8
VZ
510}
511
1ccd74da
VZ
512void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
513{
36f210c8
VZ
514 wxMessageBox("wxWindows font demo\n"
515 "(c) 1999 Vadim Zeitlin",
516 "About Font",
1ccd74da
VZ
517 wxOK | wxICON_INFORMATION, this);
518}
519
36f210c8
VZ
520void MyFrame::OnSize(wxSizeEvent& event)
521{
522 wxSize size = event.GetSize();
523
524 Resize(size);
717a57c2
VZ
525
526 event.Skip();
36f210c8
VZ
527}
528
529void MyFrame::Resize(const wxSize& size, const wxFont& font)
530{
a1d58ddc
VZ
531 if ( !m_textctrl )
532 return;
533
36f210c8
VZ
534 wxCoord h;
535 if ( font.Ok() )
536 {
537 wxClientDC dc(this);
538 dc.SetFont(font);
539
3c1866e8 540 h = 10*(dc.GetCharHeight() + 1);
36f210c8
VZ
541 }
542 else
543 {
544 h = m_textctrl->GetSize().y;
545 }
546
547 m_textctrl->SetSize(0, 0, size.x, h);
548 m_canvas->SetSize(0, h, size.x, size.y - h);
549}
1ccd74da
VZ
550
551// ----------------------------------------------------------------------------
552// MyCanvas
553// ----------------------------------------------------------------------------
554
555BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
556 EVT_PAINT(MyCanvas::OnPaint)
557END_EVENT_TABLE()
558
559MyCanvas::MyCanvas( wxWindow *parent )
11c7d5b6
VZ
560 : wxWindow( parent, -1 ),
561 m_colour(*wxRED), m_font(*wxNORMAL_FONT)
1ccd74da 562{
1ccd74da
VZ
563}
564
565MyCanvas::~MyCanvas()
566{
567}
568
569void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
570{
571 wxPaintDC dc(this);
572 PrepareDC(dc);
573
574 // set background
575 dc.SetBackground(wxBrush("white", wxSOLID));
576 dc.Clear();
577
578 // output the font name/info
579 wxString fontInfo;
580 fontInfo.Printf("Font family is '%s', style '%s', weight '%s'",
581 m_font.GetFamilyString().c_str(),
582 m_font.GetStyleString().c_str(),
583 m_font.GetWeightString().c_str());
584
585 dc.DrawText(fontInfo, 5, 5);
586
587 // prepare to draw the font
588 dc.SetFont(m_font);
589 dc.SetTextForeground(m_colour);
590
591 // the size of one cell (char + small margin)
592 int w = dc.GetCharWidth() + 5,
593 h = dc.GetCharHeight() + 4;
594
595 // the origin for our table
596 int x = 5,
36f210c8 597 y = 2*h;
1ccd74da
VZ
598
599 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
600 for ( int i = 1; i < 8; i++ )
601 {
602 for ( int j = 0; j < 32; j++ )
603 {
604 dc.DrawText(char(32*i + j), x + w*j, y + h*i);
605 }
606 }
607
608 // draw the lines between them
609 dc.SetPen(wxPen(wxColour("blue"), 1, wxSOLID));
610 int l;
611
612 // horizontal
613 y += h;
614 for ( l = 0; l < 8; l++ )
615 {
616 int yl = y + h*l - 2;
617 dc.DrawLine(x - 2, yl, x + 32*w - 2, yl);
618 }
619
620 // and vertical
621 for ( l = 0; l < 33; l++ )
622 {
623 int xl = x + w*l - 2;
624 dc.DrawLine(xl, y, xl, y + 7*h - 2);
625 }
626}