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