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 OnIncFont(wxCommandEvent
& event
) { DoResizeFont(+2); }
88 void OnDecFont(wxCommandEvent
& event
) { DoResizeFont(-2); }
90 void OnViewMsg(wxCommandEvent
& event
);
91 void OnSelectFont(wxCommandEvent
& event
);
92 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
93 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
94 { DoEnumerateFamilies(FALSE
); }
95 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
96 { DoEnumerateFamilies(TRUE
); }
97 void OnEnumerateEncodings(wxCommandEvent
& event
);
99 void OnSize(wxSizeEvent
& event
);
102 bool DoEnumerateFamilies(bool fixedWidthOnly
,
103 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
104 bool silent
= FALSE
);
106 void DoResizeFont(int diff
);
107 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
109 void Resize(const wxSize
& size
, const wxFont
& font
= wxNullFont
);
111 size_t m_fontSize
; // in points
113 wxTextCtrl
*m_textctrl
;
117 // any class wishing to process wxWindows events must use this macro
118 DECLARE_EVENT_TABLE()
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // IDs for the controls and the menu commands
135 Font_EnumFamiliesForEncoding
,
137 Font_EnumFixedFamilies
,
142 // ----------------------------------------------------------------------------
143 // event tables and other macros for wxWindows
144 // ----------------------------------------------------------------------------
146 // the event tables connect the wxWindows events with the functions (event
147 // handlers) which process them. It can be also done at run-time, but for the
148 // simple menu events like this the static method is much simpler.
149 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
150 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
151 EVT_MENU(Font_About
, MyFrame::OnAbout
)
152 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
153 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
154 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
155 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
156 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
157 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
158 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
159 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
161 EVT_SIZE(MyFrame::OnSize
)
164 // Create a new application object: this macro will allow wxWindows to create
165 // the application object during program execution (it's better than using a
166 // static object for many reasons) and also declares the accessor function
167 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
171 // ============================================================================
173 // ============================================================================
175 // ----------------------------------------------------------------------------
176 // the application class
177 // ----------------------------------------------------------------------------
179 // `Main program' equivalent: the program execution "starts" here
182 // Create the main application window
183 MyFrame
*frame
= new MyFrame("Font wxWindows demo",
184 wxPoint(50, 50), wxSize(450, 340));
186 // Show it and tell the application that it's our main window
190 // success: wxApp::OnRun() will be called which will enter the main message
191 // loop and the application will run. If we returned FALSE here, the
192 // application would exit immediately.
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
201 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
202 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
), m_textctrl(NULL
)
207 wxMenu
*menuFile
= new wxMenu
;
209 menuFile
->Append(Font_ViewMsg
, "&View...\tCtrl-V",
210 "View an email message file");
211 menuFile
->AppendSeparator();
212 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
213 menuFile
->AppendSeparator();
214 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
216 wxMenu
*menuFont
= new wxMenu
;
217 menuFont
->Append(Font_IncSize
, "&Increase font size by 2 points\tCtrl-I");
218 menuFont
->Append(Font_DecSize
, "&Decrease font size by 2 points\tCtrl-D");
219 menuFont
->AppendSeparator();
220 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-S",
221 "Select a standard font");
222 menuFont
->AppendSeparator();
223 menuFont
->Append(Font_EnumFamilies
, "Enumerate font &families\tCtrl-F");
224 menuFont
->Append(Font_EnumFixedFamilies
,
225 "Enumerate fi&xed font families\tCtrl-X");
226 menuFont
->Append(Font_EnumEncodings
,
227 "Enumerate &encodings\tCtrl-E");
228 menuFont
->Append(Font_EnumFamiliesForEncoding
,
229 "Find font for en&coding...\tCtrl-C",
230 "Find font families for given encoding");
232 // now append the freshly created menu to the menu bar...
233 wxMenuBar
*menuBar
= new wxMenuBar
;
234 menuBar
->Append(menuFile
, "&File");
235 menuBar
->Append(menuFont
, "F&ont");
237 // ... and attach this menu bar to the frame
240 m_textctrl
= new wxTextCtrl(this, -1,
241 "Paste text here to see how it looks\n"
242 "like in the given font",
243 wxDefaultPosition
, wxDefaultSize
,
246 m_canvas
= new MyCanvas(this);
248 // create a status bar just for fun (by default with 1 pane only)
250 SetStatusText("Welcome to wxWindows font demo!");
253 // --------------------------------------------------------
255 class MyEncodingEnumerator
: public wxFontEnumerator
258 MyEncodingEnumerator()
261 const wxString
& GetText() const
265 virtual bool OnFontEncoding(const wxString
& facename
,
266 const wxString
& encoding
)
269 text
.Printf("Encoding %d: %s (available in facename '%s')\n",
270 ++m_n
, encoding
.c_str(), facename
.c_str());
280 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
282 MyEncodingEnumerator fontEnumerator
;
284 fontEnumerator
.EnumerateEncodings();
286 wxLogMessage("Enumerating all available encodings:\n%s",
287 fontEnumerator
.GetText().c_str());
290 // -------------------------------------------------------------
292 class MyFontEnumerator
: public wxFontEnumerator
296 { return !m_facenames
.IsEmpty(); }
298 const wxArrayString
& GetFacenames() const
299 { return m_facenames
; }
302 virtual bool OnFacename(const wxString
& facename
)
304 m_facenames
.Add(facename
);
309 wxArrayString m_facenames
;
312 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
313 wxFontEncoding encoding
,
316 MyFontEnumerator fontEnumerator
;
318 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
320 if ( fontEnumerator
.GotAny() )
322 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
325 wxLogStatus(this, "Found %d %sfonts",
326 nFacenames
, fixedWidthOnly
? "fixed width " : "");
333 facename
= fontEnumerator
.GetFacenames().Item(0);
337 // let the user choose
338 wxString
*facenames
= new wxString
[nFacenames
];
340 for ( n
= 0; n
< nFacenames
; n
++ )
341 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
343 n
= wxGetSingleChoiceIndex("Choose a facename", "Font demo",
344 nFacenames
, facenames
, this);
347 facename
= facenames
[n
];
352 if ( !facename
.IsEmpty() )
354 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
355 wxFONTWEIGHT_NORMAL
, FALSE
, facename
, encoding
);
364 wxLogWarning("No such fonts found.");
370 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
372 static wxFontEncoding encodings
[] =
374 wxFONTENCODING_ISO8859_1
,
375 wxFONTENCODING_ISO8859_2
,
376 wxFONTENCODING_ISO8859_5
,
377 wxFONTENCODING_ISO8859_7
,
378 wxFONTENCODING_ISO8859_15
,
380 wxFONTENCODING_CP1250
,
381 wxFONTENCODING_CP1251
,
382 wxFONTENCODING_CP1252
,
385 static const char *encodingNames
[] =
387 "West European (Latin 1)",
388 "Central European (Latin 2)",
389 "Cyrillic (Latin 5)",
391 "West European new (Latin 0)",
398 int n
= wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
399 WXSIZEOF(encodingNames
),
400 (char **)encodingNames
,
405 DoEnumerateFamilies(FALSE
, encodings
[n
]);
409 void MyFrame::DoResizeFont(int diff
)
411 wxFont fontOld
= m_canvas
->GetTextFont();
415 fontOld
.GetPointSize() + diff
,
419 fontOld
.GetUnderlined(),
420 fontOld
.GetFaceName(),
421 fontOld
.GetEncoding()
426 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
428 Resize(GetSize(), font
);
430 m_canvas
->SetTextFont(font
);
432 m_canvas
->SetColour(col
);
435 m_textctrl
->SetFont(font
);
437 m_textctrl
->SetForegroundColour(col
);
440 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
443 data
.SetInitialFont(m_canvas
->GetTextFont());
444 data
.SetColour(m_canvas
->GetColour());
446 wxFontDialog
dialog(this, &data
);
447 if ( dialog
.ShowModal() == wxID_OK
)
449 wxFontData retData
= dialog
.GetFontData();
450 wxFont font
= retData
.GetChosenFont();
451 wxColour colour
= retData
.GetColour();
453 DoChangeFont(font
, colour
);
457 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
459 // TRUE is to force the frame to close
463 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
465 // first, choose the file
466 static wxString s_dir
, s_file
;
467 wxFileDialog
dialog(this, "Open an email message file",
469 if ( dialog
.ShowModal() != wxID_OK
)
472 // save for the next time
473 s_dir
= dialog
.GetDirectory();
474 s_file
= dialog
.GetFilename();
476 wxString filename
= dialog
.GetPath();
478 // load it and search for Content-Type header
479 wxTextFile
file(filename
);
485 static const char *prefix
= "Content-Type: text/plain; charset=";
486 const size_t len
= strlen(prefix
);
488 size_t n
, count
= file
.GetLineCount();
489 for ( n
= 0; n
< count
; n
++ )
491 wxString line
= file
[n
];
495 // if it is an email message, headers are over, no need to parse
500 if ( line
.Left(len
) == prefix
)
503 const char *pc
= line
.c_str() + len
;
507 while ( *pc
&& *pc
!= '"' )
518 wxLogError("The file '%s' doesn't contain charset information.",
524 // ok, now get the corresponding encoding
525 wxFontEncoding fontenc
= wxTheFontMapper
->CharsetToEncoding(charset
);
526 if ( fontenc
== wxFONTENCODING_SYSTEM
)
528 wxLogError("Charset '%s' is unsupported.", charset
.c_str());
532 m_textctrl
->LoadFile(filename
);
534 if (!wxTheFontMapper
->IsEncodingAvailable(fontenc
))
536 // try to find some similar encoding:
537 wxFontEncoding encAlt
;
538 if ( wxTheFontMapper
->GetAltForEncoding(fontenc
, &encAlt
) )
540 wxEncodingConverter conv
;
542 if (conv
.Init(fontenc
, encAlt
))
545 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
549 wxLogWarning("Cannot convert from '%s' to '%s'.",
550 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
551 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
555 wxLogWarning("No fonts for encoding '%s' on this system.",
556 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
559 // and now create the correct font
560 if ( !DoEnumerateFamilies(FALSE
, fontenc
, TRUE
/* silent */) )
562 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
563 wxFONTWEIGHT_NORMAL
, FALSE
/* !underlined */,
564 wxEmptyString
/* facename */, fontenc
);
571 wxLogWarning("No fonts for encoding '%s' on this system.",
572 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
577 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
579 wxMessageBox("wxWindows font demo\n"
580 "(c) 1999 Vadim Zeitlin",
582 wxOK
| wxICON_INFORMATION
, this);
585 void MyFrame::OnSize(wxSizeEvent
& event
)
587 wxSize size
= event
.GetSize();
594 void MyFrame::Resize(const wxSize
& size
, const wxFont
& font
)
605 h
= 10*(dc
.GetCharHeight() + 1);
609 h
= m_textctrl
->GetSize().y
;
612 m_textctrl
->SetSize(0, 0, size
.x
, h
);
613 m_canvas
->SetSize(0, h
, size
.x
, size
.y
- h
);
616 // ----------------------------------------------------------------------------
618 // ----------------------------------------------------------------------------
620 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
621 EVT_PAINT(MyCanvas::OnPaint
)
624 MyCanvas::MyCanvas( wxWindow
*parent
)
625 : wxWindow( parent
, -1 ),
626 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
630 MyCanvas::~MyCanvas()
634 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
640 dc
.SetBackground(wxBrush("white", wxSOLID
));
643 // output the font name/info
645 fontInfo
.Printf("Font size is %d points, family is %s, style %s, weight %s",
646 m_font
.GetPointSize(),
647 m_font
.GetFamilyString().c_str(),
648 m_font
.GetStyleString().c_str(),
649 m_font
.GetWeightString().c_str());
651 dc
.DrawText(fontInfo
, 5, 5);
653 // prepare to draw the font
655 dc
.SetTextForeground(m_colour
);
657 // the size of one cell (char + small margin)
658 int w
= dc
.GetCharWidth() + 5,
659 h
= dc
.GetCharHeight() + 4;
661 // the origin for our table
665 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
666 for ( int i
= 1; i
< 8; i
++ )
668 for ( int j
= 0; j
< 32; j
++ )
670 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
674 // draw the lines between them
675 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
680 for ( l
= 0; l
< 8; l
++ )
682 int yl
= y
+ h
*l
- 2;
683 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
687 for ( l
= 0; l
< 33; l
++ )
689 int xl
= x
+ w
*l
- 2;
690 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);