r576: no message
[ctsim.git] / src / views.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          view.cpp
5 **   Purpose:       View & Canvas 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: views.cpp,v 1.115 2001/02/23 18:56:56 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 #include "wx/wxprec.h"
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #if !wxUSE_DOC_VIEW_ARCHITECTURE
34 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 #endif
36
37 #include "wx/image.h"
38 #include "wx/progdlg.h"
39
40 #include "ct.h"
41 #include "ctsim.h"
42 #include "docs.h"
43 #include "views.h"
44 #include "dialogs.h"
45 #include "dlgprojections.h"
46 #include "dlgreconstruct.h"
47 #include "backprojectors.h"
48 #include "reconstruct.h"
49 #include "timer.h"
50 #include "threadrecon.h"
51
52 #if defined(MSVC) || HAVE_SSTREAM
53 #include <sstream>
54 #else
55 #include <sstream_subst>
56 #endif
57
58
59 // ImageFileCanvas
60
61 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
62 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
63 EVT_CHAR(ImageFileCanvas::OnChar)
64 END_EVENT_TABLE()
65
66
67 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
68 : wxScrolledWindow(frame, -1, pos, size, style), m_pView(v), m_xCursor(-1), m_yCursor(-1)
69 {
70 }
71
72 ImageFileCanvas::~ImageFileCanvas()
73 {
74   m_pView = NULL;
75 }
76
77 void 
78 ImageFileCanvas::OnDraw(wxDC& dc)
79 {
80   if (m_pView)
81     m_pView->OnDraw(& dc);
82 }
83
84 void 
85 ImageFileCanvas::DrawRubberBandCursor (wxDC& dc, int x, int y)
86 {
87   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
88   int nx = rIF.nx();
89   int ny = rIF.ny();
90   
91   int yPt = ny - y - 1;
92   dc.SetLogicalFunction (wxINVERT);
93   dc.SetPen (*wxGREEN_PEN);
94   dc.DrawLine (0, yPt, nx, yPt);
95   dc.DrawLine (x, 0, x, ny);
96   dc.SetLogicalFunction (wxCOPY);
97 }
98
99 bool
100 ImageFileCanvas::GetCurrentCursor (int& x, int& y)
101 {
102   x = m_xCursor;
103   y = m_yCursor;
104   
105   if (m_xCursor >= 0 && m_yCursor >= 0)
106     return true;
107   else
108     return false;
109 }
110
111 void 
112 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
113 {
114   if (! m_pView)
115     return;
116   
117   wxClientDC dc(this);
118   PrepareDC(dc);
119   
120   wxPoint pt(event.GetLogicalPosition(dc));
121   
122   const ImageFileDocument* pIFDoc = m_pView->GetDocument();
123   if (! pIFDoc)
124     return;
125   const ImageFile& rIF = pIFDoc->getImageFile();
126   ImageFileArrayConst v = rIF.getArray();
127   int nx = rIF.nx();
128   int ny = rIF.ny();
129   const int yPt = ny - 1 - pt.y;
130   if (event.RightIsDown()) {
131     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
132       std::ostringstream os;
133       os << "Image value (" << pt.x << "," << yPt << ") = " << v[pt.x][yPt];
134       if (rIF.isComplex()) {
135         double dImag = rIF.getImaginaryArray()[pt.x][yPt];
136         if (dImag < 0)
137           os << " - " << -dImag;
138         else
139           os << " + " << dImag;
140         os << "i\n";
141       } else
142         os << "\n";
143       *theApp->getLog() << os.str().c_str();
144     } else
145       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
146   }
147   else if (event.LeftIsDown() || event.LeftUp() || event.RightUp()) {
148     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
149       if (m_xCursor >= 0 && m_yCursor >= 0) {
150         DrawRubberBandCursor (dc, m_xCursor, m_yCursor);
151       }
152       DrawRubberBandCursor (dc, pt.x, yPt);
153       m_xCursor = pt.x;
154       m_yCursor = yPt;
155       wxMenu* pMenu = m_pView->getMenuAnalyze();
156       if (pMenu && ! pMenu->IsEnabled(IFMENU_PLOT_ROW)) {
157         pMenu->Enable (IFMENU_PLOT_ROW, true);
158         pMenu->Enable (IFMENU_PLOT_COL, true);
159         pMenu->Enable (IFMENU_COMPARE_ROW, true);
160         pMenu->Enable (IFMENU_COMPARE_COL, true);
161         pMenu->Enable (IFMENU_PLOT_FFT_ROW, true);
162         pMenu->Enable (IFMENU_PLOT_FFT_COL, true);
163       }
164     } else
165       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
166   }
167   if (event.LeftUp()) {
168     std::ostringstream os;
169     os << "Selected column " << pt.x << " , row " << yPt << "\n";
170     *theApp->getLog() << os.str().c_str();
171   }
172 }
173
174 void
175 ImageFileCanvas::OnChar (wxKeyEvent& event)
176 {
177   if (event.GetKeyCode() == WXK_ESCAPE) {
178     m_xCursor = -1;
179     m_yCursor = -1;
180     if (m_pView)
181       m_pView->OnUpdate (NULL);
182   } else
183     wxScrolledWindow::OnChar (event);
184 }
185
186 wxSize
187 ImageFileCanvas::GetBestSize() const
188 {
189   if (! m_pView)
190     return wxSize(0,0);
191   
192   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
193   return wxSize (rIF.nx(), rIF.ny());
194 }
195
196
197 // ImageFileView
198
199 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
200
201 BEGIN_EVENT_TABLE(ImageFileView, wxView)
202 EVT_MENU(IFMENU_FILE_EXPORT, ImageFileView::OnExport)
203 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
204 EVT_MENU(IFMENU_VIEW_SCALE_MINMAX, ImageFileView::OnScaleMinMax)
205 EVT_MENU(IFMENU_VIEW_SCALE_AUTO, ImageFileView::OnScaleAuto)
206 EVT_MENU(IFMENU_VIEW_SCALE_FULL, ImageFileView::OnScaleFull)
207 EVT_MENU(IFMENU_COMPARE_IMAGES, ImageFileView::OnCompare)
208 EVT_MENU(IFMENU_COMPARE_ROW, ImageFileView::OnCompareRow)
209 EVT_MENU(IFMENU_COMPARE_COL, ImageFileView::OnCompareCol)
210 EVT_MENU(IFMENU_FILTER_INVERTVALUES, ImageFileView::OnInvertValues)
211 EVT_MENU(IFMENU_FILTER_SQUARE, ImageFileView::OnSquare)
212 EVT_MENU(IFMENU_FILTER_SQRT, ImageFileView::OnSquareRoot)
213 EVT_MENU(IFMENU_FILTER_LOG, ImageFileView::OnLog)
214 EVT_MENU(IFMENU_FILTER_EXP, ImageFileView::OnExp)
215 EVT_MENU(IFMENU_FILTER_FOURIER, ImageFileView::OnFourier)
216 EVT_MENU(IFMENU_FILTER_INVERSE_FOURIER, ImageFileView::OnInverseFourier)
217 EVT_MENU(IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, ImageFileView::OnShuffleFourierToNaturalOrder)
218 EVT_MENU(IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, ImageFileView::OnShuffleNaturalToFourierOrder)
219 EVT_MENU(IFMENU_IMAGE_ADD, ImageFileView::OnAdd)
220 EVT_MENU(IFMENU_IMAGE_SUBTRACT, ImageFileView::OnSubtract)
221 EVT_MENU(IFMENU_IMAGE_MULTIPLY, ImageFileView::OnMultiply)
222 EVT_MENU(IFMENU_IMAGE_DIVIDE, ImageFileView::OnDivide)
223 EVT_MENU(IFMENU_IMAGE_SCALESIZE, ImageFileView::OnScaleSize)
224 #if wxUSE_GLCANVAS
225 EVT_MENU(IFMENU_IMAGE_CONVERT3D, ImageFileView::OnConvert3d)
226 #endif
227 #ifdef HAVE_FFT
228 EVT_MENU(IFMENU_FILTER_FFT, ImageFileView::OnFFT)
229 EVT_MENU(IFMENU_FILTER_IFFT, ImageFileView::OnIFFT)
230 EVT_MENU(IFMENU_FILTER_FFT_ROWS, ImageFileView::OnFFTRows)
231 EVT_MENU(IFMENU_FILTER_IFFT_ROWS, ImageFileView::OnIFFTRows)
232 EVT_MENU(IFMENU_FILTER_FFT_COLS, ImageFileView::OnFFTCols)
233 EVT_MENU(IFMENU_FILTER_IFFT_COLS, ImageFileView::OnIFFTCols)
234 #endif
235 EVT_MENU(IFMENU_FILTER_MAGNITUDE, ImageFileView::OnMagnitude)
236 EVT_MENU(IFMENU_FILTER_PHASE, ImageFileView::OnPhase)
237 EVT_MENU(IFMENU_PLOT_ROW, ImageFileView::OnPlotRow)
238 EVT_MENU(IFMENU_PLOT_COL, ImageFileView::OnPlotCol)
239 #ifdef HAVE_FFT
240 EVT_MENU(IFMENU_PLOT_FFT_ROW, ImageFileView::OnPlotFFTRow)
241 EVT_MENU(IFMENU_PLOT_FFT_COL, ImageFileView::OnPlotFFTCol)
242 #endif
243 EVT_MENU(IFMENU_PLOT_HISTOGRAM, ImageFileView::OnPlotHistogram)
244 END_EVENT_TABLE()
245
246 ImageFileView::ImageFileView() 
247 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
248 {
249   m_iDefaultExportFormatID = ImageFile::FORMAT_PNG;
250 }
251
252 ImageFileView::~ImageFileView()
253 {
254   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
255   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
256 }
257
258
259 void
260 ImageFileView::OnProperties (wxCommandEvent& event)
261 {
262   const ImageFile& rIF = GetDocument()->getImageFile();
263   if (rIF.nx() == 0 || rIF.ny() == 0)
264     *theApp->getLog() << "Properties: empty imagefile\n";
265   else {
266     const std::string rFilename = m_pFrame->GetTitle().c_str();
267     std::ostringstream os;
268     double min, max, mean, mode, median, stddev;
269     rIF.statistics (rIF.getArray(), min, max, mean, mode, median, stddev);
270     os << "Filename: " << rFilename << "\n";
271     os << "Size: (" << rIF.nx() << "," << rIF.ny() << ")\n";
272     os << "Data type: ";
273     if (rIF.isComplex())
274       os << "Complex\n";
275     else
276       os << "Real\n";
277     os << "Minimum: "<<min<<"\nMaximum: "<<max<<"\nMean: "<<mean<<"\nMedian: "<<median<<"\nMode: "<<mode<<"\nStandard Deviation: "<<stddev << "\n";
278     if (rIF.isComplex()) {
279       rIF.statistics (rIF.getImaginaryArray(), min, max, mean, mode, median, stddev);
280       os << "Imaginary: min: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmedian: "<<median<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
281     }
282     if (rIF.nLabels() > 0) {
283       rIF.printLabelsBrief (os);
284     }
285     *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
286     wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
287     dialogMsg.ShowModal();
288   }
289 }
290
291 void 
292 ImageFileView::OnScaleAuto (wxCommandEvent& event)
293 {
294   const ImageFile& rIF = GetDocument()->getImageFile();
295   double min, max, mean, mode, median, stddev;
296   rIF.statistics(min, max, mean, mode, median, stddev);
297   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
298   int iRetVal = dialogAutoScale.ShowModal();
299   if (iRetVal == wxID_OK) {
300     m_bMinSpecified = true;
301     m_bMaxSpecified = true;
302     double dMin, dMax;
303     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
304       m_dMinPixel = dMin;
305       m_dMaxPixel = dMax;
306       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
307       OnUpdate (this, NULL);
308     }
309   }
310 }
311
312 void 
313 ImageFileView::OnScaleMinMax (wxCommandEvent& event)
314 {
315   const ImageFile& rIF = GetDocument()->getImageFile();
316   double min, max;
317   if (! m_bMinSpecified && ! m_bMaxSpecified)
318     rIF.getMinMax (min, max);
319   
320   if (m_bMinSpecified)
321     min = m_dMinPixel;
322   if (m_bMaxSpecified)
323     max = m_dMaxPixel;
324   
325   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Image Minimum & Maximum", min, max);
326   int retVal = dialogMinMax.ShowModal();
327   if (retVal == wxID_OK) {
328     m_bMinSpecified = true;
329     m_bMaxSpecified = true;
330     m_dMinPixel = dialogMinMax.getMinimum();
331     m_dMaxPixel = dialogMinMax.getMaximum();
332     OnUpdate (this, NULL);
333   }
334 }
335
336 void 
337 ImageFileView::OnScaleFull (wxCommandEvent& event)
338 {
339   if (m_bMinSpecified || m_bMaxSpecified) {
340     m_bMinSpecified = false;
341     m_bMaxSpecified = false;
342     OnUpdate (this, NULL);
343   }
344 }
345
346 void
347 ImageFileView::OnCompare (wxCommandEvent& event)
348 {
349   std::vector<ImageFileDocument*> vecIF;
350   theApp->getCompatibleImages (GetDocument(), vecIF);
351   
352   if (vecIF.size() == 0) {
353     wxMessageBox("There are no compatible image files open for comparision", "No comparison images");
354   } else {
355     DialogGetComparisonImage dialogGetCompare(getFrameForChild(), "Get Comparison Image", vecIF, true);
356     
357     if (dialogGetCompare.ShowModal() == wxID_OK) {
358       const ImageFile& rIF = GetDocument()->getImageFile();
359       ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
360       const ImageFile& rCompareIF = pCompareDoc->getImageFile();
361       std::ostringstream os;
362       double min, max, mean, mode, median, stddev;
363       rIF.statistics (min, max, mean, mode, median, stddev);
364       os << GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
365       rCompareIF.statistics (min, max, mean, mode, median, stddev);
366       os << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
367       double d, r, e;
368       rIF.comparativeStatistics (rCompareIF, d, r, e);
369       os << "Comparative Statistics: d=" << d << ", r=" << r << ", e=" << e << "\n";
370       *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
371       if (dialogGetCompare.getMakeDifferenceImage()) {
372         ImageFile* pDifferenceImage = new ImageFile;
373         
374         pDifferenceImage->setArraySize (rIF.nx(), rIF.ny());
375         if (! rIF.subtractImages (rCompareIF, *pDifferenceImage)) {
376           *theApp->getLog() << "Unable to subtract images\n";
377           delete pDifferenceImage;
378           return;
379         }
380         ImageFileDocument* pDifferenceDoc = theApp->newImageDoc();
381         if (! pDifferenceDoc) {
382           sys_error (ERR_SEVERE, "Unable to create image file");
383           return;
384         }
385         pDifferenceDoc->setImageFile (pDifferenceImage);
386         
387         wxString s = GetFrame()->GetTitle() + ": ";
388         pDifferenceImage->labelsCopy (rIF, s.c_str());
389         s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
390         pDifferenceImage->labelsCopy (rCompareIF, s.c_str());
391         std::ostringstream osLabel;
392         osLabel << "Compare image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() 
393           << " and " << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str() << ": "
394           << os.str().c_str();
395         pDifferenceImage->labelAdd (os.str().c_str());
396         if (theApp->getAskDeleteNewDocs())
397           pDifferenceDoc->Modify (true);
398         pDifferenceDoc->UpdateAllViews (this);
399         pDifferenceDoc->getView()->OnUpdate (this, NULL);
400         pDifferenceDoc->getView()->getFrame()->Show(true);
401       }
402       wxMessageBox(os.str().c_str(), "Image Comparison");
403     }
404   }
405 }
406
407 void
408 ImageFileView::OnInvertValues (wxCommandEvent& event)
409 {
410   ImageFile& rIF = GetDocument()->getImageFile();
411   rIF.invertPixelValues (rIF);
412   rIF.labelAdd ("Invert Pixel Values");
413   if (theApp->getAskDeleteNewDocs())
414     GetDocument()->Modify (true);
415   GetDocument()->UpdateAllViews (this);
416 }
417
418 void
419 ImageFileView::OnSquare (wxCommandEvent& event)
420 {
421   ImageFile& rIF = GetDocument()->getImageFile();
422   rIF.square (rIF);
423   rIF.labelAdd ("Square Pixel Values");
424   if (theApp->getAskDeleteNewDocs())
425     GetDocument()->Modify (true);
426   GetDocument()->UpdateAllViews (this);
427 }
428
429 void
430 ImageFileView::OnSquareRoot (wxCommandEvent& event)
431 {
432   ImageFile& rIF = GetDocument()->getImageFile();
433   rIF.sqrt (rIF);
434   rIF.labelAdd ("Square-root Pixel Values");
435   if (theApp->getAskDeleteNewDocs())
436     GetDocument()->Modify (true);
437   GetDocument()->UpdateAllViews (this);
438 }
439
440 void
441 ImageFileView::OnLog (wxCommandEvent& event)
442 {
443   ImageFile& rIF = GetDocument()->getImageFile();
444   rIF.log (rIF);
445   rIF.labelAdd ("Logrithm base-e Pixel Values");
446   if (theApp->getAskDeleteNewDocs())
447     GetDocument()->Modify (true);
448   GetDocument()->UpdateAllViews (this);
449 }
450
451 void
452 ImageFileView::OnExp (wxCommandEvent& event)
453 {
454   ImageFile& rIF = GetDocument()->getImageFile();
455   rIF.exp (rIF);
456   rIF.labelAdd ("Exponent base-e Pixel Values");
457   if (theApp->getAskDeleteNewDocs())
458     GetDocument()->Modify (true);
459   GetDocument()->UpdateAllViews (this);
460 }
461
462 void
463 ImageFileView::OnAdd (wxCommandEvent& event)
464 {
465   std::vector<ImageFileDocument*> vecIF;
466   theApp->getCompatibleImages (GetDocument(), vecIF);
467   
468   if (vecIF.size() == 0) {
469     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
470   } else {
471     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Add", vecIF, false);
472     
473     if (dialogGetCompare.ShowModal() == wxID_OK) {
474       ImageFile& rIF = GetDocument()->getImageFile();
475       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
476       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
477       ImageFileDocument* pNewDoc = theApp->newImageDoc();
478       if (! pNewDoc) {
479         sys_error (ERR_SEVERE, "Unable to create image file");
480         return;
481       }
482       ImageFile& newImage = pNewDoc->getImageFile();  
483       newImage.setArraySize (rIF.nx(), rIF.ny());
484       rIF.addImages (rRHSIF, newImage);
485       std::ostringstream os;
486       os << "Add image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
487         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
488       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
489       newImage.labelsCopy (rIF, s.c_str());
490       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
491       newImage.labelsCopy (rRHSIF, s.c_str());
492       newImage.labelAdd (os.str().c_str());
493       *theApp->getLog() << os.str().c_str() << "\n";
494       if (theApp->getAskDeleteNewDocs())
495         pNewDoc->Modify (true);
496       pNewDoc->UpdateAllViews (this);
497       pNewDoc->getView()->OnUpdate (this, NULL);
498       pNewDoc->getView()->getFrame()->Show(true);
499     }
500   }
501 }
502
503 void
504 ImageFileView::OnSubtract (wxCommandEvent& event)
505 {
506   std::vector<ImageFileDocument*> vecIF;
507   theApp->getCompatibleImages (GetDocument(), vecIF);
508   
509   if (vecIF.size() == 0) {
510     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
511   } else {
512     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Subtract", vecIF, false);
513     
514     if (dialogGetCompare.ShowModal() == wxID_OK) {
515       ImageFile& rIF = GetDocument()->getImageFile();
516       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
517       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
518       ImageFileDocument* pNewDoc = theApp->newImageDoc();
519       if (! pNewDoc) {
520         sys_error (ERR_SEVERE, "Unable to create image file");
521         return;
522       }
523       ImageFile& newImage = pNewDoc->getImageFile();  
524       newImage.setArraySize (rIF.nx(), rIF.ny());
525       rIF.subtractImages (rRHSIF, newImage);
526       std::ostringstream os;
527       os << "Subtract image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
528         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
529       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
530       newImage.labelsCopy (rIF, s.c_str());
531       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
532       newImage.labelsCopy (rRHSIF, s.c_str());
533       newImage.labelAdd (os.str().c_str());
534       *theApp->getLog() << os.str().c_str() << "\n";
535       if (theApp->getAskDeleteNewDocs())
536         pNewDoc->Modify (true);
537       pNewDoc->UpdateAllViews (this);
538       pNewDoc->getView()->OnUpdate (this, NULL);
539       pNewDoc->getView()->getFrame()->Show(true);
540     }
541   }
542 }
543
544 void
545 ImageFileView::OnMultiply (wxCommandEvent& event)
546 {
547   std::vector<ImageFileDocument*> vecIF;
548   theApp->getCompatibleImages (GetDocument(), vecIF);
549   
550   if (vecIF.size() == 0) {
551     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
552   } else {
553     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Multiply", vecIF, false);
554     
555     if (dialogGetCompare.ShowModal() == wxID_OK) {
556       ImageFile& rIF = GetDocument()->getImageFile();
557       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
558       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
559       ImageFileDocument* pNewDoc = theApp->newImageDoc();
560       if (! pNewDoc) {
561         sys_error (ERR_SEVERE, "Unable to create image file");
562         return;
563       }
564       ImageFile& newImage = pNewDoc->getImageFile();  
565       newImage.setArraySize (rIF.nx(), rIF.ny());
566       rIF.multiplyImages (rRHSIF, newImage);
567       std::ostringstream os;
568       os << "Multiply image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
569         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
570       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
571       newImage.labelsCopy (rIF, s.c_str());
572       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
573       newImage.labelsCopy (rRHSIF, s.c_str());
574       newImage.labelAdd (os.str().c_str());
575       *theApp->getLog() << os.str().c_str() << "\n";
576       if (theApp->getAskDeleteNewDocs())
577         pNewDoc->Modify (true);
578       pNewDoc->UpdateAllViews (this);
579       pNewDoc->getView()->OnUpdate (this, NULL);
580       pNewDoc->getView()->getFrame()->Show(true);
581     }
582   }
583 }
584
585 void
586 ImageFileView::OnDivide (wxCommandEvent& event)
587 {
588   std::vector<ImageFileDocument*> vecIF;
589   theApp->getCompatibleImages (GetDocument(), vecIF);
590   
591   if (vecIF.size() == 0) {
592     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
593   } else {
594     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Divide", vecIF, false);
595     
596     if (dialogGetCompare.ShowModal() == wxID_OK) {
597       ImageFile& rIF = GetDocument()->getImageFile();
598       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
599       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
600       ImageFileDocument* pNewDoc = theApp->newImageDoc();
601       if (! pNewDoc) {
602         sys_error (ERR_SEVERE, "Unable to create image file");
603         return;
604       }
605       ImageFile& newImage = pNewDoc->getImageFile();  
606       newImage.setArraySize (rIF.nx(), rIF.ny());
607       rIF.divideImages (rRHSIF, newImage);
608       std::ostringstream os;
609       os << "Divide image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " by " 
610         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
611       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
612       newImage.labelsCopy (rIF, s.c_str());
613       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
614       newImage.labelsCopy (rRHSIF, s.c_str());
615       newImage.labelAdd (os.str().c_str());
616       *theApp->getLog() << os.str().c_str() << "\n";
617       if (theApp->getAskDeleteNewDocs())
618         pNewDoc->Modify (true);
619       pNewDoc->UpdateAllViews (this);
620       pNewDoc->getView()->OnUpdate (this, NULL);
621       pNewDoc->getView()->getFrame()->Show(true);
622     }
623   }
624 }
625
626
627 #ifdef HAVE_FFT
628 void
629 ImageFileView::OnFFT (wxCommandEvent& event)
630 {
631   ImageFile& rIF = GetDocument()->getImageFile();
632   rIF.fft (rIF);
633   rIF.labelAdd ("FFT Image");
634   m_bMinSpecified = false;
635   m_bMaxSpecified = false;
636   if (theApp->getAskDeleteNewDocs())
637     GetDocument()->Modify (true);
638   GetDocument()->UpdateAllViews (this);
639 }
640
641 void
642 ImageFileView::OnIFFT (wxCommandEvent& event)
643 {
644   ImageFile& rIF = GetDocument()->getImageFile();
645   rIF.ifft (rIF);
646   rIF.labelAdd ("IFFT Image");
647   m_bMinSpecified = false;
648   m_bMaxSpecified = false;
649   if (theApp->getAskDeleteNewDocs())
650     GetDocument()->Modify (true);
651   GetDocument()->UpdateAllViews (this);
652 }
653
654 void
655 ImageFileView::OnFFTRows (wxCommandEvent& event)
656 {
657   ImageFile& rIF = GetDocument()->getImageFile();
658   rIF.fftRows (rIF);
659   rIF.labelAdd ("FFT Rows");
660   m_bMinSpecified = false;
661   m_bMaxSpecified = false;
662   if (theApp->getAskDeleteNewDocs())
663     GetDocument()->Modify (true);
664   GetDocument()->UpdateAllViews (this);
665 }
666
667 void
668 ImageFileView::OnIFFTRows (wxCommandEvent& event)
669 {
670   ImageFile& rIF = GetDocument()->getImageFile();
671   rIF.ifftRows (rIF);
672   rIF.labelAdd ("IFFT Rows");
673   m_bMinSpecified = false;
674   m_bMaxSpecified = false;
675   if (theApp->getAskDeleteNewDocs())
676     GetDocument()->Modify (true);
677   GetDocument()->UpdateAllViews (this);
678 }
679
680 void
681 ImageFileView::OnFFTCols (wxCommandEvent& event)
682 {
683   ImageFile& rIF = GetDocument()->getImageFile();
684   rIF.fftCols (rIF);
685   rIF.labelAdd ("FFT Columns");
686   m_bMinSpecified = false;
687   m_bMaxSpecified = false;
688   if (theApp->getAskDeleteNewDocs())
689     GetDocument()->Modify (true);
690   GetDocument()->UpdateAllViews (this);
691 }
692
693 void
694 ImageFileView::OnIFFTCols (wxCommandEvent& event)
695 {
696   ImageFile& rIF = GetDocument()->getImageFile();
697   rIF.ifftCols (rIF);
698   rIF.labelAdd ("IFFT Columns");
699   m_bMinSpecified = false;
700   m_bMaxSpecified = false;
701   if (theApp->getAskDeleteNewDocs())
702     GetDocument()->Modify (true);
703   GetDocument()->UpdateAllViews (this);
704 }
705 #endif
706
707 void
708 ImageFileView::OnFourier (wxCommandEvent& event)
709 {
710   ImageFile& rIF = GetDocument()->getImageFile();
711   wxProgressDialog dlgProgress (wxString("Fourier"), wxString("Fourier Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
712   rIF.fourier (rIF);
713   rIF.labelAdd ("Fourier Image");
714   m_bMinSpecified = false;
715   m_bMaxSpecified = false;
716   if (theApp->getAskDeleteNewDocs())
717     GetDocument()->Modify (true);
718   GetDocument()->UpdateAllViews (this);
719 }
720
721 void
722 ImageFileView::OnInverseFourier (wxCommandEvent& event)
723 {
724   ImageFile& rIF = GetDocument()->getImageFile();
725   wxProgressDialog dlgProgress (wxString("Inverse Fourier"), wxString("Inverse Fourier Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
726   rIF.inverseFourier (rIF);
727   rIF.labelAdd ("Inverse Fourier Image");
728   m_bMinSpecified = false;
729   m_bMaxSpecified = false;
730   if (theApp->getAskDeleteNewDocs())
731     GetDocument()->Modify (true);
732   GetDocument()->UpdateAllViews (this);
733 }
734
735 void
736 ImageFileView::OnShuffleNaturalToFourierOrder (wxCommandEvent& event)
737 {
738   ImageFile& rIF = GetDocument()->getImageFile();
739   Fourier::shuffleNaturalToFourierOrder (rIF);
740   rIF.labelAdd ("Shuffle Natural To Fourier Order");
741   m_bMinSpecified = false;
742   m_bMaxSpecified = false;
743   if (theApp->getAskDeleteNewDocs())
744     GetDocument()->Modify (true);
745   GetDocument()->UpdateAllViews (this);
746 }
747
748 void
749 ImageFileView::OnShuffleFourierToNaturalOrder (wxCommandEvent& event)
750 {
751   ImageFile& rIF = GetDocument()->getImageFile();
752   Fourier::shuffleFourierToNaturalOrder (rIF);
753   rIF.labelAdd ("Shuffle Fourier To Natural Order");
754   m_bMinSpecified = false;
755   m_bMaxSpecified = false;
756   if (theApp->getAskDeleteNewDocs())
757     GetDocument()->Modify (true);
758   GetDocument()->UpdateAllViews (this);
759 }
760
761 void
762 ImageFileView::OnMagnitude (wxCommandEvent& event)
763 {
764   ImageFile& rIF = GetDocument()->getImageFile();
765   if (rIF.isComplex()) {
766     rIF.magnitude (rIF);
767     rIF.labelAdd ("Magnitude of complex-image");
768     m_bMinSpecified = false;
769     m_bMaxSpecified = false;
770     if (theApp->getAskDeleteNewDocs())
771       GetDocument()->Modify (true);
772     GetDocument()->UpdateAllViews (this);
773   }
774 }
775
776 void
777 ImageFileView::OnPhase (wxCommandEvent& event)
778 {
779   ImageFile& rIF = GetDocument()->getImageFile();
780   if (rIF.isComplex()) {
781     rIF.phase (rIF);
782     rIF.labelAdd ("Phase of complex-image");
783     m_bMinSpecified = false;
784     m_bMaxSpecified = false;
785     if (theApp->getAskDeleteNewDocs())
786       GetDocument()->Modify (true);
787     GetDocument()->UpdateAllViews (this);
788   }
789 }
790
791
792 ImageFileCanvas* 
793 ImageFileView::CreateCanvas (wxFrame* parent)
794 {
795   ImageFileCanvas* pCanvas;
796   int width, height;
797   parent->GetClientSize(&width, &height);
798   
799   pCanvas = new ImageFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
800   
801   pCanvas->SetScrollbars(20, 20, 50, 50);
802   pCanvas->SetBackgroundColour(*wxWHITE);
803   pCanvas->Clear();
804   
805   return pCanvas;
806 }
807
808 #if CTSIM_MDI
809 wxDocMDIChildFrame*
810 #else
811 wxDocChildFrame*
812 #endif
813 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
814 {
815 #if CTSIM_MDI
816   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
817 #else
818   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
819 #endif
820   theApp->setIconForFrame (subframe);
821   
822   m_pFileMenu = new wxMenu;
823   
824   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
825   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
826   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
827   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
828   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
829   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
830   m_pFileMenu->Append(wxID_REVERT, "Re&vert");
831   
832   m_pFileMenu->AppendSeparator();
833   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
834   m_pFileMenu->Append(IFMENU_FILE_EXPORT, "&Export...");
835   
836   m_pFileMenu->AppendSeparator();
837   m_pFileMenu->Append(wxID_PRINT, "&Print...");
838   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
839   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
840 #ifdef CTSIM_MDI
841   m_pFileMenu->AppendSeparator();
842   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
843   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
844 #endif
845   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
846   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
847   
848   wxMenu *view_menu = new wxMenu;
849   view_menu->Append(IFMENU_VIEW_SCALE_MINMAX, "Display Scale S&et...\tCtrl-E");
850   view_menu->Append(IFMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
851   view_menu->Append(IFMENU_VIEW_SCALE_FULL, "Display F&ull Scale\tCtrl-U");
852   
853   wxMenu* filter_menu = new wxMenu;
854   filter_menu->Append (IFMENU_FILTER_INVERTVALUES, "&Invert Values");
855   filter_menu->Append (IFMENU_FILTER_SQUARE, "&Square");
856   filter_menu->Append (IFMENU_FILTER_SQRT, "Square &Root");
857   filter_menu->Append (IFMENU_FILTER_LOG, "&Log");
858   filter_menu->Append (IFMENU_FILTER_EXP, "&Exp");
859   filter_menu->AppendSeparator();
860 #ifdef HAVE_FFT
861   filter_menu->Append (IFMENU_FILTER_FFT, "2-D &FFT");
862   filter_menu->Append (IFMENU_FILTER_IFFT, "2-D &IFFT");
863   filter_menu->Append (IFMENU_FILTER_FFT_ROWS, "FFT Rows");
864   filter_menu->Append (IFMENU_FILTER_IFFT_ROWS, "IFFT Rows");
865   filter_menu->Append (IFMENU_FILTER_FFT_COLS, "FFT Columns");
866   filter_menu->Append (IFMENU_FILTER_IFFT_COLS, "IFFT Columns");
867   filter_menu->Append (IFMENU_FILTER_FOURIER, "2-D F&ourier");
868   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "2-D Inverse Fo&urier");
869 #else
870   filter_menu->Append (IFMENU_FILTER_FOURIER, "&Fourier");
871   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "&Inverse Fourier");
872 #endif
873   filter_menu->Append (IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, "S&huffle Fourier to Natural Order");
874   filter_menu->Append (IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, "Shu&ffle Natural to Fourier Order");
875   filter_menu->Append (IFMENU_FILTER_MAGNITUDE, "&Magnitude");
876   filter_menu->Append (IFMENU_FILTER_PHASE, "&Phase");
877   
878   wxMenu* image_menu = new wxMenu;
879   image_menu->Append (IFMENU_IMAGE_ADD, "&Add...");
880   image_menu->Append (IFMENU_IMAGE_SUBTRACT, "&Subtract...");
881   image_menu->Append (IFMENU_IMAGE_MULTIPLY, "&Multiply...");
882   image_menu->Append (IFMENU_IMAGE_DIVIDE, "&Divide...");
883   image_menu->AppendSeparator();
884   image_menu->Append (IFMENU_IMAGE_SCALESIZE, "S&cale Size...");
885 #if wxUSE_GLCANVAS
886   image_menu->Append (IFMENU_IMAGE_CONVERT3D, "Convert &3-D\tCtrl-3");
887 #endif
888   
889   m_pMenuAnalyze = new wxMenu;
890   m_pMenuAnalyze->Append (IFMENU_PLOT_ROW, "Plot &Row");
891   m_pMenuAnalyze->Append (IFMENU_PLOT_COL, "Plot &Column");
892   m_pMenuAnalyze->Append (IFMENU_PLOT_HISTOGRAM, "Plot &Histogram");
893   m_pMenuAnalyze->AppendSeparator();
894   m_pMenuAnalyze->Append (IFMENU_PLOT_FFT_ROW, "Plot FFT Row");
895   m_pMenuAnalyze->Append (IFMENU_PLOT_FFT_COL, "Plot FFT Column");
896   m_pMenuAnalyze->AppendSeparator();
897   m_pMenuAnalyze->Append (IFMENU_COMPARE_IMAGES, "Compare &Images...");
898   m_pMenuAnalyze->Append (IFMENU_COMPARE_ROW, "Compare &Row");
899   m_pMenuAnalyze->Append (IFMENU_COMPARE_COL, "Compare &Column");
900   m_pMenuAnalyze->Enable (IFMENU_PLOT_ROW, false);
901   m_pMenuAnalyze->Enable (IFMENU_PLOT_COL, false);
902   m_pMenuAnalyze->Enable (IFMENU_COMPARE_ROW, false);
903   m_pMenuAnalyze->Enable (IFMENU_COMPARE_COL, false);
904   m_pMenuAnalyze->Enable (IFMENU_PLOT_FFT_ROW, false);
905   m_pMenuAnalyze->Enable (IFMENU_PLOT_FFT_COL, false);
906   
907   wxMenu *help_menu = new wxMenu;
908   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
909   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
910   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
911   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
912   
913   wxMenuBar *menu_bar = new wxMenuBar;
914   
915   menu_bar->Append(m_pFileMenu, "&File");
916   menu_bar->Append(view_menu, "&View");
917   menu_bar->Append(image_menu, "&Image");
918   menu_bar->Append(filter_menu, "Fi&lter");
919   menu_bar->Append(m_pMenuAnalyze, "&Analyze");
920   menu_bar->Append(help_menu, "&Help");
921   
922   subframe->SetMenuBar(menu_bar);
923   
924   subframe->Centre(wxBOTH);
925   
926   wxAcceleratorEntry accelEntries[5];
927   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('A'), IFMENU_VIEW_SCALE_AUTO);
928   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('U'), IFMENU_VIEW_SCALE_FULL);
929   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('E'), IFMENU_VIEW_SCALE_MINMAX);
930   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('I'), IFMENU_FILE_PROPERTIES);
931 #if wxUSE_GLCANVAS
932   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('3'), IFMENU_IMAGE_CONVERT3D);
933   wxAcceleratorTable accelTable (5, accelEntries);
934 #else
935   wxAcceleratorTable accelTable (4, accelEntries);
936 #endif
937   
938   subframe->SetAcceleratorTable (accelTable);
939   
940   return subframe;
941 }
942
943
944 bool 
945 ImageFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
946 {
947   m_pFrame = CreateChildFrame(doc, this);
948   
949   m_bMinSpecified = false;
950   m_bMaxSpecified = false;
951   m_dAutoScaleFactor = 1.;
952   
953   int width, height;
954   m_pFrame->GetClientSize (&width, &height);
955   m_pFrame->SetTitle("ImageFileView");
956   m_pCanvas = CreateCanvas (m_pFrame);
957   
958   int x, y;  // X requires a forced resize
959   m_pFrame->GetSize(&x, &y);
960   m_pFrame->SetSize(-1, -1, x, y);
961   m_pFrame->SetFocus();
962   m_pFrame->Show(true);
963   Activate(true);
964   
965   return true;
966 }
967
968 void 
969 ImageFileView::OnDraw (wxDC* dc)
970 {
971   wxSize sizeWindow = m_pFrame->GetClientSize();
972   wxSize sizeBest = m_pCanvas->GetBestSize();
973   if (sizeWindow.x > sizeBest.x || sizeWindow.y > sizeBest.y)
974     m_pFrame->SetClientSize (sizeBest);
975   
976   if (m_bitmap.Ok())
977     dc->DrawBitmap(m_bitmap, 0, 0, false);
978   
979   int xCursor, yCursor;
980   if (m_pCanvas->GetCurrentCursor (xCursor, yCursor))
981     m_pCanvas->DrawRubberBandCursor (*dc, xCursor, yCursor);
982 }
983
984
985 void 
986 ImageFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
987 {
988   const ImageFile& rIF = GetDocument()->getImageFile();
989   ImageFileArrayConst v = rIF.getArray();
990   int nx = rIF.nx();
991   int ny = rIF.ny();
992   if (v != NULL && nx != 0 && ny != 0) {
993     if (! m_bMinSpecified || ! m_bMaxSpecified) {
994       double min, max;
995       rIF.getMinMax (min, max);
996       if (! m_bMinSpecified)
997         m_dMinPixel = min;
998       if (! m_bMaxSpecified)
999         m_dMaxPixel = max;
1000     }
1001     double scaleWidth = m_dMaxPixel - m_dMinPixel;
1002     
1003     unsigned char* imageData = new unsigned char [nx * ny * 3];
1004     for (int ix = 0; ix < nx; ix++) {
1005       for (int iy = 0; iy < ny; iy++) {
1006         double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
1007         int intensity = static_cast<int>(scaleValue + 0.5);
1008         intensity = clamp (intensity, 0, 255);
1009         int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
1010         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
1011       }
1012     }
1013     wxImage image (nx, ny, imageData, true);
1014     m_bitmap = image.ConvertToBitmap();
1015     delete imageData;
1016     int xSize = nx;
1017     int ySize = ny;
1018     ySize = clamp (ySize, 0, 800);
1019     m_pFrame->SetClientSize (xSize, ySize);
1020     m_pCanvas->SetScrollbars(20, 20, nx/20, ny/20);
1021     m_pCanvas->SetBackgroundColour(*wxWHITE);
1022   } 
1023   
1024   if (m_pCanvas)
1025     m_pCanvas->Refresh();
1026 }
1027
1028 bool 
1029 ImageFileView::OnClose (bool deleteWindow)
1030 {
1031   //GetDocumentManager()->ActivateView (this, false, true);
1032   if (! GetDocument() || ! GetDocument()->Close())
1033     return false;
1034   
1035   Activate (false);
1036   if (m_pCanvas) {
1037     m_pCanvas->setView(NULL);
1038     m_pCanvas = NULL;
1039   }
1040   wxString s(theApp->GetAppName());
1041   if (m_pFrame)
1042     m_pFrame->SetTitle(s);
1043   
1044   SetFrame(NULL);
1045   
1046   if (deleteWindow) {
1047     delete m_pFrame;
1048     m_pFrame = NULL;
1049     if (GetDocument() && GetDocument()->getBadFileOpen())
1050       ::wxYield();  // wxWindows bug workaround
1051   }
1052   
1053   return true;
1054 }
1055
1056 void
1057 ImageFileView::OnExport (wxCommandEvent& event)
1058 {
1059   ImageFile& rIF = GetDocument()->getImageFile();
1060   ImageFileArrayConst v = rIF.getArray();
1061   int nx = rIF.nx();
1062   int ny = rIF.ny();
1063   if (v != NULL && nx != 0 && ny != 0) {
1064     if (! m_bMinSpecified || ! m_bMaxSpecified) {
1065       double min, max;
1066       rIF.getMinMax (min, max);
1067       if (! m_bMinSpecified)
1068         m_dMinPixel = min;
1069       if (! m_bMaxSpecified)
1070         m_dMaxPixel = max;
1071     }
1072     
1073     DialogExportParameters dialogExport (getFrameForChild(), m_iDefaultExportFormatID);
1074     if (dialogExport.ShowModal() == wxID_OK) {
1075       wxString strFormatName (dialogExport.getFormatName ());
1076       m_iDefaultExportFormatID = ImageFile::convertFormatNameToID (strFormatName.c_str());
1077       
1078       wxString strExt;
1079       wxString strWildcard;
1080       if (m_iDefaultExportFormatID == ImageFile::FORMAT_PGM || m_iDefaultExportFormatID == ImageFile::FORMAT_PGMASCII) {
1081         strExt = ".pgm";
1082         strWildcard = "PGM Files (*.pgm)|*.pgm";
1083       }
1084 #ifdef HAVE_PNG
1085       else if (m_iDefaultExportFormatID == ImageFile::FORMAT_PNG || m_iDefaultExportFormatID == ImageFile::FORMAT_PNG16) {
1086         strExt = ".png";
1087         strWildcard = "PNG Files (*.png)|*.png";
1088       }
1089 #endif
1090       
1091       const wxString& strFilename = wxFileSelector (wxString("Export Filename"), wxString(""), 
1092         wxString(""), strExt, strWildcard, wxOVERWRITE_PROMPT | wxHIDE_READONLY | wxSAVE);
1093       if (strFilename) {
1094         rIF.exportImage (strFormatName.c_str(), strFilename.c_str(), 1, 1, m_dMinPixel, m_dMaxPixel);
1095         *theApp->getLog() << "Exported file " << strFilename << "\n";
1096       }
1097     }
1098   }
1099 }
1100
1101 void
1102 ImageFileView::OnScaleSize (wxCommandEvent& event)
1103 {
1104   ImageFile& rIF = GetDocument()->getImageFile();
1105   unsigned int iOldNX = rIF.nx();
1106   unsigned int iOldNY = rIF.ny();
1107   
1108   DialogGetXYSize dialogGetXYSize (getFrameForChild(), "Set New X & Y Dimensions", iOldNX, iOldNY);
1109   if (dialogGetXYSize.ShowModal() == wxID_OK) {
1110     unsigned int iNewNX = dialogGetXYSize.getXSize();
1111     unsigned int iNewNY = dialogGetXYSize.getYSize();
1112     std::ostringstream os;
1113     os << "Scale Size from (" << iOldNX << "," << iOldNY << ") to (" << iNewNX << "," << iNewNY << ")";
1114     ImageFileDocument* pScaledDoc = theApp->newImageDoc();
1115     if (! pScaledDoc) {
1116       sys_error (ERR_SEVERE, "Unable to create image file");
1117       return;
1118     }
1119     ImageFile& rScaledIF = pScaledDoc->getImageFile();
1120     rScaledIF.setArraySize (iNewNX, iNewNY);
1121     rScaledIF.labelsCopy (rIF);
1122     rScaledIF.labelAdd (os.str().c_str());
1123     rIF.scaleImage (rScaledIF);
1124     *theApp->getLog() << os.str().c_str() << "\n";
1125     if (theApp->getAskDeleteNewDocs())
1126       pScaledDoc->Modify (true);
1127     pScaledDoc->UpdateAllViews (this);
1128     pScaledDoc->getView()->OnUpdate (this, NULL);
1129     pScaledDoc->getView()->getFrame()->Show(true);
1130   }
1131 }
1132
1133 #if wxUSE_GLCANVAS
1134 void
1135 ImageFileView::OnConvert3d (wxCommandEvent& event)
1136 {
1137   ImageFile& rIF = GetDocument()->getImageFile();
1138   Graph3dFileDocument* pGraph3d = theApp->newGraph3dDoc();
1139   pGraph3d->setBadFileOpen();
1140   pGraph3d->createFromImageFile (rIF);
1141   pGraph3d->getView()->OnUpdate (this, NULL);
1142   pGraph3d->UpdateAllViews();
1143   pGraph3d->getView()->getFrame()->SetClientSize (400, 400);
1144   pGraph3d->getView()->getFrame()->Show (true);
1145   GetDocumentManager()->ActivateView (pGraph3d->getView(), true, false);
1146   ::wxYield();
1147   pGraph3d->getView()->getCanvas()->SetFocus();
1148 }
1149 #endif
1150
1151 void
1152 ImageFileView::OnPlotRow (wxCommandEvent& event)
1153 {
1154   int xCursor, yCursor;
1155   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1156     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1157     return;
1158   }
1159   
1160   const ImageFile& rIF = GetDocument()->getImageFile();
1161   ImageFileArrayConst v = rIF.getArray();
1162   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1163   int nx = rIF.nx();
1164   int ny = rIF.ny();
1165   
1166   if (v != NULL && yCursor < ny) {
1167     double* pX = new double [nx];
1168     double* pYReal = new double [nx];
1169     double *pYImag = NULL;
1170     double *pYMag = NULL;
1171     if (rIF.isComplex()) {
1172       pYImag = new double [nx];
1173       pYMag = new double [nx];
1174     }
1175     for (int i = 0; i < nx; i++) {
1176       pX[i] = i;
1177       pYReal[i] = v[i][yCursor];
1178       if (rIF.isComplex()) {
1179         pYImag[i] = vImag[i][yCursor];
1180         pYMag[i] = ::sqrt (v[i][yCursor] * v[i][yCursor] + vImag[i][yCursor] * vImag[i][yCursor]);
1181       }
1182     }
1183     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1184     if (! pPlotDoc) {
1185       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1186     } else {
1187       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1188       std::ostringstream os;
1189       os << "Row " << yCursor;
1190       std::string title("title ");
1191       title += os.str();
1192       rPlotFile.addEzsetCommand (title.c_str());
1193       rPlotFile.addEzsetCommand ("xlabel Column");
1194       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1195       rPlotFile.addEzsetCommand ("lxfrac 0");
1196       rPlotFile.addEzsetCommand ("box");
1197       rPlotFile.addEzsetCommand ("grid");
1198       rPlotFile.addEzsetCommand ("curve 1");
1199       rPlotFile.addEzsetCommand ("color 1");
1200       if (rIF.isComplex()) {
1201         rPlotFile.addEzsetCommand ("dash 1");
1202         rPlotFile.addEzsetCommand ("curve 2");
1203         rPlotFile.addEzsetCommand ("color 4");
1204         rPlotFile.addEzsetCommand ("dash 3");
1205         rPlotFile.addEzsetCommand ("curve 3");
1206         rPlotFile.addEzsetCommand ("color 0");
1207         rPlotFile.addEzsetCommand ("solid");
1208         rPlotFile.setCurveSize (4, nx);
1209       } else
1210         rPlotFile.setCurveSize (2, nx);
1211       rPlotFile.addColumn (0, pX);
1212       rPlotFile.addColumn (1, pYReal); 
1213       if (rIF.isComplex()) {
1214         rPlotFile.addColumn (2, pYImag);
1215         rPlotFile.addColumn (3, pYMag);
1216       }
1217       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1218         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1219       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1220       *theApp->getLog() << os.str().c_str() << "\n";
1221       rPlotFile.addDescription (os.str().c_str());
1222     }
1223     delete pX;
1224     delete pYReal;
1225     if (rIF.isComplex()) {
1226       delete pYImag;
1227       delete pYMag;
1228     }
1229     if (theApp->getAskDeleteNewDocs())
1230       pPlotDoc->Modify (true);
1231     pPlotDoc->UpdateAllViews ();
1232     pPlotDoc->getView()->OnUpdate (this, NULL);
1233     pPlotDoc->getView()->getFrame()->Show(true);
1234   }
1235 }
1236
1237 void
1238 ImageFileView::OnPlotCol (wxCommandEvent& event)
1239 {
1240   int xCursor, yCursor;
1241   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1242     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1243     return;
1244   }
1245   
1246   const ImageFile& rIF = GetDocument()->getImageFile();
1247   ImageFileArrayConst v = rIF.getArray();
1248   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1249   int nx = rIF.nx();
1250   int ny = rIF.ny();
1251   
1252   if (v != NULL && xCursor < nx) {
1253     double* pX = new double [ny];
1254     double* pYReal = new double [ny];
1255     double* pYImag = NULL;
1256     double* pYMag = NULL;
1257     if (rIF.isComplex()) {
1258       pYImag = new double [ny];
1259       pYMag = new double [ny];
1260     }
1261     for (int i = 0; i < ny; i++) {
1262       pX[i] = i;
1263       pYReal[i] = v[xCursor][i];
1264       if (rIF.isComplex()) {
1265         pYImag[i] = vImag[xCursor][i];
1266         pYMag[i] = ::sqrt (v[xCursor][i] * v[xCursor][i] + vImag[xCursor][i] * vImag[xCursor][i]);
1267       }
1268     }
1269     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1270     if (! pPlotDoc) {
1271       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1272     } else {
1273       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1274       std::ostringstream os;
1275       os << "Column " << xCursor;
1276       std::string title("title ");
1277       title += os.str();
1278       rPlotFile.addEzsetCommand (title.c_str());
1279       rPlotFile.addEzsetCommand ("xlabel Row");
1280       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1281       rPlotFile.addEzsetCommand ("lxfrac 0");
1282       rPlotFile.addEzsetCommand ("box");
1283       rPlotFile.addEzsetCommand ("grid");
1284       rPlotFile.addEzsetCommand ("curve 1");
1285       rPlotFile.addEzsetCommand ("color 1");
1286       if (rIF.isComplex()) {
1287         rPlotFile.addEzsetCommand ("dash 1");
1288         rPlotFile.addEzsetCommand ("curve 2");
1289         rPlotFile.addEzsetCommand ("color 4");
1290         rPlotFile.addEzsetCommand ("dash 3");
1291         rPlotFile.addEzsetCommand ("curve 3");
1292         rPlotFile.addEzsetCommand ("color 0");
1293         rPlotFile.addEzsetCommand ("solid");
1294         rPlotFile.setCurveSize (4, ny);
1295       } else
1296         rPlotFile.setCurveSize (2, ny);
1297       rPlotFile.addColumn (0, pX);
1298       rPlotFile.addColumn (1, pYReal); 
1299       if (rIF.isComplex()) {
1300         rPlotFile.addColumn (2, pYImag);
1301         rPlotFile.addColumn (3, pYMag);
1302       }
1303       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1304         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1305       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1306       *theApp->getLog() << os.str().c_str() << "\n";
1307       rPlotFile.addDescription (os.str().c_str());
1308     }
1309     delete pX;
1310     delete pYReal;
1311     if (rIF.isComplex()) {
1312       delete pYImag;
1313       delete pYMag;
1314     }
1315     if (theApp->getAskDeleteNewDocs())
1316       pPlotDoc->Modify (true);
1317     pPlotDoc->UpdateAllViews ();
1318     pPlotDoc->getView()->OnUpdate (this, NULL);
1319     pPlotDoc->getView()->getFrame()->Show(true);
1320   }
1321 }
1322
1323 #ifdef HAVE_FFT
1324 void
1325 ImageFileView::OnPlotFFTRow (wxCommandEvent& event)
1326 {
1327   int xCursor, yCursor;
1328   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1329     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1330     return;
1331   }
1332   
1333   const ImageFile& rIF = GetDocument()->getImageFile();
1334   ImageFileArrayConst v = rIF.getArray();
1335   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1336   int nx = rIF.nx();
1337   int ny = rIF.ny();
1338   
1339   if (v != NULL && yCursor < ny) {
1340     fftw_complex* pcIn = new fftw_complex [nx];
1341     
1342     int i;
1343     for (i = 0; i < nx; i++) {
1344       pcIn[i].re = v[i][yCursor];
1345       if (rIF.isComplex())
1346         pcIn[i].im = vImag[i][yCursor];
1347       else
1348         pcIn[i].im = 0;
1349     }
1350     
1351     fftw_plan plan = fftw_create_plan (nx, FFTW_FORWARD, FFTW_IN_PLACE);
1352     fftw_one (plan, pcIn, NULL);
1353     fftw_destroy_plan (plan);
1354     
1355     double* pX = new double [nx];
1356     double* pYReal = new double [nx];
1357     double* pYImag = new double [nx];
1358     double* pYMag = new double [nx];
1359     for (i = 0; i < nx; i++) {
1360       pX[i] = i;
1361       pYReal[i] = pcIn[i].re;
1362       pYImag[i] = pcIn[i].im;
1363       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1364     }
1365     Fourier::shuffleFourierToNaturalOrder (pYReal, nx);
1366     Fourier::shuffleFourierToNaturalOrder (pYImag, nx);
1367     Fourier::shuffleFourierToNaturalOrder (pYMag, nx);
1368     
1369     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1370     if (! pPlotDoc) {
1371       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1372     } else {
1373       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1374       std::ostringstream os;
1375       os << "Row " << yCursor;
1376       std::string title("title ");
1377       title += os.str();
1378       rPlotFile.addEzsetCommand (title.c_str());
1379       rPlotFile.addEzsetCommand ("xlabel Column");
1380       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1381       rPlotFile.addEzsetCommand ("lxfrac 0");
1382       rPlotFile.addEzsetCommand ("curve 1");
1383       rPlotFile.addEzsetCommand ("color 1");
1384       rPlotFile.addEzsetCommand ("dash 1");
1385       rPlotFile.addEzsetCommand ("curve 2");
1386       rPlotFile.addEzsetCommand ("color 4");
1387       rPlotFile.addEzsetCommand ("dash 3");
1388       rPlotFile.addEzsetCommand ("curve 3");
1389       rPlotFile.addEzsetCommand ("color 0");
1390       rPlotFile.addEzsetCommand ("solid");
1391       rPlotFile.addEzsetCommand ("box");
1392       rPlotFile.addEzsetCommand ("grid");
1393       rPlotFile.setCurveSize (4, nx);
1394       rPlotFile.addColumn (0, pX);
1395       rPlotFile.addColumn (1, pYReal);
1396       rPlotFile.addColumn (2, pYImag);
1397       rPlotFile.addColumn (3, pYMag);
1398       for (int iL = 0; iL < rIF.nLabels(); iL++)
1399         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1400       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1401       *theApp->getLog() << os.str().c_str() << "\n";
1402       rPlotFile.addDescription (os.str().c_str());
1403     }
1404     delete pX;
1405     delete pYReal;
1406     delete pYImag;
1407     delete pYMag;
1408     delete [] pcIn;
1409     
1410     if (theApp->getAskDeleteNewDocs())
1411       pPlotDoc->Modify (true);
1412     pPlotDoc->UpdateAllViews ();
1413     pPlotDoc->getView()->OnUpdate (this, NULL);
1414     pPlotDoc->getView()->getFrame()->Show(true);
1415   }
1416 }
1417
1418 void
1419 ImageFileView::OnPlotFFTCol (wxCommandEvent& event)
1420 {
1421   int xCursor, yCursor;
1422   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1423     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1424     return;
1425   }
1426   
1427   const ImageFile& rIF = GetDocument()->getImageFile();
1428   ImageFileArrayConst v = rIF.getArray();
1429   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1430   int nx = rIF.nx();
1431   int ny = rIF.ny();
1432   
1433   if (v != NULL && xCursor < nx) {
1434     fftw_complex* pcIn = new fftw_complex [ny];
1435     double *pdTemp = new double [ny];
1436     
1437     int i;
1438     for (i = 0; i < ny; i++)
1439       pdTemp[i] = v[xCursor][i];
1440     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1441     for (i = 0; i < ny; i++) 
1442       pcIn[i].re = pdTemp[i];
1443     
1444     for (i = 0; i < ny; i++) {
1445       if (rIF.isComplex())
1446         pdTemp[i] = vImag[xCursor][i];
1447       else
1448         pdTemp[i] = 0;
1449     }
1450     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1451     for (i = 0; i < ny; i++)
1452       pcIn[i].im = pdTemp[i];
1453     
1454     fftw_plan plan = fftw_create_plan (ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1455     fftw_one (plan, pcIn, NULL);
1456     fftw_destroy_plan (plan);
1457     
1458     double* pX = new double [ny];
1459     double* pYReal = new double [ny];
1460     double* pYImag = new double [ny];
1461     double* pYMag = new double [ny];
1462     for (i = 0; i < ny; i++) {
1463       pX[i] = i;
1464       pYReal[i] = pcIn[i].re;
1465       pYImag[i] = pcIn[i].im;
1466       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1467     }
1468     
1469     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1470     if (! pPlotDoc) {
1471       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1472     } else {
1473       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1474       std::ostringstream os;
1475       os << "Column " << xCursor;
1476       std::string title("title ");
1477       title += os.str();
1478       rPlotFile.addEzsetCommand (title.c_str());
1479       rPlotFile.addEzsetCommand ("xlabel Column");
1480       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1481       rPlotFile.addEzsetCommand ("lxfrac 0");
1482       rPlotFile.addEzsetCommand ("curve 1");
1483       rPlotFile.addEzsetCommand ("color 1");
1484       rPlotFile.addEzsetCommand ("dash 1");
1485       rPlotFile.addEzsetCommand ("curve 2");
1486       rPlotFile.addEzsetCommand ("color 4");
1487       rPlotFile.addEzsetCommand ("dash 3");
1488       rPlotFile.addEzsetCommand ("curve 3");
1489       rPlotFile.addEzsetCommand ("color 0");
1490       rPlotFile.addEzsetCommand ("solid");
1491       rPlotFile.addEzsetCommand ("box");
1492       rPlotFile.addEzsetCommand ("grid");
1493       rPlotFile.setCurveSize (4, ny);
1494       rPlotFile.addColumn (0, pX);
1495       rPlotFile.addColumn (1, pYReal);
1496       rPlotFile.addColumn (2, pYImag);
1497       rPlotFile.addColumn (3, pYMag);
1498       for (int iL = 0; iL < rIF.nLabels(); iL++)
1499         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1500       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1501       *theApp->getLog() << os.str().c_str() << "\n";
1502       rPlotFile.addDescription (os.str().c_str());
1503     }
1504     delete pX;
1505     delete pYReal;
1506     delete pYImag;
1507     delete pYMag;
1508     delete pdTemp;
1509     delete [] pcIn;
1510     
1511     if (theApp->getAskDeleteNewDocs())
1512       pPlotDoc->Modify (true);
1513     pPlotDoc->UpdateAllViews ();
1514     pPlotDoc->getView()->OnUpdate (this, NULL);
1515     pPlotDoc->getView()->getFrame()->Show(true);
1516   }
1517 }
1518 #endif
1519
1520 void
1521 ImageFileView::OnCompareCol (wxCommandEvent& event)
1522 {
1523   int xCursor, yCursor;
1524   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1525     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1526     return;
1527   }
1528   
1529   std::vector<ImageFileDocument*> vecIFDoc;
1530   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1531   if (vecIFDoc.size() == 0) {
1532     wxMessageBox ("No compatible images for Column Comparison", "Error");
1533     return;
1534   }
1535   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1536   
1537   if (dialogGetCompare.ShowModal() == wxID_OK) {
1538     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1539     const ImageFile& rIF = GetDocument()->getImageFile();
1540     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1541     
1542     ImageFileArrayConst v1 = rIF.getArray();
1543     ImageFileArrayConst v2 = rCompareIF.getArray();
1544     int nx = rIF.nx();
1545     int ny = rIF.ny();
1546     
1547     if (v1 != NULL && xCursor < nx) {
1548       double* pX = new double [ny];
1549       double* pY1 = new double [ny];
1550       double* pY2 = new double [ny];
1551       for (int i = 0; i < ny; i++) {
1552         pX[i] = i;
1553         pY1[i] = v1[xCursor][i];
1554         pY2[i] = v2[xCursor][i];
1555       }
1556       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1557       if (! pPlotDoc) {
1558         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1559       } else {
1560         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1561         std::ostringstream os;
1562         os << "Column " << xCursor << " Comparison";
1563         std::string title("title ");
1564         title += os.str();
1565         rPlotFile.addEzsetCommand (title.c_str());
1566         rPlotFile.addEzsetCommand ("xlabel Row");
1567         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1568         rPlotFile.addEzsetCommand ("lxfrac 0");
1569         rPlotFile.addEzsetCommand ("curve 1");
1570         rPlotFile.addEzsetCommand ("color 2");
1571         rPlotFile.addEzsetCommand ("curve 2");
1572         rPlotFile.addEzsetCommand ("color 4");
1573         rPlotFile.addEzsetCommand ("dash 5");
1574         rPlotFile.addEzsetCommand ("box");
1575         rPlotFile.addEzsetCommand ("grid");
1576         rPlotFile.setCurveSize (3, ny);
1577         rPlotFile.addColumn (0, pX);
1578         rPlotFile.addColumn (1, pY1);
1579         rPlotFile.addColumn (2, pY2);
1580         
1581         unsigned int iL;
1582         for (iL = 0; iL < rIF.nLabels(); iL++) {
1583           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1584           s += ": ";
1585           s += rIF.labelGet(iL).getLabelString();
1586           rPlotFile.addDescription (s.c_str());
1587         }
1588         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1589           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1590           s += ": ";
1591           s += rCompareIF.labelGet(iL).getLabelString();
1592           rPlotFile.addDescription (s.c_str());
1593         }
1594         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1595           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1596         *theApp->getLog() << os.str().c_str() << "\n";
1597         rPlotFile.addDescription (os.str().c_str());
1598       }
1599       delete pX;
1600       delete pY1;
1601       delete pY2;
1602       if (theApp->getAskDeleteNewDocs())
1603         pPlotDoc->Modify (true);
1604       pPlotDoc->UpdateAllViews ();
1605       pPlotDoc->getView()->OnUpdate (this, NULL);
1606       pPlotDoc->getView()->getFrame()->Show(true);
1607     }
1608   }
1609 }
1610
1611 void
1612 ImageFileView::OnCompareRow (wxCommandEvent& event)
1613 {
1614   int xCursor, yCursor;
1615   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1616     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1617     return;
1618   }
1619   
1620   std::vector<ImageFileDocument*> vecIFDoc;
1621   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1622   
1623   if (vecIFDoc.size() == 0) {
1624     wxMessageBox ("No compatible images for Row Comparison", "Error");
1625     return;
1626   }
1627   
1628   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1629   
1630   if (dialogGetCompare.ShowModal() == wxID_OK) {
1631     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1632     const ImageFile& rIF = GetDocument()->getImageFile();
1633     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1634     
1635     ImageFileArrayConst v1 = rIF.getArray();
1636     ImageFileArrayConst v2 = rCompareIF.getArray();
1637     int nx = rIF.nx();
1638     int ny = rIF.ny();
1639     
1640     if (v1 != NULL && yCursor < ny) {
1641       double* pX = new double [nx];
1642       double* pY1 = new double [nx];
1643       double* pY2 = new double [nx];
1644       for (int i = 0; i < nx; i++) {
1645         pX[i] = i;
1646         pY1[i] = v1[i][yCursor];
1647         pY2[i] = v2[i][yCursor];
1648       }
1649       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1650       if (! pPlotDoc) {
1651         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1652       } else {
1653         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1654         std::ostringstream os;
1655         os << "Row " << yCursor << " Comparison";
1656         std::string title("title ");
1657         title += os.str();
1658         rPlotFile.addEzsetCommand (title.c_str());
1659         rPlotFile.addEzsetCommand ("xlabel Column");
1660         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1661         rPlotFile.addEzsetCommand ("lxfrac 0");
1662         rPlotFile.addEzsetCommand ("curve 1");
1663         rPlotFile.addEzsetCommand ("color 2");
1664         rPlotFile.addEzsetCommand ("curve 2");
1665         rPlotFile.addEzsetCommand ("color 4");
1666         rPlotFile.addEzsetCommand ("dash 5");
1667         rPlotFile.addEzsetCommand ("box");
1668         rPlotFile.addEzsetCommand ("grid");
1669         rPlotFile.setCurveSize (3, nx);
1670         rPlotFile.addColumn (0, pX);
1671         rPlotFile.addColumn (1, pY1);
1672         rPlotFile.addColumn (2, pY2);
1673         unsigned int iL;
1674         for (iL = 0; iL < rIF.nLabels(); iL++) {
1675           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1676           s += ": ";
1677           s += rIF.labelGet(iL).getLabelString();
1678           rPlotFile.addDescription (s.c_str());
1679         }
1680         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1681           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1682           s += ": ";
1683           s += rCompareIF.labelGet(iL).getLabelString();
1684           rPlotFile.addDescription (s.c_str());
1685         }
1686         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1687           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1688         *theApp->getLog() << os.str().c_str() << "\n";
1689         rPlotFile.addDescription (os.str().c_str());
1690       }
1691       delete pX;
1692       delete pY1;
1693       delete pY2;
1694       if (theApp->getAskDeleteNewDocs())
1695         pPlotDoc->Modify (true);
1696       pPlotDoc->UpdateAllViews ();
1697       pPlotDoc->getView()->OnUpdate (this, NULL);
1698       pPlotDoc->getView()->getFrame()->Show(true);
1699     }
1700   }
1701 }
1702
1703 static int NUMBER_HISTOGRAM_BINS = 256;
1704
1705 void
1706 ImageFileView::OnPlotHistogram (wxCommandEvent& event)
1707
1708   const ImageFile& rIF = GetDocument()->getImageFile();
1709   ImageFileArrayConst v = rIF.getArray();
1710   int nx = rIF.nx();
1711   int ny = rIF.ny();
1712   
1713   if (v != NULL && nx > 0 && ny > 0) {
1714     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1715     if (! pPlotDoc) {
1716       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1717       return;
1718     }
1719     
1720     double* pX = new double [NUMBER_HISTOGRAM_BINS];
1721     double* pY = new double [NUMBER_HISTOGRAM_BINS];
1722     double dMin, dMax;
1723     rIF.getMinMax (dMin, dMax);
1724     double dBinWidth = (dMax - dMin) / NUMBER_HISTOGRAM_BINS;
1725     
1726     for (int i = 0; i < NUMBER_HISTOGRAM_BINS; i++) {
1727       pX[i] = dMin + (i + 0.5) * dBinWidth;
1728       pY[i] = 0;
1729     }
1730     for (int ix = 0; ix < nx; ix++)
1731       for (int iy = 0; iy < ny; iy++) {
1732         int iBin = nearest<int> ((v[ix][iy] - dMin) / dBinWidth);
1733         if (iBin >= 0 && iBin < NUMBER_HISTOGRAM_BINS)
1734           pY[iBin] += 1;
1735       }
1736       
1737       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1738       std::ostringstream os;
1739       os << "Histogram";
1740       std::string title("title ");
1741       title += os.str();
1742       rPlotFile.addEzsetCommand (title.c_str());
1743       rPlotFile.addEzsetCommand ("xlabel Pixel Value");
1744       rPlotFile.addEzsetCommand ("ylabel Count");
1745       rPlotFile.addEzsetCommand ("box");
1746       rPlotFile.addEzsetCommand ("grid");
1747       rPlotFile.setCurveSize (2, NUMBER_HISTOGRAM_BINS);
1748       rPlotFile.addColumn (0, pX);
1749       rPlotFile.addColumn (1, pY);
1750       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++) {
1751         std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1752         s += ": ";
1753         s += rIF.labelGet(iL).getLabelString();
1754         rPlotFile.addDescription (s.c_str());
1755       }
1756       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1757       *theApp->getLog() << os.str().c_str() << "\n";
1758       rPlotFile.addDescription (os.str().c_str());
1759       delete pX;
1760       delete pY;
1761       if (theApp->getAskDeleteNewDocs())
1762         pPlotDoc->Modify (true);
1763       pPlotDoc->UpdateAllViews ();
1764       pPlotDoc->getView()->OnUpdate (this, NULL);
1765       pPlotDoc->getView()->getFrame()->Show(true);
1766   }
1767 }
1768
1769
1770 // PhantomCanvas
1771
1772 PhantomCanvas::PhantomCanvas (PhantomFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1773 : wxScrolledWindow(frame, -1, pos, size, style)
1774 {
1775   m_pView = v;
1776 }
1777
1778 PhantomCanvas::~PhantomCanvas ()
1779 {
1780   m_pView = NULL;
1781 }
1782
1783 void 
1784 PhantomCanvas::OnDraw (wxDC& dc)
1785 {
1786   if (m_pView)
1787     m_pView->OnDraw(& dc);
1788 }
1789
1790 wxSize
1791 PhantomCanvas::GetBestSize() const
1792 {
1793   if (! m_pView)
1794     return wxSize(0,0);
1795   
1796   int xSize, ySize;
1797   theApp->getMainFrame()->GetClientSize (&xSize, &ySize);
1798   xSize = maxValue<int> (xSize, ySize);
1799   ySize = xSize = (xSize / 3);
1800   return wxSize (xSize, ySize);
1801 }
1802
1803
1804
1805 // PhantomFileView
1806
1807 IMPLEMENT_DYNAMIC_CLASS(PhantomFileView, wxView)
1808
1809 BEGIN_EVENT_TABLE(PhantomFileView, wxView)
1810 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomFileView::OnProperties)
1811 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomFileView::OnRasterize)
1812 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomFileView::OnProjections)
1813 END_EVENT_TABLE()
1814
1815 PhantomFileView::PhantomFileView() 
1816 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0)
1817 {
1818 #if defined(DEBUG) || defined(_DEBUG)
1819   m_iDefaultNDet = 165;
1820   m_iDefaultNView = 180;
1821 #else
1822   m_iDefaultNDet = 367;
1823   m_iDefaultNView = 320;
1824 #endif
1825   m_iDefaultNSample = 2;
1826   m_dDefaultRotation = 1;
1827   m_dDefaultFocalLength = 2;
1828   m_dDefaultViewRatio = 1;
1829   m_dDefaultScanRatio = 1;
1830   m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
1831   m_iDefaultTrace = Trace::TRACE_NONE;
1832   
1833 #ifdef DEBUG 
1834   m_iDefaultRasterNX = 115;
1835   m_iDefaultRasterNY = 115;
1836   m_iDefaultRasterNSamples = 1;
1837 #else
1838   m_iDefaultRasterNX = 256;
1839   m_iDefaultRasterNY = 256;
1840   m_iDefaultRasterNSamples = 2;
1841 #endif
1842   m_dDefaultRasterViewRatio = 1;
1843 }
1844
1845 PhantomFileView::~PhantomFileView()
1846 {
1847   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
1848   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
1849 }
1850
1851 void
1852 PhantomFileView::OnProperties (wxCommandEvent& event)
1853 {
1854   const int idPhantom = GetDocument()->getPhantomID();
1855   const wxString& namePhantom = GetDocument()->getPhantomName();
1856   std::ostringstream os;
1857   os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")" << "\n";
1858   const Phantom& rPhantom = GetDocument()->getPhantom();
1859   rPhantom.printDefinitions (os);
1860 #if DEBUG
1861   rPhantom.print (os);
1862 #endif
1863   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
1864   wxMessageBox (os.str().c_str(), "Phantom Properties");
1865 }
1866
1867
1868 void
1869 PhantomFileView::OnProjections (wxCommandEvent& event)
1870 {
1871   DialogGetProjectionParameters dialogProjection (getFrameForChild(), 
1872     m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, 
1873     m_dDefaultFocalLength, m_dDefaultViewRatio, m_dDefaultScanRatio, m_iDefaultGeometry, 
1874     m_iDefaultTrace);
1875   int retVal = dialogProjection.ShowModal();
1876   if (retVal == wxID_OK) {
1877     m_iDefaultNDet = dialogProjection.getNDet();
1878     m_iDefaultNView = dialogProjection.getNView();
1879     m_iDefaultNSample = dialogProjection.getNSamples();
1880     m_iDefaultTrace = dialogProjection.getTrace();
1881     m_dDefaultRotation = dialogProjection.getRotAngle();
1882     m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
1883     m_dDefaultViewRatio = dialogProjection.getViewRatio();
1884     m_dDefaultScanRatio = dialogProjection.getScanRatio();
1885     wxString sGeometry = dialogProjection.getGeometry();
1886     m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
1887     
1888     if (m_iDefaultNDet > 0 && m_iDefaultNView > 0 && sGeometry != "") {
1889       const Phantom& rPhantom = GetDocument()->getPhantom();
1890       Projections* pProj = new Projections;
1891       Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, 
1892         m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultViewRatio, m_dDefaultScanRatio);
1893       if (theScanner.fail()) {
1894         wxString msg = "Failed making scanner\n";
1895         msg += theScanner.failMessage().c_str();
1896         *theApp->getLog() << msg << "\n";
1897         wxMessageBox (msg, "Error");
1898         return;
1899       }
1900       pProj->initFromScanner (theScanner);
1901       m_dDefaultRotation /= TWOPI;  // convert back to fraction of a circle
1902       
1903       Timer timer;
1904       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
1905         ProjectionsDialog dialogProjections (theScanner, *pProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(getFrameForChild()));
1906         for (int iView = 0; iView < pProj->nView(); iView++) {
1907           ::wxYield();
1908           if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1909             delete pProj;
1910             return;
1911           }
1912           ::wxYield();
1913           while (dialogProjections.isPaused()) {
1914             ::wxYield();
1915             ::wxUsleep(50);
1916           }
1917         }
1918       } else {
1919         wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), pProj->nView() + 1, getFrameForChild(), wxPD_CAN_ABORT );
1920         for (int i = 0; i < pProj->nView(); i++) {
1921           theScanner.collectProjections (*pProj, rPhantom, i, 1, true, m_iDefaultTrace);
1922           if (! dlgProgress.Update (i+1)) {
1923             delete pProj;
1924             return;
1925           }
1926         }
1927       }
1928       
1929       std::ostringstream os;
1930       os << "Projections for " << rPhantom.name() << ": nDet=" << m_iDefaultNDet 
1931         << ", nView=" << m_iDefaultNView << ", nSamples=" << m_iDefaultNSample 
1932         << ", RotAngle=" << m_dDefaultRotation << ", FocalLengthRatio=" << m_dDefaultFocalLength 
1933         << ", ViewRatio=" << m_dDefaultViewRatio << ", ScanRatio=" << m_dDefaultScanRatio 
1934         << ", Geometry=" << sGeometry.c_str() << ", FanBeamAngle=" << 
1935         convertRadiansToDegrees (theScanner.fanBeamAngle());
1936       pProj->setCalcTime (timer.timerEnd());
1937       pProj->setRemark (os.str());
1938       *theApp->getLog() << os.str().c_str() << "\n";
1939       
1940       ::wxYield();
1941       ProjectionFileDocument* pProjectionDoc = theApp->newProjectionDoc();
1942       if (! pProjectionDoc) {
1943         sys_error (ERR_SEVERE, "Unable to create projection document");
1944         return;
1945       }
1946       pProjectionDoc->setProjections (pProj);
1947       ProjectionFileView* projView = pProjectionDoc->getView();
1948       if (projView) {
1949         projView->OnUpdate (projView, NULL);
1950         if (projView->getCanvas())
1951               projView->getCanvas()->SetClientSize (m_iDefaultNDet, m_iDefaultNView);
1952         if (wxFrame* pFrame = projView->getFrame()) {
1953           pFrame->Show(true);
1954           pFrame->SetFocus();
1955           pFrame->Raise();
1956         }
1957         GetDocumentManager()->ActivateView (projView, true, false);
1958       }
1959       ::wxYield();
1960       if (theApp->getAskDeleteNewDocs())
1961         pProjectionDoc-> Modify(true);
1962       pProjectionDoc->UpdateAllViews (this);
1963     }
1964   }
1965 }
1966
1967
1968 void
1969 PhantomFileView::OnRasterize (wxCommandEvent& event)
1970 {
1971   DialogGetRasterParameters dialogRaster (getFrameForChild(), m_iDefaultRasterNX, m_iDefaultRasterNY, 
1972     m_iDefaultRasterNSamples, m_dDefaultRasterViewRatio);
1973   int retVal = dialogRaster.ShowModal();
1974   if (retVal == wxID_OK) {
1975     m_iDefaultRasterNX = dialogRaster.getXSize();
1976     m_iDefaultRasterNY  = dialogRaster.getYSize();
1977     m_iDefaultRasterNSamples = dialogRaster.getNSamples();
1978     m_dDefaultRasterViewRatio = dialogRaster.getViewRatio();
1979     if (m_iDefaultRasterNSamples < 1)
1980       m_iDefaultRasterNSamples = 1;
1981     if (m_dDefaultRasterViewRatio < 0)
1982       m_dDefaultRasterViewRatio = 0;
1983     if (m_iDefaultRasterNX > 0 && m_iDefaultRasterNY > 0) {
1984       const Phantom& rPhantom = GetDocument()->getPhantom();
1985       
1986       ImageFile* pImageFile = new ImageFile;
1987       
1988       pImageFile->setArraySize (m_iDefaultRasterNX, m_iDefaultRasterNY);
1989       wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), 
1990         pImageFile->nx() + 1, getFrameForChild(), wxPD_CAN_ABORT );
1991       Timer timer;
1992       for (unsigned int i = 0; i < pImageFile->nx(); i++) {
1993         rPhantom.convertToImagefile (*pImageFile, m_dDefaultRasterViewRatio, m_iDefaultRasterNSamples, 
1994           Trace::TRACE_NONE, i, 1, true);
1995         if (! dlgProgress.Update (i+1)) {
1996           delete pImageFile;
1997           return;
1998         }
1999       }
2000       
2001       ImageFileDocument* pRasterDoc = theApp->newImageDoc();
2002       if (! pRasterDoc) {
2003         sys_error (ERR_SEVERE, "Unable to create image file");
2004         return;
2005       }
2006       pRasterDoc->setImageFile (pImageFile);
2007       
2008       if (theApp->getAskDeleteNewDocs())
2009         pRasterDoc->Modify (true);
2010       pRasterDoc->UpdateAllViews (this);
2011       pRasterDoc->getView()->getFrame()->Show(true);
2012       std::ostringstream os;
2013       os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
2014         << m_iDefaultRasterNY << ", ViewRatio=" << m_dDefaultRasterViewRatio << ", nSamples=" 
2015         << m_iDefaultRasterNSamples;;
2016       *theApp->getLog() << os.str().c_str() << "\n";
2017       pImageFile->labelAdd (os.str().c_str(), timer.timerEnd());
2018       ImageFileView* rasterView = pRasterDoc->getView();
2019       if (rasterView) {
2020         rasterView->getFrame()->SetFocus();
2021         rasterView->OnUpdate (rasterView, NULL);
2022       }
2023       
2024     }
2025   }
2026 }
2027
2028
2029 PhantomCanvas* 
2030 PhantomFileView::CreateCanvas (wxFrame *parent)
2031 {
2032   PhantomCanvas* pCanvas;
2033   
2034   pCanvas = new PhantomCanvas (this, parent, wxPoint(0, 0), wxSize(0,0), 0);
2035   pCanvas->SetBackgroundColour(*wxWHITE);
2036   pCanvas->Clear();
2037   
2038   return pCanvas;
2039 }
2040
2041 #if CTSIM_MDI
2042 wxDocMDIChildFrame*
2043 #else
2044 wxDocChildFrame*
2045 #endif
2046 PhantomFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2047 {
2048 #if CTSIM_MDI
2049   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2050 #else
2051   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2052 #endif
2053   theApp->setIconForFrame (subframe);
2054   
2055   m_pFileMenu = new wxMenu;
2056   
2057   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2058   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2059   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2060   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2061   m_pFileMenu->Append(wxID_CLOSE, "&Close");
2062   
2063   m_pFileMenu->AppendSeparator();
2064   m_pFileMenu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2065   
2066   m_pFileMenu->AppendSeparator();
2067   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2068   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2069   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2070 #ifdef CTSIM_MDI
2071   m_pFileMenu->AppendSeparator();
2072   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2073   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2074 #endif
2075   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2076   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2077   
2078   wxMenu *process_menu = new wxMenu;
2079   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...\tCtrl-R");
2080   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...\tCtrl-J");
2081   
2082   wxMenu *help_menu = new wxMenu;
2083   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2084   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2085   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2086   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2087   
2088   wxMenuBar *menu_bar = new wxMenuBar;
2089   
2090   menu_bar->Append(m_pFileMenu, "&File");
2091   menu_bar->Append(process_menu, "&Process");
2092   menu_bar->Append(help_menu, "&Help");
2093   
2094   subframe->SetMenuBar(menu_bar);
2095   subframe->Centre(wxBOTH);
2096   
2097   wxAcceleratorEntry accelEntries[3];
2098   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('J'), PHMMENU_PROCESS_PROJECTIONS);
2099   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('R'), PHMMENU_PROCESS_RASTERIZE);
2100   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('I'), PHMMENU_FILE_PROPERTIES);
2101   wxAcceleratorTable accelTable (3, accelEntries);
2102   subframe->SetAcceleratorTable (accelTable);
2103   
2104   return subframe;
2105 }
2106
2107
2108 bool 
2109 PhantomFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2110 {
2111   m_pFrame = CreateChildFrame(doc, this);
2112   SetFrame(m_pFrame);
2113   m_pCanvas = CreateCanvas (m_pFrame);
2114   m_pFrame->SetClientSize (m_pCanvas->GetBestSize());
2115   m_pCanvas->SetClientSize (m_pCanvas->GetBestSize());
2116   
2117   m_pFrame->SetTitle ("PhantomFileView");
2118   
2119 #ifdef __X__
2120   int x, y;  // X requires a forced resize
2121   m_pFrame->GetSize(&x, &y);
2122   m_pFrame->SetSize(-1, -1, x, y);
2123 #endif
2124   
2125   m_pFrame->Show(true);
2126   Activate(true);
2127   
2128   return true;
2129 }
2130
2131 void 
2132 PhantomFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2133 {
2134   if (m_pCanvas)
2135     m_pCanvas->Refresh();
2136 }
2137
2138 bool 
2139 PhantomFileView::OnClose (bool deleteWindow)
2140 {
2141   //GetDocumentManager()->ActivateView (this, false, true);
2142   if (! GetDocument() || ! GetDocument()->Close())
2143     return false;
2144   
2145   Activate(false);
2146   if (m_pCanvas) {
2147     m_pCanvas->setView(NULL);
2148     m_pCanvas = NULL;
2149   }
2150   wxString s(wxTheApp->GetAppName());
2151   if (m_pFrame)
2152     m_pFrame->SetTitle(s);
2153   
2154   SetFrame(NULL);
2155   
2156   if (deleteWindow) {
2157     delete m_pFrame;
2158     m_pFrame = NULL;
2159     if (GetDocument() && GetDocument()->getBadFileOpen())
2160       ::wxYield();  // wxWindows bug workaround
2161   }
2162   
2163   return true;
2164 }
2165
2166 void
2167 PhantomFileView::OnDraw (wxDC* dc)
2168 {
2169   int xsize, ysize;
2170   m_pCanvas->GetClientSize (&xsize, &ysize);
2171   SGPDriver driver (dc, xsize, ysize);
2172   SGP sgp (driver);
2173   const Phantom& rPhantom = GetDocument()->getPhantom();
2174   sgp.setColor (C_RED);
2175   rPhantom.show (sgp);
2176 }
2177
2178 // ProjectionCanvas
2179
2180 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2181 : wxScrolledWindow(frame, -1, pos, size, style)
2182 {
2183   m_pView = v;
2184 }
2185
2186 ProjectionFileCanvas::~ProjectionFileCanvas ()
2187 {
2188   m_pView = NULL;
2189 }
2190
2191 void 
2192 ProjectionFileCanvas::OnDraw(wxDC& dc)
2193 {
2194   if (m_pView)
2195     m_pView->OnDraw(& dc);
2196 }
2197
2198 wxSize
2199 ProjectionFileCanvas::GetBestSize () const
2200 {
2201   wxSize best (0, 0);
2202   if (m_pView) {
2203     Projections& rProj = m_pView->GetDocument()->getProjections();
2204     best.Set (rProj.nDet(), rProj.nView());
2205   }
2206   
2207   return best;
2208 }
2209
2210
2211 // ProjectionFileView
2212
2213 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
2214
2215 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
2216 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
2217 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
2218 EVT_MENU(PJMENU_RECONSTRUCT_FOURIER, ProjectionFileView::OnReconstructFourier)
2219 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2220 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2221 END_EVENT_TABLE()
2222
2223 ProjectionFileView::ProjectionFileView() 
2224 : wxView(), m_pFrame(0), m_pCanvas(0), m_pFileMenu(0)
2225 {
2226 #ifdef DEBUG
2227   m_iDefaultNX = 115;
2228   m_iDefaultNY = 115;
2229 #else
2230   m_iDefaultNX = 256;
2231   m_iDefaultNY = 256;
2232 #endif
2233   
2234   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2235   m_dDefaultFilterParam = 1.;
2236 #if HAVE_FFTW
2237   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2238   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
2239 #else
2240   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2241   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2242 #endif
2243   m_iDefaultZeropad = 1;
2244   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF;
2245   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2246   m_iDefaultInterpParam = 1;
2247   m_iDefaultTrace = Trace::TRACE_NONE;
2248   
2249   m_iDefaultPolarNX = 256;
2250   m_iDefaultPolarNY = 256;
2251   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2252   m_iDefaultPolarZeropad = 1;
2253 }
2254
2255 ProjectionFileView::~ProjectionFileView()
2256 {
2257   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
2258   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
2259 }
2260
2261 void
2262 ProjectionFileView::OnProperties (wxCommandEvent& event)
2263 {
2264   const Projections& rProj = GetDocument()->getProjections();
2265   std::ostringstream os;
2266   rProj.printScanInfo(os);
2267   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
2268   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2269   dialogMsg.ShowModal();
2270 }
2271
2272
2273 void
2274 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2275 {
2276   Projections& rProj = GetDocument()->getProjections();
2277   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2278     m_iDefaultPolarInterpolation, -1);
2279   if (dialogPolar.ShowModal() == wxID_OK) {
2280     wxString strInterpolation (dialogPolar.getInterpolationName());
2281     m_iDefaultPolarNX = dialogPolar.getXSize();
2282     m_iDefaultPolarNY = dialogPolar.getYSize();
2283     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2284     ImageFile* pIF = new ImageFile (m_iDefaultPolarNX, m_iDefaultPolarNY);
2285     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2286     if (! rProj.convertPolar (*pIF, m_iDefaultPolarInterpolation)) {
2287       delete pIF;
2288       *theApp->getLog() << "Error converting to Polar\n";
2289       return;
2290     }
2291     pPolarDoc = theApp->newImageDoc ();
2292     if (! pPolarDoc) {
2293       sys_error (ERR_SEVERE, "Unable to create image file");
2294       return;
2295     }
2296     pPolarDoc->setImageFile (pIF);
2297     pIF->labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2298     std::ostringstream os;
2299     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2300       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2301       << strInterpolation.c_str();
2302     *theApp->getLog() << os.str().c_str() << "\n";
2303     pIF->labelAdd (os.str().c_str());
2304     if (theApp->getAskDeleteNewDocs())
2305       pPolarDoc->Modify (true);
2306     pPolarDoc->UpdateAllViews ();
2307     pPolarDoc->getView()->OnUpdate (this, NULL);
2308     pPolarDoc->getView()->getFrame()->Show(true);
2309   }
2310 }
2311
2312 void
2313 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2314 {
2315   Projections& rProj = GetDocument()->getProjections();
2316   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2317     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2318   if (dialogPolar.ShowModal() == wxID_OK) {
2319     wxString strInterpolation (dialogPolar.getInterpolationName());
2320     m_iDefaultPolarNX = dialogPolar.getXSize();
2321     m_iDefaultPolarNY = dialogPolar.getYSize();
2322     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2323     ImageFile* pIF = new ImageFile (m_iDefaultPolarNX, m_iDefaultPolarNY);
2324     
2325     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2326     if (! rProj.convertFFTPolar (*pIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad)) {
2327       delete pIF;
2328       *theApp->getLog() << "Error converting to polar\n";
2329       return;
2330     }
2331     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2332     if (! pPolarDoc) {
2333       sys_error (ERR_SEVERE, "Unable to create image file");
2334       return;
2335     }
2336     pPolarDoc->setImageFile (pIF);
2337     pIF->labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2338     std::ostringstream os;
2339     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2340       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2341       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2342     *theApp->getLog() << os.str().c_str() << "\n";
2343     pIF->labelAdd (os.str().c_str());
2344     if (theApp->getAskDeleteNewDocs())
2345       pPolarDoc->Modify (true);
2346     pPolarDoc->UpdateAllViews ();
2347     pPolarDoc->getView()->OnUpdate (this, NULL);
2348     pPolarDoc->getView()->getFrame()->Show(true);
2349   }
2350 }
2351
2352 void
2353 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2354 {
2355   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2356 }
2357
2358 void
2359 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2360 {
2361   DialogGetReconstructionParameters dialogReconstruction (getFrameForChild(), m_iDefaultNX, m_iDefaultNY, 
2362     m_iDefaultFilter, m_dDefaultFilterParam, m_iDefaultFilterMethod, m_iDefaultFilterGeneration, 
2363     m_iDefaultZeropad, m_iDefaultInterpolation, m_iDefaultInterpParam, m_iDefaultBackprojector, 
2364     m_iDefaultTrace);
2365   
2366   int retVal = dialogReconstruction.ShowModal();
2367   if (retVal == wxID_OK) {
2368     m_iDefaultNX = dialogReconstruction.getXSize();
2369     m_iDefaultNY = dialogReconstruction.getYSize();
2370     wxString optFilterName = dialogReconstruction.getFilterName();
2371     m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2372     m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2373     wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2374     m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2375     m_iDefaultZeropad = dialogReconstruction.getZeropad();
2376     wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2377     m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2378     wxString optInterpName = dialogReconstruction.getInterpName();
2379     m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2380     m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2381     wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2382     m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2383     m_iDefaultTrace = dialogReconstruction.getTrace();
2384     
2385     if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
2386       const Projections& rProj = GetDocument()->getProjections();
2387       std::ostringstream os;
2388       os << "Reconstruct " << rProj.getFilename() << ": xSize=" << m_iDefaultNX << ", ySize=" << m_iDefaultNY << ", Filter=" << optFilterName.c_str() << ", FilterParam=" << m_dDefaultFilterParam << ", FilterMethod=" << optFilterMethodName.c_str() << ", FilterGeneration=" << optFilterGenerationName.c_str() << ", Zeropad=" << m_iDefaultZeropad << ", Interpolation=" << optInterpName.c_str() << ", InterpolationParam=" << m_iDefaultInterpParam << ", Backprojection=" << optBackprojectName.c_str();
2389       
2390       Timer timerRecon;
2391       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2392         ImageFile* pImageFile = new ImageFile;
2393         pImageFile->setArraySize (m_iDefaultNX, m_iDefaultNY);
2394         Reconstructor* pReconstructor = new Reconstructor (rProj, *pImageFile, optFilterName.c_str(), 
2395           m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2396           optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
2397         
2398         ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstructor, rProj, *pImageFile, m_iDefaultTrace, getFrameForChild());
2399         for (int iView = 0; iView < rProj.nView(); iView++) {
2400           ::wxYield();
2401           if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView, true)) {
2402             delete pDlgReconstruct;
2403             delete pReconstructor;
2404             delete pImageFile;
2405             return;
2406           }
2407           ::wxYield();
2408           ::wxYield();
2409           while (pDlgReconstruct->isPaused()) {
2410             ::wxYield();
2411             ::wxUsleep(50);
2412           }
2413         }
2414         pReconstructor->postProcessing();
2415         delete pDlgReconstruct;
2416         delete pReconstructor;
2417         ImageFileDocument* pReconDoc = theApp->newImageDoc();
2418         if (! pReconDoc) {
2419           sys_error (ERR_SEVERE, "Unable to create image file");
2420           return;
2421         }
2422         pReconDoc->setImageFile (pImageFile);
2423         if (theApp->getAskDeleteNewDocs())
2424           pReconDoc->Modify (true);
2425         pReconDoc->UpdateAllViews (this);
2426         if (ImageFileView* rasterView = pReconDoc->getView()) {
2427           rasterView->OnUpdate (rasterView, NULL);
2428           rasterView->getFrame()->SetFocus();
2429           rasterView->getFrame()->Show(true);
2430         }
2431         *theApp->getLog() << os.str().c_str() << "\n";
2432         pImageFile->labelAdd (rProj.getLabel());
2433         pImageFile->labelAdd (os.str().c_str(), timerRecon.timerEnd());
2434         
2435       } else {
2436         if (theApp->getUseBackgroundTasks() || theApp->getNumberCPU() > 1) {
2437           ReconstructorSupervisor* pReconstructor = new ReconstructorSupervisor (this, 
2438             m_iDefaultNX, m_iDefaultNY, optFilterName.c_str(), 
2439             m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2440             optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), os.str().c_str());
2441           if (! pReconstructor->start()) {
2442             delete pReconstructor;
2443             return;
2444           }
2445           // delete pReconstructor;  // reconstructor is still running
2446         } else {
2447           ImageFile* pImageFile = new ImageFile;
2448           pImageFile->setArraySize (m_iDefaultNX, m_iDefaultNY);
2449           Reconstructor* pReconstructor = new Reconstructor (rProj, *pImageFile, optFilterName.c_str(), 
2450             m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2451             optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
2452           
2453           wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT );
2454           for (int iView = 0; iView < rProj.nView(); iView++) {
2455             pReconstructor->reconstructView (iView, 1);
2456             if (! dlgProgress.Update (iView + 1)) {
2457               delete pReconstructor;
2458               delete pImageFile;
2459               return;
2460             }
2461           }
2462           pReconstructor->postProcessing();
2463           delete pReconstructor;
2464           ImageFileDocument* pReconDoc = theApp->newImageDoc();
2465           if (! pReconDoc) {
2466             sys_error (ERR_SEVERE, "Unable to create image file");
2467             return;
2468           }
2469           pReconDoc->setImageFile (pImageFile);
2470           if (theApp->getAskDeleteNewDocs())
2471             pReconDoc->Modify (true);
2472           pReconDoc->UpdateAllViews (this);
2473           if (ImageFileView* rasterView = pReconDoc->getView()) {
2474             rasterView->OnUpdate (rasterView, NULL);
2475             rasterView->getFrame()->SetFocus();
2476             rasterView->getFrame()->Show(true);
2477           }
2478           *theApp->getLog() << os.str().c_str() << "\n";
2479           pImageFile->labelAdd (rProj.getLabel());
2480           pImageFile->labelAdd (os.str().c_str(), timerRecon.timerEnd());
2481         }
2482       }
2483     }
2484   }
2485 }
2486
2487
2488 ProjectionFileCanvas* 
2489 ProjectionFileView::CreateCanvas (wxFrame *parent)
2490 {
2491   ProjectionFileCanvas* pCanvas;
2492   int width, height;
2493   parent->GetClientSize(&width, &height);
2494   
2495   pCanvas = new ProjectionFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
2496   
2497   pCanvas->SetScrollbars(20, 20, 50, 50);
2498   pCanvas->SetBackgroundColour(*wxWHITE);
2499   pCanvas->Clear();
2500   
2501   return pCanvas;
2502 }
2503
2504 #if CTSIM_MDI
2505 wxDocMDIChildFrame*
2506 #else
2507 wxDocChildFrame*
2508 #endif
2509 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2510 {
2511 #ifdef CTSIM_MDI
2512   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2513 #else
2514   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2515 #endif
2516   theApp->setIconForFrame (subframe);
2517   
2518   m_pFileMenu = new wxMenu;
2519   
2520   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2521   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2522   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2523   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2524   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2525   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2526   
2527   m_pFileMenu->AppendSeparator();
2528   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2529   
2530   m_pFileMenu->AppendSeparator();
2531   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2532   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2533   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2534 #ifdef CTSIM_MDI
2535   m_pFileMenu->AppendSeparator();
2536   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2537   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2538 #endif
2539   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2540   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2541   
2542   wxMenu *convert_menu = new wxMenu;
2543   convert_menu->Append (PJMENU_CONVERT_POLAR, "&Polar Image...\tCtrl-L");
2544   convert_menu->Append (PJMENU_CONVERT_FFT_POLAR, "&FFT->Polar Image...\tCtrl-M");
2545   
2546   wxMenu *reconstruct_menu = new wxMenu;
2547   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FBP, "&Filtered Backprojection...\tCtrl-R", "Reconstruct image using filtered backprojection");
2548   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FOURIER, "&Fourier...\tCtrl-E", "Reconstruct image using inverse Fourier");
2549   reconstruct_menu->Enable (PJMENU_RECONSTRUCT_FOURIER, false);
2550   
2551   wxMenu *help_menu = new wxMenu;
2552   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2553   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2554   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2555   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2556   
2557   wxMenuBar *menu_bar = new wxMenuBar;
2558   
2559   menu_bar->Append (m_pFileMenu, "&File");
2560   menu_bar->Append (convert_menu, "&Convert");
2561   menu_bar->Append (reconstruct_menu, "&Reconstruct");
2562   menu_bar->Append (help_menu, "&Help");
2563   
2564   subframe->SetMenuBar(menu_bar);  
2565   subframe->Centre(wxBOTH);
2566   
2567   wxAcceleratorEntry accelEntries[5];
2568   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('L'), PJMENU_CONVERT_POLAR);
2569   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('M'), PJMENU_CONVERT_FFT_POLAR);
2570   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('R'), PJMENU_RECONSTRUCT_FBP);
2571   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('E'), PJMENU_RECONSTRUCT_FOURIER);
2572   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('I'), PJMENU_FILE_PROPERTIES);
2573   wxAcceleratorTable accelTable (5, accelEntries);
2574   subframe->SetAcceleratorTable (accelTable);
2575   
2576   return subframe;
2577 }
2578
2579
2580 bool 
2581 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2582 {
2583   m_pFrame = CreateChildFrame(doc, this);
2584   SetFrame(m_pFrame);
2585   
2586   int width, height;
2587   m_pFrame->GetClientSize (&width, &height);
2588   m_pFrame->SetTitle ("ProjectionFileView");
2589   m_pCanvas = CreateCanvas (m_pFrame);
2590   
2591 #ifdef __X__
2592   int x, y;  // X requires a forced resize
2593   m_pFrame->GetSize(&x, &y);
2594   m_pFrame->SetSize(-1, -1, x, y);
2595 #endif
2596   
2597   m_pFrame->Show(true);
2598   Activate(true);
2599   
2600   return true;
2601 }
2602
2603 void 
2604 ProjectionFileView::OnDraw (wxDC* dc)
2605 {
2606   wxSize clientSize = m_pFrame->GetClientSize();
2607   wxSize bestSize = m_pCanvas->GetBestSize();
2608   
2609   if (clientSize.x > bestSize.x || clientSize.y > bestSize.y)
2610     m_pFrame->SetClientSize (bestSize);
2611   
2612   if (m_bitmap.Ok())
2613     dc->DrawBitmap (m_bitmap, 0, 0, false);
2614 }
2615
2616
2617 void 
2618 ProjectionFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2619 {
2620   const Projections& rProj = GetDocument()->getProjections();
2621   const int nDet = rProj.nDet();
2622   const int nView = rProj.nView();
2623   if (nDet != 0 && nView != 0) {
2624     const DetectorArray& detarray = rProj.getDetectorArray(0);
2625     const DetectorValue* detval = detarray.detValues();
2626     double min = detval[0];
2627     double max = detval[0];
2628     for (int iy = 0; iy < nView; iy++) {
2629       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2630       const DetectorValue* detval = detarray.detValues();
2631       for (int ix = 0; ix < nDet; ix++) {
2632         if (min > detval[ix])
2633           min = detval[ix];
2634         else if (max < detval[ix])
2635           max = detval[ix];
2636       }
2637     }
2638     
2639     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2640     double scale = (max - min) / 255;
2641     for (int iy2 = 0; iy2 < nView; iy2++) {
2642       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2643       const DetectorValue* detval = detarray.detValues();
2644       for (int ix = 0; ix < nDet; ix++) {
2645         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2646         intensity = clamp(intensity, 0, 255);
2647         int baseAddr = (iy2 * nDet + ix) * 3;
2648         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2649       }
2650     }
2651     wxImage image (nDet, nView, imageData, true);
2652     m_bitmap = image.ConvertToBitmap();
2653     delete imageData;
2654     int xSize = nDet;
2655     int ySize = nView;
2656     xSize = clamp (xSize, 0, 800);
2657     ySize = clamp (ySize, 0, 800);
2658     m_pFrame->SetClientSize (xSize, ySize);
2659     m_pCanvas->SetScrollbars (20, 20, nDet/20, nView/20);
2660   }
2661   
2662   if (m_pCanvas)
2663     m_pCanvas->Refresh();
2664 }
2665
2666 bool 
2667 ProjectionFileView::OnClose (bool deleteWindow)
2668 {
2669   //GetDocumentManager()->ActivateView (this, false, true);
2670   if (! GetDocument() || ! GetDocument()->Close())
2671     return false;
2672   
2673   Activate(false);
2674   if (m_pCanvas) {
2675         m_pCanvas->setView(NULL);
2676     m_pCanvas = NULL;
2677   }
2678   wxString s(wxTheApp->GetAppName());
2679   if (m_pFrame)
2680     m_pFrame->SetTitle(s);
2681   
2682   SetFrame(NULL);
2683   
2684   if (deleteWindow) {
2685     delete m_pFrame;
2686     m_pFrame = NULL;
2687     if (GetDocument() && GetDocument()->getBadFileOpen())
2688       ::wxYield();  // wxWindows bug workaround
2689   }
2690   
2691   return true;
2692 }
2693
2694
2695
2696 // PlotFileCanvas
2697 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2698 : wxScrolledWindow(frame, -1, pos, size, style)
2699 {
2700   m_pView = v;
2701 }
2702
2703 PlotFileCanvas::~PlotFileCanvas ()
2704 {
2705   m_pView = NULL;
2706 }
2707
2708 void 
2709 PlotFileCanvas::OnDraw(wxDC& dc)
2710 {
2711   if (m_pView)
2712     m_pView->OnDraw(& dc);
2713 }
2714
2715
2716 // PlotFileView
2717
2718 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2719
2720 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2721 EVT_MENU(PLOTMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2722 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2723 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2724 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2725 END_EVENT_TABLE()
2726
2727 PlotFileView::PlotFileView() 
2728 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pEZPlot(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
2729 {
2730 }
2731
2732 PlotFileView::~PlotFileView()
2733 {
2734   if (m_pEZPlot)
2735     delete m_pEZPlot;
2736   
2737   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);  
2738 }
2739
2740 void
2741 PlotFileView::OnProperties (wxCommandEvent& event)
2742 {
2743   const PlotFile& rPlot = GetDocument()->getPlotFile();
2744   std::ostringstream os;
2745   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2746   rPlot.printHeadersBrief (os);
2747   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<<\n";
2748   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2749   dialogMsg.ShowModal();
2750 }
2751
2752
2753 void 
2754 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2755 {
2756   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2757   double min, max, mean, mode, median, stddev;
2758   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2759   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
2760   int iRetVal = dialogAutoScale.ShowModal();
2761   if (iRetVal == wxID_OK) {
2762     m_bMinSpecified = true;
2763     m_bMaxSpecified = true;
2764     double dMin, dMax;
2765     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2766       m_dMinPixel = dMin;
2767       m_dMaxPixel = dMax;
2768       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2769       OnUpdate (this, NULL);
2770     }
2771   }
2772 }
2773
2774 void 
2775 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2776 {
2777   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2778   double min;
2779   double max;
2780   
2781   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2782     if (! rPlotFile.getMinMax (1, min, max)) {
2783       *theApp->getLog() << "Error: unable to find Min/Max\n";
2784       return;
2785     }
2786   }
2787   
2788   if (m_bMinSpecified)
2789     min = m_dMinPixel;
2790   if (m_bMaxSpecified)
2791     max = m_dMaxPixel;
2792   
2793   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Y-axis Minimum & Maximum", min, max);
2794   int retVal = dialogMinMax.ShowModal();
2795   if (retVal == wxID_OK) {
2796     m_bMinSpecified = true;
2797     m_bMaxSpecified = true;
2798     m_dMinPixel = dialogMinMax.getMinimum();
2799     m_dMaxPixel = dialogMinMax.getMaximum();
2800     OnUpdate (this, NULL);
2801   }
2802 }
2803
2804 void 
2805 PlotFileView::OnScaleFull (wxCommandEvent& event)
2806 {
2807   if (m_bMinSpecified || m_bMaxSpecified) {
2808     m_bMinSpecified = false;
2809     m_bMaxSpecified = false;
2810     OnUpdate (this, NULL);
2811   }
2812 }
2813
2814
2815 PlotFileCanvas* 
2816 PlotFileView::CreateCanvas (wxFrame* parent)
2817 {
2818   PlotFileCanvas* pCanvas;
2819   int width, height;
2820   parent->GetClientSize(&width, &height);
2821   
2822   pCanvas = new PlotFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
2823   
2824   pCanvas->SetBackgroundColour(*wxWHITE);
2825   pCanvas->Clear();
2826   
2827   return pCanvas;
2828 }
2829
2830 #if CTSIM_MDI
2831 wxDocMDIChildFrame*
2832 #else
2833 wxDocChildFrame*
2834 #endif
2835 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2836 {
2837 #ifdef CTSIM_MDI
2838   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2839 #else
2840   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2841 #endif
2842   theApp->setIconForFrame (subframe);
2843   
2844   m_pFileMenu = new wxMenu;
2845   
2846   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2847   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2848   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2849   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2850   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2851   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2852   
2853   m_pFileMenu->AppendSeparator();
2854   m_pFileMenu->Append(PLOTMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2855   
2856   m_pFileMenu->AppendSeparator();
2857   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2858   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2859   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2860 #ifdef CTSIM_MDI
2861   m_pFileMenu->AppendSeparator();
2862   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2863   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2864 #endif
2865   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2866   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2867   
2868   wxMenu *view_menu = new wxMenu;
2869   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...\tCtrl-E");
2870   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
2871   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale\tCtrl-U");
2872   
2873   wxMenu *help_menu = new wxMenu;
2874   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2875   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2876   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2877   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2878   
2879   wxMenuBar *menu_bar = new wxMenuBar;
2880   
2881   menu_bar->Append(m_pFileMenu, "&File");
2882   menu_bar->Append(view_menu, "&View");
2883   menu_bar->Append(help_menu, "&Help");
2884   
2885   subframe->SetMenuBar(menu_bar);
2886   subframe->Centre(wxBOTH);
2887   
2888   wxAcceleratorEntry accelEntries[4];
2889   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('E'), PLOTMENU_VIEW_SCALE_MINMAX);
2890   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('A'), PLOTMENU_VIEW_SCALE_AUTO);
2891   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('U'), PLOTMENU_VIEW_SCALE_FULL);
2892   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('I'), PLOTMENU_FILE_PROPERTIES);
2893   wxAcceleratorTable accelTable (4, accelEntries);
2894   subframe->SetAcceleratorTable (accelTable);
2895   
2896   return subframe;
2897 }
2898
2899
2900 bool 
2901 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
2902 {
2903   m_pFrame = CreateChildFrame(doc, this);
2904   SetFrame(m_pFrame);
2905   
2906   m_bMinSpecified = false;
2907   m_bMaxSpecified = false;
2908   m_dAutoScaleFactor = 1.;
2909   
2910   int width, height;
2911   m_pFrame->GetClientSize(&width, &height);
2912   m_pFrame->SetTitle ("Plot File");
2913   m_pCanvas = CreateCanvas (m_pFrame);
2914   
2915 #ifdef __X__
2916   int x, y;  // X requires a forced resize
2917   m_pFrame->GetSize(&x, &y);
2918   m_pFrame->SetSize(-1, -1, x, y);
2919 #endif
2920   
2921   m_pFrame->Show(true);
2922   Activate(true);
2923   
2924   return true;
2925 }
2926
2927 void 
2928 PlotFileView::OnDraw (wxDC* dc)
2929 {
2930   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2931   const int iNColumns = rPlotFile.getNumColumns();
2932   const int iNRecords = rPlotFile.getNumRecords();
2933   
2934   if (iNColumns > 0 && iNRecords > 0) {
2935     int xsize, ysize;
2936     m_pCanvas->GetClientSize (&xsize, &ysize);
2937     SGPDriver driver (dc, xsize, ysize);
2938     SGP sgp (driver);
2939     if (m_pEZPlot)
2940       m_pEZPlot->plot (&sgp);
2941   }
2942 }
2943
2944
2945 void 
2946 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2947 {
2948   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2949   const int iNColumns = rPlotFile.getNumColumns();
2950   const int iNRecords = rPlotFile.getNumRecords();
2951   
2952   if (iNColumns > 0 && iNRecords > 0) {
2953     if (m_pEZPlot)
2954       delete m_pEZPlot;
2955     m_pEZPlot = new EZPlot;
2956     
2957     for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
2958       m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
2959     
2960     if (m_bMinSpecified) {
2961       std::ostringstream os;
2962       os << "ymin " << m_dMinPixel;
2963       m_pEZPlot->ezset (os.str());
2964     }
2965     
2966     if (m_bMaxSpecified) {
2967       std::ostringstream os;
2968       os << "ymax " << m_dMaxPixel;
2969       m_pEZPlot->ezset (os.str());
2970     }
2971     
2972     m_pEZPlot->ezset("box");
2973     m_pEZPlot->ezset("grid");
2974     
2975     double* pdXaxis = new double [iNRecords];
2976     rPlotFile.getColumn (0, pdXaxis);
2977     
2978     double* pdY = new double [iNRecords];
2979     for (int iCol = 1; iCol < iNColumns; iCol++) {
2980       rPlotFile.getColumn (iCol, pdY);
2981       m_pEZPlot->addCurve (pdXaxis, pdY, iNRecords);
2982     }
2983     
2984     delete pdXaxis;
2985     delete pdY;
2986   }
2987   
2988   if (m_pCanvas)
2989     m_pCanvas->Refresh();
2990 }
2991
2992 bool 
2993 PlotFileView::OnClose (bool deleteWindow)
2994 {
2995   //GetDocumentManager()->ActivateView (this, false, true);
2996   if (! GetDocument() || ! GetDocument()->Close())
2997     return false;
2998   
2999   Activate(false);
3000   if (m_pCanvas) {
3001     m_pCanvas->setView (NULL);
3002     m_pCanvas = NULL;
3003   }
3004   wxString s(wxTheApp->GetAppName());
3005   if (m_pFrame)
3006     m_pFrame->SetTitle(s);
3007   
3008   SetFrame(NULL);
3009   if (deleteWindow) {
3010     delete m_pFrame;
3011     m_pFrame = NULL;
3012     if (GetDocument() && GetDocument()->getBadFileOpen())
3013       ::wxYield();  // wxWindows bug workaround
3014   }
3015   
3016   return true;
3017 }
3018
3019
3020 ////////////////////////////////////////////////////////////////
3021
3022
3023 IMPLEMENT_DYNAMIC_CLASS(TextFileView, wxView)
3024
3025 TextFileView::~TextFileView() 
3026 {
3027   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
3028   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
3029 }
3030
3031 bool TextFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
3032 {
3033   m_pFrame = CreateChildFrame(doc, this);
3034   SetFrame (m_pFrame);
3035   
3036   int width, height;
3037   m_pFrame->GetClientSize(&width, &height);
3038   m_pFrame->SetTitle("TextFile");
3039   m_pCanvas = new TextFileCanvas (this, m_pFrame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE | wxTE_READONLY);
3040   m_pFrame->SetTitle("Log");
3041   
3042 #ifdef __X__
3043   // X seems to require a forced resize
3044   int x, y;
3045   frame->GetSize(&x, &y);
3046   frame->SetSize(-1, -1, x, y);
3047 #endif
3048   
3049   m_pFrame->Show (true);
3050   Activate (true);
3051   
3052   return true;
3053 }
3054
3055 // Handled by wxTextWindow
3056 void TextFileView::OnDraw(wxDC *WXUNUSED(dc) )
3057 {
3058 }
3059
3060 void TextFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
3061 {
3062 }
3063
3064 bool 
3065 TextFileView::OnClose (bool deleteWindow)
3066 {
3067   if (! theApp->getMainFrame()->getShuttingDown())
3068     return false;
3069   
3070   Activate(false);
3071   //GetDocumentManager()->ActivateView (this, false, true);
3072   if (! GetDocument() || ! GetDocument()->Close())
3073     return false;
3074   
3075   SetFrame(NULL);
3076   if (deleteWindow) {
3077     delete m_pFrame;
3078     m_pFrame = NULL;
3079     if (GetDocument() && GetDocument()->getBadFileOpen())
3080       ::wxYield();  // wxWindows bug workaround
3081   }
3082   
3083   return TRUE;
3084 }
3085
3086 #if CTSIM_MDI
3087 wxDocMDIChildFrame*
3088 #else
3089 wxDocChildFrame*
3090 #endif
3091 TextFileView::CreateChildFrame (wxDocument *doc, wxView *view)
3092 {
3093 #if CTSIM_MDI
3094   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(0,0), wxDEFAULT_FRAME_STYLE, "Log");
3095 #else
3096   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE, "Log");
3097 #endif
3098   theApp->setIconForFrame (subframe);
3099   
3100   m_pFileMenu = new wxMenu;
3101   
3102   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
3103   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
3104   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
3105   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
3106   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
3107   //  m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
3108   
3109   m_pFileMenu->AppendSeparator();
3110   m_pFileMenu->Append(wxID_PRINT, "&Print...");
3111   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
3112   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
3113 #ifdef CTSIM_MDI
3114   m_pFileMenu->AppendSeparator();
3115   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
3116   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
3117 #endif
3118   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
3119   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
3120   
3121   wxMenu *help_menu = new wxMenu;
3122   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
3123   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
3124   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
3125   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
3126   
3127   wxMenuBar *menu_bar = new wxMenuBar;
3128   
3129   menu_bar->Append(m_pFileMenu, "&File");
3130   menu_bar->Append(help_menu, "&Help");
3131   
3132   subframe->SetMenuBar(menu_bar);
3133   subframe->Centre(wxBOTH);
3134   
3135   return subframe;
3136 }
3137
3138
3139 // Define a constructor for my text subwindow
3140 TextFileCanvas::TextFileCanvas (TextFileView* v, wxFrame* frame, const wxPoint& pos, const wxSize& size, long style)
3141 : wxTextCtrl (frame, -1, "", pos, size, style), m_pView(v)
3142 {
3143 }
3144
3145 TextFileCanvas::~TextFileCanvas ()
3146 {
3147   m_pView = NULL;
3148 }