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.32 2001/03/05 15:10: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);
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)
134 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundProcessingDocument::OnAddBackground)
135 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundProcessingDocument::OnRemoveBackground)
141 BackgroundProcessingDocument::OnAddBackground (wxCommandEvent& event)
143 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
144 wxASSERT (pSupervisor != NULL);
146 wxCriticalSectionLocker locker (m_criticalSection);
148 m_vecpBackgroundSupervisors.push_back (pSupervisor);
152 BackgroundProcessingDocument::OnRemoveBackground (wxCommandEvent& event)
154 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
155 wxASSERT (pSupervisor != NULL);
157 m_criticalSection.Enter();
159 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
160 i != m_vecpBackgroundSupervisors.end();
162 if (*i == pSupervisor) {
163 m_vecpBackgroundSupervisors.erase(i);
167 m_criticalSection.Leave();
170 sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
172 pSupervisor->onAckDocumentRemove();
177 BackgroundProcessingDocument::cancelRunningTasks()
180 m_criticalSection.Enter();
181 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
182 i != m_vecpBackgroundSupervisors.end(); i++)
184 m_criticalSection.Leave();
186 while (m_vecpBackgroundSupervisors.size() > 0) {
194 // ProjectionFileDocument
196 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
199 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
201 if (! m_pProjectionFile->write (filename.c_str())) {
202 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
205 if (theApp->getVerboseLogging())
206 *theApp->getLog() << "Wrote projection file " << filename << "\n";
211 ProjectionFileDocument::~ProjectionFileDocument()
213 cancelRunningTasks();
215 delete m_pProjectionFile;
219 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
221 if (! OnSaveModified())
224 if (! m_pProjectionFile->read (filename.c_str())) {
225 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
226 m_bBadFileOpen = true;
229 if (theApp->getVerboseLogging())
230 *theApp->getLog() << "Read projection file " << filename << "\n";
231 SetFilename(filename, true);
234 GetFirstView()->OnUpdate (GetFirstView(), NULL);
235 m_bBadFileOpen = false;
241 ProjectionFileDocument::IsModified(void) const
243 return wxDocument::IsModified();
247 ProjectionFileDocument::Modify(bool mod)
249 wxDocument::Modify(mod);
254 ProjectionFileDocument::getView() const
256 return dynamic_cast<ProjectionFileView*>(GetFirstView());
259 // PhantomFileDocument
261 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
263 PhantomFileDocument::~PhantomFileDocument()
265 cancelRunningTasks();
269 PhantomFileDocument::OnOpenDocument(const wxString& filename)
271 if (! OnSaveModified())
274 wxString myFilename = filename;
275 if (wxFile::Exists (myFilename)) {
276 m_phantom.createFromFile (myFilename);
277 if (theApp->getVerboseLogging())
278 *theApp->getLog() << "Read phantom file " << filename << "\n";
280 myFilename.Replace (".phm", "");
281 m_phantom.createFromPhantom (myFilename);
283 m_namePhantom = myFilename;
284 SetFilename (myFilename, true);
285 if (m_phantom.fail()) {
286 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
287 m_bBadFileOpen = true;
290 m_idPhantom = m_phantom.id();
293 GetFirstView()->OnUpdate (GetFirstView(), NULL);
294 m_bBadFileOpen = false;
300 PhantomFileDocument::OnSaveDocument(const wxString& filename)
302 if (! m_phantom.fileWrite (filename.c_str())) {
303 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
306 if (theApp->getVerboseLogging())
307 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
313 PhantomFileDocument::IsModified(void) const
319 PhantomFileDocument::Modify(bool mod)
321 wxDocument::Modify(mod);
326 PhantomFileDocument::getView() const
328 return dynamic_cast<PhantomFileView*>(GetFirstView());
333 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
336 PlotFileDocument::OnSaveDocument(const wxString& filename)
338 m_namePlot = filename.c_str();
339 if (! m_plot.fileWrite (filename)) {
340 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
343 if (theApp->getVerboseLogging())
344 *theApp->getLog() << "Wrote plot file " << filename << "\n";
350 PlotFileDocument::OnOpenDocument(const wxString& filename)
352 if (! OnSaveModified())
355 if (! m_plot.fileRead (filename.c_str())) {
356 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
357 m_bBadFileOpen = true;
360 if (theApp->getVerboseLogging())
361 *theApp->getLog() << "Read plot file " << filename << "\n";
362 SetFilename (filename, true);
363 m_namePlot = filename.c_str();
366 GetFirstView()->OnUpdate (NULL, NULL);
367 m_bBadFileOpen = false;
374 PlotFileDocument::IsModified(void) const
376 return wxDocument::IsModified();
380 PlotFileDocument::Modify (bool mod)
382 wxDocument::Modify(mod);
386 PlotFileDocument::getView() const
388 return dynamic_cast<PlotFileView*>(GetFirstView());
391 //////////////////////////////////////////////////////////////////////////
395 //////////////////////////////////////////////////////////////////////////
397 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
400 TextFileDocument::OnSaveDocument(const wxString& filename)
402 TextFileView *view = getView();
403 if (! view->getTextCtrl()->SaveFile(filename))
410 TextFileDocument::OnOpenDocument(const wxString& filename)
412 TextFileView *view = getView();
414 if (! view->getTextCtrl()->LoadFile(filename)) {
415 m_bBadFileOpen = true;
419 SetFilename (filename, true);
422 m_bBadFileOpen = false;
427 TextFileDocument::IsModified(void) const
431 TextFileView *view = getView();
434 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
436 return wxDocument::IsModified();
441 TextFileDocument::getView() const
443 return dynamic_cast<TextFileView*>(GetFirstView());
447 TextFileDocument::getTextCtrl()
449 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
452 //////////////////////////////////////////////////////////////////////////
454 // Graph3dFileDocument
456 //////////////////////////////////////////////////////////////////////////
460 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
462 Graph3dFileDocument::Graph3dFileDocument(void)
463 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
467 Graph3dFileDocument::~Graph3dFileDocument()
469 // delete [] m_pVertices;
470 // delete [] m_pNormals;
474 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
481 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
483 SetFilename (filename, true);
486 m_bBadFileOpen = false;
491 Graph3dFileDocument::IsModified(void) const
493 return wxDocument::IsModified();
498 Graph3dFileDocument::getView() const
500 return dynamic_cast<Graph3dFileView*>(GetFirstView());
504 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
506 // delete [] m_pVertices;
507 // delete [] m_pNormals;
510 m_nx = rImageFile.nx();
511 m_ny = rImageFile.ny();
512 m_array = rImageFile.getArray();
517 #endif // wxUSE_GLCANVAS