r2098: *** 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.39 2002/06/02 17:57:02 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   getView()->setInitialClientSize();
83   UpdateAllViews();
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 bool
108 ImageFileDocument::Revert ()
109 {
110   if (IsModified()) {
111     wxString msg ("Revert to saved ");
112     msg += GetFilename();
113     msg += "?";
114     wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
115     if (dialog.ShowModal() == wxID_YES) {
116       if (theApp->getVerboseLogging())
117         *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
118       Modify (false);
119       OnOpenDocument (GetFilename());
120     }
121   }
122   //getView()->OnUpdate (getView(), NULL);
123   UpdateAllViews();
124
125   return true;
126 }
127
128 void
129 ImageFileDocument::Activate()
130 {
131 #if CTSIM_MDI
132   getView()->getFrame()->Activate();
133 #endif
134 };
135
136 // BackgroundProcessingDocument - Base Class
137
138 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
139 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
140 END_EVENT_TABLE()
141
142 #ifdef HAVE_WXTHREADS
143 void
144 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
145 {
146   wxCriticalSectionLocker locker (m_criticalSection);
147   if (pSupervisor)
148     m_vecpBackgroundSupervisors.push_back (pSupervisor);
149 }
150
151 void
152 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
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 #endif
170
171 void
172 BackgroundProcessingDocument::cancelRunningTasks()
173 {
174 #ifdef HAVE_WXTHREADS
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 #endif
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   getView()->setInitialClientSize();
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 void
255 ProjectionFileDocument::Activate()
256 {
257 #if CTSIM_MDI
258   getView()->getFrame()->Activate();
259 #endif
260 };
261
262 // PhantomFileDocument
263
264 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingTask)
265
266 PhantomFileDocument::~PhantomFileDocument()
267 {
268   cancelRunningTasks();
269 }
270
271 bool 
272 PhantomFileDocument::OnOpenDocument(const wxString& filename)
273 {
274   if (! OnSaveModified())
275     return false;
276
277   wxString myFilename = filename;
278   if (wxFile::Exists (myFilename)) {
279     m_phantom.createFromFile (myFilename);
280     if (theApp->getVerboseLogging())
281       *theApp->getLog() << "Read phantom file " << filename << "\n";
282   } else {
283     myFilename.Replace (".phm", "");
284     m_phantom.createFromPhantom (myFilename);
285   }
286   m_namePhantom = myFilename;
287   SetFilename (myFilename, true);
288   if (m_phantom.fail()) {
289     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
290     m_bBadFileOpen = true;
291     return false;
292   }
293   m_idPhantom = m_phantom.id();
294   Modify(false);
295   UpdateAllViews();
296   m_bBadFileOpen = false;
297   
298   return true;
299 }
300
301 bool 
302 PhantomFileDocument::OnSaveDocument(const wxString& filename)
303 {
304   if (! m_phantom.fileWrite (filename.c_str())) {
305     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
306     return false;
307   }
308   if (theApp->getVerboseLogging())
309     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
310   Modify(false);
311   return true;
312 }
313
314 bool 
315 PhantomFileDocument::IsModified(void) const
316 {
317   return false;
318 }
319
320 void 
321 PhantomFileDocument::Modify(bool mod)
322 {
323   wxDocument::Modify(mod);
324 }
325
326
327 PhantomFileView* 
328 PhantomFileDocument::getView() const
329
330   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
331 }
332
333 void
334 PhantomFileDocument::Activate()
335 {
336 #if CTSIM_MDI
337   getView()->getFrame()->Activate();
338 #endif
339 };
340
341 // PlotFileDocument
342
343 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
344
345 bool 
346 PlotFileDocument::OnSaveDocument(const wxString& filename)
347 {
348   m_namePlot = filename.c_str();
349   if (! m_plot.fileWrite (filename)) {
350     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
351     return false;
352   }
353   if (theApp->getVerboseLogging())
354     *theApp->getLog() << "Wrote plot file " << filename << "\n";
355   Modify(false);
356   return true;
357 }
358
359 bool 
360 PlotFileDocument::OnOpenDocument(const wxString& filename)
361 {
362   if (! OnSaveModified())
363     return false;
364
365   if (! m_plot.fileRead (filename.c_str())) {
366     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
367     m_bBadFileOpen = true;
368     return false;
369   }
370   if (theApp->getVerboseLogging())
371     *theApp->getLog() << "Read plot file " << filename << "\n";
372   SetFilename (filename, true);
373   m_namePlot = filename.c_str();
374   Modify (false);
375   UpdateAllViews();
376   //GetFirstView()->OnUpdate (NULL, NULL);
377   m_bBadFileOpen = false;
378   
379   return true;
380 }
381
382
383 bool 
384 PlotFileDocument::IsModified(void) const
385 {
386   return wxDocument::IsModified();
387 }
388
389 void 
390 PlotFileDocument::Modify (bool mod)
391 {
392   wxDocument::Modify(mod);
393 }
394
395 PlotFileView* 
396 PlotFileDocument::getView() const
397
398   return dynamic_cast<PlotFileView*>(GetFirstView()); 
399 }
400
401 void
402 PlotFileDocument::Activate()
403 {
404 #if CTSIM_MDI
405   getView()->getFrame()->Activate();
406 #endif
407 };
408
409 //////////////////////////////////////////////////////////////////////////
410 //
411 // TextFileDocument
412 //
413 //////////////////////////////////////////////////////////////////////////
414
415 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
416
417 bool 
418 TextFileDocument::OnSaveDocument(const wxString& filename)
419 {
420   TextFileView *view = getView();
421   if (! view->getTextCtrl()->SaveFile(filename))
422     return false;
423   Modify(false);
424   return true;
425 }
426
427 bool 
428 TextFileDocument::OnOpenDocument(const wxString& filename)
429 {
430   TextFileView *view = getView();
431   
432   if (! view->getTextCtrl()->LoadFile(filename)) {
433     m_bBadFileOpen = true;
434     return false;
435   }
436   
437   SetFilename (filename, true);
438   Modify (false);
439   //  UpdateAllViews();
440   m_bBadFileOpen = false;
441   return true;
442 }
443
444 bool 
445 TextFileDocument::IsModified(void) const
446 {
447   return false;
448   
449   TextFileView *view = getView();
450   
451   if (view)
452     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
453   else
454     return wxDocument::IsModified();
455 }
456
457
458 TextFileView* 
459 TextFileDocument::getView() const
460
461   return dynamic_cast<TextFileView*>(GetFirstView()); 
462 }
463
464 wxTextCtrl* 
465 TextFileDocument::getTextCtrl()
466
467   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
468 }
469
470 //////////////////////////////////////////////////////////////////////////
471 //
472 // Graph3dFileDocument
473 //
474 //////////////////////////////////////////////////////////////////////////
475
476 #if wxUSE_GLCANVAS
477
478 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
479
480 Graph3dFileDocument::Graph3dFileDocument(void) 
481 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
482 {
483 }
484
485 Graph3dFileDocument::~Graph3dFileDocument() 
486 {
487 }
488
489 bool 
490 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
491 {
492   Modify(false);
493   return true;
494 }
495
496 bool 
497 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
498 {
499   SetFilename (filename, true);
500   Modify (false);
501   //  UpdateAllViews();
502   m_bBadFileOpen = false;
503   return true;
504 }
505
506 bool 
507 Graph3dFileDocument::IsModified(void) const
508 {
509     return wxDocument::IsModified();
510 }
511
512
513 Graph3dFileView* 
514 Graph3dFileDocument::getView() const
515
516   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
517 }
518
519 bool
520 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
521 {
522   m_nx = rImageFile.nx();
523   m_ny = rImageFile.ny();
524   m_array = rImageFile.getArray();
525
526   return true;
527 }
528
529 void
530 Graph3dFileDocument::Activate()
531 {
532 #if CTSIM_MDI
533   getView()->getFrame()->Activate();
534 #endif
535 };
536
537
538 #endif // wxUSE_GLCANVAS