1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont demo
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include <wx/wxprec.h>
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all <standard< wxWindows headers
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/textfile.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // Define a new application type, each program should derive a class from wxApp
39 class MyApp
: public wxApp
42 // override base class virtuals
43 // ----------------------------
45 // this one is called on application startup and is a good place for the app
46 // initialization (doing it here and not in the ctor allows to have an error
47 // return: if OnInit() returns false, the application terminates)
48 virtual bool OnInit();
51 // MyCanvas is a canvas on which we show the font sample
52 class MyCanvas
: public wxWindow
55 MyCanvas( wxWindow
*parent
);
58 // accessors for the frame
59 const wxFont
& GetTextFont() const { return m_font
; }
60 const wxColour
& GetColour() const { return m_colour
; }
61 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
62 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
65 void OnPaint( wxPaintEvent
&event
);
74 // Define a new frame type: this is going to be our main frame
75 class MyFrame
: public wxFrame
79 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
82 MyCanvas
*GetCanvas() const { return m_canvas
; }
84 // event handlers (these functions should _not_ be virtual)
85 void OnQuit(wxCommandEvent
& event
);
86 void OnAbout(wxCommandEvent
& event
);
87 void OnViewMsg(wxCommandEvent
& event
);
88 void OnSelectFont(wxCommandEvent
& event
);
89 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
90 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
91 { DoEnumerateFamilies(FALSE
); }
92 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
93 { DoEnumerateFamilies(TRUE
); }
94 void OnEnumerateEncodings(wxCommandEvent
& event
);
96 void OnSize(wxSizeEvent
& event
);
99 bool DoEnumerateFamilies(bool fixedWidthOnly
,
100 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
101 bool silent
= FALSE
);
103 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
105 void Resize(const wxSize
& size
, const wxFont
& font
= wxNullFont
);
107 wxTextCtrl
*m_textctrl
;
111 // any class wishing to process wxWindows events must use this macro
112 DECLARE_EVENT_TABLE()
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // IDs for the controls and the menu commands
127 Font_EnumFamiliesForEncoding
,
129 Font_EnumFixedFamilies
,
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWindows
136 // ----------------------------------------------------------------------------
138 // the event tables connect the wxWindows events with the functions (event
139 // handlers) which process them. It can be also done at run-time, but for the
140 // simple menu events like this the static method is much simpler.
141 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
142 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
143 EVT_MENU(Font_About
, MyFrame::OnAbout
)
144 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
145 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
146 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
147 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
148 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
149 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
151 EVT_SIZE(MyFrame::OnSize
)
154 // Create a new application object: this macro will allow wxWindows to create
155 // the application object during program execution (it's better than using a
156 // static object for many reasons) and also declares the accessor function
157 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
161 // ============================================================================
163 // ============================================================================
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
169 // `Main program' equivalent: the program execution "starts" here
172 // Create the main application window
173 MyFrame
*frame
= new MyFrame("Font wxWindows demo",
174 wxPoint(50, 50), wxSize(450, 340));
176 // Show it and tell the application that it's our main window
180 // success: wxApp::OnRun() will be called which will enter the main message
181 // loop and the application will run. If we returned FALSE here, the
182 // application would exit immediately.
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
192 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
), m_textctrl(NULL
)
195 wxMenu
*menuFile
= new wxMenu
;
197 menuFile
->Append(Font_ViewMsg
, "&View...\tCtrl-V",
198 "View an email message file");
199 menuFile
->AppendSeparator();
200 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
201 menuFile
->AppendSeparator();
202 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
204 wxMenu
*menuFont
= new wxMenu
;
205 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-S",
206 "Select a standard font");
207 menuFont
->AppendSeparator();
208 menuFont
->Append(Font_EnumFamilies
, "Enumerate font &families\tCtrl-F");
209 menuFont
->Append(Font_EnumFixedFamilies
,
210 "Enumerate f&ixed font families\tCtrl-I");
211 menuFont
->Append(Font_EnumEncodings
,
212 "Enumerate &encodings\tCtrl-E");
213 menuFont
->Append(Font_EnumFamiliesForEncoding
,
214 "Find font for en&coding...\tCtrl-C",
215 "Find font families for given encoding");
217 // now append the freshly created menu to the menu bar...
218 wxMenuBar
*menuBar
= new wxMenuBar
;
219 menuBar
->Append(menuFile
, "&File");
220 menuBar
->Append(menuFont
, "F&ont");
222 // ... and attach this menu bar to the frame
225 m_textctrl
= new wxTextCtrl(this, -1,
226 "Paste text here to see how it looks\n"
227 "like in the given font",
228 wxDefaultPosition
, wxDefaultSize
,
231 m_canvas
= new MyCanvas(this);
233 // create a status bar just for fun (by default with 1 pane only)
235 SetStatusText("Welcome to wxWindows font demo!");
238 // --------------------------------------------------------
240 class MyEncodingEnumerator
: public wxFontEnumerator
243 MyEncodingEnumerator()
246 const wxString
& GetText() const
250 virtual bool OnFontEncoding(const wxString
& facename
,
251 const wxString
& encoding
)
254 text
.Printf("Encoding %d: %s (available in facename '%s')\n",
255 ++m_n
, encoding
.c_str(), facename
.c_str());
265 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
267 MyEncodingEnumerator fontEnumerator
;
269 fontEnumerator
.EnumerateEncodings();
271 wxLogMessage("Enumerating all available encodings:\n%s",
272 fontEnumerator
.GetText().c_str());
275 // -------------------------------------------------------------
277 class MyFontEnumerator
: public wxFontEnumerator
281 { return !m_facenames
.IsEmpty(); }
283 const wxArrayString
& GetFacenames() const
284 { return m_facenames
; }
287 virtual bool OnFacename(const wxString
& facename
)
289 m_facenames
.Add(facename
);
294 wxArrayString m_facenames
;
297 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
298 wxFontEncoding encoding
,
301 MyFontEnumerator fontEnumerator
;
303 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
305 if ( fontEnumerator
.GotAny() )
307 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
310 wxLogStatus(this, "Found %d %sfonts",
311 nFacenames
, fixedWidthOnly
? "fixed width " : "");
318 facename
= fontEnumerator
.GetFacenames().Item(0);
322 // let the user choose
323 wxString
*facenames
= new wxString
[nFacenames
];
325 for ( n
= 0; n
< nFacenames
; n
++ )
326 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
328 n
= wxGetSingleChoiceIndex("Choose a facename", "Font demo",
329 nFacenames
, facenames
, this);
332 facename
= facenames
[n
];
337 if ( !facename
.IsEmpty() )
339 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
340 wxFONTWEIGHT_NORMAL
, FALSE
, facename
, encoding
);
349 wxLogWarning("No such fonts found.");
355 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
357 static wxFontEncoding encodings
[] =
359 wxFONTENCODING_ISO8859_1
,
360 wxFONTENCODING_ISO8859_2
,
361 wxFONTENCODING_ISO8859_5
,
362 wxFONTENCODING_ISO8859_7
,
363 wxFONTENCODING_ISO8859_15
,
365 wxFONTENCODING_CP1250
,
366 wxFONTENCODING_CP1251
,
367 wxFONTENCODING_CP1252
,
370 static const char *encodingNames
[] =
372 "West European (Latin 1)",
373 "Central European (Latin 2)",
374 "Cyrillic (Latin 5)",
376 "West European new (Latin 0)",
383 int n
= wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
384 WXSIZEOF(encodingNames
),
385 (char **)encodingNames
,
390 DoEnumerateFamilies(FALSE
, encodings
[n
]);
394 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
396 Resize(GetSize(), font
);
398 m_canvas
->SetTextFont(font
);
400 m_canvas
->SetColour(col
);
403 m_textctrl
->SetFont(font
);
405 m_textctrl
->SetForegroundColour(col
);
408 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
411 data
.SetInitialFont(m_canvas
->GetTextFont());
412 data
.SetColour(m_canvas
->GetColour());
414 wxFontDialog
dialog(this, &data
);
415 if ( dialog
.ShowModal() == wxID_OK
)
417 wxFontData retData
= dialog
.GetFontData();
418 wxFont font
= retData
.GetChosenFont();
419 wxColour colour
= retData
.GetColour();
421 DoChangeFont(font
, colour
);
425 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
427 // TRUE is to force the frame to close
431 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
433 // first, choose the file
434 static wxString s_dir
, s_file
;
435 wxFileDialog
dialog(this, "Open an email message file",
437 if ( dialog
.ShowModal() != wxID_OK
)
440 // save for the next time
441 s_dir
= dialog
.GetDirectory();
442 s_file
= dialog
.GetFilename();
444 wxString filename
= dialog
.GetPath();
446 // load it and search for Content-Type header
447 wxTextFile
file(filename
);
453 static const char *prefix
= "Content-Type: text/plain; charset=";
454 const size_t len
= strlen(prefix
);
456 size_t n
, count
= file
.GetLineCount();
457 for ( n
= 0; n
< count
; n
++ )
459 wxString line
= file
[n
];
463 // if it is an email message, headers are over, no need to parse
468 if ( line
.Left(len
) == prefix
)
471 const char *pc
= line
.c_str() + len
;
475 while ( *pc
&& *pc
!= '"' )
486 wxLogError("The file '%s' doesn't contain charset information.",
492 // ok, now get the corresponding encoding
493 wxFontEncoding fontenc
= wxTheFontMapper
->CharsetToEncoding(charset
);
494 if ( fontenc
== wxFONTENCODING_SYSTEM
)
496 wxLogError("Charset '%s' is unsupported.", charset
.c_str());
500 m_textctrl
->LoadFile(filename
);
502 if (!wxTheFontMapper
->IsEncodingAvailable(fontenc
))
504 // try to find some similar encoding:
505 wxFontEncoding encAlt
;
506 if ( wxTheFontMapper
->GetAltForEncoding(fontenc
, &encAlt
) )
508 wxEncodingConverter conv
;
510 if (conv
.Init(fontenc
, encAlt
))
513 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
517 wxLogWarning("Cannot convert from '%s' to '%s'.",
518 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
519 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
523 wxLogWarning("No fonts for encoding '%s' on this system.",
524 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
527 // and now create the correct font
528 if ( !DoEnumerateFamilies(FALSE
, fontenc
, TRUE
/* silent */) )
530 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
531 wxFONTWEIGHT_NORMAL
, FALSE
/* !underlined */,
532 wxEmptyString
/* facename */, fontenc
);
539 wxLogWarning("No fonts for encoding '%s' on this system.",
540 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
545 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
547 wxMessageBox("wxWindows font demo\n"
548 "(c) 1999 Vadim Zeitlin",
550 wxOK
| wxICON_INFORMATION
, this);
553 void MyFrame::OnSize(wxSizeEvent
& event
)
555 wxSize size
= event
.GetSize();
562 void MyFrame::Resize(const wxSize
& size
, const wxFont
& font
)
573 h
= 10*(dc
.GetCharHeight() + 1);
577 h
= m_textctrl
->GetSize().y
;
580 m_textctrl
->SetSize(0, 0, size
.x
, h
);
581 m_canvas
->SetSize(0, h
, size
.x
, size
.y
- h
);
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
589 EVT_PAINT(MyCanvas::OnPaint
)
592 MyCanvas::MyCanvas( wxWindow
*parent
)
593 : wxWindow( parent
, -1 ),
594 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
598 MyCanvas::~MyCanvas()
602 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
608 dc
.SetBackground(wxBrush("white", wxSOLID
));
611 // output the font name/info
613 fontInfo
.Printf("Font family is '%s', style '%s', weight '%s'",
614 m_font
.GetFamilyString().c_str(),
615 m_font
.GetStyleString().c_str(),
616 m_font
.GetWeightString().c_str());
618 dc
.DrawText(fontInfo
, 5, 5);
620 // prepare to draw the font
622 dc
.SetTextForeground(m_colour
);
624 // the size of one cell (char + small margin)
625 int w
= dc
.GetCharWidth() + 5,
626 h
= dc
.GetCharHeight() + 4;
628 // the origin for our table
632 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
633 for ( int i
= 1; i
< 8; i
++ )
635 for ( int j
= 0; j
< 32; j
++ )
637 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
641 // draw the lines between them
642 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
647 for ( l
= 0; l
< 8; l
++ )
649 int yl
= y
+ h
*l
- 2;
650 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
654 for ( l
= 0; l
< 33; l
++ )
656 int xl
= x
+ w
*l
- 2;
657 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);