]> git.saurik.com Git - wxWidgets.git/blame - samples/event/event.cpp
don't link GUI libs into wxrc
[wxWidgets.git] / samples / event / event.cpp
CommitLineData
6164f93c
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: event.cpp
be5a51fb 3// Purpose: wxWidgets sample demonstrating different event usage
6164f93c
VZ
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
788233da 20#if defined(__GNUG__) && !defined(__APPLE__)
6164f93c
VZ
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
be5a51fb 35// need because it includes almost all "standard" wxWidgets headers)
6164f93c
VZ
36#ifndef WX_PRECOMP
37 #include "wx/wx.h"
38#endif
39
4ec2df6c
VZ
40// ----------------------------------------------------------------------------
41// event constants
42// ----------------------------------------------------------------------------
43
44// declare a custom event type
45//
46// note that in wxWin 2.3+ these macros expand simply into the following code:
47//
48// extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
49//
50// const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
51//
52// and you may use this code directly if you don't care about 2.2 compatibility
53BEGIN_DECLARE_EVENT_TYPES()
54 DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777)
55END_DECLARE_EVENT_TYPES()
56
57DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)
58
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( \
07850a49 62 wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
3a818b15 63 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
4ec2df6c
VZ
64 (wxObject *) NULL \
65 ),
66
6164f93c
VZ
67// ----------------------------------------------------------------------------
68// private classes
69// ----------------------------------------------------------------------------
70
71// Define a new application type, each program should derive a class from wxApp
72class MyApp : public wxApp
73{
74public:
75 // override base class virtuals
76 // ----------------------------
77
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();
82};
83
84// Define a new frame type: this is going to be our main frame
85class MyFrame : public wxFrame
86{
87public:
88 // ctor(s)
89 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
90 virtual ~MyFrame();
91
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);
99
4ec2df6c
VZ
100 void OnFireCustom(wxCommandEvent& event);
101 void OnProcessCustom(wxCommandEvent& event);
102
6164f93c
VZ
103 void OnUpdateUIPop(wxUpdateUIEvent& event);
104
105protected:
106 // number of pushed event handlers
107 size_t m_nPush;
108
109private:
be5a51fb 110 // any class wishing to process wxWidgets events must use this macro
6164f93c
VZ
111 DECLARE_EVENT_TABLE()
112};
113
114// Define a custom event handler
115class MyEvtHandler : public wxEvtHandler
116{
117public:
118 MyEvtHandler(size_t level) { m_level = level; }
119
120 void OnTest(wxCommandEvent& event)
121 {
122 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
123
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
126 event.Skip();
127 }
128
129private:
130 size_t m_level;
131
132 DECLARE_EVENT_TABLE()
133};
134
135// ----------------------------------------------------------------------------
136// constants
137// ----------------------------------------------------------------------------
138
139// IDs for the controls and the menu commands
140enum
141{
142 // menu items
143 Event_Quit = 1,
144 Event_About,
145 Event_Connect,
146 Event_Dynamic,
147 Event_Push,
148 Event_Pop,
4ec2df6c 149 Event_Custom,
6164f93c
VZ
150 Event_Test
151};
152
153// status bar fields
154enum
155{
156 Status_Main = 0,
157 Status_Dynamic,
158 Status_Push
159};
160
161// ----------------------------------------------------------------------------
be5a51fb 162// event tables and other macros for wxWidgets
6164f93c
VZ
163// ----------------------------------------------------------------------------
164
be5a51fb 165// the event tables connect the wxWidgets events with the functions (event
6164f93c
VZ
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.
168BEGIN_EVENT_TABLE(MyFrame, wxFrame)
169 EVT_MENU(Event_Quit, MyFrame::OnQuit)
170 EVT_MENU(Event_About, MyFrame::OnAbout)
171
172 EVT_MENU(Event_Connect, MyFrame::OnConnect)
173
4ec2df6c 174 EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
6164f93c
VZ
175 EVT_MENU(Event_Test, MyFrame::OnTest)
176 EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
177 EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
178
179 EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
4ec2df6c 180
07850a49 181 EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
4b614012 182
19fe5fd1
VZ
183 // the line below would also work if OnProcessCustom() were defined as
184 // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
07850a49 185 //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
6164f93c
VZ
186END_EVENT_TABLE()
187
188BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
189 EVT_MENU(Event_Test, MyEvtHandler::OnTest)
190END_EVENT_TABLE()
191
be5a51fb 192// Create a new application object: this macro will allow wxWidgets to create
6164f93c
VZ
193// the application object during program execution (it's better than using a
194// static object for many reasons) and also declares the accessor function
195// wxGetApp() which will return the reference of the right type (i.e. MyApp and
196// not wxApp)
197IMPLEMENT_APP(MyApp)
198
199// ============================================================================
200// implementation
201// ============================================================================
202
203// ----------------------------------------------------------------------------
204// the application class
205// ----------------------------------------------------------------------------
206
207// 'Main program' equivalent: the program execution "starts" here
208bool MyApp::OnInit()
209{
210 // create the main application window
be5a51fb 211 MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
4ec2df6c 212 wxPoint(50, 50), wxSize(600, 340));
6164f93c
VZ
213
214 // and show it (the frames, unlike simple controls, are not shown when
215 // created initially)
07850a49 216 frame->Show(true);
6164f93c
VZ
217
218 // success: wxApp::OnRun() will be called which will enter the main message
07850a49 219 // loop and the application will run. If we returned false here, the
6164f93c 220 // application would exit immediately.
07850a49 221 return true;
6164f93c
VZ
222}
223
224// ----------------------------------------------------------------------------
225// main frame
226// ----------------------------------------------------------------------------
227
228// frame constructor
229MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
07850a49 230 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
6164f93c 231{
3aa467b6
VZ
232 // init members
233 m_nPush = 0;
234
6164f93c
VZ
235 // create a menu bar
236 wxMenu *menuFile = new wxMenu;
237
4ec2df6c 238 menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
6164f93c 239 menuFile->AppendSeparator();
4ec2df6c 240 menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
6164f93c
VZ
241
242 wxMenu *menuEvent = new wxMenu;
4ec2df6c
VZ
243 menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
244 _T("Connect or disconnect the dynamic event handler"),
07850a49 245 true /* checkable */);
4ec2df6c
VZ
246 menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
247 _T("Dynamic event sample - only works after Connect"));
248 menuEvent->AppendSeparator();
249 menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
250 _T("Push event handler for test event"));
251 menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
252 _T("Pop event handler for test event"));
253 menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
254 _T("Test event processed by pushed event handler"));
6164f93c 255 menuEvent->AppendSeparator();
4ec2df6c
VZ
256 menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
257 _T("Generate a custom event"));
6164f93c
VZ
258
259 // now append the freshly created menu to the menu bar...
260 wxMenuBar *menuBar = new wxMenuBar();
4ec2df6c
VZ
261 menuBar->Append(menuFile, _T("&File"));
262 menuBar->Append(menuEvent, _T("&Event"));
6164f93c
VZ
263
264 // ... and attach this menu bar to the frame
265 SetMenuBar(menuBar);
266
267#if wxUSE_STATUSBAR
268 CreateStatusBar(3);
be5a51fb 269 SetStatusText(_T("Welcome to wxWidgets event sample"));
6164f93c
VZ
270 SetStatusText(_T("Dynamic: off"), Status_Dynamic);
271 SetStatusText(_T("Push count: 0"), Status_Push);
272#endif // wxUSE_STATUSBAR
273}
274
275MyFrame::~MyFrame()
276{
277 // we must pop any remaining event handlers to avoid memory leaks and
278 // crashes!
279 while ( m_nPush-- != 0 )
280 {
07850a49 281 PopEventHandler(true /* delete handler */);
6164f93c
VZ
282 }
283}
284
285// ----------------------------------------------------------------------------
286// standard event handlers
287// ----------------------------------------------------------------------------
288
289void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
290{
07850a49
WS
291 // true is to force the frame to close
292 Close(true);
6164f93c
VZ
293}
294
295void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
296{
f565a6c2
VZ
297 wxMessageBox( wxT("Event sample shows different ways of using events\n")
298