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