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.27 2001/02/23 21:58:31 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 // ProjectionFileDocument
131 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
132 BEGIN_EVENT_TABLE(ProjectionFileDocument, wxDocument)
133 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, ProjectionFileDocument::OnAddBackground)
134 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, ProjectionFileDocument::OnRemoveBackground)
138 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
140 if (! m_pProjectionFile->write (filename.c_str())) {
141 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
144 if (theApp->getVerboseLogging())
145 *theApp->getLog() << "Wrote projection file " << filename << "\n";
150 ProjectionFileDocument::~ProjectionFileDocument()
152 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
153 i != m_vecpBackgroundSupervisors.end(); i++) {
154 BackgroundSupervisor::cancelSupervisor(*i);
157 while (m_vecpBackgroundSupervisors.size() > 0)
160 delete m_pProjectionFile;
164 ProjectionFileDocument::OnAddBackground (wxCommandEvent& event)
166 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
167 wxASSERT (pSupervisor != NULL);
169 wxCriticalSectionLocker locker (m_criticalSection);
171 m_vecpBackgroundSupervisors.push_back (pSupervisor);
175 ProjectionFileDocument::OnRemoveBackground (wxCommandEvent& event)
177 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
178 wxASSERT (pSupervisor != NULL);
180 wxCriticalSectionLocker locker (m_criticalSection);
182 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
183 i != m_vecpBackgroundSupervisors.end();
185 if (*i == pSupervisor) {
186 m_vecpBackgroundSupervisors.erase(i);
191 sys_error (ERR_SEVERE, "Could not find background task [ProjectionFileDocument::removeBackgroundTask");
195 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
197 if (! OnSaveModified())
200 if (! m_pProjectionFile->read (filename.c_str())) {
201 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
202 m_bBadFileOpen = true;
205 if (theApp->getVerboseLogging())
206 *theApp->getLog() << "Read projection file " << filename << "\n";
207 SetFilename(filename, true);
210 GetFirstView()->OnUpdate (GetFirstView(), NULL);
211 m_bBadFileOpen = false;
217 ProjectionFileDocument::IsModified(void) const
219 return wxDocument::IsModified();
223 ProjectionFileDocument::Modify(bool mod)
225 wxDocument::Modify(mod);
230 ProjectionFileDocument::getView() const
232 return dynamic_cast<ProjectionFileView*>(GetFirstView());
235 // PhantomFileDocument
237 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
240 PhantomFileDocument::OnOpenDocument(const wxString& filename)
242 if (! OnSaveModified())
245 wxString myFilename = filename;
246 if (wxFile::Exists (myFilename)) {
247 m_phantom.createFromFile (myFilename);
248 if (theApp->getVerboseLogging())
249 *theApp->getLog() << "Read phantom file " << filename << "\n";
251 myFilename.Replace (".phm", "");
252 m_phantom.createFromPhantom (myFilename);
254 m_namePhantom = myFilename;
255 SetFilename (myFilename, true);
256 if (m_phantom.fail()) {
257 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
258 m_bBadFileOpen = true;
261 m_idPhantom = m_phantom.id();
264 GetFirstView()->OnUpdate (GetFirstView(), NULL);
265 m_bBadFileOpen = false;
271 PhantomFileDocument::OnSaveDocument(const wxString& filename)
273 if (! m_phantom.fileWrite (filename.c_str())) {
274 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
277 if (theApp->getVerboseLogging())
278 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
284 PhantomFileDocument::IsModified(void) const
290 PhantomFileDocument::Modify(bool mod)
292 wxDocument::Modify(mod);
297 PhantomFileDocument::getView() const
299 return dynamic_cast<PhantomFileView*>(GetFirstView());
304 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
307 PlotFileDocument::OnSaveDocument(const wxString& filename)
309 m_namePlot = filename.c_str();
310 if (! m_plot.fileWrite (filename)) {
311 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
314 if (theApp->getVerboseLogging())
315 *theApp->getLog() << "Wrote plot file " << filename << "\n";
321 PlotFileDocument::OnOpenDocument(const wxString& filename)
323 if (! OnSaveModified())
326 if (! m_plot.fileRead (filename.c_str())) {
327 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
328 m_bBadFileOpen = true;
331 if (theApp->getVerboseLogging())
332 *theApp->getLog() << "Read plot file " << filename << "\n";
333 SetFilename (filename, true);
334 m_namePlot = filename.c_str();
337 GetFirstView()->OnUpdate (NULL, NULL);
338 m_bBadFileOpen = false;
345 PlotFileDocument::IsModified(void) const
347 return wxDocument::IsModified();
351 PlotFileDocument::Modify (bool mod)
353 wxDocument::Modify(mod);
357 PlotFileDocument::getView() const
359 return dynamic_cast<PlotFileView*>(GetFirstView());
362 //////////////////////////////////////////////////////////////////////////
366 //////////////////////////////////////////////////////////////////////////
368 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
371 TextFileDocument::OnSaveDocument(const wxString& filename)
373 TextFileView *view = getView();
374 if (! view->getTextCtrl()->SaveFile(filename))
381 TextFileDocument::OnOpenDocument(const wxString& filename)
383 TextFileView *view = getView();
385 if (! view->getTextCtrl()->LoadFile(filename)) {
386 m_bBadFileOpen = true;
390 SetFilename (filename, true);
393 m_bBadFileOpen = false;
398 TextFileDocument::IsModified(void) const
402 TextFileView *view = getView();
405 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
407 return wxDocument::IsModified();
412 TextFileDocument::getView() const
414 return dynamic_cast<TextFileView*>(GetFirstView());
418 TextFileDocument::getTextCtrl()
420 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
423 //////////////////////////////////////////////////////////////////////////
425 // Graph3dFileDocument
427 //////////////////////////////////////////////////////////////////////////
431 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
433 Graph3dFileDocument::Graph3dFileDocument(void)
434 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
438 Graph3dFileDocument::~Graph3dFileDocument()
440 // delete [] m_pVertices;
441 // delete [] m_pNormals;
445 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
452 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
454 SetFilename (filename, true);
457 m_bBadFileOpen = false;
462 Graph3dFileDocument::IsModified(void) const
464 return wxDocument::IsModified();
469 Graph3dFileDocument::getView() const
471 return dynamic_cast<Graph3dFileView*>(GetFirstView());
475 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
477 // delete [] m_pVertices;
478 // delete [] m_pNormals;
481 m_nx = rImageFile.nx();
482 m_ny = rImageFile.ny();
483 m_array = rImageFile.getArray();
488 #endif // wxUSE_GLCANVAS