From d38eb01c91f5d84236a881c71c78ae0e3b06fb17 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 22 Jul 2011 16:16:09 +0000 Subject: [PATCH] Added customizable wxDocManager::OnMRUFileNotExist() virtual method. This method can be overridden to customize the previously hard-coded handling of the case when a file selected from the MRU menu doesn't exist any more: we used to always remove it from the file history completely. This may, however, be inappropriate and, in fact, probably never, or very rarely, is the right thing to do when the file that we failed to open still exists. So never remove the file from the MRU if we failed to open an existing file (also don't give an error about it as it should have been already given by CreateDocument()) and, while we still do it for the non-existent files, allow to override this behaviour by overriding the new OnMRUFileNotExist() method. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68334 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/docview.h | 5 +++++ interface/wx/docview.h | 30 ++++++++++++++++++++++++++++++ src/common/docview.cpp | 23 ++++++++++++----------- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 7344aa9..182b570 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -447,6 +447,7 @@ All (GUI): - Fix keyboard navigation in wxGrid with hidden columns (ivan_14_32). - Add wxDataViewEvent::IsEditCancelled() (Allonii). - Allow marking wxTreeBook nodes to expand initially in XRC (RedTide). +- Added customizable wxDocManager::OnMRUFileNotExist() virtual method. OSX: diff --git a/include/wx/docview.h b/include/wx/docview.h index a6efd5d..502a714 100644 --- a/include/wx/docview.h +++ b/include/wx/docview.h @@ -510,6 +510,11 @@ public: protected: + // Called when a file selected from the MRU list doesn't exist any more. + // The default behaviour is to remove the file from the MRU and notify the + // user about it but this method can be overridden to customize it. + virtual void OnMRUFileNotExist(unsigned n, const wxString& filename); + // Open the MRU file with the given index in our associated file history. void DoOpenMRUFile(unsigned n); #if wxUSE_PRINTING_ARCHITECTURE diff --git a/interface/wx/docview.h b/interface/wx/docview.h index 1b2c9f9..5d5f266 100644 --- a/interface/wx/docview.h +++ b/interface/wx/docview.h @@ -750,6 +750,36 @@ public: protected: /** + Called when a file selected from the MRU list doesn't exist any more. + + The default behaviour is to remove the file from the MRU (most recently + used) files list and the corresponding menu and notify the user about + it but this method can be overridden to customize it. + + For example, an application may want to just give an error about the + missing file @a filename but not remove it from the file history. Or it + could ask the user whether the file should be kept or removed. + + Notice that this method is called only if the file selected by user + from the MRU files in the menu doesn't exist, but not if opening it + failed for any other reason because in the latter case the default + behaviour of removing the file from the MRU list is inappropriate. + If you still want to do it, you would need to do it by calling + RemoveFileFromHistory() explicitly in the part of the file opening code + that may fail. + + @since 2.9.3 + + @param n + The index of the file in the MRU list, it can be passed to + RemoveFileFromHistory() to remove this file from the list. + @param filename + The full name of the file. + */ + virtual void OnMRUFileNotExist(unsigned n, const wxString& filename); + + + /** The currently active view. */ wxView* m_currentView; diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 6d6950b..e4f6bf3 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -1153,26 +1153,27 @@ void wxDocManager::DoOpenMRUFile(unsigned n) wxString errMsg; // must contain exactly one "%s" if non-empty if ( wxFile::Exists(filename) ) { - // try to open it - if ( CreateDocument(filename, wxDOC_SILENT) ) - return; - - errMsg = _("The file '%s' couldn't be opened."); + // Try to open it but don't give an error if it failed: this could be + // normal, e.g. because the user cancelled opening it, and we don't + // have any useful information to put in the error message anyhow, so + // we assume that in case of an error the appropriate message had been + // already logged. + (void)CreateDocument(filename, wxDOC_SILENT); } else // file doesn't exist { - errMsg = _("The file '%s' doesn't exist and couldn't be opened."); + OnMRUFileNotExist(n, filename); } +} - - wxASSERT_MSG( !errMsg.empty(), "should have an error message" ); - +void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename) +{ // remove the file which we can't open from the MRU list RemoveFileFromHistory(n); // and tell the user about it - wxLogError(errMsg + '\n' + - _("It has been removed from the most recently used files list."), + wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n" + "It has been removed from the most recently used files list."), filename); } -- 2.7.4