]>
git.saurik.com Git - wxWidgets.git/blob - samples/except/except.cpp
f5423780ab0634d45a774b402e18e7e3b4ee0afd
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"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
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"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 // ----------------------------
70 virtual bool OnInit();
72 // 2nd-level exception handling: we get all the exceptions occuring in any
74 virtual void HandleEvent(wxEvtHandler
*handler
,
76 wxEvent
& event
) const;
78 // 3rd, and final, level exception handling: whenever an unhandled
79 // exception is caught, this function is called
80 virtual void OnUnhandledException();
83 // Define a new frame type: this is going to be our main frame
84 class MyFrame
: public wxFrame
88 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
89 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
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
);
102 // any class wishing to process wxWidgets events must use this macro
103 DECLARE_EVENT_TABLE()
106 // A simple dialog which has only some buttons to throw exceptions
107 class MyDialog
: public wxDialog
110 MyDialog(wxFrame
*parent
);
113 void OnThrowInt(wxCommandEvent
& event
);
114 void OnThrowObject(wxCommandEvent
& event
);
115 void OnCrash(wxCommandEvent
& event
);
118 DECLARE_EVENT_TABLE()
121 // A trivial exception class
125 MyException(const wxString
& msg
) : m_msg(msg
) { }
127 const wxChar
*what() const { return m_msg
.c_str(); }
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 // IDs for the controls and the menu commands
141 Except_ThrowInt
= 100,
146 Except_ThrowString
= 200,
149 Except_Quit
= wxID_EXIT
,
150 Except_About
= wxID_ABOUT
153 // ----------------------------------------------------------------------------
154 // event tables and other macros for wxWidgets
155 // ----------------------------------------------------------------------------
157 // the event tables connect the wxWidgets events with the functions (event
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
)
167 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
168 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
169 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
170 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
173 // Create a new application object: this macro will allow wxWidgets to create
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
180 // ============================================================================
181 // MyApp implementation
182 // ============================================================================
184 // 'Main program' equivalent: the program execution "starts" here
187 // create the main application window
188 MyFrame
*frame
= new MyFrame(_T("Except wxWidgets App"),
189 wxPoint(50, 50), wxSize(450, 340));
191 // and show it (the frames, unlike simple controls, are not shown when
192 // created initially)
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.
202 MyApp::HandleEvent(wxEvtHandler
*handler
,
203 wxEventFunction func
,
204 wxEvent
& event
) const
208 wxApp::HandleEvent(handler
, func
, event
);
212 wxLogError(_T("Caught an int %d in MyApp."), i
);
216 void MyApp::OnUnhandledException()
218 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
219 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
222 // ============================================================================
223 // MyFrame implementation
224 // ============================================================================
227 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
228 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
230 // set the frame icon
231 SetIcon(wxICON(sample
));
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"));
241 wxMenu
*helpMenu
= new wxMenu
;
242 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
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"));
249 // ... and attach this menu bar to the frame
251 #endif // wxUSE_MENUS
253 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
254 // create a status bar just for fun (by default with 1 pane only)
256 SetStatusText(_T("Welcome to wxWidgets!"));
257 #endif // wxUSE_STATUSBAR
260 bool MyFrame::ProcessEvent(wxEvent
& event
)
264 return wxFrame::ProcessEvent(event
);
266 catch ( const wxChar
*msg
)
268 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
274 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
276 // true is to force the frame to close
280 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
295 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
297 throw _T("some string");
300 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
303 msg
.Printf( _T("This is the About dialog of the except sample.\n")
304 _T("Welcome to %s"), wxVERSION_STRING
);
306 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
309 // ============================================================================
310 // MyDialog implementation
311 // ============================================================================
313 MyDialog::MyDialog(wxFrame
*parent
)
314 : wxDialog(parent
, wxID_ANY
, wxString(_T("Throw exception dialog")))
316 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
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);
322 sizerTop
->Add(new wxButton(this, Except_Crash
, _T("&Crash")),
323 0, wxCENTRE
| wxALL
, 5);
324 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, _T("&Cancel")),
325 0, wxCENTRE
| wxALL
, 5);
331 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
336 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
338 throw MyException(_T("Exception thrown from the dialog"));
341 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))
344 strcpy(p
, "Let's crash");