small font demo
[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
29 // ----------------------------------------------------------------------------
30 // private classes
31 // ----------------------------------------------------------------------------
32
33 // Define a new application type, each program should derive a class from wxApp
34 class MyApp : public wxApp
35 {
36 public:
37 // override base class virtuals
38 // ----------------------------
39
40 // this one is called on application startup and is a good place for the app
41 // initialization (doing it here and not in the ctor allows to have an error
42 // return: if OnInit() returns false, the application terminates)
43 virtual bool OnInit();
44 };
45
46 // MyCanvas is a canvas on which we show the font sample
47 class MyCanvas: public wxWindow
48 {
49 public:
50 MyCanvas( wxWindow *parent );
51 ~MyCanvas();
52
53 // accessors for the frame
54 const wxFont& GetTextFont() const { return m_font; }
55 const wxColour& GetColour() const { return m_colour; }
56 void SetTextFont(const wxFont& font) { m_font = font; }
57 void SetColour(const wxColour& colour) { m_colour = colour; }
58
59 // event handlers
60 void OnPaint( wxPaintEvent &event );
61
62 private:
63 wxColour m_colour;
64 wxFont m_font;
65
66 DECLARE_EVENT_TABLE()
67 };
68
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame : public wxFrame
71 {
72 public:
73 // ctor(s)
74 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
75
76 // accessors
77 MyCanvas *GetCanvas() const { return m_canvas; }
78
79 // event handlers (these functions should _not_ be virtual)
80 void OnQuit(wxCommandEvent& event);
81 void OnAbout(wxCommandEvent& event);
82 void OnSelectFont(wxCommandEvent& event);
83 void OnCreateFont(wxCommandEvent& event);
84
85 protected:
86 MyCanvas *m_canvas;
87
88 private:
89 // any class wishing to process wxWindows events must use this macro
90 DECLARE_EVENT_TABLE()
91 };
92
93 // A custom font dialog which allows to directly edit wxFont proprieties
94 class MyFontDialog : public wxDialog
95 {
96 public:
97 MyFontDialog(MyFrame *frame);
98
99 // event handlers
100 void OnApply(wxCommandEvent& WXUNUSED(event)) { DoApply(); }
101
102 protected:
103 void DoApply();
104
105 MyCanvas *m_canvas;
106
107 private:
108 //DECLARE_EVENT_TABLE() TODO
109 };
110
111 // ----------------------------------------------------------------------------
112 // constants
113 // ----------------------------------------------------------------------------
114
115 // IDs for the controls and the menu commands
116 enum
117 {
118 // menu items
119 Font_Quit = 1,
120 Font_About,
121 Font_Choose = 100,
122 Font_Create
123 };
124
125 // ----------------------------------------------------------------------------
126 // event tables and other macros for wxWindows
127 // ----------------------------------------------------------------------------
128
129 // the event tables connect the wxWindows events with the functions (event
130 // handlers) which process them. It can be also done at run-time, but for the
131 // simple menu events like this the static method is much simpler.
132 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
133 EVT_MENU(Font_Quit, MyFrame::OnQuit)
134 EVT_MENU(Font_About, MyFrame::OnAbout)
135 EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
136 EVT_MENU(Font_Create, MyFrame::OnCreateFont)
137 END_EVENT_TABLE()
138
139 // Create a new application object: this macro will allow wxWindows to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also declares the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // not wxApp)
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // `Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 // Create the main application window
158 MyFrame *frame = new MyFrame("Minimal wxWindows App",
159 wxPoint(50, 50), wxSize(450, 340));
160
161 // Show it and tell the application that it's our main window
162 frame->Show(TRUE);
163 SetTopWindow(frame);
164
165 // success: wxApp::OnRun() will be called which will enter the main message
166 // loop and the application will run. If we returned FALSE here, the
167 // application would exit immediately.
168 return TRUE;
169 }
170
171 // ----------------------------------------------------------------------------
172 // main frame
173 // ----------------------------------------------------------------------------
174
175 // frame constructor
176 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
177 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
178 {
179 // set the frame icon
180 SetIcon(wxICON(mondrian));
181
182 // create a menu bar
183 wxMenu *menuFile = new wxMenu;
184
185 menuFile->Append(Font_About, "&About...\tCtrl-A", "Show about dialog");
186 menuFile->AppendSeparator();
187 menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program");
188
189 wxMenu *menuFont = new wxMenu;
190 menuFont->Append(Font_Choose, "&Select font...\tCtrl-F",
191 "Select a standard font");
192 menuFont->Append(Font_Create, "&Create font...\tCtrl-C",
193 "Create a custom font");
194
195 // now append the freshly created menu to the menu bar...
196 wxMenuBar *menuBar = new wxMenuBar;
197 menuBar->Append(menuFile, "&File");
198 menuBar->Append(menuFont, "F&ont");
199
200 // ... and attach this menu bar to the frame
201 SetMenuBar(menuBar);
202
203 m_canvas = new MyCanvas(this);
204
205 // create a status bar just for fun (by default with 1 pane only)
206 CreateStatusBar(2);
207 SetStatusText("Welcome to wxWindows!");
208 }
209
210
211 // event handlers
212
213 void MyFrame::OnCreateFont(wxCommandEvent& WXUNUSED(event))
214 {
215 MyFontDialog dialog(this);
216
217 (void)dialog.ShowModal();
218 }
219
220 void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
221 {
222 wxFontData data;
223 data.SetInitialFont(m_canvas->GetTextFont());
224 data.SetColour(m_canvas->GetColour());
225
226 wxFontDialog dialog(this, &data);
227 if ( dialog.ShowModal() == wxID_OK )
228 {
229 wxFontData retData = dialog.GetFontData();
230 m_canvas->SetTextFont(retData.GetChosenFont());
231 m_canvas->SetColour(retData.GetColour());
232 m_canvas->Refresh();
233 }
234 }
235
236 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
237 {
238 // TRUE is to force the frame to close
239 Close(TRUE);
240 }
241
242 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
243 {
244 wxMessageBox("wxWindows font demo.", "About Font",
245 wxOK | wxICON_INFORMATION, this);
246 }
247
248
249 // ----------------------------------------------------------------------------
250 // MyCanvas
251 // ----------------------------------------------------------------------------
252
253 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
254 EVT_PAINT(MyCanvas::OnPaint)
255 END_EVENT_TABLE()
256
257 MyCanvas::MyCanvas( wxWindow *parent )
258 : wxWindow( parent, -1 )
259 {
260 m_font = *wxNORMAL_FONT;
261 m_colour = *wxRED;
262 }
263
264 MyCanvas::~MyCanvas()
265 {
266 }
267
268 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
269 {
270 wxPaintDC dc(this);
271 PrepareDC(dc);
272
273 // set background
274 dc.SetBackground(wxBrush("white", wxSOLID));
275 dc.Clear();
276
277 // output the font name/info
278 wxString fontInfo;
279 fontInfo.Printf("Font family is '%s', style '%s', weight '%s'",
280 m_font.GetFamilyString().c_str(),
281 m_font.GetStyleString().c_str(),
282 m_font.GetWeightString().c_str());
283
284 dc.DrawText(fontInfo, 5, 5);
285
286 // prepare to draw the font
287 dc.SetFont(m_font);
288 dc.SetTextForeground(m_colour);
289
290 // the size of one cell (char + small margin)
291 int w = dc.GetCharWidth() + 5,
292 h = dc.GetCharHeight() + 4;
293
294 // the origin for our table
295 int x = 5,
296 y = 2*h + 5;
297
298 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
299 for ( int i = 1; i < 8; i++ )
300 {
301 for ( int j = 0; j < 32; j++ )
302 {
303 dc.DrawText(char(32*i + j), x + w*j, y + h*i);
304 }
305 }
306
307 // draw the lines between them
308 dc.SetPen(wxPen(wxColour("blue"), 1, wxSOLID));
309 int l;
310
311 // horizontal
312 y += h;
313 for ( l = 0; l < 8; l++ )
314 {
315 int yl = y + h*l - 2;
316 dc.DrawLine(x - 2, yl, x + 32*w - 2, yl);
317 }
318
319 // and vertical
320 for ( l = 0; l < 33; l++ )
321 {
322 int xl = x + w*l - 2;
323 dc.DrawLine(xl, y, xl, y + 7*h - 2);
324 }
325 }
326
327 // ----------------------------------------------------------------------------
328 // MyFontDialog
329 // ----------------------------------------------------------------------------
330
331 MyFontDialog::MyFontDialog(MyFrame *frame)
332 : wxDialog(frame, -1, wxString("Edit font attributes"))
333 {
334 m_canvas = frame->GetCanvas();
335
336 // create controls
337 wxSize sizeBtn = wxButton::GetDefaultSize();
338
339 // TODO
340
341 // position and size the dialog
342 SetClientSize(4*sizeBtn.x, 10*sizeBtn.y);
343 Centre();
344 }
345
346 void MyFontDialog::DoApply()
347 {
348 wxFont font; //(size, family, style, weight, underlined, face, encoding);
349 if ( !font.Ok() )
350 {
351 wxLogError("Font creation failed.");
352 }
353 else
354 {
355 m_canvas->SetTextFont(font);
356 m_canvas->Refresh();
357 }
358 }