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