r570: 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-2001 Kevin Rosenberg
11 **
12 **  $Id: ctsim.cpp,v 1.86 2001/02/22 00:56:50 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 #ifdef __WXMSW__
47 #include "wx/msw/helpchm.h"
48 #endif
49
50 #if !wxUSE_DOC_VIEW_ARCHITECTURE
51 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
52 #endif
53
54
55 #include "ct.h"
56 #include "ctsim.h"
57 #include "docs.h"
58 #include "views.h"
59 #include "dialogs.h"
60 #include "tips.h"
61
62 #if defined(HAVE_CONFIG_H)
63 #include "config.h"
64 #endif
65
66 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
67 #ifdef MSVC
68 #define __STDC__ 1
69 #endif
70 #include "getopt.h"
71 #ifdef MSVC
72 #undef __STDC__
73 #endif
74 #endif
75
76 static const char* rcsindent = "$Id: ctsim.cpp,v 1.86 2001/02/22 00:56:50 kevin Exp $";
77
78 struct option CTSimApp::ctsimOptions[] = 
79 {
80         {"help", 0, 0, O_HELP},
81         {"version", 0, 0, O_VERSION},
82   {"print", 0, 0, O_PRINT},
83         {0, 0, 0, 0}
84 };
85
86 IMPLEMENT_APP(CTSimApp)
87
88 CTSimApp::CTSimApp()
89 :  m_bAdvancedOptions(false), m_bSetModifyNewDocs(true), m_bVerboseLogging(false), m_bShowStartupTips(true),
90    m_iCurrentTip(0),
91   m_docManager(NULL), m_pFrame(NULL), m_pLog(0), m_pLogDoc(0), m_pConfig(0)
92 {
93         theApp = this;
94 }
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99
100 #ifdef HAVE_SYS_RESOURCE_H
101 #include <sys/resource.h>
102 #endif
103
104 bool
105 CTSimApp::OnInit()
106 {
107 #ifdef HAVE_SETPRIORITY
108         setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
109 #endif
110         
111         openConfig();
112         
113         g_bRunningWXWindows = true;
114   bool bPrintFiles = false;
115         // process options
116         while (1) {
117                 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
118                 if (c == -1)
119                         break;
120                 
121                 switch (c) {
122                 case O_VERSION:
123                         std::cout << rcsindent << std::endl;
124 #ifdef CTSIMVERSION
125                         std::cout << "Version: CTSIMVERSION" << std::endl;
126 #elif defined(VERSION)
127                         std::cout << "Version: VERSION" << std::endl;
128 #endif
129                         exit(0);
130                 case O_HELP:
131                 case '?':
132                         usage (argv[0]);
133                         exit (0);
134     case O_PRINT:
135       bPrintFiles = true;
136       break;
137                 default:
138                         usage (argv[0]);
139                         exit (1);
140                 }
141         }
142         
143         m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
144         
145         m_pDocTemplImage = new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
146         m_pDocTemplProjection = new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
147         m_pDocTemplPhantom = new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
148         m_pDocTemplPlot = new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
149         m_pDocTemplText = new wxDocTemplate (m_docManager, "TextFile", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
150 #if wxUSE_GLCANVAS
151         m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, "Graph3dFile", "*.g3d", "", "g3d", "Graph3dFile", "Graph3dView", CLASSINFO(Graph3dFileDocument), CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
152 #endif
153
154 #if wxUSE_GIF
155         wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
156 #endif
157         
158 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
159         wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
160 #endif
161         
162         // Create the main frame window
163         int xDisplay, yDisplay;
164         ::wxDisplaySize (&xDisplay, &yDisplay);
165         m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), 
166                 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .755)), wxDEFAULT_FRAME_STYLE);
167         
168         setIconForFrame (m_pFrame);
169         m_pFrame->Centre(wxBOTH);
170         m_pFrame->Show(true);  
171         SetTopWindow (m_pFrame);
172         
173         if (m_pConfig)
174                 m_docManager->FileHistoryLoad(*m_pConfig);
175         
176 #ifdef CTSIM_MDI
177         m_pLogDoc = newTextDoc();
178         if (m_pLogDoc) {
179                 m_pLog = m_pLogDoc->getTextCtrl();
180                 m_pLogDoc->SetDocumentName("Log.txt");
181                 m_pLogDoc->SetFilename("Log.txt");
182                 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
183                 int xSize, ySize;
184                 m_pFrame->GetClientSize(&xSize, &ySize);
185                 int yLogSize = ySize / 4;
186                 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
187                 m_pLogDoc->getView()->getFrame()->Show (true);
188         } else
189 #else
190                 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
191 #endif
192         wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
193
194         wxString helpDir;
195         if (! m_pConfig->Read("HelpDir", &helpDir))
196                 helpDir = ::wxGetCwd();
197 #ifdef CTSIM_WINHELP
198         if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
199                 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
200 #else
201         if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
202                 ! m_pFrame->getHtmlHelpController().Initialize("/usr/local/man/ctsim"))
203                 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
204         else {
205             if (::wxDirExists ("/tmp"))
206                 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
207             m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
208         }
209 #endif
210
211         for (int i = optind + 1; i <= argc; i++) {
212                 wxString filename = argv [i - 1];
213                 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
214     if (bPrintFiles) {
215       wxView* pNewView = pNewDoc->GetFirstView();
216       wxPrintout *printout = pNewView->OnCreatePrintout();
217       if (printout) {
218         wxPrinter printer;
219         printer.Print(pNewView->GetFrame(), printout, TRUE);
220         delete printout;
221       }
222       wxCommandEvent nullEvent;
223       nullEvent.SetId (wxID_CLOSE);
224       m_docManager->OnFileClose (nullEvent);
225     }
226         }
227   if (bPrintFiles) {
228     wxCommandEvent closeEvent;
229     closeEvent.SetInt (MAINMENU_FILE_EXIT);
230     m_pFrame->AddPendingEvent(closeEvent);
231   }
232
233   if (getStartupTips())
234     ShowTips();
235
236   return true;
237 }
238
239 void
240 CTSimApp::ShowTips()
241 {
242     CTSimTipProvider tipProvider (m_iCurrentTip);
243     setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
244     m_iCurrentTip = tipProvider.GetCurrentTip();
245 }
246
247
248 #include "./ctsim.xpm"
249 void
250 CTSimApp::setIconForFrame(wxFrame* pFrame)
251 {
252         wxIcon iconApp (ctsim16_xpm);
253         
254         if (iconApp.Ok())
255                 pFrame->SetIcon (iconApp);
256 }
257
258 void
259 CTSimApp::usage(const char* program)
260 {
261         std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
262         std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
263         std::cout << "\n";
264         std::cout << "  --version Display version\n";
265         std::cout << "  --help    Display this help message\n";
266 }
267
268 int
269 CTSimApp::OnExit()
270 {
271   closeConfig();
272
273 #ifdef HAVE_DMALLOC
274         dmalloc_shutdown();
275 #endif
276         return 0;
277 }
278
279 void
280 CTSimApp::openConfig()
281 {
282 #ifdef MSVC
283   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
284 #else
285   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
286 #endif
287
288         wxConfigBase::Set(m_pConfig);
289   m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
290   m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
291   m_pConfig->Read ("VerboseLogging", &m_bVerboseLogging);
292   m_pConfig->Read ("StartupTips", &m_bShowStartupTips);
293   m_pConfig->Read ("CurrentTip", &m_iCurrentTip);
294 }
295
296 void
297 CTSimApp::closeConfig()
298 {
299   m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
300   m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
301   m_pConfig->Write ("VerboseLogging", m_bVerboseLogging);
302   m_pConfig->Write ("StartupTips", m_bShowStartupTips);
303   m_pConfig->Write ("CurrentTip", m_iCurrentTip);
304
305         delete m_pConfig;
306 }
307
308
309 wxString
310 CTSimApp::getUntitledFilename()
311 {
312         static int untitledNumber = 1;
313         
314         wxString filename ("Untitled");
315         filename << untitledNumber++;
316         
317         return (filename);
318 }
319
320
321 // Top-level window for CTSim
322
323 #if CTSIM_MDI
324 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
325
326 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
327 #else
328 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
329
330 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
331 #endif
332
333 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
334 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
335 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
336 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
337 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
338 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
339 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
340 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
341 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
342 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
343 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
344 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
345 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
346 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
347 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
348 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
349
350 EVT_SIZE(MainFrame::OnSize)
351
352 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
353 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
354 #endif
355 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
356 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
357 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
358 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
359 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
360 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
361 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
362 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
363 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
364 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
365 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
366 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
367 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
368 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
369 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
370 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
371 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
372 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
373 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
374 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
375 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
376 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
377 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
378 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
379 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
380 END_EVENT_TABLE()
381
382
383
384 #if CTSIM_MDI
385 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
386 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
387 #else
388 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
389 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
390 #endif
391 {
392   m_bShuttingDown = false;
393
394         //// Make a menubar
395         wxMenu *file_menu = new wxMenu;
396         
397         file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
398         file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
399         file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
400         
401         file_menu->AppendSeparator();
402   file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
403         file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
404         
405         //  history of files visited
406         theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
407         theApp->getDocManager()->FileHistoryUseMenu(file_menu);
408         
409 #ifndef CTSIM_MDI
410         m_pWindowMenu = new wxMenu;
411         m_pWindowMenu->UpdateUI (this);
412 #endif
413         
414         wxMenu* help_menu = new wxMenu;
415         help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
416   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
417   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
418 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
419         help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
420 #endif
421         help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
422         
423         wxMenuBar* menu_bar = new wxMenuBar;
424         
425         menu_bar->Append(file_menu, "&File");
426 #ifndef CTSIM_MDI
427         menu_bar->Append(m_pWindowMenu, "&Window");
428 #endif
429         menu_bar->Append(help_menu, "&Help");
430         
431         SetMenuBar(menu_bar);
432         
433 #ifndef CTSIM_MDI
434         int i;
435         for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
436                 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
437                 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
438                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
439         }
440 #endif
441         
442         m_iDefaultPhantomID = Phantom::PHM_HERMAN;
443         m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
444         m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
445         m_iDefaultFilterXSize = 256;
446         m_iDefaultFilterYSize = 256;
447         m_dDefaultFilterParam = 1.;
448         m_dDefaultFilterBandwidth = 1.;
449         m_dDefaultFilterInputScale = 1.;
450         m_dDefaultFilterOutputScale = 1.;
451         
452         wxAcceleratorEntry accelEntries[15];
453         accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
454         accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
455         accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
456         accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
457 #ifndef CTSIM_MDI
458         for (i = 0; i < 10; i++)
459                 accelEntries[i+4].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
460         wxAcceleratorTable accelTable (15, accelEntries);
461 #else
462         wxAcceleratorTable accelTable (5, accelEntries);
463 #endif
464         
465         SetAcceleratorTable (accelTable);
466 }
467
468 MainFrame::~MainFrame()
469 {
470   m_bShuttingDown = true; // Currently used so that Log Window will close
471 #if 0
472   // delete all non-modified documents
473         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
474         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
475                 wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
476     if (pDoc && ! pDoc->IsModified()) {
477       theApp->getDocManager()->RemoveDocument(pDoc);
478       delete pDoc;
479     }
480         }
481 #endif
482   ::wxYield();
483         if (theApp->getConfig())
484                 theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
485   ::wxYield();
486         delete theApp->getDocManager(); 
487
488 }
489
490 void
491 MainFrame::OnSize (wxSizeEvent& event)
492 {
493 #ifdef CTSIM_MDI
494     if (theApp->getLogDoc()) {
495         int xSize, ySize;       
496                 GetClientSize(&xSize, &ySize);
497                 int yLogSize = ySize / 4;
498                   theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
499                   theApp->getLogDoc()->getView()->getFrame()->Show (true);
500     }
501 #endif
502         
503 #if CTSIM_MDI
504         wxDocMDIParentFrame::OnSize (event);
505 #else
506         wxDocParentFrame::OnSize (event);
507 #endif
508 }
509
510 void 
511 MainFrame::OnCreatePhantom(wxCommandEvent& event)
512 {
513         DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
514         int dialogReturn = dialogPhantom.ShowModal();
515         if (dialogReturn == wxID_OK) {
516                 wxString selection (dialogPhantom.getPhantom());
517     if (theApp->getVerboseLogging())
518                 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
519                 wxString filename = selection + ".phm";
520                 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
521                 theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
522         }
523         
524 }
525
526 void 
527 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
528 {
529         DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
530         int dialogReturn = dialogFilter.ShowModal();
531         if (dialogReturn == wxID_OK) {
532                 wxString strFilter (dialogFilter.getFilterName());
533                 wxString strDomain (dialogFilter.getDomainName());
534                 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
535                 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
536                 m_iDefaultFilterXSize = dialogFilter.getXSize();
537                 m_iDefaultFilterYSize = dialogFilter.getYSize();
538                 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
539                 m_dDefaultFilterParam= dialogFilter.getFilterParam();
540                 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
541                 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
542                 std::ostringstream os;
543                 os << "Generate Filter=" << strFilter.c_str() 
544                         << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
545                         << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
546                         << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
547                 *theApp->getLog() << os.str().c_str() << "\n";
548                 wxString filename = "untitled.if";
549                 ImageFileDocument* pFilterDoc = theApp->newImageDoc();
550     pFilterDoc->setBadFileOpen();
551                 if (! pFilterDoc) {
552                         sys_error (ERR_SEVERE, "Unable to create filter image");
553                         return;
554                 }
555                 ImageFile& rIF = pFilterDoc->getImageFile();
556                 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
557                 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
558                 rIF.labelAdd (os.str().c_str());
559     pFilterDoc->Modify (true);
560                 pFilterDoc->UpdateAllViews();
561                 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
562                 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
563                 pFilterDoc->getView()->getFrame()->Show(true);
564         }
565 }
566
567 void
568 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
569 {
570         const ImageFile& rIF = pIFDoc->getImageFile();
571         unsigned int nx = rIF.nx();
572         unsigned int ny = rIF.ny();
573         wxList& rListDocs = m_docManager->GetDocuments();
574         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
575                 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
576                 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
577                 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
578                         const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
579                         if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
580                                 vecIF.push_back (pIFCompareDoc);
581                 }
582         }
583 }
584
585
586 void
587 MainFrame::OnHelpTips (wxCommandEvent& event)
588 {
589   theApp->ShowTips();
590 }
591
592 void 
593 MainFrame::OnHelpContents (wxCommandEvent& event)
594 {
595         showHelp (event.GetId());
596 }
597
598 void 
599 MainFrame::OnHelpButton (wxCommandEvent& event)
600 {
601         showHelp (event.GetId());
602 }
603
604 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
605 void
606 MainFrame::OnHelpSecondary (wxCommandEvent& event)
607 {
608         m_htmlHelp.DisplayContents();
609 }
610 #endif
611
612 void
613 MainFrame::showHelp (int commandID)
614 {
615         switch (commandID) {
616                 
617         case MAINMENU_HELP_CONTENTS:
618 #ifdef CTSIM_WINHELP
619                 m_winHelp.DisplayContents ();
620 #else
621                 m_htmlHelp.DisplayContents ();
622 #endif
623                 break;
624
625
626         default:
627 #ifdef CTSIM_WINHELP
628     m_winHelp.DisplaySection (commandID);
629 #else
630     m_htmlHelp.Display (commandID);
631 #endif
632                 break;
633         }
634 }
635
636 void 
637 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
638 {
639         Close(true);
640 }
641
642 void
643 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
644 {
645 #ifndef CTSIM_MDI
646         int iPos = 0;
647         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
648         wxNode* pNode = rListDocs.GetFirst();
649         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
650                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
651                 wxString strFilename = pDoc->GetFilename();
652                 if (iPos < 10) {
653                         strFilename += "\tCtrl-";
654                         strFilename += static_cast<char>('0' + iPos);
655                 }
656                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
657                 m_apWindowMenuData[iPos] = pDoc;
658                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
659                 iPos++;
660                 pNode = pNode->GetNext();
661         }
662         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
663                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
664                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
665                 m_apWindowMenuData[i] = NULL;
666         }
667 #endif  
668 }
669
670 #ifdef CTSIM_CUSTOM_MRU
671 void 
672 MainFrame::OnMRUFile (wxCommandEvent& event)
673 {
674         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
675         if (fileName != "")
676                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
677 }
678 #endif
679
680 void 
681 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
682 {
683         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
684                 wxString strFilename = pDoc->GetFilename();
685                 const wxView* pView = pDoc->GetFirstView();
686                 if (pView) {
687                         wxFrame* pFrame = pView->GetFrame();
688                         pFrame->SetFocus();
689                         pFrame->Raise();
690                 }
691         }
692 }
693
694 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
695 { DoWindowMenu (0, event); }
696
697 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
698 { DoWindowMenu (1, event); }
699
700 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
701 { DoWindowMenu (2, event); }
702
703 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
704 { DoWindowMenu (3, event); }
705
706 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
707 { DoWindowMenu (4, event); }
708
709 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
710 { DoWindowMenu (5, event); }
711
712 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
713 { DoWindowMenu (6, event); }
714
715 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
716 { DoWindowMenu (7, event); }
717
718 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
719 { DoWindowMenu (8, event); }
720
721 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
722 { DoWindowMenu (9, event); }
723
724 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
725 { DoWindowMenu (10, event); }
726
727 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
728 { DoWindowMenu (11, event); }
729
730 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
731 { DoWindowMenu (12, event); }
732
733 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
734 { DoWindowMenu (13, event); }
735
736 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
737 { DoWindowMenu (14, event); }
738
739 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
740 { DoWindowMenu (15, event); }
741
742 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
743 { DoWindowMenu (16, event); }
744
745 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
746 { DoWindowMenu (17, event); }
747
748 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
749 { DoWindowMenu (18, event); }
750
751 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
752 { DoWindowMenu (19, event); }
753
754
755 class BitmapControl : public wxPanel
756 {
757 private:  
758         DECLARE_DYNAMIC_CLASS (BitmapControl)
759                 DECLARE_EVENT_TABLE ()
760                 wxBitmap* m_pBitmap;
761         
762 public:
763         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
764                 const wxPoint& pos = wxDefaultPosition,
765                 const wxSize& size = wxDefaultSize,
766                 long style = wxSTATIC_BORDER,
767                 const wxValidator& validator = wxDefaultValidator,
768                 const wxString& name = "BitmapCtrl");
769         
770         
771         virtual ~BitmapControl();
772         
773         virtual wxSize GetBestSize() const;
774         
775         wxBitmap* getBitmap() 
776         { return m_pBitmap; }
777         
778         void OnPaint(wxPaintEvent& event);
779 };
780
781
782 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
783 EVT_PAINT(BitmapControl::OnPaint)
784 END_EVENT_TABLE()
785
786 IMPLEMENT_CLASS(BitmapControl, wxPanel)
787
788
789 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
790                               long style, const wxValidator& validator, const wxString& name)
791                               : m_pBitmap(pBitmap)
792 {
793         Create(parent, id, pos, size, style, name);
794         
795         SetSize (GetBestSize());
796 }
797
798 wxSize
799 BitmapControl::GetBestSize () const
800 {
801         if (m_pBitmap)
802                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
803         else
804                 return wxSize(0,0);
805 }
806
807 BitmapControl::~BitmapControl()
808 {}
809
810 void
811 BitmapControl::OnPaint (wxPaintEvent& event)
812 {
813         wxPaintDC dc(this);
814         if (m_pBitmap)
815                 dc.DrawBitmap (*m_pBitmap, 0, 0);
816 }
817
818
819 class BitmapDialog : public wxDialog {
820 private:
821         BitmapControl* m_pBitmapCtrl;
822         
823 public:
824         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
825         virtual ~BitmapDialog();
826 };
827
828 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
829 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
830 {
831         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
832         
833         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
834         
835         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
836         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
837         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
838         
839         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
840         
841         SetAutoLayout (true);
842         SetSizer (pTopSizer);
843         pTopSizer->Fit (this);
844         pTopSizer->SetSizeHints (this);
845 }
846
847 BitmapDialog::~BitmapDialog()
848 {}
849
850
851 void 
852 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
853 {
854   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
855     theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips());
856   if (dlg.ShowModal() == wxID_OK) {
857     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
858     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
859     theApp->setVerboseLogging (dlg.getVerboseLogging());
860     theApp->setStartupTips (dlg.getStartupTips());
861   }
862 }
863
864
865 #include "./splash.xpm"
866 void 
867 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
868 {
869   long lFreeMem = ::wxGetFreeMemory() / (1024L * 1024L);
870   wxString strOSDesc = ::wxGetOsDescription();
871   *theApp->getLog() << "Operation System: " << strOSDesc;
872   if (lFreeMem > 0)
873     *theApp->getLog() << ",  Free Memory: " << lFreeMem << " MB";
874   *theApp->getLog() << "\n";
875
876   wxBitmap bmp (splash);
877         if (bmp.Ok()) {
878                 BitmapDialog dlg (&bmp, "About CTSim");
879                 dlg.ShowModal();
880         } else {
881                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
882 #ifdef CTSIMVERSION
883                 msg += "Version ";
884                 msg += CTSIMVERSION;
885                 msg += "\n\n";
886 #elif defined(VERSION)
887                 msg << "Version: " <<  VERSION << "\n\n";
888 #endif
889                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
890                 
891                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
892         }
893 }
894
895
896 ProjectionFileDocument*
897 CTSimApp::newProjectionDoc()
898 {
899         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
900         if (newDoc) {
901                 ProjectionFileView* pView = newDoc->getView();
902                 if (pView) {
903                         wxFrame* pFrame = pView->getFrame();
904                         if (pFrame) {
905                                 pFrame->SetSize (0,0);
906                                 pFrame->Show (false);
907                         }
908                 }
909     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
910     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
911     newDoc->OnNewDocument();
912         }
913         
914         return newDoc;
915 }
916
917 ImageFileDocument*
918 CTSimApp::newImageDoc()
919 {
920         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
921         if (newDoc) {
922                 ImageFileView* pView = newDoc->getView();
923                 if (pView) {
924                         wxFrame* pFrame = pView->getFrame();
925                         if (pFrame) {
926                                 pFrame->SetSize (0,0);
927                                 pFrame->Show (false);
928                         }
929                 }
930     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
931     newDoc->SetDocumentTemplate (m_pDocTemplImage);
932     newDoc->OnNewDocument();
933         }
934         
935         return newDoc;
936 }
937
938 PlotFileDocument*
939 CTSimApp::newPlotDoc()
940 {
941         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
942         if (newDoc) {
943                 PlotFileView* pView = newDoc->getView();
944                 if (pView) {
945                         wxFrame* pFrame = pView->getFrame();
946                         if (pFrame) {
947                                 wxSize size;
948                                 m_pFrame->GetClientSize (&size.x, &size.y);
949                                 pFrame->SetSize (size.x / 2, size.y / 2);
950                                 pFrame->Show (false);
951                         }
952                 }
953     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
954     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
955     newDoc->OnNewDocument();
956         }
957         
958         return newDoc;
959 }
960
961
962 TextFileDocument*
963 CTSimApp::newTextDoc()
964 {
965         wxString strFilename (getUntitledFilename());
966         strFilename += ".txt";
967         
968         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
969         if (newDoc) {
970                 TextFileView* pView = newDoc->getView();
971                 if (pView) {
972                         wxFrame* pFrame = pView->getFrame();
973                         if (pFrame) {
974                                 wxSize size;
975                                 m_pFrame->GetClientSize (&size.x, &size.y);;
976                                 pFrame->SetSize (size.x / 2, size.y / 2);
977                                 pFrame->Show (false);
978                         }
979                 }
980     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
981     newDoc->SetDocumentTemplate (m_pDocTemplText);
982     newDoc->OnNewDocument();
983         }
984         
985         return newDoc;
986 }
987
988
989 PhantomFileDocument*
990 CTSimApp::newPhantomDoc()
991 {
992         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
993         if (newDoc) {
994                 PhantomFileView* pView = newDoc->getView();
995                 if (pView) {
996                         wxFrame* pFrame = pView->getFrame();
997                         if (pFrame)
998                                 pFrame->SetSize (0,0);
999                 }
1000     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1001     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1002     newDoc->OnNewDocument();
1003         }
1004         
1005         return newDoc;
1006 }
1007
1008 #if wxUSE_GLCANVAS
1009
1010 Graph3dFileDocument*
1011 CTSimApp::newGraph3dDoc()
1012 {
1013         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
1014         if (newDoc) {
1015                 Graph3dFileView* pView = newDoc->getView();
1016                 if (pView) {
1017                         wxFrame* pFrame = pView->getFrame();
1018                         if (pFrame)
1019                                 pFrame->SetSize (0,0);
1020                 }
1021     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1022     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1023     newDoc->OnNewDocument();
1024         }
1025         
1026         return newDoc;
1027 }
1028 #endif