r609: *** 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-2001 Kevin Rosenberg
11 **
12 **  $Id: ctsim.cpp,v 1.96 2001/03/05 20:29:23 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.96 2001/03/05 20:29:23 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_pConfig(0), m_pLog(0), m_pLogDoc(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 #ifdef HAVE_WXTHREADS
233   m_pBackgroundMgr = new BackgroundManager;
234 #endif
235
236   return true;
237 }
238
239 void
240 CTSimApp::ShowTips()
241 {
242   CTSimTipProvider tipProvider (m_iCurrentTip);
243   setStartupTips (::wxShowTip (m_pFrame, &tipProvider, getStartupTips()));
244   m_iCurrentTip = tipProvider.GetCurrentTip();
245 }
246
247
248 #include "./ctsim.xpm"
249 void
250 CTSimApp::setIconForFrame(wxFrame* pFrame)
251 {
252   wxIcon iconApp (ctsim16_xpm);
253   
254   if (iconApp.Ok())
255     pFrame->SetIcon (iconApp);
256 }
257
258 void
259 CTSimApp::usage(const char* program)
260 {
261   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
262   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
263   std::cout << "\n";
264   std::cout << "  --version Display version\n";
265   std::cout << "  --help    Display this help message\n";
266 }
267
268 int
269 CTSimApp::OnExit()
270 {
271   closeConfig();
272   
273 #ifdef HAVE_DMALLOC
274   dmalloc_shutdown();
275 #endif
276   return 0;
277 }
278
279 void
280 CTSimApp::openConfig()
281 {
282 #ifdef MSVC
283   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", "", "", wxCONFIG_USE_LOCAL_FILE);
284 #else
285   m_pConfig = new wxConfig("ctsim", "Kevin Rosenberg", ".ctsim", "", wxCONFIG_USE_LOCAL_FILE);
286 #endif
287   
288   wxConfigBase::Set(m_pConfig);
289   m_pConfig->Read ("AdvancedOptions", &m_bAdvancedOptions);
290   m_pConfig->Read ("SetModifyNewDocs", &m_bSetModifyNewDocs);
291   m_pConfig->Read ("VerboseLogging", &m_bVerboseLogging);
292   m_pConfig->Read ("StartupTips", &m_bShowStartupTips);
293   m_pConfig->Read ("CurrentTip", &m_iCurrentTip);
294   m_pConfig->Read ("UseBackgroundTasks", &m_bUseBackgroundTasks);
295 }
296
297 void
298 CTSimApp::closeConfig()
299 {
300   m_pConfig->Write ("AdvancedOptions", m_bAdvancedOptions);
301   m_pConfig->Write ("SetModifyNewDocs", m_bSetModifyNewDocs);
302   m_pConfig->Write ("VerboseLogging", m_bVerboseLogging);
303   m_pConfig->Write ("StartupTips", m_bShowStartupTips);
304   m_pConfig->Write ("CurrentTip", m_iCurrentTip);
305   m_pConfig->Write ("UseBackgroundTasks", m_bUseBackgroundTasks);
306   
307   delete m_pConfig;
308 }
309
310
311 wxString
312 CTSimApp::getUntitledFilename()
313 {
314   static int untitledNumber = 1;
315   
316   wxString filename ("Untitled");
317   filename << untitledNumber++;
318   
319   return (filename);
320 }
321
322
323 // Top-level window for CTSim
324
325 #if CTSIM_MDI
326 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
327
328 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
329 #else
330 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
331
332 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
333 #endif
334
335 EVT_MENU(MAINMENU_FILE_PREFERENCES, MainFrame::OnPreferences)
336 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
337 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
338 EVT_MENU(MAINMENU_HELP_TIPS, MainFrame::OnHelpTips)
339 EVT_MENU(MAINMENU_IMPORT, MainFrame::OnImport)
340 EVT_MENU(IDH_QUICKSTART, MainFrame::OnHelpButton)
341 EVT_MENU(MAINMENU_LOG_EVENT, MainFrame::OnLogEvent)
342 EVT_MENU(NEW_IMAGEFILE_EVENT, MainFrame::OnNewImageFile)
343 EVT_MENU(NEW_PROJECTIONFILE_EVENT, MainFrame::OnNewProjectionFile)
344 EVT_BUTTON(IDH_DLG_RASTERIZE, MainFrame::OnHelpButton)
345 EVT_BUTTON(IDH_DLG_PROJECTIONS, MainFrame::OnHelpButton)
346 EVT_BUTTON(IDH_DLG_RECONSTRUCTION, MainFrame::OnHelpButton)
347 EVT_BUTTON(IDH_DLG_FILTER, MainFrame::OnHelpButton)
348 EVT_BUTTON(IDH_DLG_MINMAX, MainFrame::OnHelpButton)
349 EVT_BUTTON(IDH_DLG_EXPORT, MainFrame::OnHelpButton)
350 EVT_BUTTON(IDH_DLG_PHANTOM, MainFrame::OnHelpButton)
351 EVT_BUTTON(IDH_DLG_COMPARISON, MainFrame::OnHelpButton)
352 EVT_BUTTON(IDH_DLG_PREFERENCES, MainFrame::OnHelpButton)
353 EVT_BUTTON(IDH_DLG_POLAR, MainFrame::OnHelpButton)
354 EVT_BUTTON(IDH_DLG_AUTOSCALE, MainFrame::OnHelpButton)
355
356 EVT_SIZE(MainFrame::OnSize)
357
358 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
359 EVT_MENU(MAINMENU_HELP_SECONDARY, MainFrame::OnHelpSecondary)
360 #endif
361 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
362 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
363 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
364 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MainFrame::OnMRUFile)
365 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
366 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
367 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
368 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
369 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
370 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
371 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
372 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
373 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
374 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
375 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
376 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
377 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
378 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
379 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
380 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
381 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
382 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
383 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
384 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
385 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
386 END_EVENT_TABLE()
387
388
389
390 #if CTSIM_MDI
391 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
392 : wxDocMDIParentFrame(manager, NULL, id, title, pos, size, type, "MainFrame")
393 #else
394 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
395 : wxDocParentFrame(manager, frame, id, title, pos, size, type, "MainFrame")
396 #endif
397 {
398   m_bShuttingDown = false;
399   
400   //// Make a menubar
401   wxMenu *file_menu = new wxMenu;
402   
403   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
404   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
405   file_menu->Append(wxID_OPEN, "&Open...\tCtrl-O");
406   
407   file_menu->AppendSeparator();
408   file_menu->Append (MAINMENU_IMPORT, "&Import...\tCtrl-M");
409   file_menu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
410   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
411   
412   //  history of files visited
413   theApp->getDocManager()->FileHistoryAddFilesToMenu(file_menu);
414   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
415   
416 #ifndef CTSIM_MDI
417   m_pWindowMenu = new wxMenu;
418   m_pWindowMenu->UpdateUI (this);
419 #endif
420   
421   wxMenu* help_menu = new wxMenu;
422   help_menu->Append (MAINMENU_HELP_CONTENTS, "&Contents\tF1");
423   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
424   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
425 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
426   help_menu->Append (MAINMENU_HELP_SECONDARY, "&Secondary Help");
427 #endif
428   help_menu->Append (MAINMENU_HELP_ABOUT, "&About");
429   
430   wxMenuBar* menu_bar = new wxMenuBar;
431   
432   menu_bar->Append(file_menu, "&File");
433 #ifndef CTSIM_MDI
434   menu_bar->Append(m_pWindowMenu, "&Window");
435 #endif
436   menu_bar->Append(help_menu, "&Help");
437   
438   SetMenuBar(menu_bar);
439   
440   
441 #ifndef CTSIM_MDI
442   int i;
443   for (i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
444     m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("[EMPTY]"));
445     m_pWindowMenu->Append (m_apWindowMenuItems[i]);
446     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
447   }
448 #endif
449   
450   m_iDefaultPhantomID = Phantom::PHM_HERMAN;
451   m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
452   m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
453   m_iDefaultFilterXSize = 256;
454   m_iDefaultFilterYSize = 256;
455   m_dDefaultFilterParam = 1.;
456   m_dDefaultFilterBandwidth = 1.;
457   m_dDefaultFilterInputScale = 1.;
458   m_dDefaultFilterOutputScale = 1.;
459   m_iDefaultImportFormat = ImageFile::IMPORT_FORMAT_PPM;
460   
461   wxAcceleratorEntry accelEntries[15];
462   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
463   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
464   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
465   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), MAINMENU_IMPORT);
466   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
467 #ifndef CTSIM_MDI
468   for (i = 0; i < 10; i++)
469     accelEntries[i+5].Set (wxACCEL_CTRL, static_cast<int>('0'+i), MAINMENU_WINDOW_BASE+i);
470   wxAcceleratorTable accelTable (15, accelEntries);
471 #else
472   wxAcceleratorTable accelTable (5, accelEntries);
473 #endif
474   
475   SetAcceleratorTable (accelTable);
476 }
477
478 MainFrame::~MainFrame()
479 {
480   m_bShuttingDown = true; // Currently used so that Log Window will close
481 #if 0
482   // delete all non-modified documents
483   wxList& rListDocs = theApp->getDocManager()->GetDocuments();
484   for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
485     wxDocument* pDoc = dynamic_cast<wxDocument*>(pNode->GetData());
486     if (pDoc && ! pDoc->IsModified()) {
487       theApp->getDocManager()->RemoveDocument(pDoc);
488       delete pDoc;
489     }
490   }
491 #endif
492   ::wxYield();
493   if (theApp->getConfig())
494     theApp->getDocManager()->FileHistorySave (*theApp->getConfig());
495   ::wxYield();
496   delete theApp->getDocManager();       
497   
498 }
499
500 void
501 MainFrame::OnSize (wxSizeEvent& event)
502 {
503 #ifdef CTSIM_MDI
504   if (theApp->getLogDoc()) {
505     int xSize, ySize;   
506     GetClientSize(&xSize, &ySize);
507     int yLogSize = ySize / 4;
508                   theApp->getLogDoc()->getView()->getFrame()->SetSize (0, ySize - yLogSize, xSize, yLogSize);
509       theApp->getLogDoc()->getView()->getFrame()->Show (true);
510   }
511 #endif
512   
513 #if CTSIM_MDI
514   wxDocMDIParentFrame::OnSize (event);
515 #else
516   wxDocParentFrame::OnSize (event);
517 #endif
518 }
519
520 void 
521 MainFrame::OnCreatePhantom(wxCommandEvent& event)
522 {
523   DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
524   int dialogReturn = dialogPhantom.ShowModal();
525   if (dialogReturn == wxID_OK) {
526     wxString selection (dialogPhantom.getPhantom());
527     if (theApp->getVerboseLogging())
528       *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
529     wxString filename = selection + ".phm";
530     m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
531     theApp->getDocManager()->CreateDocument (filename, wxDOC_SILENT);
532   }
533   
534 }
535
536 void 
537 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
538 {
539   DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
540   int dialogReturn = dialogFilter.ShowModal();
541   if (dialogReturn == wxID_OK) {
542     wxString strFilter (dialogFilter.getFilterName());
543     wxString strDomain (dialogFilter.getDomainName());
544     m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
545     m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
546     m_iDefaultFilterXSize = dialogFilter.getXSize();
547     m_iDefaultFilterYSize = dialogFilter.getYSize();
548     m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
549     m_dDefaultFilterParam= dialogFilter.getFilterParam();
550     m_dDefaultFilterInputScale = dialogFilter.getInputScale();
551     m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
552     std::ostringstream os;
553     os << "Generate Filter=" << strFilter.c_str() 
554       << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
555       << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
556       << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
557     *theApp->getLog() << os.str().c_str() << "\n";
558     wxString filename = "untitled.if";
559     ImageFileDocument* pFilterDoc = theApp->newImageDoc();
560     pFilterDoc->setBadFileOpen();
561     if (! pFilterDoc) {
562       sys_error (ERR_SEVERE, "Unable to create filter image");
563       return;
564     }
565     ImageFile& rIF = pFilterDoc->getImageFile();
566     rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
567     rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
568     rIF.labelAdd (os.str().c_str());
569     if (theApp->getAskDeleteNewDocs())
570       pFilterDoc->Modify (true);
571     pFilterDoc->UpdateAllViews();
572     pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
573     pFilterDoc->getView()->getFrame()->SetClientSize(m_iDefaultFilterXSize, m_iDefaultFilterYSize);
574     pFilterDoc->getView()->getFrame()->Show(true);
575   }
576 }
577
578 void
579 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
580 {
581   const ImageFile& rIF = pIFDoc->getImageFile();
582   unsigned int nx = rIF.nx();
583   unsigned int ny = rIF.ny();
584   wxList& rListDocs = m_docManager->GetDocuments();
585   for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
586     wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
587     ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
588     if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
589       const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
590       if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
591         vecIF.push_back (pIFCompareDoc);
592     }
593   }
594 }
595
596
597 void
598 MainFrame::OnNewImageFile (wxCommandEvent& event)
599 {
600   ImageFile* pImageFile = reinterpret_cast<ImageFile*>(event.GetClientData());
601
602   ImageFileDocument* pImageDoc = theApp->newImageDoc();
603   if (! pImageDoc) {
604     sys_error (ERR_SEVERE, "Unable to create image file");
605     return;
606   }  
607   pImageDoc->setImageFile (pImageFile);
608   pImageDoc->UpdateAllViews (NULL);
609   if (ImageFileView* imageView = pImageDoc->getView()) {
610     imageView->OnUpdate (imageView, NULL);
611     imageView->getFrame()->SetFocus();
612     imageView->getFrame()->Show(true);
613   }
614   if (theApp->getAskDeleteNewDocs())
615     pImageDoc->Modify (true);
616
617 }
618
619 void
620 MainFrame::OnNewProjectionFile (wxCommandEvent& event)
621 {
622   Projections* pProjections = reinterpret_cast<Projections*>(event.GetClientData());
623   ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
624   if (! pProjDoc) {
625     sys_error (ERR_SEVERE, "Unable to create projection file");
626     return;
627   }  
628   pProjDoc->setProjections (pProjections);
629   pProjDoc->UpdateAllViews (NULL);
630   if (ProjectionFileView* projView = pProjDoc->getView()) {
631     projView->OnUpdate (projView, NULL);
632     projView->getFrame()->SetFocus();
633     projView->getFrame()->Show(true);
634   }
635
636   if (theApp->getAskDeleteNewDocs())
637     pProjDoc->Modify (true);
638 }
639
640 void
641 MainFrame::OnLogEvent (wxCommandEvent& event)
642 {
643   *theApp->getLog() << event.GetString();
644 }
645
646 void
647 MainFrame::OnHelpTips (wxCommandEvent& event)
648 {
649   theApp->ShowTips();
650 }
651
652 void 
653 MainFrame::OnHelpContents (wxCommandEvent& event)
654 {
655   showHelp (event.GetId());
656 }
657
658 void 
659 MainFrame::OnHelpButton (wxCommandEvent& event)
660 {
661   showHelp (event.GetId());
662 }
663
664 #if defined(CTSIM_WINHELP) && (defined(DEBUG) || defined(_DEBUG))
665 void
666 MainFrame::OnHelpSecondary (wxCommandEvent& event)
667 {
668   m_htmlHelp.DisplayContents();
669 }
670 #endif
671
672 void
673 MainFrame::showHelp (int commandID)
674 {
675   switch (commandID) {
676     
677   case MAINMENU_HELP_CONTENTS:
678 #ifdef CTSIM_WINHELP
679     m_winHelp.DisplayContents ();
680 #else
681     m_htmlHelp.DisplayContents ();
682 #endif
683     break;
684     
685     
686   default:
687 #ifdef CTSIM_WINHELP
688     m_winHelp.DisplaySection (commandID);
689 #else
690     m_htmlHelp.Display (commandID);
691 #endif
692     break;
693   }
694 }
695
696 void 
697 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
698 {
699   Close(true);
700 }
701
702 void
703 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
704 {
705 #ifndef CTSIM_MDI
706   int iPos = 0;
707   wxList& rListDocs = theApp->getDocManager()->GetDocuments();
708   wxNode* pNode = rListDocs.GetFirst();
709   while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
710     wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
711     wxString strFilename = pDoc->GetFilename();
712     if (iPos < 10) {
713       strFilename += "\tCtrl-";
714       strFilename += static_cast<char>('0' + iPos);
715     }
716     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
717     m_apWindowMenuData[iPos] = pDoc;
718     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
719     iPos++;
720     pNode = pNode->GetNext();
721   }
722   for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
723     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
724     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("[EMPTY]"));
725     m_apWindowMenuData[i] = NULL;
726   }
727 #endif  
728 }
729
730 #ifdef CTSIM_CUSTOM_MRU
731 void 
732 MainFrame::OnMRUFile (wxCommandEvent& event)
733 {
734   wxString fileName (theApp->getDocManager()->GetHistoryFile(event.GetId() - wxID_FILE1));
735   if (fileName != "")
736     theApp->getDocManager()->CreateDocument(fileName, wxDOC_SILENT);
737 }
738 #endif
739
740 void 
741 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
742 {
743   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
744     wxString strFilename = pDoc->GetFilename();
745     const wxView* pView = pDoc->GetFirstView();
746     if (pView) {
747       wxFrame* pFrame = pView->GetFrame();
748       pFrame->SetFocus();
749       pFrame->Raise();
750     }
751   }
752 }
753
754 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
755 { DoWindowMenu (0, event); }
756
757 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
758 { DoWindowMenu (1, event); }
759
760 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
761 { DoWindowMenu (2, event); }
762
763 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
764 { DoWindowMenu (3, event); }
765
766 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
767 { DoWindowMenu (4, event); }
768
769 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
770 { DoWindowMenu (5, event); }
771
772 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
773 { DoWindowMenu (6, event); }
774
775 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
776 { DoWindowMenu (7, event); }
777
778 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
779 { DoWindowMenu (8, event); }
780
781 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
782 { DoWindowMenu (9, event); }
783
784 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
785 { DoWindowMenu (10, event); }
786
787 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
788 { DoWindowMenu (11, event); }
789
790 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
791 { DoWindowMenu (12, event); }
792
793 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
794 { DoWindowMenu (13, event); }
795
796 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
797 { DoWindowMenu (14, event); }
798
799 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
800 { DoWindowMenu (15, event); }
801
802 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
803 { DoWindowMenu (16, event); }
804
805 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
806 { DoWindowMenu (17, event); }
807
808 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
809 { DoWindowMenu (18, event); }
810
811 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
812 { DoWindowMenu (19, event); }
813
814
815 class BitmapControl : public wxPanel
816 {
817 private:  
818   DECLARE_DYNAMIC_CLASS (BitmapControl)
819     DECLARE_EVENT_TABLE ()
820     wxBitmap* m_pBitmap;
821   
822 public:
823   BitmapControl (wxBitmap* pBitmap, wxWindow *parent, wxWindowID id = -1,
824     const wxPoint& pos = wxDefaultPosition,
825     const wxSize& size = wxDefaultSize,
826     long style = wxSTATIC_BORDER,
827     const wxValidator& validator = wxDefaultValidator,
828     const wxString& name = "BitmapCtrl");
829   
830   
831   virtual ~BitmapControl();
832   
833   virtual wxSize GetBestSize() const;
834   
835   wxBitmap* getBitmap() 
836   { return m_pBitmap; }
837   
838   void OnPaint(wxPaintEvent& event);
839 };
840
841
842 BEGIN_EVENT_TABLE(BitmapControl, wxPanel)
843 EVT_PAINT(BitmapControl::OnPaint)
844 END_EVENT_TABLE()
845
846 IMPLEMENT_CLASS(BitmapControl, wxPanel)
847
848
849 BitmapControl::BitmapControl (wxBitmap* pBitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, 
850                               long style, const wxValidator& validator, const wxString& name)
851                               : m_pBitmap(pBitmap)
852 {
853   Create(parent, id, pos, size, style, name);
854   
855   SetSize (GetBestSize());
856 }
857
858 wxSize
859 BitmapControl::GetBestSize () const
860 {
861   if (m_pBitmap)
862     return wxSize (m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
863   else
864     return wxSize(0,0);
865 }
866
867 BitmapControl::~BitmapControl()
868 {}
869
870 void
871 BitmapControl::OnPaint (wxPaintEvent& event)
872 {
873   wxPaintDC dc(this);
874   if (m_pBitmap)
875     dc.DrawBitmap (*m_pBitmap, 0, 0);
876 }
877
878
879 class BitmapDialog : public wxDialog {
880 private:
881   BitmapControl* m_pBitmapCtrl;
882   
883 public:
884   BitmapDialog (wxBitmap* pBitmap, char const* pszTitle);
885   virtual ~BitmapDialog();
886 };
887
888 BitmapDialog::BitmapDialog (wxBitmap* pBitmap, char const* pszTitle)
889 : wxDialog(theApp->getMainFrame(), -1, wxString(pszTitle), wxDefaultPosition, wxDefaultSize, wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE)
890 {
891   wxBoxSizer* pTopSizer = new wxBoxSizer (wxVERTICAL);
892   
893   pTopSizer->Add (new BitmapControl (pBitmap, this), 0, wxALIGN_CENTER | wxALL, 5);
894   
895   wxBoxSizer* pButtonSizer = new wxBoxSizer (wxHORIZONTAL);
896   wxButton* pButtonOk = new wxButton (this, wxID_OK, "Ok");
897   pButtonSizer->Add (pButtonOk, 0, wxEXPAND | wxALL, 10);
898   
899   pTopSizer->Add (pButtonSizer, 0, wxALIGN_CENTER);
900   
901   SetAutoLayout (true);
902   SetSizer (pTopSizer);
903   pTopSizer->Fit (this);
904   pTopSizer->SetSizeHints (this);
905 }
906
907 BitmapDialog::~BitmapDialog()
908 {}
909
910
911 void 
912 MainFrame::OnPreferences (wxCommandEvent& WXUNUSED(event) )
913 {
914   DialogPreferences dlg (this, "CTSim Preferences", theApp->getAdvancedOptions(), 
915     theApp->getAskDeleteNewDocs(), theApp->getVerboseLogging(), theApp->getStartupTips(), 
916     theApp->getUseBackgroundTasks());
917   if (dlg.ShowModal() == wxID_OK) {
918     theApp->setAdvancedOptions (dlg.getAdvancedOptions());
919     theApp->setAskDeleteNewDocs (dlg.getAskDeleteNewDocs());
920     theApp->setVerboseLogging (dlg.getVerboseLogging());
921     theApp->setStartupTips (dlg.getStartupTips());
922     theApp->setUseBackgroundTasks (dlg.getUseBackgroundTasks());
923   }
924 }
925
926 void 
927 MainFrame::OnImport (wxCommandEvent& WXUNUSED(event) )
928 {
929   DialogImportParameters dialogImport (this, m_iDefaultImportFormat);
930   if (dialogImport.ShowModal() != wxID_OK)
931     return;
932   
933   wxString strFormatName (dialogImport.getFormatName ());
934   m_iDefaultImportFormat = ImageFile::convertImportFormatNameToID (strFormatName.c_str());
935   
936   wxString strExt;
937   wxString strWildcard;
938   if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM) {
939     strExt = ".ppm";
940     strWildcard = "PPM Files (*.ppm)|*.ppm|PGM Files (*.pgm)|*.pgm";
941   }
942 #ifdef HAVE_PNG
943   else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
944     strExt = ".png";
945     strWildcard = "PNG Files (*.png)|*.png";
946   }
947 #endif
948 #ifdef HAVE_CTN_DICOM
949   else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
950     strExt = "*.*";
951     strWildcard = "Dicom Files (*.*)|*.*";
952   }
953 #endif
954   else {
955     return;
956   }
957   
958   wxString strFilename = wxFileSelector (wxString("Import Filename"), wxString(""), 
959     wxString(""), strExt, strWildcard, wxHIDE_READONLY | wxOPEN);
960   if (! strFilename.IsEmpty()) {
961     if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PPM || m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_PNG) {
962       ImageFile* pIF = new ImageFile;
963       if (pIF->importImage (strFormatName.c_str(), strFilename.c_str())) {
964         ImageFileDocument* pIFDoc = theApp->newImageDoc();
965         pIFDoc->setImageFile(pIF);
966         pIFDoc->getView()->getFrame()->Show(true);
967         std::ostringstream os;
968         os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
969         pIF->labelAdd (os.str().c_str());
970         if (theApp->getAskDeleteNewDocs())
971           pIFDoc->Modify (true);
972         pIFDoc->UpdateAllViews();
973         pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
974         pIFDoc->getView()->getFrame()->Show(true);
975       } else
976         delete pIF;
977     } 
978 #ifdef HAVE_CTN_DICOM
979     else if (m_iDefaultImportFormat == ImageFile::IMPORT_FORMAT_DICOM) {
980       DicomImporter dicomImport (strFilename.c_str());
981       if (dicomImport.fail()) {
982         ::wxMessageBox (dicomImport.failMessage().c_str(), "Import Error");
983       } else if (dicomImport.testImage()) {
984         ImageFileDocument* pIFDoc = theApp->newImageDoc();
985         ImageFile* pIF = dicomImport.getImageFile();
986         pIFDoc->setImageFile (pIF);
987         pIFDoc->getView()->getFrame()->Show(true);
988         std::ostringstream os;
989         os << "Import file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
990         pIF->labelAdd (os.str().c_str());
991         if (theApp->getAskDeleteNewDocs())
992           pIFDoc->Modify (true);
993         pIFDoc->UpdateAllViews();
994         pIFDoc->GetFirstView()->OnUpdate (NULL, NULL);
995         pIFDoc->getView()->getFrame()->Show(true);
996       } else if (dicomImport.testProjections()) {
997         ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
998         Projections* pProj = dicomImport.getProjections();
999         pProjDoc->setProjections (pProj);
1000         pProjDoc->getView()->getFrame()->Show(true);
1001         std::ostringstream os;
1002         os << "Import projection file " << strFilename.c_str() << " (type " << strFormatName.c_str() << ")";
1003         pProj->setRemark (os.str().c_str());
1004         if (theApp->getAskDeleteNewDocs())
1005           pProjDoc->Modify (true);
1006         pProjDoc->UpdateAllViews();
1007         pProjDoc->GetFirstView()->OnUpdate (NULL, NULL);
1008         pProjDoc->getView()->getFrame()->Show(true);
1009       } else
1010         ::wxMessageBox ("Unrecognized DICOM file contents", "Import Error");
1011     } 
1012 #endif
1013     else
1014       sys_error (ERR_WARNING, "Unknown import format type");
1015   }
1016 }
1017
1018 #include "./splash.xpm"
1019 void 
1020 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1021 {
1022   long lFreeMem = ::wxGetFreeMemory() / (1024L * 1024L);
1023   wxString strOSDesc = ::wxGetOsDescription();
1024   *theApp->getLog() << "Operation System: " << strOSDesc;
1025   if (lFreeMem > 0)
1026     *theApp->getLog() << ",  Free Memory: " << lFreeMem << " MB";
1027   *theApp->getLog() << "\n";
1028   
1029   wxBitmap bmp (splash);
1030   if (bmp.Ok()) {
1031     BitmapDialog dlg (&bmp, "About CTSim");
1032     dlg.ShowModal();
1033   } else {
1034     wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
1035 #ifdef CTSIMVERSION
1036     msg += "Version ";
1037     msg += CTSIMVERSION;
1038     msg += "\n\n";
1039 #elif defined(VERSION)
1040     msg << "Version: " <<  VERSION << "\n\n";
1041 #endif
1042     msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
1043     
1044     wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
1045   }
1046 }
1047
1048
1049 ProjectionFileDocument*
1050 CTSimApp::newProjectionDoc()
1051 {
1052   ProjectionFileDocument* newDoc = dynamic_cast<ProjectionFileDocument*>(m_pDocTemplProjection->CreateDocument (""));
1053   if (newDoc) {
1054     ProjectionFileView* pView = newDoc->getView();
1055     if (pView) {
1056       wxFrame* pFrame = pView->getFrame();
1057       if (pFrame) {
1058         pFrame->SetSize (0,0);
1059         pFrame->Show (false);
1060       }
1061     }
1062     newDoc->SetDocumentName (m_pDocTemplProjection->GetDocumentName());
1063     newDoc->SetDocumentTemplate (m_pDocTemplProjection);
1064     newDoc->OnNewDocument();
1065   }
1066   
1067   return newDoc;
1068 }
1069
1070 ImageFileDocument*
1071 CTSimApp::newImageDoc()
1072 {
1073   ImageFileDocument* newDoc = dynamic_cast<ImageFileDocument*>(m_pDocTemplImage->CreateDocument (""));
1074   if (newDoc) {
1075     ImageFileView* pView = newDoc->getView();
1076     if (pView) {
1077       wxFrame* pFrame = pView->getFrame();
1078       if (pFrame) {
1079         pFrame->SetSize (0,0);
1080         pFrame->Show (false);
1081       }
1082     }
1083     newDoc->SetDocumentName (m_pDocTemplImage->GetDocumentName());
1084     newDoc->SetDocumentTemplate (m_pDocTemplImage);
1085     newDoc->OnNewDocument();
1086   }
1087   
1088   return newDoc;
1089 }
1090
1091 PlotFileDocument*
1092 CTSimApp::newPlotDoc()
1093 {
1094   PlotFileDocument* newDoc = dynamic_cast<PlotFileDocument*>(m_pDocTemplPlot->CreateDocument (""));
1095   if (newDoc) {
1096     PlotFileView* pView = newDoc->getView();
1097     if (pView) {
1098       wxFrame* pFrame = pView->getFrame();
1099       if (pFrame) {
1100         wxSize size;
1101         m_pFrame->GetClientSize (&size.x, &size.y);
1102         pFrame->SetSize (size.x / 2, size.y / 2);
1103         pFrame->Show (false);
1104       }
1105     }
1106     newDoc->SetDocumentName (m_pDocTemplPlot->GetDocumentName());
1107     newDoc->SetDocumentTemplate (m_pDocTemplPlot);
1108     newDoc->OnNewDocument();
1109   }
1110   
1111   return newDoc;
1112 }
1113
1114
1115 TextFileDocument*
1116 CTSimApp::newTextDoc()
1117 {
1118   wxString strFilename (getUntitledFilename());
1119   strFilename += ".txt";
1120   
1121   TextFileDocument* newDoc = dynamic_cast<TextFileDocument*>(m_pDocTemplText->CreateDocument (""));
1122   if (newDoc) {
1123     TextFileView* pView = newDoc->getView();
1124     if (pView) {
1125       wxFrame* pFrame = pView->getFrame();
1126       if (pFrame) {
1127         wxSize size;
1128         m_pFrame->GetClientSize (&size.x, &size.y);;
1129         pFrame->SetSize (size.x / 2, size.y / 2);
1130         pFrame->Show (false);
1131       }
1132     }
1133     newDoc->SetDocumentName (m_pDocTemplText->GetDocumentName());
1134     newDoc->SetDocumentTemplate (m_pDocTemplText);
1135     newDoc->OnNewDocument();
1136   }
1137   
1138   return newDoc;
1139 }
1140
1141
1142 PhantomFileDocument*
1143 CTSimApp::newPhantomDoc()
1144 {
1145   PhantomFileDocument* newDoc = dynamic_cast<PhantomFileDocument*>(m_pDocTemplPhantom->CreateDocument (""));
1146   if (newDoc) {
1147     PhantomFileView* pView = newDoc->getView();
1148     if (pView) {
1149       wxFrame* pFrame = pView->getFrame();
1150       if (pFrame)
1151         pFrame->SetSize (0,0);
1152     }
1153     newDoc->SetDocumentName (m_pDocTemplPhantom->GetDocumentName());
1154     newDoc->SetDocumentTemplate (m_pDocTemplPhantom);
1155     newDoc->OnNewDocument();
1156   }
1157   
1158   return newDoc;
1159 }
1160
1161 #if wxUSE_GLCANVAS
1162
1163 Graph3dFileDocument*
1164 CTSimApp::newGraph3dDoc()
1165 {
1166   Graph3dFileDocument* newDoc = dynamic_cast<Graph3dFileDocument*>(m_pDocTemplGraph3d->CreateDocument (""));
1167   if (newDoc) {
1168     Graph3dFileView* pView = newDoc->getView();
1169     if (pView) {
1170       wxFrame* pFrame = pView->getFrame();
1171       if (pFrame)
1172         pFrame->SetSize (0,0);
1173     }
1174     newDoc->SetDocumentName (m_pDocTemplGraph3d->GetDocumentName());
1175     newDoc->SetDocumentTemplate (m_pDocTemplGraph3d);
1176     newDoc->OnNewDocument();
1177   }
1178   
1179   return newDoc;
1180 }
1181 #endif