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