]> git.saurik.com Git - wxWidgets.git/blame - samples/event/event.cpp
added missing headers for PCH-less compilation
[wxWidgets.git] / samples / event / event.cpp
CommitLineData
6164f93c
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: event.cpp
be5a51fb 3// Purpose: wxWidgets sample demonstrating different event usage
6164f93c
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 31.01.01
7// RCS-ID: $Id$
8584ae1c 8// Copyright: (c) 2001-2009 Vadim Zeitlin
6164f93c
VZ
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
6164f93c
VZ
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
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers)
6164f93c
VZ
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
41f02b9a
FM
33#ifndef __WXMSW__
34 #include "../sample.xpm"
35#endif
36
4ec2df6c
VZ
37// ----------------------------------------------------------------------------
38// event constants
39// ----------------------------------------------------------------------------
40
862c917d 41// define a custom event type (we don't need a separate declaration here but
1afe8c83 42// usually you would use a matching wxDECLARE_EVENT in a header)
16be58c1 43wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent);
4ec2df6c
VZ
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( \
07850a49 48 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
79935f61 49 wxCommandEventHandler(fn), \
4ec2df6c
VZ
50 (wxObject *) NULL \
51 ),
52
6164f93c
VZ
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);
8584ae1c
VZ
80#if !wxEVENTS_COMPATIBILITY_2_8
81 void OnBind(wxCommandEvent& event);
82#endif // !wxEVENTS_COMPATIBILITY_2_8
6164f93c
VZ
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
4ec2df6c
VZ
89 void OnFireCustom(wxCommandEvent& event);
90 void OnProcessCustom(wxCommandEvent& event);
91
6164f93c
VZ
92 void OnUpdateUIPop(wxUpdateUIEvent& event);
93
8584ae1c
VZ
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
6164f93c 119 // number of pushed event handlers
b143cf70 120 unsigned m_nPush;
6164f93c 121
8584ae1c
VZ
122 // the button to whose event we connect dynamically
123 wxButton *m_btnDynamic;
124
be5a51fb 125 // any class wishing to process wxWidgets events must use this macro
6164f93c
VZ
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:
b143cf70 145 unsigned m_level;
6164f93c
VZ
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,
8584ae1c 160 Event_Bind,
6164f93c
VZ
161 Event_Connect,
162 Event_Dynamic,
163 Event_Push,
164 Event_Pop,
4ec2df6c 165 Event_Custom,
6164f93c
VZ
166 Event_Test
167};
168
6164f93c 169// ----------------------------------------------------------------------------
be5a51fb 170// event tables and other macros for wxWidgets
6164f93c
VZ
171// ----------------------------------------------------------------------------
172
be5a51fb 173// the event tables connect the wxWidgets events with the functions (event
6164f93c
VZ
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
8584ae1c
VZ
180#if !wxEVENTS_COMPATIBILITY_2_8
181 EVT_MENU(Event_Bind, MyFrame::OnBind)
182#endif // !wxEVENTS_COMPATIBILITY_2_8
6164f93c
VZ
183 EVT_MENU(Event_Connect, MyFrame::OnConnect)
184
4ec2df6c 185 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
6164f93c
VZ
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)
4ec2df6c 191
07850a49 192 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
4b614012 193
19fe5fd1
VZ
194 // the line below would also work if OnProcessCustom() were defined as
195 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
07850a49 196 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
6164f93c
VZ
197END_EVENT_TABLE()
198
199BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
200 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
201END_EVENT_TABLE()
202
be5a51fb 203// Create a new application object: this macro will allow wxWidgets to create
6164f93c
VZ
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{
45e6e6f8
VZ
221 if ( !wxApp::OnInit() )
222 return false;
223
6164f93c 224 // create the main application window
be5a51fb 225 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
4ec2df6c 226 wxPoint(50, 50), wxSize(600, 340));
6164f93c
VZ
227
228 // and show it (the frames, unlike simple controls, are not shown when
229 // created initially)
07850a49 230 frame->Show(true);
6164f93c
VZ
231
232 // success: wxApp::OnRun() will be called which will enter the main message
07850a49 233 // loop and the application will run. If we returned false here, the
6164f93c 234 // application would exit immediately.
07850a49 235 return true;
6164f93c
VZ
236}
237
238// ----------------------------------------------------------------------------
239// main frame
240// ----------------------------------------------------------------------------
241
242// frame constructor
243MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
8584ae1c 244 : wxFrame(NULL, wxID_ANY, title, pos, size)
6164f93c 245{
41f02b9a
FM
246 SetIcon(wxICON(sample));
247
3aa467b6
VZ
248 // init members
249 m_nPush = 0;
8584ae1c 250 m_btnDynamic = NULL;
3aa467b6 251
6164f93c
VZ
252 // create a menu bar
253 wxMenu *menuFile = new wxMenu;
254
4ec2df6c 255 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
6164f93c 256 menuFile->AppendSeparator();
4ec2df6c 257 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
6164f93c
VZ
258
259 wxMenu *menuEvent = new wxMenu;
8584ae1c
VZ
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"));
4ec2df6c
VZ
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"));
6164f93c 275 menuEvent->AppendSeparator();
4ec2df6c
VZ
276 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
277 _T("Generate a custom event"));
6164f93c
VZ
278
279 // now append the freshly created menu to the menu bar...
280 wxMenuBar *menuBar = new wxMenuBar();
4ec2df6c
VZ
281 menuBar->Append(menuFile, _T("&File"));
282 menuBar->Append(menuEvent, _T("&Event"));
6164f93c
VZ
283
284 // ... and attach this menu bar to the frame
285 SetMenuBar(menuBar);
286
287#if wxUSE_STATUSBAR
288 CreateStatusBar(3);
be5a51fb 289 SetStatusText(_T("Welcome to wxWidgets event sample"));
6164f93c
VZ
290 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
291 SetStatusText(_T("Push count: 0"), Status_Push);
292#endif // wxUSE_STATUSBAR
8584ae1c
VZ
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);
6164f93c
VZ
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 {
07850a49 311 PopEventHandler(true /* delete handler */);
6164f93c
VZ
312 }
313}
314
315// ----------------------------------------------------------------------------
316// standard event handlers
317// ----------------------------------------------------------------------------
318
319void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
320{
07850a49
WS
321 // true is to force the frame to close
322 Close(true);
6164f93c
VZ
323}
324
325void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
326{
8584ae1c
VZ
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);
6164f93c
VZ
331}
332
333// ----------------------------------------------------------------------------
334// dynamic event handling stuff
335// ----------------------------------------------------------------------------
336
8584ae1c 337void MyFrame::OnDynamic(wxCommandEvent& event)
6164f93c 338{
8584ae1c
VZ
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
f565a6c2
VZ
347 wxMessageBox
348 (
8584ae1c
VZ
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
f565a6c2 352 );
6164f93c
VZ
353}
354
8584ae1c
VZ
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
6164f93c
VZ
384void MyFrame::OnConnect(wxCommandEvent& event)
385{
386 if ( event.IsChecked() )
387 {
8584ae1c
VZ
388 m_btnDynamic->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
389 wxCommandEventHandler(MyFrame::OnDynamic),
390 NULL, this);
862c917d
VZ
391 Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
392 wxCommandEventHandler(MyFrame::OnDynamic));
6164f93c 393 }
862c917d 394 else // disconnect
6164f93c 395 {
8584ae1c
VZ
396 m_btnDynamic->Disconnect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
397 wxCommandEventHandler(MyFrame::OnDynamic),
398 NULL, this);
862c917d
VZ
399 Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
400 wxCommandEventHandler(MyFrame::OnDynamic));
6164f93c 401 }
8584ae1c
VZ
402
403 UpdateDynamicStatus(event.IsChecked());
6164f93c
VZ
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
8520f137 414#if wxUSE_STATUSBAR
6164f93c 415 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
8520f137 416#endif // wxUSE_STATUSBAR
6164f93c
VZ
417}
418
419void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
420{
421 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
422
07850a49 423 PopEventHandler(true /* delete handler */);
6164f93c
VZ
424 m_nPush--;
425
8520f137 426#if wxUSE_STATUSBAR
6164f93c 427 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
8520f137 428#endif // wxUSE_STATUSBAR
6164f93c
VZ
429}
430
87728739 431void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
6164f93c
VZ
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
4ec2df6c
VZ
441// ----------------------------------------------------------------------------
442// custom event methods
443// ----------------------------------------------------------------------------
444
87728739 445void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
4ec2df6c
VZ
446{
447 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
448
449 wxPostEvent(this, eventCustom);
450}
451
87728739 452void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
4ec2df6c
VZ
453{
454 wxLogMessage(_T("Got a custom event!"));
455}
456