File: hworld.tex

package info (click to toggle)
wxwidgets2.8 2.8.10.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 239,052 kB
  • ctags: 289,550
  • sloc: cpp: 1,838,857; xml: 396,717; python: 282,506; ansic: 126,171; makefile: 51,406; sh: 14,581; asm: 299; sql: 258; lex: 194; perl: 139; yacc: 128; pascal: 95; php: 39; lisp: 38; tcl: 24; haskell: 20; java: 18; cs: 18; erlang: 17; ruby: 16; ada: 9; ml: 9; csh: 9
file content (166 lines) | stat: -rw-r--r-- 5,325 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
\section{wxWidgets Hello World sample}\label{helloworld}

As many people have requested a mini-sample to be published here
so that some quick judgment concerning syntax
and basic principles can be made, you can now look at wxWidgets'
"Hello World":

You have to include wxWidgets' header files, of course. This can
be done on a file by file basis (such as \#include "wx/window.h")
or using one global include (\#include "wx/wx.h"). This is
also useful on platforms which support precompiled headers such
as all major compilers on the Windows platform.

\begin{verbatim}
//
// file name: hworld.cpp
//
//   purpose: wxWidgets "Hello world"
//

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
\end{verbatim}

Practically every app should define a new class derived from wxApp.
By overriding wxApp's OnInit() the program can be initialized,
e.g. by creating a new main window. 

\begin{verbatim}
class MyApp: public wxApp
{
    virtual bool OnInit();
};
\end{verbatim}

The main window is created by deriving a class from wxFrame and 
giving it a menu and a status bar in its constructor. Also, any class
that wishes to respond to any "event" (such as mouse clicks or
messages from the menu or a button) must declare an event table 
using the macro below. Finally, the way to react to such events 
must be done in "handlers". In our sample, we react to two menu items, 
one for "Quit" and one for displaying an "About" window. These
handlers should not be virtual.

\begin{verbatim}
class MyFrame: public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

private:
    DECLARE_EVENT_TABLE()
};
\end{verbatim}

In order to be able to react to a menu command, it must be given a unique
identifier such as a const or an enum.

\begin{verbatim}
enum
{
    ID_Quit = 1,
    ID_About,
};
\end{verbatim}

We then proceed to actually implement an event table in which the events
are routed to their respective handler functions in the class MyFrame.
There are predefined macros for routing all common events, ranging from
the selection of a list box entry to a resize event when a user resizes
a window on the screen. If -1 is given as the ID, the given handler will be
invoked for any event of the specified type, so that you could add just
one entry in the event table for all menu commands or all button commands etc.
The origin of the event can still be distinguished in the event handler as
the (only) parameter in an event handler is a reference to a wxEvent object,
which holds various information about the event (such as the ID of and a
pointer to the class, which emitted the event).

\begin{verbatim}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit,  MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
\end{verbatim}

As in all programs there must be a "main" function. Under wxWidgets main is implemented
using this macro, which creates an application instance and starts the program.

\begin{verbatim}
IMPLEMENT_APP(MyApp)
\end{verbatim}

As mentioned above, wxApp::OnInit() is called upon startup and should be
used to initialize the program, maybe showing a "splash screen" and creating
the main window (or several). The frame should get a title bar text ("Hello World")
and a position and start-up size. One frame can also be declared to be the
top window. Returning true indicates a successful initialization.

\begin{verbatim}
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
    frame->Show( true );
    SetTopWindow( frame );
    return true;
}
\end{verbatim}

In the constructor of the main window (or later on) we create a menu with two menu 
items as well as a status bar to be shown at the bottom of the main window. Both have 
to be "announced" to the frame with respective calls.

\begin{verbatim}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
       : wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
    wxMenu *menuFile = new wxMenu;

    menuFile->Append( ID_About, "&About..." );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, "E&xit" );

    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );

    SetMenuBar( menuBar );

    CreateStatusBar();
    SetStatusText( "Welcome to wxWidgets!" );
}
\end{verbatim}

Here are the actual event handlers. MyFrame::OnQuit() closes the main window
by calling Close(). The parameter true indicates that other windows have no veto
power such as after asking "Do you really want to close?". If there is no other 
main window left, the application will quit.

\begin{verbatim}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close( true );
}
\end{verbatim}

MyFrame::OnAbout() will display a small window with some text in it. In this
case a typical "About" window with information about the program.

\begin{verbatim}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox( "This is a wxWidgets' Hello world sample",
                  "About Hello World", wxOK | wxICON_INFORMATION );
}
\end{verbatim}