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