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