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