source: git/src/aven.cc @ f1d5d26

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

src/aven.cc: Fix to build with wx 2.9.5 with wx2.8 compatibility
disabled.

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