source: git/src/aven.cc @ c891a7e

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since c891a7e was f1d5d26, checked in by Olly Betts <olly@…>, 11 years ago

src/aven.cc: Fix to build with wx 2.9.5 with wx2.8 compatibility
disabled.

  • Property mode set to 100644
File size: 10.4 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.
[c8b5fa5]7//  Copyright (C) 2002,2003,2004,2005,2006,2011,2013 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    /*                          <-- */
63    {HLP_ENCODELONG(0),       /*only load the sub-survey with this prefix*/199, 0},
64    {HLP_ENCODELONG(1),       /*print and exit (requires a 3d file)*/119, 0},
65    {0, 0, 0}
[879e9c3]66};
67
[5627cbb]68#ifdef __WXMSW__
[5809313]69IMPLEMENT_APP(Aven)
[5627cbb]70#else
71IMPLEMENT_APP_NO_MAIN(Aven)
72IMPLEMENT_WX_THEME_SUPPORT
73#endif
[0060ba5]74
[5809313]75Aven::Aven() :
[2c1d2f4]76    m_Frame(NULL), m_pageSetupData(NULL)
[0060ba5]77{
[1f81f3d]78    wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
[0060ba5]79}
80
[2c1d2f4]81Aven::~Aven()
82{
[81f1266]83    delete m_pageSetupData;
[2c1d2f4]84}
85
[5627cbb]86static int getopt_first_response = 0;
87
88#ifdef __WXMSW__
89bool Aven::Initialize(int& my_argc, wxChar **my_argv)
90{
[293eec8]91    // wxWidgets passes us wxChars, which may be wide characters but cmdline
92    // wants UTF-8 so we need to convert.
[aa7c3c2]93#ifdef __GNUC__
94    // This is a GCC extension.
[5627cbb]95    char *utf8_argv[my_argc + 1];
[aa7c3c2]96#else
97    vector<char *> utf8_argv(my_argc + 1);
98#endif
[5627cbb]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
[aa7c3c2]104    msg_init(&utf8_argv[0]);
[f1cbf74]105    select_charset(CHARSET_UTF8);
[293eec8]106    /* Want --version and decent --help output, which cmdline does for us.
[f1cbf74]107     * wxCmdLine is much less good.
108     */
[d8dbdff]109    cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[aa7c3c2]110    cmdline_init(my_argc, &utf8_argv[0], short_opts, long_opts, NULL, help, 0, 1);
[5627cbb]111    getopt_first_response = cmdline_getopt();
112    return wxApp::Initialize(my_argc, my_argv);
[f1cbf74]113}
[5627cbb]114#else
115static char ** real_argv;
[f1cbf74]116
[5627cbb]117int main(int argc, char **argv)
[0060ba5]118{
[27b8b59]119#ifdef __WXMAC__
[879e9c3]120    // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
121    // wx ignores for us, but in wxApp::Initialize() which hasn't been
122    // called yet.  So we need to remove it ourselves.
[5627cbb]123    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
124        --argc;
125        memmove(argv + 1, argv + 2, argc * sizeof(char *));
[879e9c3]126    }
127#endif
128    // Call msg_init() and start processing the command line first so that
129    // we can respond to --help and --version even without an X display.
[5627cbb]130    msg_init(argv);
131    select_charset(CHARSET_UTF8);
[293eec8]132    /* Want --version and decent --help output, which cmdline does for us.
[5627cbb]133     * wxCmdLine is much less good.
134     */
[d8dbdff]135    cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[5627cbb]136    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
[879e9c3]137    getopt_first_response = cmdline_getopt();
[5627cbb]138
139    real_argv = argv;
140
[f891a0c]141#if wxUSE_UNICODE
[5627cbb]142    wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
143    wxChar * wargv[2];
144    if (buf) {
145        wargv[0] = wxStrdup(buf);
146    } else {
147        // Eep - couldn't convert the executable's name to wide characters!
148        wargv[0] = wxStrdup(APP_NAME);
149    }
150    wargv[1] = NULL;
151    int wargc = 1;
152    return wxEntry(wargc, wargv);
[495cd3e]153#else
[a212391]154    char *dummy_argv[2] = { argv[0], NULL };
155    int dummy_argc = 1;
156    return wxEntry(dummy_argc, dummy_argv);
[495cd3e]157#endif
[879e9c3]158}
[5627cbb]159#endif
[879e9c3]160
161bool Aven::OnInit()
162{
[4013d9c]163    wxLog::SetActiveTarget(new MyLogWindow());
[421b7d2]164
[d607cea]165    {
[b1876d3]166        // Suppress message box warnings about messages not found.
[71ea9e1]167        wxLogNull logNo;
168        wxLocale *loc = new wxLocale();
[8a05a7a]169        loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
[b1876d3]170        wxString msg_lang_str(msg_lang, wxConvUTF8);
171        const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
172        wxString lang_str(lang, wxConvUTF8);
[f1d5d26]173#if wxCHECK_VERSION(2,9,0)
174        loc->Init(msg_lang_str, lang_str, msg_lang_str);
175#else
[6d1bc83]176        loc->Init(msg_lang_str, lang_str, msg_lang_str, true, true);
[f1d5d26]177#endif
[71ea9e1]178        // The existence of the wxLocale object is enough - no need to keep a
179        // pointer to it!
[003d953]180    }
[93c3f97]181
[d07944e]182    wxString survey;
[3ee64fb]183    bool print_and_exit = false;
[421b7d2]184
[0544329]185#ifdef __WXMSW__
186    char ** real_argv = argv;
187#endif
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
[5627cbb]206    if (print_and_exit && !real_argv[optind]) {
[3ee64fb]207        cmdline_syntax(); // FIXME : not a helpful error...
208        exit(1);
[d07944e]209    }
[068b4f2]210
[5627cbb]211    wxString fnm;
212    if (real_argv[optind]) {
213        fnm = wxString(real_argv[optind], wxConvUTF8);
214        if (fnm.empty() && *(real_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;
[ee7511a]228            m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
[c293aa9]229            wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
230            exit(1);
231        }
232        double_buffered = false;
[5892e08]233    }
234
[573f4e9]235    wxImage::AddHandler(new wxPNGHandler);
[08d2074]236
[486ae6a]237    // Obtain the screen size.
[3db0798]238    wxPoint pos(wxDefaultPosition);
[ca18348]239    int width, height;
[5627cbb]240    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
241    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
[3db0798]242    bool maximized = (width == -1);
243    bool full_screen = (width <= -2);
244    if (width <= 0 || height <= 0) {
[cea3549]245#if wxUSE_DISPLAY
[3db0798]246        wxRect geom = wxDisplay().GetGeometry();
247        pos.x = geom.x;
248        pos.y = geom.y;
249        width = geom.width;
250        height = geom.height;
[ca18348]251#else
[3db0798]252        wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
253        // Crude fix to help behaviour on multi-monitor displays.
254        // Fudge factors are a bit specific to my setup...
255        if (width > height * 3 / 2) {
256            pos.x += width;
257            width = height * 3 / 2;
258            pos.x -= width;
259        }
[ca18348]260#endif
[486ae6a]261
[3db0798]262        // Calculate a reasonable size for our window.
263        pos.x += width / 8;
264        pos.y += height / 8;
265        width = width * 3 / 4;
266        height = height * 3 / 4;
267    }
[486ae6a]268
269    // Create the main window.
[3db0798]270    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
271
272    // Select full_screen or maximised if that's the saved state.
273    if (full_screen) {
274        m_Frame->ShowFullScreen(true);
275    } else if (maximized) {
276        m_Frame->Maximize();
277    }
[068b4f2]278
[5627cbb]279    if (real_argv[optind]) {
280        m_Frame->OpenFile(fnm, survey);
[d07944e]281    }
[068b4f2]282
[3ee64fb]283    if (print_and_exit) {
284        wxCommandEvent dummy;
285        m_Frame->OnPrint(dummy);
286        m_Frame->OnQuit(dummy);
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
[5809313]336void Aven::ReportError(const wxString& msg)
[0060ba5]337{
[c8b5fa5]338    if (!m_Frame) {
339        wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
340        return;
341    }
[1c6b20d7]342    AvenAllowOnTop ontop(m_Frame);
[7015222]343    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
344    dlg.ShowModal();
[0060ba5]345}
[59fda0a]346
[5627cbb]347wxString
348wmsg(int msg_no)
349{
[833c6bc]350    return wxString::FromUTF8(msg(msg_no));
[5627cbb]351}
352
[8a05a7a]353const wxString &
354wmsg_cfgpth()
355{
356    static wxString path;
357    if (path.empty())
358        path = wxString(msg_cfgpth(), wxConvUTF8);
359    return path;
360}
361
[a9a32f2]362// called to report errors by message.c
363extern "C" void
364aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
365{
[c49e27f]366    wxString m;
367    if (fnm) {
368        m = wxString(fnm, wxConvUTF8);
369        if (line) m += wxString::Format(wxT(":%d"), line);
370        m += wxT(": ");
371    }
372
373    if (severity == 0) {
374        m += wmsg(/*warning*/4);
375        m += wxT(": ");
376    }
377
378    char buf[1024];
379    vsnprintf(buf, sizeof(buf), msg(en), ap);
380    m += wxString(buf, wxConvUTF8);
381    if (wxTheApp == NULL) {
382        // We haven't initialised the Aven app object yet.
383        if (!wxInitialize()) {
384            fputs(buf, stderr);
385            PUTC('\n', stderr);
386            exit(1);
387        }
388        wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
389        wxUninitialize();
390    } else {
391        wxGetApp().ReportError(m);
392    }
[a9a32f2]393}
Note: See TracBrowser for help on using the repository browser.