r569: 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.24 2001/02/21 20:13:03 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 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/wx.h"
41 #endif
42 #include "wx/txtstrm.h"
43 #include "wx/file.h"
44
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
47 #endif
48
49 #include "ct.h"
50 #include "ctsim.h"
51 #include "docs.h"
52 #include "views.h"
53
54 // ImageFileDocument
55
56 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
57
58 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
59 {
60   if (! m_pImageFile->fileWrite (filename)) {
61     *theApp->getLog() << "Unable to write image file " << filename << "\n";
62     return false;
63   }
64   if (theApp->getVerboseLogging())
65     *theApp->getLog() << "Wrote image file " << filename << "\n";
66   Modify(false);
67   return true;
68 }
69
70 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
71 {
72   if (! OnSaveModified())
73     return false;
74
75   if (! m_pImageFile->fileRead (filename.c_str())) {
76     *theApp->getLog() << "Unable to read image file " << filename << "\n";
77     m_bBadFileOpen = true;
78     return false;
79   }
80   if (theApp->getVerboseLogging())
81     *theApp->getLog() << "Read image file " << filename << "\n";
82   SetFilename(filename, true);  
83   Modify(false);
84   UpdateAllViews();
85   getView()->OnUpdate (getView(), NULL);
86   m_bBadFileOpen = false;
87
88   return true;
89 }
90
91 bool 
92 ImageFileDocument::IsModified(void) const
93 {
94   return wxDocument::IsModified();
95 }
96
97 void 
98 ImageFileDocument::Modify(bool mod)
99 {
100   wxDocument::Modify(mod);
101 }
102
103 ImageFileView* 
104 ImageFileDocument::getView() const
105
106   return dynamic_cast<ImageFileView*>(GetFirstView()); 
107 }
108
109
110 bool
111 ImageFileDocument::Revert ()
112 {
113   if (IsModified()) {
114     wxString msg ("Revert to saved ");
115     msg += GetFilename();
116     msg += "?";
117     wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
118     if (dialog.ShowModal() == wxID_YES) {
119       if (theApp->getVerboseLogging())
120         *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
121       Modify (false);
122       OnOpenDocument (GetFilename());
123     }
124   }
125   getView()->OnUpdate (getView(), NULL);
126   UpdateAllViews();
127
128   return true;
129 }
130
131 // ProjectionFileDocument
132
133 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
134
135 bool 
136 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
137 {
138   if (! m_pProjectionFile->write (filename.c_str())) {
139     *theApp->getLog() << "Unable to write projection file " << filename << "\n";
140     return false;
141   }
142   if (theApp->getVerboseLogging())
143     *theApp->getLog() << "Wrote projection file " << filename << "\n";
144   Modify(false);
145   return true;
146 }
147
148 bool 
149 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
150 {
151   if (! OnSaveModified())
152     return false;
153
154   if (! m_pProjectionFile->read (filename.c_str())) {
155     *theApp->getLog() << "Unable to read projection file " << filename << "\n";
156     m_bBadFileOpen = true;
157     return false;
158   }
159   if (theApp->getVerboseLogging())
160     *theApp->getLog() << "Read projection file " << filename << "\n";
161   SetFilename(filename, true);
162   Modify(false);
163   UpdateAllViews();
164   GetFirstView()->OnUpdate (GetFirstView(), NULL);
165   m_bBadFileOpen = false;
166   
167   return true;
168 }
169
170 bool 
171 ProjectionFileDocument::IsModified(void) const
172 {
173   return wxDocument::IsModified();
174 }
175
176 void 
177 ProjectionFileDocument::Modify(bool mod)
178 {
179   wxDocument::Modify(mod);
180 }
181
182
183 ProjectionFileView* 
184 ProjectionFileDocument::getView() const
185
186   return dynamic_cast<ProjectionFileView*>(GetFirstView()); 
187 }
188
189 // PhantomFileDocument
190
191 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
192
193 bool 
194 PhantomFileDocument::OnOpenDocument(const wxString& filename)
195 {
196   if (! OnSaveModified())
197     return false;
198
199   wxString myFilename = filename;
200   if (wxFile::Exists (myFilename)) {
201     m_phantom.createFromFile (myFilename);
202     if (theApp->getVerboseLogging())
203       *theApp->getLog() << "Read phantom file " << filename << "\n";
204   } else {
205     myFilename.Replace (".phm", "");
206     m_phantom.createFromPhantom (myFilename);
207   }
208   m_namePhantom = myFilename;
209   SetFilename (myFilename, true);
210   if (m_phantom.fail()) {
211     *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
212     m_bBadFileOpen = true;
213     return false;
214   }
215   m_idPhantom = m_phantom.id();
216   Modify(false);
217   UpdateAllViews();
218   GetFirstView()->OnUpdate (GetFirstView(), NULL);
219   m_bBadFileOpen = false;
220   
221   return true;
222 }
223
224 bool 
225 PhantomFileDocument::OnSaveDocument(const wxString& filename)
226 {
227   if (! m_phantom.fileWrite (filename.c_str())) {
228     *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
229     return false;
230   }
231   if (theApp->getVerboseLogging())
232     *theApp->getLog() << "Wrote phantom file " << filename << "\n";
233   Modify(false);
234   return true;
235 }
236
237 bool 
238 PhantomFileDocument::IsModified(void) const
239 {
240   return false;
241 }
242
243 void 
244 PhantomFileDocument::Modify(bool mod)
245 {
246   wxDocument::Modify(mod);
247 }
248
249
250 PhantomFileView* 
251 PhantomFileDocument::getView() const
252
253   return dynamic_cast<PhantomFileView*>(GetFirstView()); 
254 }
255
256 // PlotFileDocument
257
258 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
259
260 bool 
261 PlotFileDocument::OnSaveDocument(const wxString& filename)
262 {
263   m_namePlot = filename.c_str();
264   if (! m_plot.fileWrite (filename)) {
265     *theApp->getLog() << "Unable to write plot file " << filename << "\n";
266     return false;
267   }
268   if (theApp->getVerboseLogging())
269     *theApp->getLog() << "Wrote plot file " << filename << "\n";
270   Modify(false);
271   return true;
272 }
273
274 bool 
275 PlotFileDocument::OnOpenDocument(const wxString& filename)
276 {
277   if (! OnSaveModified())
278     return false;
279
280   if (! m_plot.fileRead (filename.c_str())) {
281     *theApp->getLog() << "Unable to read plot file " << filename << "\n";
282     m_bBadFileOpen = true;
283     return false;
284   }
285   if (theApp->getVerboseLogging())
286     *theApp->getLog() << "Read plot file " << filename << "\n";
287   SetFilename (filename, true);
288   m_namePlot = filename.c_str();
289   Modify (false);
290   UpdateAllViews();
291   GetFirstView()->OnUpdate (NULL, NULL);
292   m_bBadFileOpen = false;
293   
294   return true;
295 }
296
297
298 bool 
299 PlotFileDocument::IsModified(void) const
300 {
301   return wxDocument::IsModified();
302 }
303
304 void 
305 PlotFileDocument::Modify (bool mod)
306 {
307   wxDocument::Modify(mod);
308 }
309
310 PlotFileView* 
311 PlotFileDocument::getView() const
312
313   return dynamic_cast<PlotFileView*>(GetFirstView()); 
314 }
315
316 //////////////////////////////////////////////////////////////////////////
317 //
318 // TextFileDocument
319 //
320 //////////////////////////////////////////////////////////////////////////
321
322 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
323
324 bool 
325 TextFileDocument::OnSaveDocument(const wxString& filename)
326 {
327   TextFileView *view = getView();
328   if (! view->getTextCtrl()->SaveFile(filename))
329     return false;
330   Modify(false);
331   return true;
332 }
333
334 bool 
335 TextFileDocument::OnOpenDocument(const wxString& filename)
336 {
337   TextFileView *view = getView();
338   
339   if (! view->getTextCtrl()->LoadFile(filename)) {
340     m_bBadFileOpen = true;
341     return false;
342   }
343   
344   SetFilename (filename, true);
345   Modify (false);
346   UpdateAllViews();
347   m_bBadFileOpen = false;
348   return true;
349 }
350
351 bool 
352 TextFileDocument::IsModified(void) const
353 {
354   return false;
355   
356   TextFileView *view = getView();
357   
358   if (view)
359     return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
360   else
361     return wxDocument::IsModified();
362 }
363
364
365 TextFileView* 
366 TextFileDocument::getView() const
367
368   return dynamic_cast<TextFileView*>(GetFirstView()); 
369 }
370
371 wxTextCtrl* 
372 TextFileDocument::getTextCtrl()
373
374   return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl(); 
375 }
376
377 //////////////////////////////////////////////////////////////////////////
378 //
379 // Graph3dFileDocument
380 //
381 //////////////////////////////////////////////////////////////////////////
382
383 #if wxUSE_GLCANVAS
384
385 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
386
387 Graph3dFileDocument::Graph3dFileDocument(void) 
388 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
389 {
390 }
391
392 Graph3dFileDocument::~Graph3dFileDocument() 
393 {
394 //    delete [] m_pVertices;
395 //    delete [] m_pNormals;
396 }
397
398 bool 
399 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
400 {
401   Modify(false);
402   return true;
403 }
404
405 bool 
406 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
407 {
408   SetFilename (filename, true);
409   Modify (false);
410   UpdateAllViews();
411   m_bBadFileOpen = false;
412   return true;
413 }
414
415 bool 
416 Graph3dFileDocument::IsModified(void) const
417 {
418     return wxDocument::IsModified();
419 }
420
421
422 Graph3dFileView* 
423 Graph3dFileDocument::getView() const
424
425   return dynamic_cast<Graph3dFileView*>(GetFirstView()); 
426 }
427
428 bool
429 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
430 {
431 //  delete [] m_pVertices;
432 //  delete [] m_pNormals;
433
434
435   m_nx = rImageFile.nx();
436   m_ny = rImageFile.ny();
437   m_array = rImageFile.getArray();
438
439   return true;
440 }
441
442 #endif // wxUSE_GLCANVAS