source: git/src/aven.cc @ 4909e6bc

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

src/aven.cc: If the saved size for aven's window exceeds the current
display size (mostly likely because we're now plugged into a smaller
monitor), then reduce the size of the window to fit the display.
If the saved size is < (480x320), increase it to at least that, as
aven isn't usable in a smaller window.

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