]> git.saurik.com Git - wxWidgets.git/blob - samples/event/event.cpp
Fix for missing -g from wx-config
[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 // init members
195 m_nPush = 0;
196
197 // create a menu bar
198 wxMenu *menuFile = new wxMenu;
199
200 menuFile->Append(Event_About, "&About...\tCtrl-A", "Show about dialog");
201 menuFile->AppendSeparator();
202 menuFile->Append(Event_Quit, "E&xit\tAlt-X", "Quit this program");
203
204 wxMenu *menuEvent = new wxMenu;
205 menuEvent->Append(Event_Connect, "&Connect\tCtrl-C",
206 "Connect or disconnect the dynamic event handler",
207 TRUE /* checkable */);
208 menuEvent->Append(Event_Dynamic, "&Dynamic event\tCtrl-D",
209 "Dynamic event sample - only works after Connect");
210 menuEvent->AppendSeparator();
211 menuEvent->Append(Event_Push, "&Push event handler\tCtrl-P",
212 "Push event handler for test event");
213 menuEvent->Append(Event_Pop, "P&op event handler\tCtrl-O",
214 "Pop event handler for test event");
215 menuEvent->Append(Event_Test, "Test event\tCtrl-T",
216 "Test event processed by pushed event handler");
217
218 // now append the freshly created menu to the menu bar...
219 wxMenuBar *menuBar = new wxMenuBar();
220 menuBar->Append(menuFile, "&File");
221 menuBar->Append(menuEvent, "&Event");
222
223 // ... and attach this menu bar to the frame
224 SetMenuBar(menuBar);
225
226 #if wxUSE_STATUSBAR
227 CreateStatusBar(3);
228 SetStatusText("Welcome to wxWindows event sample");
229 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
230 SetStatusText(_T("Push count: 0"), Status_Push);
231 #endif // wxUSE_STATUSBAR
232 }
233
234 MyFrame::~MyFrame()
235 {
236 // we must pop any remaining event handlers to avoid memory leaks and
237 // crashes!
238 while ( m_nPush-- != 0 )
239 {
240 PopEventHandler(TRUE /* delete handler */);
241 }
242 }
243
244 // ----------------------------------------------------------------------------
245 // standard event handlers
246 // ----------------------------------------------------------------------------
247
248 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
249 {
250 // TRUE is to force the frame to close
251 Close(TRUE);
252 }
253
254 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
255 {
256 wxMessageBox(_T("Event sample shows different ways of using events\n"
257 "© 2001 Vadim Zeitlin"),
258 _T("About Event Sample"), wxOK | wxICON_INFORMATION, this);
259 }
260
261 // ----------------------------------------------------------------------------
262 // dynamic event handling stuff
263 // ----------------------------------------------------------------------------
264
265 void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
266 {
267 wxMessageBox(_T("This is a dynamic event handler which can be connected "
268 "and disconnected during run-time."),
269 _T("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this);
270 }
271
272 void MyFrame::OnConnect(wxCommandEvent& event)
273 {
274 if ( event.IsChecked() )
275 {
276 // disconnect
277 Connect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED,
278 (wxObjectEventFunction)
279 (wxEventFunction)
280 (wxCommandEventFunction)&MyFrame::OnDynamic);
281
282 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
283 SetStatusText(_T("Dynamic: on"), Status_Dynamic);
284 }
285 else // connect
286 {
287 Disconnect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED);
288
289 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
290 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
291 }
292 }
293
294 // ----------------------------------------------------------------------------
295 // push/pop event handlers support
296 // ----------------------------------------------------------------------------
297
298 void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
299 {
300 PushEventHandler(new MyEvtHandler(++m_nPush));
301
302 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
303 }
304
305 void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
306 {
307 wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
308
309 PopEventHandler(TRUE /* delete handler */);
310 m_nPush--;
311
312 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
313 }
314
315 void MyFrame::OnTest(wxCommandEvent& event)
316 {
317 wxLogMessage(_T("This is the test event handler in the main frame"));
318 }
319
320 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
321 {
322 event.Enable( m_nPush > 0 );
323 }
324