source: git/src/aven.cc @ 6c9c7f0

RELEASE/1.0
Last change on this file since 6c9c7f0 was 6c9c7f0, checked in by Olly Betts <olly@…>, 14 years ago

NEWS,src/aven.cc: Make error dialog modal (backport from 1.1.13).

git-svn-id: file:///home/survex-svn/survex/branches/1.0@3464 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 6.8 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 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 "mainfrm.h"
30
31#include "cmdline.h"
32#include "message.h"
33
34#include <assert.h>
35#include <signal.h>
36
37#include <wx/confbase.h>
38#include <wx/image.h>
39#if wxUSE_DISPLAY
40// wxDisplay was added in wx 2.5; but it may not be built for mingw (because
41// the header seems to be missing).
42#include <wx/display.h>
43#endif
44
45IMPLEMENT_APP(Aven)
46
47Aven::Aven() :
48    m_Frame(NULL), m_pageSetupData(NULL)
49{
50}
51
52Aven::~Aven()
53{
54    if (m_pageSetupData) delete m_pageSetupData;
55}
56
57bool Aven::OnInit()
58{
59#ifdef __WXMAC__
60    // Tell wxMac which the About menu item is so it can be put where MacOS
61    // users expect it to be.
62    wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT;
63    // wxMac is supposed to remove this magic command line option (which
64    // Finder passes), but the code in 2.4.2 is bogus.  It just decrements
65    // argc rather than copying argv down.  But luckily it also fails to
66    // set argv[argc] to NULL so we can just recalculate argc, then remove
67    // the -psn_* switch ourselves.
68    if (argv[argc]) {
69        argc = 1;
70        while (argv[argc]) ++argc;
71    }
72    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
73        --argc;
74        memmove(argv + 1, argv + 2, argc * sizeof(char *));
75    }
76#endif
77    msg_init(argv);
78
79    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
80    if (lang) {
81        // suppress message box warnings about messages not found
82        wxLogNull logNo;
83        wxLocale *loc = new wxLocale();
84        loc->AddCatalogLookupPathPrefix(msg_cfgpth());
85        if (!loc->Init(lang, lang, lang, TRUE, TRUE)) {
86            if (strcmp(lang, "sk") == 0) {
87               // As of 2.2.9, wxWidgets has cs but not sk - the two languages
88               // are close, so this makes sense...
89               loc->Init("cs", "cs", "cs", TRUE, TRUE);
90            }
91        }
92        // The existence of the wxLocale object is enough - no need to keep a
93        // pointer to it!
94    }
95
96    wxString survey;
97    bool print_and_exit = false;
98
99    /* Want --version and a decent --help output, which cmdline does for us.
100     * wxCmdLine is much less good.
101     */
102    static const struct option long_opts[] = {
103        /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
104        {"survey", required_argument, 0, 's'},
105        {"print", no_argument, 0, 'p'},
106        {"help", no_argument, 0, HLP_HELP},
107        {"version", no_argument, 0, HLP_VERSION},
108        {0, 0, 0, 0}
109    };
110
111#define short_opts "s:p"
112
113    static struct help_msg help[] = {
114        /*                           <-- */
115        {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
116        {HLP_ENCODELONG(1),          "print and exit (requires a 3d file)"},
117        {0, 0}
118    };
119
120    cmdline_set_syntax_message("[3d file]", NULL);
121    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
122    while (true) {
123        int opt = cmdline_getopt();
124        if (opt == EOF) break;
125        if (opt == 's') {
126            survey = optarg;
127        }
128        if (opt == 'p') {
129            print_and_exit = true;
130        }
131    }
132
133    if (print_and_exit && !argv[optind]) {
134        cmdline_syntax(); // FIXME : not a helpful error...
135        exit(1);
136    }
137
138    wxImage::AddHandler(new wxPNGHandler);
139
140    // Obtain the screen size.
141    int x, y;
142    int width, height;
143#if wxUSE_DISPLAY // wxDisplay was added in wx 2.5
144    wxRect geom = wxDisplay().GetGeometry();
145    x = geom.x;
146    y = geom.y;
147    width = geom.width;
148    height = geom.height;
149#else
150    wxClientDisplayRect(&x, &y, &width, &height);
151    // Crude fix to help behaviour on multi-monitor displays.
152    // Fudge factors are a bit specific to my setup...
153    if (width > height * 3 / 2) {
154        x += width;
155        width = height * 3 / 2;
156        x -= width;
157    }
158#endif
159
160    // Calculate a reasonable size for our window.
161    x += width / 8;
162    y += height / 8;
163    width = width * 3 / 4;
164    height = height * 3 / 4;
165
166    // Create the main window.
167    m_Frame = new MainFrm(APP_NAME, wxPoint(x, y), wxSize(width, height));
168
169    if (argv[optind]) {
170        m_Frame->OpenFile(wxString(argv[optind]), survey, true);
171    }
172
173    if (print_and_exit) {
174        wxCommandEvent dummy;
175        m_Frame->OnPrint(dummy);
176        m_Frame->OnQuit(dummy);
177        return true;
178    }
179
180    m_Frame->Show(true);
181#ifdef _WIN32
182    m_Frame->SetFocus();
183#endif
184    return true;
185}
186
187wxPageSetupDialogData *
188Aven::GetPageSetupDialogData()
189{
190    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
191#ifdef __WXGTK__
192    // Fetch paper margins stored on disk.
193    int left, right, top, bottom;
194    wxConfigBase * cfg = wxConfigBase::Get();
195    // These default margins were chosen by looking at all the .ppd files
196    // on my machine.
197    cfg->Read("paper_margin_left", &left, 7);
198    cfg->Read("paper_margin_right", &right, 7);
199    cfg->Read("paper_margin_top", &top, 14);
200    cfg->Read("paper_margin_bottom", &bottom, 14);
201    m_pageSetupData->SetMarginTopLeft(wxPoint(left, top));
202    m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom));
203#endif
204    return m_pageSetupData;
205}
206
207void
208Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd)
209{
210    if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData;
211    *m_pageSetupData = psdd;
212#ifdef __WXGTK__
213    wxPoint topleft = psdd.GetMarginTopLeft();
214    wxPoint bottomright = psdd.GetMarginBottomRight();
215
216    // Store user specified paper margins on disk/in registry.
217    wxConfigBase * cfg = wxConfigBase::Get();
218    cfg->Write("paper_margin_left", topleft.x);
219    cfg->Write("paper_margin_right", bottomright.x);
220    cfg->Write("paper_margin_top", topleft.y);
221    cfg->Write("paper_margin_bottom", bottomright.y);
222    cfg->Flush();
223#endif
224}
225
226void Aven::ReportError(const wxString& msg)
227{
228    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
229    dlg.ShowModal();
230}
231
232// called to report errors by message.c
233extern "C" void
234aven_v_report(int severity, const char *fnm, int line, int en, va_list ap)
235{
236   wxString m;
237   if (fnm) {
238      m = fnm;
239      if (line) m += wxString::Format(":%d", line);
240      m += ": ";
241   }
242
243   if (severity == 0) {
244      m += msg(/*warning*/4);
245      m += ": ";
246   }
247
248   wxString s;
249   s.PrintfV(msg(en), ap);
250   m += s;
251   wxGetApp().ReportError(m);
252}
Note: See TracBrowser for help on using the repository browser.