[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 |
---|
| 21 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 | |
---|
[879e9c3] | 46 | static const struct option long_opts[] = { |
---|
| 47 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 48 | {"survey", required_argument, 0, 's'}, |
---|
| 49 | {"print", no_argument, 0, 'p'}, |
---|
| 50 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 51 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 52 | {0, 0, 0, 0} |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | #define short_opts "s:p" |
---|
| 56 | |
---|
| 57 | static struct help_msg help[] = { |
---|
| 58 | /* <-- */ |
---|
| 59 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
| 60 | {HLP_ENCODELONG(1), "print and exit (requires a 3d file)"}, |
---|
| 61 | {0, 0} |
---|
| 62 | }; |
---|
| 63 | |
---|
[5809313] | 64 | IMPLEMENT_APP(Aven) |
---|
[0060ba5] | 65 | |
---|
[5809313] | 66 | Aven::Aven() : |
---|
[2c1d2f4] | 67 | m_Frame(NULL), m_pageSetupData(NULL) |
---|
[0060ba5] | 68 | { |
---|
[1f81f3d] | 69 | wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8); |
---|
[0060ba5] | 70 | } |
---|
| 71 | |
---|
[2c1d2f4] | 72 | Aven::~Aven() |
---|
| 73 | { |
---|
| 74 | if (m_pageSetupData) delete m_pageSetupData; |
---|
| 75 | } |
---|
| 76 | |
---|
[f1cbf74] | 77 | static void init_msg_and_cmdline(int& my_argc, char **my_argv) { |
---|
| 78 | msg_init(my_argv); |
---|
| 79 | select_charset(CHARSET_UTF8); |
---|
| 80 | /* Want --version and a decent --help output, which cmdline does for us. |
---|
| 81 | * wxCmdLine is much less good. |
---|
| 82 | */ |
---|
| 83 | cmdline_set_syntax_message("[3d file]", NULL); |
---|
| 84 | cmdline_init(my_argc, my_argv, short_opts, long_opts, NULL, help, 0, 1); |
---|
| 85 | } |
---|
| 86 | |
---|
[879e9c3] | 87 | static int getopt_first_response = 0; |
---|
| 88 | |
---|
| 89 | bool Aven::Initialize(int& my_argc, wxChar **my_argv) |
---|
[0060ba5] | 90 | { |
---|
[27b8b59] | 91 | #ifdef __WXMAC__ |
---|
[b72f4b5] | 92 | // Tell wxMac which the About and Quit menu items are so they can be put |
---|
| 93 | // where MacOS users expect them to be. |
---|
[27b8b59] | 94 | wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT; |
---|
[b72f4b5] | 95 | wxApp::s_macExitMenuItemId = menu_FILE_QUIT; |
---|
[879e9c3] | 96 | |
---|
| 97 | // MacOS passes a magic -psn_XXXX command line argument in argv[1] which |
---|
| 98 | // wx ignores for us, but in wxApp::Initialize() which hasn't been |
---|
| 99 | // called yet. So we need to remove it ourselves. |
---|
| 100 | if (my_argc > 1 && strncmp(my_argv[1], "-psn_", 5) == 0) { |
---|
| 101 | --my_argc; |
---|
| 102 | memmove(my_argv + 1, my_argv + 2, my_argc * sizeof(char *)); |
---|
| 103 | } |
---|
| 104 | #endif |
---|
| 105 | // Call msg_init() and start processing the command line first so that |
---|
| 106 | // we can respond to --help and --version even without an X display. |
---|
[f1cbf74] | 107 | init_msg_and_cmdline(my_argc, my_argv); |
---|
[879e9c3] | 108 | getopt_first_response = cmdline_getopt(); |
---|
| 109 | return wxApp::Initialize(my_argc, my_argv); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | bool Aven::OnInit() |
---|
| 113 | { |
---|
[4013d9c] | 114 | wxLog::SetActiveTarget(new MyLogWindow()); |
---|
[421b7d2] | 115 | |
---|
[003d953] | 116 | const char *lang = msg_lang2 ? msg_lang2 : msg_lang; |
---|
[d607cea] | 117 | { |
---|
[71ea9e1] | 118 | // suppress message box warnings about messages not found |
---|
| 119 | wxLogNull logNo; |
---|
| 120 | wxLocale *loc = new wxLocale(); |
---|
| 121 | loc->AddCatalogLookupPathPrefix(msg_cfgpth()); |
---|
[d607cea] | 122 | if (!loc->Init(msg_lang, lang, msg_lang, TRUE, TRUE)) { |
---|
| 123 | if (lang && strcmp(lang, "sk") == 0) { |
---|
[1324d6c] | 124 | // As of 2.6.3, wxWidgets has cs but not sk - the two languages |
---|
[510ac63] | 125 | // are close, so this makes sense... |
---|
| 126 | loc->Init("cs", "cs", "cs", TRUE, TRUE); |
---|
| 127 | } |
---|
| 128 | } |
---|
[71ea9e1] | 129 | // The existence of the wxLocale object is enough - no need to keep a |
---|
| 130 | // pointer to it! |
---|
[003d953] | 131 | } |
---|
[93c3f97] | 132 | |
---|
[d07944e] | 133 | wxString survey; |
---|
[3ee64fb] | 134 | bool print_and_exit = false; |
---|
[421b7d2] | 135 | |
---|
[0580c6a] | 136 | while (true) { |
---|
[879e9c3] | 137 | int opt; |
---|
| 138 | if (getopt_first_response) { |
---|
| 139 | opt = getopt_first_response; |
---|
| 140 | getopt_first_response = 0; |
---|
| 141 | } else { |
---|
| 142 | opt = cmdline_getopt(); |
---|
| 143 | } |
---|
[d07944e] | 144 | if (opt == EOF) break; |
---|
| 145 | if (opt == 's') { |
---|
| 146 | survey = optarg; |
---|
| 147 | } |
---|
[3ee64fb] | 148 | if (opt == 'p') { |
---|
| 149 | print_and_exit = true; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if (print_and_exit && !argv[optind]) { |
---|
| 154 | cmdline_syntax(); // FIXME : not a helpful error... |
---|
| 155 | exit(1); |
---|
[d07944e] | 156 | } |
---|
[068b4f2] | 157 | |
---|
[5892e08] | 158 | if (!InitGLVisual(NULL)) { |
---|
| 159 | wxString m; |
---|
| 160 | m.Printf(msg(/*This version of %s requires OpenGL to work, but it isn't available*/405), APP_NAME); |
---|
| 161 | wxMessageBox(m, APP_NAME, wxOK | wxCENTRE | wxICON_EXCLAMATION); |
---|
| 162 | exit(1); |
---|
| 163 | } |
---|
| 164 | |
---|
[573f4e9] | 165 | wxImage::AddHandler(new wxPNGHandler); |
---|
[08d2074] | 166 | |
---|
[486ae6a] | 167 | // Obtain the screen size. |
---|
[3db0798] | 168 | wxPoint pos(wxDefaultPosition); |
---|
[ca18348] | 169 | int width, height; |
---|
[3db0798] | 170 | wxConfigBase::Get()->Read("width", &width, 0); |
---|
| 171 | if (width > 0) wxConfigBase::Get()->Read("height", &height, 0); |
---|
| 172 | bool maximized = (width == -1); |
---|
| 173 | bool full_screen = (width <= -2); |
---|
| 174 | if (width <= 0 || height <= 0) { |
---|
[0329df78] | 175 | #if wxUSE_DISPLAY // wxDisplay was added in wx 2.5 |
---|
[3db0798] | 176 | wxRect geom = wxDisplay().GetGeometry(); |
---|
| 177 | pos.x = geom.x; |
---|
| 178 | pos.y = geom.y; |
---|
| 179 | width = geom.width; |
---|
| 180 | height = geom.height; |
---|
[ca18348] | 181 | #else |
---|
[3db0798] | 182 | wxClientDisplayRect(&pos.x, &pos.y, &width, &height); |
---|
| 183 | // Crude fix to help behaviour on multi-monitor displays. |
---|
| 184 | // Fudge factors are a bit specific to my setup... |
---|
| 185 | if (width > height * 3 / 2) { |
---|
| 186 | pos.x += width; |
---|
| 187 | width = height * 3 / 2; |
---|
| 188 | pos.x -= width; |
---|
| 189 | } |
---|
[ca18348] | 190 | #endif |
---|
[486ae6a] | 191 | |
---|
[3db0798] | 192 | // Calculate a reasonable size for our window. |
---|
| 193 | pos.x += width / 8; |
---|
| 194 | pos.y += height / 8; |
---|
| 195 | width = width * 3 / 4; |
---|
| 196 | height = height * 3 / 4; |
---|
| 197 | } |
---|
[486ae6a] | 198 | |
---|
| 199 | // Create the main window. |
---|
[3db0798] | 200 | m_Frame = new MainFrm(APP_NAME, pos, wxSize(width, height)); |
---|
| 201 | |
---|
| 202 | // Select full_screen or maximised if that's the saved state. |
---|
| 203 | if (full_screen) { |
---|
| 204 | m_Frame->ShowFullScreen(true); |
---|
| 205 | } else if (maximized) { |
---|
| 206 | m_Frame->Maximize(); |
---|
| 207 | } |
---|
[068b4f2] | 208 | |
---|
[d07944e] | 209 | if (argv[optind]) { |
---|
[dde4fe7] | 210 | m_Frame->OpenFile(wxString(argv[optind]), survey); |
---|
[d07944e] | 211 | } |
---|
[068b4f2] | 212 | |
---|
[3ee64fb] | 213 | if (print_and_exit) { |
---|
| 214 | wxCommandEvent dummy; |
---|
| 215 | m_Frame->OnPrint(dummy); |
---|
| 216 | m_Frame->OnQuit(dummy); |
---|
| 217 | return true; |
---|
| 218 | } |
---|
| 219 | |
---|
[5809313] | 220 | m_Frame->Show(true); |
---|
[526775d] | 221 | #ifdef _WIN32 |
---|
| 222 | m_Frame->SetFocus(); |
---|
| 223 | #endif |
---|
[0060ba5] | 224 | return true; |
---|
| 225 | } |
---|
| 226 | |
---|
[e0ffc2c] | 227 | wxPageSetupDialogData * |
---|
| 228 | Aven::GetPageSetupDialogData() |
---|
| 229 | { |
---|
[2c1d2f4] | 230 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
[e0ffc2c] | 231 | #ifdef __WXGTK__ |
---|
| 232 | // Fetch paper margins stored on disk. |
---|
| 233 | int left, right, top, bottom; |
---|
| 234 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
| 235 | // These default margins were chosen by looking at all the .ppd files |
---|
| 236 | // on my machine. |
---|
| 237 | cfg->Read("paper_margin_left", &left, 7); |
---|
| 238 | cfg->Read("paper_margin_right", &right, 7); |
---|
| 239 | cfg->Read("paper_margin_top", &top, 14); |
---|
| 240 | cfg->Read("paper_margin_bottom", &bottom, 14); |
---|
[2c1d2f4] | 241 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
---|
| 242 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
---|
[e0ffc2c] | 243 | #endif |
---|
[2c1d2f4] | 244 | return m_pageSetupData; |
---|
[e0ffc2c] | 245 | } |
---|
| 246 | |
---|
| 247 | void |
---|
| 248 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
---|
| 249 | { |
---|
[2c1d2f4] | 250 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
---|
| 251 | *m_pageSetupData = psdd; |
---|
[e0ffc2c] | 252 | #ifdef __WXGTK__ |
---|
| 253 | wxPoint topleft = psdd.GetMarginTopLeft(); |
---|
| 254 | wxPoint bottomright = psdd.GetMarginBottomRight(); |
---|
| 255 | |
---|
| 256 | // Store user specified paper margins on disk/in registry. |
---|
| 257 | wxConfigBase * cfg = wxConfigBase::Get(); |
---|
| 258 | cfg->Write("paper_margin_left", topleft.x); |
---|
| 259 | cfg->Write("paper_margin_right", bottomright.x); |
---|
| 260 | cfg->Write("paper_margin_top", topleft.y); |
---|
| 261 | cfg->Write("paper_margin_bottom", bottomright.y); |
---|
| 262 | cfg->Flush(); |
---|
| 263 | #endif |
---|
| 264 | } |
---|
| 265 | |
---|
[5809313] | 266 | void Aven::ReportError(const wxString& msg) |
---|
[0060ba5] | 267 | { |
---|
[1c6b20d7] | 268 | AvenAllowOnTop ontop(m_Frame); |
---|
[7015222] | 269 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
---|
| 270 | dlg.ShowModal(); |
---|
[0060ba5] | 271 | } |
---|
[59fda0a] | 272 | |
---|
[a9a32f2] | 273 | // called to report errors by message.c |
---|
| 274 | extern "C" void |
---|
| 275 | aven_v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 276 | { |
---|
| 277 | wxString m; |
---|
| 278 | if (fnm) { |
---|
| 279 | m = fnm; |
---|
| 280 | if (line) m += wxString::Format(":%d", line); |
---|
| 281 | m += ": "; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | if (severity == 0) { |
---|
| 285 | m += msg(/*warning*/4); |
---|
| 286 | m += ": "; |
---|
| 287 | } |
---|
| 288 | |
---|
[a4140e9] | 289 | wxString s; |
---|
| 290 | s.PrintfV(msg(en), ap); |
---|
| 291 | m += s; |
---|
[a9a32f2] | 292 | wxGetApp().ReportError(m); |
---|
| 293 | } |
---|