]>
Commit | Line | Data |
---|---|---|
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> |
1ccd74da VZ |
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); | |
36f210c8 | 85 | void OnEnumerateFamiliesForEncoding(wxCommandEvent& event); |
ddc8c2e3 VZ |
86 | void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event)) |
87 | { DoEnumerateFamilies(FALSE); } | |
88 | void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event)) | |
89 | { DoEnumerateFamilies(TRUE); } | |
d111a89a | 90 | void OnEnumerateEncodings(wxCommandEvent& event); |
1ccd74da | 91 | |
36f210c8 VZ |
92 | void OnSize(wxSizeEvent& event); |
93 | ||
1ccd74da | 94 | protected: |
36f210c8 VZ |
95 | void DoEnumerateFamilies(bool fixedWidthOnly, |
96 | wxFontEncoding encoding = wxFONTENCODING_SYSTEM); | |
97 | ||
a1d58ddc VZ |
98 | void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour); |
99 | ||
36f210c8 | 100 | void Resize(const wxSize& size, const wxFont& font = wxNullFont); |
ddc8c2e3 | 101 | |
36f210c8 VZ |
102 | wxTextCtrl *m_textctrl; |
103 | MyCanvas *m_canvas; | |
1ccd74da VZ |
104 | |
105 | private: | |
106 | // any class wishing to process wxWindows events must use this macro | |
107 | DECLARE_EVENT_TABLE() | |
108 | }; | |
109 | ||
1ccd74da VZ |
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, | |
36f210c8 | 121 | Font_EnumFamiliesForEncoding, |
ddc8c2e3 VZ |
122 | Font_EnumFamilies, |
123 | Font_EnumFixedFamilies, | |
d111a89a | 124 | Font_EnumEncodings, |
ddc8c2e3 | 125 | Font_Max |
1ccd74da VZ |
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) | |
36f210c8 | 139 | EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding) |
ddc8c2e3 VZ |
140 | EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies) |
141 | EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies) | |
d111a89a | 142 | EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings) |
36f210c8 VZ |
143 | |
144 | EVT_SIZE(MyFrame::OnSize) | |
1ccd74da VZ |
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 | |
ddc8c2e3 | 166 | MyFrame *frame = new MyFrame("Font wxWindows demo", |
1ccd74da VZ |
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) | |
a1d58ddc | 185 | : wxFrame((wxFrame *)NULL, -1, title, pos, size), m_textctrl(NULL) |
1ccd74da | 186 | { |
1ccd74da VZ |
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; | |
ddc8c2e3 | 195 | menuFont->Append(Font_Choose, "&Select font...\tCtrl-S", |
1ccd74da | 196 | "Select a standard font"); |
ddc8c2e3 | 197 | menuFont->AppendSeparator(); |
d111a89a | 198 | menuFont->Append(Font_EnumFamilies, "Enumerate font &families\tCtrl-F"); |
ddc8c2e3 | 199 | menuFont->Append(Font_EnumFixedFamilies, |
d111a89a VZ |
200 | "Enumerate f&ixed font families\tCtrl-I"); |
201 | menuFont->Append(Font_EnumEncodings, | |
202 | "Enumerate &encodings\tCtrl-E"); | |
36f210c8 VZ |
203 | menuFont->Append(Font_EnumFamiliesForEncoding, |
204 | "Find font for en&coding...\tCtrl-C", | |
205 | "Find font families for given encoding"); | |
1ccd74da VZ |
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 | ||
36f210c8 VZ |
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 | ||
1ccd74da VZ |
221 | m_canvas = new MyCanvas(this); |
222 | ||
223 | // create a status bar just for fun (by default with 1 pane only) | |
a1d58ddc VZ |
224 | CreateStatusBar(); |
225 | SetStatusText("Welcome to wxWindows font demo!"); | |
1ccd74da VZ |
226 | } |
227 | ||
228 | ||
229 | // event handlers | |
d111a89a VZ |
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 | } | |
1ccd74da | 262 | |
36f210c8 | 263 | void MyFrame::DoEnumerateFamilies(bool fixedWidthOnly, wxFontEncoding encoding) |
ddc8c2e3 VZ |
264 | { |
265 | class MyFontEnumerator : public wxFontEnumerator | |
266 | { | |
267 | public: | |
a1d58ddc | 268 | bool GotAny() const { return !m_facenames.IsEmpty(); } |
36f210c8 | 269 | |
a1d58ddc | 270 | const wxArrayString& GetFacenames() const { return m_facenames; } |
d111a89a | 271 | |
ddc8c2e3 VZ |
272 | protected: |
273 | virtual bool OnFontFamily(const wxString& family) | |
274 | { | |
a1d58ddc | 275 | m_facenames.Add(family); |
ddc8c2e3 VZ |
276 | |
277 | return TRUE; | |
278 | } | |
279 | ||
280 | private: | |
a1d58ddc | 281 | wxArrayString m_facenames; |
ddc8c2e3 VZ |
282 | } fontEnumerator; |
283 | ||
36f210c8 | 284 | fontEnumerator.EnumerateFamilies(encoding, fixedWidthOnly); |
d111a89a | 285 | |
36f210c8 VZ |
286 | if ( fontEnumerator.GotAny() ) |
287 | { | |
a1d58ddc VZ |
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; | |
36f210c8 VZ |
307 | } |
308 | else | |
309 | { | |
310 | wxLogWarning("No such fonts found."); | |
311 | } | |
ddc8c2e3 VZ |
312 | } |
313 | ||
36f210c8 | 314 | void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event)) |
1ccd74da | 315 | { |
36f210c8 VZ |
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", | |
a1d58ddc VZ |
343 | WXSIZEOF(encodingNames), |
344 | (char **)encodingNames, | |
36f210c8 VZ |
345 | this); |
346 | ||
347 | if ( n != -1 ) | |
348 | { | |
349 | DoEnumerateFamilies(FALSE, encodings[n]); | |
350 | } | |
1ccd74da VZ |
351 | } |
352 | ||
a1d58ddc VZ |
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 | ||
1ccd74da VZ |
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(); | |
36f210c8 VZ |
377 | wxFont font = retData.GetChosenFont(); |
378 | wxColour colour = retData.GetColour(); | |
379 | ||
a1d58ddc | 380 | DoChangeFont(font, colour); |
1ccd74da VZ |
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 | { | |
36f210c8 VZ |
392 | wxMessageBox("wxWindows font demo\n" |
393 | "(c) 1999 Vadim Zeitlin", | |
394 | "About Font", | |
1ccd74da VZ |
395 | wxOK | wxICON_INFORMATION, this); |
396 | } | |
397 | ||
36f210c8 VZ |
398 | void MyFrame::OnSize(wxSizeEvent& event) |
399 | { | |
400 | wxSize size = event.GetSize(); | |
401 | ||
402 | Resize(size); | |
717a57c2 VZ |
403 | |
404 | event.Skip(); | |
36f210c8 VZ |
405 | } |
406 | ||
407 | void MyFrame::Resize(const wxSize& size, const wxFont& font) | |
408 | { | |
a1d58ddc VZ |
409 | if ( !m_textctrl ) |
410 | return; | |
411 | ||
36f210c8 VZ |
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 | } | |
1ccd74da VZ |
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, | |
36f210c8 | 476 | y = 2*h; |
1ccd74da VZ |
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 | } |