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