]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/helloworld.h
Implement hatched/stippled pens/brushes in wxGraphicsContext for Cairo.
[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
2e735263
VZ
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.
928f1a07
FM
16
17First, you have to include wxWidgets' header files, of course. This can
deed8373 18be done on a file by file basis (such as @c wx/window.h) or using one
2e735263
VZ
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:
928f1a07
FM
24
25@code
2e735263 26// wxWidgets "Hello world" Program
928f1a07
FM
27
28// For compilers that support precompilation, includes "wx/wx.h".
2e735263 29#include <wx/wxprec.h>
928f1a07
FM
30
31#ifndef WX_PRECOMP
2e735263 32 #include <wx/wx.h>
928f1a07
FM
33#endif
34@endcode
35
2e735263
VZ
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.
928f1a07
FM
39
40@code
41class MyApp: public wxApp
42{
2e735263 43public:
928f1a07
FM
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".
2e735263
VZ
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.
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
2e735263 86Notice that you don't need to define identifiers for the "About" and "Exit"
928f1a07
FM
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
2e735263 92a window on the screen. If @c wxID_ANY is given as the ID, the given handler will be
928f1a07
FM
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
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
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
2e735263 113wxIMPLEMENT_APP(MyApp)
928f1a07
FM
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 );
928f1a07
FM
127 return true;
128}
129@endcode
130
2e735263 131In the constructor of the main window (or later on) we create a menu with our menu
928f1a07 132items as well as a status bar to be shown at the bottom of the main window. Both have
2e735263 133to be associated with the frame with respective calls.
928f1a07
FM
134
135@code
136MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
2e735263 137 : wxFrame(NULL, wxID_ANY, title, pos, size)
928f1a07
FM
138{
139 wxMenu *menuFile = new wxMenu;
2e735263
VZ
140 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
141 "Help string shown in status bar for this menu item");
928f1a07 142 menuFile->AppendSeparator();
2e735263
VZ
143 menuFile->Append(wxID_EXIT);
144
145 wxMenu *menuHelp = new wxMenu;
146 menuHelp->Append(wxID_ABOUT);
928f1a07
FM
147
148 wxMenuBar *menuBar = new wxMenuBar;
2e735263
VZ
149 menuBar->Append( menuFile, "&File" );
150 menuBar->Append( menuHelp, "&Help" );
928f1a07
FM
151
152 SetMenuBar( menuBar );
153
154 CreateStatusBar();
155 SetStatusText( "Welcome to wxWidgets!" );
156}
157@endcode
158
2e735263
VZ
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.
928f1a07
FM
169
170@code
2e735263 171void MyFrame::OnExit(wxCommandEvent& event)
928f1a07
FM
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
2e735263 181void MyFrame::OnAbout(wxCommandEvent& event)
928f1a07
FM
182{
183 wxMessageBox( "This is a wxWidgets' Hello world sample",
2e735263
VZ
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!");
928f1a07
FM
284}
285@endcode
36c9828f 286
c33e257b 287*/
36c9828f 288