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