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