source: git/src/aven.cc @ 6835e68

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 6835e68 was 706ddcf, checked in by Olly Betts <olly@…>, 9 years ago

Makefile.am,src/Makefile.am,src/aven.cc,src/message.c: Fix up the
handling of bundled epsg and esri files to work.

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