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