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 // declare a custom event type
39 // note that in wxWin 2.3+ these macros expand simply into the following code:
41 // extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
43 // const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
45 // and you may use this code directly if you don't care about 2.2 compatibility
46 BEGIN_DECLARE_EVENT_TYPES()
47 DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND
, 7777)
48 END_DECLARE_EVENT_TYPES()
50 DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND
)
52 // it may also be convenient to define an event table macro for this event type
53 #define EVT_MY_CUSTOM_COMMAND(id, fn) \
54 DECLARE_EVENT_TABLE_ENTRY( \
55 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
56 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp
: public wxApp
68 // override base class virtuals
69 // ----------------------------
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame
: public wxFrame
82 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
85 void OnQuit(wxCommandEvent
& event
);
86 void OnAbout(wxCommandEvent
& event
);
87 void OnConnect(wxCommandEvent
& event
);
88 void OnDynamic(wxCommandEvent
& event
);
89 void OnPushEventHandler(wxCommandEvent
& event
);
90 void OnPopEventHandler(wxCommandEvent
& event
);
91 void OnTest(wxCommandEvent
& event
);
93 void OnFireCustom(wxCommandEvent
& event
);
94 void OnProcessCustom(wxCommandEvent
& event
);
96 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
99 // number of pushed event handlers
103 // any class wishing to process wxWidgets events must use this macro
104 DECLARE_EVENT_TABLE()
107 // Define a custom event handler
108 class MyEvtHandler
: public wxEvtHandler
111 MyEvtHandler(size_t level
) { m_level
= level
; }
113 void OnTest(wxCommandEvent
& event
)
115 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level
);
117 // if we don't skip the event, the other event handlers won't get it:
118 // try commenting out this line and see what changes
125 DECLARE_EVENT_TABLE()
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 // IDs for the controls and the menu commands
154 // ----------------------------------------------------------------------------
155 // event tables and other macros for wxWidgets
156 // ----------------------------------------------------------------------------
158 // the event tables connect the wxWidgets events with the functions (event
159 // handlers) which process them. It can be also done at run-time, but for the
160 // simple menu events like this the static method is much simpler.
161 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
162 EVT_MENU(Event_Quit
, MyFrame::OnQuit
)
163 EVT_MENU(Event_About
, MyFrame::OnAbout
)
165 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
167 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
168 EVT_MENU(Event_Test
, MyFrame::OnTest
)
169 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
170 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
172 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
174 EVT_MY_CUSTOM_COMMAND(wxID_ANY
, MyFrame::OnProcessCustom
)
176 // the line below would also work if OnProcessCustom() were defined as
177 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
178 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
181 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
182 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
185 // Create a new application object: this macro will allow wxWidgets to create
186 // the application object during program execution (it's better than using a
187 // static object for many reasons) and also declares the accessor function
188 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
197 // the application class
198 // ----------------------------------------------------------------------------
200 // 'Main program' equivalent: the program execution "starts" here
203 // create the main application window
204 MyFrame
*frame
= new MyFrame(_T("Event wxWidgets Sample"),
205 wxPoint(50, 50), wxSize(600, 340));
207 // and show it (the frames, unlike simple controls, are not shown when
208 // created initially)
211 // success: wxApp::OnRun() will be called which will enter the main message
212 // loop and the application will run. If we returned false here, the
213 // application would exit immediately.
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
223 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
229 wxMenu
*menuFile
= new wxMenu
;
231 menuFile
->Append(Event_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
232 menuFile
->AppendSeparator();
233 menuFile
->Append(Event_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
235 wxMenu
*menuEvent
= new wxMenu
;
236 menuEvent
->Append(Event_Connect
, _T("&Connect\tCtrl-C"),
237 _T("Connect or disconnect the dynamic event handler"),
238 true /* checkable */);
239 menuEvent
->Append(Event_Dynamic
, _T("&Dynamic event\tCtrl-D"),
240 _T("Dynamic event sample - only works after Connect"));
241 menuEvent
->AppendSeparator();
242 menuEvent
->Append(Event_Push
, _T("&Push event handler\tCtrl-P"),
243 _T("Push event handler for test event"));
244 menuEvent
->Append(Event_Pop
, _T("P&op event handler\tCtrl-O"),
245 _T("Pop event handler for test event"));
246 menuEvent
->Append(Event_Test
, _T("Test event\tCtrl-T"),
247 _T("Test event processed by pushed event handler"));
248 menuEvent
->AppendSeparator();
249 menuEvent
->Append(Event_Custom
, _T("Fire c&ustom event\tCtrl-U"),
250 _T("Generate a custom event"));
252 // now append the freshly created menu to the menu bar...
253 wxMenuBar
*menuBar
= new wxMenuBar();
254 menuBar
->Append(menuFile
, _T("&File"));
255 menuBar
->Append(menuEvent
, _T("&Event"));
257 // ... and attach this menu bar to the frame
262 SetStatusText(_T("Welcome to wxWidgets event sample"));
263 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
264 SetStatusText(_T("Push count: 0"), Status_Push
);
265 #endif // wxUSE_STATUSBAR
270 // we must pop any remaining event handlers to avoid memory leaks and
272 while ( m_nPush
-- != 0 )
274 PopEventHandler(true /* delete handler */);
278 // ----------------------------------------------------------------------------
279 // standard event handlers
280 // ----------------------------------------------------------------------------
282 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
284 // true is to force the frame to close
288 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
290 wxMessageBox( wxT("Event sample shows different ways of using events\n")
291 wxT("(c) 2001 Vadim Zeitlin"),
292 wxT("About Event Sample"), wxOK
| wxICON_INFORMATION
, this );
295 // ----------------------------------------------------------------------------
296 // dynamic event handling stuff
297 // ----------------------------------------------------------------------------
299 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
303 wxT("This is a dynamic event handler which can be connected ")
304 wxT("and disconnected at run-time."),
305 wxT("Dynamic Event Handler"), wxOK
| wxICON_INFORMATION
, this
309 void MyFrame::OnConnect(wxCommandEvent
& event
)
311 if ( event
.IsChecked() )
314 Connect(Event_Dynamic
, wxID_ANY
, wxEVT_COMMAND_MENU_SELECTED
,
315 (wxObjectEventFunction
)
317 (wxCommandEventFunction
)&MyFrame::OnDynamic
);
320 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
321 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
322 #endif // wxUSE_STATUSBAR
326 Disconnect(Event_Dynamic
, wxID_ANY
, wxEVT_COMMAND_MENU_SELECTED
);
329 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
330 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
331 #endif // wxUSE_STATUSBAR
335 // ----------------------------------------------------------------------------
336 // push/pop event handlers support
337 // ----------------------------------------------------------------------------
339 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
341 PushEventHandler(new MyEvtHandler(++m_nPush
));
344 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
345 #endif // wxUSE_STATUSBAR
348 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
350 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
352 PopEventHandler(true /* delete handler */);
356 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
357 #endif // wxUSE_STATUSBAR
360 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
362 wxLogMessage(_T("This is the test event handler in the main frame"));
365 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
367 event
.Enable( m_nPush
> 0 );
370 // ----------------------------------------------------------------------------
371 // custom event methods
372 // ----------------------------------------------------------------------------
374 void MyFrame::OnFireCustom(wxCommandEvent
& WXUNUSED(event
))
376 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
378 wxPostEvent(this, eventCustom
);
381 void MyFrame::OnProcessCustom(wxCommandEvent
& WXUNUSED(event
))
383 wxLogMessage(_T("Got a custom event!"));