r261: Use explicit std:: namespace
[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.16 2000/12/16 06:12:47 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 \r
28 \r
29 // For compilers that support precompilation, includes "wx/wx.h".\r
30 #include "wx/wxprec.h"\r
31 \r
32 #ifdef __BORLANDC__\r
33 #pragma hdrstop\r
34 #endif\r
35 \r
36 #ifndef WX_PRECOMP\r
37 #include "wx/wx.h"\r
38 #endif\r
39 \r
40 #if !wxUSE_DOC_VIEW_ARCHITECTURE\r
41 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!\r
42 #endif\r
43 \r
44 #include "ct.h"\r
45 #include "ctsim.h"
46 #include "docs.h"
47 #include "views.h"
48 #include "dialogs.h"
49 \r
50 #if defined(HAVE_CONFIG_H)
51 #include "config.h"
52 #endif\r
53 \r
54 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
55 #ifdef MSVC\r
56 #define __STDC__ 1\r
57 #endif\r
58 #include "getopt.h"\r
59 #ifdef MSVC\r
60 #undef __STDC__\r
61 #endif
62 #endif
63 \r
64 static const char* rcsindent = "$Id: ctsim.cpp,v 1.16 2000/12/16 06:12:47 kevin Exp $";
65
66 class CTSimApp* theApp = NULL;
67
68 struct option CTSimApp::ctsimOptions[] = 
69 {
70     {"help", 0, 0, O_HELP},
71     {"version", 0, 0, O_VERSION},
72     {0, 0, 0, 0}
73 };
74
75 IMPLEMENT_APP(CTSimApp)
76
77 CTSimApp::CTSimApp()
78   : m_docManager(NULL), m_pFrame(NULL)
79 {
80     theApp = this;
81 }
82
83 #ifdef HAVE_SYS_TIME_H
84 #include <sys/time.h>
85 #endif
86
87 #ifdef HAVE_SYS_RESOURCE_H
88 #include <sys/resource.h>
89 #endif
90
91 bool
92 CTSimApp::OnInit()
93 {
94 #ifdef HAVE_SETPRIORITY
95   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
96 #endif
97
98     // process options
99     while (1) {
100       int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
101       if (c == -1)
102         break;
103
104       switch (c) {
105       case O_VERSION:
106                   std::cout << rcsindent << std::endl;
107           exit(0);
108       case O_HELP:
109       case '?':
110         usage (argv[0]);
111         exit (0);
112       default:
113         usage (argv[0]);
114         exit (1);
115       }
116     }
117
118     m_docManager = new wxDocManager;
119     
120     new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
121
122     new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
123
124     new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
125
126     //// Create the main frame window
127     m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
128     
129     SetTopWindow (m_pFrame);
130     m_pFrame->Centre(wxBOTH);
131
132     m_pFrame->Show(true);
133
134     for (int i = optind + 1; i <= argc; i++) {
135       wxString filename = argv [i - 1];
136       m_docManager->CreateDocument (filename, wxDOC_SILENT);
137     }
138
139     return true;
140 }
141
142 void
143 CTSimApp::usage(const char* program)
144 {
145         std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
146         std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
147         std::cout << "\n";
148         std::cout << "  --version Display version\n";
149         std::cout << "  --help    Display this help message\n";
150 }
151
152 int
153 CTSimApp::OnExit()
154 {
155     delete m_docManager;
156 #ifdef HAVE_DMALLOC
157     dmalloc_shutdown();
158 #endif
159     return 0;
160 }
161
162 wxString
163 CTSimApp::getUntitledFilename()
164 {
165   static int untitledNumber = 1;
166
167   wxString filename ("Untitled");
168   filename << untitledNumber++;
169
170   return (filename);
171 }
172
173
174 // Top-level window for CTSim
175
176 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
177
178 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
179   EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
180   EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
181   EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
182   EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
183   EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
184   EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
185   EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
186   EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
187   EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
188   EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
189   EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
190   EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
191   EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
192   EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
193   EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
194   EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
195   EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
196   EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
197   EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
198   EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
199   EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
200   EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
201   EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
202   EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
203   EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
204 END_EVENT_TABLE()
205
206
207
208 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
209   : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
210 {
211     m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
212     wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
213     CreateStatusBar();
214     SetStatusText ("Welcome to CTSim");
215
216     //// Make a menubar
217     wxMenu *file_menu = new wxMenu;
218     
219     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
220     file_menu->Append(wxID_OPEN, "&Open...");
221     
222     file_menu->AppendSeparator();
223     file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
224     
225     //  history of files visited
226     m_docManager->FileHistoryUseMenu(file_menu);
227     
228     m_pWindowMenu = new wxMenu;
229     m_pWindowMenu->UpdateUI (this);
230
231     wxMenu* help_menu = new wxMenu;
232     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
233     help_menu->AppendSeparator();
234     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
235     
236     wxMenuBar* menu_bar = new wxMenuBar;
237     
238     menu_bar->Append(file_menu, "&File");
239     menu_bar->Append(m_pWindowMenu, "&Window");
240     menu_bar->Append(help_menu, "&Help");
241     
242     SetMenuBar(menu_bar);
243
244     for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
245       m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
246       m_pWindowMenu->Append (m_apWindowMenuItems[i]);
247       m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
248     }
249 }
250
251 void 
252 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
253 {
254     wxMessageBox("CTSim\nThe Open Source Computed Tomography Simulator\nAuthor: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]", "About CTSim", wxOK | wxICON_INFORMATION, this);
255 }
256
257 void 
258 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
259 {
260     DialogGetPhantom dialogPhantom (this, Phantom::PHM_HERMAN);
261     int dialogReturn = dialogPhantom.ShowModal();
262     if (dialogReturn == wxID_OK) {
263       wxString selection (dialogPhantom.getPhantom());
264       *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
265       wxString filename = selection + ".phm";
266       theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
267     }
268     
269 }
270
271 void 
272 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
273 {
274     wxMessageBox("No help available, refer to man pages of command-line tools");
275 }
276
277 void 
278 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
279 {
280     Close(true);
281 }
282
283 void
284 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
285 {
286   int iPos = 0;
287   wxList& rListDocs = m_docManager->GetDocuments();
288   wxNode* pNode = rListDocs.GetFirst();
289   while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
290     wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
291     wxString strFilename = pDoc->GetFilename();
292     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
293     m_apWindowMenuData[iPos] = pDoc;
294     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
295     iPos++;
296     pNode = pNode->GetNext();
297   }
298   for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
299     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
300     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
301     m_apWindowMenuData[i] = NULL;
302   }
303     
304 }
305
306 void 
307 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
308 {
309   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
310     wxString strFilename = pDoc->GetFilename();
311     const wxView* pView = pDoc->GetFirstView();
312     if (pView) {
313       wxFrame* pFrame = pView->GetFrame();
314       pFrame->SetFocus();
315       pFrame->Raise();
316     }
317   }
318 }
319
320 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
321 { DoWindowMenu (0, event); }
322
323 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
324 { DoWindowMenu (1, event); }
325
326 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
327 { DoWindowMenu (2, event); }
328
329 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
330 { DoWindowMenu (3, event); }
331
332 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
333 { DoWindowMenu (4, event); }
334
335 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
336 { DoWindowMenu (5, event); }
337
338 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
339 { DoWindowMenu (6, event); }
340
341 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
342 { DoWindowMenu (7, event); }
343
344 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
345 { DoWindowMenu (8, event); }
346
347 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
348 { DoWindowMenu (9, event); }
349
350 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
351 { DoWindowMenu (10, event); }
352
353 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
354 { DoWindowMenu (11, event); }
355
356 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
357 { DoWindowMenu (12, event); }
358
359 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
360 { DoWindowMenu (13, event); }
361
362 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
363 { DoWindowMenu (14, event); }
364
365 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
366 { DoWindowMenu (15, event); }
367
368 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
369 { DoWindowMenu (16, event); }
370
371 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
372 { DoWindowMenu (17, event); }
373
374 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
375 { DoWindowMenu (18, event); }
376
377 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
378 { DoWindowMenu (19, event); }
379
380