]> git.saurik.com Git - wxWidgets.git/blame - contrib/utils/wxrc/wxrc.cpp
basic UDP support (patch 835128)
[wxWidgets.git] / contrib / utils / wxrc / wxrc.cpp
CommitLineData
56d2f750
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: wxrc.cpp
3// Purpose: XML resource compiler
1dce6f09 4// Author: Vaclav Slavik, Eduardo Marques <edrdo@netcabo.pt>
56d2f750
VS
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
1dce6f09
VS
36class XRCWidgetData
37{
38public:
f80ea77b 39 XRCWidgetData(const wxString& vname,const wxString& vclass)
1dce6f09
VS
40 : m_class(vclass), m_name(vname) {}
41 const wxString& GetName() const { return m_name; }
42 const wxString& GetClass() const { return m_class; }
43private:
44 wxString m_class;
45 wxString m_name;
46};
47#include "wx/arrimpl.cpp"
48WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData);
49WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData);
50
51class XRCWndClassData
52{
f80ea77b 53private:
1dce6f09
VS
54 wxString m_className;
55 wxString m_parentClassName;
56 ArrayOfXRCWidgetData m_wdata;
f80ea77b 57
1dce6f09
VS
58 void BrowseXmlNode(wxXmlNode* node)
59 {
60 wxString classValue;
61 wxString nameValue;
f80ea77b 62 wxXmlNode* children;
1dce6f09
VS
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)
f80ea77b 73 BrowseXmlNode(children);
1dce6f09
VS
74 node = node->GetNext();
75 }
76 }
f80ea77b 77
1dce6f09 78public:
aa063b24 79 XRCWndClassData(const wxString& className,const wxString& parentClassName, const wxXmlNode* node) :
f80ea77b
WS
80 m_className(className) , m_parentClassName(parentClassName) {
81
aa063b24 82 BrowseXmlNode(node->GetChildren());
f80ea77b 83
aa063b24 84 }
f80ea77b 85
aa063b24
RD
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,\"")
f80ea77b 102 + m_className
aa063b24 103 + _T("\",\"")
f80ea77b
WS
104 + m_parentClassName
105 + _T("\");\n"));
aa063b24
RD
106 for(i=0;i<m_wdata.Count();++i){
107 const XRCWidgetData& w = m_wdata.Item(i);
f80ea77b
WS
108 file.Write( _T(" ")
109 + w.GetName()
aa063b24 110 + _T(" = XRCCTRL(*this,\"")
f80ea77b 111 + w.GetName()
aa063b24
RD
112 + _T("\",")
113 + w.GetClass()
114 + _T(");\n")
115 );
116 }
1dce6f09 117file.Write(_T(" }\n"));
f80ea77b 118
1dce6f09 119file.Write(
aa063b24 120 _T("public:\n")
f80ea77b 121 + m_className
aa063b24
RD
122 + _T("::")
123 + m_className
124 + _T("(){\n")
125 + _T(" InitWidgetsFromXRC();\n")
126 _T(" }\n")
127 _T("};\n"));
128 };
1dce6f09
VS
129};
130WX_DECLARE_OBJARRAY(XRCWndClassData,ArrayOfXRCWndClassData);
131WX_DEFINE_OBJARRAY(ArrayOfXRCWndClassData);
132
133
cecc483e 134class XmlResApp : public wxAppConsole
56d2f750
VS
135{
136public:
6fcef5ed 137 // don't use builtin cmd line parsing:
f80ea77b 138 virtual bool OnInit() { return true; }
56d2f750 139 virtual int OnRun();
f80ea77b
WS
140
141private:
56d2f750
VS
142 void ParseParams(const wxCmdLineParser& cmdline);
143 void CompileRes();
144 wxArrayString PrepareTempFiles();
f6853b4a
VS
145 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
146
a7501aeb 147 wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
56d2f750
VS
148 void DeleteTempFiles(const wxArrayString& flist);
149 void MakePackageZIP(const wxArrayString& flist);
150 void MakePackageCPP(const wxArrayString& flist);
b8b8c49b 151 void MakePackagePython(const wxArrayString& flist);
c8b7a961
VS
152
153 void OutputGettext();
154 wxArrayString FindStrings();
155 wxArrayString FindStrings(wxXmlNode *node);
f80ea77b 156
b8b8c49b 157 bool flagVerbose, flagCPP, flagPython, flagGettext;
56d2f750
VS
158 wxString parOutput, parFuncname, parOutputPath;
159 wxArrayString parFiles;
160 int retCode;
1dce6f09
VS
161
162 ArrayOfXRCWndClassData aXRCWndClassData;
aa063b24
RD
163 bool flagH;
164 void GenCPPHeader();
56d2f750
VS
165};
166
80b2db4e 167IMPLEMENT_APP_CONSOLE(XmlResApp)
56d2f750
VS
168
169int XmlResApp::OnRun()
170{
171 static const wxCmdLineEntryDesc cmdLineDesc[] =
172 {
f80ea77b 173 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"),
99cd20be 174 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
2b5f62a0 175 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
aa063b24 176 { wxCMD_LINE_SWITCH, _T("e"), _T("extra-cpp-code"), _T("output C++ header file with XRC derived classes") },
2b5f62a0
VZ
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]") },
99cd20be 182#if 0 // not yet implemented
2b5f62a0 183 { wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
99cd20be 184#endif
2b5f62a0 185 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"),
f80ea77b 186 wxCMD_LINE_VAL_STRING,
99cd20be 187 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
56d2f750
VS
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;
0a0be6d5
JS
198 #if 0
199 // break is unreachable because of return earlier
56d2f750 200 break;
0a0be6d5 201 #endif
56d2f750
VS
202
203 case 0:
204 retCode = 0;
205 ParseParams(parser);
c8b7a961
VS
206 if (flagGettext)
207 OutputGettext();
208 else
209 CompileRes();
56d2f750 210 return retCode;
0a0be6d5
JS
211 #if 0
212 // break is unreachable because of return earlier
56d2f750 213 break;
0a0be6d5 214 #endif
56d2f750 215
0a0be6d5
JS
216 #if 0
217 // default return moved outside of switch to avoid warning about lack of return in function
56d2f750
VS
218 default:
219 return 1;
220 break;
0a0be6d5 221 #endif
56d2f750 222 }
0a0be6d5 223 return 1;
56d2f750
VS
224}
225
226
227
228
229void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
230{
2b5f62a0
VZ
231 flagGettext = cmdline.Found(_T("g"));
232 flagVerbose = cmdline.Found(_T("v"));
233 flagCPP = cmdline.Found(_T("c"));
234 flagPython = cmdline.Found(_T("p"));
1dce6f09
VS
235 flagH = flagCPP && cmdline.Found(_T("e"));
236
56d2f750 237
f80ea77b 238 if (!cmdline.Found(_T("o"), &parOutput))
c8b7a961
VS
239 {
240 if (flagGettext)
241 parOutput = wxEmptyString;
242 else
b8b8c49b
VS
243 {
244 if (flagCPP)
2b5f62a0 245 parOutput = _T("resource.cpp");
b8b8c49b 246 else if (flagPython)
2b5f62a0 247 parOutput = _T("resource.py");
b8b8c49b 248 else
2b5f62a0 249 parOutput = _T("resource.xrs");
b8b8c49b 250 }
c8b7a961 251 }
1dce6f09
VS
252 if (!parOutput.empty())
253 {
254 wxFileName fn(parOutput);
255 fn.Normalize();
256 parOutput = fn.GetFullPath();
257 parOutputPath = wxPathOnly(parOutput);
258 }
2b5f62a0 259 if (!parOutputPath) parOutputPath = _T(".");
56d2f750 260
f80ea77b 261 if (!cmdline.Found(_T("n"), &parFuncname))
2b5f62a0 262 parFuncname = _T("InitXmlResource");
56d2f750
VS
263
264 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
f65a69e9
VS
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
56d2f750 274 parFiles.Add(cmdline.GetParam(i));
f65a69e9
VS
275#endif
276 }
56d2f750
VS
277}
278
279
280
281
282void XmlResApp::CompileRes()
283{
284 wxArrayString files = PrepareTempFiles();
285
286 wxRemoveFile(parOutput);
287
56d2f750 288 if (!retCode)
f80ea77b 289 {
1dce6f09 290 if (flagCPP){
56d2f750 291 MakePackageCPP(files);
1dce6f09
VS
292 if (flagH)
293 GenCPPHeader();
294 }
b8b8c49b
VS
295 else if (flagPython)
296 MakePackagePython(files);
56d2f750
VS
297 else
298 MakePackageZIP(files);
299 }
f80ea77b 300
56d2f750
VS
301 DeleteTempFiles(files);
302}
303
304
a7501aeb
VS
305wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
306{
307 wxString name2 = name;
2b5f62a0
VZ
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("_"));
f80ea77b 313
2b5f62a0 314 wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2;
a7501aeb
VS
315
316 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
f80ea77b 317 {
a7501aeb
VS
318 for (int i = 0;; i++)
319 {
2b5f62a0 320 s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i);
a7501aeb
VS
321 if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
322 break;
323 }
324 }
325 return s;
326}
56d2f750
VS
327
328wxArrayString XmlResApp::PrepareTempFiles()
329{
330 wxArrayString flist;
f80ea77b 331
56d2f750
VS
332 for (size_t i = 0; i < parFiles.Count(); i++)
333 {
f80ea77b 334 if (flagVerbose)
2b5f62a0 335 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
56d2f750
VS
336
337 wxXmlDocument doc;
f80ea77b 338
56d2f750
VS
339 if (!doc.Load(parFiles[i]))
340 {
2b5f62a0 341 wxLogError(_T("Error parsing file ") + parFiles[i]);
56d2f750
VS
342 retCode = 1;
343 continue;
344 }
f80ea77b 345
f6853b4a
VS
346 wxString name, ext, path;
347 wxSplitPath(parFiles[i], &path, &name, &ext);
348
349 FindFilesInXML(doc.GetRoot(), flist, path);
1dce6f09
VS
350 if (flagH)
351 {
352 wxXmlNode* node = (doc.GetRoot())->GetChildren();
aa063b24
RD
353 wxString classValue,nameValue;
354 while(node){
1dce6f09 355 if(node->GetName() == _T("object")
aa063b24
RD
356 && node->GetPropVal(_T("class"),&classValue)
357 && node->GetPropVal(_T("name"),&nameValue)){
1dce6f09
VS
358
359 aXRCWndClassData.Add(
aa063b24 360 XRCWndClassData(nameValue,classValue,node)
1dce6f09
VS
361 );
362 }
aa063b24 363 node = node -> GetNext();
1dce6f09
VS
364 }
365 }
a7501aeb 366 wxString internalName = GetInternalFileName(parFiles[i], flist);
f80ea77b 367
4249ec2c 368 doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
a7501aeb 369 flist.Add(internalName);
56d2f750 370 }
f80ea77b 371
56d2f750
VS
372 return flist;
373}
374
375
4249ec2c
VS
376// Does 'node' contain filename information at all?
377static bool NodeContainsFilename(wxXmlNode *node)
378{
379 // Any bitmaps:
380 if (node->GetName() == _T("bitmap"))
f80ea77b 381 return true;
4249ec2c
VS
382
383 // URLs in wxHtmlWindow:
384 if (node->GetName() == _T("url"))
f80ea77b
WS
385 return true;
386
4249ec2c
VS
387 // wxBitmapButton:
388 wxXmlNode *parent = node->GetParent();
f80ea77b 389 if (parent != NULL &&
4249ec2c 390 parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
f80ea77b 391 (node->GetName() == _T("focus") ||
4249ec2c
VS
392 node->GetName() == _T("disabled") ||
393 node->GetName() == _T("selected")))
f80ea77b
WS
394 return true;
395
4249ec2c
VS
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"))
f80ea77b 401 return true;
4249ec2c 402 }
f80ea77b
WS
403
404 return false;
4249ec2c 405}
56d2f750 406
f6853b4a
VS
407// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
408void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
409{
2b5f62a0
VZ
410 // Is 'node' XML node element?
411 if (node == NULL) return;
412 if (node->GetType() != wxXML_ELEMENT_NODE) return;
413
4249ec2c 414 bool containsFilename = NodeContainsFilename(node);
2b5f62a0
VZ
415
416 wxXmlNode *n = node->GetChildren();
f6853b4a
VS
417 while (n)
418 {
2b5f62a0 419 if (containsFilename &&
f80ea77b 420 (n->GetType() == wxXML_TEXT_NODE ||
2b5f62a0 421 n->GetType() == wxXML_CDATA_SECTION_NODE))
f6853b4a
VS
422 {
423 wxString fullname;
2b5f62a0
VZ
424 if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
425 fullname = n->GetContent();
426 else
4249ec2c 427 fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
a7501aeb 428
f80ea77b 429 if (flagVerbose)
2b5f62a0
VZ
430 wxPrintf(_T("adding ") + fullname + _T("...\n"));
431
a7501aeb 432 wxString filename = GetInternalFileName(n->GetContent(), flist);
f6853b4a 433 n->SetContent(filename);
f6853b4a 434
2b5f62a0
VZ
435 if (flist.Index(filename) == wxNOT_FOUND)
436 flist.Add(filename);
f6853b4a
VS
437
438 wxFileInputStream sin(fullname);
4249ec2c 439 wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
f6853b4a
VS
440 sin.Read(sout); // copy the stream
441 }
2b5f62a0 442
f6853b4a
VS
443 // subnodes:
444 if (n->GetType() == wxXML_ELEMENT_NODE)
445 FindFilesInXML(n, flist, inputPath);
2b5f62a0 446
f6853b4a
VS
447 n = n->GetNext();
448 }
449}
450
451
452
56d2f750
VS
453void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
454{
455 for (size_t i = 0; i < flist.Count(); i++)
4249ec2c 456 wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
56d2f750
VS
457}
458
459
460
461void XmlResApp::MakePackageZIP(const wxArrayString& flist)
462{
463 wxString files;
f80ea77b 464
56d2f750 465 for (size_t i = 0; i < flist.Count(); i++)
2b5f62a0 466 files += flist[i] + _T(" ");
56d2f750 467 files.RemoveLast();
f80ea77b
WS
468
469 if (flagVerbose)
2b5f62a0 470 wxPrintf(_T("compressing ") + parOutput + _T("...\n"));
f80ea77b 471
4249ec2c
VS
472 wxString cwd = wxGetCwd();
473 wxSetWorkingDirectory(parOutputPath);
f80ea77b
WS
474 int execres = wxExecute(_T("zip -9 -j ") +
475 wxString(flagVerbose ? _T("") : _T("-q ")) +
476 parOutput + _T(" ") + files, true);
4249ec2c
VS
477 wxSetWorkingDirectory(cwd);
478 if (execres == -1)
56d2f750 479 {
2b5f62a0
VZ
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/"));
56d2f750
VS
482 retCode = 1;
483 return;
484 }
485}
486
487
488
56d2f750
VS
489static wxString FileToCppArray(wxString filename, int num)
490{
491 wxString output;
56d2f750 492 wxString tmp;
f6853b4a 493 wxString snum;
5851504d 494 wxFFile file(filename, wxT("rb"));
56d2f750 495 size_t lng = file.Length();
f80ea77b 496
2b5f62a0
VZ
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");
e066e256
VS
500 // we cannot use string literals because MSVC is dumb wannabe compiler
501 // with arbitrary limitation to 2048 strings :(
f80ea77b 502
56d2f750
VS
503 unsigned char *buffer = new unsigned char[lng];
504 file.Read(buffer, lng);
f80ea77b 505
f6853b4a 506 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 507 {
2b5f62a0
VZ
508 tmp.Printf(_T("%i"), buffer[i]);
509 if (i != 0) output << _T(',');
e066e256 510 if (linelng > 70)
f6853b4a
VS
511 {
512 linelng = 0;
2b5f62a0 513 output << _T("\n");
f6853b4a 514 }
e066e256
VS
515 output << tmp;
516 linelng += tmp.Length()+1;
56d2f750 517 }
f80ea77b 518
56d2f750 519 delete[] buffer;
f80ea77b 520
2b5f62a0 521 output += _T("};\n\n");
f80ea77b 522
56d2f750
VS
523 return output;
524}
525
526
527void XmlResApp::MakePackageCPP(const wxArrayString& flist)
528{
5851504d 529 wxFFile file(parOutput, wxT("wt"));
56d2f750
VS
530 size_t i;
531
f80ea77b 532 if (flagVerbose)
2b5f62a0 533 wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n"));
f80ea77b 534
2b5f62a0
VZ
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"));
56d2f750
VS
554
555 for (i = 0; i < flist.Count(); i++)
4249ec2c
VS
556 file.Write(
557 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
f80ea77b 558
2b5f62a0 559 file.Write(_T("")
5851504d 560_T("void ") + parFuncname + wxT("()\n")
2b5f62a0
VZ
561_T("{\n")
562_T("\n")
563_T(" // Check for memory FS. If not present, load the handler:\n")
564_T(" {\n")
5851504d 565_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
2b5f62a0 566_T(" wxFileSystem fsys;\n")
5851504d
VS
567_T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
568_T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
2b5f62a0
VZ
569_T(" if (f) delete f;\n")
570_T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
571_T(" }\n")
572_T("\n"));
56d2f750
VS
573
574 for (i = 0; i < flist.Count(); i++)
575 {
576 wxString s;
5851504d
VS
577 s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] +
578 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i);
56d2f750
VS
579 file.Write(s);
580 }
f6853b4a
VS
581
582 for (i = 0; i < parFiles.Count(); i++)
583 {
5851504d
VS
584 file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
585 GetInternalFileName(parFiles[i], flist) + _T("\"));\n"));
f6853b4a 586 }
f80ea77b 587
2b5f62a0 588 file.Write(_T("}\n"));
56d2f750 589
f6853b4a 590
56d2f750 591}
c8b7a961 592
1dce6f09
VS
593void XmlResApp::GenCPPHeader()
594{
595 wxString fileSpec = (parOutput.BeforeLast('.')).AfterLast('/');
596 wxString heaFileName = fileSpec + _T(".h");
f80ea77b 597
1dce6f09
VS
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")
f80ea77b 605);
1dce6f09 606 for(size_t i=0;i<aXRCWndClassData.Count();++i){
f80ea77b
WS
607 aXRCWndClassData.Item(i).GenerateHeaderCode(file);
608 }
1dce6f09 609 file.Write(
aa063b24
RD
610 _T("\nvoid \n")
611 + parFuncname
612 + _T("();\n#endif\n"));
1dce6f09
VS
613}
614
b8b8c49b
VS
615static wxString FileToPythonArray(wxString filename, int num)
616{
617 wxString output;
618 wxString tmp;
619 wxString snum;
5851504d 620 wxFFile file(filename, wxT("rb"));
b8b8c49b 621 size_t lng = file.Length();
f80ea77b 622
2b5f62a0
VZ
623 snum.Printf(_T("%i"), num);
624 output = _T(" xml_res_file_") + snum + _T(" = \"\"\"\\\n");
f80ea77b 625
b8b8c49b
VS
626 unsigned char *buffer = new unsigned char[lng];
627 file.Read(buffer, lng);
f80ea77b 628
b8b8c49b
VS
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)
2b5f62a0 638 tmp.Printf(_T("\\x%02x"), c);
b8b8c49b 639 else if (c == '\\')
2b5f62a0 640 tmp = _T("\\\\");
b8b8c49b
VS
641 else
642 tmp = (wxChar)c;
643 if (linelng > 70)
644 {
645 linelng = 0;
2b5f62a0 646 output << _T("\\\n");
b8b8c49b
VS
647 }
648 output << tmp;
649 linelng += tmp.Length();
650 }
f80ea77b 651
b8b8c49b 652 delete[] buffer;
f80ea77b 653
2b5f62a0 654 output += _T("\"\"\"\n\n");
f80ea77b 655
b8b8c49b
VS
656 return output;
657}
658
659
660void XmlResApp::MakePackagePython(const wxArrayString& flist)
661{
5851504d 662 wxFFile file(parOutput, wxT("wt"));
b8b8c49b
VS
663 size_t i;
664
f80ea77b 665 if (flagVerbose)
2b5f62a0 666 wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
f80ea77b 667
b8b8c49b 668 file.Write(
2b5f62a0
VZ
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")
b8b8c49b
VS
674 );
675
f80ea77b 676
2b5f62a0 677 file.Write(_T("def ") + parFuncname + _T("():\n"));
b8b8c49b
VS
678
679 for (i = 0; i < flist.Count(); i++)
4249ec2c
VS
680 file.Write(
681 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
b8b8c49b
VS
682
683 for (i = 0; i < flist.Count(); i++)
684 {
685 wxString s;
2b5f62a0 686 s.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i);
b8b8c49b
VS
687 file.Write(s);
688 }
689}
690
c8b7a961
VS
691
692
693void XmlResApp::OutputGettext()
694{
695 wxArrayString str = FindStrings();
f80ea77b 696
c8b7a961 697 wxFFile fout;
1dce6f09
VS
698 if (parOutput.empty())
699 fout.Attach(stdout);
700 else
701 fout.Open(parOutput, wxT("wt"));
f80ea77b 702
c8b7a961 703 for (size_t i = 0; i < str.GetCount(); i++)
0653d364 704 fout.Write(_T("_(\"") + str[i] + _T("\");\n"));
f80ea77b 705
c8b7a961
VS
706 if (!parOutput) fout.Detach();
707}
708
709
710
711wxArrayString XmlResApp::FindStrings()
712{
713 wxArrayString arr, a2;
714
715 for (size_t i = 0; i < parFiles.Count(); i++)
716 {
f80ea77b 717 if (flagVerbose)
2b5f62a0 718 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
c8b7a961 719
f80ea77b 720 wxXmlDocument doc;
c8b7a961
VS
721 if (!doc.Load(parFiles[i]))
722 {
2b5f62a0 723 wxLogError(_T("Error parsing file ") + parFiles[i]);
c8b7a961
VS
724 retCode = 1;
725 continue;
726 }
727 a2 = FindStrings(doc.GetRoot());
728 WX_APPEND_ARRAY(arr, a2);
729 }
f80ea77b 730
c8b7a961
VS
731 return arr;
732}
733
734
735
c109ef11
VS
736static 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 }
f80ea77b 750 else
c109ef11
VS
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;
2b5f62a0
VZ
757 case wxT('\\') : if ((*(dt+1) != 'n') &&
758 (*(dt+1) != 't') &&
759 (*(dt+1) != 'r'))
760 str2 << wxT("\\\\");
761 else
f80ea77b 762 str2 << wxT("\\");
2b5f62a0 763 break;
904a226c 764 case wxT('"') : str2 << wxT("\\\""); break;
c109ef11
VS
765 default : str2 << *dt; break;
766 }
767 }
768 }
769
770 return str2;
771}
772
773
c8b7a961
VS
774wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
775{
776 wxArrayString arr;
777
778 wxXmlNode *n = node;
779 if (n == NULL) return arr;
780 n = n->GetChildren();
f80ea77b 781
c8b7a961
VS
782 while (n)
783 {
784 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
785 // parent is an element, i.e. has subnodes...
f80ea77b 786 (n->GetType() == wxXML_TEXT_NODE ||
c8b7a961
VS
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") ||
0653d364
VS
797 node/*not n!*/->GetName() == _T("title") ||
798 node/*not n!*/->GetName() == _T("item")
c8b7a961 799 ))
c109ef11 800 // ...and known to contain translatable string
c8b7a961 801 {
c109ef11 802 arr.Add(ConvertText(n->GetContent()));
c8b7a961 803 }
f80ea77b 804
c8b7a961
VS
805 // subnodes:
806 if (n->GetType() == wxXML_ELEMENT_NODE)
807 {
808 wxArrayString a2 = FindStrings(n);
809 WX_APPEND_ARRAY(arr, a2);
810 }
f80ea77b 811
c8b7a961
VS
812 n = n->GetNext();
813 }
814 return arr;
815}