]>
git.saurik.com Git - wxWidgets.git/blob - samples/except/except.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Except wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
34 #include "wx/dialog.h"
37 #include "wx/button.h"
41 #include "wx/msgdlg.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
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"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // Define a new application type, each program should derive a class from wxApp
58 class MyApp
: public wxApp
61 // override base class virtuals
62 // ----------------------------
65 virtual bool OnInit();
67 // 2nd-level exception handling: we get all the exceptions occuring in any
69 virtual void HandleEvent(wxEvtHandler
*handler
,
71 wxEvent
& event
) const;
73 // 3rd, and final, level exception handling: whenever an unhandled
74 // exception is caught, this function is called
75 virtual void OnUnhandledException();
78 // Define a new frame type: this is going to be our main frame
79 class MyFrame
: public wxFrame
83 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
84 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
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
);
97 // any class wishing to process wxWidgets events must use this macro
101 // A simple dialog which has only some buttons to throw exceptions
102 class MyDialog
: public wxDialog
105 MyDialog(wxFrame
*parent
);
108 void OnThrowInt(wxCommandEvent
& event
);
109 void OnThrowObject(wxCommandEvent
& event
);
112 DECLARE_EVENT_TABLE()
115 // A trivial exception class
119 MyException(const wxString
& msg
) : m_msg(msg
) { }
121 const wxChar
*what() const { return m_msg
.c_str(); }
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 // IDs for the controls and the menu commands
135 Except_ThrowInt
= 100,
139 Except_ThrowString
= 200,
142 Except_Quit
= wxID_EXIT
,
143 Except_About
= wxID_ABOUT
146 // ----------------------------------------------------------------------------
147 // event tables and other macros for wxWidgets
148 // ----------------------------------------------------------------------------
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
)
160 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
161 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
162 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
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
172 // ============================================================================
173 // MyApp implementation
174 // ============================================================================
176 // 'Main program' equivalent: the program execution "starts" here
179 // create the main application window
180 MyFrame
*frame
= new MyFrame(_T("Except wxWidgets App"),
181 wxPoint(50, 50), wxSize(450, 340));
183 // and show it (the frames, unlike simple controls, are not shown when
184 // created initially)
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.
194 MyApp::HandleEvent(wxEvtHandler
*handler
,
195 wxEventFunction func
,
196 wxEvent
& event
) const
200 wxApp::HandleEvent(handler
, func
, event
);
204 wxLogError(_T("Caught an int %d in MyApp."), i
);
208 void MyApp::OnUnhandledException()
210 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
211 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
214 // ============================================================================
215 // MyFrame implementation
216 // ============================================================================
219 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
220 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
222 // set the frame icon
223 SetIcon(wxICON(sample
));
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"));
233 wxMenu
*helpMenu
= new wxMenu
;
234 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
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"));
241 // ... and attach this menu bar to the frame
243 #endif // wxUSE_MENUS
245 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
246 // create a status bar just for fun (by default with 1 pane only)
248 SetStatusText(_T("Welcome to wxWidgets!"));
249 #endif // wxUSE_STATUSBAR
252 bool MyFrame::ProcessEvent(wxEvent
& event
)
256 return wxFrame::ProcessEvent(event
);
258 catch ( const wxChar
*msg
)
260 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
266 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
268 // true is to force the frame to close
272 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
287 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
289 throw _T("some string");
292 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
295 msg
.Printf( _T("This is the About dialog of the except sample.\n")
296 _T("Welcome to %s"), wxVERSION_STRING
);
298 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
301 // ============================================================================
302 // MyDialog implementation
303 // ============================================================================
305 MyDialog::MyDialog(wxFrame
*parent
)
306 : wxDialog(parent
, wxID_ANY
, wxString(_T("Throw exception dialog")))
308 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
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);
321 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
326 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
328 throw MyException(_T("Exception thrown from the dialog"));