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