source: git/src/aven.cc@ a338ef8

RELEASE/1.2 debug-ci debug-ci-sanitisers faster-cavernlog log-select main stereo stereo-2025 walls-data walls-data-hanging-as-warning warn-only-for-hanging-survey
Last change on this file since a338ef8 was 736f7df, checked in by Olly Betts <olly@…>, 12 years ago

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

  • Property mode set to 100644
File size: 10.7 KB
RevLine 
[0060ba5]1//
[203d2a7]2// aven.cc
[0060ba5]3//
[5809313]4// Main class for Aven.
[0060ba5]5//
[3675a18]6// Copyright (C) 2001 Mark R. Shinwell.
[5440c295]7// Copyright (C) 2002,2003,2004,2005,2006,2011,2013,2014 Olly Betts
[0060ba5]8//
[89231c4]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.
[0060ba5]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
[89231c4]16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
[0060ba5]18//
[89231c4]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
[ecbc6c18]21// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[5809313]22//
[0060ba5]23
[b462168]24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
[5809313]28#include "aven.h"
[78924eb]29#include "log.h"
[5809313]30#include "mainfrm.h"
[706b033]31
[d07944e]32#include "cmdline.h"
[93c3f97]33#include "message.h"
[c49e27f]34#include "useful.h"
[0060ba5]35
36#include <assert.h>
[de7a879]37#include <signal.h>
[c49e27f]38#include <stdio.h>
[0060ba5]39
[e0ffc2c]40#include <wx/confbase.h>
[573f4e9]41#include <wx/image.h>
[e0ffc2c]42#if wxUSE_DISPLAY
43// wxDisplay was added in wx 2.5; but it may not be built for mingw (because
44// the header seems to be missing).
[ca18348]45#include <wx/display.h>
46#endif
[573f4e9]47
[c293aa9]48bool double_buffered = false;
49
[879e9c3]50static const struct option long_opts[] = {
51 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
52 {"survey", required_argument, 0, 's'},
53 {"print", no_argument, 0, 'p'},
54 {"help", no_argument, 0, HLP_HELP},
55 {"version", no_argument, 0, HLP_VERSION},
56 {0, 0, 0, 0}
57};
58
59#define short_opts "s:p"
60
61static struct help_msg help[] = {
[45af761]62 /* <-- */
[736f7df]63 /* TRANSLATORS: --help output for --survey option.
64 *
65 * "this" has been added to English translation */
[45af761]66 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
[736f7df]67 /* TRANSLATORS: --help output for aven --print option */
[45af761]68 {HLP_ENCODELONG(1), /*print and exit (requires a 3d file)*/119, 0},
69 {0, 0, 0}
[879e9c3]70};
71
[5627cbb]72#ifdef __WXMSW__
[5809313]73IMPLEMENT_APP(Aven)
[5627cbb]74#else
75IMPLEMENT_APP_NO_MAIN(Aven)
76IMPLEMENT_WX_THEME_SUPPORT
77#endif
[0060ba5]78
[5809313]79Aven::Aven() :
[2c1d2f4]80 m_Frame(NULL), m_pageSetupData(NULL)
[0060ba5]81{
[1f81f3d]82 wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
[0060ba5]83}
84
[2c1d2f4]85Aven::~Aven()
86{
[81f1266]87 delete m_pageSetupData;
[2c1d2f4]88}
89
[5627cbb]90static int getopt_first_response = 0;
91
[5440c295]92static char ** utf8_argv;
93
[5627cbb]94#ifdef __WXMSW__
95bool Aven::Initialize(int& my_argc, wxChar **my_argv)
96{
[293eec8]97 // wxWidgets passes us wxChars, which may be wide characters but cmdline
98 // wants UTF-8 so we need to convert.
[5440c295]99 utf8_argv = new char * [my_argc + 1];
[5627cbb]100 for (int i = 0; i < my_argc; ++i){
101 utf8_argv[i] = strdup(wxString(my_argv[i]).mb_str());
102 }
103 utf8_argv[my_argc] = NULL;
104
[5440c295]105 msg_init(utf8_argv);
[f1cbf74]106 select_charset(CHARSET_UTF8);
[293eec8]107 /* Want --version and decent --help output, which cmdline does for us.
[f1cbf74]108 * wxCmdLine is much less good.
109 */
[736f7df]110 /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions
111 * - it should be translated to the terminology that cavers using the
112 * language would use.
113 *
114 * Part of aven --help */
[d8dbdff]115 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[5440c295]116 cmdline_init(my_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1);
[5627cbb]117 getopt_first_response = cmdline_getopt();
118 return wxApp::Initialize(my_argc, my_argv);
[f1cbf74]119}
[5627cbb]120#else
121int main(int argc, char **argv)
[0060ba5]122{
[27b8b59]123#ifdef __WXMAC__
[879e9c3]124 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
125 // wx ignores for us, but in wxApp::Initialize() which hasn't been
126 // called yet. So we need to remove it ourselves.
[5627cbb]127 if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
128 --argc;
129 memmove(argv + 1, argv + 2, argc * sizeof(char *));
[879e9c3]130 }
131#endif
132 // Call msg_init() and start processing the command line first so that
133 // we can respond to --help and --version even without an X display.
[5627cbb]134 msg_init(argv);
135 select_charset(CHARSET_UTF8);
[293eec8]136 /* Want --version and decent --help output, which cmdline does for us.
[5627cbb]137 * wxCmdLine is much less good.
138 */
[d8dbdff]139 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[5627cbb]140 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
[879e9c3]141 getopt_first_response = cmdline_getopt();
[5627cbb]142
[5440c295]143 utf8_argv = argv;
[5627cbb]144
[f891a0c]145#if wxUSE_UNICODE
[5627cbb]146 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
147 wxChar * wargv[2];
148 if (buf) {
149 wargv[0] = wxStrdup(buf);
150 } else {
151 // Eep - couldn't convert the executable's name to wide characters!
152 wargv[0] = wxStrdup(APP_NAME);
153 }
154 wargv[1] = NULL;
155 int wargc = 1;
156 return wxEntry(wargc, wargv);
[495cd3e]157#else
[a212391]158 char *dummy_argv[2] = { argv[0], NULL };
159 int dummy_argc = 1;
160 return wxEntry(dummy_argc, dummy_argv);
[495cd3e]161#endif
[879e9c3]162}
[5627cbb]163#endif
[879e9c3]164
165bool Aven::OnInit()
166{
[4013d9c]167 wxLog::SetActiveTarget(new MyLogWindow());
[421b7d2]168
[d607cea]169 {
[b1876d3]170 // Suppress message box warnings about messages not found.
[71ea9e1]171 wxLogNull logNo;
172 wxLocale *loc = new wxLocale();
[8a05a7a]173 loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
[b1876d3]174 wxString msg_lang_str(msg_lang, wxConvUTF8);
175 const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
176 wxString lang_str(lang, wxConvUTF8);
[f1d5d26]177#if wxCHECK_VERSION(2,9,0)
178 loc->Init(msg_lang_str, lang_str, msg_lang_str);
179#else
[6d1bc83]180 loc->Init(msg_lang_str, lang_str, msg_lang_str, true, true);
[f1d5d26]181#endif
[71ea9e1]182 // The existence of the wxLocale object is enough - no need to keep a
183 // pointer to it!
[003d953]184 }
[93c3f97]185
[d07944e]186 wxString survey;
[3ee64fb]187 bool print_and_exit = false;
[421b7d2]188
[0580c6a]189 while (true) {
[879e9c3]190 int opt;
191 if (getopt_first_response) {
192 opt = getopt_first_response;
193 getopt_first_response = 0;
194 } else {
195 opt = cmdline_getopt();
196 }
[d07944e]197 if (opt == EOF) break;
198 if (opt == 's') {
[5627cbb]199 survey = wxString(optarg, wxConvUTF8);
[d07944e]200 }
[3ee64fb]201 if (opt == 'p') {
202 print_and_exit = true;
203 }
204 }
205
[5440c295]206 if (print_and_exit && !utf8_argv[optind]) {
[3ee64fb]207 cmdline_syntax(); // FIXME : not a helpful error...
208 exit(1);
[d07944e]209 }
[068b4f2]210
[5627cbb]211 wxString fnm;
[5440c295]212 if (utf8_argv[optind]) {
213 fnm = wxString(utf8_argv[optind], wxConvUTF8);
214 if (fnm.empty() && *(utf8_argv[optind])) {
[c49e27f]215 ReportError(wxT("File argument's filename has bad encoding"));
[5627cbb]216 return false;
217 }
218 }
219
[c293aa9]220 // Use a double-buffered visual if available, as it will give much smoother
221 // animation.
222 double_buffered = true;
223 int wx_gl_attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
224 if (!InitGLVisual(wx_gl_attribs)) {
225 int wx_gl_attribs_no_db[] = { WX_GL_RGBA, 0 };
226 if (!InitGLVisual(wx_gl_attribs_no_db)) {
227 wxString m;
[736f7df]228 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
229 * perhaps by "Survex" or other things in future). */
[ee7511a]230 m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
[c293aa9]231 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
232 exit(1);
233 }
234 double_buffered = false;
[5892e08]235 }
236
[573f4e9]237 wxImage::AddHandler(new wxPNGHandler);
[08d2074]238
[486ae6a]239 // Obtain the screen size.
[3db0798]240 wxPoint pos(wxDefaultPosition);
[ca18348]241 int width, height;
[5627cbb]242 wxConfigBase::Get()->Read(wxT("width"), &width, 0);
243 if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
[3db0798]244 bool maximized = (width == -1);
245 bool full_screen = (width <= -2);
246 if (width <= 0 || height <= 0) {
[cea3549]247#if wxUSE_DISPLAY
[3db0798]248 wxRect geom = wxDisplay().GetGeometry();
249 pos.x = geom.x;
250 pos.y = geom.y;
251 width = geom.width;
252 height = geom.height;
[ca18348]253#else
[3db0798]254 wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
255 // Crude fix to help behaviour on multi-monitor displays.
256 // Fudge factors are a bit specific to my setup...
257 if (width > height * 3 / 2) {
258 pos.x += width;
259 width = height * 3 / 2;
260 pos.x -= width;
261 }
[ca18348]262#endif
[486ae6a]263
[3db0798]264 // Calculate a reasonable size for our window.
265 pos.x += width / 8;
266 pos.y += height / 8;
267 width = width * 3 / 4;
268 height = height * 3 / 4;
269 }
[486ae6a]270
271 // Create the main window.
[3db0798]272 m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
273
274 // Select full_screen or maximised if that's the saved state.
275 if (full_screen) {
276 m_Frame->ShowFullScreen(true);
277 } else if (maximized) {
278 m_Frame->Maximize();
279 }
[068b4f2]280
[5440c295]281 if (utf8_argv[optind]) {
[5627cbb]282 m_Frame->OpenFile(fnm, survey);
[d07944e]283 }
[068b4f2]284
[3ee64fb]285 if (print_and_exit) {
286 wxCommandEvent dummy;
287 m_Frame->OnPrint(dummy);
288 m_Frame->OnQuit(dummy);
289 return true;
290 }
291
[5809313]292 m_Frame->Show(true);
[526775d]293#ifdef _WIN32
294 m_Frame->SetFocus();
295#endif
[0060ba5]296 return true;
297}
298
[e0ffc2c]299wxPageSetupDialogData *
300Aven::GetPageSetupDialogData()
301{
[2c1d2f4]302 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
[e0ffc2c]303#ifdef __WXGTK__
304 // Fetch paper margins stored on disk.
305 int left, right, top, bottom;
306 wxConfigBase * cfg = wxConfigBase::Get();
307 // These default margins were chosen by looking at all the .ppd files
308 // on my machine.
[5627cbb]309 cfg->Read(wxT("paper_margin_left"), &left, 7);
310 cfg->Read(wxT("paper_margin_right"), &right, 7);
311 cfg->Read(wxT("paper_margin_top"), &top, 14);
312 cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
[2c1d2f4]313 m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
314 m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
[e0ffc2c]315#endif
[2c1d2f4]316 return m_pageSetupData;
[e0ffc2c]317}
318
319void
320Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
321{
[2c1d2f4]322 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
323 *m_pageSetupData = psdd;
[e0ffc2c]324#ifdef __WXGTK__
325 wxPoint topleft = psdd.GetMarginTopLeft();
326 wxPoint bottomright = psdd.GetMarginBottomRight();
327
328 // Store user specified paper margins on disk/in registry.
329 wxConfigBase * cfg = wxConfigBase::Get();
[5627cbb]330 cfg->Write(wxT("paper_margin_left"), topleft.x);
331 cfg->Write(wxT("paper_margin_right"), bottomright.x);
332 cfg->Write(wxT("paper_margin_top"), topleft.y);
333 cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
[e0ffc2c]334 cfg->Flush();
335#endif
336}
337
[5809313]338void Aven::ReportError(const wxString& msg)
[0060ba5]339{
[c8b5fa5]340 if (!m_Frame) {
341 wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
342 return;
343 }
[1c6b20d7]344 AvenAllowOnTop ontop(m_Frame);
[7015222]345 wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
346 dlg.ShowModal();
[0060ba5]347}
[59fda0a]348
[5627cbb]349wxString
350wmsg(int msg_no)
351{
[833c6bc]352 return wxString::FromUTF8(msg(msg_no));
[5627cbb]353}
354
[8a05a7a]355const wxString &
356wmsg_cfgpth()
357{
358 static wxString path;
359 if (path.empty())
360 path = wxString(msg_cfgpth(), wxConvUTF8);
361 return path;
362}
363
[a9a32f2]364// called to report errors by message.c
365extern "C" void
366aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
367{
[c49e27f]368 wxString m;
369 if (fnm) {
370 m = wxString(fnm, wxConvUTF8);
371 if (line) m += wxString::Format(wxT(":%d"), line);
372 m += wxT(": ");
373 }
374
375 if (severity == 0) {
376 m += wmsg(/*warning*/4);
377 m += wxT(": ");
378 }
379
380 char buf[1024];
381 vsnprintf(buf, sizeof(buf), msg(en), ap);
382 m += wxString(buf, wxConvUTF8);
383 if (wxTheApp == NULL) {
384 // We haven't initialised the Aven app object yet.
385 if (!wxInitialize()) {
386 fputs(buf, stderr);
387 PUTC('\n', stderr);
388 exit(1);
389 }
390 wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
391 wxUninitialize();
392 } else {
393 wxGetApp().ReportError(m);
394 }
[a9a32f2]395}
Note: See TracBrowser for help on using the repository browser.