source: git/src/aven.cc @ ecbc6c18

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

src/: Update FSF address in licence notices.

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

  • Property mode set to 100644
File size: 9.6 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    // 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    {
159        // Suppress message box warnings about messages not found.
160        wxLogNull logNo;
161        wxLocale *loc = new wxLocale();
162        loc->AddCatalogLookupPathPrefix(wmsg_cfgpth());
163        wxString msg_lang_str(msg_lang, wxConvUTF8);
164        const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
165        wxString lang_str(lang, wxConvUTF8);
166        loc->Init(msg_lang_str, lang_str, msg_lang_str, TRUE, TRUE);
167        // The existence of the wxLocale object is enough - no need to keep a
168        // pointer to it!
169    }
170
171    wxString survey;
172    bool print_and_exit = false;
173
174    while (true) {
175        int opt;
176        if (getopt_first_response) {
177            opt = getopt_first_response;
178            getopt_first_response = 0;
179        } else {
180            opt = cmdline_getopt();
181        }
182        if (opt == EOF) break;
183        if (opt == 's') {
184            survey = wxString(optarg, wxConvUTF8);
185        }
186        if (opt == 'p') {
187            print_and_exit = true;
188        }
189    }
190
191    if (print_and_exit && !real_argv[optind]) {
192        cmdline_syntax(); // FIXME : not a helpful error...
193        exit(1);
194    }
195
196    wxString fnm;
197    if (real_argv[optind]) {
198        fnm = wxString(real_argv[optind], wxConvUTF8);
199        if (fnm.empty() && *(real_argv[optind])) {
200            wxGetApp().ReportError(wxT("File argument's filename has bad encoding"));
201            return false;
202        }
203    }
204
205    if (!InitGLVisual(NULL)) {
206        wxString m;
207        m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME);
208        wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION);
209        exit(1);
210    }
211
212    wxImage::AddHandler(new wxPNGHandler);
213
214    // Obtain the screen size.
215    wxPoint pos(wxDefaultPosition);
216    int width, height;
217    wxConfigBase::Get()->Read(wxT("width"), &width, 0);
218    if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0);
219    bool maximized = (width == -1);
220    bool full_screen = (width <= -2);
221    if (width <= 0 || height <= 0) {
222#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
223        wxRect geom = wxDisplay().GetGeometry();
224        pos.x = geom.x;
225        pos.y = geom.y;
226        width = geom.width;
227        height = geom.height;
228#else
229        wxClientDisplayRect(&pos.x, &pos.y, &width, &height);
230        // Crude fix to help behaviour on multi-monitor displays.
231        // Fudge factors are a bit specific to my setup...
232        if (width > height * 3 / 2) {
233            pos.x += width;
234            width = height * 3 / 2;
235            pos.x -= width;
236        }
237#endif
238
239        // Calculate a reasonable size for our window.
240        pos.x += width / 8;
241        pos.y += height / 8;
242        width = width * 3 / 4;
243        height = height * 3 / 4;
244    }
245
246    // Create the main window.
247    m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height));
248
249    // Select full_screen or maximised if that's the saved state.
250    if (full_screen) {
251        m_Frame->ShowFullScreen(true);
252    } else if (maximized) {
253        m_Frame->Maximize();
254    }
255
256    if (real_argv[optind]) {
257        m_Frame->OpenFile(fnm, survey);
258    }
259
260    if (print_and_exit) {
261        wxCommandEvent dummy;
262        m_Frame->OnPrint(dummy);
263        m_Frame->OnQuit(dummy);
264        return true;
265    }
266
267    m_Frame->Show(true);
268#ifdef _WIN32
269    m_Frame->SetFocus();
270#endif
271    return true;
272}
273
274wxPageSetupDialogData *
275Aven::GetPageSetupDialogData()
276{
277    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
278#ifdef __WXGTK__
279    // Fetch paper margins stored on disk.
280    int left, right, top, bottom;
281    wxConfigBase * cfg = wxConfigBase::Get();
282    // These default margins were chosen by looking at all the .ppd files
283    // on my machine.
284    cfg->Read(wxT("paper_margin_left"), &left, 7);
285    cfg->Read(wxT("paper_margin_right"), &right, 7);
286    cfg->Read(wxT("paper_margin_top"), &top, 14);
287    cfg->Read(wxT("paper_margin_bottom"), &bottom, 14);
288    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
289    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
290#endif
291    return m_pageSetupData;
292}
293
294void
295Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
296{
297    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
298    *m_pageSetupData = psdd;
299#ifdef __WXGTK__
300    wxPoint topleft = psdd.GetMarginTopLeft();
301    wxPoint bottomright = psdd.GetMarginBottomRight();
302
303    // Store user specified paper margins on disk/in registry.
304    wxConfigBase * cfg = wxConfigBase::Get();
305    cfg->Write(wxT("paper_margin_left"), topleft.x);
306    cfg->Write(wxT("paper_margin_right"), bottomright.x);
307    cfg->Write(wxT("paper_margin_top"), topleft.y);
308    cfg->Write(wxT("paper_margin_bottom"), bottomright.y);
309    cfg->Flush();
310#endif
311}
312
313void Aven::ReportError(const wxString& msg)
314{
315    AvenAllowOnTop ontop(m_Frame);
316    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
317    dlg.ShowModal();
318}
319
320wxString
321wmsg(int msg_no)
322{
323    return wxString::FromUTF8(msg(msg_no));
324}
325
326const wxString &
327wmsg_cfgpth()
328{
329    static wxString path;
330    if (path.empty())
331        path = wxString(msg_cfgpth(), wxConvUTF8);
332    return path;
333}
334
335// called to report errors by message.c
336extern "C" void
337aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
338{
339   wxString m;
340   if (fnm) {
341       m = wxString(fnm, wxConvUTF8);
342       if (line) m += wxString::Format(wxT(":%d"), line);
343       m += wxT(": ");
344   }
345
346   if (severity == 0) {
347       m += wmsg(/*warning*/4);
348       m += wxT(": ");
349   }
350
351   m += wxString::FormatV(wmsg(en), ap);
352   wxGetApp().ReportError(m);
353}
Note: See TracBrowser for help on using the repository browser.