r545: 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.78 2001/02/16 00:28:41 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 #ifdef MSVC
29 #define strdup _strdup
30 #endif
31
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/wx.h"
41 #endif
42
43 #include "wx/image.h"
44 #include "wx/filesys.h"
45 #include "wx/fs_zip.h"
46 #ifdef __WXMSW__
47 #include "wx/msw/helpchm.h"
48 #endif
49
50 #if !wxUSE_DOC_VIEW_ARCHITECTURE
51 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
52 #endif
53
54
55 #include "ct.h"
56 #include "ctsim.h"
57 #include "docs.h"
58 #include "views.h"
59 #include "dialogs.h"
60
61 #if defined(HAVE_CONFIG_H)
62 #include "config.h"
63 #endif
64
65 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
66 #ifdef MSVC
67 #define __STDC__ 1
68 #endif
69 #include "getopt.h"
70 #ifdef MSVC
71 #undef __STDC__
72 #endif
73 #endif
74
75 static const char* rcsindent = "$Id: ctsim.cpp,v 1.78 2001/02/16 00:28:41 kevin Exp $";
76
77 struct option CTSimApp::ctsimOptions[] = 
78 {
79         {"help", 0, 0, O_HELP},
80         {"version", 0, 0, O_VERSION},
81         {0, 0, 0, 0}
82 };
83
84 IMPLEMENT_APP(CTSimApp)
85
86 CTSimApp::CTSimApp()
87 : m_docManager(NULL), m_pFrame(NULL), m_pLog(0), m_pLogDoc(0), m_pConfig(0),
88   m_bAdvancedOptions(false), m_bSetModifyNewDocs(true)
89 {
90         theApp = this;
91 }
92
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96
97 #ifdef HAVE_SYS_RESOURCE_H
98 #include <sys/resource.h>
99 #endif
100
101 bool
102 CTSimApp::OnInit()
103 {
104 #ifdef HAVE_SETPRIORITY
105         setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
106 #endif
107         
108         openConfig();
109         
110         g_bRunningWXWindows = true;
111         // process options
112         while (1) {
113                 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
114                 if (c == -1)
115                         break;
116                 
117                 switch (c) {
118                 case O_VERSION:
119                         std::cout << rcsindent << std::endl;
120 #ifdef CTSIMVERSION
121                         std::cout << "Version: CTSIMVERSION" << std::endl;
122 #elif defined(VERSION)
123                         std::cout << "Version: VERSION" << std::endl;
124 #endif
125                         exit(0);
126                 case O_HELP:
127                 case '?':
128                         usage (argv[0]);
129                         exit (0);
130                 default:
131                         usage (argv[0]);
132                         exit (1);
133                 }
134         }
135         
136         m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
137         
138         m_pDocTemplImage = new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
139         m_pDocTemplProjection = new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
140         m_pDocTemplPhantom = new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
141         m_pDocTemplPlot = new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
142         m_pDocTemplText = new wxDocTemplate (m_docManager, "TextFile", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
143 #if wxUSE_GLCANVAS
144         m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, "Graph3dFile", "*.g3d", "", "g3d", "Graph3dFile", "Graph3dView", CLASSINFO(Graph3dFileDocument), CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
145 #endif
146
147 #if wxUSE_GIF
148         wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
149 #endif
150         
151 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
152         wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
153 #endif
154         
155         // Create the main frame window
156         int xDisplay, yDisplay;
157         ::wxDisplaySize (&xDisplay, &yDisplay);
158         m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), 
159                 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .755)), wxDEFAULT_FRAME_STYLE);
160         
161         setIconForFrame (m_pFrame);
162         m_pFrame->Centre(wxBOTH);
163         m_pFrame->Show(true);  
164         SetTopWindow (m_pFrame);
165         
166         if (m_pConfig)
167                 m_docManager->FileHistoryLoad(*m_pConfig);
168         
169 #ifdef CTSIM_MDI
170         m_pLogDoc = newTextDoc();
171         if (m_pLogDoc) {
172                 m_pLog = m_pLogDoc->getTextCtrl();
173                 m_pLogDoc->SetDocumentName("Log.txt");
174                 m_pLogDoc->SetFilename("Log.txt");
175                 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
176                 int xSize, ySize;
177                 m_pFrame->GetClientSize(&xSize, &ySize);
178                 int yLogSize = ySize / 4;
179                 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
180                 m_pLogDoc->getView()->getFrame()->Show (true);
181         } else
182 #else
183                 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
184 #endif
185         wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
186
187         wxString helpDir;
188         if (! m_pConfig->Read("HelpDir", &helpDir))
189                 helpDir = ::wxGetCwd();
190 #ifdef CTSIM_WINHELP
191         if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
192                 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
193 #else
194         if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
195                 ! m_pFrame->getHtmlHelpController().Initialize("/usr/local/man/ctsim"))
196                 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
197         else {
198             if (::wxDirExists ("/tmp"))
199                 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
200             m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
201         }
202 #endif
203
204         for (int i = optind + 1; i <= argc; i++) {
205                 wxString filename = argv [i - 1];
206                 m_docManager->CreateDocument (filename, wxDOC_SILENT);
207         }
208         
209         return true;
210 }
211
212
213 #include "./ctsim.xpm"
214 void
215 CTSimApp::setIconForFrame(wxFrame* pFrame)
216 {
217         wxIcon iconApp (ctsim16_xpm);
218         
219         if (iconApp.Ok())
220                 pFrame->SetIcon (iconApp);
221 }
222
223 void
224 CTSimApp::usage(const char* program)
225 {
226         std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
227         std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
228         std::cout << "\n";
229         std::cout << "  --version Display version\n";
230         std::cout << "  --help    Display this help message\n";
231 }
232
233 int
234 CTSimApp::OnExit()
235 {
236   closeConfig();
237
238 #ifdef HAVE_DMALLOC
239         dmalloc_shutdown();
240 #endif
241         return 0;
242 }
243
244 void
245 CTSimApp::openConfig()
246 {
247 #ifdef MSVC
248   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
249 #else
250   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
251 #endif
252
253         wxConfigBase::Set(m_pConfig);
254   m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
255   m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
256 }
257
258 void
259 CTSimApp::closeConfig()
260 {
261   m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
262   m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
263         delete m_pConfig;
264 }
265
266
267 wxString
268 CTSimApp::getUntitledFilename()
269 {
270         static int untitledNumber = 1;
271         
272         wxString filename ("Untitled");
273         filename << untitledNumber++;
274         
275         return (filename);
276 }
277
278
279 // Top-level window for CTSim
280
281 #if CTSIM_MDI
282 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
283
284 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
285 #else
286 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
287
288 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
289 #endif
290
291 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
292 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
293 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
294 EVT_MENU(MAINMENU_HELP_TOPICS, MainFrame::OnHelpTopics)
295 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
296 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
297 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
298 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
299 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
300 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
301 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
302 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
303 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
304 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
305 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
306 EVT_SIZE(MainFrame::OnSize)
307
308 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
309 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
310 #endif
311 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
312 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
313 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
314 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
315 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
316 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
317 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
318 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
319 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
320 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
321 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
322 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
323 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
324 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
325 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
326 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
327 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
328 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
329 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
330 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
331 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
332 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
333 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
334 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
335 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
336 END_EVENT_TABLE()
337
338
339
340 #if CTSIM_MDI
341 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
342 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
343 #else
344 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
345 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
346 #endif
347 {
348   m_bShuttingDown = false;
349
350         //// Make a menubar
351         wxMenu *file_menu = new wxMenu;
352         
353         file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
354         file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
355         file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
356         
357         file_menu->AppendSeparator();
358   file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
359         file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
360         
361         //  history of files visited
362         theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
363         theApp->getDocManager()->FileHistoryUseMenu(file_menu);
364         
365 #ifndef CTSIM_MDI
366         m_pWindowMenu = new wxMenu;
367         m_pWindowMenu->UpdateUI (this);
368 #endif
369         
370         wxMenu* help_menu = new wxMenu;
371         help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
372         help_menu->Append (MAINMENU_HELP_TOPICS, "&Topics\tCtrl-T");
373 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
374         help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
375 #endif
376         help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
377         
378         wxMenuBar* menu_bar = new wxMenuBar;
379         
380         menu_bar->Append(file_menu, "&File");
381 #ifndef CTSIM_MDI
382         menu_bar->Append(m_pWindowMenu, "&Window");
383 #endif
384         menu_bar->Append(help_menu, "&Help");
385         
386         SetMenuBar(menu_bar);
387         
388 #ifndef CTSIM_MDI
389         int i;
390         for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
391                 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
392                 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
393                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
394         }
395 #endif
396         
397         m_iDefaultPhantomID = Phantom::PHM_HERMAN;
398         m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
399         m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
400         m_iDefaultFilterXSize = 256;
401         m_iDefaultFilterYSize = 256;
402         m_dDefaultFilterParam = 1.;
403         m_dDefaultFilterBandwidth = 1.;
404         m_dDefaultFilterInputScale = 1.;
405         m_dDefaultFilterOutputScale = 1.;
406         
407         wxAcceleratorEntry accelEntries[15];
408         accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
409         accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
410         accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
411         accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
412 #ifndef CTSIM_MDI
413         accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
414         for (i = 0; i < 10; i++)
415                 accelEntries[i+4].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
416         wxAcceleratorTable accelTable (15, accelEntries);
417 #else
418         wxAcceleratorTable accelTable (4, accelEntries);
419 #endif
420         
421         SetAcceleratorTable (accelTable);
422 }
423
424 MainFrame::~MainFrame()
425 {
426   m_bShuttingDown = true; // Currently used so that Log Window will close
427 #if 0
428   // delete all non-modified documents
429         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
430         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
431                 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
432     if (pDoc && ! pDoc->IsModified()) {
433       theApp->getDocManager()->RemoveDocument(pDoc);
434       delete pDoc;
435     }
436         }
437 #endif
438   ::wxYield();
439         if (theApp->getConfig())
440                 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
441   ::wxYield();
442         delete theApp->getDocManager(); 
443
444 }
445
446 void
447 MainFrame::OnSize (wxSizeEvent& event)
448 {
449 #ifdef CTSIM_MDI
450     if (theApp->getLogDoc()) {
451         int xSize, ySize;       
452                 GetClientSize(&xSize, &ySize);
453                 int yLogSize = ySize / 4;
454                   theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
455                   theApp->getLogDoc()->getView()->getFrame()->Show (true);
456     }
457 #endif
458         
459 #if CTSIM_MDI
460         wxDocMDIParentFrame::OnSize (event);
461 #else
462         wxDocParentFrame::OnSize (event);
463 #endif
464 }
465
466 void 
467 MainFrame::OnCreatePhantom(wxCommandEvent& event)
468 {
469         DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
470         int dialogReturn = dialogPhantom.ShowModal();
471         if (dialogReturn == wxID_OK) {
472                 wxString selection (dialogPhantom.getPhantom());
473                 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
474                 wxString filename = selection + ".phm";
475                 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
476                 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
477         }
478         
479 }
480
481 void 
482 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
483 {
484         DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
485         int dialogReturn = dialogFilter.ShowModal();
486         if (dialogReturn == wxID_OK) {
487                 wxString strFilter (dialogFilter.getFilterName());
488                 wxString strDomain (dialogFilter.getDomainName());
489                 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
490                 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
491                 m_iDefaultFilterXSize = dialogFilter.getXSize();
492                 m_iDefaultFilterYSize = dialogFilter.getYSize();
493                 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
494                 m_dDefaultFilterParam= dialogFilter.getFilterParam();
495                 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
496                 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
497                 std::ostringstream os;
498                 os << "Generate Filter=" << strFilter.c_str() 
499                         << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
500                         << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
501                         << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
502                 *theApp->getLog() << os.str().c_str() << "\n";
503                 wxString filename = "untitled.if";
504                 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
505     pFilterDoc->setBadFileOpen();
506                 if (! pFilterDoc) {
507                         sys_error (ERR_SEVERE, "Unable to create filter image");
508                         return;
509                 }
510                 ImageFile& rIF = pFilterDoc->getImageFile();
511                 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
512                 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
513                 rIF.labelAdd (os.str().c_str());
514     pFilterDoc->Modify (true);
515                 pFilterDoc->UpdateAllViews();
516                 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
517                 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
518                 pFilterDoc->getView()->getFrame()->Show(true);
519         }
520 }
521
522 void
523 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
524 {
525         const ImageFile& rIF = pIFDoc->getImageFile();
526         unsigned int nx = rIF.nx();
527         unsigned int ny = rIF.ny();
528         wxList& rListDocs = m_docManager->GetDocuments();
529         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
530                 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
531                 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
532                 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
533                         const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
534                         if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
535                                 vecIF.push_back (pIFCompareDoc);
536                 }
537         }
538 }
539
540 void 
541 MainFrame::OnHelpTopics (wxCommandEvent& event)
542 {
543         showHelp (event.GetId());
544 }
545
546 void 
547 MainFrame::OnHelpContents (wxCommandEvent& event)
548 {
549         showHelp (event.GetId());
550 }
551
552 void 
553 MainFrame::OnHelpButton (wxCommandEvent& event)
554 {
555         showHelp (event.GetId());
556 }
557
558 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
559 void
560 MainFrame::OnHelpSecondary (wxCommandEvent& event)
561 {
562         m_htmlHelp.DisplayContents();
563 }
564 #endif
565
566 void
567 MainFrame::showHelp (int commandID)
568 {
569         switch (commandID) {
570                 
571         case MAINMENU_HELP_CONTENTS:
572 #ifdef CTSIM_WINHELP
573                 m_winHelp.DisplayContents ();
574 #else
575                 m_htmlHelp.DisplayContents ();
576 #endif
577                 break;
578                 
579         case MAINMENU_HELP_TOPICS:
580 #ifdef CTSIM_WINHELP
581                 m_winHelp.DisplaySection (IDH_INTRODUCTION);
582 #else
583                 m_htmlHelp.DisplayIndex();
584 #endif
585                 break;
586                 
587         default:
588 #ifdef CTSIM_WINHELP
589     m_winHelp.DisplaySection (commandID);
590 #else
591     m_htmlHelp.DisplaySection (commandID);
592 #endif
593                 break;
594         }
595 }
596
597 void 
598 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
599 {
600         Close(true);
601 }
602
603 void
604 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
605 {
606 #ifndef CTSIM_MDI
607         int iPos = 0;
608         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
609         wxNode* pNode = rListDocs.GetFirst();
610         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
611                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
612                 wxString strFilename = pDoc->GetFilename();
613                 if (iPos < 10) {
614                         strFilename += "\tCtrl-";
615                         strFilename += static_cast<char>('0' + iPos);
616                 }
617                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
618                 m_apWindowMenuData[iPos] = pDoc;
619                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
620                 iPos++;
621                 pNode = pNode->GetNext();
622         }
623         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
624                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
625                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
626                 m_apWindowMenuData[i] = NULL;
627         }
628 #endif  
629 }
630
631 #ifdef CTSIM_CUSTOM_MRU
632 void 
633 MainFrame::OnMRUFile (wxCommandEvent& event)
634 {
635         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
636         if (fileName != "")
637                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
638 }
639 #endif
640
641 void 
642 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
643 {
644         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
645                 wxString strFilename = pDoc->GetFilename();
646                 const wxView* pView = pDoc->GetFirstView();
647                 if (pView) {
648                         wxFrame* pFrame = pView->GetFrame();
649                         pFrame->SetFocus();
650                         pFrame->Raise();
651                 }
652         }
653 }
654
655 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
656 { DoWindowMenu (0, event); }
657
658 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
659 { DoWindowMenu (1, event); }
660
661 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
662 { DoWindowMenu (2, event); }
663
664 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
665 { DoWindowMenu (3, event); }
666
667 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
668 { DoWindowMenu (4, event); }
669
670 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
671 { DoWindowMenu (5, event); }
672
673 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
674 { DoWindowMenu (6, event); }
675
676 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
677 { DoWindowMenu (7, event); }
678
679 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
680 { DoWindowMenu (8, event); }
681
682 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
683 { DoWindowMenu (9, event); }
684
685 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
686 { DoWindowMenu (10, event); }
687
688 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
689 { DoWindowMenu (11, event); }
690
691 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
692 { DoWindowMenu (12, event); }
693
694 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
695 { DoWindowMenu (13, event); }
696
697 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
698 { DoWindowMenu (14, event); }
699
700 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
701 { DoWindowMenu (15, event); }
702
703 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
704 { DoWindowMenu (16, event); }
705
706 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
707 { DoWindowMenu (17, event); }
708
709 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
710 { DoWindowMenu (18, event); }
711
712 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
713 { DoWindowMenu (19, event); }
714
715
716 class BitmapControl : public wxPanel
717 {
718 private:  
719         DECLARE_DYNAMIC_CLASS (BitmapControl)
720                 DECLARE_EVENT_TABLE ()
721                 wxBitmap* m_pBitmap;
722         
723 public:
724         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
725                 const wxPoint& pos = wxDefaultPosition,
726                 const wxSize& size = wxDefaultSize,
727                 long style = wxSTATIC_BORDER,
728                 const wxValidator& validator = wxDefaultValidator,
729                 const wxString& name = "BitmapCtrl");
730         
731         
732         virtual ~BitmapControl();
733         
734         virtual wxSize GetBestSize() const;
735         
736         wxBitmap* getBitmap() 
737         { return m_pBitmap; }
738         
739         void OnPaint(wxPaintEvent& event);
740 };
741
742
743 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
744 EVT_PAINT(BitmapControl::OnPaint)
745 END_EVENT_TABLE()
746
747 IMPLEMENT_CLASS(BitmapControl, wxPanel)
748
749
750 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
751                               long style, const wxValidator& validator, const wxString& name)
752                               : m_pBitmap(pBitmap)
753 {
754         Create(parent, id, pos, size, style, name);
755         
756         SetSize (GetBestSize());
757 }
758
759 wxSize
760 BitmapControl::GetBestSize () const
761 {
762         if (m_pBitmap)
763                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
764         else
765                 return wxSize(0,0);
766 }
767
768 BitmapControl::~BitmapControl()
769 {}
770
771 void
772 BitmapControl::OnPaint (wxPaintEvent& event)
773 {
774         wxPaintDC dc(this);
775         if (m_pBitmap)
776                 dc.DrawBitmap (*m_pBitmap, 0, 0);
777 }
778
779
780 class BitmapDialog : public wxDialog {
781 private:
782         BitmapControl* m_pBitmapCtrl;
783         
784 public:
785         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
786         virtual ~BitmapDialog();
787 };
788
789 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
790 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
791 {
792         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
793         
794         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
795         
796         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
797         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
798         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
799         
800         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
801         
802         SetAutoLayout (true);
803         SetSizer (pTopSizer);
804         pTopSizer->Fit (this);
805         pTopSizer->SetSizeHints (this);
806 }
807
808 BitmapDialog::~BitmapDialog()
809 {}
810
811
812 void 
813 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
814 {
815   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
816     theApp->getAskDeleteNewDocs());
817   if (dlg.ShowModal() == wxID_OK) {
818     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
819     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
820   }
821 }
822
823
824 #include "./splash.xpm"
825 void 
826 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
827 {
828         wxBitmap bmp (splash);
829         if (bmp.Ok()) {
830                 BitmapDialog dlg (&bmp, "About CTSim");
831                 dlg.Show(true);
832         } else {
833                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
834 #ifdef CTSIMVERSION
835                 msg += "Version ";
836                 msg += CTSIMVERSION;
837                 msg += "\n\n";
838 #elif defined(VERSION)
839                 msg << "Version: " <<  VERSION << "\n\n";
840 #endif
841                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
842                 
843                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
844         }
845 }
846
847
848 ProjectionFileDocument*
849 CTSimApp::newProjectionDoc()
850 {
851         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
852         if (newDoc) {
853                 ProjectionFileView* pView = newDoc->getView();
854                 if (pView) {
855                         wxFrame* pFrame = pView->getFrame();
856                         if (pFrame) {
857                                 pFrame->SetSize (0,0);
858                                 pFrame->Show (false);
859                         }
860                 }
861     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
862     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
863     newDoc->OnNewDocument();
864         }
865         
866         return newDoc;
867 }
868
869 ImageFileDocument*
870 CTSimApp::newImageDoc()
871 {
872         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
873         if (newDoc) {
874                 ImageFileView* pView = newDoc->getView();
875                 if (pView) {
876                         wxFrame* pFrame = pView->getFrame();
877                         if (pFrame) {
878                                 pFrame->SetSize (0,0);
879                                 pFrame->Show (false);
880                         }
881                 }
882     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
883     newDoc->SetDocumentTemplate (m_pDocTemplImage);
884     newDoc->OnNewDocument();
885         }
886         
887         return newDoc;
888 }
889
890 PlotFileDocument*
891 CTSimApp::newPlotDoc()
892 {
893         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
894         if (newDoc) {
895                 PlotFileView* pView = newDoc->getView();
896                 if (pView) {
897                         wxFrame* pFrame = pView->getFrame();
898                         if (pFrame) {
899                                 wxSize size;
900                                 m_pFrame->GetClientSize (&size.x, &size.y);
901                                 pFrame->SetSize (size.x / 2, size.y / 2);
902                                 pFrame->Show (false);
903                         }
904                 }
905     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
906     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
907     newDoc->OnNewDocument();
908         }
909         
910         return newDoc;
911 }
912
913
914 TextFileDocument*
915 CTSimApp::newTextDoc()
916 {
917         wxString strFilename (getUntitledFilename());
918         strFilename += ".txt";
919         
920         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
921         if (newDoc) {
922                 TextFileView* pView = newDoc->getView();
923                 if (pView) {
924                         wxFrame* pFrame = pView->getFrame();
925                         if (pFrame) {
926                                 wxSize size;
927                                 m_pFrame->GetClientSize (&size.x, &size.y);;
928                                 pFrame->SetSize (size.x / 2, size.y / 2);
929                                 pFrame->Show (false);
930                         }
931                 }
932     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
933     newDoc->SetDocumentTemplate (m_pDocTemplText);
934     newDoc->OnNewDocument();
935         }
936         
937         return newDoc;
938 }
939
940
941 PhantomFileDocument*
942 CTSimApp::newPhantomDoc()
943 {
944         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
945         if (newDoc) {
946                 PhantomFileView* pView = newDoc->getView();
947                 if (pView) {
948                         wxFrame* pFrame = pView->getFrame();
949                         if (pFrame)
950                                 pFrame->SetSize (0,0);
951                 }
952     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
953     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
954     newDoc->OnNewDocument();
955         }
956         
957         return newDoc;
958 }
959
960 #if wxUSE_GLCANVAS
961
962 Graph3dFileDocument*
963 CTSimApp::newGraph3dDoc()
964 {
965         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
966         if (newDoc) {
967                 Graph3dFileView* pView = newDoc->getView();
968                 if (pView) {
969                         wxFrame* pFrame = pView->getFrame();
970                         if (pFrame)
971                                 pFrame->SetSize (0,0);
972                 }
973     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
974     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
975     newDoc->OnNewDocument();
976         }
977         
978         return newDoc;
979 }
980 #endif