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.36 2001/03/13 04:44:25 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());
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);
229 GetFirstView()->OnUpdate (GetFirstView(), NULL);
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 GetFirstView()->OnUpdate (GetFirstView(), NULL);
297 m_bBadFileOpen = false;
303 PhantomFileDocument::OnSaveDocument(const wxString& filename)
305 if (! m_phantom.fileWrite (filename.c_str())) {
306 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
309 if (theApp->getVerboseLogging())
310 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
316 PhantomFileDocument::IsModified(void) const
322 PhantomFileDocument::Modify(bool mod)
324 wxDocument::Modify(mod);
329 PhantomFileDocument::getView() const
331 return dynamic_cast<PhantomFileView*>(GetFirstView());
335 PhantomFileDocument::Activate()
338 getView()->getFrame()->Activate();
344 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
347 PlotFileDocument::OnSaveDocument(const wxString& filename)
349 m_namePlot = filename.c_str();
350 if (! m_plot.fileWrite (filename)) {
351 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
354 if (theApp->getVerboseLogging())
355 *theApp->getLog() << "Wrote plot file " << filename << "\n";
361 PlotFileDocument::OnOpenDocument(const wxString& filename)
363 if (! OnSaveModified())
366 if (! m_plot.fileRead (filename.c_str())) {
367 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
368 m_bBadFileOpen = true;
371 if (theApp->getVerboseLogging())
372 *theApp->getLog() << "Read plot file " << filename << "\n";
373 SetFilename (filename, true);
374 m_namePlot = filename.c_str();
377 GetFirstView()->OnUpdate (NULL, NULL);
378 m_bBadFileOpen = false;
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);
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