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