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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
26 #include "wx/wxprec.h"
31 #include "wx/txtstrm.h"
33 #include "wx/thread.h"
35 #if !wxUSE_DOC_VIEW_ARCHITECTURE
36 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
43 #include "threadrecon.h"
48 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
50 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
52 if (! m_pImageFile->fileWrite (filename.mb_str(wxConvUTF8))) {
53 *theApp->getLog() << _T("Unable to write image file ") << filename << _T("\n");
56 if (theApp->getVerboseLogging())
57 *theApp->getLog() << _T("Wrote image file ") << filename << _T("\n");
62 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
64 if (! OnSaveModified())
67 if (! m_pImageFile->fileRead (filename.mb_str(wxConvUTF8))) {
68 *theApp->getLog() << _T("Unable to read image file ") << filename << _T("\n");
69 m_bBadFileOpen = true;
73 if (theApp->getVerboseLogging() && filename != _T(""))
74 *theApp->getLog() << _T("Read image file ") << filename << _T("\n");
76 SetFilename(filename, true);
78 getView()->setInitialClientSize();
80 m_bBadFileOpen = false;
86 ImageFileDocument::IsModified(void) const
88 return wxDocument::IsModified();
92 ImageFileDocument::Modify(bool mod)
94 wxDocument::Modify(mod);
98 ImageFileDocument::getView() const
100 return dynamic_cast<ImageFileView*>(GetFirstView());
104 ImageFileDocument::Revert ()
107 wxString msg (_T("Revert to saved "));
108 msg += GetFilename();
110 wxMessageDialog dialog (getView()->getFrame(), msg, _T("Are you sure?"), wxYES_NO | wxNO_DEFAULT);
111 if (dialog.ShowModal() == wxID_YES) {
112 if (theApp->getVerboseLogging())
113 *theApp->getLog() << _T("Reverting to saved ") << GetFilename() << _T("\n");
115 OnOpenDocument (GetFilename());
124 ImageFileDocument::Activate()
127 getView()->getFrame()->Activate();
131 // BackgroundProcessingDocument - Base Class
133 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
134 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
137 #ifdef HAVE_WXTHREADS
139 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
141 wxCriticalSectionLocker locker (m_criticalSection);
143 m_vecpBackgroundSupervisors.push_back (pSupervisor);
147 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
149 m_criticalSection.Enter();
151 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
152 i != m_vecpBackgroundSupervisors.end();
154 if (*i == pSupervisor) {
155 m_vecpBackgroundSupervisors.erase(i);
159 m_criticalSection.Leave();
162 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
167 BackgroundProcessingDocument::cancelRunningTasks()
169 #ifdef HAVE_WXTHREADS
170 m_criticalSection.Enter();
171 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
172 i != m_vecpBackgroundSupervisors.end(); i++)
174 m_criticalSection.Leave();
176 while (m_vecpBackgroundSupervisors.size() > 0) {
184 // ProjectionFileDocument
186 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingDocument)
189 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
191 if (! m_pProjectionFile->write (filename.mb_str(wxConvUTF8))) {
192 *theApp->getLog() << _T("Unable to write projection file ") << filename << _T("\n");
195 if (theApp->getVerboseLogging())
196 *theApp->getLog() << _T("Wrote projection file ") << filename << _T("\n");
201 ProjectionFileDocument::~ProjectionFileDocument()
203 cancelRunningTasks();
205 delete m_pProjectionFile;
209 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
211 if (! OnSaveModified())
214 if (! m_pProjectionFile->read (filename.mb_str(wxConvUTF8))) {
215 *theApp->getLog() << _T("Unable to read projection file ") << filename << _T("\n");
216 m_bBadFileOpen = true;
219 m_bBadFileOpen = false;
221 if (theApp->getVerboseLogging() && filename != _T(""))
222 *theApp->getLog() << _T("Read projection file ") << filename << _T("\n");
224 SetFilename(filename, true);
226 getView()->setInitialClientSize();
233 ProjectionFileDocument::IsModified(void) const
235 return wxDocument::IsModified();
239 ProjectionFileDocument::Modify(bool mod)
241 wxDocument::Modify(mod);
246 ProjectionFileDocument::getView() const
248 return dynamic_cast<ProjectionFileView*>(GetFirstView());
252 ProjectionFileDocument::Activate()
255 getView()->getFrame()->Activate();
259 // PhantomFileDocument
261 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingDocument)
263 PhantomFileDocument::~PhantomFileDocument()
265 cancelRunningTasks();
269 PhantomFileDocument::OnOpenDocument(const wxString& constFilename)
271 if (! OnSaveModified())
274 wxString filename (constFilename);
276 if (wxFile::Exists (filename)) {
277 m_phantom.createFromFile (filename.mb_str(wxConvUTF8));
278 if (theApp->getVerboseLogging())
279 *theApp->getLog() << _T("Read phantom file ") << filename << _T("\n");
281 filename.Replace (_T(".phm"), _T(""));
282 m_phantom.createFromPhantom (filename.mb_str(wxConvUTF8));
284 m_namePhantom = filename;
285 SetFilename (filename, true);
286 if (m_phantom.fail()) {
287 *theApp->getLog() << _T("Failure creating phantom ") << filename << _T("\n");
288 m_bBadFileOpen = true;
291 m_idPhantom = m_phantom.id();
294 m_bBadFileOpen = false;
300 PhantomFileDocument::OnSaveDocument(const wxString& filename)
302 if (! m_phantom.fileWrite (filename.mb_str(wxConvUTF8))) {
303 *theApp->getLog() << _T("Unable to write phantom file ") << filename << _T("\n");
306 if (theApp->getVerboseLogging())
307 *theApp->getLog() << _T("Wrote phantom file ") << filename << _T("\n");
313 PhantomFileDocument::IsModified(void) const
319 PhantomFileDocument::Modify(bool mod)
321 wxDocument::Modify(mod);
326 PhantomFileDocument::getView() const
328 return dynamic_cast<PhantomFileView*>(GetFirstView());
332 PhantomFileDocument::Activate()
335 getView()->getFrame()->Activate();
341 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
344 PlotFileDocument::OnSaveDocument(const wxString& filename)
346 m_namePlot = filename.c_str();
347 if (! m_plot.fileWrite (filename.mb_str(wxConvUTF8))) {
348 *theApp->getLog() << _T("Unable to write plot file ") << filename << _T("\n");
351 if (theApp->getVerboseLogging())
352 *theApp->getLog() << _T("Wrote plot file ") << filename << _T("\n");
358 PlotFileDocument::OnOpenDocument(const wxString& filename)
360 if (! OnSaveModified())
363 if (! m_plot.fileRead (filename.mb_str(wxConvUTF8))) {
364 *theApp->getLog() << _T("Unable to read plot file ") << filename << _T("\n");
365 m_bBadFileOpen = true;
368 m_bBadFileOpen = false;
370 if (theApp->getVerboseLogging() && filename != _T(""))
371 *theApp->getLog() << _T("Read plot file ") << filename << _T("\n");
373 SetFilename (filename, true);
374 m_namePlot = filename.c_str();
376 getView()->setInitialClientSize();
384 PlotFileDocument::IsModified(void) const
386 return wxDocument::IsModified();
390 PlotFileDocument::Modify (bool mod)
392 wxDocument::Modify(mod);
396 PlotFileDocument::getView() const
398 return dynamic_cast<PlotFileView*>(GetFirstView());
402 PlotFileDocument::Activate()
405 getView()->getFrame()->Activate();
409 //////////////////////////////////////////////////////////////////////////
413 //////////////////////////////////////////////////////////////////////////
415 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
418 TextFileDocument::OnSaveDocument(const wxString& filename)
420 TextFileView *view = getView();
421 if (! view->getTextCtrl()->SaveFile(filename))
428 TextFileDocument::OnOpenDocument(const wxString& filename)
430 TextFileView *view = getView();
432 if (! view->getTextCtrl()->LoadFile(filename)) {
433 m_bBadFileOpen = true;
437 SetFilename (filename, true);
440 m_bBadFileOpen = false;
445 TextFileDocument::IsModified(void) const
449 TextFileView *view = getView();
452 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
454 return wxDocument::IsModified();
459 TextFileDocument::getView() const
461 return dynamic_cast<TextFileView*>(GetFirstView());
465 TextFileDocument::getTextCtrl()
467 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
470 //////////////////////////////////////////////////////////////////////////
472 // Graph3dFileDocument
474 //////////////////////////////////////////////////////////////////////////
478 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
480 Graph3dFileDocument::Graph3dFileDocument(void)
481 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
485 Graph3dFileDocument::~Graph3dFileDocument()
490 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
497 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
499 SetFilename (filename, true);
501 getView()->setInitialClientSize();
503 m_bBadFileOpen = false;
508 Graph3dFileDocument::IsModified(void) const
510 return wxDocument::IsModified();
515 Graph3dFileDocument::getView() const
517 return dynamic_cast<Graph3dFileView*>(GetFirstView());
521 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
523 m_nx = rImageFile.nx();
524 m_ny = rImageFile.ny();
525 m_array = rImageFile.getArray();
531 Graph3dFileDocument::Activate()
534 getView()->getFrame()->Activate();
539 #endif // wxUSE_GLCANVAS