~ubuntu-branches/debian/jessie/3depict/jessie

« back to all changes in this revision

Viewing changes to .pc/wx3-startup/src/3Depict.cpp

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2014-06-09 22:54:00 UTC
  • Revision ID: package-import@ubuntu.com-20140609225400-mxteq4a0xq9cfws3
Tags: 0.0.16-2
Add wx 3.0 startup patch (Closes: #746609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      threeDepict.cpp - main program implementation
 
3
 *      Copyright (C) 2013, D Haley 
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
*/
 
18
 
 
19
 
 
20
#include <wx/wx.h>
 
21
#include <wx/cmdline.h>
 
22
#include <wx/filename.h>
 
23
#include <wx/stdpaths.h>
 
24
 
 
25
#ifdef __APPLE__
 
26
#include "CoreFoundation/CoreFoundation.h"
 
27
#endif
 
28
 
 
29
#include "common/translation.h"
 
30
#include "gui/mainFrame.h"
 
31
 
 
32
//Unit testing code
 
33
#include "testing/testing.h"
 
34
 
 
35
enum
 
36
{
 
37
        ID_MAIN_WINDOW=wxID_ANY+1,
 
38
};
 
39
 
 
40
class threeDepictApp: public wxApp {
 
41
private:
 
42
        MainWindowFrame* MainFrame ;
 
43
        wxArrayString commandLineFiles;
 
44
        wxLocale* usrLocale;
 
45
        //long language;
 
46
 
 
47
        //void initLanguageSupport();
 
48
 
 
49
 
 
50
public:
 
51
 
 
52
    threeDepictApp() ;
 
53
    ~threeDepictApp() { if(usrLocale) delete usrLocale;}
 
54
    bool OnInit();
 
55
    virtual void OnInitCmdLine(wxCmdLineParser& parser);
 
56
    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
 
57
                 
 
58
    int FilterEvent(wxEvent &event);
 
59
#ifdef __APPLE__
 
60
    void MacOpenFile(const wxString & fileName);
 
61
    void MacReopenFile(const wxString & fileName);
 
62
#endif
 
63
 
 
64
#ifdef DEBUG
 
65
    void setEventloggerFile(const char *file);
 
66
#endif
 
67
};
 
68
 
 
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 [] =
 
72
{
 
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},
 
77
#else
 
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},
 
81
#endif
 
82
        //Unit testing system
 
83
#ifdef DEBUG
 
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},
 
87
#else
 
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},
 
91
#endif
 
92
#endif
 
93
 
 
94
  { wxCMD_LINE_NONE,NULL,NULL,NULL,wxCMD_LINE_VAL_NONE,0 }
 
95
 
 
96
};
 
97
 
 
98
#ifdef __WXMSW__
 
99
//Force a windows console to show for cerr/cout
 
100
#ifdef DEBUG
 
101
#include "winconsole.h"
 
102
winconsole winC;
 
103
#endif
 
104
#endif
 
105
 
 
106
//DEBUG NaN and INF
 
107
#ifdef DEBUG
 
108
#ifdef __linux__
 
109
#include <fenv.h>
 
110
void trapfpe () {
 
111
//  feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
 
112
}
 
113
#endif
 
114
#endif
 
115
 
 
116
IMPLEMENT_APP(threeDepictApp)
 
117
 
 
118
threeDepictApp::threeDepictApp()
 
119
{
 
120
        MainFrame=0;usrLocale=0;
 
121
#ifndef DEBUG
 
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);
 
126
#endif
 
127
#endif
 
128
}
 
