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