[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] | 45 | IMPLEMENT_APP(Aven) |
---|
[0060ba5] | 46 | |
---|
[5809313] | 47 | Aven::Aven() : |
---|
[168df28] | 48 | m_Frame(NULL), m_pageSetupData(NULL) |
---|
[0060ba5] | 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
[168df28] | 52 | Aven::~Aven() |
---|
| 53 | { |
---|
| 54 | if (m_pageSetupData) delete m_pageSetupData; |
---|
| 55 | } |
---|
| 56 | |
---|
[5809313] | 57 | bool 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) { |
---|
[e7f613a] | 81 | // Suppress message box warnings about messages not found. |
---|
[71ea9e1] | 82 | wxLogNull logNo; |
---|
| 83 | wxLocale *loc = new wxLocale(); |
---|
| 84 | loc->AddCatalogLookupPathPrefix(msg_cfgpth()); |
---|
[e7f613a] | 85 | loc->Init(lang, lang, lang, TRUE, TRUE); |
---|
[71ea9e1] | 86 | // The existence of the wxLocale object is enough - no need to keep a |
---|
| 87 | // pointer to it! |
---|
[003d953] | 88 | } |
---|
[93c3f97] | 89 | |
---|
[d07944e] | 90 | wxString survey; |
---|
[f7a217f] | 91 | bool print_and_exit = false; |
---|
[421b7d2] | 92 | |
---|
[f7a217f] | 93 | /* Want --version and a decent --help output, which cmdline does for us. |
---|
| 94 | * wxCmdLine is much less good. |
---|
| 95 | */ |
---|
[d07944e] | 96 | static const struct option long_opts[] = { |
---|
| 97 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 98 | {"survey", required_argument, 0, 's'}, |
---|
[f7a217f] | 99 | {"print", no_argument, 0, 'p'}, |
---|
[d07944e] | 100 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 101 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 102 | {0, 0, 0, 0} |
---|
| 103 | }; |
---|
| 104 | |
---|
[f7a217f] | 105 | #define short_opts "s:p" |
---|
[d07944e] | 106 | |
---|
| 107 | static struct help_msg help[] = { |
---|
| 108 | /* <-- */ |
---|
[3857c9af] | 109 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
[f7a217f] | 110 | {HLP_ENCODELONG(1), "print and exit (requires a 3d file)"}, |
---|
[d07944e] | 111 | {0, 0} |
---|
| 112 | }; |
---|
| 113 | |
---|
[271d056] | 114 | cmdline_set_syntax_message("[3d file]", NULL); |
---|
[d07944e] | 115 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1); |
---|
[f7a217f] | 116 | while (true) { |
---|
[d07944e] | 117 | int opt = cmdline_getopt(); |
---|
| 118 | if (opt == EOF) break; |
---|
| 119 | if (opt == 's') { |
---|
| 120 | survey = optarg; |
---|
| 121 | } |
---|
[f7a217f] | 122 | if (opt == 'p') { |
---|
| 123 | print_and_exit = true; |
---|
| 124 | } |
---|
[d07944e] | 125 | } |
---|
[93c3f97] | 126 | |
---|
[f7a217f] | 127 | if (print_and_exit && !argv[optind]) { |
---|
| 128 | cmdline_syntax(); // FIXME : not a helpful error... |
---|
| 129 | exit(1); |
---|
[068b4f2] | 130 | } |
---|
| 131 | |
---|
[573f4e9] | 132 | wxImage::AddHandler(new wxPNGHandler); |
---|
[08d2074] | 133 | |
---|
[486ae6a] | 134 | // Obtain the screen size. |
---|
[9018b27] | 135 | int x, y; |
---|
| 136 | int width, height; |
---|
| 137 | #if wxUSE_DISPLAY // wxDisplay was added in wx 2.5 |
---|
| 138 | wxRect geom = wxDisplay().GetGeometry(); |
---|
| 139 | x = geom.x; |
---|
| 140 | y = geom.y; |
---|
| 141 | width = geom.width; |
---|
| 142 | height = geom.height; |
---|
| 143 | #else |
---|
| 144 | wxClientDisplayRect(&x, &y, &width, &height); |
---|
[104e2dc] | 145 | // Crude fix to help behaviour on multi-monitor displays. |
---|
| 146 | // Fudge factors are a bit specific to my setup... |
---|
| 147 | if (width > height * 3 / 2) { |
---|
| 148 | x += width; |
---|
| 149 | width = height * 3 / 2; |
---|
| 150 | x -= width; |
---|
| 151 | } |
---|
[9018b27] | 152 | #endif |
---|
[486ae6a] | 153 | |
---|
| 154 | // Calculate a reasonable size for our window. |
---|
[9018b27] | 155 | x += width / 8; |
---|
| 156 | y += height / 8; |
---|
| 157 | width = width * 3 / 4; |
---|
| 158 | height = height * 3 / 4; |
---|
[486ae6a] | 159 | |
---|
| 160 | // Create the main window. |
---|
[9018b27] | 161 | m_Frame = new MainFrm(APP_NAME, wxPoint(x, y), wxSize(width, height)); |
---|
[068b4f2] | 162 | |
---|
[d07944e] | 163 | if (argv[optind]) { |
---|
[9006c59] | 164 | m_Frame->OpenFile(wxString(argv[optind]), survey, true); |
---|
[d07944e] | 165 | } |
---|
[f7a217f] | 166 | |
---|
| 167 | if (print_and_exit) { |
---|
| 168 | wxCommandEvent dummy; |
---|
| 169 | m_Frame->OnPrint(dummy); |
---|
| 170 | m_Frame->OnQuit(dummy); |
---|
| 171 | return true; |
---|
[068b4f2] | 172 | } |
---|
| 173 | |
---|
[5809313] | 174 | m_Frame->Show(true); |
---|
[526775d] | 175 | #ifdef _WIN32 |
---|
| 176 | m_Frame->SetFocus(); |
---|
| 177 | #endif |
---|
[0060ba5] | 178 | return true; |
---|
| 179 | } |
---|
| 180 | |
---|
[168df28] | 181 | wxPageSetupDialogData * |
---|
| 182 | Aven::GetPageSetupDialogData() |
---|
| 183 | { |
---|
| 184 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
| 185 | #ifdef __WXGTK__ |
---|
| 186 | // Fetch paper margins stored on disk. |
---|
| 187 | int left, right, top, bottom; |
---|
| 188 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
| 189 | // These default margins were chosen by looking at all the .ppd files |
---|
| 190 | // on my machine. |
---|
| 191 | cfg->Read("paper_margin_left", &left, 7); |
---|
| 192 | cfg->Read("paper_margin_right", &right, 7); |
---|
| 193 | cfg->Read("paper_margin_top", &top, 14); |
---|
| 194 | cfg->Read("paper_margin_bottom", &bottom, 14); |
---|
| 195 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
---|
| 196 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
---|
| 197 | #endif |
---|
| 198 | return m_pageSetupData; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void |
---|
| 202 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
---|
| 203 | { |
---|
| 204 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
| 205 | *m_pageSetupData = psdd; |
---|
| 206 | #ifdef __WXGTK__ |
---|
| 207 | wxPoint topleft = psdd.GetMarginTopLeft(); |
---|
| 208 | wxPoint bottomright = psdd.GetMarginBottomRight(); |
---|
| 209 | |
---|
| 210 | // Store user specified paper margins on disk/in registry. |
---|
| 211 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
| 212 | cfg->Write("paper_margin_left", topleft.x); |
---|
| 213 | cfg->Write("paper_margin_right", bottomright.x); |
---|
| 214 | cfg->Write("paper_margin_top", topleft.y); |
---|
| 215 | cfg->Write("paper_margin_bottom", bottomright.y); |
---|
| 216 | cfg->Flush(); |
---|
| 217 | #endif |
---|
| 218 | } |
---|
| 219 | |
---|
[5809313] | 220 | void Aven::ReportError(const wxString& msg) |
---|
[0060ba5] | 221 | { |
---|
[6c9c7f0] | 222 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
---|
| 223 | dlg.ShowModal(); |
---|
[0060ba5] | 224 | } |
---|
[59fda0a] | 225 | |
---|
[a9a32f2] | 226 | // called to report errors by message.c |
---|
| 227 | extern "C" void |
---|
| 228 | aven_v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 229 | { |
---|
| 230 | wxString m; |
---|
| 231 | if (fnm) { |
---|
| 232 | m = fnm; |
---|
| 233 | if (line) m += wxString::Format(":%d", line); |
---|
| 234 | m += ": "; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (severity == 0) { |
---|
| 238 | m += msg(/*warning*/4); |
---|
| 239 | m += ": "; |
---|
| 240 | } |
---|
| 241 | |
---|
[a4140e9] | 242 | wxString s; |
---|
| 243 | s.PrintfV(msg(en), ap); |
---|
| 244 | m += s; |
---|
[a9a32f2] | 245 | wxGetApp().ReportError(m); |
---|
| 246 | } |
---|