]>
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 | ||
41f02b9a FM |
33 | #ifndef __WXMSW__ |
34 | #include "../sample.xpm" | |
35 | #endif | |
36 | ||
4ec2df6c VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // event constants | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
862c917d | 41 | // define a custom event type (we don't need a separate declaration here but |
1afe8c83 | 42 | // usually you would use a matching wxDECLARE_EVENT in a header) |
16be58c1 | 43 | wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent); |
4ec2df6c VZ |
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( \ | |
07850a49 | 48 | wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \ |
79935f61 | 49 | wxCommandEventHandler(fn), \ |
4ec2df6c VZ |
50 | (wxObject *) NULL \ |
51 | ), | |
52 | ||
6164f93c VZ |
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 | ||
4ec2df6c VZ |
86 | void OnFireCustom(wxCommandEvent& event); |
87 | void OnProcessCustom(wxCommandEvent& event); | |
88 | ||
6164f93c VZ |
89 | void OnUpdateUIPop(wxUpdateUIEvent& event); |
90 | ||
91 | protected: | |
92 | // number of pushed event handlers | |
b143cf70 | 93 | unsigned m_nPush; |
6164f93c VZ |
94 | |
95 | private: | |
be5a51fb | 96 | // any class wishing to process wxWidgets events must use this macro |
6164f93c VZ |
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: | |
b143cf70 | 116 | unsigned m_level; |
6164f93c VZ |
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, | |
4ec2df6c | 135 | Event_Custom, |
6164f93c VZ |
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 | // ---------------------------------------------------------------------------- | |
be5a51fb | 148 | // event tables and other macros for wxWidgets |
6164f93c VZ |
149 | // ---------------------------------------------------------------------------- |
150 | ||
be5a51fb | 151 | // the event tables connect the wxWidgets events with the functions (event |
6164f93c VZ |
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 | ||
4ec2df6c | 160 | EVT_MENU(Event_Custom, MyFrame::OnFireCustom) |
6164f93c VZ |
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) | |
4ec2df6c | 166 | |
07850a49 | 167 | EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom) |
4b614012 | 168 | |
19fe5fd1 VZ |
169 | // the line below would also work if OnProcessCustom() were defined as |
170 | // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent | |
07850a49 | 171 | //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom) |
6164f93c VZ |
172 | END_EVENT_TABLE() |
173 | ||
174 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) | |
175 | EVT_MENU(Event_Test, MyEvtHandler::OnTest) | |
176 | END_EVENT_TABLE() | |
177 | ||
be5a51fb | 178 | // Create a new application object: this macro will allow wxWidgets to create |
6164f93c VZ |
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 | { | |
45e6e6f8 VZ |
196 | if ( !wxApp::OnInit() ) |
197 | return false; | |
198 | ||
6164f93c | 199 | // create the main application window |
be5a51fb | 200 | MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"), |
4ec2df6c | 201 | wxPoint(50, 50), wxSize(600, 340)); |
6164f93c VZ |
202 | |
203 | // and show it (the frames, unlike simple controls, are not shown when | |
204 | // created initially) | |
07850a49 | 205 | frame->Show(true); |
6164f93c VZ |
206 | |
207 | // success: wxApp::OnRun() will be called which will enter the main message | |
07850a49 | 208 | // loop and the application will run. If we returned false here, the |
6164f93c | 209 | // application would exit immediately. |
07850a49 | 210 | return true; |
6164f93c VZ |
211 | } |
212 | ||
213 | // ---------------------------------------------------------------------------- | |
214 | // main frame | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | // frame constructor | |
218 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
07850a49 | 219 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
6164f93c | 220 | { |
41f02b9a FM |
221 | SetIcon(wxICON(sample)); |
222 | ||
3aa467b6 VZ |
223 | // init members |
224 | m_nPush = 0; | |
225 | ||
6164f93c VZ |
226 | // create a menu bar |
227 | wxMenu *menuFile = new wxMenu; | |
228 | ||
4ec2df6c | 229 | menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
6164f93c | 230 | menuFile->AppendSeparator(); |
4ec2df6c | 231 | menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
6164f93c VZ |
232 | |
233 | wxMenu *menuEvent = new wxMenu; | |
4ec2df6c VZ |
234 | menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"), |
235 | _T("Connect or disconnect the dynamic event handler"), | |
07850a49 | 236 | true /* checkable */); |
4ec2df6c VZ |
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")); | |
6164f93c | 246 | menuEvent->AppendSeparator(); |
4ec2df6c VZ |
247 | menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"), |
248 | _T("Generate a custom event")); | |
6164f93c VZ |
249 | |
250 | // now append the freshly created menu to the menu bar... | |
251 | wxMenuBar *menuBar = new wxMenuBar(); | |
4ec2df6c VZ |
252 | menuBar->Append(menuFile, _T("&File")); |
253 | menuBar->Append(menuEvent, _T("&Event")); | |
6164f93c VZ |
254 | |
255 | // ... and attach this menu bar to the frame | |
256 | SetMenuBar(menuBar); | |
257 | ||
258 | #if wxUSE_STATUSBAR | |
259 | CreateStatusBar(3); | |
be5a51fb | 260 | SetStatusText(_T("Welcome to wxWidgets event sample")); |
6164f93c VZ |
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 | { | |
07850a49 | 272 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
273 | } |
274 | } | |
275 | ||
276 | // ---------------------------------------------------------------------------- | |
277 | // standard event handlers | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
280 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
07850a49 WS |
282 | // true is to force the frame to close |
283 | Close(true); | |
6164f93c VZ |
284 | } |
285 | ||
286 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
287 | { | |
f565a6c2 | 288 | wxMessageBox( wxT("Event sample shows different ways of using events\n") |
749bfe9a | 289 | wxT("(c) 2001 Vadim Zeitlin"), |
f565a6c2 | 290 | wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this ); |
6164f93c VZ |
291 | } |
292 | ||
293 | // ---------------------------------------------------------------------------- | |
294 | // dynamic event handling stuff | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
297 | void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event)) | |
298 | { | |
f565a6c2 VZ |
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 | ); | |
6164f93c VZ |
305 | } |
306 | ||
307 | void MyFrame::OnConnect(wxCommandEvent& event) | |
308 | { | |
309 | if ( event.IsChecked() ) | |
310 | { | |
862c917d VZ |
311 | Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED, |
312 | wxCommandEventHandler(MyFrame::OnDynamic)); | |
6164f93c | 313 | |
8520f137 | 314 | #if wxUSE_STATUSBAR |
6164f93c VZ |
315 | SetStatusText(_T("You can now use \"Dynamic\" item in the menu")); |
316 | SetStatusText(_T("Dynamic: on"), Status_Dynamic); | |
8520f137 | 317 | #endif // wxUSE_STATUSBAR |
6164f93c | 318 | } |
862c917d | 319 | else // disconnect |
6164f93c | 320 | { |
862c917d VZ |
321 | Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED, |
322 | wxCommandEventHandler(MyFrame::OnDynamic)); | |
6164f93c | 323 | |
8520f137 | 324 | #if wxUSE_STATUSBAR |
6164f93c VZ |
325 | SetStatusText(_T("You can no more use \"Dynamic\" item in the menu")); |
326 | SetStatusText(_T("Dynamic: off"), Status_Dynamic); | |
8520f137 | 327 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
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 | ||
8520f137 | 339 | #if wxUSE_STATUSBAR |
6164f93c | 340 | SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 341 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
342 | } |
343 | ||
344 | void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event)) | |
345 | { | |
346 | wxCHECK_RET( m_nPush, _T("this command should be disabled!") ); | |
347 | ||
07850a49 | 348 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
349 | m_nPush--; |
350 | ||
8520f137 | 351 | #if wxUSE_STATUSBAR |
6164f93c | 352 | SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 353 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
354 | } |
355 | ||
87728739 | 356 | void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event)) |
6164f93c VZ |
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 | ||
4ec2df6c VZ |
366 | // ---------------------------------------------------------------------------- |
367 | // custom event methods | |
368 | // ---------------------------------------------------------------------------- | |
369 | ||
87728739 | 370 | void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c VZ |
371 | { |
372 | wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND); | |
373 | ||
374 | wxPostEvent(this, eventCustom); | |
375 | } | |
376 | ||
87728739 | 377 | void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c VZ |
378 | { |
379 | wxLogMessage(_T("Got a custom event!")); | |
380 | } | |
381 |