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