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