bb8ec90d973e122046ac32d95cda6c34d7f52bb4
[ctsim.git] / src / docs.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          doc.cpp
5 **   Purpose:       Document routines for CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: docs.cpp,v 1.27 2001/02/23 21:58:31 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #ifdef __GNUG__
29 // #pragma implementation
30 #endif
31
32 #include "wx/wxprec.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37 #include "wx/txtstrm.h"
38 #include "wx/file.h"
39 #include "wx/thread.h"
40
41 #if !wxUSE_DOC_VIEW_ARCHITECTURE
42 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
43 #endif
44
45 #include "ct.h"
46 #include "ctsim.h"
47 #include "docs.h"
48 #include "views.h"
49 #include "threadrecon.h"
50
51
52 // ImageFileDocument
53
54 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
55
56 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
57 {
58   if (! m_pImageFile->fileWrite (filename)) {
59     *theApp->getLog() << "Unable to write image file " << filename << "\n";
60     return false;
61   }
62   if (theApp->getVerboseLogging())
63     *theApp->getLog() << "Wrote image file " << filename << "\n";
64   Modify(false);
65   return true;
66 }
67
68 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
69 {
70   if (! OnSaveModified())
71     return false;
72
73   if (! m_pImageFile->fileRead (filename.c_str())) {
74     *theApp->getLog() << "Unable to read image file " << filename << "\n";
75     m_bBadFileOpen = true;
76     return false;
77   }
78   if (theApp->getVerboseLogging())
79     *theApp->getLog() << "Read image file " << filename << "\n";
80   SetFilename(filename, true);  
81   Modify(false);
82   UpdateAllViews();
83   getView()->OnUpdate (getView(), NULL);
84   m_bBadFileOpen = false;
85
86   return true;
87 }
88
89 bool 
90 ImageFileDocument::IsModified(void) const
91 {
92   return wxDocument::IsModified();
93 }
94
95 void 
96 ImageFileDocument::Modify(bool mod)
97 {
98   wxDocument::Modify(mod);
99 }
100
101 ImageFileView* 
102 ImageFileDocument::getView() const
103
104   return dynamic_cast<ImageFileView*>(GetFirstView()); 
105 }
106
107
108 bool
109 ImageFileDocument::Revert ()
110 {
111   if (IsModified()) {
112     wxString msg ("Revert to saved ");
113     msg += GetFilename();
114     msg += "?";
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";
119       Modify (false);
120       OnOpenDocument (GetFilename());
121     }
122   }
123   getView()->OnUpdate (getView(), NULL);
124   UpdateAllViews();
125
126   return true;
127 }
128
129 // ProjectionFileDocument
130
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)
135 END_EVENT_TABLE()
136
137 bool 
138 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
139 {
140   if (! m_pProjectionFile->write (filename.c_str())) {
141     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
142     return false;
143   }
144   if (theApp->getVerboseLogging())
145     *theApp->getLog() << "Wrote projection file " << filename << "\n";
146   Modify(false);
147   return true;
148 }
149
150 ProjectionFileDocument::~ProjectionFileDocument()
151 {
152   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
153         i != m_vecpBackgroundSupervisors.end(); i++) {
154           BackgroundSupervisor::cancelSupervisor(*i);
155     }
156   
157   while (m_vecpBackgroundSupervisors.size() > 0)
158     ::wxYield();
159
160   delete m_pProjectionFile;
161 }
162
163 void
164 ProjectionFileDocument::OnAddBackground (wxCommandEvent& event)
165 {
166   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
167   wxASSERT (pSupervisor != NULL);
168
169   wxCriticalSectionLocker locker (m_criticalSection);
170   if (pSupervisor)
171     m_vecpBackgroundSupervisors.push_back (pSupervisor);
172 }
173
174 void
175 ProjectionFileDocument::OnRemoveBackground (wxCommandEvent& event)
176 {
177   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
178   wxASSERT (pSupervisor != NULL);
179
180   wxCriticalSectionLocker locker (m_criticalSection);
181   bool bFound = false;
182   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
183         i != m_vecpBackgroundSupervisors.end(); 
184         i++) 
185           if (*i == pSupervisor) {
186             m_vecpBackgroundSupervisors.erase(i);
187             bFound = true;
188             break;
189         }
190    if (! bFound) 
191      sys_error (ERR_SEVERE, "Could not find background task [ProjectionFileDocument::removeBackgroundTask");
192 }
193
194 bool 
195 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
196 {
197   if (! OnSaveModified())
198     return false;
199
200   if (! m_pProjectionFile->read (filename.c_str())) {
201     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
202     m_bBadFileOpen = true;
203     return false;
204   }
205   if (theApp->getVerboseLogging())
206     *theApp->getLog() << "Read projection file " << filename << "\n";
207   SetFilename(filename, true);
208   Modify(false);
209   UpdateAllViews();
210   GetFirstView()->OnUpdate (GetFirstView(), NULL);
211   m_bBadFileOpen = false;
212   
213   return true;
214 }
215
216 bool 
217 ProjectionFileDocument::IsModified(void) const
218 {
219   return wxDocument::IsModified();
220 }
221
222 void 
223 ProjectionFileDocument::Modify(bool mod)
224 {
225   wxDocument::Modify(mod);
226 }
227
228
229 ProjectionFileView* 
230 ProjectionFileDocument::getView() const
231
232   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
233 }
234
235 // PhantomFileDocument
236
237 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
238
239 bool 
240 PhantomFileDocument::OnOpenDocument(const wxString& filename)
241 {
242   if (! OnSaveModified())
243     return false;
244
245   wxString myFilename = filename;
246   if (wxFile::Exists (myFilename)) {
247     m_phantom.createFromFile (myFilename);
248     if (theApp->getVerboseLogging())
249       *theApp->getLog() << "Read phantom file " << filename << "\n";
250   } else {
251     myFilename.Replace (".phm", "");
252     m_phantom.createFromPhantom (myFilename);
253   }
254   m_namePhantom = myFilename;
255   SetFilename (myFilename, true);
256   if (m_phantom.fail()) {
257     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
258     m_bBadFileOpen = true;
259     return false;
260   }
261   m_idPhantom = m_phantom.id();
262   Modify(false);
263   UpdateAllViews();
264   GetFirstView()->OnUpdate (GetFirstView(), NULL);
265   m_bBadFileOpen = false;
266   
267   return true;
268 }
269
270 bool 
271 PhantomFileDocument::OnSaveDocument(const wxString& filename)
272 {
273   if (! m_phantom.fileWrite (filename.c_str())) {
274     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
275     return false;
276   }
277   if (theApp->getVerboseLogging())
278     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
279   Modify(false);
280   return true;
281 }
282
283 bool 
284 PhantomFileDocument::IsModified(void) const
285 {
286   return false;
287 }
288
289 void 
290 PhantomFileDocument::Modify(bool mod)
291 {
292   wxDocument::Modify(mod);
293 }
294
295
296 PhantomFileView* 
297 PhantomFileDocument::getView() const
298
299   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
300 }
301
302 // PlotFileDocument
303
304 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
305
306 bool 
307 PlotFileDocument::OnSaveDocument(const wxString& filename)
308 {
309   m_namePlot = filename.c_str();
310   if (! m_plot.fileWrite (filename)) {
311     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
312     return false;
313   }
314   if (theApp->getVerboseLogging())
315     *theApp->getLog() << "Wrote plot file " << filename << "\n";
316   Modify(false);
317   return true;
318 }
319
320 bool 
321 PlotFileDocument::OnOpenDocument(const wxString& filename)
322 {
323   if (! OnSaveModified())
324     return false;
325
326   if (! m_plot.fileRead (filename.c_str())) {
327     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
328     m_bBadFileOpen = true;
329     return false;
330   }
331   if (theApp->getVerboseLogging())
332     *theApp->getLog() << "Read plot file " << filename << "\n";
333   SetFilename (filename, true);
334   m_namePlot = filename.c_str();
335   Modify (false);
336   UpdateAllViews();
337   GetFirstView()->OnUpdate (NULL, NULL);
338   m_bBadFileOpen = false;
339   
340   return true;
341 }
342
343
344 bool 
345 PlotFileDocument::IsModified(void) const
346 {
347   return wxDocument::IsModified();
348 }
349
350 void 
351 PlotFileDocument::Modify (bool mod)
352 {
353   wxDocument::Modify(mod);
354 }
355
356 PlotFileView* 
357 PlotFileDocument::getView() const
358
359   return dynamic_cast<PlotFileView*>(GetFirstView()); 
360 }
361
362 //////////////////////////////////////////////////////////////////////////
363 //
364 // TextFileDocument
365 //
366 //////////////////////////////////////////////////////////////////////////
367
368 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
369
370 bool 
371 TextFileDocument::OnSaveDocument(const wxString& filename)
372 {
373   TextFileView *view = getView();
374   if (! view->getTextCtrl()->SaveFile(filename))
375     return false;
376   Modify(false);
377   return true;
378 }
379
380 bool 
381 TextFileDocument::OnOpenDocument(const wxString& filename)
382 {
383   TextFileView *view = getView();
384   
385   if (! view->getTextCtrl()->LoadFile(filename)) {
386     m_bBadFileOpen = true;
387     return false;
388   }
389   
390   SetFilename (filename, true);
391   Modify (false);
392   UpdateAllViews();
393   m_bBadFileOpen = false;
394   return true;
395 }
396
397 bool 
398 TextFileDocument::IsModified(void) const
399 {
400   return false;
401   
402   TextFileView *view = getView();
403   
404   if (view)
405     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
406   else
407     return wxDocument::IsModified();
408 }
409
410
411 TextFileView* 
412 TextFileDocument::getView() const
413
414   return dynamic_cast<TextFileView*>(GetFirstView()); 
415 }
416
417 wxTextCtrl* 
418 TextFileDocument::getTextCtrl()
419
420   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
421 }
422
423 //////////////////////////////////////////////////////////////////////////
424 //
425 // Graph3dFileDocument
426 //
427 //////////////////////////////////////////////////////////////////////////
428
429 #if wxUSE_GLCANVAS
430
431 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
432
433 Graph3dFileDocument::Graph3dFileDocument(void) 
434 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
435 {
436 }
437
438 Graph3dFileDocument::~Graph3dFileDocument() 
439 {
440 //    delete [] m_pVertices;
441 //    delete [] m_pNormals;
442 }
443
444 bool 
445 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
446 {
447   Modify(false);
448   return true;
449 }
450
451 bool 
452 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
453 {
454   SetFilename (filename, true);
455   Modify (false);
456   UpdateAllViews();
457   m_bBadFileOpen = false;
458   return true;
459 }
460
461 bool 
462 Graph3dFileDocument::IsModified(void) const
463 {
464     return wxDocument::IsModified();
465 }
466
467
468 Graph3dFileView* 
469 Graph3dFileDocument::getView() const
470
471   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
472 }
473
474 bool
475 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
476 {
477 //  delete [] m_pVertices;
478 //  delete [] m_pNormals;
479
480
481   m_nx = rImageFile.nx();
482   m_ny = rImageFile.ny();
483   m_array = rImageFile.getArray();
484
485   return true;
486 }
487
488 #endif // wxUSE_GLCANVAS