r387: 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.28 2001/01/13 03:51:35 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 #if !wxUSE_DOC_VIEW_ARCHITECTURE
41 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
42 #endif
43
44 #include "ct.h"
45 #include "ctsim.h"
46 #include "docs.h"
47 #include "views.h"
48 #include "dialogs.h"
49
50 #if defined(HAVE_CONFIG_H)
51 #include "config.h"
52 #endif
53
54 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
55 #ifdef MSVC
56 #define __STDC__ 1
57 #endif
58 #include "getopt.h"
59 #ifdef MSVC
60 #undef __STDC__
61 #endif
62 #endif
63
64 static const char* rcsindent = "$Id: ctsim.cpp,v 1.28 2001/01/13 03:51:35 kevin Exp $";
65
66 struct option CTSimApp::ctsimOptions[] = 
67 {
68   {"help", 0, 0, O_HELP},
69   {"version", 0, 0, O_VERSION},
70   {0, 0, 0, 0}
71 };
72
73 IMPLEMENT_APP(CTSimApp)
74
75 CTSimApp::CTSimApp()
76 : m_docManager(NULL), m_pFrame(NULL)
77 {
78   theApp = this;
79 }
80
81 #ifdef HAVE_SYS_TIME_H
82 #include <sys/time.h>
83 #endif
84
85 #ifdef HAVE_SYS_RESOURCE_H
86 #include <sys/resource.h>
87 #endif
88
89 bool
90 CTSimApp::OnInit()
91 {
92 #ifdef HAVE_SETPRIORITY
93   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
94 #endif
95
96   g_bRunningWXWindows = true;
97   // process options
98   while (1) {
99     int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
100     if (c == -1)
101       break;
102     
103     switch (c) {
104     case O_VERSION:
105       std::cout << rcsindent << std::endl;
106 #ifdef CTSIMVERSION
107       std::cout << "Version: CTSIMVERSION" << std::endl;
108 #elif defined(VERSION)
109       std::cout << "Version: VERSION" << std::endl;
110 #endif
111       exit(0);
112     case O_HELP:
113     case '?':
114       usage (argv[0]);
115       exit (0);
116     default:
117       usage (argv[0]);
118       exit (1);
119     }
120   }
121   
122   m_docManager = new wxDocManager;
123   
124   new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
125   
126   new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
127   
128   new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
129   
130   new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
131   
132   //// Create the main frame window
133   m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
134
135   SetTopWindow (m_pFrame);
136   m_pFrame->Centre(wxBOTH);
137   
138   m_pFrame->Show(true);
139   
140   for (int i = optind + 1; i <= argc; i++) {
141     wxString filename = argv [i - 1];
142     m_docManager->CreateDocument (filename, wxDOC_SILENT);
143   }
144   
145   return true;
146 }
147
148 void
149 CTSimApp::usage(const char* program)
150 {
151   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
152   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
153   std::cout << "\n";
154   std::cout << "  --version Display version\n";
155   std::cout << "  --help    Display this help message\n";
156 }
157
158 int
159 CTSimApp::OnExit()
160 {
161   delete m_docManager;
162 #ifdef HAVE_DMALLOC
163   dmalloc_shutdown();
164 #endif
165   return 0;
166 }
167
168 wxString
169 CTSimApp::getUntitledFilename()
170 {
171   static int untitledNumber = 1;
172   
173   wxString filename ("Untitled");
174   filename << untitledNumber++;
175   
176   return (filename);
177 }
178
179
180 // Top-level window for CTSim
181
182 #if CTSIM_MDI
183 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
184
185 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
186 #else
187 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
188
189 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
190 #endif
191
192 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
193 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
194 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
195 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
196 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
197 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
198 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
199 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
200 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
201 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
202 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
203 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
204 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
205 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
206 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
207 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
208 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
209 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
210 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
211 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
212 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
213 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
214 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
215 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
216 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
217 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
218 END_EVENT_TABLE()
219
220
221
222 #if CTSIM_MDI
223 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
224 : wxMDIParentFrame(NULL,  id, title, pos, size, type), m_pLog(NULL)
225 #else
226 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
227 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
228 #endif
229 {
230   m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
231   wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
232   CreateStatusBar();
233   SetStatusText ("Welcome to CTSim");
234   
235   //// Make a menubar
236   wxMenu *file_menu = new wxMenu;
237   
238   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
239   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
240   file_menu->Append(wxID_OPEN, "&Open...");
241   
242   file_menu->AppendSeparator();
243   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
244   
245   //  history of files visited
246   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
247   
248   m_pWindowMenu = new wxMenu;
249   m_pWindowMenu->UpdateUI (this);
250   
251   wxMenu* help_menu = new wxMenu;
252   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
253   help_menu->AppendSeparator();
254   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
255   
256   wxMenuBar* menu_bar = new wxMenuBar;
257   
258   menu_bar->Append(file_menu, "&File");
259   menu_bar->Append(m_pWindowMenu, "&Window");
260   menu_bar->Append(help_menu, "&Help");
261   
262   SetMenuBar(menu_bar);
263   
264   for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
265     m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
266     m_pWindowMenu->Append (m_apWindowMenuItems[i]);
267     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
268   }
269   
270   m_iDefaultPhantomID = Phantom::PHM_HERMAN;
271   m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
272   m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
273   m_iDefaultFilterXSize = 256;
274   m_iDefaultFilterYSize = 256;
275   m_dDefaultFilterParam = 1.;
276   m_dDefaultFilterBandwidth = 1.;
277   m_dDefaultFilterInputScale = 1.;
278   m_dDefaultFilterOutputScale = 1.;
279   
280 }
281
282 void 
283 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
284 {
285   wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
286 #ifdef CTSIMVERSION
287   msg += "Version ";
288   msg += CTSIMVERSION;
289   msg += "\n\n";
290 #elif defined(VERSION)
291   msg << "Version: " <<  VERSION << "\n\n";
292 #endif
293   msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
294   
295   wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
296 }
297
298 void 
299 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
300 {
301   DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
302   int dialogReturn = dialogPhantom.ShowModal();
303   if (dialogReturn == wxID_OK) {
304     wxString selection (dialogPhantom.getPhantom());
305     *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
306     wxString filename = selection + ".phm";
307     m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
308     theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
309   }
310   
311 }
312
313 void 
314 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
315 {
316   DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
317   int dialogReturn = dialogFilter.ShowModal();
318   if (dialogReturn == wxID_OK) {
319     wxString strFilter (dialogFilter.getFilterName());
320     wxString strDomain (dialogFilter.getDomainName());
321     m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
322     m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
323     m_iDefaultFilterXSize = dialogFilter.getXSize();
324     m_iDefaultFilterYSize = dialogFilter.getYSize();
325     m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
326     m_dDefaultFilterParam= dialogFilter.getFilterParam();
327     m_dDefaultFilterInputScale = dialogFilter.getInputScale();
328     m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
329     std::ostringstream os;
330     os << "Generate Filter=" << strFilter.c_str() 
331       << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
332       << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
333       << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
334     *theApp->getLog() << os.str().c_str() << "\n";
335     wxString filename = "untitled.if";
336     ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
337     if (! pFilterDoc) {
338       sys_error (ERR_SEVERE, "Unable to create filter image");
339       return;
340     }
341     ImageFile& rIF = pFilterDoc->getImageFile();
342     rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
343     rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
344     rIF.labelAdd (os.str().c_str());
345     if (theApp->getSetModifyNewDocs())
346       pFilterDoc->Modify (true);
347     pFilterDoc->UpdateAllViews();
348     pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
349   }
350 }
351
352 void
353 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
354 {
355   const ImageFile& rIF = pIFDoc->getImageFile();
356   unsigned int nx = rIF.nx();
357   unsigned int ny = rIF.ny();
358   wxList& rListDocs = m_docManager->GetDocuments();
359   for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
360     wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
361     ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
362     if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
363       const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
364       if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
365         vecIF.push_back (pIFCompareDoc);
366     }
367   }
368 }
369
370 void 
371 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
372 {
373   wxMessageBox("No help available, refer to man pages of command-line tools");
374 }
375
376 void 
377 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
378 {
379   Close(true);
380 }
381
382 void
383 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
384 {
385   int iPos = 0;
386   wxList& rListDocs = theApp->getDocManager()->GetDocuments();
387   wxNode* pNode = rListDocs.GetFirst();
388   while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
389     wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
390     wxString strFilename = pDoc->GetFilename();
391     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
392     m_apWindowMenuData[iPos] = pDoc;
393     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
394     iPos++;
395     pNode = pNode->GetNext();
396   }
397   for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
398     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
399     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
400     m_apWindowMenuData[i] = NULL;
401   }
402   
403 }
404
405 void 
406 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
407 {
408   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
409     wxString strFilename = pDoc->GetFilename();
410     const wxView* pView = pDoc->GetFirstView();
411     if (pView) {
412       wxFrame* pFrame = pView->GetFrame();
413       pFrame->SetFocus();
414       pFrame->Raise();
415     }
416   }
417 }
418
419 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
420 { DoWindowMenu (0, event); }
421
422 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
423 { DoWindowMenu (1, event); }
424
425 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
426 { DoWindowMenu (2, event); }
427
428 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
429 { DoWindowMenu (3, event); }
430
431 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
432 { DoWindowMenu (4, event); }
433
434 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
435 { DoWindowMenu (5, event); }
436
437 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
438 { DoWindowMenu (6, event); }
439
440 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
441 { DoWindowMenu (7, event); }
442
443 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
444 { DoWindowMenu (8, event); }
445
446 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
447 { DoWindowMenu (9, event); }
448
449 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
450 { DoWindowMenu (10, event); }
451
452 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
453 { DoWindowMenu (11, event); }
454
455 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
456 { DoWindowMenu (12, event); }
457
458 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
459 { DoWindowMenu (13, event); }
460
461 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
462 { DoWindowMenu (14, event); }
463
464 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
465 { DoWindowMenu (15, event); }
466
467 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
468 { DoWindowMenu (16, event); }
469
470 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
471 { DoWindowMenu (17, event); }
472
473 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
474 { DoWindowMenu (18, event); }
475
476 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
477 { DoWindowMenu (19, event); }
478
479