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