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.30 2001/02/25 16:21:36 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)
258 PhantomFileDocument::~PhantomFileDocument()
260 cancelRunningTasks();
264 PhantomFileDocument::OnOpenDocument(const wxString& filename)
266 if (! OnSaveModified())
269 wxString myFilename = filename;
270 if (wxFile::Exists (myFilename)) {
271 m_phantom.createFromFile (myFilename);
272 if (theApp->getVerboseLogging())
273 *theApp->getLog() << "Read phantom file " << filename << "\n";
275 myFilename.Replace (".phm", "");
276 m_phantom.createFromPhantom (myFilename);
278 m_namePhantom = myFilename;
279 SetFilename (myFilename, true);
280 if (m_phantom.fail()) {
281 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
282 m_bBadFileOpen = true;
285 m_idPhantom = m_phantom.id();
288 GetFirstView()->OnUpdate (GetFirstView(), NULL);
289 m_bBadFileOpen = false;
295 PhantomFileDocument::OnSaveDocument(const wxString& filename)
297 if (! m_phantom.fileWrite (filename.c_str())) {
298 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
301 if (theApp->getVerboseLogging())
302 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
308 PhantomFileDocument::IsModified(void) const
314 PhantomFileDocument::Modify(bool mod)
316 wxDocument::Modify(mod);
321 PhantomFileDocument::getView() const
323 return dynamic_cast<PhantomFileView*>(GetFirstView());
328 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
331 PlotFileDocument::OnSaveDocument(const wxString& filename)
333 m_namePlot = filename.c_str();
334 if (! m_plot.fileWrite (filename)) {
335 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
338 if (theApp->getVerboseLogging())
339 *theApp->getLog() << "Wrote plot file " << filename << "\n";
345 PlotFileDocument::OnOpenDocument(const wxString& filename)
347 if (! OnSaveModified())
350 if (! m_plot.fileRead (filename.c_str())) {
351 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
352 m_bBadFileOpen = true;
355 if (theApp->getVerboseLogging())
356 *theApp->getLog() << "Read plot file " << filename << "\n";
357 SetFilename (filename, true);
358 m_namePlot = filename.c_str();
361 GetFirstView()->OnUpdate (NULL, NULL);
362 m_bBadFileOpen = false;
369 PlotFileDocument::IsModified(void) const
371 return wxDocument::IsModified();
375 PlotFileDocument::Modify (bool mod)
377 wxDocument::Modify(mod);
381 PlotFileDocument::getView() const
383 return dynamic_cast<PlotFileView*>(GetFirstView());
386 //////////////////////////////////////////////////////////////////////////
390 //////////////////////////////////////////////////////////////////////////
392 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
395 TextFileDocument::OnSaveDocument(const wxString& filename)
397 TextFileView *view = getView();
398 if (! view->getTextCtrl()->SaveFile(filename))
405 TextFileDocument::OnOpenDocument(const wxString& filename)
407 TextFileView *view = getView();
409 if (! view->getTextCtrl()->LoadFile(filename)) {
410 m_bBadFileOpen = true;
414 SetFilename (filename, true);
417 m_bBadFileOpen = false;
422 TextFileDocument::IsModified(void) const
426 TextFileView *view = getView();
429 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
431 return wxDocument::IsModified();
436 TextFileDocument::getView() const
438 return dynamic_cast<TextFileView*>(GetFirstView());
442 TextFileDocument::getTextCtrl()
444 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
447 //////////////////////////////////////////////////////////////////////////
449 // Graph3dFileDocument
451 //////////////////////////////////////////////////////////////////////////
455 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
457 Graph3dFileDocument::Graph3dFileDocument(void)
458 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
462 Graph3dFileDocument::~Graph3dFileDocument()
464 // delete [] m_pVertices;
465 // delete [] m_pNormals;
469 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
476 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
478 SetFilename (filename, true);
481 m_bBadFileOpen = false;
486 Graph3dFileDocument::IsModified(void) const
488 return wxDocument::IsModified();
493 Graph3dFileDocument::getView() const
495 return dynamic_cast<Graph3dFileView*>(GetFirstView());
499 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
501 // delete [] m_pVertices;
502 // delete [] m_pNormals;
505 m_nx = rImageFile.nx();
506 m_ny = rImageFile.ny();
507 m_array = rImageFile.getArray();
512 #endif // wxUSE_GLCANVAS