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