source: git/src/aven.cc @ 1599605

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 1599605 was f1cbf74, checked in by Olly Betts <olly@…>, 17 years ago

(Unix version): aven: Fix display of degree symbol in status bar and messages
with accented characters in.
(Mac OS X version): Require at least wxWidgets 2.5.1.

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

  • Property mode set to 100644
File size: 8.2 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
64IMPLEMENT_APP(Aven)
65
66Aven::Aven() :
67    m_Frame(NULL), m_pageSetupData(NULL)
68{
69    wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8);
70}
71
72Aven::~Aven()
73{
74    if (m_pageSetupData) delete m_pageSetupData;
75}
76
77static void init_msg_and_cmdline(int& my_argc, char **my_argv) {
78    msg_init(my_argv);
79    select_charset(CHARSET_UTF8);
80    /* Want --version and a decent --help output, which cmdline does for us.
81     * wxCmdLine is much less good.
82     */
83    cmdline_set_syntax_message("[3d file]", NULL);
84    cmdline_init(my_argc, my_argv, short_opts, long_opts, NULL, help, 0, 1);
85}
86
87#if wxCHECK_VERSION(2,5,1)
88static int getopt_first_response = 0;
89
90bool Aven::Initialize(int& my_argc, wxChar **my_argv)
91{
92#ifdef __WXMAC__
93    // Tell wxMac which the About and Quit menu items are so they can be put
94    // where MacOS users expect them to be.
95    wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT;
96    wxApp::s_macExitMenuItemId = menu_FILE_QUIT;
97
98    // MacOS passes a magic -psn_XXXX command line argument in argv[1] which
99    // wx ignores for us, but in wxApp::Initialize() which hasn't been
100    // called yet.  So we need to remove it ourselves.
101    if (my_argc > 1 && strncmp(my_argv[1], "-psn_", 5) == 0) {
102        --my_argc;
103        memmove(my_argv + 1, my_argv + 2, my_argc * sizeof(char *));
104    }
105#endif
106    // Call msg_init() and start processing the command line first so that
107    // we can respond to --help and --version even without an X display.
108    init_msg_and_cmdline(my_argc, my_argv);
109    getopt_first_response = cmdline_getopt();
110    return wxApp::Initialize(my_argc, my_argv);
111}
112#endif
113
114bool Aven::OnInit()
115{
116    wxLog::SetActiveTarget(new MyLogWindow());
117
118#if !wxCHECK_VERSION(2,5,1)
119    init_msg_and_cmdline(argc, argv);
120#endif
121
122    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
123    {
124        // suppress message box warnings about messages not found
125        wxLogNull logNo;
126        wxLocale *loc = new wxLocale();
127        loc->AddCatalogLookupPathPrefix(msg_cfgpth());
128        if (!loc->Init(msg_lang, lang, msg_lang, TRUE, TRUE)) {
129            if (lang && strcmp(lang, "sk") == 0) {
130               // As of 2.6.3, wxWindows has cs but not sk - the two languages
131               // are close, so this makes sense...
132               loc->Init("cs", "cs", "cs", TRUE, TRUE);
133            }
134        }
135        // The existence of the wxLocale object is enough - no need to keep a
136        // pointer to it!
137    }
138
139    wxString survey;
140    bool print_and_exit = false;
141
142    while (true) {
143        int opt;
144#if wxCHECK_VERSION(2,5,1)
145        if (getopt_first_response) {
146            opt = getopt_first_response;
147            getopt_first_response = 0;
148        } else {
149            opt = cmdline_getopt();
150        }
151#else
152        opt = cmdline_getopt();
153#endif
154        if (opt == EOF) break;
155        if (opt == 's') {
156            survey = optarg;
157        }
158        if (opt == 'p') {
159            print_and_exit = true;
160        }
161    }
162
163    if (print_and_exit && !argv[optind]) {
164        cmdline_syntax(); // FIXME : not a helpful error...
165        exit(1);
166    }
167
168    if (!InitGLVisual(NULL)) {
169        wxString m;
170        m.Printf(msg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME);
171        wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
172        exit(1);
173    }
174
175    wxImage::AddHandler(new wxPNGHandler);
176
177    // Obtain the screen size.
178    wxPoint pos(wxDefaultPosition);
179    int width, height;
180    wxConfigBase::Get()->Read("width", &width, 0);
181    if (width > 0) wxConfigBase::Get()->Read("height", &height, 0);
182    bool maximized = (width == -1);
183    bool full_screen = (width <= -2);
184    if (width <= 0 || height <= 0) {
185#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
186        wxRect geom = wxDisplay().GetGeometry();
187        pos.x = geom.x;
188        pos.y = geom.y;
189        width = geom.width;
190        height = geom.height;
191#else
192        wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
193        // Crude fix to help behaviour on multi-monitor displays.
194        // Fudge factors are a bit specific to my setup...
195        if (width > height * 3 / 2) {
196            pos.x += width;
197            width = height * 3 / 2;
198            pos.x -= width;
199        }
200#endif
201
202        // Calculate a reasonable size for our window.
203        pos.x += width / 8;
204        pos.y += height / 8;
205        width = width * 3 / 4;
206        height = height * 3 / 4;
207    }
208
209    // Create the main window.
210    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
211
212    // Select full_screen or maximised if that's the saved state.
213    if (full_screen) {
214        m_Frame->ShowFullScreen(true);
215    } else if (maximized) {
216        m_Frame->Maximize();
217    }
218
219    if (argv[optind]) {
220        m_Frame->OpenFile(wxString(argv[optind]), survey);
221    }
222
223    if (print_and_exit) {
224        wxCommandEvent dummy;
225        m_Frame->OnPrint(dummy);
226        m_Frame->OnQuit(dummy);
227        return true;
228    }
229
230    m_Frame->Show(true);
231#ifdef _WIN32
232    m_Frame->SetFocus();
233#endif
234    return true;
235}
236
237wxPageSetupDialogData *
238Aven::GetPageSetupDialogData()
239{
240    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
241#ifdef __WXGTK__
242    // Fetch paper margins stored on disk.
243    int left, right, top, bottom;
244    wxConfigBase * cfg = wxConfigBase::Get();
245    // These default margins were chosen by looking at all the .ppd files
246    // on my machine.
247    cfg->Read("paper_margin_left", &left, 7);
248    cfg->Read("paper_margin_right", &right, 7);
249    cfg->Read("paper_margin_top", &top, 14);
250    cfg->Read("paper_margin_bottom", &bottom, 14);
251    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
252    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
253#endif
254    return m_pageSetupData;
255}
256
257void
258Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
259{
260    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
261    *m_pageSetupData = psdd;
262#ifdef __WXGTK__
263    wxPoint topleft = psdd.GetMarginTopLeft();
264    wxPoint bottomright = psdd.GetMarginBottomRight();
265
266    // Store user specified paper margins on disk/in registry.
267    wxConfigBase * cfg = wxConfigBase::Get();
268    cfg->Write("paper_margin_left", topleft.x);
269    cfg->Write("paper_margin_right", bottomright.x);
270    cfg->Write("paper_margin_top", topleft.y);
271    cfg->Write("paper_margin_bottom", bottomright.y);
272    cfg->Flush();
273#endif
274}
275
276void Aven::ReportError(const wxString& msg)
277{
278    AvenAllowOnTop ontop(m_Frame);
279    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
280    dlg.ShowModal();
281}
282
283// called to report errors by message.c
284extern "C" void
285aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
286{
287   wxString m;
288   if (fnm) {
289      m = fnm;
290      if (line) m += wxString::Format(":%d", line);
291      m += ": ";
292   }
293
294   if (severity == 0) {
295      m += msg(/*warning*/4);
296      m += ": ";
297   }
298
299   wxString s;
300   s.PrintfV(msg(en), ap);
301   m += s;
302   wxGetApp().ReportError(m);
303}
Note: See TracBrowser for help on using the repository browser.