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)
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 void OnConnect(wxCommandEvent
& event
);
81 void OnDynamic(wxCommandEvent
& event
);
82 void OnPushEventHandler(wxCommandEvent
& event
);
83 void OnPopEventHandler(wxCommandEvent
& event
);
84 void OnTest(wxCommandEvent
& event
);
86 void OnFireCustom(wxCommandEvent
& event
);
87 void OnProcessCustom(wxCommandEvent
& event
);
89 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
92 // number of pushed event handlers
96 // any class wishing to process wxWidgets events must use this macro
100 // Define a custom event handler
101 class MyEvtHandler
: public wxEvtHandler
104 MyEvtHandler(size_t level
) { m_level
= level
; }
106 void OnTest(wxCommandEvent
& event
)
108 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level
);
110 // if we don't skip the event, the other event handlers won't get it:
111 // try commenting out this line and see what changes
118 DECLARE_EVENT_TABLE()
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // IDs for the controls and the menu commands
147 // ----------------------------------------------------------------------------
148 // event tables and other macros for wxWidgets
149 // ----------------------------------------------------------------------------
151 // the event tables connect the wxWidgets events with the functions (event
152 // handlers) which process them. It can be also done at run-time, but for the
153 // simple menu events like this the static method is much simpler.
154 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
155 EVT_MENU(Event_Quit
, MyFrame::OnQuit
)
156 EVT_MENU(Event_About
, MyFrame::OnAbout
)
158 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
160 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
161 EVT_MENU(Event_Test
, MyFrame::OnTest
)
162 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
163 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
165 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
167 EVT_MY_CUSTOM_COMMAND(wxID_ANY
, MyFrame::OnProcessCustom
)
169 // the line below would also work if OnProcessCustom() were defined as
170 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
171 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
174 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
175 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
178 // Create a new application object: this macro will allow wxWidgets to create
179 // the application object during program execution (it's better than using a
180 // static object for many reasons) and also declares the accessor function
181 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
185 // ============================================================================
187 // ============================================================================
189 // ----------------------------------------------------------------------------
190 // the application class
191 // ----------------------------------------------------------------------------
193 // 'Main program' equivalent: the program execution "starts" here
196 if ( !wxApp::OnInit() )
199 // create the main application window
200 MyFrame
*frame
= new MyFrame(_T("Event wxWidgets Sample"),
201 wxPoint(50, 50), wxSize(600, 340));
203 // and show it (the frames, unlike simple controls, are not shown when
204 // created initially)
207 // success: wxApp::OnRun() will be called which will enter the main message
208 // loop and the application will run. If we returned false here, the
209 // application would exit immediately.
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
218 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
219 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
221 SetIcon(wxICON(sample
));
227 wxMenu
*menuFile
= new wxMenu
;
229 menuFile
->Append(Event_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
230 menuFile
->AppendSeparator();
231 menuFile
->Append(Event_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
233 wxMenu
*menuEvent
= new wxMenu
;
234 menuEvent
->Append(Event_Connect
, _T("&Connect\tCtrl-C"),
235 _T("Connect or disconnect the dynamic event handler"),
236 true /* checkable */);
237 menuEvent
->Append(Event_Dynamic
, _T("&Dynamic event\tCtrl-D"),
238 _T("Dynamic event sample - only works after Connect"));
239 menuEvent
->AppendSeparator();
240 menuEvent
->Append(Event_Push
, _T("&Push event handler\tCtrl-P"),
241 _T("Push event handler for test event"));
242 menuEvent
->Append(Event_Pop
, _T("P&op event handler\tCtrl-O"),
243 _T("Pop event handler for test event"));
244 menuEvent
->Append(Event_Test
, _T("Test event\tCtrl-T"),
245 _T("Test event processed by pushed event handler"));
246 menuEvent
->AppendSeparator();
247 menuEvent
->Append(Event_Custom
, _T("Fire c&ustom event\tCtrl-U"),
248 _T("Generate a custom event"));
250 // now append the freshly created menu to the menu bar...
251 wxMenuBar
*menuBar
= new wxMenuBar();
252 menuBar
->Append(menuFile
, _T("&File"));
253 menuBar
->Append(menuEvent
, _T("&Event"));
255 // ... and attach this menu bar to the frame
260 SetStatusText(_T("Welcome to wxWidgets event sample"));
261 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
262 SetStatusText(_T("Push count: 0"), Status_Push
);
263 #endif // wxUSE_STATUSBAR
268 // we must pop any remaining event handlers to avoid memory leaks and
270 while ( m_nPush
-- != 0 )
272 PopEventHandler(true /* delete handler */);
276 // ----------------------------------------------------------------------------
277 // standard event handlers
278 // ----------------------------------------------------------------------------
280 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
282 // true is to force the frame to close
286 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
288 wxMessageBox( wxT("Event sample shows different ways of using events\n")
289 wxT("(c) 2001 Vadim Zeitlin"),
290 wxT("About Event Sample"), wxOK
| wxICON_INFORMATION
, this );
293 // ----------------------------------------------------------------------------
294 // dynamic event handling stuff
295 // ----------------------------------------------------------------------------
297 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
301 wxT("This is a dynamic event handler which can be connected ")
302 wxT("and disconnected at run-time."),
303 wxT("Dynamic Event Handler"), wxOK
| wxICON_INFORMATION
, this
307 void MyFrame::OnConnect(wxCommandEvent
& event
)
309 if ( event
.IsChecked() )
311 Connect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
312 wxCommandEventHandler(MyFrame::OnDynamic
));
315 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
316 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
317 #endif // wxUSE_STATUSBAR
321 Disconnect(Event_Dynamic
, wxEVT_COMMAND_MENU_SELECTED
,
322 wxCommandEventHandler(MyFrame::OnDynamic
));
325 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
326 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
327 #endif // wxUSE_STATUSBAR
331 // ----------------------------------------------------------------------------
332 // push/pop event handlers support
333 // ----------------------------------------------------------------------------
335 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
337 PushEventHandler(new MyEvtHandler(++m_nPush
));
340 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
341 #endif // wxUSE_STATUSBAR
344 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
346 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
348 PopEventHandler(true /* delete handler */);
352 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
353 #endif // wxUSE_STATUSBAR
356 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
358 wxLogMessage(_T("This is the test event handler in the main frame"));
361 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
363 event
.Enable( m_nPush
> 0 );
366 // ----------------------------------------------------------------------------
367 // custom event methods
368 // ----------------------------------------------------------------------------
370 void MyFrame::OnFireCustom(wxCommandEvent
& WXUNUSED(event
))
372 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
374 wxPostEvent(this, eventCustom
);
377 void MyFrame::OnProcessCustom(wxCommandEvent
& WXUNUSED(event
))
379 wxLogMessage(_T("Got a custom event!"));