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.41 2002/06/04 19:19:40 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& filename)
228 if (! OnSaveModified())
231 if (! m_pProjectionFile->read (filename.c_str())) {
232 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
233 m_bBadFileOpen = true;
236 if (theApp->getVerboseLogging())
237 *theApp->getLog() << "Read projection file " << filename << "\n";
238 SetFilename(filename, true);
240 getView()->setInitialClientSize();
242 m_bBadFileOpen = false;
248 ProjectionFileDocument::IsModified(void) const
250 return wxDocument::IsModified();
254 ProjectionFileDocument::Modify(bool mod)
256 wxDocument::Modify(mod);
261 ProjectionFileDocument::getView() const
263 return dynamic_cast<ProjectionFileView*>(GetFirstView());
267 ProjectionFileDocument::Activate()
270 getView()->getFrame()->Activate();
274 // PhantomFileDocument
276 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
278 PhantomFileDocument::~PhantomFileDocument()
280 cancelRunningTasks();
284 PhantomFileDocument::OnOpenDocument(const wxString& filename)
286 if (! OnSaveModified())
289 wxString myFilename = filename;
290 if (wxFile::Exists (myFilename)) {
291 m_phantom.createFromFile (myFilename);
292 if (theApp->getVerboseLogging())
293 *theApp->getLog() << "Read phantom file " << filename << "\n";
295 myFilename.Replace (".phm", "");
296 m_phantom.createFromPhantom (myFilename);
298 m_namePhantom = myFilename;
299 SetFilename (myFilename, true);
300 if (m_phantom.fail()) {
301 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
302 m_bBadFileOpen = true;
305 m_idPhantom = m_phantom.id();
308 m_bBadFileOpen = false;
314 PhantomFileDocument::OnSaveDocument(const wxString& filename)
316 if (! m_phantom.fileWrite (filename.c_str())) {
317 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
320 if (theApp->getVerboseLogging())
321 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
327 PhantomFileDocument::IsModified(void) const
333 PhantomFileDocument::Modify(bool mod)
335 wxDocument::Modify(mod);
340 PhantomFileDocument::getView() const
342 return dynamic_cast<PhantomFileView*>(GetFirstView());
346 PhantomFileDocument::Activate()
349 getView()->getFrame()->Activate();
355 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
358 PlotFileDocument::OnSaveDocument(const wxString& filename)
360 m_namePlot = filename.c_str();
361 if (! m_plot.fileWrite (filename)) {
362 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
365 if (theApp->getVerboseLogging())
366 *theApp->getLog() << "Wrote plot file " << filename << "\n";
372 PlotFileDocument::OnOpenDocument(const wxString& filename)
374 if (! OnSaveModified())
377 if (! m_plot.fileRead (filename.c_str())) {
378 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
379 m_bBadFileOpen = true;
382 if (theApp->getVerboseLogging())
383 *theApp->getLog() << "Read plot file " << filename << "\n";
384 SetFilename (filename, true);
385 m_namePlot = filename.c_str();
388 //GetFirstView()->OnUpdate (NULL, NULL);
389 m_bBadFileOpen = false;
396 PlotFileDocument::IsModified(void) const
398 return wxDocument::IsModified();
402 PlotFileDocument::Modify (bool mod)
404 wxDocument::Modify(mod);
408 PlotFileDocument::getView() const
410 return dynamic_cast<PlotFileView*>(GetFirstView());
414 PlotFileDocument::Activate()
417 getView()->getFrame()->Activate();
421 //////////////////////////////////////////////////////////////////////////
425 //////////////////////////////////////////////////////////////////////////
427 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
430 TextFileDocument::OnSaveDocument(const wxString& filename)
432 TextFileView *view = getView();
433 if (! view->getTextCtrl()->SaveFile(filename))
440 TextFileDocument::OnOpenDocument(const wxString& filename)
442 TextFileView *view = getView();
444 if (! view->getTextCtrl()->LoadFile(filename)) {
445 m_bBadFileOpen = true;
449 SetFilename (filename, true);
452 m_bBadFileOpen = false;
457 TextFileDocument::IsModified(void) const
461 TextFileView *view = getView();
464 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
466 return wxDocument::IsModified();
471 TextFileDocument::getView() const
473 return dynamic_cast<TextFileView*>(GetFirstView());
477 TextFileDocument::getTextCtrl()
479 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
482 //////////////////////////////////////////////////////////////////////////
484 // Graph3dFileDocument
486 //////////////////////////////////////////////////////////////////////////
490 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
492 Graph3dFileDocument::Graph3dFileDocument(void)
493 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
497 Graph3dFileDocument::~Graph3dFileDocument()
502 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
509 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
511 SetFilename (filename, true);
514 m_bBadFileOpen = false;
519 Graph3dFileDocument::IsModified(void) const
521 return wxDocument::IsModified();
526 Graph3dFileDocument::getView() const
528 return dynamic_cast<Graph3dFileView*>(GetFirstView());
532 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
534 m_nx = rImageFile.nx();
535 m_ny = rImageFile.ny();
536 m_array = rImageFile.getArray();
542 Graph3dFileDocument::Activate()
545 getView()->getFrame()->Activate();
550 #endif // wxUSE_GLCANVAS