source: git/src/aven.cc @ a72ed95

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since a72ed95 was ddc0994, checked in by Olly Betts <olly@…>, 9 years ago

lib/INSTALL.OSX,src/aven.cc,src/aven.h: Add handlers so that files
can be loaded and printed from the finder on OS X.

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