]> git.saurik.com Git - wxWidgets.git/blob - samples/event/event.cpp
don't use deprecated toolbar API
[wxWidgets.git] / samples / event / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: event.cpp
3 // Purpose: wxWidgets sample demonstrating different event usage
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.01.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // event constants
35 // ----------------------------------------------------------------------------
36
37 // declare a custom event type
38 //
39 // note that in wxWin 2.3+ these macros expand simply into the following code:
40 //
41 // extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
42 //
43 // const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
44 //
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()
49
50 DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)
51
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 ), \
57 (wxObject *) NULL \
58 ),
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp : public wxApp
66 {
67 public:
68 // override base class virtuals
69 // ----------------------------
70
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();
75 };
76
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame : public wxFrame
79 {
80 public:
81 // ctor(s)
82 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
83 virtual ~MyFrame();
84
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);
92
93 void OnFireCustom(wxCommandEvent& event);
94 void OnProcessCustom(wxCommandEvent& event);
95
96 void OnUpdateUIPop(wxUpdateUIEvent& event);
97
98 protected:
99 // number of pushed event handlers
100 unsigned m_nPush;
101
102 private:
103 // any class wishing to process wxWidgets events must use this macro
104 DECLARE_EVENT_TABLE()
105 };
106
107 // Define a custom event handler
108 class MyEvtHandler : public wxEvtHandler
109 {
110 public:
111 MyEvtHandler(size_t level) { m_level = level; }
112
113 void OnTest(wxCommandEvent& event)
114 {
115 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
116
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
119 event.Skip();
120 }
121
122 private:
123 unsigned m_level;
124
125 DECLARE_EVENT_TABLE()
126 };
127
128 // ----------------------------------------------------------------------------
129 // constants
130 // ----------------------------------------------------------------------------
131
132 // IDs for the controls and the menu commands
133 enum
134 {
135 // menu items
136 Event_Quit = 1,
137 Event_About,
138 Event_Connect,
139 Event_Dynamic,
140 Event_Push,
141 Event_Pop,
142 Event_Custom,
143 Event_Test
144 };
145
146 // status bar fields
147 enum
148 {
149 Status_Main = 0,
150 Status_Dynamic,
151 Status_Push
152 };
153
154 // ----------------------------------------------------------------------------
155 // event tables and other macros for wxWidgets
156 // ----------------------------------------------------------------------------
157
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)
164
165 EVT_MENU(Event_Connect, MyFrame::OnConnect)
166
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)
171
172 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
173
174 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
175
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)
179 END_EVENT_TABLE()
180
181 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
182 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
183 END_EVENT_TABLE()
184
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
189 // not wxApp)
190 IMPLEMENT_APP(MyApp)
191
192 // ============================================================================
193 // implementation
194 // ============================================================================
195
196 // ----------------------------------------------------------------------------
197 // the application class
198 // ----------------------------------------------------------------------------
199
200 // 'Main program' equivalent: the program execution "starts" here
201 bool MyApp::OnInit()
202 {
203 if ( !wxApp::OnInit() )
204 return false;
205
206 // create the main application window
207 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
208 wxPoint(50, 50), wxSize(600, 340));
209
210 // and show it (the frames, unlike simple controls, are not shown when
211 // created initially)
212 frame->Show(true);
213
214 // success: wxApp::OnRun() will be called which will enter the main message
215 // loop and the application will run. If we returned false here, the
216 // application would exit immediately.
217 return true;
218 }
219
220 // ----------------------------------------------------------------------------
221 // main frame
222 // ----------------------------------------------------------------------------
223
224 // frame constructor
225 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
226 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
227 {
228 // init members
229 m_nPush = 0;
230
231 // create a menu bar
232 wxMenu *menuFile = new wxMenu;
233
234 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
235 menuFile->AppendSeparator();
236 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
237
238 wxMenu *menuEvent = new wxMenu;
239 menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
240 _T("Connect or disconnect the dynamic event handler"),
241 true /* checkable */);
242 menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
243 _T("Dynamic event sample - only works after Connect"));
244 menuEvent->AppendSeparator();
245 menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
246 _T("Push event handler for test event"));
247 menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
248 _T("Pop event handler for test event"));
249 menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
250 _T("Test event processed by pushed event handler"));
251 menuEvent->AppendSeparator();
252 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
253 _T("Generate a custom event"));
254
255 // now append the freshly created menu to the menu bar...
256 wxMenuBar *menuBar = new wxMenuBar();
257 menuBar->Append(menuFile, _T("&File"));
258 menuBar->Append(menuEvent, _T("&Event"));
259
260 // ... and attach this menu bar to the frame
261 SetMenuBar(menuBar);
262
263 #if wxUSE_STATUSBAR
264 CreateStatusBar(3);
265 SetStatusText(_T("Welcome to wxWidgets event sample"));
266 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
267 SetStatusText(_T("Push count: 0"), Status_Push);
268 #endif // wxUSE_STATUSBAR
269 }
270
271 MyFrame::~MyFrame()
272 {
273 // we must pop any remaining event handlers to avoid memory leaks and
274 // crashes!
275 while ( m_nPush-- != 0 )
276 {
277 PopEventHandler(true /* delete handler */);
278 }
279 }
280
281 // ----------------------------------------------------------------------------
282 // standard event handlers
283 // ----------------------------------------------------------------------------
284
285 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
286 {
287 // true is to force the frame to close
288 Close(true);
289 }
290
291 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
292 {
293 wxMessageBox( wxT("Event sample shows different ways of using events\n")
294 wxT("(c) 2001 Vadim Zeitlin"),
295 wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
296 }
297
298 // ----------------------------------------------------------------------------
299 // dynamic event handling stuff
300 // ----------------------------------------------------------------------------
301
302 void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
303 {
304 wxMessageBox
305 (
306 wxT("This is a dynamic event handler which can be connected ")
307 wxT("and disconnected at run-time."),
308 wxT("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this
309 );
310 }
311
312 void MyFrame::OnConnect(wxCommandEvent& event)
313 {
314 if ( event.IsChecked() )
315 {
316 // disconnect
317 Connect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED,
318 (wxObjectEventFunction)
319 (wxEventFunction)
320 (wxCommandEventFunction)&MyFrame::OnDynamic);
321
322 #if wxUSE_STATUSBAR
323 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
324 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
325 #endif // wxUSE_STATUSBAR
326 }
327 else // connect
328 {
329 Disconnect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED);
330
331 #if wxUSE_STATUSBAR
332 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
333 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
334 #endif // wxUSE_STATUSBAR
335 }
336 }
337
338 // ----------------------------------------------------------------------------
339 // push/pop event handlers support
340 // ----------------------------------------------------------------------------
341
342 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
343 {
344 PushEventHandler(new MyEvtHandler(++m_nPush));
345
346 #if wxUSE_STATUSBAR
347 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
348 #endif // wxUSE_STATUSBAR
349 }
350
351 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
352 {
353 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
354
355 PopEventHandler(true /* delete handler */);
356 m_nPush--;
357
358 #if wxUSE_STATUSBAR
359 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
360 #endif // wxUSE_STATUSBAR
361 }
362
363 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
364 {
365 wxLogMessage(_T("This is the test event handler in the main frame"));
366 }
367
368 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
369 {
370 event.Enable( m_nPush > 0 );
371 }
372
373 // ----------------------------------------------------------------------------
374 // custom event methods
375 // ----------------------------------------------------------------------------
376
377 void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
378 {
379 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
380
381 wxPostEvent(this, eventCustom);
382 }
383
384 void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
385 {
386 wxLogMessage(_T("Got a custom event!"));
387 }
388