1 /*****************************************************************************
5 ** Purpose: Document routines for CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: docs.cpp,v 1.42 2002/06/04 19:24:03 kevin Exp $
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.
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.
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 ******************************************************************************/
29 // #pragma implementation
32 #include "wx/wxprec.h"
37 #include "wx/txtstrm.h"
39 #include "wx/thread.h"
41 #if !wxUSE_DOC_VIEW_ARCHITECTURE
42 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
49 #include "threadrecon.h"
52 const char szNEW_IMAGE_FILENAME[] = "___CTSIM___INTERNAL___NEW___.if";
53 const char szNEW_PROJECTION_FILENAME[] = "___CTSIM___INTERNAL___NEW___.pj";
54 const char szNEW_GRAPH3D_FILENAME[] = "___CTSIM___INTERNAL___NEW___.3d";
55 const char szNEW_PLOT_FILENAME[] = "___CTSIM___INTERNAL___NEW___.plt";
60 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
62 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
64 if (! m_pImageFile->fileWrite (filename)) {
65 *theApp->getLog() << "Unable to write image file " << filename << "\n";
68 if (theApp->getVerboseLogging())
69 *theApp->getLog() << "Wrote image file " << filename << "\n";
74 bool ImageFileDocument::OnOpenDocument(const wxString& constFilename)
76 if (! OnSaveModified())
79 wxString filename (constFilename);
81 if (filename == szNEW_IMAGE_FILENAME)
83 else if (! m_pImageFile->fileRead (filename.c_str())) {
84 *theApp->getLog() << "Unable to read image file " << filename << "\n";
85 m_bBadFileOpen = true;
89 if (theApp->getVerboseLogging() && filename != "")
90 *theApp->getLog() << "Read image file " << filename << "\n";
92 SetFilename(filename, true);
94 getView()->setInitialClientSize();
96 m_bBadFileOpen = false;
102 ImageFileDocument::IsModified(void) const
104 return wxDocument::IsModified();
108 ImageFileDocument::Modify(bool mod)
110 wxDocument::Modify(mod);
114 ImageFileDocument::getView() const
116 return dynamic_cast<ImageFileView*>(GetFirstView());
120 ImageFileDocument::Revert ()
123 wxString msg ("Revert to saved ");
124 msg += GetFilename();
126 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
127 if (dialog.ShowModal() == wxID_YES) {
128 if (theApp->getVerboseLogging())
129 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
131 OnOpenDocument (GetFilename());
134 //getView()->OnUpdate (getView(), NULL);
141 ImageFileDocument::Activate()
144 getView()->getFrame()->Activate();
148 // BackgroundProcessingDocument - Base Class
150 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
151 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
154 #ifdef HAVE_WXTHREADS
156 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
158 wxCriticalSectionLocker locker (m_criticalSection);
160 m_vecpBackgroundSupervisors.push_back (pSupervisor);
164 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
166 m_criticalSection.Enter();
168 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
169 i != m_vecpBackgroundSupervisors.end();
171 if (*i == pSupervisor) {
172 m_vecpBackgroundSupervisors.erase(i);
176 m_criticalSection.Leave();
179 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
184 BackgroundProcessingDocument::cancelRunningTasks()
186 #ifdef HAVE_WXTHREADS
187 m_criticalSection.Enter();
188 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
189 i != m_vecpBackgroundSupervisors.end(); i++)
191 m_criticalSection.Leave();
193 while (m_vecpBackgroundSupervisors.size() > 0) {
201 // ProjectionFileDocument
203 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
206 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
208 if (! m_pProjectionFile->write (filename.c_str())) {
209 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
212 if (theApp->getVerboseLogging())
213 *theApp->getLog() << "Wrote projection file " << filename << "\n";
218 ProjectionFileDocument::~ProjectionFileDocument()
220 cancelRunningTasks();
222 delete m_pProjectionFile;
226 ProjectionFileDocument::OnOpenDocument(const wxString& constFilename)
228 if (! OnSaveModified())
231 wxString filename (constFilename);
233 if (filename == szNEW_PROJECTION_FILENAME)
235 else if (! m_pProjectionFile->read (filename.c_str())) {
236 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
237 m_bBadFileOpen = true;
241 if (theApp->getVerboseLogging() && filename != "")
242 *theApp->getLog() << "Read projection file " << filename << "\n";
244 SetFilename(filename, true);
246 getView()->setInitialClientSize();
248 m_bBadFileOpen = false;
254 ProjectionFileDocument::IsModified(void) const
256 return wxDocument::IsModified();
260 ProjectionFileDocument::Modify(bool mod)
262 wxDocument::Modify(mod);
267 ProjectionFileDocument::getView() const
269 return dynamic_cast<ProjectionFileView*>(GetFirstView());
273 ProjectionFileDocument::Activate()
276 getView()->getFrame()->Activate();
280 // PhantomFileDocument
282 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
284 PhantomFileDocument::~PhantomFileDocument()
286 cancelRunningTasks();
290 PhantomFileDocument::OnOpenDocument(const wxString& filename)
292 if (! OnSaveModified())
295 wxString myFilename = filename;
296 if (wxFile::Exists (myFilename)) {
297 m_phantom.createFromFile (myFilename);
298 if (theApp->getVerboseLogging())
299 *theApp->getLog() << "Read phantom file " << filename << "\n";
301 myFilename.Replace (".phm", "");
302 m_phantom.createFromPhantom (myFilename);
304 m_namePhantom = myFilename;
305 SetFilename (myFilename, true);
306 if (m_phantom.fail()) {
307 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
308 m_bBadFileOpen = true;
311 m_idPhantom = m_phantom.id();
314 m_bBadFileOpen = false;
320 PhantomFileDocument::OnSaveDocument(const wxString& filename)
322 if (! m_phantom.fileWrite (filename.c_str())) {
323 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
326 if (theApp->getVerboseLogging())
327 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
333 PhantomFileDocument::IsModified(void) const
339 PhantomFileDocument::Modify(bool mod)
341 wxDocument::Modify(mod);
346 PhantomFileDocument::getView() const
348 return dynamic_cast<PhantomFileView*>(GetFirstView());
352 PhantomFileDocument::Activate()
355 getView()->getFrame()->Activate();
361 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
364 PlotFileDocument::OnSaveDocument(const wxString& filename)
366 m_namePlot = filename.c_str();
367 if (! m_plot.fileWrite (filename)) {
368 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
371 if (theApp->getVerboseLogging())
372 *theApp->getLog() << "Wrote plot file " << filename << "\n";
378 PlotFileDocument::OnOpenDocument(const wxString& constFilename)
380 if (! OnSaveModified())
383 wxString filename (constFilename);
385 if (filename == szNEW_PLOT_FILENAME)
387 else if (! m_plot.fileRead (filename.c_str())) {
388 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
389 m_bBadFileOpen = true;
393 if (theApp->getVerboseLogging() && filename != "")
394 *theApp->getLog() << "Read plot file " << filename << "\n";
396 SetFilename (filename, true);
397 m_namePlot = filename.c_str();
400 m_bBadFileOpen = false;
407 PlotFileDocument::IsModified(void) const
409 return wxDocument::IsModified();
413 PlotFileDocument::Modify (bool mod)
415 wxDocument::Modify(mod);
419 PlotFileDocument::getView() const
421 return dynamic_cast<PlotFileView*>(GetFirstView());
425 PlotFileDocument::Activate()
428 getView()->getFrame()->Activate();
432 //////////////////////////////////////////////////////////////////////////
436 //////////////////////////////////////////////////////////////////////////
438 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
441 TextFileDocument::OnSaveDocument(const wxString& filename)
443 TextFileView *view = getView();
444 if (! view->getTextCtrl()->SaveFile(filename))
451 TextFileDocument::OnOpenDocument(const wxString& filename)
453 TextFileView *view = getView();
455 if (! view->getTextCtrl()->LoadFile(filename)) {
456 m_bBadFileOpen = true;
460 SetFilename (filename, true);
463 m_bBadFileOpen = false;
468 TextFileDocument::IsModified(void) const
472 TextFileView *view = getView();
475 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
477 return wxDocument::IsModified();
482 TextFileDocument::getView() const
484 return dynamic_cast<TextFileView*>(GetFirstView());
488 TextFileDocument::getTextCtrl()
490 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
493 //////////////////////////////////////////////////////////////////////////
495 // Graph3dFileDocument
497 //////////////////////////////////////////////////////////////////////////
501 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
503 Graph3dFileDocument::Graph3dFileDocument(void)
504 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
508 Graph3dFileDocument::~Graph3dFileDocument()
513 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
520 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
522 SetFilename (filename, true);
525 m_bBadFileOpen = false;
530 Graph3dFileDocument::IsModified(void) const
532 return wxDocument::IsModified();
537 Graph3dFileDocument::getView() const
539 return dynamic_cast<Graph3dFileView*>(GetFirstView());
543 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
545 m_nx = rImageFile.nx();
546 m_ny = rImageFile.ny();
547 m_array = rImageFile.getArray();
553 Graph3dFileDocument::Activate()
556 getView()->getFrame()->Activate();
561 #endif // wxUSE_GLCANVAS