r470: Fixed shutdown error, take 3
[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-2001 Kevin Rosenberg
11 **
12 **  $Id: docs.cpp,v 1.17 2001/01/30 02:20:50 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 (! OnSaveModified())
72     return false;
73
74   if (! m_imageFile.fileRead (filename.c_str())) {
75     *theApp->getLog() << "Unable to read image file " << filename << "\n";
76     m_bBadFileOpen = true;
77     return false;
78   }
79   *theApp->getLog() << "Read image file " << filename << "\n";
80   SetFilename(filename, true);  
81   Modify(false);
82   UpdateAllViews();
83   GetFirstView()->OnUpdate (GetFirstView(), NULL);
84   m_bBadFileOpen = false;
85
86   return true;
87 }
88
89 bool ImageFileDocument::IsModified(void) const
90 {
91   return wxDocument::IsModified();
92 }
93
94 void ImageFileDocument::Modify(bool mod)
95 {
96   wxDocument::Modify(mod);
97 }
98
99 ImageFileView* 
100 ImageFileDocument::getView() const
101
102   return dynamic_cast<ImageFileView*>(GetFirstView()); 
103 }
104
105 // ProjectionFileDocument
106
107 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
108
109 bool ProjectionFileDocument::OnSaveDocument(const wxString& filename)
110 {
111   if (! m_projectionFile.write (filename.c_str())) {
112     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
113     return false;
114   }
115   *theApp->getLog() << "Wrote projection file " << filename << "\n";
116   Modify(false);
117   return true;
118 }
119
120 bool ProjectionFileDocument::OnOpenDocument(const wxString& filename)
121 {
122   if (! OnSaveModified())
123     return false;
124
125   if (! m_projectionFile.read (filename.c_str())) {
126     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
127     m_bBadFileOpen = true;
128     return false;
129   }
130   *theApp->getLog() << "Read projection file " << filename << "\n";
131   SetFilename(filename, true);
132   Modify(false);
133   UpdateAllViews();
134   GetFirstView()->OnUpdate (GetFirstView(), NULL);
135   m_bBadFileOpen = false;
136   
137   return true;
138 }
139
140 bool ProjectionFileDocument::IsModified(void) const
141 {
142   return wxDocument::IsModified();
143 }
144
145 void ProjectionFileDocument::Modify(bool mod)
146 {
147   wxDocument::Modify(mod);
148 }
149
150
151 ProjectionFileView* 
152 ProjectionFileDocument::getView() const
153
154   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
155 }
156
157 // PhantomFileDocument
158
159 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
160
161 bool PhantomFileDocument::OnOpenDocument(const wxString& filename)
162 {
163   if (! OnSaveModified())
164     return false;
165
166   wxString myFilename = filename;
167   if (wxFile::Exists (myFilename)) {
168     m_phantom.createFromFile (myFilename);
169     *theApp->getLog() << "Read phantom file " << filename << "\n";
170   } else {
171     myFilename.Replace (".phm", "");
172     m_phantom.createFromPhantom (myFilename);
173   }
174   m_namePhantom = myFilename;
175   SetFilename (myFilename, true);
176   if (m_phantom.fail()) {
177     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
178     m_bBadFileOpen = true;
179     return false;
180   }
181   m_idPhantom = m_phantom.id();
182   Modify(false);
183   UpdateAllViews();
184   GetFirstView()->OnUpdate (GetFirstView(), NULL);
185   m_bBadFileOpen = false;
186   
187   return true;
188 }
189
190 bool PhantomFileDocument::OnSaveDocument(const wxString& filename)
191 {
192   if (! m_phantom.fileWrite (filename.c_str())) {
193     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
194     return false;
195   }
196   *theApp->getLog() << "Wrote phantom file " << filename << "\n";
197   Modify(false);
198   return true;
199 }
200
201 bool PhantomFileDocument::IsModified(void) const
202 {
203   return false;
204 }
205
206 void PhantomFileDocument::Modify(bool mod)
207 {
208   wxDocument::Modify(mod);
209 }
210
211
212 PhantomFileView* 
213 PhantomFileDocument::getView() const
214
215   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
216 }
217
218 // PlotFileDocument
219
220 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
221
222 bool PlotFileDocument::OnSaveDocument(const wxString& filename)
223 {
224   m_namePlot = filename.c_str();
225   if (! m_plot.fileWrite (filename)) {
226     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
227     return false;
228   }
229   *theApp->getLog() << "Wrote plot file " << filename << "\n";
230   Modify(false);
231   return true;
232 }
233
234 bool PlotFileDocument::OnOpenDocument(const wxString& filename)
235 {
236   if (! OnSaveModified())
237     return false;
238
239   if (! m_plot.fileRead (filename.c_str())) {
240     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
241     m_bBadFileOpen = true;
242     return false;
243   }
244   *theApp->getLog() << "Read plot file " << filename << "\n";
245   SetFilename (filename, true);
246   m_namePlot = filename.c_str();
247   Modify (false);
248   UpdateAllViews();
249   GetFirstView()->OnUpdate (NULL, NULL);
250   m_bBadFileOpen = false;
251   
252   return true;
253 }
254
255
256 bool PlotFileDocument::IsModified(void) const
257 {
258   return wxDocument::IsModified();
259 }
260
261 void PlotFileDocument::Modify(bool mod)
262 {
263   wxDocument::Modify(mod);
264 }
265
266 PlotFileView* 
267 PlotFileDocument::getView() const
268
269   return dynamic_cast<PlotFileView*>(GetFirstView()); 
270 }
271
272 //////////////////////////////////////////////////////////////////////////
273 //
274 // TextFileDocument
275 //
276 //////////////////////////////////////////////////////////////////////////
277
278 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
279
280 // Since text windows have their own method for saving to/loading from files,
281 // we override OnSave/OpenDocument instead of Save/LoadObject
282 bool TextFileDocument::OnSaveDocument(const wxString& filename)
283 {
284   TextFileView *view = getView();
285   if (! view->getTextCtrl()->SaveFile(filename))
286     return false;
287   Modify(false);
288   return true;
289 }
290
291 bool TextFileDocument::OnOpenDocument(const wxString& filename)
292 {
293   TextFileView *view = getView();
294   
295   if (! view->getTextCtrl()->LoadFile(filename)) {
296     m_bBadFileOpen = true;
297     return false;
298   }
299   
300   SetFilename (filename, true);
301   Modify (false);
302   UpdateAllViews();
303   m_bBadFileOpen = false;
304   return true;
305 }
306
307 bool TextFileDocument::IsModified(void) const
308 {
309   return false;
310   
311   TextFileView *view = getView();
312   
313   if (view)
314     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
315   else
316     return wxDocument::IsModified();
317 }
318
319
320 TextFileView* 
321 TextFileDocument::getView() const
322
323   return dynamic_cast<TextFileView*>(GetFirstView()); 
324 }
325
326 wxTextCtrl* 
327 TextFileDocument::getTextCtrl()
328
329   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
330 }
331