From b72b1920368319d626f486e511fceeea144f7676 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Thu, 1 May 2003 15:40:39 +0000 Subject: [PATCH] Implemented delayed closing of an existing document git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@20408 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/docview.h | 3 ++ src/common/docview.cpp | 74 ++++++++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/include/wx/docview.h b/include/wx/docview.h index 8c8bf6fa6f..ef9a47fd52 100644 --- a/include/wx/docview.h +++ b/include/wx/docview.h @@ -373,6 +373,9 @@ public: // closes all currently open documents bool CloseDocuments(bool force = TRUE); + // closes the specified document + bool CloseDocument(wxDocument* doc, bool force = FALSE); + // Clear remaining documents and templates bool Clear(bool force = TRUE); diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 9b8de09018..ef47161018 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -774,6 +774,24 @@ wxDocManager::~wxDocManager() sm_docManager = (wxDocManager*) NULL; } +// closes the specified document +bool wxDocManager::CloseDocument(wxDocument* doc, bool force) +{ + if (doc->Close() || force) + { + // Implicitly deletes the document when + // the last view is deleted + doc->DeleteAllViews(); + + // Check we're really deleted + if (m_docs.Member(doc)) + delete doc; + + return TRUE; + } + return FALSE; +} + bool wxDocManager::CloseDocuments(bool force) { wxNode *node = m_docs.GetFirst(); @@ -781,18 +799,10 @@ bool wxDocManager::CloseDocuments(bool force) { wxDocument *doc = (wxDocument *)node->GetData(); wxNode *next = node->GetNext(); - - if (!doc->Close() && !force) + + if (!CloseDocument(doc, force)) return FALSE; - // Implicitly deletes the document when the last - // view is removed (deleted) - doc->DeleteAllViews(); - - // Check document is deleted - if (m_docs.Member(doc)) - delete doc; - // This assumes that documents are not connected in // any way, i.e. deleting one document does NOT // delete another. @@ -1070,27 +1080,15 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) delete[] templates; return (wxDocument *) NULL; } + + wxDocument* docToClose = NULL; // If we've reached the max number of docs, close the // first one. if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen ) { wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData(); - if (doc->Close()) - { - // Implicitly deletes the document when - // the last view is deleted - doc->DeleteAllViews(); - - // Check we're really deleted - if (m_docs.Member(doc)) - delete doc; - } - else - { - delete[] templates; - return (wxDocument *) NULL; - } + docToClose = doc; } // New document: user chooses a template, unless there's only one. @@ -1098,9 +1096,18 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) { if (n == 1) { + if (docToClose) + { + if (!CloseDocument(docToClose, FALSE)) + { + return NULL; + } + } + wxDocTemplate *temp = templates[0]; delete[] templates; wxDocument *newDoc = temp->CreateDocument(path, flags); + if (newDoc) { newDoc->SetDocumentName(temp->GetDocumentName()); @@ -1114,7 +1121,16 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) delete[] templates; if (temp) { + if (docToClose) + { + if (!CloseDocument(docToClose, FALSE)) + { + return NULL; + } + } + wxDocument *newDoc = temp->CreateDocument(path, flags); + if (newDoc) { newDoc->SetDocumentName(temp->GetDocumentName()); @@ -1143,6 +1159,14 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) if (temp) { + if (docToClose) + { + if (!CloseDocument(docToClose, FALSE)) + { + return NULL; + } + } + wxDocument *newDoc = temp->CreateDocument(path2, flags); if (newDoc) { -- 2.45.2