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