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