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