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 // Copyright: (c) 2001-2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWidgets headers)
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #ifndef wxHAS_IMAGES_IN_RESOURCES
30 #include "../sample.xpm"
31 #endif
32
33 #include <wx/statline.h>
34 #include <wx/log.h>
35 #include "gestures.h"
36 #include "chessboard.h"
37
38 // ----------------------------------------------------------------------------
39 // event constants
40 // ----------------------------------------------------------------------------
41
42 // define a custom event type (we don't need a separate declaration here but
43 // usually you would use a matching wxDECLARE_EVENT in a header)
44 wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent);
45
46 // it may also be convenient to define an event table macro for this event type
47 #define EVT_MY_CUSTOM_COMMAND(id, fn) \
48 DECLARE_EVENT_TABLE_ENTRY( \
49 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
50 wxCommandEventHandler(fn), \
51 (wxObject *) NULL \
52 ),
53
54 // ----------------------------------------------------------------------------
55 // private classes
56 // ----------------------------------------------------------------------------
57
58 // Define a new application type, each program should derive a class from wxApp
59 class MyApp : public wxApp
60 {
61 public:
62 // override base class virtuals
63 // ----------------------------
64
65 // this one is called on application startup and is a good place for the app
66 // initialization (doing it here and not in the ctor allows to have an error
67 // return: if OnInit() returns false, the application terminates)
68 virtual bool OnInit() wxOVERRIDE;
69
70 // these are regular event handlers used to highlight the events handling
71 // order
72 void OnClickDynamicHandlerApp(wxCommandEvent& event);
73 void OnClickStaticHandlerApp(wxCommandEvent& event);
74
75 // we override wxAppConsole::FilterEvent used to highlight the events
76 // handling order
77 virtual int FilterEvent(wxEvent& event) wxOVERRIDE;
78
79 private:
80 wxDECLARE_EVENT_TABLE();
81 };
82
83 // Define a custom button used to highlight the events handling order
84 class MyEvtTestButton : public wxButton
85 {
86 public:
87 static long BUTTON_ID;
88
MyEvtTestButton(wxWindow * parent,const wxString & label)89 MyEvtTestButton(wxWindow *parent, const wxString& label)
90 : wxButton(parent, BUTTON_ID, label)
91 {
92 // Add a dynamic handler for this button event to button itself
93 Bind(wxEVT_BUTTON, &MyEvtTestButton::OnClickDynamicHandler, this);
94 }
95
96 private:
OnClickDynamicHandler(wxCommandEvent & event)97 void OnClickDynamicHandler(wxCommandEvent& event)
98 {
99 wxLogMessage("Step 3 in \"How Events are Processed\":\n"
100 "Button::ownDynamicHandler");
101
102 event.Skip();
103 }
104
OnClickStaticHandler(wxCommandEvent & event)105 void OnClickStaticHandler(wxCommandEvent& event)
106 {
107 wxLogMessage("Step 4 in \"How Events are Processed\":\n"
108 "Button::ownStaticHandler");
109
110 event.Skip();
111 }
112
113 wxDECLARE_EVENT_TABLE();
114 };
115
116 long MyEvtTestButton::BUTTON_ID = wxNewId();
117
118 // Define a new frame type: this is going to be our main frame
119 class MyFrame : public wxFrame
120 {
121 public:
122 // ctor(s)
123 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
124 virtual ~MyFrame();
125
126 void OnQuit(wxCommandEvent& event);
127 void OnAbout(wxCommandEvent& event);
128 void OnBind(wxCommandEvent& event);
129 void OnConnect(wxCommandEvent& event);
130 void OnDynamic(wxCommandEvent& event);
131 void OnPushEventHandler(wxCommandEvent& event);
132 void OnPopEventHandler(wxCommandEvent& event);
133 void OnTest(wxCommandEvent& event);
134
135 void OnFireCustom(wxCommandEvent& event);
136 void OnProcessCustom(wxCommandEvent& event);
137
138 void OnUpdateUIPop(wxUpdateUIEvent& event);
139
140 // regular event handlers used to highlight the events handling order
141 void OnClickDynamicHandlerFrame(wxCommandEvent& event);
142 void OnClickDynamicHandlerButton(wxCommandEvent& event);
143 void OnClickStaticHandlerFrame(wxCommandEvent& event);
144
145 // Gesture
146 void OnGesture(wxCommandEvent& event);
147
148 // Demonstrates using a new event class
149 void OnNewEventClass(wxCommandEvent& event);
150
151 private:
152 // symbolic names for the status bar fields
153 enum
154 {
155 Status_Main = 0,
156 Status_Dynamic,
157 Status_Push
158 };
159
UpdateDynamicStatus(bool on)160 void UpdateDynamicStatus(bool on)
161 {
162 #if wxUSE_STATUSBAR
163 if ( on )
164 {
165 SetStatusText("You can now use \"Dynamic\" item in the menu");
166 SetStatusText("Dynamic: on", Status_Dynamic);
167 }
168 else
169 {
170 SetStatusText("You can no more use \"Dynamic\" item in the menu");
171 SetStatusText("Dynamic: off", Status_Dynamic);
172 }
173 #endif // wxUSE_STATUSBAR
174 }
175
176 // number of pushed event handlers
177 unsigned m_nPush;
178
179 // the button to whose event we connect dynamically
180 wxButton *m_btnDynamic;
181
182 // the button used to highlight the event handlers execution order
183 MyEvtTestButton *m_testBtn;
184
185 wxWindowRef m_gestureFrame;
186
187
188 // any class wishing to process wxWidgets events must use this macro
189 wxDECLARE_EVENT_TABLE();
190 };
191
192 // Define a custom event handler
193 class MyEvtHandler : public wxEvtHandler
194 {
195 public:
MyEvtHandler(size_t level)196 MyEvtHandler(size_t level) { m_level = level; }
197
OnTest(wxCommandEvent & event)198 void OnTest(wxCommandEvent& event)
199 {
200 wxLogMessage("This is the pushed test event handler #%u", m_level);
201
202 // if we don't skip the event, the other event handlers won't get it:
203 // try commenting out this line and see what changes
204 event.Skip();
205 }
206
207 private:
208 unsigned m_level;
209
210 wxDECLARE_EVENT_TABLE();
211 };
212
213 // ----------------------------------------------------------------------------
214 // constants
215 // ----------------------------------------------------------------------------
216
217 // IDs for the controls and the menu commands
218 enum
219 {
220 // menu items
221 Event_Quit = 1,
222 Event_About,
223 Event_Bind,
224 Event_Connect,
225 Event_Dynamic,
226 Event_Push,
227 Event_Pop,
228 Event_Custom,
229 Event_Test,
230 Event_Gesture,
231 Event_NewEventClass
232 };
233
234 // ----------------------------------------------------------------------------
235 // event tables and other macros for wxWidgets
236 // ----------------------------------------------------------------------------
237
238 // The event tables connect the wxWidgets events with the functions (event
239 // handlers) which process them.
240 wxBEGIN_EVENT_TABLE(MyApp, wxApp)
241 // Add a static handler for button Click event in the app
242 EVT_BUTTON(MyEvtTestButton::BUTTON_ID, MyApp::OnClickStaticHandlerApp)
243 wxEND_EVENT_TABLE()
244
245 wxBEGIN_EVENT_TABLE(MyEvtTestButton, wxButton)
246 // Add a static handler to this button itself for its own event
247 EVT_BUTTON(BUTTON_ID, MyEvtTestButton::OnClickStaticHandler)
248 wxEND_EVENT_TABLE()
249
250 // This can be also done at run-time, but for the
251 // simple menu events like this the static method is much simpler.
252 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
253 EVT_MENU(Event_Quit, MyFrame::OnQuit)
254 EVT_MENU(Event_About, MyFrame::OnAbout)
255
256 EVT_MENU(Event_Bind, MyFrame::OnBind)
257 EVT_MENU(Event_Connect, MyFrame::OnConnect)
258
259 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
260 EVT_MENU(Event_Test, MyFrame::OnTest)
261 EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
262 EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
263 EVT_MENU(Event_Gesture, MyFrame::OnGesture)
264 EVT_MENU(Event_NewEventClass, MyFrame::OnNewEventClass)
265
266 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
267
268 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
269
270 // the line below would also work if OnProcessCustom() were defined as
271 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
272 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
273
274 // Add a static handler in the parent frame for button event
275 EVT_BUTTON(MyEvtTestButton::BUTTON_ID, MyFrame::OnClickStaticHandlerFrame)
276 wxEND_EVENT_TABLE()
277
278 wxBEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
279 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
280 wxEND_EVENT_TABLE()
281
282 // Create a new application object: this macro will allow wxWidgets to create
283 // the application object during program execution (it's better than using a
284 // static object for many reasons) and also declares the accessor function
285 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
286 // not wxApp)
287 wxIMPLEMENT_APP(MyApp);
288
289 // ============================================================================
290 // implementation
291 // ============================================================================
292
293 // ----------------------------------------------------------------------------
294 // the application class
295 // ----------------------------------------------------------------------------
296
297 // 'Main program' equivalent: the program execution "starts" here
OnInit()298 bool MyApp::OnInit()
299 {
300 if ( !wxApp::OnInit() )
301 return false;
302
303 // create the main application window
304 MyFrame *frame = new MyFrame("Event wxWidgets Sample",
305 wxPoint(50, 50), wxSize(600, 340));
306
307 // and show it (the frames, unlike simple controls, are not shown when
308 // created initially)
309 frame->Show(true);
310
311 // Add a dynamic handler at the application level for the test button
312 Bind(wxEVT_BUTTON, &MyApp::OnClickDynamicHandlerApp, this,
313 MyEvtTestButton::BUTTON_ID);
314
315 // success: wxApp::OnRun() will be called which will enter the main message
316 // loop and the application will run. If we returned false here, the
317 // application would exit immediately.
318 return true;
319 }
320
321 // This is always the first to handle an event !
FilterEvent(wxEvent & event)322 int MyApp::FilterEvent(wxEvent& event)
323 {
324 if ( event.GetEventType() == wxEVT_BUTTON &&
325 event.GetId() == MyEvtTestButton::BUTTON_ID )
326 {
327 wxLogMessage("Step 0 in \"How Events are Processed\":\n"
328 "App::FilterEvent");
329 }
330
331 return wxApp::FilterEvent(event);
332 }
333
OnClickDynamicHandlerApp(wxCommandEvent & event)334 void MyApp::OnClickDynamicHandlerApp(wxCommandEvent& event)
335 {
336 wxLogMessage("Step 7, 3 in \"How Events are Processed\":\n"
337 "App::DynamicHandler_InAppTable");
338
339 event.Skip();
340 }
341
OnClickStaticHandlerApp(wxCommandEvent & event)342 void MyApp::OnClickStaticHandlerApp(wxCommandEvent& event)
343 {
344 wxLogMessage("Step 7, 4 in \"How Events are Processed\":\n"
345 "App::StaticHandler_InAppTable");
346
347 wxLogMessage("Button click processed, there should be no more messages "
348 "about handling events from the button.\n\n"
349 "The log below shows the order in which the handlers "
350 "were executed.");
351 wxLog::FlushActive();
352
353 event.Skip();
354 }
355
356 // ----------------------------------------------------------------------------
357 // main frame
358 // ----------------------------------------------------------------------------
359
360 // frame constructor
MyFrame(const wxString & title,const wxPoint & pos,const wxSize & size)361 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
362 : wxFrame(NULL, wxID_ANY, title, pos, size)
363 {
364 SetIcon(wxICON(sample));
365
366 // init members
367 m_nPush = 0;
368 m_btnDynamic = NULL;
369
370 // create a menu bar
371 wxMenu *menuFile = new wxMenu;
372
373 menuFile->Append(Event_About, "&About\tCtrl-A", "Show about dialog");
374 menuFile->AppendSeparator();
375 menuFile->Append(Event_Quit, "E&xit\tAlt-X", "Quit this program");
376
377 wxMenu *menuEvent = new wxMenu;
378 menuEvent->AppendCheckItem(Event_Bind, "&Bind\tCtrl-B",
379 "Bind or unbind a dynamic event handler");
380 menuEvent->AppendCheckItem(Event_Connect, "&Connect\tCtrl-C",
381 "Connect or disconnect the dynamic event handler");
382 menuEvent->Append(Event_Dynamic, "&Dynamic event\tCtrl-D",
383 "Dynamic event sample - only works after Connect");
384 menuEvent->AppendSeparator();
385 menuEvent->Append(Event_Push, "&Push event handler\tCtrl-P",
386 "Push event handler for test event");
387 menuEvent->Append(Event_Pop, "P&op event handler\tCtrl-O",
388 "Pop event handler for test event");
389 menuEvent->Append(Event_Test, "Test event\tCtrl-T",
390 "Test event processed by pushed event handler");
391 menuEvent->AppendSeparator();
392 menuEvent->Append(Event_Custom, "Fire c&ustom event\tCtrl-U",
393 "Generate a custom event");
394 menuEvent->Append(Event_Gesture, "&Gesture events\tCtrl-G",
395 "Gesture event");
396 menuEvent->Append(Event_NewEventClass, "&New wxEvent class demo\tCtrl-N",
397 "Demonstrates a new wxEvent-derived class");
398
399 // now append the freshly created menu to the menu bar...
400 wxMenuBar *menuBar = new wxMenuBar();
401 menuBar->Append(menuFile, "&File");
402 menuBar->Append(menuEvent, "&Event");
403
404 // ... and attach this menu bar to the frame
405 SetMenuBar(menuBar);
406
407 #if wxUSE_STATUSBAR
408 CreateStatusBar(3);
409 SetStatusText("Welcome to wxWidgets event sample");
410 SetStatusText("Dynamic: off", Status_Dynamic);
411 SetStatusText("Push count: 0", Status_Push);
412 #endif // wxUSE_STATUSBAR
413
414 wxPanel * const panel = new wxPanel(this);
415 wxSizer * const mainSizer = new wxBoxSizer(wxVERTICAL);
416 wxSizer * const sizer = new wxBoxSizer(wxHORIZONTAL);
417 const wxSizerFlags centreY(wxSizerFlags().Centre().Border());
418 sizer->Add(new wxStaticText(panel, wxID_ANY,
419 "This button will only work if its handler is dynamically connected"),
420 centreY);
421 m_btnDynamic = new wxButton(panel, Event_Dynamic, "&Dynamic button");
422 sizer->Add(m_btnDynamic, centreY);
423
424 mainSizer->Add(sizer, 1, wxEXPAND);
425 mainSizer->Add(new wxStaticLine(panel), 0, wxEXPAND);
426 mainSizer->Add(new wxStaticLine(panel), 0, wxEXPAND);
427
428 m_testBtn = new MyEvtTestButton(panel, "Test Event Handlers Execution Order");
429
430 // After being created, an instance of MyEvtTestButton already has its own
431 // event handlers (see class definition);
432
433 // Add a dynamic handler for this button event in the parent frame
434 Bind(wxEVT_BUTTON, &MyFrame::OnClickDynamicHandlerFrame, this,
435 m_testBtn->GetId());
436
437 // Bind a method of this frame (notice "this" argument!) to the button
438 // itself
439 m_testBtn->Bind(wxEVT_BUTTON, &MyFrame::OnClickDynamicHandlerButton, this);
440
441 mainSizer->Add(m_testBtn);
442 panel->SetSizer(mainSizer);
443 }
444
~MyFrame()445 MyFrame::~MyFrame()
446 {
447 // we must pop any remaining event handlers to avoid memory leaks and
448 // crashes!
449 while ( m_nPush-- != 0 )
450 {
451 PopEventHandler(true /* delete handler */);
452 }
453 }
454
455 // ----------------------------------------------------------------------------
456 // standard event handlers
457 // ----------------------------------------------------------------------------
458
OnQuit(wxCommandEvent & WXUNUSED (event))459 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
460 {
461 if ( m_gestureFrame )
462 m_gestureFrame->Close(true);
463 Close(true);
464 }
465
OnAbout(wxCommandEvent & WXUNUSED (event))466 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
467 {
468 wxMessageBox("Event sample shows different ways of using events\n"
469 "(c) 2001-2009 Vadim Zeitlin",
470 "About wxWidgets Event Sample",
471 wxOK | wxICON_INFORMATION, this);
472 }
473
OnClickStaticHandlerFrame(wxCommandEvent & event)474 void MyFrame::OnClickStaticHandlerFrame(wxCommandEvent& event)
475 {
476 wxLogMessage("Step 6, 4 in \"How Events are Processed\":\n"
477 "parentWin::StaticHandler_InFrameTable");
478
479 event.Skip();
480 }
481
482 // ----------------------------------------------------------------------------
483 // dynamic event handling stuff
484 // ----------------------------------------------------------------------------
485
OnClickDynamicHandlerFrame(wxCommandEvent & event)486 void MyFrame::OnClickDynamicHandlerFrame(wxCommandEvent& event)
487 {
488 wxLogMessage("Step 6, 3 in \"How Events are Processed\":\n"
489 "parentWin::DynamicHandler_InFrameTable");
490
491 event.Skip();
492 }
493
OnClickDynamicHandlerButton(wxCommandEvent & event)494 void MyFrame::OnClickDynamicHandlerButton(wxCommandEvent& event)
495 {
496 wxLogMessage("Step 3 in \"How Events are Processed\":\n"
497 "parentWin::DynamicHandler_InButtonTable");
498
499 event.Skip();
500 }
501
OnDynamic(wxCommandEvent & event)502 void MyFrame::OnDynamic(wxCommandEvent& event)
503 {
504 wxString origin;
505 if ( event.GetEventObject() == this )
506 origin = "menu item";
507 else if ( event.GetEventObject() == m_btnDynamic )
508 origin = "button";
509 else
510 origin = "unknown event source";
511
512 wxMessageBox
513 (
514 "This message box is shown from the dynamically connected "
515 "event handler in response to event generated by " + origin,
516 "wxWidgets Event Sample", wxOK | wxICON_INFORMATION, this
517 );
518 }
519
OnBind(wxCommandEvent & event)520 void MyFrame::OnBind(wxCommandEvent& event)
521 {
522 if ( event.IsChecked() )
523 {
524 // as we bind directly to the button, there is no need to use an id
525 // here: the button will only ever get its own events
526 m_btnDynamic->Bind(wxEVT_BUTTON, &MyFrame::OnDynamic,
527 this);
528
529 // but we do need the id for the menu command as the frame gets all of
530 // them
531 Bind(wxEVT_MENU, &MyFrame::OnDynamic, this,
532 Event_Dynamic);
533 }
534 else // disconnect
535 {
536 m_btnDynamic->Unbind(wxEVT_BUTTON,
537 &MyFrame::OnDynamic, this);
538 Unbind(wxEVT_MENU, &MyFrame::OnDynamic, this,
539 Event_Dynamic);
540 }
541
542 UpdateDynamicStatus(event.IsChecked());
543 }
544
OnConnect(wxCommandEvent & event)545 void MyFrame::OnConnect(wxCommandEvent& event)
546 {
547 if ( event.IsChecked() )
548 {
549 m_btnDynamic->Bind(wxEVT_BUTTON, &MyFrame::OnDynamic, this);
550 Bind(wxEVT_MENU, &MyFrame::OnDynamic, this, Event_Dynamic);
551 }
552 else // disconnect
553 {
554 m_btnDynamic->Unbind(wxEVT_BUTTON, &MyFrame::OnDynamic, this);
555 Unbind(wxEVT_MENU, &MyFrame::OnDynamic, this, Event_Dynamic);
556 }
557
558 UpdateDynamicStatus(event.IsChecked());
559 }
560
561 // ----------------------------------------------------------------------------
562 // push/pop event handlers support
563 // ----------------------------------------------------------------------------
564
OnPushEventHandler(wxCommandEvent & WXUNUSED (event))565 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
566 {
567 PushEventHandler(new MyEvtHandler(++m_nPush));
568
569 #if wxUSE_STATUSBAR
570 SetStatusText(wxString::Format("Push count: %u", m_nPush), Status_Push);
571 #endif // wxUSE_STATUSBAR
572 }
573
OnPopEventHandler(wxCommandEvent & WXUNUSED (event))574 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
575 {
576 wxCHECK_RET( m_nPush, "this command should be disabled!" );
577
578 PopEventHandler(true /* delete handler */);
579 m_nPush--;
580
581 #if wxUSE_STATUSBAR
582 SetStatusText(wxString::Format("Push count: %u", m_nPush), Status_Push);
583 #endif // wxUSE_STATUSBAR
584 }
585
OnGesture(wxCommandEvent & WXUNUSED (event))586 void MyFrame::OnGesture(wxCommandEvent& WXUNUSED(event))
587 {
588 if ( m_gestureFrame )
589 {
590 m_gestureFrame->Raise();
591 }
592 else
593 {
594 m_gestureFrame = new MyGestureFrame();
595 m_gestureFrame->Show(true);
596 }
597 }
598
OnNewEventClass(wxCommandEvent & WXUNUSED (event))599 void MyFrame::OnNewEventClass(wxCommandEvent& WXUNUSED(event))
600 {
601 MyChessBoardDialog dlg(this);
602
603 dlg.ShowModal();
604 }
605
OnTest(wxCommandEvent & WXUNUSED (event))606 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
607 {
608 wxLogMessage("This is the test event handler in the main frame");
609 }
610
OnUpdateUIPop(wxUpdateUIEvent & event)611 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
612 {
613 event.Enable( m_nPush > 0 );
614 }
615
616 // ----------------------------------------------------------------------------
617 // custom event methods
618 // ----------------------------------------------------------------------------
619
OnFireCustom(wxCommandEvent & WXUNUSED (event))620 void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
621 {
622 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
623
624 wxPostEvent(this, eventCustom);
625 }
626
OnProcessCustom(wxCommandEvent & WXUNUSED (event))627 void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
628 {
629 wxLogMessage("Got a custom event!");
630 }
631