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

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
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
RevLine 
[0060ba5]1//
[203d2a7]2//  aven.cc
[0060ba5]3//
[5809313]4//  Main class for Aven.
[0060ba5]5//
[3675a18]6//  Copyright (C) 2001 Mark R. Shinwell.
[5892e08]7//  Copyright (C) 2002,2003,2004,2005,2006 Olly Betts
[0060ba5]8//
[89231c4]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.
[0060ba5]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
[89231c4]16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
[0060ba5]18//
[89231c4]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
[5809313]22//
[0060ba5]23
[b462168]24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
[5809313]28#include "aven.h"
[78924eb]29#include "log.h"
[5809313]30#include "mainfrm.h"
[706b033]31
[d07944e]32#include "cmdline.h"
[93c3f97]33#include "message.h"
[0060ba5]34
35#include <assert.h>
[de7a879]36#include <signal.h>
[0060ba5]37
[e0ffc2c]38#include <wx/confbase.h>
[573f4e9]39#include <wx/image.h>
[e0ffc2c]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).
[ca18348]43#include <wx/display.h>
44#endif
[573f4e9]45
[879e9c3]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
[5627cbb]64#ifdef __WXMSW__
[5809313]65IMPLEMENT_APP(Aven)
[5627cbb]66#else
67IMPLEMENT_APP_NO_MAIN(Aven)
68IMPLEMENT_WX_THEME_SUPPORT
69#endif
[0060ba5]70
[5809313]71Aven::Aven() :
[2c1d2f4]72    m_Frame(NULL), m_pageSetupData(NULL)
[0060ba5]73{
[1f81f3d]74    wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
[0060ba5]75}
76
[2c1d2f4]77Aven::~Aven()
78{
79    if (m_pageSetupData) delete m_pageSetupData;
80}
81
[5627cbb]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);
[f1cbf74]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);
[5627cbb]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);
[f1cbf74]106}
[5627cbb]107#define real_argv argv
108#else
109static char ** real_argv;
[f1cbf74]110
[5627cbb]111int main(int argc, char **argv)
[0060ba5]112{
[27b8b59]113#ifdef __WXMAC__
[b72f4b5]114    // Tell wxMac which the About and Quit menu items are so they can be put
115    // where MacOS users expect them to be.
[27b8b59]116    wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT;
[b72f4b5]117    wxApp::s_macExitMenuItemId = menu_FILE_QUIT;
[879e9c3]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.
[5627cbb]122    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
123        --argc;
124        memmove(argv + 1, argv + 2, argc * sizeof(char *));
[879e9c3]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.
[5627cbb]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);
[879e9c3]136    getopt_first_response = cmdline_getopt();
[5627cbb]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);
[879e9c3]151}
[5627cbb]152#endif
[879e9c3]153
154bool Aven::OnInit()
155{
[4013d9c]156    wxLog::SetActiveTarget(new MyLogWindow());
[421b7d2]157
[003d953]158    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
[d607cea]159    {
[71ea9e1]160        // suppress message box warnings about messages not found
161        wxLogNull logNo;
162        wxLocale *loc = new wxLocale();
[5627cbb]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)) {
[d607cea]167            if (lang && strcmp(lang, "sk") == 0) {
[1324d6c]168               // As of 2.6.3, wxWidgets has cs but not sk - the two languages
[510ac63]169               // are close, so this makes sense...
[5627cbb]170               loc->Init(wxT("cs"), wxT("cs"), wxT("cs"), TRUE, TRUE);
[510ac63]171            }
172        }
[71ea9e1]173        // The existence of the wxLocale object is enough - no need to keep a
174        // pointer to it!
[003d953]175    }
[93c3f97]176
[d07944e]177    wxString survey;
[3ee64fb]178    bool print_and_exit = false;
[421b7d2]179
[0580c6a]180    while (true) {
[879e9c3]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        }
[d07944e]188        if (opt == EOF) break;
189        if (opt == 's') {
[5627cbb]190            survey = wxString(optarg, wxConvUTF8);
[d07944e]191        }
[3ee64fb]192        if (opt == 'p') {
193            print_and_exit = true;
194        }
195    }
196
[5627cbb]197    if (print_and_exit && !real_argv[optind]) {
[3ee64fb]198        cmdline_syntax(); // FIXME : not a helpful error...
199        exit(1);
[d07944e]200    }
[068b4f2]201
[5627cbb]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
[5892e08]211    if (!InitGLVisual(NULL)) {
212        wxString m;
[5627cbb]213        m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME);
[5892e08]214        wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
215        exit(1);
216    }
217
[573f4e9]218    wxImage::AddHandler(new wxPNGHandler);
[08d2074]219
[486ae6a]220    // Obtain the screen size.
[3db0798]221    wxPoint pos(wxDefaultPosition);
[ca18348]222    int width, height;
[5627cbb]223    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
224    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
[3db0798]225    bool maximized = (width == -1);
226    bool full_screen = (width <= -2);
227    if (width <= 0 || height <= 0) {
[0329df78]228#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
[3db0798]229        wxRect geom = wxDisplay().GetGeometry();
230        pos.x = geom.x;
231        pos.y = geom.y;
232        width = geom.width;
233        height = geom.height;
[ca18348]234#else
[3db0798]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        }
[ca18348]243#endif
[486ae6a]244
[3db0798]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    }
[486ae6a]251
252    // Create the main window.
[3db0798]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    }
[068b4f2]261
[5627cbb]262    if (real_argv[optind]) {
263        m_Frame->OpenFile(fnm, survey);
[d07944e]264    }
[068b4f2]265
[3ee64fb]266    if (print_and_exit) {
267        wxCommandEvent dummy;
268        m_Frame->OnPrint(dummy);
269        m_Frame->OnQuit(dummy);
270        return true;
271    }
272
[5809313]273    m_Frame->Show(true);
[526775d]274#ifdef _WIN32
275    m_Frame->SetFocus();
276#endif
[0060ba5]277    return true;
278}
279
[e0ffc2c]280wxPageSetupDialogData *
281Aven::GetPageSetupDialogData()
282{
[2c1d2f4]283    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
[e0ffc2c]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.
[5627cbb]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);
[2c1d2f4]294    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
295    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
[e0ffc2c]296#endif
[2c1d2f4]297    return m_pageSetupData;
[e0ffc2c]298}
299
300void
301Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
302{
[2c1d2f4]303    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
304    *m_pageSetupData = psdd;
[e0ffc2c]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();
[5627cbb]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);
[e0ffc2c]315    cfg->Flush();
316#endif
317}
318
[5809313]319void Aven::ReportError(const wxString& msg)
[0060ba5]320{
[1c6b20d7]321    AvenAllowOnTop ontop(m_Frame);
[7015222]322    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
323    dlg.ShowModal();
[0060ba5]324}
[59fda0a]325
[5627cbb]326wxString
327wmsg(int msg_no)
328{
329    return wxString(msg(msg_no), wxConvUTF8);
330}
331
[a9a32f2]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) {
[5627cbb]338       m = wxString(fnm, wxConvUTF8);
339       if (line) m += wxString::Format(wxT(":%d"), line);
340       m += wxT(": ");
[a9a32f2]341   }
342
343   if (severity == 0) {
[5627cbb]344       m += wmsg(/*warning*/4);
345       m += wxT(": ");
[a9a32f2]346   }
347
[5627cbb]348   m += wxString::FormatV(wmsg(en), ap);
[a9a32f2]349   wxGetApp().ReportError(m);
350}
Note: See TracBrowser for help on using the repository browser.