]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/doxygen/overviews/helloworld.h
Implement hatched/stippled pens/brushes in wxGraphicsContext for Cairo.
[wxWidgets.git] / docs / doxygen / overviews / helloworld.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: helloworld.h
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows licence
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10
11@page overview_helloworld Hello World Example
12
13This page shows a very simple wxWidgets program that can be used as a skeleton
14for your own code. While it does nothing very useful, it introduces a couple of
15important concepts and explains how to write a working wxWidgets application.
16
17First, you have to include wxWidgets' header files, of course. This can
18be done on a file by file basis (such as @c wx/window.h) or using one
19global include (@c wx/wx.h) which includes most of the commonly needed headers
20(although not all of them as there are simply too many wxWidgets headers to
21pull in all of them). For the platforms with support for precompiled headers,
22as indicated by @c WX_PRECOMP, this global header is already included by @c
23wx/wxprec.h so we only include it for the other ones:
24
25@code
26// wxWidgets "Hello world" Program
27
28// For compilers that support precompilation, includes "wx/wx.h".
29#include <wx/wxprec.h>
30
31#ifndef WX_PRECOMP
32 #include <wx/wx.h>
33#endif
34@endcode
35
36Practically every app should define a new class derived from wxApp. By
37overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
38by creating a new main window.
39
40@code
41class MyApp: public wxApp
42{
43public:
44 virtual bool OnInit();
45};
46@endcode
47
48The main window is created by deriving a class from wxFrame and
49giving it a menu and a status bar in its constructor. Also, any class
50that wishes to respond to any "event" (such as mouse clicks or
51messages from the menu or a button) must declare an event table
52using the macro below.
53
54Finally, the way to react to such events must be done in "handlers".
55In our sample, we react to three menu items, one for our custom menu
56command and two for the standard "Exit" and "About" commands (any program
57should normally implement the latter two). Notice that these handlers
58don't need to be neither virtual nor public.
59
60@code
61class MyFrame: public wxFrame
62{
63public:
64 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
65
66private:
67 void OnHello(wxCommandEvent& event);
68 void OnExit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71 wxDECLARE_EVENT_TABLE();
72};
73@endcode
74
75In order to be able to react to a menu command, it must be given a unique
76identifier which can be defined as a const variable or an enum element. The
77latter is often used because typically many such constants will be needed:
78
79@code
80enum
81{
82 ID_Hello = 1
83};
84@endcode
85
86Notice that you don't need to define identifiers for the "About" and "Exit"
87We then proceed to actually implement an event table in which the events
88are routed to their respective handler functions in the class MyFrame.
89
90There are predefined macros for routing all common events, ranging from
91the selection of a list box entry to a resize event when a user resizes
92a window on the screen. If @c wxID_ANY is given as the ID, the given handler will be
93invoked for any event of the specified type, so that you could add just
94one entry in the event table for all menu commands or all button commands etc.
95
96The origin of the event can still be distinguished in the event handler as
97the (only) parameter in an event handler is a reference to a wxEvent object,
98which holds various information about the event (such as the ID of and a
99pointer to the class, which emitted the event).
100
101@code
102wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
103 EVT_MENU(ID_Hello, MyFrame::OnHello)
104 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
105 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
106wxEND_EVENT_TABLE()
107@endcode
108
109As in all programs there must be a "main" function. Under wxWidgets main is implemented
110using this macro, which creates an application instance and starts the program.
111
112@code
113wxIMPLEMENT_APP(MyApp)
114@endcode
115
116As mentioned above, wxApp::OnInit() is called upon startup and should be
117used to initialize the program, maybe showing a "splash screen" and creating
118the main window (or several). The frame should get a title bar text ("Hello World")
119and a position and start-up size. One frame can also be declared to be the
120top window. Returning @true indicates a successful initialization.
121
122@code
123bool MyApp::OnInit()
124{
125 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
126 frame->Show( true );
127 return true;
128}
129@endcode
130
131In the constructor of the main window (or later on) we create a menu with our menu
132items as well as a status bar to be shown at the bottom of the main window. Both have
133to be associated with the frame with respective calls.
134
135@code
136MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
137 : wxFrame(NULL, wxID_ANY, title, pos, size)
138{
139 wxMenu *menuFile = new wxMenu;
140 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
141 "Help string shown in status bar for this menu item");
142 menuFile->AppendSeparator();
143 menuFile->Append(wxID_EXIT);
144
145 wxMenu *menuHelp = new wxMenu;
146 menuHelp->Append(wxID_ABOUT);
147
148 wxMenuBar *menuBar = new wxMenuBar;
149 menuBar->Append( menuFile, "&File" );
150 menuBar->Append( menuHelp, "&Help" );
151
152 SetMenuBar( menuBar );
153
154 CreateStatusBar();
155 SetStatusText( "Welcome to wxWidgets!" );
156}
157@endcode
158
159Notice that we don't need to specify the labels for the standard menu items
160@c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
161translated) labels and also standard accelerators correct for the current
162platform making your program behaviour more native. For this reason you should
163prefer reusing the standard ids (see @ref page_stockitems) if possible.
164
165Here are the standard event handlers implementations. MyFrame::OnExit() closes
166the main window by calling Close(). The parameter @true indicates that other
167windows have no veto power such as after asking "Do you really want to close?".
168If there is no other main window left, the application will quit.
169
170@code
171void MyFrame::OnExit(wxCommandEvent& event)
172{
173 Close( true );
174}
175@endcode
176
177MyFrame::OnAbout() will display a small window with some text in it. In this
178case a typical "About" window with information about the program.
179
180@code
181void MyFrame::OnAbout(wxCommandEvent& event)
182{
183 wxMessageBox( "This is a wxWidgets' Hello world sample",
184 "About Hello World", wxOK | wxICON_INFORMATION );
185}
186@endcode
187
188The implementation of custom menu command handler may perform whatever task
189your program needs to do, in this case we will simply show a message from it as
190befits a hello world example:
191@code
192void MyFrame::OnHello(wxCommandEvent& event)
193{
194 wxLogMessage("Hello world from wxWidgets!");
195}
196@endcode
197
198Here is the entire program that can be copied and pasted:
199@code
200// wxWidgets "Hello world" Program
201
202// For compilers that support precompilation, includes "wx/wx.h".
203#include <wx/wxprec.h>
204
205#ifndef WX_PRECOMP
206 #include <wx/wx.h>
207#endif
208
209class MyApp: public wxApp
210{
211public:
212 virtual bool OnInit();
213};
214
215class MyFrame: public wxFrame
216{
217public:
218 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
219
220private:
221 void OnHello(wxCommandEvent& event);
222 void OnExit(wxCommandEvent& event);
223 void OnAbout(wxCommandEvent& event);
224
225 wxDECLARE_EVENT_TABLE();
226};
227
228enum
229{
230 ID_Hello = 1
231};
232
233wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
234 EVT_MENU(ID_Hello, MyFrame::OnHello)
235 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
236 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
237wxEND_EVENT_TABLE()
238
239wxIMPLEMENT_APP(MyApp);
240
241bool MyApp::OnInit()
242{
243 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
244 frame->Show( true );
245 return true;
246}
247
248MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
249 : wxFrame(NULL, wxID_ANY, title, pos, size)
250{
251 wxMenu *menuFile = new wxMenu;
252 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
253 "Help string shown in status bar for this menu item");
254 menuFile->AppendSeparator();
255 menuFile->Append(wxID_EXIT);
256
257 wxMenu *menuHelp = new wxMenu;
258 menuHelp->Append(wxID_ABOUT);
259
260 wxMenuBar *menuBar = new wxMenuBar;
261 menuBar->Append( menuFile, "&File" );
262 menuBar->Append( menuHelp, "&Help" );
263
264 SetMenuBar( menuBar );
265
266 CreateStatusBar();
267 SetStatusText( "Welcome to wxWidgets!" );
268}
269
270void MyFrame::OnExit(wxCommandEvent& event)
271{
272 Close( true );
273}
274
275void MyFrame::OnAbout(wxCommandEvent& event)
276{
277 wxMessageBox( "This is a wxWidgets' Hello world sample",
278 "About Hello World", wxOK | wxICON_INFORMATION );
279}
280
281void MyFrame::OnHello(wxCommandEvent& event)
282{
283 wxLogMessage("Hello world from wxWidgets!");
284}
285@endcode
286
287*/
288