From: Vadim Zeitlin Date: Wed, 27 Mar 2013 23:10:20 +0000 (+0000) Subject: Add wxDocManager::FindDocumentByPath() helper. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/1127eb3a6af16665bdfb32f147f2988fc49a11e0 Add wxDocManager::FindDocumentByPath() helper. Simply refactor the code which already existed inside wxDocManager in a new public method. Closes #15126. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73730 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 5ece48ffc8..27117857da 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -625,6 +625,7 @@ All (GUI): - Make wxGenericDataViewCtrl::SetFont() really work (Laurent Poujoulat). - Remove wxLogWindow::OnFrameCreate(), it was never called anyhow. - Added wxDocument::Activate() (troelsk). +- Added wxDocManager::FindDocumentByPath() (troelsk). wxGTK: diff --git a/include/wx/docview.h b/include/wx/docview.h index d9c34e64a6..3b06cb0826 100644 --- a/include/wx/docview.h +++ b/include/wx/docview.h @@ -446,6 +446,9 @@ public: // Find template from document class info, may return NULL. wxDocTemplate* FindTemplate(const wxClassInfo* documentClassInfo); + // Find document from file name, may return NULL. + wxDocument* FindDocumentByPath(const wxString& path) const; + wxDocument *GetCurrentDocument() const; void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; } diff --git a/interface/wx/docview.h b/interface/wx/docview.h index 279556757b..7f5a5ee99e 100644 --- a/interface/wx/docview.h +++ b/interface/wx/docview.h @@ -414,6 +414,19 @@ public: */ wxDocTemplate* FindTemplate(const wxClassInfo* classinfo); + + /** + Search for the document corresponding to the given file. + + @param path + Document file path. + @return + Pointer to a wxDocument, or @NULL if none found. + + @since 2.9.5 + */ + wxDocument* FindDocumentByPath(const wxString& path) const; + /** Closes the specified document. diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 234c5c2b85..4b1d2a0aff 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -1418,6 +1418,19 @@ void wxDocument::Activate() 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) { // this ought to be const but SelectDocumentType/Path() are not @@ -1464,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 - doc->Activate(); - return doc; - } + // file already open, just activate it and return + doc->Activate(); + return doc; } } - // no, we need to create a new document