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.98 2001/03/07 16:34:47 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.98 2001/03/07 16:34:47 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);
162 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0),
163 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .755)), wxDEFAULT_FRAME_STYLE);
165 setIconForFrame (m_pFrame);
166 m_pFrame->Centre(wxBOTH);
167 m_pFrame->Show(true);
168 SetTopWindow (m_pFrame);
171 m_docManager->FileHistoryLoad(*m_pConfig);
174 m_pLogDoc = newTextDoc();
176 m_pLog = m_pLogDoc->getTextCtrl();
177 m_pLogDoc->SetDocumentName("Log.txt");
178 m_pLogDoc->SetFilename("Log.txt");
179 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
181 m_pFrame->GetClientSize(&xSize, &ySize);
182 int yLogSize = ySize / 4;
183 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
184 m_pLogDoc->getView()->getFrame()->Show (true);
187 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
189 wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
192 if (! m_pConfig->Read("HelpDir", &helpDir))
193 helpDir = ::wxGetCwd();
195 if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
196 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
198 if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
199 ! m_pFrame->getHtmlHelpController().Initialize("/usr/local/man/ctsim"))
200 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
202 if (::wxDirExists ("/tmp"))
203 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
204 m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
208 for (int i = optind + 1; i <= argc; i++) {
209 wxString filename = argv [i - 1];
210 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
212 wxView* pNewView = pNewDoc->GetFirstView();
213 wxPrintout *printout = pNewView->OnCreatePrintout();
216 printer.Print(pNewView->GetFrame(), printout, TRUE);
219 wxCommandEvent nullEvent;
220 nullEvent.SetId (wxID_CLOSE);
221 m_docManager->OnFileClose (nullEvent);
225 wxCommandEvent closeEvent;
226 closeEvent.SetInt (MAINMENU_FILE_EXIT);
227 m_pFrame->AddPendingEvent(closeEvent);
230 if (getStartupTips())
233 #ifdef HAVE_WXTHREADS
234 m_pBackgroundMgr = new BackgroundManager;
243 CTSimTipProvider tipProvider (m_iCurrentTip);
244 setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
245 m_iCurrentTip = tipProvider.GetCurrentTip();
249 #include "./ctsim.xpm"
251 CTSimApp::setIconForFrame(wxFrame* pFrame)
253 wxIcon iconApp (ctsim16_xpm);
256 pFrame->SetIcon (iconApp);
260 CTSimApp::usage(const char* program)
262 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
263 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
265 std::cout << " --version Display version\n";
266 std::cout << " --help Display this help message\n";
281 CTSimApp::openConfig()
284 m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
286 m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
289 wxConfigBase::Set(m_pConfig);
290 m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
291 m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
292 m_pConfig->Read ("VerboseLogging", &m_bVerboseLogging);
293 m_pConfig->Read ("StartupTips", &m_bShowStartupTips);
294 m_pConfig->Read ("CurrentTip", &m_iCurrentTip);
295 m_pConfig->Read ("UseBackgroundTasks", &m_bUseBackgroundTasks);
299 CTSimApp::closeConfig()
301 m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
302 m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
303 m_pConfig->Write ("VerboseLogging", m_bVerboseLogging);
304 m_pConfig->Write ("StartupTips", m_bShowStartupTips);
305 m_pConfig->Write ("CurrentTip", m_iCurrentTip);
306 m_pConfig->Write ("UseBackgroundTasks", m_bUseBackgroundTasks);
313 CTSimApp::getUntitledFilename()
315 static int untitledNumber = 1;
317 wxString filename ("Untitled");
318 filename << untitledNumber++;
324 // Top-level window for CTSim
327 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
329 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
331 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
333 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
336 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
337 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
338 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
339 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
340 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
341 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
342 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
343 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
344 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
345 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
346 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
347 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
348 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
349 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
350 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
351 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
352 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
353 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
354 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
355 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
357 EVT_SIZE(MainFrame::OnSize)
359 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
360 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
362 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
363 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
364 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
365 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
366 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
367 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
368 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
369 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
370 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
371 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
372 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
373 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
374 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
375 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
376 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
377 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
378 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
379 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
380 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
381 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
382 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
383 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
384 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
385 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
386 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
392 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
393 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
395 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
396 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
399 m_bShuttingDown = false;
402 wxMenu *file_menu = new wxMenu;
404 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
405 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
406 file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
408 file_menu->AppendSeparator();
409 file_menu->Append (MAINMENU_IMPORT, "&Import...\tCtrl-M");
410 file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
411 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
413 // history of files visited
414 theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
415 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
418 m_pWindowMenu = new wxMenu;
419 m_pWindowMenu->UpdateUI (this);
422 wxMenu* help_menu = new wxMenu;
423 help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
424 help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
425 help_menu->Append (IDH_QUICKSTART, "&Quick Start");
426 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
427 help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
429 help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
431 wxMenuBar* menu_bar = new wxMenuBar;
433 menu_bar->Append(file_menu, "&File");
435 menu_bar->Append(m_pWindowMenu, "&Window");
437 menu_bar->Append(help_menu, "&Help");
439 SetMenuBar(menu_bar);
444 for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
445 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
446 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
447 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
451 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
452 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
453 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
454 m_iDefaultFilterXSize = 256;
455 m_iDefaultFilterYSize = 256;
456 m_dDefaultFilterParam = 1.;
457 m_dDefaultFilterBandwidth = 1.;
458 m_dDefaultFilterInputScale = 1.;
459 m_dDefaultFilterOutputScale = 1.;
460 m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PNG;
462 wxAcceleratorEntry accelEntries[15];
463 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
464 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
465 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
466 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
467 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
469 for (i = 0; i < 10; i++)
470 accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
471 wxAcceleratorTable accelTable (15, accelEntries);
473 wxAcceleratorTable accelTable (5, accelEntries);
476 SetAcceleratorTable (accelTable);
479 MainFrame::~MainFrame()
481 m_bShuttingDown = true; // Currently used so that Log Window will close
483 // delete all non-modified documents
484 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
485 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
486 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
487 if (pDoc && ! pDoc->IsModified()) {
488 theApp->getDocManager()->RemoveDocument(pDoc);
494 if (theApp->getConfig())
495 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
497 delete theApp->getDocManager();
502 MainFrame::OnSize (wxSizeEvent& event)
505 if (theApp->getLogDoc()) {
507 GetClientSize(&xSize, &ySize);
508 int yLogSize = ySize / 4;
509 theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
510 theApp->getLogDoc()->getView()->getFrame()->Show (true);
515 wxDocMDIParentFrame::OnSize (event);
517 wxDocParentFrame::OnSize (event);
522 MainFrame::OnCreatePhantom(wxCommandEvent& event)
524 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
525 int dialogReturn = dialogPhantom.ShowModal();
526 if (dialogReturn == wxID_OK) {
527 wxString selection (dialogPhantom.getPhantom());
528 if (theApp->getVerboseLogging())
529 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
530 wxString filename = selection + ".phm";
531 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
532 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
538 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
540 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
541 int dialogReturn = dialogFilter.ShowModal();
542 if (dialogReturn == wxID_OK) {
543 wxString strFilter (dialogFilter.getFilterName());
544 wxString strDomain (dialogFilter.getDomainName());
545 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
546 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
547 m_iDefaultFilterXSize = dialogFilter.getXSize();
548 m_iDefaultFilterYSize = dialogFilter.getYSize();
549 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
550 m_dDefaultFilterParam= dialogFilter.getFilterParam();
551 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
552 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
553 std::ostringstream os;
554 os << "Generate Filter=" << strFilter.c_str()
555 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
556 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
557 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
558 *theApp->getLog() << os.str().c_str() << "\n";
559 wxString filename = "untitled.if";
560 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
561 pFilterDoc->setBadFileOpen();
563 sys_error (ERR_SEVERE, "Unable to create filter image");
566 ImageFile& rIF = pFilterDoc->getImageFile();
567 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
568 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
569 rIF.labelAdd (os.str().c_str());
570 if (theApp->getAskDeleteNewDocs())
571 pFilterDoc->Modify (true);
572 pFilterDoc->UpdateAllViews();
573 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
574 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
575 pFilterDoc->getView()->getFrame()->Show(true);
580 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
582 const ImageFile& rIF = pIFDoc->getImageFile();
583 unsigned int nx = rIF.nx();
584 unsigned int ny = rIF.ny();
585 wxList& rListDocs = m_docManager->GetDocuments();
586 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
587 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
588 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
589 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
590 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
591 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
592 vecIF.push_back (pIFCompareDoc);
599 MainFrame::OnNewImageFile (wxCommandEvent& event)
601 ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
603 ImageFileDocument* pImageDoc = theApp->newImageDoc();
605 sys_error (ERR_SEVERE, "Unable to create image file");
608 pImageDoc->setImageFile (pImageFile);
609 pImageDoc->UpdateAllViews (NULL);
610 if (ImageFileView* imageView = pImageDoc->getView()) {
611 imageView->OnUpdate (imageView, NULL);
612 imageView->getFrame()->SetFocus();
613 imageView->getFrame()->Show(true);
615 if (theApp->getAskDeleteNewDocs())
616 pImageDoc->Modify (true);
621 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
623 Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
624 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
626 sys_error (ERR_SEVERE, "Unable to create projection file");
629 pProjDoc->setProjections (pProjections);
630 pProjDoc->UpdateAllViews (NULL);
631 if (ProjectionFileView* projView = pProjDoc->getView()) {
632 projView->OnUpdate (projView, NULL);
633 projView->getFrame()->SetFocus();
634 projView->getFrame()->Show(true);
637 if (theApp->getAskDeleteNewDocs())
638 pProjDoc->Modify (true);
642 MainFrame::OnLogEvent (wxCommandEvent& event)
644 *theApp->getLog() << event.GetString();
648 MainFrame::OnHelpTips (wxCommandEvent& event)
654 MainFrame::OnHelpContents (wxCommandEvent& event)
656 showHelp (event.GetId());
660 MainFrame::OnHelpButton (wxCommandEvent& event)
662 showHelp (event.GetId());
665 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
667 MainFrame::OnHelpSecondary (wxCommandEvent& event)
669 m_htmlHelp.DisplayContents();
674 MainFrame::showHelp (int commandID)
678 case MAINMENU_HELP_CONTENTS:
680 m_winHelp.DisplayContents ();
682 m_htmlHelp.DisplayContents ();
689 m_winHelp.DisplaySection (commandID);
691 m_htmlHelp.Display (commandID);
698 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
704 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
708 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
709 wxNode* pNode = rListDocs.GetFirst();
710 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
711 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
712 wxString strFilename = pDoc->GetFilename();
714 strFilename += "\tCtrl-";
715 strFilename += static_cast<char>('0' + iPos);
717 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
718 m_apWindowMenuData[iPos] = pDoc;
719 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
721 pNode = pNode->GetNext();
723 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
724 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
725 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
726 m_apWindowMenuData[i] = NULL;
731 #ifdef CTSIM_CUSTOM_MRU
733 MainFrame::OnMRUFile (wxCommandEvent& event)
735 wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
737 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
742 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
744 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
745 wxString strFilename = pDoc->GetFilename();
746 const wxView* pView = pDoc->GetFirstView();
748 wxFrame* pFrame = pView->GetFrame();
755 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
756 { DoWindowMenu (0, event); }
758 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
759 { DoWindowMenu (1, event); }
761 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
762 { DoWindowMenu (2, event); }
764 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
765 { DoWindowMenu (3, event); }
767 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
768 { DoWindowMenu (4, event); }
770 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
771 { DoWindowMenu (5, event); }
773 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
774 { DoWindowMenu (6, event); }
776 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
777 { DoWindowMenu (7, event); }
779 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
780 { DoWindowMenu (8, event); }
782 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
783 { DoWindowMenu (9, event); }
785 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
786 { DoWindowMenu (10, event); }
788 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
789 { DoWindowMenu (11, event); }
791 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
792 { DoWindowMenu (12, event); }
794 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
795 { DoWindowMenu (13, event); }
797 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
798 { DoWindowMenu (14, event); }
800 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
801 { DoWindowMenu (15, event); }
803 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
804 { DoWindowMenu (16, event); }
806 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
807 { DoWindowMenu (17, event); }
809 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
810 { DoWindowMenu (18, event); }
812 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
813 { DoWindowMenu (19, event); }
816 class BitmapControl : public wxPanel
819 DECLARE_DYNAMIC_CLASS (BitmapControl)
820 DECLARE_EVENT_TABLE ()
824 BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
825 const wxPoint& pos = wxDefaultPosition,
826 const wxSize& size = wxDefaultSize,
827 long style = wxSTATIC_BORDER,
828 const wxValidator& validator = wxDefaultValidator,
829 const wxString& name = "BitmapCtrl");
832 virtual ~BitmapControl();
834 virtual wxSize GetBestSize() const;
836 wxBitmap* getBitmap()
837 { return m_pBitmap; }
839 void OnPaint(wxPaintEvent& event);
843 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
844 EVT_PAINT(BitmapControl::OnPaint)
847 IMPLEMENT_CLASS(BitmapControl, wxPanel)
850 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
851 long style, const wxValidator& validator, const wxString& name)
854 Create(parent, id, pos, size, style, name);
856 SetSize (GetBestSize());
860 BitmapControl::GetBestSize () const
863 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
868 BitmapControl::~BitmapControl()
872 BitmapControl::OnPaint (wxPaintEvent& event)
876 dc.DrawBitmap (*m_pBitmap, 0, 0);
880 class BitmapDialog : public wxDialog {
882 BitmapControl* m_pBitmapCtrl;
885 BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
886 virtual ~BitmapDialog();
889 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
890 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
892 wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
894 pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
896 wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
897 wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
898 pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
900 pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
902 SetAutoLayout (true);
903 SetSizer (pTopSizer);
904 pTopSizer->Fit (this);
905 pTopSizer->SetSizeHints (this);
908 BitmapDialog::~BitmapDialog()
913 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
915 DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(),
916 theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(),
917 theApp->getUseBackgroundTasks());
918 if (dlg.ShowModal() == wxID_OK) {
919 theApp->setAdvancedOptions (dlg.getAdvancedOptions());
920 theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
921 theApp->setVerboseLogging (dlg.getVerboseLogging());
922 theApp->setStartupTips (dlg.getStartupTips());
923 theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
928 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
930 DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
931 if (dialogImport.ShowModal() != wxID_OK)
934 wxString strFormatName (dialogImport.getFormatName ());
935 m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.c_str());
938 wxString strWildcard;
939 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
941 strWildcard = "PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm";
944 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
946 strWildcard = "PNG Files (*.png)|*.png";
949 #ifdef HAVE_CTN_DICOM
950 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
952 strWildcard = "Dicom Files (*.*)|*.*";
959 wxString strFilename = wxFileSelector (wxString("Import Filename"), wxString(""),
960 wxString(""), strExt, strWildcard, wxHIDE_READONLY | wxOPEN);
961 if (! strFilename.IsEmpty()) {
962 if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM || m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
963 ImageFile* pIF = new ImageFile;
964 if (pIF->importImage (strFormatName.c_str(), strFilename.c_str())) {
965 ImageFileDocument* pIFDoc = theApp->newImageDoc();
966 pIFDoc->setImageFile(pIF);
967 pIFDoc->getView()->getFrame()->Show(true);
968 std::ostringstream os;
969 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
970 pIF->labelAdd (os.str().c_str());
971 if (theApp->getAskDeleteNewDocs())
972 pIFDoc->Modify (true);
973 pIFDoc->UpdateAllViews();
974 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
975 pIFDoc->getView()->getFrame()->Show(true);
979 #ifdef HAVE_CTN_DICOM
980 else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
981 DicomImporter dicomImport (strFilename.c_str());
982 if (dicomImport.fail()) {
983 ::wxMessageBox (dicomImport.failMessage().c_str(), "Import Error");
984 } else if (dicomImport.testImage()) {
985 ImageFileDocument* pIFDoc = theApp->newImageDoc();
986 ImageFile* pIF = dicomImport.getImageFile();
987 pIFDoc->setImageFile (pIF);
988 pIFDoc->getView()->getFrame()->Show(true);
989 std::ostringstream os;
990 os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
991 pIF->labelAdd (os.str().c_str());
992 if (theApp->getAskDeleteNewDocs())
993 pIFDoc->Modify (true);
994 pIFDoc->UpdateAllViews();
995 pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
996 pIFDoc->getView()->getFrame()->Show(true);
997 } else if (dicomImport.testProjections()) {
998 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
999 Projections* pProj = dicomImport.getProjections();
1000 pProjDoc->setProjections (pProj);
1001 pProjDoc->getView()->getFrame()->Show(true);
1002 std::ostringstream os;
1003 os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1004 pProj->setRemark (os.str().c_str());
1005 if (theApp->getAskDeleteNewDocs())
1006 pProjDoc->Modify (true);
1007 pProjDoc->UpdateAllViews();
1008 pProjDoc->GetFirstView()->OnUpdate (NULL, NULL);
1009 pProjDoc->getView()->getFrame()->Show(true);
1011 ::wxMessageBox ("Unrecognized DICOM file contents", "Import Error");
1015 sys_error (ERR_WARNING, "Unknown import format type");
1019 #include "./splash.xpm"
1021 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1023 long lFreeMem = ::wxGetFreeMemory() / (1024L * 1024L);
1024 wxString strOSDesc = ::wxGetOsDescription();
1025 *theApp->getLog() << "Operation System: " << strOSDesc;
1027 *theApp->getLog() << ", Free Memory: " << lFreeMem << " MB";
1028 *theApp->getLog() << "\n";
1030 wxBitmap bmp (splash);
1032 BitmapDialog dlg (&bmp, "About CTSim");
1035 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
1038 msg += CTSIMVERSION;
1040 #elif defined(VERSION)
1041 msg << "Version: " << VERSION << "\n\n";
1043 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
1045 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
1050 ProjectionFileDocument*
1051 CTSimApp::newProjectionDoc()
1053 ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
1055 ProjectionFileView* pView = newDoc->getView();
1057 wxFrame* pFrame = pView->getFrame();
1059 pFrame->SetSize (0,0);
1060 pFrame->Show (false);
1063 newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
1064 newDoc->SetDocumentTemplate (m_pDocTemplProjection);
1065 newDoc->OnNewDocument();
1072 CTSimApp::newImageDoc()
1074 ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
1076 ImageFileView* pView = newDoc->getView();
1078 wxFrame* pFrame = pView->getFrame();
1080 pFrame->SetSize (0,0);
1081 pFrame->Show (false);
1084 newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
1085 newDoc->SetDocumentTemplate (m_pDocTemplImage);
1086 newDoc->OnNewDocument();
1093 CTSimApp::newPlotDoc()
1095 PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
1097 PlotFileView* pView = newDoc->getView();
1099 wxFrame* pFrame = pView->getFrame();
1102 m_pFrame->GetClientSize (&size.x, &size.y);
1103 pFrame->SetSize (size.x / 2, size.y / 2);
1104 pFrame->Show (false);
1107 newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
1108 newDoc->SetDocumentTemplate (m_pDocTemplPlot);
1109 newDoc->OnNewDocument();
1117 CTSimApp::newTextDoc()
1119 wxString strFilename (getUntitledFilename());
1120 strFilename += ".txt";
1122 TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
1124 TextFileView* pView = newDoc->getView();
1126 wxFrame* pFrame = pView->getFrame();
1129 m_pFrame->GetClientSize (&size.x, &size.y);;
1130 pFrame->SetSize (size.x / 2, size.y / 2);
1131 pFrame->Show (false);
1134 newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
1135 newDoc->SetDocumentTemplate (m_pDocTemplText);
1136 newDoc->OnNewDocument();
1143 PhantomFileDocument*
1144 CTSimApp::newPhantomDoc()
1146 PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
1148 PhantomFileView* pView = newDoc->getView();
1150 wxFrame* pFrame = pView->getFrame();
1152 pFrame->SetSize (0,0);
1154 newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1155 newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1156 newDoc->OnNewDocument();
1164 Graph3dFileDocument*
1165 CTSimApp::newGraph3dDoc()
1167 Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
1169 Graph3dFileView* pView = newDoc->getView();
1171 wxFrame* pFrame = pView->getFrame();
1173 pFrame->SetSize (0,0);
1175 newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1176 newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1177 newDoc->OnNewDocument();