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