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