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.31 2001/03/04 22:30:19 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 // BackgroundProcessingDocument - Base Class
131 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
132 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
133 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundProcessingDocument::OnAddBackground)
134 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundProcessingDocument::OnRemoveBackground)
138 BackgroundProcessingDocument::OnAddBackground (wxCommandEvent& event)
140 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
141 wxASSERT (pSupervisor != NULL);
143 wxCriticalSectionLocker locker (m_criticalSection);
145 m_vecpBackgroundSupervisors.push_back (pSupervisor);
149 BackgroundProcessingDocument::OnRemoveBackground (wxCommandEvent& event)
151 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
152 wxASSERT (pSupervisor != NULL);
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]");
169 pSupervisor->onAckDocumentRemove();
173 BackgroundProcessingDocument::cancelRunningTasks()
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) {
188 // ProjectionFileDocument
190 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
193 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
195 if (! m_pProjectionFile->write (filename.c_str())) {
196 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
199 if (theApp->getVerboseLogging())
200 *theApp->getLog() << "Wrote projection file " << filename << "\n";
205 ProjectionFileDocument::~ProjectionFileDocument()
207 cancelRunningTasks();
209 delete m_pProjectionFile;
213 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
215 if (! OnSaveModified())
218 if (! m_pProjectionFile->read (filename.c_str())) {
219 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
220 m_bBadFileOpen = true;
223 if (theApp->getVerboseLogging())
224 *theApp->getLog() << "Read projection file " << filename << "\n";
225 SetFilename(filename, true);
228 GetFirstView()->OnUpdate (GetFirstView(), NULL);
229 m_bBadFileOpen = false;
235 ProjectionFileDocument::IsModified(void) const
237 return wxDocument::IsModified();
241 ProjectionFileDocument::Modify(bool mod)
243 wxDocument::Modify(mod);
248 ProjectionFileDocument::getView() const
250 return dynamic_cast<ProjectionFileView*>(GetFirstView());
253 // PhantomFileDocument
255 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
257 PhantomFileDocument::~PhantomFileDocument()
259 cancelRunningTasks();
263 PhantomFileDocument::OnOpenDocument(const wxString& filename)
265 if (! OnSaveModified())
268 wxString myFilename = filename;
269 if (wxFile::Exists (myFilename)) {
270 m_phantom.createFromFile (myFilename);
271 if (theApp->getVerboseLogging())
272 *theApp->getLog() << "Read phantom file " << filename << "\n";
274 myFilename.Replace (".phm", "");
275 m_phantom.createFromPhantom (myFilename);
277 m_namePhantom = myFilename;
278 SetFilename (myFilename, true);
279 if (m_phantom.fail()) {
280 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
281 m_bBadFileOpen = true;
284 m_idPhantom = m_phantom.id();
287 GetFirstView()->OnUpdate (GetFirstView(), NULL);
288 m_bBadFileOpen = false;
294 PhantomFileDocument::OnSaveDocument(const wxString& filename)
296 if (! m_phantom.fileWrite (filename.c_str())) {
297 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
300 if (theApp->getVerboseLogging())
301 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
307 PhantomFileDocument::IsModified(void) const
313 PhantomFileDocument::Modify(bool mod)
315 wxDocument::Modify(mod);
320 PhantomFileDocument::getView() const
322 return dynamic_cast<PhantomFileView*>(GetFirstView());
327 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
330 PlotFileDocument::OnSaveDocument(const wxString& filename)
332 m_namePlot = filename.c_str();
333 if (! m_plot.fileWrite (filename)) {
334 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
337 if (theApp->getVerboseLogging())
338 *theApp->getLog() << "Wrote plot file " << filename << "\n";
344 PlotFileDocument::OnOpenDocument(const wxString& filename)
346 if (! OnSaveModified())
349 if (! m_plot.fileRead (filename.c_str())) {
350 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
351 m_bBadFileOpen = true;
354 if (theApp->getVerboseLogging())
355 *theApp->getLog() << "Read plot file " << filename << "\n";
356 SetFilename (filename, true);
357 m_namePlot = filename.c_str();
360 GetFirstView()->OnUpdate (NULL, NULL);
361 m_bBadFileOpen = false;
368 PlotFileDocument::IsModified(void) const
370 return wxDocument::IsModified();
374 PlotFileDocument::Modify (bool mod)
376 wxDocument::Modify(mod);
380 PlotFileDocument::getView() const
382 return dynamic_cast<PlotFileView*>(GetFirstView());
385 //////////////////////////////////////////////////////////////////////////
389 //////////////////////////////////////////////////////////////////////////
391 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
394 TextFileDocument::OnSaveDocument(const wxString& filename)
396 TextFileView *view = getView();
397 if (! view->getTextCtrl()->SaveFile(filename))
404 TextFileDocument::OnOpenDocument(const wxString& filename)
406 TextFileView *view = getView();
408 if (! view->getTextCtrl()->LoadFile(filename)) {
409 m_bBadFileOpen = true;
413 SetFilename (filename, true);
416 m_bBadFileOpen = false;
421 TextFileDocument::IsModified(void) const
425 TextFileView *view = getView();
428 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
430 return wxDocument::IsModified();
435 TextFileDocument::getView() const
437 return dynamic_cast<TextFileView*>(GetFirstView());
441 TextFileDocument::getTextCtrl()
443 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
446 //////////////////////////////////////////////////////////////////////////
448 // Graph3dFileDocument
450 //////////////////////////////////////////////////////////////////////////
454 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
456 Graph3dFileDocument::Graph3dFileDocument(void)
457 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
461 Graph3dFileDocument::~Graph3dFileDocument()
463 // delete [] m_pVertices;
464 // delete [] m_pNormals;
468 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
475 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
477 SetFilename (filename, true);
480 m_bBadFileOpen = false;
485 Graph3dFileDocument::IsModified(void) const
487 return wxDocument::IsModified();
492 Graph3dFileDocument::getView() const
494 return dynamic_cast<Graph3dFileView*>(GetFirstView());
498 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
500 // delete [] m_pVertices;
501 // delete [] m_pNormals;
504 m_nx = rImageFile.nx();
505 m_ny = rImageFile.ny();
506 m_array = rImageFile.getArray();
511 #endif // wxUSE_GLCANVAS