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