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