X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b4a980f4f57a4e7eea00c55cbb3d139f97d90c20..bba35861478a26f5d8c756a9a7fa2bbd19a69cb1:/src/common/docview.cpp?ds=sidebyside diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 41b31fb23e..d678ca6b54 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -66,11 +66,13 @@ #if wxUSE_STD_IOSTREAM #include "wx/ioswrap.h" + #include "wx/beforestd.h" #if wxUSE_IOSTREAMH #include #else #include #endif + #include "wx/afterstd.h" #else #include "wx/wfstream.h" #endif @@ -112,7 +114,7 @@ static const wxChar *s_MRUEntryFormat = wxT("&%d %s"); // local functions // ---------------------------------------------------------------------------- -static wxString FindExtension(const wxChar *path) +static wxString FindExtension(const wxString& path) { wxString ext; wxSplitPath(path, NULL, NULL, &ext); @@ -241,8 +243,7 @@ bool wxDocument::OnNewDocument() Modify(false); SetDocumentSaved(false); - wxString name; - GetDocumentManager()->MakeDefaultName(name); + const wxString name = GetDocumentManager()->MakeNewDocumentName(); SetTitle(name); SetFilename(name, true); @@ -297,8 +298,12 @@ bool wxDocument::SaveAs() #else wxString filter = docTemplate->GetFileFilter() ; #endif - wxString tmp = wxFileSelector(_("Save as"), - docTemplate->GetDirectory(), + wxString defaultDir = docTemplate->GetDirectory(); + if (defaultDir.IsEmpty()) + defaultDir = wxPathOnly(GetFilename()); + + wxString tmp = wxFileSelector(_("Save As"), + defaultDir, wxFileNameFromPath(GetFilename()), docTemplate->GetDefaultExtension(), filter, @@ -408,23 +413,41 @@ bool wxDocument::Revert() // Get title, or filename if no title, else unnamed +#if WXWIN_COMPATIBILITY_2_8 bool wxDocument::GetPrintableName(wxString& buf) const { - if (!m_documentTitle.empty()) - { - buf = m_documentTitle; - return true; - } - else if (!m_documentFile.empty()) - { - buf = wxFileNameFromPath(m_documentFile); - return true; - } - else - { - buf = _("unnamed"); - return true; - } + // this function can not only be overridden by the user code but also + // called by it so we need to ensure that we return the same thing as + // GetUserReadableName() but we can't call it because this would result in + // an infinite recursion, hence we use the helper DoGetUserReadableName() + buf = DoGetUserReadableName(); + + return true; +} +#endif // WXWIN_COMPATIBILITY_2_8 + +wxString wxDocument::GetUserReadableName() const +{ +#if WXWIN_COMPATIBILITY_2_8 + // we need to call the old virtual function to ensure that the overridden + // version of it is still called + wxString name; + if ( GetPrintableName(name) ) + return name; +#endif // WXWIN_COMPATIBILITY_2_8 + + return DoGetUserReadableName(); +} + +wxString wxDocument::DoGetUserReadableName() const +{ + if ( !m_documentTitle.empty() ) + return m_documentTitle; + + if ( !m_documentFile.empty() ) + return wxFileNameFromPath(m_documentFile); + + return _("unnamed"); } wxWindow *wxDocument::GetDocumentWindow() const @@ -446,18 +469,16 @@ bool wxDocument::OnSaveModified() { if (IsModified()) { - wxString title; - GetPrintableName(title); + wxString title = GetUserReadableName(); wxString msgTitle; - if (!wxTheApp->GetAppName().empty()) - msgTitle = wxTheApp->GetAppName(); + if (!wxTheApp->GetAppDisplayName().empty()) + msgTitle = wxTheApp->GetAppDisplayName(); else msgTitle = wxString(_("Warning")); wxString prompt; - prompt.Printf(_("Do you want to save changes to document %s?"), - (const wxChar *)title); + prompt.Printf(_("Do you want to save changes to document %s?"), title); int res = wxMessageBox(prompt, msgTitle, wxYES_NO|wxCANCEL|wxICON_QUESTION, GetDocumentWindow()); @@ -560,8 +581,8 @@ void wxDocument::SetFilename(const wxString& filename, bool notifyViews) bool wxDocument::DoSaveDocument(const wxString& file) { wxString msgTitle; - if (!wxTheApp->GetAppName().empty()) - msgTitle = wxTheApp->GetAppName(); + if (!wxTheApp->GetAppDisplayName().empty()) + msgTitle = wxTheApp->GetAppDisplayName(); else msgTitle = wxString(_("File error")); @@ -664,9 +685,7 @@ void wxView::OnChangeFilename() wxDocument *doc = GetDocument(); if (!doc) return; - wxString name; - doc->GetPrintableName(name); - win->SetLabel(name); + win->SetLabel(doc->GetUserReadableName()); } void wxView::SetDocument(wxDocument *doc) @@ -1405,27 +1424,43 @@ wxDocument *wxDocManager::GetCurrentDocument() const return (wxDocument *) NULL; } -// Make a default document name -bool wxDocManager::MakeDefaultName(wxString& name) +// Make a default name for a new document +#if WXWIN_COMPATIBILITY_2_8 +bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name)) +{ + // we consider that this function can only be overridden by the user code, + // not called by it as it only makes sense to call it internally, so we + // don't bother to return anything from here + return false; +} +#endif // WXWIN_COMPATIBILITY_2_8 + +wxString wxDocManager::MakeNewDocumentName() { - name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter); - m_defaultDocumentNameCounter++; + wxString name; - return true; +#if WXWIN_COMPATIBILITY_2_8 + if ( !MakeDefaultName(name) ) +#endif // WXWIN_COMPATIBILITY_2_8 + { + name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter); + m_defaultDocumentNameCounter++; + } + + return name; } // Make a frame title (override this to do something different) // If docName is empty, a document is not currently active. wxString wxDocManager::MakeFrameTitle(wxDocument* doc) { - wxString appName = wxTheApp->GetAppName(); + wxString appName = wxTheApp->GetAppDisplayName(); wxString title; if (!doc) title = appName; else { - wxString docName; - doc->GetPrintableName(docName); + wxString docName = doc->GetUserReadableName(); title = docName + wxString(_(" - ")) + appName; } return title; @@ -1586,7 +1621,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, wxWindow* parent = wxFindSuitableParent(); - wxString pathTmp = wxFileSelectorEx(_("Select a file"), + wxString pathTmp = wxFileSelectorEx(_("Open File"), m_lastDirectory, wxEmptyString, &FilterIndex, @@ -1600,8 +1635,8 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, if (!wxFileExists(pathTmp)) { wxString msgTitle; - if (!wxTheApp->GetAppName().empty()) - msgTitle = wxTheApp->GetAppName(); + if (!wxTheApp->GetAppDisplayName().empty()) + msgTitle = wxTheApp->GetAppDisplayName(); else msgTitle = wxString(_("File error")); @@ -2185,7 +2220,7 @@ void wxFileHistory::RemoveFileFromHistory(size_t i) wxCHECK_RET( i < m_fileHistory.GetCount(), wxT("invalid index in wxFileHistory::RemoveFileFromHistory") ); - // delete the element from the array + // delete the element from the array m_fileHistory.RemoveAt(i); wxList::compatibility_iterator node = m_fileMenus.GetFirst(); @@ -2248,7 +2283,7 @@ void wxFileHistory::Load(wxConfigBase& config) buf.Printf(wxT("file%d"), 1); wxString historyFile; - while ((m_fileHistory.GetCount() < m_fileMaxFiles) && + while ((m_fileHistory.GetCount() < m_fileMaxFiles) && config.Read(buf, &historyFile) && !historyFile.empty()) { m_fileHistory.Add(historyFile); @@ -2404,15 +2439,22 @@ bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) return false; char buf[4096]; - do + for ( ;; ) { stream.Read(buf, WXSIZEOF(buf)); const size_t nRead = stream.LastRead(); - if ( !nRead || !file.Write(buf, nRead) ) + if ( !nRead ) + { + if ( stream.Eof() ) + break; + + return false; + } + + if ( !file.Write(buf, nRead) ) return false; } - while ( !stream.Eof() ); return true; }