03ad1138293dd42647bb123eedab51c0f4bac16c
[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.18 2001/01/30 05:05:41 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_pImageFile->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_pImageFile->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   getView()->OnUpdate (getView(), NULL);
84   m_bBadFileOpen = false;
85
86   return true;
87 }
88
89 bool 
90 ImageFileDocument::IsModified(void) const
91 {
92   return wxDocument::IsModified();
93 }
94
95 void 
96 ImageFileDocument::Modify(bool mod)
97 {
98   wxDocument::Modify(mod);
99 }
100
101 ImageFileView* 
102 ImageFileDocument::getView() const
103
104   return dynamic_cast<ImageFileView*>(GetFirstView()); 
105 }
106
107
108 bool
109 ImageFileDocument::Revert ()
110 {
111   if (IsModified()) {
112     wxString msg ("Revert to saved ");
113     msg += GetFilename();
114     msg += "?";
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";
118       Modify(false);
119       OnOpenDocument (GetFilename());
120     }
121   }
122   getView()->OnUpdate (getView(), NULL);
123   UpdateAllViews();
124
125   return true;
126 }
127
128 // ProjectionFileDocument
129
130 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
131
132 bool ProjectionFileDocument::OnSaveDocument(const wxString& filename)
133 {
134   if (! m_pProjectionFile->write (filename.c_str())) {
135     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
136     return false;
137   }
138   *theApp->getLog() << "Wrote projection file " << filename << "\n";
139   Modify(false);
140   return true;
141 }
142
143 bool ProjectionFileDocument::OnOpenDocument(const wxString& filename)
144 {
145   if (! OnSaveModified())
146     return false;
147
148   if (! m_pProjectionFile->read (filename.c_str())) {
149     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
150     m_bBadFileOpen = true;
151     return false;
152   }
153   *theApp->getLog() << "Read projection file " << filename << "\n";
154   SetFilename(filename, true);
155   Modify(false);
156   UpdateAllViews();
157   GetFirstView()->OnUpdate (GetFirstView(), NULL);
158   m_bBadFileOpen = false;
159   
160   return true;
161 }
162
163 bool ProjectionFileDocument::IsModified(void) const
164 {
165   return wxDocument::IsModified();
166 }
167
168 void ProjectionFileDocument::Modify(bool mod)
169 {
170   wxDocument::Modify(mod);
171 }
172
173
174 ProjectionFileView* 
175 ProjectionFileDocument::getView() const
176
177   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
178 }
179
180 // PhantomFileDocument
181
182 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
183
184 bool PhantomFileDocument::OnOpenDocument(const wxString& filename)
185 {
186   if (! OnSaveModified())
187     return false;
188
189   wxString myFilename = filename;
190   if (wxFile::Exists (myFilename)) {
191     m_phantom.createFromFile (myFilename);
192     *theApp->getLog() << "Read phantom file " << filename << "\n";
193   } else {
194     myFilename.Replace (".phm", "");
195     m_phantom.createFromPhantom (myFilename);
196   }
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;
202     return false;
203   }
204   m_idPhantom = m_phantom.id();
205   Modify(false);
206   UpdateAllViews();
207   GetFirstView()->OnUpdate (GetFirstView(), NULL);
208   m_bBadFileOpen = false;
209   
210   return true;
211 }
212
213 bool PhantomFileDocument::OnSaveDocument(const wxString& filename)
214 {
215   if (! m_phantom.fileWrite (filename.c_str())) {
216     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
217     return false;
218   }
219   *theApp->getLog() << "Wrote phantom file " << filename << "\n";
220   Modify(false);
221   return true;
222 }
223
224 bool PhantomFileDocument::IsModified(void) const
225 {
226   return false;
227 }
228
229 void PhantomFileDocument::Modify(bool mod)
230 {
231   wxDocument::Modify(mod);
232 }
233
234
235 PhantomFileView* 
236 PhantomFileDocument::getView() const
237
238   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
239 }
240
241 // PlotFileDocument
242
243 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
244
245 bool PlotFileDocument::OnSaveDocument(const wxString& filename)
246 {
247   m_namePlot = filename.c_str();
248   if (! m_plot.fileWrite (filename)) {
249     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
250     return false;
251   }
252   *theApp->getLog() << "Wrote plot file " << filename << "\n";
253   Modify(false);
254   return true;
255 }
256
257 bool PlotFileDocument::OnOpenDocument(const wxString& filename)
258 {
259   if (! OnSaveModified())
260     return false;
261
262   if (! m_plot.fileRead (filename.c_str())) {
263     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
264     m_bBadFileOpen = true;
265     return false;
266   }
267   *theApp->getLog() << "Read plot file " << filename << "\n";
268   SetFilename (filename, true);
269   m_namePlot = filename.c_str();
270   Modify (false);
271   UpdateAllViews();
272   GetFirstView()->OnUpdate (NULL, NULL);
273   m_bBadFileOpen = false;
274   
275   return true;
276 }
277
278
279 bool PlotFileDocument::IsModified(void) const
280 {
281   return wxDocument::IsModified();
282 }
283
284 void PlotFileDocument::Modify(bool mod)
285 {
286   wxDocument::Modify(mod);
287 }
288
289 PlotFileView* 
290 PlotFileDocument::getView() const
291
292   return dynamic_cast<PlotFileView*>(GetFirstView()); 
293 }
294
295 //////////////////////////////////////////////////////////////////////////
296 //
297 // TextFileDocument
298 //
299 //////////////////////////////////////////////////////////////////////////
300
301 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
302
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)
306 {
307   TextFileView *view = getView();
308   if (! view->getTextCtrl()->SaveFile(filename))
309     return false;
310   Modify(false);
311   return true;
312 }
313
314 bool TextFileDocument::OnOpenDocument(const wxString& filename)
315 {
316   TextFileView *view = getView();
317   
318   if (! view->getTextCtrl()->LoadFile(filename)) {
319     m_bBadFileOpen = true;
320     return false;
321   }
322   
323   SetFilename (filename, true);
324   Modify (false);
325   UpdateAllViews();
326   m_bBadFileOpen = false;
327   return true;
328 }
329
330 bool TextFileDocument::IsModified(void) const
331 {
332   return false;
333   
334   TextFileView *view = getView();
335   
336   if (view)
337     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
338   else
339     return wxDocument::IsModified();
340 }
341
342
343 TextFileView* 
344 TextFileDocument::getView() const
345
346   return dynamic_cast<TextFileView*>(GetFirstView()); 
347 }
348
349 wxTextCtrl* 
350 TextFileDocument::getTextCtrl()
351
352   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
353 }
354