source: git/src/aven.cc @ 1dfd718

stereo-2025
Last change on this file since 1dfd718 was 736ec93, checked in by Olly Betts <olly@…>, 6 months ago

Update wx bug URL

  • Property mode set to 100644
File size: 13.7 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.
[dba56bb]7//  Copyright (C) 2002-2024 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#include <config.h>
25
[9990aab]26#define MSG_SETUP_PROJ_SEARCH_PATH 1
27
[5809313]28#include "aven.h"
[78924eb]29#include "log.h"
[caa5fda]30#include "gla.h"
[5809313]31#include "mainfrm.h"
[706b033]32
[d07944e]33#include "cmdline.h"
[93c3f97]34#include "message.h"
[c49e27f]35#include "useful.h"
[0060ba5]36
37#include <assert.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
[faf83bee]48#ifdef __WXMSW__
49#include <windows.h>
50#endif
51
[879e9c3]52static const struct option long_opts[] = {
53    /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
54    {"survey", required_argument, 0, 's'},
55    {"print", no_argument, 0, 'p'},
56    {"help", no_argument, 0, HLP_HELP},
57    {"version", no_argument, 0, HLP_VERSION},
58    {0, 0, 0, 0}
59};
60
61#define short_opts "s:p"
62
63static struct help_msg help[] = {
[45af761]64    /*                          <-- */
[736f7df]65    /* TRANSLATORS: --help output for --survey option.
66     *
67     * "this" has been added to English translation */
[5a2d346]68    {HLP_ENCODELONG(0),       /*only load the sub-survey with this prefix*/199, 0, 0},
[736f7df]69    /* TRANSLATORS: --help output for aven --print option */
[5a2d346]70    {HLP_ENCODELONG(1),       /*print and exit (requires a 3d file)*/119, 0, 0},
71    {0, 0, 0, 0}
[879e9c3]72};
73
[5627cbb]74#ifdef __WXMSW__
[5809313]75IMPLEMENT_APP(Aven)
[5627cbb]76#else
77IMPLEMENT_APP_NO_MAIN(Aven)
78IMPLEMENT_WX_THEME_SUPPORT
79#endif
[0060ba5]80
[a3f83057]81Aven::Aven()
[0060ba5]82{
[1f81f3d]83    wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
[0060ba5]84}
85
[2c1d2f4]86Aven::~Aven()
87{
[81f1266]88    delete m_pageSetupData;
[2c1d2f4]89}
90
[5627cbb]91static int getopt_first_response = 0;
92
[5440c295]93static char ** utf8_argv;
94
[5627cbb]95#ifdef __WXMSW__
96bool Aven::Initialize(int& my_argc, wxChar **my_argv)
97{
[faf83bee]98    const wxChar * cmd_line = GetCommandLineW();
99
[b208d0b]100    // Horrible bodge to handle therion's assumptions about the "Process"
101    // file association.
[faf83bee]102    if (cmd_line) {
103        // None of these are valid aven command line options, so this is not
104        // going to be triggered accidentally.
[ccaf632]105        const wxChar * p = wxStrstr(cmd_line,
106                                    wxT("aven.exe\" --quiet --log --output="));
[faf83bee]107        if (p) {
108            // Just change the command name in the command line string - that
109            // way the quoting should match what the C runtime expects.
110            wxString cmd(cmd_line, p - cmd_line);
111            cmd += "cavern";
112            cmd += p + 4;
113            exit(wxExecute(cmd, wxEXEC_SYNC));
114        }
[b208d0b]115    }
116
[faf83bee]117    int utf8_argc;
118    {
119        // wxWidgets doesn't split up the command line in the standard way, so
120        // redo it ourselves using the standard API function.
121        //
122        // Warning: The returned array from this has no terminating NULL
123        // element.
124        wxChar ** new_argv = NULL;
125        if (cmd_line)
126            new_argv = CommandLineToArgvW(cmd_line, &utf8_argc);
127        bool failed = (new_argv == NULL);
128        if (failed) {
129            wxChar * p;
130            FormatMessage(
131                    FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
132                    NULL,
133                    GetLastError(),
134                    0,
135                    (LPWSTR)&p,
136                    4096,
137                    NULL);
138            wxString m = "CommandLineToArgvW failed: ";
139            m += p;
140            wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
141            LocalFree(p);
142            utf8_argc = my_argc;
143            new_argv = my_argv;
144        }
145
146        // Convert wide characters to UTF-8.
147        utf8_argv = new char * [utf8_argc + 1];
148        for (int i = 0; i < utf8_argc; ++i){
[8562abc]149            utf8_argv[i] = strdup(wxString(new_argv[i]).utf8_str());
[faf83bee]150        }
151        utf8_argv[utf8_argc] = NULL;
152
153        if (!failed) LocalFree(new_argv);
[5627cbb]154    }
155
[5440c295]156    msg_init(utf8_argv);
[f1cbf74]157    select_charset(CHARSET_UTF8);
[293eec8]158    /* Want --version and decent --help output, which cmdline does for us.
[f1cbf74]159     * wxCmdLine is much less good.
160     */
[736f7df]161    /* TRANSLATORS: Here "survey" is a "cave map" rather than list of questions
162     * - it should be translated to the terminology that cavers using the
163     * language would use.
[b49ac56]164     *
[736f7df]165     * Part of aven --help */
[d8dbdff]166    cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[faf83bee]167    cmdline_init(utf8_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1);
[5627cbb]168    getopt_first_response = cmdline_getopt();
[faf83bee]169
170    // The argc and argv arguments don't actually get used here.
171    int dummy_argc = 0;
172    return wxApp::Initialize(dummy_argc, NULL);
[f1cbf74]173}
[5627cbb]174#else
[b49ac56]175int main(int argc, char **argv)
[0060ba5]176{
[96c239c]177#ifdef __WXGTK3__
[47f55ed]178# if !(wxUSE_GLCANVAS_EGL-0)
179    // The GLX-based wxGLCanvas doesn't work under Wayland, and the code
[736ec93]180    // segfaults: https://github.com/wxWidgets/wxWidgets/issues/17702
[47f55ed]181    //
182    // Therefore we force X11 unless we're using the EGL-based wxGLCanvas
183    // (which was added in wxWidgets 3.1.5 and hasn't been backported to
184    // 3.0.x).
185    //
[96c239c]186    // Setting GDK_BACKEND=x11 is the recommended workaround, and it seems to
[b0908ea]187    // work to set it here.  GTK2 doesn't support Wayland, so doesn't need
188    // this.
[96c239c]189    setenv("GDK_BACKEND", "x11", 1);
[47f55ed]190# endif
[96c239c]191#endif
[705adee9]192
[27b8b59]193#ifdef __WXMAC__
[879e9c3]194    // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
195    // wx ignores for us, but in wxApp::Initialize() which hasn't been
196    // called yet.  So we need to remove it ourselves.
[5627cbb]197    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
198        --argc;
199        memmove(argv + 1, argv + 2, argc * sizeof(char *));
[879e9c3]200    }
201#endif
202    // Call msg_init() and start processing the command line first so that
203    // we can respond to --help and --version even without an X display.
[5627cbb]204    msg_init(argv);
205    select_charset(CHARSET_UTF8);
[293eec8]206    /* Want --version and decent --help output, which cmdline does for us.
[5627cbb]207     * wxCmdLine is much less good.
208     */
[d8dbdff]209    cmdline_set_syntax_message(/*[SURVEY_FILE]*/269, 0, NULL);
[5627cbb]210    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
[879e9c3]211    getopt_first_response = cmdline_getopt();
[5627cbb]212
[5440c295]213    utf8_argv = argv;
[5627cbb]214
[f891a0c]215#if wxUSE_UNICODE
[5627cbb]216    wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
217    wxChar * wargv[2];
218    if (buf) {
219        wargv[0] = wxStrdup(buf);
220    } else {
221        // Eep - couldn't convert the executable's name to wide characters!
222        wargv[0] = wxStrdup(APP_NAME);
223    }
224    wargv[1] = NULL;
225    int wargc = 1;
226    return wxEntry(wargc, wargv);
[495cd3e]227#else
[a212391]228    char *dummy_argv[2] = { argv[0], NULL };
229    int dummy_argc = 1;
230    return wxEntry(dummy_argc, dummy_argv);
[495cd3e]231#endif
[879e9c3]232}
[5627cbb]233#endif
[879e9c3]234
235bool Aven::OnInit()
236{
[4013d9c]237    wxLog::SetActiveTarget(new MyLogWindow());
[421b7d2]238
[d607cea]239    {
[b1876d3]240        // Suppress message box warnings about messages not found.
[71ea9e1]241        wxLogNull logNo;
242        wxLocale *loc = new wxLocale();
[8a05a7a]243        loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
[b1876d3]244        wxString msg_lang_str(msg_lang, wxConvUTF8);
245        const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
246        wxString lang_str(lang, wxConvUTF8);
[f1d5d26]247        loc->Init(msg_lang_str, lang_str, msg_lang_str);
[71ea9e1]248        // The existence of the wxLocale object is enough - no need to keep a
249        // pointer to it!
[003d953]250    }
[93c3f97]251
[668b930]252    const char* opt_survey = NULL;
[3ee64fb]253    bool print_and_exit = false;
[421b7d2]254
[0580c6a]255    while (true) {
[879e9c3]256        int opt;
257        if (getopt_first_response) {
258            opt = getopt_first_response;
259            getopt_first_response = 0;
260        } else {
261            opt = cmdline_getopt();
262        }
[d07944e]263        if (opt == EOF) break;
264        if (opt == 's') {
[668b930]265            if (opt_survey != NULL) {
266                // FIXME: Not a helpful error, but this is temporary until
267                // we actually hook up support for specifying multiple
268                // --survey options properly here.
269                cmdline_syntax();
270                exit(1);
271            }
272            opt_survey = optarg;
[d07944e]273        }
[3ee64fb]274        if (opt == 'p') {
275            print_and_exit = true;
276        }
277    }
278
[5440c295]279    if (print_and_exit && !utf8_argv[optind]) {
[3ee64fb]280        cmdline_syntax(); // FIXME : not a helpful error...
281        exit(1);
[d07944e]282    }
[068b4f2]283
[5627cbb]284    wxString fnm;
[5440c295]285    if (utf8_argv[optind]) {
286        fnm = wxString(utf8_argv[optind], wxConvUTF8);
287        if (fnm.empty() && *(utf8_argv[optind])) {
[c49e27f]288            ReportError(wxT("File argument's filename has bad encoding"));
[5627cbb]289            return false;
290        }
291    }
292
[caa5fda]293    if (!GLACanvas::check_visual()) {
294        wxString m;
295        /* TRANSLATORS: %s will be replaced with "Aven" currently (and
296         * perhaps by "Survex" or other things in future). */
297        m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn’t available.*/405), APP_NAME);
298        wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
299        exit(1);
[5892e08]300    }
301
[573f4e9]302    wxImage::AddHandler(new wxPNGHandler);
[08d2074]303
[4909e6bc]304    // Obtain the screen geometry.
305#if wxUSE_DISPLAY
306    wxRect geom = wxDisplay().GetGeometry();
307#else
308    wxRect geom;
309    wxClientDisplayRect(&geom.x, &geom.y, &geom.width, &geom.height);
310#endif
311
[3db0798]312    wxPoint pos(wxDefaultPosition);
[ca18348]313    int width, height;
[5627cbb]314    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
315    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
[30621f5]316    // We used to persist full screen mode (-1 was maximized,
317    // -2 full screen), but people would get stuck in full
318    // screen mode, unsure how to exit.
319    bool maximized = (width <= -1);
[3db0798]320    if (width <= 0 || height <= 0) {
321        pos.x = geom.x;
322        pos.y = geom.y;
323        width = geom.width;
324        height = geom.height;
[486ae6a]325
[3db0798]326        // Calculate a reasonable size for our window.
327        pos.x += width / 8;
328        pos.y += height / 8;
329        width = width * 3 / 4;
330        height = height * 3 / 4;
[4909e6bc]331    } else {
332        // Impose a minimum size for sanity, and make sure the window fits on
333        // the display (in case the current display is smaller than the one
334        // in use when the window size was saved).  (480x320) is about the
335        // smallest usable size for aven's window.
336        const int min_width = min(geom.width, 480);
337        const int min_height = min(geom.height, 320);
338        if (width < min_width || height < min_height) {
339            if (width < min_width) {
340                width = min_width;
341            }
342            if (height < min_height) {
343                height = min_height;
344            }
345            pos.x = geom.x + (geom.width - width) / 4;
346            pos.y = geom.y + (geom.height - height) / 4;
347        }
[3db0798]348    }
[486ae6a]349
350    // Create the main window.
[3db0798]351    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
352
[30621f5]353    // Select maximised if that's the saved state.
354    if (maximized) {
[3db0798]355        m_Frame->Maximize();
356    }
[068b4f2]357
[5440c295]358    if (utf8_argv[optind]) {
[668b930]359        if (!opt_survey) opt_survey = "";
360        m_Frame->OpenFile(fnm, wxString(opt_survey, wxConvUTF8));
[d07944e]361    }
[068b4f2]362
[3ee64fb]363    if (print_and_exit) {
[4ed8154]364        m_Frame->PrintAndExit();
[3ee64fb]365        return true;
366    }
367
[5809313]368    m_Frame->Show(true);
[526775d]369#ifdef _WIN32
370    m_Frame->SetFocus();
371#endif
[0060ba5]372    return true;
373}
374
[e0ffc2c]375wxPageSetupDialogData *
376Aven::GetPageSetupDialogData()
377{
[2c1d2f4]378    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
[e0ffc2c]379#ifdef __WXGTK__
380    // Fetch paper margins stored on disk.
381    int left, right, top, bottom;
382    wxConfigBase * cfg = wxConfigBase::Get();
383    // These default margins were chosen by looking at all the .ppd files
384    // on my machine.
[5627cbb]385    cfg->Read(wxT("paper_margin_left"), &left, 7);
386    cfg->Read(wxT("paper_margin_right"), &right, 7);
387    cfg->Read(wxT("paper_margin_top"), &top, 14);
388    cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
[2c1d2f4]389    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
390    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
[e0ffc2c]391#endif
[2c1d2f4]392    return m_pageSetupData;
[e0ffc2c]393}
394
395void
396Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
397{
[2c1d2f4]398    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
399    *m_pageSetupData = psdd;
[e0ffc2c]400#ifdef __WXGTK__
401    wxPoint topleft = psdd.GetMarginTopLeft();
402    wxPoint bottomright = psdd.GetMarginBottomRight();
403
404    // Store user specified paper margins on disk/in registry.
405    wxConfigBase * cfg = wxConfigBase::Get();
[5627cbb]406    cfg->Write(wxT("paper_margin_left"), topleft.x);
407    cfg->Write(wxT("paper_margin_right"), bottomright.x);
408    cfg->Write(wxT("paper_margin_top"), topleft.y);
409    cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
[e0ffc2c]410    cfg->Flush();
411#endif
412}
413
[ddc0994]414#ifdef __WXMAC__
415void
416Aven::MacOpenFiles(const wxArrayString & filenames)
417{
418    if (filenames.size() != 1) {
419        ReportError(wxT("Aven can only load one file at a time"));
420        return;
421    }
422    m_Frame->OpenFile(filenames[0], wxString());
423}
424
425void
426Aven::MacPrintFiles(const wxArrayString & filenames)
427{
428    if (filenames.size() != 1) {
429        ReportError(wxT("Aven can only print one file at a time"));
430        return;
431    }
432    m_Frame->OpenFile(filenames[0], wxString());
433    m_Frame->PrintAndExit();
434}
435#endif
436
[5809313]437void Aven::ReportError(const wxString& msg)
[0060ba5]438{
[c8b5fa5]439    if (!m_Frame) {
440        wxMessageBox(msg, APP_NAME, wxOK | wxICON_ERROR);
441        return;
442    }
[7015222]443    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
444    dlg.ShowModal();
[0060ba5]445}
[59fda0a]446
[8a05a7a]447const wxString &
448wmsg_cfgpth()
449{
450    static wxString path;
451    if (path.empty())
452        path = wxString(msg_cfgpth(), wxConvUTF8);
453    return path;
454}
455
[a9a32f2]456// called to report errors by message.c
457extern "C" void
458aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
459{
[c49e27f]460    wxString m;
461    if (fnm) {
462        m = wxString(fnm, wxConvUTF8);
463        if (line) m += wxString::Format(wxT(":%d"), line);
464        m += wxT(": ");
465    }
466
[37d6b84]467    if (severity == DIAG_WARN) {
[c49e27f]468        m += wmsg(/*warning*/4);
469        m += wxT(": ");
470    }
471
472    char buf[1024];
[dba56bb]473#ifdef HAVE__VSPRINTF_P
474    // Microsoft's vsnprintf() doesn't support positional argument specifiers,
475    // so we need to use the Microsoft-specific _vsprintf_p() which (despite
476    // its name) takes a buffer size like vsnprintf() does.
477    _vsprintf_p(buf, sizeof(buf), msg(en), ap);
478#else
[9b5f251]479    vsnprintf(buf, sizeof(buf), msg(en), ap);
[dba56bb]480#endif
[c49e27f]481    m += wxString(buf, wxConvUTF8);
482    if (wxTheApp == NULL) {
483        // We haven't initialised the Aven app object yet.
484        if (!wxInitialize()) {
485            fputs(buf, stderr);
486            PUTC('\n', stderr);
487            exit(1);
488        }
489        wxMessageBox(m, APP_NAME, wxOK | wxICON_ERROR);
490        wxUninitialize();
491    } else {
492        wxGetApp().ReportError(m);
493    }
[a9a32f2]494}
Note: See TracBrowser for help on using the repository browser.