git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@20408 
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
     // closes all currently open documents
     bool CloseDocuments(bool force = TRUE);
 
     // 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);
 
     // Clear remaining documents and templates
     bool Clear(bool force = TRUE);
 
 
     sm_docManager = (wxDocManager*) NULL;
 }
 
     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();
 bool wxDocManager::CloseDocuments(bool force)
 {
     wxNode *node = m_docs.GetFirst();
     {
         wxDocument *doc = (wxDocument *)node->GetData();
         wxNode *next = node->GetNext();
     {
         wxDocument *doc = (wxDocument *)node->GetData();
         wxNode *next = node->GetNext();
-
-        if (!doc->Close() && !force)
+        
+        if (!CloseDocument(doc, force))
-        // 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.
         // This assumes that documents are not connected in
         // any way, i.e. deleting one document does NOT
         // delete another.
         delete[] templates;
         return (wxDocument *) NULL;
     }
         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 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;
-        }
     }
 
     // New document: user chooses a template, unless there's only one.
     }
 
     // New document: user chooses a template, unless there's only one.
+            if (docToClose)
+            {
+                if (!CloseDocument(docToClose, FALSE))
+                {
+                    return NULL;
+                }
+            }
+            
             wxDocTemplate *temp = templates[0];
             delete[] templates;
             wxDocument *newDoc = temp->CreateDocument(path, flags);
             wxDocTemplate *temp = templates[0];
             delete[] templates;
             wxDocument *newDoc = temp->CreateDocument(path, flags);
             if (newDoc)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
             if (newDoc)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
         delete[] templates;
         if (temp)
         {
         delete[] templates;
         if (temp)
         {
+            if (docToClose)
+            {
+                if (!CloseDocument(docToClose, FALSE))
+                {
+                    return NULL;
+                }
+            }
+            
             wxDocument *newDoc = temp->CreateDocument(path, flags);
             wxDocument *newDoc = temp->CreateDocument(path, flags);
             if (newDoc)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
             if (newDoc)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
+        if (docToClose)
+        {
+            if (!CloseDocument(docToClose, FALSE))
+            {
+                return NULL;
+            }
+        }
+        
         wxDocument *newDoc = temp->CreateDocument(path2, flags);
         if (newDoc)
         {
         wxDocument *newDoc = temp->CreateDocument(path2, flags);
         if (newDoc)
         {