1. added encoding param to wxFontEncoding::EnumFamilies() which allows to get
[wxWidgets.git] / samples / font / font.cpp
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$
8 // Copyright: (c) 1999 Vadim Zeitlin
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
27 #include <wx/choicdlg.h>
28 #include <wx/fontdlg.h>
29 #include <wx/fontenum.h>
30
31 // ----------------------------------------------------------------------------
32 // private classes
33 // ----------------------------------------------------------------------------
34
35 // Define a new application type, each program should derive a class from wxApp
36 class MyApp : public wxApp
37 {
38 public:
39 // override base class virtuals
40 // ----------------------------
41
42 // this one is called on application startup and is a good place for the app
43 // initialization (doing it here and not in the ctor allows to have an error
44 // return: if OnInit() returns false, the application terminates)
45 virtual bool OnInit();
46 };
47
48 // MyCanvas is a canvas on which we show the font sample
49 class MyCanvas: public wxWindow
50 {
51 public:
52 MyCanvas( wxWindow *parent );
53 ~MyCanvas();
54
55 // accessors for the frame
56 const wxFont& GetTextFont() const { return m_font; }
57 const wxColour& GetColour() const { return m_colour; }
58 void SetTextFont(const wxFont& font) { m_font = font; }
59 void SetColour(const wxColour& colour) { m_colour = colour; }
60
61 // event handlers
62 void OnPaint( wxPaintEvent &event );
63
64 private:
65 wxColour m_colour;
66 wxFont m_font;
67
68 DECLARE_EVENT_TABLE()
69 };
70
71 // Define a new frame type: this is going to be our main frame
72 class MyFrame : public wxFrame
73 {
74 public:
75 // ctor(s)
76 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
77
78 // accessors
79 MyCanvas *GetCanvas() const { return m_canvas; }
80
81 // event handlers (these functions should _not_ be virtual)
82 void OnQuit(wxCommandEvent& event);
83 void OnAbout(wxCommandEvent& event);
84 void OnSelectFont(wxCommandEvent& event);
85 void OnEnumerateFamiliesForEncoding(wxCommandEvent& event);
86 void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event))
87 { DoEnumerateFamilies(FALSE); }
88 void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event))
89 { DoEnumerateFamilies(TRUE); }
90 void OnEnumerateEncodings(wxCommandEvent& event);
91
92 void OnSize(wxSizeEvent& event);
93
94 protected:
95 void DoEnumerateFamilies(bool fixedWidthOnly,
96 wxFontEncoding encoding = wxFONTENCODING_SYSTEM);
97
98 void Resize(const wxSize& size, const wxFont& font = wxNullFont);
99
100 wxTextCtrl *m_textctrl;
101 MyCanvas *m_canvas;
102
103 private:
104 // any class wishing to process wxWindows events must use this macro
105 DECLARE_EVENT_TABLE()
106 };
107
108 // ----------------------------------------------------------------------------
109 // constants
110 // ----------------------------------------------------------------------------
111
112 // IDs for the controls and the menu commands
113 enum
114 {
115 // menu items
116 Font_Quit = 1,
117 Font_About,
118 Font_Choose = 100,
119 Font_EnumFamiliesForEncoding,
120 Font_EnumFamilies,
121 Font_EnumFixedFamilies,
122 Font_EnumEncodings,
123 Font_Max
124 };
125
126 // ----------------------------------------------------------------------------
127 // event tables and other macros for wxWindows
128 // ----------------------------------------------------------------------------
129
130 // the event tables connect the wxWindows events with the functions (event
131 // handlers) which process them. It can be also done at run-time, but for the
132 // simple menu events like this the static method is much simpler.
133 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
134 EVT_MENU(Font_Quit, MyFrame::OnQuit)
135 EVT_MENU(Font_About, MyFrame::OnAbout)
136 EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
137 EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding)
138 EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
139 EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
140 EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
141
142 EVT_SIZE(MyFrame::OnSize)
143 END_EVENT_TABLE()
144
145 // Create a new application object: this macro will allow wxWindows to create
146 // the application object during program execution (it's better than using a
147 // static object for many reasons) and also declares the accessor function
148 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
149 // not wxApp)
150 IMPLEMENT_APP(MyApp)
151
152 // ============================================================================
153 // implementation
154 // ============================================================================
155
156 // ----------------------------------------------------------------------------
157 // the application class
158 // ----------------------------------------------------------------------------
159
160 // `Main program' equivalent: the program execution "starts" here
161 bool MyApp::OnInit()
162 {
163 // Create the main application window
164 MyFrame *frame = new MyFrame("Font wxWindows demo",
165 wxPoint(50, 50), wxSize(450, 340));
166
167 // Show it and tell the application that it's our main window
168 frame->Show(TRUE);
169 SetTopWindow(frame);
170
171 // success: wxApp::OnRun() will be called which will enter the main message
172 // loop and the application will run. If we returned FALSE here, the
173 // application would exit immediately.
174 return TRUE;
175 }
176
177 // ----------------------------------------------------------------------------
178 // main frame
179 // ----------------------------------------------------------------------------
180
181 // frame constructor
182 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
183 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
184 {
185 // create a menu bar
186 wxMenu *menuFile = new wxMenu;
187
188 menuFile->Append(Font_About, "&About...\tCtrl-A", "Show about dialog");
189 menuFile->AppendSeparator();
190 menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program");
191
192 wxMenu *menuFont = new wxMenu;
193 menuFont->Append(Font_Choose, "&Select font...\tCtrl-S",
194 "Select a standard font");
195 menuFont->AppendSeparator();
196 menuFont->Append(Font_EnumFamilies, "Enumerate font &families\tCtrl-F");
197 menuFont->Append(Font_EnumFixedFamilies,
198 "Enumerate f&ixed font families\tCtrl-I");
199 menuFont->Append(Font_EnumEncodings,
200 "Enumerate &encodings\tCtrl-E");
201 menuFont->Append(Font_EnumFamiliesForEncoding,
202 "Find font for en&coding...\tCtrl-C",
203 "Find font families for given encoding");
204
205 // now append the freshly created menu to the menu bar...
206 wxMenuBar *menuBar = new wxMenuBar;
207 menuBar->Append(menuFile, "&File");
208 menuBar->Append(menuFont, "F&ont");
209
210 // ... and attach this menu bar to the frame
211 SetMenuBar(menuBar);
212
213 m_textctrl = new wxTextCtrl(this, -1,
214 "Paste text here to see how it looks\n"
215 "like in the given font",
216 wxDefaultPosition, wxDefaultSize,
217 wxTE_MULTILINE);
218
219 m_canvas = new MyCanvas(this);
220
221 // create a status bar just for fun (by default with 1 pane only)
222 CreateStatusBar(2);
223 SetStatusText("Welcome to wxWindows!");
224 }
225
226
227 // event handlers
228 void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
229 {
230 class MyEncodingEnumerator : public wxFontEnumerator
231 {
232 public:
233 MyEncodingEnumerator() { m_n = 0; }
234
235 const wxString& GetText() const { return m_text; }
236
237 protected:
238 virtual bool OnFontEncoding(const wxString& family,
239 const wxString& encoding)
240 {
241 wxString text;
242 text.Printf("Encoding %d: %s (available in family '%s')\n",
243 ++m_n, encoding.c_str(), family.c_str());
244 m_text += text;
245
246 return TRUE;
247 }
248
249 private:
250 size_t m_n;
251
252 wxString m_text;
253 } fontEnumerator;
254
255 fontEnumerator.EnumerateEncodings();
256
257 wxLogMessage("Enumerating all available encodings:\n%s",
258 fontEnumerator.GetText().c_str());
259 }
260
261 void MyFrame::DoEnumerateFamilies(bool fixedWidthOnly, wxFontEncoding encoding)
262 {
263 class MyFontEnumerator : public wxFontEnumerator
264 {
265 public:
266 MyFontEnumerator() { m_n = 0; }
267
268 bool GotAny() const { return m_n; }
269
270 const wxString& GetText() const { return m_text; }
271
272 protected:
273 virtual bool OnFontFamily(const wxString& family)
274 {
275 wxString text;
276 text.Printf("Font family %d: %s\n", ++m_n, family.c_str());
277 m_text += text;
278
279 return TRUE;
280 }
281
282 private:
283 size_t m_n;
284
285 wxString m_text;
286 } fontEnumerator;
287
288 fontEnumerator.EnumerateFamilies(encoding, fixedWidthOnly);
289
290 if ( fontEnumerator.GotAny() )
291 {
292 wxLogMessage("Enumerating %s font families:\n%s",
293 fixedWidthOnly ? "fixed width" : "all",
294 fontEnumerator.GetText().c_str());
295 }
296 else
297 {
298 wxLogWarning("No such fonts found.");
299 }
300 }
301
302 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
303 {
304 static wxFontEncoding encodings[] =
305 {
306 wxFONTENCODING_ISO8859_1,
307 wxFONTENCODING_ISO8859_2,
308 wxFONTENCODING_ISO8859_5,
309 wxFONTENCODING_ISO8859_7,
310 wxFONTENCODING_ISO8859_15,
311 wxFONTENCODING_KOI8,
312 wxFONTENCODING_CP1250,
313 wxFONTENCODING_CP1251,
314 wxFONTENCODING_CP1252,
315 };
316
317 static const char *encodingNames[] =
318 {
319 "West European (Latin 1)",
320 "Central European (Latin 2)",
321 "Cyrillic (Latin 5)",
322 "Greek (Latin 7)",
323 "West European new (Latin 0)",
324 "KOI8-R",
325 "Windows Latin 2",
326 "Windows Cyrillic",
327 "Windows Latin 1",
328 };
329
330 int n = wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
331 WXSIZEOF(encodingNames), encodingNames,
332 this);
333
334 if ( n != -1 )
335 {
336 DoEnumerateFamilies(FALSE, encodings[n]);
337 }
338 }
339
340 void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
341 {
342 wxFontData data;
343 data.SetInitialFont(m_canvas->GetTextFont());
344 data.SetColour(m_canvas->GetColour());
345
346 wxFontDialog dialog(this, &data);
347 if ( dialog.ShowModal() == wxID_OK )
348 {
349 wxFontData retData = dialog.GetFontData();
350 wxFont font = retData.GetChosenFont();
351 wxColour colour = retData.GetColour();
352
353 Resize(GetSize(), font);
354
355 m_canvas->SetTextFont(font);
356 m_canvas->SetColour(colour);
357 m_canvas->Refresh();
358
359 m_textctrl->SetFont(font);
360 m_textctrl->SetForegroundColour(colour);
361 }
362 }
363
364 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
365 {
366 // TRUE is to force the frame to close
367 Close(TRUE);
368 }
369
370 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
371 {
372 wxMessageBox("wxWindows font demo\n"
373 "(c) 1999 Vadim Zeitlin",
374 "About Font",
375 wxOK | wxICON_INFORMATION, this);
376 }
377
378 void MyFrame::OnSize(wxSizeEvent& event)
379 {
380 wxSize size = event.GetSize();
381
382 Resize(size);
383 }
384
385 void MyFrame::Resize(const wxSize& size, const wxFont& font)
386 {
387 wxCoord h;
388 if ( font.Ok() )
389 {
390 wxClientDC dc(this);
391 dc.SetFont(font);
392
393 h = 4*dc.GetCharHeight() + 4;
394 }
395 else
396 {
397 h = m_textctrl->GetSize().y;
398 }
399
400 m_textctrl->SetSize(0, 0, size.x, h);
401 m_canvas->SetSize(0, h, size.x, size.y - h);
402 }
403
404 // ----------------------------------------------------------------------------
405 // MyCanvas
406 // ----------------------------------------------------------------------------
407
408 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
409 EVT_PAINT(MyCanvas::OnPaint)
410 END_EVENT_TABLE()
411
412 MyCanvas::MyCanvas( wxWindow *parent )
413 : wxWindow( parent, -1 )
414 {
415 m_font = *wxNORMAL_FONT;
416 m_colour = *wxRED;
417 }
418
419 MyCanvas::~MyCanvas()
420 {
421 }
422
423 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
424 {
425 wxPaintDC dc(this);
426 PrepareDC(dc);
427
428 // set background
429 dc.SetBackground(wxBrush("white", wxSOLID));
430 dc.Clear();
431
432 // output the font name/info
433 wxString fontInfo;
434 fontInfo.Printf("Font family is '%s', style '%s', weight '%s'",
435 m_font.GetFamilyString().c_str(),
436 m_font.GetStyleString().c_str(),
437 m_font.GetWeightString().c_str());
438
439 dc.DrawText(fontInfo, 5, 5);
440
441 // prepare to draw the font
442 dc.SetFont(m_font);
443 dc.SetTextForeground(m_colour);
444
445 // the size of one cell (char + small margin)
446 int w = dc.GetCharWidth() + 5,
447 h = dc.GetCharHeight() + 4;
448
449 // the origin for our table
450 int x = 5,
451 y = 2*h;
452
453 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
454 for ( int i = 1; i < 8; i++ )
455 {
456 for ( int j = 0; j < 32; j++ )
457 {
458 dc.DrawText(char(32*i + j), x + w*j, y + h*i);
459 }
460 }
461
462 // draw the lines between them
463 dc.SetPen(wxPen(wxColour("blue"), 1, wxSOLID));
464 int l;
465
466 // horizontal
467 y += h;
468 for ( l = 0; l < 8; l++ )
469 {
470 int yl = y + h*l - 2;
471 dc.DrawLine(x - 2, yl, x + 32*w - 2, yl);
472 }
473
474 // and vertical
475 for ( l = 0; l < 33; l++ )
476 {
477 int xl = x + w*l - 2;
478 dc.DrawLine(xl, y, xl, y + 7*h - 2);
479 }
480 }