r188: *** 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-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsim.cpp,v 1.11 2000/09/02 16:40:36 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.11 2000/09/02 16:40:36 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(void)
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(void)
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     (void) new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
113
114     (void) new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
115
116     (void) 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     //// Make a menubar
122     wxMenu *file_menu = new wxMenu;
123     
124     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
125     file_menu->Append(wxID_OPEN, "&Open...");
126     
127     file_menu->AppendSeparator();
128     file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
129     
130     // A nice touch: a history of files visited. Use this menu.
131     m_docManager->FileHistoryUseMenu(file_menu);
132     
133     wxMenu *help_menu = new wxMenu;
134     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
135     help_menu->AppendSeparator();
136     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
137     
138     wxMenuBar *menu_bar = new wxMenuBar;
139     
140     menu_bar->Append(file_menu, "&File");
141     menu_bar->Append(help_menu, "&Help");
142     
143     m_pFrame->SetMenuBar(menu_bar);
144     
145     SetTopWindow (m_pFrame);
146     m_pFrame->Centre(wxBOTH);
147
148     m_pFrame->Show(true);
149
150     for (int i = optind + 1; i <= argc; i++) {
151       wxString filename = argv [i - 1];
152       m_docManager->CreateDocument (filename, wxDOC_SILENT);
153     }
154
155     return true;
156 }
157
158 void
159 CTSimApp::usage(const char* program)
160 {
161     cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
162     cout << "Computed Tomography Simulator (Graphical Shell)\n";
163     cout << "\n";
164     cout << "  --version Display version\n";
165     cout << "  --help    Display this help message\n";
166 }
167
168 int
169 CTSimApp::OnExit(void)
170 {
171     delete m_docManager;
172 #ifdef HAVE_DMALLOC
173     dmalloc_shutdown();
174 #endif
175     return 0;
176 }
177
178 wxString
179 CTSimApp::getUntitledFilename(void)
180 {
181   static int untitledNumber = 1;
182
183   wxString filename ("Untitled");
184   filename << untitledNumber++;
185
186   return (filename);
187 }
188
189
190 // Top-level window for CTSim
191
192 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
193
194 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
195   EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
196   EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
197   EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
198   EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
199 END_EVENT_TABLE()
200
201
202 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
203   : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
204 {
205     CreateStatusBar();
206     m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
207     wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
208     SetStatusText ("Welcome to CTSim");
209 }
210
211 void 
212 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
213 {
214     wxMessageBox("CTSim\nAuthor: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]", "About CTSim", wxOK | wxICON_INFORMATION, this);
215 }
216
217 void 
218 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
219 {
220     DialogGetPhantom dialogPhantom (this, Phantom::PHM_HERMAN);
221     int dialogReturn = dialogPhantom.ShowModal();
222     if (dialogReturn == wxID_OK) {
223       wxString selection (dialogPhantom.getPhantom());
224       *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
225       wxString filename = selection + ".phm";
226       theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
227     }
228     
229 }
230
231 void 
232 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
233 {
234     wxMessageBox("No help available, refer to man pages of command-line tools");
235 }
236
237 void 
238 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
239 {
240     Close(true);
241 }
242