source: git/src/aven.cc @ e1d6f8b

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

Backport changes from 1.2.0:
src/printwx.cc: Use stock wx button IDs wxID_PRINT and
wxID_PREVIEW where appropriate.
src/aven.cc,src/mainfrm.cc,src/mainfrm.h: Use more stock IDs.

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

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