Fix GCC warning
[survex.git] / src / aven.cc
blob11c4de85ed5fa1c9f5dccd6f2f458d59cd8676e2
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,2006,2011,2013,2014,2015,2016,2017,2018 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.
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.
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
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #define MSG_SETUP_PROJ_SEARCH_PATH 1
30 #include "aven.h"
31 #include "log.h"
32 #include "gla.h"
33 #include "mainfrm.h"
35 #include "cmdline.h"
36 #include "message.h"
37 #include "useful.h"
39 #include <assert.h>
40 #include <stdio.h>
42 #include <wx/confbase.h>
43 #include <wx/image.h>
44 #if wxUSE_DISPLAY
45 // wxDisplay was added in wx 2.5; but it may not be built for mingw (because
46 // the header seems to be missing).
47 #include <wx/display.h>
48 #endif
50 #ifdef __WXMSW__
51 #include <windows.h>
52 #endif
54 static const struct option long_opts[] = {
55 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
56 {"survey", required_argument, 0, 's'},
57 {"print", no_argument, 0, 'p'},
58 {"help", no_argument, 0, HLP_HELP},
59 {"version", no_argument, 0, HLP_VERSION},
60 {0, 0, 0, 0}
63 #define short_opts "s:p"
65 static struct help_msg help[] = {
66 /* <-- */
67 /* TRANSLATORS: --help output for --survey option.
69 * "this" has been added to English translation */
70 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
71 /* TRANSLATORS: --help output for aven --print option */
72 {HLP_ENCODELONG(1), /*print and exit (requires a 3d file)*/119, 0},
73 {0, 0, 0}
76 #ifdef __WXMSW__
77 IMPLEMENT_APP(Aven)
78 #else
79 IMPLEMENT_APP_NO_MAIN(Aven)
80 IMPLEMENT_WX_THEME_SUPPORT
81 #endif
83 Aven::Aven() :
84 m_Frame(NULL), m_pageSetupData(NULL)
86 wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
89 Aven::~Aven()
91 delete m_pageSetupData;
94 static int getopt_first_response = 0;
96 static char ** utf8_argv;
98 #ifdef __WXMSW__
99 bool Aven::Initialize(int& my_argc, wxChar **my_argv)
101 const wxChar * cmd_line = GetCommandLineW();
103 // Horrible bodge to handle therion's assumptions about the "Process"
104 // file association.
105 if (cmd_line) {
106 // None of these are valid aven command line options, so this is not
107 // going to be triggered accidentally.
108 const wxChar * p = wxStrstr(cmd_line,
109 wxT("aven.exe\" --quiet --log --output="));
110 if (p) {
111 // Just change the command name in the command line string - that
112 // way the quoting should match what the C runtime expects.
113 wxString cmd(cmd_line, p - cmd_line);
114 cmd += "cavern";
115 cmd += p + 4;
116 exit(wxExecute(cmd, wxEXEC_SYNC));
120 int utf8_argc;
122 // wxWidgets doesn't split up the command line in the standard way, so
123 // redo it ourselves using the standard API function.
125 // Warning: The returned array from this has no terminating NULL
126 // element.
127 wxChar ** new_argv = NULL;
128 if (cmd_line)
129 new_argv = CommandLineToArgvW(cmd_line, &utf8_argc);
130 bool failed = (new_argv == NULL);
131 if (failed) {
132 wxChar * p;
133 FormatMessage(
134 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
135 NULL,
136 GetLastError(),
138 (LPWSTR)&p,
139 4096,
140 NULL);
141 wxString m = "CommandLineToArgvW failed: ";
142 m += p;
143 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
144 LocalFree(p);
145 utf8_argc = my_argc;
146 new_argv = my_argv;
149 // Convert wide characters to UTF-8.
150 utf8_argv = new char * [utf8_argc + 1];
151 for (int i = 0; i < utf8_argc; ++i){
152 utf8_argv[i] = strdup(wxString(new_argv[i]).utf8_str());
154 utf8_argv[utf8_argc] = NULL;
156 if (!failed) LocalFree(new_argv);
159 msg_init(utf8_argv);
160 select_charset(CHARSET_UTF8);
161 /* Want --version and decent --help output, which cmdline does for us.
162 * wxCmdLine is much less good.
164 /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions
165 * - it should be translated to the terminology that cavers using the
166 * language would use.
168 * Part of aven --help */
169 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
170 cmdline_init(utf8_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1);
171 getopt_first_response = cmdline_getopt();
173 // The argc and argv arguments don't actually get used here.
174 int dummy_argc = 0;
175 return wxApp::Initialize(dummy_argc, NULL);
177 #else
178 int main(int argc, char **argv)
180 #ifdef __WXGTK3__
181 # if !(wxUSE_GLCANVAS_EGL-0)
182 // The GLX-based wxGLCanvas doesn't work under Wayland, and the code
183 // segfaults: https://trac.wxwidgets.org/ticket/17702
185 // Therefore we force X11 unless we're using the EGL-based wxGLCanvas
186 // (which was added in wxWidgets 3.1.5 and hasn't been backported to
187 // 3.0.x).
189 // Setting GDK_BACKEND=x11 is the recommended workaround, and it seems to
190 // work to set it here. GTK2 doesn't support Wayland, so doesn't need
191 // this.
192 setenv("GDK_BACKEND", "x11", 1);
193 # endif
194 #endif
196 #ifdef __WXMAC__
197 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
198 // wx ignores for us, but in wxApp::Initialize() which hasn't been
199 // called yet. So we need to remove it ourselves.
200 if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
201 --argc;
202 memmove(argv + 1, argv + 2, argc * sizeof(char *));
204 #endif
205 // Call msg_init() and start processing the command line first so that
206 // we can respond to --help and --version even without an X display.
207 msg_init(argv);
208 select_charset(CHARSET_UTF8);
209 /* Want --version and decent --help output, which cmdline does for us.
210 * wxCmdLine is much less good.
212 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
213 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
214 getopt_first_response = cmdline_getopt();
216 utf8_argv = argv;
218 #if wxUSE_UNICODE
219 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
220 wxChar * wargv[2];
221 if (buf) {
222 wargv[0] = wxStrdup(buf);
223 } else {
224 // Eep - couldn't convert the executable's name to wide characters!
225 wargv[0] = wxStrdup(APP_NAME);
227 wargv[1] = NULL;
228 int wargc = 1;
229 return wxEntry(wargc, wargv);
230 #else
231 char *dummy_argv[2] = { argv[0], NULL };
232 int dummy_argc = 1;
233 return wxEntry(dummy_argc, dummy_argv);
234 #endif
236 #endif
238 bool Aven::OnInit()
240 wxLog::SetActiveTarget(new MyLogWindow());
243 // Suppress message box warnings about messages not found.
244 wxLogNull logNo;
245 wxLocale *loc = new wxLocale();
246 loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
247 wxString msg_lang_str(msg_lang, wxConvUTF8);
248 const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
249 wxString lang_str(lang, wxConvUTF8);
250 loc->Init(msg_lang_str, lang_str, msg_lang_str);
251 // The existence of the wxLocale object is enough - no need to keep a
252 // pointer to it!
255 const char* opt_survey = NULL;
256 bool print_and_exit = false;
258 while (true) {
259 int opt;
260 if (getopt_first_response) {
261 opt = getopt_first_response;
262 getopt_first_response = 0;
263 } else {
264 opt = cmdline_getopt();
266 if (opt == EOF) break;
267 if (opt == 's') {
268 if (opt_survey != NULL) {
269 // FIXME: Not a helpful error, but this is temporary until
270 // we actually hook up support for specifying multiple
271 // --survey options properly here.
272 cmdline_syntax();
273 exit(1);
275 opt_survey = optarg;
277 if (opt == 'p') {
278 print_and_exit = true;
282 if (print_and_exit && !utf8_argv[optind]) {
283 cmdline_syntax(); // FIXME : not a helpful error...
284 exit(1);
287 wxString fnm;
288 if (utf8_argv[optind]) {
289 fnm = wxString(utf8_argv[optind], wxConvUTF8);
290 if (fnm.empty() && *(utf8_argv[optind])) {
291 ReportError(wxT("File argument's filename has bad encoding"));
292 return false;
296 if (!GLACanvas::check_visual()) {
297 wxString m;
298 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
299 * perhaps by "Survex" or other things in future). */
300 m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
301 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
302 exit(1);
305 wxImage::AddHandler(new wxPNGHandler);
307 // Obtain the screen geometry.
308 #if wxUSE_DISPLAY
309 wxRect geom = wxDisplay().GetGeometry();
310 #else
311 wxRect geom;
312 wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height);
313 #endif
315 wxPoint pos(wxDefaultPosition);
316 int width, height;
317 wxConfigBase::Get()->Read(wxT("width"), &width, 0);
318 if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
319 // We used to persist full screen mode (-1 was maximized,
320 // -2 full screen), but people would get stuck in full
321 // screen mode, unsure how to exit.
322 bool maximized = (width <= -1);
323 if (width <= 0 || height <= 0) {
324 pos.x = geom.x;
325 pos.y = geom.y;
326 width = geom.width;
327 height = geom.height;
329 // Calculate a reasonable size for our window.
330 pos.x += width / 8;
331 pos.y += height / 8;
332 width = width * 3 / 4;
333 height = height * 3 / 4;
334 } else {
335 // Impose a minimum size for sanity, and make sure the window fits on
336 // the display (in case the current display is smaller than the one
337 // in use when the window size was saved). (480x320) is about the
338 // smallest usable size for aven's window.
339 const int min_width = min(geom.width, 480);
340 const int min_height = min(geom.height, 320);
341 if (width < min_width || height < min_height) {
342 if (width < min_width) {
343 width = min_width;
345 if (height < min_height) {
346 height = min_height;
348 pos.x = geom.x + (geom.width - width) / 4;
349 pos.y = geom.y + (geom.height - height) / 4;
353 // Create the main window.
354 m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
356 // Select maximised if that's the saved state.
357 if (maximized) {
358 m_Frame->Maximize();
361 if (utf8_argv[optind]) {
362 if (!opt_survey) opt_survey = "";
363 m_Frame->OpenFile(fnm, wxString(opt_survey, wxConvUTF8));
366 if (print_and_exit) {
367 m_Frame->PrintAndExit();
368 return true;
371 m_Frame->Show(true);
372 #ifdef _WIN32
373 m_Frame->SetFocus();
374 #endif
375 return true;
378 wxPageSetupDialogData *
379 Aven::GetPageSetupDialogData()
381 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
382 #ifdef __WXGTK__
383 // Fetch paper margins stored on disk.
384 int left, right, top, bottom;
385 wxConfigBase * cfg = wxConfigBase::Get();
386 // These default margins were chosen by looking at all the .ppd files
387 // on my machine.
388 cfg->Read(wxT("paper_margin_left"), &left, 7);
389 cfg->Read(wxT("paper_margin_right"), &right, 7);
390 cfg->Read(wxT("paper_margin_top"), &top, 14);
391 cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
392 m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
393 m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
394 #endif
395 return m_pageSetupData;
398 void
399 Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
401 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
402 *m_pageSetupData = psdd;
403 #ifdef __WXGTK__
404 wxPoint topleft = psdd.GetMarginTopLeft();
405 wxPoint bottomright = psdd.GetMarginBottomRight();
407 // Store user specified paper margins on disk/in registry.
408 wxConfigBase * cfg = wxConfigBase::Get();
409 cfg->Write(wxT("paper_margin_left"), topleft.x);
410 cfg->Write(wxT("paper_margin_right"), bottomright.x);
411 cfg->Write(wxT("paper_margin_top"), topleft.y);
412 cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
413 cfg->Flush();
414 #endif
417 #ifdef __WXMAC__
418 void
419 Aven::MacOpenFiles(const wxArrayString & filenames)
421 if (filenames.size() != 1) {
422 ReportError(wxT("Aven can only load one file at a time"));
423 return;
425 m_Frame->OpenFile(filenames[0], wxString());
428 void
429 Aven::MacPrintFiles(const wxArrayString & filenames)
431 if (filenames.size() != 1) {
432 ReportError(wxT("Aven can only print one file at a time"));
433 return;
435 m_Frame->OpenFile(filenames[0], wxString());
436 m_Frame->PrintAndExit();
438 #endif
440 void Aven::ReportError(const wxString& msg)
442 if (!m_Frame) {
443 wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
444 return;
446 wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
447 dlg.ShowModal();
450 const wxString &
451 wmsg_cfgpth()
453 static wxString path;
454 if (path.empty())
455 path = wxString(msg_cfgpth(), wxConvUTF8);
456 return path;
459 // called to report errors by message.c
460 extern "C" void
461 aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
463 wxString m;
464 if (fnm) {
465 m = wxString(fnm, wxConvUTF8);
466 if (line) m += wxString::Format(wxT(":%d"), line);
467 m += wxT(": ");
470 if (severity == DIAG_WARN) {
471 m += wmsg(/*warning*/4);
472 m += wxT(": ");
475 char buf[1024];
476 vsnprintf(buf, sizeof(buf), msg(en), ap);
477 m += wxString(buf, wxConvUTF8);
478 if (wxTheApp == NULL) {
479 // We haven't initialised the Aven app object yet.
480 if (!wxInitialize()) {
481 fputs(buf, stderr);
482 PUTC('\n', stderr);
483 exit(1);
485 wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
486 wxUninitialize();
487 } else {
488 wxGetApp().ReportError(m);