| 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 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. |
|---|
| 13 | // |
|---|
| 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. |
|---|
| 18 | // |
|---|
| 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 |
|---|
| 22 | // |
|---|
| 23 | |
|---|
| 24 | #ifdef HAVE_CONFIG_H |
|---|
| 25 | #include <config.h> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include "aven.h" |
|---|
| 29 | #include "log.h" |
|---|
| 30 | #include "mainfrm.h" |
|---|
| 31 | |
|---|
| 32 | #include "cmdline.h" |
|---|
| 33 | #include "message.h" |
|---|
| 34 | #include "useful.h" |
|---|
| 35 | |
|---|
| 36 | #include <assert.h> |
|---|
| 37 | #include <stdio.h> |
|---|
| 38 | |
|---|
| 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 |
|---|
| 46 | |
|---|
| 47 | bool double_buffered = false; |
|---|
| 48 | |
|---|
| 49 | static const struct option long_opts[] = { |
|---|
| 50 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
|---|
| 51 | {"survey", required_argument, 0, 's'}, |
|---|
| 52 | {"print", no_argument, 0, 'p'}, |
|---|
| 53 | {"help", no_argument, 0, HLP_HELP}, |
|---|
| 54 | {"version", no_argument, 0, HLP_VERSION}, |
|---|
| 55 | {0, 0, 0, 0} |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | #define short_opts "s:p" |
|---|
| 59 | |
|---|
| 60 | static struct help_msg help[] = { |
|---|
| 61 | /* <-- */ |
|---|
| 62 | /* TRANSLATORS: --help output for --survey option. |
|---|
| 63 | * |
|---|
| 64 | * "this" has been added to English translation */ |
|---|
| 65 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
|---|
| 66 | /* TRANSLATORS: --help output for aven --print option */ |
|---|
| 67 | {HLP_ENCODELONG(1), /*print and exit (requires a 3d file)*/119, 0}, |
|---|
| 68 | {0, 0, 0} |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | #ifdef __WXMSW__ |
|---|
| 72 | IMPLEMENT_APP(Aven) |
|---|
| 73 | #else |
|---|
| 74 | IMPLEMENT_APP_NO_MAIN(Aven) |
|---|
| 75 | IMPLEMENT_WX_THEME_SUPPORT |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | Aven::Aven() : |
|---|
| 79 | m_Frame(NULL), m_pageSetupData(NULL) |
|---|
| 80 | { |
|---|
| 81 | wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | Aven::~Aven() |
|---|
| 85 | { |
|---|
| 86 | delete m_pageSetupData; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | static int getopt_first_response = 0; |
|---|
| 90 | |
|---|
| 91 | static char ** utf8_argv; |
|---|
| 92 | |
|---|
| 93 | #ifdef __WXMSW__ |
|---|
| 94 | bool Aven::Initialize(int& my_argc, wxChar **my_argv) |
|---|
| 95 | { |
|---|
| 96 | // wxWidgets passes us wxChars, which may be wide characters but cmdline |
|---|
| 97 | // wants UTF-8 so we need to convert. |
|---|
| 98 | utf8_argv = new char * [my_argc + 1]; |
|---|
| 99 | for (int i = 0; i < my_argc; ++i){ |
|---|
| 100 | utf8_argv[i] = strdup(wxString(my_argv[i]).mb_str()); |
|---|
| 101 | } |
|---|
| 102 | utf8_argv[my_argc] = NULL; |
|---|
| 103 | |
|---|
| 104 | msg_init(utf8_argv); |
|---|
| 105 | pj_set_finder(msg_proj_finder); |
|---|
| 106 | select_charset(CHARSET_UTF8); |
|---|
| 107 | /* Want --version and decent --help output, which cmdline does for us. |
|---|
| 108 | * wxCmdLine is much less good. |
|---|
| 109 | */ |
|---|
| 110 | /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions |
|---|
| 111 | * - it should be translated to the terminology that cavers using the |
|---|
| 112 | * language would use. |
|---|
| 113 | * |
|---|
| 114 | * Part of aven --help */ |
|---|
| 115 | cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL); |
|---|
| 116 | cmdline_init(my_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1); |
|---|
| 117 | getopt_first_response = cmdline_getopt(); |
|---|
| 118 | return wxApp::Initialize(my_argc, my_argv); |
|---|
| 119 | } |
|---|
| 120 | #else |
|---|
| 121 | int main(int argc, char **argv) |
|---|
| 122 | { |
|---|
| 123 | #ifdef __WXMAC__ |
|---|
| 124 | // MacOS passes a magic -psn_XXXX command line argument in argv[1] which |
|---|
| 125 | // wx ignores for us, but in wxApp::Initialize() which hasn't been |
|---|
| 126 | // called yet. So we need to remove it ourselves. |
|---|
| 127 | if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) { |
|---|
| 128 | --argc; |
|---|
| 129 | memmove(argv + 1, argv + 2, argc * sizeof(char *)); |
|---|
| 130 | } |
|---|
| 131 | #endif |
|---|
| 132 | // Call msg_init() and start processing the command line first so that |
|---|
| 133 | // we can respond to --help and --version even without an X display. |
|---|
| 134 | msg_init(argv); |
|---|
| 135 | #ifdef __WXMAC__ |
|---|
| 136 | pj_set_finder(msg_proj_finder); |
|---|
| 137 | #endif |
|---|
| 138 | select_charset(CHARSET_UTF8); |
|---|
| 139 | /* Want --version and decent --help output, which cmdline does for us. |
|---|
| 140 | * wxCmdLine is much less good. |
|---|
| 141 | */ |
|---|
| 142 | cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL); |
|---|
| 143 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1); |
|---|
| 144 | getopt_first_response = cmdline_getopt(); |
|---|
| 145 | |
|---|
| 146 | utf8_argv = argv; |
|---|
| 147 | |
|---|
| 148 | #if wxUSE_UNICODE |
|---|
| 149 | wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0])); |
|---|
| 150 | wxChar * wargv[2]; |
|---|
| 151 | if (buf) { |
|---|
| 152 | wargv[0] = wxStrdup(buf); |
|---|
| 153 | } else { |
|---|
| 154 | // Eep - couldn't convert the executable's name to wide characters! |
|---|
| 155 | wargv[0] = wxStrdup(APP_NAME); |
|---|
| 156 | } |
|---|
| 157 | wargv[1] = NULL; |
|---|
| 158 | int wargc = 1; |
|---|
| 159 | return wxEntry(wargc, wargv); |
|---|
| 160 | #else |
|---|
| 161 | char *dummy_argv[2] = { argv[0], NULL }; |
|---|
| 162 | int dummy_argc = 1; |
|---|
| 163 | return wxEntry(dummy_argc, dummy_argv); |
|---|
| 164 | #endif |
|---|
| 165 | } |
|---|
| 166 | #endif |
|---|
| 167 | |
|---|
| 168 | bool Aven::OnInit() |
|---|
| 169 | { |
|---|
| 170 | wxLog::SetActiveTarget(new MyLogWindow()); |
|---|
| 171 | |
|---|
| 172 | { |
|---|
| 173 | // Suppress message box warnings about messages not found. |
|---|
| 174 | wxLogNull logNo; |
|---|
| 175 | wxLocale *loc = new wxLocale(); |
|---|
| 176 | loc->AddCatalogLookupPathPrefix(wmsg_cfgpth()); |
|---|
| 177 | wxString msg_lang_str(msg_lang, wxConvUTF8); |
|---|
| 178 | const char *lang = msg_lang2 ? msg_lang2 : msg_lang; |
|---|
| 179 | wxString lang_str(lang, wxConvUTF8); |
|---|
| 180 | #if wxCHECK_VERSION(2,9,0) |
|---|
| 181 | loc->Init(msg_lang_str, lang_str, msg_lang_str); |
|---|
| 182 | #else |
|---|
| 183 | loc->Init(msg_lang_str, lang_str, msg_lang_str, true, true); |
|---|
| 184 | #endif |
|---|
| 185 | // The existence of the wxLocale object is enough - no need to keep a |
|---|
| 186 | // pointer to it! |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | wxString survey; |
|---|
| 190 | bool print_and_exit = false; |
|---|
| 191 | |
|---|
| 192 | while (true) { |
|---|
| 193 | int opt; |
|---|
| 194 | if (getopt_first_response) { |
|---|
| 195 | opt = getopt_first_response; |
|---|
| 196 | getopt_first_response = 0; |
|---|
| 197 | } else { |
|---|
| 198 | opt = cmdline_getopt(); |
|---|
| 199 | } |
|---|
| 200 | if (opt == EOF) break; |
|---|
| 201 | if (opt == 's') { |
|---|
| 202 | survey = wxString(optarg, wxConvUTF8); |
|---|
| 203 | } |
|---|
| 204 | if (opt == 'p') { |
|---|
| 205 | print_and_exit = true; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | if (print_and_exit && !utf8_argv[optind]) { |
|---|
| 210 | cmdline_syntax(); // FIXME : not a helpful error... |
|---|
| 211 | exit(1); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | wxString fnm; |
|---|
| 215 | if (utf8_argv[optind]) { |
|---|
| 216 | fnm = wxString(utf8_argv[optind], wxConvUTF8); |
|---|
| 217 | if (fnm.empty() && *(utf8_argv[optind])) { |
|---|
| 218 | ReportError(wxT("File argument's filename has bad encoding")); |
|---|
| 219 | return false; |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | // Use a double-buffered visual if available, as it will give much smoother |
|---|
| 224 | // animation. |
|---|
| 225 | double_buffered = true; |
|---|
| 226 | const int wx_gl_attribs[] = { |
|---|
| 227 | WX_GL_DOUBLEBUFFER, |
|---|
| 228 | WX_GL_RGBA, |
|---|
| 229 | 0 |
|---|
| 230 | }; |
|---|
| 231 | if (!InitGLVisual(wx_gl_attribs)) { |
|---|
| 232 | if (!InitGLVisual(wx_gl_attribs + 1)) { |
|---|
| 233 | wxString m; |
|---|
| 234 | /* TRANSLATORS: %s will be replaced with "Aven" currently (and |
|---|
| 235 | * perhaps by "Survex" or other things in future). */ |
|---|
| 236 | m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME); |
|---|
| 237 | wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION); |
|---|
| 238 | exit(1); |
|---|
| 239 | } |
|---|
| 240 | double_buffered = false; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | wxImage::AddHandler(new wxPNGHandler); |
|---|
| 244 | |
|---|
| 245 | // Obtain the screen geometry. |
|---|
| 246 | #if wxUSE_DISPLAY |
|---|
| 247 | wxRect geom = wxDisplay().GetGeometry(); |
|---|
| 248 | #else |
|---|
| 249 | wxRect geom; |
|---|
| 250 | wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height); |
|---|
| 251 | #endif |
|---|
| 252 | |
|---|
| 253 | wxPoint pos(wxDefaultPosition); |
|---|
| 254 | int width, height; |
|---|
| 255 | wxConfigBase::Get()->Read(wxT("width"), &width, 0); |
|---|
| 256 | if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0); |
|---|
| 257 | // We used to persist full screen mode (-1 was maximized, |
|---|
| 258 | // -2 full screen), but people would get stuck in full |
|---|
| 259 | // screen mode, unsure how to exit. |
|---|
| 260 | bool maximized = (width <= -1); |
|---|
| 261 | if (width <= 0 || height <= 0) { |
|---|
| 262 | pos.x = geom.x; |
|---|
| 263 | pos.y = geom.y; |
|---|
| 264 | width = geom.width; |
|---|
| 265 | height = geom.height; |
|---|
| 266 | |
|---|
| 267 | // Calculate a reasonable size for our window. |
|---|
| 268 | pos.x += width / 8; |
|---|
| 269 | pos.y += height / 8; |
|---|
| 270 | width = width * 3 / 4; |
|---|
| 271 | height = height * 3 / 4; |
|---|
| 272 | } else { |
|---|
| 273 | // Impose a minimum size for sanity, and make sure the window fits on |
|---|
| 274 | // the display (in case the current display is smaller than the one |
|---|
| 275 | // in use when the window size was saved). (480x320) is about the |
|---|
| 276 | // smallest usable size for aven's window. |
|---|
| 277 | const int min_width = min(geom.width, 480); |
|---|
| 278 | const int min_height = min(geom.height, 320); |
|---|
| 279 | if (width < min_width || height < min_height) { |
|---|
| 280 | if (width < min_width) { |
|---|
| 281 | width = min_width; |
|---|
| 282 | } |
|---|
| 283 | if (height < min_height) { |
|---|
| 284 | height = min_height; |
|---|
| 285 | } |
|---|
| 286 | pos.x = geom.x + (geom.width - width) / 4; |
|---|
| 287 | pos.y = geom.y + (geom.height - height) / 4; |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | // Create the main window. |
|---|
| 292 | m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height)); |
|---|
| 293 | |
|---|
| 294 | // Select maximised if that's the saved state. |
|---|
| 295 | if (maximized) { |
|---|
| 296 | m_Frame->Maximize(); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | if (utf8_argv[optind]) { |
|---|
| 300 | m_Frame->OpenFile(fnm, survey); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | if (print_and_exit) { |
|---|
| 304 | m_Frame->PrintAndExit(); |
|---|
| 305 | return true; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | m_Frame->Show(true); |
|---|
| 309 | #ifdef _WIN32 |
|---|
| 310 | m_Frame->SetFocus(); |
|---|
| 311 | #endif |
|---|
| 312 | return true; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | wxPageSetupDialogData * |
|---|
| 316 | Aven::GetPageSetupDialogData() |
|---|
| 317 | { |
|---|
| 318 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
|---|
| 319 | #ifdef __WXGTK__ |
|---|
| 320 | // Fetch paper margins stored on disk. |
|---|
| 321 | int left, right, top, bottom; |
|---|
| 322 | wxConfigBase * cfg = wxConfigBase::Get(); |
|---|
| 323 | // These default margins were chosen by looking at all the .ppd files |
|---|
| 324 | // on my machine. |
|---|
| 325 | cfg->Read(wxT("paper_margin_left"), &left, 7); |
|---|
| 326 | cfg->Read(wxT("paper_margin_right"), &right, 7); |
|---|
| 327 | cfg->Read(wxT("paper_margin_top"), &top, 14); |
|---|
| 328 | cfg->Read(wxT("paper_margin_bottom"), &bottom, 14); |
|---|
| 329 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
|---|
| 330 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
|---|
| 331 | #endif |
|---|
| 332 | return m_pageSetupData; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | void |
|---|
| 336 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
|---|
| 337 | { |
|---|
| 338 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
|---|
| 339 | *m_pageSetupData = psdd; |
|---|
| 340 | #ifdef __WXGTK__ |
|---|
| 341 | wxPoint topleft = psdd.GetMarginTopLeft(); |
|---|
| 342 | wxPoint bottomright = psdd.GetMarginBottomRight(); |
|---|
| 343 | |
|---|
| 344 | // Store user specified paper margins on disk/in registry. |
|---|
| 345 | wxConfigBase * cfg = wxConfigBase::Get(); |
|---|
| 346 | cfg->Write(wxT("paper_margin_left"), topleft.x); |
|---|
| 347 | cfg->Write(wxT("paper_margin_right"), bottomright.x); |
|---|
| 348 | cfg->Write(wxT("paper_margin_top"), topleft.y); |
|---|
| 349 | cfg->Write(wxT("paper_margin_bottom"), bottomright.y); |
|---|
| 350 | cfg->Flush(); |
|---|
| 351 | #endif |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | #ifdef __WXMAC__ |
|---|
| 355 | void |
|---|
| 356 | Aven::MacOpenFiles(const wxArrayString & filenames) |
|---|
| 357 | { |
|---|
| 358 | if (filenames.size() != 1) { |
|---|
| 359 | ReportError(wxT("Aven can only load one file at a time")); |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | m_Frame->OpenFile(filenames[0], wxString()); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | void |
|---|
| 366 | Aven::MacPrintFiles(const wxArrayString & filenames) |
|---|
| 367 | { |
|---|
| 368 | if (filenames.size() != 1) { |
|---|
| 369 | ReportError(wxT("Aven can only print one file at a time")); |
|---|
| 370 | return; |
|---|
| 371 | } |
|---|
| 372 | m_Frame->OpenFile(filenames[0], wxString()); |
|---|
| 373 | m_Frame->PrintAndExit(); |
|---|
| 374 | } |
|---|
| 375 | #endif |
|---|
| 376 | |
|---|
| 377 | void Aven::ReportError(const wxString& msg) |
|---|
| 378 | { |
|---|
| 379 | if (!m_Frame) { |
|---|
| 380 | wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR); |
|---|
| 381 | return; |
|---|
| 382 | } |
|---|
| 383 | AvenAllowOnTop ontop(m_Frame); |
|---|
| 384 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
|---|
| 385 | dlg.ShowModal(); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | wxString |
|---|
| 389 | wmsg(int msg_no) |
|---|
| 390 | { |
|---|
| 391 | return wxString::FromUTF8(msg(msg_no)); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | const wxString & |
|---|
| 395 | wmsg_cfgpth() |
|---|
| 396 | { |
|---|
| 397 | static wxString path; |
|---|
| 398 | if (path.empty()) |
|---|
| 399 | path = wxString(msg_cfgpth(), wxConvUTF8); |
|---|
| 400 | return path; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | // called to report errors by message.c |
|---|
| 404 | extern "C" void |
|---|
| 405 | aven_v_report(int severity, const char *fnm, int line, int en, va_list ap) |
|---|
| 406 | { |
|---|
| 407 | wxString m; |
|---|
| 408 | if (fnm) { |
|---|
| 409 | m = wxString(fnm, wxConvUTF8); |
|---|
| 410 | if (line) m += wxString::Format(wxT(":%d"), line); |
|---|
| 411 | m += wxT(": "); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | if (severity == 0) { |
|---|
| 415 | m += wmsg(/*warning*/4); |
|---|
| 416 | m += wxT(": "); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | char buf[1024]; |
|---|
| 420 | vsnprintf(buf, sizeof(buf), msg(en), ap); |
|---|
| 421 | m += wxString(buf, wxConvUTF8); |
|---|
| 422 | if (wxTheApp == NULL) { |
|---|
| 423 | // We haven't initialised the Aven app object yet. |
|---|
| 424 | if (!wxInitialize()) { |
|---|
| 425 | fputs(buf, stderr); |
|---|
| 426 | PUTC('\n', stderr); |
|---|
| 427 | exit(1); |
|---|
| 428 | } |
|---|
| 429 | wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR); |
|---|
| 430 | wxUninitialize(); |
|---|
| 431 | } else { |
|---|
| 432 | wxGetApp().ReportError(m); |
|---|
| 433 | } |
|---|
| 434 | } |
|---|