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 OnCheckNativeToFromString(wxCommandEvent
& event
);
101 void OnSize(wxSizeEvent
& event
);
104 bool DoEnumerateFamilies(bool fixedWidthOnly
,
105 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
,
106 bool silent
= FALSE
);
108 void DoResizeFont(int diff
);
109 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
111 void Resize(const wxSize
& size
, const wxFont
& font
= wxNullFont
);
113 size_t m_fontSize
; // in points
115 wxTextCtrl
*m_textctrl
;
119 // any class wishing to process wxWindows events must use this macro
120 DECLARE_EVENT_TABLE()
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // IDs for the controls and the menu commands
137 Font_EnumFamiliesForEncoding
,
139 Font_EnumFixedFamilies
,
141 Font_CheckNativeToFromString
145 // ----------------------------------------------------------------------------
146 // event tables and other macros for wxWindows
147 // ----------------------------------------------------------------------------
149 // the event tables connect the wxWindows events with the functions (event
150 // handlers) which process them. It can be also done at run-time, but for the
151 // simple menu events like this the static method is much simpler.
152 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
153 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
154 EVT_MENU(Font_About
, MyFrame::OnAbout
)
155 EVT_MENU(Font_IncSize
, MyFrame::OnIncFont
)
156 EVT_MENU(Font_DecSize
, MyFrame::OnDecFont
)
157 EVT_MENU(Font_ViewMsg
, MyFrame::OnViewMsg
)
158 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
159 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
160 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
161 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
162 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
163 EVT_MENU(Font_CheckNativeToFromString
, MyFrame::OnCheckNativeToFromString
)
165 EVT_SIZE(MyFrame::OnSize
)
168 // Create a new application object: this macro will allow wxWindows to create
169 // the application object during program execution (it's better than using a
170 // static object for many reasons) and also declares the accessor function
171 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
175 // ============================================================================
177 // ============================================================================
179 // ----------------------------------------------------------------------------
180 // the application class
181 // ----------------------------------------------------------------------------
183 // `Main program' equivalent: the program execution "starts" here
186 // Create the main application window
187 MyFrame
*frame
= new MyFrame("Font wxWindows demo",
188 wxPoint(50, 50), wxSize(450, 340));
190 // Show it and tell the application that it's our main window
194 // success: wxApp::OnRun() will be called which will enter the main message
195 // loop and the application will run. If we returned FALSE here, the
196 // application would exit immediately.
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
205 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
206 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
), m_textctrl(NULL
)
211 wxMenu
*menuFile
= new wxMenu
;
213 menuFile
->Append(Font_ViewMsg
, "&View...\tCtrl-V",
214 "View an email message file");
215 menuFile
->AppendSeparator();
216 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
217 menuFile
->AppendSeparator();
218 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
220 wxMenu
*menuFont
= new wxMenu
;
221 menuFont
->Append(Font_IncSize
, "&Increase font size by 2 points\tCtrl-I");
222 menuFont
->Append(Font_DecSize
, "&Decrease font size by 2 points\tCtrl-D");
223 menuFont
->AppendSeparator();
224 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-S",
225 "Select a standard font");
226 menuFont
->AppendSeparator();
227 menuFont
->Append(Font_EnumFamilies
, "Enumerate font &families\tCtrl-F");
228 menuFont
->Append(Font_EnumFixedFamilies
,
229 "Enumerate fi&xed font families\tCtrl-X");
230 menuFont
->Append(Font_EnumEncodings
,
231 "Enumerate &encodings\tCtrl-E");
232 menuFont
->Append(Font_EnumFamiliesForEncoding
,
233 "Find font for en&coding...\tCtrl-C",
234 "Find font families for given encoding");
235 menuFont
->AppendSeparator();
236 menuFont
->Append(Font_CheckNativeToFromString
,
237 "Check Native Font Info To/From String");
239 // now append the freshly created menu to the menu bar...
240 wxMenuBar
*menuBar
= new wxMenuBar
;
241 menuBar
->Append(menuFile
, "&File");
242 menuBar
->Append(menuFont
, "F&ont");
244 // ... and attach this menu bar to the frame
247 m_textctrl
= new wxTextCtrl(this, -1,
248 "Paste text here to see how it looks\n"
249 "like in the given font",
250 wxDefaultPosition
, wxDefaultSize
,
253 m_canvas
= new MyCanvas(this);
255 // create a status bar just for fun (by default with 1 pane only)
257 SetStatusText("Welcome to wxWindows font demo!");
260 // --------------------------------------------------------
262 class MyEncodingEnumerator
: public wxFontEnumerator
265 MyEncodingEnumerator()
268 const wxString
& GetText() const
272 virtual bool OnFontEncoding(const wxString
& facename
,
273 const wxString
& encoding
)
276 text
.Printf("Encoding %d: %s (available in facename '%s')\n",
277 ++m_n
, encoding
.c_str(), facename
.c_str());
287 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
289 MyEncodingEnumerator fontEnumerator
;
291 fontEnumerator
.EnumerateEncodings();
293 wxLogMessage("Enumerating all available encodings:\n%s",
294 fontEnumerator
.GetText().c_str());
297 // -------------------------------------------------------------
299 class MyFontEnumerator
: public wxFontEnumerator
303 { return !m_facenames
.IsEmpty(); }
305 const wxArrayString
& GetFacenames() const
306 { return m_facenames
; }
309 virtual bool OnFacename(const wxString
& facename
)
311 m_facenames
.Add(facename
);
316 wxArrayString m_facenames
;
319 bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
,
320 wxFontEncoding encoding
,
323 MyFontEnumerator fontEnumerator
;
325 fontEnumerator
.EnumerateFacenames(encoding
, fixedWidthOnly
);
327 if ( fontEnumerator
.GotAny() )
329 int nFacenames
= fontEnumerator
.GetFacenames().GetCount();
332 wxLogStatus(this, "Found %d %sfonts",
333 nFacenames
, fixedWidthOnly
? "fixed width " : "");
340 facename
= fontEnumerator
.GetFacenames().Item(0);
344 // let the user choose
345 wxString
*facenames
= new wxString
[nFacenames
];
347 for ( n
= 0; n
< nFacenames
; n
++ )
348 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
350 n
= wxGetSingleChoiceIndex("Choose a facename", "Font demo",
351 nFacenames
, facenames
, this);
354 facename
= facenames
[n
];
359 if ( !facename
.IsEmpty() )
361 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
362 wxFONTWEIGHT_NORMAL
, FALSE
, facename
, encoding
);
371 wxLogWarning("No such fonts found.");
377 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
379 static wxFontEncoding encodings
[] =
381 wxFONTENCODING_ISO8859_1
,
382 wxFONTENCODING_ISO8859_2
,
383 wxFONTENCODING_ISO8859_5
,
384 wxFONTENCODING_ISO8859_7
,
385 wxFONTENCODING_ISO8859_15
,
387 wxFONTENCODING_CP1250
,
388 wxFONTENCODING_CP1251
,
389 wxFONTENCODING_CP1252
,
392 static const wxString encodingNames
[] =
394 "West European (Latin 1)",
395 "Central European (Latin 2)",
396 "Cyrillic (Latin 5)",
398 "West European new (Latin 0)",
405 int n
= wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
406 WXSIZEOF(encodingNames
),
412 DoEnumerateFamilies(FALSE
, encodings
[n
]);
416 void MyFrame::OnCheckNativeToFromString(wxCommandEvent
& WXUNUSED(event
))
418 wxString fontInfo
= m_canvas
->GetTextFont().GetNativeFontInfo().ToString();
420 if(fontInfo
.IsEmpty())
421 wxMessageBox("Native font info string is empty!", "Font demo",
425 wxNativeFontInfo info
;
426 info
.FromString(fontInfo
);
428 if(fontInfo
== font
.GetNativeFontInfo().ToString())
429 wxMessageBox("wxNativeFontInfo ToString()/FromString() works!",
432 wxMessageBox("wxNativeFontInfo ToString()/FromString() doesn't work!",
437 void MyFrame::DoResizeFont(int diff
)
439 wxFont fontOld
= m_canvas
->GetTextFont();
443 fontOld
.GetPointSize() + diff
,
447 fontOld
.GetUnderlined(),
448 fontOld
.GetFaceName(),
449 fontOld
.GetEncoding()
454 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
456 Resize(GetSize(), font
);
458 m_canvas
->SetTextFont(font
);
460 m_canvas
->SetColour(col
);
463 m_textctrl
->SetFont(font
);
465 m_textctrl
->SetForegroundColour(col
);
468 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
471 data
.SetInitialFont(m_canvas
->GetTextFont());
472 data
.SetColour(m_canvas
->GetColour());
474 wxFontDialog
dialog(this, &data
);
475 if ( dialog
.ShowModal() == wxID_OK
)
477 wxFontData retData
= dialog
.GetFontData();
478 wxFont font
= retData
.GetChosenFont();
479 wxColour colour
= retData
.GetColour();
481 DoChangeFont(font
, colour
);
485 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
487 // TRUE is to force the frame to close
491 void MyFrame::OnViewMsg(wxCommandEvent
& WXUNUSED(event
))
493 // first, choose the file
494 static wxString s_dir
, s_file
;
495 wxFileDialog
dialog(this, "Open an email message file",
497 if ( dialog
.ShowModal() != wxID_OK
)
500 // save for the next time
501 s_dir
= dialog
.GetDirectory();
502 s_file
= dialog
.GetFilename();
504 wxString filename
= dialog
.GetPath();
506 // load it and search for Content-Type header
507 wxTextFile
file(filename
);
513 static const char *prefix
= "Content-Type: text/plain; charset=";
514 const size_t len
= strlen(prefix
);
516 size_t n
, count
= file
.GetLineCount();
517 for ( n
= 0; n
< count
; n
++ )
519 wxString line
= file
[n
];
523 // if it is an email message, headers are over, no need to parse
528 if ( line
.Left(len
) == prefix
)
531 const char *pc
= line
.c_str() + len
;
535 while ( *pc
&& *pc
!= '"' )
546 wxLogError("The file '%s' doesn't contain charset information.",
552 // ok, now get the corresponding encoding
553 wxFontEncoding fontenc
= wxTheFontMapper
->CharsetToEncoding(charset
);
554 if ( fontenc
== wxFONTENCODING_SYSTEM
)
556 wxLogError("Charset '%s' is unsupported.", charset
.c_str());
560 m_textctrl
->LoadFile(filename
);
562 if (!wxTheFontMapper
->IsEncodingAvailable(fontenc
))
564 // try to find some similar encoding:
565 wxFontEncoding encAlt
;
566 if ( wxTheFontMapper
->GetAltForEncoding(fontenc
, &encAlt
) )
568 wxEncodingConverter conv
;
570 if (conv
.Init(fontenc
, encAlt
))
573 m_textctrl
-> SetValue(conv
.Convert(m_textctrl
-> GetValue()));
577 wxLogWarning("Cannot convert from '%s' to '%s'.",
578 wxFontMapper::GetEncodingDescription(fontenc
).c_str(),
579 wxFontMapper::GetEncodingDescription(encAlt
).c_str());
583 wxLogWarning("No fonts for encoding '%s' on this system.",
584 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
587 // and now create the correct font
588 if ( !DoEnumerateFamilies(FALSE
, fontenc
, TRUE
/* silent */) )
590 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
591 wxFONTWEIGHT_NORMAL
, FALSE
/* !underlined */,
592 wxEmptyString
/* facename */, fontenc
);
599 wxLogWarning("No fonts for encoding '%s' on this system.",
600 wxFontMapper::GetEncodingDescription(fontenc
).c_str());
605 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
607 wxMessageBox("wxWindows font demo\n"
608 "(c) 1999 Vadim Zeitlin",
610 wxOK
| wxICON_INFORMATION
, this);
613 void MyFrame::OnSize(wxSizeEvent
& event
)
615 wxSize size
= event
.GetSize();
622 void MyFrame::Resize(const wxSize
& size
, const wxFont
& font
)
633 h
= 10*(dc
.GetCharHeight() + 1);
637 h
= m_textctrl
->GetSize().y
;
640 m_textctrl
->SetSize(0, 0, size
.x
, h
);
641 m_canvas
->SetSize(0, h
, size
.x
, size
.y
- h
);
644 // ----------------------------------------------------------------------------
646 // ----------------------------------------------------------------------------
648 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
649 EVT_PAINT(MyCanvas::OnPaint
)
652 MyCanvas::MyCanvas( wxWindow
*parent
)
653 : wxWindow( parent
, -1 ),
654 m_colour(*wxRED
), m_font(*wxNORMAL_FONT
)
658 MyCanvas::~MyCanvas()
662 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
668 dc
.SetBackground(wxBrush("white", wxSOLID
));
671 // output the font name/info
673 fontInfo
.Printf("Font size is %d points, family is %s, style %s, weight %s",
674 m_font
.GetPointSize(),
675 m_font
.GetFamilyString().c_str(),
676 m_font
.GetStyleString().c_str(),
677 m_font
.GetWeightString().c_str());
679 dc
.DrawText(fontInfo
, 5, 5);
683 dc
.SetFont(wxFont(m_font
.GetNativeFontInfo()));
684 fontInfo
.Printf("Native font info: %s", m_font
.GetNativeFontInfo().ToString().GetData());
685 dc
.DrawText(fontInfo
, 5, 5 + dc
.GetCharHeight());
688 // prepare to draw the font
690 dc
.SetTextForeground(m_colour
);
692 // the size of one cell (char + small margin)
693 int w
= dc
.GetCharWidth() + 5,
694 h
= dc
.GetCharHeight() + 4;
696 // the origin for our table
700 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
701 for ( int i
= 1; i
< 8; i
++ )
703 for ( int j
= 0; j
< 32; j
++ )
705 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
709 // draw the lines between them
710 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
715 for ( l
= 0; l
< 8; l
++ )
717 int yl
= y
+ h
*l
- 2;
718 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
722 for ( l
= 0; l
< 33; l
++ )
724 int xl
= x
+ w
*l
- 2;
725 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);