1 | // |
---|
2 | // aven.cc |
---|
3 | // |
---|
4 | // Main class for Aven. |
---|
5 | // |
---|
6 | // Copyright (C) 2001 Mark R. Shinwell. |
---|
7 | // Copyright (C) 2002,2003,2004,2005 Olly Betts |
---|
8 | // |
---|
9 | // This program is free software; you can redistribute it and/or modify |
---|
10 | // it under the terms of the GNU General Public License as published by |
---|
11 | // the Free Software Foundation; either version 2 of the License, or |
---|
12 | // (at your option) any later version. |
---|
13 | // |
---|
14 | // This program is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | // GNU General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU General Public License |
---|
20 | // along with this program; if not, write to the Free Software |
---|
21 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | // |
---|
23 | |
---|
24 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "aven.h" |
---|
29 | #include "mainfrm.h" |
---|
30 | |
---|
31 | #include "cmdline.h" |
---|
32 | #include "message.h" |
---|
33 | |
---|
34 | #include <assert.h> |
---|
35 | #include <signal.h> |
---|
36 | |
---|
37 | #include <wx/confbase.h> |
---|
38 | #include <wx/image.h> |
---|
39 | #if wxUSE_DISPLAY |
---|
40 | // wxDisplay was added in wx 2.5; but it may not be built for mingw (because |
---|
41 | // the header seems to be missing). |
---|
42 | #include <wx/display.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | IMPLEMENT_APP(Aven) |
---|
46 | |
---|
47 | Aven::Aven() : |
---|
48 | m_Frame(NULL), m_pageSetupData(NULL) |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | Aven::~Aven() |
---|
53 | { |
---|
54 | if (m_pageSetupData) delete m_pageSetupData; |
---|
55 | } |
---|
56 | |
---|
57 | bool Aven::OnInit() |
---|
58 | { |
---|
59 | #ifdef __WXMAC__ |
---|
60 | // Tell wxMac which the About menu item is so it can be put where MacOS |
---|
61 | // users expect it to be. |
---|
62 | wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT; |
---|
63 | // wxMac is supposed to remove this magic command line option (which |
---|
64 | // Finder passes), but the code in 2.4.2 is bogus. It just decrements |
---|
65 | // argc rather than copying argv down. But luckily it also fails to |
---|
66 | // set argv[argc] to NULL so we can just recalculate argc, then remove |
---|
67 | // the -psn_* switch ourselves. |
---|
68 | if (argv[argc]) { |
---|
69 | argc = 1; |
---|
70 | while (argv[argc]) ++argc; |
---|
71 | } |
---|
72 | if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) { |
---|
73 | --argc; |
---|
74 | memmove(argv + 1, argv + 2, argc * sizeof(char *)); |
---|
75 | } |
---|
76 | #endif |
---|
77 | msg_init(argv); |
---|
78 | |
---|
79 | const char *lang = msg_lang2 ? msg_lang2 : msg_lang; |
---|
80 | if (lang) { |
---|
81 | // Suppress message box warnings about messages not found. |
---|
82 | wxLogNull logNo; |
---|
83 | wxLocale *loc = new wxLocale(); |
---|
84 | loc->AddCatalogLookupPathPrefix(msg_cfgpth()); |
---|
85 | loc->Init(lang, lang, lang, TRUE, TRUE); |
---|
86 | // The existence of the wxLocale object is enough - no need to keep a |
---|
87 | // pointer to it! |
---|
88 | } |
---|
89 | |
---|
90 | wxString survey; |
---|
91 | bool print_and_exit = false; |
---|
92 | |
---|
93 | /* Want --version and a decent --help output, which cmdline does for us. |
---|
94 | * wxCmdLine is much less good. |
---|
95 | */ |
---|
96 | static const struct option long_opts[] = { |
---|
97 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
98 | {"survey", required_argument, 0, 's'}, |
---|
99 | {"print", no_argument, 0, 'p'}, |
---|
100 | {"help", no_argument, 0, HLP_HELP}, |
---|
101 | {"version", no_argument, 0, HLP_VERSION}, |
---|
102 | {0, 0, 0, 0} |
---|
103 | }; |
---|
104 | |
---|
105 | #define short_opts "s:p" |
---|
106 | |
---|
107 | static struct help_msg help[] = { |
---|
108 | /* <-- */ |
---|
109 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
110 | {HLP_ENCODELONG(1), "print and exit (requires a 3d file)"}, |
---|
111 | {0, 0} |
---|
112 | }; |
---|
113 | |
---|
114 | cmdline_set_syntax_message("[3d file]", NULL); |
---|
115 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1); |
---|
116 | while (true) { |
---|
117 | int opt = cmdline_getopt(); |
---|
118 | if (opt == EOF) break; |
---|
119 | if (opt == 's') { |
---|
120 | survey = optarg; |
---|
121 | } |
---|
122 | if (opt == 'p') { |
---|
123 | print_and_exit = true; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | if (print_and_exit && !argv[optind]) { |
---|
128 | cmdline_syntax(); // FIXME : not a helpful error... |
---|
129 | exit(1); |
---|
130 | } |
---|
131 | |
---|
132 | wxImage::AddHandler(new wxPNGHandler); |
---|
133 | |
---|
134 | // Obtain the screen size. |
---|
135 | int x, y; |
---|
136 | int width, height; |
---|
137 | #if wxUSE_DISPLAY // wxDisplay was added in wx 2.5 |
---|
138 | wxRect geom = wxDisplay().GetGeometry(); |
---|
139 | x = geom.x; |
---|
140 | y = geom.y; |
---|
141 | width = geom.width; |
---|
142 | height = geom.height; |
---|
143 | #else |
---|
144 | wxClientDisplayRect(&x, &y, &width, &height); |
---|
145 | // Crude fix to help behaviour on multi-monitor displays. |
---|
146 | // Fudge factors are a bit specific to my setup... |
---|
147 | if (width > height * 3 / 2) { |
---|
148 | x += width; |
---|
149 | width = height * 3 / 2; |
---|
150 | x -= width; |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | // Calculate a reasonable size for our window. |
---|
155 | x += width / 8; |
---|
156 | y += height / 8; |
---|
157 | width = width * 3 / 4; |
---|
158 | height = height * 3 / 4; |
---|
159 | |
---|
160 | // Create the main window. |
---|
161 | m_Frame = new MainFrm(APP_NAME, wxPoint(x, y), wxSize(width, height)); |
---|
162 | |
---|
163 | if (argv[optind]) { |
---|
164 | m_Frame->OpenFile(wxString(argv[optind]), survey, true); |
---|
165 | } |
---|
166 | |
---|
167 | if (print_and_exit) { |
---|
168 | wxCommandEvent dummy; |
---|
169 | m_Frame->OnPrint(dummy); |
---|
170 | m_Frame->OnQuit(dummy); |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | m_Frame->Show(true); |
---|
175 | #ifdef _WIN32 |
---|
176 | m_Frame->SetFocus(); |
---|
177 | #endif |
---|
178 | return true; |
---|
179 | } |
---|
180 | |
---|
181 | wxPageSetupDialogData * |
---|
182 | Aven::GetPageSetupDialogData() |
---|
183 | { |
---|
184 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
185 | #ifdef __WXGTK__ |
---|
186 | // Fetch paper margins stored on disk. |
---|
187 | int left, right, top, bottom; |
---|
188 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
189 | // These default margins were chosen by looking at all the .ppd files |
---|
190 | // on my machine. |
---|
191 | cfg->Read("paper_margin_left", &left, 7); |
---|
192 | cfg->Read("paper_margin_right", &right, 7); |
---|
193 | cfg->Read("paper_margin_top", &top, 14); |
---|
194 | cfg->Read("paper_margin_bottom", &bottom, 14); |
---|
195 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
---|
196 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
---|
197 | #endif |
---|
198 | return m_pageSetupData; |
---|
199 | } |
---|
200 | |
---|
201 | void |
---|
202 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
---|
203 | { |
---|
204 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
205 | *m_pageSetupData = psdd; |
---|
206 | #ifdef __WXGTK__ |
---|
207 | wxPoint topleft = psdd.GetMarginTopLeft(); |
---|
208 | wxPoint bottomright = psdd.GetMarginBottomRight(); |
---|
209 | |
---|
210 | // Store user specified paper margins on disk/in registry. |
---|
211 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
212 | cfg->Write("paper_margin_left", topleft.x); |
---|
213 | cfg->Write("paper_margin_right", bottomright.x); |
---|
214 | cfg->Write("paper_margin_top", topleft.y); |
---|
215 | cfg->Write("paper_margin_bottom", bottomright.y); |
---|
216 | cfg->Flush(); |
---|
217 | #endif |
---|
218 | } |
---|
219 | |
---|
220 | void Aven::ReportError(const wxString& msg) |
---|
221 | { |
---|
222 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
---|
223 | dlg.ShowModal(); |
---|
224 | } |
---|
225 | |
---|
226 | // called to report errors by message.c |
---|
227 | extern "C" void |
---|
228 | aven_v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
229 | { |
---|
230 | wxString m; |
---|
231 | if (fnm) { |
---|
232 | m = fnm; |
---|
233 | if (line) m += wxString::Format(":%d", line); |
---|
234 | m += ": "; |
---|
235 | } |
---|
236 | |
---|
237 | if (severity == 0) { |
---|
238 | m += msg(/*warning*/4); |
---|
239 | m += ": "; |
---|
240 | } |
---|
241 | |
---|
242 | wxString s; |
---|
243 | s.PrintfV(msg(en), ap); |
---|
244 | m += s; |
---|
245 | wxGetApp().ReportError(m); |
---|
246 | } |
---|