]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/hworld.tex
1. added wxTE_AUTO_URL style and support for it under Win32 (and brief
[wxWidgets.git] / docs / latex / wx / hworld.tex
CommitLineData
ccaaf5b0
RR
1\section{wxWindows "Hello World"}\label{helloworld}
2
3As many people have requested a mini-sample to be published here
f6bcfd97 4so that some quick judgments concerning syntax
ccaaf5b0
RR
5and basic principles can be made, you can now look at wxWindows'
6"Hello World":
7
f6bcfd97 8You have to include wxWindows' header files, of course. This can
605d715d
VS
9be done on a file by file basis (such as \#include "wx/window.h")
10or using one global include (\#include "wx/wx.h"). This is
ccaaf5b0
RR
11also useful on platforms which support precompiled headers such
12as all major compilers on the Windows platform.
13
14\begin{verbatim}
15//
16// file name: hworld.cpp
17//
18// purpose: wxWindows "Hello world"
19//
20
21// For compilers that support precompilation, includes "wx/wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
30#endif
31\end{verbatim}
32
33Practically every app should define a new class derived from wxApp.
34By overriding wxApp's OnInit() the program can be initialized,
35e.g. by creating a new main window.
36
37\begin{verbatim}
38class MyApp: public wxApp
39{
40 virtual bool OnInit();
41};
42\end{verbatim}
43
44The main window is created by deriving a class from wxFrame and
45giving it a menu and a status bar in its constructor. Also, any class
46that wishes to respond to any "event" (such as mouse clicks or
47messages from the menu or a button) must declare an event table
48using the macro below. Finally, the way to react to such events
49must be done in "handlers". In our sample, we react to two menu items,
50one for "Quit" and one for displaying an "About" window. These
51handlers should not be virtual.
52
53\begin{verbatim}
54class MyFrame: public wxFrame
55{
56public:
57 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
58
59 void OnQuit(wxCommandEvent& event);
60 void OnAbout(wxCommandEvent& event);
61
62private:
63 DECLARE_EVENT_TABLE()
64};
65\end{verbatim}
66
67In order to be able to react to a menu command, it must be given a unique
68identifier such as a const or an enum.
69
70\begin{verbatim}
71enum
72{
73 ID_Quit = 1,
74 ID_About,
75};
76\end{verbatim}
77
f6bcfd97 78We then proceed to actually implement an event table in which the events
ccaaf5b0
RR
79are routed to their respective handler functions in the class MyFrame.
80There are predefined macros for routing all common events, ranging from
81the selection of a list box entry to a resize event when a user resizes
82a window on the screen. If -1 is given as the ID, the given handler will be
83invoked for any event of the specified type, so that you could add just
84one entry in the event table for all menu commands or all button commands etc.
85The origin of the event can still be distinguished in the event handler as
86the (only) parameter in an event handler is a reference to a wxEvent object,
87which holds various information about the event (such as the ID of and a
88pointer to the class, which emitted the event).
89
90\begin{verbatim}
91BEGIN_EVENT_TABLE(MyFrame, wxFrame)
92 EVT_MENU(ID_Quit, MyFrame::OnQuit)
93 EVT_MENU(ID_About, MyFrame::OnAbout)
94END_EVENT_TABLE()
95\end{verbatim}
96
97As in all programs there must be a "main" function. Under wxWindows main is implemented
98using this macro, which creates an application instance and starts the program.
99
100\begin{verbatim}
101IMPLEMENT_APP(MyApp)
102\end{verbatim}
103
f6bcfd97 104As mentioned above, wxApp::OnInit() is called upon startup and should be
ccaaf5b0
RR
105used to initialize the program, maybe showing a "splash screen" and creating
106the main window (or several). The frame should get a title bar text ("Hello World")
107and a position and start-up size. One frame can also be declared to be the
f6bcfd97 108top window. Returning TRUE indicates a successful initialization.
ccaaf5b0
RR
109
110\begin{verbatim}
111bool MyApp::OnInit()
112{
113 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
114 frame->Show( TRUE );
115 SetTopWindow( frame );
116 return TRUE;
117}
118\end{verbatim}
119
120In the constructor of the main window (or later on) we create a menu with two menu
121items as well as a status bar to be shown at the bottom of the main window. Both have
122to be "announced" to the frame with respective calls.
123
124\begin{verbatim}
125MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
126 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
127{
128 wxMenu *menuFile = new wxMenu;
129
130 menuFile->Append( ID_About, "&About..." );
131 menuFile->AppendSeparator();
132 menuFile->Append( ID_Quit, "E&xit" );
133
134 wxMenuBar *menuBar = new wxMenuBar;
135 menuBar->Append( menuFile, "&File" );
136
137 SetMenuBar( menuBar );
138
139 CreateStatusBar();
140 SetStatusText( "Welcome to wxWindows!" );
141}
142\end{verbatim}
143
144Here are the actual event handlers. MyFrame::OnQuit() closes the main window
f6bcfd97 145by calling Close(). The parameter TRUE indicates that other windows have no veto
ccaaf5b0 146power such as after asking "Do you really want to close?". If there is no other
f6bcfd97 147main window left, the application will quit.
ccaaf5b0
RR
148
149\begin{verbatim}
150void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
151{
152 Close( TRUE );
153}
154\end{verbatim}
155
156MyFrame::OnAbout() will display a small window with some text in it. In this
157case a typical "About" window with information about the program.
158
159\begin{verbatim}
160void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
161{
f6bcfd97 162 wxMessageBox( "This is a wxWindows' Hello world sample",
ccaaf5b0
RR
163 "About Hello World", wxOK | wxICON_INFORMATION );
164}
165\end{verbatim}
166