X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/838dd956a4049c7c95f015ee6b79d3bcf2ec9d6e..8cf30134cd3881c0da18ef348206215f5b62bcb7:/src/common/docview.cpp?ds=sidebyside diff --git a/src/common/docview.cpp b/src/common/docview.cpp index db67b46123..ac7af5109a 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -57,6 +57,7 @@ #include "wx/tokenzr.h" #include "wx/filename.h" #include "wx/vector.h" +#include "wx/ptr_scpd.h" #if wxUSE_STD_IOSTREAM #include "wx/ioswrap.h" @@ -111,7 +112,7 @@ wxWindow *wxFindSuitableParent() wxString FindExtension(const wxString& path) { wxString ext; - wxSplitPath(path, NULL, NULL, &ext); + wxFileName::SplitPath(path, NULL, NULL, &ext); // VZ: extensions are considered not case sensitive - is this really a good // idea? @@ -259,7 +260,7 @@ bool wxDocument::OnNewDocument() bool wxDocument::Save() { - if (!IsModified() && m_savedYet) + if ( AlreadySaved() ) return true; if ( m_documentFile.empty() || !m_savedYet ) @@ -309,7 +310,7 @@ bool wxDocument::SaveAs() if (defaultDir.IsEmpty()) defaultDir = wxPathOnly(GetFilename()); - wxString tmp = wxFileSelector(_("Save As"), + wxString fileName = wxFileSelector(_("Save As"), defaultDir, wxFileNameFromPath(GetFilename()), docTemplate->GetDefaultExtension(), @@ -317,12 +318,11 @@ bool wxDocument::SaveAs() wxFD_SAVE | wxFD_OVERWRITE_PROMPT, GetDocumentWindow()); - if (tmp.empty()) + if (fileName.empty()) return false; - wxString fileName(tmp); - wxString path, name, ext; - wxSplitPath(fileName, & path, & name, & ext); + wxString ext; + wxFileName::SplitPath(fileName, NULL, NULL, &ext); if (ext.empty()) { @@ -330,22 +330,13 @@ bool wxDocument::SaveAs() fileName += docTemplate->GetDefaultExtension(); } - SetFilename(fileName); - SetTitle(wxFileNameFromPath(fileName)); - - // Notify the views that the filename has changed - wxList::compatibility_iterator node = m_documentViews.GetFirst(); - while (node) - { - wxView *view = (wxView *)node->GetData(); - view->OnChangeFilename(); - node = node->GetNext(); - } - // Files that were not saved correctly are not added to the FileHistory. - if (!OnSaveDocument(m_documentFile)) + if (!OnSaveDocument(fileName)) return false; + SetTitle(wxFileNameFromPath(fileName)); + SetFilename(fileName, true); // will call OnChangeFileName automatically + // A file that doesn't use the default extension of its document template cannot be opened // via the FileHistory, so we do not add it. if (docTemplate->FileMatchesTemplate(fileName)) @@ -484,8 +475,8 @@ bool wxDocument::OnSaveModified() GetUserReadableName() ), wxTheApp->GetAppDisplayName(), - wxYES_NO | wxCANCEL | wxICON_QUESTION, - GetDocumentWindow() + wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE, + wxFindSuitableParent() ) ) { case wxNO: @@ -565,6 +556,11 @@ void wxDocument::NotifyClosing() void wxDocument::SetFilename(const wxString& filename, bool notifyViews) { m_documentFile = filename; + OnChangeFilename(notifyViews); +} + +void wxDocument::OnChangeFilename(bool notifyViews) +{ if ( notifyViews ) { // Notify the views that the filename has changed @@ -608,7 +604,7 @@ bool wxDocument::DoOpenDocument(const wxString& file) if ( !store ) #else wxFileInputStream store(file); - if (store.GetLastError() != wxSTREAM_NO_ERROR) + if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk()) #endif { wxLogError(_("File \"%s\" could not be opened for reading."), file); @@ -779,20 +775,15 @@ wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) { - wxView *view = DoCreateView(); - if ( view == NULL ) + wxScopedPtr view(DoCreateView()); + if ( !view ) return NULL; view->SetDocument(doc); - if (view->OnCreate(doc, flags)) - { - return view; - } - else - { - delete view; + if ( !view->OnCreate(doc, flags) ) return NULL; - } + + return view.release(); } // The default (very primitive) format detection: check is the extension is @@ -1079,7 +1070,9 @@ void wxDocManager::OnRedo(wxCommandEvent& event) void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event) { - event.Enable( true ); + // CreateDocument() (which is called from OnFileOpen) may succeed + // only when there is at least a template: + event.Enable( GetTemplates().GetCount()>0 ); } void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event) @@ -1089,13 +1082,15 @@ void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event) void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event) { - event.Enable( true ); + // CreateDocument() (which is called from OnFileNew) may succeed + // only when there is at least a template: + event.Enable( GetTemplates().GetCount()>0 ); } void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event) { - wxDocument *doc = GetCurrentDocument(); - event.Enable( doc && doc->IsModified() ); + wxDocument * const doc = GetCurrentDocument(); + event.Enable( doc && !doc->AlreadySaved() ); } void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) @@ -1513,7 +1508,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, else msgTitle = wxString(_("File error")); - (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION, + (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION | wxCENTRE, parent); path = wxEmptyString; @@ -1535,7 +1530,7 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, // can only happen if the application changes the allowed templates in runtime. (void)wxMessageBox(_("Sorry, the format for this file is unknown."), _("Open File"), - wxOK | wxICON_EXCLAMATION, wxFindSuitableParent()); + wxOK | wxICON_EXCLAMATION | wxCENTRE, wxFindSuitableParent()); } } else