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
)
198 wxMenu
*menuFile
= new wxMenu
;
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");
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");
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");
223 // ... and attach this menu bar to the frame
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
236 // we must pop any remaining event handlers to avoid memory leaks and
238 while ( m_nPush
-- != 0 )
240 PopEventHandler(TRUE
/* delete handler */);
244 // ----------------------------------------------------------------------------
245 // standard event handlers
246 // ----------------------------------------------------------------------------
248 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
250 // TRUE is to force the frame to close
254 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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);
261 // ----------------------------------------------------------------------------
262 // dynamic event handling stuff
263 // ----------------------------------------------------------------------------
265 void MyFrame::OnDynamic(wxCommandEvent
& WXUNUSED(event
))
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);
272 void MyFrame::OnConnect(wxCommandEvent
& event
)
274 if ( event
.IsChecked() )
277 Connect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
,
278 (wxObjectEventFunction
)
280 (wxCommandEventFunction
)&MyFrame::OnDynamic
);
282 SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
283 SetStatusText(_T("Dynamic: on"), Status_Dynamic
);
287 Disconnect(Event_Dynamic
, -1, wxEVT_COMMAND_MENU_SELECTED
);
289 SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
290 SetStatusText(_T("Dynamic: off"), Status_Dynamic
);
294 // ----------------------------------------------------------------------------
295 // push/pop event handlers support
296 // ----------------------------------------------------------------------------
298 void MyFrame::OnPushEventHandler(wxCommandEvent
& WXUNUSED(event
))
300 PushEventHandler(new MyEvtHandler(++m_nPush
));
302 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
305 void MyFrame::OnPopEventHandler(wxCommandEvent
& WXUNUSED(event
))
307 wxCHECK_RET( m_nPush
, _T("this command should be disabled!") );
309 PopEventHandler(TRUE
/* delete handler */);
312 SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush
), Status_Push
);
315 void MyFrame::OnTest(wxCommandEvent
& event
)
317 wxLogMessage(_T("This is the test event handler in the main frame"));
320 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent
& event
)
322 event
.Enable( m_nPush
> 0 );