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();
79 this->SetFilename(filename, true);
81 m_bBadFileOpen = false;
87 ImageFileDocument::IsModified(void) const
89 return wxDocument::IsModified();
93 ImageFileDocument::Modify(bool mod)
95 wxDocument::Modify(mod);
99 ImageFileDocument::getView() const
101 return dynamic_cast<ImageFileView*>(GetFirstView());
105 ImageFileDocument::Revert ()
108 wxString msg (_T("Revert to saved "));
109 msg += GetFilename();
111 wxMessageDialog dialog (getView()->getFrame(), msg, _T("Are you sure?"), wxYES_NO | wxNO_DEFAULT);
112 if (dialog.ShowModal() == wxID_YES) {
113 if (theApp->getVerboseLogging())
114 *theApp->getLog() << _T("Reverting to saved ") << GetFilename() << _T("\n");
116 OnOpenDocument (GetFilename());
125 ImageFileDocument::Activate()
128 getView()->getFrame()->Activate();
132 // BackgroundProcessingDocument - Base Class
134 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
135 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
138 #ifdef HAVE_WXTHREADS
140 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
142 wxCriticalSectionLocker locker (m_criticalSection);
144 m_vecpBackgroundSupervisors.push_back (pSupervisor);
148 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
150 m_criticalSection.Enter();
152 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
153 i != m_vecpBackgroundSupervisors.end();
155 if (*i == pSupervisor) {
156 m_vecpBackgroundSupervisors.erase(i);
160 m_criticalSection.Leave();
163 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
168 BackgroundProcessingDocument::cancelRunningTasks()
170 #ifdef HAVE_WXTHREADS
171 m_criticalSection.Enter();
172 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
173 i != m_vecpBackgroundSupervisors.end(); i++)
175 m_criticalSection.Leave();
177 while (m_vecpBackgroundSupervisors.size() > 0) {
185 // ProjectionFileDocument
187 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingDocument)
190 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
192 if (! m_pProjectionFile->write (filename.mb_str(wxConvUTF8))) {
193 *theApp->getLog() << _T("Unable to write projection file ") << filename << _T("\n");
196 if (theApp->getVerboseLogging())
197 *theApp->getLog() << _T("Wrote projection file ") << filename << _T("\n");
202 ProjectionFileDocument::~ProjectionFileDocument()
204 cancelRunningTasks();
206 delete m_pProjectionFile;
210 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
212 if (! OnSaveModified())
215 if (! m_pProjectionFile->read (filename.mb_str(wxConvUTF8))) {
216 *theApp->getLog() << _T("Unable to read projection file ") << filename << _T("\n");
217 m_bBadFileOpen = true;
220 m_bBadFileOpen = false;
222 if (theApp->getVerboseLogging() && filename != _T(""))
223 *theApp->getLog() << _T("Read projection file ") << filename << _T("\n");
225 SetFilename(filename, true);
227 getView()->setInitialClientSize();
234 ProjectionFileDocument::IsModified(void) const
236 return wxDocument::IsModified();
240 ProjectionFileDocument::Modify(bool mod)
242 wxDocument::Modify(mod);
247 ProjectionFileDocument::getView() const
249 return dynamic_cast<ProjectionFileView*>(GetFirstView());
253 ProjectionFileDocument::Activate()
256 getView()->getFrame()->Activate();
260 // PhantomFileDocument
262 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingDocument)
264 PhantomFileDocument::~PhantomFileDocument()
266 cancelRunningTasks();
270 PhantomFileDocument::OnOpenDocument(const wxString& constFilename)
272 if (! OnSaveModified())
275 wxString filename (constFilename);
277 if (wxFile::Exists (filename)) {
278 m_phantom.createFromPhmFile (filename.mb_str(wxConvUTF8));
279 if (theApp->getVerboseLogging())
280 *theApp->getLog() << _T("Read phantom file ") << filename << _T("\n");
282 filename.Replace (_T(".phm"), _T(""));
283 m_phantom.createFromPhantom (filename.mb_str(wxConvUTF8));
285 m_namePhantom = filename;
286 SetFilename (filename, true);
287 if (m_phantom.fail()) {
288 *theApp->getLog() << _T("Failure creating phantom ") << filename << _T("\n");
289 m_bBadFileOpen = true;
292 m_idPhantom = m_phantom.id();
295 m_bBadFileOpen = false;
301 PhantomFileDocument::OnSaveDocument(const wxString& filename)
303 if (! m_phantom.fileWrite (filename.mb_str(wxConvUTF8))) {
304 *theApp->getLog() << _T("Unable to write phantom file ") << filename << _T("\n");
307 if (theApp->getVerboseLogging())
308 *theApp->getLog() << _T("Wrote phantom file ") << filename << _T("\n");
314 PhantomFileDocument::IsModified(void) const
320 PhantomFileDocument::Modify(bool mod)
322 wxDocument::Modify(mod);
327 PhantomFileDocument::getView() const
329 return dynamic_cast<PhantomFileView*>(GetFirstView());
333 PhantomFileDocument::Activate()
336 getView()->getFrame()->Activate();
342 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
345 PlotFileDocument::OnSaveDocument(const wxString& filename)
347 m_namePlot = filename.c_str();
348 if (! m_plot.fileWrite (filename.mb_str(wxConvUTF8))) {
349 *theApp->getLog() << _T("Unable to write plot file ") << filename << _T("\n");
352 if (theApp->getVerboseLogging())
353 *theApp->getLog() << _T("Wrote plot file ") << filename << _T("\n");
359 PlotFileDocument::OnOpenDocument(const wxString& filename)
361 if (! OnSaveModified())
364 if (! m_plot.fileRead (filename.mb_str(wxConvUTF8))) {
365 *theApp->getLog() << _T("Unable to read plot file ") << filename << _T("\n");
366 m_bBadFileOpen = true;
369 m_bBadFileOpen = false;
371 if (theApp->getVerboseLogging() && filename != _T(""))
372 *theApp->getLog() << _T("Read plot file ") << filename << _T("\n");
374 SetFilename (filename, true);
375 m_namePlot = filename.c_str();
377 getView()->setInitialClientSize();
385 PlotFileDocument::IsModified(void) const
387 return wxDocument::IsModified();
391 PlotFileDocument::Modify (bool mod)
393 wxDocument::Modify(mod);
397 PlotFileDocument::getView() const
399 return dynamic_cast<PlotFileView*>(GetFirstView());
403 PlotFileDocument::Activate()
406 getView()->getFrame()->Activate();
410 //////////////////////////////////////////////////////////////////////////
414 //////////////////////////////////////////////////////////////////////////
416 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
419 TextFileDocument::OnSaveDocument(const wxString& filename)
421 TextFileView *view = getView();
422 if (! view->getTextCtrl()->SaveFile(filename))
429 TextFileDocument::OnOpenDocument(const wxString& filename)
431 TextFileView *view = getView();
433 if (! view->getTextCtrl()->LoadFile(filename)) {
434 m_bBadFileOpen = true;
438 SetFilename (filename, true);
441 m_bBadFileOpen = false;
446 TextFileDocument::IsModified(void) const
450 TextFileView *view = getView();
453 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
455 return wxDocument::IsModified();
460 TextFileDocument::getView() const
462 return dynamic_cast<TextFileView*>(GetFirstView());
466 TextFileDocument::getTextCtrl()
468 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
471 //////////////////////////////////////////////////////////////////////////
473 // Graph3dFileDocument
475 //////////////////////////////////////////////////////////////////////////
479 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
481 Graph3dFileDocument::Graph3dFileDocument(void)
482 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
486 Graph3dFileDocument::~Graph3dFileDocument()
491 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
498 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
500 SetFilename (filename, true);
502 getView()->setInitialClientSize();
504 m_bBadFileOpen = false;
509 Graph3dFileDocument::IsModified(void) const
511 return wxDocument::IsModified();
516 Graph3dFileDocument::getView() const
518 return dynamic_cast<Graph3dFileView*>(GetFirstView());
522 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
524 m_nx = rImageFile.nx();
525 m_ny = rImageFile.ny();
526 m_array = rImageFile.getArray();
532 Graph3dFileDocument::Activate()
535 getView()->getFrame()->Activate();
540 #endif // wxUSE_GLCANVAS