]>
git.saurik.com Git - wxWidgets.git/blob - samples/except/except.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Except wxWindows 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" wxWindows headers)
34 #include "wx/dialog.h"
37 #include "wx/button.h"
40 #include "wx/utils.h" // for wxMessageBox
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the application icon (under Windows and OS/2 it is in resources)
48 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
49 #include "../sample.xpm"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp
: public wxApp
60 // override base class virtuals
61 // ----------------------------
64 virtual bool OnInit();
66 // 2nd-level exception handling: we get all the exceptions occuring in any
68 virtual void HandleEvent(wxEvtHandler
*handler
,
70 wxEvent
& event
) const;
72 // 3rd, and final, level exception handling: whenever an unhandled
73 // exception is caught, this function is called
74 virtual void OnUnhandledException();
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame
: public wxFrame
82 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
83 long style
= wxDEFAULT_FRAME_STYLE
);
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
88 void OnDialog(wxCommandEvent
& event
);
89 void OnThrowString(wxCommandEvent
& event
);
91 // 1st-level exception handling: we overload ProcessEvent() to be able to
92 // catch exceptions which occur in MyFrame methods here
93 virtual bool ProcessEvent(wxEvent
& event
);
96 // any class wishing to process wxWindows events must use this macro
100 // A simple dialog which has only some buttons to throw exceptions
101 class MyDialog
: public wxDialog
104 MyDialog(wxFrame
*parent
);
107 void OnThrowInt(wxCommandEvent
& event
);
108 void OnThrowObject(wxCommandEvent
& event
);
111 DECLARE_EVENT_TABLE()
114 // A trivial exception class
118 MyException(const wxString
& msg
) : m_msg(msg
) { }
120 const wxChar
*what() const { return m_msg
.c_str(); }
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 // IDs for the controls and the menu commands
134 Except_ThrowInt
= 100,
138 Except_ThrowString
= 200,
141 Except_Quit
= wxID_EXIT
,
142 Except_About
= wxID_ABOUT
145 // ----------------------------------------------------------------------------
146 // event tables and other macros for wxWindows
147 // ----------------------------------------------------------------------------
149 // the event tables connect the wxWindows events with the functions (event
150 // handlers) which process them. It can be also done at run-time, but for the
151 // simple menu events like this the static method is much simpler.
152 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
153 EVT_MENU(Except_Quit
, MyFrame::OnQuit
)
154 EVT_MENU(Except_About
, MyFrame::OnAbout
)
155 EVT_MENU(Except_Dialog
, MyFrame::OnDialog
)
156 EVT_MENU(Except_ThrowString
, MyFrame::OnThrowString
)
159 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
160 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
161 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
164 // Create a new application object: this macro will allow wxWindows to create
165 // the application object during program execution (it's better than using a
166 // static object for many reasons) and also implements the accessor function
167 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
171 // ============================================================================
172 // MyApp implementation
173 // ============================================================================
175 // 'Main program' equivalent: the program execution "starts" here
178 // create the main application window
179 MyFrame
*frame
= new MyFrame(_T("Except wxWindows App"),
180 wxPoint(50, 50), wxSize(450, 340));
182 // and show it (the frames, unlike simple controls, are not shown when
183 // created initially)
186 // success: wxApp::OnRun() will be called which will enter the main message
187 // loop and the application will run. If we returned false here, the
188 // application would exit immediately.
193 MyApp::HandleEvent(wxEvtHandler
*handler
,
194 wxEventFunction func
,
195 wxEvent
& event
) const
199 wxApp::HandleEvent(handler
, func
, event
);
203 wxLogError(_T("Caught an int %d in MyApp."), i
);
207 void MyApp::OnUnhandledException()
209 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
210 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
213 // ============================================================================
214 // MyFrame implementation
215 // ============================================================================
218 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
219 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
221 // set the frame icon
222 SetIcon(wxICON(mondrian
));
226 wxMenu
*menuFile
= new wxMenu
;
227 menuFile
->Append(Except_Dialog
, _T("Show &dialog\tCtrl-D"));
228 menuFile
->Append(Except_ThrowString
, _T("Throw a &string\tCtrl-S"));
229 menuFile
->AppendSeparator();
230 menuFile
->Append(Except_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
232 wxMenu
*helpMenu
= new wxMenu
;
233 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
235 // now append the freshly created menu to the menu bar...
236 wxMenuBar
*menuBar
= new wxMenuBar();
237 menuBar
->Append(menuFile
, _T("&File"));
238 menuBar
->Append(helpMenu
, _T("&Help"));
240 // ... and attach this menu bar to the frame
242 #endif // wxUSE_MENUS
244 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
245 // create a status bar just for fun (by default with 1 pane only)
247 SetStatusText(_T("Welcome to wxWindows!"));
248 #endif // wxUSE_STATUSBAR
251 bool MyFrame::ProcessEvent(wxEvent
& event
)
255 return wxFrame::ProcessEvent(event
);
257 catch ( const wxChar
*msg
)
259 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
265 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
267 // true is to force the frame to close
271 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
286 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
288 throw _T("some string");
291 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
294 msg
.Printf( _T("This is the About dialog of the except sample.\n")
295 _T("Welcome to %s"), wxVERSION_STRING
);
297 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
300 // ============================================================================
301 // MyDialog implementation
302 // ============================================================================
304 MyDialog::MyDialog(wxFrame
*parent
)
305 : wxDialog(parent
, -1, wxString(_T("Throw exception dialog")))
307 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
309 sizerTop
->Add(new wxButton(this, Except_ThrowInt
, _T("Throw &int")),
310 0, wxCENTRE
| wxALL
, 5);
311 sizerTop
->Add(new wxButton(this, Except_ThrowObject
, _T("Throw &object")),
312 0, wxCENTRE
| wxALL
, 5);
313 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, _T("&Cancel")),
314 0, wxCENTRE
| wxALL
, 5);
320 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
325 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
327 throw MyException(_T("Exception thrown from the dialog"));