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.43 2002/06/06 22:50:24 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"
54 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
56 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
58 if (! m_pImageFile->fileWrite (filename)) {
59 *theApp->getLog() << "Unable to write image file " << filename << "\n";
62 if (theApp->getVerboseLogging())
63 *theApp->getLog() << "Wrote image file " << filename << "\n";
68 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
70 if (! OnSaveModified())
73 if (! m_pImageFile->fileRead (filename.c_str())) {
74 *theApp->getLog() << "Unable to read image file " << filename << "\n";
75 m_bBadFileOpen = true;
79 if (theApp->getVerboseLogging() && filename != "")
80 *theApp->getLog() << "Read image file " << filename << "\n";
82 SetFilename(filename, true);
84 getView()->setInitialClientSize();
86 m_bBadFileOpen = false;
92 ImageFileDocument::IsModified(void) const
94 return wxDocument::IsModified();
98 ImageFileDocument::Modify(bool mod)
100 wxDocument::Modify(mod);
104 ImageFileDocument::getView() const
106 return dynamic_cast<ImageFileView*>(GetFirstView());
110 ImageFileDocument::Revert ()
113 wxString msg ("Revert to saved ");
114 msg += GetFilename();
116 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
117 if (dialog.ShowModal() == wxID_YES) {
118 if (theApp->getVerboseLogging())
119 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
121 OnOpenDocument (GetFilename());
130 ImageFileDocument::Activate()
133 getView()->getFrame()->Activate();
137 // BackgroundProcessingDocument - Base Class
139 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
140 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
143 #ifdef HAVE_WXTHREADS
145 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
147 wxCriticalSectionLocker locker (m_criticalSection);
149 m_vecpBackgroundSupervisors.push_back (pSupervisor);
153 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
155 m_criticalSection.Enter();
157 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
158 i != m_vecpBackgroundSupervisors.end();
160 if (*i == pSupervisor) {
161 m_vecpBackgroundSupervisors.erase(i);
165 m_criticalSection.Leave();
168 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
173 BackgroundProcessingDocument::cancelRunningTasks()
175 #ifdef HAVE_WXTHREADS
176 m_criticalSection.Enter();
177 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
178 i != m_vecpBackgroundSupervisors.end(); i++)
180 m_criticalSection.Leave();
182 while (m_vecpBackgroundSupervisors.size() > 0) {
190 // ProjectionFileDocument
192 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
195 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
197 if (! m_pProjectionFile->write (filename.c_str())) {
198 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
201 if (theApp->getVerboseLogging())
202 *theApp->getLog() << "Wrote projection file " << filename << "\n";
207 ProjectionFileDocument::~ProjectionFileDocument()
209 cancelRunningTasks();
211 delete m_pProjectionFile;
215 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
217 if (! OnSaveModified())
220 if (! m_pProjectionFile->read (filename.c_str())) {
221 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
222 m_bBadFileOpen = true;
225 m_bBadFileOpen = false;
227 if (theApp->getVerboseLogging() && filename != "")
228 *theApp->getLog() << "Read projection file " << filename << "\n";
230 SetFilename(filename, true);
232 getView()->setInitialClientSize();
239 ProjectionFileDocument::IsModified(void) const
241 return wxDocument::IsModified();
245 ProjectionFileDocument::Modify(bool mod)
247 wxDocument::Modify(mod);
252 ProjectionFileDocument::getView() const
254 return dynamic_cast<ProjectionFileView*>(GetFirstView());
258 ProjectionFileDocument::Activate()
261 getView()->getFrame()->Activate();
265 // PhantomFileDocument
267 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
269 PhantomFileDocument::~PhantomFileDocument()
271 cancelRunningTasks();
275 PhantomFileDocument::OnOpenDocument(const wxString& constFilename)
277 if (! OnSaveModified())
280 wxString filename (constFilename);
282 if (wxFile::Exists (filename)) {
283 m_phantom.createFromFile (filename);
284 if (theApp->getVerboseLogging())
285 *theApp->getLog() << "Read phantom file " << filename << "\n";
287 filename.Replace (".phm", "");
288 m_phantom.createFromPhantom (filename);
290 m_namePhantom = filename;
291 SetFilename (filename, true);
292 if (m_phantom.fail()) {
293 *theApp->getLog() << "Failure creating phantom " << filename << "\n";
294 m_bBadFileOpen = true;
297 m_idPhantom = m_phantom.id();
300 m_bBadFileOpen = false;
306 PhantomFileDocument::OnSaveDocument(const wxString& filename)
308 if (! m_phantom.fileWrite (filename.c_str())) {
309 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
312 if (theApp->getVerboseLogging())
313 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
319 PhantomFileDocument::IsModified(void) const
325 PhantomFileDocument::Modify(bool mod)
327 wxDocument::Modify(mod);
332 PhantomFileDocument::getView() const
334 return dynamic_cast<PhantomFileView*>(GetFirstView());
338 PhantomFileDocument::Activate()
341 getView()->getFrame()->Activate();
347 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
350 PlotFileDocument::OnSaveDocument(const wxString& filename)
352 m_namePlot = filename.c_str();
353 if (! m_plot.fileWrite (filename)) {
354 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
357 if (theApp->getVerboseLogging())
358 *theApp->getLog() << "Wrote plot file " << filename << "\n";
364 PlotFileDocument::OnOpenDocument(const wxString& filename)
366 if (! OnSaveModified())
369 if (! m_plot.fileRead (filename.c_str())) {
370 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
371 m_bBadFileOpen = true;
374 m_bBadFileOpen = false;
376 if (theApp->getVerboseLogging() && filename != "")
377 *theApp->getLog() << "Read plot file " << filename << "\n";
379 SetFilename (filename, true);
380 m_namePlot = filename.c_str();
382 getView()->setInitialClientSize();
390 PlotFileDocument::IsModified(void) const
392 return wxDocument::IsModified();
396 PlotFileDocument::Modify (bool mod)
398 wxDocument::Modify(mod);
402 PlotFileDocument::getView() const
404 return dynamic_cast<PlotFileView*>(GetFirstView());
408 PlotFileDocument::Activate()
411 getView()->getFrame()->Activate();
415 //////////////////////////////////////////////////////////////////////////
419 //////////////////////////////////////////////////////////////////////////
421 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
424 TextFileDocument::OnSaveDocument(const wxString& filename)
426 TextFileView *view = getView();
427 if (! view->getTextCtrl()->SaveFile(filename))
434 TextFileDocument::OnOpenDocument(const wxString& filename)
436 TextFileView *view = getView();
438 if (! view->getTextCtrl()->LoadFile(filename)) {
439 m_bBadFileOpen = true;
443 SetFilename (filename, true);
446 m_bBadFileOpen = false;
451 TextFileDocument::IsModified(void) const
455 TextFileView *view = getView();
458 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
460 return wxDocument::IsModified();
465 TextFileDocument::getView() const
467 return dynamic_cast<TextFileView*>(GetFirstView());
471 TextFileDocument::getTextCtrl()
473 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
476 //////////////////////////////////////////////////////////////////////////
478 // Graph3dFileDocument
480 //////////////////////////////////////////////////////////////////////////
484 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
486 Graph3dFileDocument::Graph3dFileDocument(void)
487 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
491 Graph3dFileDocument::~Graph3dFileDocument()
496 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
503 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
505 SetFilename (filename, true);
508 m_bBadFileOpen = false;
513 Graph3dFileDocument::IsModified(void) const
515 return wxDocument::IsModified();
520 Graph3dFileDocument::getView() const
522 return dynamic_cast<Graph3dFileView*>(GetFirstView());
526 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
528 m_nx = rImageFile.nx();
529 m_ny = rImageFile.ny();
530 m_array = rImageFile.getArray();
536 Graph3dFileDocument::Activate()
539 getView()->getFrame()->Activate();
544 #endif // wxUSE_GLCANVAS