r413: no message
[ctsim.git] / src / ctsim.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsim.cpp
5 **   Purpose:       Top-level routines of CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsim.cpp,v 1.31 2001/01/17 11:00:18 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28
29 // For compilers that support precompilation, includes "wx/wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/wx.h"
38 #endif
39
40 #include "wx/image.h"
41 #include "wx/filesys.h"
42 #include "wx/fs_zip.h"
43
44 #if !wxUSE_DOC_VIEW_ARCHITECTURE
45 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
46 #endif
47
48 #include "ct.h"
49 #include "ctsim.h"
50 #include "docs.h"
51 #include "views.h"
52 #include "dialogs.h"
53
54 #if defined(HAVE_CONFIG_H)
55 #include "config.h"
56 #endif
57
58 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
59 #ifdef MSVC
60 #define __STDC__ 1
61 #endif
62 #include "getopt.h"
63 #ifdef MSVC
64 #undef __STDC__
65 #endif
66 #endif
67
68 static const char* rcsindent = "$Id: ctsim.cpp,v 1.31 2001/01/17 11:00:18 kevin Exp $";
69
70 struct option CTSimApp::ctsimOptions[] = 
71 {
72   {"help", 0, 0, O_HELP},
73   {"version", 0, 0, O_VERSION},
74   {0, 0, 0, 0}
75 };
76
77 IMPLEMENT_APP(CTSimApp)
78
79 CTSimApp::CTSimApp()
80 : m_docManager(NULL), m_pFrame(NULL)
81 {
82   theApp = this;
83 }
84
85 #ifdef HAVE_SYS_TIME_H
86 #include <sys/time.h>
87 #endif
88
89 #ifdef HAVE_SYS_RESOURCE_H
90 #include <sys/resource.h>
91 #endif
92
93 bool
94 CTSimApp::OnInit()
95 {
96 #ifdef HAVE_SETPRIORITY
97   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
98 #endif
99
100   g_bRunningWXWindows = true;
101   // process options
102   while (1) {
103     int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
104     if (c == -1)
105       break;
106     
107     switch (c) {
108     case O_VERSION:
109       std::cout << rcsindent << std::endl;
110 #ifdef CTSIMVERSION
111       std::cout << "Version: CTSIMVERSION" << std::endl;
112 #elif defined(VERSION)
113       std::cout << "Version: VERSION" << std::endl;
114 #endif
115       exit(0);
116     case O_HELP:
117     case '?':
118       usage (argv[0]);
119       exit (0);
120     default:
121       usage (argv[0]);
122       exit (1);
123     }
124   }
125   
126   m_docManager = new wxDocManager;
127   
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));
132   
133 #if wxUSE_GIF
134     wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
135 #endif
136
137 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
138       wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
139 #endif
140
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);
143
144   SetTopWindow (m_pFrame);
145   m_pFrame->Centre(wxBOTH);
146   
147   m_pFrame->Show(true);
148   
149   for (int i = optind + 1; i <= argc; i++) {
150     wxString filename = argv [i - 1];
151     m_docManager->CreateDocument (filename, wxDOC_SILENT);
152   }
153
154   setIconForFrame (m_pFrame);
155
156   if (! m_pFrame->getHelpController().Initialize("ctsim"))
157     ::wxMessageBox ("Cannot initialize the help system", "Error");
158
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);
165
166   return true;
167 }
168
169
170 #include "./ctsim.xpm"
171 void
172 CTSimApp::setIconForFrame(wxFrame* pFrame)
173 {
174   wxIcon iconApp (ctsim16_xpm);
175
176   if (iconApp.Ok())
177     pFrame->SetIcon (iconApp);
178 }
179
180 void
181 CTSimApp::usage(const char* program)
182 {
183   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
184   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
185   std::cout << "\n";
186   std::cout << "  --version Display version\n";
187   std::cout << "  --help    Display this help message\n";
188 }
189
190 int
191 CTSimApp::OnExit()
192 {
193   delete m_docManager;
194 #ifdef HAVE_DMALLOC
195   dmalloc_shutdown();
196 #endif
197   return 0;
198 }
199
200 wxString
201 CTSimApp::getUntitledFilename()
202 {
203   static int untitledNumber = 1;
204   
205   wxString filename ("Untitled");
206   filename << untitledNumber++;
207   
208   return (filename);
209 }
210
211
212 // Top-level window for CTSim
213
214 #if CTSIM_MDI
215 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
216
217 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
218 #else
219 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
220
221 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
222 #endif
223
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)
250 END_EVENT_TABLE()
251
252
253
254 #if CTSIM_MDI
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)
257 #else
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)
260 #endif
261 {
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));
264   CreateStatusBar();
265   SetStatusText ("Welcome to CTSim");
266   
267   //// Make a menubar
268   wxMenu *file_menu = new wxMenu;
269   
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...");
273   
274   file_menu->AppendSeparator();
275   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
276   
277   //  history of files visited
278   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
279   
280   m_pWindowMenu = new wxMenu;
281   m_pWindowMenu->UpdateUI (this);
282   
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");
287   
288   wxMenuBar* menu_bar = new wxMenuBar;
289   
290   menu_bar->Append(file_menu, "&File");
291   menu_bar->Append(m_pWindowMenu, "&Window");
292   menu_bar->Append(help_menu, "&Help");
293   
294   SetMenuBar(menu_bar);
295   
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);
300   }
301   
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.;
311   
312 }
313
314 void 
315 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
316 {
317   wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
318 #ifdef CTSIMVERSION
319   msg += "Version ";
320   msg += CTSIMVERSION;
321   msg += "\n\n";
322 #elif defined(VERSION)
323   msg << "Version: " <<  VERSION << "\n\n";
324 #endif
325   msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
326   
327   wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
328 }
329
330 void 
331 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
332 {
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);
341   }
342   
343 }
344
345 void 
346 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
347 {
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));
369     if (! pFilterDoc) {
370       sys_error (ERR_SEVERE, "Unable to create filter image");
371       return;
372     }
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);
381   }
382 }
383
384 void
385 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
386 {
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);
398     }
399   }
400 }
401
402 void 
403 MainFrame::OnHelpTopics (wxCommandEvent& event)
404 {
405    showHelp (event.GetId());
406 }
407
408 void
409 MainFrame::showHelp (int commandID)
410 {
411   m_help.LoadFile();
412
413   switch (commandID) {
414   case MAINMENU_HELP_TOPICS:
415      m_help.DisplayContents();
416      break;
417
418   default:
419     *getLog() << "Unknown help command # " << commandID << "\n";
420     break;
421   }
422 }
423
424 void 
425 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
426 {
427   Close(true);
428 }
429
430 void
431 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
432 {
433   int iPos = 0;
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);
442     iPos++;
443     pNode = pNode->GetNext();
444   }
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;
449   }
450   
451 }
452
453 void 
454 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
455 {
456   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
457     wxString strFilename = pDoc->GetFilename();
458     const wxView* pView = pDoc->GetFirstView();
459     if (pView) {
460       wxFrame* pFrame = pView->GetFrame();
461       pFrame->SetFocus();
462       pFrame->Raise();
463     }
464   }
465 }
466
467 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
468 { DoWindowMenu (0, event); }
469
470 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
471 { DoWindowMenu (1, event); }
472
473 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
474 { DoWindowMenu (2, event); }
475
476 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
477 { DoWindowMenu (3, event); }
478
479 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
480 { DoWindowMenu (4, event); }
481
482 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
483 { DoWindowMenu (5, event); }
484
485 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
486 { DoWindowMenu (6, event); }
487
488 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
489 { DoWindowMenu (7, event); }
490
491 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
492 { DoWindowMenu (8, event); }
493
494 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
495 { DoWindowMenu (9, event); }
496
497 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
498 { DoWindowMenu (10, event); }
499
500 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
501 { DoWindowMenu (11, event); }
502
503 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
504 { DoWindowMenu (12, event); }
505
506 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
507 { DoWindowMenu (13, event); }
508
509 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
510 { DoWindowMenu (14, event); }
511
512 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
513 { DoWindowMenu (15, event); }
514
515 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
516 { DoWindowMenu (16, event); }
517
518 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
519 { DoWindowMenu (17, event); }
520
521 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
522 { DoWindowMenu (18, event); }
523
524 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
525 { DoWindowMenu (19, event); }
526
527