r607: *** empty log message ***
[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.33 2001/03/05 17:26:46 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 // BackgroundProcessingDocument - Base Class
130
131 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
132 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
133 END_EVENT_TABLE()
134
135 #ifdef HAVE_WXTHREADS
136 void
137 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
138 {
139   wxCriticalSectionLocker locker (m_criticalSection);
140   if (pSupervisor)
141     m_vecpBackgroundSupervisors.push_back (pSupervisor);
142 }
143
144 void
145 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
146 {
147   m_criticalSection.Enter();
148   bool bFound = false;
149   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
150         i != m_vecpBackgroundSupervisors.end(); 
151         i++) 
152           if (*i == pSupervisor) {
153             m_vecpBackgroundSupervisors.erase(i);
154             bFound = true;
155             break;
156         }
157   m_criticalSection.Leave();
158
159   if (! bFound) 
160      sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
161 }
162 #endif
163
164 void
165 BackgroundProcessingDocument::cancelRunningTasks()
166 {
167 #ifdef HAVE_WXTHREADS
168   m_criticalSection.Enter();
169   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
170         i != m_vecpBackgroundSupervisors.end(); i++)
171           (*i)->onCancel();
172   m_criticalSection.Leave();
173
174   while (m_vecpBackgroundSupervisors.size() > 0) {
175      ::wxYield();
176      ::wxUsleep(50);
177   }
178 #endif
179 }
180
181
182 // ProjectionFileDocument
183
184 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
185
186 bool 
187 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
188 {
189   if (! m_pProjectionFile->write (filename.c_str())) {
190     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
191     return false;
192   }
193   if (theApp->getVerboseLogging())
194     *theApp->getLog() << "Wrote projection file " << filename << "\n";
195   Modify(false);
196   return true;
197 }
198
199 ProjectionFileDocument::~ProjectionFileDocument()
200 {
201   cancelRunningTasks();
202
203   delete m_pProjectionFile;
204 }
205
206 bool 
207 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
208 {
209   if (! OnSaveModified())
210     return false;
211
212   if (! m_pProjectionFile->read (filename.c_str())) {
213     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
214     m_bBadFileOpen = true;
215     return false;
216   }
217   if (theApp->getVerboseLogging())
218     *theApp->getLog() << "Read projection file " << filename << "\n";
219   SetFilename(filename, true);
220   Modify(false);
221   UpdateAllViews();
222   GetFirstView()->OnUpdate (GetFirstView(), NULL);
223   m_bBadFileOpen = false;
224   
225   return true;
226 }
227
228 bool 
229 ProjectionFileDocument::IsModified(void) const
230 {
231   return wxDocument::IsModified();
232 }
233
234 void 
235 ProjectionFileDocument::Modify(bool mod)
236 {
237   wxDocument::Modify(mod);
238 }
239
240
241 ProjectionFileView* 
242 ProjectionFileDocument::getView() const
243
244   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
245 }
246
247 // PhantomFileDocument
248
249 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
250
251 PhantomFileDocument::~PhantomFileDocument()
252 {
253   cancelRunningTasks();
254 }
255
256 bool 
257 PhantomFileDocument::OnOpenDocument(const wxString& filename)
258 {
259   if (! OnSaveModified())
260     return false;
261
262   wxString myFilename = filename;
263   if (wxFile::Exists (myFilename)) {
264     m_phantom.createFromFile (myFilename);
265     if (theApp->getVerboseLogging())
266       *theApp->getLog() << "Read phantom file " << filename << "\n";
267   } else {
268     myFilename.Replace (".phm", "");
269     m_phantom.createFromPhantom (myFilename);
270   }
271   m_namePhantom = myFilename;
272   SetFilename (myFilename, true);
273   if (m_phantom.fail()) {
274     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
275     m_bBadFileOpen = true;
276     return false;
277   }
278   m_idPhantom = m_phantom.id();
279   Modify(false);
280   UpdateAllViews();
281   GetFirstView()->OnUpdate (GetFirstView(), NULL);
282   m_bBadFileOpen = false;
283   
284   return true;
285 }
286
287 bool 
288 PhantomFileDocument::OnSaveDocument(const wxString& filename)
289 {
290   if (! m_phantom.fileWrite (filename.c_str())) {
291     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
292     return false;
293   }
294   if (theApp->getVerboseLogging())
295     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
296   Modify(false);
297   return true;
298 }
299
300 bool 
301 PhantomFileDocument::IsModified(void) const
302 {
303   return false;
304 }
305
306 void 
307 PhantomFileDocument::Modify(bool mod)
308 {
309   wxDocument::Modify(mod);
310 }
311
312
313 PhantomFileView* 
314 PhantomFileDocument::getView() const
315
316   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
317 }
318
319 // PlotFileDocument
320
321 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
322
323 bool 
324 PlotFileDocument::OnSaveDocument(const wxString& filename)
325 {
326   m_namePlot = filename.c_str();
327   if (! m_plot.fileWrite (filename)) {
328     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
329     return false;
330   }
331   if (theApp->getVerboseLogging())
332     *theApp->getLog() << "Wrote plot file " << filename << "\n";
333   Modify(false);
334   return true;
335 }
336
337 bool 
338 PlotFileDocument::OnOpenDocument(const wxString& filename)
339 {
340   if (! OnSaveModified())
341     return false;
342
343   if (! m_plot.fileRead (filename.c_str())) {
344     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
345     m_bBadFileOpen = true;
346     return false;
347   }
348   if (theApp->getVerboseLogging())
349     *theApp->getLog() << "Read plot file " << filename << "\n";
350   SetFilename (filename, true);
351   m_namePlot = filename.c_str();
352   Modify (false);
353   UpdateAllViews();
354   GetFirstView()->OnUpdate (NULL, NULL);
355   m_bBadFileOpen = false;
356   
357   return true;
358 }
359
360
361 bool 
362 PlotFileDocument::IsModified(void) const
363 {
364   return wxDocument::IsModified();
365 }
366
367 void 
368 PlotFileDocument::Modify (bool mod)
369 {
370   wxDocument::Modify(mod);
371 }
372
373 PlotFileView* 
374 PlotFileDocument::getView() const
375
376   return dynamic_cast<PlotFileView*>(GetFirstView()); 
377 }
378
379 //////////////////////////////////////////////////////////////////////////
380 //
381 // TextFileDocument
382 //
383 //////////////////////////////////////////////////////////////////////////
384
385 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
386
387 bool 
388 TextFileDocument::OnSaveDocument(const wxString& filename)
389 {
390   TextFileView *view = getView();
391   if (! view->getTextCtrl()->SaveFile(filename))
392     return false;
393   Modify(false);
394   return true;
395 }
396
397 bool 
398 TextFileDocument::OnOpenDocument(const wxString& filename)
399 {
400   TextFileView *view = getView();
401   
402   if (! view->getTextCtrl()->LoadFile(filename)) {
403     m_bBadFileOpen = true;
404     return false;
405   }
406   
407   SetFilename (filename, true);
408   Modify (false);
409   UpdateAllViews();
410   m_bBadFileOpen = false;
411   return true;
412 }
413
414 bool 
415 TextFileDocument::IsModified(void) const
416 {
417   return false;
418   
419   TextFileView *view = getView();
420   
421   if (view)
422     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
423   else
424     return wxDocument::IsModified();
425 }
426
427
428 TextFileView* 
429 TextFileDocument::getView() const
430
431   return dynamic_cast<TextFileView*>(GetFirstView()); 
432 }
433
434 wxTextCtrl* 
435 TextFileDocument::getTextCtrl()
436
437   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
438 }
439
440 //////////////////////////////////////////////////////////////////////////
441 //
442 // Graph3dFileDocument
443 //
444 //////////////////////////////////////////////////////////////////////////
445
446 #if wxUSE_GLCANVAS
447
448 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
449
450 Graph3dFileDocument::Graph3dFileDocument(void) 
451 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
452 {
453 }
454
455 Graph3dFileDocument::~Graph3dFileDocument() 
456 {
457 //    delete [] m_pVertices;
458 //    delete [] m_pNormals;
459 }
460
461 bool 
462 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
463 {
464   Modify(false);
465   return true;
466 }
467
468 bool 
469 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
470 {
471   SetFilename (filename, true);
472   Modify (false);
473   UpdateAllViews();
474   m_bBadFileOpen = false;
475   return true;
476 }
477
478 bool 
479 Graph3dFileDocument::IsModified(void) const
480 {
481     return wxDocument::IsModified();
482 }
483
484
485 Graph3dFileView* 
486 Graph3dFileDocument::getView() const
487
488   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
489 }
490
491 bool
492 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
493 {
494 //  delete [] m_pVertices;
495 //  delete [] m_pNormals;
496
497
498   m_nx = rImageFile.nx();
499   m_ny = rImageFile.ny();
500   m_array = rImageFile.getArray();
501
502   return true;
503 }
504
505 #endif // wxUSE_GLCANVAS