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