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