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