source: git/src/aven.cc @ 5440c295

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 5440c295 was 5440c295, checked in by Olly Betts <olly@…>, 10 years ago

src/aven.cc: Simplify handling of command line under WXMSW,
fixing error if run with no parameters.

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