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-2001 Kevin Rosenberg
12 ** $Id: ctsim.cpp,v 1.106 2002/05/03 01:01:15 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 #define strdup _strdup
32 #include "wx/wxprec.h"
39 #include "wx/filesys.h"
40 #include "wx/fs_zip.h"
42 #include "wx/msw/helpchm.h"
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
57 #include "backgroundmgr.h"
59 #if defined(HAVE_CONFIG_H)
63 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
73 static const char* rcsindent = "$Id: ctsim.cpp,v 1.106 2002/05/03 01:01:15 kevin Exp $";
75 struct option CTSimApp::ctsimOptions[] =
77 {"help", 0, 0, O_HELP},
78 {"version", 0, 0, O_VERSION},
79 {"print", 0, 0, O_PRINT},
83 IMPLEMENT_APP(CTSimApp)
86 : m_bAdvancedOptions(false), m_bSetModifyNewDocs(true), m_bVerboseLogging(false), m_bShowStartupTips(true),
87 m_iCurrentTip(0), m_bUseBackgroundTasks(false),
88 m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0)
93 #ifdef HAVE_SYS_TIME_H
97 #ifdef HAVE_SYS_RESOURCE_H
98 #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;
114 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
120 std::cout << rcsindent << std::endl;
122 std::cout << "Version: CTSIMVERSION" << std::endl;
123 #elif defined(VERSION)
124 std::cout << "Version: VERSION" << std::endl;
140 m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
142 m_pDocTemplImage = new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
143 m_pDocTemplProjection = new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
144 m_pDocTemplPhantom = new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
145 m_pDocTemplPlot = new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
146 m_pDocTemplText = new wxDocTemplate (m_docManager, "TextFile", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
148 m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, "Graph3dFile", "*.g3d", "", "g3d", "Graph3dFile", "Graph3dView", CLASSINFO(Graph3dFileDocument), CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
152 wxImage::AddHandler(new wxGIFHandler); // Required for images in the online documentation
155 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
156 wxFileSystem::AddHandler(new wxZipFSHandler); // Required for advanced HTML help
159 // Create the main frame window
160 int xDisplay, yDisplay;
161 ::wxDisplaySize (&xDisplay, &yDisplay);
163 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0),
165 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .75)),
167 wxSize(nearest<int>(xDisplay * .25), nearest<int>(yDisplay * .25)),
169 wxDEFAULT_FRAME_STYLE);
171 setIconForFrame (m_pFrame);
172 m_pFrame->Centre(wxBOTH);
173 m_pFrame->Show(true);
174 SetTopWindow (m_pFrame);
177 m_docManager->FileHistoryLoad(*m_pConfig);
180 m_pLogDoc = newTextDoc();
182 m_pLog = m_pLogDoc->getTextCtrl();
183 m_pLogDoc->SetDocumentName("Log.txt");
184 m_pLogDoc->SetFilename("Log.txt");
185 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
187 m_pFrame->GetClientSize(&xSize, &ySize);
188 int yLogSize = ySize / 4;
189 m_pLogDoc->getView()->getFrame()->SetClientSize (0, ySize - yLogSize, xSize, yLogSize);
190 m_pLogDoc->getView()->getFrame()->Show (true);
193 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
195 wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
198 if (! m_pConfig->Read("HelpDir", &helpDir))
199 helpDir = ::wxGetCwd();
201 if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
202 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
204 if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
205 ! m_pFrame->getHtmlHelpController().Initialize("/usr/share/ctsim"))
206 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
208 if (::wxDirExists ("/tmp"))
209 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
210 m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
214 for (int i = optind + 1; i <= argc; i++) {
215 wxString filename = argv [i - 1];
216 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
218 wxView* pNewView = pNewDoc->GetFirstView();
219 wxPrintout *printout = pNewView->OnCreatePrintout();
222 printer.Print(pNewView->GetFrame(), printout, TRUE);
225 wxCommandEvent nullEvent;
226 nullEvent.SetId (wxID_CLOSE);
227 m_docManager->OnFileClose (nullEvent);
231 wxCommandEvent closeEvent;
232 closeEvent.SetInt (MAINMENU_FILE_EXIT);
233 m_pFrame->AddPendingEvent(closeEvent);
236 if (getStartupTips())
239 #ifdef HAVE_WXTHREADS
240 m_pBackgroundMgr = new BackgroundManager;
249 CTSimTipProvider tipProvider (m_iCurrentTip);
250 setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
251 m_iCurrentTip = tipProvider.GetCurrentTip();
255 #include "./ctsim.xpm"
257 CTSimApp::setIconForFrame(wxFrame* pFrame)
259 wxIcon iconApp (ctsim16_xpm);
262 pFrame->SetIcon (iconApp);
266 CTSimApp::usage(const char* program)
268 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
269 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
271 std::cout << " --version Display version\n";
272 std::cout << " --help Display this help message\n";
287 CTSimApp::openConfig()
290 m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
292 m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
295 wxConfigBase::Set(m_pConfig);
296 m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
297 m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
298 m_pConfig->Read ("VerboseLogging", &m_bVerboseLogging);
299 m_pConfig->Read ("StartupTips", &m_bShowStartupTips);
300 m_pConfig->Read ("CurrentTip", &m_iCurrentTip);
301 m_pConfig->Read ("UseBackgroundTasks", &m_bUseBackgroundTasks);
303 wxString strFftwWisdom;
304 m_pConfig->Read ("FftwWisdom", strFftwWisdom);
305 if (strFftwWisdom.size() > 0)
306 fftw_import_wisdom_from_string (strFftwWisdom.c_str());
311 CTSimApp::closeConfig()
313 m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
314 m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
315 m_pConfig->Write ("VerboseLogging", m_bVerboseLogging);
316 m_pConfig->Write ("StartupTips", m_bShowStartupTips);
317 m_pConfig->Write ("CurrentTip", m_iCurrentTip);
318 m_pConfig->Write ("UseBackgroundTasks", m_bUseBackgroundTasks);
320 const char* const pszWisdom = fftw_export_wisdom_to_string();
321 wxString strFftwWisdom (pszWisdom);
322 fftw_free ((void*) pszWisdom);
323 m_pConfig->Write ("FftwWisdom", strFftwWisdom);
331 CTSimApp::getUntitledFilename()
333 static int untitledNumber = 1;
335 wxString filename ("Untitled");
336 filename << untitledNumber++;
342 // Top-level window for CTSim
345 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
347 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
349 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
351 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
354 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
355 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
356 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
357 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
358 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
359 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
360 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
361 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
362 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
363 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
364 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
365 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
366 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
367 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
368 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
369 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
370 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
371 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
372 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
373 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
375 EVT_SIZE(MainFrame::OnSize)
377 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
378 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
380 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
381 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
382 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
383 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
384 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
385 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
386 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
387 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
388 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
389 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
390 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
391 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
392 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
393 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
394 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
395 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
396 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
397 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
398 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
399 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
400 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
401 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
402 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
403 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
404 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
410 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
411 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
413 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
414 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
417 m_bShuttingDown = false;
420 wxMenu *file_menu = new wxMenu;
422 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
423 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
424 file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
426 file_menu->AppendSeparator();
427 file_menu->Append (MAINMENU_IMPORT, "&Import...\tCtrl-M");
428 file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
429 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
431 // history of files visited
432 theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
433 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
436 m_pWindowMenu = new wxMenu;
437 m_pWindowMenu->UpdateUI (this);
440 wxMenu* help_menu = new wxMenu;
441 help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
442 help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
443 help_menu->Append (IDH_QUICKSTART, "&Quick Start");
444 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
445 help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
447 help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
449 wxMenuBar* menu_bar = new wxMenuBar;
451 menu_bar->Append(file_menu, "&File");
453 menu_bar->Append(m_pWindowMenu, "&Window");
455 menu_bar->Append(help_menu, "&Help");
457 SetMenuBar(menu_bar);
462 for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
463 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
464 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
465 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
469 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
470 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
471 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
472 m_iDefaultFilterXSize = 256;
473 m_iDefaultFilterYSize = 256;
474 m_dDefaultFilterParam = 1.;
475 m_dDefaultFilterBandwidth = 1.;
476 m_dDefaultFilterInputScale = 1.;
477 m_dDefaultFilterOutputScale = 1.;
478 m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PNG;
480 wxAcceleratorEntry accelEntries[15];
481 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
482 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
483 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
484 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
485 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
487 for (i = 0; i < 10; i++)
488 accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
489 wxAcceleratorTable accelTable (15, accelEntries);
491 wxAcceleratorTable accelTable (5, accelEntries);
494 SetAcceleratorTable (accelTable);
497 MainFrame::~MainFrame()
499 m_bShuttingDown = true; // Currently used so that Log Window will close
501 // delete all non-modified documents
502 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
503 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
504 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
505 if (pDoc && ! pDoc->IsModified()) {
506 theApp->getDocManager()->RemoveDocument(pDoc);
512 if (theApp->getConfig())
513 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
515 delete theApp->getDocManager();
520 MainFrame::OnSize (wxSizeEvent& event)
523 if (theApp->getLogDoc()) {
525 GetClientSize(&xSize, &ySize);
526 int yLogSize = ySize / 4;
527 theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
528 theApp->getLogDoc()->getView()->getFrame()->Show (true);
533 wxDocMDIParentFrame::OnSize (event);
535 wxDocParentFrame::OnSize (event);
540 MainFrame::OnCreatePhantom(wxCommandEvent& event)
542 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
543 int dialogReturn = dialogPhantom.ShowModal();
544 if (dialogReturn == wxID_OK) {
545 wxString selection (dialogPhantom.getPhantom());
546 if (theApp->getVerboseLogging())
547 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
548 wxString filename = selection + ".phm";
549 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
550 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
556 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
558 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
559 int dialogReturn = dialogFilter.ShowModal();
560 if (dialogReturn == wxID_OK) {
561 wxString strFilter (dialogFilter.getFilterName());
562 wxString strDomain (dialogFilter.getDomainName());
563 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
564 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
565 m_iDefaultFilterXSize = dialogFilter.getXSize();
566 m_iDefaultFilterYSize = dialogFilter.getYSize();
567 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
568 m_dDefaultFilterParam= dialogFilter.getFilterParam();
569 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
570 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
571 std::ostringstream os;
572 os << "Generate Filter=" << strFilter.c_str()
573 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
574 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
575 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
576 *theApp->getLog() << os.str().c_str() << "\n";
577 wxString filename = "untitled.if";
578 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
579 pFilterDoc->setBadFileOpen();
581 sys_error (ERR_SEVERE, "Unable to create filter image");
584 ImageFile& rIF = pFilterDoc->getImageFile();
585 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
586 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
587 rIF.labelAdd (os.str().c_str());
588 if (theApp->getAskDeleteNewDocs())
589 pFilterDoc->Modify (true);
590 pFilterDoc->UpdateAllViews();
591 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
592 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
593 pFilterDoc->getView()->getFrame()->Show(true);
598 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
600 const ImageFile& rIF = pIFDoc->getImageFile();
601 unsigned int nx = rIF.nx();
602 unsigned int ny = rIF.ny();
603 wxList& rListDocs = m_docManager->GetDocuments();
604 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
605 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
606 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
607 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
608 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
609 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
610 vecIF.push_back (pIFCompareDoc);
617 MainFrame::OnNewImageFile (wxCommandEvent& event)
619 ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
621 ImageFileDocument* pImageDoc = theApp->newImageDoc();
623 sys_error (ERR_SEVERE, "Unable to create image file");
626 pImageDoc->setImageFile (pImageFile);
627 // pImageDoc->UpdateAllViews (NULL);
628 // if (ImageFileView* imageView = pImageDoc->getView()) {
629 // imageView->OnUpdate (imageView, NULL);
630 // imageView->getFrame()->SetFocus();
631 // imageView->getFrame()->Show(true);
633 if (theApp->getAskDeleteNewDocs())
634 pImageDoc->Modify (true);
638 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
640 Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
641 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
643 sys_error (ERR_SEVERE, "Unable to create projection file");
646 pProjDoc->setProjections (pProjections);
647 pProjDoc->UpdateAllViews (NULL);
648 if (ProjectionFileView* projView = pProjDoc->getView()) {
649 projView->OnUpdate (projView, NULL);
650 projView->getFrame()->SetFocus();
651 projView->getFrame()->Show(true);
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.DisplayContents();
691 MainFrame::showHelp (int commandID)
695 case MAINMENU_HELP_CONTENTS:
697 m_winHelp.DisplayContents ();
699 m_htmlHelp.DisplayContents ();
706 m_winHelp.DisplaySection (commandID);
708 m_htmlHelp.Display (commandID);
715 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
721 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
725 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
726 wxNode* pNode = rListDocs.GetFirst();
727 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
728 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
729 wxString strFilename = pDoc->GetFilename();
731 strFilename += "\tCtrl-";
732 strFilename += static_cast<char>('0' + iPos);
734 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
735 m_apWindowMenuData[iPos] = pDoc;
736 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
738 pNode = pNode->GetNext();
740 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
741 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
742 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
743 m_apWindowMenuData[i] = NULL;
750 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
752 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
753 wxString strFilename = pDoc->GetFilename();
754 const wxView* pView = pDoc->GetFirstView();
756 wxFrame* pFrame = pView->GetFrame();
763 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
764 { DoWindowMenu (0, event); }
766 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
767 { DoWindowMenu (1, event); }
769 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
770 { DoWindowMenu (2, event); }
772 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
773 { DoWindowMenu (3, event); }
775 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
776 { DoWindowMenu (4, event); }
778 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
779 { DoWindowMenu (5, event); }
781 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
782 { DoWindowMenu (6, event); }
784 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
785 { DoWindowMenu (7, event); }
787 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
788 { DoWindowMenu (8, event); }
790 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
791 { DoWindowMenu (9, event); }
793 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
794 { DoWindowMenu (10, event); }
796 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
797 { DoWindowMenu (11, event); }
799 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
800 { DoWindowMenu (12, event); }
802 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
803 { DoWindowMenu (13, event); }
805 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
806 { DoWindowMenu (14, event); }
808 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
809 { DoWindowMenu (15, event); }
811 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
812 { DoWindowMenu (16, event); }
814 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
815 { DoWindowMenu (17, event); }
817 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
818 { DoWindowMenu (18, event); }
820 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
821 { DoWindowMenu (19, event); }
824 class BitmapControl : public wxPanel
827 DECLARE_DYNAMIC_CLASS (BitmapControl)
828 DECLARE_EVENT_TABLE ()
832 BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
833 const wxPoint& pos = wxDefaultPosition,
834 const wxSize& size = wxDefaultSize,
835 long style = wxSTATIC_BORDER,
836 const wxValidator& validator = wxDefaultValidator,
837 const wxString& name = "BitmapCtrl");
840 virtual ~BitmapControl();
842 virtual wxSize GetBestSize() const;
844 wxBitmap* getBitmap()
845 { return m_pBitmap; }
847 void OnPaint(wxPaintEvent& event);
851 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
852 EVT_PAINT(BitmapControl::OnPaint)
855 IMPLEMENT_CLASS(BitmapControl, wxPanel)
858 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
859 long style, const wxValidator& validator, const wxString& name)
862 Create(parent, id, pos, size, style, name);
864 SetSize (GetBestSize());
868 BitmapControl::GetBestSize () const
871 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
876 BitmapControl::~BitmapControl()
880 BitmapControl::OnPaint (wxPaintEvent& event)
884 dc.DrawBitmap (*m_pBitmap, 0, 0);
888 class BitmapDialog : public wxDialog {
890 BitmapControl* m_pBitmapCtrl;
893 BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
894 virtual ~BitmapDialog();
897 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
898 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
900 wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
902 pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
904 wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
905 wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
906 pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
908 pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
910 SetAutoLayout (true);
911 SetSizer (pTopSizer);
912 pTopSizer->Fit (this);
913 pTopSizer->SetSizeHints (this);
916 BitmapDialog::~BitmapDialog()
921 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
923 DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(),
924 theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(),
925 theApp->getUseBackgroundTasks());
926 if (dlg.ShowModal() == wxID_OK) {
927 theApp->setAdvancedOptions (dlg.getAdvancedOptions());
928 theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
929 theApp->setVerboseLogging (dlg.getVerboseLogging());
930 theApp->setStartupTips (dlg.getStartupTips());
931 theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
936 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
938 DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
939 if (dialogImport.ShowModal() != wxID_OK)
942 wxString strFormatName (dialogImport.getFormatName ());
943 m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.c_str());
946 wxString strWildcard;
947 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
949 strWildcard = "PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm";
952 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
954 strWildcard = "PNG Files (*.png)|*.png";
957 #ifdef HAVE_CTN_DICOM
958 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
960 strWildcard = "Dicom Files (*.*)|*.*";
967 wxString strFilename = wxFileSelector (wxString("Import Filename"), wxString(""),
968 wxString(""), strExt, strWildcard, wxHIDE_READONLY | wxOPEN);
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.c_str(), strFilename.c_str())) {
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.c_str());
990 if (dicomImport.fail()) {
991 ::wxMessageBox (dicomImport.failMessage().c_str(), "Import Error");
992 } else if (dicomImport.testImage()) {
993 ImageFileDocument* pIFDoc = theApp->newImageDoc();
994 ImageFile* pIF = dicomImport.getImageFile();
995 pIFDoc->setImageFile (pIF);
996 pIFDoc->getView()->getFrame()->Show(true);
997 std::ostringstream os;
998 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
999 pIF->labelAdd (os.str().c_str());
1000 if (theApp->getAskDeleteNewDocs())
1001 pIFDoc->Modify (true);
1002 pIFDoc->UpdateAllViews();
1003 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
1004 pIFDoc->getView()->getFrame()->Show(true);
1005 } else if (dicomImport.testProjections()) {
1006 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
1007 Projections* pProj = dicomImport.getProjections();
1008 pProjDoc->setProjections (pProj);
1009 pProjDoc->getView()->getFrame()->Show(true);
1010 std::ostringstream os;
1011 os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1012 pProj->setRemark (os.str().c_str());
1013 if (theApp->getAskDeleteNewDocs())
1014 pProjDoc->Modify (true);
1015 pProjDoc->UpdateAllViews();
1016 pProjDoc->GetFirstView()->OnUpdate (NULL, NULL);
1017 pProjDoc->getView()->getFrame()->Show(true);
1019 ::wxMessageBox ("Unrecognized DICOM file contents", "Import Error");
1023 sys_error (ERR_WARNING, "Unknown import format type");
1027 #include "./splash.xpm"
1029 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1031 long lFreeMem = ::wxGetFreeMemory() / (1024L * 1024L);
1032 wxString strOSDesc = ::wxGetOsDescription();
1033 *theApp->getLog() << "Operating System: " << strOSDesc;
1035 *theApp->getLog() << ", Free Memory: " << lFreeMem << " MB";
1036 *theApp->getLog() << ", wxWindows: " << wxVERSION_STRING;
1037 #ifdef __TIMESTAMP__
1038 *theApp->getLog() << ", Build Date: " << __TIMESTAMP__;
1041 *theApp->getLog() << ", CTSim Debug version";
1043 *theApp->getLog() << ", CTSim Release version";
1046 *theApp->getLog() << "\n";
1048 wxBitmap bmp (splash);
1050 BitmapDialog dlg (&bmp, "About CTSim");
1053 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
1056 msg += CTSIMVERSION;
1058 #elif defined(VERSION)
1059 msg << "Version: " << VERSION << "\n\n";
1061 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
1063 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
1064 *theApp->getLog() << msg << "\n";
1069 ProjectionFileDocument*
1070 CTSimApp::newProjectionDoc()
1072 ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
1074 ProjectionFileView* pView = newDoc->getView();
1076 wxFrame* pFrame = pView->getFrame();
1079 // m_pFrame->GetClientSize (&size.x, &size.y);
1080 // pFrame->SetClientSize (size.x / 2, size.y / 2);
1081 pFrame->Show (false);
1084 newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
1085 newDoc->SetDocumentTemplate (m_pDocTemplProjection);
1086 newDoc->OnNewDocument();
1093 CTSimApp::newImageDoc()
1095 ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
1097 newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
1098 newDoc->SetDocumentTemplate (m_pDocTemplImage);
1099 newDoc->OnNewDocument();
1106 CTSimApp::newPlotDoc()
1108 PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
1110 newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
1111 newDoc->SetDocumentTemplate (m_pDocTemplPlot);
1112 newDoc->OnNewDocument();
1120 CTSimApp::newTextDoc()
1122 wxString strFilename (getUntitledFilename());
1123 strFilename += ".txt";
1125 TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
1127 newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
1128 newDoc->SetDocumentTemplate (m_pDocTemplText);
1129 newDoc->OnNewDocument();
1136 PhantomFileDocument*
1137 CTSimApp::newPhantomDoc()
1139 PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
1141 newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1142 newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1143 newDoc->OnNewDocument();
1151 Graph3dFileDocument*
1152 CTSimApp::newGraph3dDoc()
1154 Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
1156 newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1157 newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1158 newDoc->OnNewDocument();