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