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
RevLine 
[0060ba5]1//
[17aa816]2//  aven.cc
[0060ba5]3//
[5809313]4//  Main class for Aven.
[0060ba5]5//
[0eab709]6//  Copyright (C) 2001 Mark R. Shinwell.
[168df28]7//  Copyright (C) 2002,2003,2004,2005 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
[1b71c05]21//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[5809313]22//
[0060ba5]23
[17aa816]24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
[5809313]28#include "aven.h"
29#include "mainfrm.h"
[706b033]30
[d07944e]31#include "cmdline.h"
[93c3f97]32#include "message.h"
[0060ba5]33
34#include <assert.h>
[de7a879]35#include <signal.h>
[0060ba5]36
[168df28]37#include <wx/confbase.h>
[573f4e9]38#include <wx/image.h>
[168df28]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).
[9018b27]42#include <wx/display.h>
43#endif
[573f4e9]44
[5809313]45IMPLEMENT_APP(Aven)
[0060ba5]46
[5809313]47Aven::Aven() :
[168df28]48    m_Frame(NULL), m_pageSetupData(NULL)
[0060ba5]49{
50}
51
[168df28]52Aven::~Aven()
53{
54    if (m_pageSetupData) delete m_pageSetupData;
55}
56
[5809313]57bool Aven::OnInit()
[0060ba5]58{
[422eb92]59#ifdef __WXMAC__
60    // Tell wxMac which the About menu item is so it can be put where MacOS
[168df28]61    // users expect it to be.
[422eb92]62    wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT;
[3f9efe8]63    // wxMac is supposed to remove this magic command line option (which
[1bf1d42]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    }
[3f9efe8]72    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
73        --argc;
74        memmove(argv + 1, argv + 2, argc * sizeof(char *));
75    }
[0eab709]76#endif
[bdfe97f]77    msg_init(argv);
[421b7d2]78
[003d953]79    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
80    if (lang) {
[71ea9e1]81        // suppress message box warnings about messages not found
82        wxLogNull logNo;
83        wxLocale *loc = new wxLocale();
84        loc->AddCatalogLookupPathPrefix(msg_cfgpth());
[510ac63]85        if (!loc->Init(lang, lang, lang, TRUE, TRUE)) {
86            if (strcmp(lang, "sk") == 0) {
[e53ec10]87               // As of 2.2.9, wxWidgets has cs but not sk - the two languages
[510ac63]88               // are close, so this makes sense...
89               loc->Init("cs", "cs", "cs", TRUE, TRUE);
90            }
91        }
[71ea9e1]92        // The existence of the wxLocale object is enough - no need to keep a
93        // pointer to it!
[003d953]94    }
[93c3f97]95
[d07944e]96    wxString survey;
[f7a217f]97    bool print_and_exit = false;
[421b7d2]98
[f7a217f]99    /* Want --version and a decent --help output, which cmdline does for us.
100     * wxCmdLine is much less good.
101     */
[d07944e]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'},
[f7a217f]105        {"print", no_argument, 0, 'p'},
[d07944e]106        {"help", no_argument, 0, HLP_HELP},
107        {"version", no_argument, 0, HLP_VERSION},
108        {0, 0, 0, 0}
109    };
110
[f7a217f]111#define short_opts "s:p"
[d07944e]112
113    static struct help_msg help[] = {
114        /*                           <-- */
[3857c9af]115        {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
[f7a217f]116        {HLP_ENCODELONG(1),          "print and exit (requires a 3d file)"},
[d07944e]117        {0, 0}
118    };
119
[271d056]120    cmdline_set_syntax_message("[3d file]", NULL);
[d07944e]121    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
[f7a217f]122    while (true) {
[d07944e]123        int opt = cmdline_getopt();
124        if (opt == EOF) break;
125        if (opt == 's') {
126            survey = optarg;
127        }
[f7a217f]128        if (opt == 'p') {
129            print_and_exit = true;
130        }
[d07944e]131    }
[93c3f97]132
[f7a217f]133    if (print_and_exit && !argv[optind]) {
134        cmdline_syntax(); // FIXME : not a helpful error...
135        exit(1);
[068b4f2]136    }
137
[573f4e9]138    wxImage::AddHandler(new wxPNGHandler);
[08d2074]139
[486ae6a]140    // Obtain the screen size.
[9018b27]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);
[104e2dc]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    }
[9018b27]158#endif
[486ae6a]159
160    // Calculate a reasonable size for our window.
[9018b27]161    x += width / 8;
162    y += height / 8;
163    width = width * 3 / 4;
164    height = height * 3 / 4;
[486ae6a]165
166    // Create the main window.
[9018b27]167    m_Frame = new MainFrm(APP_NAME, wxPoint(x, y), wxSize(width, height));
[068b4f2]168
[d07944e]169    if (argv[optind]) {
[9006c59]170        m_Frame->OpenFile(wxString(argv[optind]), survey, true);
[d07944e]171    }
[f7a217f]172
173    if (print_and_exit) {
174        wxCommandEvent dummy;
175        m_Frame->OnPrint(dummy);
176        m_Frame->OnQuit(dummy);
177        return true;
[068b4f2]178    }
179
[5809313]180    m_Frame->Show(true);
[526775d]181#ifdef _WIN32
182    m_Frame->SetFocus();
183#endif
[0060ba5]184    return true;
185}
186
[168df28]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
[5809313]226void Aven::ReportError(const wxString& msg)
[0060ba5]227{
[6c9c7f0]228    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
229    dlg.ShowModal();
[0060ba5]230}
[59fda0a]231
[a9a32f2]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
[a4140e9]248   wxString s;
249   s.PrintfV(msg(en), ap);
250   m += s;
[a9a32f2]251   wxGetApp().ReportError(m);
252}
Note: See TracBrowser for help on using the repository browser.