1 /*****************************************************************************
5 ** Purpose: Document routines for CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: docs.cpp,v 1.18 2001/01/30 05:05:41 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 // #pragma implementation
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
42 #include "wx/txtstrm.h"
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
56 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
58 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
60 if (! m_pImageFile->fileWrite (filename)) {
61 *theApp->getLog() << "Unable to write image file " << filename << "\n";
64 *theApp->getLog() << "Wrote image file " << filename << "\n";
69 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
71 if (! OnSaveModified())
74 if (! m_pImageFile->fileRead (filename.c_str())) {
75 *theApp->getLog() << "Unable to read image file " << filename << "\n";
76 m_bBadFileOpen = true;
79 *theApp->getLog() << "Read image file " << filename << "\n";
80 SetFilename(filename, true);
83 getView()->OnUpdate (getView(), NULL);
84 m_bBadFileOpen = false;
90 ImageFileDocument::IsModified(void) const
92 return wxDocument::IsModified();
96 ImageFileDocument::Modify(bool mod)
98 wxDocument::Modify(mod);
102 ImageFileDocument::getView() const
104 return dynamic_cast<ImageFileView*>(GetFirstView());
109 ImageFileDocument::Revert ()
112 wxString msg ("Revert to saved ");
113 msg += GetFilename();
115 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
116 if (dialog.ShowModal() == wxID_YES) {
117 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
119 OnOpenDocument (GetFilename());
122 getView()->OnUpdate (getView(), NULL);
128 // ProjectionFileDocument
130 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
132 bool ProjectionFileDocument::OnSaveDocument(const wxString& filename)
134 if (! m_pProjectionFile->write (filename.c_str())) {
135 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
138 *theApp->getLog() << "Wrote projection file " << filename << "\n";
143 bool ProjectionFileDocument::OnOpenDocument(const wxString& filename)
145 if (! OnSaveModified())
148 if (! m_pProjectionFile->read (filename.c_str())) {
149 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
150 m_bBadFileOpen = true;
153 *theApp->getLog() << "Read projection file " << filename << "\n";
154 SetFilename(filename, true);
157 GetFirstView()->OnUpdate (GetFirstView(), NULL);
158 m_bBadFileOpen = false;
163 bool ProjectionFileDocument::IsModified(void) const
165 return wxDocument::IsModified();
168 void ProjectionFileDocument::Modify(bool mod)
170 wxDocument::Modify(mod);
175 ProjectionFileDocument::getView() const
177 return dynamic_cast<ProjectionFileView*>(GetFirstView());
180 // PhantomFileDocument
182 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
184 bool PhantomFileDocument::OnOpenDocument(const wxString& filename)
186 if (! OnSaveModified())
189 wxString myFilename = filename;
190 if (wxFile::Exists (myFilename)) {
191 m_phantom.createFromFile (myFilename);
192 *theApp->getLog() << "Read phantom file " << filename << "\n";
194 myFilename.Replace (".phm", "");
195 m_phantom.createFromPhantom (myFilename);
197 m_namePhantom = myFilename;
198 SetFilename (myFilename, true);
199 if (m_phantom.fail()) {
200 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
201 m_bBadFileOpen = true;
204 m_idPhantom = m_phantom.id();
207 GetFirstView()->OnUpdate (GetFirstView(), NULL);
208 m_bBadFileOpen = false;
213 bool PhantomFileDocument::OnSaveDocument(const wxString& filename)
215 if (! m_phantom.fileWrite (filename.c_str())) {
216 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
219 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
224 bool PhantomFileDocument::IsModified(void) const
229 void PhantomFileDocument::Modify(bool mod)
231 wxDocument::Modify(mod);
236 PhantomFileDocument::getView() const
238 return dynamic_cast<PhantomFileView*>(GetFirstView());
243 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
245 bool PlotFileDocument::OnSaveDocument(const wxString& filename)
247 m_namePlot = filename.c_str();
248 if (! m_plot.fileWrite (filename)) {
249 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
252 *theApp->getLog() << "Wrote plot file " << filename << "\n";
257 bool PlotFileDocument::OnOpenDocument(const wxString& filename)
259 if (! OnSaveModified())
262 if (! m_plot.fileRead (filename.c_str())) {
263 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
264 m_bBadFileOpen = true;
267 *theApp->getLog() << "Read plot file " << filename << "\n";
268 SetFilename (filename, true);
269 m_namePlot = filename.c_str();
272 GetFirstView()->OnUpdate (NULL, NULL);
273 m_bBadFileOpen = false;
279 bool PlotFileDocument::IsModified(void) const
281 return wxDocument::IsModified();
284 void PlotFileDocument::Modify(bool mod)
286 wxDocument::Modify(mod);
290 PlotFileDocument::getView() const
292 return dynamic_cast<PlotFileView*>(GetFirstView());
295 //////////////////////////////////////////////////////////////////////////
299 //////////////////////////////////////////////////////////////////////////
301 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
303 // Since text windows have their own method for saving to/loading from files,
304 // we override OnSave/OpenDocument instead of Save/LoadObject
305 bool TextFileDocument::OnSaveDocument(const wxString& filename)
307 TextFileView *view = getView();
308 if (! view->getTextCtrl()->SaveFile(filename))
314 bool TextFileDocument::OnOpenDocument(const wxString& filename)
316 TextFileView *view = getView();
318 if (! view->getTextCtrl()->LoadFile(filename)) {
319 m_bBadFileOpen = true;
323 SetFilename (filename, true);
326 m_bBadFileOpen = false;
330 bool TextFileDocument::IsModified(void) const
334 TextFileView *view = getView();
337 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
339 return wxDocument::IsModified();
344 TextFileDocument::getView() const
346 return dynamic_cast<TextFileView*>(GetFirstView());
350 TextFileDocument::getTextCtrl()
352 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();