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