source: git/src/aven.cc @ d225571

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

src/aven.cc,src/gla-gl.cc: Ensure that the window has a depth buffer.
Whether it does by default seems to vary depending on OS and maybe
graphics card. Fixes #55 (terrain is no longer visible through
itself), and also the rendering of passage tubes. Thanks to Martin
Green for pointing me in the right direction for finding this fix.

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