]>
Commit | Line | Data |
---|---|---|
56d2f750 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wxrc.cpp | |
3 | // Purpose: XML resource compiler | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2000/03/05 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2000 Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
ab7ce33c | 11 | #if defined(__GNUG__) && !defined(__APPLE__) |
56d2f750 VS |
12 | #pragma implementation |
13 | #pragma interface | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
07ee782b | 23 | // for all others, include the necessary headers |
56d2f750 | 24 | #ifndef WX_PRECOMP |
07ee782b MB |
25 | #include "wx/app.h" |
26 | #include "wx/log.h" | |
56d2f750 VS |
27 | #endif |
28 | ||
29 | #include "wx/cmdline.h" | |
cecc483e | 30 | #include "wx/xml/xml.h" |
56d2f750 | 31 | #include "wx/ffile.h" |
4249ec2c | 32 | #include "wx/filename.h" |
f6853b4a VS |
33 | #include "wx/wfstream.h" |
34 | ||
35 | ||
cecc483e | 36 | class XmlResApp : public wxAppConsole |
56d2f750 VS |
37 | { |
38 | public: | |
6fcef5ed VS |
39 | // don't use builtin cmd line parsing: |
40 | virtual bool OnInit() { return true; } | |
031dfec8 | 41 | |
56d2f750 VS |
42 | virtual int OnRun(); |
43 | ||
44 | private: | |
45 | ||
46 | void ParseParams(const wxCmdLineParser& cmdline); | |
47 | void CompileRes(); | |
48 | wxArrayString PrepareTempFiles(); | |
f6853b4a VS |
49 | void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath); |
50 | ||
a7501aeb | 51 | wxString GetInternalFileName(const wxString& name, const wxArrayString& flist); |
56d2f750 VS |
52 | void DeleteTempFiles(const wxArrayString& flist); |
53 | void MakePackageZIP(const wxArrayString& flist); | |
54 | void MakePackageCPP(const wxArrayString& flist); | |
b8b8c49b | 55 | void MakePackagePython(const wxArrayString& flist); |
c8b7a961 VS |
56 | |
57 | void OutputGettext(); | |
58 | wxArrayString FindStrings(); | |
59 | wxArrayString FindStrings(wxXmlNode *node); | |
56d2f750 | 60 | |
b8b8c49b | 61 | bool flagVerbose, flagCPP, flagPython, flagGettext; |
56d2f750 VS |
62 | wxString parOutput, parFuncname, parOutputPath; |
63 | wxArrayString parFiles; | |
64 | int retCode; | |
65 | }; | |
66 | ||
80b2db4e | 67 | IMPLEMENT_APP_CONSOLE(XmlResApp) |
56d2f750 VS |
68 | |
69 | int XmlResApp::OnRun() | |
70 | { | |
71 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
72 | { | |
2b5f62a0 | 73 | { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"), |
99cd20be | 74 | wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, |
2b5f62a0 VZ |
75 | { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") }, |
76 | { wxCMD_LINE_SWITCH, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") }, | |
77 | { wxCMD_LINE_SWITCH, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") }, | |
78 | { wxCMD_LINE_SWITCH, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") }, | |
79 | { wxCMD_LINE_OPTION, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") }, | |
80 | { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") }, | |
99cd20be | 81 | #if 0 // not yet implemented |
2b5f62a0 | 82 | { wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" }, |
99cd20be | 83 | #endif |
2b5f62a0 | 84 | { wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"), |
99cd20be VS |
85 | wxCMD_LINE_VAL_STRING, |
86 | wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY }, | |
56d2f750 VS |
87 | |
88 | { wxCMD_LINE_NONE } | |
89 | }; | |
90 | ||
91 | wxCmdLineParser parser(cmdLineDesc, argc, argv); | |
92 | ||
93 | switch (parser.Parse()) | |
94 | { | |
95 | case -1: | |
96 | return 0; | |
0a0be6d5 JS |
97 | #if 0 |
98 | // break is unreachable because of return earlier | |
56d2f750 | 99 | break; |
0a0be6d5 | 100 | #endif |
56d2f750 VS |
101 | |
102 | case 0: | |
103 | retCode = 0; | |
104 | ParseParams(parser); | |
c8b7a961 VS |
105 | if (flagGettext) |
106 | OutputGettext(); | |
107 | else | |
108 | CompileRes(); | |
56d2f750 | 109 | return retCode; |
0a0be6d5 JS |
110 | #if 0 |
111 | // break is unreachable because of return earlier | |
56d2f750 | 112 | break; |
0a0be6d5 | 113 | #endif |
56d2f750 | 114 | |
0a0be6d5 JS |
115 | #if 0 |
116 | // default return moved outside of switch to avoid warning about lack of return in function | |
56d2f750 VS |
117 | default: |
118 | return 1; | |
119 | break; | |
0a0be6d5 | 120 | #endif |
56d2f750 | 121 | } |
0a0be6d5 | 122 | return 1; |
56d2f750 VS |
123 | } |
124 | ||
125 | ||
126 | ||
127 | ||
128 | void XmlResApp::ParseParams(const wxCmdLineParser& cmdline) | |
129 | { | |
2b5f62a0 VZ |
130 | flagGettext = cmdline.Found(_T("g")); |
131 | flagVerbose = cmdline.Found(_T("v")); | |
132 | flagCPP = cmdline.Found(_T("c")); | |
133 | flagPython = cmdline.Found(_T("p")); | |
56d2f750 | 134 | |
2b5f62a0 | 135 | if (!cmdline.Found(_T("o"), &parOutput)) |
c8b7a961 VS |
136 | { |
137 | if (flagGettext) | |
138 | parOutput = wxEmptyString; | |
139 | else | |
b8b8c49b VS |
140 | { |
141 | if (flagCPP) | |
2b5f62a0 | 142 | parOutput = _T("resource.cpp"); |
b8b8c49b | 143 | else if (flagPython) |
2b5f62a0 | 144 | parOutput = _T("resource.py"); |
b8b8c49b | 145 | else |
2b5f62a0 | 146 | parOutput = _T("resource.xrs"); |
b8b8c49b | 147 | } |
c8b7a961 | 148 | } |
4249ec2c VS |
149 | wxFileName fn(parOutput); |
150 | fn.Normalize(); | |
151 | parOutput = fn.GetFullPath(); | |
56d2f750 | 152 | parOutputPath = wxPathOnly(parOutput); |
2b5f62a0 | 153 | if (!parOutputPath) parOutputPath = _T("."); |
56d2f750 | 154 | |
2b5f62a0 VZ |
155 | if (!cmdline.Found(_T("n"), &parFuncname)) |
156 | parFuncname = _T("InitXmlResource"); | |
56d2f750 VS |
157 | |
158 | for (size_t i = 0; i < cmdline.GetParamCount(); i++) | |
f65a69e9 VS |
159 | { |
160 | #ifdef __WINDOWS__ | |
161 | wxString fn=wxFindFirstFile(cmdline.GetParam(i), wxFILE); | |
162 | while (!fn.IsEmpty()) | |
163 | { | |
164 | parFiles.Add(fn); | |
165 | fn=wxFindNextFile(); | |
166 | } | |
167 | #else | |
56d2f750 | 168 | parFiles.Add(cmdline.GetParam(i)); |
f65a69e9 VS |
169 | #endif |
170 | } | |
56d2f750 VS |
171 | } |
172 | ||
173 | ||
174 | ||
175 | ||
176 | void XmlResApp::CompileRes() | |
177 | { | |
178 | wxArrayString files = PrepareTempFiles(); | |
179 | ||
180 | wxRemoveFile(parOutput); | |
181 | ||
56d2f750 VS |
182 | if (!retCode) |
183 | { | |
184 | if (flagCPP) | |
185 | MakePackageCPP(files); | |
b8b8c49b VS |
186 | else if (flagPython) |
187 | MakePackagePython(files); | |
56d2f750 VS |
188 | else |
189 | MakePackageZIP(files); | |
190 | } | |
191 | ||
192 | DeleteTempFiles(files); | |
193 | } | |
194 | ||
195 | ||
a7501aeb VS |
196 | wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist) |
197 | { | |
198 | wxString name2 = name; | |
2b5f62a0 VZ |
199 | name2.Replace(_T(":"), _T("_")); |
200 | name2.Replace(_T("/"), _T("_")); | |
201 | name2.Replace(_T("\\"), _T("_")); | |
202 | name2.Replace(_T("*"), _T("_")); | |
203 | name2.Replace(_T("?"), _T("_")); | |
a7501aeb | 204 | |
2b5f62a0 | 205 | wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2; |
a7501aeb VS |
206 | |
207 | if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND) | |
208 | { | |
209 | for (int i = 0;; i++) | |
210 | { | |
2b5f62a0 | 211 | s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i); |
a7501aeb VS |
212 | if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND) |
213 | break; | |
214 | } | |
215 | } | |
216 | return s; | |
217 | } | |
56d2f750 VS |
218 | |
219 | wxArrayString XmlResApp::PrepareTempFiles() | |
220 | { | |
221 | wxArrayString flist; | |
222 | ||
223 | for (size_t i = 0; i < parFiles.Count(); i++) | |
224 | { | |
225 | if (flagVerbose) | |
2b5f62a0 | 226 | wxPrintf(_T("processing ") + parFiles[i] + _T("...\n")); |
56d2f750 VS |
227 | |
228 | wxXmlDocument doc; | |
229 | ||
230 | if (!doc.Load(parFiles[i])) | |
231 | { | |
2b5f62a0 | 232 | wxLogError(_T("Error parsing file ") + parFiles[i]); |
56d2f750 VS |
233 | retCode = 1; |
234 | continue; | |
235 | } | |
236 | ||
f6853b4a VS |
237 | wxString name, ext, path; |
238 | wxSplitPath(parFiles[i], &path, &name, &ext); | |
239 | ||
240 | FindFilesInXML(doc.GetRoot(), flist, path); | |
56d2f750 | 241 | |
a7501aeb VS |
242 | wxString internalName = GetInternalFileName(parFiles[i], flist); |
243 | ||
4249ec2c | 244 | doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName); |
a7501aeb | 245 | flist.Add(internalName); |
56d2f750 VS |
246 | } |
247 | ||
248 | return flist; | |
249 | } | |
250 | ||
251 | ||
4249ec2c VS |
252 | // Does 'node' contain filename information at all? |
253 | static bool NodeContainsFilename(wxXmlNode *node) | |
254 | { | |
255 | // Any bitmaps: | |
256 | if (node->GetName() == _T("bitmap")) | |
257 | return TRUE; | |
258 | ||
259 | // URLs in wxHtmlWindow: | |
260 | if (node->GetName() == _T("url")) | |
261 | return TRUE; | |
262 | ||
263 | // wxBitmapButton: | |
264 | wxXmlNode *parent = node->GetParent(); | |
265 | if (parent != NULL && | |
266 | parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") && | |
267 | (node->GetName() == _T("focus") || | |
268 | node->GetName() == _T("disabled") || | |
269 | node->GetName() == _T("selected"))) | |
270 | return TRUE; | |
271 | ||
272 | // wxBitmap or wxIcon toplevel resources: | |
273 | if (node->GetName() == _T("object")) | |
274 | { | |
275 | wxString klass = node->GetPropVal(_T("class"), wxEmptyString); | |
276 | if (klass == _T("wxBitmap") || klass == _T("wxIcon")) | |
277 | return TRUE; | |
278 | } | |
279 | ||
280 | return FALSE; | |
281 | } | |
56d2f750 | 282 | |
f6853b4a VS |
283 | // find all files mentioned in structure, e.g. <bitmap>filename</bitmap> |
284 | void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath) | |
285 | { | |
2b5f62a0 VZ |
286 | // Is 'node' XML node element? |
287 | if (node == NULL) return; | |
288 | if (node->GetType() != wxXML_ELEMENT_NODE) return; | |
289 | ||
4249ec2c | 290 | bool containsFilename = NodeContainsFilename(node); |
2b5f62a0 VZ |
291 | |
292 | wxXmlNode *n = node->GetChildren(); | |
f6853b4a VS |
293 | while (n) |
294 | { | |
2b5f62a0 | 295 | if (containsFilename && |
f6853b4a | 296 | (n->GetType() == wxXML_TEXT_NODE || |
2b5f62a0 | 297 | n->GetType() == wxXML_CDATA_SECTION_NODE)) |
f6853b4a VS |
298 | { |
299 | wxString fullname; | |
2b5f62a0 VZ |
300 | if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty()) |
301 | fullname = n->GetContent(); | |
302 | else | |
4249ec2c | 303 | fullname = inputPath + wxFILE_SEP_PATH + n->GetContent(); |
a7501aeb VS |
304 | |
305 | if (flagVerbose) | |
2b5f62a0 VZ |
306 | wxPrintf(_T("adding ") + fullname + _T("...\n")); |
307 | ||
a7501aeb | 308 | wxString filename = GetInternalFileName(n->GetContent(), flist); |
f6853b4a | 309 | n->SetContent(filename); |
f6853b4a | 310 | |
2b5f62a0 VZ |
311 | if (flist.Index(filename) == wxNOT_FOUND) |
312 | flist.Add(filename); | |
f6853b4a VS |
313 | |
314 | wxFileInputStream sin(fullname); | |
4249ec2c | 315 | wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename); |
f6853b4a VS |
316 | sin.Read(sout); // copy the stream |
317 | } | |
2b5f62a0 | 318 | |
f6853b4a VS |
319 | // subnodes: |
320 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
321 | FindFilesInXML(n, flist, inputPath); | |
2b5f62a0 | 322 | |
f6853b4a VS |
323 | n = n->GetNext(); |
324 | } | |
325 | } | |
326 | ||
327 | ||
328 | ||
56d2f750 VS |
329 | void XmlResApp::DeleteTempFiles(const wxArrayString& flist) |
330 | { | |
331 | for (size_t i = 0; i < flist.Count(); i++) | |
4249ec2c | 332 | wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]); |
56d2f750 VS |
333 | } |
334 | ||
335 | ||
336 | ||
337 | void XmlResApp::MakePackageZIP(const wxArrayString& flist) | |
338 | { | |
339 | wxString files; | |
340 | ||
341 | for (size_t i = 0; i < flist.Count(); i++) | |
2b5f62a0 | 342 | files += flist[i] + _T(" "); |
56d2f750 VS |
343 | files.RemoveLast(); |
344 | ||
345 | if (flagVerbose) | |
2b5f62a0 | 346 | wxPrintf(_T("compressing ") + parOutput + _T("...\n")); |
4249ec2c VS |
347 | |
348 | wxString cwd = wxGetCwd(); | |
349 | wxSetWorkingDirectory(parOutputPath); | |
350 | int execres = wxExecute(_T("zip -9 -j ") + | |
351 | wxString(flagVerbose ? _T("") : _T("-q ")) + | |
352 | parOutput + _T(" ") + files, TRUE); | |
353 | wxSetWorkingDirectory(cwd); | |
354 | if (execres == -1) | |
56d2f750 | 355 | { |
2b5f62a0 VZ |
356 | wxLogError(_T("Unable to execute zip program. Make sure it is in the path.")); |
357 | wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/")); | |
56d2f750 VS |
358 | retCode = 1; |
359 | return; | |
360 | } | |
361 | } | |
362 | ||
363 | ||
364 | ||
365 | ||
366 | static wxString FileToCppArray(wxString filename, int num) | |
367 | { | |
368 | wxString output; | |
56d2f750 | 369 | wxString tmp; |
f6853b4a | 370 | wxString snum; |
5851504d | 371 | wxFFile file(filename, wxT("rb")); |
56d2f750 VS |
372 | size_t lng = file.Length(); |
373 | ||
2b5f62a0 VZ |
374 | snum.Printf(_T("%i"), num); |
375 | output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng); | |
376 | output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n"); | |
e066e256 VS |
377 | // we cannot use string literals because MSVC is dumb wannabe compiler |
378 | // with arbitrary limitation to 2048 strings :( | |
56d2f750 VS |
379 | |
380 | unsigned char *buffer = new unsigned char[lng]; | |
381 | file.Read(buffer, lng); | |
382 | ||
f6853b4a | 383 | for (size_t i = 0, linelng = 0; i < lng; i++) |
56d2f750 | 384 | { |
2b5f62a0 VZ |
385 | tmp.Printf(_T("%i"), buffer[i]); |
386 | if (i != 0) output << _T(','); | |
e066e256 | 387 | if (linelng > 70) |
f6853b4a VS |
388 | { |
389 | linelng = 0; | |
2b5f62a0 | 390 | output << _T("\n"); |
f6853b4a | 391 | } |
e066e256 VS |
392 | output << tmp; |
393 | linelng += tmp.Length()+1; | |
56d2f750 VS |
394 | } |
395 | ||
396 | delete[] buffer; | |
397 | ||
2b5f62a0 | 398 | output += _T("};\n\n"); |
56d2f750 VS |
399 | |
400 | return output; | |
401 | } | |
402 | ||
403 | ||
404 | void XmlResApp::MakePackageCPP(const wxArrayString& flist) | |
405 | { | |
5851504d | 406 | wxFFile file(parOutput, wxT("wt")); |
56d2f750 VS |
407 | size_t i; |
408 | ||
409 | if (flagVerbose) | |
2b5f62a0 | 410 | wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n")); |
56d2f750 | 411 | |
2b5f62a0 VZ |
412 | file.Write(_T("") |
413 | _T("//\n") | |
414 | _T("// This file was automatically generated by wxrc, do not edit by hand.\n") | |
415 | _T("//\n\n") | |
416 | _T("#include <wx/wxprec.h>\n") | |
417 | _T("\n") | |
418 | _T("#ifdef __BORLANDC__\n") | |
419 | _T(" #pragma hdrstop\n") | |
420 | _T("#endif\n") | |
421 | _T("\n") | |
422 | _T("#ifndef WX_PRECOMP\n") | |
423 | _T(" #include <wx/wx.h>\n") | |
424 | _T("#endif\n") | |
425 | _T("") | |
426 | _T("#include <wx/filesys.h>\n") | |
427 | _T("#include <wx/fs_mem.h>\n") | |
428 | _T("#include <wx/xrc/xmlres.h>\n") | |
429 | _T("#include <wx/xrc/xh_all.h>\n") | |
430 | _T("\n")); | |
56d2f750 VS |
431 | |
432 | for (i = 0; i < flist.Count(); i++) | |
4249ec2c VS |
433 | file.Write( |
434 | FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i)); | |
56d2f750 | 435 | |
2b5f62a0 | 436 | file.Write(_T("") |
5851504d | 437 | _T("void ") + parFuncname + wxT("()\n") |
2b5f62a0 VZ |
438 | _T("{\n") |
439 | _T("\n") | |
440 | _T(" // Check for memory FS. If not present, load the handler:\n") | |
441 | _T(" {\n") | |
5851504d | 442 | _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n") |
2b5f62a0 | 443 | _T(" wxFileSystem fsys;\n") |
5851504d VS |
444 | _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n") |
445 | _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n") | |
2b5f62a0 VZ |
446 | _T(" if (f) delete f;\n") |
447 | _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n") | |
448 | _T(" }\n") | |
449 | _T("\n")); | |
56d2f750 VS |
450 | |
451 | for (i = 0; i < flist.Count(); i++) | |
452 | { | |
453 | wxString s; | |
5851504d VS |
454 | s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] + |
455 | _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i); | |
56d2f750 VS |
456 | file.Write(s); |
457 | } | |
f6853b4a VS |
458 | |
459 | for (i = 0; i < parFiles.Count(); i++) | |
460 | { | |
5851504d VS |
461 | file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") + |
462 | GetInternalFileName(parFiles[i], flist) + _T("\"));\n")); | |
f6853b4a | 463 | } |
56d2f750 | 464 | |
2b5f62a0 | 465 | file.Write(_T("}\n")); |
56d2f750 | 466 | |
f6853b4a | 467 | |
56d2f750 | 468 | } |
c8b7a961 | 469 | |
b8b8c49b VS |
470 | static wxString FileToPythonArray(wxString filename, int num) |
471 | { | |
472 | wxString output; | |
473 | wxString tmp; | |
474 | wxString snum; | |
5851504d | 475 | wxFFile file(filename, wxT("rb")); |
b8b8c49b VS |
476 | size_t lng = file.Length(); |
477 | ||
2b5f62a0 VZ |
478 | snum.Printf(_T("%i"), num); |
479 | output = _T(" xml_res_file_") + snum + _T(" = \"\"\"\\\n"); | |
b8b8c49b VS |
480 | |
481 | unsigned char *buffer = new unsigned char[lng]; | |
482 | file.Read(buffer, lng); | |
483 | ||
484 | for (size_t i = 0, linelng = 0; i < lng; i++) | |
485 | { | |
486 | unsigned char c = buffer[i]; | |
487 | if (c == '\n') | |
488 | { | |
489 | tmp = (wxChar)c; | |
490 | linelng = 0; | |
491 | } | |
492 | else if (c < 32 || c > 127) | |
2b5f62a0 | 493 | tmp.Printf(_T("\\x%02x"), c); |
b8b8c49b | 494 | else if (c == '\\') |
2b5f62a0 | 495 | tmp = _T("\\\\"); |
b8b8c49b VS |
496 | else |
497 | tmp = (wxChar)c; | |
498 | if (linelng > 70) | |
499 | { | |
500 | linelng = 0; | |
2b5f62a0 | 501 | output << _T("\\\n"); |
b8b8c49b VS |
502 | } |
503 | output << tmp; | |
504 | linelng += tmp.Length(); | |
505 | } | |
506 | ||
507 | delete[] buffer; | |
508 | ||
2b5f62a0 | 509 | output += _T("\"\"\"\n\n"); |
b8b8c49b VS |
510 | |
511 | return output; | |
512 | } | |
513 | ||
514 | ||
515 | void XmlResApp::MakePackagePython(const wxArrayString& flist) | |
516 | { | |
5851504d | 517 | wxFFile file(parOutput, wxT("wt")); |
b8b8c49b VS |
518 | size_t i; |
519 | ||
520 | if (flagVerbose) | |
2b5f62a0 | 521 | wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n")); |
b8b8c49b VS |
522 | |
523 | file.Write( | |
2b5f62a0 VZ |
524 | _T("#\n") |
525 | _T("# This file was automatically generated by wxrc, do not edit by hand.\n") | |
526 | _T("#\n\n") | |
527 | _T("from wxPython.wx import *\n") | |
528 | _T("from wxPython.xrc import *\n\n") | |
b8b8c49b VS |
529 | ); |
530 | ||
531 | ||
2b5f62a0 | 532 | file.Write(_T("def ") + parFuncname + _T("():\n")); |
b8b8c49b VS |
533 | |
534 | for (i = 0; i < flist.Count(); i++) | |
4249ec2c VS |
535 | file.Write( |
536 | FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i)); | |
b8b8c49b VS |
537 | |
538 | for (i = 0; i < flist.Count(); i++) | |
539 | { | |
540 | wxString s; | |
2b5f62a0 | 541 | s.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i); |
b8b8c49b VS |
542 | file.Write(s); |
543 | } | |
544 | } | |
545 | ||
c8b7a961 VS |
546 | |
547 | ||
548 | void XmlResApp::OutputGettext() | |
549 | { | |
550 | wxArrayString str = FindStrings(); | |
551 | ||
552 | wxFFile fout; | |
553 | if (!parOutput) fout.Attach(stdout); | |
5851504d | 554 | else fout.Open(parOutput, wxT("wt")); |
c8b7a961 VS |
555 | |
556 | for (size_t i = 0; i < str.GetCount(); i++) | |
0653d364 | 557 | fout.Write(_T("_(\"") + str[i] + _T("\");\n")); |
c8b7a961 VS |
558 | |
559 | if (!parOutput) fout.Detach(); | |
560 | } | |
561 | ||
562 | ||
563 | ||
564 | wxArrayString XmlResApp::FindStrings() | |
565 | { | |
566 | wxArrayString arr, a2; | |
567 | ||
568 | for (size_t i = 0; i < parFiles.Count(); i++) | |
569 | { | |
570 | if (flagVerbose) | |
2b5f62a0 | 571 | wxPrintf(_T("processing ") + parFiles[i] + _T("...\n")); |
c8b7a961 VS |
572 | |
573 | wxXmlDocument doc; | |
574 | if (!doc.Load(parFiles[i])) | |
575 | { | |
2b5f62a0 | 576 | wxLogError(_T("Error parsing file ") + parFiles[i]); |
c8b7a961 VS |
577 | retCode = 1; |
578 | continue; | |
579 | } | |
580 | a2 = FindStrings(doc.GetRoot()); | |
581 | WX_APPEND_ARRAY(arr, a2); | |
582 | } | |
583 | ||
584 | return arr; | |
585 | } | |
586 | ||
587 | ||
588 | ||
c109ef11 VS |
589 | static wxString ConvertText(const wxString& str) |
590 | { | |
591 | wxString str2; | |
592 | const wxChar *dt; | |
593 | ||
594 | for (dt = str.c_str(); *dt; dt++) | |
595 | { | |
596 | if (*dt == wxT('_')) | |
597 | { | |
598 | if ( *(++dt) == wxT('_') ) | |
599 | str2 << wxT('_'); | |
600 | else | |
601 | str2 << wxT('&') << *dt; | |
602 | } | |
603 | else | |
604 | { | |
605 | switch (*dt) | |
606 | { | |
607 | case wxT('\n') : str2 << wxT("\\n"); break; | |
608 | case wxT('\t') : str2 << wxT("\\t"); break; | |
609 | case wxT('\r') : str2 << wxT("\\r"); break; | |
2b5f62a0 VZ |
610 | case wxT('\\') : if ((*(dt+1) != 'n') && |
611 | (*(dt+1) != 't') && | |
612 | (*(dt+1) != 'r')) | |
613 | str2 << wxT("\\\\"); | |
614 | else | |
615 | str2 << wxT("\\"); | |
616 | break; | |
904a226c | 617 | case wxT('"') : str2 << wxT("\\\""); break; |
c109ef11 VS |
618 | default : str2 << *dt; break; |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | return str2; | |
624 | } | |
625 | ||
626 | ||
c8b7a961 VS |
627 | wxArrayString XmlResApp::FindStrings(wxXmlNode *node) |
628 | { | |
629 | wxArrayString arr; | |
630 | ||
631 | wxXmlNode *n = node; | |
632 | if (n == NULL) return arr; | |
633 | n = n->GetChildren(); | |
634 | ||
635 | while (n) | |
636 | { | |
637 | if ((node->GetType() == wxXML_ELEMENT_NODE) && | |
638 | // parent is an element, i.e. has subnodes... | |
639 | (n->GetType() == wxXML_TEXT_NODE || | |
640 | n->GetType() == wxXML_CDATA_SECTION_NODE) && | |
641 | // ...it is textnode... | |
642 | ( | |
643 | node/*not n!*/->GetName() == _T("label") || | |
644 | (node/*not n!*/->GetName() == _T("value") && | |
645 | !n->GetContent().IsNumber()) || | |
646 | node/*not n!*/->GetName() == _T("help") || | |
647 | node/*not n!*/->GetName() == _T("longhelp") || | |
648 | node/*not n!*/->GetName() == _T("tooltip") || | |
649 | node/*not n!*/->GetName() == _T("htmlcode") || | |
0653d364 VS |
650 | node/*not n!*/->GetName() == _T("title") || |
651 | node/*not n!*/->GetName() == _T("item") | |
c8b7a961 | 652 | )) |
c109ef11 | 653 | // ...and known to contain translatable string |
c8b7a961 | 654 | { |
c109ef11 | 655 | arr.Add(ConvertText(n->GetContent())); |
c8b7a961 VS |
656 | } |
657 | ||
658 | // subnodes: | |
659 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
660 | { | |
661 | wxArrayString a2 = FindStrings(n); | |
662 | WX_APPEND_ARRAY(arr, a2); | |
663 | } | |
664 | ||
665 | n = n->GetNext(); | |
666 | } | |
667 | return arr; | |
668 | } |