]> git.saurik.com Git - wxWidgets.git/blob - samples/event/event.cpp
fixed 'patch' #422993 (Error in wxConfigBase::Write for doubles)
[wxWidgets.git] / samples / event / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: event.cpp
3 // Purpose: wxWindows 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 #ifdef __GNUG__
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"
25 #endif
26
27 // For compilers that support precompilation, includes "wx/wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 // for all others, include the necessary headers (this file is usually all you
35 // need because it includes almost all "standard" wxWindows headers)
36 #ifndef WX_PRECOMP
37 #include "wx/wx.h"
38 #endif
39
40 // ----------------------------------------------------------------------------
41 // private classes
42 // ----------------------------------------------------------------------------
43
44 // Define a new application type, each program should derive a class from wxApp
45 class MyApp : public wxApp
46 {
47 public:
48 // override base class virtuals
49 // ----------------------------
50
51 // this one is called on application startup and is a good place for the app
52 // initialization (doing it here and not in the ctor allows to have an error
53 // return: if OnInit() returns false, the application terminates)
54 virtual bool OnInit();
55 };
56
57 // Define a new frame type: this is going to be our main frame
58 class MyFrame : public wxFrame
59 {
60 public:
61 // ctor(s)
62 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
63 virtual ~MyFrame();
64
65 void OnQuit(wxCommandEvent& event);
66 void OnAbout(wxCommandEvent& event);
67 void OnConnect(wxCommandEvent& event);
68 void OnDynamic(wxCommandEvent& event);
69 void OnPushEventHandler(wxCommandEvent& event);
70 void OnPopEventHandler(wxCommandEvent& event);
71 void OnTest(wxCommandEvent& event);
72
73 void OnUpdateUIPop(wxUpdateUIEvent& event);
74
75 protected:
76 // number of pushed event handlers
77 size_t m_nPush;
78
79 private:
80 // any class wishing to process wxWindows events must use this macro
81 DECLARE_EVENT_TABLE()
82 };
83
84 // Define a custom event handler
85 class MyEvtHandler : public wxEvtHandler
86 {
87 public:
88 MyEvtHandler(size_t level) { m_level = level; }
89
90 void OnTest(wxCommandEvent& event)
91 {
92 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
93
94 // if we don't skip the event, the other event handlers won't get it:
95 // try commenting out this line and see what changes
96 event.Skip();
97 }
98
99 private:
100 size_t m_level;
101
102 DECLARE_EVENT_TABLE()
103 };
104
105 // ----------------------------------------------------------------------------
106 // constants
107 // ----------------------------------------------------------------------------
108
109 // IDs for the controls and the menu commands
110 enum
111 {
112 // menu items
113 Event_Quit = 1,
114 Event_About,
115 Event_Connect,
116 Event_Dynamic,
117 Event_Push,
118 Event_Pop,
119 Event_Test
120 };
121
122 // status bar fields
123 enum
124 {
125 Status_Main = 0,
126 Status_Dynamic,
127 Status_Push
128 };
129
130 // ----------------------------------------------------------------------------
131 // event tables and other macros for wxWindows
132 // ----------------------------------------------------------------------------
133
134 // the event tables connect the wxWindows events with the functions (event
135 // handlers) which process them. It can be also done at run-time, but for the
136 // simple menu events like this the static method is much simpler.
137 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
138 EVT_MENU(Event_Quit, MyFrame::OnQuit)
139 EVT_MENU(Event_About, MyFrame::OnAbout)
140
141 EVT_MENU(Event_Connect, MyFrame::OnConnect)
142
143 EVT_MENU(Event_Test, MyFrame::OnTest)
144 EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
145 EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
146
147 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
148 END_EVENT_TABLE()
149
150 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
151 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
152 END_EVENT_TABLE()
153
154 // Create a new application object: this macro will allow wxWindows to create
155 // the application object during program execution (it's better than using a
156 // static object for many reasons) and also declares the accessor function
157 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
158 // not wxApp)
159 IMPLEMENT_APP(MyApp)
160
161 // ============================================================================
162 // implementation
163 // ============================================================================
164
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
168
169 // 'Main program' equivalent: the program execution "starts" here
170 bool MyApp::OnInit()
171 {
172 // create the main application window
173 MyFrame *frame = new MyFrame("Event wxWindows Sample",
174 wxPoint(50, 50), wxSize(450, 340));
175
176 // and show it (the frames, unlike simple controls, are not shown when
177 // created initially)
178 frame->Show(TRUE);
179
180 // success: wxApp::OnRun() will be called which will enter the main message
181 // loop and the application will run. If we returned FALSE here, the
182 // application would exit immediately.
183 return TRUE;
184 }
185
186 // ----------------------------------------------------------------------------
187 // main frame
188 // ----------------------------------------------------------------------------
189
190 // frame constructor
191 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
192 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
193 {
194 // create a menu bar
195 wxMenu *menuFile = new wxMenu;
196
197 menuFile->Append(Event_About, "&About...\tCtrl-A", "Show about dialog");
198 menuFile->AppendSeparator();
199 menuFile->Append(Event_Quit, "E&xit\tAlt-X", "Quit this program");
200
201 wxMenu *menuEvent = new wxMenu;
202 menuEvent->Append(Event_Connect, "&Connect\tCtrl-C",
203 "Connect or disconnect the dynamic event handler",
204 TRUE /* checkable */);
205 menuEvent->Append(Event_Dynamic, "&Dynamic event\tCtrl-D",
206 "Dynamic event sample - only works after Connect");
207 menuEvent->AppendSeparator();
208 menuEvent->Append(Event_Push, "&Push event handler\tCtrl-P",
209 "Push event handler for test event");
210 menuEvent->Append(Event_Pop, "P&op event handler\tCtrl-O",
211 "Pop event handler for test event");
212 menuEvent->Append(Event_Test, "Test event\tCtrl-T",
213 "Test event processed by pushed event handler");
214
215 // now append the freshly created menu to the menu bar...
216 wxMenuBar *menuBar = new wxMenuBar();
217 menuBar->Append(menuFile, "&File");
218 menuBar->Append(menuEvent, "&Event");
219
220 // ... and attach this menu bar to the frame
221 SetMenuBar(menuBar);
222
223 #if wxUSE_STATUSBAR
224 CreateStatusBar(3);
225 SetStatusText("Welcome to wxWindows event sample");
226 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
227 SetStatusText(_T("Push count: 0"), Status_Push);
228 #endif // wxUSE_STATUSBAR
229 }
230
231 MyFrame::~MyFrame()
232 {
233 // we must pop any remaining event handlers to avoid memory leaks and
234 // crashes!
235 while ( m_nPush-- != 0 )
236 {
237 PopEventHandler(TRUE /* delete handler */);
238 }
239 }
240
241 // ----------------------------------------------------------------------------
242 // standard event handlers
243 // ----------------------------------------------------------------------------
244
245 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
246 {
247 // TRUE is to force the frame to close
248 Close(TRUE);
249 }
250
251 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
252 {
253 wxMessageBox(_T("Event sample shows different ways of using events\n"
254 "© 2001 Vadim Zeitlin"),
255 _T("About Event Sample"), wxOK | wxICON_INFORMATION, this);
256 }
257
258 // ----------------------------------------------------------------------------
259 // dynamic event handling stuff
260 // ----------------------------------------------------------------------------
261
262 void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
263 {
264 wxMessageBox(_T("This is a dynamic event handler which can be connected "
265 "and disconnected during run-time."),
266 _T("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this);
267 }
268
269 void MyFrame::OnConnect(wxCommandEvent& event)
270 {
271 if ( event.IsChecked() )
272 {
273 // disconnect
274 Connect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED,
275 (wxObjectEventFunction)
276 (wxEventFunction)
277 (wxCommandEventFunction)&MyFrame::OnDynamic);
278
279 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
280 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
281 }
282 else // connect
283 {
284 Disconnect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED);
285
286 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
287 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
288 }
289 }
290
291 // ----------------------------------------------------------------------------
292 // push/pop event handlers support
293 // ----------------------------------------------------------------------------
294
295 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
296 {
297 PushEventHandler(new MyEvtHandler(++m_nPush));
298
299 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
300 }
301
302 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
303 {
304 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
305
306 PopEventHandler(TRUE /* delete handler */);
307 m_nPush--;
308
309 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
310 }
311
312 void MyFrame::OnTest(wxCommandEvent& event)
313 {
314 wxLogMessage(_T("This is the test event handler in the main frame"));
315 }
316
317 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
318 {
319 event.Enable( m_nPush > 0 );
320 }
321