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