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