]> git.saurik.com Git - wxWidgets.git/blob - samples/event/event.cpp
Dtor for wxHtmlCell
[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 // RCS-ID: $Id$
8 // Copyright: (c) 2001 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 // ----------------------------------------------------------------------------
34 // event constants
35 // ----------------------------------------------------------------------------
36
37 // declare a custom event type
38 //
39 // note that in wxWin 2.3+ these macros expand simply into the following code:
40 //
41 // extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
42 //
43 // const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
44 //
45 // and you may use this code directly if you don't care about 2.2 compatibility
46 BEGIN_DECLARE_EVENT_TYPES()
47 DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777)
48 END_DECLARE_EVENT_TYPES()
49
50 DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)
51
52 // it may also be convenient to define an event table macro for this event type
53 #define EVT_MY_CUSTOM_COMMAND(id, fn) \
54 DECLARE_EVENT_TABLE_ENTRY( \
55 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
56 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
57 (wxObject *) NULL \
58 ),
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp : public wxApp
66 {
67 public:
68 // override base class virtuals
69 // ----------------------------
70
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
75 };
76
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame : public wxFrame
79 {
80 public:
81 // ctor(s)
82 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
83 virtual ~MyFrame();
84
85 void OnQuit(wxCommandEvent& event);
86 void OnAbout(wxCommandEvent& event);
87 void OnConnect(wxCommandEvent& event);
88 void OnDynamic(wxCommandEvent& event);
89 void OnPushEventHandler(wxCommandEvent& event);
90 void OnPopEventHandler(wxCommandEvent& event);
91 void OnTest(wxCommandEvent& event);
92
93 void OnFireCustom(wxCommandEvent& event);
94 void OnProcessCustom(wxCommandEvent& event);
95
96 void OnUpdateUIPop(wxUpdateUIEvent& event);
97
98 protected:
99 // number of pushed event handlers
100 size_t m_nPush;
101
102 private:
103 // any class wishing to process wxWidgets events must use this macro
104 DECLARE_EVENT_TABLE()
105 };
106
107 // Define a custom event handler
108 class MyEvtHandler : public wxEvtHandler
109 {
110 public:
111 MyEvtHandler(size_t level) { m_level = level; }
112
113 void OnTest(wxCommandEvent& event)
114 {
115 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
116
117 // if we don't skip the event, the other event handlers won't get it:
118 // try commenting out this line and see what changes
119 event.Skip();
120 }
121
122 private:
123 size_t m_level;
124
125 DECLARE_EVENT_TABLE()
126 };
127
128 // ----------------------------------------------------------------------------
129 // constants
130 // ----------------------------------------------------------------------------
131
132 // IDs for the controls and the menu commands
133 enum
134 {
135 // menu items
136 Event_Quit = 1,
137 Event_About,
138 Event_Connect,
139 Event_Dynamic,
140 Event_Push,
141 Event_Pop,
142 Event_Custom,
143 Event_Test
144 };
145
146 // status bar fields
147 enum
148 {
149 Status_Main = 0,
150 Status_Dynamic,
151 Status_Push
152 };
153
154 // ----------------------------------------------------------------------------
155 // event tables and other macros for wxWidgets
156 // ----------------------------------------------------------------------------
157
158 // the event tables connect the wxWidgets events with the functions (event
159 // handlers) which process them. It can be also done at run-time, but for the
160 // simple menu events like this the static method is much simpler.
161 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
162 EVT_MENU(Event_Quit, MyFrame::OnQuit)
163 EVT_MENU(Event_About, MyFrame::OnAbout)
164
165 EVT_MENU(Event_Connect, MyFrame::OnConnect)
166
167 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
168 EVT_MENU(Event_Test, MyFrame::OnTest)
169 EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
170 EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
171
172 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
173
174 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
175
176 // the line below would also work if OnProcessCustom() were defined as
177 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
178 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
179 END_EVENT_TABLE()
180
181 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
182 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
183 END_EVENT_TABLE()
184
185 // Create a new application object: this macro will allow wxWidgets to create
186 // the application object during program execution (it's better than using a
187 // static object for many reasons) and also declares the accessor function
188 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
189 // not wxApp)
190 IMPLEMENT_APP(MyApp)
191
192 // ============================================================================
193 // implementation
194 // ============================================================================
195
196 // ----------------------------------------------------------------------------
197 // the application class
198 // ----------------------------------------------------------------------------
199
200 // 'Main program' equivalent: the program execution "starts" here
201 bool MyApp::OnInit()
202 {
203 // create the main application window
204 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
205 wxPoint(50, 50), wxSize(600, 340));
206
207 // and show it (the frames, unlike simple controls, are not shown when
208 // created initially)
209 frame->Show(true);
210
211 // success: wxApp::OnRun() will be called which will enter the main message
212 // loop and the application will run. If we returned false here, the
213 // application would exit immediately.
214 return true;
215 }
216
217 // ----------------------------------------------------------------------------
218 // main frame
219 // ----------------------------------------------------------------------------
220
221 // frame constructor
222 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
223 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
224 {
225 // init members
226 m_nPush = 0;
227
228 // create a menu bar
229 wxMenu *menuFile = new wxMenu;
230
231 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
232 menuFile->AppendSeparator();
233 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
234
235 wxMenu *menuEvent = new wxMenu;
236 menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
237 _T("Connect or disconnect the dynamic event handler"),
238 true /* checkable */);
239 menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
240 _T("Dynamic event sample - only works after Connect"));
241 menuEvent->AppendSeparator();
242 menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
243 _T("Push event handler for test event"));
244 menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
245 _T("Pop event handler for test event"));
246 menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
247 _T("Test event processed by pushed event handler"));
248 menuEvent->AppendSeparator();
249 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
250 _T("Generate a custom event"));
251
252 // now append the freshly created menu to the menu bar...
253 wxMenuBar *menuBar = new wxMenuBar();
254 menuBar->Append(menuFile, _T("&File"));
255 menuBar->Append(menuEvent, _T("&Event"));
256
257 // ... and attach this menu bar to the frame
258 SetMenuBar(menuBar);
259
260 #if wxUSE_STATUSBAR
261 CreateStatusBar(3);
262 SetStatusText(_T("Welcome to wxWidgets event sample"));
263 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
264 SetStatusText(_T("Push count: 0"), Status_Push);
265 #endif // wxUSE_STATUSBAR
266 }
267
268 MyFrame::~MyFrame()
269 {
270 // we must pop any remaining event handlers to avoid memory leaks and
271 // crashes!
272 while ( m_nPush-- != 0 )
273 {
274 PopEventHandler(true /* delete handler */);
275 }
276 }
277
278 // ----------------------------------------------------------------------------
279 // standard event handlers
280 // ----------------------------------------------------------------------------
281
282 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
283 {
284 // true is to force the frame to close
285 Close(true);
286 }
287
288 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
289 {
290 wxMessageBox( wxT("Event sample shows different ways of using events\n")
291 wxT("(c) 2001 Vadim Zeitlin"),
292 wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
293 }
294
295 // ----------------------------------------------------------------------------
296 // dynamic event handling stuff
297 // ----------------------------------------------------------------------------
298
299 void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
300 {
301 wxMessageBox
302 (
303 wxT("This is a dynamic event handler which can be connected ")
304 wxT("and disconnected at run-time."),
305 wxT("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this
306 );
307 }
308
309 void MyFrame::OnConnect(wxCommandEvent& event)
310 {
311 if ( event.IsChecked() )
312 {
313 // disconnect
314 Connect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED,
315 (wxObjectEventFunction)
316 (wxEventFunction)
317 (wxCommandEventFunction)&MyFrame::OnDynamic);
318
319 #if wxUSE_STATUSBAR
320 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
321 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
322 #endif // wxUSE_STATUSBAR
323 }
324 else // connect
325 {
326 Disconnect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED);
327
328 #if wxUSE_STATUSBAR
329 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
330 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
331 #endif // wxUSE_STATUSBAR
332 }
333 }
334
335 // ----------------------------------------------------------------------------
336 // push/pop event handlers support
337 // ----------------------------------------------------------------------------
338
339 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
340 {
341 PushEventHandler(new MyEvtHandler(++m_nPush));
342
343 #if wxUSE_STATUSBAR
344 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
345 #endif // wxUSE_STATUSBAR
346 }
347
348 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
349 {
350 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
351
352 PopEventHandler(true /* delete handler */);
353 m_nPush--;
354
355 #if wxUSE_STATUSBAR
356 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
357 #endif // wxUSE_STATUSBAR
358 }
359
360 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
361 {
362 wxLogMessage(_T("This is the test event handler in the main frame"));
363 }
364
365 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
366 {
367 event.Enable( m_nPush > 0 );
368 }
369
370 // ----------------------------------------------------------------------------
371 // custom event methods
372 // ----------------------------------------------------------------------------
373
374 void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
375 {
376 wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
377
378 wxPostEvent(this, eventCustom);
379 }
380
381 void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
382 {
383 wxLogMessage(_T("Got a custom event!"));
384 }
385