]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/docview.cpp
Fix wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK name in the documentation.
[wxWidgets.git] / src / common / docview.cpp
index e4f6bf3bdb2b5f097298e56a8f30be8f83f9ef68..4b1d2a0affece98aa03e069ac1ea97b380460f98 100644 (file)
@@ -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<wxDocTemplate *> wxDocTemplates;
-
 // ----------------------------------------------------------------------------
 // wxWidgets macros
 // ----------------------------------------------------------------------------
@@ -397,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);
@@ -856,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)
@@ -1316,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();
 }
@@ -1329,10 +1337,13 @@ 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();
 }
@@ -1371,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);
@@ -1396,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)
@@ -1412,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 )
     {
@@ -1453,21 +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);
-                return doc;
-            }
+            // file already open, just activate it and return
+            doc->Activate();
+            return doc;
         }
     }
 
-
     // no, we need to create a new document
 
 
@@ -1512,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 )