4 // Main class for Aven.
6 // Copyright (C) 2001 Mark R. Shinwell.
7 // Copyright (C) 2002,2003,2004,2005,2006,2011,2013,2014,2015,2016,2017 Olly Betts
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
28 #define MSG_SETUP_PROJ_SERACH_PATH 1
42 #include <wx/confbase.h>
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>
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
},
63 #define short_opts "s:p"
65 static struct help_msg help
[] = {
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},
79 IMPLEMENT_APP_NO_MAIN(Aven
)
80 IMPLEMENT_WX_THEME_SUPPORT
84 m_Frame(NULL
), m_pageSetupData(NULL
)
86 wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8
);
91 delete m_pageSetupData
;
94 static int getopt_first_response
= 0;
96 static char ** utf8_argv
;
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"
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="));
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
);
116 exit(wxExecute(cmd
, wxEXEC_SYNC
));
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
127 wxChar
** new_argv
= NULL
;
129 new_argv
= CommandLineToArgvW(cmd_line
, &utf8_argc
);
130 bool failed
= (new_argv
== NULL
);
134 FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
141 wxString m
= "CommandLineToArgvW failed: ";
143 wxMessageBox(m
, APP_NAME
, wxOK
| wxCENTRE
| wxICON_EXCLAMATION
);
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
);
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.
175 return wxApp::Initialize(dummy_argc
, NULL
);
178 int main(int argc
, char **argv
)
181 // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
182 // wx ignores for us, but in wxApp::Initialize() which hasn't been
183 // called yet. So we need to remove it ourselves.
184 if (argc
> 1 && strncmp(argv
[1], "-psn_", 5) == 0) {
186 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
189 // Call msg_init() and start processing the command line first so that
190 // we can respond to --help and --version even without an X display.
192 select_charset(CHARSET_UTF8
);
193 /* Want --version and decent --help output, which cmdline does for us.
194 * wxCmdLine is much less good.
196 cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL
);
197 cmdline_init(argc
, argv
, short_opts
, long_opts
, NULL
, help
, 0, 1);
198 getopt_first_response
= cmdline_getopt();
203 wxWCharBuffer
buf(wxConvFileName
->cMB2WX(argv
[0]));
206 wargv
[0] = wxStrdup(buf
);
208 // Eep - couldn't convert the executable's name to wide characters!
209 wargv
[0] = wxStrdup(APP_NAME
);
213 return wxEntry(wargc
, wargv
);
215 char *dummy_argv
[2] = { argv
[0], NULL
};
217 return wxEntry(dummy_argc
, dummy_argv
);
224 wxLog::SetActiveTarget(new MyLogWindow());
227 // Suppress message box warnings about messages not found.
229 wxLocale
*loc
= new wxLocale();
230 loc
->AddCatalogLookupPathPrefix(wmsg_cfgpth());
231 wxString
msg_lang_str(msg_lang
, wxConvUTF8
);
232 const char *lang
= msg_lang2
? msg_lang2
: msg_lang
;
233 wxString
lang_str(lang
, wxConvUTF8
);
234 #if wxCHECK_VERSION(2,9,0)
235 loc
->Init(msg_lang_str
, lang_str
, msg_lang_str
);
237 loc
->Init(msg_lang_str
, lang_str
, msg_lang_str
, true, true);
239 // The existence of the wxLocale object is enough - no need to keep a
244 bool print_and_exit
= false;
248 if (getopt_first_response
) {
249 opt
= getopt_first_response
;
250 getopt_first_response
= 0;
252 opt
= cmdline_getopt();
254 if (opt
== EOF
) break;
256 survey
= wxString(optarg
, wxConvUTF8
);
259 print_and_exit
= true;
263 if (print_and_exit
&& !utf8_argv
[optind
]) {
264 cmdline_syntax(); // FIXME : not a helpful error...
269 if (utf8_argv
[optind
]) {
270 fnm
= wxString(utf8_argv
[optind
], wxConvUTF8
);
271 if (fnm
.empty() && *(utf8_argv
[optind
])) {
272 ReportError(wxT("File argument's filename has bad encoding"));
277 if (!GLACanvas::check_visual()) {
279 /* TRANSLATORS: %s will be replaced with "Aven" currently (and
280 * perhaps by "Survex" or other things in future). */
281 m
.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME
);
282 wxMessageBox(m
, APP_NAME
, wxOK
| wxCENTRE
| wxICON_EXCLAMATION
);
286 wxImage::AddHandler(new wxPNGHandler
);
288 // Obtain the screen geometry.
290 wxRect geom
= wxDisplay().GetGeometry();
293 wxClientDisplayRect(&geom
.x
, &geom
.y
, &geom
.width
, &geom
.height
);
296 wxPoint
pos(wxDefaultPosition
);
298 wxConfigBase::Get()->Read(wxT("width"), &width
, 0);
299 if (width
> 0) wxConfigBase::Get()->Read(wxT("height"), &height
, 0);
300 // We used to persist full screen mode (-1 was maximized,
301 // -2 full screen), but people would get stuck in full
302 // screen mode, unsure how to exit.
303 bool maximized
= (width
<= -1);
304 if (width
<= 0 || height
<= 0) {
308 height
= geom
.height
;
310 // Calculate a reasonable size for our window.
313 width
= width
* 3 / 4;
314 height
= height
* 3 / 4;
316 // Impose a minimum size for sanity, and make sure the window fits on
317 // the display (in case the current display is smaller than the one
318 // in use when the window size was saved). (480x320) is about the
319 // smallest usable size for aven's window.
320 const int min_width
= min(geom
.width
, 480);
321 const int min_height
= min(geom
.height
, 320);
322 if (width
< min_width
|| height
< min_height
) {
323 if (width
< min_width
) {
326 if (height
< min_height
) {
329 pos
.x
= geom
.x
+ (geom
.width
- width
) / 4;
330 pos
.y
= geom
.y
+ (geom
.height
- height
) / 4;
334 // Create the main window.
335 m_Frame
= new MainFrm(APP_NAME
, pos
, wxSize(width
, height
));
337 // Select maximised if that's the saved state.
342 if (utf8_argv
[optind
]) {
343 m_Frame
->OpenFile(fnm
, survey
);
346 if (print_and_exit
) {
347 m_Frame
->PrintAndExit();
358 wxPageSetupDialogData
*
359 Aven::GetPageSetupDialogData()
361 if (!m_pageSetupData
) m_pageSetupData
= new wxPageSetupDialogData
;
363 // Fetch paper margins stored on disk.
364 int left
, right
, top
, bottom
;
365 wxConfigBase
* cfg
= wxConfigBase::Get();
366 // These default margins were chosen by looking at all the .ppd files
368 cfg
->Read(wxT("paper_margin_left"), &left
, 7);
369 cfg
->Read(wxT("paper_margin_right"), &right
, 7);
370 cfg
->Read(wxT("paper_margin_top"), &top
, 14);
371 cfg
->Read(wxT("paper_margin_bottom"), &bottom
, 14);
372 m_pageSetupData
->SetMarginTopLeft(wxPoint(left
, top
));
373 m_pageSetupData
->SetMarginBottomRight(wxPoint(right
, bottom
));
375 return m_pageSetupData
;
379 Aven::SetPageSetupDialogData(const wxPageSetupDialogData
& psdd
)
381 if (!m_pageSetupData
) m_pageSetupData
= new wxPageSetupDialogData
;
382 *m_pageSetupData
= psdd
;
384 wxPoint topleft
= psdd
.GetMarginTopLeft();
385 wxPoint bottomright
= psdd
.GetMarginBottomRight();
387 // Store user specified paper margins on disk/in registry.
388 wxConfigBase
* cfg
= wxConfigBase::Get();
389 cfg
->Write(wxT("paper_margin_left"), topleft
.x
);
390 cfg
->Write(wxT("paper_margin_right"), bottomright
.x
);
391 cfg
->Write(wxT("paper_margin_top"), topleft
.y
);
392 cfg
->Write(wxT("paper_margin_bottom"), bottomright
.y
);
399 Aven::MacOpenFiles(const wxArrayString
& filenames
)
401 if (filenames
.size() != 1) {
402 ReportError(wxT("Aven can only load one file at a time"));
405 m_Frame
->OpenFile(filenames
[0], wxString());
409 Aven::MacPrintFiles(const wxArrayString
& filenames
)
411 if (filenames
.size() != 1) {
412 ReportError(wxT("Aven can only print one file at a time"));
415 m_Frame
->OpenFile(filenames
[0], wxString());
416 m_Frame
->PrintAndExit();
420 void Aven::ReportError(const wxString
& msg
)
423 wxMessageBox(msg
, APP_NAME
, wxOK
| wxICON_ERROR
);
426 AvenAllowOnTop
ontop(m_Frame
);
427 wxMessageDialog
dlg(m_Frame
, msg
, APP_NAME
, wxOK
| wxICON_ERROR
);
434 static wxString path
;
436 path
= wxString(msg_cfgpth(), wxConvUTF8
);
440 // called to report errors by message.c
442 aven_v_report(int severity
, const char *fnm
, int line
, int en
, va_list ap
)
446 m
= wxString(fnm
, wxConvUTF8
);
447 if (line
) m
+= wxString::Format(wxT(":%d"), line
);
452 m
+= wmsg(/*warning*/4);
457 vsnprintf(buf
, sizeof(buf
), msg(en
), ap
);
458 m
+= wxString(buf
, wxConvUTF8
);
459 if (wxTheApp
== NULL
) {
460 // We haven't initialised the Aven app object yet.
461 if (!wxInitialize()) {
466 wxMessageBox(m
, APP_NAME
, wxOK
| wxICON_ERROR
);
469 wxGetApp().ReportError(m
);