source: git/src/aven.cc @ 293eec8

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 293eec8 was 293eec8, checked in by Olly Betts <olly@…>, 13 years ago

src/aven.cc: Comment improvements.

git-svn-id: file:///home/survex-svn/survex/trunk@3636 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
[ecbc6c18]21//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
[c293aa9]46bool double_buffered = false;
47
[879e9c3]48static const struct option long_opts[] = {
49    /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
50    {"survey", required_argument, 0, 's'},
51    {"print", no_argument, 0, 'p'},
52    {"help", no_argument, 0, HLP_HELP},
53    {"version", no_argument, 0, HLP_VERSION},
54    {0, 0, 0, 0}
55};
56
57#define short_opts "s:p"
58
59static struct help_msg help[] = {
60    /*                       <-- */
61    {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
62    {HLP_ENCODELONG(1),          "print and exit (requires a 3d file)"},
63    {0, 0}
64};
65
[5627cbb]66#ifdef __WXMSW__
[5809313]67IMPLEMENT_APP(Aven)
[5627cbb]68#else
69IMPLEMENT_APP_NO_MAIN(Aven)
70IMPLEMENT_WX_THEME_SUPPORT
71#endif
[0060ba5]72
[5809313]73Aven::Aven() :
[2c1d2f4]74    m_Frame(NULL), m_pageSetupData(NULL)
[0060ba5]75{
[1f81f3d]76    wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
[0060ba5]77}
78
[2c1d2f4]79Aven::~Aven()
80{
81    if (m_pageSetupData) delete m_pageSetupData;
82}
83
[5627cbb]84static int getopt_first_response = 0;
85
86#ifdef __WXMSW__
87bool Aven::Initialize(int& my_argc, wxChar **my_argv)
88{
[293eec8]89    // wxWidgets passes us wxChars, which may be wide characters but cmdline
90    // wants UTF-8 so we need to convert.
[5627cbb]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);
[293eec8]99    /* Want --version and decent --help output, which cmdline does for us.
[f1cbf74]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__
[879e9c3]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.
[5627cbb]117    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
118        --argc;
119        memmove(argv + 1, argv + 2, argc * sizeof(char *));
[879e9c3]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.
[5627cbb]124    msg_init(argv);
125    select_charset(CHARSET_UTF8);
[293eec8]126    /* Want --version and decent --help output, which cmdline does for us.
[5627cbb]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);
[879e9c3]131    getopt_first_response = cmdline_getopt();
[5627cbb]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);
[879e9c3]146}
[5627cbb]147#endif
[879e9c3]148
149bool Aven::OnInit()
150{
[4013d9c]151    wxLog::SetActiveTarget(new MyLogWindow());
[421b7d2]152
[d607cea]153    {
[b1876d3]154        // Suppress message box warnings about messages not found.
[71ea9e1]155        wxLogNull logNo;
156        wxLocale *loc = new wxLocale();
[8a05a7a]157        loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
[b1876d3]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);
[71ea9e1]162        // The existence of the wxLocale object is enough - no need to keep a
163        // pointer to it!
[003d953]164    }
[93c3f97]165
[d07944e]166    wxString survey;
[3ee64fb]167    bool print_and_exit = false;
[421b7d2]168
[0580c6a]169    while (true) {
[879e9c3]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        }
[d07944e]177        if (opt == EOF) break;
178        if (opt == 's') {
[5627cbb]179            survey = wxString(optarg, wxConvUTF8);
[d07944e]180        }
[3ee64fb]181        if (opt == 'p') {
182            print_and_exit = true;
183        }
184    }
185
[5627cbb]186    if (print_and_exit && !real_argv[optind]) {
[3ee64fb]187        cmdline_syntax(); // FIXME : not a helpful error...
188        exit(1);
[d07944e]189    }
[068b4f2]190
[5627cbb]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
[c293aa9]200    // Use a double-buffered visual if available, as it will give much smoother
201    // animation.
202    double_buffered = true;
203    int wx_gl_attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
204    if (!InitGLVisual(wx_gl_attribs)) {
205        int wx_gl_attribs_no_db[] = { WX_GL_RGBA, 0 };
206        if (!InitGLVisual(wx_gl_attribs_no_db)) {
207            wxString m;
208            m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME);
209            wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
210            exit(1);
211        }
212        double_buffered = false;
[5892e08]213    }
214
[573f4e9]215    wxImage::AddHandler(new wxPNGHandler);
[08d2074]216
[486ae6a]217    // Obtain the screen size.
[3db0798]218    wxPoint pos(wxDefaultPosition);
[ca18348]219    int width, height;
[5627cbb]220    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
221    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
[3db0798]222    bool maximized = (width == -1);
223    bool full_screen = (width <= -2);
224    if (width <= 0 || height <= 0) {
[0329df78]225#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
[3db0798]226        wxRect geom = wxDisplay().GetGeometry();
227        pos.x = geom.x;
228        pos.y = geom.y;
229        width = geom.width;
230        height = geom.height;
[ca18348]231#else
[3db0798]232        wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
233        // Crude fix to help behaviour on multi-monitor displays.
234        // Fudge factors are a bit specific to my setup...
235        if (width > height * 3 / 2) {
236            pos.x += width;
237            width = height * 3 / 2;
238            pos.x -= width;
239        }
[ca18348]240#endif
[486ae6a]241
[3db0798]242        // Calculate a reasonable size for our window.
243        pos.x += width / 8;
244        pos.y += height / 8;
245        width = width * 3 / 4;
246        height = height * 3 / 4;
247    }
[486ae6a]248
249    // Create the main window.
[3db0798]250    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
251
252    // Select full_screen or maximised if that's the saved state.
253    if (full_screen) {
254        m_Frame->ShowFullScreen(true);
255    } else if (maximized) {
256        m_Frame->Maximize();
257    }
[068b4f2]258
[5627cbb]259    if (real_argv[optind]) {
260        m_Frame->OpenFile(fnm, survey);
[d07944e]261    }
[068b4f2]262
[3ee64fb]263    if (print_and_exit) {
264        wxCommandEvent dummy;
265        m_Frame->OnPrint(dummy);
266        m_Frame->OnQuit(dummy);
267        return true;
268    }
269
[5809313]270    m_Frame->Show(true);
[526775d]271#ifdef _WIN32
272    m_Frame->SetFocus();
273#endif
[0060ba5]274    return true;
275}
276
[e0ffc2c]277wxPageSetupDialogData *
278Aven::GetPageSetupDialogData()
279{
[2c1d2f4]280    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
[e0ffc2c]281#ifdef __WXGTK__
282    // Fetch paper margins stored on disk.
283    int left, right, top, bottom;
284    wxConfigBase * cfg = wxConfigBase::Get();
285    // These default margins were chosen by looking at all the .ppd files
286    // on my machine.
[5627cbb]287    cfg->Read(wxT("paper_margin_left"), &left, 7);
288    cfg->Read(wxT("paper_margin_right"), &right, 7);
289    cfg->Read(wxT("paper_margin_top"), &top, 14);
290    cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
[2c1d2f4]291    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
292    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
[e0ffc2c]293#endif
[2c1d2f4]294    return m_pageSetupData;
[e0ffc2c]295}
296
297void
298Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
299{
[2c1d2f4]300    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
301    *m_pageSetupData = psdd;
[e0ffc2c]302#ifdef __WXGTK__
303    wxPoint topleft = psdd.GetMarginTopLeft();
304    wxPoint bottomright = psdd.GetMarginBottomRight();
305
306    // Store user specified paper margins on disk/in registry.
307    wxConfigBase * cfg = wxConfigBase::Get();
[5627cbb]308    cfg->Write(wxT("paper_margin_left"), topleft.x);
309    cfg->Write(wxT("paper_margin_right"), bottomright.x);
310    cfg->Write(wxT("paper_margin_top"), topleft.y);
311    cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
[e0ffc2c]312    cfg->Flush();
313#endif
314}
315
[5809313]316void Aven::ReportError(const wxString& msg)
[0060ba5]317{
[1c6b20d7]318    AvenAllowOnTop ontop(m_Frame);
[7015222]319    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
320    dlg.ShowModal();
[0060ba5]321}
[59fda0a]322
[5627cbb]323wxString
324wmsg(int msg_no)
325{
[4056cdc]326#if !wxCHECK_VERSION(2,8,0)
327    return wxString(msg(msg_no), wxConvUTF8);
328#else
[833c6bc]329    return wxString::FromUTF8(msg(msg_no));
[4056cdc]330#endif
[5627cbb]331}
332
[8a05a7a]333const wxString &
334wmsg_cfgpth()
335{
336    static wxString path;
337    if (path.empty())
338        path = wxString(msg_cfgpth(), wxConvUTF8);
339    return path;
340}
341
[a9a32f2]342// called to report errors by message.c
343extern "C" void
344aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
345{
346   wxString m;
347   if (fnm) {
[5627cbb]348       m = wxString(fnm, wxConvUTF8);
349       if (line) m += wxString::Format(wxT(":%d"), line);
350       m += wxT(": ");
[a9a32f2]351   }
352
353   if (severity == 0) {
[5627cbb]354       m += wmsg(/*warning*/4);
355       m += wxT(": ");
[a9a32f2]356   }
357
[5627cbb]358   m += wxString::FormatV(wmsg(en), ap);
[a9a32f2]359   wxGetApp().ReportError(m);
360}
Note: See TracBrowser for help on using the repository browser.