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>
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // Define a new application type, each program should derive a class from wxApp
36 class MyApp
: public wxApp
39 // override base class virtuals
40 // ----------------------------
42 // this one is called on application startup and is a good place for the app
43 // initialization (doing it here and not in the ctor allows to have an error
44 // return: if OnInit() returns false, the application terminates)
45 virtual bool OnInit();
48 // MyCanvas is a canvas on which we show the font sample
49 class MyCanvas
: public wxWindow
52 MyCanvas( wxWindow
*parent
);
55 // accessors for the frame
56 const wxFont
& GetTextFont() const { return m_font
; }
57 const wxColour
& GetColour() const { return m_colour
; }
58 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
59 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
62 void OnPaint( wxPaintEvent
&event
);
71 // Define a new frame type: this is going to be our main frame
72 class MyFrame
: public wxFrame
76 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
79 MyCanvas
*GetCanvas() const { return m_canvas
; }
81 // event handlers (these functions should _not_ be virtual)
82 void OnQuit(wxCommandEvent
& event
);
83 void OnAbout(wxCommandEvent
& event
);
84 void OnSelectFont(wxCommandEvent
& event
);
85 void OnEnumerateFamiliesForEncoding(wxCommandEvent
& event
);
86 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
87 { DoEnumerateFamilies(FALSE
); }
88 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
89 { DoEnumerateFamilies(TRUE
); }
90 void OnEnumerateEncodings(wxCommandEvent
& event
);
92 void OnSize(wxSizeEvent
& event
);
95 void DoEnumerateFamilies(bool fixedWidthOnly
,
96 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
);
98 void DoChangeFont(const wxFont
& font
, const wxColour
& col
= wxNullColour
);
100 void Resize(const wxSize
& size
, const wxFont
& font
= wxNullFont
);
102 wxTextCtrl
*m_textctrl
;
106 // any class wishing to process wxWindows events must use this macro
107 DECLARE_EVENT_TABLE()
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 // IDs for the controls and the menu commands
121 Font_EnumFamiliesForEncoding
,
123 Font_EnumFixedFamilies
,
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWindows
130 // ----------------------------------------------------------------------------
132 // the event tables connect the wxWindows events with the functions (event
133 // handlers) which process them. It can be also done at run-time, but for the
134 // simple menu events like this the static method is much simpler.
135 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
136 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
137 EVT_MENU(Font_About
, MyFrame::OnAbout
)
138 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
139 EVT_MENU(Font_EnumFamiliesForEncoding
, MyFrame::OnEnumerateFamiliesForEncoding
)
140 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
141 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
142 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
144 EVT_SIZE(MyFrame::OnSize
)
147 // Create a new application object: this macro will allow wxWindows to create
148 // the application object during program execution (it's better than using a
149 // static object for many reasons) and also declares the accessor function
150 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
159 // the application class
160 // ----------------------------------------------------------------------------
162 // `Main program' equivalent: the program execution "starts" here
165 // Create the main application window
166 MyFrame
*frame
= new MyFrame("Font wxWindows demo",
167 wxPoint(50, 50), wxSize(450, 340));
169 // Show it and tell the application that it's our main window
173 // success: wxApp::OnRun() will be called which will enter the main message
174 // loop and the application will run. If we returned FALSE here, the
175 // application would exit immediately.
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
184 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
185 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
), m_textctrl(NULL
)
188 wxMenu
*menuFile
= new wxMenu
;
190 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
191 menuFile
->AppendSeparator();
192 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
194 wxMenu
*menuFont
= new wxMenu
;
195 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-S",
196 "Select a standard font");
197 menuFont
->AppendSeparator();
198 menuFont
->Append(Font_EnumFamilies
, "Enumerate font &families\tCtrl-F");
199 menuFont
->Append(Font_EnumFixedFamilies
,
200 "Enumerate f&ixed font families\tCtrl-I");
201 menuFont
->Append(Font_EnumEncodings
,
202 "Enumerate &encodings\tCtrl-E");
203 menuFont
->Append(Font_EnumFamiliesForEncoding
,
204 "Find font for en&coding...\tCtrl-C",
205 "Find font families for given encoding");
207 // now append the freshly created menu to the menu bar...
208 wxMenuBar
*menuBar
= new wxMenuBar
;
209 menuBar
->Append(menuFile
, "&File");
210 menuBar
->Append(menuFont
, "F&ont");
212 // ... and attach this menu bar to the frame
215 m_textctrl
= new wxTextCtrl(this, -1,
216 "Paste text here to see how it looks\n"
217 "like in the given font",
218 wxDefaultPosition
, wxDefaultSize
,
221 m_canvas
= new MyCanvas(this);
223 // create a status bar just for fun (by default with 1 pane only)
225 SetStatusText("Welcome to wxWindows font demo!");
230 void MyFrame::OnEnumerateEncodings(wxCommandEvent
& WXUNUSED(event
))
232 class MyEncodingEnumerator
: public wxFontEnumerator
235 MyEncodingEnumerator() { m_n
= 0; }
237 const wxString
& GetText() const { return m_text
; }
240 virtual bool OnFontEncoding(const wxString
& family
,
241 const wxString
& encoding
)
244 text
.Printf("Encoding %d: %s (available in family '%s')\n",
245 ++m_n
, encoding
.c_str(), family
.c_str());
257 fontEnumerator
.EnumerateEncodings();
259 wxLogMessage("Enumerating all available encodings:\n%s",
260 fontEnumerator
.GetText().c_str());
263 void MyFrame::DoEnumerateFamilies(bool fixedWidthOnly
, wxFontEncoding encoding
)
265 class MyFontEnumerator
: public wxFontEnumerator
268 bool GotAny() const { return !m_facenames
.IsEmpty(); }
270 const wxArrayString
& GetFacenames() const { return m_facenames
; }
273 virtual bool OnFontFamily(const wxString
& family
)
275 m_facenames
.Add(family
);
281 wxArrayString m_facenames
;
284 fontEnumerator
.EnumerateFamilies(encoding
, fixedWidthOnly
);
286 if ( fontEnumerator
.GotAny() )
288 int n
, nFacenames
= fontEnumerator
.GetFacenames().GetCount();
289 wxLogStatus(this, "Found %d %sfonts",
290 nFacenames
, fixedWidthOnly
? "fixed width " : "");
292 wxString
*facenames
= new wxString
[nFacenames
];
293 for ( n
= 0; n
< nFacenames
; n
++ )
294 facenames
[n
] = fontEnumerator
.GetFacenames().Item(n
);
296 n
= wxGetSingleChoiceIndex("Choose a facename", "Font demo",
297 nFacenames
, facenames
, this);
300 wxFont
font(14, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
301 wxFONTWEIGHT_NORMAL
, FALSE
, facenames
[n
], encoding
);
310 wxLogWarning("No such fonts found.");
314 void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent
& WXUNUSED(event
))
316 static wxFontEncoding encodings
[] =
318 wxFONTENCODING_ISO8859_1
,
319 wxFONTENCODING_ISO8859_2
,
320 wxFONTENCODING_ISO8859_5
,
321 wxFONTENCODING_ISO8859_7
,
322 wxFONTENCODING_ISO8859_15
,
324 wxFONTENCODING_CP1250
,
325 wxFONTENCODING_CP1251
,
326 wxFONTENCODING_CP1252
,
329 static const char *encodingNames
[] =
331 "West European (Latin 1)",
332 "Central European (Latin 2)",
333 "Cyrillic (Latin 5)",
335 "West European new (Latin 0)",
342 int n
= wxGetSingleChoiceIndex("Choose an encoding", "Font demo",
343 WXSIZEOF(encodingNames
),
344 (char **)encodingNames
,
349 DoEnumerateFamilies(FALSE
, encodings
[n
]);
353 void MyFrame::DoChangeFont(const wxFont
& font
, const wxColour
& col
)
355 Resize(GetSize(), font
);
357 m_canvas
->SetTextFont(font
);
359 m_canvas
->SetColour(col
);
362 m_textctrl
->SetFont(font
);
364 m_textctrl
->SetForegroundColour(col
);
367 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
370 data
.SetInitialFont(m_canvas
->GetTextFont());
371 data
.SetColour(m_canvas
->GetColour());
373 wxFontDialog
dialog(this, &data
);
374 if ( dialog
.ShowModal() == wxID_OK
)
376 wxFontData retData
= dialog
.GetFontData();
377 wxFont font
= retData
.GetChosenFont();
378 wxColour colour
= retData
.GetColour();
380 DoChangeFont(font
, colour
);
384 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
386 // TRUE is to force the frame to close
390 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
392 wxMessageBox("wxWindows font demo\n"
393 "(c) 1999 Vadim Zeitlin",
395 wxOK
| wxICON_INFORMATION
, this);
398 void MyFrame::OnSize(wxSizeEvent
& event
)
400 wxSize size
= event
.GetSize();
407 void MyFrame::Resize(const wxSize
& size
, const wxFont
& font
)
418 h
= 4*dc
.GetCharHeight() + 4;
422 h
= m_textctrl
->GetSize().y
;
425 m_textctrl
->SetSize(0, 0, size
.x
, h
);
426 m_canvas
->SetSize(0, h
, size
.x
, size
.y
- h
);
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
434 EVT_PAINT(MyCanvas::OnPaint
)
437 MyCanvas::MyCanvas( wxWindow
*parent
)
438 : wxWindow( parent
, -1 )
440 m_font
= *wxNORMAL_FONT
;
444 MyCanvas::~MyCanvas()
448 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
454 dc
.SetBackground(wxBrush("white", wxSOLID
));
457 // output the font name/info
459 fontInfo
.Printf("Font family is '%s', style '%s', weight '%s'",
460 m_font
.GetFamilyString().c_str(),
461 m_font
.GetStyleString().c_str(),
462 m_font
.GetWeightString().c_str());
464 dc
.DrawText(fontInfo
, 5, 5);
466 // prepare to draw the font
468 dc
.SetTextForeground(m_colour
);
470 // the size of one cell (char + small margin)
471 int w
= dc
.GetCharWidth() + 5,
472 h
= dc
.GetCharHeight() + 4;
474 // the origin for our table
478 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
479 for ( int i
= 1; i
< 8; i
++ )
481 for ( int j
= 0; j
< 32; j
++ )
483 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
487 // draw the lines between them
488 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
493 for ( l
= 0; l
< 8; l
++ )
495 int yl
= y
+ h
*l
- 2;
496 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
500 for ( l
= 0; l
< 33; l
++ )
502 int xl
= x
+ w
*l
- 2;
503 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);