]>
git.saurik.com Git - wxWidgets.git/blob - samples/font/font.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont demo
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 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/fontdlg.h>
28 #include <wx/fontenum.h>
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // Define a new application type, each program should derive a class from wxApp
35 class MyApp
: public wxApp
38 // override base class virtuals
39 // ----------------------------
41 // this one is called on application startup and is a good place for the app
42 // initialization (doing it here and not in the ctor allows to have an error
43 // return: if OnInit() returns false, the application terminates)
44 virtual bool OnInit();
47 // MyCanvas is a canvas on which we show the font sample
48 class MyCanvas
: public wxWindow
51 MyCanvas( wxWindow
*parent
);
54 // accessors for the frame
55 const wxFont
& GetTextFont() const { return m_font
; }
56 const wxColour
& GetColour() const { return m_colour
; }
57 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
58 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
61 void OnPaint( wxPaintEvent
&event
);
70 // Define a new frame type: this is going to be our main frame
71 class MyFrame
: public wxFrame
75 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
78 MyCanvas
*GetCanvas() const { return m_canvas
; }
80 // event handlers (these functions should _not_ be virtual)
81 void OnQuit(wxCommandEvent
& event
);
82 void OnAbout(wxCommandEvent
& event
);
83 void OnSelectFont(wxCommandEvent
& event
);
84 void OnCreateFont(wxCommandEvent
& event
);
85 void OnEnumerateFamilies(wxCommandEvent
& WXUNUSED(event
))
86 { DoEnumerateFamilies(FALSE
); }
87 void OnEnumerateFixedFamilies(wxCommandEvent
& WXUNUSED(event
))
88 { DoEnumerateFamilies(TRUE
); }
89 void OnEnumerateEncodings(wxCommandEvent
& event
);
92 void DoEnumerateFamilies(bool fixedWidthOnly
);
97 // any class wishing to process wxWindows events must use this macro
101 // A custom font dialog which allows to directly edit wxFont proprieties
102 class MyFontDialog
: public wxDialog
105 MyFontDialog(MyFrame
*frame
);
108 void OnApply(wxCommandEvent
& WXUNUSED(event
)) { DoApply(); }
116 //DECLARE_EVENT_TABLE() TODO
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 // IDs for the controls and the menu commands
132 Font_EnumFixedFamilies
,
137 // ----------------------------------------------------------------------------
138 // event tables and other macros for wxWindows
139 // ----------------------------------------------------------------------------
141 // the event tables connect the wxWindows events with the functions (event
142 // handlers) which process them. It can be also done at run-time, but for the
143 // simple menu events like this the static method is much simpler.
144 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
145 EVT_MENU(Font_Quit
, MyFrame::OnQuit
)
146 EVT_MENU(Font_About
, MyFrame::OnAbout
)
147 EVT_MENU(Font_Choose
, MyFrame::OnSelectFont
)
148 EVT_MENU(Font_Create
, MyFrame::OnCreateFont
)
149 EVT_MENU(Font_EnumFamilies
, MyFrame::OnEnumerateFamilies
)
150 EVT_MENU(Font_EnumFixedFamilies
, MyFrame::OnEnumerateFixedFamilies
)
151 EVT_MENU(Font_EnumEncodings
, MyFrame::OnEnumerateEncodings
)
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
)
195 wxMenu
*menuFile
= new wxMenu
;
197 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
198 menuFile
->AppendSeparator();
199 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
201 wxMenu
*menuFont
= new wxMenu
;
202 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-S",
203 "Select a standard font");
204 menuFont
->Append(Font_Create
, "&Create font...\tCtrl-C",
205 "Create a custom font");
206 menuFont
->AppendSeparator();
207 menuFont
->Append(Font_EnumFamilies
, "Enumerate font &families\tCtrl-F");
208 menuFont
->Append(Font_EnumFixedFamilies
,
209 "Enumerate f&ixed font families\tCtrl-I");
210 menuFont
->Append(Font_EnumEncodings
,
211 "Enumerate &encodings\tCtrl-E");
213 // now append the freshly created menu to the menu bar...
214 wxMenuBar
*menuBar
= new wxMenuBar
;
215 menuBar
->Append(menuFile
, "&File");
216 menuBar
->Append(menuFont
, "F&ont");
218 // ... and attach this menu bar to the frame
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!");
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
)
265 class MyFontEnumerator
: public wxFontEnumerator
268 MyFontEnumerator() { m_n
= 0; }
270 const wxString
& GetText() const { return m_text
; }
273 virtual bool OnFontFamily(const wxString
& family
)
276 text
.Printf("Font family %d: %s\n", ++m_n
, family
.c_str());
288 fontEnumerator
.EnumerateFamilies(fixedWidthOnly
);
290 wxLogMessage("Enumerating %s font families:\n%s",
291 fixedWidthOnly
? "fixed width" : "all",
292 fontEnumerator
.GetText().c_str());
295 void MyFrame::OnCreateFont(wxCommandEvent
& WXUNUSED(event
))
297 MyFontDialog
dialog(this);
299 (void)dialog
.ShowModal();
302 void MyFrame::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
305 data
.SetInitialFont(m_canvas
->GetTextFont());
306 data
.SetColour(m_canvas
->GetColour());
308 wxFontDialog
dialog(this, &data
);
309 if ( dialog
.ShowModal() == wxID_OK
)
311 wxFontData retData
= dialog
.GetFontData();
312 m_canvas
->SetTextFont(retData
.GetChosenFont());
313 m_canvas
->SetColour(retData
.GetColour());
318 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
320 // TRUE is to force the frame to close
324 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
326 wxMessageBox("wxWindows font demo.", "About Font",
327 wxOK
| wxICON_INFORMATION
, this);
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
336 EVT_PAINT(MyCanvas::OnPaint
)
339 MyCanvas::MyCanvas( wxWindow
*parent
)
340 : wxWindow( parent
, -1 )
342 m_font
= *wxNORMAL_FONT
;
346 MyCanvas::~MyCanvas()
350 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
356 dc
.SetBackground(wxBrush("white", wxSOLID
));
359 // output the font name/info
361 fontInfo
.Printf("Font family is '%s', style '%s', weight '%s'",
362 m_font
.GetFamilyString().c_str(),
363 m_font
.GetStyleString().c_str(),
364 m_font
.GetWeightString().c_str());
366 dc
.DrawText(fontInfo
, 5, 5);
368 // prepare to draw the font
370 dc
.SetTextForeground(m_colour
);
372 // the size of one cell (char + small margin)
373 int w
= dc
.GetCharWidth() + 5,
374 h
= dc
.GetCharHeight() + 4;
376 // the origin for our table
380 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
381 for ( int i
= 1; i
< 8; i
++ )
383 for ( int j
= 0; j
< 32; j
++ )
385 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
389 // draw the lines between them
390 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
395 for ( l
= 0; l
< 8; l
++ )
397 int yl
= y
+ h
*l
- 2;
398 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
402 for ( l
= 0; l
< 33; l
++ )
404 int xl
= x
+ w
*l
- 2;
405 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 MyFontDialog::MyFontDialog(MyFrame
*frame
)
414 : wxDialog(frame
, -1, wxString("Edit font attributes"))
416 m_canvas
= frame
->GetCanvas();
419 wxSize sizeBtn
= wxButton::GetDefaultSize();
423 // position and size the dialog
424 SetClientSize(4*sizeBtn
.x
, 10*sizeBtn
.y
);
428 void MyFontDialog::DoApply()
430 wxFont font
; //(size, family, style, weight, underlined, face, encoding);
433 wxLogError("Font creation failed.");
437 m_canvas
->SetTextFont(font
);