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!");
240 void MyFrame
::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
242 class MyEncodingEnumerator
: public wxFontEnumerator
245 MyEncodingEnumerator() { m_n
= 0; }
247 const wxString
& GetText() const { return m_text
; }
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());
267 fontEnumerator
.EnumerateEncodings();
269 wxLogMessage("Enumerating all available encodings:\n%s",
270 fontEnumerator
.GetText().c_str());
273 bool MyFrame
::DoEnumerateFamilies(bool fixedWidthOnly
,
274 wxFontEncoding encoding
,
277 class MyFontEnumerator
: public wxFontEnumerator
280 bool GotAny() const { return !m_facenames
.IsEmpty(); }
282 const wxArrayString
& GetFacenames() const { return m_facenames
; }
285 virtual bool OnFacename(const wxString
& facename
)
287 m_facenames
.Add(facename
);
293 wxArrayString m_facenames
;
296 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
298 if ( fontEnumerator
.GotAny() )
300 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
303 wxLogStatus(this, "Found %d %sfonts",
304 nFacenames
, fixedWidthOnly ?
"fixed width " : "");
311 facename
= fontEnumerator
.GetFacenames().Item(0);
315 // let the user choose
316 wxString
*facenames
= new wxString
[nFacenames
];
318 for ( n
= 0; n
< nFacenames
; n
++ )
319 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
321 n
= wxGetSingleChoiceIndex("Choose a facename", "Font demo",
322 nFacenames
, facenames
, this);
325 facename
= facenames
[n
];
330 if ( !facename
.IsEmpty() )
332 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
333 wxFONTWEIGHT_NORMAL
, FALSE
, facename
, encoding
);
342 wxLogWarning("No such fonts found.");
348 void MyFrame
::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
350 static wxFontEncoding encodings
[] =
352 wxFONTENCODING_ISO8859_1
,
353 wxFONTENCODING_ISO8859_2
,
354 wxFONTENCODING_ISO8859_5
,
355 wxFONTENCODING_ISO8859_7
,
356 wxFONTENCODING_ISO8859_15
,
358 wxFONTENCODING_CP1250
,
359 wxFONTENCODING_CP1251
,
360 wxFONTENCODING_CP1252
,
363 static const char *encodingNames
[] =
365 "West European (Latin 1)",
366 "Central European (Latin 2)",
367 "Cyrillic (Latin 5)",
369 "West European new (Latin 0)",
376 int n
= wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
377 WXSIZEOF(encodingNames
),
378 (char **)encodingNames
,
383 DoEnumerateFamilies(FALSE
, encodings
[n
]);
387 void MyFrame
::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
389 Resize(GetSize(), font
);
391 m_canvas
->SetTextFont(font
);
393 m_canvas
->SetColour(col
);
396 m_textctrl
->SetFont(font
);
398 m_textctrl
->SetForegroundColour(col
);
401 void MyFrame
::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
404 data
.SetInitialFont(m_canvas
->GetTextFont());
405 data
.SetColour(m_canvas
->GetColour());
407 wxFontDialog
dialog(this, &data
);
408 if ( dialog
.ShowModal() == wxID_OK
)
410 wxFontData retData
= dialog
.GetFontData();
411 wxFont font
= retData
.GetChosenFont();
412 wxColour colour
= retData
.GetColour();
414 DoChangeFont(font
, colour
);
418 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
420 // TRUE is to force the frame to close
424 void MyFrame
::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
426 // first, choose the file
427 static wxString s_dir
, s_file
;
428 wxFileDialog
dialog(this, "Open an email message file",
430 if ( dialog
.ShowModal() != wxID_OK
)
433 // save for the next time
434 s_dir
= dialog
.GetDirectory();
435 s_file
= dialog
.GetFilename();
437 wxString filename
= dialog
.GetPath();
439 // load it and search for Content-Type header
440 wxTextFile
file(filename
);
446 static const char *prefix
= "Content-Type: text/plain; charset=";
447 const size_t len
= strlen(prefix
);
449 size_t n
, count
= file
.GetLineCount();
450 for ( n
= 0; n
< count
; n
++ )
452 wxString line
= file
[n
];
456 // if it is an email message, headers are over, no need to parse
461 if ( line
.Left(len
) == prefix
)
464 const char *pc
= line
.c_str() + len
;
468 while ( *pc
&& *pc
!= '"' )
479 wxLogError("The file '%s' doesn't contain charset information.",
485 // ok, now get the corresponding encoding
486 wxFontEncoding fontenc
= wxTheFontMapper
->CharsetToEncoding(charset
);
487 if ( fontenc
== wxFONTENCODING_SYSTEM
)
489 wxLogError("Charset '%s' is unsupported.", charset
.c_str());
493 m_textctrl
->LoadFile(filename
);
495 if (!wxTheFontMapper
->IsEncodingAvailable(fontenc
))
497 // try to find some similar encoding:
499 if (wxTheFontMapper
->GetAltForEncoding(fontenc
, &enc2
,
500 wxEmptyString
/*facename*/, FALSE
/*interactive*/))
502 wxEncodingConverter conv
;
504 if (conv
.Init(fontenc
, enc2
))
507 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
510 wxLogWarning("Cannot convert from '%s' to '%s'.",
511 wxFontMapper
::GetEncodingDescription(fontenc
).c_str(),
512 wxFontMapper
::GetEncodingDescription(enc2
).c_str());
515 wxLogWarning("No fonts for encoding '%s' on this system.",
516 wxFontMapper
::GetEncodingDescription(fontenc
).c_str());
519 // and now create the correct font
520 if ( !DoEnumerateFamilies(FALSE
, fontenc
, TRUE
/* silent */) )
522 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
523 wxFONTWEIGHT_NORMAL
, FALSE
/* !underlined */,
524 wxEmptyString
/* facename */, fontenc
);
531 wxLogWarning("No fonts for encoding '%s' on this system.",
532 wxFontMapper
::GetEncodingDescription(fontenc
).c_str());
537 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
539 wxMessageBox("wxWindows font demo\n"
540 "(c) 1999 Vadim Zeitlin",
542 wxOK
| wxICON_INFORMATION
, this);
545 void MyFrame
::OnSize(wxSizeEvent
& event
)
547 wxSize size
= event
.GetSize();
554 void MyFrame
::Resize(const wxSize
& size
, const wxFont
& font
)
565 h
= 10*(dc
.GetCharHeight() + 1);
569 h
= m_textctrl
->GetSize().y
;
572 m_textctrl
->SetSize(0, 0, size
.x
, h
);
573 m_canvas
->SetSize(0, h
, size
.x
, size
.y
- h
);
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
581 EVT_PAINT(MyCanvas
::OnPaint
)
584 MyCanvas
::MyCanvas( wxWindow
*parent
)
585 : wxWindow( parent
, -1 ),
586 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
590 MyCanvas
::~MyCanvas()
594 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
600 dc
.SetBackground(wxBrush("white", wxSOLID
));
603 // output the font name/info
605 fontInfo
.Printf("Font family is '%s', style '%s', weight '%s'",
606 m_font
.GetFamilyString().c_str(),
607 m_font
.GetStyleString().c_str(),
608 m_font
.GetWeightString().c_str());
610 dc
.DrawText(fontInfo
, 5, 5);
612 // prepare to draw the font
614 dc
.SetTextForeground(m_colour
);
616 // the size of one cell (char + small margin)
617 int w
= dc
.GetCharWidth() + 5,
618 h
= dc
.GetCharHeight() + 4;
620 // the origin for our table
624 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
625 for ( int i
= 1; i
< 8; i
++ )
627 for ( int j
= 0; j
< 32; j
++ )
629 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
633 // draw the lines between them
634 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
639 for ( l
= 0; l
< 8; l
++ )
641 int yl
= y
+ h
*l
- 2;
642 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
646 for ( l
= 0; l
< 33; l
++ )
648 int xl
= x
+ w
*l
- 2;
649 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);