source: git/src/aven.cc @ 42c7efe

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

lib/codes.po,src/aven.cc,src/mainfrm.cc,src/mainfrm.h: Use more stock
IDs.

git-svn-id: file:///home/survex-svn/survex/trunk@3610 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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