source: git/src/aven.cc @ 5627cbb

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 5627cbb was 5627cbb, checked in by Olly Betts <olly@…>, 14 years ago
  • Fix to build with a "unicode" build of wx.
  • Add "Copy" button to the About dialog to copy the system info to the clipboard.
  • List OpenGL extensions last, since there are usually lots of them with a modern gfx card.
  • When processing survey data, auto-scroll the log window until we've reported a warning or error.
  • Put the survey data log window in a splitter in the standard frame rather than having a separate frame for it.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@3356 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 9.7 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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    // Tell wxMac which the About and Quit menu items are so they can be put
115    // where MacOS users expect them to be.
116    wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT;
117    wxApp::s_macExitMenuItemId = menu_FILE_QUIT;
118
119    // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
120    // wx ignores for us, but in wxApp::Initialize() which hasn't been
121    // called yet.  So we need to remove it ourselves.
122    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
123        --argc;
124        memmove(argv + 1, argv + 2, argc * sizeof(char *));
125    }
126#endif
127    // Call msg_init() and start processing the command line first so that
128    // we can respond to --help and --version even without an X display.
129    msg_init(argv);
130    select_charset(CHARSET_UTF8);
131    /* Want --version and a decent --help output, which cmdline does for us.
132     * wxCmdLine is much less good.
133     */
134    cmdline_set_syntax_message("[3d file]", NULL);
135    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
136    getopt_first_response = cmdline_getopt();
137
138    real_argv = argv;
139
140    wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0]));
141    wxChar * wargv[2];
142    if (buf) {
143        wargv[0] = wxStrdup(buf);
144    } else {
145        // Eep - couldn't convert the executable's name to wide characters!
146        wargv[0] = wxStrdup(APP_NAME);
147    }
148    wargv[1] = NULL;
149    int wargc = 1;
150    return wxEntry(wargc, wargv);
151}
152#endif
153
154bool Aven::OnInit()
155{
156    wxLog::SetActiveTarget(new MyLogWindow());
157
158    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
159    {
160        // suppress message box warnings about messages not found
161        wxLogNull logNo;
162        wxLocale *loc = new wxLocale();
163        loc->AddCatalogLookupPathPrefix(wxString(msg_cfgpth(), wxConvUTF8));
164        if (!loc->Init(wxString(msg_lang, wxConvUTF8),
165                       wxString(lang, wxConvUTF8),
166                       wxString(msg_lang, wxConvUTF8), TRUE, TRUE)) {
167            if (lang && strcmp(lang, "sk") == 0) {
168               // As of 2.6.3, wxWidgets has cs but not sk - the two languages
169               // are close, so this makes sense...
170               loc->Init(wxT("cs"), wxT("cs"), wxT("cs"), TRUE, TRUE);
171            }
172        }
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 && !real_argv[optind]) {
198        cmdline_syntax(); // FIXME : not a helpful error...
199        exit(1);
200    }
201
202    wxString fnm;
203    if (real_argv[optind]) {
204        fnm = wxString(real_argv[optind], wxConvUTF8);
205        if (fnm.empty() && *(real_argv[optind])) {
206            wxGetApp().ReportError(wxT("File argument's filename has bad encoding"));
207            return false;
208        }
209    }
210
211    if (!InitGLVisual(NULL)) {
212        wxString m;
213        m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME);
214        wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
215        exit(1);
216    }
217
218    wxImage::AddHandler(new wxPNGHandler);
219
220    // Obtain the screen size.
221    wxPoint pos(wxDefaultPosition);
222    int width, height;
223    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
224    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
225    bool maximized = (width == -1);
226    bool full_screen = (width <= -2);
227    if (width <= 0 || height <= 0) {
228#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
229        wxRect geom = wxDisplay().GetGeometry();
230        pos.x = geom.x;
231        pos.y = geom.y;
232        width = geom.width;
233        height = geom.height;
234#else
235        wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
236        // Crude fix to help behaviour on multi-monitor displays.
237        // Fudge factors are a bit specific to my setup...
238        if (width > height * 3 / 2) {
239            pos.x += width;
240            width = height * 3 / 2;
241            pos.x -= width;
242        }
243#endif
244
245        // Calculate a reasonable size for our window.
246        pos.x += width / 8;
247        pos.y += height / 8;
248        width = width * 3 / 4;
249        height = height * 3 / 4;
250    }
251
252    // Create the main window.
253    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
254
255    // Select full_screen or maximised if that's the saved state.
256    if (full_screen) {
257        m_Frame->ShowFullScreen(true);
258    } else if (maximized) {
259        m_Frame->Maximize();
260    }
261
262    if (real_argv[optind]) {
263        m_Frame->OpenFile(fnm, survey);
264    }
265
266    if (print_and_exit) {
267        wxCommandEvent dummy;
268        m_Frame->OnPrint(dummy);
269        m_Frame->OnQuit(dummy);
270        return true;
271    }
272
273    m_Frame->Show(true);
274#ifdef _WIN32
275    m_Frame->SetFocus();
276#endif
277    return true;
278}
279
280wxPageSetupDialogData *
281Aven::GetPageSetupDialogData()
282{
283    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
284#ifdef __WXGTK__
285    // Fetch paper margins stored on disk.
286    int left, right, top, bottom;
287    wxConfigBase * cfg = wxConfigBase::Get();
288    // These default margins were chosen by looking at all the .ppd files
289    // on my machine.
290    cfg->Read(wxT("paper_margin_left"), &left, 7);
291    cfg->Read(wxT("paper_margin_right"), &right, 7);
292    cfg->Read(wxT("paper_margin_top"), &top, 14);
293    cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
294    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
295    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
296#endif
297    return m_pageSetupData;
298}
299
300void
301Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
302{
303    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
304    *m_pageSetupData = psdd;
305#ifdef __WXGTK__
306    wxPoint topleft = psdd.GetMarginTopLeft();
307    wxPoint bottomright = psdd.GetMarginBottomRight();
308
309    // Store user specified paper margins on disk/in registry.
310    wxConfigBase * cfg = wxConfigBase::Get();
311    cfg->Write(wxT("paper_margin_left"), topleft.x);
312    cfg->Write(wxT("paper_margin_right"), bottomright.x);
313    cfg->Write(wxT("paper_margin_top"), topleft.y);
314    cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
315    cfg->Flush();
316#endif
317}
318
319void Aven::ReportError(const wxString& msg)
320{
321    AvenAllowOnTop ontop(m_Frame);
322    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
323    dlg.ShowModal();
324}
325
326wxString
327wmsg(int msg_no)
328{
329    return wxString(msg(msg_no), wxConvUTF8);
330}
331
332// called to report errors by message.c
333extern "C" void
334aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
335{
336   wxString m;
337   if (fnm) {
338       m = wxString(fnm, wxConvUTF8);
339       if (line) m += wxString::Format(wxT(":%d"), line);
340       m += wxT(": ");
341   }
342
343   if (severity == 0) {
344       m += wmsg(/*warning*/4);
345       m += wxT(": ");
346   }
347
348   m += wxString::FormatV(wmsg(en), ap);
349   wxGetApp().ReportError(m);
350}
Note: See TracBrowser for help on using the repository browser.