1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating different event usage
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001-2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers)
32 #ifndef wxHAS_IMAGES_IN_RESOURCES
33 #include "../sample.xpm"
36 #include <wx/statline.h>
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // define a custom event type (we don't need a separate declaration here but
44 // usually you would use a matching wxDECLARE_EVENT in a header)
45 wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND
, wxCommandEvent
);
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( \
50 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
51 wxCommandEventHandler(fn), \
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
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();
71 // these are regular event handlers used to highlight the events handling
73 void OnClickDynamicHandlerApp(wxCommandEvent
& event
);
74 void OnClickStaticHandlerApp(wxCommandEvent
& event
);
76 // we override wxConsoleApp::FilterEvent used to highlight the events
78 virtual int FilterEvent(wxEvent
& event
);
84 // Define a custom button used to highlight the events handling order
85 class MyEvtTestButton
: public wxButton
88 static long BUTTON_ID
;
90 MyEvtTestButton(wxWindow
*parent
, const wxString
& label
)
91 : wxButton(parent
, BUTTON_ID
, label
)
93 // Add a dynamic handler for this button event to button itself
95 wxCommandEventHandler(MyEvtTestButton::OnClickDynamicHandler
));
99 void OnClickDynamicHandler(wxCommandEvent
& event
)
101 wxLogMessage("Step 3 in \"How Events are Processed\":\n"
102 "Button::ownDynamicHandler");
107 void OnClickStaticHandler(wxCommandEvent
& event
)
109 wxLogMessage("Step 4 in \"How Events are Processed\":\n"
110 "Button::ownStaticHandler");
115 DECLARE_EVENT_TABLE()
118 long MyEvtTestButton::BUTTON_ID
= wxNewId();
120 // Define a new frame type: this is going to be our main frame
121 class MyFrame
: public wxFrame
125 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
128 void OnQuit(wxCommandEvent
& event
);
129 void OnAbout(wxCommandEvent
& event
);
130 #ifdef wxHAS_EVENT_BIND
131 void OnBind(wxCommandEvent
& event
);
132 #endif // wxHAS_EVENT_BIND
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
);
139 void OnFireCustom(wxCommandEvent
& event
);
140 void OnProcessCustom(wxCommandEvent
& event
);
142 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
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
);
150 // symbolic names for the status bar fields
158 void UpdateDynamicStatus(bool on
)
163 SetStatusText("You can now use \"Dynamic\" item in the menu");
164 SetStatusText("Dynamic: on", Status_Dynamic
);
168 SetStatusText("You can no more use \"Dynamic\" item in the menu");
169 SetStatusText("Dynamic: off", Status_Dynamic
);
171 #endif // wxUSE_STATUSBAR
174 // number of pushed event handlers
177 // the button to whose event we connect dynamically
178 wxButton
*m_btnDynamic
;
180 // the button used to highlight the event handlers execution order
181 MyEvtTestButton
*m_testBtn
;
184 // any class wishing to process wxWidgets events must use this macro
185 DECLARE_EVENT_TABLE()
188 // Define a custom event handler
189 class MyEvtHandler
: public wxEvtHandler
192 MyEvtHandler(size_t level
) { m_level
= level
; }
194 void OnTest(wxCommandEvent
& event
)
196 wxLogMessage(wxT("This is the pushed test event handler #%u"), m_level
);
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
206 DECLARE_EVENT_TABLE()
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 // IDs for the controls and the menu commands
228 // ----------------------------------------------------------------------------
229 // event tables and other macros for wxWidgets
230 // ----------------------------------------------------------------------------
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
)
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
)
244 // This can be also done at run-time, but for the
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
)
250 #ifdef wxHAS_EVENT_BIND
251 EVT_MENU(Event_Bind
, MyFrame::OnBind
)
252 #endif // wxHAS_EVENT_BIND
253 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
255 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
256 EVT_MENU(Event_Test
, MyFrame::OnTest
)
257 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
258 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
260 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
262 EVT_MY_CUSTOM_COMMAND(wxID_ANY
, MyFrame::OnProcessCustom
)
264 // the line below would also work if OnProcessCustom() were defined as
265 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
266 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
268 // Add a static handler in the parent frame for button event
269 EVT_BUTTON(MyEvtTestButton::BUTTON_ID
, MyFrame::OnClickStaticHandlerFrame
)
272 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
273 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
276 // Create a new application object: this macro will allow wxWidgets to create
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
283 // ============================================================================
285 // ============================================================================
287 // ----------------------------------------------------------------------------
288 // the application class
289 // ----------------------------------------------------------------------------
291 // 'Main program' equivalent: the program execution "starts" here
294 if ( !wxApp::OnInit() )
297 // create the main application window
298 MyFrame
*frame
= new MyFrame(wxT("Event wxWidgets Sample"),
299 wxPoint(50, 50), wxSize(600, 340));
301 // and show it (the frames, unlike simple controls, are not shown when
302 // created initially)
305 // Add a dynamic handler at the application level for the test button
306 Connect(MyEvtTestButton::BUTTON_ID
, wxEVT_BUTTON
,
307 wxCommandEventHandler(MyApp::OnClickDynamicHandlerApp
));
309 // success: wxApp::OnRun() will be called which will enter the main message
310 // loop and the application will run. If we returned false here, the
311 // application would exit immediately.
315 // This is always the first to handle an event !
316 int MyApp::FilterEvent(wxEvent
& event
)
318 if ( event
.GetEventType() == wxEVT_BUTTON
&&
319 event
.GetId() == MyEvtTestButton::BUTTON_ID
)
321 wxLogMessage("Step 0 in \"How Events are Processed\":\n"
325 return wxApp::FilterEvent(event
);
328 void MyApp::OnClickDynamicHandlerApp(wxCommandEvent
& event
)
330 wxLogMessage("Step 7, 3 in \"How Events are Processed\":\n"
331 "App::DynamicHandler_InAppTable");
336 void MyApp::OnClickStaticHandlerApp(wxCommandEvent
& event
)
338 wxLogMessage("Step 7, 4 in \"How Events are Processed\":\n"
339 "App::StaticHandler_InAppTable");
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 "
345 wxLog::FlushActive();
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
356 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
)
358 SetIcon(wxICON(sample
));
365 wxMenu
*menuFile
= new wxMenu
;
367 menuFile
->Append(Event_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
368 menuFile
->AppendSeparator();
369 menuFile
->Append(Event_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
371 wxMenu
*menuEvent
= new wxMenu
;
372 #ifdef wxHAS_EVENT_BIND
373 menuEvent
->AppendCheckItem(Event_Bind
, "&Bind\tCtrl-B",
374 "Bind or unbind a dynamic event handler");
375 #endif // wxHAS_EVENT_BIND
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"));
380 menuEvent
->AppendSeparator();
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"));
387 menuEvent
->AppendSeparator();
388 menuEvent
->Append(Event_Custom
, wxT("Fire c&ustom event\tCtrl-U"),
389 wxT("Generate a custom event"));
391 // now append the freshly created menu to the menu bar...
392 wxMenuBar
*menuBar
= new wxMenuBar();
393 menuBar
->Append(menuFile
, wxT("&File"));
394 menuBar
->Append(menuEvent
, wxT("&Event"));
396 // ... and attach this menu bar to the frame
401 SetStatusText(wxT("Welcome to wxWidgets event sample"));
402 SetStatusText(wxT("Dynamic: off"), Status_Dynamic
);
403 SetStatusText(wxT("Push count: 0"), Status_Push
);
404 #endif // wxUSE_STATUSBAR
406 wxPanel
* const panel
= new wxPanel(this);
407 wxSizer
* const mainSizer
= new wxBoxSizer(wxVERTICAL
);
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"),
413 m_btnDynamic
= new wxButton(panel
, Event_Dynamic
, "&Dynamic button");
414 sizer
->Add(m_btnDynamic
, centreY
);
416 mainSizer
->Add(sizer
, 1, wxEXPAND
);
417 mainSizer
->Add(new wxStaticLine(panel
), 0, wxEXPAND
);
418 mainSizer
->Add(new wxStaticLine(panel
), 0, wxEXPAND
);
420 m_testBtn
= new MyEvtTestButton(panel
, "Test Event Handlers Execution Order");
422 // After being created, an instance of MyEvtTestButton already has its own
423 // event handlers (see class definition);
425 // Add a dynamic handler for this button event in the parent frame
426 Connect(m_testBtn
->GetId(), wxEVT_BUTTON
,
427 wxCommandEventHandler(MyFrame::OnClickDynamicHandlerFrame
));
429 // Bind a method of this frame (notice "this" argument!) to the button
431 m_testBtn
->Connect(wxEVT_BUTTON
,
432 wxCommandEventHandler(MyFrame::OnClickDynamicHandlerButton
),
436 mainSizer
->Add(m_testBtn
);
437 panel
->SetSizer(mainSizer
);
442 // we must pop any remaining event handlers to avoid memory leaks and
444 while ( m_nPush
-- != 0 )
446 PopEventHandler(true /* delete handler */);
450 // ----------------------------------------------------------------------------
451 // standard event handlers
452 // ----------------------------------------------------------------------------
454 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
456 // true is to force the frame to close
460 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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);
468 void MyFrame::OnClickStaticHandlerFrame(wxCommandEvent
& event
)
470 wxLogMessage("Step 6, 4 in \"How Events are Processed\":\n"
471 "parentWin::StaticHandler_InFrameTable");
476 // ----------------------------------------------------------------------------
477 // dynamic event handling stuff
478 // ----------------------------------------------------------------------------
480 void MyFrame::OnClickDynamicHandlerFrame(wxCommandEvent
& event
)
482 wxLogMessage("Step 6, 3 in \"How Events are Processed\":\n"
483 "parentWin::DynamicHandler_InFrameTable");
488 void MyFrame::OnClickDynamicHandlerButton(wxCommandEvent
& event
)
490 wxLogMessage("Step 3 in \"How Events are Processed\":\n"
491 "parentWin::DynamicHandler_InButtonTable");
496 void MyFrame::OnDynamic(wxCommandEvent
& event
)
499 if ( event
.GetEventObject() == this )
500 origin
= "menu item";
501 else if ( event
.GetEventObject() == m_btnDynamic
)
504 origin
= "unknown event source";
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
514 #ifdef wxHAS_EVENT_BIND
516 void MyFrame::OnBind(wxCommandEvent
& event
)
518 if ( event
.IsChecked() )
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
522 m_btnDynamic
->Bind(wxEVT_BUTTON
, &MyFrame::OnDynamic
,
525 // but we do need the id for the menu command as the frame gets all of
527 Bind(wxEVT_MENU
, &MyFrame::OnDynamic
, this,
532 m_btnDynamic
->Unbind(wxEVT_BUTTON
,
533 &MyFrame::OnDynamic
, this);
534 Unbind(wxEVT_MENU
, &MyFrame::OnDynamic
, this,
538 UpdateDynamicStatus(event
.IsChecked());
541 #endif // wxHAS_EVENT_BIND
543 void MyFrame::OnConnect(wxCommandEvent
& event
)
545 if ( event
.IsChecked() )
547 m_btnDynamic
->Connect(wxID_ANY
, wxEVT_BUTTON
,
548 wxCommandEventHandler(MyFrame::OnDynamic
),
550 Connect(Event_Dynamic
, wxEVT_MENU
,
551 wxCommandEventHandler(MyFrame::OnDynamic
));
555 m_btnDynamic
->Disconnect(wxID_ANY
, wxEVT_BUTTON
,
556 wxCommandEventHandler(MyFrame::OnDynamic
),
558 Disconnect(Event_Dynamic
, wxEVT_MENU
,
559 wxCommandEventHandler(MyFrame::OnDynamic
));
562 UpdateDynamicStatus(event
.IsChecked());
565 // ----------------------------------------------------------------------------
566 // push/pop event handlers support
567 // ----------------------------------------------------------------------------
569 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
571 PushEventHandler(new MyEvtHandler(++m_nPush
));
574 SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush
), Status_Push
);
575 #endif // wxUSE_STATUSBAR
578 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
580 wxCHECK_RET( m_nPush
, wxT("this command should be disabled!") );
582 PopEventHandler(true /* delete handler */);
586 SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush
), Status_Push
);
587 #endif // wxUSE_STATUSBAR
590 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
592 wxLogMessage(wxT("This is the test event handler in the main frame"));
595 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
597 event
.Enable( m_nPush
> 0 );
600 // ----------------------------------------------------------------------------
601 // custom event methods
602 // ----------------------------------------------------------------------------
604 void MyFrame::OnFireCustom(wxCommandEvent
& WXUNUSED(event
))
606 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
608 wxPostEvent(this, eventCustom
);
611 void MyFrame::OnProcessCustom(wxCommandEvent
& WXUNUSED(event
))
613 wxLogMessage(wxT("Got a custom event!"));