X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b6b375a9178af71eacdb06965560bb8a56617176..703ee9f5eccb0a002c02b60d87f239a2b6529e1e:/samples/except/except.cpp diff --git a/samples/except/except.cpp b/samples/except/except.cpp index 6dbdee3eb8..5e487845d5 100644 --- a/samples/except/except.cpp +++ b/samples/except/except.cpp @@ -79,7 +79,7 @@ public: // program startup virtual bool OnInit(); - // 2nd-level exception handling: we get all the exceptions occuring in any + // 2nd-level exception handling: we get all the exceptions occurring in any // event handler here virtual bool OnExceptionInMainLoop(); @@ -90,6 +90,15 @@ public: // and now for something different: this function is called in case of a // crash (e.g. dereferencing null pointer, division by 0, ...) virtual void OnFatalException(); + +#ifdef __WXDEBUG__ + // in debug mode, you can override this function to do something different + // (e.g. log the assert to file) whenever an assertion fails + virtual void OnAssert(const wxChar *file, + int line, + const wxChar *cond, + const wxChar *msg); +#endif // __WXDEBUG__ }; // Define a new frame type: this is going to be our main frame @@ -118,6 +127,11 @@ public: // catch exceptions which occur in MyFrame methods here virtual bool ProcessEvent(wxEvent& event); +#ifdef __WXDEBUG__ + // show how an assert failure message box looks like + void OnShowAssert(wxCommandEvent& event); +#endif // __WXDEBUG__ + private: // any class wishing to process wxWidgets events must use this macro DECLARE_EVENT_TABLE() @@ -170,7 +184,10 @@ enum Except_Crash, #if wxUSE_ON_FATAL_EXCEPTION Except_HandleCrash, -#endif +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + Except_ShowAssert, +#endif // __WXDEBUG__ Except_Dialog, Except_Quit = wxID_EXIT, @@ -195,7 +212,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Except_Crash, MyFrame::OnCrash) #if wxUSE_ON_FATAL_EXCEPTION EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash) -#endif +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert) +#endif // __WXDEBUG__ END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyDialog, wxDialog) @@ -218,6 +238,9 @@ IMPLEMENT_APP(MyApp) // 'Main program' equivalent: the program execution "starts" here bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + // create the main application window MyFrame *frame = new MyFrame(); @@ -277,6 +300,19 @@ void MyApp::OnFatalException() _T("wxExcept Sample"), wxOK | wxICON_ERROR); } +#ifdef __WXDEBUG__ + +void MyApp::OnAssert(const wxChar *file, + int line, + const wxChar *cond, + const wxChar *msg) +{ + // we don't have anything special to do here + wxApp::OnAssert(file, line, cond, msg); +} + +#endif // __WXDEBUG__ + // ============================================================================ // MyFrame implementation // ============================================================================ @@ -304,7 +340,11 @@ MyFrame::MyFrame() #if wxUSE_ON_FATAL_EXCEPTION menuFile->AppendCheckItem(Except_HandleCrash, _T("&Handle crashes\tCtrl-H")); menuFile->AppendSeparator(); -#endif +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + menuFile->Append(Except_ShowAssert, _T("Provoke &assert failure\tCtrl-A")); + menuFile->AppendSeparator(); +#endif // __WXDEBUG__ menuFile->Append(Except_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); wxMenu *helpMenu = new wxMenu; @@ -395,7 +435,18 @@ void MyFrame::OnHandleCrash(wxCommandEvent& event) wxHandleFatalExceptions(event.IsChecked()); } -#endif +#endif // wxUSE_ON_FATAL_EXCEPTION + +#ifdef __WXDEBUG__ + +void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) +{ + // provoke an assert from wxArrayString + wxArrayString arr; + arr[0]; +} + +#endif // __WXDEBUG__ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { @@ -424,8 +475,7 @@ MyDialog::MyDialog(wxFrame *parent) sizerTop->Add(new wxButton(this, wxID_CANCEL, _T("&Cancel")), 0, wxCENTRE | wxALL, 5); - SetSizer(sizerTop); - sizerTop->Fit(this); + SetSizerAndFit(sizerTop); } void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event))