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