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