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