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.40 2002/06/02 19:01:58 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);
82 getView()->setInitialClientSize();
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());
108 ImageFileDocument::Revert ()
111 wxString msg ("Revert to saved ");
112 msg += GetFilename();
114 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
115 if (dialog.ShowModal() == wxID_YES) {
116 if (theApp->getVerboseLogging())
117 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
119 OnOpenDocument (GetFilename());
122 //getView()->OnUpdate (getView(), NULL);
129 ImageFileDocument::Activate()
132 getView()->getFrame()->Activate();
136 // BackgroundProcessingDocument - Base Class
138 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
139 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
142 #ifdef HAVE_WXTHREADS
144 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
146 wxCriticalSectionLocker locker (m_criticalSection);
148 m_vecpBackgroundSupervisors.push_back (pSupervisor);
152 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
154 m_criticalSection.Enter();
156 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
157 i != m_vecpBackgroundSupervisors.end();
159 if (*i == pSupervisor) {
160 m_vecpBackgroundSupervisors.erase(i);
164 m_criticalSection.Leave();
167 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
172 BackgroundProcessingDocument::cancelRunningTasks()
174 #ifdef HAVE_WXTHREADS
175 m_criticalSection.Enter();
176 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
177 i != m_vecpBackgroundSupervisors.end(); i++)
179 m_criticalSection.Leave();
181 while (m_vecpBackgroundSupervisors.size() > 0) {
189 // ProjectionFileDocument
191 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
194 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
196 if (! m_pProjectionFile->write (filename.c_str())) {
197 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
200 if (theApp->getVerboseLogging())
201 *theApp->getLog() << "Wrote projection file " << filename << "\n";
206 ProjectionFileDocument::~ProjectionFileDocument()
208 cancelRunningTasks();
210 delete m_pProjectionFile;
214 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
216 if (! OnSaveModified())
219 if (! m_pProjectionFile->read (filename.c_str())) {
220 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
221 m_bBadFileOpen = true;
224 if (theApp->getVerboseLogging())
225 *theApp->getLog() << "Read projection file " << filename << "\n";
226 SetFilename(filename, true);
228 getView()->setInitialClientSize();
230 m_bBadFileOpen = false;
236 ProjectionFileDocument::IsModified(void) const
238 return wxDocument::IsModified();
242 ProjectionFileDocument::Modify(bool mod)
244 wxDocument::Modify(mod);
249 ProjectionFileDocument::getView() const
251 return dynamic_cast<ProjectionFileView*>(GetFirstView());
255 ProjectionFileDocument::Activate()
258 getView()->getFrame()->Activate();
262 // PhantomFileDocument
264 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
266 PhantomFileDocument::~PhantomFileDocument()
268 cancelRunningTasks();
272 PhantomFileDocument::OnOpenDocument(const wxString& filename)
274 if (! OnSaveModified())
277 wxString myFilename = filename;
278 if (wxFile::Exists (myFilename)) {
279 m_phantom.createFromFile (myFilename);
280 if (theApp->getVerboseLogging())
281 *theApp->getLog() << "Read phantom file " << filename << "\n";
283 myFilename.Replace (".phm", "");
284 m_phantom.createFromPhantom (myFilename);
286 m_namePhantom = myFilename;
287 SetFilename (myFilename, true);
288 if (m_phantom.fail()) {
289 *theApp->getLog() << "Failure creating phantom " << myFilename << "\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 if (theApp->getVerboseLogging())
371 *theApp->getLog() << "Read plot file " << filename << "\n";
372 SetFilename (filename, true);
373 m_namePlot = filename.c_str();
376 //GetFirstView()->OnUpdate (NULL, NULL);
377 m_bBadFileOpen = false;
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);
502 m_bBadFileOpen = false;
507 Graph3dFileDocument::IsModified(void) const
509 return wxDocument::IsModified();
514 Graph3dFileDocument::getView() const
516 return dynamic_cast<Graph3dFileView*>(GetFirstView());
520 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
522 m_nx = rImageFile.nx();
523 m_ny = rImageFile.ny();
524 m_array = rImageFile.getArray();
530 Graph3dFileDocument::Activate()
533 getView()->getFrame()->Activate();
538 #endif // wxUSE_GLCANVAS