source: git/src/aven.cc @ c72bb58

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

Makefile.am,src/Makefile.am,src/aven.cc,src/message.c: Fix up the
handling of bundled epsg and esri files to work.

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