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.29 2001/02/25 10:52:55 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 wxCommandEvent ackEvent (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_DOCUMENT_ACK_REMOVE);
170 wxPostEvent (pSupervisor, ackEvent);
174 BackgroundProcessingDocument::cancelRunningTasks()
176 m_criticalSection.Enter();
177 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
178 i != m_vecpBackgroundSupervisors.end(); i++)
179 BackgroundSupervisor::cancelSupervisor(*i);
180 m_criticalSection.Leave();
182 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());
254 // PhantomFileDocument
256 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
259 PhantomFileDocument::OnOpenDocument(const wxString& filename)
261 if (! OnSaveModified())
264 wxString myFilename = filename;
265 if (wxFile::Exists (myFilename)) {
266 m_phantom.createFromFile (myFilename);
267 if (theApp->getVerboseLogging())
268 *theApp->getLog() << "Read phantom file " << filename << "\n";
270 myFilename.Replace (".phm", "");
271 m_phantom.createFromPhantom (myFilename);
273 m_namePhantom = myFilename;
274 SetFilename (myFilename, true);
275 if (m_phantom.fail()) {
276 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
277 m_bBadFileOpen = true;
280 m_idPhantom = m_phantom.id();
283 GetFirstView()->OnUpdate (GetFirstView(), NULL);
284 m_bBadFileOpen = false;
290 PhantomFileDocument::OnSaveDocument(const wxString& filename)
292 if (! m_phantom.fileWrite (filename.c_str())) {
293 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
296 if (theApp->getVerboseLogging())
297 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
303 PhantomFileDocument::IsModified(void) const
309 PhantomFileDocument::Modify(bool mod)
311 wxDocument::Modify(mod);
316 PhantomFileDocument::getView() const
318 return dynamic_cast<PhantomFileView*>(GetFirstView());
323 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
326 PlotFileDocument::OnSaveDocument(const wxString& filename)
328 m_namePlot = filename.c_str();
329 if (! m_plot.fileWrite (filename)) {
330 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
333 if (theApp->getVerboseLogging())
334 *theApp->getLog() << "Wrote plot file " << filename << "\n";
340 PlotFileDocument::OnOpenDocument(const wxString& filename)
342 if (! OnSaveModified())
345 if (! m_plot.fileRead (filename.c_str())) {
346 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
347 m_bBadFileOpen = true;
350 if (theApp->getVerboseLogging())
351 *theApp->getLog() << "Read plot file " << filename << "\n";
352 SetFilename (filename, true);
353 m_namePlot = filename.c_str();
356 GetFirstView()->OnUpdate (NULL, NULL);
357 m_bBadFileOpen = false;
364 PlotFileDocument::IsModified(void) const
366 return wxDocument::IsModified();
370 PlotFileDocument::Modify (bool mod)
372 wxDocument::Modify(mod);
376 PlotFileDocument::getView() const
378 return dynamic_cast<PlotFileView*>(GetFirstView());
381 //////////////////////////////////////////////////////////////////////////
385 //////////////////////////////////////////////////////////////////////////
387 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
390 TextFileDocument::OnSaveDocument(const wxString& filename)
392 TextFileView *view = getView();
393 if (! view->getTextCtrl()->SaveFile(filename))
400 TextFileDocument::OnOpenDocument(const wxString& filename)
402 TextFileView *view = getView();
404 if (! view->getTextCtrl()->LoadFile(filename)) {
405 m_bBadFileOpen = true;
409 SetFilename (filename, true);
412 m_bBadFileOpen = false;
417 TextFileDocument::IsModified(void) const
421 TextFileView *view = getView();
424 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
426 return wxDocument::IsModified();
431 TextFileDocument::getView() const
433 return dynamic_cast<TextFileView*>(GetFirstView());
437 TextFileDocument::getTextCtrl()
439 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
442 //////////////////////////////////////////////////////////////////////////
444 // Graph3dFileDocument
446 //////////////////////////////////////////////////////////////////////////
450 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
452 Graph3dFileDocument::Graph3dFileDocument(void)
453 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
457 Graph3dFileDocument::~Graph3dFileDocument()
459 // delete [] m_pVertices;
460 // delete [] m_pNormals;
464 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
471 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
473 SetFilename (filename, true);
476 m_bBadFileOpen = false;
481 Graph3dFileDocument::IsModified(void) const
483 return wxDocument::IsModified();
488 Graph3dFileDocument::getView() const
490 return dynamic_cast<Graph3dFileView*>(GetFirstView());
494 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
496 // delete [] m_pVertices;
497 // delete [] m_pNormals;
500 m_nx = rImageFile.nx();
501 m_ny = rImageFile.ny();
502 m_array = rImageFile.getArray();
507 #endif // wxUSE_GLCANVAS