r435: *** empty log 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.41 2001/01/26 00:45:24 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.41 2001/01/26 00:45:24 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)
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");
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   new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
137   new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
138   new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
139   new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
140   new wxDocTemplate (m_docManager, "TextEdit", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextEditDocument), CLASSINFO(TextEditView), wxTEMPLATE_INVISIBLE);
141   
142 #if wxUSE_GIF
143   wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
144 #endif
145   
146 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
147   wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
148 #endif
149   
150   // Create the main frame window
151   m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
152   
153   setIconForFrame (m_pFrame);
154   m_pFrame->Centre(wxBOTH);
155   m_pFrame->Show(true);  
156   SetTopWindow (m_pFrame);
157
158   for (int i = optind + 1; i <= argc; i++) {
159     wxString filename = argv [i - 1];
160     m_docManager->CreateDocument (filename, wxDOC_SILENT);
161   }
162   
163   if (m_pConfig)
164     m_docManager->FileHistoryLoad(*m_pConfig);
165
166 #ifdef CTSIM_WINHELP
167   if (! m_pFrame->getWinHelpController().Initialize("ctsim"))
168     ::wxMessageBox ("Cannot initialize the help system", "Error");
169 #endif
170   if (! m_pFrame->getHtmlHelpController().Initialize(::wxGetCwd() + "/ctsim"))
171     ::wxMessageBox ("Cannot initialize the help system", "Error");
172
173 #ifdef CTSIM_MDI
174   TextEditDocument* pLogDoc = dynamic_cast<TextEditDocument*>(m_docManager->CreateDocument("Log.txt", wxDOC_SILENT));
175   if (pLogDoc) {
176     m_pLog = pLogDoc->getTextCtrl();
177   } else
178 #else
179     m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
180 #endif
181   wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
182
183   return true;
184 }
185
186
187 #include "./ctsim.xpm"
188 void
189 CTSimApp::setIconForFrame(wxFrame* pFrame)
190 {
191   wxIcon iconApp (ctsim16_xpm);
192   
193   if (iconApp.Ok())
194     pFrame->SetIcon (iconApp);
195 }
196
197 void
198 CTSimApp::usage(const char* program)
199 {
200   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
201   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
202   std::cout << "\n";
203   std::cout << "  --version Display version\n";
204   std::cout << "  --help    Display this help message\n";
205 }
206
207 int
208 CTSimApp::OnExit()
209 {
210     if (theApp->getConfig())
211       theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
212     delete theApp->getDocManager();
213   m_docManager = NULL;
214
215 #ifdef HAVE_DMALLOC
216   dmalloc_shutdown();
217 #endif
218   return 0;
219 }
220
221 wxString
222 CTSimApp::getUntitledFilename()
223 {
224   static int untitledNumber = 1;
225   
226   wxString filename ("Untitled");
227   filename << untitledNumber++;
228   
229   return (filename);
230 }
231
232
233 // Top-level window for CTSim
234
235 #if CTSIM_MDI
236 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
237
238 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
239 #else
240 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
241
242 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
243 #endif
244
245 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
246 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
247 EVT_MENU(MAINMENU_HELP_TOPICS, MainFrame::OnHelpTopics)
248 EVT_SIZE(MainFrame::OnSize)
249
250 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
251 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
252 #endif
253 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
254 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
255 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
256 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
257 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
258 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
259 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
260 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
261 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
262 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
263 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
264 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
265 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
266 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
267 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
268 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
269 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
270 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
271 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
272 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
273 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
274 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
275 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
276 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
277 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
278 END_EVENT_TABLE()
279
280
281
282 #if CTSIM_MDI
283 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
284 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
285 #else
286 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
287 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
288 #endif
289 {
290   //// Make a menubar
291   wxMenu *file_menu = new wxMenu;
292   
293   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
294   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
295   file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
296   
297   file_menu->AppendSeparator();
298   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
299   
300   //  history of files visited
301   theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
302   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
303
304 #ifndef CTSIM_MDI
305   m_pWindowMenu = new wxMenu;
306   m_pWindowMenu->UpdateUI (this);
307 #endif
308
309   wxMenu* help_menu = new wxMenu;
310   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
311   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-T");
312 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
313   help_menu->Append(MAINMENU_HELP_SECONDARY, "&Secondary Help");
314 #endif
315   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
316   
317   wxMenuBar* menu_bar = new wxMenuBar;
318   
319   menu_bar->Append(file_menu, "&File");
320 #ifndef CTSIM_MDI
321   menu_bar->Append(m_pWindowMenu, "&Window");
322 #endif
323   menu_bar->Append(help_menu, "&Help");
324   
325   SetMenuBar(menu_bar);
326   
327 #ifndef CTSIM_MDI
328   for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
329     m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
330     m_pWindowMenu->Append (m_apWindowMenuItems[i]);
331     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
332   }
333 #endif
334
335   m_iDefaultPhantomID = Phantom::PHM_HERMAN;
336   m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
337   m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
338   m_iDefaultFilterXSize = 256;
339   m_iDefaultFilterYSize = 256;
340   m_dDefaultFilterParam = 1.;
341   m_dDefaultFilterBandwidth = 1.;
342   m_dDefaultFilterInputScale = 1.;
343   m_dDefaultFilterOutputScale = 1.;
344   
345   wxAcceleratorEntry accelEntries[15];
346   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
347   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
348   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
349   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
350 #ifndef CTSIM_MDI
351   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
352   for (i = 0; i < 10; i++)
353     accelEntries[i+4].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
354   wxAcceleratorTable accelTable (15, accelEntries);
355 #else
356   wxAcceleratorTable accelTable (4, accelEntries);
357 #endif
358
359   SetAcceleratorTable (accelTable);
360 }
361
362 MainFrame::~MainFrame()
363 {
364 }
365
366 void
367 MainFrame::OnSize (wxSizeEvent& event)
368 {
369   wxSize sizeClient = GetClientSize();
370   wxSize sizeLog = sizeClient;
371   if (sizeClient.y < 100) {
372     sizeLog.y = 0;
373     sizeLog.x = 0;
374   } else {
375     sizeLog.x = sizeClient.x;
376     sizeLog.y = 100;
377   }
378   sizeClient.x -= sizeLog.x;
379   sizeClient.y -= sizeLog.y;
380 //  m_pLog->SetSize (0, sizeClient.y, sizeLog.x, sizeLog.y);
381 //  GetClientWindow()->SetSize (0, 0, sizeClient.x, sizeClient.y);
382 //  GetClientWindow()->Refresh();
383 #if CTSIM_MDI
384   wxDocMDIParentFrame::OnSize (event);
385 #else
386   wxDocParentFrame::OnSize (event);
387 #endif
388 }
389
390 void 
391 MainFrame::OnCreatePhantom(wxCommandEvent& event)
392 {
393   DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
394   int dialogReturn = dialogPhantom.ShowModal();
395   if (dialogReturn == wxID_OK) {
396     wxString selection (dialogPhantom.getPhantom());
397     *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
398     wxString filename = selection + ".phm";
399     m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
400     theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
401   }
402   
403 }
404
405 void 
406 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
407 {
408   DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
409   int dialogReturn = dialogFilter.ShowModal();
410   if (dialogReturn == wxID_OK) {
411     wxString strFilter (dialogFilter.getFilterName());
412     wxString strDomain (dialogFilter.getDomainName());
413     m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
414     m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
415     m_iDefaultFilterXSize = dialogFilter.getXSize();
416     m_iDefaultFilterYSize = dialogFilter.getYSize();
417     m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
418     m_dDefaultFilterParam= dialogFilter.getFilterParam();
419     m_dDefaultFilterInputScale = dialogFilter.getInputScale();
420     m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
421     std::ostringstream os;
422     os << "Generate Filter=" << strFilter.c_str() 
423       << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
424       << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
425       << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
426     *theApp->getLog() << os.str().c_str() << "\n";
427     wxString filename = "untitled.if";
428     ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
429     if (! pFilterDoc) {
430       sys_error (ERR_SEVERE, "Unable to create filter image");
431       return;
432     }
433     ImageFile& rIF = pFilterDoc->getImageFile();
434     rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
435     rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
436     rIF.labelAdd (os.str().c_str());
437     if (theApp->getSetModifyNewDocs())
438       pFilterDoc->Modify (true);
439     pFilterDoc->UpdateAllViews();
440     pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
441   }
442 }
443
444 void
445 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
446 {
447   const ImageFile& rIF = pIFDoc->getImageFile();
448   unsigned int nx = rIF.nx();
449   unsigned int ny = rIF.ny();
450   wxList& rListDocs = m_docManager->GetDocuments();
451   for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
452     wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
453     ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
454     if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
455       const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
456       if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
457         vecIF.push_back (pIFCompareDoc);
458     }
459   }
460 }
461
462 void 
463 MainFrame::OnHelpTopics (wxCommandEvent& event)
464 {
465   showHelp (event.GetId());
466 }
467
468 void 
469 MainFrame::OnHelpContents (wxCommandEvent& event)
470 {
471   showHelp (event.GetId());
472 }
473
474 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
475 void
476 MainFrame::OnHelpSecondary (wxCommandEvent& event)
477 {
478     m_htmlHelp.DisplayContents();
479 }
480 #endif
481
482 void
483 MainFrame::showHelp (int commandID)
484 {
485 #ifdef CTSIM_WINHELP
486   m_winHelp.LoadFile();
487 #else
488   m_htmlHelp.LoadFile();
489 #endif
490   
491   switch (commandID) {
492
493   case MAINMENU_HELP_CONTENTS:
494 #ifdef CTSIM_WINHELP
495     m_winHelp.DisplayContents ();
496 #else
497     m_htmlHelp.DisplayContents ();
498 #endif
499     break;
500
501   case MAINMENU_HELP_TOPICS:
502 #ifdef CTSIM_WINHELP
503     m_winHelp.DisplaySection (introduction);
504 #else
505     m_htmlHelp.DisplayIndex();
506 #endif
507     break;
508
509   default:
510     *theApp->getLog() << "Unknown help command # " << commandID << "\n";
511     break;
512   }
513 }
514
515 void 
516 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
517 {
518   Close(true);
519 }
520
521 void
522 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
523 {
524   int iPos = 0;
525   wxList& rListDocs = theApp->getDocManager()->GetDocuments();
526   wxNode* pNode = rListDocs.GetFirst();
527   while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
528     wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
529     wxString strFilename = pDoc->GetFilename();
530     if (iPos < 10) {
531       strFilename += "\tCtrl-";
532       strFilename += static_cast<char>('0' + iPos);
533     }
534     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
535     m_apWindowMenuData[iPos] = pDoc;
536     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
537     iPos++;
538     pNode = pNode->GetNext();
539   }
540   for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
541     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
542     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
543     m_apWindowMenuData[i] = NULL;
544   }
545   
546 }
547
548 #ifdef CTSIM_CUSTOM_MRU
549 void 
550 MainFrame::OnMRUFile (wxCommandEvent& event)
551 {
552    wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
553    if (fileName != "")
554      theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
555 }
556 #endif
557
558 void 
559 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
560 {
561   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
562     wxString strFilename = pDoc->GetFilename();
563     const wxView* pView = pDoc->GetFirstView();
564     if (pView) {
565       wxFrame* pFrame = pView->GetFrame();
566       pFrame->SetFocus();
567       pFrame->Raise();
568     }
569   }
570 }
571
572 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
573 { DoWindowMenu (0, event); }
574
575 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
576 { DoWindowMenu (1, event); }
577
578 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
579 { DoWindowMenu (2, event); }
580
581 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
582 { DoWindowMenu (3, event); }
583
584 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
585 { DoWindowMenu (4, event); }
586
587 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
588 { DoWindowMenu (5, event); }
589
590 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
591 { DoWindowMenu (6, event); }
592
593 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
594 { DoWindowMenu (7, event); }
595
596 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
597 { DoWindowMenu (8, event); }
598
599 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
600 { DoWindowMenu (9, event); }
601
602 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
603 { DoWindowMenu (10, event); }
604
605 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
606 { DoWindowMenu (11, event); }
607
608 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
609 { DoWindowMenu (12, event); }
610
611 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
612 { DoWindowMenu (13, event); }
613
614 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
615 { DoWindowMenu (14, event); }
616
617 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
618 { DoWindowMenu (15, event); }
619
620 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
621 { DoWindowMenu (16, event); }
622
623 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
624 { DoWindowMenu (17, event); }
625
626 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
627 { DoWindowMenu (18, event); }
628
629 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
630 { DoWindowMenu (19, event); }
631
632
633 class BitmapControl : public wxPanel
634 {
635 private:  
636   DECLARE_DYNAMIC_CLASS (BitmapControl)
637     DECLARE_EVENT_TABLE ()
638     wxBitmap* m_pBitmap;
639   
640 public:
641   BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
642     const wxPoint& pos = wxDefaultPosition,
643     const wxSize& size = wxDefaultSize,
644     long style = wxSTATIC_BORDER,
645     const wxValidator& validator = wxDefaultValidator,
646     const wxString& name = "BitmapCtrl");
647   
648   
649   virtual ~BitmapControl();
650   
651   virtual wxSize GetBestSize() const;
652   
653   wxBitmap* getBitmap() 
654   { return m_pBitmap; }
655   
656   void OnPaint(wxPaintEvent& event);
657 };
658
659
660 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
661 EVT_PAINT(BitmapControl::OnPaint)
662 END_EVENT_TABLE()
663
664 IMPLEMENT_CLASS(BitmapControl, wxPanel)
665
666
667 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
668                               long style, const wxValidator& validator, const wxString& name)
669                               : m_pBitmap(pBitmap)
670 {
671   Create(parent, id, pos, size, style, name);
672   
673   SetSize (GetBestSize());
674 }
675
676 wxSize
677 BitmapControl::GetBestSize () const
678 {
679   if (m_pBitmap)
680     return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
681   else
682     return wxSize(0,0);
683 }
684
685 BitmapControl::~BitmapControl()
686 {}
687
688 void
689 BitmapControl::OnPaint (wxPaintEvent& event)
690 {
691   wxPaintDC dc(this);
692   if (m_pBitmap)
693     dc.DrawBitmap (*m_pBitmap, 0, 0);
694 }
695
696
697 class BitmapDialog : public wxDialog {
698 private:
699   BitmapControl* m_pBitmapCtrl;
700   
701 public:
702   BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
703   virtual ~BitmapDialog();
704 };
705
706 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
707 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
708 {
709   wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
710   
711   pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
712   
713   wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
714   wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
715   pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
716   
717   pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
718   
719   SetAutoLayout (true);
720   SetSizer (pTopSizer);
721   pTopSizer->Fit (this);
722   pTopSizer->SetSizeHints (this);
723 }
724
725 BitmapDialog::~BitmapDialog()
726 {}
727
728
729 #include "./splash.xpm"
730 void 
731 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
732 {
733   wxBitmap bmp (splash);
734   if (bmp.Ok()) {
735     BitmapDialog dlg (&bmp, "About CTSim");
736     dlg.Show(true);
737   } else {
738     wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
739 #ifdef CTSIMVERSION
740     msg += "Version ";
741     msg += CTSIMVERSION;
742     msg += "\n\n";
743 #elif defined(VERSION)
744     msg << "Version: " <<  VERSION << "\n\n";
745 #endif
746     msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
747     
748     wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
749   }
750 }
751