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