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"
39 #include "wx/cmdline.h"
41 #include "wx/msw/helpchm.h"
44 #if !wxUSE_DOC_VIEW_ARCHITECTURE
45 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
55 #include "backgroundmgr.h"
57 #if defined(HAVE_CONFIG_H)
61 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
72 IMPLEMENT_APP(CTSimApp)
75 : m_bAdvancedOptions(false), m_bSetModifyNewDocs(true),
76 m_bVerboseLogging(false), m_bShowStartupTips(true),
77 m_iCurrentTip(0), m_bUseBackgroundTasks(false),
78 m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0),
79 m_bPrintCmdLineImages(false), m_bCmdLineVerboseFlag(false)
84 #ifdef HAVE_SYS_TIME_H
88 #ifdef HAVE_SYS_RESOURCE_H
89 #include <sys/resource.h>
93 void CTSimApp::OnInitCmdLine(wxCmdLineParser& parser)
95 static const wxCmdLineEntryDesc cmdLineDesc[] = {
96 { wxCMD_LINE_SWITCH, wxT_2("l"), wxT_2("verbose"), wxT_2("verbose logging") },
97 { wxCMD_LINE_SWITCH, wxT_2("v"), wxT_2("version"), wxT_2("print version") },
98 { wxCMD_LINE_SWITCH, wxT_2("p"), wxT_2("print"), wxT_2("print images from command line"),
100 wxCMD_LINE_PARAM_OPTIONAL },
101 { wxCMD_LINE_SWITCH, wxT_2("h"), wxT_2("help"), wxT_2("print this help message"),
103 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_OPTION_HELP },
104 { wxCMD_LINE_PARAM, NULL, NULL, wxT_2("input file"),
105 wxCMD_LINE_VAL_STRING,
106 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
110 parser.SetDesc(cmdLineDesc);
113 bool CTSimApp::OnCmdLineParsed(wxCmdLineParser& parser)
115 if (wxApp::OnCmdLineParsed(parser) == false)
118 if (parser.Found(_T("version"))) {
120 std::cout << "Version: " << VERSION << std::endl;
121 #elif defined(CTSIMVERSION)
122 std::cout << "Version: " << CTSIMVERSION << std::endl;
124 std::cout << "Version: " << "Unknown" << std::endl;
128 if (parser.Found(_T("print"))) {
129 m_bPrintCmdLineImages = true;
131 if (parser.Found(_T("verbose"))) {
132 m_bCmdLineVerboseFlag = true;
141 if (! wxApp::OnInit())
144 #ifdef HAVE_SETPRIORITY
145 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
150 g_bRunningWXWindows = true;
151 m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
153 m_pDocTemplImage = new wxDocTemplate (m_docManager, _T("ImageFile"), _T("*.if"), _T(""), _T("if"), _T("ImageFile"), _T("ImageView"), CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
154 m_pDocTemplProjection = new wxDocTemplate (m_docManager, _T("ProjectionFile"), _T("*.pj"), _T(""), _T("pj"), _T("ProjectionFile"), _T("ProjectionView"), CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
155 m_pDocTemplPhantom = new wxDocTemplate (m_docManager, _T("PhantomFile"), _T("*.phm"), _T(""), _T("phm"), _T("PhantomFile"), _T("PhantomView"), CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
156 m_pDocTemplPlot = new wxDocTemplate (m_docManager, _T("PlotFile"), _T("*.plt"), _T(""), _T("plt"), _T("PlotFile"), _T("PlotView"), CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
157 m_pDocTemplText = new wxDocTemplate (m_docManager, _T("TextFile"), _T("*.txt"), _T(""), _T("txt"), _T("TextFile"), _T("TextView"), CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
159 m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, _T("Graph3dFile"), _T("*.g3d"), _T(""), _T("g3d"),
160 _T("Graph3dFile"), _T("Graph3dView"), CLASSINFO(Graph3dFileDocument),
161 CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
165 wxImage::AddHandler(new wxGIFHandler); // Required for images in the online documentation
168 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
169 wxFileSystem::AddHandler(new wxZipFSHandler); // Required for advanced HTML help
172 // Create the main frame window
173 int xDisplay, yDisplay;
174 ::wxDisplaySize (&xDisplay, &yDisplay);
177 wxSize frameSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .75));
179 wxSize frameSize(nearest<int>(xDisplay * .6), nearest<int>(yDisplay * .4));
182 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, _T("CTSim"), wxPoint(0, 0), frameSize, wxDEFAULT_FRAME_STYLE);
184 setIconForFrame (m_pFrame);
185 m_pFrame->Centre(wxBOTH);
186 m_pFrame->Show(true);
187 SetTopWindow (m_pFrame);
190 m_docManager->FileHistoryLoad(*m_pConfig);
193 m_pLogDoc = newTextDoc();
195 m_pLog = m_pLogDoc->getTextCtrl();
196 m_pLogDoc->SetDocumentName(_T("Log.txt"));
197 m_pLogDoc->SetFilename(_T("Log.txt"));
198 m_pLogDoc->getView()->getFrame()->SetTitle(_T("Log"));
200 m_pFrame->GetClientSize(&xSize, &ySize);
201 int yLogSize = ySize / 4;
202 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
203 m_pLogDoc->getView()->getFrame()->Show (true);
206 m_pLog = new wxTextCtrl (m_pFrame, -1, _T("Log Window\n"), wxPoint(0, 0), frameSize, wxTE_MULTILINE | wxTE_READONLY);
208 wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
211 if (! m_pConfig->Read(_T("HelpDir"), &helpDir))
212 helpDir = ::wxGetCwd();
214 if (! m_pFrame->getWinHelpController().Initialize(helpDir + _T("/ctsim")))
215 *m_pLog << _T("Cannot initialize the Windows Help system") << _T("\n");
218 wxString docDir (DATADIR, *wxConvCurrent);
220 wxString docDir (::wxGetCwd());
222 wxString docFile = docDir + _T("ctsim.htb");
223 if (! m_pFrame->getHtmlHelpController().AddBook(docFile) &&
224 ! m_pFrame->getHtmlHelpController().AddBook(_T("/usr/share/ctsim/ctsim.htb")) &&
225 ! m_pFrame->getHtmlHelpController().AddBook(_T("/tmp/ctsim.htb")))
226 *m_pLog << _T("Cannot initialize the HTML Help system") << _T("\n");
228 if (::wxDirExists (_T("/tmp")))
229 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
230 m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
234 for (int i = optind + 1; i <= argc; i++) {
235 wxString filename = argv [i - 1];
236 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
237 if (m_bPrintCmdLineImages) {
238 wxView* pNewView = pNewDoc->GetFirstView();
239 wxPrintout *printout = pNewView->OnCreatePrintout();
242 printer.Print(pNewView->GetFrame(), printout, TRUE);
245 wxCommandEvent nullEvent;
246 nullEvent.SetId (wxID_CLOSE);
247 m_docManager->OnFileClose (nullEvent);
251 if (m_bPrintCmdLineImages) {
252 wxCommandEvent closeEvent;
253 closeEvent.SetInt (MAINMENU_FILE_EXIT);
254 m_pFrame->GetEventHandler()->ProcessEvent(closeEvent);
257 if (getStartupTips())
260 #ifdef HAVE_WXTHREADS
261 m_pBackgroundMgr = new BackgroundManager;
270 CTSimTipProvider tipProvider (m_iCurrentTip);
271 setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
272 m_iCurrentTip = tipProvider.GetCurrentTip();
276 #include "./ctsim.xpm"
278 CTSimApp::setIconForFrame(wxFrame* pFrame)
280 wxIcon iconApp (ctsim16_xpm);
283 pFrame->SetIcon (iconApp);
287 CTSimApp::usage(const char* program)
289 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
290 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
292 std::cout << " --version Display version\n";
293 std::cout << " --help Display this help message\n";
308 CTSimApp::openConfig()
311 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(""), _T(""), wxCONFIG_USE_LOCAL_FILE);
313 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(".ctsim"), _T(""), wxCONFIG_USE_LOCAL_FILE);
316 wxConfigBase::Set(m_pConfig);
317 m_pConfig->Read (_T("AdvancedOptions"), &m_bAdvancedOptions);
318 m_pConfig->Read (_T("SetModifyNewDocs"), &m_bSetModifyNewDocs);
319 m_pConfig->Read (_T("VerboseLogging"), &m_bVerboseLogging);
320 m_pConfig->Read (_T("StartupTips"), &m_bShowStartupTips);
321 m_pConfig->Read (_T("CurrentTip"), &m_iCurrentTip);
322 m_pConfig->Read (_T("UseBackgroundTasks"), &m_bUseBackgroundTasks);
324 wxString strFftwWisdom;
325 m_pConfig->Read (_T("FftwWisdom"), strFftwWisdom);
326 if (strFftwWisdom.size() > 0)
327 fftw_import_wisdom_from_string (strFftwWisdom.mb_str(wxConvUTF8));
332 CTSimApp::closeConfig()
334 m_pConfig->Write (_T("AdvancedOptions"), m_bAdvancedOptions);
335 m_pConfig->Write (_T("SetModifyNewDocs"), m_bSetModifyNewDocs);
336 m_pConfig->Write (_T("VerboseLogging"), m_bVerboseLogging);
337 m_pConfig->Write (_T("StartupTips"), m_bShowStartupTips);
338 m_pConfig->Write (_T("CurrentTip"), m_iCurrentTip);
339 m_pConfig->Write (_T("UseBackgroundTasks"), m_bUseBackgroundTasks);
341 const char* const pszWisdom = fftw_export_wisdom_to_string();
342 wxString strFftwWisdom (pszWisdom, *wxConvCurrent);
343 fftw_free ((void*) pszWisdom);
344 m_pConfig->Write (_T("FftwWisdom"), strFftwWisdom);
351 // Top-level window for CTSim
354 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
356 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
358 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
360 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
363 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
364 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
365 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
366 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
367 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
368 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
369 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
370 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
371 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
372 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
373 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
374 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
375 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
376 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
377 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
378 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
379 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
380 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
381 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
382 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
384 EVT_SIZE(MainFrame::OnSize)
386 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
387 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
389 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
390 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
391 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
393 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
395 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
396 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
397 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
398 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
399 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
400 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
401 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
402 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
403 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
404 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
405 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
406 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
407 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
408 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
409 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
410 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
411 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
412 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
413 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
414 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
415 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
421 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
422 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, _T("MainFrame"))
424 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
425 : wxDocParentFrame(manager, frame, id, title, pos, size, type, _T("MainFrame"))
428 m_bShuttingDown = false;
431 wxMenu *file_menu = new wxMenu;
433 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, _T("Cr&eate Phantom...\tCtrl-P"));
434 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, _T("Create &Filter...\tCtrl-F"));
435 file_menu->Append(wxID_OPEN, _T("&Open...\tCtrl-O"));
437 file_menu->AppendSeparator();
438 file_menu->Append (MAINMENU_IMPORT, _T("&Import...\tCtrl-M"));
439 file_menu->Append (MAINMENU_FILE_PREFERENCES, _T("Prefere&nces..."));
440 file_menu->Append(MAINMENU_FILE_EXIT, _T("E&xit"));
442 // history of files visited
443 theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
444 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
447 m_pWindowMenu = new wxMenu;
448 m_pWindowMenu->UpdateUI (this);
451 wxMenu* help_menu = new wxMenu;
452 help_menu->Append (MAINMENU_HELP_CONTENTS, _T("&Contents\tF1"));
453 help_menu->Append (MAINMENU_HELP_TIPS, _T("&Tips"));
454 help_menu->Append (IDH_QUICKSTART, _T("&Quick Start"));
455 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
456 help_menu->Append (MAINMENU_HELP_SECONDARY, _T("&Secondary Help"));
458 help_menu->Append (MAINMENU_HELP_ABOUT, _T("&About"));
460 wxMenuBar* menu_bar = new wxMenuBar;
462 menu_bar->Append(file_menu, _T("&File"));
464 menu_bar->Append(m_pWindowMenu, _T("&Window"));
466 menu_bar->Append(help_menu, _T("&Help"));
468 SetMenuBar(menu_bar);
473 for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
474 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, _T("[EMPTY]"));
475 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
476 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
480 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
481 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
482 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
483 m_iDefaultFilterXSize = 256;
484 m_iDefaultFilterYSize = 256;
485 m_dDefaultFilterParam = 1.;
486 m_dDefaultFilterBandwidth = 1.;
487 m_dDefaultFilterInputScale = 1.;
488 m_dDefaultFilterOutputScale = 1.;
489 m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PNG;
491 wxAcceleratorEntry accelEntries[15];
492 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
493 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
494 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
495 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
496 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
498 for (i = 0; i < 10; i++)
499 accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
500 wxAcceleratorTable accelTable (15, accelEntries);
502 wxAcceleratorTable accelTable (5, accelEntries);
505 SetAcceleratorTable (accelTable);
508 MainFrame::~MainFrame()
510 m_bShuttingDown = true; // Currently used so that Log Window will close
512 // delete all non-modified documents
513 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
514 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
515 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
516 if (pDoc && ! pDoc->IsModified()) {
517 theApp->getDocManager()->RemoveDocument(pDoc);
523 if (theApp->getConfig())
524 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
526 delete theApp->getDocManager();
531 MainFrame::OnSize (wxSizeEvent& event)
534 if (theApp->getLogDoc()) {
536 GetClientSize(&xSize, &ySize);
537 int yLogSize = ySize / 4;
538 theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
539 theApp->getLogDoc()->getView()->getFrame()->Show (true);
544 wxDocMDIParentFrame::OnSize (event);
546 wxDocParentFrame::OnSize (event);
551 MainFrame::OnCreatePhantom(wxCommandEvent& event)
553 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
554 int dialogReturn = dialogPhantom.ShowModal();
555 if (dialogReturn == wxID_OK) {
556 wxString selection (dialogPhantom.getPhantom(), *wxConvCurrent);
557 if (theApp->getVerboseLogging())
558 *theApp->getLog() << _T("Selected phantom ") << selection.c_str() << _T("\n");
559 wxString filename = selection + _T(".phm");
560 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.mb_str(wxConvUTF8));
561 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
567 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
569 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
570 int dialogReturn = dialogFilter.ShowModal();
571 if (dialogReturn == wxID_OK) {
572 wxString strFilter (dialogFilter.getFilterName(), *wxConvCurrent);
573 wxString strDomain (dialogFilter.getDomainName(), *wxConvCurrent);
574 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.mb_str(wxConvUTF8));
575 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.mb_str(wxConvUTF8));
576 m_iDefaultFilterXSize = dialogFilter.getXSize();
577 m_iDefaultFilterYSize = dialogFilter.getYSize();
578 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
579 m_dDefaultFilterParam= dialogFilter.getFilterParam();
580 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
581 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
583 os << _T("Generate Filter=") << strFilter
584 << _T(", size=(") << static_cast<int>(m_iDefaultFilterXSize) << _T(",")
585 << static_cast<int>(m_iDefaultFilterYSize)
586 << _T("), domain=") << strDomain.c_str() << _T(", filterParam=")
587 << m_dDefaultFilterParam << _T(", bandwidth=") <<
588 m_dDefaultFilterBandwidth
589 << _T(", inputScale=") << m_dDefaultFilterInputScale << _T(", outputScale=") << m_dDefaultFilterOutputScale;
590 *theApp->getLog() << os << _T("\n");
591 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
592 pFilterDoc->setBadFileOpen();
594 sys_error (ERR_SEVERE, "Unable to create filter image");
597 ImageFile& rIF = pFilterDoc->getImageFile();
598 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
599 rIF.filterResponse (strDomain.mb_str(wxConvUTF8), m_dDefaultFilterBandwidth, strFilter.mb_str(wxConvUTF8), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
600 rIF.labelAdd (os.mb_str(wxConvUTF8));
601 if (theApp->getAskDeleteNewDocs())
602 pFilterDoc->Modify (true);
603 pFilterDoc->UpdateAllViews();
604 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
605 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
606 pFilterDoc->getView()->getFrame()->Show(true);
611 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
613 const ImageFile& rIF = pIFDoc->getImageFile();
614 unsigned int nx = rIF.nx();
615 unsigned int ny = rIF.ny();
616 wxList& rListDocs = m_docManager->GetDocuments();
617 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
618 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
619 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
620 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
621 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
622 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
623 vecIF.push_back (pIFCompareDoc);
630 MainFrame::OnNewImageFile (wxCommandEvent& event)
632 ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
634 ImageFileDocument* pImageDoc = theApp->newImageDoc();
636 sys_error (ERR_SEVERE, "Unable to create image file");
639 pImageDoc->setImageFile (pImageFile);
640 if (theApp->getAskDeleteNewDocs())
641 pImageDoc->Modify (true);
645 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
647 Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
648 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
650 sys_error (ERR_SEVERE, "Unable to create projection file");
653 pProjDoc->setProjections (pProjections);
654 if (theApp->getAskDeleteNewDocs())
655 pProjDoc->Modify (true);
659 MainFrame::OnLogEvent (wxCommandEvent& event)
661 *theApp->getLog() << event.GetString();
665 MainFrame::OnHelpTips (wxCommandEvent& event)
671 MainFrame::OnHelpContents (wxCommandEvent& event)
673 showHelp (event.GetId());
677 MainFrame::OnHelpButton (wxCommandEvent& event)
679 showHelp (event.GetId());
682 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
684 MainFrame::OnHelpSecondary (wxCommandEvent& event)
686 m_htmlHelp.Display ("Contents");
691 MainFrame::showHelp (int commandID)
695 case MAINMENU_HELP_CONTENTS:
697 m_winHelp.DisplayContents ();
699 m_htmlHelp.Display (_T("Contents"));
705 m_winHelp.DisplaySection (commandID);
707 m_htmlHelp.Display (commandID);
714 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
720 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
724 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
725 wxNode* pNode = rListDocs.GetFirst();
726 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
727 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
728 wxString strFilename = pDoc->GetFilename();
730 strFilename += _T("\tCtrl-");
731 strFilename += static_cast<char>('0' + iPos);
733 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetItemLabel (strFilename);
734 m_apWindowMenuData[iPos] = pDoc;
735 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
737 pNode = pNode->GetNext();
739 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
740 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
741 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetItemLabel (_T("[EMPTY]"));
742 m_apWindowMenuData[i] = NULL;
749 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
751 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
752 wxString strFilename = pDoc->GetFilename();
753 const wxView* pView = pDoc->GetFirstView();
755 wxWindow* pWindow = pView->GetFrame();
762 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
763 { DoWindowMenu (0, event); }
765 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
766 { DoWindowMenu (1, event); }
768 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
769 { DoWindowMenu (2, event); }
771 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
772 { DoWindowMenu (3, event); }
774 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
775 { DoWindowMenu (4, event); }
777 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
778 { DoWindowMenu (5, event); }
780 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
781 { DoWindowMenu (6, event); }
783 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
784 { DoWindowMenu (7, event); }
786 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
787 { DoWindowMenu (8, event); }
789 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
790 { DoWindowMenu (9, event); }
792 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
793 { DoWindowMenu (10, event); }
795 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
796 { DoWindowMenu (11, event); }
798 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
799 { DoWindowMenu (12, event); }
801 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
802 { DoWindowMenu (13, event); }
804 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
805 { DoWindowMenu (14, event); }
807 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
808 { DoWindowMenu (15, event); }
810 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
811 { DoWindowMenu (16, event); }
813 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
814 { DoWindowMenu (17, event); }
816 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
817 { DoWindowMenu (18, event); }
819 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
820 { DoWindowMenu (19, event); }
823 class BitmapControl : public wxPanel
826 DECLARE_DYNAMIC_CLASS (BitmapControl)
827 DECLARE_EVENT_TABLE ()
831 BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
832 const wxPoint& pos = wxDefaultPosition,
833 const wxSize& size = wxDefaultSize,
834 long style = wxSTATIC_BORDER,
835 const wxValidator& validator = wxDefaultValidator,
836 const wxString& name = _T("BitmapCtrl"));
839 virtual ~BitmapControl();
841 virtual wxSize GetBestSize() const;
843 wxBitmap* getBitmap()
844 { return m_pBitmap; }
846 void OnPaint(wxPaintEvent& event);
850 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
851 EVT_PAINT(BitmapControl::OnPaint)
854 IMPLEMENT_CLASS(BitmapControl, wxPanel)
857 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
858 long style, const wxValidator& validator, const wxString& name)
861 Create(parent, id, pos, size, style, name);
863 SetSize (GetBestSize());
867 BitmapControl::GetBestSize () const
870 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
875 BitmapControl::~BitmapControl()
879 BitmapControl::OnPaint (wxPaintEvent& event)
883 dc.DrawBitmap (*m_pBitmap, 0, 0);
887 class BitmapDialog : public wxDialog {
889 BitmapControl* m_pBitmapCtrl;
892 BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
893 virtual ~BitmapDialog();
896 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
897 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle,*wxConvCurrent))
899 wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
901 pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
903 wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
904 wxButton* pButtonOk = new wxButton (this, wxID_OK, _T("Ok"));
905 pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
907 pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
909 SetAutoLayout (true);
910 SetSizer (pTopSizer);
911 pTopSizer->Fit (this);
912 pTopSizer->SetSizeHints (this);
915 BitmapDialog::~BitmapDialog()
920 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
922 DialogPreferences dlg (this, _T("CTSim Preferences"), theApp->getAdvancedOptions(),
923 theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(),
924 theApp->getUseBackgroundTasks());
925 if (dlg.ShowModal() == wxID_OK) {
926 theApp->setAdvancedOptions (dlg.getAdvancedOptions());
927 theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
928 theApp->setVerboseLogging (dlg.getVerboseLogging());
929 theApp->setStartupTips (dlg.getStartupTips());
930 theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
935 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
937 DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
938 if (dialogImport.ShowModal() != wxID_OK)
941 wxString strFormatName (dialogImport.getFormatName (), *wxConvCurrent);
942 m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.mb_str(wxConvUTF8));
945 wxString strWildcard;
946 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
948 strWildcard = _T("PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm");
951 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
953 strWildcard = _T("PNG Files (*.png)|*.png");
956 #ifdef HAVE_CTN_DICOM
957 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
959 strWildcard = _T("Dicom Files (*.*)|*.*");
966 wxString strFilename = wxFileSelector (wxString(wxConvUTF8.cMB2WX("Import Filename")), wxString(wxConvUTF8.cMB2WX("")),
967 wxString(wxConvUTF8.cMB2WX("")), strExt, strWildcard, wxFD_OPEN);
969 if (! strFilename.IsEmpty()) {
970 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM || m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
971 ImageFile* pIF = new ImageFile;
972 if (pIF->importImage (strFormatName.mb_str(wxConvUTF8), strFilename.mb_str(wxConvUTF8))) {
973 ImageFileDocument* pIFDoc = theApp->newImageDoc();
974 pIFDoc->setImageFile(pIF);
975 pIFDoc->getView()->getFrame()->Show(true);
976 std::ostringstream os;
977 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
978 pIF->labelAdd (os.str().c_str());
979 if (theApp->getAskDeleteNewDocs())
980 pIFDoc->Modify (true);
981 pIFDoc->UpdateAllViews();
982 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
983 pIFDoc->getView()->getFrame()->Show(true);
987 #ifdef HAVE_CTN_DICOM
988 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
989 DicomImporter dicomImport (strFilename.mb_str(wxConvUTF8));
990 if (dicomImport.fail()) {
991 ::wxMessageBox (wxConvUTF8.cMB2WX(dicomImport.failMessage().c_str()), _T("Import Error"));
992 } else if (dicomImport.testImage()) {
993 ImageFileDocument* pIFDoc = theApp->newImageDoc();
994 ImageFile* pIF = dicomImport.getImageFile();
995 pIFDoc->setImageFile (pIF);
996 std::ostringstream os;
997 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
998 pIF->labelAdd (os.str().c_str());
999 if (theApp->getAskDeleteNewDocs())
1000 pIFDoc->Modify (true);
1001 pIFDoc->UpdateAllViews();
1002 pIFDoc->getView()->setInitialClientSize();
1004 } else if (dicomImport.testProjections()) {
1005 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
1006 Projections* pProj = dicomImport.getProjections();
1007 pProjDoc->setProjections (pProj);
1008 pProjDoc->getView()->getFrame()->Show(true);
1009 std::ostringstream os;
1010 os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1011 pProj->setRemark (os.str().c_str());
1012 if (theApp->getAskDeleteNewDocs())
1013 pProjDoc->Modify (true);
1014 pProjDoc->UpdateAllViews();
1015 pProjDoc->getView()->setInitialClientSize();
1016 pProjDoc->Activate();
1018 ::wxMessageBox (_T("Unrecognized DICOM file contents"), _T("Import Error"));
1022 sys_error (ERR_WARNING, "Unknown import format type");
1026 #include "./splash.xpm"
1028 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1030 wxString strOSDesc = ::wxGetOsDescription();
1031 *theApp->getLog() << _T("Operating System: ") << strOSDesc;
1032 *theApp->getLog() << _T(", wxWindows: ") << wxVERSION_STRING;
1034 *theApp->getLog() << _T(", Build Date: ") << wxConvUTF8.cMB2WX(_TIMESTAMP__);
1037 *theApp->getLog() << _T(", Debug version");
1039 *theApp->getLog() << _T(", Release version");
1042 *theApp->getLog() << _T(" ") << wxConvUTF8.cMB2WX(VERSION);
1043 #elif defined(CTSIMVERSION)
1044 *theApp->getLog() << _T(" ") << _T(CTSIMVERSION);
1046 *theApp->getLog() << _T("\n");
1048 wxBitmap bmp (splash);
1050 BitmapDialog dlg (&bmp, "About CTSim");
1053 wxString msg = _T("CTSim\nThe Open Source Computed Tomography Simulator\n");
1055 msg << _T("Version: ") << wxConvUTF8.cMB2WX(VERSION) << _T("\n\n");
1056 #elif defined(CTSIMVERSION)
1057 msg << _T("Version: ") << wxConvUTF8.cMB2WX(CTSIMVERSION) << _T("\n\n");
1059 msg += _T("Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]");
1061 wxMessageBox(msg, _T("About CTSim"), wxOK | wxICON_INFORMATION, this);
1062 *theApp->getLog() << msg << wxConvUTF8.cMB2WX("\n");
1067 // Create new documents
1070 CTSimApp::newDocumentHelper (wxDocTemplate* tmpl) {
1071 wxDocument* newDoc = tmpl->CreateDocument (_T(""));
1073 newDoc->SetDocumentTemplate (tmpl);
1074 newDoc->OnNewDocument();
1075 wxString fname = newDoc->GetFilename();
1077 fname += tmpl->GetDefaultExtension();
1078 newDoc->SetDocumentName(fname);
1079 newDoc->SetTitle(fname);
1080 newDoc->SetFilename(fname, true);
1086 ProjectionFileDocument*
1087 CTSimApp::newProjectionDoc()
1089 ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>
1090 (newDocumentHelper(m_pDocTemplProjection));
1092 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1097 CTSimApp::newImageDoc()
1099 ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>
1100 (newDocumentHelper(m_pDocTemplImage));
1102 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1108 CTSimApp::newPlotDoc()
1110 PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>
1111 (newDocumentHelper(m_pDocTemplPlot));
1113 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1120 CTSimApp::newTextDoc()
1122 TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>
1123 (newDocumentHelper(m_pDocTemplText));
1125 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1131 PhantomFileDocument*
1132 CTSimApp::newPhantomDoc()
1134 PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>
1135 (newDocumentHelper(m_pDocTemplPhantom));
1137 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1144 Graph3dFileDocument*
1145 CTSimApp::newGraph3dDoc()
1147 Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>
1148 (newDocumentHelper(m_pDocTemplGraph3d));
1150 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());