cf20b310993aaa9c02d3cc6b856b58852c4d0571
[ctsim.git] / src / docs.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          doc.cpp
5 **   Purpose:       Document routines for CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: docs.cpp,v 1.15 2001/01/26 05:37:24 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #ifdef __GNUG__
29 // #pragma implementation
30 #endif
31
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/wx.h"
41 #endif
42 #include "wx/txtstrm.h"
43 #include "wx/file.h"
44
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
47 #endif
48
49 #include "ct.h"
50 #include "ctsim.h"
51 #include "docs.h"
52 #include "views.h"
53
54 // ImageFileDocument
55
56 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
57
58 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
59 {
60   if (! m_imageFile.fileWrite (filename)) {
61     *theApp->getLog() << "Unable to write image file " << filename << "\n";
62     return false;
63   }
64   *theApp->getLog() << "Wrote image file " << filename << "\n";
65   Modify(false);
66   return true;
67 }
68
69 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
70 {
71   if (! m_imageFile.fileRead (filename.c_str())) {
72     *theApp->getLog() << "Unable to read image file " << filename << "\n";
73     return false;
74   }
75   *theApp->getLog() << "Read image file " << filename << "\n";
76   SetFilename(filename, true);
77   
78   Modify(false);
79   UpdateAllViews();
80   GetFirstView()->OnUpdate (GetFirstView(), NULL);
81   
82   return true;
83 }
84
85 bool ImageFileDocument::IsModified(void) const
86 {
87   return wxDocument::IsModified();
88 }
89
90 void ImageFileDocument::Modify(bool mod)
91 {
92   wxDocument::Modify(mod);
93 }
94
95 ImageFileView* 
96 ImageFileDocument::getView() const
97
98   return dynamic_cast<ImageFileView*>(GetFirstView()); 
99 }
100
101 // ProjectionFileDocument
102
103 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
104
105 bool ProjectionFileDocument::OnSaveDocument(const wxString& filename)
106 {
107   if (! m_projectionFile.write (filename.c_str())) {
108     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
109     return false;
110   }
111   *theApp->getLog() << "Wrote projection file " << filename << "\n";
112   Modify(false);
113   return true;
114 }
115
116 bool ProjectionFileDocument::OnOpenDocument(const wxString& filename)
117 {
118   if (! m_projectionFile.read (filename.c_str())) {
119     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
120     return false;
121   }
122   *theApp->getLog() << "Read projection file " << filename << "\n";
123   SetFilename(filename, true);
124   Modify(false);
125   UpdateAllViews();
126   GetFirstView()->OnUpdate (GetFirstView(), NULL);
127   
128   return true;
129 }
130
131 bool ProjectionFileDocument::IsModified(void) const
132 {
133   return wxDocument::IsModified();
134 }
135
136 void ProjectionFileDocument::Modify(bool mod)
137 {
138   wxDocument::Modify(mod);
139 }
140
141
142 ProjectionFileView* 
143 ProjectionFileDocument::getView() const
144
145   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
146 }
147
148 // PhantomFileDocument
149
150 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
151
152 bool PhantomFileDocument::OnOpenDocument(const wxString& filename)
153 {
154   wxString myFilename = filename;
155   if (wxFile::Exists (myFilename)) {
156     m_phantom.createFromFile (myFilename);
157     *theApp->getLog() << "Read phantom file " << filename << "\n";
158   } else {
159     myFilename.Replace (".phm", "");
160     m_phantom.createFromPhantom (myFilename);
161   }
162   m_namePhantom = myFilename;
163   SetFilename (myFilename, true);
164   if (m_phantom.fail()) {
165     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
166     return false;
167   }
168   m_idPhantom = m_phantom.id();
169   Modify(false);
170   UpdateAllViews();
171   GetFirstView()->OnUpdate (GetFirstView(), NULL);
172   
173   return true;
174 }
175
176 bool PhantomFileDocument::OnSaveDocument(const wxString& filename)
177 {
178   if (! m_phantom.fileWrite (filename.c_str())) {
179     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
180     return false;
181   }
182   *theApp->getLog() << "Wrote phantom file " << filename << "\n";
183   Modify(false);
184   return true;
185 }
186
187 bool PhantomFileDocument::IsModified(void) const
188 {
189   return false;
190 }
191
192 void PhantomFileDocument::Modify(bool mod)
193 {
194   wxDocument::Modify(mod);
195 }
196
197
198 PhantomFileView* 
199 PhantomFileDocument::getView() const
200
201   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
202 }
203
204 // PlotFileDocument
205
206 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
207
208 bool PlotFileDocument::OnSaveDocument(const wxString& filename)
209 {
210   m_namePlot = filename.c_str();
211   if (! m_plot.fileWrite (filename)) {
212     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
213     return false;
214   }
215   *theApp->getLog() << "Wrote plot file " << filename << "\n";
216   Modify(false);
217   return true;
218 }
219
220 bool PlotFileDocument::OnOpenDocument(const wxString& filename)
221 {
222   if (! m_plot.fileRead (filename.c_str())) {
223     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
224     return false;
225   }
226   *theApp->getLog() << "Read plot file " << filename << "\n";
227   SetFilename (filename, true);
228   m_namePlot = filename.c_str();
229   Modify (false);
230   UpdateAllViews();
231   GetFirstView()->OnUpdate (NULL, NULL);
232   
233   return true;
234 }
235
236
237 bool PlotFileDocument::IsModified(void) const
238 {
239   return wxDocument::IsModified();
240 }
241
242 void PlotFileDocument::Modify(bool mod)
243 {
244   wxDocument::Modify(mod);
245 }
246
247 PlotFileView* 
248 PlotFileDocument::getView() const
249
250   return dynamic_cast<PlotFileView*>(GetFirstView()); 
251 }
252
253 //////////////////////////////////////////////////////////////////////////
254 //
255 // TextFileDocument
256 //
257 //////////////////////////////////////////////////////////////////////////
258
259 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
260
261 // Since text windows have their own method for saving to/loading from files,
262 // we override OnSave/OpenDocument instead of Save/LoadObject
263 bool TextFileDocument::OnSaveDocument(const wxString& filename)
264 {
265   TextFileView *view = getView();
266   if (! view->getTextCtrl()->SaveFile(filename))
267     return false;
268   Modify(false);
269   return true;
270 }
271
272 bool TextFileDocument::OnOpenDocument(const wxString& filename)
273 {
274   TextFileView *view = getView();
275   
276   if (! view->getTextCtrl()->LoadFile(filename))
277     return false;
278   
279   SetFilename (filename, true);
280   Modify (false);
281   UpdateAllViews();
282   return true;
283 }
284
285 bool TextFileDocument::IsModified(void) const
286 {
287   return false;
288   
289   TextFileView *view = getView();
290   
291   if (view)
292     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
293   else
294     return wxDocument::IsModified();
295 }
296
297
298 TextFileView* 
299 TextFileDocument::getView() const
300
301   return dynamic_cast<TextFileView*>(GetFirstView()); 
302 }