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