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>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // Define a new application type, each program should derive a class from wxApp
34 class MyApp
: public wxApp
37 // override base class virtuals
38 // ----------------------------
40 // this one is called on application startup and is a good place for the app
41 // initialization (doing it here and not in the ctor allows to have an error
42 // return: if OnInit() returns false, the application terminates)
43 virtual bool OnInit();
46 // MyCanvas is a canvas on which we show the font sample
47 class MyCanvas
: public wxWindow
50 MyCanvas( wxWindow
*parent
);
53 // accessors for the frame
54 const wxFont
& GetTextFont() const { return m_font
; }
55 const wxColour
& GetColour() const { return m_colour
; }
56 void SetTextFont(const wxFont
& font
) { m_font
= font
; }
57 void SetColour(const wxColour
& colour
) { m_colour
= colour
; }
60 void OnPaint( wxPaintEvent
&event
);
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame
: public wxFrame
74 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
77 MyCanvas
*GetCanvas() const { return m_canvas
; }
79 // event handlers (these functions should _not_ be virtual)
80 void OnQuit(wxCommandEvent
& event
);
81 void OnAbout(wxCommandEvent
& event
);
82 void OnSelectFont(wxCommandEvent
& event
);
83 void OnCreateFont(wxCommandEvent
& event
);
89 // any class wishing to process wxWindows events must use this macro
93 // A custom font dialog which allows to directly edit wxFont proprieties
94 class MyFontDialog
: public wxDialog
97 MyFontDialog(MyFrame
*frame
);
100 void OnApply(wxCommandEvent
& WXUNUSED(event
)) { DoApply(); }
108 //DECLARE_EVENT_TABLE() TODO
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // IDs for the controls and the menu commands
125 // ----------------------------------------------------------------------------
126 // event tables and other macros for wxWindows
127 // ----------------------------------------------------------------------------
129 // the event tables connect the wxWindows events with the functions (event
130 // handlers) which process them. It can be also done at run-time, but for the
131 // simple menu events like this the static method is much simpler.
132 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
133 EVT_MENU(Font_Quit
, MyFrame
::OnQuit
)
134 EVT_MENU(Font_About
, MyFrame
::OnAbout
)
135 EVT_MENU(Font_Choose
, MyFrame
::OnSelectFont
)
136 EVT_MENU(Font_Create
, MyFrame
::OnCreateFont
)
139 // Create a new application object: this macro will allow wxWindows to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also declares the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
154 // `Main program' equivalent: the program execution "starts" here
157 // Create the main application window
158 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
159 wxPoint(50, 50), wxSize(450, 340));
161 // Show it and tell the application that it's our main window
165 // success: wxApp::OnRun() will be called which will enter the main message
166 // loop and the application will run. If we returned FALSE here, the
167 // application would exit immediately.
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
176 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
177 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
179 // set the frame icon
180 SetIcon(wxICON(mondrian
));
183 wxMenu
*menuFile
= new wxMenu
;
185 menuFile
->Append(Font_About
, "&About...\tCtrl-A", "Show about dialog");
186 menuFile
->AppendSeparator();
187 menuFile
->Append(Font_Quit
, "E&xit\tAlt-X", "Quit this program");
189 wxMenu
*menuFont
= new wxMenu
;
190 menuFont
->Append(Font_Choose
, "&Select font...\tCtrl-F",
191 "Select a standard font");
192 menuFont
->Append(Font_Create
, "&Create font...\tCtrl-C",
193 "Create a custom font");
195 // now append the freshly created menu to the menu bar...
196 wxMenuBar
*menuBar
= new wxMenuBar
;
197 menuBar
->Append(menuFile
, "&File");
198 menuBar
->Append(menuFont
, "F&ont");
200 // ... and attach this menu bar to the frame
203 m_canvas
= new MyCanvas(this);
205 // create a status bar just for fun (by default with 1 pane only)
207 SetStatusText("Welcome to wxWindows!");
213 void MyFrame
::OnCreateFont(wxCommandEvent
& WXUNUSED(event
))
215 MyFontDialog
dialog(this);
217 (void)dialog
.ShowModal();
220 void MyFrame
::OnSelectFont(wxCommandEvent
& WXUNUSED(event
))
223 data
.SetInitialFont(m_canvas
->GetTextFont());
224 data
.SetColour(m_canvas
->GetColour());
226 wxFontDialog
dialog(this, &data
);
227 if ( dialog
.ShowModal() == wxID_OK
)
229 wxFontData retData
= dialog
.GetFontData();
230 m_canvas
->SetTextFont(retData
.GetChosenFont());
231 m_canvas
->SetColour(retData
.GetColour());
236 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
238 // TRUE is to force the frame to close
242 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
244 wxMessageBox("wxWindows font demo.", "About Font",
245 wxOK
| wxICON_INFORMATION
, this);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 BEGIN_EVENT_TABLE(MyCanvas
, wxWindow
)
254 EVT_PAINT(MyCanvas
::OnPaint
)
257 MyCanvas
::MyCanvas( wxWindow
*parent
)
258 : wxWindow( parent
, -1 )
260 m_font
= *wxNORMAL_FONT
;
264 MyCanvas
::~MyCanvas()
268 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
274 dc
.SetBackground(wxBrush("white", wxSOLID
));
277 // output the font name/info
279 fontInfo
.Printf("Font family is '%s', style '%s', weight '%s'",
280 m_font
.GetFamilyString().c_str(),
281 m_font
.GetStyleString().c_str(),
282 m_font
.GetWeightString().c_str());
284 dc
.DrawText(fontInfo
, 5, 5);
286 // prepare to draw the font
288 dc
.SetTextForeground(m_colour
);
290 // the size of one cell (char + small margin)
291 int w
= dc
.GetCharWidth() + 5,
292 h
= dc
.GetCharHeight() + 4;
294 // the origin for our table
298 // print all font symbols from 32 to 256 in 7 rows of 32 chars each
299 for ( int i
= 1; i
< 8; i
++ )
301 for ( int j
= 0; j
< 32; j
++ )
303 dc
.DrawText(char(32*i
+ j
), x
+ w
*j
, y
+ h
*i
);
307 // draw the lines between them
308 dc
.SetPen(wxPen(wxColour("blue"), 1, wxSOLID
));
313 for ( l
= 0; l
< 8; l
++ )
315 int yl
= y
+ h
*l
- 2;
316 dc
.DrawLine(x
- 2, yl
, x
+ 32*w
- 2, yl
);
320 for ( l
= 0; l
< 33; l
++ )
322 int xl
= x
+ w
*l
- 2;
323 dc
.DrawLine(xl
, y
, xl
, y
+ 7*h
- 2);
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 MyFontDialog
::MyFontDialog(MyFrame
*frame
)
332 : wxDialog(frame
, -1, wxString("Edit font attributes"))
334 m_canvas
= frame
->GetCanvas();
337 wxSize sizeBtn
= wxButton
::GetDefaultSize();
341 // position and size the dialog
342 SetClientSize(4*sizeBtn
.x
, 10*sizeBtn
.y
);
346 void MyFontDialog
::DoApply()
348 wxFont font
; //(size, family, style, weight, underlined, face, encoding);
351 wxLogError("Font creation failed.");
355 m_canvas
->SetTextFont(font
);