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
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 ******************************************************************************/
28 #include "wx/wxprec.h"
33 #include "wx/txtstrm.h"
35 #include "wx/thread.h"
37 #if !wxUSE_DOC_VIEW_ARCHITECTURE
38 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
45 #include "threadrecon.h"
50 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
52 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
54 if (! m_pImageFile->fileWrite (filename)) {
55 *theApp->getLog() << "Unable to write image file " << filename << "\n";
58 if (theApp->getVerboseLogging())
59 *theApp->getLog() << "Wrote image file " << filename << "\n";
64 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
66 if (! OnSaveModified())
69 if (! m_pImageFile->fileRead (filename.c_str())) {
70 *theApp->getLog() << "Unable to read image file " << filename << "\n";
71 m_bBadFileOpen = true;
75 if (theApp->getVerboseLogging() && filename != "")
76 *theApp->getLog() << "Read image file " << filename << "\n";
78 SetFilename(filename, true);
80 getView()->setInitialClientSize();
82 m_bBadFileOpen = false;
88 ImageFileDocument::IsModified(void) const
90 return wxDocument::IsModified();
94 ImageFileDocument::Modify(bool mod)
96 wxDocument::Modify(mod);
100 ImageFileDocument::getView() const
102 return dynamic_cast<ImageFileView*>(GetFirstView());
106 ImageFileDocument::Revert ()
109 wxString msg ("Revert to saved ");
110 msg += GetFilename();
112 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
113 if (dialog.ShowModal() == wxID_YES) {
114 if (theApp->getVerboseLogging())
115 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
117 OnOpenDocument (GetFilename());
126 ImageFileDocument::Activate()
129 getView()->getFrame()->Activate();
133 // BackgroundProcessingDocument - Base Class
135 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
136 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
139 #ifdef HAVE_WXTHREADS
141 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
143 wxCriticalSectionLocker locker (m_criticalSection);
145 m_vecpBackgroundSupervisors.push_back (pSupervisor);
149 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
151 m_criticalSection.Enter();
153 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
154 i != m_vecpBackgroundSupervisors.end();
156 if (*i == pSupervisor) {
157 m_vecpBackgroundSupervisors.erase(i);
161 m_criticalSection.Leave();
164 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
169 BackgroundProcessingDocument::cancelRunningTasks()
171 #ifdef HAVE_WXTHREADS
172 m_criticalSection.Enter();
173 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
174 i != m_vecpBackgroundSupervisors.end(); i++)
176 m_criticalSection.Leave();
178 while (m_vecpBackgroundSupervisors.size() > 0) {
186 // ProjectionFileDocument
188 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingDocument)
191 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
193 if (! m_pProjectionFile->write (filename.c_str())) {
194 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
197 if (theApp->getVerboseLogging())
198 *theApp->getLog() << "Wrote projection file " << filename << "\n";
203 ProjectionFileDocument::~ProjectionFileDocument()
205 cancelRunningTasks();
207 delete m_pProjectionFile;
211 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
213 if (! OnSaveModified())
216 if (! m_pProjectionFile->read (filename.c_str())) {
217 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
218 m_bBadFileOpen = true;
221 m_bBadFileOpen = false;
223 if (theApp->getVerboseLogging() && filename != "")
224 *theApp->getLog() << "Read projection file " << filename << "\n";
226 SetFilename(filename, true);
228 getView()->setInitialClientSize();
235 ProjectionFileDocument::IsModified(void) const
237 return wxDocument::IsModified();
241 ProjectionFileDocument::Modify(bool mod)
243 wxDocument::Modify(mod);
248 ProjectionFileDocument::getView() const
250 return dynamic_cast<ProjectionFileView*>(GetFirstView());
254 ProjectionFileDocument::Activate()
257 getView()->getFrame()->Activate();
261 // PhantomFileDocument
263 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingDocument)
265 PhantomFileDocument::~PhantomFileDocument()
267 cancelRunningTasks();
271 PhantomFileDocument::OnOpenDocument(const wxString& constFilename)
273 if (! OnSaveModified())
276 wxString filename (constFilename);
278 if (wxFile::Exists (filename)) {
279 m_phantom.createFromFile (filename);
280 if (theApp->getVerboseLogging())
281 *theApp->getLog() << "Read phantom file " << filename << "\n";
283 filename.Replace (".phm", "");
284 m_phantom.createFromPhantom (filename);
286 m_namePhantom = filename;
287 SetFilename (filename, true);
288 if (m_phantom.fail()) {
289 *theApp->getLog() << "Failure creating phantom " << filename << "\n";
290 m_bBadFileOpen = true;
293 m_idPhantom = m_phantom.id();
296 m_bBadFileOpen = false;
302 PhantomFileDocument::OnSaveDocument(const wxString& filename)
304 if (! m_phantom.fileWrite (filename.c_str())) {
305 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
308 if (theApp->getVerboseLogging())
309 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
315 PhantomFileDocument::IsModified(void) const
321 PhantomFileDocument::Modify(bool mod)
323 wxDocument::Modify(mod);
328 PhantomFileDocument::getView() const
330 return dynamic_cast<PhantomFileView*>(GetFirstView());
334 PhantomFileDocument::Activate()
337 getView()->getFrame()->Activate();
343 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
346 PlotFileDocument::OnSaveDocument(const wxString& filename)
348 m_namePlot = filename.c_str();
349 if (! m_plot.fileWrite (filename)) {
350 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
353 if (theApp->getVerboseLogging())
354 *theApp->getLog() << "Wrote plot file " << filename << "\n";
360 PlotFileDocument::OnOpenDocument(const wxString& filename)
362 if (! OnSaveModified())
365 if (! m_plot.fileRead (filename.c_str())) {
366 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
367 m_bBadFileOpen = true;
370 m_bBadFileOpen = false;
372 if (theApp->getVerboseLogging() && filename != "")
373 *theApp->getLog() << "Read plot file " << filename << "\n";
375 SetFilename (filename, true);
376 m_namePlot = filename.c_str();
378 getView()->setInitialClientSize();
386 PlotFileDocument::IsModified(void) const
388 return wxDocument::IsModified();
392 PlotFileDocument::Modify (bool mod)
394 wxDocument::Modify(mod);
398 PlotFileDocument::getView() const
400 return dynamic_cast<PlotFileView*>(GetFirstView());
404 PlotFileDocument::Activate()
407 getView()->getFrame()->Activate();
411 //////////////////////////////////////////////////////////////////////////
415 //////////////////////////////////////////////////////////////////////////
417 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
420 TextFileDocument::OnSaveDocument(const wxString& filename)
422 TextFileView *view = getView();
423 if (! view->getTextCtrl()->SaveFile(filename))
430 TextFileDocument::OnOpenDocument(const wxString& filename)
432 TextFileView *view = getView();
434 if (! view->getTextCtrl()->LoadFile(filename)) {
435 m_bBadFileOpen = true;
439 SetFilename (filename, true);
442 m_bBadFileOpen = false;
447 TextFileDocument::IsModified(void) const
451 TextFileView *view = getView();
454 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
456 return wxDocument::IsModified();
461 TextFileDocument::getView() const
463 return dynamic_cast<TextFileView*>(GetFirstView());
467 TextFileDocument::getTextCtrl()
469 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
472 //////////////////////////////////////////////////////////////////////////
474 // Graph3dFileDocument
476 //////////////////////////////////////////////////////////////////////////
480 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
482 Graph3dFileDocument::Graph3dFileDocument(void)
483 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
487 Graph3dFileDocument::~Graph3dFileDocument()
492 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
499 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
501 SetFilename (filename, true);
503 getView()->setInitialClientSize();
505 m_bBadFileOpen = false;
510 Graph3dFileDocument::IsModified(void) const
512 return wxDocument::IsModified();
517 Graph3dFileDocument::getView() const
519 return dynamic_cast<Graph3dFileView*>(GetFirstView());
523 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
525 m_nx = rImageFile.nx();
526 m_ny = rImageFile.ny();
527 m_array = rImageFile.getArray();
533 Graph3dFileDocument::Activate()
536 getView()->getFrame()->Activate();
541 #endif // wxUSE_GLCANVAS