129
 
 
130
 
 
131
/*void threeDepictApp::initLanguageSupport()
 
132
{
 
133
        language =  wxLANGUAGE_DEFAULT;
 
134
 
 
135
        // load language if possible, fall back to English otherwise
 
136
        if(false)// (wxLocale::IsAvailable(language))
 
137
        {
 
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);
 
142
#else
 
143
                usrLocale = new wxLocale( language, wxLOCALE_LOAD_DEFAULT);
 
144
#endif
 
145
 
 
146
#if defined(__WXMAC__)
 
147
                wxStandardPaths* paths = (wxStandardPaths*) &wxStandardPaths::Get();
 
148
                usrLocale->AddCatalogLookupPathPrefix(paths->GetResourcesDir());
 
149
                
 
150
#elif defined(__WIN32__)
 
151
                wxStandardPaths* paths = (wxStandardPaths*) &wxStandardPaths::Get();
 
152
                usrLocale->AddCatalogLookupPathPrefix(paths->GetResourcesDir());
 
153
                usrLocale->AddCatalogLookupPathPrefix ( wxT ( "locales" ) );
 
154
#endif
 
155
                usrLocale->AddCatalog(wxCStr(PROGRAM_NAME));
 
156
 
 
157
 
 
158
 
 
159
                if(! usrLocale->IsOk() )
 
160
                {
 
161
                        std::cerr << "Unable to initialise usrLocale, falling back to English" << std::endl;
 
162
                        delete usrLocale;
 
163
                        usrLocale = new wxLocale( wxLANGUAGE_ENGLISH );
 
164
                        language = wxLANGUAGE_ENGLISH;
 
165
                }
 
166
                else
 
167
                {
 
168
                        //Set the gettext language
 
169
                        textdomain( "3depict");
 
170
                        setlocale (LC_ALL, "");
 
171
#ifdef __WXMAC__
 
172
                        bindtextdomain( PROGRAM_NAME, paths->GetResourcesDir().mb_str(wxConvUTF8) );
 
173
#elif defined(__WIN32) || defined(__WIN64)
 
174
                        cerr << paths->GetResourcesDir().mb_str(wxConvUTF8) << endl;
 
175
                        std::string s;
 
176
                        s =  paths->GetResourcesDir().mb_str(wxConvUTF8);
 
177
                        s+="/locales/";
 
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.
 
181
                        //
 
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;
 
185
                        curPage=GetACP();
 
186
                        switch(curPage)
 
187
                        {
 
188
                                case 1252:
 
189
                                        cerr << "Bound cp1252" << endl;
 
190
                                        bind_textdomain_codeset(PROGRAM_NAME, "CP1252");
 
191
                                        break;
 
192
                                case 65001:
 
193
                                        cerr << "Bound utf8"<< endl;
 
194
                                        bind_textdomain_codeset(PROGRAM_NAME, "UTF-8");
 
195
                                        break;
 
196
                                default:
 
197
                                        cerr << "Unknown codepage " << curPage << endl;
 
198
                                        break;
 
199
                        }                       
 
200
#else
 
201
                        bindtextdomain( "3depict", "/usr/share/locale" );
 
202
                        bind_textdomain_codeset("3depict", "utf-8");
 
203
#endif
 
204
                }
 
205
        }
 
206
        else
 
207
        {
 
208
                std::cout << "Language not supported, falling back to English" << endl;
 
209
                usrLocale = new wxLocale( wxLANGUAGE_ENGLISH );
 
210
                language = wxLANGUAGE_ENGLISH;
 
211
        }
 
212
}
 
213
*/
 
214
//Catching key events globally.
 
215
int threeDepictApp::FilterEvent(wxEvent& event)
 
