r160: *** 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.9 2000/07/23 01:49:03 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";
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 bool
76 CTSimApp::OnInit(void)
77 {
78     // process options
79     while (1) {
80       int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
81       if (c == -1)
82         break;
83
84       switch (c) {
85       case O_VERSION:
86           cout << rcsindent << endl;
87           exit(0);
88       case O_HELP:
89       case '?':
90         usage (argv[0]);
91         exit (0);
92       default:
93         usage (argv[0]);
94         exit (1);
95       }
96     }
97
98     m_docManager = new wxDocManager;
99     
100     (void) new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
101
102     (void) new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
103
104     (void) new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
105
106     //// Create the main frame window
107     m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
108     
109     //// Make a menubar
110     wxMenu *file_menu = new wxMenu;
111     
112     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
113     file_menu->Append(wxID_OPEN, "&Open...");
114     
115     file_menu->AppendSeparator();
116     file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
117     
118     // A nice touch: a history of files visited. Use this menu.
119     m_docManager->FileHistoryUseMenu(file_menu);
120     
121     wxMenu *help_menu = new wxMenu;
122     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
123     help_menu->AppendSeparator();
124     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
125     
126     wxMenuBar *menu_bar = new wxMenuBar;
127     
128     menu_bar->Append(file_menu, "&File");
129     menu_bar->Append(help_menu, "&Help");
130     
131     m_pFrame->SetMenuBar(menu_bar);
132     
133     SetTopWindow (m_pFrame);
134     m_pFrame->Centre(wxBOTH);
135
136     m_pFrame->Show(true);
137
138     for (int i = optind + 1; i <= argc; i++) {
139       wxString filename = argv [i - 1];
140       m_docManager->CreateDocument (filename, wxDOC_SILENT);
141     }
142
143     return true;
144 }
145
146 void
147 CTSimApp::usage(const char* program)
148 {
149     cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
150     cout << "Computed Tomography Simulator (Graphical Shell)\n";
151     cout << "\n";
152     cout << "  --version Display version\n";
153     cout << "  --help    Display this help message\n";
154 }
155
156 int
157 CTSimApp::OnExit(void)
158 {
159     delete m_docManager;
160 #ifdef HAVE_DMALLOC
161     dmalloc_shutdown();
162 #endif
163     return 0;
164 }
165
166 wxString
167 CTSimApp::getUntitledFilename(void)
168 {
169   static int untitledNumber = 1;
170
171   wxString filename ("Untitled");
172   filename << untitledNumber++;
173
174   return (filename);
175 }
176
177
178 // Top-level window for CTSim
179
180 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
181
182 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
183   EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
184   EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
185   EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
186   EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
187 END_EVENT_TABLE()
188
189
190 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
191   : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
192 {
193     CreateStatusBar();
194     m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
195     wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
196     SetStatusText ("Welcome to CTSim");
197 }
198
199 void 
200 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
201 {
202     wxMessageBox("CTSim\nAuthor: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]", "About CTSim", wxOK | wxICON_INFORMATION, this);
203 }
204
205 void 
206 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
207 {
208     DialogGetPhantom dialogPhantom (this, Phantom::PHM_HERMAN);
209     int dialogReturn = dialogPhantom.ShowModal();
210     if (dialogReturn == wxID_OK) {
211       wxString selection (dialogPhantom.getPhantom());
212       *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
213       wxString filename = selection + ".phm";
214       theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
215     }
216     
217 }
218
219 void 
220 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
221 {
222     wxMessageBox("No help available, refer to man pages of command-line tools");
223 }
224
225 void 
226 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
227 {
228     Close(true);
229 }
230