1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating different event usage
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001-2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
34 #include "../sample.xpm"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // define a custom event type (we don't need a separate declaration here but
42 // usually you would use a matching wxDECLARE_EVENT in a header)
43 wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND
, wxCommandEvent
);
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( \
48 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
49 wxCommandEventHandler(fn), \
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // Define a new application type, each program should derive a class from wxApp
58 class MyApp
: public wxApp
61 // override base class virtuals
62 // ----------------------------
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();
70 // Define a new frame type: this is going to be our main frame
71 class MyFrame
: public wxFrame
75 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
80 #ifdef wxHAS_EVENT_BIND
81 void OnBind(wxCommandEvent
& event
);
82 #endif // wxHAS_EVENT_BIND
83 void OnConnect(wxCommandEvent
& event
);
84 void OnDynamic(wxCommandEvent
& event
);
85 void OnPushEventHandler(wxCommandEvent
& event
);
86 void OnPopEventHandler(wxCommandEvent
& event
);
87 void OnTest(wxCommandEvent
& event
);
89 void OnFireCustom(wxCommandEvent
& event
);
90 void OnProcessCustom(wxCommandEvent
& event
);
92 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
95 // symbolic names for the status bar fields
103 void UpdateDynamicStatus(bool on
)
108 SetStatusText("You can now use \"Dynamic\" item in the menu");
109 SetStatusText("Dynamic: on", Status_Dynamic
);
113 SetStatusText("You can no more use \"Dynamic\" item in the menu");
114 SetStatusText("Dynamic: off", Status_Dynamic
);
116 #endif // wxUSE_STATUSBAR
119 // number of pushed event handlers
122 // the button to whose event we connect dynamically
123 wxButton
*m_btnDynamic
;
125 // any class wishing to process wxWidgets events must use this macro
126 DECLARE_EVENT_TABLE()
129 // Define a custom event handler
130 class MyEvtHandler
: public wxEvtHandler
133 MyEvtHandler(size_t level
) { m_level
= level
; }
135 void OnTest(wxCommandEvent
& event
)
137 wxLogMessage(wxT("This is the pushed test event handler #%u"), m_level
);
139 // if we don't skip the event, the other event handlers won't get it:
140 // try commenting out this line and see what changes
147 DECLARE_EVENT_TABLE()
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 // IDs for the controls and the menu commands
169 // ----------------------------------------------------------------------------
170 // event tables and other macros for wxWidgets
171 // ----------------------------------------------------------------------------
173 // the event tables connect the wxWidgets events with the functions (event
174 // handlers) which process them. It can be also done at run-time, but for the
175 // simple menu events like this the static method is much simpler.
176 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
177 EVT_MENU(Event_Quit
, MyFrame::OnQuit
)
178 EVT_MENU(Event_About
, MyFrame::OnAbout
)
180 #ifdef wxHAS_EVENT_BIND
181 EVT_MENU(Event_Bind
, MyFrame::OnBind
)
182 #endif // wxHAS_EVENT_BIND
183 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
185 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
186 EVT_MENU(Event_Test
, MyFrame::OnTest
)
187 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
188 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
190 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
192 EVT_MY_CUSTOM_COMMAND(wxID_ANY
, MyFrame::OnProcessCustom
)
194 // the line below would also work if OnProcessCustom() were defined as
195 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
196 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
199 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
200 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
203 // Create a new application object: this macro will allow wxWidgets to create
204 // the application object during program execution (it's better than using a
205 // static object for many reasons) and also declares the accessor function
206 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
210 // ============================================================================
212 // ============================================================================
214 // ----------------------------------------------------------------------------
215 // the application class
216 // ----------------------------------------------------------------------------
218 // 'Main program' equivalent: the program execution "starts" here
221 if ( !wxApp::OnInit() )
224 // create the main application window
225 MyFrame
*frame
= new MyFrame(wxT("Event wxWidgets Sample"),
226 wxPoint(50, 50), wxSize(600, 340));
228 // and show it (the frames, unlike simple controls, are not shown when
229 // created initially)
232 // success: wxApp::OnRun() will be called which will enter the main message
233 // loop and the application will run. If we returned false here, the
234 // application would exit immediately.
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
243 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
244 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
)
246 SetIcon(wxICON(sample
));
253 wxMenu
*menuFile
= new wxMenu
;
255 menuFile
->Append(Event_About
, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
256 menuFile
->AppendSeparator();
257 menuFile
->Append(Event_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
259 wxMenu
*menuEvent
= new wxMenu
;
260 #ifdef wxHAS_EVENT_BIND
261 menuEvent
->AppendCheckItem(Event_Bind
, "&Bind\tCtrl-B",
262 "Bind or unbind a dynamic event handler");
263 #endif // wxHAS_EVENT_BIND
264 menuEvent
->AppendCheckItem(Event_Connect
, wxT("&Connect\tCtrl-C"),
265 wxT("Connect or disconnect the dynamic event handler"));
266 menuEvent
->Append(Event_Dynamic
, wxT("&Dynamic event\tCtrl-D"),
267 wxT("Dynamic event sample - only works after Connect"));
268 menuEvent
->AppendSeparator();
269 menuEvent
->Append(Event_Push
, wxT("&Push event handler\tCtrl-P"),
270 wxT("Push event handler for test event"));
271 menuEvent
->Append(Event_Pop
, wxT("P&op event handler\tCtrl-O"),
272 wxT("Pop event handler for test event"));
273 menuEvent
->Append(Event_Test
, wxT("Test event\tCtrl-T"),
274 wxT("Test event processed by pushed event handler"));
275 menuEvent
->AppendSeparator();
276 menuEvent
->Append(Event_Custom
, wxT("Fire c&ustom event\tCtrl-U"),
277 wxT("Generate a custom event"));
279 // now append the freshly created menu to the menu bar...
280 wxMenuBar
*menuBar
= new wxMenuBar();
281 menuBar
->Append(menuFile
, wxT("&File"));
282 menuBar
->Append(menuEvent
, wxT("&Event"));
284 // ... and attach this menu bar to the frame
289 SetStatusText(wxT("Welcome to wxWidgets event sample"));
290 SetStatusText(wxT("Dynamic: off"), Status_Dynamic
);
291 SetStatusText(wxT("Push count: 0"), Status_Push
);
292 #endif // wxUSE_STATUSBAR
294 wxPanel
* const panel
= new wxPanel(this);
295 wxSizer
* const sizer
= new wxBoxSizer(wxHORIZONTAL
);
296 const wxSizerFlags
centreY(wxSizerFlags().Centre().Border());
297 sizer
->Add(new wxStaticText(panel
, wxID_ANY
,
298 "This button will only work if its handler is dynamically connected"),
300 m_btnDynamic
= new wxButton(panel
, Event_Dynamic
, "&Dynamic button");
301 sizer
->Add(m_btnDynamic
, centreY
);
302 panel
->SetSizer(sizer
);
307 // we must pop any remaining event handlers to avoid memory leaks and
309 while ( m_nPush
-- != 0 )
311 PopEventHandler(true /* delete handler */);
315 // ----------------------------------------------------------------------------
316 // standard event handlers
317 // ----------------------------------------------------------------------------
319 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
321 // true is to force the frame to close
325 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
327 wxMessageBox("Event sample shows different ways of using events\n"
328 "(c) 2001-2009 Vadim Zeitlin",
329 "About wxWidgets Event Sample",
330 wxOK
| wxICON_INFORMATION
, this);
333 // ----------------------------------------------------------------------------
334 // dynamic event handling stuff
335 // ----------------------------------------------------------------------------
337 void MyFrame::OnDynamic(wxCommandEvent
& event
)
340 if ( event
.GetEventObject() == this )
341 origin
= "menu item";
342 else if ( event
.GetEventObject() == m_btnDynamic
)
345 origin
= "unknown event source";
349 "This message box is shown from the dynamically connected "
350 "event handler in response to event generated by " + origin
,
351 "wxWidgets Event Sample", wxOK
| wxICON_INFORMATION
, this
355 #ifdef wxHAS_EVENT_BIND
357 void MyFrame::OnBind(wxCommandEvent
& event
)
359 if ( event
.IsChecked() )
361 // as we bind directly to the button, there is no need to use an id
362 // here: the button will only ever get its own events
363 m_btnDynamic
->Bind(wxEVT_COMMAND_BUTTON_CLICKED
, &MyFrame::OnDynamic
,
366 // but we do need the id for the menu command as the frame gets all of
368 Bind(wxEVT_COMMAND_MENU_SELECTED
, &MyFrame::OnDynamic
, this,
373 m_btnDynamic
->Unbind(wxEVT_COMMAND_BUTTON_CLICKED
,
374 &MyFrame::OnDynamic
, this);
375 Unbind(wxEVT_COMMAND_MENU_SELECTED
, &MyFrame::OnDynamic
, this,
379 UpdateDynamicStatus(event
.IsChecked());
382 #endif // wxHAS_EVENT_BIND
384 void MyFrame::OnConnect(wxCommandEvent
& event
)
386 if ( event
.IsChecked() )
388 m_btnDynamic
->Connect(wxID_ANY
, wxEVT_COMMAND_BUTTON_CLICKED
,
389 wxCommandEventHandler(MyFrame::OnDynamic
),
391 Connect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
392 wxCommandEventHandler(MyFrame::OnDynamic
));
396 m_btnDynamic
->Disconnect(wxID_ANY
, wxEVT_COMMAND_BUTTON_CLICKED
,
397 wxCommandEventHandler(MyFrame::OnDynamic
),
399 Disconnect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
400 wxCommandEventHandler(MyFrame::OnDynamic
));
403 UpdateDynamicStatus(event
.IsChecked());
406 // ----------------------------------------------------------------------------
407 // push/pop event handlers support
408 // ----------------------------------------------------------------------------
410 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
412 PushEventHandler(new MyEvtHandler(++m_nPush
));
415 SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush
), Status_Push
);
416 #endif // wxUSE_STATUSBAR
419 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
421 wxCHECK_RET( m_nPush
, wxT("this command should be disabled!") );
423 PopEventHandler(true /* delete handler */);
427 SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush
), Status_Push
);
428 #endif // wxUSE_STATUSBAR
431 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
433 wxLogMessage(wxT("This is the test event handler in the main frame"));
436 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
438 event
.Enable( m_nPush
> 0 );
441 // ----------------------------------------------------------------------------
442 // custom event methods
443 // ----------------------------------------------------------------------------
445 void MyFrame::OnFireCustom(wxCommandEvent
& WXUNUSED(event
))
447 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
449 wxPostEvent(this, eventCustom
);
452 void MyFrame::OnProcessCustom(wxCommandEvent
& WXUNUSED(event
))
454 wxLogMessage(wxT("Got a custom event!"));