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