1. moved fontenum.cpp to unix because implementation is common to X and GTK+
[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) 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/fontdlg.h>
28 #include <wx/fontenum.h>
29
30 // ----------------------------------------------------------------------------
31 // private classes
32 // ----------------------------------------------------------------------------
33
34 // Define a new application type, each program should derive a class from wxApp
35 class MyApp : public wxApp
36 {
37 public:
38 // override base class virtuals
39 // ----------------------------
40
41 // this one is called on application startup and is a good place for the app
42 // initialization (doing it here and not in the ctor allows to have an error
43 // return: if OnInit() returns false, the application terminates)
44 virtual bool OnInit();
45 };
46
47 // MyCanvas is a canvas on which we show the font sample
48 class MyCanvas: public wxWindow
49 {
50 public:
51 MyCanvas( wxWindow *parent );
52 ~MyCanvas();
53
54 // accessors for the frame
55 const wxFont& GetTextFont() const { return m_font; }
56 const wxColour& GetColour() const { return m_colour; }
57 void SetTextFont(const wxFont& font) { m_font = font; }
58 void SetColour(const wxColour& colour) { m_colour = colour; }
59
60 // event handlers
61 void OnPaint( wxPaintEvent &event );
62
63 private:
64 wxColour m_colour;
65 wxFont m_font;
66
67 DECLARE_EVENT_TABLE()
68 };
69
70 // Define a new frame type: this is going to be our main frame
71 class MyFrame : public wxFrame
72 {
73 public:
74 // ctor(s)
75 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
76
77 // accessors
78 MyCanvas *GetCanvas() const { return m_canvas; }
79
80 // event handlers (these functions should _not_ be virtual)
81 void OnQuit(wxCommandEvent& event);
82 void OnAbout(wxCommandEvent& event);
83 void OnSelectFont(wxCommandEvent& event);
84 void OnCreateFont(wxCommandEvent& event);
85 void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event))
86 { DoEnumerateFamilies(FALSE); }
87 void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event))
88 { DoEnumerateFamilies(TRUE); }
89 void OnEnumerateEncodings(wxCommandEvent& event);
90
91 protected:
92 void DoEnumerateFamilies(bool fixedWidthOnly);
93
94 MyCanvas *m_canvas;
95
96 private:
97 // any class wishing to process wxWindows events must use this macro
98 DECLARE_EVENT_TABLE()
99 };
100
101 // A custom font dialog which allows to directly edit wxFont proprieties
102 class MyFontDialog : public wxDialog
103 {
104 public:
105 MyFontDialog(MyFrame *frame);
106
107 // event handlers
108 void OnApply(wxCommandEvent& WXUNUSED(event)) { DoApply(); }
109
110 protected:
111 void DoApply();
112
113 MyCanvas *m_canvas;
114
115 private:
116 //DECLARE_EVENT_TABLE() TODO
117 };
118
119 // ----------------------------------------------------------------------------
120 // constants
121 // ----------------------------------------------------------------------------
122
123 // IDs for the controls and the menu commands
124 enum
125 {
126 // menu items
127 Font_Quit = 1,
128 Font_About,
129 Font_Choose = 100,
130 Font_Create,
131 Font_EnumFamilies,
132 Font_EnumFixedFamilies,
133 Font_EnumEncodings,
134 Font_Max
135 };
136
137 // ----------------------------------------------------------------------------
138 // event tables and other macros for wxWindows
139 // ----------------------------------------------------------------------------
140
141 // the event tables connect the wxWindows events with the functions (event
142 // handlers) which process them. It can be also done at run-time, but for the
143 // simple menu events like this the static method is much simpler.
144 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
145 EVT_MENU(Font_Quit, MyFrame::OnQuit)
146 EVT_MENU(Font_About, MyFrame::OnAbout)
147 EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
148 EVT_MENU(Font_Create, MyFrame::OnCreateFont)
149 EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
150 EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
151 EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
152 END_EVENT_TABLE()
153
154 // Create a new application object: this macro will allow wxWindows to create
155 // the application object during program execution (it's better than using a
156 // static object for many reasons) and also declares the accessor function
157 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
158 // not wxApp)
159 IMPLEMENT_APP(MyApp)
160
161 // ============================================================================
162 // implementation
163 // ============================================================================
164
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
168
169 // `Main program' equivalent: the program execution "starts" here
170 bool MyApp::OnInit()
171 {
172 // Create the main application window
173 MyFrame *frame = new MyFrame("Font wxWindows demo",
174 wxPoint(50, 50), wxSize(450, 340));
175
176 // Show it and tell the application that it's our main window
177 frame->Show(TRUE);
178 SetTopWindow(frame);
179
180 // success: wxApp::OnRun() will be called which will enter the main message
181 // loop and the application will run. If we returned FALSE here, the
182 // application would exit immediately.
183 return TRUE;
184 }
185
186 // ----------------------------------------------------------------------------
187 // main frame
188 // ----------------------------------------------------------------------------
189
190 // frame constructor
191 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
192 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
193 {
194 // create a menu bar
195 wxMenu *menuFile = new wxMenu;
196
197 menuFile->Append(Font_About, "&About...\tCtrl-A", "Show about dialog");
198 menuFile->AppendSeparator();
199 menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program");
200
201 wxMenu *menuFont = new wxMenu;
202 menuFont->Append(Font_Choose, "&Select font...\tCtrl-S",
203 "Select a standard font");
204 menuFont->Append(Font_Create, "&Create font...\tCtrl-C",
205 "Create a custom font");
206 menuFont->AppendSeparator();
207 menuFont->Append(Font_EnumFamilies, "Enumerate font &families\tCtrl-F");
208 menuFont->Append(Font_EnumFixedFamilies,
209 "Enumerate f&ixed font families\tCtrl-I");
210 menuFont->Append(Font_EnumEncodings,
211 "Enumerate &encodings\tCtrl-E");
212
213 // now append the freshly created menu to the menu bar...
214 wxMenuBar *menuBar = new wxMenuBar;
215 menuBar->Append(menuFile, "&File");
216 menuBar->Append(menuFont, "F&ont");
217
218 // ... and attach this menu bar to the frame
219 SetMenuBar(menuBar);
220
221 m_canvas = new MyCanvas(this);
222
223 // create a status bar just for fun (by default with 1 pane only)
224 CreateStatusBar(2);
225 SetStatusText("Welcome to wxWindows!");
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)
264 {
265 class MyFontEnumerator : public wxFontEnumerator
266 {
267 public:
268 MyFontEnumerator() { m_n = 0; }
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(fixedWidthOnly);
289
290 wxLogMessage("Enumerating %s font families:\n%s",
291 fixedWidthOnly ? "fixed width" : "all",
292 fontEnumerator.GetText().c_str());
293 }
294
295 void MyFrame::OnCreateFont(wxCommandEvent& WXUNUSED(event))
296 {
297 MyFontDialog dialog(this);
298
299 (void)dialog.ShowModal();
300 }
301
302 void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
303 {
304 wxFontData data;
305 data.SetInitialFont(m_canvas->GetTextFont());
306 data.SetColour(m_canvas->GetColour());
307
308 wxFontDialog dialog(this, &data);
309 if ( dialog.ShowModal() == wxID_OK )
310 {
311 wxFontData retData = dialog.GetFontData();
312 m_canvas->SetTextFont(retData.GetChosenFont());
313 m_canvas->SetColour(retData.GetColour());
314 m_canvas->Refresh();
315 }
316 }
317
318 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
319 {
320 // TRUE is to force the frame to close
321 Close(TRUE);
322 }
323
324 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
325 {
326 wxMessageBox("wxWindows font demo.", "About Font",
327 wxOK | wxICON_INFORMATION, this);
328 }
329
330
331 // ----------------------------------------------------------------------------
332 // MyCanvas
333 // ----------------------------------------------------------------------------
334
335 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
336 EVT_PAINT(MyCanvas::OnPaint)
337 END_EVENT_TABLE()
338
339 MyCanvas::MyCanvas( wxWindow *parent )
340 : wxWindow( parent, -1 )
341 {
342 m_font = *wxNORMAL_FONT;
343 m_colour = *wxRED;
344 }
345
346 MyCanvas::~MyCanvas()
347 {
348 }
349
350 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
351 {
352 wxPaintDC dc(this);
353 PrepareDC(dc);
354
355 // set background
356 dc.SetBackground(wxBrush("white", wxSOLID));
357 dc.Clear();
358
359 // output the font name/info
360 wxString fontInfo;
361 fontInfo.Printf("Font family is '%s', style '%s', weight '%s'",
362 m_font.GetFamilyString().c_str(),
363 m_font.GetStyleString().c_str(),
364 m_font.GetWeightString().c_str());
365
366 dc.DrawText(fontInfo, 5, 5);
367
368 // prepare to draw the font
369 dc.SetFont(m_font);
370 dc.SetTextForeground(m_colour);
371
372 // the size of one cell (char + small margin)
373 int w = dc.GetCharWidth() + 5,
374 h = dc.GetCharHeight() + 4;
375
376 // the origin for our table
377 int x = 5,
378 y = 2*h + 5;
379
380 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
381 for ( int i = 1; i < 8; i++ )
382 {
383 for ( int j = 0; j < 32; j++ )
384 {
385 dc.DrawText(char(32*i + j), x + w*j, y + h*i);
386 }
387 }
388
389 // draw the lines between them
390 dc.SetPen(wxPen(wxColour("blue"), 1, wxSOLID));
391 int l;
392
393 // horizontal
394 y += h;
395 for ( l = 0; l < 8; l++ )
396 {
397 int yl = y + h*l - 2;
398 dc.DrawLine(x - 2, yl, x + 32*w - 2, yl);
399 }
400
401 // and vertical
402 for ( l = 0; l < 33; l++ )
403 {
404 int xl = x + w*l - 2;
405 dc.DrawLine(xl, y, xl, y + 7*h - 2);
406 }
407 }
408
409 // ----------------------------------------------------------------------------
410 // MyFontDialog
411 // ----------------------------------------------------------------------------
412
413 MyFontDialog::MyFontDialog(MyFrame *frame)
414 : wxDialog(frame, -1, wxString("Edit font attributes"))
415 {
416 m_canvas = frame->GetCanvas();
417
418 // create controls
419 wxSize sizeBtn = wxButton::GetDefaultSize();
420
421 // TODO
422
423 // position and size the dialog
424 SetClientSize(4*sizeBtn.x, 10*sizeBtn.y);
425 Centre();
426 }
427
428 void MyFontDialog::DoApply()
429 {
430 wxFont font; //(size, family, style, weight, underlined, face, encoding);
431 if ( !font.Ok() )
432 {
433 wxLogError("Font creation failed.");
434 }
435 else
436 {
437 m_canvas->SetTextFont(font);
438 m_canvas->Refresh();
439 }
440 }