]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/event/event.cpp
added missing headers for PCH-less compilation
[wxWidgets.git] / samples / event / event.cpp
... / ...
CommitLineData
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-2009 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#ifndef __WXMSW__
34 #include "../sample.xpm"
35#endif
36
37// ----------------------------------------------------------------------------
38// event constants
39// ----------------------------------------------------------------------------
40
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)
43wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent);
44
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), \
50 (wxObject *) NULL \
51 ),
52
53// ----------------------------------------------------------------------------
54// private classes
55// ----------------------------------------------------------------------------
56
57// Define a new application type, each program should derive a class from wxApp
58class MyApp : public wxApp
59{
60public:
61 // override base class virtuals
62 // ----------------------------
63
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();
68};
69
70// Define a new frame type: this is going to be our main frame
71class MyFrame : public wxFrame
72{
73public:
74 // ctor(s)
75 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
76 virtual ~MyFrame();
77
78 void OnQuit(wxCommandEvent& event);
79 void OnAbout(wxCommandEvent& event);
80#if !wxEVENTS_COMPATIBILITY_2_8
81 void OnBind(wxCommandEvent& event);
82#endif // !wxEVENTS_COMPATIBILITY_2_8
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);
88
89 void OnFireCustom(wxCommandEvent& event);
90 void OnProcessCustom(wxCommandEvent& event);
91
92 void OnUpdateUIPop(wxUpdateUIEvent& event);
93
94private:
95 // symbolic names for the status bar fields
96 enum
97 {
98 Status_Main = 0,
99 Status_Dynamic,
100 Status_Push
101 };
102
103 void UpdateDynamicStatus(bool on)
104 {
105#if wxUSE_STATUSBAR
106 if ( on )
107 {
108 SetStatusText("You can now use \"Dynamic\" item in the menu");
109 SetStatusText("Dynamic: on", Status_Dynamic);
110 }
111 else
112 {
113 SetStatusText("You can no more use \"Dynamic\" item in the menu");
114 SetStatusText("Dynamic: off", Status_Dynamic);
115 }
116#endif // wxUSE_STATUSBAR
117 }
118
119 // number of pushed event handlers
120 unsigned m_nPush;
121
122 // the button to whose event we connect dynamically
123 wxButton *m_btnDynamic;
124
125 // any class wishing to process wxWidgets events must use this macro
126 DECLARE_EVENT_TABLE()
127};
128
129// Define a custom event handler
130class MyEvtHandler : public wxEvtHandler
131{
132public:
133 MyEvtHandler(size_t level) { m_level = level; }
134
135 void OnTest(wxCommandEvent& event)
136 {
137 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
138
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
141 event.Skip();
142 }
143
144private:
145 unsigned m_level;
146
147 DECLARE_EVENT_TABLE()
148};
149
150// ----------------------------------------------------------------------------
151// constants
152// ----------------------------------------------------------------------------
153
154// IDs for the controls and the menu commands
155enum
156{
157 // menu items
158 Event_Quit = 1,
159 Event_About,
160 Event_Bind,
161 Event_Connect,
162 Event_Dynamic,
163 Event_Push,
164 Event_Pop,
165 Event_Custom,
166 Event_Test
167};
168
169// ----------------------------------------------------------------------------
170// event tables and other macros for wxWidgets
171// ----------------------------------------------------------------------------
172
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.
176BEGIN_EVENT_TABLE(MyFrame, wxFrame)
177 EVT_MENU(Event_Quit, MyFrame::OnQuit)
178 EVT_MENU(Event_About, MyFrame::OnAbout)
179
180#if !wxEVENTS_COMPATIBILITY_2_8
181 EVT_MENU(Event_Bind, MyFrame::OnBind)
182#endif // !wxEVENTS_COMPATIBILITY_2_8
183 EVT_MENU(Event_Connect, MyFrame::OnConnect)
184
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)
189
190 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
191
192 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
193
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)
197END_EVENT_TABLE()
198
199BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
200 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
201END_EVENT_TABLE()
202
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
207// not wxApp)
208IMPLEMENT_APP(MyApp)
209
210// ============================================================================
211// implementation
212// ============================================================================
213
214// ----------------------------------------------------------------------------
215// the application class
216// ----------------------------------------------------------------------------
217
218// 'Main program' equivalent: the program execution "starts" here
219bool MyApp::OnInit()
220{
221 if ( !wxApp::OnInit() )
222 return false;
223
224 // create the main application window
225 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
226 wxPoint(50, 50), wxSize(600, 340));
227
228 // and show it (the frames, unlike simple controls, are not shown when
229 // created initially)
230 frame->Show(true);
231
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.
235 return true;
236}
237
238// ----------------------------------------------------------------------------
239// main frame
240// ----------------------------------------------------------------------------
241
242// frame constructor
243MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
244 : wxFrame(NULL, wxID_ANY, title, pos, size)
245{
246 SetIcon(wxICON(sample));
247
248 // init members
249 m_nPush = 0;
250 m_btnDynamic = NULL;
251
252 // create a menu bar
253 wxMenu *menuFile = new wxMenu;
254
255 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
256 menuFile->AppendSeparator();
257 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
258
259 wxMenu *menuEvent = new wxMenu;
260#if !wxEVENTS_COMPATIBILITY_2_8
261 menuEvent->AppendCheckItem(Event_Bind, "&Bind\tCtrl-B",
262 "Bind or unbind a dynamic event handler");
263#endif // !wxEVENTS_COMPATIBILITY_2_8
264 menuEvent->AppendCheckItem(Event_Connect, _T("&Connect\tCtrl-C"),
265 _T("Connect or disconnect the dynamic event handler"));
266 menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
267 _T("Dynamic event sample - only works after Connect"));
268 menuEvent->AppendSeparator();
269 menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
270 _T("Push event handler for test event"));
271 menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
272 _T("Pop event handler for test event"));
273 menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
274 _T("Test event processed by pushed event handler"));
275 menuEvent->AppendSeparator();
276 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
277 _T("Generate a custom event"));
278
279 // now append the freshly created menu to the menu bar...
280 wxMenuBar *menuBar = new wxMenuBar();
281 menuBar->Append(menuFile, _T("&File"));
282 menuBar->Append(menuEvent, _T("&Event"));
283
284 // ... and attach this menu bar to the frame
285 SetMenuBar(menuBar);
286
287#if wxUSE_STATUSBAR
288 CreateStatusBar(3);
289 SetStatusText(_T("Welcome to wxWidgets event sample"));
290 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
291 SetStatusText(_T("Push count: 0"), Status_Push);
292#endif // wxUSE_STATUSBAR
293
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"),
299 centreY);
300 m_btnDynamic = new wxButton(panel, Event_Dynamic, "&Dynamic button");
301 sizer->Add(m_btnDynamic, centreY);
302 panel->SetSizer(sizer);
303}
304
305MyFrame::~MyFrame()
306{
307 // we must pop any remaining event handlers to avoid memory leaks and
308 // crashes!
309 while ( m_nPush-- != 0 )
310 {
311 PopEventHandler(true /* delete handler */);
312 }
313}
314
315// ----------------------------------------------------------------------------
316// standard event handlers
317// ----------------------------------------------------------------------------
318
319void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
320{
321 // true is to force the frame to close
322 Close(true);
323}
324
325void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
326{
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);
331}
332
333// ----------------------------------------------------------------------------
334// dynamic event handling stuff
335// ----------------------------------------------------------------------------
336
337void MyFrame::OnDynamic(wxCommandEvent& event)
338{
339 wxString origin;
340 if ( event.GetEventObject() == this )
341 origin = "menu item";
342 else if ( event.GetEventObject() == m_btnDynamic )
343 origin = "button";
344 else
345 origin = "unknown event source";
346
347 wxMessageBox
348 (
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
352 );
353}
354
355#if !wxEVENTS_COMPATIBILITY_2_8
356
357void MyFrame::OnBind(wxCommandEvent& event)
358{
359 if ( event.IsChecked() )
360 {
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,
364 this);
365
366 // but we do need the id for the menu command as the frame gets all of
367 // them
368 Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this,
369 Event_Dynamic);
370 }
371 else // disconnect
372 {
373 m_btnDynamic->Unbind(wxEVT_COMMAND_BUTTON_CLICKED,
374 &MyFrame::OnDynamic, this);
375 Unbind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this,
376 Event_Dynamic);
377 }
378
379 UpdateDynamicStatus(event.IsChecked());
380}
381
382#endif // !wxEVENTS_COMPATIBILITY_2_8
383
384void MyFrame::OnConnect(wxCommandEvent& event)
385{
386 if ( event.IsChecked() )
387 {
388 m_btnDynamic->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
389 wxCommandEventHandler(MyFrame::OnDynamic),
390 NULL, this);
391 Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
392 wxCommandEventHandler(MyFrame::OnDynamic));
393 }
394 else // disconnect
395 {
396 m_btnDynamic->Disconnect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
397 wxCommandEventHandler(MyFrame::OnDynamic),
398 NULL, this);
399 Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
400 wxCommandEventHandler(MyFrame::OnDynamic));
401 }
402
403 UpdateDynamicStatus(event.IsChecked());
404}
405
406// ----------------------------------------------------------------------------
407// push/pop event handlers support
408// ----------------------------------------------------------------------------
409
410void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
411{
412 PushEventHandler(new MyEvtHandler(++m_nPush));
413
414#if wxUSE_STATUSBAR
415 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
416#endif // wxUSE_STATUSBAR
417}
418
419void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
420{
421 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
422
423 PopEventHandler(true /* delete handler */);
424 m_nPush--;
425
426#if wxUSE_STATUSBAR
427 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
428#endif // wxUSE_STATUSBAR
429}
430
431void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
432{
433 wxLogMessage(_T("This is the test event handler in the main frame"));
434}
435
436void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
437{
438 event.Enable( m_nPush > 0 );
439}
440
441// ----------------------------------------------------------------------------
442// custom event methods
443// ----------------------------------------------------------------------------
444
445void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
446{
447 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
448
449 wxPostEvent(this, eventCustom);
450}
451
452void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
453{
454 wxLogMessage(_T("Got a custom event!"));
455}
456