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