]>
git.saurik.com Git - wxWidgets.git/blob - samples/except/except.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/except/except.cpp
3 // Purpose: shows how C++ exceptions can be used in wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
28 #error "This sample only works with wxUSE_EXCEPTIONS == 1"
29 #endif // !wxUSE_EXCEPTIONS
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWidgets headers)
38 #include "wx/dialog.h"
41 #include "wx/button.h"
45 #include "wx/msgdlg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
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"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp
: public wxApp
65 // override base class virtuals
66 // ----------------------------
69 virtual bool OnInit();
71 // 2nd-level exception handling: we get all the exceptions occuring in any
73 virtual void HandleEvent(wxEvtHandler
*handler
,
75 wxEvent
& event
) const;
77 // 3rd, and final, level exception handling: whenever an unhandled
78 // exception is caught, this function is called
79 virtual void OnUnhandledException();
82 // Define a new frame type: this is going to be our main frame
83 class MyFrame
: public wxFrame
87 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
88 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
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
);
101 // any class wishing to process wxWidgets events must use this macro
102 DECLARE_EVENT_TABLE()
105 // A simple dialog which has only some buttons to throw exceptions
106 class MyDialog
: public wxDialog
109 MyDialog(wxFrame
*parent
);
112 void OnThrowInt(wxCommandEvent
& event
);
113 void OnThrowObject(wxCommandEvent
& event
);
114 void OnCrash(wxCommandEvent
& event
);
117 DECLARE_EVENT_TABLE()
120 // A trivial exception class
124 MyException(const wxString
& msg
) : m_msg(msg
) { }
126 const wxChar
*what() const { return m_msg
.c_str(); }
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 // IDs for the controls and the menu commands
140 Except_ThrowInt
= 100,
145 Except_ThrowString
= 200,
148 Except_Quit
= wxID_EXIT
,
149 Except_About
= wxID_ABOUT
152 // ----------------------------------------------------------------------------
153 // event tables and other macros for wxWidgets
154 // ----------------------------------------------------------------------------
156 // the event tables connect the wxWidgets events with the functions (event
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
)
166 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
167 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
168 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
169 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
172 // Create a new application object: this macro will allow wxWidgets to create
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
179 // ============================================================================
180 // MyApp implementation
181 // ============================================================================
183 // 'Main program' equivalent: the program execution "starts" here
186 // create the main application window
187 MyFrame
*frame
= new MyFrame(_T("Except wxWidgets App"),
188 wxPoint(50, 50), wxSize(450, 340));
190 // and show it (the frames, unlike simple controls, are not shown when
191 // created initially)
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.
201 MyApp::HandleEvent(wxEvtHandler
*handler
,
202 wxEventFunction func
,
203 wxEvent
& event
) const
207 wxApp::HandleEvent(handler
, func
, event
);
211 wxLogError(_T("Caught an int %d in MyApp."), i
);
215 void MyApp::OnUnhandledException()
217 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
218 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
221 // ============================================================================
222 // MyFrame implementation
223 // ============================================================================
226 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
227 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
229 // set the frame icon
230 SetIcon(wxICON(sample
));
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"));
240 wxMenu
*helpMenu
= new wxMenu
;
241 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
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"));
248 // ... and attach this menu bar to the frame
250 #endif // wxUSE_MENUS
252 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
253 // create a status bar just for fun (by default with 1 pane only)
255 SetStatusText(_T("Welcome to wxWidgets!"));
256 #endif // wxUSE_STATUSBAR
259 bool MyFrame::ProcessEvent(wxEvent
& event
)
263 return wxFrame::ProcessEvent(event
);
265 catch ( const wxChar
*msg
)
267 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
273 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
275 // true is to force the frame to close
279 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
294 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
296 throw _T("some string");
299 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
302 msg
.Printf( _T("This is the About dialog of the except sample.\n")
303 _T("Welcome to %s"), wxVERSION_STRING
);
305 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
308 // ============================================================================
309 // MyDialog implementation
310 // ============================================================================
312 MyDialog::MyDialog(wxFrame
*parent
)
313 : wxDialog(parent
, wxID_ANY
, wxString(_T("Throw exception dialog")))
315 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
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);
321 sizerTop
->Add(new wxButton(this, Except_Crash
, _T("&Crash")),
322 0, wxCENTRE
| wxALL
, 5);
323 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, _T("&Cancel")),
324 0, wxCENTRE
| wxALL
, 5);
330 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
335 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
337 throw MyException(_T("Exception thrown from the dialog"));
340 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))
343 strcpy(p
, "Let's crash");