216
{
 
217
        //Process global keyboard (non-accelerator) events
 
218
        if ( event.GetEventType()==wxEVT_KEY_DOWN )
 
219
        {
 
220
                bool mainActive;
 
221
#ifdef __APPLE__
 
222
                mainActive=true; //Any way to actually get this?? wxGetActiveWindow() apparently returns null here.
 
223
#else
 
224
                mainActive =( wxGetTopLevelParent((wxWindow*)(wxGetActiveWindow())) == (wxWindow*)MainFrame);
 
225
#endif
 
226
        
 
227
                if(MainFrame && mainActive)
 
228
                {
 
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)
 
233
                        {
 
234
                                if( MainFrame->isCurrentlyUpdatingScene())
 
235
                                {
 
236
                                        wxCommandEvent cmd;
 
237
                                        MainFrame->OnProgressAbort( cmd);
 
238
                                        return true;
 
239
                                }
 
240
#ifdef __APPLE__
 
241
                                else if(MainFrame->IsFullScreen())
 
242
                                {
 
243
                                        wxCommandEvent cmd;
 
244
                                        MainFrame->OnViewFullscreen(cmd);
 
245
                                        MainFrame->ShowFullScreen(false);
 
246
                                        return true;
 
247
                                }
 
248
#endif
 
249
 
 
250
                        }
 
251
 
 
252
                        //If the user presses F5, generate refresh
 
253
                        if( keyEvent.GetKeyCode()==WXK_F5)
 
254
                        {
 
255
                                wxCommandEvent cmd;
 
256
                                MainFrame->OnButtonRefresh(cmd);
 
257
                        }
 
258
 
 
259
#ifdef DEBUG
 
260
                        //Determine if control or meta key is down (dep. platform)
 
261
                        bool commandDown;
 
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()) )
 
269
                        {
 
270
                                wxTimerEvent evt;
 
271
                                MainFrame->OnAutosaveTimer(evt);
 
272
                        }
 
273
#endif
 
274
                }
 
275
 
 
276
        }
 
277
 
 
278
    return -1;
 
279
}
 
280
 
 
281
//Command line help table and setup
 
282
void threeDepictApp::OnInitCmdLine(wxCmdLineParser& parser)
 
283
{
 
284
        wxString name,version,preamble;
 
285
 
 
286
        name=wxCStr(PROGRAM_NAME);
 
287
        name=name+ wxT(" ");
 
288
        version=wxCStr(PROGRAM_VERSION);
 
289
        version+=wxT("\n");
 
290
 
 
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);
 
296
 
 
297
        parser.SetDesc (g_cmdLineDesc);
 
298
        parser.SetSwitchChars (wxT("-"));
 
299
}
 
300
 
 
301
//Initialise wxwidgets parser
 
302
bool threeDepictApp::OnCmdLineParsed(wxCmdLineParser& parser)
 
303
{
 
304
#ifdef DEBUG
 
305
        if( parser.Found(wxT("test"))) 
 
306
        {
 
307
                //If we were given arguments, try to load them
 
308
                //otherwise use the inbuilt test files
 
309
                if(parser.GetParamCount())
 
310
                {
 
311
                        for(unsigned int ui=0;ui<parser.GetParamCount();ui++)
 
312
                        {
 
313
                                wxFileName f;
 
314
                                f.Assign(parser.GetParam(ui));
 
315
 
 
316
                                std::string strFile;
 
317
                                strFile=stlStr(f.GetFullPath());
 
318
                                if( !f.FileExists() )
 
319
                                {
 
320
                                        cerr << "Unable to locate file:" << strFile << endl;
 
321
                                        continue;
 
322
                                }
 
323
 
 
324
                                cerr << "Loading :" << strFile << endl ;
 
325
 
 
326
                                {
 
327
                                VisController visControl;
 
328
                                if(!visControl.loadState(strFile.c_str(),cerr,false,true))
 
329
                                {
 
330
                                        cerr << "Error loading state file:" << endl;
 
331
                                        exit(1);
 
332
                                }
 
333
 
 
334
                                //Run a refresh over the filter tree as a test
 
335
                                FilterTree f;
 
336
                                visControl.cloneFilterTree(f);
 
337
                                if(f.hasHazardousContents())
 
338
                                {
 
339
                                        f.stripHazardousContents();
 
340
                                        cerr << "For security reasons, the tree was pruned prior to execution." << endl;
 
341
                                }
 
342
                                
 
343
                                if(!testFilterTree(f))
 
344
                                {
 
345
                                        cerr << "Failed loading :" << strFile << " , aborting" << endl;
 
346
                                        exit(1);
 
347
                                }
 
348
                                }
 
349
 
 
350
                                cerr << "OK" << endl; 
 
351
 
 
352
                        }
 
353
                        
 
354
                         
 
355
                        cerr << "Test XML File(s) Loaded OK" << endl;   
 
356
                        exit(0);
 
357
                }
 
358
                else
 
359
                {
 
360
                        //Unit tests failed
 
361
                        if(!runUnitTests()) 
 
362
                        {
 
363
                                cerr << "Unit tests failed" <<endl;
 
364
                                exit(1);
 
365
                        }
 
366
                        else
 
367
                        {
 
368
                                cerr << "Unit tests succeeded!" <<endl;
 
369
                                exit(0);
 
370
                        }
 
371
                }
 
372
        }
 
373
#endif
 
374
 
 
375
        for(unsigned int ui=0;ui<parser.GetParamCount();ui++)
 
376
        {
 
377
                wxFileName f;
 
378
                f.Assign(parser.GetParam(ui));
 
379
 
 
380
                if( f.FileExists() )
 
381
                        commandLineFiles.Add(f.GetFullPath());
 
382
                else
 
383
                        std::cerr << TRANS("File : ") << stlStr(f.GetFullPath()) << TRANS(" does not exist. Skipping") << std::endl;
 
384
 
 
385
        }
 
386
 
 
387
        return true;
 
388
}
 
