Fix coding style
[survex.git] / src / aven.cc
blob4a828bb5ca01dd19c75f00fc8437028d5b0c59c2
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 // Currently wxGLCanvas doesn't work under Wayland, and the code segfaults.
182 // https://trac.wxwidgets.org/ticket/17702
183 // Setting GDK_BACKEND=x11 is the recommended workaround, and it seems to
184 // work to set it here.
185 setenv("GDK_BACKEND", "x11", 1);
186 #endif
187 #ifdef __WXMAC__
188 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
189 // wx ignores for us, but in wxApp::Initialize() which hasn't been
190 // called yet. So we need to remove it ourselves.
191 if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
192 --argc;
193 memmove(argv + 1, argv + 2, argc * sizeof(char *));
195 #endif
196 // Call msg_init() and start processing the command line first so that
197 // we can respond to --help and --version even without an X display.
198 msg_init(argv);
199 select_charset(CHARSET_UTF8);
200 /* Want --version and decent --help output, which cmdline does for us.
201 * wxCmdLine is much less good.
203 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
204 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
205 getopt_first_response = cmdline_getopt();
207 utf8_argv = argv;
209 #if wxUSE_UNICODE
210 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
211 wxChar * wargv[2];
212 if (buf) {
213 wargv[0] = wxStrdup(buf);
214 } else {
215 // Eep - couldn't convert the executable's name to wide characters!
216 wargv[0] = wxStrdup(APP_NAME);
218 wargv[1] = NULL;
219 int wargc = 1;
220 return wxEntry(wargc, wargv);
221 #else
222 char *dummy_argv[2] = { argv[0], NULL };
223 int dummy_argc = 1;
224 return wxEntry(dummy_argc, dummy_argv);
225 #endif
227 #endif
229 bool Aven::OnInit()
231 wxLog::SetActiveTarget(new MyLogWindow());
234 // Suppress message box warnings about messages not found.
235 wxLogNull logNo;
236 wxLocale *loc = new wxLocale();
237 loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
238 wxString msg_lang_str(msg_lang, wxConvUTF8);
239 const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
240 wxString lang_str(lang, wxConvUTF8);
241 #if wxCHECK_VERSION(2,9,0)
242 loc->Init(msg_lang_str, lang_str, msg_lang_str);
243 #else
244 loc->Init(msg_lang_str, lang_str, msg_lang_str, true, true);
245 #endif
246 // The existence of the wxLocale object is enough - no need to keep a
247 // pointer to it!
250 const char* opt_survey = NULL;
251 bool print_and_exit = false;
253 while (true) {
254 int opt;
255 if (getopt_first_response) {
256 opt = getopt_first_response;
257 getopt_first_response = 0;
258 } else {
259 opt = cmdline_getopt();
261 if (opt == EOF) break;
262 if (opt == 's') {
263 if (opt_survey != NULL) {
264 // FIXME: Not a helpful error, but this is temporary until
265 // we actually hook up support for specifying multiple
266 // --survey options properly here.
267 cmdline_syntax();
268 exit(1);
270 opt_survey = optarg;
272 if (opt == 'p') {
273 print_and_exit = true;
277 if (print_and_exit && !utf8_argv[optind]) {
278 cmdline_syntax(); // FIXME : not a helpful error...
279 exit(1);
282 wxString fnm;
283 if (utf8_argv[optind]) {
284 fnm = wxString(utf8_argv[optind], wxConvUTF8);
285 if (fnm.empty() && *(utf8_argv[optind])) {
286 ReportError(wxT("File argument's filename has bad encoding"));
287 return false;
291 if (!GLACanvas::check_visual()) {
292 wxString m;
293 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
294 * perhaps by "Survex" or other things in future). */
295 m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
296 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
297 exit(1);
300 wxImage::AddHandler(new wxPNGHandler);
302 // Obtain the screen geometry.
303 #if wxUSE_DISPLAY
304 wxRect geom = wxDisplay().GetGeometry();
305 #else
306 wxRect geom;
307 wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height);
308 #endif
310 wxPoint pos(wxDefaultPosition);
311 int width, height;
312 wxConfigBase::Get()->Read(wxT("width"), &width, 0);
313 if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
314 // We used to persist full screen mode (-1 was maximized,
315 // -2 full screen), but people would get stuck in full
316 // screen mode, unsure how to exit.
317 bool maximized = (width <= -1);
318 if (width <= 0 || height <= 0) {
319 pos.x = geom.x;
320 pos.y = geom.y;
321 width = geom.width;
322 height = geom.height;
324 // Calculate a reasonable size for our window.
325 pos.x += width / 8;
326 pos.y += height / 8;
327 width = width * 3 / 4;
328 height = height * 3 / 4;
329 } else {
330 // Impose a minimum size for sanity, and make sure the window fits on
331 // the display (in case the current display is smaller than the one
332 // in use when the window size was saved). (480x320) is about the
333 // smallest usable size for aven's window.
334 const int min_width = min(geom.width, 480);
335 const int min_height = min(geom.height, 320);
336 if (width < min_width || height < min_height) {
337 if (width < min_width) {
338 width = min_width;
340 if (height < min_height) {
341 height = min_height;
343 pos.x = geom.x + (geom.width - width) / 4;
344 pos.y = geom.y + (geom.height - height) / 4;
348 // Create the main window.
349 m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
351 // Select maximised if that's the saved state.
352 if (maximized) {
353 m_Frame->Maximize();
356 if (utf8_argv[optind]) {
357 if (!opt_survey) opt_survey = "";
358 m_Frame->OpenFile(fnm, wxString(opt_survey, wxConvUTF8));
361 if (print_and_exit) {
362 m_Frame->PrintAndExit();
363 return true;
366 m_Frame->Show(true);
367 #ifdef _WIN32
368 m_Frame->SetFocus();
369 #endif
370 return true;
373 wxPageSetupDialogData *
374 Aven::GetPageSetupDialogData()
376 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
377 #ifdef __WXGTK__
378 // Fetch paper margins stored on disk.
379 int left, right, top, bottom;
380 wxConfigBase * cfg = wxConfigBase::Get();
381 // These default margins were chosen by looking at all the .ppd files
382 // on my machine.
383 cfg->Read(wxT("paper_margin_left"), &left, 7);
384 cfg->Read(wxT("paper_margin_right"), &right, 7);
385 cfg->Read(wxT("paper_margin_top"), &top, 14);
386 cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
387 m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
388 m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
389 #endif
390 return m_pageSetupData;
393 void
394 Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
396 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
397 *m_pageSetupData = psdd;
398 #ifdef __WXGTK__
399 wxPoint topleft = psdd.GetMarginTopLeft();
400 wxPoint bottomright = psdd.GetMarginBottomRight();
402 // Store user specified paper margins on disk/in registry.
403 wxConfigBase * cfg = wxConfigBase::Get();
404 cfg->Write(wxT("paper_margin_left"), topleft.x);
405 cfg->Write(wxT("paper_margin_right"), bottomright.x);
406 cfg->Write(wxT("paper_margin_top"), topleft.y);
407 cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
408 cfg->Flush();
409 #endif
412 #ifdef __WXMAC__
413 void
414 Aven::MacOpenFiles(const wxArrayString & filenames)
416 if (filenames.size() != 1) {
417 ReportError(wxT("Aven can only load one file at a time"));
418 return;
420 m_Frame->OpenFile(filenames[0], wxString());
423 void
424 Aven::MacPrintFiles(const wxArrayString & filenames)
426 if (filenames.size() != 1) {
427 ReportError(wxT("Aven can only print one file at a time"));
428 return;
430 m_Frame->OpenFile(filenames[0], wxString());
431 m_Frame->PrintAndExit();
433 #endif
435 void Aven::ReportError(const wxString& msg)
437 if (!m_Frame) {
438 wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
439 return;
441 AvenAllowOnTop ontop(m_Frame);
442 wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
443 dlg.ShowModal();
446 const wxString &
447 wmsg_cfgpth()
449 static wxString path;
450 if (path.empty())
451 path = wxString(msg_cfgpth(), wxConvUTF8);
452 return path;
455 // called to report errors by message.c
456 extern "C" void
457 aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
459 wxString m;
460 if (fnm) {
461 m = wxString(fnm, wxConvUTF8);
462 if (line) m += wxString::Format(wxT(":%d"), line);
463 m += wxT(": ");
466 if (severity == 0) {
467 m += wmsg(/*warning*/4);
468 m += wxT(": ");
471 char buf[1024];
472 vsnprintf(buf, sizeof(buf), msg(en), ap);
473 m += wxString(buf, wxConvUTF8);
474 if (wxTheApp == NULL) {
475 // We haven't initialised the Aven app object yet.
476 if (!wxInitialize()) {
477 fputs(buf, stderr);
478 PUTC('\n', stderr);
479 exit(1);
481 wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
482 wxUninitialize();
483 } else {
484 wxGetApp().ReportError(m);