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