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