r550: no 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.80 2001/02/19 20:23:17 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.80 2001/02/19 20:23:17 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_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
295 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
296 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
297 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
298 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
299 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
300 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
301 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
302 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
303 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
304 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
305 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
306
307 EVT_SIZE(MainFrame::OnSize)
308
309 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
310 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
311 #endif
312 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
313 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
314 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
315 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
316 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
317 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
318 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
319 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
320 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
321 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
322 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
323 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
324 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
325 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
326 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
327 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
328 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
329 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
330 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
331 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
332 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
333 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
334 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
335 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
336 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
337 END_EVENT_TABLE()
338
339
340
341 #if CTSIM_MDI
342 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
343 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
344 #else
345 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
346 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
347 #endif
348 {
349   m_bShuttingDown = false;
350
351         //// Make a menubar
352         wxMenu *file_menu = new wxMenu;
353         
354         file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
355         file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
356         file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
357         
358         file_menu->AppendSeparator();
359   file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
360         file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
361         
362         //  history of files visited
363         theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
364         theApp->getDocManager()->FileHistoryUseMenu(file_menu);
365         
366 #ifndef CTSIM_MDI
367         m_pWindowMenu = new wxMenu;
368         m_pWindowMenu->UpdateUI (this);
369 #endif
370         
371         wxMenu* help_menu = new wxMenu;
372         help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
373   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
374 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
375         help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
376 #endif
377         help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
378         
379         wxMenuBar* menu_bar = new wxMenuBar;
380         
381         menu_bar->Append(file_menu, "&File");
382 #ifndef CTSIM_MDI
383         menu_bar->Append(m_pWindowMenu, "&Window");
384 #endif
385         menu_bar->Append(help_menu, "&Help");
386         
387         SetMenuBar(menu_bar);
388         
389 #ifndef CTSIM_MDI
390         int i;
391         for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
392                 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
393                 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
394                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
395         }
396 #endif
397         
398         m_iDefaultPhantomID = Phantom::PHM_HERMAN;
399         m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
400         m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
401         m_iDefaultFilterXSize = 256;
402         m_iDefaultFilterYSize = 256;
403         m_dDefaultFilterParam = 1.;
404         m_dDefaultFilterBandwidth = 1.;
405         m_dDefaultFilterInputScale = 1.;
406         m_dDefaultFilterOutputScale = 1.;
407         
408         wxAcceleratorEntry accelEntries[15];
409         accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
410         accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
411         accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
412 #ifndef CTSIM_MDI
413         accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
414         for (i = 0; i < 10; i++)
415                 accelEntries[i+4].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
416         wxAcceleratorTable accelTable (15, accelEntries);
417 #else
418         wxAcceleratorTable accelTable (4, accelEntries);
419 #endif
420         
421         SetAcceleratorTable (accelTable);
422 }
423
424 MainFrame::~MainFrame()
425 {
426   m_bShuttingDown = true; // Currently used so that Log Window will close
427 #if 0
428   // delete all non-modified documents
429         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
430         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
431                 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
432     if (pDoc && ! pDoc->IsModified()) {
433       theApp->getDocManager()->RemoveDocument(pDoc);
434       delete pDoc;
435     }
436         }
437 #endif
438   ::wxYield();
439         if (theApp->getConfig())
440                 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
441   ::wxYield();
442         delete theApp->getDocManager(); 
443
444 }
445
446 void
447 MainFrame::OnSize (wxSizeEvent& event)
448 {
449 #ifdef CTSIM_MDI
450     if (theApp->getLogDoc()) {
451         int xSize, ySize;       
452                 GetClientSize(&xSize, &ySize);
453                 int yLogSize = ySize / 4;
454                   theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
455                   theApp->getLogDoc()->getView()->getFrame()->Show (true);
456     }
457 #endif
458         
459 #if CTSIM_MDI
460         wxDocMDIParentFrame::OnSize (event);
461 #else
462         wxDocParentFrame::OnSize (event);
463 #endif
464 }
465
466 void 
467 MainFrame::OnCreatePhantom(wxCommandEvent& event)
468 {
469         DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
470         int dialogReturn = dialogPhantom.ShowModal();
471         if (dialogReturn == wxID_OK) {
472                 wxString selection (dialogPhantom.getPhantom());
473                 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
474                 wxString filename = selection + ".phm";
475                 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
476                 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
477         }
478         
479 }
480
481 void 
482 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
483 {
484         DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
485         int dialogReturn = dialogFilter.ShowModal();
486         if (dialogReturn == wxID_OK) {
487                 wxString strFilter (dialogFilter.getFilterName());
488                 wxString strDomain (dialogFilter.getDomainName());
489                 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
490                 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
491                 m_iDefaultFilterXSize = dialogFilter.getXSize();
492                 m_iDefaultFilterYSize = dialogFilter.getYSize();
493                 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
494                 m_dDefaultFilterParam= dialogFilter.getFilterParam();
495                 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
496                 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
497                 std::ostringstream os;
498                 os << "Generate Filter=" << strFilter.c_str() 
499                         << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
500                         << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
501                         << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
502                 *theApp->getLog() << os.str().c_str() << "\n";
503                 wxString filename = "untitled.if";
504                 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
505     pFilterDoc->setBadFileOpen();
506                 if (! pFilterDoc) {
507                         sys_error (ERR_SEVERE, "Unable to create filter image");
508                         return;
509                 }
510                 ImageFile& rIF = pFilterDoc->getImageFile();
511                 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
512                 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
513                 rIF.labelAdd (os.str().c_str());
514     pFilterDoc->Modify (true);
515                 pFilterDoc->UpdateAllViews();
516                 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
517                 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
518                 pFilterDoc->getView()->getFrame()->Show(true);
519         }
520 }
521
522 void
523 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
524 {
525         const ImageFile& rIF = pIFDoc->getImageFile();
526         unsigned int nx = rIF.nx();
527         unsigned int ny = rIF.ny();
528         wxList& rListDocs = m_docManager->GetDocuments();
529         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
530                 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
531                 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
532                 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
533                         const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
534                         if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
535                                 vecIF.push_back (pIFCompareDoc);
536                 }
537         }
538 }
539
540
541 void 
542 MainFrame::OnHelpContents (wxCommandEvent& event)
543 {
544         showHelp (event.GetId());
545 }
546
547 void 
548 MainFrame::OnHelpButton (wxCommandEvent& event)
549 {
550         showHelp (event.GetId());
551 }
552
553 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
554 void
555 MainFrame::OnHelpSecondary (wxCommandEvent& event)
556 {
557         m_htmlHelp.DisplayContents();
558 }
559 #endif
560
561 void
562 MainFrame::showHelp (int commandID)
563 {
564         switch (commandID) {
565                 
566         case MAINMENU_HELP_CONTENTS:
567 #ifdef CTSIM_WINHELP
568                 m_winHelp.DisplayContents ();
569 #else
570                 m_htmlHelp.DisplayContents ();
571 #endif
572                 break;
573                 
574         default:
575 #ifdef CTSIM_WINHELP
576     m_winHelp.DisplaySection (commandID);
577 #else
578     m_htmlHelp.DisplaySection (commandID);
579 #endif
580                 break;
581         }
582 }
583
584 void 
585 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
586 {
587         Close(true);
588 }
589
590 void
591 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
592 {
593 #ifndef CTSIM_MDI
594         int iPos = 0;
595         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
596         wxNode* pNode = rListDocs.GetFirst();
597         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
598                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
599                 wxString strFilename = pDoc->GetFilename();
600                 if (iPos < 10) {
601                         strFilename += "\tCtrl-";
602                         strFilename += static_cast<char>('0' + iPos);
603                 }
604                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
605                 m_apWindowMenuData[iPos] = pDoc;
606                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
607                 iPos++;
608                 pNode = pNode->GetNext();
609         }
610         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
611                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
612                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
613                 m_apWindowMenuData[i] = NULL;
614         }
615 #endif  
616 }
617
618 #ifdef CTSIM_CUSTOM_MRU
619 void 
620 MainFrame::OnMRUFile (wxCommandEvent& event)
621 {
622         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
623         if (fileName != "")
624                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
625 }
626 #endif
627
628 void 
629 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
630 {
631         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
632                 wxString strFilename = pDoc->GetFilename();
633                 const wxView* pView = pDoc->GetFirstView();
634                 if (pView) {
635                         wxFrame* pFrame = pView->GetFrame();
636                         pFrame->SetFocus();
637                         pFrame->Raise();
638                 }
639         }
640 }
641
642 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
643 { DoWindowMenu (0, event); }
644
645 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
646 { DoWindowMenu (1, event); }
647
648 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
649 { DoWindowMenu (2, event); }
650
651 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
652 { DoWindowMenu (3, event); }
653
654 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
655 { DoWindowMenu (4, event); }
656
657 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
658 { DoWindowMenu (5, event); }
659
660 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
661 { DoWindowMenu (6, event); }
662
663 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
664 { DoWindowMenu (7, event); }
665
666 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
667 { DoWindowMenu (8, event); }
668
669 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
670 { DoWindowMenu (9, event); }
671
672 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
673 { DoWindowMenu (10, event); }
674
675 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
676 { DoWindowMenu (11, event); }
677
678 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
679 { DoWindowMenu (12, event); }
680
681 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
682 { DoWindowMenu (13, event); }
683
684 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
685 { DoWindowMenu (14, event); }
686
687 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
688 { DoWindowMenu (15, event); }
689
690 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
691 { DoWindowMenu (16, event); }
692
693 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
694 { DoWindowMenu (17, event); }
695
696 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
697 { DoWindowMenu (18, event); }
698
699 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
700 { DoWindowMenu (19, event); }
701
702
703 class BitmapControl : public wxPanel
704 {
705 private:  
706         DECLARE_DYNAMIC_CLASS (BitmapControl)
707                 DECLARE_EVENT_TABLE ()
708                 wxBitmap* m_pBitmap;
709         
710 public:
711         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
712                 const wxPoint& pos = wxDefaultPosition,
713                 const wxSize& size = wxDefaultSize,
714                 long style = wxSTATIC_BORDER,
715                 const wxValidator& validator = wxDefaultValidator,
716                 const wxString& name = "BitmapCtrl");
717         
718         
719         virtual ~BitmapControl();
720         
721         virtual wxSize GetBestSize() const;
722         
723         wxBitmap* getBitmap() 
724         { return m_pBitmap; }
725         
726         void OnPaint(wxPaintEvent& event);
727 };
728
729
730 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
731 EVT_PAINT(BitmapControl::OnPaint)
732 END_EVENT_TABLE()
733
734 IMPLEMENT_CLASS(BitmapControl, wxPanel)
735
736
737 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
738                               long style, const wxValidator& validator, const wxString& name)
739                               : m_pBitmap(pBitmap)
740 {
741         Create(parent, id, pos, size, style, name);
742         
743         SetSize (GetBestSize());
744 }
745
746 wxSize
747 BitmapControl::GetBestSize () const
748 {
749         if (m_pBitmap)
750                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
751         else
752                 return wxSize(0,0);
753 }
754
755 BitmapControl::~BitmapControl()
756 {}
757
758 void
759 BitmapControl::OnPaint (wxPaintEvent& event)
760 {
761         wxPaintDC dc(this);
762         if (m_pBitmap)
763                 dc.DrawBitmap (*m_pBitmap, 0, 0);
764 }
765
766
767 class BitmapDialog : public wxDialog {
768 private:
769         BitmapControl* m_pBitmapCtrl;
770         
771 public:
772         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
773         virtual ~BitmapDialog();
774 };
775
776 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
777 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
778 {
779         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
780         
781         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
782         
783         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
784         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
785         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
786         
787         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
788         
789         SetAutoLayout (true);
790         SetSizer (pTopSizer);
791         pTopSizer->Fit (this);
792         pTopSizer->SetSizeHints (this);
793 }
794
795 BitmapDialog::~BitmapDialog()
796 {}
797
798
799 void 
800 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
801 {
802   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
803     theApp->getAskDeleteNewDocs());
804   if (dlg.ShowModal() == wxID_OK) {
805     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
806     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
807   }
808 }
809
810
811 #include "./splash.xpm"
812 void 
813 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
814 {
815         wxBitmap bmp (splash);
816         if (bmp.Ok()) {
817                 BitmapDialog dlg (&bmp, "About CTSim");
818                 dlg.Show(true);
819         } else {
820                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
821 #ifdef CTSIMVERSION
822                 msg += "Version ";
823                 msg += CTSIMVERSION;
824                 msg += "\n\n";
825 #elif defined(VERSION)
826                 msg << "Version: " <<  VERSION << "\n\n";
827 #endif
828                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
829                 
830                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
831         }
832 }
833
834
835 ProjectionFileDocument*
836 CTSimApp::newProjectionDoc()
837 {
838         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
839         if (newDoc) {
840                 ProjectionFileView* pView = newDoc->getView();
841                 if (pView) {
842                         wxFrame* pFrame = pView->getFrame();
843                         if (pFrame) {
844                                 pFrame->SetSize (0,0);
845                                 pFrame->Show (false);
846                         }
847                 }
848     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
849     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
850     newDoc->OnNewDocument();
851         }
852         
853         return newDoc;
854 }
855
856 ImageFileDocument*
857 CTSimApp::newImageDoc()
858 {
859         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
860         if (newDoc) {
861                 ImageFileView* pView = newDoc->getView();
862                 if (pView) {
863                         wxFrame* pFrame = pView->getFrame();
864                         if (pFrame) {
865                                 pFrame->SetSize (0,0);
866                                 pFrame->Show (false);
867                         }
868                 }
869     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
870     newDoc->SetDocumentTemplate (m_pDocTemplImage);
871     newDoc->OnNewDocument();
872         }
873         
874         return newDoc;
875 }
876
877 PlotFileDocument*
878 CTSimApp::newPlotDoc()
879 {
880         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
881         if (newDoc) {
882                 PlotFileView* pView = newDoc->getView();
883                 if (pView) {
884                         wxFrame* pFrame = pView->getFrame();
885                         if (pFrame) {
886                                 wxSize size;
887                                 m_pFrame->GetClientSize (&size.x, &size.y);
888                                 pFrame->SetSize (size.x / 2, size.y / 2);
889                                 pFrame->Show (false);
890                         }
891                 }
892     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
893     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
894     newDoc->OnNewDocument();
895         }
896         
897         return newDoc;
898 }
899
900
901 TextFileDocument*
902 CTSimApp::newTextDoc()
903 {
904         wxString strFilename (getUntitledFilename());
905         strFilename += ".txt";
906         
907         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
908         if (newDoc) {
909                 TextFileView* pView = newDoc->getView();
910                 if (pView) {
911                         wxFrame* pFrame = pView->getFrame();
912                         if (pFrame) {
913                                 wxSize size;
914                                 m_pFrame->GetClientSize (&size.x, &size.y);;
915                                 pFrame->SetSize (size.x / 2, size.y / 2);
916                                 pFrame->Show (false);
917                         }
918                 }
919     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
920     newDoc->SetDocumentTemplate (m_pDocTemplText);
921     newDoc->OnNewDocument();
922         }
923         
924         return newDoc;
925 }
926
927
928 PhantomFileDocument*
929 CTSimApp::newPhantomDoc()
930 {
931         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
932         if (newDoc) {
933                 PhantomFileView* pView = newDoc->getView();
934                 if (pView) {
935                         wxFrame* pFrame = pView->getFrame();
936                         if (pFrame)
937                                 pFrame->SetSize (0,0);
938                 }
939     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
940     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
941     newDoc->OnNewDocument();
942         }
943         
944         return newDoc;
945 }
946
947 #if wxUSE_GLCANVAS
948
949 Graph3dFileDocument*
950 CTSimApp::newGraph3dDoc()
951 {
952         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
953         if (newDoc) {
954                 Graph3dFileView* pView = newDoc->getView();
955                 if (pView) {
956                         wxFrame* pFrame = pView->getFrame();
957                         if (pFrame)
958                                 pFrame->SetSize (0,0);
959                 }
960     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
961     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
962     newDoc->OnNewDocument();
963         }
964         
965         return newDoc;
966 }
967 #endif