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