X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/575e4cd67024ca1c49b4218ce748c68aa938ce27..0cb9ba2518e309fadf30ae39daba683ccd80e417:/src/common/docview.cpp?ds=sidebyside diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 9c5118b3c1..11d6bca96d 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -58,7 +58,9 @@ #include "wx/filename.h" #include "wx/stdpaths.h" #include "wx/vector.h" -#include "wx/ptr_scpd.h" +#include "wx/scopedarray.h" +#include "wx/scopedptr.h" +#include "wx/except.h" #if wxUSE_STD_IOSTREAM #include "wx/ioswrap.h" @@ -103,13 +105,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject) namespace { -wxWindow *wxFindSuitableParent() -{ - wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus()); - - return win ? win : wxTheApp->GetTopWindow(); -} - wxString FindExtension(const wxString& path) { wxString ext; @@ -245,11 +240,13 @@ wxDocManager *wxDocument::GetDocumentManager() const bool wxDocument::OnNewDocument() { - if ( !OnSaveModified() ) - return false; + // notice that there is no need to neither reset nor even check the + // modified flag here as the document itself is a new object (this is only + // called from CreateDocument()) and so it shouldn't be saved anyhow even + // if it is modified -- this could happen if the user code creates + // documents pre-filled with some user-entered (and which hence must not be + // lost) information - DeleteContents(); - Modify(false); SetDocumentSaved(false); const wxString name = GetDocumentManager()->MakeNewDocumentName(); @@ -378,15 +375,20 @@ bool wxDocument::OnSaveDocument(const wxString& file) bool wxDocument::OnOpenDocument(const wxString& file) { - if ( !OnSaveModified() ) - return false; + // notice that there is no need to check the modified flag here for the + // reasons explained in OnNewDocument() if ( !DoOpenDocument(file) ) return false; SetFilename(file, true); - Modify(false); - m_savedYet = true; + + // stretching the logic a little this does make sense because the document + // had been saved into the file we just loaded it from, it just could have + // happened during a previous program execution, it's just that the name of + // this method is a bit unfortunate, it should probably have been called + // HasAssociatedFileName() + SetDocumentSaved(true); UpdateAllViews(); @@ -480,8 +482,7 @@ bool wxDocument::OnSaveModified() GetUserReadableName() ), wxTheApp->GetAppDisplayName(), - wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE, - wxFindSuitableParent() + wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE ) ) { case wxNO: @@ -641,15 +642,46 @@ wxView::wxView() m_viewDocument = NULL; m_viewFrame = NULL; + + m_docChildFrame = NULL; } wxView::~wxView() { GetDocumentManager()->ActivateView(this, false); - m_viewDocument->RemoveView(this); + + // reset our frame view first, before removing it from the document as + // SetView(NULL) is a simple call while RemoveView() may result in user + // code being executed and this user code can, for example, show a message + // box which would result in an activation event for m_docChildFrame and so + // could reactivate the view being destroyed -- unless we reset it first + if ( m_docChildFrame && m_docChildFrame->GetView() == this ) + { + // prevent it from doing anything with us + m_docChildFrame->SetView(NULL); + + // it doesn't make sense to leave the frame alive if its associated + // view doesn't exist any more so unconditionally close it as well + // + // notice that we only get here if m_docChildFrame is non-NULL in the + // first place and it will be always NULL if we're deleted because our + // frame was closed, so this only catches the case of directly deleting + // the view, as it happens if its creation fails in wxDocTemplate:: + // CreateView() for example + m_docChildFrame->GetWindow()->Destroy(); + } + + if ( m_viewDocument ) + m_viewDocument->RemoveView(this); } -bool wxView::TryValidator(wxEvent& event) +void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame) +{ + SetFrame(docChildFrame ? docChildFrame->GetWindow() : NULL); + m_docChildFrame = docChildFrame; +} + +bool wxView::TryBefore(wxEvent& event) { wxDocument * const doc = GetDocument(); return doc && doc->ProcessEventHere(event); @@ -754,11 +786,15 @@ wxDocTemplate::~wxDocTemplate() // Tries to dynamically construct an object of the right class. wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) { + // InitDocument() is supposed to delete the document object if its + // initialization fails so don't use wxScopedPtr<> here: this is fragile + // but unavoidable because the default implementation uses CreateView() + // which may -- or not -- create a wxView and if it does create it and its + // initialization fails then the view destructor will delete the document + // (via RemoveView()) and as we can't distinguish between the two cases we + // just have to assume that it always deletes it in case of failure wxDocument * const doc = DoCreateDocument(); - // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate()) - // fails, is this intentional? - return doc && InitDocument(doc, path, flags) ? doc : NULL; } @@ -1164,7 +1200,7 @@ wxView *wxDocManager::GetActiveView() const return view; } -bool wxDocManager::TryValidator(wxEvent& event) +bool wxDocManager::TryBefore(wxEvent& event) { wxView * const view = GetActiveView(); return view && view->ProcessEventHere(event); @@ -1287,15 +1323,18 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) docNew->SetDocumentName(temp->GetDocumentName()); docNew->SetDocumentTemplate(temp); - // call the appropriate function depending on whether we're creating a new - // file or opening an existing one - if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument() - : docNew->OnOpenDocument(path)) ) + wxTRY { - // Document is implicitly deleted by DeleteAllViews - docNew->DeleteAllViews(); - return NULL; + // call the appropriate function depending on whether we're creating a + // new file or opening an existing one + if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument() + : docNew->OnOpenDocument(path)) ) + { + docNew->DeleteAllViews(); + return NULL; + } } + wxCATCH_ALL( docNew->DeleteAllViews(); throw; ) // add the successfully opened file to MRU, but only if we're going to be // able to reopen it successfully later which requires the template for @@ -1519,15 +1558,11 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, int FilterIndex = -1; - wxWindow* parent = wxFindSuitableParent(); - wxString pathTmp = wxFileSelectorEx(_("Open File"), GetLastDirectory(), wxEmptyString, &FilterIndex, - descrBuf, - 0, - parent); + descrBuf); wxDocTemplate *theTemplate = NULL; if (!pathTmp.empty()) @@ -1542,8 +1577,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, wxMessageBox(_("Sorry, could not open this file."), msgTitle, - wxOK | wxICON_EXCLAMATION | wxCENTRE, - parent); + wxOK | wxICON_EXCLAMATION | wxCENTRE); path = wxEmptyString; return NULL; @@ -1566,8 +1600,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, // allowed templates in runtime. wxMessageBox(_("Sorry, the format for this file is unknown."), _("Open File"), - wxOK | wxICON_EXCLAMATION | wxCENTRE, - parent); + wxOK | wxICON_EXCLAMATION | wxCENTRE); } } else @@ -1649,8 +1682,7 @@ wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, _("Select a document template"), _("Templates"), strings, - (void **)data.get(), - wxFindSuitableParent() + (void **)data.get() ); } @@ -1724,8 +1756,7 @@ wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates, _("Select a document view"), _("Views"), strings, - (void **)data.get(), - wxFindSuitableParent() + (void **)data.get() ); } @@ -1775,68 +1806,34 @@ void wxDocManager::ActivateView(wxView *view, bool activate) } // ---------------------------------------------------------------------------- -// Default document child frame +// wxDocChildFrameAnyBase // ---------------------------------------------------------------------------- -BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame) - EVT_ACTIVATE(wxDocChildFrame::OnActivate) - EVT_CLOSE(wxDocChildFrame::OnCloseWindow) -END_EVENT_TABLE() - -wxDocChildFrame::wxDocChildFrame(wxDocument *doc, - wxView *view, - wxFrame *frame, - wxWindowID id, - const wxString& title, - const wxPoint& pos, - const wxSize& size, - long style, - const wxString& name) - : wxFrame(frame, id, title, pos, size, style, name) +bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) { - m_childDocument = doc; - m_childView = view; - if (view) - view->SetFrame(this); -} - -bool wxDocChildFrame::TryValidator(wxEvent& event) -{ - if ( !m_childView ) - return false; - - // FIXME: why is this needed here? - m_childView->Activate(true); - - return m_childView->ProcessEventHere(event); -} - -void wxDocChildFrame::OnActivate(wxActivateEvent& event) -{ - wxFrame::OnActivate(event); - - if (m_childView) - m_childView->Activate(event.GetActive()); -} + if ( m_childView ) + { + if ( event.CanVeto() && !m_childView->Close(false) ) + { + event.Veto(); + return false; + } -void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event) -{ - if ( !m_childView ) - return; + m_childView->Activate(false); - // passing false to Close() means to not delete associated window - if ( event.CanVeto() && !m_childView->Close(false) ) - { - event.Veto(); - return; + // it is important to reset m_childView frame pointer to NULL before + // deleting it because while normally it is the frame which deletes the + // view when it's closed, the view also closes the frame if it is + // deleted directly not by us as indicated by its doc child frame + // pointer still being set + m_childView->SetDocChildFrame(NULL); + delete m_childView; + m_childView = NULL; } - m_childView->Activate(false); - delete m_childView; - m_childView = NULL; m_childDocument = NULL; - Destroy(); + return true; } // ---------------------------------------------------------------------------- @@ -1919,9 +1916,12 @@ void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) } // Extend event processing to search the view's event table -bool wxDocParentFrame::TryValidator(wxEvent& event) +bool wxDocParentFrame::TryBefore(wxEvent& event) { - return m_docManager && m_docManager->ProcessEventHere(event); + if ( m_docManager && m_docManager->ProcessEventHere(event) ) + return true; + + return wxFrame::TryBefore(event); } // Define the behaviour for the frame closing