2
* threeDepict.cpp - main program implementation
3
* Copyright (C) 2013, D Haley
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include <wx/cmdline.h>
22
#include <wx/filename.h>
23
#include <wx/stdpaths.h>
26
#include "CoreFoundation/CoreFoundation.h"
29
#include "common/translation.h"
30
#include "gui/mainFrame.h"
33
#include "testing/testing.h"
37
ID_MAIN_WINDOW=wxID_ANY+1,
40
class threeDepictApp: public wxApp {
42
MainWindowFrame* MainFrame ;
43
wxArrayString commandLineFiles;
47
//void initLanguageSupport();
53
~threeDepictApp() { if(usrLocale) delete usrLocale;}
55
virtual void OnInitCmdLine(wxCmdLineParser& parser);
56
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
58
int FilterEvent(wxEvent &event);
60
void MacOpenFile(const wxString & fileName);
61
void MacReopenFile(const wxString & fileName);
65
void setEventloggerFile(const char *file);
69
//Check version is in place because wxT is deprecated for wx 2.9
70
//Command line parameter table
71
static const wxCmdLineEntryDesc g_cmdLineDesc [] =
73
#if wxCHECK_VERSION(2,9,0)
74
{ wxCMD_LINE_SWITCH, ("h"), ("help"), ("displays this message"),
75
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
76
{ wxCMD_LINE_PARAM, NULL, NULL, ("inputfile"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE},
78
{ wxCMD_LINE_SWITCH, wxT("h"), wxT("help"), wxNTRANS("displays this message"),
79
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
80
{ wxCMD_LINE_PARAM, NULL, NULL, wxNTRANS("inputfile"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE},
84
#if wxCHECK_VERSION(2,9,0)
85
{ wxCMD_LINE_SWITCH, ("t"), ("test"), ("Run debug unit tests, returns nonzero on test failure, zero on success.\n\t\t"
86
"XML files may be passed to run , instead of default tests"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_SWITCH},
88
{ wxCMD_LINE_SWITCH, wxT("t"), wxT("test"),
89
wxNTRANS("Run debug unit tests, returns nonzero on test failure, zero on success.\n\t\t"
90
"XML files may be passed to run, instead of default tests"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_SWITCH},
94
{ wxCMD_LINE_NONE,NULL,NULL,NULL,wxCMD_LINE_VAL_NONE,0 }
99
//Force a windows console to show for cerr/cout
101
#include "winconsole.h"
111
// feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
116
IMPLEMENT_APP(threeDepictApp)
118
threeDepictApp::threeDepictApp()
120
MainFrame=0;usrLocale=0;
122
#if wxCHECK_VERSION(2,9,0)
123
//Wx 2.9 and up now has assertions auto-enabled.
124
//Disable for release builds
125
wxSetAssertHandler(NULL);
131
/*void threeDepictApp::initLanguageSupport()
133
language = wxLANGUAGE_DEFAULT;
135
// load language if possible, fall back to English otherwise
136
if(false)// (wxLocale::IsAvailable(language))
138
//Wx 2.9 and above are now unicode, so locale encoding
139
//conversion is deprecated.
140
#if !wxCHECK_VERSION(2, 9, 0)
141
usrLocale = new wxLocale( language, wxLOCALE_CONV_ENCODING | wxLOCALE_LOAD_DEFAULT);
143
usrLocale = new wxLocale( language, wxLOCALE_LOAD_DEFAULT);
146
#if defined(__WXMAC__)
147
wxStandardPaths* paths = (wxStandardPaths*) &wxStandardPaths::Get();
148
usrLocale->AddCatalogLookupPathPrefix(paths->GetResourcesDir());
150
#elif defined(__WIN32__)
151
wxStandardPaths* paths = (wxStandardPaths*) &wxStandardPaths::Get();
152
usrLocale->AddCatalogLookupPathPrefix(paths->GetResourcesDir());
153
usrLocale->AddCatalogLookupPathPrefix ( wxT ( "locales" ) );
155
usrLocale->AddCatalog(wxCStr(PROGRAM_NAME));
159
if(! usrLocale->IsOk() )
161
std::cerr << "Unable to initialise usrLocale, falling back to English" << std::endl;
163
usrLocale = new wxLocale( wxLANGUAGE_ENGLISH );
164
language = wxLANGUAGE_ENGLISH;
168
//Set the gettext language
169
textdomain( "3depict");
170
setlocale (LC_ALL, "");
172
bindtextdomain( PROGRAM_NAME, paths->GetResourcesDir().mb_str(wxConvUTF8) );
173
#elif defined(__WIN32) || defined(__WIN64)
174
cerr << paths->GetResourcesDir().mb_str(wxConvUTF8) << endl;
176
s = paths->GetResourcesDir().mb_str(wxConvUTF8);
178
bindtextdomain( PROGRAM_NAME, s.c_str() );
179
//The names for the codesets are in confg.charset in gettext-runtime/intl in
180
// the gettext package. Tell gettext what codepage windows is using.
182
// The windows lookup codes are at
183
// http://msdn.microsoft.com/en-us/library/dd317756%28v=VS.85%29.aspx
184
unsigned int curPage;
189
cerr << "Bound cp1252" << endl;
190
bind_textdomain_codeset(PROGRAM_NAME, "CP1252");
193
cerr << "Bound utf8"<< endl;
194
bind_textdomain_codeset(PROGRAM_NAME, "UTF-8");
197
cerr << "Unknown codepage " << curPage << endl;
201
bindtextdomain( "3depict", "/usr/share/locale" );
202
bind_textdomain_codeset("3depict", "utf-8");
208
std::cout << "Language not supported, falling back to English" << endl;
209
usrLocale = new wxLocale( wxLANGUAGE_ENGLISH );
210
language = wxLANGUAGE_ENGLISH;
214
//Catching key events globally.
215
int threeDepictApp::FilterEvent(wxEvent& event)
217
//Process global keyboard (non-accelerator) events
218
if ( event.GetEventType()==wxEVT_KEY_DOWN )
222
mainActive=true; //Any way to actually get this?? wxGetActiveWindow() apparently returns null here.
224
mainActive =( wxGetTopLevelParent((wxWindow*)(wxGetActiveWindow())) == (wxWindow*)MainFrame);
227
if(MainFrame && mainActive)
229
wxKeyEvent& keyEvent = (wxKeyEvent&)event;
230
//Under GTK, escape aborts refresh. under mac,
231
//set it to also abort fullscreen, if not refreshing
232
if(keyEvent.GetKeyCode()==WXK_ESCAPE)
234
if( MainFrame->isCurrentlyUpdatingScene())
237
MainFrame->OnProgressAbort( cmd);
241
else if(MainFrame->IsFullScreen())
244
MainFrame->OnViewFullscreen(cmd);
245
MainFrame->ShowFullScreen(false);
252
//If the user presses F5, generate refresh
253
if( keyEvent.GetKeyCode()==WXK_F5)
256
MainFrame->OnButtonRefresh(cmd);
260
//Determine if control or meta key is down (dep. platform)
262
commandDown=keyEvent.ControlDown();
263
//If the user presses ctrl+insert, or alt+f5
264
//(ctrl+f5 doesn't work on mac, nor does it have an insert key)
265
//this activates hidden
266
//functionality to create autosave from filtertree
267
if((keyEvent.GetKeyCode()==WXK_INSERT && commandDown)
268
|| (keyEvent.GetKeyCode()==WXK_F5 && keyEvent.AltDown()) )
271
MainFrame->OnAutosaveTimer(evt);
281
//Command line help table and setup
282
void threeDepictApp::OnInitCmdLine(wxCmdLineParser& parser)
284
wxString name,version,preamble;
286
name=wxCStr(PROGRAM_NAME);
288
version=wxCStr(PROGRAM_VERSION);
291
preamble=wxT("Copyright (C) 2013 3Depict team\n");
292
preamble+=wxT("This program comes with ABSOLUTELY NO WARRANTY; for details see LICENCE file.\n");
293
preamble+=wxT("This is free software, and you are welcome to redistribute it under certain conditions.\n");
294
preamble+=wxT("Source code is available under the terms of the GNU GPL v3.0 or any later version (http://www.gnu.org/licenses/gpl.txt)\n");
295
parser.SetLogo(name+version+preamble);
297
parser.SetDesc (g_cmdLineDesc);
298
parser.SetSwitchChars (wxT("-"));
301
//Initialise wxwidgets parser
302
bool threeDepictApp::OnCmdLineParsed(wxCmdLineParser& parser)
305
if( parser.Found(wxT("test")))
307
//If we were given arguments, try to load them
308
//otherwise use the inbuilt test files
309
if(parser.GetParamCount())
311
for(unsigned int ui=0;ui<parser.GetParamCount();ui++)
314
f.Assign(parser.GetParam(ui));
317
strFile=stlStr(f.GetFullPath());
318
if( !f.FileExists() )
320
cerr << "Unable to locate file:" << strFile << endl;
324
cerr << "Loading :" << strFile << endl ;
327
VisController visControl;
328
if(!visControl.loadState(strFile.c_str(),cerr,false,true))
330
cerr << "Error loading state file:" << endl;
334
//Run a refresh over the filter tree as a test
336
visControl.cloneFilterTree(f);
337
if(f.hasHazardousContents())
339
f.stripHazardousContents();
340
cerr << "For security reasons, the tree was pruned prior to execution." << endl;
343
if(!testFilterTree(f))
345
cerr << "Failed loading :" << strFile << " , aborting" << endl;
350
cerr << "OK" << endl;
355
cerr << "Test XML File(s) Loaded OK" << endl;
363
cerr << "Unit tests failed" <<endl;
368
cerr << "Unit tests succeeded!" <<endl;
375
for(unsigned int ui=0;ui<parser.GetParamCount();ui++)
378
f.Assign(parser.GetParam(ui));
381
commandLineFiles.Add(f.GetFullPath());
383
std::cerr << TRANS("File : ") << stlStr(f.GetFullPath()) << TRANS(" does not exist. Skipping") << std::endl;
391
//Mac OSX drag/drop file support
392
void threeDepictApp::MacOpenFile(const wxString &filename)
398
MainFrame->OnDropFiles(array,0,0);
401
void threeDepictApp::MacReopenFile(const wxString &filename)
406
MacOpenFile(filename);
411
bool threeDepictApp::OnInit()
414
//initLanguageSupport();
417
//Set the gettext language
418
//Register signal handler for backtraces
419
if (!wxApp::OnInit())
422
//Need to seed random number generator for entire program
425
//Use a heuristic method (ie look around) to find a good sans-serif font
426
TTFFinder fontFinder;
427
setDefaultFontFile(fontFinder.getBestFontFile(TTFFINDER_FONT_SANS));
429
wxInitAllImageHandlers();
430
MainFrame = new MainWindowFrame(NULL, ID_MAIN_WINDOW, wxEmptyString,wxDefaultPosition,wxDefaultSize);
432
if(!MainFrame->initOK())
436
SetTopWindow(MainFrame);
438
#if defined(DEBUG) && defined(__linux__)
439
trapfpe(); //Under Linux, enable segfault on invalid floating point operations
443
//Switch the working directory into the .app bundle's resources
444
//directory using the absolute path
445
CFBundleRef mainBundle = CFBundleGetMainBundle();
446
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
448
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
453
CFRelease(resourcesURL);
461
if(commandLineFiles.GetCount())
462
MainFrame->SetCommandLineFiles(commandLineFiles);
464
MainFrame->fixSplitterWindow();