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.31 2001/01/17 11:00:18 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 // For compilers that support precompilation, includes "wx/wx.h".
30 #include "wx/wxprec.h"
41 #include "wx/filesys.h"
42 #include "wx/fs_zip.h"
44 #if !wxUSE_DOC_VIEW_ARCHITECTURE
45 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
54 #if defined(HAVE_CONFIG_H)
58 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
68 static const char* rcsindent = "$Id: ctsim.cpp,v 1.31 2001/01/17 11:00:18 kevin Exp $";
70 struct option CTSimApp::ctsimOptions[] =
72 {"help", 0, 0, O_HELP},
73 {"version", 0, 0, O_VERSION},
77 IMPLEMENT_APP(CTSimApp)
80 : m_docManager(NULL), m_pFrame(NULL)
85 #ifdef HAVE_SYS_TIME_H
89 #ifdef HAVE_SYS_RESOURCE_H
90 #include <sys/resource.h>
96 #ifdef HAVE_SETPRIORITY
97 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
100 g_bRunningWXWindows = true;
103 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
109 std::cout << rcsindent << std::endl;
111 std::cout << "Version: CTSIMVERSION" << std::endl;
112 #elif defined(VERSION)
113 std::cout << "Version: VERSION" << std::endl;
126 m_docManager = new wxDocManager;
128 new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
129 new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
130 new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
131 new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
134 wxImage::AddHandler(new wxGIFHandler); // Required for images in the online documentation
137 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
138 wxFileSystem::AddHandler(new wxZipFSHandler); // Required for advanced HTML help
141 // Create the main frame window
142 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
144 SetTopWindow (m_pFrame);
145 m_pFrame->Centre(wxBOTH);
147 m_pFrame->Show(true);
149 for (int i = optind + 1; i <= argc; i++) {
150 wxString filename = argv [i - 1];
151 m_docManager->CreateDocument (filename, wxDOC_SILENT);
154 setIconForFrame (m_pFrame);
156 if (! m_pFrame->getHelpController().Initialize("ctsim"))
157 ::wxMessageBox ("Cannot initialize the help system", "Error");
159 wxAcceleratorEntry accelEntries[3];
160 accelEntries[0].Set(wxACCEL_CTRL, WXK_F1, MAINMENU_HELP_TOPICS);
161 accelEntries[1].Set(wxACCEL_CTRL, (int) 'P', MAINMENU_FILE_CREATE_PHANTOM);
162 accelEntries[2].Set(wxACCEL_CTRL, (int) 'F', MAINMENU_FILE_CREATE_FILTER);
163 wxAcceleratorTable accelTable (2, accelEntries);
164 m_pFrame->SetAcceleratorTable (accelTable);
170 #include "./ctsim.xpm"
172 CTSimApp::setIconForFrame(wxFrame* pFrame)
174 wxIcon iconApp (ctsim16_xpm);
177 pFrame->SetIcon (iconApp);
181 CTSimApp::usage(const char* program)
183 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
184 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
186 std::cout << " --version Display version\n";
187 std::cout << " --help Display this help message\n";
201 CTSimApp::getUntitledFilename()
203 static int untitledNumber = 1;
205 wxString filename ("Untitled");
206 filename << untitledNumber++;
212 // Top-level window for CTSim
215 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
217 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
219 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
221 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
224 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
225 EVT_MENU(MAINMENU_HELP_TOPICS, MainFrame::OnHelpTopics)
226 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
227 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
228 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
229 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
230 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
231 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
232 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
233 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
234 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
235 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
236 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
237 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
238 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
239 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
240 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
241 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
242 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
243 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
244 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
245 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
246 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
247 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
248 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
249 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
255 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
256 : wxMDIParentFrame(NULL, id, title, pos, size, type), m_pLog(NULL)
258 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
259 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
262 m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
263 wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
265 SetStatusText ("Welcome to CTSim");
268 wxMenu *file_menu = new wxMenu;
270 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
271 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
272 file_menu->Append(wxID_OPEN, "&Open...");
274 file_menu->AppendSeparator();
275 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
277 // history of files visited
278 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
280 m_pWindowMenu = new wxMenu;
281 m_pWindowMenu->UpdateUI (this);
283 wxMenu* help_menu = new wxMenu;
284 help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tF1");
285 help_menu->AppendSeparator();
286 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
288 wxMenuBar* menu_bar = new wxMenuBar;
290 menu_bar->Append(file_menu, "&File");
291 menu_bar->Append(m_pWindowMenu, "&Window");
292 menu_bar->Append(help_menu, "&Help");
294 SetMenuBar(menu_bar);
296 for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
297 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
298 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
299 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
302 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
303 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
304 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
305 m_iDefaultFilterXSize = 256;
306 m_iDefaultFilterYSize = 256;
307 m_dDefaultFilterParam = 1.;
308 m_dDefaultFilterBandwidth = 1.;
309 m_dDefaultFilterInputScale = 1.;
310 m_dDefaultFilterOutputScale = 1.;
315 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
317 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
322 #elif defined(VERSION)
323 msg << "Version: " << VERSION << "\n\n";
325 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
327 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
331 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
333 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
334 int dialogReturn = dialogPhantom.ShowModal();
335 if (dialogReturn == wxID_OK) {
336 wxString selection (dialogPhantom.getPhantom());
337 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
338 wxString filename = selection + ".phm";
339 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
340 theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
346 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
348 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
349 int dialogReturn = dialogFilter.ShowModal();
350 if (dialogReturn == wxID_OK) {
351 wxString strFilter (dialogFilter.getFilterName());
352 wxString strDomain (dialogFilter.getDomainName());
353 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
354 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
355 m_iDefaultFilterXSize = dialogFilter.getXSize();
356 m_iDefaultFilterYSize = dialogFilter.getYSize();
357 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
358 m_dDefaultFilterParam= dialogFilter.getFilterParam();
359 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
360 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
361 std::ostringstream os;
362 os << "Generate Filter=" << strFilter.c_str()
363 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
364 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
365 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
366 *theApp->getLog() << os.str().c_str() << "\n";
367 wxString filename = "untitled.if";
368 ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
370 sys_error (ERR_SEVERE, "Unable to create filter image");
373 ImageFile& rIF = pFilterDoc->getImageFile();
374 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
375 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
376 rIF.labelAdd (os.str().c_str());
377 if (theApp->getSetModifyNewDocs())
378 pFilterDoc->Modify (true);
379 pFilterDoc->UpdateAllViews();
380 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
385 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
387 const ImageFile& rIF = pIFDoc->getImageFile();
388 unsigned int nx = rIF.nx();
389 unsigned int ny = rIF.ny();
390 wxList& rListDocs = m_docManager->GetDocuments();
391 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
392 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
393 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
394 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
395 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
396 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
397 vecIF.push_back (pIFCompareDoc);
403 MainFrame::OnHelpTopics (wxCommandEvent& event)
405 showHelp (event.GetId());
409 MainFrame::showHelp (int commandID)
414 case MAINMENU_HELP_TOPICS:
415 m_help.DisplayContents();
419 *getLog() << "Unknown help command # " << commandID << "\n";
425 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
431 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
434 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
435 wxNode* pNode = rListDocs.GetFirst();
436 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
437 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
438 wxString strFilename = pDoc->GetFilename();
439 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
440 m_apWindowMenuData[iPos] = pDoc;
441 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
443 pNode = pNode->GetNext();
445 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
446 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
447 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
448 m_apWindowMenuData[i] = NULL;
454 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
456 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
457 wxString strFilename = pDoc->GetFilename();
458 const wxView* pView = pDoc->GetFirstView();
460 wxFrame* pFrame = pView->GetFrame();
467 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
468 { DoWindowMenu (0, event); }
470 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
471 { DoWindowMenu (1, event); }
473 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
474 { DoWindowMenu (2, event); }
476 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
477 { DoWindowMenu (3, event); }
479 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
480 { DoWindowMenu (4, event); }
482 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
483 { DoWindowMenu (5, event); }
485 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
486 { DoWindowMenu (6, event); }
488 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
489 { DoWindowMenu (7, event); }
491 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
492 { DoWindowMenu (8, event); }
494 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
495 { DoWindowMenu (9, event); }
497 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
498 { DoWindowMenu (10, event); }
500 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
501 { DoWindowMenu (11, event); }
503 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
504 { DoWindowMenu (12, event); }
506 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
507 { DoWindowMenu (13, event); }
509 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
510 { DoWindowMenu (14, event); }
512 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
513 { DoWindowMenu (15, event); }
515 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
516 { DoWindowMenu (16, event); }
518 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
519 { DoWindowMenu (17, event); }
521 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
522 { DoWindowMenu (18, event); }
524 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
525 { DoWindowMenu (19, event); }