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