X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b27367a5bd7a2e1d88f03b0f1931672ae92219ec..8472511246c9160d4ff40ab86f635fc67c10b54a:/src/common/docview.cpp diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 9188b25ed8..fc1c256337 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -60,6 +60,7 @@ #include "wx/vector.h" #include "wx/scopedarray.h" #include "wx/scopedptr.h" +#include "wx/scopeguard.h" #include "wx/except.h" #if wxUSE_STD_IOSTREAM @@ -75,8 +76,6 @@ #include "wx/wfstream.h" #endif -typedef wxVector wxDocTemplates; - // ---------------------------------------------------------------------------- // wxWidgets macros // ---------------------------------------------------------------------------- @@ -122,8 +121,12 @@ wxString FindExtension(const wxString& path) wxDocument::wxDocument(wxDocument *parent) { m_documentModified = false; - m_documentParent = parent; m_documentTemplate = NULL; + + m_documentParent = parent; + if ( parent ) + parent->m_childDocuments.push_back(this); + m_commandProcessor = NULL; m_savedYet = false; } @@ -140,6 +143,9 @@ wxDocument::~wxDocument() if (GetDocumentManager()) GetDocumentManager()->RemoveDocument(this); + if ( m_documentParent ) + m_documentParent->m_childDocuments.remove(this); + // Not safe to do here, since it'll invoke virtual view functions // expecting to see valid derived objects: and by the time we get here, // we've called destructors higher up. @@ -151,6 +157,40 @@ bool wxDocument::Close() if ( !OnSaveModified() ) return false; + // When the parent document closes, its children must be closed as well as + // they can't exist without the parent. + + // As usual, first check if all children can be closed. + DocsList::const_iterator it = m_childDocuments.begin(); + for ( DocsList::const_iterator end = m_childDocuments.end(); it != end; ++it ) + { + if ( !(*it)->OnSaveModified() ) + { + // Leave the parent document opened if a child can't close. + return false; + } + } + + // Now that they all did, do close them: as m_childDocuments is modified as + // we iterate over it, don't use the usual for-style iteration here. + while ( !m_childDocuments.empty() ) + { + wxDocument * const childDoc = m_childDocuments.front(); + + // This will call OnSaveModified() once again but it shouldn't do + // anything as the document was just saved or marked as not needing to + // be saved by the call to OnSaveModified() that returned true above. + if ( !childDoc->Close() ) + { + wxFAIL_MSG( "Closing the child document unexpectedly failed " + "after its OnSaveModified() returned true" ); + } + + // Delete the child document by deleting all its views. + childDoc->DeleteAllViews(); + } + + return OnCloseDocument(); } @@ -231,6 +271,12 @@ void wxDocument::Modify(bool mod) wxDocManager *wxDocument::GetDocumentManager() const { + // For child documents we use the same document manager as the parent, even + // though we don't have our own template (as children are not opened/saved + // directly). + if ( m_documentParent ) + return m_documentParent->GetDocumentManager(); + return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL; } @@ -350,6 +396,9 @@ bool wxDocument::OnSaveDocument(const wxString& file) if ( !DoSaveDocument(file) ) return false; + if ( m_commandProcessor ) + m_commandProcessor->MarkAsSaved(); + Modify(false); SetFilename(file); SetDocumentSaved(true); @@ -425,7 +474,7 @@ bool wxDocument::Revert() #if WXWIN_COMPATIBILITY_2_8 bool wxDocument::GetPrintableName(wxString& buf) const { - // this function can not only be overridden by the user code but also + // this function cannot only be overridden by the user code but also // called by it so we need to ensure that we return the same thing as // GetUserReadableName() but we can't call it because this would result in // an infinite recursion, hence we use the helper DoGetUserReadableName() @@ -809,17 +858,19 @@ wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) { + wxScopeGuard g = wxMakeObjGuard(*doc, &wxDocument::DeleteAllViews); + doc->SetFilename(path); doc->SetDocumentTemplate(this); GetDocumentManager()->AddDocument(doc); doc->SetCommandProcessor(doc->OnCreateCommandProcessor()); - if (doc->OnCreate(path, flags)) - return true; + if ( !doc->OnCreate(path, flags) ) + return false; - if (GetDocumentManager()->GetDocuments().Member(doc)) - doc->DeleteAllViews(); - return false; + g.Dismiss(); // no need to call DeleteAllViews() anymore + + return true; } wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) @@ -884,13 +935,18 @@ BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler) EVT_MENU(wxID_UNDO, wxDocManager::OnUndo) EVT_MENU(wxID_REDO, wxDocManager::OnRedo) + // We don't know in advance how many items can there be in the MRU files + // list so set up OnMRUFile() as a handler for all menu events and do the + // check for the id of the menu item clicked inside it. + EVT_MENU(wxID_ANY, wxDocManager::OnMRUFile) + EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen) EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc) EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc) EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert) EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew) EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave) - EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateDisableIfNoDoc) + EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs) EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo) EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo) @@ -1031,14 +1087,8 @@ wxFileHistory *wxDocManager::OnCreateFileHistory() void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event)) { wxDocument *doc = GetCurrentDocument(); - if (!doc) - return; - if (doc->Close()) - { - doc->DeleteAllViews(); - if (m_docs.Member(doc)) - delete doc; - } + if (doc) + CloseDocument(doc); } void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event)) @@ -1083,11 +1133,59 @@ void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event)) doc->SaveAs(); } +void wxDocManager::OnMRUFile(wxCommandEvent& event) +{ + // Check if the id is in the range assigned to MRU list entries. + const int id = event.GetId(); + if ( id >= wxID_FILE1 && + id < wxID_FILE1 + static_cast(m_fileHistory->GetCount()) ) + { + DoOpenMRUFile(id - wxID_FILE1); + } + else + { + event.Skip(); + } +} + +void wxDocManager::DoOpenMRUFile(unsigned n) +{ + wxString filename(GetHistoryFile(n)); + if ( filename.empty() ) + return; + + wxString errMsg; // must contain exactly one "%s" if non-empty + if ( wxFile::Exists(filename) ) + { + // Try to open it but don't give an error if it failed: this could be + // normal, e.g. because the user cancelled opening it, and we don't + // have any useful information to put in the error message anyhow, so + // we assume that in case of an error the appropriate message had been + // already logged. + (void)CreateDocument(filename, wxDOC_SILENT); + } + else // file doesn't exist + { + OnMRUFileNotExist(n, filename); + } +} + +void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename) +{ + // remove the file which we can't open from the MRU list + RemoveFileFromHistory(n); + + // and tell the user about it + wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n" + "It has been removed from the most recently used files list."), + filename); +} + #if wxUSE_PRINTING_ARCHITECTURE void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event)) { - wxView *view = GetActiveView(); + wxView *view = GetAnyUsableView(); if (!view) return; @@ -1121,7 +1219,7 @@ wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview, void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) { wxBusyCursor busy; - wxView *view = GetActiveView(); + wxView *view = GetAnyUsableView(); if (!view) return; @@ -1135,7 +1233,7 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) preview = new wxPrintPreview(printout, view->OnCreatePrintout(), &printDialogData); - if ( !preview->Ok() ) + if ( !preview->IsOk() ) { delete preview; wxLogError(_("Print preview creation failed.")); @@ -1208,7 +1306,13 @@ void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event) void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event) { wxDocument * const doc = GetCurrentDocument(); - event.Enable( doc && !doc->AlreadySaved() ); + event.Enable( doc && !doc->IsChildDocument() && !doc->AlreadySaved() ); +} + +void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event) +{ + wxDocument * const doc = GetCurrentDocument(); + event.Enable( doc && !doc->IsChildDocument() ); } void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) @@ -1216,10 +1320,14 @@ void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); if ( !cmdproc ) { - event.Enable(false); + // If we don't have any document at all, the menu item should really be + // disabled. + if ( !GetCurrentDocument() ) + event.Enable(false); + else // But if we do have it, it might handle wxID_UNDO on its own + event.Skip(); return; } - event.Enable(cmdproc->CanUndo()); cmdproc->SetMenuStrings(); } @@ -1229,15 +1337,18 @@ void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event) wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); if ( !cmdproc ) { - event.Enable(false); + // Use same logic as in OnUpdateUndo() above. + if ( !GetCurrentDocument() ) + event.Enable(false); + else + event.Skip(); return; } - event.Enable(cmdproc->CanRedo()); cmdproc->SetMenuStrings(); } -wxView *wxDocManager::GetActiveView() const +wxView *wxDocManager::GetAnyUsableView() const { wxView *view = GetCurrentView(); @@ -1263,7 +1374,7 @@ wxView *wxDocManager::GetActiveView() const bool wxDocManager::TryBefore(wxEvent& event) { - wxView * const view = GetActiveView(); + wxView * const view = GetAnyUsableView(); return view && view->ProcessEventLocally(event); } @@ -1271,11 +1382,11 @@ namespace { // helper function: return only the visible templates -wxDocTemplates GetVisibleTemplates(const wxList& allTemplates) +wxDocTemplateVector GetVisibleTemplates(const wxList& allTemplates) { // select only the visible templates const size_t totalNumTemplates = allTemplates.GetCount(); - wxDocTemplates templates; + wxDocTemplateVector templates; if ( totalNumTemplates ) { templates.reserve(totalNumTemplates); @@ -1296,15 +1407,28 @@ wxDocTemplates GetVisibleTemplates(const wxList& allTemplates) } // anonymous namespace -void wxDocManager::ActivateDocument(wxDocument *doc) +void wxDocument::Activate() { - wxView * const view = doc->GetFirstView(); + wxView * const view = GetFirstView(); if ( !view ) return; view->Activate(true); if ( wxWindow *win = view->GetFrame() ) - win->SetFocus(); + win->Raise(); +} + +wxDocument* wxDocManager::FindDocumentByPath(const wxString& path) const +{ + const wxFileName fileName(path); + for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i ) + { + wxDocument * const doc = wxStaticCast(*i, wxDocument); + + if ( fileName == wxFileName(doc->GetFilename()) ) + return doc; + } + return NULL; } wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) @@ -1312,7 +1436,7 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) // this ought to be const but SelectDocumentType/Path() are not // const-correct and can't be changed as, being virtual, this risks // breaking user code overriding them - wxDocTemplates templates(GetVisibleTemplates(m_templates)); + wxDocTemplateVector templates(GetVisibleTemplates(m_templates)); const size_t numTemplates = templates.size(); if ( !numTemplates ) { @@ -1353,20 +1477,15 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) // check whether the document with this path is already opened if ( !path.empty() ) { - const wxFileName fn(path); - for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i ) + wxDocument * const doc = FindDocumentByPath(path); + if (doc) { - wxDocument * const doc = (wxDocument*)*i; - - if ( fn == doc->GetFilename() ) - { - // file already open, just activate it and return - ActivateDocument(doc); - } + // file already open, just activate it and return + doc->Activate(); + return doc; } } - // no, we need to create a new document @@ -1411,14 +1530,14 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) // at least under Mac (where views are top level windows) it seems to be // necessary to manually activate the new document to bring it to the // forefront -- and it shouldn't hurt doing this under the other platforms - ActivateDocument(docNew); + docNew->Activate(); return docNew; } wxView *wxDocManager::CreateView(wxDocument *doc, long flags) { - wxDocTemplates templates(GetVisibleTemplates(m_templates)); + wxDocTemplateVector templates(GetVisibleTemplates(m_templates)); const size_t numTemplates = templates.size(); if ( numTemplates == 0 ) @@ -1451,7 +1570,7 @@ bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc)) wxDocument *wxDocManager::GetCurrentDocument() const { - wxView * const view = GetActiveView(); + wxView * const view = GetAnyUsableView(); return view ? view->GetDocument() : NULL; } @@ -1847,6 +1966,20 @@ void wxDocManager::DisassociateTemplate(wxDocTemplate *temp) m_templates.DeleteObject(temp); } +wxDocTemplate* wxDocManager::FindTemplate(const wxClassInfo* classinfo) +{ + for ( wxList::compatibility_iterator node = m_templates.GetFirst(); + node; + node = node->GetNext() ) + { + wxDocTemplate* t = wxStaticCast(node->GetData(), wxDocTemplate); + if ( t->GetDocClassInfo() == classinfo ) + return t; + } + + return NULL; +} + // Add and remove a document from the manager's list void wxDocManager::AddDocument(wxDocument *doc) { @@ -1881,6 +2014,26 @@ void wxDocManager::ActivateView(wxView *view, bool activate) // wxDocChildFrameAnyBase // ---------------------------------------------------------------------------- +bool wxDocChildFrameAnyBase::TryProcessEvent(wxEvent& event) +{ + if ( !m_childView ) + { + // We must be being destroyed, don't forward events anywhere as + // m_childDocument could be invalid by now. + return false; + } + + // Forward the event to the document manager which will, in turn, forward + // it to its active view which must be our m_childView. + // + // Notice that we do things in this roundabout way to guarantee the correct + // event handlers call order: first the document, then the new and then the + // document manager itself. And if we forwarded the event directly to the + // view, then the document manager would do it once again when we forwarded + // it to it. + return m_childDocument->GetDocumentManager()->ProcessEventLocally(event); +} + bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) { if ( m_childView ) @@ -1901,8 +2054,7 @@ bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) // 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; + wxDELETE(m_childView); } m_childDocument = NULL; @@ -1914,42 +2066,67 @@ bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) // wxDocParentFrameAnyBase // ---------------------------------------------------------------------------- -void wxDocParentFrameAnyBase::DoOpenMRUFile(unsigned n) +bool wxDocParentFrameAnyBase::TryProcessEvent(wxEvent& event) { - wxString filename(m_docManager->GetHistoryFile(n)); - if ( filename.empty() ) - return; - - wxString errMsg; // must contain exactly one "%s" if non-empty - if ( wxFile::Exists(filename) ) - { - // try to open it - if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) ) - return; + if ( !m_docManager ) + return false; - errMsg = _("The file '%s' couldn't be opened."); - } - else // file doesn't exist + // If we have an active view, its associated child frame may have + // already forwarded the event to wxDocManager, check for this: + if ( wxView* const view = m_docManager->GetAnyUsableView() ) { - errMsg = _("The file '%s' doesn't exist and couldn't be opened."); + wxWindow* win = view->GetFrame(); + if ( win && win != m_frame ) + { + // Notice that we intentionally don't use wxGetTopLevelParent() + // here because we want to check both for the case of a child + // "frame" (e.g. MDI child frame or notebook page) inside this TLW + // and a separate child TLW frame (as used in the SDI mode) here. + for ( win = win->GetParent(); win; win = win->GetParent() ) + { + if ( win == m_frame ) + return false; + } + } + //else: This view is directly associated with the parent frame (which + // can happen in the so called "single" mode in which only one + // document can be opened and so is managed by the parent frame + // itself), there can be no child frame in play so we must forward + // the event to wxDocManager ourselves. } + // But forward the event to wxDocManager ourselves if there are no views at + // all or if we are the frame's view ourselves. + return m_docManager->ProcessEventLocally(event); +} - wxASSERT_MSG( !errMsg.empty(), "should have an error message" ); +// ---------------------------------------------------------------------------- +// Printing support +// ---------------------------------------------------------------------------- - // remove the file which we can't open from the MRU list - m_docManager->RemoveFileFromHistory(n); +#if wxUSE_PRINTING_ARCHITECTURE - // and tell the user about it - wxLogError(errMsg + '\n' + - _("It has been removed from the most recently used files list."), - filename); +namespace +{ + +wxString GetAppropriateTitle(const wxView *view, const wxString& titleGiven) +{ + wxString title(titleGiven); + if ( title.empty() ) + { + if ( view && view->GetDocument() ) + title = view->GetDocument()->GetUserReadableName(); + else + title = _("Printout"); + } + + return title; } -#if wxUSE_PRINTING_ARCHITECTURE +} // anonymous namespace wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title) - : wxPrintout(title) + : wxPrintout(GetAppropriateTitle(view, title)) { m_printoutView = view; }