1 /*****************************************************************************
5 ** Purpose: Top-level routines of CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
27 #define strdup _strdup
30 #include "wx/wxprec.h"
37 #include "wx/filesys.h"
38 #include "wx/fs_zip.h"
40 #include "wx/msw/helpchm.h"
43 #if !wxUSE_DOC_VIEW_ARCHITECTURE
44 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
54 #include "backgroundmgr.h"
56 #if defined(HAVE_CONFIG_H)
60 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
70 static const char* rcsindent = "$Id$";
72 struct option CTSimApp::ctsimOptions[] =
74 {"help", 0, 0, O_HELP},
75 {"version", 0, 0, O_VERSION},
76 {"print", 0, 0, O_PRINT},
80 IMPLEMENT_APP(CTSimApp)
83 : m_bAdvancedOptions(false), m_bSetModifyNewDocs(true),
84 m_bVerboseLogging(false), m_bShowStartupTips(true),
85 m_iCurrentTip(0), m_bUseBackgroundTasks(false),
86 m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0)
91 #ifdef HAVE_SYS_TIME_H
95 #ifdef HAVE_SYS_RESOURCE_H
96 #include <sys/resource.h>
104 #ifdef HAVE_SETPRIORITY
105 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
110 g_bRunningWXWindows = true;
111 bool bPrintFiles = false;
117 char** cargv = new char* [argc];
118 for (int i = 0; i < argc; i++) {
119 const char* p = wxConvUTF8.cWX2MB(argv[i]);
120 cargv[i] = new char [strlen(p)+1];
123 int c = getopt_long (argc, cargv, "", ctsimOptions, NULL);
124 for (int i = 0; i < argc; i++) {
135 std::cout << rcsindent << std::endl;
137 std::cout << "Version: " << VERSION << std::endl;
138 #elif defined(CTSIMVERSION)
139 std::cout << "Version: " << CTSIMVERSION << std::endl;
144 usage (wxConvCurrent->cWX2MB(argv[0]));
150 usage (wxConvCurrent->cWX2MB(argv[0]));
155 m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
157 m_pDocTemplImage = new wxDocTemplate (m_docManager, _T("ImageFile"), _T("*.if"), _T(""), _T("if"), _T("ImageFile"), _T("ImageView"), CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
158 m_pDocTemplProjection = new wxDocTemplate (m_docManager, _T("ProjectionFile"), _T("*.pj"), _T(""), _T("pj"), _T("ProjectionFile"), _T("ProjectionView"), CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
159 m_pDocTemplPhantom = new wxDocTemplate (m_docManager, _T("PhantomFile"), _T("*.phm"), _T(""), _T("phm"), _T("PhantomFile"), _T("PhantomView"), CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
160 m_pDocTemplPlot = new wxDocTemplate (m_docManager, _T("PlotFile"), _T("*.plt"), _T(""), _T("plt"), _T("PlotFile"), _T("PlotView"), CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
161 m_pDocTemplText = new wxDocTemplate (m_docManager, _T("TextFile"), _T("*.txt"), _T(""), _T("txt"), _T("TextFile"), _T("TextView"), CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
163 m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, _T("Graph3dFile"), _T("*.g3d"), _T(""), _T("g3d"),
164 _T("Graph3dFile"), _T("Graph3dView"), CLASSINFO(Graph3dFileDocument),
165 CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
169 wxImage::AddHandler(new wxGIFHandler); // Required for images in the online documentation
172 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
173 wxFileSystem::AddHandler(new wxZipFSHandler); // Required for advanced HTML help
176 // Create the main frame window
177 int xDisplay, yDisplay;
178 ::wxDisplaySize (&xDisplay, &yDisplay);
181 wxSize frameSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .75));
183 wxSize frameSize(nearest<int>(xDisplay * .6), nearest<int>(yDisplay * .4));
186 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, _T("CTSim"), wxPoint(0, 0), frameSize, wxDEFAULT_FRAME_STYLE);
188 setIconForFrame (m_pFrame);
189 m_pFrame->Centre(wxBOTH);
190 m_pFrame->Show(true);
191 SetTopWindow (m_pFrame);
194 m_docManager->FileHistoryLoad(*m_pConfig);
197 m_pLogDoc = newTextDoc();
199 m_pLog = m_pLogDoc->getTextCtrl();
200 m_pLogDoc->SetDocumentName("Log.txt");
201 m_pLogDoc->SetFilename("Log.txt");
202 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
204 m_pFrame->GetClientSize(&xSize, &ySize);
205 int yLogSize = ySize / 4;
206 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
207 m_pLogDoc->getView()->getFrame()->Show (true);
210 m_pLog = new wxTextCtrl (m_pFrame, -1, _T("Log Window\n"), wxPoint(0, 0), frameSize, wxTE_MULTILINE | wxTE_READONLY);
212 wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
215 if (! m_pConfig->Read(_T("HelpDir"), &helpDir))
216 helpDir = ::wxGetCwd();
218 if (! m_pFrame->getWinHelpController().Initialize(helpDir + _T("/ctsim")))
219 *m_pLog << _T("Cannot initialize the Windows Help system") << _T("\n");
222 wxString docDir (DATADIR, *wxConvCurrent);
224 wxString docDir (::wxGetCwd());
226 wxString docFile = docDir + _T("ctsim.htb");
227 if (! m_pFrame->getHtmlHelpController().AddBook(docFile) &&
228 ! m_pFrame->getHtmlHelpController().AddBook(_T("/usr/share/ctsim/ctsim.htb")) &&
229 ! m_pFrame->getHtmlHelpController().AddBook(_T("/tmp/ctsim.htb")))
230 *m_pLog << _T("Cannot initialize the HTML Help system") << _T("\n");
232 if (::wxDirExists (_T("/tmp")))
233 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
234 m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
238 for (int i = optind + 1; i <= argc; i++) {
239 wxString filename = argv [i - 1];
240 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
242 wxView* pNewView = pNewDoc->GetFirstView();
243 wxPrintout *printout = pNewView->OnCreatePrintout();
246 printer.Print(pNewView->GetFrame(), printout, TRUE);
249 wxCommandEvent nullEvent;
250 nullEvent.SetId (wxID_CLOSE);
251 m_docManager->OnFileClose (nullEvent);
255 wxCommandEvent closeEvent;
256 closeEvent.SetInt (MAINMENU_FILE_EXIT);
257 m_pFrame->AddPendingEvent(closeEvent);
260 if (getStartupTips())
263 #ifdef HAVE_WXTHREADS
264 m_pBackgroundMgr = new BackgroundManager;
273 CTSimTipProvider tipProvider (m_iCurrentTip);
274 setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
275 m_iCurrentTip = tipProvider.GetCurrentTip();
279 #include "./ctsim.xpm"
281 CTSimApp::setIconForFrame(wxFrame* pFrame)
283 wxIcon iconApp (ctsim16_xpm);
286 pFrame->SetIcon (iconApp);
290 CTSimApp::usage(const char* program)
292 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
293 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
295 std::cout << " --version Display version\n";
296 std::cout << " --help Display this help message\n";
311 CTSimApp::openConfig()
314 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(""), _T(""), wxCONFIG_USE_LOCAL_FILE);
316 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(".ctsim"), _T(""), wxCONFIG_USE_LOCAL_FILE);
319 wxConfigBase::Set(m_pConfig);
320 m_pConfig->Read (_T("AdvancedOptions"), &m_bAdvancedOptions);
321 m_pConfig->Read (_T("SetModifyNewDocs"), &m_bSetModifyNewDocs);
322 m_pConfig->Read (_T("VerboseLogging"), &m_bVerboseLogging);
323 m_pConfig->Read (_T("StartupTips"), &m_bShowStartupTips);
324 m_pConfig->Read (_T("CurrentTip"), &m_iCurrentTip);
325 m_pConfig->Read (_T("UseBackgroundTasks"), &m_bUseBackgroundTasks);
327 wxString strFftwWisdom;
328 m_pConfig->Read (_T("FftwWisdom"), strFftwWisdom);
329 if (strFftwWisdom.size() > 0)
330 fftw_import_wisdom_from_string (strFftwWisdom.mb_str(wxConvUTF8));
335 CTSimApp::closeConfig()
337 m_pConfig->Write (_T("AdvancedOptions"), m_bAdvancedOptions);
338 m_pConfig->Write (_T("SetModifyNewDocs"), m_bSetModifyNewDocs);
339 m_pConfig->Write (_T("VerboseLogging"), m_bVerboseLogging);
340 m_pConfig->Write (_T("StartupTips"), m_bShowStartupTips);
341 m_pConfig->Write (_T("CurrentTip"), m_iCurrentTip);
342 m_pConfig->Write (_T("UseBackgroundTasks"), m_bUseBackgroundTasks);
344 const char* const pszWisdom = fftw_export_wisdom_to_string();
345 wxString strFftwWisdom (pszWisdom, *wxConvCurrent);
346 fftw_free ((void*) pszWisdom);
347 m_pConfig->Write (_T("FftwWisdom"), strFftwWisdom);
355 CTSimApp::getUntitledFilename()
357 static int untitledNumber = 1;
359 wxString filename (_T("Untitled"));
360 filename << untitledNumber++;
366 // Top-level window for CTSim
369 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
371 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
373 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
375 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
378 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
379 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
380 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
381 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
382 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
383 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
384 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
385 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
386 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
387 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
388 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
389 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
390 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
391 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
392 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
393 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
394 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
395 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
396 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
397 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
399 EVT_SIZE(MainFrame::OnSize)
401 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
402 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
404 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
405 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
406 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
407 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
408 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
409 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
410 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
411 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
412 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
413 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
414 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
415 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
416 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
417 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
418 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
419 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
420 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
421 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
422 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
423 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
424 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
425 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
426 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
427 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
428 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
434 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
435 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
437 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
438 : wxDocParentFrame(manager, frame, id, title, pos, size, type, _T("MainFrame"))
441 m_bShuttingDown = false;
444 wxMenu *file_menu = new wxMenu;
446 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, _T("Cr&eate Phantom...\tCtrl-P"));
447 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, _T("Create &Filter...\tCtrl-F"));
448 file_menu->Append(wxID_OPEN, _T("&Open...\tCtrl-O"));
450 file_menu->AppendSeparator();
451 file_menu->Append (MAINMENU_IMPORT, _T("&Import...\tCtrl-M"));
452 file_menu->Append (MAINMENU_FILE_PREFERENCES, _T("Prefere&nces..."));
453 file_menu->Append(MAINMENU_FILE_EXIT, _T("E&xit"));
455 // history of files visited
456 theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
457 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
460 m_pWindowMenu = new wxMenu;
461 m_pWindowMenu->UpdateUI (this);
464 wxMenu* help_menu = new wxMenu;
465 help_menu->Append (MAINMENU_HELP_CONTENTS, _T("&Contents\tF1"));
466 help_menu->Append (MAINMENU_HELP_TIPS, _T("&Tips"));
467 help_menu->Append (IDH_QUICKSTART, _T("&Quick Start"));
468 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
469 help_menu->Append (MAINMENU_HELP_SECONDARY, _T("&Secondary Help"));
471 help_menu->Append (MAINMENU_HELP_ABOUT, _T("&About"));
473 wxMenuBar* menu_bar = new wxMenuBar;
475 menu_bar->Append(file_menu, _T("&File"));
477 menu_bar->Append(m_pWindowMenu, _T("&Window"));
479 menu_bar->Append(help_menu, _T("&Help"));
481 SetMenuBar(menu_bar);
486 for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
487 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, _T("[EMPTY]"));
488 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
489 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
493 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
494 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
495 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
496 m_iDefaultFilterXSize = 256;
497 m_iDefaultFilterYSize = 256;
498 m_dDefaultFilterParam = 1.;
499 m_dDefaultFilterBandwidth = 1.;
500 m_dDefaultFilterInputScale = 1.;
501 m_dDefaultFilterOutputScale = 1.;
502 m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PNG;
504 wxAcceleratorEntry accelEntries[15];
505 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
506 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
507 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
508 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
509 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
511 for (i = 0; i < 10; i++)
512 accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
513 wxAcceleratorTable accelTable (15, accelEntries);
515 wxAcceleratorTable accelTable (5, accelEntries);
518 SetAcceleratorTable (accelTable);
521 MainFrame::~MainFrame()
523 m_bShuttingDown = true; // Currently used so that Log Window will close
525 // delete all non-modified documents
526 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
527 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
528 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
529 if (pDoc && ! pDoc->IsModified()) {
530 theApp->getDocManager()->RemoveDocument(pDoc);
536 if (theApp->getConfig())
537 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
539 delete theApp->getDocManager();
544 MainFrame::OnSize (wxSizeEvent& event)
547 if (theApp->getLogDoc()) {
549 GetClientSize(&xSize, &ySize);
550 int yLogSize = ySize / 4;
551 theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
552 theApp->getLogDoc()->getView()->getFrame()->Show (true);
557 wxDocMDIParentFrame::OnSize (event);
559 wxDocParentFrame::OnSize (event);
564 MainFrame::OnCreatePhantom(wxCommandEvent& event)
566 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
567 int dialogReturn = dialogPhantom.ShowModal();
568 if (dialogReturn == wxID_OK) {
569 wxString selection (dialogPhantom.getPhantom(), *wxConvCurrent);
570 if (theApp->getVerboseLogging())
571 *theApp->getLog() << _T("Selected phantom ") << selection.c_str() << _T("\n");
572 wxString filename = selection + _T(".phm");
573 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.mb_str(wxConvUTF8));
574 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
580 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
582 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
583 int dialogReturn = dialogFilter.ShowModal();
584 if (dialogReturn == wxID_OK) {
585 wxString strFilter (dialogFilter.getFilterName(), *wxConvCurrent);
586 wxString strDomain (dialogFilter.getDomainName(), *wxConvCurrent);
587 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.mb_str(wxConvUTF8));
588 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.mb_str(wxConvUTF8));
589 m_iDefaultFilterXSize = dialogFilter.getXSize();
590 m_iDefaultFilterYSize = dialogFilter.getYSize();
591 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
592 m_dDefaultFilterParam= dialogFilter.getFilterParam();
593 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
594 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
596 os << _T("Generate Filter=") << strFilter
597 << _T(", size=(") << static_cast<int>(m_iDefaultFilterXSize) << _T(",")
598 << static_cast<int>(m_iDefaultFilterYSize)
599 << _T("), domain=") << strDomain.c_str() << _T(", filterParam=")
600 << m_dDefaultFilterParam << _T(", bandwidth=") <<
601 m_dDefaultFilterBandwidth
602 << _T(", inputScale=") << m_dDefaultFilterInputScale << _T(", outputScale=") << m_dDefaultFilterOutputScale;
603 *theApp->getLog() << os << _T("\n");
604 wxString filename = _T("untitled.if");
605 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
606 pFilterDoc->setBadFileOpen();
608 sys_error (ERR_SEVERE, "Unable to create filter image");
611 ImageFile& rIF = pFilterDoc->getImageFile();
612 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
613 rIF.filterResponse (strDomain.mb_str(wxConvUTF8), m_dDefaultFilterBandwidth, strFilter.mb_str(wxConvUTF8), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
614 rIF.labelAdd (os.mb_str(wxConvUTF8));
615 if (theApp->getAskDeleteNewDocs())
616 pFilterDoc->Modify (true);
617 pFilterDoc->UpdateAllViews();
618 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
619 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
620 pFilterDoc->getView()->getFrame()->Show(true);
625 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
627 const ImageFile& rIF = pIFDoc->getImageFile();
628 unsigned int nx = rIF.nx();
629 unsigned int ny = rIF.ny();
630 wxList& rListDocs = m_docManager->GetDocuments();
631 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
632 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
633 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
634 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
635 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
636 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
637 vecIF.push_back (pIFCompareDoc);
644 MainFrame::OnNewImageFile (wxCommandEvent& event)
646 ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
648 ImageFileDocument* pImageDoc = theApp->newImageDoc();
650 sys_error (ERR_SEVERE, "Unable to create image file");
653 pImageDoc->setImageFile (pImageFile);
654 if (theApp->getAskDeleteNewDocs())
655 pImageDoc->Modify (true);
659 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
661 Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
662 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
664 sys_error (ERR_SEVERE, "Unable to create projection file");
667 pProjDoc->setProjections (pProjections);
668 if (theApp->getAskDeleteNewDocs())
669 pProjDoc->Modify (true);
673 MainFrame::OnLogEvent (wxCommandEvent& event)
675 *theApp->getLog() << event.GetString();
679 MainFrame::OnHelpTips (wxCommandEvent& event)
685 MainFrame::OnHelpContents (wxCommandEvent& event)
687 showHelp (event.GetId());
691 MainFrame::OnHelpButton (wxCommandEvent& event)
693 showHelp (event.GetId());
696 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
698 MainFrame::OnHelpSecondary (wxCommandEvent& event)
700 m_htmlHelp.Display ("Contents");
705 MainFrame::showHelp (int commandID)
709 case MAINMENU_HELP_CONTENTS:
711 m_winHelp.DisplayContents ();
713 m_htmlHelp.Display (_T("Contents"));
719 m_winHelp.DisplaySection (commandID);
721 m_htmlHelp.Display (commandID);
728 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
734 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
738 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
739 wxNode* pNode = rListDocs.GetFirst();
740 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
741 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
742 wxString strFilename = pDoc->GetFilename();
744 strFilename += _T("\tCtrl-");
745 strFilename += static_cast<char>('0' + iPos);
747 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
748 m_apWindowMenuData[iPos] = pDoc;
749 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
751 pNode = pNode->GetNext();
753 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
754 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
755 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (_T("[EMPTY]"));
756 m_apWindowMenuData[i] = NULL;
763 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
765 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
766 wxString strFilename = pDoc->GetFilename();
767 const wxView* pView = pDoc->GetFirstView();
769 wxWindow* pWindow = pView->GetFrame();
776 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
777 { DoWindowMenu (0, event); }
779 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
780 { DoWindowMenu (1, event); }
782 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
783 { DoWindowMenu (2, event); }
785 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
786 { DoWindowMenu (3, event); }
788 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
789 { DoWindowMenu (4, event); }
791 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
792 { DoWindowMenu (5, event); }
794 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
795 { DoWindowMenu (6, event); }
797 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
798 { DoWindowMenu (7, event); }
800 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
801 { DoWindowMenu (8, event); }
803 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
804 { DoWindowMenu (9, event); }
806 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
807 { DoWindowMenu (10, event); }
809 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
810 { DoWindowMenu (11, event); }
812 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
813 { DoWindowMenu (12, event); }
815 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
816 { DoWindowMenu (13, event); }
818 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
819 { DoWindowMenu (14, event); }
821 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
822 { DoWindowMenu (15, event); }
824 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
825 { DoWindowMenu (16, event); }
827 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
828 { DoWindowMenu (17, event); }
830 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
831 { DoWindowMenu (18, event); }
833 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
834 { DoWindowMenu (19, event); }
837 class BitmapControl : public wxPanel
840 DECLARE_DYNAMIC_CLASS (BitmapControl)
841 DECLARE_EVENT_TABLE ()
845 BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
846 const wxPoint& pos = wxDefaultPosition,
847 const wxSize& size = wxDefaultSize,
848 long style = wxSTATIC_BORDER,
849 const wxValidator& validator = wxDefaultValidator,
850 const wxString& name = _T("BitmapCtrl"));
853 virtual ~BitmapControl();
855 virtual wxSize GetBestSize() const;
857 wxBitmap* getBitmap()
858 { return m_pBitmap; }
860 void OnPaint(wxPaintEvent& event);
864 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
865 EVT_PAINT(BitmapControl::OnPaint)
868 IMPLEMENT_CLASS(BitmapControl, wxPanel)
871 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
872 long style, const wxValidator& validator, const wxString& name)
875 Create(parent, id, pos, size, style, name);
877 SetSize (GetBestSize());
881 BitmapControl::GetBestSize () const
884 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
889 BitmapControl::~BitmapControl()
893 BitmapControl::OnPaint (wxPaintEvent& event)
897 dc.DrawBitmap (*m_pBitmap, 0, 0);
901 class BitmapDialog : public wxDialog {
903 BitmapControl* m_pBitmapCtrl;
906 BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
907 virtual ~BitmapDialog();
910 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
911 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle,*wxConvCurrent), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
913 wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
915 pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
917 wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
918 wxButton* pButtonOk = new wxButton (this, wxID_OK, _T("Ok"));
919 pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
921 pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
923 SetAutoLayout (true);
924 SetSizer (pTopSizer);
925 pTopSizer->Fit (this);
926 pTopSizer->SetSizeHints (this);
929 BitmapDialog::~BitmapDialog()
934 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
936 DialogPreferences dlg (this, _T("CTSim Preferences"), theApp->getAdvancedOptions(),
937 theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(),
938 theApp->getUseBackgroundTasks());
939 if (dlg.ShowModal() == wxID_OK) {
940 theApp->setAdvancedOptions (dlg.getAdvancedOptions());
941 theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
942 theApp->setVerboseLogging (dlg.getVerboseLogging());
943 theApp->setStartupTips (dlg.getStartupTips());
944 theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
949 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
951 DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
952 if (dialogImport.ShowModal() != wxID_OK)
955 wxString strFormatName (dialogImport.getFormatName (), *wxConvCurrent);
956 m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.mb_str(wxConvUTF8));
959 wxString strWildcard;
960 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
962 strWildcard = _T("PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm");
965 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
967 strWildcard = _T("PNG Files (*.png)|*.png");
970 #ifdef HAVE_CTN_DICOM
971 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
973 strWildcard = _T("Dicom Files (*.*)|*.*");
980 #if WXWIN_COMPATIBILITY_2_4
981 wxString strFilename = wxFileSelector (wxString(wxConvUTF8.cMB2WX("Import Filename")), wxString(wxConvUTF8.cMB2WX("")),
982 wxString(wxConvUTF8.cMB2WX("")), strExt, strWildcard, wxHIDE_READONLY | wxOPEN);
984 wxString strFilename = wxFileSelector (wxString(wxConvUTF8.cMB2WX("Import Filename")), wxString(wxConvUTF8.cMB2WX("")),
985 wxString(wxConvUTF8.cMB2WX("")), strExt, strWildcard, wxOPEN);
988 if (! strFilename.IsEmpty()) {
989 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM || m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
990 ImageFile* pIF = new ImageFile;
991 if (pIF->importImage (strFormatName.mb_str(wxConvUTF8), strFilename.mb_str(wxConvUTF8))) {
992 ImageFileDocument* pIFDoc = theApp->newImageDoc();
993 pIFDoc->setImageFile(pIF);
994 pIFDoc->getView()->getFrame()->Show(true);
995 std::ostringstream os;
996 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
997 pIF->labelAdd (os.str().c_str());
998 if (theApp->getAskDeleteNewDocs())
999 pIFDoc->Modify (true);
1000 pIFDoc->UpdateAllViews();
1001 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
1002 pIFDoc->getView()->getFrame()->Show(true);
1006 #ifdef HAVE_CTN_DICOM
1007 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
1008 DicomImporter dicomImport (strFilename.mb_str(wxConvUTF8));
1009 if (dicomImport.fail()) {
1010 ::wxMessageBox (wxConvUTF8.cMB2WX(dicomImport.failMessage().c_str()), _T("Import Error"));
1011 } else if (dicomImport.testImage()) {
1012 ImageFileDocument* pIFDoc = theApp->newImageDoc();
1013 ImageFile* pIF = dicomImport.getImageFile();
1014 pIFDoc->setImageFile (pIF);
1015 std::ostringstream os;
1016 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1017 pIF->labelAdd (os.str().c_str());
1018 if (theApp->getAskDeleteNewDocs())
1019 pIFDoc->Modify (true);
1020 pIFDoc->UpdateAllViews();
1021 pIFDoc->getView()->setInitialClientSize();
1023 } else if (dicomImport.testProjections()) {
1024 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
1025 Projections* pProj = dicomImport.getProjections();
1026 pProjDoc->setProjections (pProj);
1027 pProjDoc->getView()->getFrame()->Show(true);
1028 std::ostringstream os;
1029 os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1030 pProj->setRemark (os.str().c_str());
1031 if (theApp->getAskDeleteNewDocs())
1032 pProjDoc->Modify (true);
1033 pProjDoc->UpdateAllViews();
1034 pProjDoc->getView()->setInitialClientSize();
1035 pProjDoc->Activate();
1037 ::wxMessageBox (_T("Unrecognized DICOM file contents"), _T("Import Error"));
1041 sys_error (ERR_WARNING, "Unknown import format type");
1045 #include "./splash.xpm"
1047 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1049 wxString strOSDesc = ::wxGetOsDescription();
1050 *theApp->getLog() << _T("Operating System: ") << strOSDesc;
1051 *theApp->getLog() << _T(", wxWindows: ") << wxVERSION_STRING;
1053 *theApp->getLog() << _T(", Build Date: ") << wxConvUTF8.cMB2WX(_TIMESTAMP__);
1056 *theApp->getLog() << _T(", Debug version");
1058 *theApp->getLog() << _T(", Release version");
1061 *theApp->getLog() << _T(" ") << wxConvUTF8.cMB2WX(VERSION);
1062 #elif defined(CTSIMVERSION)
1063 *theApp->getLog() << _T(" ") << _T(CTSIMVERSION);
1065 *theApp->getLog() << _T("\n");
1067 wxBitmap bmp (splash);
1069 BitmapDialog dlg (&bmp, "About CTSim");
1072 wxString msg = _T("CTSim\nThe Open Source Computed Tomography Simulator\n");
1074 msg << _T("Version: ") << wxConvUTF8.cMB2WX(VERSION) << _T("\n\n");
1075 #elif defined(CTSIMVERSION)
1076 msg << _T("Version: ") << wxConvUTF8.cMB2WX(CTSIMVERSION) << _T("\n\n");
1078 msg += _T("Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]");
1080 wxMessageBox(msg, _T("About CTSim"), wxOK | wxICON_INFORMATION, this);
1081 *theApp->getLog() << msg << wxConvUTF8.cMB2WX("\n");
1086 // Create new documents
1088 ProjectionFileDocument*
1089 CTSimApp::newProjectionDoc()
1091 ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (_T("")));
1093 newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
1094 newDoc->SetDocumentTemplate (m_pDocTemplProjection);
1095 newDoc->OnNewDocument();
1102 CTSimApp::newImageDoc()
1104 ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (_T("")));
1106 newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
1107 newDoc->SetDocumentTemplate (m_pDocTemplImage);
1108 newDoc->OnNewDocument();
1115 CTSimApp::newPlotDoc()
1117 PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (_T("")));
1119 newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
1120 newDoc->SetDocumentTemplate (m_pDocTemplPlot);
1121 newDoc->OnNewDocument();
1129 CTSimApp::newTextDoc()
1131 wxString strFilename (getUntitledFilename());
1132 strFilename += wxString(".txt", *wxConvCurrent);
1134 TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (_T("")));
1136 newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
1137 newDoc->SetDocumentTemplate (m_pDocTemplText);
1138 newDoc->OnNewDocument();
1145 PhantomFileDocument*
1146 CTSimApp::newPhantomDoc()
1148 PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (_T("")));
1150 newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1151 newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1152 newDoc->OnNewDocument();
1160 Graph3dFileDocument*
1161 CTSimApp::newGraph3dDoc()
1163 Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (_T("")));
1165 newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1166 newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1167 newDoc->OnNewDocument();