389
 
 
390
#ifdef __APPLE__
 
391
//Mac OSX drag/drop file support
 
392
void threeDepictApp::MacOpenFile(const wxString &filename)
 
393
{
 
394
        ASSERT(MainFrame);
 
395
        wxArrayString array;
 
396
        array.Add(filename);
 
397
 
 
398
        MainFrame->OnDropFiles(array,0,0);
 
399
}
 
400
 
 
401
void threeDepictApp::MacReopenFile(const wxString &filename)
 
402
{
 
403
        ASSERT(MainFrame);
 
404
        MainFrame->Raise();
 
405
 
 
406
        MacOpenFile(filename);
 
407
}
 
408
 
 
409
#endif
 
410
 
 
411
bool threeDepictApp::OnInit()
 
412
{
 
413
 
 
414
    //initLanguageSupport();
 
415
        
 
416
 
 
417
    //Set the gettext language
 
418
    //Register signal handler for backtraces
 
419
    if (!wxApp::OnInit())
 
420
        return false; 
 
421
 
 
422
    //Need to seed random number generator for entire program
 
423
    srand (time(NULL));
 
424
 
 
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));
 
428
    
 
429
    wxInitAllImageHandlers();
 
430
    MainFrame = new MainWindowFrame(NULL, ID_MAIN_WINDOW, wxEmptyString,wxDefaultPosition,wxDefaultSize);
 
431
 
 
432
    if(!MainFrame->initOK())
 
433
            return false;
 
434
  
 
435
 
 
436
    SetTopWindow(MainFrame);
 
437
 
 
438
#if defined(DEBUG) && defined(__linux__)
 
439
   trapfpe(); //Under Linux, enable  segfault on invalid floating point operations
 
440
#endif
 
441
 
 
442
#ifdef __APPLE__    
 
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);
 
447
        char path[PATH_MAX];
 
448
        if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
 
449
        {
 
450
                // error!
 
451
        } else
 
452
        {
 
453
                CFRelease(resourcesURL);
 
454
                chdir(path);
 
455
        }
 
456
#endif
 
457
 
 
458
 
 
459
    MainFrame->Show();
 
460
    
 
461
    if(commandLineFiles.GetCount())
 
462
        MainFrame->SetCommandLineFiles(commandLineFiles);
 
463
 
 
464
    MainFrame->fixSplitterWindow();
 
465
    return true;
 
466
}
 
467
 
 
468