r582: 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.89 2001/02/25 10:52:55 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 #include "wx/wxprec.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37
38 #include "wx/image.h"
39 #include "wx/filesys.h"
40 #include "wx/fs_zip.h"
41 #ifdef __WXMSW__
42 #include "wx/msw/helpchm.h"
43 #endif
44
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
47 #endif
48
49
50 #include "ct.h"
51 #include "ctsim.h"
52 #include "docs.h"
53 #include "views.h"
54 #include "dialogs.h"
55 #include "tips.h"
56 #include "backgroundmgr.h"
57
58 #if defined(HAVE_CONFIG_H)
59 #include "config.h"
60 #endif
61
62 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
63 #ifdef MSVC
64 #define __STDC__ 1
65 #endif
66 #include "getopt.h"
67 #ifdef MSVC
68 #undef __STDC__
69 #endif
70 #endif
71
72 static const char* rcsindent = "$Id: ctsim.cpp,v 1.89 2001/02/25 10:52:55 kevin Exp $";
73
74 struct option CTSimApp::ctsimOptions[] = 
75 {
76         {"help", 0, 0, O_HELP},
77         {"version", 0, 0, O_VERSION},
78   {"print", 0, 0, O_PRINT},
79         {0, 0, 0, 0}
80 };
81
82 IMPLEMENT_APP(CTSimApp)
83
84 CTSimApp::CTSimApp()
85 :  m_bAdvancedOptions(false), m_bSetModifyNewDocs(true), m_bVerboseLogging(false), m_bShowStartupTips(true),
86    m_iCurrentTip(0), m_bUseBackgroundTasks(false),
87   m_docManager(NULL), m_pFrame(NULL), m_pLog(0), m_pLogDoc(0), m_pConfig(0)
88 {
89         theApp = this;
90 }
91
92 #ifdef HAVE_SYS_TIME_H
93 #include <sys/time.h>
94 #endif
95
96 #ifdef HAVE_SYS_RESOURCE_H
97 #include <sys/resource.h>
98 #endif
99
100 bool
101 CTSimApp::OnInit()
102 {
103 #ifdef HAVE_SETPRIORITY
104         setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
105 #endif
106         
107         openConfig();
108         
109         g_bRunningWXWindows = true;
110   bool bPrintFiles = false;
111         // process options
112         while (1) {
113                 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
114                 if (c == -1)
115                         break;
116                 
117                 switch (c) {
118                 case O_VERSION:
119                         std::cout << rcsindent << std::endl;
120 #ifdef CTSIMVERSION
121                         std::cout << "Version: CTSIMVERSION" << std::endl;
122 #elif defined(VERSION)
123                         std::cout << "Version: VERSION" << std::endl;
124 #endif
125                         exit(0);
126                 case O_HELP:
127                 case '?':
128                         usage (argv[0]);
129                         exit (0);
130     case O_PRINT:
131       bPrintFiles = true;
132       break;
133                 default:
134                         usage (argv[0]);
135                         exit (1);
136                 }
137         }
138         
139         m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
140         
141         m_pDocTemplImage = new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile", "ImageView", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
142         m_pDocTemplProjection = new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile", "ProjectionView", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
143         m_pDocTemplPhantom = new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "PhantomFile", "PhantomView", CLASSINFO(PhantomFileDocument), CLASSINFO(PhantomFileView));
144         m_pDocTemplPlot = new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "PlotFile", "PlotView", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
145         m_pDocTemplText = new wxDocTemplate (m_docManager, "TextFile", "*.txt", "", "txt", "TextFile", "TextView", CLASSINFO(TextFileDocument), CLASSINFO(TextFileView), wxTEMPLATE_INVISIBLE);
146 #if wxUSE_GLCANVAS
147         m_pDocTemplGraph3d = new wxDocTemplate (m_docManager, "Graph3dFile", "*.g3d", "", "g3d", "Graph3dFile", "Graph3dView", CLASSINFO(Graph3dFileDocument), CLASSINFO(Graph3dFileView), wxTEMPLATE_INVISIBLE);
148 #endif
149
150 #if wxUSE_GIF
151         wxImage::AddHandler(new wxGIFHandler);     // Required for images in the online documentation
152 #endif
153         
154 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
155         wxFileSystem::AddHandler(new wxZipFSHandler);     // Required for advanced HTML help
156 #endif
157         
158         // Create the main frame window
159         int xDisplay, yDisplay;
160         ::wxDisplaySize (&xDisplay, &yDisplay);
161         m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), 
162                 wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .755)), wxDEFAULT_FRAME_STYLE);
163         
164         setIconForFrame (m_pFrame);
165         m_pFrame->Centre(wxBOTH);
166         m_pFrame->Show(true);  
167         SetTopWindow (m_pFrame);
168         
169         if (m_pConfig)
170                 m_docManager->FileHistoryLoad(*m_pConfig);
171         
172 #ifdef CTSIM_MDI
173         m_pLogDoc = newTextDoc();
174         if (m_pLogDoc) {
175                 m_pLog = m_pLogDoc->getTextCtrl();
176                 m_pLogDoc->SetDocumentName("Log.txt");
177                 m_pLogDoc->SetFilename("Log.txt");
178                 m_pLogDoc->getView()->getFrame()->SetTitle("Log");
179                 int xSize, ySize;
180                 m_pFrame->GetClientSize(&xSize, &ySize);
181                 int yLogSize = ySize / 4;
182                 m_pLogDoc->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
183                 m_pLogDoc->getView()->getFrame()->Show (true);
184         } else
185 #else
186                 m_pLog = new wxTextCtrl (m_pFrame, -1, "Log Window\n", wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
187 #endif
188         wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
189
190         wxString helpDir;
191         if (! m_pConfig->Read("HelpDir", &helpDir))
192                 helpDir = ::wxGetCwd();
193 #ifdef CTSIM_WINHELP
194         if (! m_pFrame->getWinHelpController().Initialize(helpDir + "/ctsim"))
195                 *m_pLog << "Cannot initialize the Windows Help system" << "\n";
196 #else
197         if (! m_pFrame->getHtmlHelpController().Initialize(helpDir + "/ctsim") &&
198                 ! m_pFrame->getHtmlHelpController().Initialize("/usr/local/man/ctsim"))
199                 *m_pLog << "Cannot initialize the HTML Help system" << "\n";
200         else {
201             if (::wxDirExists ("/tmp"))
202                 m_pFrame->getHtmlHelpController().SetTempDir(_T("/tmp"));
203             m_pFrame->getHtmlHelpController().UseConfig (m_pConfig);
204         }
205 #endif
206
207         for (int i = optind + 1; i <= argc; i++) {
208                 wxString filename = argv [i - 1];
209                 wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
210     if (bPrintFiles) {
211       wxView* pNewView = pNewDoc->GetFirstView();
212       wxPrintout *printout = pNewView->OnCreatePrintout();
213       if (printout) {
214         wxPrinter printer;
215         printer.Print(pNewView->GetFrame(), printout, TRUE);
216         delete printout;
217       }
218       wxCommandEvent nullEvent;
219       nullEvent.SetId (wxID_CLOSE);
220       m_docManager->OnFileClose (nullEvent);
221     }
222         }
223   if (bPrintFiles) {
224     wxCommandEvent closeEvent;
225     closeEvent.SetInt (MAINMENU_FILE_EXIT);
226     m_pFrame->AddPendingEvent(closeEvent);
227   }
228
229   if (getStartupTips())
230     ShowTips();
231
232   m_pBackgroundMgr = new BackgroundManager;
233   return true;
234 }
235
236 void
237 CTSimApp::ShowTips()
238 {
239     CTSimTipProvider tipProvider (m_iCurrentTip);
240     setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
241     m_iCurrentTip = tipProvider.GetCurrentTip();
242 }
243
244
245 #include "./ctsim.xpm"
246 void
247 CTSimApp::setIconForFrame(wxFrame* pFrame)
248 {
249         wxIcon iconApp (ctsim16_xpm);
250         
251         if (iconApp.Ok())
252                 pFrame->SetIcon (iconApp);
253 }
254
255 void
256 CTSimApp::usage(const char* program)
257 {
258         std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
259         std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
260         std::cout << "\n";
261         std::cout << "  --version Display version\n";
262         std::cout << "  --help    Display this help message\n";
263 }
264
265 int
266 CTSimApp::OnExit()
267 {
268   closeConfig();
269
270 #ifdef HAVE_DMALLOC
271         dmalloc_shutdown();
272 #endif
273         return 0;
274 }
275
276 void
277 CTSimApp::openConfig()
278 {
279 #ifdef MSVC
280   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
281 #else
282   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
283 #endif
284
285         wxConfigBase::Set(m_pConfig);
286   m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
287   m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
288   m_pConfig->Read ("VerboseLogging", &m_bVerboseLogging);
289   m_pConfig->Read ("StartupTips", &m_bShowStartupTips);
290   m_pConfig->Read ("CurrentTip", &m_iCurrentTip);
291   m_pConfig->Read ("UseBackgroundTasks", &m_bUseBackgroundTasks);
292 }
293
294 void
295 CTSimApp::closeConfig()
296 {
297   m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
298   m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
299   m_pConfig->Write ("VerboseLogging", m_bVerboseLogging);
300   m_pConfig->Write ("StartupTips", m_bShowStartupTips);
301   m_pConfig->Write ("CurrentTip", m_iCurrentTip);
302   m_pConfig->Write ("UseBackgroundTasks", m_bUseBackgroundTasks);
303
304         delete m_pConfig;
305 }
306
307
308 wxString
309 CTSimApp::getUntitledFilename()
310 {
311         static int untitledNumber = 1;
312         
313         wxString filename ("Untitled");
314         filename << untitledNumber++;
315         
316         return (filename);
317 }
318
319
320 // Top-level window for CTSim
321
322 #if CTSIM_MDI
323 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
324
325 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
326 #else
327 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
328
329 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
330 #endif
331
332 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
333 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
334 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
335 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
336 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
337 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
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     if (theApp->getAskDeleteNewDocs())
560       pFilterDoc->Modify (true);
561                 pFilterDoc->UpdateAllViews();
562                 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
563                 pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
564                 pFilterDoc->getView()->getFrame()->Show(true);
565         }
566 }
567
568 void
569 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
570 {
571         const ImageFile& rIF = pIFDoc->getImageFile();
572         unsigned int nx = rIF.nx();
573         unsigned int ny = rIF.ny();
574         wxList& rListDocs = m_docManager->GetDocuments();
575         for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
576                 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
577                 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
578                 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
579                         const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
580                         if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
581                                 vecIF.push_back (pIFCompareDoc);
582                 }
583         }
584 }
585
586
587 void
588 MainFrame::OnLogEvent (wxCommandEvent& event)
589 {
590   *theApp->getLog() << event.GetString();
591 }
592
593 void
594 MainFrame::OnHelpTips (wxCommandEvent& event)
595 {
596   theApp->ShowTips();
597 }
598
599 void 
600 MainFrame::OnHelpContents (wxCommandEvent& event)
601 {
602         showHelp (event.GetId());
603 }
604
605 void 
606 MainFrame::OnHelpButton (wxCommandEvent& event)
607 {
608         showHelp (event.GetId());
609 }
610
611 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
612 void
613 MainFrame::OnHelpSecondary (wxCommandEvent& event)
614 {
615         m_htmlHelp.DisplayContents();
616 }
617 #endif
618
619 void
620 MainFrame::showHelp (int commandID)
621 {
622         switch (commandID) {
623                 
624         case MAINMENU_HELP_CONTENTS:
625 #ifdef CTSIM_WINHELP
626                 m_winHelp.DisplayContents ();
627 #else
628                 m_htmlHelp.DisplayContents ();
629 #endif
630                 break;
631
632
633         default:
634 #ifdef CTSIM_WINHELP
635     m_winHelp.DisplaySection (commandID);
636 #else
637     m_htmlHelp.Display (commandID);
638 #endif
639                 break;
640         }
641 }
642
643 void 
644 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
645 {
646         Close(true);
647 }
648
649 void
650 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
651 {
652 #ifndef CTSIM_MDI
653         int iPos = 0;
654         wxList& rListDocs = theApp->getDocManager()->GetDocuments();
655         wxNode* pNode = rListDocs.GetFirst();
656         while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
657                 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
658                 wxString strFilename = pDoc->GetFilename();
659                 if (iPos < 10) {
660                         strFilename += "\tCtrl-";
661                         strFilename += static_cast<char>('0' + iPos);
662                 }
663                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
664                 m_apWindowMenuData[iPos] = pDoc;
665                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
666                 iPos++;
667                 pNode = pNode->GetNext();
668         }
669         for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
670                 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
671                 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
672                 m_apWindowMenuData[i] = NULL;
673         }
674 #endif  
675 }
676
677 #ifdef CTSIM_CUSTOM_MRU
678 void 
679 MainFrame::OnMRUFile (wxCommandEvent& event)
680 {
681         wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
682         if (fileName != "")
683                 theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
684 }
685 #endif
686
687 void 
688 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
689 {
690         if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
691                 wxString strFilename = pDoc->GetFilename();
692                 const wxView* pView = pDoc->GetFirstView();
693                 if (pView) {
694                         wxFrame* pFrame = pView->GetFrame();
695                         pFrame->SetFocus();
696                         pFrame->Raise();
697                 }
698         }
699 }
700
701 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
702 { DoWindowMenu (0, event); }
703
704 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
705 { DoWindowMenu (1, event); }
706
707 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
708 { DoWindowMenu (2, event); }
709
710 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
711 { DoWindowMenu (3, event); }
712
713 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
714 { DoWindowMenu (4, event); }
715
716 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
717 { DoWindowMenu (5, event); }
718
719 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
720 { DoWindowMenu (6, event); }
721
722 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
723 { DoWindowMenu (7, event); }
724
725 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
726 { DoWindowMenu (8, event); }
727
728 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
729 { DoWindowMenu (9, event); }
730
731 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
732 { DoWindowMenu (10, event); }
733
734 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
735 { DoWindowMenu (11, event); }
736
737 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
738 { DoWindowMenu (12, event); }
739
740 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
741 { DoWindowMenu (13, event); }
742
743 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
744 { DoWindowMenu (14, event); }
745
746 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
747 { DoWindowMenu (15, event); }
748
749 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
750 { DoWindowMenu (16, event); }
751
752 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
753 { DoWindowMenu (17, event); }
754
755 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
756 { DoWindowMenu (18, event); }
757
758 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
759 { DoWindowMenu (19, event); }
760
761
762 class BitmapControl : public wxPanel
763 {
764 private:  
765         DECLARE_DYNAMIC_CLASS (BitmapControl)
766                 DECLARE_EVENT_TABLE ()
767                 wxBitmap* m_pBitmap;
768         
769 public:
770         BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
771                 const wxPoint& pos = wxDefaultPosition,
772                 const wxSize& size = wxDefaultSize,
773                 long style = wxSTATIC_BORDER,
774                 const wxValidator& validator = wxDefaultValidator,
775                 const wxString& name = "BitmapCtrl");
776         
777         
778         virtual ~BitmapControl();
779         
780         virtual wxSize GetBestSize() const;
781         
782         wxBitmap* getBitmap() 
783         { return m_pBitmap; }
784         
785         void OnPaint(wxPaintEvent& event);
786 };
787
788
789 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
790 EVT_PAINT(BitmapControl::OnPaint)
791 END_EVENT_TABLE()
792
793 IMPLEMENT_CLASS(BitmapControl, wxPanel)
794
795
796 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
797                               long style, const wxValidator& validator, const wxString& name)
798                               : m_pBitmap(pBitmap)
799 {
800         Create(parent, id, pos, size, style, name);
801         
802         SetSize (GetBestSize());
803 }
804
805 wxSize
806 BitmapControl::GetBestSize () const
807 {
808         if (m_pBitmap)
809                 return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
810         else
811                 return wxSize(0,0);
812 }
813
814 BitmapControl::~BitmapControl()
815 {}
816
817 void
818 BitmapControl::OnPaint (wxPaintEvent& event)
819 {
820         wxPaintDC dc(this);
821         if (m_pBitmap)
822                 dc.DrawBitmap (*m_pBitmap, 0, 0);
823 }
824
825
826 class BitmapDialog : public wxDialog {
827 private:
828         BitmapControl* m_pBitmapCtrl;
829         
830 public:
831         BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
832         virtual ~BitmapDialog();
833 };
834
835 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
836 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
837 {
838         wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
839         
840         pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
841         
842         wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
843         wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
844         pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
845         
846         pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
847         
848         SetAutoLayout (true);
849         SetSizer (pTopSizer);
850         pTopSizer->Fit (this);
851         pTopSizer->SetSizeHints (this);
852 }
853
854 BitmapDialog::~BitmapDialog()
855 {}
856
857
858 void 
859 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
860 {
861   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
862     theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(), 
863     theApp->getUseBackgroundTasks());
864   if (dlg.ShowModal() == wxID_OK) {
865     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
866     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
867     theApp->setVerboseLogging (dlg.getVerboseLogging());
868     theApp->setStartupTips (dlg.getStartupTips());
869     theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
870   }
871 }
872
873
874 #include "./splash.xpm"
875 void 
876 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
877 {
878   long lFreeMem = ::wxGetFreeMemory() / (1024L * 1024L);
879   wxString strOSDesc = ::wxGetOsDescription();
880   *theApp->getLog() << "Operation System: " << strOSDesc;
881   if (lFreeMem > 0)
882     *theApp->getLog() << ",  Free Memory: " << lFreeMem << " MB";
883   *theApp->getLog() << "\n";
884
885   wxBitmap bmp (splash);
886         if (bmp.Ok()) {
887                 BitmapDialog dlg (&bmp, "About CTSim");
888                 dlg.ShowModal();
889         } else {
890                 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
891 #ifdef CTSIMVERSION
892                 msg += "Version ";
893                 msg += CTSIMVERSION;
894                 msg += "\n\n";
895 #elif defined(VERSION)
896                 msg << "Version: " <<  VERSION << "\n\n";
897 #endif
898                 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
899                 
900                 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
901         }
902 }
903
904
905 ProjectionFileDocument*
906 CTSimApp::newProjectionDoc()
907 {
908         ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
909         if (newDoc) {
910                 ProjectionFileView* pView = newDoc->getView();
911                 if (pView) {
912                         wxFrame* pFrame = pView->getFrame();
913                         if (pFrame) {
914                                 pFrame->SetSize (0,0);
915                                 pFrame->Show (false);
916                         }
917                 }
918     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
919     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
920     newDoc->OnNewDocument();
921         }
922         
923         return newDoc;
924 }
925
926 ImageFileDocument*
927 CTSimApp::newImageDoc()
928 {
929         ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
930         if (newDoc) {
931                 ImageFileView* pView = newDoc->getView();
932                 if (pView) {
933                         wxFrame* pFrame = pView->getFrame();
934                         if (pFrame) {
935                                 pFrame->SetSize (0,0);
936                                 pFrame->Show (false);
937                         }
938                 }
939     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
940     newDoc->SetDocumentTemplate (m_pDocTemplImage);
941     newDoc->OnNewDocument();
942         }
943         
944         return newDoc;
945 }
946
947 PlotFileDocument*
948 CTSimApp::newPlotDoc()
949 {
950         PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
951         if (newDoc) {
952                 PlotFileView* pView = newDoc->getView();
953                 if (pView) {
954                         wxFrame* pFrame = pView->getFrame();
955                         if (pFrame) {
956                                 wxSize size;
957                                 m_pFrame->GetClientSize (&size.x, &size.y);
958                                 pFrame->SetSize (size.x / 2, size.y / 2);
959                                 pFrame->Show (false);
960                         }
961                 }
962     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
963     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
964     newDoc->OnNewDocument();
965         }
966         
967         return newDoc;
968 }
969
970
971 TextFileDocument*
972 CTSimApp::newTextDoc()
973 {
974         wxString strFilename (getUntitledFilename());
975         strFilename += ".txt";
976         
977         TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
978         if (newDoc) {
979                 TextFileView* pView = newDoc->getView();
980                 if (pView) {
981                         wxFrame* pFrame = pView->getFrame();
982                         if (pFrame) {
983                                 wxSize size;
984                                 m_pFrame->GetClientSize (&size.x, &size.y);;
985                                 pFrame->SetSize (size.x / 2, size.y / 2);
986                                 pFrame->Show (false);
987                         }
988                 }
989     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
990     newDoc->SetDocumentTemplate (m_pDocTemplText);
991     newDoc->OnNewDocument();
992         }
993         
994         return newDoc;
995 }
996
997
998 PhantomFileDocument*
999 CTSimApp::newPhantomDoc()
1000 {
1001         PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
1002         if (newDoc) {
1003                 PhantomFileView* pView = newDoc->getView();
1004                 if (pView) {
1005                         wxFrame* pFrame = pView->getFrame();
1006                         if (pFrame)
1007                                 pFrame->SetSize (0,0);
1008                 }
1009     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1010     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1011     newDoc->OnNewDocument();
1012         }
1013         
1014         return newDoc;
1015 }
1016
1017 #if wxUSE_GLCANVAS
1018
1019 Graph3dFileDocument*
1020 CTSimApp::newGraph3dDoc()
1021 {
1022         Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
1023         if (newDoc) {
1024                 Graph3dFileView* pView = newDoc->getView();
1025                 if (pView) {
1026                         wxFrame* pFrame = pView->getFrame();
1027                         if (pFrame)
1028                                 pFrame->SetSize (0,0);
1029                 }
1030     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1031     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1032     newDoc->OnNewDocument();
1033         }
1034         
1035         return newDoc;
1036 }
1037 #endif