From 69936aea19dfef8d738abb6ed48a77ec6065e142 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Jul 2004 15:42:11 +0000 Subject: [PATCH] added DoCreate/Load/SaveDocument() (patches 983570 and 983571) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28474 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/docview.h | 12 ++++ src/common/docview.cpp | 154 ++++++++++++++++++++++++++++++------------------- 2 files changed, 107 insertions(+), 59 deletions(-) diff --git a/include/wx/docview.h b/include/wx/docview.h index 0d005d0..94a13b2 100644 --- a/include/wx/docview.h +++ b/include/wx/docview.h @@ -162,6 +162,12 @@ protected: wxCommandProcessor* m_commandProcessor; bool m_savedYet; + // Called by OnSaveDocument and OnOpenDocument to implement standard + // Save/Load behavior. Re-implement in derived class for custom + // behavior. + virtual bool DoSaveDocument(const wxString& file); + virtual bool DoOpenDocument(const wxString& file); + private: DECLARE_ABSTRACT_CLASS(wxDocument) DECLARE_NO_COPY_CLASS(wxDocument) @@ -295,6 +301,12 @@ protected: wxClassInfo* m_docClassInfo; wxClassInfo* m_viewClassInfo; + // Called by CreateDocument and CreateView to create the actual document/view object. + // By default uses the ClassInfo provided to the constructor. Override these functions + // to provide a different method of creation. + virtual wxDocument *DoCreateDocument(); + virtual wxView *DoCreateView(); + private: DECLARE_CLASS(wxDocTemplate) DECLARE_NO_COPY_CLASS(wxDocTemplate) diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 6e5f249..767a07f 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -340,32 +340,9 @@ bool wxDocument::OnSaveDocument(const wxString& file) if ( !file ) return FALSE; - wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) - msgTitle = wxTheApp->GetAppName(); - else - msgTitle = wxString(_("File error")); + if ( !DoSaveDocument(file) ) + return FALSE; -#if wxUSE_STD_IOSTREAM - wxSTD ofstream store(file.mb_str()); - if (store.fail() || store.bad()) -#else - wxFileOutputStream store(file); - if (store.GetLastError() != wxSTREAM_NO_ERROR) -#endif - { - (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION, - GetDocumentWindow()); - // Saving error - return FALSE; - } - if (!SaveObject(store)) - { - (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION, - GetDocumentWindow()); - // Saving error - return FALSE; - } Modify(FALSE); SetFilename(file); SetDocumentSaved(TRUE); @@ -381,37 +358,9 @@ bool wxDocument::OnOpenDocument(const wxString& file) if (!OnSaveModified()) return FALSE; - wxString msgTitle; - if (wxTheApp->GetAppName() != wxT("")) - msgTitle = wxTheApp->GetAppName(); - else - msgTitle = wxString(_("File error")); + if ( !DoOpenDocument(file) ) + return FALSE; -#if wxUSE_STD_IOSTREAM - wxSTD ifstream store(file.mb_str()); - if (store.fail() || store.bad()) -#else - wxFileInputStream store(file); - if (store.GetLastError() != wxSTREAM_NO_ERROR) -#endif - { - (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, - GetDocumentWindow()); - return FALSE; - } -#if wxUSE_STD_IOSTREAM - LoadObject(store); - if ( !store && !store.eof() ) -#else - int res = LoadObject(store).GetLastError(); - if ((res != wxSTREAM_NO_ERROR) && - (res != wxSTREAM_EOF)) -#endif - { - (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, - GetDocumentWindow()); - return FALSE; - } SetFilename(file, TRUE); Modify(FALSE); m_savedYet = TRUE; @@ -595,6 +544,76 @@ void wxDocument::SetFilename(const wxString& filename, bool notifyViews) } } +bool wxDocument::DoSaveDocument(const wxString& file) +{ + wxString msgTitle; + if (wxTheApp->GetAppName() != wxT("")) + msgTitle = wxTheApp->GetAppName(); + else + msgTitle = wxString(_("File error")); + +#if wxUSE_STD_IOSTREAM + wxSTD ofstream store(file.mb_str()); + if (store.fail() || store.bad()) +#else + wxFileOutputStream store(file); + if (store.GetLastError() != wxSTREAM_NO_ERROR) +#endif + { + (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION, + GetDocumentWindow()); + // Saving error + return FALSE; + } + if (!SaveObject(store)) + { + (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION, + GetDocumentWindow()); + // Saving error + return FALSE; + } + + return TRUE; +} + +bool wxDocument::DoOpenDocument(const wxString& file) +{ + wxString msgTitle; + if (wxTheApp->GetAppName() != wxT("")) + msgTitle = wxTheApp->GetAppName(); + else + msgTitle = wxString(_("File error")); + +#if wxUSE_STD_IOSTREAM + wxSTD ifstream store(file.mb_str()); + if (store.fail() || store.bad()) +#else + wxFileInputStream store(file); + if (store.GetLastError() != wxSTREAM_NO_ERROR) +#endif + { + (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, + GetDocumentWindow()); + return FALSE; + } +#if wxUSE_STD_IOSTREAM + LoadObject(store); + if ( !store && !store.eof() ) +#else + int res = LoadObject(store).GetLastError(); + if ((res != wxSTREAM_NO_ERROR) && + (res != wxSTREAM_EOF)) +#endif + { + (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, + GetDocumentWindow()); + return FALSE; + } + + return TRUE; +} + + // ---------------------------------------------------------------------------- // Document view // ---------------------------------------------------------------------------- @@ -719,9 +738,9 @@ wxDocTemplate::~wxDocTemplate() // Tries to dynamically construct an object of the right class. wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) { - if (!m_docClassInfo) + wxDocument *doc = DoCreateDocument(); + if ( doc == NULL ) return (wxDocument *) NULL; - wxDocument *doc = (wxDocument *)m_docClassInfo->CreateObject(); if (InitDocument(doc, path, flags)) { @@ -752,9 +771,10 @@ bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long fla wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) { - if (!m_viewClassInfo) + wxView *view = DoCreateView(); + if ( view == NULL ) return (wxView *) NULL; - wxView *view = (wxView *)m_viewClassInfo->CreateObject(); + view->SetDocument(doc); if (view->OnCreate(doc, flags)) { @@ -774,6 +794,22 @@ bool wxDocTemplate::FileMatchesTemplate(const wxString& path) return GetDefaultExtension().IsSameAs(FindExtension(path)); } +wxDocument *wxDocTemplate::DoCreateDocument() +{ + if (!m_docClassInfo) + return (wxDocument *) NULL; + + return (wxDocument *)m_docClassInfo->CreateObject(); +} + +wxView *wxDocTemplate::DoCreateView() +{ + if (!m_viewClassInfo) + return (wxView *) NULL; + + return (wxView *)m_viewClassInfo->CreateObject(); +} + // ---------------------------------------------------------------------------- // wxDocManager // ---------------------------------------------------------------------------- -- 2.7.4