]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/docview.cpp
adding OS level Window Menu by default
[wxWidgets.git] / src / common / docview.cpp
index 64ea2a86273a038b3f8596e0d5d67b1ca567d43e..221ce1b1364ab66a3209fe731c1727f3cf1e0d9a 100644 (file)
@@ -183,23 +183,43 @@ bool wxDocument::OnCloseDocument()
 bool wxDocument::DeleteAllViews()
 {
     wxDocManager* manager = GetDocumentManager();
-    wxList::iterator it, en;
 
-    for ( it = m_documentViews.begin(), en = m_documentViews.end();
-          it != en;
-          ++it )
+    // first check if all views agree to be closed
+    const wxList::iterator end = m_documentViews.end();
+    for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
     {
-        wxView *view = (wxView *)*it;
-        if (!view->Close())
+        wxView *view = (wxView *)*i;
+        if ( !view->Close() )
             return false;
+    }
 
-        delete view; // Deletes node implicitly
+    // all views agreed to close, now do close them
+    if ( m_documentViews.empty() )
+    {
+        // normally the document would be implicitly deleted when the last view
+        // is, but if don't have any views, do it here instead
+        if ( manager && manager->GetDocuments().Member(this) )
+            delete this;
     }
+    else // have views
+    {
+        // as we delete elements we iterate over, don't use the usual "from
+        // begin to end" loop
+        for ( ;; )
+        {
+            wxView *view = (wxView *)*m_documentViews.begin();
+
+            bool isLastOne = m_documentViews.size() == 1;
 
-    // If we haven't yet deleted the document (for example
-    // if there were no views) then delete it.
-    if (manager && manager->GetDocuments().Member(this))
-        delete this;
+            // this always deletes the node implicitly and if this is the last
+            // view also deletes this object itself (also implicitly, great),
+            // so we can't test for m_documentViews.empty() after calling this!
+            delete view;
+
+            if ( isLastOne )
+                break;
+        }
+    }
 
     return true;
 }
@@ -1205,7 +1225,12 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
                 newDoc->SetDocumentTemplate(temp);
-                newDoc->OnNewDocument();
+                if (!newDoc->OnNewDocument() )
+                {
+                     // Document is implicitly deleted by DeleteAllViews
+                     newDoc->DeleteAllViews();
+                     return NULL;
+                }
             }
             return newDoc;
         }
@@ -1228,7 +1253,12 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
             {
                 newDoc->SetDocumentName(temp->GetDocumentName());
                 newDoc->SetDocumentTemplate(temp);
-                newDoc->OnNewDocument();
+                if (!newDoc->OnNewDocument() )
+                {
+                     // Document is implicitly deleted by DeleteAllViews
+                     newDoc->DeleteAllViews();
+                     return NULL;
+                }
             }
             return newDoc;
         }
@@ -1268,6 +1298,28 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
             }
         }
 
+        //see if this file is already open
+        for (size_t i = 0; i < GetDocuments().GetCount(); ++i)
+        {
+            wxDocument* currentDoc = (wxDocument*)(GetDocuments().Item(i)->GetData());
+#ifdef __WXMSW__
+            //file paths are case-insensitive on Windows
+            if (path2.CmpNoCase(currentDoc->GetFilename()) == 0)
+#else
+            if (path2.Cmp(currentDoc->GetFilename()) == 0)
+#endif
+            {
+                //file already open. Just activate it and return
+                if (currentDoc->GetFirstView())
+                {
+                    ActivateView(currentDoc->GetFirstView(), true);
+                    if (currentDoc->GetDocumentWindow())
+                        currentDoc->GetDocumentWindow()->SetFocus();
+                    return currentDoc;
+                }
+            }
+        }
+
         wxDocument *newDoc = temp->CreateDocument(path2, flags);
         if (newDoc)
         {