]>
Commit | Line | Data |
---|---|---|
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 | |
46 | BEGIN_DECLARE_EVENT_TYPES() | |
47 | DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777) | |
48 | END_DECLARE_EVENT_TYPES() | |
49 | ||
50 | DEFINE_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 | |
65 | class MyApp : public wxApp | |
66 | { | |
67 | public: | |
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 | |
78 | class MyFrame : public wxFrame | |
79 | { | |
80 | public: | |
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 | ||
98 | protected: | |
99 | // number of pushed event handlers | |
100 | size_t m_nPush; | |
101 | ||
102 | private: | |
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 | |
108 | class MyEvtHandler : public wxEvtHandler | |
109 | { | |
110 | public: | |
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 | ||
122 | private: | |
123 | size_t m_level; | |
124 | ||
125 | DECLARE_EVENT_TABLE() | |
126 | }; | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
129 | // constants | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | // IDs for the controls and the menu commands | |
133 | enum | |
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 | |
147 | enum | |
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. | |
161 | BEGIN_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 |
179 | END_EVENT_TABLE() |
180 | ||
181 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) | |
182 | EVT_MENU(Event_Test, MyEvtHandler::OnTest) | |
183 | END_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) | |
190 | IMPLEMENT_APP(MyApp) | |
191 | ||
192 | // ============================================================================ | |
193 | // implementation | |
194 | // ============================================================================ | |
195 | ||
196 | // ---------------------------------------------------------------------------- | |
197 | // the application class | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
200 | // 'Main program' equivalent: the program execution "starts" here | |
201 | bool MyApp::OnInit() | |
202 | { | |
203 | // create the main application window | |
be5a51fb | 204 | MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"), |
4ec2df6c | 205 | wxPoint(50, 50), wxSize(600, 340)); |
6164f93c VZ |
206 | |
207 | // and show it (the frames, unlike simple controls, are not shown when | |
208 | // created initially) | |
07850a49 | 209 | frame->Show(true); |
6164f93c VZ |
210 | |
211 | // success: wxApp::OnRun() will be called which will enter the main message | |
07850a49 | 212 | // loop and the application will run. If we returned false here, the |
6164f93c | 213 | // application would exit immediately. |
07850a49 | 214 | return true; |
6164f93c VZ |
215 | } |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // main frame | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | // frame constructor | |
222 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
07850a49 | 223 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
6164f93c | 224 | { |
3aa467b6 VZ |
225 | // init members |
226 | m_nPush = 0; | |
227 | ||
6164f93c VZ |
228 | // create a menu bar |
229 | wxMenu *menuFile = new wxMenu; | |
230 | ||
4ec2df6c | 231 | menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
6164f93c | 232 | menuFile->AppendSeparator(); |
4ec2df6c | 233 | menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
6164f93c VZ |
234 | |
235 | wxMenu *menuEvent = new wxMenu; | |
4ec2df6c VZ |
236 | menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"), |
237 | _T("Connect or disconnect the dynamic event handler"), | |
07850a49 | 238 | true /* checkable */); |
4ec2df6c VZ |
239 | menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"), |
240 | _T("Dynamic event sample - only works after Connect")); | |
241 | menuEvent->AppendSeparator(); | |
242 | menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"), | |
243 | _T("Push event handler for test event")); | |
244 | menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"), | |
245 | _T("Pop event handler for test event")); | |
246 | menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"), | |
247 | _T("Test event processed by pushed event handler")); | |
6164f93c | 248 | menuEvent->AppendSeparator(); |
4ec2df6c VZ |
249 | menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"), |
250 | _T("Generate a custom event")); | |
6164f93c VZ |
251 | |
252 | // now append the freshly created menu to the menu bar... | |
253 | wxMenuBar *menuBar = new wxMenuBar(); | |
4ec2df6c VZ |
254 | menuBar->Append(menuFile, _T("&File")); |
255 | menuBar->Append(menuEvent, _T("&Event")); | |
6164f93c VZ |
256 | |
257 | // ... and attach this menu bar to the frame | |
258 | SetMenuBar(menuBar); | |
259 | ||
260 | #if wxUSE_STATUSBAR | |
261 | CreateStatusBar(3); | |
be5a51fb | 262 | SetStatusText(_T("Welcome to wxWidgets event sample")); |
6164f93c VZ |
263 | SetStatusText(_T("Dynamic: off"), Status_Dynamic); |
264 | SetStatusText(_T("Push count: 0"), Status_Push); | |
265 | #endif // wxUSE_STATUSBAR | |
266 | } | |
267 | ||
268 | MyFrame::~MyFrame() | |
269 | { | |
270 | // we must pop any remaining event handlers to avoid memory leaks and | |
271 | // crashes! | |
272 | while ( m_nPush-- != 0 ) | |
273 | { | |
07850a49 | 274 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
275 | } |
276 | } | |
277 | ||
278 | // ---------------------------------------------------------------------------- | |
279 | // standard event handlers | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
282 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
283 | { | |
07850a49 WS |
284 | // true is to force the frame to close |
285 | Close(true); | |
6164f93c VZ |
286 | } |
287 | ||
288 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
289 | { | |
f565a6c2 | 290 | wxMessageBox( wxT("Event sample shows different ways of using events\n") |
749bfe9a | 291 | wxT("(c) 2001 Vadim Zeitlin"), |
f565a6c2 | 292 | wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this ); |
6164f93c VZ |
293 | } |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // dynamic event handling stuff | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event)) | |
300 | { | |
f565a6c2 VZ |
301 | wxMessageBox |
302 | ( | |
303 | wxT("This is a dynamic event handler which can be connected ") | |
304 | wxT("and disconnected at run-time."), | |
305 | wxT("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this | |
306 | ); | |
6164f93c VZ |
307 | } |
308 | ||
309 | void MyFrame::OnConnect(wxCommandEvent& event) | |
310 | { | |
311 | if ( event.IsChecked() ) | |
312 | { | |
313 | // disconnect | |
07850a49 | 314 | Connect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, |
6164f93c VZ |
315 | (wxObjectEventFunction) |
316 | (wxEventFunction) | |
317 | (wxCommandEventFunction)&MyFrame::OnDynamic); | |
318 | ||
8520f137 | 319 | #if wxUSE_STATUSBAR |
6164f93c VZ |
320 | SetStatusText(_T("You can now use \"Dynamic\" item in the menu")); |
321 | SetStatusText(_T("Dynamic: on"), Status_Dynamic); | |
8520f137 | 322 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
323 | } |
324 | else // connect | |
325 | { | |
07850a49 | 326 | Disconnect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED); |
6164f93c | 327 | |
8520f137 | 328 | #if wxUSE_STATUSBAR |
6164f93c VZ |
329 | SetStatusText(_T("You can no more use \"Dynamic\" item in the menu")); |
330 | SetStatusText(_T("Dynamic: off"), Status_Dynamic); | |
8520f137 | 331 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
332 | } |
333 | } | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // push/pop event handlers support | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event)) | |
340 | { | |
341 | PushEventHandler(new MyEvtHandler(++m_nPush)); | |
342 | ||
8520f137 | 343 | #if wxUSE_STATUSBAR |
6164f93c | 344 | SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 345 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
346 | } |
347 | ||
348 | void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event)) | |
349 | { | |
350 | wxCHECK_RET( m_nPush, _T("this command should be disabled!") ); | |
351 | ||
07850a49 | 352 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
353 | m_nPush--; |
354 | ||
8520f137 | 355 | #if wxUSE_STATUSBAR |
6164f93c | 356 | SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 357 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
358 | } |
359 | ||
87728739 | 360 | void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event)) |
6164f93c VZ |
361 | { |
362 | wxLogMessage(_T("This is the test event handler in the main frame")); | |
363 | } | |
364 | ||
365 | void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event) | |
366 | { | |
367 | event.Enable( m_nPush > 0 ); | |
368 | } | |
369 | ||
4ec2df6c VZ |
370 | // ---------------------------------------------------------------------------- |
371 | // custom event methods | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
87728739 | 374 | void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c VZ |
375 | { |
376 | wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND); | |
377 | ||
378 | wxPostEvent(this, eventCustom); | |
379 | } | |
380 | ||
87728739 | 381 | void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c VZ |
382 | { |
383 | wxLogMessage(_T("Got a custom event!")); | |
384 | } | |
385 |