r571: 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.25 2001/02/22 11:05:38 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 #include "wx/wxprec.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37 #include "wx/txtstrm.h"
38 #include "wx/file.h"
39 #include "wx/thread.h"
40
41 #if !wxUSE_DOC_VIEW_ARCHITECTURE
42 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
43 #endif
44
45 #include "ct.h"
46 #include "ctsim.h"
47 #include "docs.h"
48 #include "views.h"
49 #include "threadrecon.h"
50
51
52 // ImageFileDocument
53
54 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
55
56 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
57 {
58   if (! m_pImageFile->fileWrite (filename)) {
59     *theApp->getLog() << "Unable to write image file " << filename << "\n";
60     return false;
61   }
62   if (theApp->getVerboseLogging())
63     *theApp->getLog() << "Wrote image file " << filename << "\n";
64   Modify(false);
65   return true;
66 }
67
68 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
69 {
70   if (! OnSaveModified())
71     return false;
72
73   if (! m_pImageFile->fileRead (filename.c_str())) {
74     *theApp->getLog() << "Unable to read image file " << filename << "\n";
75     m_bBadFileOpen = true;
76     return false;
77   }
78   if (theApp->getVerboseLogging())
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       if (theApp->getVerboseLogging())
118         *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
119       Modify (false);
120       OnOpenDocument (GetFilename());
121     }
122   }
123   getView()->OnUpdate (getView(), NULL);
124   UpdateAllViews();
125
126   return true;
127 }
128
129 // ProjectionFileDocument
130
131 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
132
133 bool 
134 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
135 {
136   if (! m_pProjectionFile->write (filename.c_str())) {
137     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
138     return false;
139   }
140   if (theApp->getVerboseLogging())
141     *theApp->getLog() << "Wrote projection file " << filename << "\n";
142   Modify(false);
143   return true;
144 }
145
146 ProjectionFileDocument::~ProjectionFileDocument()
147 {
148   for (int i = 0; i < m_vecpActiveReconstructors.size(); i++) {
149     ThreadedReconstructor* pThreadedReconstructor = m_vecpActiveReconstructors[i];
150     if (pThreadedReconstructor) {
151       pThreadedReconstructor->cancel();
152     }
153   }
154
155     delete m_pProjectionFile;
156 }
157
158 void
159 ProjectionFileDocument::addReconstructor (ThreadedReconstructor* pRecon)
160 {
161   wxCriticalSectionLocker locker (m_criticalSection);
162   m_vecpActiveReconstructors.push_back (pRecon);
163 }
164
165 void
166 ProjectionFileDocument::removeReconstructor (ThreadedReconstructor* pRecon)
167 {
168   wxCriticalSectionLocker locker (m_criticalSection);
169   for (int i = 0; i < m_vecpActiveReconstructors.size(); i++) {
170     if (m_vecpActiveReconstructors[i] == pRecon)
171       m_vecpActiveReconstructors[i] = NULL;
172   }
173 }
174
175 bool 
176 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
177 {
178   if (! OnSaveModified())
179     return false;
180
181   if (! m_pProjectionFile->read (filename.c_str())) {
182     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
183     m_bBadFileOpen = true;
184     return false;
185   }
186   if (theApp->getVerboseLogging())
187     *theApp->getLog() << "Read projection file " << filename << "\n";
188   SetFilename(filename, true);
189   Modify(false);
190   UpdateAllViews();
191   GetFirstView()->OnUpdate (GetFirstView(), NULL);
192   m_bBadFileOpen = false;
193   
194   return true;
195 }
196
197 bool 
198 ProjectionFileDocument::IsModified(void) const
199 {
200   return wxDocument::IsModified();
201 }
202
203 void 
204 ProjectionFileDocument::Modify(bool mod)
205 {
206   wxDocument::Modify(mod);
207 }
208
209
210 ProjectionFileView* 
211 ProjectionFileDocument::getView() const
212
213   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
214 }
215
216 // PhantomFileDocument
217
218 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
219
220 bool 
221 PhantomFileDocument::OnOpenDocument(const wxString& filename)
222 {
223   if (! OnSaveModified())
224     return false;
225
226   wxString myFilename = filename;
227   if (wxFile::Exists (myFilename)) {
228     m_phantom.createFromFile (myFilename);
229     if (theApp->getVerboseLogging())
230       *theApp->getLog() << "Read phantom file " << filename << "\n";
231   } else {
232     myFilename.Replace (".phm", "");
233     m_phantom.createFromPhantom (myFilename);
234   }
235   m_namePhantom = myFilename;
236   SetFilename (myFilename, true);
237   if (m_phantom.fail()) {
238     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
239     m_bBadFileOpen = true;
240     return false;
241   }
242   m_idPhantom = m_phantom.id();
243   Modify(false);
244   UpdateAllViews();
245   GetFirstView()->OnUpdate (GetFirstView(), NULL);
246   m_bBadFileOpen = false;
247   
248   return true;
249 }
250
251 bool 
252 PhantomFileDocument::OnSaveDocument(const wxString& filename)
253 {
254   if (! m_phantom.fileWrite (filename.c_str())) {
255     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
256     return false;
257   }
258   if (theApp->getVerboseLogging())
259     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
260   Modify(false);
261   return true;
262 }
263
264 bool 
265 PhantomFileDocument::IsModified(void) const
266 {
267   return false;
268 }
269
270 void 
271 PhantomFileDocument::Modify(bool mod)
272 {
273   wxDocument::Modify(mod);
274 }
275
276
277 PhantomFileView* 
278 PhantomFileDocument::getView() const
279
280   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
281 }
282
283 // PlotFileDocument
284
285 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
286
287 bool 
288 PlotFileDocument::OnSaveDocument(const wxString& filename)
289 {
290   m_namePlot = filename.c_str();
291   if (! m_plot.fileWrite (filename)) {
292     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
293     return false;
294   }
295   if (theApp->getVerboseLogging())
296     *theApp->getLog() << "Wrote plot file " << filename << "\n";
297   Modify(false);
298   return true;
299 }
300
301 bool 
302 PlotFileDocument::OnOpenDocument(const wxString& filename)
303 {
304   if (! OnSaveModified())
305     return false;
306
307   if (! m_plot.fileRead (filename.c_str())) {
308     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
309     m_bBadFileOpen = true;
310     return false;
311   }
312   if (theApp->getVerboseLogging())
313     *theApp->getLog() << "Read plot file " << filename << "\n";
314   SetFilename (filename, true);
315   m_namePlot = filename.c_str();
316   Modify (false);
317   UpdateAllViews();
318   GetFirstView()->OnUpdate (NULL, NULL);
319   m_bBadFileOpen = false;
320   
321   return true;
322 }
323
324
325 bool 
326 PlotFileDocument::IsModified(void) const
327 {
328   return wxDocument::IsModified();
329 }
330
331 void 
332 PlotFileDocument::Modify (bool mod)
333 {
334   wxDocument::Modify(mod);
335 }
336
337 PlotFileView* 
338 PlotFileDocument::getView() const
339
340   return dynamic_cast<PlotFileView*>(GetFirstView()); 
341 }
342
343 //////////////////////////////////////////////////////////////////////////
344 //
345 // TextFileDocument
346 //
347 //////////////////////////////////////////////////////////////////////////
348
349 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
350
351 bool 
352 TextFileDocument::OnSaveDocument(const wxString& filename)
353 {
354   TextFileView *view = getView();
355   if (! view->getTextCtrl()->SaveFile(filename))
356     return false;
357   Modify(false);
358   return true;
359 }
360
361 bool 
362 TextFileDocument::OnOpenDocument(const wxString& filename)
363 {
364   TextFileView *view = getView();
365   
366   if (! view->getTextCtrl()->LoadFile(filename)) {
367     m_bBadFileOpen = true;
368     return false;
369   }
370   
371   SetFilename (filename, true);
372   Modify (false);
373   UpdateAllViews();
374   m_bBadFileOpen = false;
375   return true;
376 }
377
378 bool 
379 TextFileDocument::IsModified(void) const
380 {
381   return false;
382   
383   TextFileView *view = getView();
384   
385   if (view)
386     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
387   else
388     return wxDocument::IsModified();
389 }
390
391
392 TextFileView* 
393 TextFileDocument::getView() const
394
395   return dynamic_cast<TextFileView*>(GetFirstView()); 
396 }
397
398 wxTextCtrl* 
399 TextFileDocument::getTextCtrl()
400
401   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
402 }
403
404 //////////////////////////////////////////////////////////////////////////
405 //
406 // Graph3dFileDocument
407 //
408 //////////////////////////////////////////////////////////////////////////
409
410 #if wxUSE_GLCANVAS
411
412 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
413
414 Graph3dFileDocument::Graph3dFileDocument(void) 
415 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
416 {
417 }
418
419 Graph3dFileDocument::~Graph3dFileDocument() 
420 {
421 //    delete [] m_pVertices;
422 //    delete [] m_pNormals;
423 }
424
425 bool 
426 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
427 {
428   Modify(false);
429   return true;
430 }
431
432 bool 
433 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
434 {
435   SetFilename (filename, true);
436   Modify (false);
437   UpdateAllViews();
438   m_bBadFileOpen = false;
439   return true;
440 }
441
442 bool 
443 Graph3dFileDocument::IsModified(void) const
444 {
445     return wxDocument::IsModified();
446 }
447
448
449 Graph3dFileView* 
450 Graph3dFileDocument::getView() const
451
452   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
453 }
454
455 bool
456 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
457 {
458 //  delete [] m_pVertices;
459 //  delete [] m_pNormals;
460
461
462   m_nx = rImageFile.nx();
463   m_ny = rImageFile.ny();
464   m_array = rImageFile.getArray();
465
466   return true;
467 }
468
469 #endif // wxUSE_GLCANVAS