[0060ba5] | 1 | // |
---|
[203d2a7] | 2 | // aven.cc |
---|
[0060ba5] | 3 | // |
---|
[5809313] | 4 | // Main class for Aven. |
---|
[0060ba5] | 5 | // |
---|
[3675a18] | 6 | // Copyright (C) 2001 Mark R. Shinwell. |
---|
[5892e08] | 7 | // Copyright (C) 2002,2003,2004,2005,2006 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 |
---|
[ecbc6c18] | 21 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[5809313] | 22 | // |
---|
[0060ba5] | 23 | |
---|
[b462168] | 24 | #ifdef HAVE_CONFIG_H |
---|
| 25 | #include <config.h> |
---|
| 26 | #endif |
---|
| 27 | |
---|
[5809313] | 28 | #include "aven.h" |
---|
[78924eb] | 29 | #include "log.h" |
---|
[5809313] | 30 | #include "mainfrm.h" |
---|
[706b033] | 31 | |
---|
[d07944e] | 32 | #include "cmdline.h" |
---|
[93c3f97] | 33 | #include "message.h" |
---|
[0060ba5] | 34 | |
---|
| 35 | #include <assert.h> |
---|
[de7a879] | 36 | #include <signal.h> |
---|
[0060ba5] | 37 | |
---|
[e0ffc2c] | 38 | #include <wx/confbase.h> |
---|
[573f4e9] | 39 | #include <wx/image.h> |
---|
[e0ffc2c] | 40 | #if wxUSE_DISPLAY |
---|
| 41 | // wxDisplay was added in wx 2.5; but it may not be built for mingw (because |
---|
| 42 | // the header seems to be missing). |
---|
[ca18348] | 43 | #include <wx/display.h> |
---|
| 44 | #endif |
---|
[573f4e9] | 45 | |
---|
[c293aa9] | 46 | bool double_buffered = false; |
---|
| 47 | |
---|
[879e9c3] | 48 | static const struct option long_opts[] = { |
---|
| 49 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 50 | {"survey", required_argument, 0, 's'}, |
---|
| 51 | {"print", no_argument, 0, 'p'}, |
---|
| 52 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 53 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 54 | {0, 0, 0, 0} |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | #define short_opts "s:p" |
---|
| 58 | |
---|
| 59 | static struct help_msg help[] = { |
---|
| 60 | /* <-- */ |
---|
| 61 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
| 62 | {HLP_ENCODELONG(1), "print and exit (requires a 3d file)"}, |
---|
| 63 | {0, 0} |
---|
| 64 | }; |
---|
| 65 | |
---|
[5627cbb] | 66 | #ifdef __WXMSW__ |
---|
[5809313] | 67 | IMPLEMENT_APP(Aven) |
---|
[5627cbb] | 68 | #else |
---|
| 69 | IMPLEMENT_APP_NO_MAIN(Aven) |
---|
| 70 | IMPLEMENT_WX_THEME_SUPPORT |
---|
| 71 | #endif |
---|
[0060ba5] | 72 | |
---|
[5809313] | 73 | Aven::Aven() : |
---|
[2c1d2f4] | 74 | m_Frame(NULL), m_pageSetupData(NULL) |
---|
[0060ba5] | 75 | { |
---|
[1f81f3d] | 76 | wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8); |
---|
[0060ba5] | 77 | } |
---|
| 78 | |
---|
[2c1d2f4] | 79 | Aven::~Aven() |
---|
| 80 | { |
---|
| 81 | if (m_pageSetupData) delete m_pageSetupData; |
---|
| 82 | } |
---|
| 83 | |
---|
[5627cbb] | 84 | static int getopt_first_response = 0; |
---|
| 85 | |
---|
| 86 | #ifdef __WXMSW__ |
---|
| 87 | bool Aven::Initialize(int& my_argc, wxChar **my_argv) |
---|
| 88 | { |
---|
| 89 | // Call msg_init() and start processing the command line first so that |
---|
| 90 | // we can respond to --help and --version even without an X display. |
---|
| 91 | // However, wxWidgets passes us wxChars, which may be wide characters |
---|
| 92 | // and cmdline wants UTF-8 so we need to convert. |
---|
| 93 | char *utf8_argv[my_argc + 1]; |
---|
| 94 | for (int i = 0; i < my_argc; ++i){ |
---|
| 95 | utf8_argv[i] = strdup(wxString(my_argv[i]).mb_str()); |
---|
| 96 | } |
---|
| 97 | utf8_argv[my_argc] = NULL; |
---|
| 98 | |
---|
| 99 | msg_init(utf8_argv); |
---|
[f1cbf74] | 100 | select_charset(CHARSET_UTF8); |
---|
| 101 | /* Want --version and a decent --help output, which cmdline does for us. |
---|
| 102 | * wxCmdLine is much less good. |
---|
| 103 | */ |
---|
| 104 | cmdline_set_syntax_message("[3d file]", NULL); |
---|
[5627cbb] | 105 | cmdline_init(my_argc, utf8_argv, short_opts, long_opts, NULL, help, 0, 1); |
---|
| 106 | getopt_first_response = cmdline_getopt(); |
---|
| 107 | return wxApp::Initialize(my_argc, my_argv); |
---|
[f1cbf74] | 108 | } |
---|
[5627cbb] | 109 | #define real_argv argv |
---|
| 110 | #else |
---|
| 111 | static char ** real_argv; |
---|
[f1cbf74] | 112 | |
---|
[5627cbb] | 113 | int main(int argc, char **argv) |
---|
[0060ba5] | 114 | { |
---|
[27b8b59] | 115 | #ifdef __WXMAC__ |
---|
[879e9c3] | 116 | // MacOS passes a magic -psn_XXXX command line argument in argv[1] which |
---|
| 117 | // wx ignores for us, but in wxApp::Initialize() which hasn't been |
---|
| 118 | // called yet. So we need to remove it ourselves. |
---|
[5627cbb] | 119 | if (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) { |
---|
| 120 | --argc; |
---|
| 121 | memmove(argv + 1, argv + 2, argc * sizeof(char *)); |
---|
[879e9c3] | 122 | } |
---|
| 123 | #endif |
---|
| 124 | // Call msg_init() and start processing the command line first so that |
---|
| 125 | // we can respond to --help and --version even without an X display. |
---|
[5627cbb] | 126 | msg_init(argv); |
---|
| 127 | select_charset(CHARSET_UTF8); |
---|
| 128 | /* Want --version and a decent --help output, which cmdline does for us. |
---|
| 129 | * wxCmdLine is much less good. |
---|
| 130 | */ |
---|
| 131 | cmdline_set_syntax_message("[3d file]", NULL); |
---|
| 132 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 0, 1); |
---|
[879e9c3] | 133 | getopt_first_response = cmdline_getopt(); |
---|
[5627cbb] | 134 | |
---|
| 135 | real_argv = argv; |
---|
| 136 | |
---|
| 137 | wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[0])); |
---|
| 138 | wxChar * wargv[2]; |
---|
| 139 | if (buf) { |
---|
| 140 | wargv[0] = wxStrdup(buf); |
---|
| 141 | } else { |
---|
| 142 | // Eep - couldn't convert the executable's name to wide characters! |
---|
| 143 | wargv[0] = wxStrdup(APP_NAME); |
---|
| 144 | } |
---|
| 145 | wargv[1] = NULL; |
---|
| 146 | int wargc = 1; |
---|
| 147 | return wxEntry(wargc, wargv); |
---|
[879e9c3] | 148 | } |
---|
[5627cbb] | 149 | #endif |
---|
[879e9c3] | 150 | |
---|
| 151 | bool Aven::OnInit() |
---|
| 152 | { |
---|
[4013d9c] | 153 | wxLog::SetActiveTarget(new MyLogWindow()); |
---|
[421b7d2] | 154 | |
---|
[d607cea] | 155 | { |
---|
[b1876d3] | 156 | // Suppress message box warnings about messages not found. |
---|
[71ea9e1] | 157 | wxLogNull logNo; |
---|
| 158 | wxLocale *loc = new wxLocale(); |
---|
[8a05a7a] | 159 | loc->AddCatalogLookupPathPrefix(wmsg_cfgpth()); |
---|
[b1876d3] | 160 | wxString msg_lang_str(msg_lang, wxConvUTF8); |
---|
| 161 | const char *lang = msg_lang2 ? msg_lang2 : msg_lang; |
---|
| 162 | wxString lang_str(lang, wxConvUTF8); |
---|
| 163 | loc->Init(msg_lang_str, lang_str, msg_lang_str, TRUE, TRUE); |
---|
[71ea9e1] | 164 | // The existence of the wxLocale object is enough - no need to keep a |
---|
| 165 | // pointer to it! |
---|
[003d953] | 166 | } |
---|
[93c3f97] | 167 | |
---|
[d07944e] | 168 | wxString survey; |
---|
[3ee64fb] | 169 | bool print_and_exit = false; |
---|
[421b7d2] | 170 | |
---|
[0580c6a] | 171 | while (true) { |
---|
[879e9c3] | 172 | int opt; |
---|
| 173 | if (getopt_first_response) { |
---|
| 174 | opt = getopt_first_response; |
---|
| 175 | getopt_first_response = 0; |
---|
| 176 | } else { |
---|
| 177 | opt = cmdline_getopt(); |
---|
| 178 | } |
---|
[d07944e] | 179 | if (opt == EOF) break; |
---|
| 180 | if (opt == 's') { |
---|
[5627cbb] | 181 | survey = wxString(optarg, wxConvUTF8); |
---|
[d07944e] | 182 | } |
---|
[3ee64fb] | 183 | if (opt == 'p') { |
---|
| 184 | print_and_exit = true; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[5627cbb] | 188 | if (print_and_exit && !real_argv[optind]) { |
---|
[3ee64fb] | 189 | cmdline_syntax(); // FIXME : not a helpful error... |
---|
| 190 | exit(1); |
---|
[d07944e] | 191 | } |
---|
[068b4f2] | 192 | |
---|
[5627cbb] | 193 | wxString fnm; |
---|
| 194 | if (real_argv[optind]) { |
---|
| 195 | fnm = wxString(real_argv[optind], wxConvUTF8); |
---|
| 196 | if (fnm.empty() && *(real_argv[optind])) { |
---|
| 197 | wxGetApp().ReportError(wxT("File argument's filename has bad encoding")); |
---|
| 198 | return false; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[c293aa9] | 202 | // Use a double-buffered visual if available, as it will give much smoother |
---|
| 203 | // animation. |
---|
| 204 | double_buffered = true; |
---|
| 205 | int wx_gl_attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 }; |
---|
| 206 | if (!InitGLVisual(wx_gl_attribs)) { |
---|
| 207 | int wx_gl_attribs_no_db[] = { WX_GL_RGBA, 0 }; |
---|
| 208 | if (!InitGLVisual(wx_gl_attribs_no_db)) { |
---|
| 209 | wxString m; |
---|
| 210 | m.Printf(wmsg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME); |
---|
| 211 | wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION); |
---|
| 212 | exit(1); |
---|
| 213 | } |
---|
| 214 | double_buffered = false; |
---|
[5892e08] | 215 | } |
---|
| 216 | |
---|
[573f4e9] | 217 | wxImage::AddHandler(new wxPNGHandler); |
---|
[08d2074] | 218 | |
---|
[486ae6a] | 219 | // Obtain the screen size. |
---|
[3db0798] | 220 | wxPoint pos(wxDefaultPosition); |
---|
[ca18348] | 221 | int width, height; |
---|
[5627cbb] | 222 | wxConfigBase::Get()->Read(wxT("width"), &width, 0); |
---|
| 223 | if (width > 0) wxConfigBase::Get()->Read(wxT("height"), &height, 0); |
---|
[3db0798] | 224 | bool maximized = (width == -1); |
---|
| 225 | bool full_screen = (width <= -2); |
---|
| 226 | if (width <= 0 || height <= 0) { |
---|
[0329df78] | 227 | #if wxUSE_DISPLAY // wxDisplay was added in wx 2.5 |
---|
[3db0798] | 228 | wxRect geom = wxDisplay().GetGeometry(); |
---|
| 229 | pos.x = geom.x; |
---|
| 230 | pos.y = geom.y; |
---|
| 231 | width = geom.width; |
---|
| 232 | height = geom.height; |
---|
[ca18348] | 233 | #else |
---|
[3db0798] | 234 | wxClientDisplayRect(&pos.x, &pos.y, &width, &height); |
---|
| 235 | // Crude fix to help behaviour on multi-monitor displays. |
---|
| 236 | // Fudge factors are a bit specific to my setup... |
---|
| 237 | if (width > height * 3 / 2) { |
---|
| 238 | pos.x += width; |
---|
| 239 | width = height * 3 / 2; |
---|
| 240 | pos.x -= width; |
---|
| 241 | } |
---|
[ca18348] | 242 | #endif |
---|
[486ae6a] | 243 | |
---|
[3db0798] | 244 | // Calculate a reasonable size for our window. |
---|
| 245 | pos.x += width / 8; |
---|
| 246 | pos.y += height / 8; |
---|
| 247 | width = width * 3 / 4; |
---|
| 248 | height = height * 3 / 4; |
---|
| 249 | } |
---|
[486ae6a] | 250 | |
---|
| 251 | // Create the main window. |
---|
[3db0798] | 252 | m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height)); |
---|
| 253 | |
---|
| 254 | // Select full_screen or maximised if that's the saved state. |
---|
| 255 | if (full_screen) { |
---|
| 256 | m_Frame->ShowFullScreen(true); |
---|
| 257 | } else if (maximized) { |
---|
| 258 | m_Frame->Maximize(); |
---|
| 259 | } |
---|
[068b4f2] | 260 | |
---|
[5627cbb] | 261 | if (real_argv[optind]) { |
---|
| 262 | m_Frame->OpenFile(fnm, survey); |
---|
[d07944e] | 263 | } |
---|
[068b4f2] | 264 | |
---|
[3ee64fb] | 265 | if (print_and_exit) { |
---|
| 266 | wxCommandEvent dummy; |
---|
| 267 | m_Frame->OnPrint(dummy); |
---|
| 268 | m_Frame->OnQuit(dummy); |
---|
| 269 | return true; |
---|
| 270 | } |
---|
| 271 | |
---|
[5809313] | 272 | m_Frame->Show(true); |
---|
[526775d] | 273 | #ifdef _WIN32 |
---|
| 274 | m_Frame->SetFocus(); |
---|
| 275 | #endif |
---|
[0060ba5] | 276 | return true; |
---|
| 277 | } |
---|
| 278 | |
---|
[e0ffc2c] | 279 | wxPageSetupDialogData * |
---|
| 280 | Aven::GetPageSetupDialogData() |
---|
| 281 | { |
---|
[2c1d2f4] | 282 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
[e0ffc2c] | 283 | #ifdef __WXGTK__ |
---|
| 284 | // Fetch paper margins stored on disk. |
---|
| 285 | int left, right, top, bottom; |
---|
| 286 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
| 287 | // These default margins were chosen by looking at all the .ppd files |
---|
| 288 | // on my machine. |
---|
[5627cbb] | 289 | cfg->Read(wxT("paper_margin_left"), &left, 7); |
---|
| 290 | cfg->Read(wxT("paper_margin_right"), &right, 7); |
---|
| 291 | cfg->Read(wxT("paper_margin_top"), &top, 14); |
---|
| 292 | cfg->Read(wxT("paper_margin_bottom"), &bottom, 14); |
---|
[2c1d2f4] | 293 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
---|
| 294 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
---|
[e0ffc2c] | 295 | #endif |
---|
[2c1d2f4] | 296 | return m_pageSetupData; |
---|
[e0ffc2c] | 297 | } |
---|
| 298 | |
---|
| 299 | void |
---|
| 300 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
---|
| 301 | { |
---|
[2c1d2f4] | 302 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
| 303 | *m_pageSetupData = psdd; |
---|
[e0ffc2c] | 304 | #ifdef __WXGTK__ |
---|
| 305 | wxPoint topleft = psdd.GetMarginTopLeft(); |
---|
| 306 | wxPoint bottomright = psdd.GetMarginBottomRight(); |
---|
| 307 | |
---|
| 308 | // Store user specified paper margins on disk/in registry. |
---|
| 309 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
[5627cbb] | 310 | cfg->Write(wxT("paper_margin_left"), topleft.x); |
---|
| 311 | cfg->Write(wxT("paper_margin_right"), bottomright.x); |
---|
| 312 | cfg->Write(wxT("paper_margin_top"), topleft.y); |
---|
| 313 | cfg->Write(wxT("paper_margin_bottom"), bottomright.y); |
---|
[e0ffc2c] | 314 | cfg->Flush(); |
---|
| 315 | #endif |
---|
| 316 | } |
---|
| 317 | |
---|
[5809313] | 318 | void Aven::ReportError(const wxString& msg) |
---|
[0060ba5] | 319 | { |
---|
[1c6b20d7] | 320 | AvenAllowOnTop ontop(m_Frame); |
---|
[7015222] | 321 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
---|
| 322 | dlg.ShowModal(); |
---|
[0060ba5] | 323 | } |
---|
[59fda0a] | 324 | |
---|
[5627cbb] | 325 | wxString |
---|
| 326 | wmsg(int msg_no) |
---|
| 327 | { |
---|
[4056cdc] | 328 | #if !wxCHECK_VERSION(2,8,0) |
---|
| 329 | return wxString(msg(msg_no), wxConvUTF8); |
---|
| 330 | #else |
---|
[833c6bc] | 331 | return wxString::FromUTF8(msg(msg_no)); |
---|
[4056cdc] | 332 | #endif |
---|
[5627cbb] | 333 | } |
---|
| 334 | |
---|
[8a05a7a] | 335 | const wxString & |
---|
| 336 | wmsg_cfgpth() |
---|
| 337 | { |
---|
| 338 | static wxString path; |
---|
| 339 | if (path.empty()) |
---|
| 340 | path = wxString(msg_cfgpth(), wxConvUTF8); |
---|
| 341 | return path; |
---|
| 342 | } |
---|
| 343 | |
---|
[a9a32f2] | 344 | // called to report errors by message.c |
---|
| 345 | extern "C" void |
---|
| 346 | aven_v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 347 | { |
---|
| 348 | wxString m; |
---|
| 349 | if (fnm) { |
---|
[5627cbb] | 350 | m = wxString(fnm, wxConvUTF8); |
---|
| 351 | if (line) m += wxString::Format(wxT(":%d"), line); |
---|
| 352 | m += wxT(": "); |
---|
[a9a32f2] | 353 | } |
---|
| 354 | |
---|
| 355 | if (severity == 0) { |
---|
[5627cbb] | 356 | m += wmsg(/*warning*/4); |
---|
| 357 | m += wxT(": "); |
---|
[a9a32f2] | 358 | } |
---|
| 359 | |
---|
[5627cbb] | 360 | m += wxString::FormatV(wmsg(en), ap); |
---|
[a9a32f2] | 361 | wxGetApp().ReportError(m); |
---|
| 362 | } |
---|