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