1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#ifdef __INTELLISENSE__
#pragma diag_suppress 102
#endif
#include <GL/glew.h>
#include <wx/app.h>
#include <wx/cmdline.h>
#include <wx/event.h>
#include <wx/image.h>
#include <wx/msgdlg.h>
#include <wx/stdpaths.h>
#include <wx/textfile.h>
#include <wx/cmdline.h>
#include <wx/msgout.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "MainFrame.h"
#include "PropertiesData.h"
/*static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
{wxCMD_LINE_SWITCH, "h", "help", "displays help on the command line parameters", wxCMD_LINE_VAL_NONE,
wxCMD_LINE_OPTION_HELP},
{wxCMD_LINE_SWITCH, "t", "test", "test switch", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_MANDATORY},
{wxCMD_LINE_SWITCH, "s", "silent", "disables the GUI"},
{wxCMD_LINE_NONE}};*/
// Define the MainApp
class MainApp : public wxApp
{
public:
MainApp() {}
virtual ~MainApp() {}
bool LoadInitFile(PropertiesData* propertiesData)
{
// Load configuration file, if don't exists create it.
// Find the executable location path.
wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
wxTextFile file(fn.GetPath() + wxFileName::GetPathSeparator() + "config.ini");
auto data = propertiesData->GetGeneralPropertiesData();
if(!file.Create()) {
if(!file.Open()) return false;
wxString line;
for(line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine()) {
wxString tag = "";
wxString tagValue = "";
bool parseValue = false;
for(unsigned int i = 0; i < line.Len(); ++i) {
if(line[i] == '=') {
parseValue = true;
} else {
if(parseValue)
tagValue += line[i];
else
tag += line[i];
}
}
// Language
if(tag == "lang") {
if(tagValue == "pt-br" || tagValue == "pt") {
data.language = wxLANGUAGE_PORTUGUESE_BRAZILIAN;
} else if(tagValue == "en" || tagValue == "en-us" || tagValue == "en-uk") {
data.language = wxLANGUAGE_ENGLISH;
}
}
if(tag == "theme") {
if(tagValue == "light") {
data.theme = THEME_LIGHT;
} else if(tagValue == "dark") {
data.theme = THEME_DARK;
}
}
if (tag == "useOpenGL") {
if (tagValue == "yes") {
data.useOpenGL = true;
}
else if (tagValue == "no") {
data.useOpenGL = false;
}
}
}
file.Close();
} else { // Create default init file.
if(!file.Open()) return false;
// Default parameters.
file.AddLine("lang=en");
file.AddLine("theme=light");
file.AddLine("useOpenGL=yes");
file.Write();
file.Close();
data.language = wxLANGUAGE_ENGLISH;
data.theme = THEME_LIGHT;
data.useOpenGL = true;
propertiesData->SetGeneralPropertiesData(data);
}
propertiesData->SetGeneralPropertiesData(data);
return true;
}
void LoadCatalogs(wxLocale* locale, PropertiesData* propertiesData)
{
// Load language catalogs according the propertiesData attribute.
if(!locale->Init(propertiesData->GetGeneralPropertiesData().language, wxLOCALE_DONT_LOAD_DEFAULT)) {
wxMessageDialog msgDialog(nullptr, _("This language is not supported by the system."), _("Error"),
wxOK | wxCENTRE | wxICON_ERROR);
msgDialog.ShowModal();
}
wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
wxString langPath = fn.GetPath() + wxFileName::DirName("\\..\\data\\lang", wxPATH_WIN).GetPath();
locale->AddCatalogLookupPathPrefix(langPath);
// pt_BR
if(propertiesData->GetGeneralPropertiesData().language == wxLANGUAGE_PORTUGUESE_BRAZILIAN) {
if(!locale->AddCatalog(wxT("pt_BR"))) {
wxMessageDialog msgDialog(nullptr, _("Fail to load brazilian portuguese language catalog."), _("Error"),
wxOK | wxCENTRE | wxICON_ERROR);
msgDialog.ShowModal();
}
}
}
virtual bool OnInit()
{
// Add image handlers
wxImage::AddHandler(new wxPNGHandler);
wxImage::AddHandler(new wxJPEGHandler);
PropertiesData* propertiesData = new PropertiesData();
LoadInitFile(propertiesData);
wxLocale* locale = new wxLocale();
LoadCatalogs(locale, propertiesData);
wxString openFilePath = "";
// Load command line attributes. This is used to directly open saved files (.psp).
wxCmdLineParser cmdLineParser(wxApp::argc, wxApp::argv);
//cmdLineParser.SetDesc(cmdLineDesc);
cmdLineParser.AddParam("", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
cmdLineParser.AddSwitch("t", "test", "Run PSP-UFU tests");
bool test = false;
if(cmdLineParser.Parse() == 0) {
wxCmdLineArgs args = cmdLineParser.GetArguments();
for(auto it = args.begin(), itEnd = args.end(); it != itEnd; ++it) {
if(it->GetKind() == wxCMD_LINE_PARAM) { openFilePath = it->GetStrVal();}
else if(it->GetShortName() == "t") {
test = true;
}
}
}
MainFrame* mainFrame = new MainFrame(nullptr, locale, propertiesData, openFilePath);
#ifdef __WXMSW__
// Set application icon for windows
mainFrame->SetIcon(wxICON(aaaaprogicon));
#endif
SetTopWindow(mainFrame);
if(test) {
GetTopWindow()->Show();
exit(mainFrame->RunPSPTest());
}
return GetTopWindow()->Show();
}
virtual int OnExit()
{
// clean up
return 0;
}
/*virtual int OnRun()
{
int exitcode = wxApp::OnRun();
// wxTheClipboard->Flush();
if(exitcode != 0) return exitcode;
}
virtual void OnInitCmdLine(wxCmdLineParser& parser)
{
parser.SetDesc(g_cmdLineDesc);
// must refuse '/' as parameter starter or cannot use "/path" style paths
parser.SetSwitchChars(wxT("-"));
}
virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
{
//silent_mode = parser.Found(wxT("s"));
// to get at your unnamed parameters use
wxArrayString files;
for(unsigned int i = 0; i < parser.GetParamCount(); i++) { files.Add(parser.GetParam(i));
wxMessageBox(parser.GetParam(i)); }
// and other command line parameters
// then do what you need with them.
return true;
}*/
};
DECLARE_APP(MainApp)
IMPLEMENT_APP(MainApp)
|