]>
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$ | |
8584ae1c | 8 | // Copyright: (c) 2001-2009 Vadim Zeitlin |
6164f93c VZ |
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 | ||
7c9cc312 VZ |
37 | #include <wx/statline.h> |
38 | #include <wx/log.h> | |
39 | ||
4ec2df6c VZ |
40 | // ---------------------------------------------------------------------------- |
41 | // event constants | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
862c917d | 44 | // define a custom event type (we don't need a separate declaration here but |
1afe8c83 | 45 | // usually you would use a matching wxDECLARE_EVENT in a header) |
16be58c1 | 46 | wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent); |
4ec2df6c VZ |
47 | |
48 | // it may also be convenient to define an event table macro for this event type | |
49 | #define EVT_MY_CUSTOM_COMMAND(id, fn) \ | |
50 | DECLARE_EVENT_TABLE_ENTRY( \ | |
07850a49 | 51 | wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \ |
79935f61 | 52 | wxCommandEventHandler(fn), \ |
4ec2df6c VZ |
53 | (wxObject *) NULL \ |
54 | ), | |
55 | ||
6164f93c VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // private classes | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | // Define a new application type, each program should derive a class from wxApp | |
61 | class MyApp : public wxApp | |
62 | { | |
63 | public: | |
64 | // override base class virtuals | |
65 | // ---------------------------- | |
66 | ||
67 | // this one is called on application startup and is a good place for the app | |
68 | // initialization (doing it here and not in the ctor allows to have an error | |
69 | // return: if OnInit() returns false, the application terminates) | |
70 | virtual bool OnInit(); | |
7c9cc312 VZ |
71 | |
72 | // these are regular event handlers used to highlight the events handling | |
73 | // order | |
74 | void OnClickDynamicHandlerApp(wxCommandEvent& event); | |
75 | void OnClickStaticHandlerApp(wxCommandEvent& event); | |
76 | ||
77 | // we override wxConsoleApp::FilterEvent used to highlight the events | |
78 | // handling order | |
79 | virtual int FilterEvent(wxEvent& event); | |
80 | ||
81 | private: | |
82 | DECLARE_EVENT_TABLE() | |
6164f93c VZ |
83 | }; |
84 | ||
7c9cc312 VZ |
85 | // Define a custom button used to highlight the events handling order |
86 | class MyEvtTestButton : public wxButton | |
87 | { | |
88 | public: | |
89 | static long BUTTON_ID; | |
90 | ||
91 | MyEvtTestButton(wxWindow *parent, const wxString& label) | |
92 | : wxButton(parent, BUTTON_ID, label) | |
93 | { | |
94 | // Add a dynamic handler for this button event to button itself | |
95 | Connect(wxEVT_COMMAND_BUTTON_CLICKED, | |
96 | wxCommandEventHandler(MyEvtTestButton::OnClickDynamicHandler)); | |
97 | } | |
98 | ||
99 | private: | |
100 | void OnClickDynamicHandler(wxCommandEvent& event) | |
101 | { | |
102 | wxLogMessage("Step 3 in \"How Events are Processed\":\n" | |
103 | "Button::ownDynamicHandler"); | |
104 | ||
105 | event.Skip(); | |
106 | } | |
107 | ||
108 | void OnClickStaticHandler(wxCommandEvent& event) | |
109 | { | |
110 | wxLogMessage("Step 4 in \"How Events are Processed\":\n" | |
111 | "Button::ownStaticHandler"); | |
112 | ||
113 | event.Skip(); | |
114 | } | |
115 | ||
116 | DECLARE_EVENT_TABLE() | |
117 | }; | |
118 | ||
119 | long MyEvtTestButton::BUTTON_ID = wxNewId(); | |
120 | ||
6164f93c VZ |
121 | // Define a new frame type: this is going to be our main frame |
122 | class MyFrame : public wxFrame | |
123 | { | |
124 | public: | |
125 | // ctor(s) | |
126 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
127 | virtual ~MyFrame(); | |
128 | ||
129 | void OnQuit(wxCommandEvent& event); | |
130 | void OnAbout(wxCommandEvent& event); | |
890d70eb | 131 | #ifdef wxHAS_EVENT_BIND |
8584ae1c | 132 | void OnBind(wxCommandEvent& event); |
890d70eb | 133 | #endif // wxHAS_EVENT_BIND |
6164f93c VZ |
134 | void OnConnect(wxCommandEvent& event); |
135 | void OnDynamic(wxCommandEvent& event); | |
136 | void OnPushEventHandler(wxCommandEvent& event); | |
137 | void OnPopEventHandler(wxCommandEvent& event); | |
138 | void OnTest(wxCommandEvent& event); | |
139 | ||
4ec2df6c VZ |
140 | void OnFireCustom(wxCommandEvent& event); |
141 | void OnProcessCustom(wxCommandEvent& event); | |
142 | ||
6164f93c VZ |
143 | void OnUpdateUIPop(wxUpdateUIEvent& event); |
144 | ||
7c9cc312 VZ |
145 | // regular event handlers used to highlight the events handling order |
146 | void OnClickDynamicHandlerFrame(wxCommandEvent& event); | |
147 | void OnClickDynamicHandlerButton(wxCommandEvent& event); | |
148 | void OnClickStaticHandlerFrame(wxCommandEvent& event); | |
149 | ||
8584ae1c VZ |
150 | private: |
151 | // symbolic names for the status bar fields | |
152 | enum | |
153 | { | |
154 | Status_Main = 0, | |
155 | Status_Dynamic, | |
156 | Status_Push | |
157 | }; | |
158 | ||
159 | void UpdateDynamicStatus(bool on) | |
160 | { | |
161 | #if wxUSE_STATUSBAR | |
162 | if ( on ) | |
163 | { | |
164 | SetStatusText("You can now use \"Dynamic\" item in the menu"); | |
165 | SetStatusText("Dynamic: on", Status_Dynamic); | |
166 | } | |
167 | else | |
168 | { | |
169 | SetStatusText("You can no more use \"Dynamic\" item in the menu"); | |
170 | SetStatusText("Dynamic: off", Status_Dynamic); | |
171 | } | |
172 | #endif // wxUSE_STATUSBAR | |
173 | } | |
174 | ||
6164f93c | 175 | // number of pushed event handlers |
b143cf70 | 176 | unsigned m_nPush; |
6164f93c | 177 | |
8584ae1c VZ |
178 | // the button to whose event we connect dynamically |
179 | wxButton *m_btnDynamic; | |
180 | ||
7c9cc312 VZ |
181 | // the button used to highlight the event handlers execution order |
182 | MyEvtTestButton *m_testBtn; | |
183 | ||
184 | ||
be5a51fb | 185 | // any class wishing to process wxWidgets events must use this macro |
6164f93c VZ |
186 | DECLARE_EVENT_TABLE() |
187 | }; | |
188 | ||
189 | // Define a custom event handler | |
190 | class MyEvtHandler : public wxEvtHandler | |
191 | { | |
192 | public: | |
193 | MyEvtHandler(size_t level) { m_level = level; } | |
194 | ||
195 | void OnTest(wxCommandEvent& event) | |
196 | { | |
9a83f860 | 197 | wxLogMessage(wxT("This is the pushed test event handler #%u"), m_level); |
6164f93c VZ |
198 | |
199 | // if we don't skip the event, the other event handlers won't get it: | |
200 | // try commenting out this line and see what changes | |
201 | event.Skip(); | |
202 | } | |
203 | ||
204 | private: | |
b143cf70 | 205 | unsigned m_level; |
6164f93c VZ |
206 | |
207 | DECLARE_EVENT_TABLE() | |
208 | }; | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // constants | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | // IDs for the controls and the menu commands | |
215 | enum | |
216 | { | |
217 | // menu items | |
218 | Event_Quit = 1, | |
219 | Event_About, | |
8584ae1c | 220 | Event_Bind, |
6164f93c VZ |
221 | Event_Connect, |
222 | Event_Dynamic, | |
223 | Event_Push, | |
224 | Event_Pop, | |
4ec2df6c | 225 | Event_Custom, |
6164f93c VZ |
226 | Event_Test |
227 | }; | |
228 | ||
6164f93c | 229 | // ---------------------------------------------------------------------------- |
be5a51fb | 230 | // event tables and other macros for wxWidgets |
6164f93c VZ |
231 | // ---------------------------------------------------------------------------- |
232 | ||
7c9cc312 VZ |
233 | // The event tables connect the wxWidgets events with the functions (event |
234 | // handlers) which process them. | |
235 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
236 | // Add a static handler for button Click event in the app | |
237 | EVT_BUTTON(MyEvtTestButton::BUTTON_ID, MyApp::OnClickStaticHandlerApp) | |
238 | END_EVENT_TABLE() | |
239 | ||
240 | BEGIN_EVENT_TABLE(MyEvtTestButton, wxButton) | |
241 | // Add a static handler to this button itself for its own event | |
242 | EVT_BUTTON(BUTTON_ID, MyEvtTestButton::OnClickStaticHandler) | |
243 | END_EVENT_TABLE() | |
244 | ||
245 | // This can be also done at run-time, but for the | |
6164f93c VZ |
246 | // simple menu events like this the static method is much simpler. |
247 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
248 | EVT_MENU(Event_Quit, MyFrame::OnQuit) | |
249 | EVT_MENU(Event_About, MyFrame::OnAbout) | |
250 | ||
890d70eb | 251 | #ifdef wxHAS_EVENT_BIND |
8584ae1c | 252 | EVT_MENU(Event_Bind, MyFrame::OnBind) |
890d70eb | 253 | #endif // wxHAS_EVENT_BIND |
6164f93c VZ |
254 | EVT_MENU(Event_Connect, MyFrame::OnConnect) |
255 | ||
4ec2df6c | 256 | EVT_MENU(Event_Custom, MyFrame::OnFireCustom) |
6164f93c VZ |
257 | EVT_MENU(Event_Test, MyFrame::OnTest) |
258 | EVT_MENU(Event_Push, MyFrame::OnPushEventHandler) | |
259 | EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler) | |
260 | ||
261 | EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop) | |
4ec2df6c | 262 | |
07850a49 | 263 | EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom) |
4b614012 | 264 | |
19fe5fd1 VZ |
265 | // the line below would also work if OnProcessCustom() were defined as |
266 | // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent | |
07850a49 | 267 | //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom) |
7c9cc312 VZ |
268 | |
269 | // Add a static handler in the parent frame for button event | |
270 | EVT_BUTTON(MyEvtTestButton::BUTTON_ID, MyFrame::OnClickStaticHandlerFrame) | |
6164f93c VZ |
271 | END_EVENT_TABLE() |
272 | ||
273 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) | |
274 | EVT_MENU(Event_Test, MyEvtHandler::OnTest) | |
275 | END_EVENT_TABLE() | |
276 | ||
be5a51fb | 277 | // Create a new application object: this macro will allow wxWidgets to create |
6164f93c VZ |
278 | // the application object during program execution (it's better than using a |
279 | // static object for many reasons) and also declares the accessor function | |
280 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
281 | // not wxApp) | |
282 | IMPLEMENT_APP(MyApp) | |
283 | ||
284 | // ============================================================================ | |
285 | // implementation | |
286 | // ============================================================================ | |
287 | ||
288 | // ---------------------------------------------------------------------------- | |
289 | // the application class | |
290 | // ---------------------------------------------------------------------------- | |
291 | ||
292 | // 'Main program' equivalent: the program execution "starts" here | |
293 | bool MyApp::OnInit() | |
294 | { | |
45e6e6f8 VZ |
295 | if ( !wxApp::OnInit() ) |
296 | return false; | |
297 | ||
6164f93c | 298 | // create the main application window |
9a83f860 | 299 | MyFrame *frame = new MyFrame(wxT("Event wxWidgets Sample"), |
4ec2df6c | 300 | wxPoint(50, 50), wxSize(600, 340)); |
6164f93c VZ |
301 | |
302 | // and show it (the frames, unlike simple controls, are not shown when | |
303 | // created initially) | |
07850a49 | 304 | frame->Show(true); |
6164f93c | 305 | |
7c9cc312 VZ |
306 | // Add a dynamic handler at the application level for the test button |
307 | Connect(MyEvtTestButton::BUTTON_ID, wxEVT_COMMAND_BUTTON_CLICKED, | |
308 | wxCommandEventHandler(MyApp::OnClickDynamicHandlerApp)); | |
309 | ||
6164f93c | 310 | // success: wxApp::OnRun() will be called which will enter the main message |
07850a49 | 311 | // loop and the application will run. If we returned false here, the |
6164f93c | 312 | // application would exit immediately. |
07850a49 | 313 | return true; |
6164f93c VZ |
314 | } |
315 | ||
7c9cc312 VZ |
316 | // This is always the first to handle an event ! |
317 | int MyApp::FilterEvent(wxEvent& event) | |
318 | { | |
319 | if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED && | |
320 | event.GetId() == MyEvtTestButton::BUTTON_ID ) | |
321 | { | |
322 | wxLogMessage("Step 0 in \"How Events are Processed\":\n" | |
323 | "App::FilterEvent"); | |
324 | } | |
325 | ||
326 | return wxApp::FilterEvent(event); | |
327 | } | |
328 | ||
329 | void MyApp::OnClickDynamicHandlerApp(wxCommandEvent& event) | |
330 | { | |
331 | wxLogMessage("Step 7, 3 in \"How Events are Processed\":\n" | |
332 | "App::DynamicHandler_InAppTable"); | |
333 | ||
334 | event.Skip(); | |
335 | } | |
336 | ||
337 | void MyApp::OnClickStaticHandlerApp(wxCommandEvent& event) | |
338 | { | |
339 | wxLogMessage("Step 7, 4 in \"How Events are Processed\":\n" | |
340 | "App::StaticHandler_InAppTable"); | |
341 | ||
342 | wxLogMessage("Button click processed, there should be no more messages " | |
343 | "about handling events from the button.\n\n" | |
344 | "The log below shows the order in which the handlers " | |
345 | "were executed."); | |
346 | wxLog::FlushActive(); | |
347 | ||
348 | event.Skip(); | |
349 | } | |
350 | ||
6164f93c VZ |
351 | // ---------------------------------------------------------------------------- |
352 | // main frame | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | // frame constructor | |
356 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
8584ae1c | 357 | : wxFrame(NULL, wxID_ANY, title, pos, size) |
6164f93c | 358 | { |
41f02b9a FM |
359 | SetIcon(wxICON(sample)); |
360 | ||
3aa467b6 VZ |
361 | // init members |
362 | m_nPush = 0; | |
8584ae1c | 363 | m_btnDynamic = NULL; |
3aa467b6 | 364 | |
6164f93c VZ |
365 | // create a menu bar |
366 | wxMenu *menuFile = new wxMenu; | |
367 | ||
2d143b66 | 368 | menuFile->Append(Event_About, wxT("&About\tCtrl-A"), wxT("Show about dialog")); |
6164f93c | 369 | menuFile->AppendSeparator(); |
9a83f860 | 370 | menuFile->Append(Event_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); |
6164f93c VZ |
371 | |
372 | wxMenu *menuEvent = new wxMenu; | |
890d70eb | 373 | #ifdef wxHAS_EVENT_BIND |
8584ae1c VZ |
374 | menuEvent->AppendCheckItem(Event_Bind, "&Bind\tCtrl-B", |
375 | "Bind or unbind a dynamic event handler"); | |
890d70eb | 376 | #endif // wxHAS_EVENT_BIND |
9a83f860 VZ |
377 | menuEvent->AppendCheckItem(Event_Connect, wxT("&Connect\tCtrl-C"), |
378 | wxT("Connect or disconnect the dynamic event handler")); | |
379 | menuEvent->Append(Event_Dynamic, wxT("&Dynamic event\tCtrl-D"), | |
380 | wxT("Dynamic event sample - only works after Connect")); | |
4ec2df6c | 381 | menuEvent->AppendSeparator(); |
9a83f860 VZ |
382 | menuEvent->Append(Event_Push, wxT("&Push event handler\tCtrl-P"), |
383 | wxT("Push event handler for test event")); | |
384 | menuEvent->Append(Event_Pop, wxT("P&op event handler\tCtrl-O"), | |
385 | wxT("Pop event handler for test event")); | |
386 | menuEvent->Append(Event_Test, wxT("Test event\tCtrl-T"), | |
387 | wxT("Test event processed by pushed event handler")); | |
6164f93c | 388 | menuEvent->AppendSeparator(); |
9a83f860 VZ |
389 | menuEvent->Append(Event_Custom, wxT("Fire c&ustom event\tCtrl-U"), |
390 | wxT("Generate a custom event")); | |
6164f93c VZ |
391 | |
392 | // now append the freshly created menu to the menu bar... | |
393 | wxMenuBar *menuBar = new wxMenuBar(); | |
9a83f860 VZ |
394 | menuBar->Append(menuFile, wxT("&File")); |
395 | menuBar->Append(menuEvent, wxT("&Event")); | |
6164f93c VZ |
396 | |
397 | // ... and attach this menu bar to the frame | |
398 | SetMenuBar(menuBar); | |
399 | ||
400 | #if wxUSE_STATUSBAR | |
401 | CreateStatusBar(3); | |
9a83f860 VZ |
402 | SetStatusText(wxT("Welcome to wxWidgets event sample")); |
403 | SetStatusText(wxT("Dynamic: off"), Status_Dynamic); | |
404 | SetStatusText(wxT("Push count: 0"), Status_Push); | |
6164f93c | 405 | #endif // wxUSE_STATUSBAR |
8584ae1c VZ |
406 | |
407 | wxPanel * const panel = new wxPanel(this); | |
7c9cc312 | 408 | wxSizer * const mainSizer = new wxBoxSizer(wxVERTICAL); |
8584ae1c VZ |
409 | wxSizer * const sizer = new wxBoxSizer(wxHORIZONTAL); |
410 | const wxSizerFlags centreY(wxSizerFlags().Centre().Border()); | |
411 | sizer->Add(new wxStaticText(panel, wxID_ANY, | |
412 | "This button will only work if its handler is dynamically connected"), | |
413 | centreY); | |
414 | m_btnDynamic = new wxButton(panel, Event_Dynamic, "&Dynamic button"); | |
415 | sizer->Add(m_btnDynamic, centreY); | |
7c9cc312 VZ |
416 | |
417 | mainSizer->Add(sizer, 1, wxEXPAND); | |
418 | mainSizer->Add(new wxStaticLine(panel), 0, wxEXPAND); | |
419 | mainSizer->Add(new wxStaticLine(panel), 0, wxEXPAND); | |
420 | ||
421 | m_testBtn = new MyEvtTestButton(panel, "Test Event Handlers Execution Order"); | |
422 | ||
423 | // After being created, an instance of MyEvtTestButton already has its own | |
424 | // event handlers (see class definition); | |
425 | ||
426 | // Add a dynamic handler for this button event in the parent frame | |
427 | Connect(m_testBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, | |
428 | wxCommandEventHandler(MyFrame::OnClickDynamicHandlerFrame)); | |
429 | ||
430 | // Bind a method of this frame (notice "this" argument!) to the button | |
431 | // itself | |
432 | m_testBtn->Connect(wxEVT_COMMAND_BUTTON_CLICKED, | |
433 | wxCommandEventHandler(MyFrame::OnClickDynamicHandlerButton), | |
434 | NULL, | |
435 | this); | |
436 | ||
437 | mainSizer->Add(m_testBtn); | |
438 | panel->SetSizer(mainSizer); | |
6164f93c VZ |
439 | } |
440 | ||
441 | MyFrame::~MyFrame() | |
442 | { | |
443 | // we must pop any remaining event handlers to avoid memory leaks and | |
444 | // crashes! | |
445 | while ( m_nPush-- != 0 ) | |
446 | { | |
07850a49 | 447 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
448 | } |
449 | } | |
450 | ||
451 | // ---------------------------------------------------------------------------- | |
452 | // standard event handlers | |
453 | // ---------------------------------------------------------------------------- | |
454 | ||
455 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
456 | { | |
07850a49 WS |
457 | // true is to force the frame to close |
458 | Close(true); | |
6164f93c VZ |
459 | } |
460 | ||
461 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
462 | { | |
8584ae1c VZ |
463 | wxMessageBox("Event sample shows different ways of using events\n" |
464 | "(c) 2001-2009 Vadim Zeitlin", | |
465 | "About wxWidgets Event Sample", | |
466 | wxOK | wxICON_INFORMATION, this); | |
6164f93c VZ |
467 | } |
468 | ||
7c9cc312 VZ |
469 | void MyFrame::OnClickStaticHandlerFrame(wxCommandEvent& event) |
470 | { | |
471 | wxLogMessage("Step 6, 4 in \"How Events are Processed\":\n" | |
472 | "parentWin::StaticHandler_InFrameTable"); | |
473 | ||
474 | event.Skip(); | |
475 | } | |
476 | ||
6164f93c VZ |
477 | // ---------------------------------------------------------------------------- |
478 | // dynamic event handling stuff | |
479 | // ---------------------------------------------------------------------------- | |
480 | ||
7c9cc312 VZ |
481 | void MyFrame::OnClickDynamicHandlerFrame(wxCommandEvent& event) |
482 | { | |
483 | wxLogMessage("Step 6, 3 in \"How Events are Processed\":\n" | |
484 | "parentWin::DynamicHandler_InFrameTable"); | |
485 | ||
486 | event.Skip(); | |
487 | } | |
488 | ||
489 | void MyFrame::OnClickDynamicHandlerButton(wxCommandEvent& event) | |
490 | { | |
491 | wxLogMessage("Step 3 in \"How Events are Processed\":\n" | |
492 | "parentWin::DynamicHandler_InButtonTable"); | |
493 | ||
494 | event.Skip(); | |
495 | } | |
496 | ||
8584ae1c | 497 | void MyFrame::OnDynamic(wxCommandEvent& event) |
6164f93c | 498 | { |
8584ae1c VZ |
499 | wxString origin; |
500 | if ( event.GetEventObject() == this ) | |
501 | origin = "menu item"; | |
502 | else if ( event.GetEventObject() == m_btnDynamic ) | |
503 | origin = "button"; | |
504 | else | |
505 | origin = "unknown event source"; | |
506 | ||
f565a6c2 VZ |
507 | wxMessageBox |
508 | ( | |
8584ae1c VZ |
509 | "This message box is shown from the dynamically connected " |
510 | "event handler in response to event generated by " + origin, | |
511 | "wxWidgets Event Sample", wxOK | wxICON_INFORMATION, this | |
f565a6c2 | 512 | ); |
6164f93c VZ |
513 | } |
514 | ||
890d70eb | 515 | #ifdef wxHAS_EVENT_BIND |
8584ae1c VZ |
516 | |
517 | void MyFrame::OnBind(wxCommandEvent& event) | |
518 | { | |
519 | if ( event.IsChecked() ) | |
520 | { | |
521 | // as we bind directly to the button, there is no need to use an id | |
522 | // here: the button will only ever get its own events | |
523 | m_btnDynamic->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnDynamic, | |
524 | this); | |
525 | ||
526 | // but we do need the id for the menu command as the frame gets all of | |
527 | // them | |
528 | Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this, | |
529 | Event_Dynamic); | |
530 | } | |
531 | else // disconnect | |
532 | { | |
533 | m_btnDynamic->Unbind(wxEVT_COMMAND_BUTTON_CLICKED, | |
534 | &MyFrame::OnDynamic, this); | |
535 | Unbind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this, | |
536 | Event_Dynamic); | |
537 | } | |
538 | ||
539 | UpdateDynamicStatus(event.IsChecked()); | |
540 | } | |
541 | ||
890d70eb | 542 | #endif // wxHAS_EVENT_BIND |
8584ae1c | 543 | |
6164f93c VZ |
544 | void MyFrame::OnConnect(wxCommandEvent& event) |
545 | { | |
546 | if ( event.IsChecked() ) | |
547 | { | |
8584ae1c VZ |
548 | m_btnDynamic->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, |
549 | wxCommandEventHandler(MyFrame::OnDynamic), | |
550 | NULL, this); | |
862c917d VZ |
551 | Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED, |
552 | wxCommandEventHandler(MyFrame::OnDynamic)); | |
6164f93c | 553 | } |
862c917d | 554 | else // disconnect |
6164f93c | 555 | { |
8584ae1c VZ |
556 | m_btnDynamic->Disconnect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, |
557 | wxCommandEventHandler(MyFrame::OnDynamic), | |
558 | NULL, this); | |
862c917d VZ |
559 | Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED, |
560 | wxCommandEventHandler(MyFrame::OnDynamic)); | |
6164f93c | 561 | } |
8584ae1c VZ |
562 | |
563 | UpdateDynamicStatus(event.IsChecked()); | |
6164f93c VZ |
564 | } |
565 | ||
566 | // ---------------------------------------------------------------------------- | |
567 | // push/pop event handlers support | |
568 | // ---------------------------------------------------------------------------- | |
569 | ||
570 | void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event)) | |
571 | { | |
572 | PushEventHandler(new MyEvtHandler(++m_nPush)); | |
573 | ||
8520f137 | 574 | #if wxUSE_STATUSBAR |
9a83f860 | 575 | SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 576 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
577 | } |
578 | ||
579 | void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event)) | |
580 | { | |
9a83f860 | 581 | wxCHECK_RET( m_nPush, wxT("this command should be disabled!") ); |
6164f93c | 582 | |
07850a49 | 583 | PopEventHandler(true /* delete handler */); |
6164f93c VZ |
584 | m_nPush--; |
585 | ||
8520f137 | 586 | #if wxUSE_STATUSBAR |
9a83f860 | 587 | SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush), Status_Push); |
8520f137 | 588 | #endif // wxUSE_STATUSBAR |
6164f93c VZ |
589 | } |
590 | ||
87728739 | 591 | void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event)) |
6164f93c | 592 | { |
9a83f860 | 593 | wxLogMessage(wxT("This is the test event handler in the main frame")); |
6164f93c VZ |
594 | } |
595 | ||
596 | void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event) | |
597 | { | |
598 | event.Enable( m_nPush > 0 ); | |
599 | } | |
600 | ||
4ec2df6c VZ |
601 | // ---------------------------------------------------------------------------- |
602 | // custom event methods | |
603 | // ---------------------------------------------------------------------------- | |
604 | ||
87728739 | 605 | void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c VZ |
606 | { |
607 | wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND); | |
608 | ||
609 | wxPostEvent(this, eventCustom); | |
610 | } | |
611 | ||
87728739 | 612 | void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event)) |
4ec2df6c | 613 | { |
9a83f860 | 614 | wxLogMessage(wxT("Got a custom event!")); |
4ec2df6c VZ |
615 | } |
616 |