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 // Move to wx3.0 event processing
252 if (m_bPrintCmdLineImages) {
253 wxCommandEvent closeEvent;
254 closeEvent.SetInt (MAINMENU_FILE_EXIT);
255 m_pFrame->AddPendingEvent(closeEvent);
258 if (getStartupTips())
261 #ifdef HAVE_WXTHREADS
262 m_pBackgroundMgr = new BackgroundManager;
271 CTSimTipProvider tipProvider (m_iCurrentTip);
272 setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
273 m_iCurrentTip = tipProvider.GetCurrentTip();
277 #include "./ctsim.xpm"
279 CTSimApp::setIconForFrame(wxFrame* pFrame)
281 wxIcon iconApp (ctsim16_xpm);
284 pFrame->SetIcon (iconApp);
288 CTSimApp::usage(const char* program)
290 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
291 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
293 std::cout << " --version Display version\n";
294 std::cout << " --help Display this help message\n";
309 CTSimApp::openConfig()
312 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(""), _T(""), wxCONFIG_USE_LOCAL_FILE);
314 m_pConfig = new wxConfig(_T("ctsim"), _T("Kevin Rosenberg"), _T(".ctsim"), _T(""), wxCONFIG_USE_LOCAL_FILE);
317 wxConfigBase::Set(m_pConfig);
318 m_pConfig->Read (_T("AdvancedOptions"), &m_bAdvancedOptions);
319 m_pConfig->Read (_T("SetModifyNewDocs"), &m_bSetModifyNewDocs);
320 m_pConfig->Read (_T("VerboseLogging"), &m_bVerboseLogging);
321 m_pConfig->Read (_T("StartupTips"), &m_bShowStartupTips);
322 m_pConfig->Read (_T("CurrentTip"), &m_iCurrentTip);
323 m_pConfig->Read (_T("UseBackgroundTasks"), &m_bUseBackgroundTasks);
325 wxString strFftwWisdom;
326 m_pConfig->Read (_T("FftwWisdom"), strFftwWisdom);
327 if (strFftwWisdom.size() > 0)
328 fftw_import_wisdom_from_string (strFftwWisdom.mb_str(wxConvUTF8));
333 CTSimApp::closeConfig()
335 m_pConfig->Write (_T("AdvancedOptions"), m_bAdvancedOptions);
336 m_pConfig->Write (_T("SetModifyNewDocs"), m_bSetModifyNewDocs);
337 m_pConfig->Write (_T("VerboseLogging"), m_bVerboseLogging);
338 m_pConfig->Write (_T("StartupTips"), m_bShowStartupTips);
339 m_pConfig->Write (_T("CurrentTip"), m_iCurrentTip);
340 m_pConfig->Write (_T("UseBackgroundTasks"), m_bUseBackgroundTasks);
342 const char* const pszWisdom = fftw_export_wisdom_to_string();
343 wxString strFftwWisdom (pszWisdom, *wxConvCurrent);
344 fftw_free ((void*) pszWisdom);
345 m_pConfig->Write (_T("FftwWisdom"), strFftwWisdom);
352 // Top-level window for CTSim
355 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
357 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
359 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
361 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
364 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
365 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
366 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
367 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
368 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
369 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
370 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
371 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
372 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
373 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
374 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
375 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
376 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
377 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
378 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
379 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
380 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
381 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
382 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
383 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
385 EVT_SIZE(MainFrame::OnSize)
387 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
388 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
390 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
391 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
392 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
394 // Unsure how to fix, OnMRUFile procedure not found in packge
396 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
399 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
400 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
401 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
402 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
403 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
404 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
405 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
406 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
407 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
408 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
409 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
410 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
411 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
412 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
413 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
414 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
415 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
416 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
417 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
418 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
419 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
425 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
426 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, _T("MainFrame"))
428 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
429 : wxDocParentFrame(manager, frame, id, title, pos, size, type, _T("MainFrame"))
432 m_bShuttingDown = false;
435 wxMenu *file_menu = new wxMenu;
437 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, _T("Cr&eate Phantom...\tCtrl-P"));
438 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, _T("Create &Filter...\tCtrl-F"));
439 file_menu->Append(wxID_OPEN, _T("&Open...\tCtrl-O"));
441 file_menu->AppendSeparator();
442 file_menu->Append (MAINMENU_IMPORT, _T("&Import...\tCtrl-M"));
443 file_menu->Append (MAINMENU_FILE_PREFERENCES, _T("Prefere&nces..."));
444 file_menu->Append(MAINMENU_FILE_EXIT, _T("E&xit"));
446 // history of files visited
447 theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
448 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
451 m_pWindowMenu = new wxMenu;
452 m_pWindowMenu->UpdateUI (this);
455 wxMenu* help_menu = new wxMenu;
456 help_menu->Append (MAINMENU_HELP_CONTENTS, _T("&Contents\tF1"));
457 help_menu->Append (MAINMENU_HELP_TIPS, _T("&Tips"));
458 help_menu->Append (IDH_QUICKSTART, _T("&Quick Start"));
459 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
460 help_menu->Append (MAINMENU_HELP_SECONDARY, _T("&Secondary Help"));
462 help_menu->Append (MAINMENU_HELP_ABOUT, _T("&About"));
464 wxMenuBar* menu_bar = new wxMenuBar;
466 menu_bar->Append(file_menu, _T("&File"));
468 menu_bar->Append(m_pWindowMenu, _T("&Window"));
470 menu_bar->Append(help_menu, _T("&Help"));
472 SetMenuBar(menu_bar);
477 for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
478 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, _T("[EMPTY]"));
479 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
480 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
484 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
485 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
486 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
487 m_iDefaultFilterXSize = 256;
488 m_iDefaultFilterYSize = 256;
489 m_dDefaultFilterParam = 1.;
490 m_dDefaultFilterBandwidth = 1.;
491 m_dDefaultFilterInputScale = 1.;
492 m_dDefaultFilterOutputScale = 1.;
493 m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PNG;
495 wxAcceleratorEntry accelEntries[15];
496 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
497 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
498 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
499 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
500 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
502 for (i = 0; i < 10; i++)
503 accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
504 wxAcceleratorTable accelTable (15, accelEntries);
506 wxAcceleratorTable accelTable (5, accelEntries);
509 SetAcceleratorTable (accelTable);
512 MainFrame::~MainFrame()
514 m_bShuttingDown = true; // Currently used so that Log Window will close
516 // delete all non-modified documents
517 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
518 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
519 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
520 if (pDoc && ! pDoc->IsModified()) {
521 theApp->getDocManager()->RemoveDocument(pDoc);
527 if (theApp->getConfig())
528 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
530 delete theApp->getDocManager();
535 MainFrame::OnSize (wxSizeEvent& event)
538 if (theApp->getLogDoc()) {
540 GetClientSize(&xSize, &ySize);
541 int yLogSize = ySize / 4;
542 theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
543 theApp->getLogDoc()->getView()->getFrame()->Show (true);
548 wxDocMDIParentFrame::OnSize (event);
550 wxDocParentFrame::OnSize (event);
555 MainFrame::OnCreatePhantom(wxCommandEvent& event)
557 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
558 int dialogReturn = dialogPhantom.ShowModal();
559 if (dialogReturn == wxID_OK) {
560 wxString selection (dialogPhantom.getPhantom(), *wxConvCurrent);
561 if (theApp->getVerboseLogging())
562 *theApp->getLog() << _T("Selected phantom ") << selection.c_str() << _T("\n");
563 wxString filename = selection + _T(".phm");
564 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.mb_str(wxConvUTF8));
565 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
571 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
573 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
574 int dialogReturn = dialogFilter.ShowModal();
575 if (dialogReturn == wxID_OK) {
576 wxString strFilter (dialogFilter.getFilterName(), *wxConvCurrent);
577 wxString strDomain (dialogFilter.getDomainName(), *wxConvCurrent);
578 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.mb_str(wxConvUTF8));
579 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.mb_str(wxConvUTF8));
580 m_iDefaultFilterXSize = dialogFilter.getXSize();
581 m_iDefaultFilterYSize = dialogFilter.getYSize();
582 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
583 m_dDefaultFilterParam= dialogFilter.getFilterParam();
584 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
585 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
587 os << _T("Generate Filter=") << strFilter
588 << _T(", size=(") << static_cast<int>(m_iDefaultFilterXSize) << _T(",")
589 << static_cast<int>(m_iDefaultFilterYSize)
590 << _T("), domain=") << strDomain.c_str() << _T(", filterParam=")
591 << m_dDefaultFilterParam << _T(", bandwidth=") <<
592 m_dDefaultFilterBandwidth
593 << _T(", inputScale=") << m_dDefaultFilterInputScale << _T(", outputScale=") << m_dDefaultFilterOutputScale;
594 *theApp->getLog() << os << _T("\n");
595 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
596 pFilterDoc->setBadFileOpen();
598 sys_error (ERR_SEVERE, "Unable to create filter image");
601 ImageFile& rIF = pFilterDoc->getImageFile();
602 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
603 rIF.filterResponse (strDomain.mb_str(wxConvUTF8), m_dDefaultFilterBandwidth, strFilter.mb_str(wxConvUTF8), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
604 rIF.labelAdd (os.mb_str(wxConvUTF8));
605 if (theApp->getAskDeleteNewDocs())
606 pFilterDoc->Modify (true);
607 pFilterDoc->UpdateAllViews();
608 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
609 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
610 pFilterDoc->getView()->getFrame()->Show(true);
615 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
617 const ImageFile& rIF = pIFDoc->getImageFile();
618 unsigned int nx = rIF.nx();
619 unsigned int ny = rIF.ny();
620 wxList& rListDocs = m_docManager->GetDocuments();
621 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
622 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
623 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
624 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
625 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
626 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
627 vecIF.push_back (pIFCompareDoc);
634 MainFrame::OnNewImageFile (wxCommandEvent& event)
636 ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
638 ImageFileDocument* pImageDoc = theApp->newImageDoc();
640 sys_error (ERR_SEVERE, "Unable to create image file");
643 pImageDoc->setImageFile (pImageFile);
644 if (theApp->getAskDeleteNewDocs())
645 pImageDoc->Modify (true);
649 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
651 Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
652 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
654 sys_error (ERR_SEVERE, "Unable to create projection file");
657 pProjDoc->setProjections (pProjections);
658 if (theApp->getAskDeleteNewDocs())
659 pProjDoc->Modify (true);
663 MainFrame::OnLogEvent (wxCommandEvent& event)
665 *theApp->getLog() << event.GetString();
669 MainFrame::OnHelpTips (wxCommandEvent& event)
675 MainFrame::OnHelpContents (wxCommandEvent& event)
677 showHelp (event.GetId());
681 MainFrame::OnHelpButton (wxCommandEvent& event)
683 showHelp (event.GetId());
686 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
688 MainFrame::OnHelpSecondary (wxCommandEvent& event)
690 m_htmlHelp.Display ("Contents");
695 MainFrame::showHelp (int commandID)
699 case MAINMENU_HELP_CONTENTS:
701 m_winHelp.DisplayContents ();
703 m_htmlHelp.Display (_T("Contents"));
709 m_winHelp.DisplaySection (commandID);
711 m_htmlHelp.Display (commandID);
718 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
724 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
728 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
729 wxNode* pNode = rListDocs.GetFirst();
730 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
731 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
732 wxString strFilename = pDoc->GetFilename();
734 strFilename += _T("\tCtrl-");
735 strFilename += static_cast<char>('0' + iPos);
737 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
738 m_apWindowMenuData[iPos] = pDoc;
739 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
741 pNode = pNode->GetNext();
743 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
744 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
745 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (_T("[EMPTY]"));
746 m_apWindowMenuData[i] = NULL;
753 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
755 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
756 wxString strFilename = pDoc->GetFilename();
757 const wxView* pView = pDoc->GetFirstView();
759 wxWindow* pWindow = pView->GetFrame();
766 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
767 { DoWindowMenu (0, event); }
769 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
770 { DoWindowMenu (1, event); }
772 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
773 { DoWindowMenu (2, event); }
775 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
776 { DoWindowMenu (3, event); }
778 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
779 { DoWindowMenu (4, event); }
781 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
782 { DoWindowMenu (5, event); }
784 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
785 { DoWindowMenu (6, event); }
787 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
788 { DoWindowMenu (7, event); }
790 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
791 { DoWindowMenu (8, event); }
793 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
794 { DoWindowMenu (9, event); }
796 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
797 { DoWindowMenu (10, event); }
799 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
800 { DoWindowMenu (11, event); }
802 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
803 { DoWindowMenu (12, event); }
805 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
806 { DoWindowMenu (13, event); }
808 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
809 { DoWindowMenu (14, event); }
811 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
812 { DoWindowMenu (15, event); }
814 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
815 { DoWindowMenu (16, event); }
817 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
818 { DoWindowMenu (17, event); }
820 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
821 { DoWindowMenu (18, event); }
823 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
824 { DoWindowMenu (19, event); }
827 class BitmapControl : public wxPanel
830 DECLARE_DYNAMIC_CLASS (BitmapControl)
831 DECLARE_EVENT_TABLE ()
835 BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
836 const wxPoint& pos = wxDefaultPosition,
837 const wxSize& size = wxDefaultSize,
838 long style = wxSTATIC_BORDER,
839 const wxValidator& validator = wxDefaultValidator,
840 const wxString& name = _T("BitmapCtrl"));
843 virtual ~BitmapControl();
845 virtual wxSize GetBestSize() const;
847 wxBitmap* getBitmap()
848 { return m_pBitmap; }
850 void OnPaint(wxPaintEvent& event);
854 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
855 EVT_PAINT(BitmapControl::OnPaint)
858 IMPLEMENT_CLASS(BitmapControl, wxPanel)
861 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
862 long style, const wxValidator& validator, const wxString& name)
865 Create(parent, id, pos, size, style, name);
867 SetSize (GetBestSize());
871 BitmapControl::GetBestSize () const
874 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
879 BitmapControl::~BitmapControl()
883 BitmapControl::OnPaint (wxPaintEvent& event)
887 dc.DrawBitmap (*m_pBitmap, 0, 0);
891 class BitmapDialog : public wxDialog {
893 BitmapControl* m_pBitmapCtrl;
896 BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
897 virtual ~BitmapDialog();
900 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
901 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle,*wxConvCurrent), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE)
903 wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
905 pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
907 wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
908 wxButton* pButtonOk = new wxButton (this, wxID_OK, _T("Ok"));
909 pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
911 pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
913 SetAutoLayout (true);
914 SetSizer (pTopSizer);
915 pTopSizer->Fit (this);
916 pTopSizer->SetSizeHints (this);
919 BitmapDialog::~BitmapDialog()
924 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
926 DialogPreferences dlg (this, _T("CTSim Preferences"), theApp->getAdvancedOptions(),
927 theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(),
928 theApp->getUseBackgroundTasks());
929 if (dlg.ShowModal() == wxID_OK) {
930 theApp->setAdvancedOptions (dlg.getAdvancedOptions());
931 theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
932 theApp->setVerboseLogging (dlg.getVerboseLogging());
933 theApp->setStartupTips (dlg.getStartupTips());
934 theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
939 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
941 DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
942 if (dialogImport.ShowModal() != wxID_OK)
945 wxString strFormatName (dialogImport.getFormatName (), *wxConvCurrent);
946 m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.mb_str(wxConvUTF8));
949 wxString strWildcard;
950 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
952 strWildcard = _T("PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm");
955 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
957 strWildcard = _T("PNG Files (*.png)|*.png");
960 #ifdef HAVE_CTN_DICOM
961 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
963 strWildcard = _T("Dicom Files (*.*)|*.*");
970 wxString strFilename = wxFileSelector (wxString(wxConvUTF8.cMB2WX("Import Filename")), wxString(wxConvUTF8.cMB2WX("")),
971 wxString(wxConvUTF8.cMB2WX("")), strExt, strWildcard, wxFD_OPEN);
973 if (! strFilename.IsEmpty()) {
974 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM || m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
975 ImageFile* pIF = new ImageFile;
976 if (pIF->importImage (strFormatName.mb_str(wxConvUTF8), strFilename.mb_str(wxConvUTF8))) {
977 ImageFileDocument* pIFDoc = theApp->newImageDoc();
978 pIFDoc->setImageFile(pIF);
979 pIFDoc->getView()->getFrame()->Show(true);
980 std::ostringstream os;
981 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
982 pIF->labelAdd (os.str().c_str());
983 if (theApp->getAskDeleteNewDocs())
984 pIFDoc->Modify (true);
985 pIFDoc->UpdateAllViews();
986 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
987 pIFDoc->getView()->getFrame()->Show(true);
991 #ifdef HAVE_CTN_DICOM
992 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
993 DicomImporter dicomImport (strFilename.mb_str(wxConvUTF8));
994 if (dicomImport.fail()) {
995 ::wxMessageBox (wxConvUTF8.cMB2WX(dicomImport.failMessage().c_str()), _T("Import Error"));
996 } else if (dicomImport.testImage()) {
997 ImageFileDocument* pIFDoc = theApp->newImageDoc();
998 ImageFile* pIF = dicomImport.getImageFile();
999 pIFDoc->setImageFile (pIF);
1000 std::ostringstream os;
1001 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1002 pIF->labelAdd (os.str().c_str());
1003 if (theApp->getAskDeleteNewDocs())
1004 pIFDoc->Modify (true);
1005 pIFDoc->UpdateAllViews();
1006 pIFDoc->getView()->setInitialClientSize();
1008 } else if (dicomImport.testProjections()) {
1009 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
1010 Projections* pProj = dicomImport.getProjections();
1011 pProjDoc->setProjections (pProj);
1012 pProjDoc->getView()->getFrame()->Show(true);
1013 std::ostringstream os;
1014 os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1015 pProj->setRemark (os.str().c_str());
1016 if (theApp->getAskDeleteNewDocs())
1017 pProjDoc->Modify (true);
1018 pProjDoc->UpdateAllViews();
1019 pProjDoc->getView()->setInitialClientSize();
1020 pProjDoc->Activate();
1022 ::wxMessageBox (_T("Unrecognized DICOM file contents"), _T("Import Error"));
1026 sys_error (ERR_WARNING, "Unknown import format type");
1030 #include "./splash.xpm"
1032 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1034 wxString strOSDesc = ::wxGetOsDescription();
1035 *theApp->getLog() << _T("Operating System: ") << strOSDesc;
1036 *theApp->getLog() << _T(", wxWindows: ") << wxVERSION_STRING;
1038 *theApp->getLog() << _T(", Build Date: ") << wxConvUTF8.cMB2WX(_TIMESTAMP__);
1041 *theApp->getLog() << _T(", Debug version");
1043 *theApp->getLog() << _T(", Release version");
1046 *theApp->getLog() << _T(" ") << wxConvUTF8.cMB2WX(VERSION);
1047 #elif defined(CTSIMVERSION)
1048 *theApp->getLog() << _T(" ") << _T(CTSIMVERSION);
1050 *theApp->getLog() << _T("\n");
1052 wxBitmap bmp (splash);
1054 BitmapDialog dlg (&bmp, "About CTSim");
1057 wxString msg = _T("CTSim\nThe Open Source Computed Tomography Simulator\n");
1059 msg << _T("Version: ") << wxConvUTF8.cMB2WX(VERSION) << _T("\n\n");
1060 #elif defined(CTSIMVERSION)
1061 msg << _T("Version: ") << wxConvUTF8.cMB2WX(CTSIMVERSION) << _T("\n\n");
1063 msg += _T("Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]");
1065 wxMessageBox(msg, _T("About CTSim"), wxOK | wxICON_INFORMATION, this);
1066 *theApp->getLog() << msg << wxConvUTF8.cMB2WX("\n");
1071 // Create new documents
1074 CTSimApp::newDocumentHelper (wxDocTemplate* tmpl) {
1075 wxDocument* newDoc = tmpl->CreateDocument (_T(""));
1077 newDoc->SetDocumentTemplate (tmpl);
1078 newDoc->OnNewDocument();
1079 wxString fname = newDoc->GetFilename();
1081 fname += tmpl->GetDefaultExtension();
1082 newDoc->SetDocumentName(fname);
1083 newDoc->SetTitle(fname);
1084 newDoc->SetFilename(fname, true);
1090 ProjectionFileDocument*
1091 CTSimApp::newProjectionDoc()
1093 ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>
1094 (newDocumentHelper(m_pDocTemplProjection));
1096 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1101 CTSimApp::newImageDoc()
1103 ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>
1104 (newDocumentHelper(m_pDocTemplImage));
1106 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1112 CTSimApp::newPlotDoc()
1114 PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>
1115 (newDocumentHelper(m_pDocTemplPlot));
1117 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1124 CTSimApp::newTextDoc()
1126 TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>
1127 (newDocumentHelper(m_pDocTemplText));
1129 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1135 PhantomFileDocument*
1136 CTSimApp::newPhantomDoc()
1138 PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>
1139 (newDocumentHelper(m_pDocTemplPhantom));
1141 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());
1148 Graph3dFileDocument*
1149 CTSimApp::newGraph3dDoc()
1151 Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>
1152 (newDocumentHelper(m_pDocTemplGraph3d));
1154 newDoc->getView()->getFrame()->SetTitle(newDoc->GetDocumentName());