| 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,2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 22 | // |
|---|
| 23 | |
|---|
| 24 | #ifdef HAVE_CONFIG_H |
|---|
| 25 | #include <config.h> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include "aven.h" |
|---|
| 29 | #include "log.h" |
|---|
| 30 | #include "mainfrm.h" |
|---|
| 31 | |
|---|
| 32 | #include "cmdline.h" |
|---|
| 33 | #include "message.h" |
|---|
| 34 | |
|---|
| 35 | #include <assert.h> |
|---|
| 36 | #include <signal.h> |
|---|
| 37 | |
|---|
| 38 | #include <wx/confbase.h> |
|---|
| 39 | #include <wx/image.h> |
|---|
| 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). |
|---|
| 43 | #include <wx/display.h> |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 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 | |
|---|
| 64 | IMPLEMENT_APP(Aven) |
|---|
| 65 | |
|---|
| 66 | Aven::Aven() : |
|---|
| 67 | m_Frame(NULL), m_pageSetupData(NULL) |
|---|
| 68 | { |
|---|
| 69 | wxFont::SetDefaultEncoding(wxFONTENCODING_UTF8); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | Aven::~Aven() |
|---|
| 73 | { |
|---|
| 74 | if (m_pageSetupData) delete m_pageSetupData; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 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 | |
|---|
| 87 | static int getopt_first_response = 0; |
|---|
| 88 | |
|---|
| 89 | bool Aven::Initialize(int& my_argc, wxChar **my_argv) |
|---|
| 90 | { |
|---|
| 91 | #ifdef __WXMAC__ |
|---|
| 92 | // Tell wxMac which the About and Quit menu items are so they can be put |
|---|
| 93 | // where MacOS users expect them to be. |
|---|
| 94 | wxApp::s_macAboutMenuItemId = menu_HELP_ABOUT; |
|---|
| 95 | wxApp::s_macExitMenuItemId = menu_FILE_QUIT; |
|---|
| 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. |
|---|
| 107 | init_msg_and_cmdline(my_argc, my_argv); |
|---|
| 108 | getopt_first_response = cmdline_getopt(); |
|---|
| 109 | return wxApp::Initialize(my_argc, my_argv); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | bool Aven::OnInit() |
|---|
| 113 | { |
|---|
| 114 | wxLog::SetActiveTarget(new MyLogWindow()); |
|---|
| 115 | |
|---|
| 116 | const char *lang = msg_lang2 ? msg_lang2 : msg_lang; |
|---|
| 117 | { |
|---|
| 118 | // suppress message box warnings about messages not found |
|---|
| 119 | wxLogNull logNo; |
|---|
| 120 | wxLocale *loc = new wxLocale(); |
|---|
| 121 | loc->AddCatalogLookupPathPrefix(msg_cfgpth()); |
|---|
| 122 | if (!loc->Init(msg_lang, lang, msg_lang, TRUE, TRUE)) { |
|---|
| 123 | if (lang && strcmp(lang, "sk") == 0) { |
|---|
| 124 | // As of 2.6.3, wxWidgets has cs but not sk - the two languages |
|---|
| 125 | // are close, so this makes sense... |
|---|
| 126 | loc->Init("cs", "cs", "cs", TRUE, TRUE); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | // The existence of the wxLocale object is enough - no need to keep a |
|---|
| 130 | // pointer to it! |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | wxString survey; |
|---|
| 134 | bool print_and_exit = false; |
|---|
| 135 | |
|---|
| 136 | while (true) { |
|---|
| 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 | } |
|---|
| 144 | if (opt == EOF) break; |
|---|
| 145 | if (opt == 's') { |
|---|
| 146 | survey = optarg; |
|---|
| 147 | } |
|---|
| 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); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 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 | |
|---|
| 165 | wxImage::AddHandler(new wxPNGHandler); |
|---|
| 166 | |
|---|
| 167 | // Obtain the screen size. |
|---|
| 168 | wxPoint pos(wxDefaultPosition); |
|---|
| 169 | int width, height; |
|---|
| 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) { |
|---|
| 175 | #if wxUSE_DISPLAY // wxDisplay was added in wx 2.5 |
|---|
| 176 | wxRect geom = wxDisplay().GetGeometry(); |
|---|
| 177 | pos.x = geom.x; |
|---|
| 178 | pos.y = geom.y; |
|---|
| 179 | width = geom.width; |
|---|
| 180 | height = geom.height; |
|---|
| 181 | #else |
|---|
| 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 | } |
|---|
| 190 | #endif |
|---|
| 191 | |
|---|
| 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 | } |
|---|
| 198 | |
|---|
| 199 | // Create the main window. |
|---|
| 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 | } |
|---|
| 208 | |
|---|
| 209 | if (argv[optind]) { |
|---|
| 210 | m_Frame->OpenFile(wxString(argv[optind]), survey); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if (print_and_exit) { |
|---|
| 214 | wxCommandEvent dummy; |
|---|
| 215 | m_Frame->OnPrint(dummy); |
|---|
| 216 | m_Frame->OnQuit(dummy); |
|---|
| 217 | return true; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | m_Frame->Show(true); |
|---|
| 221 | #ifdef _WIN32 |
|---|
| 222 | m_Frame->SetFocus(); |
|---|
| 223 | #endif |
|---|
| 224 | return true; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | wxPageSetupDialogData * |
|---|
| 228 | Aven::GetPageSetupDialogData() |
|---|
| 229 | { |
|---|
| 230 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
|---|
| 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); |
|---|
| 241 | m_pageSetupData->SetMarginTopLeft(wxPoint(left, top)); |
|---|
| 242 | m_pageSetupData->SetMarginBottomRight(wxPoint(right, bottom)); |
|---|
| 243 | #endif |
|---|
| 244 | return m_pageSetupData; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | void |
|---|
| 248 | Aven::SetPageSetupDialogData(const wxPageSetupDialogData & psdd) |
|---|
| 249 | { |
|---|
| 250 | if (!m_pageSetupData) m_pageSetupData = new wxPageSetupDialogData; |
|---|
| 251 | *m_pageSetupData = psdd; |
|---|
| 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 | |
|---|
| 266 | void Aven::ReportError(const wxString& msg) |
|---|
| 267 | { |
|---|
| 268 | AvenAllowOnTop ontop(m_Frame); |
|---|
| 269 | wxMessageDialog dlg(m_Frame, msg, APP_NAME, wxOK | wxICON_ERROR); |
|---|
| 270 | dlg.ShowModal(); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 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 | |
|---|
| 289 | wxString s; |
|---|
| 290 | s.PrintfV(msg(en), ap); |
|---|
| 291 | m += s; |
|---|
| 292 | wxGetApp().ReportError(m); |
|---|
| 293 | } |
|---|