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