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
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.
[e1d6f8b]7//  Copyright (C) 2002,2003,2004,2005,2011 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__
[3f9efe8]60    // wxMac is supposed to remove this magic command line option (which
[1bf1d42]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    }
[3f9efe8]69    if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) {
70        --argc;
71        memmove(argv + 1, argv + 2, argc * sizeof(char *));
72    }
[0eab709]73#endif
[bdfe97f]74    msg_init(argv);
[421b7d2]75
[003d953]76    const char *lang = msg_lang2 ? msg_lang2 : msg_lang;
77    if (lang) {
[e7f613a]78        // Suppress message box warnings about messages not found.
[71ea9e1]79        wxLogNull logNo;
80        wxLocale *loc = new wxLocale();
81        loc->AddCatalogLookupPathPrefix(msg_cfgpth());
[e7f613a]82        loc->Init(lang, lang, lang, TRUE, TRUE);
[71ea9e1]83        // The existence of the wxLocale object is enough - no need to keep a
84        // pointer to it!
[003d953]85    }
[93c3f97]86
[d07944e]87    wxString survey;
[f7a217f]88    bool print_and_exit = false;
[421b7d2]89
[f7a217f]90    /* Want --version and a decent --help output, which cmdline does for us.
91     * wxCmdLine is much less good.
92     */
[d07944e]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'},
[f7a217f]96        {"print", no_argument, 0, 'p'},
[d07944e]97        {"help", no_argument, 0, HLP_HELP},
98        {"version", no_argument, 0, HLP_VERSION},
99        {0, 0, 0, 0}
100    };
101
[f7a217f]102#define short_opts "s:p"
[d07944e]103
104    static struct help_msg help[] = {
105        /*                           <-- */
[3857c9af]106        {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
[f7a217f]107        {HLP_ENCODELONG(1),          "print and exit (requires a 3d file)"},
[d07944e]108        {0, 0}
109    };
110
[271d056]111    cmdline_set_syntax_message("[3d file]", NULL);
[d07944e]112    cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1);
[f7a217f]113    while (true) {
[d07944e]114        int opt = cmdline_getopt();
115        if (opt == EOF) break;
116        if (opt == 's') {
117            survey = optarg;
118        }
[f7a217f]119        if (opt == 'p') {
120            print_and_exit = true;
121        }
[d07944e]122    }
[93c3f97]123
[f7a217f]124    if (print_and_exit && !argv[optind]) {
125        cmdline_syntax(); // FIXME : not a helpful error...
126        exit(1);
[068b4f2]127    }
128
[573f4e9]129    wxImage::AddHandler(new wxPNGHandler);
[08d2074]130
[486ae6a]131    // Obtain the screen size.
[9018b27]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);
[104e2dc]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    }
[9018b27]149#endif
[486ae6a]150
151    // Calculate a reasonable size for our window.
[9018b27]152    x += width / 8;
153    y += height / 8;
154    width = width * 3 / 4;
155    height = height * 3 / 4;
[486ae6a]156
157    // Create the main window.
[9018b27]158    m_Frame = new MainFrm(APP_NAME, wxPoint(x, y), wxSize(width, height));
[068b4f2]159
[d07944e]160    if (argv[optind]) {
[9006c59]161        m_Frame->OpenFile(wxString(argv[optind]), survey, true);
[d07944e]162    }
[f7a217f]163
164    if (print_and_exit) {
165        wxCommandEvent dummy;
166        m_Frame->OnPrint(dummy);
167        m_Frame->OnQuit(dummy);
168        return true;
[068b4f2]169    }
170
[5809313]171    m_Frame->Show(true);
[526775d]172#ifdef _WIN32
173    m_Frame->SetFocus();
174#endif
[0060ba5]175    return true;
176}
177
[168df28]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
[5809313]217void Aven::ReportError(const wxString& msg)
[0060ba5]218{
[6c9c7f0]219    wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR);
220    dlg.ShowModal();
[0060ba5]221}
[59fda0a]222
[a9a32f2]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
[a4140e9]239   wxString s;
240   s.PrintfV(msg(en), ap);
241   m += s;
[a9a32f2]242   wxGetApp().ReportError(m);
243}
Note: See TracBrowser for help on using the repository browser.