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