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