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-2000 Kevin Rosenberg
12 ** $Id: ctsim.cpp,v 1.34 2001/01/18 18:09:35 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 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
44 #include "wx/filesys.h"
45 #include "wx/fs_zip.h"
47 #if !wxUSE_DOC_VIEW_ARCHITECTURE
48 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
53 #include "ctsim-map.h"
58 #if defined(HAVE_CONFIG_H)
62 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
72 static const char* rcsindent = "$Id: ctsim.cpp,v 1.34 2001/01/18 18:09:35 kevin Exp $";
74 struct option CTSimApp::ctsimOptions[] =
76 {"help", 0, 0, O_HELP},
77 {"version", 0, 0, O_VERSION},
81 IMPLEMENT_APP(CTSimApp)
84 : m_docManager(NULL), m_pFrame(NULL)
89 #ifdef HAVE_SYS_TIME_H
93 #ifdef HAVE_SYS_RESOURCE_H
94 #include <sys/resource.h>
100 #ifdef HAVE_SETPRIORITY
101 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
104 g_bRunningWXWindows = true;
107 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
113 std::cout << rcsindent << std::endl;
115 std::cout << "Version: CTSIMVERSION" << std::endl;
116 #elif defined(VERSION)
117 std::cout << "Version: VERSION" << std::endl;
130 m_docManager = new wxDocManager;
132 new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
133 new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
134 new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
135 new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
138 wxImage::AddHandler(new wxGIFHandler); // Required for images in the online documentation
141 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
142 wxFileSystem::AddHandler(new wxZipFSHandler); // Required for advanced HTML help
145 // Create the main frame window
146 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
148 SetTopWindow (m_pFrame);
149 m_pFrame->Centre(wxBOTH);
151 m_pFrame->Show(true);
153 for (int i = optind + 1; i <= argc; i++) {
154 wxString filename = argv [i - 1];
155 m_docManager->CreateDocument (filename, wxDOC_SILENT);
158 setIconForFrame (m_pFrame);
160 if (! m_pFrame->getHelpController().Initialize("ctsim"))
161 ::wxMessageBox ("Cannot initialize the help system", "Error");
163 wxAcceleratorEntry accelEntries[5];
164 accelEntries[0].Set (wxACCEL_CTRL, (int) 'O', wxID_OPEN);
165 accelEntries[1].Set (wxACCEL_CTRL, (int) 'T', MAINMENU_HELP_TOPICS);
166 accelEntries[2].Set (wxACCEL_CTRL, (int) 'P', MAINMENU_FILE_CREATE_PHANTOM);
167 accelEntries[3].Set (wxACCEL_CTRL, (int) 'F', MAINMENU_FILE_CREATE_FILTER);
168 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
169 wxAcceleratorTable accelTable (5, accelEntries);
170 m_pFrame->SetAcceleratorTable (accelTable);
176 #include "./ctsim.xpm"
178 CTSimApp::setIconForFrame(wxFrame* pFrame)
180 wxIcon iconApp (ctsim16_xpm);
183 pFrame->SetIcon (iconApp);
187 CTSimApp::usage(const char* program)
189 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
190 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
192 std::cout << " --version Display version\n";
193 std::cout << " --help Display this help message\n";
207 CTSimApp::getUntitledFilename()
209 static int untitledNumber = 1;
211 wxString filename ("Untitled");
212 filename << untitledNumber++;
218 // Top-level window for CTSim
221 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
223 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
225 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
227 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
230 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
231 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
232 EVT_MENU(MAINMENU_HELP_TOPICS, MainFrame::OnHelpTopics)
233 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
234 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
235 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
236 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
237 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
238 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
239 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
240 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
241 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
242 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
243 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
244 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
245 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
246 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
247 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
248 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
249 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
250 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
251 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
252 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
253 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
254 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
255 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
256 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
262 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
263 : wxMDIParentFrame(NULL, id, title, pos, size, type), m_pLog(NULL)
265 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
266 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
269 m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
270 wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
272 SetStatusText ("Welcome to CTSim");
275 wxMenu *file_menu = new wxMenu;
277 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
278 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
279 file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
281 file_menu->AppendSeparator();
282 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
284 // history of files visited
285 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
287 m_pWindowMenu = new wxMenu;
288 m_pWindowMenu->UpdateUI (this);
290 wxMenu* help_menu = new wxMenu;
291 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
292 help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-T");
293 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
295 wxMenuBar* menu_bar = new wxMenuBar;
297 menu_bar->Append(file_menu, "&File");
298 menu_bar->Append(m_pWindowMenu, "&Window");
299 menu_bar->Append(help_menu, "&Help");
301 SetMenuBar(menu_bar);
303 for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
304 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
305 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
306 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
309 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
310 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
311 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
312 m_iDefaultFilterXSize = 256;
313 m_iDefaultFilterYSize = 256;
314 m_dDefaultFilterParam = 1.;
315 m_dDefaultFilterBandwidth = 1.;
316 m_dDefaultFilterInputScale = 1.;
317 m_dDefaultFilterOutputScale = 1.;
322 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
324 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
329 #elif defined(VERSION)
330 msg << "Version: " << VERSION << "\n\n";
332 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
334 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
338 MainFrame::OnCreatePhantom(wxCommandEvent& event)
340 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
341 int dialogReturn = dialogPhantom.ShowModal();
342 if (dialogReturn == wxID_OK) {
343 wxString selection (dialogPhantom.getPhantom());
344 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
345 wxString filename = selection + ".phm";
346 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
347 theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
353 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
355 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
356 int dialogReturn = dialogFilter.ShowModal();
357 if (dialogReturn == wxID_OK) {
358 wxString strFilter (dialogFilter.getFilterName());
359 wxString strDomain (dialogFilter.getDomainName());
360 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
361 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
362 m_iDefaultFilterXSize = dialogFilter.getXSize();
363 m_iDefaultFilterYSize = dialogFilter.getYSize();
364 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
365 m_dDefaultFilterParam= dialogFilter.getFilterParam();
366 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
367 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
368 std::ostringstream os;
369 os << "Generate Filter=" << strFilter.c_str()
370 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
371 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
372 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
373 *theApp->getLog() << os.str().c_str() << "\n";
374 wxString filename = "untitled.if";
375 ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
377 sys_error (ERR_SEVERE, "Unable to create filter image");
380 ImageFile& rIF = pFilterDoc->getImageFile();
381 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
382 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
383 rIF.labelAdd (os.str().c_str());
384 if (theApp->getSetModifyNewDocs())
385 pFilterDoc->Modify (true);
386 pFilterDoc->UpdateAllViews();
387 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
392 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
394 const ImageFile& rIF = pIFDoc->getImageFile();
395 unsigned int nx = rIF.nx();
396 unsigned int ny = rIF.ny();
397 wxList& rListDocs = m_docManager->GetDocuments();
398 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
399 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
400 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
401 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
402 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
403 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
404 vecIF.push_back (pIFCompareDoc);
410 MainFrame::OnHelpTopics (wxCommandEvent& event)
412 showHelp (event.GetId());
416 MainFrame::OnHelpContents (wxCommandEvent& event)
418 showHelp (event.GetId());
422 MainFrame::showHelp (int commandID)
427 case MAINMENU_HELP_CONTENTS:
428 m_help.DisplayContents ();
430 case MAINMENU_HELP_TOPICS:
431 m_help.DisplaySection (introduction);
435 *getLog() << "Unknown help command # " << commandID << "\n";
441 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
447 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
450 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
451 wxNode* pNode = rListDocs.GetFirst();
452 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
453 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
454 wxString strFilename = pDoc->GetFilename();
455 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
456 m_apWindowMenuData[iPos] = pDoc;
457 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
459 pNode = pNode->GetNext();
461 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
462 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
463 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
464 m_apWindowMenuData[i] = NULL;
470 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
472 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
473 wxString strFilename = pDoc->GetFilename();
474 const wxView* pView = pDoc->GetFirstView();
476 wxFrame* pFrame = pView->GetFrame();
483 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
484 { DoWindowMenu (0, event); }
486 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
487 { DoWindowMenu (1, event); }
489 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
490 { DoWindowMenu (2, event); }
492 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
493 { DoWindowMenu (3, event); }
495 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
496 { DoWindowMenu (4, event); }
498 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
499 { DoWindowMenu (5, event); }
501 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
502 { DoWindowMenu (6, event); }
504 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
505 { DoWindowMenu (7, event); }
507 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
508 { DoWindowMenu (8, event); }
510 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
511 { DoWindowMenu (9, event); }
513 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
514 { DoWindowMenu (10, event); }
516 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
517 { DoWindowMenu (11, event); }
519 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
520 { DoWindowMenu (12, event); }
522 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
523 { DoWindowMenu (13, event); }
525 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
526 { DoWindowMenu (14, event); }
528 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
529 { DoWindowMenu (15, event); }
531 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
532 { DoWindowMenu (16, event); }
534 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
535 { DoWindowMenu (17, event); }
537 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
538 { DoWindowMenu (18, event); }
540 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
541 { DoWindowMenu (19, event); }