[ci] GHA fixes
[survex.git] / src / aven.cc
blobbfa02b9f7103ab0f5fae5e3a3ef2b610f18216e4
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 #include "aven.h"
29 #include "log.h"
30 #include "gla.h"
31 #include "mainfrm.h"
33 #include "cmdline.h"
34 #include "message.h"
35 #include "useful.h"
37 #include <assert.h>
38 #include <stdio.h>
40 #include <wx/confbase.h>
41 #include <wx/image.h>
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).
45 #include <wx/display.h>
46 #endif
48 #ifdef __WXMSW__
49 #include <windows.h>
50 #endif
52 static const struct option long_opts[] = {
53 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
54 {"survey", required_argument, 0, 's'},
55 {"print", no_argument, 0, 'p'},
56 {"help", no_argument, 0, HLP_HELP},
57 {"version", no_argument, 0, HLP_VERSION},
58 {0, 0, 0, 0}
61 #define short_opts "s:p"
63 static struct help_msg help[] = {
64 /* <-- */
65 /* TRANSLATORS: --help output for --survey option.
67 * "this" has been added to English translation */
68 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
69 /* TRANSLATORS: --help output for aven --print option */
70 {HLP_ENCODELONG(1), /*print and exit (requires a 3d file)*/119, 0},
71 {0, 0, 0}
74 #ifdef __WXMSW__
75 IMPLEMENT_APP(Aven)
76 #else
77 IMPLEMENT_APP_NO_MAIN(Aven)
78 IMPLEMENT_WX_THEME_SUPPORT
79 #endif
81 Aven::Aven() :
82 m_Frame(NULL), m_pageSetupData(NULL)
84 wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
87 Aven::~Aven()
89 delete m_pageSetupData;
92 static int getopt_first_response = 0;
94 static char ** utf8_argv;
96 #ifdef __WXMSW__
97 bool Aven::Initialize(int& my_argc, wxChar **my_argv)
99 const wxChar * cmd_line = GetCommandLineW();
101 // Horrible bodge to handle therion's assumptions about the "Process"
102 // file association.
103 if (cmd_line) {
104 // None of these are valid aven command line options, so this is not
105 // going to be triggered accidentally.
106 const wxChar * p = wxStrstr(cmd_line,
107 wxT("aven.exe\" --quiet --log --output="));
108 if (p) {
109 // Just change the command name in the command line string - that
110 // way the quoting should match what the C runtime expects.
111 wxString cmd(cmd_line, p - cmd_line);
112 cmd += "cavern";
113 cmd += p + 4;
114 exit(wxExecute(cmd, wxEXEC_SYNC));
118 int utf8_argc;
120 // wxWidgets doesn't split up the command line in the standard way, so
121 // redo it ourselves using the standard API function.
123 // Warning: The returned array from this has no terminating NULL
124 // element.
125 wxChar ** new_argv = NULL;
126 if (cmd_line)
127 new_argv = CommandLineToArgvW(cmd_line, &utf8_argc);
128 bool failed = (new_argv == NULL);
129 if (failed) {
130 wxChar * p;
131 FormatMessage(
132 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
133 NULL,
134 GetLastError(),
136 (LPWSTR)&p,
137 4096,
138 NULL);
139 wxString m = "CommandLineToArgvW failed: ";
140 m += p;
141 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
142 LocalFree(p);
143 utf8_argc = my_argc;
144 new_argv = my_argv;
147 // Convert wide characters to UTF-8.
148 utf8_argv = new char * [utf8_argc + 1];
149 for (int i = 0; i < utf8_argc; ++i){
150 utf8_argv[i] = strdup(wxString(new_argv[i]).utf8_str());
152 utf8_argv[utf8_argc] = NULL;
154 if (!failed) LocalFree(new_argv);
157 msg_init(utf8_argv);
158 select_charset(CHARSET_UTF8);
159 /* Want --version and decent --help output, which cmdline does for us.
160 * wxCmdLine is much less good.
162 /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions
163 * - it should be translated to the terminology that cavers using the
164 * language would use.
166 * Part of aven --help */
167 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
168 cmdline_init(utf8_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1);
169 getopt_first_response = cmdline_getopt();
171 // The argc and argv arguments don't actually get used here.
172 int dummy_argc = 0;
173 return wxApp::Initialize(dummy_argc, NULL);
175 #else
176 int main(int argc, char **argv)
178 #ifdef __WXGTK3__
179 // Currently wxGLCanvas doesn't work under Wayland, and the code segfaults.
180 // https://trac.wxwidgets.org/ticket/17702
181 // Setting GDK_BACKEND=x11 is the recommended workaround, and it seems to
182 // work to set it here. GTK2 doesn't support Wayland, so doesn't need
183 // this.
184 setenv("GDK_BACKEND", "x11", 1);
185 // FIXME: The OpenGL code needs work before scaling on hidpi displays will
186 // work usefully, so for now disable such scaling (which simulates how
187 // things are when using GTK2).
188 setenv("GDK_SCALE", "1", 1);
189 #endif
190 #ifdef __WXMAC__
191 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
192 // wx ignores for us, but in wxApp::Initialize() which hasn't been
193 // called yet. So we need to remove it ourselves.
194 if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
195 --argc;
196 memmove(argv + 1, argv + 2, argc * sizeof(char *));
198 #endif
199 // Call msg_init() and start processing the command line first so that
200 // we can respond to --help and --version even without an X display.
201 msg_init(argv);
202 select_charset(CHARSET_UTF8);
203 /* Want --version and decent --help output, which cmdline does for us.
204 * wxCmdLine is much less good.
206 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
207 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
208 getopt_first_response = cmdline_getopt();
210 utf8_argv = argv;
212 #if wxUSE_UNICODE
213 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
214 wxChar * wargv[2];
215 if (buf) {
216 wargv[0] = wxStrdup(buf);
217 } else {
218 // Eep - couldn't convert the executable's name to wide characters!
219 wargv[0] = wxStrdup(APP_NAME);
221 wargv[1] = NULL;
222 int wargc = 1;
223 return wxEntry(wargc, wargv);
224 #else
225 char *dummy_argv[2] = { argv[0], NULL };
226 int dummy_argc = 1;
227 return wxEntry(dummy_argc, dummy_argv);
228 #endif
230 #endif
232 bool Aven::OnInit()
234 wxLog::SetActiveTarget(new MyLogWindow());
237 // Suppress message box warnings about messages not found.
238 wxLogNull logNo;
239 wxLocale *loc = new wxLocale();
240 loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
241 wxString msg_lang_str(msg_lang, wxConvUTF8);
242 const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
243 wxString lang_str(lang, wxConvUTF8);
244 loc->Init(msg_lang_str, lang_str, msg_lang_str);
245 // The existence of the wxLocale object is enough - no need to keep a
246 // pointer to it!
249 const char* opt_survey = NULL;
250 bool print_and_exit = false;
252 while (true) {
253 int opt;
254 if (getopt_first_response) {
255 opt = getopt_first_response;
256 getopt_first_response = 0;
257 } else {
258 opt = cmdline_getopt();
260 if (opt == EOF) break;
261 if (opt == 's') {
262 if (opt_survey != NULL) {
263 // FIXME: Not a helpful error, but this is temporary until
264 // we actually hook up support for specifying multiple
265 // --survey options properly here.
266 cmdline_syntax();
267 exit(1);
269 opt_survey = optarg;
271 if (opt == 'p') {
272 print_and_exit = true;
276 if (print_and_exit && !utf8_argv[optind]) {
277 cmdline_syntax(); // FIXME : not a helpful error...
278 exit(1);
281 wxString fnm;
282 if (utf8_argv[optind]) {
283 fnm = wxString(utf8_argv[optind], wxConvUTF8);
284 if (fnm.empty() && *(utf8_argv[optind])) {
285 ReportError(wxT("File argument's filename has bad encoding"));
286 return false;
290 if (!GLACanvas::check_visual()) {
291 wxString m;
292 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
293 * perhaps by "Survex" or other things in future). */
294 m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
295 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
296 exit(1);
299 wxImage::AddHandler(new wxPNGHandler);
301 // Obtain the screen geometry.
302 #if wxUSE_DISPLAY
303 wxRect geom = wxDisplay().GetGeometry();
304 #else
305 wxRect geom;
306 wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height);
307 #endif
309 wxPoint pos(wxDefaultPosition);
310 int width, height;
311 wxConfigBase::Get()->Read(wxT("width"), &width, 0);
312 if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
313 // We used to persist full screen mode (-1 was maximized,
314 // -2 full screen), but people would get stuck in full
315 // screen mode, unsure how to exit.
316 bool maximized = (width <= -1);
317 if (width <= 0 || height <= 0) {
318 pos.x = geom.x;
319 pos.y = geom.y;
320 width = geom.width;
321 height = geom.height;
323 // Calculate a reasonable size for our window.
324 pos.x += width / 8;
325 pos.y += height / 8;
326 width = width * 3 / 4;
327 height = height * 3 / 4;
328 } else {
329 // Impose a minimum size for sanity, and make sure the window fits on
330 // the display (in case the current display is smaller than the one
331 // in use when the window size was saved). (480x320) is about the
332 // smallest usable size for aven's window.
333 const int min_width = min(geom.width, 480);
334 const int min_height = min(geom.height, 320);
335 if (width < min_width || height < min_height) {
336 if (width < min_width) {
337 width = min_width;
339 if (height < min_height) {
340 height = min_height;
342 pos.x = geom.x + (geom.width - width) / 4;
343 pos.y = geom.y + (geom.height - height) / 4;
347 // Create the main window.
348 m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
350 // Select maximised if that's the saved state.
351 if (maximized) {
352 m_Frame->Maximize();
355 if (utf8_argv[optind]) {
356 if (!opt_survey) opt_survey = "";
357 m_Frame->OpenFile(fnm, wxString(opt_survey, wxConvUTF8));
360 if (print_and_exit) {
361 m_Frame->PrintAndExit();
362 return true;
365 m_Frame->Show(true);
366 #ifdef _WIN32
367 m_Frame->SetFocus();
368 #endif
369 return true;
372 wxPageSetupDialogData *
373 Aven::GetPageSetupDialogData()
375 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
376 #ifdef __WXGTK__
377 // Fetch paper margins stored on disk.
378 int left, right, top, bottom;
379 wxConfigBase * cfg = wxConfigBase::Get();
380 // These default margins were chosen by looking at all the .ppd files
381 // on my machine.
382 cfg->Read(wxT("paper_margin_left"), &left, 7);
383 cfg->Read(wxT("paper_margin_right"), &right, 7);
384 cfg->Read(wxT("paper_margin_top"), &top, 14);
385 cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
386 m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
387 m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
388 #endif
389 return m_pageSetupData;
392 void
393 Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
395 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
396 *m_pageSetupData = psdd;
397 #ifdef __WXGTK__
398 wxPoint topleft = psdd.GetMarginTopLeft();
399 wxPoint bottomright = psdd.GetMarginBottomRight();
401 // Store user specified paper margins on disk/in registry.
402 wxConfigBase * cfg = wxConfigBase::Get();
403 cfg->Write(wxT("paper_margin_left"), topleft.x);
404 cfg->Write(wxT("paper_margin_right"), bottomright.x);
405 cfg->Write(wxT("paper_margin_top"), topleft.y);
406 cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
407 cfg->Flush();
408 #endif
411 #ifdef __WXMAC__
412 void
413 Aven::MacOpenFiles(const wxArrayString & filenames)
415 if (filenames.size() != 1) {
416 ReportError(wxT("Aven can only load one file at a time"));
417 return;
419 m_Frame->OpenFile(filenames[0], wxString());
422 void
423 Aven::MacPrintFiles(const wxArrayString & filenames)
425 if (filenames.size() != 1) {
426 ReportError(wxT("Aven can only print one file at a time"));
427 return;
429 m_Frame->OpenFile(filenames[0], wxString());
430 m_Frame->PrintAndExit();
432 #endif
434 void Aven::ReportError(const wxString& msg)
436 if (!m_Frame) {
437 wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
438 return;
440 wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
441 dlg.ShowModal();
444 const wxString &
445 wmsg_cfgpth()
447 static wxString path;
448 if (path.empty())
449 path = wxString(msg_cfgpth(), wxConvUTF8);
450 return path;
453 // called to report errors by message.c
454 extern "C" void
455 aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
457 wxString m;
458 if (fnm) {
459 m = wxString(fnm, wxConvUTF8);
460 if (line) m += wxString::Format(wxT(":%d"), line);
461 m += wxT(": ");
464 if (severity == 0) {
465 m += wmsg(/*warning*/4);
466 m += wxT(": ");
469 char buf[1024];
470 vsnprintf(buf, sizeof(buf), msg(en), ap);
471 m += wxString(buf, wxConvUTF8);
472 if (wxTheApp == NULL) {
473 // We haven't initialised the Aven app object yet.
474 if (!wxInitialize()) {
475 fputs(buf, stderr);
476 PUTC('\n', stderr);
477 exit(1);
479 wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
480 wxUninitialize();
481 } else {
482 wxGetApp().ReportError(m);