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