35ab913bd63e2680c3b015a826da5db52c1c84a0
[ctsim.git] / src / ctsim.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsim.cpp
5 **   Purpose:       Top-level routines of CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsim.cpp,v 1.79 2001/02/16 02:36:18 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 #ifdef MSVC
29 #define strdup _strdup
30 #endif
31
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/wx.h"
41 #endif
42
43 #include "wx/image.h"
44 #include "wx/filesys.h"
45 #include "wx/fs_zip.h"
46 #ifdef __WXMSW__
47 #include "wx/msw/helpchm.h"
48 #endif
49
50 #if !wxUSE_DOC_VIEW_ARCHITECTURE
51 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
52 #endif
53
54
55 #include "ct.h"
56 #include "ctsim.h"
57 #include "docs.h"
58 #include "views.h"
59 #include "dialogs.h"
60
61 #if defined(HAVE_CONFIG_H)
62 #include "config.h"
63 #endif
64
65 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
66 #ifdef MSVC
67 #define __STDC__ 1
68 #endif
69 #include "getopt.h"
70 #ifdef MSVC
71 #undef __STDC__
72 #endif
73 #endif
74
75 static const char* rcsindent = "$Id: ctsim.cpp,v 1.79 2001/02/16 02:36:18 kevin Exp $";
76
77 struct option CTSimApp::ctsimOptions[] = 
78 {
79         {"help", 0, 0, O_HELP},
80         {"version", 0, 0, O_VERSION},
81         {0, 0, 0, 0}
82 };
83
84 IMPLEMENT_APP(CTSimApp)
85
86 CTSimApp::CTSimApp()
87 : m_docManager(NULL), m_pFrame(NULL), m_pLog(0), m_pLogDoc(0), m_pConfig(0),
88   m_bAdvancedOptions(false), m_bSetModifyNewDocs(true)
89 {
90         theApp = this;
91 }
92
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96
97 #ifdef HAVE_SYS_RESOURCE_H
98 #include <sys/resource.h>
99 #endif
100
101 bool
102 CTSimApp::OnInit()
103 {
104 #ifdef HAVE_SETPRIORITY
105         setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
106 #endif
107         
108         openConfig();
109         
110         g_bRunningWXWindows = true;
111         // process options
112         while (1) {
113                 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
114                 if (c == -1)
115                         break;
116                 
117                 switch (c) {
118                 case O_VERSION:
119                         std::cout << rcsindent << std::endl;
120 #ifdef CTSIMVERSION
121                         std::cout << "Version: CTSIMVERSION" << std::endl;
122 #elif defined(VERSION)
123                         std::cout << "Version: VERSION" << std::endl;
124 #endif
125                         exit(0);
126                 case O_HELP:
127                 case '?':
128                         usage (argv[0]);
129                         exit (0);
130                 default:
131                         usage (argv[0]);
132                         exit (1);
133                 }
134         }
135         
136         m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
137         
138         m_pDocTemplImage = new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
139         m_pDocTemplProjection = new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
140         m_pDocTemplPhantom = new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
141         m_pDocTemplPlot = new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
142         m_pDocTemplText = new wxDocTemplate (m_docManager, "TextFile", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
143 #if wxUSE_GLCANVAS
144         m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, "Graph3dFile", "*.g3d", "", "g3d", "Graph3dFile", "Graph3dView", CLASSINFO(Graph3dFileDocument), CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
145 #endif
146
147 #if wxUSE_GIF
148         wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
149 #endif
150         
151 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
152         wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
153 #endif
154         
155         // Create the main frame window
156         int xDisplay, yDisplay;
157         ::wxDisplaySize (&xDisplay, &yDisplay);
158         m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), 
159                 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .755)), wxDEFAULT_FRAME_STYLE);
160         
161         setIconForFrame (m_pFrame);
162         m_pFrame->Centre(wxBOTH);
163         m_pFrame->Show(true);  
164         SetTopWindow (m_pFrame);
165         
166         if (m_pConfig)
167                 m_docManager->FileHistoryLoad(*m_pConfig);
168         
169 #ifdef CTSIM_MDI
170         m_pLogDoc = newTextDoc();
171         if (m_pLogDoc) {
172                 m_pLog = m_pLogDoc->getTextCtrl();
173                 m_pLogDoc->SetDocumentName("Log.txt");
174                 m_pLogDoc->SetFilename("Log.txt");
175                 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
176                 int xSize, ySize;
177                 m_pFrame->GetClientSize(&xSize, &ySize);
178                 int yLogSize = ySize / 4;
179                 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
180                 m_pLogDoc->getView()->getFrame()->Show (true);
181         } else
182 #else
183                 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
184 #endif
185         wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
186
187         wxString helpDir;
188         if (! m_pConfig->Read("HelpDir", &helpDir))
189                 helpDir = ::wxGetCwd();
190 #ifdef CTSIM_WINHELP
191         if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
192                 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
193 #else
194         if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
195                 ! m_pFrame->getHtmlHelpController().Initialize("/usr/local/man/ctsim"))
196                 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
197         else {
198             if (::wxDirExists ("/tmp"))
199                 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
200             m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
201         }
202 #endif
203
204         for (int i = optind + 1; i <= argc; i++) {
205                 wxString filename = argv [i - 1];
206                 m_docManager->CreateDocument (filename, wxDOC_SILENT);
207         }
208         
209         return true;
210 }
211
212
213 #include "./ctsim.xpm"
214 void
215 CTSimApp::setIconForFrame(wxFrame* pFrame)
216 {
217         wxIcon iconApp (ctsim16_xpm);
218         
219         if (iconApp.Ok())
220                 pFrame->SetIcon (iconApp);
221 }
222
223 void
224 CTSimApp::usage(const char* program)
225 {
226         std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
227         std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
228         std::cout << "\n";
229         std::cout << "  --version Display version\n";
230         std::cout << "  --help    Display this help message\n";
231 }
232
233 int
234 CTSimApp::OnExit()
235 {
236   closeConfig();
237
238 #ifdef HAVE_DMALLOC
239         dmalloc_shutdown();
240 #endif
241         return 0;
242 }
243
244 void
245 CTSimApp::openConfig()
246 {
247 #ifdef MSVC
248   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
249 #else
250   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
251 #endif
252
253         wxConfigBase::Set(m_pConfig);
254   m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
255   m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
256 }
257
258 void
259 CTSimApp::closeConfig()
260 {
261   m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
262   m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
263         delete m_pConfig;
264 }
265
266
267 wxString
268 CTSimApp::getUntitledFilename()
269 {
270         static int untitledNumber = 1;
271         
272         wxString filename ("Untitled");
273         filename << untitledNumber++;
274         
275         return (filename);
276 }
277
278
279 // Top-level window for CTSim
280
281 #if CTSIM_MDI
282 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
283
284 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
285 #else
286 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
287
288 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
289 #endif
290
291 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
292 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
293 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
294 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
295 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
296 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
297 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
298 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
299 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
300 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
301 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
302 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
303 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
304 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
305 EVT_SIZE(MainFrame::OnSize)
306
307 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
308 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
309 #endif
310 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
311 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
312 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
313 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
314 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
315 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
316 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
317 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
318 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
319 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
320 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
321 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
322 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
323 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
324 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
325 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
326 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
327 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
328 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
329 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
330 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
331 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
332 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
333 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
334 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
335 END_EVENT_TABLE()
336
337
338
339 #if CTSIM_MDI
340 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
341 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
342 #else
343 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
344 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
345 #endif
346 {
347   m_bShuttingDown = false;
348
349         //// Make a menubar
350         wxMenu *file_menu = new wxMenu;
351         
352         file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
353         file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
354         file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
355         
356         file_menu->AppendSeparator();
357   file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
358         file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
359         
360         //  history of files visited
361         theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
362         theApp->getDocManager()->FileHistoryUseMenu(file_menu);
363         
364 #ifndef CTSIM_MDI
365         m_pWindowMenu = new wxMenu;
366         m_pWindowMenu->UpdateUI (this);
367 #endif
368         
369         wxMenu* help_menu = new wxMenu;
370         help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
371 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
372         help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
373 #endif
374         help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
375         
376         wxMenuBar* menu_bar = new wxMenuBar;
377         
378         menu_bar->Append(file_menu, "&File");
379 #ifndef CTSIM_MDI
380         menu_bar->Append(m_pWindowMenu, "&Window");
381 #endif
382         menu_bar->Append(help_menu, "&Help");
383         
384         SetMenuBar(menu_bar);
385         
386 #ifndef CTSIM_MDI
387         int i;
388         for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
389                 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
390                 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
391                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
392         }
393 #endif
394         
395         m_iDefaultPhantomID = Phantom::PHM_HERMAN;
396         m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
397         m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
398         m_iDefaultFilterXSize = 256;
399         m_iDefaultFilterYSize = 256;
400         m_dDefaultFilterParam = 1.;
401         m_dDefaultFilterBandwidth = 1.;
402         m_dDefaultFilterInputScale = 1.;
403         m_dDefaultFilterOutputScale = 1.;
404         
405         wxAcceleratorEntry accelEntries[15];
406         accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
407         accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
408         accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
409 #ifndef CTSIM_MDI
410         accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
411         for (i = 0; i < 10; i++)
412                 accelEntries[i+4].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
413         wxAcceleratorTable accelTable (15, accelEntries);
414 #else
415         wxAcceleratorTable accelTable (4, accelEntries);
416 #endif
417         
418         SetAcceleratorTable (accelTable);
419 }
420
421 MainFrame::~MainFrame()
422 {
423   m_bShuttingDown = true; // Currently used so that Log Window will close
424 #if 0
425   // delete all non-modified documents
426         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
427         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
428                 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
429     if (pDoc && ! pDoc->IsModified()) {
430       theApp->getDocManager()->RemoveDocument(pDoc);
431       delete pDoc;
432     }
433         }
434 #endif
435   ::wxYield();
436         if (theApp->getConfig())
437                 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
438   ::wxYield();
439         delete theApp->getDocManager(); 
440
441 }
442
443 void
444 MainFrame::OnSize (wxSizeEvent& event)
445 {
446 #ifdef CTSIM_MDI
447     if (theApp->getLogDoc()) {
448         int xSize, ySize;       
449                 GetClientSize(&xSize, &ySize);
450                 int yLogSize = ySize / 4;
451                   theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
452                   theApp->getLogDoc()->getView()->getFrame()->Show (true);
453     }
454 #endif
455         
456 #if CTSIM_MDI
457         wxDocMDIParentFrame::OnSize (event);
458 #else
459         wxDocParentFrame::OnSize (event);
460 #endif
461 }
462
463 void 
464 MainFrame::OnCreatePhantom(wxCommandEvent& event)
465 {
466         DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
467         int dialogReturn = dialogPhantom.ShowModal();
468         if (dialogReturn == wxID_OK) {
469                 wxString selection (dialogPhantom.getPhantom());
470                 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
471                 wxString filename = selection + ".phm";
472                 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
473                 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
474         }
475         
476 }
477
478 void 
479 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
480 {
481         DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
482         int dialogReturn = dialogFilter.ShowModal();
483         if (dialogReturn == wxID_OK) {
484                 wxString strFilter (dialogFilter.getFilterName());
485                 wxString strDomain (dialogFilter.getDomainName());
486                 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
487                 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
488                 m_iDefaultFilterXSize = dialogFilter.getXSize();
489                 m_iDefaultFilterYSize = dialogFilter.getYSize();
490                 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
491                 m_dDefaultFilterParam= dialogFilter.getFilterParam();
492                 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
493                 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
494                 std::ostringstream os;
495                 os << "Generate Filter=" << strFilter.c_str() 
496                         << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
497                         << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
498                         << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
499                 *theApp->getLog() << os.str().c_str() << "\n";
500                 wxString filename = "untitled.if";
501                 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
502     pFilterDoc->setBadFileOpen();
503                 if (! pFilterDoc) {
504                         sys_error (ERR_SEVERE, "Unable to create filter image");
505                         return;
506                 }
507                 ImageFile& rIF = pFilterDoc->getImageFile();
508                 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
509                 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
510                 rIF.labelAdd (os.str().c_str());
511     pFilterDoc->Modify (true);
512                 pFilterDoc->UpdateAllViews();
513                 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
514                 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
515                 pFilterDoc->getView()->getFrame()->Show(true);
516         }
517 }
518
519 void
520 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
521 {
522         const ImageFile& rIF = pIFDoc->getImageFile();
523         unsigned int nx = rIF.nx();
524         unsigned int ny = rIF.ny();
525         wxList& rListDocs = m_docManager->GetDocuments();
526         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
527                 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
528                 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
529                 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
530                         const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
531                         if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
532                                 vecIF.push_back (pIFCompareDoc);
533                 }
534         }
535 }
536
537
538 void 
539 MainFrame::OnHelpContents (wxCommandEvent& event)
540 {
541         showHelp (event.GetId());
542 }
543
544 void 
545 MainFrame::OnHelpButton (wxCommandEvent& event)
546 {
547         showHelp (event.GetId());
548 }
549
550 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
551 void
552 MainFrame::OnHelpSecondary (wxCommandEvent& event)
553 {
554         m_htmlHelp.DisplayContents();
555 }
556 #endif
557
558 void
559 MainFrame::showHelp (int commandID)
560 {
561         switch (commandID) {
562                 
563         case MAINMENU_HELP_CONTENTS:
564 #ifdef CTSIM_WINHELP
565                 m_winHelp.DisplayContents ();
566 #else
567                 m_htmlHelp.DisplayContents ();
568 #endif
569                 break;
570                 
571         default:
572 #ifdef CTSIM_WINHELP
573     m_winHelp.DisplaySection (commandID);
574 #else
575     m_htmlHelp.DisplaySection (commandID);
576 #endif
577                 break;
578         }
579 }
580
581 void 
582 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
583 {
584         Close(true);
585 }
586
587 void
588 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
589 {
590 #ifndef CTSIM_MDI
591         int iPos = 0;
592         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
593         wxNode* pNode = rListDocs.GetFirst();
594         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
595                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
596                 wxString strFilename = pDoc->GetFilename();
597                 if (iPos < 10) {
598                         strFilename += "\tCtrl-";
599                         strFilename += static_cast<char>('0' + iPos);
600                 }
601                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
602                 m_apWindowMenuData[iPos] = pDoc;
603                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
604                 iPos++;
605                 pNode = pNode->GetNext();
606         }
607         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
608                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
609                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
610                 m_apWindowMenuData[i] = NULL;
611         }
612 #endif  
613 }
614
615 #ifdef CTSIM_CUSTOM_MRU
616 void 
617 MainFrame::OnMRUFile (wxCommandEvent& event)
618 {
619         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
620         if (fileName != "")
621                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
622 }
623 #endif
624
625 void 
626 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
627 {
628         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
629                 wxString strFilename = pDoc->GetFilename();
630                 const wxView* pView = pDoc->GetFirstView();
631                 if (pView) {
632                         wxFrame* pFrame = pView->GetFrame();
633                         pFrame->SetFocus();
634                         pFrame->Raise();
635                 }
636         }
637 }
638
639 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
640 { DoWindowMenu (0, event); }
641
642 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
643 { DoWindowMenu (1, event); }
644
645 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
646 { DoWindowMenu (2, event); }
647
648 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
649 { DoWindowMenu (3, event); }
650
651 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
652 { DoWindowMenu (4, event); }
653
654 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
655 { DoWindowMenu (5, event); }
656
657 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
658 { DoWindowMenu (6, event); }
659
660 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
661 { DoWindowMenu (7, event); }
662
663 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
664 { DoWindowMenu (8, event); }
665
666 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
667 { DoWindowMenu (9, event); }
668
669 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
670 { DoWindowMenu (10, event); }
671
672 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
673 { DoWindowMenu (11, event); }
674
675 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
676 { DoWindowMenu (12, event); }
677
678 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
679 { DoWindowMenu (13, event); }
680
681 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
682 { DoWindowMenu (14, event); }
683
684 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
685 { DoWindowMenu (15, event); }
686
687 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
688 { DoWindowMenu (16, event); }
689
690 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
691 { DoWindowMenu (17, event); }
692
693 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
694 { DoWindowMenu (18, event); }
695
696 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
697 { DoWindowMenu (19, event); }
698
699
700 class BitmapControl : public wxPanel
701 {
702 private:  
703         DECLARE_DYNAMIC_CLASS (BitmapControl)
704                 DECLARE_EVENT_TABLE ()
705                 wxBitmap* m_pBitmap;
706         
707 public:
708         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
709                 const wxPoint& pos = wxDefaultPosition,
710                 const wxSize& size = wxDefaultSize,
711                 long style = wxSTATIC_BORDER,
712                 const wxValidator& validator = wxDefaultValidator,
713                 const wxString& name = "BitmapCtrl");
714         
715         
716         virtual ~BitmapControl();
717         
718         virtual wxSize GetBestSize() const;
719         
720         wxBitmap* getBitmap() 
721         { return m_pBitmap; }
722         
723         void OnPaint(wxPaintEvent& event);
724 };
725
726
727 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
728 EVT_PAINT(BitmapControl::OnPaint)
729 END_EVENT_TABLE()
730
731 IMPLEMENT_CLASS(BitmapControl, wxPanel)
732
733
734 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
735                               long style, const wxValidator& validator, const wxString& name)
736                               : m_pBitmap(pBitmap)
737 {
738         Create(parent, id, pos, size, style, name);
739         
740         SetSize (GetBestSize());
741 }
742
743 wxSize
744 BitmapControl::GetBestSize () const
745 {
746         if (m_pBitmap)
747                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
748         else
749                 return wxSize(0,0);
750 }
751
752 BitmapControl::~BitmapControl()
753 {}
754
755 void
756 BitmapControl::OnPaint (wxPaintEvent& event)
757 {
758         wxPaintDC dc(this);
759         if (m_pBitmap)
760                 dc.DrawBitmap (*m_pBitmap, 0, 0);
761 }
762
763
764 class BitmapDialog : public wxDialog {
765 private:
766         BitmapControl* m_pBitmapCtrl;
767         
768 public:
769         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
770         virtual ~BitmapDialog();
771 };
772
773 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
774 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
775 {
776         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
777         
778         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
779         
780         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
781         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
782         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
783         
784         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
785         
786         SetAutoLayout (true);
787         SetSizer (pTopSizer);
788         pTopSizer->Fit (this);
789         pTopSizer->SetSizeHints (this);
790 }
791
792 BitmapDialog::~BitmapDialog()
793 {}
794
795
796 void 
797 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
798 {
799   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
800     theApp->getAskDeleteNewDocs());
801   if (dlg.ShowModal() == wxID_OK) {
802     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
803     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
804   }
805 }
806
807
808 #include "./splash.xpm"
809 void 
810 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
811 {
812         wxBitmap bmp (splash);
813         if (bmp.Ok()) {
814                 BitmapDialog dlg (&bmp, "About CTSim");
815                 dlg.Show(true);
816         } else {
817                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
818 #ifdef CTSIMVERSION
819                 msg += "Version ";
820                 msg += CTSIMVERSION;
821                 msg += "\n\n";
822 #elif defined(VERSION)
823                 msg << "Version: " <<  VERSION << "\n\n";
824 #endif
825                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
826                 
827                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
828         }
829 }
830
831
832 ProjectionFileDocument*
833 CTSimApp::newProjectionDoc()
834 {
835         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
836         if (newDoc) {
837                 ProjectionFileView* pView = newDoc->getView();
838                 if (pView) {
839                         wxFrame* pFrame = pView->getFrame();
840                         if (pFrame) {
841                                 pFrame->SetSize (0,0);
842                                 pFrame->Show (false);
843                         }
844                 }
845     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
846     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
847     newDoc->OnNewDocument();
848         }
849         
850         return newDoc;
851 }
852
853 ImageFileDocument*
854 CTSimApp::newImageDoc()
855 {
856         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
857         if (newDoc) {
858                 ImageFileView* pView = newDoc->getView();
859                 if (pView) {
860                         wxFrame* pFrame = pView->getFrame();
861                         if (pFrame) {
862                                 pFrame->SetSize (0,0);
863                                 pFrame->Show (false);
864                         }
865                 }
866     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
867     newDoc->SetDocumentTemplate (m_pDocTemplImage);
868     newDoc->OnNewDocument();
869         }
870         
871         return newDoc;
872 }
873
874 PlotFileDocument*
875 CTSimApp::newPlotDoc()
876 {
877         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
878         if (newDoc) {
879                 PlotFileView* pView = newDoc->getView();
880                 if (pView) {
881                         wxFrame* pFrame = pView->getFrame();
882                         if (pFrame) {
883                                 wxSize size;
884                                 m_pFrame->GetClientSize (&size.x, &size.y);
885                                 pFrame->SetSize (size.x / 2, size.y / 2);
886                                 pFrame->Show (false);
887                         }
888                 }
889     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
890     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
891     newDoc->OnNewDocument();
892         }
893         
894         return newDoc;
895 }
896
897
898 TextFileDocument*
899 CTSimApp::newTextDoc()
900 {
901         wxString strFilename (getUntitledFilename());
902         strFilename += ".txt";
903         
904         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
905         if (newDoc) {
906                 TextFileView* pView = newDoc->getView();
907                 if (pView) {
908                         wxFrame* pFrame = pView->getFrame();
909                         if (pFrame) {
910                                 wxSize size;
911                                 m_pFrame->GetClientSize (&size.x, &size.y);;
912                                 pFrame->SetSize (size.x / 2, size.y / 2);
913                                 pFrame->Show (false);
914                         }
915                 }
916     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
917     newDoc->SetDocumentTemplate (m_pDocTemplText);
918     newDoc->OnNewDocument();
919         }
920         
921         return newDoc;
922 }
923
924
925 PhantomFileDocument*
926 CTSimApp::newPhantomDoc()
927 {
928         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
929         if (newDoc) {
930                 PhantomFileView* pView = newDoc->getView();
931                 if (pView) {
932                         wxFrame* pFrame = pView->getFrame();
933                         if (pFrame)
934                                 pFrame->SetSize (0,0);
935                 }
936     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
937     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
938     newDoc->OnNewDocument();
939         }
940         
941         return newDoc;
942 }
943
944 #if wxUSE_GLCANVAS
945
946 Graph3dFileDocument*
947 CTSimApp::newGraph3dDoc()
948 {
949         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
950         if (newDoc) {
951                 Graph3dFileView* pView = newDoc->getView();
952                 if (pView) {
953                         wxFrame* pFrame = pView->getFrame();
954                         if (pFrame)
955                                 pFrame->SetSize (0,0);
956                 }
957     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
958     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
959     newDoc->OnNewDocument();
960         }
961         
962         return newDoc;
963 }
964 #endif