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