r540: Improved dialog boxes
[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.104 2001/02/14 18:39:47 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, "2D &FFT");
867   filter_menu->Append (IFMENU_FILTER_IFFT, "2D &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, "F&ourier");
873   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "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(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
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[11];
931   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
932   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
933   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
934   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
935   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
936   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
937   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
938   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('A'), IFMENU_VIEW_SCALE_AUTO);
939   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('U'), IFMENU_VIEW_SCALE_FULL);
940   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('E'), IFMENU_VIEW_SCALE_MINMAX);
941 #if wxUSE_GLCANVAS
942   accelEntries[10].Set (wxACCEL_CTRL, static_cast<int>('3'), IFMENU_IMAGE_CONVERT3D);
943   wxAcceleratorTable accelTable (11, accelEntries);
944 #else
945   wxAcceleratorTable accelTable (10, accelEntries);
946 #endif
947
948   subframe->SetAcceleratorTable (accelTable);
949   
950   return subframe;
951 }
952
953
954 bool 
955 ImageFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
956 {
957   m_pFrame = CreateChildFrame(doc, this);
958   
959   m_bMinSpecified = false;
960   m_bMaxSpecified = false;
961   m_dAutoScaleFactor = 1.;
962   
963   int width, height;
964   m_pFrame->GetClientSize (&width, &height);
965   m_pFrame->SetTitle("ImageFileView");
966   m_pCanvas = CreateCanvas (m_pFrame);
967   
968   int x, y;  // X requires a forced resize
969   m_pFrame->GetSize(&x, &y);
970   m_pFrame->SetSize(-1, -1, x, y);
971   m_pFrame->SetFocus();
972   m_pFrame->Show(true);
973   Activate(true);
974   
975   return true;
976 }
977
978 void 
979 ImageFileView::OnDraw (wxDC* dc)
980 {
981   wxSize sizeWindow = m_pFrame->GetClientSize();
982   wxSize sizeBest = m_pCanvas->GetBestSize();
983   if (sizeWindow.x > sizeBest.x || sizeWindow.y > sizeBest.y)
984     m_pFrame->SetClientSize (sizeBest);
985   
986   if (m_bitmap.Ok())
987     dc->DrawBitmap(m_bitmap, 0, 0, false);
988   
989   int xCursor, yCursor;
990   if (m_pCanvas->GetCurrentCursor (xCursor, yCursor))
991     m_pCanvas->DrawRubberBandCursor (*dc, xCursor, yCursor);
992 }
993
994
995 void 
996 ImageFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
997 {
998   const ImageFile& rIF = GetDocument()->getImageFile();
999   ImageFileArrayConst v = rIF.getArray();
1000   int nx = rIF.nx();
1001   int ny = rIF.ny();
1002   if (v != NULL && nx != 0 && ny != 0) {
1003     if (! m_bMinSpecified || ! m_bMaxSpecified) {
1004       double min, max;
1005       rIF.getMinMax (min, max);
1006       if (! m_bMinSpecified)
1007         m_dMinPixel = min;
1008       if (! m_bMaxSpecified)
1009         m_dMaxPixel = max;
1010     }
1011     double scaleWidth = m_dMaxPixel - m_dMinPixel;
1012     
1013     unsigned char* imageData = new unsigned char [nx * ny * 3];
1014     for (int ix = 0; ix < nx; ix++) {
1015       for (int iy = 0; iy < ny; iy++) {
1016         double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
1017         int intensity = static_cast<int>(scaleValue + 0.5);
1018         intensity = clamp (intensity, 0, 255);
1019         int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
1020         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
1021       }
1022     }
1023     wxImage image (nx, ny, imageData, true);
1024     m_bitmap = image.ConvertToBitmap();
1025     delete imageData;
1026     int xSize = nx;
1027     int ySize = ny;
1028     ySize = clamp (ySize, 0, 800);
1029     m_pFrame->SetClientSize (xSize, ySize);
1030     m_pCanvas->SetScrollbars(20, 20, nx/20, ny/20);
1031     m_pCanvas->SetBackgroundColour(*wxWHITE);
1032   } 
1033   
1034   if (m_pCanvas)
1035     m_pCanvas->Refresh();
1036 }
1037
1038 bool 
1039 ImageFileView::OnClose (bool deleteWindow)
1040 {
1041   //GetDocumentManager()->ActivateView (this, false, true);
1042   if (! GetDocument() || ! GetDocument()->Close())
1043     return false;
1044
1045   Activate (false);
1046   if (m_pCanvas) {
1047     m_pCanvas->setView(NULL);
1048     m_pCanvas = NULL;
1049   }
1050   wxString s(theApp->GetAppName());
1051   if (m_pFrame)
1052     m_pFrame->SetTitle(s);
1053   
1054   SetFrame(NULL);
1055   
1056   if (deleteWindow) {
1057     delete m_pFrame;
1058     m_pFrame = NULL;
1059     if (GetDocument() && GetDocument()->getBadFileOpen())
1060       ::wxYield();  // wxWindows bug workaround
1061   }
1062   
1063   return true;
1064 }
1065
1066 void
1067 ImageFileView::OnExport (wxCommandEvent& event)
1068 {
1069   ImageFile& rIF = GetDocument()->getImageFile();
1070   ImageFileArrayConst v = rIF.getArray();
1071   int nx = rIF.nx();
1072   int ny = rIF.ny();
1073   if (v != NULL && nx != 0 && ny != 0) {
1074     if (! m_bMinSpecified || ! m_bMaxSpecified) {
1075       double min, max;
1076       rIF.getMinMax (min, max);
1077       if (! m_bMinSpecified)
1078         m_dMinPixel = min;
1079       if (! m_bMaxSpecified)
1080         m_dMaxPixel = max;
1081     }
1082     
1083     DialogExportParameters dialogExport (getFrameForChild(), m_iDefaultExportFormatID);
1084     if (dialogExport.ShowModal() == wxID_OK) {
1085       wxString strFormatName (dialogExport.getFormatName ());
1086       m_iDefaultExportFormatID = ImageFile::convertFormatNameToID (strFormatName.c_str());
1087       
1088       wxString strExt;
1089       wxString strWildcard;
1090       if (m_iDefaultExportFormatID == ImageFile::FORMAT_PGM || m_iDefaultExportFormatID == ImageFile::FORMAT_PGMASCII) {
1091         strExt = ".pgm";
1092         strWildcard = "PGM Files (*.pgm)|*.pgm";
1093       }
1094 #ifdef HAVE_PNG
1095       else if (m_iDefaultExportFormatID == ImageFile::FORMAT_PNG || m_iDefaultExportFormatID == ImageFile::FORMAT_PNG16) {
1096         strExt = ".png";
1097         strWildcard = "PNG Files (*.png)|*.png";
1098       }
1099 #endif
1100       
1101       const wxString& strFilename = wxFileSelector (wxString("Export Filename"), wxString(""), 
1102         wxString(""), strExt, strWildcard, wxOVERWRITE_PROMPT | wxHIDE_READONLY | wxSAVE);
1103       if (strFilename) {
1104         rIF.exportImage (strFormatName.c_str(), strFilename.c_str(), 1, 1, m_dMinPixel, m_dMaxPixel);
1105         *theApp->getLog() << "Exported file " << strFilename << "\n";
1106       }
1107     }
1108   }
1109 }
1110
1111 void
1112 ImageFileView::OnScaleSize (wxCommandEvent& event)
1113 {
1114   ImageFile& rIF = GetDocument()->getImageFile();
1115   unsigned int iOldNX = rIF.nx();
1116   unsigned int iOldNY = rIF.ny();
1117   
1118   DialogGetXYSize dialogGetXYSize (getFrameForChild(), "Set New X & Y Dimensions", iOldNX, iOldNY);
1119   if (dialogGetXYSize.ShowModal() == wxID_OK) {
1120     unsigned int iNewNX = dialogGetXYSize.getXSize();
1121     unsigned int iNewNY = dialogGetXYSize.getYSize();
1122     std::ostringstream os;
1123     os << "Scale Size from (" << iOldNX << "," << iOldNY << ") to (" << iNewNX << "," << iNewNY << ")";
1124     ImageFileDocument* pScaledDoc = theApp->newImageDoc();
1125     if (! pScaledDoc) {
1126       sys_error (ERR_SEVERE, "Unable to create image file");
1127       return;
1128     }
1129     ImageFile& rScaledIF = pScaledDoc->getImageFile();
1130     rScaledIF.setArraySize (iNewNX, iNewNY);
1131     rScaledIF.labelsCopy (rIF);
1132     rScaledIF.labelAdd (os.str().c_str());
1133     rIF.scaleImage (rScaledIF);
1134     *theApp->getLog() << os.str().c_str() << "\n";
1135     if (theApp->getAskDeleteNewDocs())
1136        pScaledDoc->Modify (true);
1137     pScaledDoc->UpdateAllViews (this);
1138     pScaledDoc->getView()->OnUpdate (this, NULL);
1139     pScaledDoc->getView()->getFrame()->Show(true);
1140   }
1141 }
1142
1143 #if wxUSE_GLCANVAS
1144 void
1145 ImageFileView::OnConvert3d (wxCommandEvent& event)
1146 {
1147   ImageFile& rIF = GetDocument()->getImageFile();
1148   Graph3dFileDocument* pGraph3d = theApp->newGraph3dDoc();
1149   pGraph3d->setBadFileOpen();
1150   pGraph3d->createFromImageFile (rIF);
1151   pGraph3d->getView()->OnUpdate (this, NULL);
1152   pGraph3d->UpdateAllViews();
1153   pGraph3d->getView()->getFrame()->SetClientSize (400, 400);
1154   pGraph3d->getView()->getFrame()->Show (true);
1155   GetDocumentManager()->ActivateView (pGraph3d->getView(), true, false);
1156   ::wxYield();
1157   pGraph3d->getView()->getCanvas()->SetFocus();
1158 }
1159 #endif
1160
1161 void
1162 ImageFileView::OnPlotRow (wxCommandEvent& event)
1163 {
1164   int xCursor, yCursor;
1165   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1166     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1167     return;
1168   }
1169   
1170   const ImageFile& rIF = GetDocument()->getImageFile();
1171   ImageFileArrayConst v = rIF.getArray();
1172   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1173   int nx = rIF.nx();
1174   int ny = rIF.ny();
1175   
1176   if (v != NULL && yCursor < ny) {
1177     double* pX = new double [nx];
1178     double* pYReal = new double [nx];
1179     double *pYImag = NULL;
1180     double *pYMag = NULL;
1181     if (rIF.isComplex()) {
1182       pYImag = new double [nx];
1183       pYMag = new double [nx];
1184     }
1185     for (int i = 0; i < nx; i++) {
1186       pX[i] = i;
1187       pYReal[i] = v[i][yCursor];
1188       if (rIF.isComplex()) {
1189         pYImag[i] = vImag[i][yCursor];
1190         pYMag[i] = ::sqrt (v[i][yCursor] * v[i][yCursor] + vImag[i][yCursor] * vImag[i][yCursor]);
1191       }
1192     }
1193     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1194     if (! pPlotDoc) {
1195       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1196     } else {
1197       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1198       std::ostringstream os;
1199       os << "Row " << yCursor;
1200       std::string title("title ");
1201       title += os.str();
1202       rPlotFile.addEzsetCommand (title.c_str());
1203       rPlotFile.addEzsetCommand ("xlabel Column");
1204       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1205       rPlotFile.addEzsetCommand ("lxfrac 0");
1206       rPlotFile.addEzsetCommand ("box");
1207       rPlotFile.addEzsetCommand ("grid");
1208       rPlotFile.addEzsetCommand ("curve 1");
1209       rPlotFile.addEzsetCommand ("color 1");
1210       if (rIF.isComplex()) {
1211         rPlotFile.addEzsetCommand ("dash 1");
1212         rPlotFile.addEzsetCommand ("curve 2");
1213         rPlotFile.addEzsetCommand ("color 4");
1214         rPlotFile.addEzsetCommand ("dash 3");
1215         rPlotFile.addEzsetCommand ("curve 3");
1216         rPlotFile.addEzsetCommand ("color 0");
1217         rPlotFile.addEzsetCommand ("solid");
1218         rPlotFile.setCurveSize (4, nx);
1219       } else
1220         rPlotFile.setCurveSize (2, nx);
1221       rPlotFile.addColumn (0, pX);
1222       rPlotFile.addColumn (1, pYReal); 
1223       if (rIF.isComplex()) {
1224         rPlotFile.addColumn (2, pYImag);
1225         rPlotFile.addColumn (3, pYMag);
1226       }
1227       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1228         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1229       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1230       *theApp->getLog() << os.str().c_str() << "\n";
1231       rPlotFile.addDescription (os.str().c_str());
1232     }
1233     delete pX;
1234     delete pYReal;
1235     if (rIF.isComplex()) {
1236       delete pYImag;
1237       delete pYMag;
1238     }
1239     if (theApp->getAskDeleteNewDocs())
1240        pPlotDoc->Modify (true);
1241     pPlotDoc->UpdateAllViews ();
1242     pPlotDoc->getView()->OnUpdate (this, NULL);
1243     pPlotDoc->getView()->getFrame()->Show(true);
1244   }
1245 }
1246
1247 void
1248 ImageFileView::OnPlotCol (wxCommandEvent& event)
1249 {
1250   int xCursor, yCursor;
1251   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1252     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1253     return;
1254   }
1255   
1256   const ImageFile& rIF = GetDocument()->getImageFile();
1257   ImageFileArrayConst v = rIF.getArray();
1258   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1259   int nx = rIF.nx();
1260   int ny = rIF.ny();
1261   
1262   if (v != NULL && xCursor < nx) {
1263     double* pX = new double [ny];
1264     double* pYReal = new double [ny];
1265     double* pYImag = NULL;
1266     double* pYMag = NULL;
1267     if (rIF.isComplex()) {
1268       pYImag = new double [ny];
1269       pYMag = new double [ny];
1270     }
1271     for (int i = 0; i < ny; i++) {
1272       pX[i] = i;
1273       pYReal[i] = v[xCursor][i];
1274       if (rIF.isComplex()) {
1275         pYImag[i] = vImag[xCursor][i];
1276         pYMag[i] = ::sqrt (v[xCursor][i] * v[xCursor][i] + vImag[xCursor][i] * vImag[xCursor][i]);
1277       }
1278     }
1279     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1280     if (! pPlotDoc) {
1281       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1282     } else {
1283       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1284       std::ostringstream os;
1285       os << "Column " << xCursor;
1286       std::string title("title ");
1287       title += os.str();
1288       rPlotFile.addEzsetCommand (title.c_str());
1289       rPlotFile.addEzsetCommand ("xlabel Row");
1290       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1291       rPlotFile.addEzsetCommand ("lxfrac 0");
1292       rPlotFile.addEzsetCommand ("box");
1293       rPlotFile.addEzsetCommand ("grid");
1294       rPlotFile.addEzsetCommand ("curve 1");
1295       rPlotFile.addEzsetCommand ("color 1");
1296       if (rIF.isComplex()) {
1297         rPlotFile.addEzsetCommand ("dash 1");
1298         rPlotFile.addEzsetCommand ("curve 2");
1299         rPlotFile.addEzsetCommand ("color 4");
1300         rPlotFile.addEzsetCommand ("dash 3");
1301         rPlotFile.addEzsetCommand ("curve 3");
1302         rPlotFile.addEzsetCommand ("color 0");
1303         rPlotFile.addEzsetCommand ("solid");
1304         rPlotFile.setCurveSize (4, ny);
1305       } else
1306         rPlotFile.setCurveSize (2, ny);
1307       rPlotFile.addColumn (0, pX);
1308       rPlotFile.addColumn (1, pYReal); 
1309       if (rIF.isComplex()) {
1310         rPlotFile.addColumn (2, pYImag);
1311         rPlotFile.addColumn (3, pYMag);
1312       }
1313       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1314         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1315       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1316       *theApp->getLog() << os.str().c_str() << "\n";
1317       rPlotFile.addDescription (os.str().c_str());
1318     }
1319     delete pX;
1320     delete pYReal;
1321     if (rIF.isComplex()) {
1322       delete pYImag;
1323       delete pYMag;
1324     }
1325     if (theApp->getAskDeleteNewDocs())
1326        pPlotDoc->Modify (true);
1327     pPlotDoc->UpdateAllViews ();
1328     pPlotDoc->getView()->OnUpdate (this, NULL);
1329     pPlotDoc->getView()->getFrame()->Show(true);
1330   }
1331 }
1332
1333 #ifdef HAVE_FFT
1334 void
1335 ImageFileView::OnPlotFFTRow (wxCommandEvent& event)
1336 {
1337   int xCursor, yCursor;
1338   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1339     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1340     return;
1341   }
1342   
1343   const ImageFile& rIF = GetDocument()->getImageFile();
1344   ImageFileArrayConst v = rIF.getArray();
1345   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1346   int nx = rIF.nx();
1347   int ny = rIF.ny();
1348   
1349   if (v != NULL && yCursor < ny) {
1350     fftw_complex* pcIn = new fftw_complex [nx];
1351     
1352     int i;
1353     for (i = 0; i < nx; i++) {
1354       pcIn[i].re = v[i][yCursor];
1355       if (rIF.isComplex())
1356         pcIn[i].im = vImag[i][yCursor];
1357       else
1358         pcIn[i].im = 0;
1359     }
1360     
1361     fftw_plan plan = fftw_create_plan (nx, FFTW_FORWARD, FFTW_IN_PLACE);
1362     fftw_one (plan, pcIn, NULL);
1363     fftw_destroy_plan (plan);
1364     
1365     double* pX = new double [nx];
1366     double* pYReal = new double [nx];
1367     double* pYImag = new double [nx];
1368     double* pYMag = new double [nx];
1369     for (i = 0; i < nx; i++) {
1370       pX[i] = i;
1371       pYReal[i] = pcIn[i].re;
1372       pYImag[i] = pcIn[i].im;
1373       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1374     }
1375     Fourier::shuffleFourierToNaturalOrder (pYReal, nx);
1376     Fourier::shuffleFourierToNaturalOrder (pYImag, nx);
1377     Fourier::shuffleFourierToNaturalOrder (pYMag, nx);
1378     
1379     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1380     if (! pPlotDoc) {
1381       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1382     } else {
1383       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1384       std::ostringstream os;
1385       os << "Row " << yCursor;
1386       std::string title("title ");
1387       title += os.str();
1388       rPlotFile.addEzsetCommand (title.c_str());
1389       rPlotFile.addEzsetCommand ("xlabel Column");
1390       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1391       rPlotFile.addEzsetCommand ("lxfrac 0");
1392       rPlotFile.addEzsetCommand ("curve 1");
1393       rPlotFile.addEzsetCommand ("color 1");
1394       rPlotFile.addEzsetCommand ("dash 1");
1395       rPlotFile.addEzsetCommand ("curve 2");
1396       rPlotFile.addEzsetCommand ("color 4");
1397       rPlotFile.addEzsetCommand ("dash 3");
1398       rPlotFile.addEzsetCommand ("curve 3");
1399       rPlotFile.addEzsetCommand ("color 0");
1400       rPlotFile.addEzsetCommand ("solid");
1401       rPlotFile.addEzsetCommand ("box");
1402       rPlotFile.addEzsetCommand ("grid");
1403       rPlotFile.setCurveSize (4, nx);
1404       rPlotFile.addColumn (0, pX);
1405       rPlotFile.addColumn (1, pYReal);
1406       rPlotFile.addColumn (2, pYImag);
1407       rPlotFile.addColumn (3, pYMag);
1408       for (int iL = 0; iL < rIF.nLabels(); iL++)
1409         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1410       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1411       *theApp->getLog() << os.str().c_str() << "\n";
1412       rPlotFile.addDescription (os.str().c_str());
1413     }
1414     delete pX;
1415     delete pYReal;
1416     delete pYImag;
1417     delete pYMag;
1418     delete [] pcIn;
1419     
1420     if (theApp->getAskDeleteNewDocs())
1421        pPlotDoc->Modify (true);
1422     pPlotDoc->UpdateAllViews ();
1423     pPlotDoc->getView()->OnUpdate (this, NULL);
1424     pPlotDoc->getView()->getFrame()->Show(true);
1425   }
1426 }
1427
1428 void
1429 ImageFileView::OnPlotFFTCol (wxCommandEvent& event)
1430 {
1431   int xCursor, yCursor;
1432   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1433     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1434     return;
1435   }
1436   
1437   const ImageFile& rIF = GetDocument()->getImageFile();
1438   ImageFileArrayConst v = rIF.getArray();
1439   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1440   int nx = rIF.nx();
1441   int ny = rIF.ny();
1442   
1443   if (v != NULL && xCursor < nx) {
1444     fftw_complex* pcIn = new fftw_complex [ny];
1445     double *pdTemp = new double [ny];
1446     
1447     int i;
1448     for (i = 0; i < ny; i++)
1449       pdTemp[i] = v[xCursor][i];
1450     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1451     for (i = 0; i < ny; i++) 
1452       pcIn[i].re = pdTemp[i];
1453     
1454     for (i = 0; i < ny; i++) {
1455       if (rIF.isComplex())
1456         pdTemp[i] = vImag[xCursor][i];
1457       else
1458         pdTemp[i] = 0;
1459     }
1460     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1461     for (i = 0; i < ny; i++)
1462       pcIn[i].im = pdTemp[i];
1463     
1464     fftw_plan plan = fftw_create_plan (ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1465     fftw_one (plan, pcIn, NULL);
1466     fftw_destroy_plan (plan);
1467     
1468     double* pX = new double [ny];
1469     double* pYReal = new double [ny];
1470     double* pYImag = new double [ny];
1471     double* pYMag = new double [ny];
1472     for (i = 0; i < ny; i++) {
1473       pX[i] = i;
1474       pYReal[i] = pcIn[i].re;
1475       pYImag[i] = pcIn[i].im;
1476       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1477     }
1478     
1479     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1480     if (! pPlotDoc) {
1481       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1482     } else {
1483       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1484       std::ostringstream os;
1485       os << "Column " << xCursor;
1486       std::string title("title ");
1487       title += os.str();
1488       rPlotFile.addEzsetCommand (title.c_str());
1489       rPlotFile.addEzsetCommand ("xlabel Column");
1490       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1491       rPlotFile.addEzsetCommand ("lxfrac 0");
1492       rPlotFile.addEzsetCommand ("curve 1");
1493       rPlotFile.addEzsetCommand ("color 1");
1494       rPlotFile.addEzsetCommand ("dash 1");
1495       rPlotFile.addEzsetCommand ("curve 2");
1496       rPlotFile.addEzsetCommand ("color 4");
1497       rPlotFile.addEzsetCommand ("dash 3");
1498       rPlotFile.addEzsetCommand ("curve 3");
1499       rPlotFile.addEzsetCommand ("color 0");
1500       rPlotFile.addEzsetCommand ("solid");
1501       rPlotFile.addEzsetCommand ("box");
1502       rPlotFile.addEzsetCommand ("grid");
1503       rPlotFile.setCurveSize (4, ny);
1504       rPlotFile.addColumn (0, pX);
1505       rPlotFile.addColumn (1, pYReal);
1506       rPlotFile.addColumn (2, pYImag);
1507       rPlotFile.addColumn (3, pYMag);
1508       for (int iL = 0; iL < rIF.nLabels(); iL++)
1509         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1510       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1511       *theApp->getLog() << os.str().c_str() << "\n";
1512       rPlotFile.addDescription (os.str().c_str());
1513     }
1514     delete pX;
1515     delete pYReal;
1516     delete pYImag;
1517     delete pYMag;
1518     delete pdTemp;
1519     delete [] pcIn;
1520     
1521     if (theApp->getAskDeleteNewDocs())
1522        pPlotDoc->Modify (true);
1523     pPlotDoc->UpdateAllViews ();
1524     pPlotDoc->getView()->OnUpdate (this, NULL);
1525     pPlotDoc->getView()->getFrame()->Show(true);
1526   }
1527 }
1528 #endif
1529
1530 void
1531 ImageFileView::OnCompareCol (wxCommandEvent& event)
1532 {
1533   int xCursor, yCursor;
1534   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1535     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1536     return;
1537   }
1538   
1539   std::vector<ImageFileDocument*> vecIFDoc;
1540   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1541   if (vecIFDoc.size() == 0) {
1542     wxMessageBox ("No compatible images for Column Comparison", "Error");
1543     return;
1544   }
1545   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1546   
1547   if (dialogGetCompare.ShowModal() == wxID_OK) {
1548     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1549     const ImageFile& rIF = GetDocument()->getImageFile();
1550     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1551     
1552     ImageFileArrayConst v1 = rIF.getArray();
1553     ImageFileArrayConst v2 = rCompareIF.getArray();
1554     int nx = rIF.nx();
1555     int ny = rIF.ny();
1556     
1557     if (v1 != NULL && xCursor < nx) {
1558       double* pX = new double [ny];
1559       double* pY1 = new double [ny];
1560       double* pY2 = new double [ny];
1561       for (int i = 0; i < ny; i++) {
1562         pX[i] = i;
1563         pY1[i] = v1[xCursor][i];
1564         pY2[i] = v2[xCursor][i];
1565       }
1566       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1567       if (! pPlotDoc) {
1568         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1569       } else {
1570         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1571         std::ostringstream os;
1572         os << "Column " << xCursor << " Comparison";
1573         std::string title("title ");
1574         title += os.str();
1575         rPlotFile.addEzsetCommand (title.c_str());
1576         rPlotFile.addEzsetCommand ("xlabel Row");
1577         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1578         rPlotFile.addEzsetCommand ("lxfrac 0");
1579         rPlotFile.addEzsetCommand ("curve 1");
1580         rPlotFile.addEzsetCommand ("color 2");
1581         rPlotFile.addEzsetCommand ("curve 2");
1582         rPlotFile.addEzsetCommand ("color 4");
1583         rPlotFile.addEzsetCommand ("dash 5");
1584         rPlotFile.addEzsetCommand ("box");
1585         rPlotFile.addEzsetCommand ("grid");
1586         rPlotFile.setCurveSize (3, ny);
1587         rPlotFile.addColumn (0, pX);
1588         rPlotFile.addColumn (1, pY1);
1589         rPlotFile.addColumn (2, pY2);
1590         
1591         unsigned int iL;
1592         for (iL = 0; iL < rIF.nLabels(); iL++) {
1593           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1594           s += ": ";
1595           s += rIF.labelGet(iL).getLabelString();
1596           rPlotFile.addDescription (s.c_str());
1597         }
1598         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1599           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1600           s += ": ";
1601           s += rCompareIF.labelGet(iL).getLabelString();
1602           rPlotFile.addDescription (s.c_str());
1603         }
1604         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1605           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1606         *theApp->getLog() << os.str().c_str() << "\n";
1607         rPlotFile.addDescription (os.str().c_str());
1608       }
1609       delete pX;
1610       delete pY1;
1611       delete pY2;
1612       if (theApp->getAskDeleteNewDocs())
1613         pPlotDoc->Modify (true);
1614       pPlotDoc->UpdateAllViews ();
1615       pPlotDoc->getView()->OnUpdate (this, NULL);
1616       pPlotDoc->getView()->getFrame()->Show(true);
1617     }
1618   }
1619 }
1620
1621 void
1622 ImageFileView::OnCompareRow (wxCommandEvent& event)
1623 {
1624   int xCursor, yCursor;
1625   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1626     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1627     return;
1628   }
1629   
1630   std::vector<ImageFileDocument*> vecIFDoc;
1631   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1632   
1633   if (vecIFDoc.size() == 0) {
1634     wxMessageBox ("No compatible images for Row Comparison", "Error");
1635     return;
1636   }
1637   
1638   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1639   
1640   if (dialogGetCompare.ShowModal() == wxID_OK) {
1641     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1642     const ImageFile& rIF = GetDocument()->getImageFile();
1643     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1644     
1645     ImageFileArrayConst v1 = rIF.getArray();
1646     ImageFileArrayConst v2 = rCompareIF.getArray();
1647     int nx = rIF.nx();
1648     int ny = rIF.ny();
1649     
1650     if (v1 != NULL && yCursor < ny) {
1651       double* pX = new double [nx];
1652       double* pY1 = new double [nx];
1653       double* pY2 = new double [nx];
1654       for (int i = 0; i < nx; i++) {
1655         pX[i] = i;
1656         pY1[i] = v1[i][yCursor];
1657         pY2[i] = v2[i][yCursor];
1658       }
1659       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1660       if (! pPlotDoc) {
1661         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1662       } else {
1663         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1664         std::ostringstream os;
1665         os << "Row " << yCursor << " Comparison";
1666         std::string title("title ");
1667         title += os.str();
1668         rPlotFile.addEzsetCommand (title.c_str());
1669         rPlotFile.addEzsetCommand ("xlabel Column");
1670         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1671         rPlotFile.addEzsetCommand ("lxfrac 0");
1672         rPlotFile.addEzsetCommand ("curve 1");
1673         rPlotFile.addEzsetCommand ("color 2");
1674         rPlotFile.addEzsetCommand ("curve 2");
1675         rPlotFile.addEzsetCommand ("color 4");
1676         rPlotFile.addEzsetCommand ("dash 5");
1677         rPlotFile.addEzsetCommand ("box");
1678         rPlotFile.addEzsetCommand ("grid");
1679         rPlotFile.setCurveSize (3, nx);
1680         rPlotFile.addColumn (0, pX);
1681         rPlotFile.addColumn (1, pY1);
1682         rPlotFile.addColumn (2, pY2);
1683         unsigned int iL;
1684         for (iL = 0; iL < rIF.nLabels(); iL++) {
1685           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1686           s += ": ";
1687           s += rIF.labelGet(iL).getLabelString();
1688           rPlotFile.addDescription (s.c_str());
1689         }
1690         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1691           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1692           s += ": ";
1693           s += rCompareIF.labelGet(iL).getLabelString();
1694           rPlotFile.addDescription (s.c_str());
1695         }
1696         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1697           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1698         *theApp->getLog() << os.str().c_str() << "\n";
1699         rPlotFile.addDescription (os.str().c_str());
1700       }
1701       delete pX;
1702       delete pY1;
1703       delete pY2;
1704       if (theApp->getAskDeleteNewDocs())
1705         pPlotDoc->Modify (true);
1706       pPlotDoc->UpdateAllViews ();
1707       pPlotDoc->getView()->OnUpdate (this, NULL);
1708       pPlotDoc->getView()->getFrame()->Show(true);
1709     }
1710   }
1711 }
1712
1713 static int NUMBER_HISTOGRAM_BINS = 256;
1714
1715 void
1716 ImageFileView::OnPlotHistogram (wxCommandEvent& event)
1717
1718   const ImageFile& rIF = GetDocument()->getImageFile();
1719   ImageFileArrayConst v = rIF.getArray();
1720   int nx = rIF.nx();
1721   int ny = rIF.ny();
1722   
1723   if (v != NULL && nx > 0 && ny > 0) {
1724     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1725     if (! pPlotDoc) {
1726       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1727       return;
1728     }
1729     
1730     double* pX = new double [NUMBER_HISTOGRAM_BINS];
1731     double* pY = new double [NUMBER_HISTOGRAM_BINS];
1732     double dMin, dMax;
1733     rIF.getMinMax (dMin, dMax);
1734     double dBinWidth = (dMax - dMin) / NUMBER_HISTOGRAM_BINS;
1735     
1736     for (int i = 0; i < NUMBER_HISTOGRAM_BINS; i++) {
1737       pX[i] = dMin + (i + 0.5) * dBinWidth;
1738       pY[i] = 0;
1739     }
1740     for (int ix = 0; ix < nx; ix++)
1741       for (int iy = 0; iy < ny; iy++) {
1742         int iBin = nearest<int> ((v[ix][iy] - dMin) / dBinWidth);
1743         if (iBin >= 0 && iBin < NUMBER_HISTOGRAM_BINS)
1744           pY[iBin] += 1;
1745       }
1746       
1747       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1748       std::ostringstream os;
1749       os << "Histogram";
1750       std::string title("title ");
1751       title += os.str();
1752       rPlotFile.addEzsetCommand (title.c_str());
1753       rPlotFile.addEzsetCommand ("xlabel Pixel Value");
1754       rPlotFile.addEzsetCommand ("ylabel Count");
1755       rPlotFile.addEzsetCommand ("box");
1756       rPlotFile.addEzsetCommand ("grid");
1757       rPlotFile.setCurveSize (2, NUMBER_HISTOGRAM_BINS);
1758       rPlotFile.addColumn (0, pX);
1759       rPlotFile.addColumn (1, pY);
1760       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++) {
1761         std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1762         s += ": ";
1763         s += rIF.labelGet(iL).getLabelString();
1764         rPlotFile.addDescription (s.c_str());
1765       }
1766       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1767       *theApp->getLog() << os.str().c_str() << "\n";
1768       rPlotFile.addDescription (os.str().c_str());
1769       delete pX;
1770       delete pY;
1771       if (theApp->getAskDeleteNewDocs())
1772         pPlotDoc->Modify (true);
1773       pPlotDoc->UpdateAllViews ();
1774       pPlotDoc->getView()->OnUpdate (this, NULL);
1775       pPlotDoc->getView()->getFrame()->Show(true);
1776   }
1777 }
1778
1779
1780 // PhantomCanvas
1781
1782 PhantomCanvas::PhantomCanvas (PhantomFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1783 : wxScrolledWindow(frame, -1, pos, size, style)
1784 {
1785   m_pView = v;
1786 }
1787
1788 PhantomCanvas::~PhantomCanvas ()
1789 {
1790   m_pView = NULL;
1791 }
1792
1793 void 
1794 PhantomCanvas::OnDraw (wxDC& dc)
1795 {
1796   if (m_pView)
1797     m_pView->OnDraw(& dc);
1798 }
1799
1800 wxSize
1801 PhantomCanvas::GetBestSize() const
1802 {
1803   if (! m_pView)
1804     return wxSize(0,0);
1805
1806   int xSize, ySize;
1807   theApp->getMainFrame()->GetClientSize (&xSize, &ySize);
1808   xSize = maxValue<int> (xSize, ySize);
1809   ySize = xSize = (xSize / 3);
1810   return wxSize (xSize, ySize);
1811 }
1812
1813
1814
1815 // PhantomFileView
1816
1817 IMPLEMENT_DYNAMIC_CLASS(PhantomFileView, wxView)
1818
1819 BEGIN_EVENT_TABLE(PhantomFileView, wxView)
1820 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomFileView::OnProperties)
1821 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomFileView::OnRasterize)
1822 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomFileView::OnProjections)
1823 END_EVENT_TABLE()
1824
1825 PhantomFileView::PhantomFileView() 
1826 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0)
1827 {
1828 #if defined(DEBUG) || defined(_DEBUG)
1829   m_iDefaultNDet = 165;
1830   m_iDefaultNView = 180;
1831 #else
1832   m_iDefaultNDet = 367;
1833   m_iDefaultNView = 320;
1834 #endif
1835   m_iDefaultNSample = 1;
1836   m_dDefaultRotation = 2;
1837   m_dDefaultFocalLength = 2;
1838   m_dDefaultViewRatio = 1;
1839   m_dDefaultScanRatio = 1;
1840   m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
1841   m_iDefaultTrace = Trace::TRACE_NONE;
1842   
1843 #ifdef DEBUG 
1844   m_iDefaultRasterNX = 115;
1845   m_iDefaultRasterNY = 115;
1846   m_iDefaultRasterNSamples = 1;
1847 #else
1848   m_iDefaultRasterNX = 256;
1849   m_iDefaultRasterNY = 256;
1850   m_iDefaultRasterNSamples = 2;
1851 #endif
1852   m_dDefaultRasterViewRatio = 1;
1853 }
1854
1855 PhantomFileView::~PhantomFileView()
1856 {
1857   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
1858   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
1859 }
1860
1861 void
1862 PhantomFileView::OnProperties (wxCommandEvent& event)
1863 {
1864   const int idPhantom = GetDocument()->getPhantomID();
1865   const wxString& namePhantom = GetDocument()->getPhantomName();
1866   std::ostringstream os;
1867   os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")" << "\n";
1868   const Phantom& rPhantom = GetDocument()->getPhantom();
1869   rPhantom.printDefinitions (os);
1870 #if DEBUG
1871   rPhantom.print (os);
1872 #endif
1873   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
1874   wxMessageBox (os.str().c_str(), "Phantom Properties");
1875 }
1876
1877
1878 void
1879 PhantomFileView::OnProjections (wxCommandEvent& event)
1880 {
1881   DialogGetProjectionParameters dialogProjection (getFrameForChild(), 
1882     m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, 
1883     m_dDefaultFocalLength, m_dDefaultViewRatio, m_dDefaultScanRatio, m_iDefaultGeometry, 
1884     m_iDefaultTrace);
1885   int retVal = dialogProjection.ShowModal();
1886   if (retVal == wxID_OK) {
1887     m_iDefaultNDet = dialogProjection.getNDet();
1888     m_iDefaultNView = dialogProjection.getNView();
1889     m_iDefaultNSample = dialogProjection.getNSamples();
1890     m_iDefaultTrace = dialogProjection.getTrace();
1891     m_dDefaultRotation = dialogProjection.getRotAngle();
1892     m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
1893     m_dDefaultViewRatio = dialogProjection.getViewRatio();
1894     m_dDefaultScanRatio = dialogProjection.getScanRatio();
1895     wxString sGeometry = dialogProjection.getGeometry();
1896     m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
1897     
1898     if (m_iDefaultNDet > 0 && m_iDefaultNView > 0 && sGeometry != "") {
1899       const Phantom& rPhantom = GetDocument()->getPhantom();
1900       Projections* pProj = new Projections;
1901       Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, 
1902                           m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultViewRatio, m_dDefaultScanRatio);
1903       if (theScanner.fail()) {
1904         wxString msg = "Failed making scanner\n";
1905         msg += theScanner.failMessage().c_str();
1906         *theApp->getLog() << msg << "\n";
1907         wxMessageBox (msg, "Error");
1908         return;
1909       }
1910       pProj->initFromScanner (theScanner);
1911       m_dDefaultRotation /= PI;  // convert back to PI units
1912       
1913       Timer timer;
1914       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
1915         ProjectionsDialog dialogProjections (theScanner, *pProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(getFrameForChild()));
1916         for (int iView = 0; iView < pProj->nView(); iView++) {
1917           ::wxYield();
1918           if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1919             delete pProj;
1920             return;
1921           }
1922           ::wxYield();
1923           while (dialogProjections.isPaused()) {
1924             ::wxYield();
1925             ::wxUsleep(50);
1926           }
1927         }
1928       } else {
1929         wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), pProj->nView() + 1, getFrameForChild(), wxPD_CAN_ABORT);
1930         for (int i = 0; i < pProj->nView(); i++) {
1931           theScanner.collectProjections (*pProj, rPhantom, i, 1, true, m_iDefaultTrace);
1932           if (! dlgProgress.Update (i+1)) {
1933             delete pProj;
1934             return;
1935           }
1936         }
1937       }
1938       
1939       std::ostringstream os;
1940       os << "Projections for " << rPhantom.name() << ": nDet=" << m_iDefaultNDet 
1941         << ", nView=" << m_iDefaultNView << ", nSamples=" << m_iDefaultNSample 
1942         << ", RotAngle=" << m_dDefaultRotation << ", FocalLengthRatio=" << m_dDefaultFocalLength 
1943         << ", ViewRatio=" << m_dDefaultViewRatio << ", ScanRatio=" << m_dDefaultScanRatio 
1944         << ", Geometry=" << sGeometry.c_str() << ", FanBeamAngle=" << 
1945         convertRadiansToDegrees (theScanner.fanBeamAngle());
1946       pProj->setCalcTime (timer.timerEnd());
1947       pProj->setRemark (os.str());
1948       *theApp->getLog() << os.str().c_str() << "\n";
1949       
1950       ::wxYield();
1951       ProjectionFileDocument* pProjectionDoc = theApp->newProjectionDoc();
1952       if (! pProjectionDoc) {
1953         sys_error (ERR_SEVERE, "Unable to create projection document");
1954         return;
1955       }
1956       pProjectionDoc->setProjections (pProj);
1957       ProjectionFileView* projView = pProjectionDoc->getView();
1958       if (projView) {
1959         projView->OnUpdate (projView, NULL);
1960         if (projView->getCanvas())
1961               projView->getCanvas()->SetClientSize (m_iDefaultNDet, m_iDefaultNView);
1962         if (wxFrame* pFrame = projView->getFrame()) {
1963           pFrame->Show(true);
1964           pFrame->SetFocus();
1965           pFrame->Raise();
1966         }
1967         GetDocumentManager()->ActivateView (projView, true, false);
1968       }
1969       ::wxYield();
1970       if (theApp->getAskDeleteNewDocs())
1971         pProjectionDoc-> Modify(true);
1972       pProjectionDoc->UpdateAllViews (this);
1973     }
1974   }
1975 }
1976
1977
1978 void
1979 PhantomFileView::OnRasterize (wxCommandEvent& event)
1980 {
1981   DialogGetRasterParameters dialogRaster (getFrameForChild(), m_iDefaultRasterNX, m_iDefaultRasterNY, 
1982     m_iDefaultRasterNSamples, m_dDefaultRasterViewRatio);
1983   int retVal = dialogRaster.ShowModal();
1984   if (retVal == wxID_OK) {
1985     m_iDefaultRasterNX = dialogRaster.getXSize();
1986     m_iDefaultRasterNY  = dialogRaster.getYSize();
1987     m_iDefaultRasterNSamples = dialogRaster.getNSamples();
1988     m_dDefaultRasterViewRatio = dialogRaster.getViewRatio();
1989     if (m_iDefaultRasterNSamples < 1)
1990       m_iDefaultRasterNSamples = 1;
1991     if (m_dDefaultRasterViewRatio < 0)
1992       m_dDefaultRasterViewRatio = 0;
1993     if (m_iDefaultRasterNX > 0 && m_iDefaultRasterNY > 0) {
1994       const Phantom& rPhantom = GetDocument()->getPhantom();
1995
1996       ImageFile* pImageFile = new ImageFile;
1997       
1998       pImageFile->setArraySize (m_iDefaultRasterNX, m_iDefaultRasterNY);
1999       wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), 
2000                                     pImageFile->nx() + 1, getFrameForChild(), wxPD_CAN_ABORT);
2001       Timer timer;
2002       for (unsigned int i = 0; i < pImageFile->nx(); i++) {
2003         rPhantom.convertToImagefile (*pImageFile, m_dDefaultRasterViewRatio, m_iDefaultRasterNSamples, 
2004           Trace::TRACE_NONE, i, 1, true);
2005         if (! dlgProgress.Update (i+1)) {
2006           delete pImageFile;
2007           return;
2008         }
2009       }
2010
2011       ImageFileDocument* pRasterDoc = theApp->newImageDoc();
2012       if (! pRasterDoc) {
2013         sys_error (ERR_SEVERE, "Unable to create image file");
2014         return;
2015       }
2016       pRasterDoc->setImageFile (pImageFile);
2017
2018       if (theApp->getAskDeleteNewDocs())
2019         pRasterDoc->Modify (true);
2020       pRasterDoc->UpdateAllViews (this);
2021       pRasterDoc->getView()->getFrame()->Show(true);
2022       std::ostringstream os;
2023       os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
2024         << m_iDefaultRasterNY << ", ViewRatio=" << m_dDefaultRasterViewRatio << ", nSamples=" 
2025         << m_iDefaultRasterNSamples;;
2026       *theApp->getLog() << os.str().c_str() << "\n";
2027       pImageFile->labelAdd (os.str().c_str(), timer.timerEnd());
2028       ImageFileView* rasterView = pRasterDoc->getView();
2029       if (rasterView) {
2030         rasterView->getFrame()->SetFocus();
2031         rasterView->OnUpdate (rasterView, NULL);
2032       }
2033       
2034     }
2035   }
2036 }
2037
2038
2039 PhantomCanvas* 
2040 PhantomFileView::CreateCanvas (wxFrame *parent)
2041 {
2042   PhantomCanvas* pCanvas;
2043   
2044   pCanvas = new PhantomCanvas (this, parent, wxPoint(0, 0), wxSize(0,0), 0);
2045   pCanvas->SetBackgroundColour(*wxWHITE);
2046   pCanvas->Clear();
2047   
2048   return pCanvas;
2049 }
2050
2051 #if CTSIM_MDI
2052 wxDocMDIChildFrame*
2053 #else
2054 wxDocChildFrame*
2055 #endif
2056 PhantomFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2057 {
2058 #if CTSIM_MDI
2059   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2060 #else
2061   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2062 #endif
2063   theApp->setIconForFrame (subframe);
2064   
2065   m_pFileMenu = new wxMenu;
2066   
2067   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2068   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2069   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2070   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2071   m_pFileMenu->Append(wxID_CLOSE, "&Close");
2072   
2073   m_pFileMenu->AppendSeparator();
2074   m_pFileMenu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
2075   
2076   m_pFileMenu->AppendSeparator();
2077   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2078   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2079   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2080 #ifdef CTSIM_MDI
2081   m_pFileMenu->AppendSeparator();
2082   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2083   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2084 #endif
2085   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2086   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2087   
2088   wxMenu *process_menu = new wxMenu;
2089   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...\tCtrl-R");
2090   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...\tCtrl-J");
2091   
2092   wxMenu *help_menu = new wxMenu;
2093   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2094   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2095   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2096   
2097   wxMenuBar *menu_bar = new wxMenuBar;
2098   
2099   menu_bar->Append(m_pFileMenu, "&File");
2100   menu_bar->Append(process_menu, "&Process");
2101   menu_bar->Append(help_menu, "&Help");
2102   
2103   subframe->SetMenuBar(menu_bar);
2104   subframe->Centre(wxBOTH);
2105   
2106   wxAcceleratorEntry accelEntries[8];
2107   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2108   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2109   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2110   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2111   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2112   accelEntries[5].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2113   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('J'), PHMMENU_PROCESS_PROJECTIONS);
2114   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('R'), PHMMENU_PROCESS_RASTERIZE);
2115   wxAcceleratorTable accelTable (8, accelEntries);
2116   subframe->SetAcceleratorTable (accelTable);
2117   
2118   return subframe;
2119 }
2120
2121
2122 bool 
2123 PhantomFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2124 {
2125   m_pFrame = CreateChildFrame(doc, this);
2126   SetFrame(m_pFrame);
2127   m_pCanvas = CreateCanvas (m_pFrame);
2128   m_pFrame->SetClientSize (m_pCanvas->GetBestSize());
2129   m_pCanvas->SetClientSize (m_pCanvas->GetBestSize());
2130   
2131   m_pFrame->SetTitle ("PhantomFileView");
2132   
2133 #ifdef __X__
2134   int x, y;  // X requires a forced resize
2135   m_pFrame->GetSize(&x, &y);
2136   m_pFrame->SetSize(-1, -1, x, y);
2137 #endif
2138   
2139   m_pFrame->Show(true);
2140   Activate(true);
2141   
2142   return true;
2143 }
2144
2145 void 
2146 PhantomFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2147 {
2148   if (m_pCanvas)
2149     m_pCanvas->Refresh();
2150 }
2151
2152 bool 
2153 PhantomFileView::OnClose (bool deleteWindow)
2154 {
2155   //GetDocumentManager()->ActivateView (this, false, true);
2156   if (! GetDocument() || ! GetDocument()->Close())
2157     return false;
2158   
2159   Activate(false);
2160   if (m_pCanvas) {
2161     m_pCanvas->setView(NULL);
2162     m_pCanvas = NULL;
2163   }
2164   wxString s(wxTheApp->GetAppName());
2165   if (m_pFrame)
2166     m_pFrame->SetTitle(s);
2167   
2168   SetFrame(NULL);
2169   
2170   if (deleteWindow) {
2171     delete m_pFrame;
2172     m_pFrame = NULL;
2173     if (GetDocument() && GetDocument()->getBadFileOpen())
2174       ::wxYield();  // wxWindows bug workaround
2175   }
2176   
2177   return true;
2178 }
2179
2180 void
2181 PhantomFileView::OnDraw (wxDC* dc)
2182 {
2183   int xsize, ysize;
2184   m_pCanvas->GetClientSize (&xsize, &ysize);
2185   SGPDriver driver (dc, xsize, ysize);
2186   SGP sgp (driver);
2187   const Phantom& rPhantom = GetDocument()->getPhantom();
2188   sgp.setColor (C_RED);
2189   rPhantom.show (sgp);
2190 }
2191
2192 // ProjectionCanvas
2193
2194 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2195 : wxScrolledWindow(frame, -1, pos, size, style)
2196 {
2197   m_pView = v;
2198 }
2199
2200 ProjectionFileCanvas::~ProjectionFileCanvas ()
2201 {
2202   m_pView = NULL;
2203 }
2204
2205 void 
2206 ProjectionFileCanvas::OnDraw(wxDC& dc)
2207 {
2208   if (m_pView)
2209     m_pView->OnDraw(& dc);
2210 }
2211
2212 wxSize
2213 ProjectionFileCanvas::GetBestSize () const
2214 {
2215   wxSize best (0, 0);
2216   if (m_pView) {
2217     Projections& rProj = m_pView->GetDocument()->getProjections();
2218     best.Set (rProj.nDet(), rProj.nView());
2219   }
2220   
2221   return best;
2222 }
2223
2224
2225 // ProjectionFileView
2226
2227 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
2228
2229 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
2230 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
2231 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
2232 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2233 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2234 END_EVENT_TABLE()
2235
2236 ProjectionFileView::ProjectionFileView() 
2237 : wxView(), m_pFrame(0), m_pCanvas(0), m_pFileMenu(0)
2238 {
2239 #ifdef DEBUG
2240   m_iDefaultNX = 115;
2241   m_iDefaultNY = 115;
2242 #else
2243   m_iDefaultNX = 256;
2244   m_iDefaultNY = 256;
2245 #endif
2246   
2247   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2248   m_dDefaultFilterParam = 1.;
2249 #if HAVE_FFTW
2250   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2251   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
2252 #else
2253   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2254   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2255 #endif
2256   m_iDefaultZeropad = 1;
2257   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF;
2258   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2259   m_iDefaultInterpParam = 1;
2260   m_iDefaultTrace = Trace::TRACE_NONE;
2261   
2262   m_iDefaultPolarNX = 256;
2263   m_iDefaultPolarNY = 256;
2264   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2265   m_iDefaultPolarZeropad = 1;
2266 }
2267
2268 ProjectionFileView::~ProjectionFileView()
2269 {
2270   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
2271   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
2272 }
2273
2274 void
2275 ProjectionFileView::OnProperties (wxCommandEvent& event)
2276 {
2277   const Projections& rProj = GetDocument()->getProjections();
2278   std::ostringstream os;
2279   rProj.printScanInfo(os);
2280   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
2281   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2282   dialogMsg.ShowModal();
2283 }
2284
2285
2286 void
2287 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2288 {
2289   Projections& rProj = GetDocument()->getProjections();
2290   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2291     m_iDefaultPolarInterpolation, -1);
2292   if (dialogPolar.ShowModal() == wxID_OK) {
2293     wxString strInterpolation (dialogPolar.getInterpolationName());
2294     m_iDefaultPolarNX = dialogPolar.getXSize();
2295     m_iDefaultPolarNY = dialogPolar.getYSize();
2296     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2297     ImageFile& rIF = pPolarDoc->getImageFile();
2298     if (! pPolarDoc) {
2299       sys_error (ERR_SEVERE, "Unable to create image file");
2300       return;
2301     }
2302     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2303     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2304     rProj.convertPolar (rIF, m_iDefaultPolarInterpolation);
2305     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2306     std::ostringstream os;
2307     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2308       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2309       << strInterpolation.c_str();
2310     *theApp->getLog() << os.str().c_str() << "\n";
2311     rIF.labelAdd (os.str().c_str());
2312     if (theApp->getAskDeleteNewDocs())
2313       pPolarDoc->Modify (true);
2314     pPolarDoc->UpdateAllViews ();
2315     pPolarDoc->getView()->OnUpdate (this, NULL);
2316     pPolarDoc->getView()->getFrame()->Show(true);
2317   }
2318 }
2319
2320 void
2321 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2322 {
2323   Projections& rProj = GetDocument()->getProjections();
2324   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2325     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2326   if (dialogPolar.ShowModal() == wxID_OK) {
2327     wxString strInterpolation (dialogPolar.getInterpolationName());
2328     m_iDefaultPolarNX = dialogPolar.getXSize();
2329     m_iDefaultPolarNY = dialogPolar.getYSize();
2330     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2331     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2332     ImageFile& rIF = pPolarDoc->getImageFile();
2333     if (! pPolarDoc) {
2334       sys_error (ERR_SEVERE, "Unable to create image file");
2335       return;
2336     }
2337     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2338     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2339     rProj.convertFFTPolar (rIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2340     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2341     std::ostringstream os;
2342     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2343       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2344       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2345     *theApp->getLog() << os.str().c_str() << "\n";
2346     rIF.labelAdd (os.str().c_str());
2347     if (theApp->getAskDeleteNewDocs())
2348       pPolarDoc->Modify (true);
2349     pPolarDoc->UpdateAllViews ();
2350     pPolarDoc->getView()->OnUpdate (this, NULL);
2351     pPolarDoc->getView()->getFrame()->Show(true);
2352   }
2353 }
2354
2355 void
2356 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2357 {
2358   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2359 }
2360
2361 void
2362 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2363 {
2364   DialogGetReconstructionParameters dialogReconstruction (getFrameForChild(), m_iDefaultNX, m_iDefaultNY, 
2365     m_iDefaultFilter, m_dDefaultFilterParam, m_iDefaultFilterMethod, m_iDefaultFilterGeneration, 
2366     m_iDefaultZeropad, m_iDefaultInterpolation, m_iDefaultInterpParam, m_iDefaultBackprojector, 
2367     m_iDefaultTrace);
2368   
2369   int retVal = dialogReconstruction.ShowModal();
2370   if (retVal == wxID_OK) {
2371     m_iDefaultNX = dialogReconstruction.getXSize();
2372     m_iDefaultNY = dialogReconstruction.getYSize();
2373     wxString optFilterName = dialogReconstruction.getFilterName();
2374     m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2375     m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2376     wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2377     m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2378     m_iDefaultZeropad = dialogReconstruction.getZeropad();
2379     wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2380     m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2381     wxString optInterpName = dialogReconstruction.getInterpName();
2382     m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2383     m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2384     wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2385     m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2386     m_iDefaultTrace = dialogReconstruction.getTrace();
2387     if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
2388       const Projections& rProj = GetDocument()->getProjections();
2389
2390       ImageFile* pImageFile = new ImageFile;
2391       pImageFile->setArraySize (m_iDefaultNX, m_iDefaultNY);
2392       
2393       Reconstructor* pReconstructor = new Reconstructor (rProj, *pImageFile, optFilterName.c_str(), m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
2394       
2395       Timer timerRecon;
2396       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2397         ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstructor, rProj, *pImageFile, m_iDefaultTrace, getFrameForChild());
2398         for (int iView = 0; iView < rProj.nView(); iView++) {
2399           ::wxYield();
2400           if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView)) {
2401             delete pDlgReconstruct;
2402             delete pReconstructor;
2403             delete pImageFile;
2404             return;
2405           }
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 i = 0; i < rProj.nView(); i++) {
2416           pReconstructor->reconstructView (i, 1);
2417           if (! dlgProgress.Update (i + 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(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
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[11];
2528   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2529   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2530   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2531   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2532   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2533   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2534   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2535   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), PJMENU_CONVERT_POLAR);
2536   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('I'), PJMENU_CONVERT_FFT_POLAR);
2537   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('R'), PJMENU_RECONSTRUCT_FBP);
2538   accelEntries[10].Set (wxACCEL_CTRL, static_cast<int>('E'), PJMENU_RECONSTRUCT_FOURIER);
2539   wxAcceleratorTable accelTable (11, accelEntries);
2540   subframe->SetAcceleratorTable (accelTable);
2541   
2542   return subframe;
2543 }
2544
2545
2546 bool 
2547 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2548 {
2549   m_pFrame = CreateChildFrame(doc, this);
2550   SetFrame(m_pFrame);
2551   
2552   int width, height;
2553   m_pFrame->GetClientSize (&width, &height);
2554   m_pFrame->SetTitle ("ProjectionFileView");
2555   m_pCanvas = CreateCanvas (m_pFrame);
2556   
2557 #ifdef __X__
2558   int x, y;  // X requires a forced resize
2559   m_pFrame->GetSize(&x, &y);
2560   m_pFrame->SetSize(-1, -1, x, y);
2561 #endif
2562   
2563   m_pFrame->Show(true);
2564   Activate(true);
2565   
2566   return true;
2567 }
2568
2569 void 
2570 ProjectionFileView::OnDraw (wxDC* dc)
2571 {
2572   wxSize clientSize = m_pFrame->GetClientSize();
2573   wxSize bestSize = m_pCanvas->GetBestSize();
2574   
2575   if (clientSize.x > bestSize.x || clientSize.y > bestSize.y)
2576     m_pFrame->SetClientSize (bestSize);
2577   
2578   if (m_bitmap.Ok())
2579     dc->DrawBitmap (m_bitmap, 0, 0, false);
2580 }
2581
2582
2583 void 
2584 ProjectionFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2585 {
2586   const Projections& rProj = GetDocument()->getProjections();
2587   const int nDet = rProj.nDet();
2588   const int nView = rProj.nView();
2589   if (nDet != 0 && nView != 0) {
2590     const DetectorArray& detarray = rProj.getDetectorArray(0);
2591     const DetectorValue* detval = detarray.detValues();
2592     double min = detval[0];
2593     double max = detval[0];
2594     for (int iy = 0; iy < nView; iy++) {
2595       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2596       const DetectorValue* detval = detarray.detValues();
2597       for (int ix = 0; ix < nDet; ix++) {
2598         if (min > detval[ix])
2599           min = detval[ix];
2600         else if (max < detval[ix])
2601           max = detval[ix];
2602       }
2603     }
2604     
2605     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2606     double scale = (max - min) / 255;
2607     for (int iy2 = 0; iy2 < nView; iy2++) {
2608       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2609       const DetectorValue* detval = detarray.detValues();
2610       for (int ix = 0; ix < nDet; ix++) {
2611         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2612         intensity = clamp(intensity, 0, 255);
2613         int baseAddr = (iy2 * nDet + ix) * 3;
2614         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2615       }
2616     }
2617     wxImage image (nDet, nView, imageData, true);
2618     m_bitmap = image.ConvertToBitmap();
2619     delete imageData;
2620     int xSize = nDet;
2621     int ySize = nView;
2622     xSize = clamp (xSize, 0, 800);
2623     ySize = clamp (ySize, 0, 800);
2624     m_pFrame->SetClientSize (xSize, ySize);
2625     m_pCanvas->SetScrollbars (20, 20, nDet/20, nView/20);
2626   }
2627   
2628   if (m_pCanvas)
2629     m_pCanvas->Refresh();
2630 }
2631
2632 bool 
2633 ProjectionFileView::OnClose (bool deleteWindow)
2634 {
2635   //GetDocumentManager()->ActivateView (this, false, true);
2636   if (! GetDocument() || ! GetDocument()->Close())
2637     return false;
2638   
2639   Activate(false);
2640   if (m_pCanvas) {
2641         m_pCanvas->setView(NULL);
2642     m_pCanvas = NULL;
2643   }
2644   wxString s(wxTheApp->GetAppName());
2645   if (m_pFrame)
2646     m_pFrame->SetTitle(s);
2647   
2648   SetFrame(NULL);
2649   
2650   if (deleteWindow) {
2651     delete m_pFrame;
2652     m_pFrame = NULL;
2653     if (GetDocument() && GetDocument()->getBadFileOpen())
2654       ::wxYield();  // wxWindows bug workaround
2655   }
2656
2657   return true;
2658 }
2659
2660
2661
2662 // PlotFileCanvas
2663 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2664 : wxScrolledWindow(frame, -1, pos, size, style)
2665 {
2666   m_pView = v;
2667 }
2668
2669 PlotFileCanvas::~PlotFileCanvas ()
2670 {
2671   m_pView = NULL;
2672 }
2673
2674 void 
2675 PlotFileCanvas::OnDraw(wxDC& dc)
2676 {
2677   if (m_pView)
2678     m_pView->OnDraw(& dc);
2679 }
2680
2681
2682 // PlotFileView
2683
2684 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2685
2686 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2687 EVT_MENU(PJMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2688 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2689 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2690 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2691 END_EVENT_TABLE()
2692
2693 PlotFileView::PlotFileView() 
2694 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pEZPlot(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
2695 {
2696 }
2697
2698 PlotFileView::~PlotFileView()
2699 {
2700   if (m_pEZPlot)
2701     delete m_pEZPlot;
2702   
2703   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);  
2704 }
2705
2706 void
2707 PlotFileView::OnProperties (wxCommandEvent& event)
2708 {
2709   const PlotFile& rPlot = GetDocument()->getPlotFile();
2710   std::ostringstream os;
2711   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2712   rPlot.printHeadersBrief (os);
2713   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<<\n";
2714   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2715   dialogMsg.ShowModal();
2716 }
2717
2718
2719 void 
2720 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2721 {
2722   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2723   double min, max, mean, mode, median, stddev;
2724   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2725   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
2726   int iRetVal = dialogAutoScale.ShowModal();
2727   if (iRetVal == wxID_OK) {
2728     m_bMinSpecified = true;
2729     m_bMaxSpecified = true;
2730     double dMin, dMax;
2731     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2732       m_dMinPixel = dMin;
2733       m_dMaxPixel = dMax;
2734       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2735       OnUpdate (this, NULL);
2736     }
2737   }
2738 }
2739
2740 void 
2741 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2742 {
2743   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2744   double min;
2745   double max;
2746   
2747   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2748     if (! rPlotFile.getMinMax (1, min, max)) {
2749       *theApp->getLog() << "Error: unable to find Min/Max\n";
2750       return;
2751     }
2752   }
2753   
2754   if (m_bMinSpecified)
2755     min = m_dMinPixel;
2756   if (m_bMaxSpecified)
2757     max = m_dMaxPixel;
2758   
2759   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Y-axis Minimum & Maximum", min, max);
2760   int retVal = dialogMinMax.ShowModal();
2761   if (retVal == wxID_OK) {
2762     m_bMinSpecified = true;
2763     m_bMaxSpecified = true;
2764     m_dMinPixel = dialogMinMax.getMinimum();
2765     m_dMaxPixel = dialogMinMax.getMaximum();
2766     OnUpdate (this, NULL);
2767   }
2768 }
2769
2770 void 
2771 PlotFileView::OnScaleFull (wxCommandEvent& event)
2772 {
2773   if (m_bMinSpecified || m_bMaxSpecified) {
2774     m_bMinSpecified = false;
2775     m_bMaxSpecified = false;
2776     OnUpdate (this, NULL);
2777   }
2778 }
2779
2780
2781 PlotFileCanvas* 
2782 PlotFileView::CreateCanvas (wxFrame* parent)
2783 {
2784   PlotFileCanvas* pCanvas;
2785   int width, height;
2786   parent->GetClientSize(&width, &height);
2787   
2788   pCanvas = new PlotFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
2789   
2790   pCanvas->SetBackgroundColour(*wxWHITE);
2791   pCanvas->Clear();
2792   
2793   return pCanvas;
2794 }
2795
2796 #if CTSIM_MDI
2797 wxDocMDIChildFrame*
2798 #else
2799 wxDocChildFrame*
2800 #endif
2801 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2802 {
2803 #ifdef CTSIM_MDI
2804   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2805 #else
2806   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2807 #endif
2808   theApp->setIconForFrame (subframe);
2809   
2810   m_pFileMenu = new wxMenu;
2811   
2812   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2813   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2814   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2815   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2816   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2817   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2818   
2819   m_pFileMenu->AppendSeparator();
2820   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2821   
2822   m_pFileMenu->AppendSeparator();
2823   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2824   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2825   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2826 #ifdef CTSIM_MDI
2827   m_pFileMenu->AppendSeparator();
2828   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2829   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2830 #endif
2831   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2832   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2833   
2834   wxMenu *view_menu = new wxMenu;
2835   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...\tCtrl-E");
2836   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
2837   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale\tCtrl-U");
2838   
2839   wxMenu *help_menu = new wxMenu;
2840   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2841   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2842   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2843   
2844   wxMenuBar *menu_bar = new wxMenuBar;
2845   
2846   menu_bar->Append(m_pFileMenu, "&File");
2847   menu_bar->Append(view_menu, "&View");
2848   menu_bar->Append(help_menu, "&Help");
2849   
2850   subframe->SetMenuBar(menu_bar);
2851   subframe->Centre(wxBOTH);
2852   
2853   wxAcceleratorEntry accelEntries[10];
2854   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2855   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2856   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2857   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2858   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2859   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2860   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2861   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('E'), PLOTMENU_VIEW_SCALE_MINMAX);
2862   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('A'), PLOTMENU_VIEW_SCALE_AUTO);
2863   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('U'), PLOTMENU_VIEW_SCALE_FULL);
2864   wxAcceleratorTable accelTable (10, accelEntries);
2865   subframe->SetAcceleratorTable (accelTable);
2866   
2867   return subframe;
2868 }
2869
2870
2871 bool 
2872 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
2873 {
2874   m_pFrame = CreateChildFrame(doc, this);
2875   SetFrame(m_pFrame);
2876   
2877   m_bMinSpecified = false;
2878   m_bMaxSpecified = false;
2879   m_dAutoScaleFactor = 1.;
2880   
2881   int width, height;
2882   m_pFrame->GetClientSize(&width, &height);
2883   m_pFrame->SetTitle ("Plot File");
2884   m_pCanvas = CreateCanvas (m_pFrame);
2885   
2886 #ifdef __X__
2887   int x, y;  // X requires a forced resize
2888   m_pFrame->GetSize(&x, &y);
2889   m_pFrame->SetSize(-1, -1, x, y);
2890 #endif
2891   
2892   m_pFrame->Show(true);
2893   Activate(true);
2894   
2895   return true;
2896 }
2897
2898 void 
2899 PlotFileView::OnDraw (wxDC* dc)
2900 {
2901   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2902   const int iNColumns = rPlotFile.getNumColumns();
2903   const int iNRecords = rPlotFile.getNumRecords();
2904   
2905   if (iNColumns > 0 && iNRecords > 0) {
2906     int xsize, ysize;
2907     m_pCanvas->GetClientSize (&xsize, &ysize);
2908     SGPDriver driver (dc, xsize, ysize);
2909     SGP sgp (driver);
2910     if (m_pEZPlot)
2911       m_pEZPlot->plot (&sgp);
2912   }
2913 }
2914
2915
2916 void 
2917 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2918 {
2919   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2920   const int iNColumns = rPlotFile.getNumColumns();
2921   const int iNRecords = rPlotFile.getNumRecords();
2922   
2923   if (iNColumns > 0 && iNRecords > 0) {
2924     if (m_pEZPlot)
2925       delete m_pEZPlot;
2926     m_pEZPlot = new EZPlot;
2927     
2928     for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
2929       m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
2930     
2931     if (m_bMinSpecified) {
2932       std::ostringstream os;
2933       os << "ymin " << m_dMinPixel;
2934       m_pEZPlot->ezset (os.str());
2935     }
2936     
2937     if (m_bMaxSpecified) {
2938       std::ostringstream os;
2939       os << "ymax " << m_dMaxPixel;
2940       m_pEZPlot->ezset (os.str());
2941     }
2942     
2943     m_pEZPlot->ezset("box");
2944     m_pEZPlot->ezset("grid");
2945     
2946     double* pdXaxis = new double [iNRecords];
2947     rPlotFile.getColumn (0, pdXaxis);
2948     
2949     double* pdY = new double [iNRecords];
2950     for (int iCol = 1; iCol < iNColumns; iCol++) {
2951       rPlotFile.getColumn (iCol, pdY);
2952       m_pEZPlot->addCurve (pdXaxis, pdY, iNRecords);
2953     }
2954     
2955     delete pdXaxis;
2956     delete pdY;
2957   }
2958   
2959   if (m_pCanvas)
2960     m_pCanvas->Refresh();
2961 }
2962
2963 bool 
2964 PlotFileView::OnClose (bool deleteWindow)
2965 {
2966   //GetDocumentManager()->ActivateView (this, false, true);
2967   if (! GetDocument() || ! GetDocument()->Close())
2968     return false;
2969   
2970   Activate(false);
2971   if (m_pCanvas) {
2972     m_pCanvas->setView (NULL);
2973     m_pCanvas = NULL;
2974   }
2975   wxString s(wxTheApp->GetAppName());
2976   if (m_pFrame)
2977     m_pFrame->SetTitle(s);
2978   
2979   SetFrame(NULL);
2980   if (deleteWindow) {
2981     delete m_pFrame;
2982     m_pFrame = NULL;
2983     if (GetDocument() && GetDocument()->getBadFileOpen())
2984       ::wxYield();  // wxWindows bug workaround
2985  }
2986
2987   return true;
2988 }
2989
2990
2991 ////////////////////////////////////////////////////////////////
2992
2993
2994 IMPLEMENT_DYNAMIC_CLASS(TextFileView, wxView)
2995
2996 TextFileView::~TextFileView() 
2997 {
2998   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
2999   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
3000 }
3001
3002 bool TextFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
3003 {
3004   m_pFrame = CreateChildFrame(doc, this);
3005   SetFrame (m_pFrame);
3006   
3007   int width, height;
3008   m_pFrame->GetClientSize(&width, &height);
3009   m_pFrame->SetTitle("TextFile");
3010   m_pCanvas = new TextFileCanvas (this, m_pFrame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE | wxTE_READONLY);
3011   m_pFrame->SetTitle("Log");
3012   
3013 #ifdef __X__
3014   // X seems to require a forced resize
3015   int x, y;
3016   frame->GetSize(&x, &y);
3017   frame->SetSize(-1, -1, x, y);
3018 #endif
3019   
3020   m_pFrame->Show (true);
3021   Activate (true);
3022   
3023   return true;
3024 }
3025
3026 // Handled by wxTextWindow
3027 void TextFileView::OnDraw(wxDC *WXUNUSED(dc) )
3028 {
3029 }
3030
3031 void TextFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
3032 {
3033 }
3034
3035 bool 
3036 TextFileView::OnClose (bool deleteWindow)
3037 {
3038   if (! theApp->getMainFrame()->getShuttingDown())
3039     return false;
3040   
3041   Activate(false);
3042   //GetDocumentManager()->ActivateView (this, false, true);
3043   if (! GetDocument() || ! GetDocument()->Close())
3044     return false;
3045   
3046   SetFrame(NULL);
3047   if (deleteWindow) {
3048     delete m_pFrame;
3049     m_pFrame = NULL;
3050     if (GetDocument() && GetDocument()->getBadFileOpen())
3051       ::wxYield();  // wxWindows bug workaround
3052   }
3053
3054   return TRUE;
3055 }
3056
3057 #if CTSIM_MDI
3058 wxDocMDIChildFrame*
3059 #else
3060 wxDocChildFrame*
3061 #endif
3062 TextFileView::CreateChildFrame (wxDocument *doc, wxView *view)
3063 {
3064 #if CTSIM_MDI
3065   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(0,0), wxDEFAULT_FRAME_STYLE, "Log");
3066 #else
3067   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE, "Log");
3068 #endif
3069   theApp->setIconForFrame (subframe);
3070   
3071  m_pFileMenu = new wxMenu;
3072   
3073   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
3074   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
3075   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
3076   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
3077   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
3078   //  m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
3079   
3080   m_pFileMenu->AppendSeparator();
3081   m_pFileMenu->Append(wxID_PRINT, "&Print...");
3082   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
3083   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
3084 #ifdef CTSIM_MDI
3085   m_pFileMenu->AppendSeparator();
3086   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
3087   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
3088 #endif
3089   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
3090   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
3091   
3092   wxMenu *help_menu = new wxMenu;
3093   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
3094   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
3095   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
3096   
3097   wxMenuBar *menu_bar = new wxMenuBar;
3098   
3099   menu_bar->Append(m_pFileMenu, "&File");
3100   menu_bar->Append(help_menu, "&Help");
3101   
3102   subframe->SetMenuBar(menu_bar);
3103   subframe->Centre(wxBOTH);
3104   
3105   wxAcceleratorEntry accelEntries[5];
3106   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
3107   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
3108   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
3109   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
3110   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
3111   wxAcceleratorTable accelTable (5, accelEntries);
3112   subframe->SetAcceleratorTable (accelTable);
3113   
3114   return subframe;
3115 }
3116
3117
3118 // Define a constructor for my text subwindow
3119 TextFileCanvas::TextFileCanvas (TextFileView* v, wxFrame* frame, const wxPoint& pos, const wxSize& size, long style)
3120 : wxTextCtrl (frame, -1, "", pos, size, style), m_pView(v)
3121 {
3122 }
3123
3124 TextFileCanvas::~TextFileCanvas ()
3125 {
3126   m_pView = NULL;
3127 }