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 // Define a new application type, each program should derive a class from wxApp
45 class MyApp
: public wxApp
48 // override base class virtuals
49 // ----------------------------
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();
57 // Define a new frame type: this is going to be our main frame
58 class MyFrame
: public wxFrame
62 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
73 void OnUpdateUIPop(wxUpdateUIEvent
& event
);
76 // number of pushed event handlers
80 // any class wishing to process wxWindows events must use this macro
84 // Define a custom event handler
85 class MyEvtHandler
: public wxEvtHandler
88 MyEvtHandler(size_t level
) { m_level
= level
; }
90 void OnTest(wxCommandEvent
& event
)
92 wxLogMessage(_T("This is the pushed test event handler #%u"), m_level
);
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
102 DECLARE_EVENT_TABLE()
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // IDs for the controls and the menu commands
130 // ----------------------------------------------------------------------------
131 // event tables and other macros for wxWindows
132 // ----------------------------------------------------------------------------
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
)
141 EVT_MENU(Event_Connect
, MyFrame::OnConnect
)
143 EVT_MENU(Event_Test
, MyFrame::OnTest
)
144 EVT_MENU(Event_Push
, MyFrame::OnPushEventHandler
)
145 EVT_MENU(Event_Pop
, MyFrame::OnPopEventHandler
)
147 EVT_UPDATE_UI(Event_Pop
, MyFrame::OnUpdateUIPop
)
150 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
151 EVT_MENU(Event_Test
, MyEvtHandler::OnTest
)
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
161 // ============================================================================
163 // ============================================================================
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
169 // 'Main program' equivalent: the program execution "starts" here
172 // create the main application window
173 MyFrame
*frame
= new MyFrame("Event wxWindows Sample",
174 wxPoint(50, 50), wxSize(450, 340));
176 // and show it (the frames, unlike simple controls, are not shown when
177 // created initially)
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.
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
192 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
195 wxMenu
*menuFile
= new wxMenu
;
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");
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");
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");
220 // ... and attach this menu bar to the frame
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
233 // we must pop any remaining event handlers to avoid memory leaks and
235 while ( m_nPush
-- != 0 )
237 PopEventHandler(TRUE
/* delete handler */);
241 // ----------------------------------------------------------------------------
242 // standard event handlers
243 // ----------------------------------------------------------------------------
245 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
247 // TRUE is to force the frame to close
251 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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);
258 // ----------------------------------------------------------------------------
259 // dynamic event handling stuff
260 // ----------------------------------------------------------------------------
262 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
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);
269 void MyFrame::OnConnect(wxCommandEvent
& event
)
271 if ( event
.IsChecked() )
274 Connect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
,
275 (wxObjectEventFunction
)
277 (wxCommandEventFunction
)&MyFrame::OnDynamic
);
279 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
280 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
284 Disconnect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
);
286 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
287 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
291 // ----------------------------------------------------------------------------
292 // push/pop event handlers support
293 // ----------------------------------------------------------------------------
295 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
297 PushEventHandler(new MyEvtHandler(++m_nPush
));
299 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
302 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
304 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
306 PopEventHandler(TRUE
/* delete handler */);
309 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
312 void MyFrame::OnTest(wxCommandEvent
& event
)
314 wxLogMessage(_T("This is the test event handler in the main frame"));
317 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
319 event
.Enable( m_nPush
> 0 );