X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e58eaff7617f0936b1b8c448dd381f8e2d7ccf78..8cf30134cd3881c0da18ef348206215f5b62bcb7:/src/common/docview.cpp diff --git a/src/common/docview.cpp b/src/common/docview.cpp index e308d867c0..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 @@ -582,10 +578,10 @@ bool wxDocument::DoSaveDocument(const wxString& file) { #if wxUSE_STD_IOSTREAM wxSTD ofstream store(file.mb_str(), wxSTD ios::binary); - if (store.fail() || store.bad()) + if ( !store ) #else wxFileOutputStream store(file); - if (store.GetLastError() != wxSTREAM_NO_ERROR) + if ( store.GetLastError() != wxSTREAM_NO_ERROR ) #endif { wxLogError(_("File \"%s\" could not be opened for writing."), file); @@ -605,10 +601,10 @@ bool wxDocument::DoOpenDocument(const wxString& file) { #if wxUSE_STD_IOSTREAM wxSTD ifstream store(file.mb_str(), wxSTD ios::binary); - if ( store.fail() || store.bad() ) + 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); @@ -617,7 +613,7 @@ bool wxDocument::DoOpenDocument(const wxString& file) #if wxUSE_STD_IOSTREAM LoadObject(store); - if ( store.fail() || store.bad() ) + if ( !store ) #else int res = LoadObject(store).GetLastError(); if ( res != wxSTREAM_NO_ERROR && res != wxSTREAM_EOF ) @@ -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 @@ -972,7 +963,7 @@ void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event)) void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event)) { - CreateDocument( wxEmptyString, wxDOC_NEW ); + CreateNewDocument(); } void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event)) @@ -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 @@ -2013,24 +2008,22 @@ void wxFileHistory::AddFileToHistory(const wxString& file) { RemoveFileFromHistory(--numFiles); } - else // add a new menu item to all file menus (will be updated below) + + // add a new menu item to all file menus (they will be updated below) + for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst(); + node; + node = node->GetNext() ) { - for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst(); - node; - node = node->GetNext() ) - { - wxMenu * const menu = (wxMenu *)node->GetData(); + wxMenu * const menu = (wxMenu *)node->GetData(); - if ( !numFiles && menu->GetMenuItemCount() ) - menu->AppendSeparator(); + if ( !numFiles && menu->GetMenuItemCount() ) + menu->AppendSeparator(); - // label doesn't matter, it will be set below anyhow, but it can't - // be empty (this is supposed to indicate a stock item) - menu->Append(m_idBase + numFiles, " "); - } + // label doesn't matter, it will be set below anyhow, but it can't + // be empty (this is supposed to indicate a stock item) + menu->Append(m_idBase + numFiles, " "); } - // insert the new file in the beginning of the file history m_fileHistory.insert(m_fileHistory.begin(), file); numFiles++;