r148: *** empty log message ***
[ctsim.git] / src / ctsim.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsim.h
5 **   Purpose:       Header file for 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.5 2000/07/15 08:36:13 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 "ct.h"
45 #include "ctsim.h"
46 #include "docs.h"
47 #include "views.h"
48
49 class CTSimApp* theApp = NULL;
50
51 struct option CTSimApp::ctsimOptions[] = 
52 {
53     {"help", 0, 0, O_HELP},
54     {0, 0, 0, 0}
55 };
56
57 IMPLEMENT_APP(CTSimApp)
58
59 CTSimApp::CTSimApp(void)
60   : m_docManager(NULL), m_pFrame(NULL)
61 {
62     theApp = this;
63 }
64
65 bool
66 CTSimApp::OnInit(void)
67 {
68     // process options
69     while (1) {
70       int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
71       if (c == -1)
72         break;
73
74       switch (c) {
75       case O_HELP:
76       case '?':
77         usage (argv[0]);
78         exit (0);
79       default:
80         usage (argv[0]);
81         exit (1);
82       }
83     }
84
85     m_docManager = new wxDocManager;
86     
87     (void) new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
88
89     (void) new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
90
91     (void) new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
92
93     //// Create the main frame window
94     m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
95     
96     //// Make a menubar
97     wxMenu *file_menu = new wxMenu;
98     
99     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "&Create Phantom...");
100     file_menu->Append(wxID_OPEN, "&Open...");
101     
102     file_menu->AppendSeparator();
103     file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
104     
105     // A nice touch: a history of files visited. Use this menu.
106     m_docManager->FileHistoryUseMenu(file_menu);
107     
108     wxMenu *help_menu = new wxMenu;
109     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
110     help_menu->AppendSeparator();
111     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
112     
113     wxMenuBar *menu_bar = new wxMenuBar;
114     
115     menu_bar->Append(file_menu, "&File");
116     menu_bar->Append(help_menu, "&Help");
117     
118     m_pFrame->SetMenuBar(menu_bar);
119     
120     SetTopWindow (m_pFrame);
121     m_pFrame->Centre(wxBOTH);
122
123     m_pFrame->Show(true);
124
125     for (int i = optind + 1; i <= argc; i++) {
126       wxString filename = argv [i - 1];
127       m_docManager->CreateDocument (filename, wxDOC_SILENT);
128     }
129
130     return true;
131 }
132
133 void
134 CTSimApp::usage(const char* program)
135 {
136     cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
137     cout << "Computed Tomography Simulator (Graphical Shell)\n";
138     cout << "\n";
139     cout << "  --help    Display this help message\n";
140 }
141
142 int
143 CTSimApp::OnExit(void)
144 {
145     delete m_docManager;
146     return 0;
147 }
148
149 wxString
150 CTSimApp::getUntitledFilename(void)
151 {
152   static int untitledNumber = 1;
153
154   wxString filename ("Untitled");
155   filename << untitledNumber++;
156
157   return (filename);
158 }
159
160
161 // Top-level window for CTSim
162
163 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
164
165 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
166   EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
167   EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
168   EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
169   EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
170 END_EVENT_TABLE()
171
172
173 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
174   : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
175 {
176     CreateStatusBar();
177     m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
178     wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
179     SetStatusText ("Welcome to CTSim");
180 }
181
182 void 
183 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
184 {
185     wxMessageBox("CTSim\nAuthor: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]", "About CTSim", wxOK | wxICON_INFORMATION, this);
186 }
187
188 void 
189 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
190 {
191     wxString choiceStringList [5];
192     choiceStringList[0] = Phantom::PHM_HERMAN_STR;
193     choiceStringList[1] = Phantom::PHM_BHERMAN_STR;
194     choiceStringList[2] = Phantom::PHM_ROWLAND_STR;;
195     choiceStringList[3] = Phantom::PHM_BROWLAND_STR;
196     choiceStringList[4] = Phantom::PHM_UNITPULSE_STR;
197     wxString choiceTitleList [5];
198     choiceTitleList[0] = "Herman Head";
199     choiceTitleList[1] = "Herman Head Bordered";
200     choiceTitleList[2] = "Rowland Head";
201     choiceTitleList[3] = "Rowland Head Bordered";
202     choiceTitleList[4] = "Unit Pulse";
203     wxSingleChoiceDialog dialog (this, "Select phantom", "Phantom Selection", 5, choiceTitleList, NULL, wxOK|wxCANCEL|wxCENTRE);
204
205     int dialogReturn = dialog.ShowModal();
206     if (dialogReturn == wxID_OK) {
207         int selection = dialog.GetSelection();
208         *theApp->getLog() << "Selected phantom " << selection << "\n";
209         wxString filename = choiceStringList[selection] + ".phm";
210         theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
211     }
212     
213 }
214
215 void 
216 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
217 {
218     wxMessageBox("No help available, refer to man pages of command-line tools");
219 }
220
221 void 
222 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
223 {
224     Close(true);
225 }
226