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