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