]> git.saurik.com Git - wxWidgets.git/blame - samples/event/event.cpp
update/reorganize events overview and changed links to it to reflect the fact that...
[wxWidgets.git] / samples / event / event.cpp
CommitLineData
6164f93c
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: event.cpp
be5a51fb 3// Purpose: wxWidgets sample demonstrating different event usage
6164f93c
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 31.01.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
6164f93c
VZ
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers (this file is usually all you
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers)
6164f93c
VZ
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
4ec2df6c
VZ
33// ----------------------------------------------------------------------------
34// event constants
35// ----------------------------------------------------------------------------
36
862c917d
VZ
37// define a custom event type (we don't need a separate declaration here but
38// usually you would use a matching wxDECLARE_LOCAL_EVENT in a header)
39wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent)
4ec2df6c
VZ
40
41// it may also be convenient to define an event table macro for this event type
42#define EVT_MY_CUSTOM_COMMAND(id, fn) \
43 DECLARE_EVENT_TABLE_ENTRY( \
07850a49 44 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
79935f61 45 wxCommandEventHandler(fn), \
4ec2df6c
VZ
46 (wxObject *) NULL \
47 ),
48
6164f93c
VZ
49// ----------------------------------------------------------------------------
50// private classes
51// ----------------------------------------------------------------------------
52
53// Define a new application type, each program should derive a class from wxApp
54class MyApp : public wxApp
55{
56public:
57 // override base class virtuals
58 // ----------------------------
59
60 // this one is called on application startup and is a good place for the app
61 // initialization (doing it here and not in the ctor allows to have an error
62 // return: if OnInit() returns false, the application terminates)
63 virtual bool OnInit();
64};
65
66// Define a new frame type: this is going to be our main frame
67class MyFrame : public wxFrame
68{
69public:
70 // ctor(s)
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72 virtual ~MyFrame();
73
74 void OnQuit(wxCommandEvent& event);
75 void OnAbout(wxCommandEvent& event);
76 void OnConnect(wxCommandEvent& event);
77 void OnDynamic(wxCommandEvent& event);
78 void OnPushEventHandler(wxCommandEvent& event);
79 void OnPopEventHandler(wxCommandEvent& event);
80 void OnTest(wxCommandEvent& event);
81
4ec2df6c
VZ
82 void OnFireCustom(wxCommandEvent& event);
83 void OnProcessCustom(wxCommandEvent& event);
84
6164f93c
VZ
85 void OnUpdateUIPop(wxUpdateUIEvent& event);
86
87protected:
88 // number of pushed event handlers
b143cf70 89 unsigned m_nPush;
6164f93c
VZ
90
91private:
be5a51fb 92 // any class wishing to process wxWidgets events must use this macro
6164f93c
VZ
93 DECLARE_EVENT_TABLE()
94};
95
96// Define a custom event handler
97class MyEvtHandler : public wxEvtHandler
98{
99public:
100 MyEvtHandler(size_t level) { m_level = level; }
101
102 void OnTest(wxCommandEvent& event)
103 {
104 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
105
106 // if we don't skip the event, the other event handlers won't get it:
107 // try commenting out this line and see what changes
108 event.Skip();
109 }
110
111private:
b143cf70 112 unsigned m_level;
6164f93c
VZ
113
114 DECLARE_EVENT_TABLE()
115};
116
117// ----------------------------------------------------------------------------
118// constants
119// ----------------------------------------------------------------------------
120
121// IDs for the controls and the menu commands
122enum
123{
124 // menu items
125 Event_Quit = 1,
126 Event_About,
127 Event_Connect,
128 Event_Dynamic,
129 Event_Push,
130 Event_Pop,
4ec2df6c 131 Event_Custom,
6164f93c
VZ
132 Event_Test
133};
134
135// status bar fields
136enum
137{
138 Status_Main = 0,
139 Status_Dynamic,
140 Status_Push
141};
142
143// ----------------------------------------------------------------------------
be5a51fb 144// event tables and other macros for wxWidgets
6164f93c
VZ
145// ----------------------------------------------------------------------------
146
be5a51fb 147// the event tables connect the wxWidgets events with the functions (event
6164f93c
VZ
148// handlers) which process them. It can be also done at run-time, but for the
149// simple menu events like this the static method is much simpler.
150BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_MENU(Event_Quit, MyFrame::OnQuit)
152 EVT_MENU(Event_About, MyFrame::OnAbout)
153
154 EVT_MENU(Event_Connect, MyFrame::OnConnect)
155
4ec2df6c 156 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
6164f93c
VZ
157 EVT_MENU(Event_Test, MyFrame::OnTest)
158 EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
159 EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
160
161 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
4ec2df6c 162
07850a49 163 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
4b614012 164
19fe5fd1
VZ
165 // the line below would also work if OnProcessCustom() were defined as
166 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
07850a49 167 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
6164f93c
VZ
168END_EVENT_TABLE()
169
170BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
171 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
172END_EVENT_TABLE()
173
be5a51fb 174// Create a new application object: this macro will allow wxWidgets to create
6164f93c
VZ
175// the application object during program execution (it's better than using a
176// static object for many reasons) and also declares the accessor function
177// wxGetApp() which will return the reference of the right type (i.e. MyApp and
178// not wxApp)
179IMPLEMENT_APP(MyApp)
180
181// ============================================================================
182// implementation
183// ============================================================================
184
185// ----------------------------------------------------------------------------
186// the application class
187// ----------------------------------------------------------------------------
188
189// 'Main program' equivalent: the program execution "starts" here
190bool MyApp::OnInit()
191{
45e6e6f8
VZ
192 if ( !wxApp::OnInit() )
193 return false;
194
6164f93c 195 // create the main application window
be5a51fb 196 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
4ec2df6c 197 wxPoint(50, 50), wxSize(600, 340));
6164f93c
VZ
198
199 // and show it (the frames, unlike simple controls, are not shown when
200 // created initially)
07850a49 201 frame->Show(true);
6164f93c
VZ
202
203 // success: wxApp::OnRun() will be called which will enter the main message
07850a49 204 // loop and the application will run. If we returned false here, the
6164f93c 205 // application would exit immediately.
07850a49 206 return true;
6164f93c
VZ
207}
208
209// ----------------------------------------------------------------------------
210// main frame
211// ----------------------------------------------------------------------------
212
213// frame constructor
214MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
07850a49 215 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
6164f93c 216{
3aa467b6
VZ
217 // init members
218 m_nPush = 0;
219
6164f93c
VZ
220 // create a menu bar
221 wxMenu *menuFile = new wxMenu;
222
4ec2df6c 223 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
6164f93c 224 menuFile->AppendSeparator();
4ec2df6c 225 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
6164f93c
VZ
226
227 wxMenu *menuEvent = new wxMenu;
4ec2df6c
VZ
228 menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
229 _T("Connect or disconnect the dynamic event handler"),
07850a49 230 true /* checkable */);
4ec2df6c
VZ
231 menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
232 _T("Dynamic event sample - only works after Connect"));
233 menuEvent->AppendSeparator();
234 menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
235 _T("Push event handler for test event"));
236 menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
237 _T("Pop event handler for test event"));
238 menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
239 _T("Test event processed by pushed event handler"));
6164f93c 240 menuEvent->AppendSeparator();
4ec2df6c
VZ
241 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
242 _T("Generate a custom event"));
6164f93c
VZ
243
244 // now append the freshly created menu to the menu bar...
245 wxMenuBar *menuBar = new wxMenuBar();
4ec2df6c
VZ
246 menuBar->Append(menuFile, _T("&File"));
247 menuBar->Append(menuEvent, _T("&Event"));
6164f93c
VZ
248
249 // ... and attach this menu bar to the frame
250 SetMenuBar(menuBar);
251
252#if wxUSE_STATUSBAR
253 CreateStatusBar(3);
be5a51fb 254 SetStatusText(_T("Welcome to wxWidgets event sample"));
6164f93c
VZ
255 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
256 SetStatusText(_T("Push count: 0"), Status_Push);
257#endif // wxUSE_STATUSBAR
258}
259
260MyFrame::~MyFrame()
261{
262 // we must pop any remaining event handlers to avoid memory leaks and
263 // crashes!
264 while ( m_nPush-- != 0 )
265 {
07850a49 266 PopEventHandler(true /* delete handler */);
6164f93c
VZ
267 }
268}
269
270// ----------------------------------------------------------------------------
271// standard event handlers
272// ----------------------------------------------------------------------------
273
274void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
275{
07850a49
WS
276 // true is to force the frame to close
277 Close(true);
6164f93c
VZ
278}
279
280void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
281{
f565a6c2 282 wxMessageBox( wxT("Event sample shows different ways of using events\n")
749bfe9a 283 wxT("(c) 2001 Vadim Zeitlin"),
f565a6c2 284 wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
6164f93c
VZ
285}
286
287// ----------------------------------------------------------------------------
288// dynamic event handling stuff
289// ----------------------------------------------------------------------------
290
291void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
292{
f565a6c2
VZ
293 wxMessageBox
294 (
295 wxT("This is a dynamic event handler which can be connected ")
296 wxT("and disconnected at run-time."),
297 wxT("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this
298 );
6164f93c
VZ
299}
300
301void MyFrame::OnConnect(wxCommandEvent& event)
302{
303 if ( event.IsChecked() )
304 {
862c917d
VZ
305 Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
306 wxCommandEventHandler(MyFrame::OnDynamic));
6164f93c 307
8520f137 308#if wxUSE_STATUSBAR
6164f93c
VZ
309 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
310 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
8520f137 311#endif // wxUSE_STATUSBAR
6164f93c 312 }
862c917d 313 else // disconnect
6164f93c 314 {
862c917d
VZ
315 Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
316 wxCommandEventHandler(MyFrame::OnDynamic));
6164f93c 317
8520f137 318#if wxUSE_STATUSBAR
6164f93c
VZ
319 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
320 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
8520f137 321#endif // wxUSE_STATUSBAR
6164f93c
VZ
322 }
323}
324
325// ----------------------------------------------------------------------------
326// push/pop event handlers support
327// ----------------------------------------------------------------------------
328
329void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
330{
331 PushEventHandler(new MyEvtHandler(++m_nPush));
332
8520f137 333#if wxUSE_STATUSBAR
6164f93c 334 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
8520f137 335#endif // wxUSE_STATUSBAR
6164f93c
VZ
336}
337
338void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
339{
340 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
341
07850a49 342 PopEventHandler(true /* delete handler */);
6164f93c
VZ
343 m_nPush--;
344
8520f137 345#if wxUSE_STATUSBAR
6164f93c 346 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
8520f137 347#endif // wxUSE_STATUSBAR
6164f93c
VZ
348}
349
87728739 350void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
6164f93c
VZ
351{
352 wxLogMessage(_T("This is the test event handler in the main frame"));
353}
354
355void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
356{
357 event.Enable( m_nPush > 0 );
358}
359
4ec2df6c
VZ
360// ----------------------------------------------------------------------------
361// custom event methods
362// ----------------------------------------------------------------------------
363
87728739 364void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
4ec2df6c
VZ
365{
366 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
367
368 wxPostEvent(this, eventCustom);
369}
370
87728739 371void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
4ec2df6c
VZ
372{
373 wxLogMessage(_T("Got a custom event!"));
374}
375