r473: no message
[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.20 2001/01/30 10:58:13 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 
133 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
134 {
135   if (! m_pProjectionFile->write (filename.c_str())) {
136     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
137     return false;
138   }
139   *theApp->getLog() << "Wrote projection file " << filename << "\n";
140   Modify(false);
141   return true;
142 }
143
144 bool 
145 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
146 {
147   if (! OnSaveModified())
148     return false;
149
150   if (! m_pProjectionFile->read (filename.c_str())) {
151     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
152     m_bBadFileOpen = true;
153     return false;
154   }
155   *theApp->getLog() << "Read projection file " << filename << "\n";
156   SetFilename(filename, true);
157   Modify(false);
158   UpdateAllViews();
159   GetFirstView()->OnUpdate (GetFirstView(), NULL);
160   m_bBadFileOpen = false;
161   
162   return true;
163 }
164
165 bool 
166 ProjectionFileDocument::IsModified(void) const
167 {
168   return wxDocument::IsModified();
169 }
170
171 void 
172 ProjectionFileDocument::Modify(bool mod)
173 {
174   wxDocument::Modify(mod);
175 }
176
177
178 ProjectionFileView* 
179 ProjectionFileDocument::getView() const
180
181   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
182 }
183
184 // PhantomFileDocument
185
186 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
187
188 bool 
189 PhantomFileDocument::OnOpenDocument(const wxString& filename)
190 {
191   if (! OnSaveModified())
192     return false;
193
194   wxString myFilename = filename;
195   if (wxFile::Exists (myFilename)) {
196     m_phantom.createFromFile (myFilename);
197     *theApp->getLog() << "Read phantom file " << filename << "\n";
198   } else {
199     myFilename.Replace (".phm", "");
200     m_phantom.createFromPhantom (myFilename);
201   }
202   m_namePhantom = myFilename;
203   SetFilename (myFilename, true);
204   if (m_phantom.fail()) {
205     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
206     m_bBadFileOpen = true;
207     return false;
208   }
209   m_idPhantom = m_phantom.id();
210   Modify(false);
211   UpdateAllViews();
212   GetFirstView()->OnUpdate (GetFirstView(), NULL);
213   m_bBadFileOpen = false;
214   
215   return true;
216 }
217
218 bool 
219 PhantomFileDocument::OnSaveDocument(const wxString& filename)
220 {
221   if (! m_phantom.fileWrite (filename.c_str())) {
222     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
223     return false;
224   }
225   *theApp->getLog() << "Wrote phantom file " << filename << "\n";
226   Modify(false);
227   return true;
228 }
229
230 bool 
231 PhantomFileDocument::IsModified(void) const
232 {
233   return false;
234 }
235
236 void 
237 PhantomFileDocument::Modify(bool mod)
238 {
239   wxDocument::Modify(mod);
240 }
241
242
243 PhantomFileView* 
244 PhantomFileDocument::getView() const
245
246   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
247 }
248
249 // PlotFileDocument
250
251 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
252
253 bool 
254 PlotFileDocument::OnSaveDocument(const wxString& filename)
255 {
256   m_namePlot = filename.c_str();
257   if (! m_plot.fileWrite (filename)) {
258     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
259     return false;
260   }
261   *theApp->getLog() << "Wrote plot file " << filename << "\n";
262   Modify(false);
263   return true;
264 }
265
266 bool 
267 PlotFileDocument::OnOpenDocument(const wxString& filename)
268 {
269   if (! OnSaveModified())
270     return false;
271
272   if (! m_plot.fileRead (filename.c_str())) {
273     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
274     m_bBadFileOpen = true;
275     return false;
276   }
277   *theApp->getLog() << "Read plot file " << filename << "\n";
278   SetFilename (filename, true);
279   m_namePlot = filename.c_str();
280   Modify (false);
281   UpdateAllViews();
282   GetFirstView()->OnUpdate (NULL, NULL);
283   m_bBadFileOpen = false;
284   
285   return true;
286 }
287
288
289 bool 
290 PlotFileDocument::IsModified(void) const
291 {
292   return wxDocument::IsModified();
293 }
294
295 void 
296 PlotFileDocument::Modify (bool mod)
297 {
298   wxDocument::Modify(mod);
299 }
300
301 PlotFileView* 
302 PlotFileDocument::getView() const
303
304   return dynamic_cast<PlotFileView*>(GetFirstView()); 
305 }
306
307 //////////////////////////////////////////////////////////////////////////
308 //
309 // TextFileDocument
310 //
311 //////////////////////////////////////////////////////////////////////////
312
313 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
314
315 bool 
316 TextFileDocument::OnSaveDocument(const wxString& filename)
317 {
318   TextFileView *view = getView();
319   if (! view->getTextCtrl()->SaveFile(filename))
320     return false;
321   Modify(false);
322   return true;
323 }
324
325 bool 
326 TextFileDocument::OnOpenDocument(const wxString& filename)
327 {
328   TextFileView *view = getView();
329   
330   if (! view->getTextCtrl()->LoadFile(filename)) {
331     m_bBadFileOpen = true;
332     return false;
333   }
334   
335   SetFilename (filename, true);
336   Modify (false);
337   UpdateAllViews();
338   m_bBadFileOpen = false;
339   return true;
340 }
341
342 bool 
343 TextFileDocument::IsModified(void) const
344 {
345   return false;
346   
347   TextFileView *view = getView();
348   
349   if (view)
350     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
351   else
352     return wxDocument::IsModified();
353 }
354
355
356 TextFileView* 
357 TextFileDocument::getView() const
358
359   return dynamic_cast<TextFileView*>(GetFirstView()); 
360 }
361
362 wxTextCtrl* 
363 TextFileDocument::getTextCtrl()
364
365   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
366 }
367
368 //////////////////////////////////////////////////////////////////////////
369 //
370 // Graph3dFileDocument
371 //
372 //////////////////////////////////////////////////////////////////////////
373
374 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
375
376 Graph3dFileDocument::Graph3dFileDocument(void) 
377 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0)
378 {
379 }
380
381 Graph3dFileDocument::~Graph3dFileDocument() 
382 {
383     delete [] m_pVertices;
384     delete [] m_pNormals;
385 }
386
387 bool 
388 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
389 {
390   Modify(false);
391   return true;
392 }
393
394 bool 
395 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
396 {
397   SetFilename (filename, true);
398   Modify (false);
399   UpdateAllViews();
400   m_bBadFileOpen = false;
401   return true;
402 }
403
404 bool 
405 Graph3dFileDocument::IsModified(void) const
406 {
407     return wxDocument::IsModified();
408 }
409
410
411 Graph3dFileView* 
412 Graph3dFileDocument::getView() const
413
414   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
415 }
416
417 bool
418 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
419 {
420   delete [] m_pVertices;
421   delete [] m_pNormals;
422
423   m_nx = rImageFile.nx();
424   m_ny = rImageFile.ny();
425   m_array = rImageFile.getArray();
426 #if 0
427   const int nTriangles = nx * ny;
428   m_nVertices = nTriangles;
429   m_pVertices = new glTripleFloat [nTriangles];
430   m_pNormals = new glTripleFloat [nTriangles];
431
432   for (unsigned int ix = 0; ix < nx; ix++) {
433     for (unsigned int iy = 0; iy < ny; iy++) {
434       const int iTriangle = ix * iy;
435       m_pVertices[iTriangle][0] = ix;
436       m_pVertices[iTriangle][1] = iy;
437       m_pVertices[iTriangle][2] = v[ix][iy];
438       m_pNormals[iTriangle][0] = 0;
439       m_pNormals[iTriangle][1] = 0;
440       m_pNormals[iTriangle][2] = 0;
441     }
442   }
443 #endif
444   return true;
445 }