1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating different event usage
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 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)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // define a custom event type (we don't need a separate declaration here but
38 // usually you would use a matching wxDECLARE_EVENT in a header)
39 wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND
, wxCommandEvent
)
41 // it may also be convenient to define an event table macro for this event type
42 #define EVT_MY_CUSTOM_COMMAND(id, fn) \
43 DECLARE_EVENT_TABLE_ENTRY( \
44 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
45 wxCommandEventHandler(fn), \
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // Define a new application type, each program should derive a class from wxApp
54 class MyApp
: public wxApp
57 // override base class virtuals
58 // ----------------------------
60 // this one is called on application startup and is a good place for the app
61 // initialization (doing it here and not in the ctor allows to have an error
62 // return: if OnInit() returns false, the application terminates)
63 virtual bool OnInit();
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame
: public wxFrame
71 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
74 void OnQuit(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
76 void OnConnect(wxCommandEvent
& event
);
77 void OnDynamic(wxCommandEvent
& event
);
78 void OnPushEventHandler(wxCommandEvent
& event
);
79 void OnPopEventHandler(wxCommandEvent
& event
);
80 void OnTest(wxCommandEvent
& event
);
82 void OnFireCustom(wxCommandEvent
& event
);
83 void OnProcessCustom(wxCommandEvent
& event
);
85 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
88 // number of pushed event handlers
92 // any class wishing to process wxWidgets events must use this macro
96 // Define a custom event handler
97 class MyEvtHandler
: public wxEvtHandler
100 MyEvtHandler(size_t level
) { m_level
= level
; }
102 void OnTest(wxCommandEvent
& event
)
104 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level
);
106 // if we don't skip the event, the other event handlers won't get it:
107 // try commenting out this line and see what changes
114 DECLARE_EVENT_TABLE()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 // IDs for the controls and the menu commands
143 // ----------------------------------------------------------------------------
144 // event tables and other macros for wxWidgets
145 // ----------------------------------------------------------------------------
147 // the event tables connect the wxWidgets events with the functions (event
148 // handlers) which process them. It can be also done at run-time, but for the
149 // simple menu events like this the static method is much simpler.
150 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
151 EVT_MENU(Event_Quit
, MyFrame::OnQuit
)
152 EVT_MENU(Event_About
, MyFrame::OnAbout
)
154 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
156 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
157 EVT_MENU(Event_Test
, MyFrame::OnTest
)
158 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
159 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
161 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
163 EVT_MY_CUSTOM_COMMAND(wxID_ANY
, MyFrame::OnProcessCustom
)
165 // the line below would also work if OnProcessCustom() were defined as
166 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
167 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
170 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
171 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
174 // Create a new application object: this macro will allow wxWidgets to create
175 // the application object during program execution (it's better than using a
176 // static object for many reasons) and also declares the accessor function
177 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
181 // ============================================================================
183 // ============================================================================
185 // ----------------------------------------------------------------------------
186 // the application class
187 // ----------------------------------------------------------------------------
189 // 'Main program' equivalent: the program execution "starts" here
192 if ( !wxApp::OnInit() )
195 // create the main application window
196 MyFrame
*frame
= new MyFrame(_T("Event wxWidgets Sample"),
197 wxPoint(50, 50), wxSize(600, 340));
199 // and show it (the frames, unlike simple controls, are not shown when
200 // created initially)
203 // success: wxApp::OnRun() will be called which will enter the main message
204 // loop and the application will run. If we returned false here, the
205 // application would exit immediately.
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
215 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
221 wxMenu
*menuFile
= new wxMenu
;
223 menuFile
->Append(Event_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
224 menuFile
->AppendSeparator();
225 menuFile
->Append(Event_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
227 wxMenu
*menuEvent
= new wxMenu
;
228 menuEvent
->Append(Event_Connect
, _T("&Connect\tCtrl-C"),
229 _T("Connect or disconnect the dynamic event handler"),
230 true /* checkable */);
231 menuEvent
->Append(Event_Dynamic
, _T("&Dynamic event\tCtrl-D"),
232 _T("Dynamic event sample - only works after Connect"));
233 menuEvent
->AppendSeparator();
234 menuEvent
->Append(Event_Push
, _T("&Push event handler\tCtrl-P"),
235 _T("Push event handler for test event"));
236 menuEvent
->Append(Event_Pop
, _T("P&op event handler\tCtrl-O"),
237 _T("Pop event handler for test event"));
238 menuEvent
->Append(Event_Test
, _T("Test event\tCtrl-T"),
239 _T("Test event processed by pushed event handler"));
240 menuEvent
->AppendSeparator();
241 menuEvent
->Append(Event_Custom
, _T("Fire c&ustom event\tCtrl-U"),
242 _T("Generate a custom event"));
244 // now append the freshly created menu to the menu bar...
245 wxMenuBar
*menuBar
= new wxMenuBar();
246 menuBar
->Append(menuFile
, _T("&File"));
247 menuBar
->Append(menuEvent
, _T("&Event"));
249 // ... and attach this menu bar to the frame
254 SetStatusText(_T("Welcome to wxWidgets event sample"));
255 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
256 SetStatusText(_T("Push count: 0"), Status_Push
);
257 #endif // wxUSE_STATUSBAR
262 // we must pop any remaining event handlers to avoid memory leaks and
264 while ( m_nPush
-- != 0 )
266 PopEventHandler(true /* delete handler */);
270 // ----------------------------------------------------------------------------
271 // standard event handlers
272 // ----------------------------------------------------------------------------
274 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
276 // true is to force the frame to close
280 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
282 wxMessageBox( wxT("Event sample shows different ways of using events\n")
283 wxT("(c) 2001 Vadim Zeitlin"),
284 wxT("About Event Sample"), wxOK
| wxICON_INFORMATION
, this );
287 // ----------------------------------------------------------------------------
288 // dynamic event handling stuff
289 // ----------------------------------------------------------------------------
291 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
295 wxT("This is a dynamic event handler which can be connected ")
296 wxT("and disconnected at run-time."),
297 wxT("Dynamic Event Handler"), wxOK
| wxICON_INFORMATION
, this
301 void MyFrame::OnConnect(wxCommandEvent
& event
)
303 if ( event
.IsChecked() )
305 Connect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
306 wxCommandEventHandler(MyFrame::OnDynamic
));
309 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
310 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
311 #endif // wxUSE_STATUSBAR
315 Disconnect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
316 wxCommandEventHandler(MyFrame::OnDynamic
));
319 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
320 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
321 #endif // wxUSE_STATUSBAR
325 // ----------------------------------------------------------------------------
326 // push/pop event handlers support
327 // ----------------------------------------------------------------------------
329 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
331 PushEventHandler(new MyEvtHandler(++m_nPush
));
334 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
335 #endif // wxUSE_STATUSBAR
338 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
340 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
342 PopEventHandler(true /* delete handler */);
346 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
347 #endif // wxUSE_STATUSBAR
350 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
352 wxLogMessage(_T("This is the test event handler in the main frame"));
355 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
357 event
.Enable( m_nPush
> 0 );
360 // ----------------------------------------------------------------------------
361 // custom event methods
362 // ----------------------------------------------------------------------------
364 void MyFrame::OnFireCustom(wxCommandEvent
& WXUNUSED(event
))
366 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
368 wxPostEvent(this, eventCustom
);
371 void MyFrame::OnProcessCustom(wxCommandEvent
& WXUNUSED(event
))
373 wxLogMessage(_T("Got a custom event!"));