]>
Commit | Line | Data |
---|---|---|
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 wxWidgets 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 | #include "wx/fontmap.h" | |
31 | #include "wx/encconv.h" | |
32 | #include "wx/splitter.h" | |
33 | #include "wx/textfile.h" | |
34 | ||
35 | #include "../sample.xpm" | |
36 | ||
37 | #ifdef __WXMAC__ | |
38 | #undef wxFontDialog | |
39 | #include "wx/mac/fontdlg.h" | |
40 | #endif | |
41 | ||
42 | // used as title for several dialog boxes | |
43 | static const wxChar SAMPLE_TITLE[] = _T("wxWidgets Font Sample"); | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // private classes | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | // Define a new application type, each program should derive a class from wxApp | |
50 | class MyApp : public wxApp | |
51 | { | |
52 | public: | |
53 | // override base class virtuals | |
54 | // ---------------------------- | |
55 | ||
56 | // this one is called on application startup and is a good place for the app | |
57 | // initialization (doing it here and not in the ctor allows to have an error | |
58 | // return: if OnInit() returns false, the application terminates) | |
59 | virtual bool OnInit(); | |
60 | }; | |
61 | ||
62 | // MyCanvas is a canvas on which we show the font sample | |
63 | class MyCanvas: public wxWindow | |
64 | { | |
65 | public: | |
66 | MyCanvas( wxWindow *parent ); | |
67 | virtual ~MyCanvas(){}; | |
68 | ||
69 | // accessors for the frame | |
70 | const wxFont& GetTextFont() const { return m_font; } | |
71 | const wxColour& GetColour() const { return m_colour; } | |
72 | void SetTextFont(const wxFont& font) { m_font = font; } | |
73 | void SetColour(const wxColour& colour) { m_colour = colour; } | |
74 | ||
75 | // event handlers | |
76 | void OnPaint( wxPaintEvent &event ); | |
77 | ||
78 | private: | |
79 | wxColour m_colour; | |
80 | wxFont m_font; | |
81 | ||
82 | DECLARE_EVENT_TABLE() | |
83 | }; | |
84 | ||
85 | // Define a new frame type: this is going to be our main frame | |
86 | class MyFrame : public wxFrame | |
87 | { | |
88 | public: | |
89 | // ctor(s) | |
90 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
91 | ||
92 | // accessors | |
93 | MyCanvas *GetCanvas() const { return m_canvas; } | |
94 | ||
95 | // event handlers (these functions should _not_ be virtual) | |
96 | void OnQuit(wxCommandEvent& event); | |
97 | void OnAbout(wxCommandEvent& event); | |
98 | ||
99 | void OnIncFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(+2); } | |
100 | void OnDecFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(-2); } | |
101 | ||
102 | void OnBold(wxCommandEvent& event); | |
103 | void OnItalic(wxCommandEvent& event); | |
104 | void OnUnderline(wxCommandEvent& event); | |
105 | ||
106 | void OnwxPointerFont(wxCommandEvent& event); | |
107 | ||
108 | void OnTestTextValue(wxCommandEvent& event); | |
109 | void OnViewMsg(wxCommandEvent& event); | |
110 | void OnSelectFont(wxCommandEvent& event); | |
111 | void OnEnumerateFamiliesForEncoding(wxCommandEvent& event); | |
112 | void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event)) | |
113 | { DoEnumerateFamilies(false); } | |
114 | void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event)) | |
115 | { DoEnumerateFamilies(true); } | |
116 | void OnEnumerateEncodings(wxCommandEvent& event); | |
117 | ||
118 | void OnSetNativeDesc(wxCommandEvent& event); | |
119 | void OnSetNativeUserDesc(wxCommandEvent& event); | |
120 | void OnSetFaceName(wxCommandEvent& event); | |
121 | void OnSetEncoding(wxCommandEvent& event); | |
122 | ||
123 | protected: | |
124 | bool DoEnumerateFamilies(bool fixedWidthOnly, | |
125 | wxFontEncoding encoding = wxFONTENCODING_SYSTEM, | |
126 | bool silent = false); | |
127 | ||
128 | void DoResizeFont(int diff); | |
129 | void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour); | |
130 | ||
131 | // ask the user to choose an encoding and return it or | |
132 | // wxFONTENCODING_SYSTEM if the dialog was cancelled | |
133 | wxFontEncoding GetEncodingFromUser(); | |
134 | ||
135 | ||
136 | size_t m_fontSize; // in points | |
137 | ||
138 | wxTextCtrl *m_textctrl; | |
139 | MyCanvas *m_canvas; | |
140 | ||
141 | private: | |
142 | // any class wishing to process wxWidgets events must use this macro | |
143 | DECLARE_EVENT_TABLE() | |
144 | }; | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // constants | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
150 | // IDs for the controls and the menu commands | |
151 | enum | |
152 | { | |
153 | // menu items | |
154 | Font_Quit = 1, | |
155 | Font_About, | |
156 | Font_ViewMsg, | |
157 | Font_TestTextValue, | |
158 | ||
159 | Font_IncSize, | |
160 | Font_DecSize, | |
161 | Font_Bold, | |
162 | Font_Italic, | |
163 | Font_Underlined, | |
164 | Font_wxNORMAL_FONT, | |
165 | Font_wxSMALL_FONT, | |
166 | Font_wxITALIC_FONT, | |
167 | Font_wxSWISS_FONT, | |
168 | Font_Standard, | |
169 | ||
170 | Font_Choose = 100, | |
171 | Font_EnumFamiliesForEncoding, | |
172 | Font_EnumFamilies, | |
173 | Font_EnumFixedFamilies, | |
174 | Font_EnumEncodings, | |
175 | Font_SetNativeDesc, | |
176 | Font_SetNativeUserDesc, | |
177 | Font_SetFaceName, | |
178 | Font_SetEncoding, | |
179 | Font_Max | |
180 | }; | |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // event tables and other macros for wxWidgets | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | // the event tables connect the wxWidgets events with the functions (event | |
187 | // handlers) which process them. It can be also done at run-time, but for the | |
188 | // simple menu events like this the static method is much simpler. | |
189 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
190 | EVT_MENU(Font_Quit, MyFrame::OnQuit) | |
191 | EVT_MENU(Font_TestTextValue, MyFrame::OnTestTextValue) | |
192 | EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg) | |
193 | EVT_MENU(Font_About, MyFrame::OnAbout) | |
194 | ||
195 | EVT_MENU(Font_IncSize, MyFrame::OnIncFont) | |
196 | EVT_MENU(Font_DecSize, MyFrame::OnDecFont) | |
197 | EVT_MENU(Font_Bold, MyFrame::OnBold) | |
198 | EVT_MENU(Font_Italic, MyFrame::OnItalic) | |
199 | EVT_MENU(Font_Underlined, MyFrame::OnUnderline) | |
200 | ||
201 | EVT_MENU(Font_wxNORMAL_FONT, MyFrame::OnwxPointerFont) | |
202 | EVT_MENU(Font_wxSMALL_FONT, MyFrame::OnwxPointerFont) | |
203 | EVT_MENU(Font_wxITALIC_FONT, MyFrame::OnwxPointerFont) | |
204 | EVT_MENU(Font_wxSWISS_FONT, MyFrame::OnwxPointerFont) | |
205 | ||
206 | ||
207 | EVT_MENU(Font_SetNativeDesc, MyFrame::OnSetNativeDesc) | |
208 | EVT_MENU(Font_SetNativeUserDesc, MyFrame::OnSetNativeUserDesc) | |
209 | EVT_MENU(Font_SetFaceName, MyFrame::OnSetFaceName) | |
210 | EVT_MENU(Font_SetEncoding, MyFrame::OnSetEncoding) | |
211 | ||
212 | EVT_MENU(Font_Choose, MyFrame::OnSelectFont) | |
213 | EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding) | |
214 | EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies) | |
215 | EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies) | |
216 | EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings) | |
217 | END_EVENT_TABLE() | |
218 | ||
219 | // Create a new application object: this macro will allow wxWidgets to create | |
220 | // the application object during program execution (it's better than using a | |
221 | // static object for many reasons) and also declares the accessor function | |
222 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
223 | // not wxApp) | |
224 | IMPLEMENT_APP(MyApp) | |
225 | ||
226 | // ============================================================================ | |
227 | // implementation | |
228 | // ============================================================================ | |
229 | ||
230 | // ---------------------------------------------------------------------------- | |
231 | // the application class | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
234 | // `Main program' equivalent: the program execution "starts" here | |
235 | bool MyApp::OnInit() | |
236 | { | |
237 | if ( !wxApp::OnInit() ) | |
238 | return false; | |
239 | ||
240 | // Create the main application window | |
241 | MyFrame *frame = new MyFrame(wxT("Font wxWidgets demo"), | |
242 | wxPoint(50, 50), wxSize(600, 400)); | |
243 | ||
244 | // Show it and tell the application that it's our main window | |
245 | frame->Show(true); | |
246 | SetTopWindow(frame); | |
247 | ||
248 | // success: wxApp::OnRun() will be called which will enter the main message | |
249 | // loop and the application will run. If we returned 'false' here, the | |
250 | // application would exit immediately. | |
251 | return true; | |
252 | } | |
253 | ||
254 | // ---------------------------------------------------------------------------- | |
255 | // main frame | |
256 | // ---------------------------------------------------------------------------- | |
257 | ||
258 | // frame constructor | |
259 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
260 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), m_textctrl(NULL) | |
261 | { | |
262 | m_fontSize = wxNORMAL_FONT->GetPointSize(); | |
263 | ||
264 | SetIcon(wxIcon(sample_xpm)); | |
265 | ||
266 | // create a menu bar | |
267 | wxMenu *menuFile = new wxMenu; | |
268 | ||
269 | menuFile->Append(Font_TestTextValue, wxT("&Test text value"), | |
270 | wxT("Verify that getting and setting text value doesn't change it")); | |
271 | menuFile->Append(Font_ViewMsg, wxT("&View...\tCtrl-V"), | |
272 | wxT("View an email message file")); | |
273 | menuFile->AppendSeparator(); | |
274 | menuFile->Append(Font_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog")); | |
275 | menuFile->AppendSeparator(); | |
276 | menuFile->Append(Font_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); | |
277 | ||
278 | wxMenu *menuFont = new wxMenu; | |
279 | menuFont->Append(Font_IncSize, wxT("&Increase font size by 2 points\tCtrl-I")); | |
280 | menuFont->Append(Font_DecSize, wxT("&Decrease font size by 2 points\tCtrl-D")); | |
281 | menuFont->AppendSeparator(); | |
282 | menuFont->AppendCheckItem(Font_Bold, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state")); | |
283 | menuFont->AppendCheckItem(Font_Italic, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state")); | |
284 | menuFont->AppendCheckItem(Font_Underlined, wxT("&Underlined\tCtrl-U"), | |
285 | wxT("Toggle underlined state")); | |
286 | ||
287 | menuFont->AppendSeparator(); | |
288 | menuFont->Append(Font_SetNativeDesc, | |
289 | wxT("Set native font &description\tShift-Ctrl-D")); | |
290 | menuFont->Append(Font_SetNativeUserDesc, | |
291 | wxT("Set &user font description\tShift-Ctrl-U")); | |
292 | menuFont->Append(Font_SetFaceName, wxT("Check font face name")); | |
293 | menuFont->Append(Font_SetEncoding, wxT("Set font &encoding\tShift-Ctrl-E")); | |
294 | ||
295 | wxMenu *menuSelect = new wxMenu; | |
296 | menuSelect->Append(Font_Choose, wxT("&Select font...\tCtrl-S"), | |
297 | wxT("Select a standard font")); | |
298 | ||
299 | wxMenu *menuStdFonts = new wxMenu; | |
300 | menuStdFonts->Append(Font_wxNORMAL_FONT, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets")); | |
301 | menuStdFonts->Append(Font_wxSMALL_FONT, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets")); | |
302 | menuStdFonts->Append(Font_wxITALIC_FONT, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets")); | |
303 | menuStdFonts->Append(Font_wxSWISS_FONT, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets")); | |
304 | menuSelect->Append(Font_Standard, wxT("Standar&d fonts"), menuStdFonts); | |
305 | ||
306 | menuSelect->AppendSeparator(); | |
307 | menuSelect->Append(Font_EnumFamilies, wxT("Enumerate font &families\tCtrl-F")); | |
308 | menuSelect->Append(Font_EnumFixedFamilies, | |
309 | wxT("Enumerate fi&xed font families\tCtrl-X")); | |
310 | menuSelect->Append(Font_EnumEncodings, | |
311 | wxT("Enumerate &encodings\tCtrl-E")); | |
312 | menuSelect->Append(Font_EnumFamiliesForEncoding, | |
313 | wxT("Find font for en&coding...\tCtrl-C"), | |
314 | wxT("Find font families for given encoding")); | |
315 | ||
316 | // now append the freshly created menu to the menu bar... | |
317 | wxMenuBar *menuBar = new wxMenuBar; | |
318 | menuBar->Append(menuFile, wxT("&File")); | |
319 | menuBar->Append(menuFont, wxT("F&ont")); | |
320 | menuBar->Append(menuSelect, wxT("&Select")); | |
321 | ||
322 | // ... and attach this menu bar to the frame | |
323 | SetMenuBar(menuBar); | |
324 | ||
325 | wxSplitterWindow *splitter = new wxSplitterWindow(this); | |
326 | ||
327 | m_textctrl = new wxTextCtrl(splitter, wxID_ANY, | |
328 | wxT("Paste text here to see how it looks\nlike in the given font"), | |
329 | wxDefaultPosition, wxDefaultSize, | |
330 | wxTE_MULTILINE); | |
331 | ||
332 | m_canvas = new MyCanvas(splitter); | |
333 | ||
334 | splitter->SplitHorizontally(m_textctrl, m_canvas, 100); | |
335 | ||
336 | #if wxUSE_STATUSBAR | |
337 | // create a status bar just for fun (by default with 1 pane only) | |
338 | CreateStatusBar(); | |
339 | SetStatusText(wxT("Welcome to wxWidgets font demo!")); | |
340 | #endif // wxUSE_STATUSBAR | |
341 | } | |
342 | ||
343 | // -------------------------------------------------------- | |
344 | ||
345 | class MyEncodingEnumerator : public wxFontEnumerator | |
346 | { | |
347 | public: | |
348 | MyEncodingEnumerator() | |
349 | { m_n = 0; } | |
350 | ||
351 | const wxString& GetText() const | |
352 | { return m_text; } | |
353 | ||
354 | protected: | |
355 | virtual bool OnFontEncoding(const wxString& facename, | |
356 | const wxString& encoding) | |
357 | { | |
358 | wxString text; | |
359 | text.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"), | |
360 | (unsigned int) ++m_n, encoding.c_str(), facename.c_str()); | |
361 | m_text += text; | |
362 | return true; | |
363 | } | |
364 | ||
365 | private: | |
366 | size_t m_n; | |
367 | wxString m_text; | |
368 | }; | |
369 | ||
370 | void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event)) | |
371 | { | |
372 | MyEncodingEnumerator fontEnumerator; | |
373 | ||
374 | fontEnumerator.EnumerateEncodings(); | |
375 | ||
376 | wxLogMessage(wxT("Enumerating all available encodings:\n%s"), | |
377 | fontEnumerator.GetText().c_str()); | |
378 | } | |
379 | ||
380 | // ------------------------------------------------------------- | |
381 | ||
382 | class MyFontEnumerator : public wxFontEnumerator | |
383 | { | |
384 | public: | |
385 | bool GotAny() const | |
386 | { return !m_facenames.IsEmpty(); } | |
387 | ||
388 | const wxArrayString& GetFacenames() const | |
389 | { return m_facenames; } | |
390 | ||
391 | protected: | |
392 | virtual bool OnFacename(const wxString& facename) | |
393 | { | |
394 | m_facenames.Add(facename); | |
395 | return true; | |
396 | } | |
397 | ||
398 | private: | |
399 | wxArrayString m_facenames; | |
400 | } fontEnumerator; | |
401 | ||
402 | bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly, | |
403 | wxFontEncoding encoding, | |
404 | bool silent) | |
405 | { | |
406 | MyFontEnumerator fontEnumerator; | |
407 | ||
408 | fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly); | |
409 | ||
410 | if ( fontEnumerator.GotAny() ) | |
411 | { | |
412 | int nFacenames = fontEnumerator.GetFacenames().GetCount(); | |
413 | if ( !silent ) | |
414 | { | |
415 | wxLogStatus(this, wxT("Found %d %sfonts"), | |
416 | nFacenames, fixedWidthOnly ? wxT("fixed width ") : wxT("")); | |
417 | } | |
418 | ||
419 | wxString facename; | |
420 | ||
421 | if ( silent ) | |
422 | { | |
423 | // choose the first | |
424 | facename = fontEnumerator.GetFacenames().Item(0); | |
425 | } | |
426 | else | |
427 | { | |
428 | // let the user choose | |
429 | wxString *facenames = new wxString[nFacenames]; | |
430 | int n; | |
431 | for ( n = 0; n < nFacenames; n++ ) | |
432 | facenames[n] = fontEnumerator.GetFacenames().Item(n); | |
433 | ||
434 | n = wxGetSingleChoiceIndex | |
435 | ( | |
436 | wxT("Choose a facename"), | |
437 | SAMPLE_TITLE, | |
438 | nFacenames, | |
439 | facenames, | |
440 | this | |
441 | ); | |
442 | ||
443 | if ( n != -1 ) | |
444 | facename = facenames[n]; | |
445 | ||
446 | delete [] facenames; | |
447 | } | |
448 | ||
449 | if ( !facename.empty() ) | |
450 | { | |
451 | wxFont font(wxNORMAL_FONT->GetPointSize(), | |
452 | wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, | |
453 | wxFONTWEIGHT_NORMAL, false, facename, encoding); | |
454 | ||
455 | DoChangeFont(font); | |
456 | } | |
457 | ||
458 | return true; | |
459 | } | |
460 | else if ( !silent ) | |
461 | { | |
462 | wxLogWarning(wxT("No such fonts found.")); | |
463 | } | |
464 | ||
465 | return false; | |
466 | } | |
467 | ||
468 | void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event)) | |
469 | { | |
470 | wxFontEncoding enc = GetEncodingFromUser(); | |
471 | if ( enc != wxFONTENCODING_SYSTEM ) | |
472 | { | |
473 | DoEnumerateFamilies(false, enc); | |
474 | } | |
475 | } | |
476 | ||
477 | void MyFrame::OnSetNativeDesc(wxCommandEvent& WXUNUSED(event)) | |
478 | { | |
479 | wxString fontInfo = wxGetTextFromUser | |
480 | ( | |
481 | wxT("Enter native font string"), | |
482 | wxT("Input font description"), | |
483 | m_canvas->GetTextFont().GetNativeFontInfoDesc(), | |
484 | this | |
485 | ); | |
486 | if ( fontInfo.empty() ) | |
487 | return; // user clicked "Cancel" - do nothing | |
488 | ||
489 | wxFont font; | |
490 | font.SetNativeFontInfo(fontInfo); | |
491 | if ( !font.Ok() ) | |
492 | { | |
493 | wxLogError(wxT("Font info string \"%s\" is invalid."), | |
494 | fontInfo.c_str()); | |
495 | return; | |
496 | } | |
497 | ||
498 | DoChangeFont(font); | |
499 | } | |
500 | ||
501 | void MyFrame::OnSetFaceName(wxCommandEvent& WXUNUSED(event)) | |
502 | { | |
503 | wxString facename = GetCanvas()->GetTextFont().GetFaceName(); | |
504 | wxString newFaceName = wxGetTextFromUser( | |
505 | wxT("Here you can edit current font face name."), | |
506 | wxT("Input font facename"), facename, | |
507 | this); | |
508 | if (newFaceName.IsEmpty()) | |
509 | return; // user clicked "Cancel" - do nothing | |
510 | ||
511 | wxFont font(GetCanvas()->GetTextFont()); | |
512 | if (font.SetFaceName(newFaceName)) // change facename only | |
513 | { | |
514 | wxASSERT_MSG(font.Ok(), wxT("The font should now be valid")); | |
515 | DoChangeFont(font); | |
516 | } | |
517 | else | |
518 | { | |
519 | wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid")); | |
520 | wxMessageBox(wxT("There is no font with such face name..."), | |
521 | wxT("Invalid face name"), wxOK|wxICON_ERROR, this); | |
522 | } | |
523 | } | |
524 | ||
525 | void MyFrame::OnSetNativeUserDesc(wxCommandEvent& WXUNUSED(event)) | |
526 | { | |
527 | wxString fontdesc = GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc(); | |
528 | wxString fontUserInfo = wxGetTextFromUser( | |
529 | wxT("Here you can edit current font description"), | |
530 | wxT("Input font description"), fontdesc, | |
531 | this); | |
532 | if (fontUserInfo.IsEmpty()) | |
533 | return; // user clicked "Cancel" - do nothing | |
534 | ||
535 | wxFont font; | |
536 | if (font.SetNativeFontInfoUserDesc(fontUserInfo)) | |
537 | { | |
538 | wxASSERT_MSG(font.Ok(), wxT("The font should now be valid")); | |
539 | DoChangeFont(font); | |
540 | } | |
541 | else | |
542 | { | |
543 | wxASSERT_MSG(!font.Ok(), wxT("The font should now be invalid")); | |
544 | wxMessageBox(wxT("Error trying to create a font with such description...")); | |
545 | } | |
546 | } | |
547 | ||
548 | void MyFrame::OnSetEncoding(wxCommandEvent& WXUNUSED(event)) | |
549 | { | |
550 | wxFontEncoding enc = GetEncodingFromUser(); | |
551 | if ( enc == wxFONTENCODING_SYSTEM ) | |
552 | return; | |
553 | ||
554 | wxFont font = m_canvas->GetTextFont(); | |
555 | font.SetEncoding(enc); | |
556 | DoChangeFont(font); | |
557 | } | |
558 | ||
559 | wxFontEncoding MyFrame::GetEncodingFromUser() | |
560 | { | |
561 | wxArrayString names; | |
562 | wxArrayInt encodings; | |
563 | ||
564 | const size_t count = wxFontMapper::GetSupportedEncodingsCount(); | |
565 | names.reserve(count); | |
566 | encodings.reserve(count); | |
567 | ||
568 | for ( size_t n = 0; n < count; n++ ) | |
569 | { | |
570 | wxFontEncoding enc = wxFontMapper::GetEncoding(n); | |
571 | encodings.push_back(enc); | |
572 | names.push_back(wxFontMapper::GetEncodingName(enc)); | |
573 | } | |
574 | ||
575 | int i = wxGetSingleChoiceIndex | |
576 | ( | |
577 | wxT("Choose the encoding"), | |
578 | SAMPLE_TITLE, | |
579 | names, | |
580 | this | |
581 | ); | |
582 | ||
583 | return i == -1 ? wxFONTENCODING_SYSTEM : (wxFontEncoding)encodings[i]; | |
584 | } | |
585 | ||
586 | void MyFrame::DoResizeFont(int diff) | |
587 | { | |
588 | wxFont font = m_canvas->GetTextFont(); | |
589 | ||
590 | font.SetPointSize(font.GetPointSize() + diff); | |
591 | DoChangeFont(font); | |
592 | } | |
593 | ||
594 | void MyFrame::OnBold(wxCommandEvent& event) | |
595 | { | |
596 | wxFont font = m_canvas->GetTextFont(); | |
597 | ||
598 | font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL); | |
599 | DoChangeFont(font); | |
600 | } | |
601 | ||
602 | void MyFrame::OnItalic(wxCommandEvent& event) | |
603 | { | |
604 | wxFont font = m_canvas->GetTextFont(); | |
605 | ||
606 | font.SetStyle(event.IsChecked() ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL); | |
607 | DoChangeFont(font); | |
608 | } | |
609 | ||
610 | void MyFrame::OnUnderline(wxCommandEvent& event) | |
611 | { | |
612 | wxFont font = m_canvas->GetTextFont(); | |
613 | ||
614 | font.SetUnderlined(event.IsChecked()); | |
615 | DoChangeFont(font); | |
616 | } | |
617 | ||
618 | void MyFrame::OnwxPointerFont(wxCommandEvent& event) | |
619 | { | |
620 | wxFont font; | |
621 | ||
622 | switch ( event.GetId() ) | |
623 | { | |
624 | case Font_wxNORMAL_FONT: | |
625 | font = *wxNORMAL_FONT; | |
626 | break; | |
627 | ||
628 | case Font_wxSMALL_FONT: | |
629 | font = *wxSMALL_FONT; | |
630 | break; | |
631 | ||
632 | case Font_wxITALIC_FONT: | |
633 | font = *wxITALIC_FONT; | |
634 | break; | |
635 | ||
636 | case Font_wxSWISS_FONT: | |
637 | font = *wxSWISS_FONT; | |
638 | break; | |
639 | ||
640 | default: | |
641 | wxFAIL_MSG( wxT("unknown standard font") ); | |
642 | return; | |
643 | } | |
644 | ||
645 | DoChangeFont(font); | |
646 | } | |
647 | ||
648 | void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col) | |
649 | { | |
650 | m_canvas->SetTextFont(font); | |
651 | if ( col.Ok() ) | |
652 | m_canvas->SetColour(col); | |
653 | m_canvas->Refresh(); | |
654 | ||
655 | m_textctrl->SetFont(font); | |
656 | if ( col.Ok() ) | |
657 | m_textctrl->SetForegroundColour(col); | |
658 | ||
659 | // update the state of the bold/italic/underlined menu items | |
660 | wxMenuBar *mbar = GetMenuBar(); | |
661 | if ( mbar ) | |
662 | { | |
663 | mbar->Check(Font_Bold, font.GetWeight() == wxFONTWEIGHT_BOLD); | |
664 | mbar->Check(Font_Italic, font.GetStyle() == wxFONTSTYLE_ITALIC); | |
665 | mbar->Check(Font_Underlined, font.GetUnderlined()); | |
666 | } | |
667 | } | |
668 | ||
669 | void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event)) | |
670 | { | |
671 | wxFontData data; | |
672 | data.SetInitialFont(m_canvas->GetTextFont()); | |
673 | data.SetColour(m_canvas->GetColour()); | |
674 | ||
675 | wxFontDialog dialog(this, data); | |
676 | if ( dialog.ShowModal() == wxID_OK ) | |
677 | { | |
678 | wxFontData retData = dialog.GetFontData(); | |
679 | wxFont font = retData.GetChosenFont(); | |
680 | wxColour colour = retData.GetColour(); | |
681 | ||
682 | DoChangeFont(font, colour); | |
683 | } | |
684 | } | |
685 | ||
686 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
687 | { | |
688 | // true is to force the frame to close | |
689 | Close(true); | |
690 | } | |
691 | ||
692 | void MyFrame::OnTestTextValue(wxCommandEvent& WXUNUSED(event)) | |
693 | { | |
694 | wxString value = m_textctrl->GetValue(); | |
695 | m_textctrl->SetValue(value); | |
696 | if ( m_textctrl->GetValue() != value ) | |
697 | { | |
698 | wxLogError(wxT("Text value changed after getting and setting it")); | |
699 | } | |
700 | } | |
701 | ||
702 | void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event)) | |
703 | { | |
704 | #if wxUSE_FILEDLG | |
705 | // first, choose the file | |
706 | static wxString s_dir, s_file; | |
707 | wxFileDialog dialog(this, wxT("Open an email message file"), | |
708 | s_dir, s_file); | |
709 | if ( dialog.ShowModal() != wxID_OK ) | |
710 | return; | |
711 | ||
712 | // save for the next time | |
713 | s_dir = dialog.GetDirectory(); | |
714 | s_file = dialog.GetFilename(); | |
715 | ||
716 | wxString filename = dialog.GetPath(); | |
717 | ||
718 | // load it and search for Content-Type header | |
719 | wxTextFile file(filename); | |
720 | if ( !file.Open() ) | |
721 | return; | |
722 | ||
723 | wxString charset; | |
724 | ||
725 | static const wxChar *prefix = wxT("Content-Type: text/plain; charset="); | |
726 | const size_t len = wxStrlen(prefix); | |
727 | ||
728 | size_t n, count = file.GetLineCount(); | |
729 | for ( n = 0; n < count; n++ ) | |
730 | { | |
731 | wxString line = file[n]; | |
732 | ||
733 | if ( !line ) | |
734 | { | |
735 | // if it is an email message, headers are over, no need to parse | |
736 | // all the file | |
737 | break; | |
738 | } | |
739 | ||
740 | if ( line.Left(len) == prefix ) | |
741 | { | |
742 | // found! | |
743 | const wxChar *pc = line.c_str() + len; | |
744 | if ( *pc == wxT('"') ) | |
745 | pc++; | |
746 | ||
747 | while ( *pc && *pc != wxT('"') ) | |
748 | { | |
749 | charset += *pc++; | |
750 | } | |
751 | ||
752 | break; | |
753 | } | |
754 | } | |
755 | ||
756 | if ( !charset ) | |
757 | { | |
758 | wxLogError(wxT("The file '%s' doesn't contain charset information."), | |
759 | filename.c_str()); | |
760 | ||
761 | return; | |
762 | } | |
763 | ||
764 | // ok, now get the corresponding encoding | |
765 | wxFontEncoding fontenc = wxFontMapper::Get()->CharsetToEncoding(charset); | |
766 | if ( fontenc == wxFONTENCODING_SYSTEM ) | |
767 | { | |
768 | wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str()); | |
769 | return; | |
770 | } | |
771 | ||
772 | m_textctrl->LoadFile(filename); | |
773 | ||
774 | if ( fontenc == wxFONTENCODING_UTF8 || | |
775 | !wxFontMapper::Get()->IsEncodingAvailable(fontenc) ) | |
776 | { | |
777 | // try to find some similar encoding: | |
778 | wxFontEncoding encAlt; | |
779 | if ( wxFontMapper::Get()->GetAltForEncoding(fontenc, &encAlt) ) | |
780 | { | |
781 | wxEncodingConverter conv; | |
782 | ||
783 | if (conv.Init(fontenc, encAlt)) | |
784 | { | |
785 | fontenc = encAlt; | |
786 | m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue())); | |
787 | } | |
788 | else | |
789 | { | |
790 | wxLogWarning(wxT("Cannot convert from '%s' to '%s'."), | |
791 | wxFontMapper::GetEncodingDescription(fontenc).c_str(), | |
792 | wxFontMapper::GetEncodingDescription(encAlt).c_str()); | |
793 | } | |
794 | } | |
795 | else | |
796 | wxLogWarning(wxT("No fonts for encoding '%s' on this system."), | |
797 | wxFontMapper::GetEncodingDescription(fontenc).c_str()); | |
798 | } | |
799 | ||
800 | // and now create the correct font | |
801 | if ( !DoEnumerateFamilies(false, fontenc, true /* silent */) ) | |
802 | { | |
803 | wxFont font(wxNORMAL_FONT->GetPointSize(), | |
804 | wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, | |
805 | wxFONTWEIGHT_NORMAL, false /* !underlined */, | |
806 | wxEmptyString /* facename */, fontenc); | |
807 | if ( font.Ok() ) | |
808 | { | |
809 | DoChangeFont(font); | |
810 | } | |
811 | else | |
812 | { | |
813 | wxLogWarning(wxT("No fonts for encoding '%s' on this system."), | |
814 | wxFontMapper::GetEncodingDescription(fontenc).c_str()); | |
815 | } | |
816 | } | |
817 | #endif // wxUSE_FILEDLG | |
818 | } | |
819 | ||
820 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
821 | { | |
822 | wxMessageBox(wxT("wxWidgets font sample\n") | |
823 | wxT("(c) 1999-2006 Vadim Zeitlin"), | |
824 | wxString(wxT("About ")) + SAMPLE_TITLE, | |
825 | wxOK | wxICON_INFORMATION, this); | |
826 | } | |
827 | ||
828 | // ---------------------------------------------------------------------------- | |
829 | // MyCanvas | |
830 | // ---------------------------------------------------------------------------- | |
831 | ||
832 | BEGIN_EVENT_TABLE(MyCanvas, wxWindow) | |
833 | EVT_PAINT(MyCanvas::OnPaint) | |
834 | END_EVENT_TABLE() | |
835 | ||
836 | MyCanvas::MyCanvas( wxWindow *parent ) | |
837 | : wxWindow( parent, wxID_ANY ), | |
838 | m_colour(*wxRED), m_font(*wxNORMAL_FONT) | |
839 | { | |
840 | } | |
841 | ||
842 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
843 | { | |
844 | wxPaintDC dc(this); | |
845 | PrepareDC(dc); | |
846 | ||
847 | // set background | |
848 | dc.SetBackground(wxBrush(wxT("white"), wxSOLID)); | |
849 | dc.Clear(); | |
850 | ||
851 | // one text line height | |
852 | wxCoord hLine = dc.GetCharHeight(); | |
853 | ||
854 | // the current text origin | |
855 | wxCoord x = 5, | |
856 | y = 5; | |
857 | ||
858 | // output the font name/info | |
859 | wxString fontInfo; | |
860 | fontInfo.Printf(wxT("Font size is %d points, family: %s, encoding: %s"), | |
861 | m_font.GetPointSize(), | |
862 | m_font.GetFamilyString().c_str(), | |
863 | wxFontMapper:: | |
864 | GetEncodingDescription(m_font.GetEncoding()).c_str()); | |
865 | ||
866 | dc.DrawText(fontInfo, x, y); | |
867 | y += hLine; | |
868 | ||
869 | fontInfo.Printf(wxT("Style: %s, weight: %s, fixed width: %s"), | |
870 | m_font.GetStyleString().c_str(), | |
871 | m_font.GetWeightString().c_str(), | |
872 | m_font.IsFixedWidth() ? _T("yes") : _T("no")); | |
873 | ||
874 | dc.DrawText(fontInfo, x, y); | |
875 | y += hLine; | |
876 | ||
877 | if ( m_font.Ok() ) | |
878 | { | |
879 | const wxNativeFontInfo *info = m_font.GetNativeFontInfo(); | |
880 | if ( info ) | |
881 | { | |
882 | wxString fontDesc = m_font.GetNativeFontInfoUserDesc(); | |
883 | fontInfo.Printf(wxT("Native font info: %s"), fontDesc.c_str()); | |
884 | ||
885 | dc.DrawText(fontInfo, x, y); | |
886 | y += hLine; | |
887 | } | |
888 | } | |
889 | ||
890 | y += hLine; | |
891 | ||
892 | // prepare to draw the font | |
893 | dc.SetFont(m_font); | |
894 | dc.SetTextForeground(m_colour); | |
895 | ||
896 | // the size of one cell (Normally biggest char + small margin) | |
897 | wxCoord maxCharWidth, maxCharHeight; | |
898 | dc.GetTextExtent(wxT("W"), &maxCharWidth, &maxCharHeight); | |
899 | int w = maxCharWidth + 5, | |
900 | h = maxCharHeight + 4; | |
901 | ||
902 | ||
903 | // print all font symbols from 32 to 256 in 7 rows of 32 chars each | |
904 | for ( int i = 0; i < 7; i++ ) | |
905 | { | |
906 | for ( int j = 0; j < 32; j++ ) | |
907 | { | |
908 | wxChar c = (wxChar)(32 * (i + 1) + j); | |
909 | ||
910 | wxCoord charWidth, charHeight; | |
911 | dc.GetTextExtent(c, &charWidth, &charHeight); | |
912 | dc.DrawText | |
913 | ( | |
914 | c, | |
915 | x + w*j + (maxCharWidth - charWidth) / 2 + 1, | |
916 | y + h*i + (maxCharHeight - charHeight) / 2 | |
917 | ); | |
918 | } | |
919 | } | |
920 | ||
921 | // draw the lines between them | |
922 | dc.SetPen(wxPen(wxColour(_T("blue")), 1, wxSOLID)); | |
923 | int l; | |
924 | ||
925 | // horizontal | |
926 | for ( l = 0; l < 8; l++ ) | |
927 | { | |
928 | int yl = y + h*l - 2; | |
929 | dc.DrawLine(x - 2, yl, x + 32*w - 1, yl); | |
930 | } | |
931 | ||
932 | // and vertical | |
933 | for ( l = 0; l < 33; l++ ) | |
934 | { | |
935 | int xl = x + w*l - 2; | |
936 | dc.DrawLine(xl, y - 2, xl, y + 7*h - 1); | |
937 | } | |
938 | } |