r2106: *** 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.42 2002/06/04 19:24:03 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& constFilename)
227 {
228   if (! OnSaveModified())
229     return false;
230
231   wxString filename (constFilename);
232
233   if (filename == szNEW_PROJECTION_FILENAME)
234     filename = "";
235   else if (! m_pProjectionFile->read (filename.c_str())) {
236     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
237     m_bBadFileOpen = true;
238     return false;
239   }
240
241   if (theApp->getVerboseLogging() && filename != "")
242     *theApp->getLog() << "Read projection file " << filename << "\n";
243
244   SetFilename(filename, true);
245   Modify(false);
246   getView()->setInitialClientSize();
247   UpdateAllViews();
248   m_bBadFileOpen = false;
249   
250   return true;
251 }
252
253 bool 
254 ProjectionFileDocument::IsModified(void) const
255 {
256   return wxDocument::IsModified();
257 }
258
259 void 
260 ProjectionFileDocument::Modify(bool mod)
261 {
262   wxDocument::Modify(mod);
263 }
264
265
266 ProjectionFileView* 
267 ProjectionFileDocument::getView() const
268
269   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
270 }
271
272 void
273 ProjectionFileDocument::Activate()
274 {
275 #if CTSIM_MDI
276   getView()->getFrame()->Activate();
277 #endif
278 };
279
280 // PhantomFileDocument
281
282 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
283
284 PhantomFileDocument::~PhantomFileDocument()
285 {
286   cancelRunningTasks();
287 }
288
289 bool 
290 PhantomFileDocument::OnOpenDocument(const wxString& filename)
291 {
292   if (! OnSaveModified())
293     return false;
294
295   wxString myFilename = filename;
296   if (wxFile::Exists (myFilename)) {
297     m_phantom.createFromFile (myFilename);
298     if (theApp->getVerboseLogging())
299       *theApp->getLog() << "Read phantom file " << filename << "\n";
300   } else {
301     myFilename.Replace (".phm", "");
302     m_phantom.createFromPhantom (myFilename);
303   }
304   m_namePhantom = myFilename;
305   SetFilename (myFilename, true);
306   if (m_phantom.fail()) {
307     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
308     m_bBadFileOpen = true;
309     return false;
310   }
311   m_idPhantom = m_phantom.id();
312   Modify(false);
313   UpdateAllViews();
314   m_bBadFileOpen = false;
315   
316   return true;
317 }
318
319 bool 
320 PhantomFileDocument::OnSaveDocument(const wxString& filename)
321 {
322   if (! m_phantom.fileWrite (filename.c_str())) {
323     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
324     return false;
325   }
326   if (theApp->getVerboseLogging())
327     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
328   Modify(false);
329   return true;
330 }
331
332 bool 
333 PhantomFileDocument::IsModified(void) const
334 {
335   return false;
336 }
337
338 void 
339 PhantomFileDocument::Modify(bool mod)
340 {
341   wxDocument::Modify(mod);
342 }
343
344
345 PhantomFileView* 
346 PhantomFileDocument::getView() const
347
348   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
349 }
350
351 void
352 PhantomFileDocument::Activate()
353 {
354 #if CTSIM_MDI
355   getView()->getFrame()->Activate();
356 #endif
357 };
358
359 // PlotFileDocument
360
361 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
362
363 bool 
364 PlotFileDocument::OnSaveDocument(const wxString& filename)
365 {
366   m_namePlot = filename.c_str();
367   if (! m_plot.fileWrite (filename)) {
368     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
369     return false;
370   }
371   if (theApp->getVerboseLogging())
372     *theApp->getLog() << "Wrote plot file " << filename << "\n";
373   Modify(false);
374   return true;
375 }
376
377 bool 
378 PlotFileDocument::OnOpenDocument(const wxString& constFilename)
379 {
380   if (! OnSaveModified())
381     return false;
382
383   wxString filename (constFilename);
384
385   if (filename == szNEW_PLOT_FILENAME)
386     filename = "";
387   else if (! m_plot.fileRead (filename.c_str())) {
388     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
389     m_bBadFileOpen = true;
390     return false;
391   }
392
393   if (theApp->getVerboseLogging() && filename != "")
394     *theApp->getLog() << "Read plot file " << filename << "\n";
395
396   SetFilename (filename, true);
397   m_namePlot = filename.c_str();
398   Modify (false);
399   UpdateAllViews();
400   m_bBadFileOpen = false;
401   
402   return true;
403 }
404
405
406 bool 
407 PlotFileDocument::IsModified(void) const
408 {
409   return wxDocument::IsModified();
410 }
411
412 void 
413 PlotFileDocument::Modify (bool mod)
414 {
415   wxDocument::Modify(mod);
416 }
417
418 PlotFileView* 
419 PlotFileDocument::getView() const
420
421   return dynamic_cast<PlotFileView*>(GetFirstView()); 
422 }
423
424 void
425 PlotFileDocument::Activate()
426 {
427 #if CTSIM_MDI
428   getView()->getFrame()->Activate();
429 #endif
430 };
431
432 //////////////////////////////////////////////////////////////////////////
433 //
434 // TextFileDocument
435 //
436 //////////////////////////////////////////////////////////////////////////
437
438 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
439
440 bool 
441 TextFileDocument::OnSaveDocument(const wxString& filename)
442 {
443   TextFileView *view = getView();
444   if (! view->getTextCtrl()->SaveFile(filename))
445     return false;
446   Modify(false);
447   return true;
448 }
449
450 bool 
451 TextFileDocument::OnOpenDocument(const wxString& filename)
452 {
453   TextFileView *view = getView();
454   
455   if (! view->getTextCtrl()->LoadFile(filename)) {
456     m_bBadFileOpen = true;
457     return false;
458   }
459   
460   SetFilename (filename, true);
461   Modify (false);
462   UpdateAllViews();
463   m_bBadFileOpen = false;
464   return true;
465 }
466
467 bool 
468 TextFileDocument::IsModified(void) const
469 {
470   return false;
471   
472   TextFileView *view = getView();
473   
474   if (view)
475     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
476   else
477     return wxDocument::IsModified();
478 }
479
480
481 TextFileView* 
482 TextFileDocument::getView() const
483
484   return dynamic_cast<TextFileView*>(GetFirstView()); 
485 }
486
487 wxTextCtrl* 
488 TextFileDocument::getTextCtrl()
489
490   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
491 }
492
493 //////////////////////////////////////////////////////////////////////////
494 //
495 // Graph3dFileDocument
496 //
497 //////////////////////////////////////////////////////////////////////////
498
499 #if wxUSE_GLCANVAS
500
501 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
502
503 Graph3dFileDocument::Graph3dFileDocument(void) 
504 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
505 {
506 }
507
508 Graph3dFileDocument::~Graph3dFileDocument() 
509 {
510 }
511
512 bool 
513 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
514 {
515   Modify(false);
516   return true;
517 }
518
519 bool 
520 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
521 {
522   SetFilename (filename, true);
523   Modify (false);
524   UpdateAllViews();
525   m_bBadFileOpen = false;
526   return true;
527 }
528
529 bool 
530 Graph3dFileDocument::IsModified(void) const
531 {
532     return wxDocument::IsModified();
533 }
534
535
536 Graph3dFileView* 
537 Graph3dFileDocument::getView() const
538
539   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
540 }
541
542 bool
543 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
544 {
545   m_nx = rImageFile.nx();
546   m_ny = rImageFile.ny();
547   m_array = rImageFile.getArray();
548
549   return true;
550 }
551
552 void
553 Graph3dFileDocument::Activate()
554 {
555 #if CTSIM_MDI
556   getView()->getFrame()->Activate();
557 #endif
558 };
559
560
561 #endif // wxUSE_GLCANVAS