r565: *** 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.84 2001/02/21 00:34:12 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.84 2001/02/21 00:34:12 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
599         default:
600 #ifdef CTSIM_WINHELP
601     m_winHelp.DisplaySection (commandID);
602 #else
603     m_htmlHelp.Display (commandID);
604 #endif
605                 break;
606         }
607 }
608
609 void 
610 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
611 {
612         Close(true);
613 }
614
615 void
616 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
617 {
618 #ifndef CTSIM_MDI
619         int iPos = 0;
620         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
621         wxNode* pNode = rListDocs.GetFirst();
622         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
623                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
624                 wxString strFilename = pDoc->GetFilename();
625                 if (iPos < 10) {
626                         strFilename += "\tCtrl-";
627                         strFilename += static_cast<char>('0' + iPos);
628                 }
629                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
630                 m_apWindowMenuData[iPos] = pDoc;
631                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
632                 iPos++;
633                 pNode = pNode->GetNext();
634         }
635         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
636                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
637                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
638                 m_apWindowMenuData[i] = NULL;
639         }
640 #endif  
641 }
642
643 #ifdef CTSIM_CUSTOM_MRU
644 void 
645 MainFrame::OnMRUFile (wxCommandEvent& event)
646 {
647         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
648         if (fileName != "")
649                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
650 }
651 #endif
652
653 void 
654 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
655 {
656         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
657                 wxString strFilename = pDoc->GetFilename();
658                 const wxView* pView = pDoc->GetFirstView();
659                 if (pView) {
660                         wxFrame* pFrame = pView->GetFrame();
661                         pFrame->SetFocus();
662                         pFrame->Raise();
663                 }
664         }
665 }
666
667 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
668 { DoWindowMenu (0, event); }
669
670 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
671 { DoWindowMenu (1, event); }
672
673 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
674 { DoWindowMenu (2, event); }
675
676 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
677 { DoWindowMenu (3, event); }
678
679 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
680 { DoWindowMenu (4, event); }
681
682 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
683 { DoWindowMenu (5, event); }
684
685 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
686 { DoWindowMenu (6, event); }
687
688 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
689 { DoWindowMenu (7, event); }
690
691 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
692 { DoWindowMenu (8, event); }
693
694 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
695 { DoWindowMenu (9, event); }
696
697 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
698 { DoWindowMenu (10, event); }
699
700 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
701 { DoWindowMenu (11, event); }
702
703 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
704 { DoWindowMenu (12, event); }
705
706 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
707 { DoWindowMenu (13, event); }
708
709 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
710 { DoWindowMenu (14, event); }
711
712 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
713 { DoWindowMenu (15, event); }
714
715 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
716 { DoWindowMenu (16, event); }
717
718 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
719 { DoWindowMenu (17, event); }
720
721 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
722 { DoWindowMenu (18, event); }
723
724 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
725 { DoWindowMenu (19, event); }
726
727
728 class BitmapControl : public wxPanel
729 {
730 private:  
731         DECLARE_DYNAMIC_CLASS (BitmapControl)
732                 DECLARE_EVENT_TABLE ()
733                 wxBitmap* m_pBitmap;
734         
735 public:
736         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
737                 const wxPoint& pos = wxDefaultPosition,
738                 const wxSize& size = wxDefaultSize,
739                 long style = wxSTATIC_BORDER,
740                 const wxValidator& validator = wxDefaultValidator,
741                 const wxString& name = "BitmapCtrl");
742         
743         
744         virtual ~BitmapControl();
745         
746         virtual wxSize GetBestSize() const;
747         
748         wxBitmap* getBitmap() 
749         { return m_pBitmap; }
750         
751         void OnPaint(wxPaintEvent& event);
752 };
753
754
755 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
756 EVT_PAINT(BitmapControl::OnPaint)
757 END_EVENT_TABLE()
758
759 IMPLEMENT_CLASS(BitmapControl, wxPanel)
760
761
762 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
763                               long style, const wxValidator& validator, const wxString& name)
764                               : m_pBitmap(pBitmap)
765 {
766         Create(parent, id, pos, size, style, name);
767         
768         SetSize (GetBestSize());
769 }
770
771 wxSize
772 BitmapControl::GetBestSize () const
773 {
774         if (m_pBitmap)
775                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
776         else
777                 return wxSize(0,0);
778 }
779
780 BitmapControl::~BitmapControl()
781 {}
782
783 void
784 BitmapControl::OnPaint (wxPaintEvent& event)
785 {
786         wxPaintDC dc(this);
787         if (m_pBitmap)
788                 dc.DrawBitmap (*m_pBitmap, 0, 0);
789 }
790
791
792 class BitmapDialog : public wxDialog {
793 private:
794         BitmapControl* m_pBitmapCtrl;
795         
796 public:
797         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
798         virtual ~BitmapDialog();
799 };
800
801 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
802 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
803 {
804         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
805         
806         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
807         
808         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
809         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
810         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
811         
812         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
813         
814         SetAutoLayout (true);
815         SetSizer (pTopSizer);
816         pTopSizer->Fit (this);
817         pTopSizer->SetSizeHints (this);
818 }
819
820 BitmapDialog::~BitmapDialog()
821 {}
822
823
824 void 
825 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
826 {
827   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
828     theApp->getAskDeleteNewDocs());
829   if (dlg.ShowModal() == wxID_OK) {
830     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
831     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
832   }
833 }
834
835
836 #include "./splash.xpm"
837 void 
838 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
839 {
840         wxBitmap bmp (splash);
841         if (bmp.Ok()) {
842                 BitmapDialog dlg (&bmp, "About CTSim");
843                 dlg.ShowModal();
844         } else {
845                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
846 #ifdef CTSIMVERSION
847                 msg += "Version ";
848                 msg += CTSIMVERSION;
849                 msg += "\n\n";
850 #elif defined(VERSION)
851                 msg << "Version: " <<  VERSION << "\n\n";
852 #endif
853                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
854                 
855                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
856         }
857 }
858
859
860 ProjectionFileDocument*
861 CTSimApp::newProjectionDoc()
862 {
863         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
864         if (newDoc) {
865                 ProjectionFileView* pView = newDoc->getView();
866                 if (pView) {
867                         wxFrame* pFrame = pView->getFrame();
868                         if (pFrame) {
869                                 pFrame->SetSize (0,0);
870                                 pFrame->Show (false);
871                         }
872                 }
873     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
874     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
875     newDoc->OnNewDocument();
876         }
877         
878         return newDoc;
879 }
880
881 ImageFileDocument*
882 CTSimApp::newImageDoc()
883 {
884         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
885         if (newDoc) {
886                 ImageFileView* pView = newDoc->getView();
887                 if (pView) {
888                         wxFrame* pFrame = pView->getFrame();
889                         if (pFrame) {
890                                 pFrame->SetSize (0,0);
891                                 pFrame->Show (false);
892                         }
893                 }
894     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
895     newDoc->SetDocumentTemplate (m_pDocTemplImage);
896     newDoc->OnNewDocument();
897         }
898         
899         return newDoc;
900 }
901
902 PlotFileDocument*
903 CTSimApp::newPlotDoc()
904 {
905         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
906         if (newDoc) {
907                 PlotFileView* pView = newDoc->getView();
908                 if (pView) {
909                         wxFrame* pFrame = pView->getFrame();
910                         if (pFrame) {
911                                 wxSize size;
912                                 m_pFrame->GetClientSize (&size.x, &size.y);
913                                 pFrame->SetSize (size.x / 2, size.y / 2);
914                                 pFrame->Show (false);
915                         }
916                 }
917     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
918     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
919     newDoc->OnNewDocument();
920         }
921         
922         return newDoc;
923 }
924
925
926 TextFileDocument*
927 CTSimApp::newTextDoc()
928 {
929         wxString strFilename (getUntitledFilename());
930         strFilename += ".txt";
931         
932         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
933         if (newDoc) {
934                 TextFileView* pView = newDoc->getView();
935                 if (pView) {
936                         wxFrame* pFrame = pView->getFrame();
937                         if (pFrame) {
938                                 wxSize size;
939                                 m_pFrame->GetClientSize (&size.x, &size.y);;
940                                 pFrame->SetSize (size.x / 2, size.y / 2);
941                                 pFrame->Show (false);
942                         }
943                 }
944     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
945     newDoc->SetDocumentTemplate (m_pDocTemplText);
946     newDoc->OnNewDocument();
947         }
948         
949         return newDoc;
950 }
951
952
953 PhantomFileDocument*
954 CTSimApp::newPhantomDoc()
955 {
956         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
957         if (newDoc) {
958                 PhantomFileView* pView = newDoc->getView();
959                 if (pView) {
960                         wxFrame* pFrame = pView->getFrame();
961                         if (pFrame)
962                                 pFrame->SetSize (0,0);
963                 }
964     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
965     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
966     newDoc->OnNewDocument();
967         }
968         
969         return newDoc;
970 }
971
972 #if wxUSE_GLCANVAS
973
974 Graph3dFileDocument*
975 CTSimApp::newGraph3dDoc()
976 {
977         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
978         if (newDoc) {
979                 Graph3dFileView* pView = newDoc->getView();
980                 if (pView) {
981                         wxFrame* pFrame = pView->getFrame();
982                         if (pFrame)
983                                 pFrame->SetSize (0,0);
984                 }
985     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
986     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
987     newDoc->OnNewDocument();
988         }
989         
990         return newDoc;
991 }
992 #endif