rename wxDECLARE_LOCAL_EVENT to just wxDECLARE_EVENT
[wxWidgets.git] / samples / event / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: event.cpp
3 // Purpose: wxWidgets sample demonstrating different event usage
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
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
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // event constants
35 // ----------------------------------------------------------------------------
36
37 // define a custom event type (we don't need a separate declaration here but
38 // usually you would use a matching wxDECLARE_EVENT in a header)
39 wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent)
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( \
44 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
45 wxCommandEventHandler(fn), \
46 (wxObject *) NULL \
47 ),
48
49 // ----------------------------------------------------------------------------
50 // private classes
51 // ----------------------------------------------------------------------------
52
53 // Define a new application type, each program should derive a class from wxApp
54 class MyApp : public wxApp
55 {
56 public:
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
67 class MyFrame : public wxFrame
68 {
69 public:
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
82 void OnFireCustom(wxCommandEvent& event);
83 void OnProcessCustom(wxCommandEvent& event);
84
85 void OnUpdateUIPop(wxUpdateUIEvent& event);
86
87 protected:
88 // number of pushed event handlers
89 unsigned m_nPush;
90
91 private:
92 // any class wishing to process wxWidgets events must use this macro
93 DECLARE_EVENT_TABLE()
94 };
95
96 // Define a custom event handler
97 class MyEvtHandler : public wxEvtHandler
98 {
99 public:
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
111 private:
112 unsigned m_level;
113
114 DECLARE_EVENT_TABLE()
115 };
116
117 // ----------------------------------------------------------------------------
118 // constants
119 // ----------------------------------------------------------------------------
120
121 // IDs for the controls and the menu commands
122 enum
123 {
124 // menu items
125 Event_Quit = 1,
126 Event_About,
127 Event_Connect,
128 Event_Dynamic,
129 Event_Push,
130 Event_Pop,
131 Event_Custom,
132 Event_Test
133 };
134
135 // status bar fields
136 enum
137 {
138 Status_Main = 0,
139 Status_Dynamic,
140 Status_Push
141 };
142
143 // ----------------------------------------------------------------------------
144 // event tables and other macros for wxWidgets
145 // ----------------------------------------------------------------------------
146
147 // the event tables connect the wxWidgets events with the functions (event
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.
150 BEGIN_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
156 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
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)
162
163 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
164
165 // the line below would also work if OnProcessCustom() were defined as
166 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
167 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
168 END_EVENT_TABLE()
169
170 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
171 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
172 END_EVENT_TABLE()
173
174 // Create a new application object: this macro will allow wxWidgets to create
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)
179 IMPLEMENT_APP(MyApp)
180
181 // ============================================================================
182 // implementation
183 // ============================================================================
184
185 // ----------------------------------------------------------------------------
186 // the application class
187 // ----------------------------------------------------------------------------
188
189 // 'Main program' equivalent: the program execution "starts" here
190 bool MyApp::OnInit()
191 {
192 if ( !wxApp::OnInit() )
193 return false;
194
195 // create the main application window
196 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
197 wxPoint(50, 50), wxSize(600, 340));
198
199 // and show it (the frames, unlike simple controls, are not shown when
200 // created initially)
201 frame->Show(true);
202
203 // success: wxApp::OnRun() will be called which will enter the main message
204 // loop and the application will run. If we returned false here, the
205 // application would exit immediately.
206 return true;
207 }
208
209 // ----------------------------------------------------------------------------
210 // main frame
211 // ----------------------------------------------------------------------------
212
213 // frame constructor
214 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
215 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
216 {
217 // init members
218 m_nPush = 0;
219
220 // create a menu bar
221 wxMenu *menuFile = new wxMenu;
222
223 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
224 menuFile->AppendSeparator();
225 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
226
227 wxMenu *menuEvent = new wxMenu;
228 menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
229 _T("Connect or disconnect the dynamic event handler"),
230 true /* checkable */);
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"));
240 menuEvent->AppendSeparator();
241 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
242 _T("Generate a custom event"));
243
244 // now append the freshly created menu to the menu bar...
245 wxMenuBar *menuBar = new wxMenuBar();
246 menuBar->Append(menuFile, _T("&File"));
247 menuBar->Append(menuEvent, _T("&Event"));
248
249 // ... and attach this menu bar to the frame
250 SetMenuBar(menuBar);
251
252 #if wxUSE_STATUSBAR
253 CreateStatusBar(3);
254 SetStatusText(_T("Welcome to wxWidgets event sample"));
255 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
256 SetStatusText(_T("Push count: 0"), Status_Push);
257 #endif // wxUSE_STATUSBAR
258 }
259
260 MyFrame::~MyFrame()
261 {
262 // we must pop any remaining event handlers to avoid memory leaks and
263 // crashes!
264 while ( m_nPush-- != 0 )
265 {
266 PopEventHandler(true /* delete handler */);
267 }
268 }
269
270 // ----------------------------------------------------------------------------
271 // standard event handlers
272 // ----------------------------------------------------------------------------
273
274 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
275 {
276 // true is to force the frame to close
277 Close(true);
278 }
279
280 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
281 {
282 wxMessageBox( wxT("Event sample shows different ways of using events\n")
283 wxT("(c) 2001 Vadim Zeitlin"),
284 wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
285 }
286
287 // ----------------------------------------------------------------------------
288 // dynamic event handling stuff
289 // ----------------------------------------------------------------------------
290
291 void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
292 {
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 );
299 }
300
301 void MyFrame::OnConnect(wxCommandEvent& event)
302 {
303 if ( event.IsChecked() )
304 {
305 Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
306 wxCommandEventHandler(MyFrame::OnDynamic));
307
308 #if wxUSE_STATUSBAR
309 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
310 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
311 #endif // wxUSE_STATUSBAR
312 }
313 else // disconnect
314 {
315 Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
316 wxCommandEventHandler(MyFrame::OnDynamic));
317
318 #if wxUSE_STATUSBAR
319 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
320 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
321 #endif // wxUSE_STATUSBAR
322 }
323 }
324
325 // ----------------------------------------------------------------------------
326 // push/pop event handlers support
327 // ----------------------------------------------------------------------------
328
329 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
330 {
331 PushEventHandler(new MyEvtHandler(++m_nPush));
332
333 #if wxUSE_STATUSBAR
334 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
335 #endif // wxUSE_STATUSBAR
336 }
337
338 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
339 {
340 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
341
342 PopEventHandler(true /* delete handler */);
343 m_nPush--;
344
345 #if wxUSE_STATUSBAR
346 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
347 #endif // wxUSE_STATUSBAR
348 }
349
350 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
351 {
352 wxLogMessage(_T("This is the test event handler in the main frame"));
353 }
354
355 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
356 {
357 event.Enable( m_nPush > 0 );
358 }
359
360 // ----------------------------------------------------------------------------
361 // custom event methods
362 // ----------------------------------------------------------------------------
363
364 void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
365 {
366 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
367
368 wxPostEvent(this, eventCustom);
369 }
370
371 void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
372 {
373 wxLogMessage(_T("Got a custom event!"));
374 }
375