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