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