Escape for CreateProcess(), not cmd.exe
[survex.git] / src / aven.cc
blob7e294d970e31c3de2f3f83536e11e73615f65b2c
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 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 "mainfrm.h"
32 #include "cmdline.h"
33 #include "message.h"
34 #include "useful.h"
36 #include <assert.h>
37 #include <stdio.h>
39 #include <wx/confbase.h>
40 #include <wx/image.h>
41 #if wxUSE_DISPLAY
42 // wxDisplay was added in wx 2.5; but it may not be built for mingw (because
43 // the header seems to be missing).
44 #include <wx/display.h>
45 #endif
47 #if defined __WXMAC__ || defined __WXMSW__
48 #include <proj_api.h>
49 #endif
51 #ifdef __WXMSW__
52 #include <windows.h>
53 #endif
55 bool double_buffered = false;
57 static const struct option long_opts[] = {
58 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
59 {"survey", required_argument, 0, 's'},
60 {"print", no_argument, 0, 'p'},
61 {"help", no_argument, 0, HLP_HELP},
62 {"version", no_argument, 0, HLP_VERSION},
63 {0, 0, 0, 0}
66 #define short_opts "s:p"
68 static struct help_msg help[] = {
69 /* <-- */
70 /* TRANSLATORS: --help output for --survey option.
72 * "this" has been added to English translation */
73 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
74 /* TRANSLATORS: --help output for aven --print option */
75 {HLP_ENCODELONG(1), /*print and exit (requires a 3d file)*/119, 0},
76 {0, 0, 0}
79 #ifdef __WXMSW__
80 IMPLEMENT_APP(Aven)
81 #else
82 IMPLEMENT_APP_NO_MAIN(Aven)
83 IMPLEMENT_WX_THEME_SUPPORT
84 #endif
86 Aven::Aven() :
87 m_Frame(NULL), m_pageSetupData(NULL)
89 wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
92 Aven::~Aven()
94 delete m_pageSetupData;
97 static int getopt_first_response = 0;
99 static char ** utf8_argv;
101 #ifdef __WXMSW__
102 bool Aven::Initialize(int& my_argc, wxChar **my_argv)
104 const wxChar * cmd_line = GetCommandLineW();
106 // Horrible bodge to handle therion's assumptions about the "Process"
107 // file association.
108 if (cmd_line) {
109 // None of these are valid aven command line options, so this is not
110 // going to be triggered accidentally.
111 wxChar * p = wxStrStr(cmd_line, "aven.exe\" --quiet --log --output=");
112 if (p) {
113 // Just change the command name in the command line string - that
114 // way the quoting should match what the C runtime expects.
115 wxString cmd(cmd_line, p - cmd_line);
116 cmd += "cavern";
117 cmd += p + 4;
118 exit(wxExecute(cmd, wxEXEC_SYNC));
122 int utf8_argc;
124 // wxWidgets doesn't split up the command line in the standard way, so
125 // redo it ourselves using the standard API function.
127 // Warning: The returned array from this has no terminating NULL
128 // element.
129 wxChar ** new_argv = NULL;
130 if (cmd_line)
131 new_argv = CommandLineToArgvW(cmd_line, &utf8_argc);
132 bool failed = (new_argv == NULL);
133 if (failed) {
134 wxChar * p;
135 FormatMessage(
136 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
137 NULL,
138 GetLastError(),
140 (LPWSTR)&p,
141 4096,
142 NULL);
143 wxString m = "CommandLineToArgvW failed: ";
144 m += p;
145 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
146 LocalFree(p);
147 utf8_argc = my_argc;
148 new_argv = my_argv;
151 // Convert wide characters to UTF-8.
152 utf8_argv = new char * [utf8_argc + 1];
153 for (int i = 0; i < utf8_argc; ++i){
154 utf8_argv[i] = strdup(wxString(new_argv[i]).mb_str());
156 utf8_argv[utf8_argc] = NULL;
158 if (!failed) LocalFree(new_argv);
161 msg_init(utf8_argv);
162 pj_set_finder(msg_proj_finder);
163 select_charset(CHARSET_UTF8);
164 /* Want --version and decent --help output, which cmdline does for us.
165 * wxCmdLine is much less good.
167 /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions
168 * - it should be translated to the terminology that cavers using the
169 * language would use.
171 * Part of aven --help */
172 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
173 cmdline_init(utf8_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1);
174 getopt_first_response = cmdline_getopt();
176 // The argc and argv arguments don't actually get used here.
177 int dummy_argc = 0;
178 return wxApp::Initialize(dummy_argc, NULL);
180 #else
181 int main(int argc, char **argv)
183 #ifdef __WXMAC__
184 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
185 // wx ignores for us, but in wxApp::Initialize() which hasn't been
186 // called yet. So we need to remove it ourselves.
187 if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
188 --argc;
189 memmove(argv + 1, argv + 2, argc * sizeof(char *));
191 #endif
192 // Call msg_init() and start processing the command line first so that
193 // we can respond to --help and --version even without an X display.
194 msg_init(argv);
195 #ifdef __WXMAC__
196 pj_set_finder(msg_proj_finder);
197 #endif
198 select_charset(CHARSET_UTF8);
199 /* Want --version and decent --help output, which cmdline does for us.
200 * wxCmdLine is much less good.
202 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
203 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
204 getopt_first_response = cmdline_getopt();
206 utf8_argv = argv;
208 #if wxUSE_UNICODE
209 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
210 wxChar * wargv[2];
211 if (buf) {
212 wargv[0] = wxStrdup(buf);
213 } else {
214 // Eep - couldn't convert the executable's name to wide characters!
215 wargv[0] = wxStrdup(APP_NAME);
217 wargv[1] = NULL;
218 int wargc = 1;
219 return wxEntry(wargc, wargv);
220 #else
221 char *dummy_argv[2] = { argv[0], NULL };
222 int dummy_argc = 1;
223 return wxEntry(dummy_argc, dummy_argv);
224 #endif
226 #endif
228 bool Aven::OnInit()
230 wxLog::SetActiveTarget(new MyLogWindow());
233 // Suppress message box warnings about messages not found.
234 wxLogNull logNo;
235 wxLocale *loc = new wxLocale();
236 loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
237 wxString msg_lang_str(msg_lang, wxConvUTF8);
238 const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
239 wxString lang_str(lang, wxConvUTF8);
240 #if wxCHECK_VERSION(2,9,0)
241 loc->Init(msg_lang_str, lang_str, msg_lang_str);
242 #else
243 loc->Init(msg_lang_str, lang_str, msg_lang_str, true, true);
244 #endif
245 // The existence of the wxLocale object is enough - no need to keep a
246 // pointer to it!
249 wxString survey;
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 survey = wxString(optarg, wxConvUTF8);
264 if (opt == 'p') {
265 print_and_exit = true;
269 if (print_and_exit && !utf8_argv[optind]) {
270 cmdline_syntax(); // FIXME : not a helpful error...
271 exit(1);
274 wxString fnm;
275 if (utf8_argv[optind]) {
276 fnm = wxString(utf8_argv[optind], wxConvUTF8);
277 if (fnm.empty() && *(utf8_argv[optind])) {
278 ReportError(wxT("File argument's filename has bad encoding"));
279 return false;
283 // Use a double-buffered visual if available, as it will give much smoother
284 // animation.
285 double_buffered = true;
286 const int wx_gl_attribs[] = {
287 WX_GL_DOUBLEBUFFER,
288 WX_GL_RGBA,
291 if (!InitGLVisual(wx_gl_attribs)) {
292 if (!InitGLVisual(wx_gl_attribs + 1)) {
293 wxString m;
294 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
295 * perhaps by "Survex" or other things in future). */
296 m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
297 wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
298 exit(1);
300 double_buffered = false;
303 wxImage::AddHandler(new wxPNGHandler);
305 // Obtain the screen geometry.
306 #if wxUSE_DISPLAY
307 wxRect geom = wxDisplay().GetGeometry();
308 #else
309 wxRect geom;
310 wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height);
311 #endif
313 wxPoint pos(wxDefaultPosition);
314 int width, height;
315 wxConfigBase::Get()->Read(wxT("width"), &width, 0);
316 if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
317 // We used to persist full screen mode (-1 was maximized,
318 // -2 full screen), but people would get stuck in full
319 // screen mode, unsure how to exit.
320 bool maximized = (width <= -1);
321 if (width <= 0 || height <= 0) {
322 pos.x = geom.x;
323 pos.y = geom.y;
324 width = geom.width;
325 height = geom.height;
327 // Calculate a reasonable size for our window.
328 pos.x += width / 8;
329 pos.y += height / 8;
330 width = width * 3 / 4;
331 height = height * 3 / 4;
332 } else {
333 // Impose a minimum size for sanity, and make sure the window fits on
334 // the display (in case the current display is smaller than the one
335 // in use when the window size was saved). (480x320) is about the
336 // smallest usable size for aven's window.
337 const int min_width = min(geom.width, 480);
338 const int min_height = min(geom.height, 320);
339 if (width < min_width || height < min_height) {
340 if (width < min_width) {
341 width = min_width;
343 if (height < min_height) {
344 height = min_height;
346 pos.x = geom.x + (geom.width - width) / 4;
347 pos.y = geom.y + (geom.height - height) / 4;
351 // Create the main window.
352 m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
354 // Select maximised if that's the saved state.
355 if (maximized) {
356 m_Frame->Maximize();
359 if (utf8_argv[optind]) {
360 m_Frame->OpenFile(fnm, survey);
363 if (print_and_exit) {
364 m_Frame->PrintAndExit();
365 return true;
368 m_Frame->Show(true);
369 #ifdef _WIN32
370 m_Frame->SetFocus();
371 #endif
372 return true;
375 wxPageSetupDialogData *
376 Aven::GetPageSetupDialogData()
378 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
379 #ifdef __WXGTK__
380 // Fetch paper margins stored on disk.
381 int left, right, top, bottom;
382 wxConfigBase * cfg = wxConfigBase::Get();
383 // These default margins were chosen by looking at all the .ppd files
384 // on my machine.
385 cfg->Read(wxT("paper_margin_left"), &left, 7);
386 cfg->Read(wxT("paper_margin_right"), &right, 7);
387 cfg->Read(wxT("paper_margin_top"), &top, 14);
388 cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
389 m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
390 m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
391 #endif
392 return m_pageSetupData;
395 void
396 Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
398 if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
399 *m_pageSetupData = psdd;
400 #ifdef __WXGTK__
401 wxPoint topleft = psdd.GetMarginTopLeft();
402 wxPoint bottomright = psdd.GetMarginBottomRight();
404 // Store user specified paper margins on disk/in registry.
405 wxConfigBase * cfg = wxConfigBase::Get();
406 cfg->Write(wxT("paper_margin_left"), topleft.x);
407 cfg->Write(wxT("paper_margin_right"), bottomright.x);
408 cfg->Write(wxT("paper_margin_top"), topleft.y);
409 cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
410 cfg->Flush();
411 #endif
414 #ifdef __WXMAC__
415 void
416 Aven::MacOpenFiles(const wxArrayString & filenames)
418 if (filenames.size() != 1) {
419 ReportError(wxT("Aven can only load one file at a time"));
420 return;
422 m_Frame->OpenFile(filenames[0], wxString());
425 void
426 Aven::MacPrintFiles(const wxArrayString & filenames)
428 if (filenames.size() != 1) {
429 ReportError(wxT("Aven can only print one file at a time"));
430 return;
432 m_Frame->OpenFile(filenames[0], wxString());
433 m_Frame->PrintAndExit();
435 #endif
437 void Aven::ReportError(const wxString& msg)
439 if (!m_Frame) {
440 wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
441 return;
443 AvenAllowOnTop ontop(m_Frame);
444 wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
445 dlg.ShowModal();
448 wxString
449 wmsg(int msg_no)
451 return wxString::FromUTF8(msg(msg_no));
454 const wxString &
455 wmsg_cfgpth()
457 static wxString path;
458 if (path.empty())
459 path = wxString(msg_cfgpth(), wxConvUTF8);
460 return path;
463 // called to report errors by message.c
464 extern "C" void
465 aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
467 wxString m;
468 if (fnm) {
469 m = wxString(fnm, wxConvUTF8);
470 if (line) m += wxString::Format(wxT(":%d"), line);
471 m += wxT(": ");
474 if (severity == 0) {
475 m += wmsg(/*warning*/4);
476 m += wxT(": ");
479 char buf[1024];
480 vsnprintf(buf, sizeof(buf), msg(en), ap);
481 m += wxString(buf, wxConvUTF8);
482 if (wxTheApp == NULL) {
483 // We haven't initialised the Aven app object yet.
484 if (!wxInitialize()) {
485 fputs(buf, stderr);
486 PUTC('\n', stderr);
487 exit(1);
489 wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
490 wxUninitialize();
491 } else {
492 wxGetApp().ReportError(m);