]>
Commit | Line | Data |
---|---|---|
556b8c1a | 1 | ///////////////////////////////////////////////////////////////////////////// |
f78e4942 VZ |
2 | // Name: samples/except/except.cpp |
3 | // Purpose: shows how C++ exceptions can be used in wxWidgets | |
4 | // Author: Vadim Zeitlin | |
556b8c1a | 5 | // Modified by: |
f78e4942 | 6 | // Created: 2003-09-17 |
556b8c1a | 7 | // RCS-ID: $Id$ |
f78e4942 | 8 | // Copyright: (c) 2003-2005 Vadim Zeitlin |
556b8c1a VZ |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
f78e4942 VZ |
27 | #if !wxUSE_EXCEPTIONS |
28 | #error "This sample only works with wxUSE_EXCEPTIONS == 1" | |
29 | #endif // !wxUSE_EXCEPTIONS | |
30 | ||
556b8c1a | 31 | // for all others, include the necessary headers (this file is usually all you |
be5a51fb | 32 | // need because it includes almost all "standard" wxWidgets headers) |
556b8c1a | 33 | #ifndef WX_PRECOMP |
532c7736 VZ |
34 | #include "wx/log.h" |
35 | ||
556b8c1a VZ |
36 | #include "wx/app.h" |
37 | #include "wx/frame.h" | |
38 | #include "wx/dialog.h" | |
532c7736 | 39 | #include "wx/menu.h" |
556b8c1a VZ |
40 | |
41 | #include "wx/button.h" | |
42 | #include "wx/sizer.h" | |
43 | ||
30deac1f WS |
44 | #include "wx/utils.h" |
45 | #include "wx/msgdlg.h" | |
60c474a0 | 46 | #include "wx/icon.h" |
556b8c1a VZ |
47 | #endif |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // resources | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | // the application icon (under Windows and OS/2 it is in resources) | |
54 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) | |
55 | #include "../sample.xpm" | |
56 | #endif | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // private classes | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // Define a new application type, each program should derive a class from wxApp | |
63 | class MyApp : public wxApp | |
64 | { | |
65 | public: | |
66 | // override base class virtuals | |
67 | // ---------------------------- | |
68 | ||
69 | // program startup | |
70 | virtual bool OnInit(); | |
71 | ||
72 | // 2nd-level exception handling: we get all the exceptions occuring in any | |
73 | // event handler here | |
74 | virtual void HandleEvent(wxEvtHandler *handler, | |
75 | wxEventFunction func, | |
76 | wxEvent& event) const; | |
77 | ||
78 | // 3rd, and final, level exception handling: whenever an unhandled | |
79 | // exception is caught, this function is called | |
80 | virtual void OnUnhandledException(); | |
81 | }; | |
82 | ||
83 | // Define a new frame type: this is going to be our main frame | |
84 | class MyFrame : public wxFrame | |
85 | { | |
86 | public: | |
87 | // ctor(s) | |
88 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, | |
89 | long style = wxDEFAULT_FRAME_STYLE); | |
90 | ||
91 | // event handlers (these functions should _not_ be virtual) | |
92 | void OnQuit(wxCommandEvent& event); | |
93 | void OnAbout(wxCommandEvent& event); | |
94 | void OnDialog(wxCommandEvent& event); | |
95 | void OnThrowString(wxCommandEvent& event); | |
96 | ||
97 | // 1st-level exception handling: we overload ProcessEvent() to be able to | |
98 | // catch exceptions which occur in MyFrame methods here | |
99 | virtual bool ProcessEvent(wxEvent& event); | |
100 | ||
101 | private: | |
be5a51fb | 102 | // any class wishing to process wxWidgets events must use this macro |
556b8c1a VZ |
103 | DECLARE_EVENT_TABLE() |
104 | }; | |
105 | ||
106 | // A simple dialog which has only some buttons to throw exceptions | |
107 | class MyDialog : public wxDialog | |
108 | { | |
109 | public: | |
110 | MyDialog(wxFrame *parent); | |
111 | ||
112 | // event handlers | |
113 | void OnThrowInt(wxCommandEvent& event); | |
114 | void OnThrowObject(wxCommandEvent& event); | |
f78e4942 | 115 | void OnCrash(wxCommandEvent& event); |
556b8c1a VZ |
116 | |
117 | private: | |
118 | DECLARE_EVENT_TABLE() | |
119 | }; | |
120 | ||
121 | // A trivial exception class | |
122 | class MyException | |
123 | { | |
124 | public: | |
125 | MyException(const wxString& msg) : m_msg(msg) { } | |
126 | ||
127 | const wxChar *what() const { return m_msg.c_str(); } | |
128 | ||
129 | private: | |
130 | wxString m_msg; | |
131 | }; | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // constants | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | // IDs for the controls and the menu commands | |
138 | enum | |
139 | { | |
140 | // control ids | |
141 | Except_ThrowInt = 100, | |
142 | Except_ThrowObject, | |
f78e4942 | 143 | Except_Crash, |
556b8c1a VZ |
144 | |
145 | // menu items | |
146 | Except_ThrowString = 200, | |
147 | Except_Dialog, | |
148 | ||
149 | Except_Quit = wxID_EXIT, | |
150 | Except_About = wxID_ABOUT | |
151 | }; | |
152 | ||
153 | // ---------------------------------------------------------------------------- | |
be5a51fb | 154 | // event tables and other macros for wxWidgets |
556b8c1a VZ |
155 | // ---------------------------------------------------------------------------- |
156 | ||
be5a51fb | 157 | // the event tables connect the wxWidgets events with the functions (event |
556b8c1a VZ |
158 | // handlers) which process them. It can be also done at run-time, but for the |
159 | // simple menu events like this the static method is much simpler. | |
160 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
161 | EVT_MENU(Except_Quit, MyFrame::OnQuit) | |
162 | EVT_MENU(Except_About, MyFrame::OnAbout) | |
163 | EVT_MENU(Except_Dialog, MyFrame::OnDialog) | |
164 | EVT_MENU(Except_ThrowString, MyFrame::OnThrowString) | |
165 | END_EVENT_TABLE() | |
166 | ||
167 | BEGIN_EVENT_TABLE(MyDialog, wxDialog) | |
168 | EVT_BUTTON(Except_ThrowInt, MyDialog::OnThrowInt) | |
169 | EVT_BUTTON(Except_ThrowObject, MyDialog::OnThrowObject) | |
f78e4942 | 170 | EVT_BUTTON(Except_Crash, MyDialog::OnCrash) |
556b8c1a VZ |
171 | END_EVENT_TABLE() |
172 | ||
be5a51fb | 173 | // Create a new application object: this macro will allow wxWidgets to create |
556b8c1a VZ |
174 | // the application object during program execution (it's better than using a |
175 | // static object for many reasons) and also implements the accessor function | |
176 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
177 | // not wxApp) | |
178 | IMPLEMENT_APP(MyApp) | |
179 | ||
180 | // ============================================================================ | |
181 | // MyApp implementation | |
182 | // ============================================================================ | |
183 | ||
184 | // 'Main program' equivalent: the program execution "starts" here | |
185 | bool MyApp::OnInit() | |
186 | { | |
187 | // create the main application window | |
be5a51fb | 188 | MyFrame *frame = new MyFrame(_T("Except wxWidgets App"), |
556b8c1a VZ |
189 | wxPoint(50, 50), wxSize(450, 340)); |
190 | ||
191 | // and show it (the frames, unlike simple controls, are not shown when | |
192 | // created initially) | |
193 | frame->Show(true); | |
194 | ||
195 | // success: wxApp::OnRun() will be called which will enter the main message | |
196 | // loop and the application will run. If we returned false here, the | |
197 | // application would exit immediately. | |
198 | return true; | |
199 | } | |
200 | ||
201 | void | |
202 | MyApp::HandleEvent(wxEvtHandler *handler, | |
203 | wxEventFunction func, | |
204 | wxEvent& event) const | |
205 | { | |
206 | try | |
207 | { | |
208 | wxApp::HandleEvent(handler, func, event); | |
209 | } | |
210 | catch ( int i ) | |
211 | { | |
212 | wxLogError(_T("Caught an int %d in MyApp."), i); | |
213 | } | |
214 | } | |
215 | ||
216 | void MyApp::OnUnhandledException() | |
217 | { | |
218 | wxMessageBox(_T("Unhandled exception caught, program will terminate."), | |
219 | _T("wxExcept Sample"), wxOK | wxICON_ERROR); | |
220 | } | |
221 | ||
222 | // ============================================================================ | |
223 | // MyFrame implementation | |
224 | // ============================================================================ | |
225 | ||
226 | // frame constructor | |
227 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) | |
30deac1f | 228 | : wxFrame(NULL, wxID_ANY, title, pos, size, style) |
556b8c1a VZ |
229 | { |
230 | // set the frame icon | |
d32b901b | 231 | SetIcon(wxICON(sample)); |
556b8c1a VZ |
232 | |
233 | #if wxUSE_MENUS | |
234 | // create a menu bar | |
235 | wxMenu *menuFile = new wxMenu; | |
236 | menuFile->Append(Except_Dialog, _T("Show &dialog\tCtrl-D")); | |
237 | menuFile->Append(Except_ThrowString, _T("Throw a &string\tCtrl-S")); | |
238 | menuFile->AppendSeparator(); | |
239 | menuFile->Append(Except_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); | |
240 | ||
241 | wxMenu *helpMenu = new wxMenu; | |
242 | helpMenu->Append(Except_About, _T("&About...\tF1"), _T("Show about dialog")); | |
243 | ||
244 | // now append the freshly created menu to the menu bar... | |
245 | wxMenuBar *menuBar = new wxMenuBar(); | |
246 | menuBar->Append(menuFile, _T("&File")); | |
247 | menuBar->Append(helpMenu, _T("&Help")); | |
248 | ||
249 | // ... and attach this menu bar to the frame | |
250 | SetMenuBar(menuBar); | |
251 | #endif // wxUSE_MENUS | |
252 | ||
253 | #if wxUSE_STATUSBAR && !defined(__WXWINCE__) | |
254 | // create a status bar just for fun (by default with 1 pane only) | |
255 | CreateStatusBar(2); | |
be5a51fb | 256 | SetStatusText(_T("Welcome to wxWidgets!")); |
556b8c1a VZ |
257 | #endif // wxUSE_STATUSBAR |
258 | } | |
259 | ||
260 | bool MyFrame::ProcessEvent(wxEvent& event) | |
261 | { | |
262 | try | |
263 | { | |
264 | return wxFrame::ProcessEvent(event); | |
265 | } | |
266 | catch ( const wxChar *msg ) | |
267 | { | |
268 | wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg); | |
269 | ||
270 | return true; | |
271 | } | |
272 | } | |
273 | ||
274 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
275 | { | |
276 | // true is to force the frame to close | |
277 | Close(true); | |
278 | } | |
279 | ||
280 | void MyFrame::OnDialog(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
df5a063f VZ |
282 | try |
283 | { | |
284 | MyDialog dlg(this); | |
556b8c1a | 285 | |
df5a063f VZ |
286 | dlg.ShowModal(); |
287 | } | |
288 | catch ( ... ) | |
289 | { | |
290 | Destroy(); | |
291 | throw; | |
292 | } | |
556b8c1a VZ |
293 | } |
294 | ||
295 | void MyFrame::OnThrowString(wxCommandEvent& WXUNUSED(event)) | |
296 | { | |
297 | throw _T("some string"); | |
298 | } | |
299 | ||
300 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
301 | { | |
302 | wxString msg; | |
303 | msg.Printf( _T("This is the About dialog of the except sample.\n") | |
304 | _T("Welcome to %s"), wxVERSION_STRING); | |
305 | ||
306 | wxMessageBox(msg, _T("About Except"), wxOK | wxICON_INFORMATION, this); | |
307 | } | |
308 | ||
309 | // ============================================================================ | |
310 | // MyDialog implementation | |
311 | // ============================================================================ | |
312 | ||
313 | MyDialog::MyDialog(wxFrame *parent) | |
30deac1f | 314 | : wxDialog(parent, wxID_ANY, wxString(_T("Throw exception dialog"))) |
556b8c1a VZ |
315 | { |
316 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
317 | ||
318 | sizerTop->Add(new wxButton(this, Except_ThrowInt, _T("Throw &int")), | |
319 | 0, wxCENTRE | wxALL, 5); | |
320 | sizerTop->Add(new wxButton(this, Except_ThrowObject, _T("Throw &object")), | |
321 | 0, wxCENTRE | wxALL, 5); | |
f78e4942 VZ |
322 | sizerTop->Add(new wxButton(this, Except_Crash, _T("&Crash")), |
323 | 0, wxCENTRE | wxALL, 5); | |
556b8c1a VZ |
324 | sizerTop->Add(new wxButton(this, wxID_CANCEL, _T("&Cancel")), |
325 | 0, wxCENTRE | wxALL, 5); | |
326 | ||
327 | SetSizer(sizerTop); | |
328 | sizerTop->Fit(this); | |
329 | } | |
330 | ||
331 | void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event)) | |
332 | { | |
333 | throw 17; | |
334 | } | |
335 | ||
336 | void MyDialog::OnThrowObject(wxCommandEvent& WXUNUSED(event)) | |
337 | { | |
338 | throw MyException(_T("Exception thrown from the dialog")); | |
339 | } | |
340 | ||
f78e4942 VZ |
341 | void MyDialog::OnCrash(wxCommandEvent& WXUNUSED(event)) |
342 | { | |
343 | char *p = 0; | |
344 | strcpy(p, "Let's crash"); | |
345 | } | |
346 |