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.28 2001/02/25 06:32:12 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 m_criticalSection.Enter();
153 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
154 i != m_vecpBackgroundSupervisors.end(); i++)
155 BackgroundSupervisor::cancelSupervisor(*i);
156 m_criticalSection.Leave();
158 while (m_vecpBackgroundSupervisors.size() > 0) {
163 delete m_pProjectionFile;
167 ProjectionFileDocument::OnAddBackground (wxCommandEvent& event)
169 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
170 wxASSERT (pSupervisor != NULL);
172 wxCriticalSectionLocker locker (m_criticalSection);
174 m_vecpBackgroundSupervisors.push_back (pSupervisor);
178 ProjectionFileDocument::OnRemoveBackground (wxCommandEvent& event)
180 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
181 wxASSERT (pSupervisor != NULL);
183 m_criticalSection.Enter();
185 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
186 i != m_vecpBackgroundSupervisors.end();
188 if (*i == pSupervisor) {
189 m_vecpBackgroundSupervisors.erase(i);
193 m_criticalSection.Leave();
196 sys_error (ERR_SEVERE, "Could not find background task [ProjectionFileDocument::removeBackgroundTask");
198 wxCommandEvent ackEvent (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_DOCUMENT_ACK_REMOVE);
199 wxPostEvent (pSupervisor, ackEvent);
203 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
205 if (! OnSaveModified())
208 if (! m_pProjectionFile->read (filename.c_str())) {
209 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
210 m_bBadFileOpen = true;
213 if (theApp->getVerboseLogging())
214 *theApp->getLog() << "Read projection file " << filename << "\n";
215 SetFilename(filename, true);
218 GetFirstView()->OnUpdate (GetFirstView(), NULL);
219 m_bBadFileOpen = false;
225 ProjectionFileDocument::IsModified(void) const
227 return wxDocument::IsModified();
231 ProjectionFileDocument::Modify(bool mod)
233 wxDocument::Modify(mod);
238 ProjectionFileDocument::getView() const
240 return dynamic_cast<ProjectionFileView*>(GetFirstView());
243 // PhantomFileDocument
245 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
248 PhantomFileDocument::OnOpenDocument(const wxString& filename)
250 if (! OnSaveModified())
253 wxString myFilename = filename;
254 if (wxFile::Exists (myFilename)) {
255 m_phantom.createFromFile (myFilename);
256 if (theApp->getVerboseLogging())
257 *theApp->getLog() << "Read phantom file " << filename << "\n";
259 myFilename.Replace (".phm", "");
260 m_phantom.createFromPhantom (myFilename);
262 m_namePhantom = myFilename;
263 SetFilename (myFilename, true);
264 if (m_phantom.fail()) {
265 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
266 m_bBadFileOpen = true;
269 m_idPhantom = m_phantom.id();
272 GetFirstView()->OnUpdate (GetFirstView(), NULL);
273 m_bBadFileOpen = false;
279 PhantomFileDocument::OnSaveDocument(const wxString& filename)
281 if (! m_phantom.fileWrite (filename.c_str())) {
282 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
285 if (theApp->getVerboseLogging())
286 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
292 PhantomFileDocument::IsModified(void) const
298 PhantomFileDocument::Modify(bool mod)
300 wxDocument::Modify(mod);
305 PhantomFileDocument::getView() const
307 return dynamic_cast<PhantomFileView*>(GetFirstView());
312 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
315 PlotFileDocument::OnSaveDocument(const wxString& filename)
317 m_namePlot = filename.c_str();
318 if (! m_plot.fileWrite (filename)) {
319 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
322 if (theApp->getVerboseLogging())
323 *theApp->getLog() << "Wrote plot file " << filename << "\n";
329 PlotFileDocument::OnOpenDocument(const wxString& filename)
331 if (! OnSaveModified())
334 if (! m_plot.fileRead (filename.c_str())) {
335 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
336 m_bBadFileOpen = true;
339 if (theApp->getVerboseLogging())
340 *theApp->getLog() << "Read plot file " << filename << "\n";
341 SetFilename (filename, true);
342 m_namePlot = filename.c_str();
345 GetFirstView()->OnUpdate (NULL, NULL);
346 m_bBadFileOpen = false;
353 PlotFileDocument::IsModified(void) const
355 return wxDocument::IsModified();
359 PlotFileDocument::Modify (bool mod)
361 wxDocument::Modify(mod);
365 PlotFileDocument::getView() const
367 return dynamic_cast<PlotFileView*>(GetFirstView());
370 //////////////////////////////////////////////////////////////////////////
374 //////////////////////////////////////////////////////////////////////////
376 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
379 TextFileDocument::OnSaveDocument(const wxString& filename)
381 TextFileView *view = getView();
382 if (! view->getTextCtrl()->SaveFile(filename))
389 TextFileDocument::OnOpenDocument(const wxString& filename)
391 TextFileView *view = getView();
393 if (! view->getTextCtrl()->LoadFile(filename)) {
394 m_bBadFileOpen = true;
398 SetFilename (filename, true);
401 m_bBadFileOpen = false;
406 TextFileDocument::IsModified(void) const
410 TextFileView *view = getView();
413 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
415 return wxDocument::IsModified();
420 TextFileDocument::getView() const
422 return dynamic_cast<TextFileView*>(GetFirstView());
426 TextFileDocument::getTextCtrl()
428 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
431 //////////////////////////////////////////////////////////////////////////
433 // Graph3dFileDocument
435 //////////////////////////////////////////////////////////////////////////
439 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
441 Graph3dFileDocument::Graph3dFileDocument(void)
442 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
446 Graph3dFileDocument::~Graph3dFileDocument()
448 // delete [] m_pVertices;
449 // delete [] m_pNormals;
453 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
460 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
462 SetFilename (filename, true);
465 m_bBadFileOpen = false;
470 Graph3dFileDocument::IsModified(void) const
472 return wxDocument::IsModified();
477 Graph3dFileDocument::getView() const
479 return dynamic_cast<Graph3dFileView*>(GetFirstView());
483 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
485 // delete [] m_pVertices;
486 // delete [] m_pNormals;
489 m_nx = rImageFile.nx();
490 m_ny = rImageFile.ny();
491 m_array = rImageFile.getArray();
496 #endif // wxUSE_GLCANVAS