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