r2105: *** empty log message ***
[ctsim.git] / src / docs.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          docs.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.41 2002/06/04 19:19:40 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 const char szNEW_IMAGE_FILENAME[] = "___CTSIM___INTERNAL___NEW___.if";
53 const char szNEW_PROJECTION_FILENAME[] = "___CTSIM___INTERNAL___NEW___.pj";
54 const char szNEW_GRAPH3D_FILENAME[] = "___CTSIM___INTERNAL___NEW___.3d";
55 const char szNEW_PLOT_FILENAME[] = "___CTSIM___INTERNAL___NEW___.plt";
56
57
58 // ImageFileDocument
59
60 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
61
62 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
63 {
64   if (! m_pImageFile->fileWrite (filename)) {
65     *theApp->getLog() << "Unable to write image file " << filename << "\n";
66     return false;
67   }
68   if (theApp->getVerboseLogging())
69     *theApp->getLog() << "Wrote image file " << filename << "\n";
70   Modify(false);
71   return true;
72 }
73
74 bool ImageFileDocument::OnOpenDocument(const wxString& constFilename)
75 {
76   if (! OnSaveModified())
77     return false;
78
79   wxString filename (constFilename);
80
81   if (filename == szNEW_IMAGE_FILENAME) {
82     filename = "";
83   } else if (! m_pImageFile->fileRead (filename.c_str())) {
84     *theApp->getLog() << "Unable to read image file " << filename << "\n";
85     m_bBadFileOpen = true;
86     return false;
87   }
88   
89   if (theApp->getVerboseLogging() && filename != "")
90     *theApp->getLog() << "Read image file " << filename << "\n";
91   
92   SetFilename(filename, true);  
93   Modify(false);
94   getView()->setInitialClientSize();
95   UpdateAllViews();
96   m_bBadFileOpen = false;
97
98   return true;
99 }
100
101 bool 
102 ImageFileDocument::IsModified(void) const
103 {
104   return wxDocument::IsModified();
105 }
106
107 void 
108 ImageFileDocument::Modify(bool mod)
109 {
110   wxDocument::Modify(mod);
111 }
112
113 ImageFileView* 
114 ImageFileDocument::getView() const
115
116   return dynamic_cast<ImageFileView*>(GetFirstView()); 
117 }
118
119 bool
120 ImageFileDocument::Revert ()
121 {
122   if (IsModified()) {
123     wxString msg ("Revert to saved ");
124     msg += GetFilename();
125     msg += "?";
126     wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
127     if (dialog.ShowModal() == wxID_YES) {
128       if (theApp->getVerboseLogging())
129         *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
130       Modify (false);
131       OnOpenDocument (GetFilename());
132     }
133   }
134   //getView()->OnUpdate (getView(), NULL);
135   UpdateAllViews();
136
137   return true;
138 }
139
140 void
141 ImageFileDocument::Activate()
142 {
143 #if CTSIM_MDI
144   getView()->getFrame()->Activate();
145 #endif
146 };
147
148 // BackgroundProcessingDocument - Base Class
149
150 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
151 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
152 END_EVENT_TABLE()
153
154 #ifdef HAVE_WXTHREADS
155 void
156 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
157 {
158   wxCriticalSectionLocker locker (m_criticalSection);
159   if (pSupervisor)
160     m_vecpBackgroundSupervisors.push_back (pSupervisor);
161 }
162
163 void
164 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
165 {
166   m_criticalSection.Enter();
167   bool bFound = false;
168   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
169         i != m_vecpBackgroundSupervisors.end(); 
170         i++) 
171           if (*i == pSupervisor) {
172             m_vecpBackgroundSupervisors.erase(i);
173             bFound = true;
174             break;
175         }
176   m_criticalSection.Leave();
177
178   if (! bFound) 
179      sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
180 }
181 #endif
182
183 void
184 BackgroundProcessingDocument::cancelRunningTasks()
185 {
186 #ifdef HAVE_WXTHREADS
187   m_criticalSection.Enter();
188   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin(); 
189         i != m_vecpBackgroundSupervisors.end(); i++)
190           (*i)->onCancel();
191   m_criticalSection.Leave();
192
193   while (m_vecpBackgroundSupervisors.size() > 0) {
194      ::wxYield();
195      ::wxUsleep(50);
196   }
197 #endif
198 }
199
200
201 // ProjectionFileDocument
202
203 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingTask)
204
205 bool 
206 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
207 {
208   if (! m_pProjectionFile->write (filename.c_str())) {
209     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
210     return false;
211   }
212   if (theApp->getVerboseLogging())
213     *theApp->getLog() << "Wrote projection file " << filename << "\n";
214   Modify(false);
215   return true;
216 }
217
218 ProjectionFileDocument::~ProjectionFileDocument()
219 {
220   cancelRunningTasks();
221
222   delete m_pProjectionFile;
223 }
224
225 bool 
226 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
227 {
228   if (! OnSaveModified())
229     return false;
230
231   if (! m_pProjectionFile->read (filename.c_str())) {
232     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
233     m_bBadFileOpen = true;
234     return false;
235   }
236   if (theApp->getVerboseLogging())
237     *theApp->getLog() << "Read projection file " << filename << "\n";
238   SetFilename(filename, true);
239   Modify(false);
240   getView()->setInitialClientSize();
241   UpdateAllViews();
242   m_bBadFileOpen = false;
243   
244   return true;
245 }
246
247 bool 
248 ProjectionFileDocument::IsModified(void) const
249 {
250   return wxDocument::IsModified();
251 }
252
253 void 
254 ProjectionFileDocument::Modify(bool mod)
255 {
256   wxDocument::Modify(mod);
257 }
258
259
260 ProjectionFileView* 
261 ProjectionFileDocument::getView() const
262
263   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
264 }
265
266 void
267 ProjectionFileDocument::Activate()
268 {
269 #if CTSIM_MDI
270   getView()->getFrame()->Activate();
271 #endif
272 };
273
274 // PhantomFileDocument
275
276 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
277
278 PhantomFileDocument::~PhantomFileDocument()
279 {
280   cancelRunningTasks();
281 }
282
283 bool 
284 PhantomFileDocument::OnOpenDocument(const wxString& filename)
285 {
286   if (! OnSaveModified())
287     return false;
288
289   wxString myFilename = filename;
290   if (wxFile::Exists (myFilename)) {
291     m_phantom.createFromFile (myFilename);
292     if (theApp->getVerboseLogging())
293       *theApp->getLog() << "Read phantom file " << filename << "\n";
294   } else {
295     myFilename.Replace (".phm", "");
296     m_phantom.createFromPhantom (myFilename);
297   }
298   m_namePhantom = myFilename;
299   SetFilename (myFilename, true);
300   if (m_phantom.fail()) {
301     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
302     m_bBadFileOpen = true;
303     return false;
304   }
305   m_idPhantom = m_phantom.id();
306   Modify(false);
307   UpdateAllViews();
308   m_bBadFileOpen = false;
309   
310   return true;
311 }
312
313 bool 
314 PhantomFileDocument::OnSaveDocument(const wxString& filename)
315 {
316   if (! m_phantom.fileWrite (filename.c_str())) {
317     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
318     return false;
319   }
320   if (theApp->getVerboseLogging())
321     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
322   Modify(false);
323   return true;
324 }
325
326 bool 
327 PhantomFileDocument::IsModified(void) const
328 {
329   return false;
330 }
331
332 void 
333 PhantomFileDocument::Modify(bool mod)
334 {
335   wxDocument::Modify(mod);
336 }
337
338
339 PhantomFileView* 
340 PhantomFileDocument::getView() const
341
342   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
343 }
344
345 void
346 PhantomFileDocument::Activate()
347 {
348 #if CTSIM_MDI
349   getView()->getFrame()->Activate();
350 #endif
351 };
352
353 // PlotFileDocument
354
355 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
356
357 bool 
358 PlotFileDocument::OnSaveDocument(const wxString& filename)
359 {
360   m_namePlot = filename.c_str();
361   if (! m_plot.fileWrite (filename)) {
362     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
363     return false;
364   }
365   if (theApp->getVerboseLogging())
366     *theApp->getLog() << "Wrote plot file " << filename << "\n";
367   Modify(false);
368   return true;
369 }
370
371 bool 
372 PlotFileDocument::OnOpenDocument(const wxString& filename)
373 {
374   if (! OnSaveModified())
375     return false;
376
377   if (! m_plot.fileRead (filename.c_str())) {
378     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
379     m_bBadFileOpen = true;
380     return false;
381   }
382   if (theApp->getVerboseLogging())
383     *theApp->getLog() << "Read plot file " << filename << "\n";
384   SetFilename (filename, true);
385   m_namePlot = filename.c_str();
386   Modify (false);
387   UpdateAllViews();
388   //GetFirstView()->OnUpdate (NULL, NULL);
389   m_bBadFileOpen = false;
390   
391   return true;
392 }
393
394
395 bool 
396 PlotFileDocument::IsModified(void) const
397 {
398   return wxDocument::IsModified();
399 }
400
401 void 
402 PlotFileDocument::Modify (bool mod)
403 {
404   wxDocument::Modify(mod);
405 }
406
407 PlotFileView* 
408 PlotFileDocument::getView() const
409
410   return dynamic_cast<PlotFileView*>(GetFirstView()); 
411 }
412
413 void
414 PlotFileDocument::Activate()
415 {
416 #if CTSIM_MDI
417   getView()->getFrame()->Activate();
418 #endif
419 };
420
421 //////////////////////////////////////////////////////////////////////////
422 //
423 // TextFileDocument
424 //
425 //////////////////////////////////////////////////////////////////////////
426
427 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
428
429 bool 
430 TextFileDocument::OnSaveDocument(const wxString& filename)
431 {
432   TextFileView *view = getView();
433   if (! view->getTextCtrl()->SaveFile(filename))
434     return false;
435   Modify(false);
436   return true;
437 }
438
439 bool 
440 TextFileDocument::OnOpenDocument(const wxString& filename)
441 {
442   TextFileView *view = getView();
443   
444   if (! view->getTextCtrl()->LoadFile(filename)) {
445     m_bBadFileOpen = true;
446     return false;
447   }
448   
449   SetFilename (filename, true);
450   Modify (false);
451   //  UpdateAllViews();
452   m_bBadFileOpen = false;
453   return true;
454 }
455
456 bool 
457 TextFileDocument::IsModified(void) const
458 {
459   return false;
460   
461   TextFileView *view = getView();
462   
463   if (view)
464     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
465   else
466     return wxDocument::IsModified();
467 }
468
469
470 TextFileView* 
471 TextFileDocument::getView() const
472
473   return dynamic_cast<TextFileView*>(GetFirstView()); 
474 }
475
476 wxTextCtrl* 
477 TextFileDocument::getTextCtrl()
478
479   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
480 }
481
482 //////////////////////////////////////////////////////////////////////////
483 //
484 // Graph3dFileDocument
485 //
486 //////////////////////////////////////////////////////////////////////////
487
488 #if wxUSE_GLCANVAS
489
490 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
491
492 Graph3dFileDocument::Graph3dFileDocument(void) 
493 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
494 {
495 }
496
497 Graph3dFileDocument::~Graph3dFileDocument() 
498 {
499 }
500
501 bool 
502 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
503 {
504   Modify(false);
505   return true;
506 }
507
508 bool 
509 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
510 {
511   SetFilename (filename, true);
512   Modify (false);
513   //  UpdateAllViews();
514   m_bBadFileOpen = false;
515   return true;
516 }
517
518 bool 
519 Graph3dFileDocument::IsModified(void) const
520 {
521     return wxDocument::IsModified();
522 }
523
524
525 Graph3dFileView* 
526 Graph3dFileDocument::getView() const
527
528   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
529 }
530
531 bool
532 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
533 {
534   m_nx = rImageFile.nx();
535   m_ny = rImageFile.ny();
536   m_array = rImageFile.getArray();
537
538   return true;
539 }
540
541 void
542 Graph3dFileDocument::Activate()
543 {
544 #if CTSIM_MDI
545   getView()->getFrame()->Activate();
546 #endif
547 };
548
549
550 #endif // wxUSE_GLCANVAS