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.33 2001/03/05 17:26:46 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;
78 if (theApp->getVerboseLogging())
79 *theApp->getLog() << "Read image file " << filename << "\n";
80 SetFilename(filename, true);
83 getView()->OnUpdate (getView(), NULL);
84 m_bBadFileOpen = false;
90 ImageFileDocument::IsModified(void) const
92 return wxDocument::IsModified();
96 ImageFileDocument::Modify(bool mod)
98 wxDocument::Modify(mod);
102 ImageFileDocument::getView() const
104 return dynamic_cast<ImageFileView*>(GetFirstView());
109 ImageFileDocument::Revert ()
112 wxString msg ("Revert to saved ");
113 msg += GetFilename();
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";
120 OnOpenDocument (GetFilename());
123 getView()->OnUpdate (getView(), NULL);
129 // BackgroundProcessingDocument - Base Class
131 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
132 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
135 #ifdef HAVE_WXTHREADS
137 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
139 wxCriticalSectionLocker locker (m_criticalSection);
141 m_vecpBackgroundSupervisors.push_back (pSupervisor);
145 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
147 m_criticalSection.Enter();
149 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
150 i != m_vecpBackgroundSupervisors.end();
152 if (*i == pSupervisor) {
153 m_vecpBackgroundSupervisors.erase(i);
157 m_criticalSection.Leave();
160 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
165 BackgroundProcessingDocument::cancelRunningTasks()
167 #ifdef HAVE_WXTHREADS
168 m_criticalSection.Enter();
169 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
170 i != m_vecpBackgroundSupervisors.end(); i++)
172 m_criticalSection.Leave();
174 while (m_vecpBackgroundSupervisors.size() > 0) {
182 // ProjectionFileDocument
184 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
187 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
189 if (! m_pProjectionFile->write (filename.c_str())) {
190 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
193 if (theApp->getVerboseLogging())
194 *theApp->getLog() << "Wrote projection file " << filename << "\n";
199 ProjectionFileDocument::~ProjectionFileDocument()
201 cancelRunningTasks();
203 delete m_pProjectionFile;
207 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
209 if (! OnSaveModified())
212 if (! m_pProjectionFile->read (filename.c_str())) {
213 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
214 m_bBadFileOpen = true;
217 if (theApp->getVerboseLogging())
218 *theApp->getLog() << "Read projection file " << filename << "\n";
219 SetFilename(filename, true);
222 GetFirstView()->OnUpdate (GetFirstView(), NULL);
223 m_bBadFileOpen = false;
229 ProjectionFileDocument::IsModified(void) const
231 return wxDocument::IsModified();
235 ProjectionFileDocument::Modify(bool mod)
237 wxDocument::Modify(mod);
242 ProjectionFileDocument::getView() const
244 return dynamic_cast<ProjectionFileView*>(GetFirstView());
247 // PhantomFileDocument
249 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
251 PhantomFileDocument::~PhantomFileDocument()
253 cancelRunningTasks();
257 PhantomFileDocument::OnOpenDocument(const wxString& filename)
259 if (! OnSaveModified())
262 wxString myFilename = filename;
263 if (wxFile::Exists (myFilename)) {
264 m_phantom.createFromFile (myFilename);
265 if (theApp->getVerboseLogging())
266 *theApp->getLog() << "Read phantom file " << filename << "\n";
268 myFilename.Replace (".phm", "");
269 m_phantom.createFromPhantom (myFilename);
271 m_namePhantom = myFilename;
272 SetFilename (myFilename, true);
273 if (m_phantom.fail()) {
274 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
275 m_bBadFileOpen = true;
278 m_idPhantom = m_phantom.id();
281 GetFirstView()->OnUpdate (GetFirstView(), NULL);
282 m_bBadFileOpen = false;
288 PhantomFileDocument::OnSaveDocument(const wxString& filename)
290 if (! m_phantom.fileWrite (filename.c_str())) {
291 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
294 if (theApp->getVerboseLogging())
295 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
301 PhantomFileDocument::IsModified(void) const
307 PhantomFileDocument::Modify(bool mod)
309 wxDocument::Modify(mod);
314 PhantomFileDocument::getView() const
316 return dynamic_cast<PhantomFileView*>(GetFirstView());
321 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
324 PlotFileDocument::OnSaveDocument(const wxString& filename)
326 m_namePlot = filename.c_str();
327 if (! m_plot.fileWrite (filename)) {
328 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
331 if (theApp->getVerboseLogging())
332 *theApp->getLog() << "Wrote plot file " << filename << "\n";
338 PlotFileDocument::OnOpenDocument(const wxString& filename)
340 if (! OnSaveModified())
343 if (! m_plot.fileRead (filename.c_str())) {
344 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
345 m_bBadFileOpen = true;
348 if (theApp->getVerboseLogging())
349 *theApp->getLog() << "Read plot file " << filename << "\n";
350 SetFilename (filename, true);
351 m_namePlot = filename.c_str();
354 GetFirstView()->OnUpdate (NULL, NULL);
355 m_bBadFileOpen = false;
362 PlotFileDocument::IsModified(void) const
364 return wxDocument::IsModified();
368 PlotFileDocument::Modify (bool mod)
370 wxDocument::Modify(mod);
374 PlotFileDocument::getView() const
376 return dynamic_cast<PlotFileView*>(GetFirstView());
379 //////////////////////////////////////////////////////////////////////////
383 //////////////////////////////////////////////////////////////////////////
385 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
388 TextFileDocument::OnSaveDocument(const wxString& filename)
390 TextFileView *view = getView();
391 if (! view->getTextCtrl()->SaveFile(filename))
398 TextFileDocument::OnOpenDocument(const wxString& filename)
400 TextFileView *view = getView();
402 if (! view->getTextCtrl()->LoadFile(filename)) {
403 m_bBadFileOpen = true;
407 SetFilename (filename, true);
410 m_bBadFileOpen = false;
415 TextFileDocument::IsModified(void) const
419 TextFileView *view = getView();
422 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
424 return wxDocument::IsModified();
429 TextFileDocument::getView() const
431 return dynamic_cast<TextFileView*>(GetFirstView());
435 TextFileDocument::getTextCtrl()
437 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
440 //////////////////////////////////////////////////////////////////////////
442 // Graph3dFileDocument
444 //////////////////////////////////////////////////////////////////////////
448 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
450 Graph3dFileDocument::Graph3dFileDocument(void)
451 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
455 Graph3dFileDocument::~Graph3dFileDocument()
457 // delete [] m_pVertices;
458 // delete [] m_pNormals;
462 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
469 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
471 SetFilename (filename, true);
474 m_bBadFileOpen = false;
479 Graph3dFileDocument::IsModified(void) const
481 return wxDocument::IsModified();
486 Graph3dFileDocument::getView() const
488 return dynamic_cast<Graph3dFileView*>(GetFirstView());
492 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
494 // delete [] m_pVertices;
495 // delete [] m_pNormals;
498 m_nx = rImageFile.nx();
499 m_ny = rImageFile.ny();
500 m_array = rImageFile.getArray();
505 #endif // wxUSE_GLCANVAS