r11859: Canonicalize whitespace
[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$
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 #include "wx/wxprec.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33 #include "wx/txtstrm.h"
34 #include "wx/file.h"
35 #include "wx/thread.h"
36
37 #if !wxUSE_DOC_VIEW_ARCHITECTURE
38 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
39 #endif
40
41 #include "ct.h"
42 #include "ctsim.h"
43 #include "docs.h"
44 #include "views.h"
45 #include "threadrecon.h"
46
47
48 // ImageFileDocument
49
50 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
51
52 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
53 {
54   if (! m_pImageFile->fileWrite (filename)) {
55     *theApp->getLog() << "Unable to write image file " << filename << "\n";
56     return false;
57   }
58   if (theApp->getVerboseLogging())
59     *theApp->getLog() << "Wrote image file " << filename << "\n";
60   Modify(false);
61   return true;
62 }
63
64 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
65 {
66   if (! OnSaveModified())
67     return false;
68
69   if (! m_pImageFile->fileRead (filename.c_str())) {
70     *theApp->getLog() << "Unable to read image file " << filename << "\n";
71     m_bBadFileOpen = true;
72     return false;
73   }
74
75   if (theApp->getVerboseLogging() && filename != "")
76     *theApp->getLog() << "Read image file " << filename << "\n";
77
78   SetFilename(filename, true);
79   Modify(false);
80   getView()->setInitialClientSize();
81   UpdateAllViews();
82   m_bBadFileOpen = false;
83
84   return true;
85 }
86
87 bool
88 ImageFileDocument::IsModified(void) const
89 {
90   return wxDocument::IsModified();
91 }
92
93 void
94 ImageFileDocument::Modify(bool mod)
95 {
96   wxDocument::Modify(mod);
97 }
98
99 ImageFileView*
100 ImageFileDocument::getView() const
101 {
102   return dynamic_cast<ImageFileView*>(GetFirstView());
103 }
104
105 bool
106 ImageFileDocument::Revert ()
107 {
108   if (IsModified()) {
109     wxString msg ("Revert to saved ");
110     msg += GetFilename();
111     msg += "?";
112     wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
113     if (dialog.ShowModal() == wxID_YES) {
114       if (theApp->getVerboseLogging())
115         *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
116       Modify (false);
117       OnOpenDocument (GetFilename());
118     }
119   }
120   UpdateAllViews();
121
122   return true;
123 }
124
125 void
126 ImageFileDocument::Activate()
127 {
128 #if CTSIM_MDI
129   getView()->getFrame()->Activate();
130 #endif
131 };
132
133 // BackgroundProcessingDocument - Base Class
134
135 IMPLEMENT_DYNAMIC_CLASS(BackgroundProcessingDocument, wxDocument)
136 BEGIN_EVENT_TABLE(BackgroundProcessingDocument, wxDocument)
137 END_EVENT_TABLE()
138
139 #ifdef HAVE_WXTHREADS
140 void
141 BackgroundProcessingDocument::addBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
142 {
143   wxCriticalSectionLocker locker (m_criticalSection);
144   if (pSupervisor)
145     m_vecpBackgroundSupervisors.push_back (pSupervisor);
146 }
147
148 void
149 BackgroundProcessingDocument::removeBackgroundSupervisor (BackgroundSupervisor* pSupervisor)
150 {
151   m_criticalSection.Enter();
152   bool bFound = false;
153   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
154         i != m_vecpBackgroundSupervisors.end();
155         i++)
156           if (*i == pSupervisor) {
157             m_vecpBackgroundSupervisors.erase(i);
158             bFound = true;
159             break;
160         }
161   m_criticalSection.Leave();
162
163   if (! bFound)
164      sys_error (ERR_SEVERE, "Could not find background task [OnRemoveBackground]");
165 }
166 #endif
167
168 void
169 BackgroundProcessingDocument::cancelRunningTasks()
170 {
171 #ifdef HAVE_WXTHREADS
172   m_criticalSection.Enter();
173   for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
174         i != m_vecpBackgroundSupervisors.end(); i++)
175           (*i)->onCancel();
176   m_criticalSection.Leave();
177
178   while (m_vecpBackgroundSupervisors.size() > 0) {
179      ::wxYield();
180      ::wxUsleep(50);
181   }
182 #endif
183 }
184
185
186 // ProjectionFileDocument
187
188 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, BackgroundProcessingDocument)
189
190 bool
191 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
192 {
193   if (! m_pProjectionFile->write (filename.c_str())) {
194     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
195     return false;
196   }
197   if (theApp->getVerboseLogging())
198     *theApp->getLog() << "Wrote projection file " << filename << "\n";
199   Modify(false);
200   return true;
201 }
202
203 ProjectionFileDocument::~ProjectionFileDocument()
204 {
205   cancelRunningTasks();
206
207   delete m_pProjectionFile;
208 }
209
210 bool
211 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
212 {
213   if (! OnSaveModified())
214     return false;
215
216   if (! m_pProjectionFile->read (filename.c_str())) {
217     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
218     m_bBadFileOpen = true;
219     return false;
220   }
221   m_bBadFileOpen = false;
222
223   if (theApp->getVerboseLogging() && filename != "")
224     *theApp->getLog() << "Read projection file " << filename << "\n";
225
226   SetFilename(filename, true);
227   Modify(false);
228   getView()->setInitialClientSize();
229   UpdateAllViews();
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 void
254 ProjectionFileDocument::Activate()
255 {
256 #if CTSIM_MDI
257   getView()->getFrame()->Activate();
258 #endif
259 };
260
261 // PhantomFileDocument
262
263 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, BackgroundProcessingDocument)
264
265 PhantomFileDocument::~PhantomFileDocument()
266 {
267   cancelRunningTasks();
268 }
269
270 bool
271 PhantomFileDocument::OnOpenDocument(const wxString& constFilename)
272 {
273   if (! OnSaveModified())
274     return false;
275
276   wxString filename (constFilename);
277
278   if (wxFile::Exists (filename)) {
279     m_phantom.createFromFile (filename);
280     if (theApp->getVerboseLogging())
281       *theApp->getLog() << "Read phantom file " << filename << "\n";
282   } else {
283     filename.Replace (".phm", "");
284     m_phantom.createFromPhantom (filename);
285   }
286   m_namePhantom = filename;
287   SetFilename (filename, true);
288   if (m_phantom.fail()) {
289     *theApp->getLog() << "Failure creating phantom " << filename << "\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   m_bBadFileOpen = false;
371
372   if (theApp->getVerboseLogging() && filename != "")
373     *theApp->getLog() << "Read plot file " << filename << "\n";
374
375   SetFilename (filename, true);
376   m_namePlot = filename.c_str();
377   Modify (false);
378   getView()->setInitialClientSize();
379   UpdateAllViews();
380
381   return true;
382 }
383
384
385 bool
386 PlotFileDocument::IsModified(void) const
387 {
388   return wxDocument::IsModified();
389 }
390
391 void
392 PlotFileDocument::Modify (bool mod)
393 {
394   wxDocument::Modify(mod);
395 }
396
397 PlotFileView*
398 PlotFileDocument::getView() const
399 {
400   return dynamic_cast<PlotFileView*>(GetFirstView());
401 }
402
403 void
404 PlotFileDocument::Activate()
405 {
406 #if CTSIM_MDI
407   getView()->getFrame()->Activate();
408 #endif
409 };
410
411 //////////////////////////////////////////////////////////////////////////
412 //
413 // TextFileDocument
414 //
415 //////////////////////////////////////////////////////////////////////////
416
417 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
418
419 bool
420 TextFileDocument::OnSaveDocument(const wxString& filename)
421 {
422   TextFileView *view = getView();
423   if (! view->getTextCtrl()->SaveFile(filename))
424     return false;
425   Modify(false);
426   return true;
427 }
428
429 bool
430 TextFileDocument::OnOpenDocument(const wxString& filename)
431 {
432   TextFileView *view = getView();
433
434   if (! view->getTextCtrl()->LoadFile(filename)) {
435     m_bBadFileOpen = true;
436     return false;
437   }
438
439   SetFilename (filename, true);
440   Modify (false);
441   UpdateAllViews();
442   m_bBadFileOpen = false;
443   return true;
444 }
445
446 bool
447 TextFileDocument::IsModified(void) const
448 {
449   return false;
450
451   TextFileView *view = getView();
452
453   if (view)
454     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
455   else
456     return wxDocument::IsModified();
457 }
458
459
460 TextFileView*
461 TextFileDocument::getView() const
462 {
463   return dynamic_cast<TextFileView*>(GetFirstView());
464 }
465
466 wxTextCtrl*
467 TextFileDocument::getTextCtrl()
468 {
469   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
470 }
471
472 //////////////////////////////////////////////////////////////////////////
473 //
474 // Graph3dFileDocument
475 //
476 //////////////////////////////////////////////////////////////////////////
477
478 #if wxUSE_GLCANVAS
479
480 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
481
482 Graph3dFileDocument::Graph3dFileDocument(void)
483 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
484 {
485 }
486
487 Graph3dFileDocument::~Graph3dFileDocument()
488 {
489 }
490
491 bool
492 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
493 {
494   Modify(false);
495   return true;
496 }
497
498 bool
499 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
500 {
501   SetFilename (filename, true);
502   Modify (false);
503   getView()->setInitialClientSize();
504   UpdateAllViews();
505   m_bBadFileOpen = false;
506   return true;
507 }
508
509 bool
510 Graph3dFileDocument::IsModified(void) const
511 {
512     return wxDocument::IsModified();
513 }
514
515
516 Graph3dFileView*
517 Graph3dFileDocument::getView() const
518 {
519   return dynamic_cast<Graph3dFileView*>(GetFirstView());
520 }
521
522 bool
523 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
524 {
525   m_nx = rImageFile.nx();
526   m_ny = rImageFile.ny();
527   m_array = rImageFile.getArray();
528
529   return true;
530 }
531
532 void
533 Graph3dFileDocument::Activate()
534 {
535 #if CTSIM_MDI
536   getView()->getFrame()->Activate();
537 #endif
538 };
539
540
541 #endif // wxUSE_GLCANVAS