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