1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows sample demonstrating different event usage
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 // DO NOT use event.cpp here, it breaks statics initialization in
22 // src/common/event.cpp and nothing works at all then!
23 #pragma implementation "eventsample.cpp"
24 #pragma interface "eventsample.cpp"
27 // For compilers that support precompilation, includes "wx/wx.h".
28 #include "wx/wxprec.h"
34 // for all others, include the necessary headers (this file is usually all you
35 // need because it includes almost all "standard" wxWindows headers)
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // declare a custom event type
46 // note that in wxWin 2.3+ these macros expand simply into the following code:
48 // extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
50 // const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
52 // and you may use this code directly if you don't care about 2.2 compatibility
53 BEGIN_DECLARE_EVENT_TYPES()
54 DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND
, 7777)
55 END_DECLARE_EVENT_TYPES()
57 DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND
)
59 // it may also be convenient to define an event table macro for this event type
60 #define EVT_MY_CUSTOM_COMMAND(id, fn) \
61 DECLARE_EVENT_TABLE_ENTRY( \
62 wxEVT_MY_CUSTOM_COMMAND, id, -1, \
63 (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // Define a new application type, each program should derive a class from wxApp
72 class MyApp
: public wxApp
75 // override base class virtuals
76 // ----------------------------
78 // this one is called on application startup and is a good place for the app
79 // initialization (doing it here and not in the ctor allows to have an error
80 // return: if OnInit() returns false, the application terminates)
81 virtual bool OnInit();
84 // Define a new frame type: this is going to be our main frame
85 class MyFrame
: public wxFrame
89 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
92 void OnQuit(wxCommandEvent
& event
);
93 void OnAbout(wxCommandEvent
& event
);
94 void OnConnect(wxCommandEvent
& event
);
95 void OnDynamic(wxCommandEvent
& event
);
96 void OnPushEventHandler(wxCommandEvent
& event
);
97 void OnPopEventHandler(wxCommandEvent
& event
);
98 void OnTest(wxCommandEvent
& event
);
100 void OnFireCustom(wxCommandEvent
& event
);
101 void OnProcessCustom(wxCommandEvent
& event
);
103 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
106 // number of pushed event handlers
110 // any class wishing to process wxWindows events must use this macro
111 DECLARE_EVENT_TABLE()
114 // Define a custom event handler
115 class MyEvtHandler
: public wxEvtHandler
118 MyEvtHandler(size_t level
) { m_level
= level
; }
120 void OnTest(wxCommandEvent
& event
)
122 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level
);
124 // if we don't skip the event, the other event handlers won't get it:
125 // try commenting out this line and see what changes
132 DECLARE_EVENT_TABLE()
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 // IDs for the controls and the menu commands
161 // ----------------------------------------------------------------------------
162 // event tables and other macros for wxWindows
163 // ----------------------------------------------------------------------------
165 // the event tables connect the wxWindows events with the functions (event
166 // handlers) which process them. It can be also done at run-time, but for the
167 // simple menu events like this the static method is much simpler.
168 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
169 EVT_MENU(Event_Quit
, MyFrame::OnQuit
)
170 EVT_MENU(Event_About
, MyFrame::OnAbout
)
172 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
174 EVT_MENU(Event_Custom
, MyFrame::OnFireCustom
)
175 EVT_MENU(Event_Test
, MyFrame::OnTest
)
176 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
177 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
179 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
181 EVT_MY_CUSTOM_COMMAND(-1, MyFrame::OnProcessCustom
)
183 // this would also work:
184 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, -1, MyFrame::OnProcessCustom)
187 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
188 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
191 // Create a new application object: this macro will allow wxWindows to create
192 // the application object during program execution (it's better than using a
193 // static object for many reasons) and also declares the accessor function
194 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
198 // ============================================================================
200 // ============================================================================
202 // ----------------------------------------------------------------------------
203 // the application class
204 // ----------------------------------------------------------------------------
206 // 'Main program' equivalent: the program execution "starts" here
209 // create the main application window
210 MyFrame
*frame
= new MyFrame(_T("Event wxWindows Sample"),
211 wxPoint(50, 50), wxSize(600, 340));
213 // and show it (the frames, unlike simple controls, are not shown when
214 // created initially)
217 // success: wxApp::OnRun() will be called which will enter the main message
218 // loop and the application will run. If we returned FALSE here, the
219 // application would exit immediately.
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
228 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
229 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
235 wxMenu
*menuFile
= new wxMenu
;
237 menuFile
->Append(Event_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
238 menuFile
->AppendSeparator();
239 menuFile
->Append(Event_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
241 wxMenu
*menuEvent
= new wxMenu
;
242 menuEvent
->Append(Event_Connect
, _T("&Connect\tCtrl-C"),
243 _T("Connect or disconnect the dynamic event handler"),
244 TRUE
/* checkable */);
245 menuEvent
->Append(Event_Dynamic
, _T("&Dynamic event\tCtrl-D"),
246 _T("Dynamic event sample - only works after Connect"));
247 menuEvent
->AppendSeparator();
248 menuEvent
->Append(Event_Push
, _T("&Push event handler\tCtrl-P"),
249 _T("Push event handler for test event"));
250 menuEvent
->Append(Event_Pop
, _T("P&op event handler\tCtrl-O"),
251 _T("Pop event handler for test event"));
252 menuEvent
->Append(Event_Test
, _T("Test event\tCtrl-T"),
253 _T("Test event processed by pushed event handler"));
254 menuEvent
->AppendSeparator();
255 menuEvent
->Append(Event_Custom
, _T("Fire c&ustom event\tCtrl-U"),
256 _T("Generate a custom event"));
258 // now append the freshly created menu to the menu bar...
259 wxMenuBar
*menuBar
= new wxMenuBar();
260 menuBar
->Append(menuFile
, _T("&File"));
261 menuBar
->Append(menuEvent
, _T("&Event"));
263 // ... and attach this menu bar to the frame
268 SetStatusText(_T("Welcome to wxWindows event sample"));
269 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
270 SetStatusText(_T("Push count: 0"), Status_Push
);
271 #endif // wxUSE_STATUSBAR
276 // we must pop any remaining event handlers to avoid memory leaks and
278 while ( m_nPush
-- != 0 )
280 PopEventHandler(TRUE
/* delete handler */);
284 // ----------------------------------------------------------------------------
285 // standard event handlers
286 // ----------------------------------------------------------------------------
288 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
290 // TRUE is to force the frame to close
294 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
296 wxMessageBox(_T("Event sample shows different ways of using events\n"
297 "© 2001 Vadim Zeitlin"),
298 _T("About Event Sample"), wxOK
| wxICON_INFORMATION
, this);
301 // ----------------------------------------------------------------------------
302 // dynamic event handling stuff
303 // ----------------------------------------------------------------------------
305 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
307 wxMessageBox(_T("This is a dynamic event handler which can be connected "
308 "and disconnected during run-time."),
309 _T("Dynamic Event Handler"), wxOK
| wxICON_INFORMATION
, this);
312 void MyFrame::OnConnect(wxCommandEvent
& event
)
314 if ( event
.IsChecked() )
317 Connect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
,
318 (wxObjectEventFunction
)
320 (wxCommandEventFunction
)&MyFrame::OnDynamic
);
322 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
323 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
327 Disconnect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
);
329 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
330 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
334 // ----------------------------------------------------------------------------
335 // push/pop event handlers support
336 // ----------------------------------------------------------------------------
338 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
340 PushEventHandler(new MyEvtHandler(++m_nPush
));
342 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
345 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
347 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
349 PopEventHandler(TRUE
/* delete handler */);
352 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
355 void MyFrame::OnTest(wxCommandEvent
& event
)
357 wxLogMessage(_T("This is the test event handler in the main frame"));
360 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
362 event
.Enable( m_nPush
> 0 );
365 // ----------------------------------------------------------------------------
366 // custom event methods
367 // ----------------------------------------------------------------------------
369 void MyFrame::OnFireCustom(wxCommandEvent
& event
)
371 wxCommandEvent
eventCustom(wxEVT_MY_CUSTOM_COMMAND
);
373 wxPostEvent(this, eventCustom
);
376 void MyFrame::OnProcessCustom(wxCommandEvent
& event
)
378 wxLogMessage(_T("Got a custom event!"));