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