]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
Fix OS X compilation in C++11 mode with libc++.
[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
7a7fa93b 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 {
9a83f860
VZ
68 if (node->GetName() == wxT("object")
69 && node->GetAttribute(wxT("class"),&classValue)
70 && node->GetAttribute(wxT("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 {
9a83f860 87 if ( className == wxT("wxMenu") )
aac18ec7 88 {
9a83f860
VZ
89 m_ancestorClassNames.insert(wxT("wxMenu"));
90 m_ancestorClassNames.insert(wxT("wxMenuBar"));
aac18ec7 91 }
9a83f860 92 else if ( className == wxT("wxMDIChildFrame") )
aac18ec7 93 {
9a83f860 94 m_ancestorClassNames.insert(wxT("wxMDIParentFrame"));
aac18ec7 95 }
9a83f860
VZ
96 else if( className == wxT("wxMenuBar") ||
97 className == wxT("wxStatusBar") ||
98 className == wxT("wxToolBar") )
aac18ec7 99 {
9a83f860 100 m_ancestorClassNames.insert(wxT("wxFrame"));
aac18ec7
VZ
101 }
102 else
103 {
9a83f860 104 m_ancestorClassNames.insert(wxT("wxWindow"));
aac18ec7
VZ
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 {
9a83f860
VZ
117 if (name == wxT("tool") ||
118 name == wxT("data") ||
119 name == wxT("unknown") ||
120 name == wxT("notebookpage") ||
121 name == wxT("separator") ||
122 name == wxT("sizeritem") ||
123 name == wxT("wxMenu") ||
124 name == wxT("wxMenuBar") ||
125 name == wxT("wxMenuItem") ||
126 name.EndsWith(wxT("Sizer")) )
76ee0497
VS
127 {
128 return false;
129 }
130 return true;
131 }
2ad1ff54 132
76ee0497
VS
133 void GenerateHeaderCode(wxFFile& file)
134 {
aa063b24 135
9a83f860
VZ
136 file.Write(wxT("class ") + m_className + wxT(" : public ") + m_parentClassName
137 + wxT(" {\nprotected:\n"));
aa063b24 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;
6636ef8d 143 if( w.GetName().empty() ) continue;
aa063b24 144 file.Write(
9a83f860
VZ
145 wxT(" ") + w.GetClass() + wxT("* ") + w.GetName()
146 + wxT(";\n"));
aa063b24 147 }
9a83f860
VZ
148 file.Write(wxT("\nprivate:\n void InitWidgetsFromXRC(wxWindow *parent){\n")
149 wxT(" wxXmlResource::Get()->LoadObject(this,parent,wxT(\"")
f80ea77b 150 + m_className
9a83f860 151 + wxT("\"), wxT(\"")
f80ea77b 152 + m_parentClassName
9a83f860 153 + wxT("\"));\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;
6636ef8d 158 if( w.GetName().empty() ) continue;
9a83f860 159 file.Write( wxT(" ")
f80ea77b 160 + w.GetName()
9a83f860 161 + wxT(" = XRCCTRL(*this,\"")
f80ea77b 162 + w.GetName()
9a83f860 163 + wxT("\",")
aa063b24 164 + w.GetClass()
9a83f860 165 + wxT(");\n"));
aa063b24 166 }
9a83f860 167 file.Write(wxT(" }\n"));
2ad1ff54 168
9a83f860 169 file.Write( wxT("public:\n"));
aac18ec7
VZ
170
171 if ( m_ancestorClassNames.size() == 1 )
172 {
173 file.Write
174 (
175 m_className +
9a83f860 176 wxT("(") +
aac18ec7 177 *m_ancestorClassNames.begin() +
9a83f860
VZ
178 wxT(" *parent=NULL){\n") +
179 wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n")
180 wxT(" }\n")
181 wxT("};\n")
aac18ec7
VZ
182 );
183 }
184 else
185 {
9a83f860
VZ
186 file.Write(m_className + wxT("(){\n") +
187 wxT(" InitWidgetsFromXRC(NULL);\n")
188 wxT(" }\n")
189 wxT("};\n"));
aac18ec7
VZ
190
191 for ( StringSet::const_iterator it = m_ancestorClassNames.begin();
192 it != m_ancestorClassNames.end();
193 ++it )
194 {
9a83f860
VZ
195 file.Write(m_className + wxT("(") + *it + wxT(" *parent){\n") +
196 wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n")
197 wxT(" }\n")
198 wxT("};\n"));
aac18ec7
VZ
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)
9a83f860 318 parOutput = wxT("resource.cpp");
b8b8c49b 319 else if (flagPython)
9a83f860 320 parOutput = wxT("resource.py");
b8b8c49b 321 else
9a83f860 322 parOutput = wxT("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 }
9a83f860 332 if (!parOutputPath) parOutputPath = wxT(".");
56d2f750 333
db51a209 334 if (!cmdline.Found("n", &parFuncname))
9a83f860 335 parFuncname = wxT("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
3c4845b4
VS
359 if ( wxFileExists(parOutput) )
360 wxRemoveFile(parOutput);
56d2f750 361
56d2f750 362 if (!retCode)
f80ea77b 363 {
1dce6f09 364 if (flagCPP){
56d2f750 365 MakePackageCPP(files);
1dce6f09
VS
366 if (flagH)
367 GenCPPHeader();
368 }
b8b8c49b
VS
369 else if (flagPython)
370 MakePackagePython(files);
56d2f750
VS
371 else
372 MakePackageZIP(files);
373 }
f80ea77b 374
56d2f750
VS
375 DeleteTempFiles(files);
376}
377
378
a7501aeb
VS
379wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
380{
381 wxString name2 = name;
9a83f860
VZ
382 name2.Replace(wxT(":"), wxT("_"));
383 name2.Replace(wxT("/"), wxT("_"));
384 name2.Replace(wxT("\\"), wxT("_"));
385 name2.Replace(wxT("*"), wxT("_"));
386 name2.Replace(wxT("?"), wxT("_"));
f80ea77b 387
9a83f860 388 wxString s = wxFileNameFromPath(parOutput) + wxT("$") + name2;
a7501aeb
VS
389
390 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
f80ea77b 391 {
a7501aeb
VS
392 for (int i = 0;; i++)
393 {
9a83f860 394 s.Printf(wxFileNameFromPath(parOutput) + wxT("$%03i-") + name2, i);
a7501aeb
VS
395 if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
396 break;
397 }
398 }
399 return s;
400}
56d2f750
VS
401
402wxArrayString XmlResApp::PrepareTempFiles()
403{
404 wxArrayString flist;
f80ea77b 405
b4a980f4 406 for (size_t i = 0; i < parFiles.GetCount(); i++)
56d2f750 407 {
f80ea77b 408 if (flagVerbose)
9a83f860 409 wxPrintf(wxT("processing ") + parFiles[i] + wxT("...\n"));
56d2f750
VS
410
411 wxXmlDocument doc;
f80ea77b 412
56d2f750
VS
413 if (!doc.Load(parFiles[i]))
414 {
9a83f860 415 wxLogError(wxT("Error parsing file ") + parFiles[i]);
56d2f750
VS
416 retCode = 1;
417 continue;
418 }
f80ea77b 419
f6853b4a 420 wxString name, ext, path;
bd365871 421 wxFileName::SplitPath(parFiles[i], &path, &name, &ext);
f6853b4a
VS
422
423 FindFilesInXML(doc.GetRoot(), flist, path);
1dce6f09
VS
424 if (flagH)
425 {
426 wxXmlNode* node = (doc.GetRoot())->GetChildren();
aa063b24
RD
427 wxString classValue,nameValue;
428 while(node){
9a83f860
VZ
429 if(node->GetName() == wxT("object")
430 && node->GetAttribute(wxT("class"),&classValue)
431 && node->GetAttribute(wxT("name"),&nameValue)){
1dce6f09
VS
432
433 aXRCWndClassData.Add(
aa063b24 434 XRCWndClassData(nameValue,classValue,node)
1dce6f09
VS
435 );
436 }
aa063b24 437 node = node -> GetNext();
1dce6f09
VS
438 }
439 }
a7501aeb 440 wxString internalName = GetInternalFileName(parFiles[i], flist);
f80ea77b 441
4249ec2c 442 doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
a7501aeb 443 flist.Add(internalName);
56d2f750 444 }
f80ea77b 445
56d2f750
VS
446 return flist;
447}
448
449
4249ec2c
VS
450// Does 'node' contain filename information at all?
451static bool NodeContainsFilename(wxXmlNode *node)
452{
bc151e84
VZ
453 const wxString name = node->GetName();
454
455 // Any bitmaps (bitmap2 is used for disabled toolbar buttons):
9a83f860 456 if ( name == wxT("bitmap") || name == wxT("bitmap2") )
f80ea77b 457 return true;
2ad1ff54 458
9a83f860 459 if ( name == wxT("icon") )
8b34993d 460 return true;
4249ec2c 461
4249ec2c
VS
462 // wxBitmapButton:
463 wxXmlNode *parent = node->GetParent();
f80ea77b 464 if (parent != NULL &&
9a83f860
VZ
465 parent->GetAttribute(wxT("class"), wxT("")) == wxT("wxBitmapButton") &&
466 (name == wxT("focus") ||
467 name == wxT("disabled") ||
468 name == wxT("hover") ||
469 name == wxT("selected")))
f80ea77b
WS
470 return true;
471
4249ec2c 472 // wxBitmap or wxIcon toplevel resources:
9a83f860 473 if ( name == wxT("object") )
4249ec2c 474 {
9a83f860
VZ
475 wxString klass = node->GetAttribute(wxT("class"), wxEmptyString);
476 if (klass == wxT("wxBitmap") ||
477 klass == wxT("wxIcon") ||
478 klass == wxT("data") )
f80ea77b 479 return true;
4249ec2c 480 }
f80ea77b 481
9ec6078f 482 // URLs in wxHtmlWindow:
9a83f860 483 if ( name == wxT("url") &&
9ec6078f 484 parent != NULL &&
9a83f860 485 parent->GetAttribute(wxT("class"), wxT("")) == wxT("wxHtmlWindow") )
9ec6078f
VS
486 {
487 // FIXME: this is wrong for e.g. http:// URLs
488 return true;
489 }
490
f80ea77b 491 return false;
4249ec2c 492}
56d2f750 493
f6853b4a
VS
494// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
495void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
496{
2b5f62a0
VZ
497 // Is 'node' XML node element?
498 if (node == NULL) return;
499 if (node->GetType() != wxXML_ELEMENT_NODE) return;
500
4249ec2c 501 bool containsFilename = NodeContainsFilename(node);
2b5f62a0
VZ
502
503 wxXmlNode *n = node->GetChildren();
f6853b4a
VS
504 while (n)
505 {
2b5f62a0 506 if (containsFilename &&
f80ea77b 507 (n->GetType() == wxXML_TEXT_NODE ||
2b5f62a0 508 n->GetType() == wxXML_CDATA_SECTION_NODE))
f6853b4a
VS
509 {
510 wxString fullname;
2b5f62a0
VZ
511 if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
512 fullname = n->GetContent();
513 else
4249ec2c 514 fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
a7501aeb 515
f80ea77b 516 if (flagVerbose)
9a83f860 517 wxPrintf(wxT("adding ") + fullname + wxT("...\n"));
2b5f62a0 518
a7501aeb 519 wxString filename = GetInternalFileName(n->GetContent(), flist);
f6853b4a 520 n->SetContent(filename);
f6853b4a 521
2b5f62a0
VZ
522 if (flist.Index(filename) == wxNOT_FOUND)
523 flist.Add(filename);
f6853b4a
VS
524
525 wxFileInputStream sin(fullname);
4249ec2c 526 wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
f6853b4a
VS
527 sin.Read(sout); // copy the stream
528 }
2b5f62a0 529
f6853b4a
VS
530 // subnodes:
531 if (n->GetType() == wxXML_ELEMENT_NODE)
532 FindFilesInXML(n, flist, inputPath);
2b5f62a0 533
f6853b4a
VS
534 n = n->GetNext();
535 }
536}
537
538
539
56d2f750
VS
540void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
541{
b4a980f4 542 for (size_t i = 0; i < flist.GetCount(); i++)
4249ec2c 543 wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
56d2f750
VS
544}
545
546
547
548void XmlResApp::MakePackageZIP(const wxArrayString& flist)
549{
550 wxString files;
f80ea77b 551
b4a980f4 552 for (size_t i = 0; i < flist.GetCount(); i++)
9a83f860 553 files += flist[i] + wxT(" ");
56d2f750 554 files.RemoveLast();
f80ea77b
WS
555
556 if (flagVerbose)
9a83f860 557 wxPrintf(wxT("compressing ") + parOutput + wxT("...\n"));
f80ea77b 558
4249ec2c
VS
559 wxString cwd = wxGetCwd();
560 wxSetWorkingDirectory(parOutputPath);
9a83f860
VZ
561 int execres = wxExecute(wxT("zip -9 -j ") +
562 wxString(flagVerbose ? wxT("\"") : wxT("-q \"")) +
563 parOutput + wxT("\" ") + files, true);
4249ec2c
VS
564 wxSetWorkingDirectory(cwd);
565 if (execres == -1)
56d2f750 566 {
9a83f860
VZ
567 wxLogError(wxT("Unable to execute zip program. Make sure it is in the path."));
568 wxLogError(wxT("You can download it at http://www.cdrom.com/pub/infozip/"));
56d2f750
VS
569 retCode = 1;
570 return;
571 }
572}
573
574
575
56d2f750
VS
576static wxString FileToCppArray(wxString filename, int num)
577{
578 wxString output;
56d2f750 579 wxString tmp;
f6853b4a 580 wxString snum;
5851504d 581 wxFFile file(filename, wxT("rb"));
2ad1ff54
WS
582 wxFileOffset offset = file.Length();
583 wxASSERT_MSG( offset >= 0 , wxT("Invalid file length") );
17a1ebd1
VZ
584
585 const size_t lng = wx_truncate_cast(size_t, offset);
81d3348a 586 wxASSERT_MSG( static_cast<wxFileOffset>(lng) == offset,
563a24f0 587 wxT("Huge file not supported") );
f80ea77b 588
9a83f860 589 snum.Printf(wxT("%i"), num);
e22fa4d7
VZ
590 output.Printf(wxT("static size_t xml_res_size_") + snum + wxT(" = %lu;\n"),
591 static_cast<unsigned long>(lng));
9a83f860 592 output += wxT("static unsigned char xml_res_file_") + snum + wxT("[] = {\n");
e066e256
VS
593 // we cannot use string literals because MSVC is dumb wannabe compiler
594 // with arbitrary limitation to 2048 strings :(
f80ea77b 595
56d2f750
VS
596 unsigned char *buffer = new unsigned char[lng];
597 file.Read(buffer, lng);
f80ea77b 598
f6853b4a 599 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 600 {
9a83f860
VZ
601 tmp.Printf(wxT("%i"), buffer[i]);
602 if (i != 0) output << wxT(',');
e066e256 603 if (linelng > 70)
f6853b4a
VS
604 {
605 linelng = 0;
9a83f860 606 output << wxT("\n");
f6853b4a 607 }
e066e256
VS
608 output << tmp;
609 linelng += tmp.Length()+1;
56d2f750 610 }
f80ea77b 611
56d2f750 612 delete[] buffer;
f80ea77b 613
9a83f860 614 output += wxT("};\n\n");
f80ea77b 615
56d2f750
VS
616 return output;
617}
618
619
620void XmlResApp::MakePackageCPP(const wxArrayString& flist)
621{
5851504d 622 wxFFile file(parOutput, wxT("wt"));
9b1aeed1 623 unsigned i;
56d2f750 624
f80ea77b 625 if (flagVerbose)
9a83f860 626 wxPrintf(wxT("creating C++ source file ") + parOutput + wxT("...\n"));
f80ea77b 627
5881d27b
VS
628 file.Write(""
629"//\n"
630"// This file was automatically generated by wxrc, do not edit by hand.\n"
631"//\n\n"
632"#include <wx/wxprec.h>\n"
633"\n"
634"#ifdef __BORLANDC__\n"
635" #pragma hdrstop\n"
636"#endif\n"
637"\n"
638""
639"#include <wx/filesys.h>\n"
640"#include <wx/fs_mem.h>\n"
641"#include <wx/xrc/xmlres.h>\n"
642"#include <wx/xrc/xh_all.h>\n"
643"\n"
644"#if wxCHECK_VERSION(2,8,5) && wxABI_VERSION >= 20805\n"
645" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
646" wxMemoryFSHandler::AddFileWithMimeType(name, data, size, mime)\n"
647"#else\n"
648" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
649" wxMemoryFSHandler::AddFile(name, data, size)\n"
650"#endif\n"
651"\n");
56d2f750 652
b4a980f4 653 for (i = 0; i < flist.GetCount(); i++)
4249ec2c
VS
654 file.Write(
655 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
f80ea77b 656
5881d27b 657 file.Write(""
09f01082 658"void " + parFuncname + "()\n"
5881d27b
VS
659"{\n"
660"\n"
661" // Check for memory FS. If not present, load the handler:\n"
662" {\n"
663" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n"
664" wxFileSystem fsys;\n"
665" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n"
666" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n"
667" if (f) delete f;\n"
668" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n"
669" }\n"
670"\n");
56d2f750 671
b4a980f4 672 for (i = 0; i < flist.GetCount(); i++)
56d2f750
VS
673 {
674 wxString s;
c5d7b81e
VS
675
676 wxString mime;
677 wxString ext = wxFileName(flist[i]).GetExt();
9a83f860
VZ
678 if ( ext.Lower() == wxT("xrc") )
679 mime = wxT("text/xml");
e74f1202 680#if wxUSE_MIMETYPE
c5d7b81e
VS
681 else
682 {
683 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
684 if ( ft )
e74f1202 685 {
c5d7b81e 686 ft->GetMimeType(&mime);
e74f1202
VZ
687 delete ft;
688 }
c5d7b81e 689 }
e74f1202 690#endif // wxUSE_MIMETYPE
c5d7b81e 691
5881d27b 692 s.Printf(" XRC_ADD_FILE(wxT(\"XRC_resource/" + flist[i] +
9b1aeed1 693 "\"), xml_res_file_%u, xml_res_size_%u, wxT(\"%s\"));\n",
c5d7b81e 694 i, i, mime.c_str());
56d2f750
VS
695 file.Write(s);
696 }
f6853b4a 697
b4a980f4 698 for (i = 0; i < parFiles.GetCount(); i++)
f6853b4a 699 {
5881d27b
VS
700 file.Write(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/" +
701 GetInternalFileName(parFiles[i], flist) + "\"));\n");
f6853b4a 702 }
f80ea77b 703
5881d27b 704 file.Write("}\n");
56d2f750 705
f6853b4a 706
56d2f750 707}
c8b7a961 708
1dce6f09
VS
709void XmlResApp::GenCPPHeader()
710{
21567b09
VZ
711 // Generate the output header in the same directory as the source file.
712 wxFileName headerName(parOutput);
713 headerName.SetExt("h");
f80ea77b 714
21567b09 715 wxFFile file(headerName.GetFullPath(), wxT("wt"));
1dce6f09 716 file.Write(
5881d27b
VS
717"//\n"
718"// This file was automatically generated by wxrc, do not edit by hand.\n"
719"//\n\n"
21567b09
VZ
720"#ifndef __" + headerName.GetName() + "_h__\n"
721"#define __" + headerName.GetName() + "_h__\n"
f80ea77b 722);
b4a980f4 723 for(size_t i=0;i<aXRCWndClassData.GetCount();++i){
f80ea77b
WS
724 aXRCWndClassData.Item(i).GenerateHeaderCode(file);
725 }
1dce6f09 726 file.Write(
5881d27b 727 "\nvoid \n"
aa063b24 728 + parFuncname
5881d27b 729 + "();\n#endif\n");
1dce6f09
VS
730}
731
b8b8c49b
VS
732static wxString FileToPythonArray(wxString filename, int num)
733{
734 wxString output;
735 wxString tmp;
736 wxString snum;
5851504d 737 wxFFile file(filename, wxT("rb"));
2ad1ff54
WS
738 wxFileOffset offset = file.Length();
739 wxASSERT_MSG( offset >= 0 , wxT("Invalid file length") );
17a1ebd1
VZ
740
741 const size_t lng = wx_truncate_cast(size_t, offset);
81d3348a 742 wxASSERT_MSG( static_cast<wxFileOffset>(lng) == offset,
563a24f0 743 wxT("Huge file not supported") );
f80ea77b 744
9a83f860 745 snum.Printf(wxT("%i"), num);
09f01082 746 output = " xml_res_file_" + snum + " = '''\\\n";
f80ea77b 747
b8b8c49b
VS
748 unsigned char *buffer = new unsigned char[lng];
749 file.Read(buffer, lng);
f80ea77b 750
b8b8c49b
VS
751 for (size_t i = 0, linelng = 0; i < lng; i++)
752 {
753 unsigned char c = buffer[i];
754 if (c == '\n')
755 {
756 tmp = (wxChar)c;
757 linelng = 0;
758 }
9a9942f7 759 else if (c < 32 || c > 127 || c == '\'')
9a83f860 760 tmp.Printf(wxT("\\x%02x"), c);
b8b8c49b 761 else if (c == '\\')
9a83f860 762 tmp = wxT("\\\\");
b8b8c49b
VS
763 else
764 tmp = (wxChar)c;
765 if (linelng > 70)
766 {
767 linelng = 0;
9a83f860 768 output << wxT("\\\n");
b8b8c49b
VS
769 }
770 output << tmp;
771 linelng += tmp.Length();
772 }
f80ea77b 773
b8b8c49b 774 delete[] buffer;
f80ea77b 775
9a83f860 776 output += wxT("'''\n\n");
f80ea77b 777
b8b8c49b
VS
778 return output;
779}
780
781
782void XmlResApp::MakePackagePython(const wxArrayString& flist)
783{
5851504d 784 wxFFile file(parOutput, wxT("wt"));
9b1aeed1 785 unsigned i;
b8b8c49b 786
f80ea77b 787 if (flagVerbose)
9a83f860 788 wxPrintf(wxT("creating Python source file ") + parOutput + wxT("...\n"));
f80ea77b 789
b8b8c49b 790 file.Write(
5881d27b
VS
791 "#\n"
792 "# This file was automatically generated by wxrc, do not edit by hand.\n"
793 "#\n\n"
794 "import wx\n"
795 "import wx.xrc\n\n"
b8b8c49b
VS
796 );
797
f80ea77b 798
09f01082 799 file.Write("def " + parFuncname + "():\n");
b8b8c49b 800
b4a980f4 801 for (i = 0; i < flist.GetCount(); i++)
4249ec2c
VS
802 file.Write(
803 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
b8b8c49b 804
2ad1ff54 805 file.Write(
5881d27b
VS
806 " # check if the memory filesystem handler has been loaded yet, and load it if not\n"
807 " wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n"
808 " fsys = wx.FileSystem()\n"
809 " f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n"
810 " wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n"
811 " if f is not None:\n"
812 " f.Destroy()\n"
813 " else:\n"
814 " wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n"
815 "\n"
816 " # load all the strings as memory files and load into XmlRes\n"
b27a4ef4
RD
817 );
818
2ad1ff54 819
b4a980f4 820 for (i = 0; i < flist.GetCount(); i++)
b8b8c49b
VS
821 {
822 wxString s;
5881d27b 823 s.Printf(" wx.MemoryFSHandler.AddFile('XRC_resource/" + flist[i] +
9b1aeed1 824 "', xml_res_file_%u)\n", i);
b8b8c49b
VS
825 file.Write(s);
826 }
b4a980f4 827 for (i = 0; i < parFiles.GetCount(); i++)
b27a4ef4 828 {
5881d27b
VS
829 file.Write(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/" +
830 GetInternalFileName(parFiles[i], flist) + "')\n");
b27a4ef4
RD
831 }
832
5881d27b 833 file.Write("\n");
b8b8c49b
VS
834}
835
c8b7a961
VS
836
837
838void XmlResApp::OutputGettext()
839{
21b2dde5 840 ExtractedStrings str = FindStrings();
f80ea77b 841
c8b7a961 842 wxFFile fout;
1dce6f09
VS
843 if (parOutput.empty())
844 fout.Attach(stdout);
845 else
846 fout.Open(parOutput, wxT("wt"));
f80ea77b 847
21b2dde5
VS
848 for (ExtractedStrings::const_iterator i = str.begin(); i != str.end(); ++i)
849 {
0d373eac
VS
850 const wxFileName filename(i->filename);
851
21b2dde5 852 wxString s;
0d373eac
VS
853 s.Printf("#line %d \"%s\"\n",
854 i->lineNo, filename.GetFullPath(wxPATH_UNIX));
21b2dde5 855
21b2dde5
VS
856 fout.Write(s);
857 fout.Write("_(\"" + i->str + "\");\n");
858 }
f80ea77b 859
c8b7a961
VS
860 if (!parOutput) fout.Detach();
861}
862
863
864
21b2dde5 865ExtractedStrings XmlResApp::FindStrings()
c8b7a961 866{
21b2dde5 867 ExtractedStrings arr, a2;
c8b7a961 868
b4a980f4 869 for (size_t i = 0; i < parFiles.GetCount(); i++)
c8b7a961 870 {
f80ea77b 871 if (flagVerbose)
9a83f860 872 wxPrintf(wxT("processing ") + parFiles[i] + wxT("...\n"));
c8b7a961 873
f80ea77b 874 wxXmlDocument doc;
c8b7a961
VS
875 if (!doc.Load(parFiles[i]))
876 {
9a83f860 877 wxLogError(wxT("Error parsing file ") + parFiles[i]);
c8b7a961
VS
878 retCode = 1;
879 continue;
880 }
21b2dde5
VS
881 a2 = FindStrings(parFiles[i], doc.GetRoot());
882
c8b7a961
VS
883 WX_APPEND_ARRAY(arr, a2);
884 }
f80ea77b 885
c8b7a961
VS
886 return arr;
887}
888
889
890
c109ef11
VS
891static wxString ConvertText(const wxString& str)
892{
893 wxString str2;
894 const wxChar *dt;
895
896 for (dt = str.c_str(); *dt; dt++)
897 {
898 if (*dt == wxT('_'))
899 {
2148394a
VS
900 if ( *(dt+1) == 0 )
901 str2 << wxT('_');
902 else if ( *(++dt) == wxT('_') )
c109ef11
VS
903 str2 << wxT('_');
904 else
905 str2 << wxT('&') << *dt;
906 }
f80ea77b 907 else
c109ef11
VS
908 {
909 switch (*dt)
910 {
911 case wxT('\n') : str2 << wxT("\\n"); break;
912 case wxT('\t') : str2 << wxT("\\t"); break;
913 case wxT('\r') : str2 << wxT("\\r"); break;
2b5f62a0
VZ
914 case wxT('\\') : if ((*(dt+1) != 'n') &&
915 (*(dt+1) != 't') &&
916 (*(dt+1) != 'r'))
917 str2 << wxT("\\\\");
918 else
f80ea77b 919 str2 << wxT("\\");
2b5f62a0 920 break;
904a226c 921 case wxT('"') : str2 << wxT("\\\""); break;
c109ef11
VS
922 default : str2 << *dt; break;
923 }
924 }
925 }
926
927 return str2;
928}
929
930
21b2dde5
VS
931ExtractedStrings
932XmlResApp::FindStrings(const wxString& filename, wxXmlNode *node)
c8b7a961 933{
21b2dde5 934 ExtractedStrings arr;
c8b7a961
VS
935
936 wxXmlNode *n = node;
937 if (n == NULL) return arr;
938 n = n->GetChildren();
f80ea77b 939
c8b7a961
VS
940 while (n)
941 {
942 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
943 // parent is an element, i.e. has subnodes...
f80ea77b 944 (n->GetType() == wxXML_TEXT_NODE ||
c8b7a961
VS
945 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
946 // ...it is textnode...
947 (
9a83f860
VZ
948 node/*not n!*/->GetName() == wxT("label") ||
949 (node/*not n!*/->GetName() == wxT("value") &&
c8b7a961 950 !n->GetContent().IsNumber()) ||
9a83f860
VZ
951 node/*not n!*/->GetName() == wxT("help") ||
952 node/*not n!*/->GetName() == wxT("longhelp") ||
953 node/*not n!*/->GetName() == wxT("tooltip") ||
954 node/*not n!*/->GetName() == wxT("htmlcode") ||
955 node/*not n!*/->GetName() == wxT("title") ||
3496ce18
VZ
956 node/*not n!*/->GetName() == wxT("item") ||
957 node/*not n!*/->GetName() == wxT("message") ||
958 node/*not n!*/->GetName() == wxT("note") ||
959 node/*not n!*/->GetName() == wxT("defaultdirectory") ||
960 node/*not n!*/->GetName() == wxT("defaultfilename") ||
961 node/*not n!*/->GetName() == wxT("defaultfolder") ||
962 node/*not n!*/->GetName() == wxT("filter") ||
963 node/*not n!*/->GetName() == wxT("caption")
c8b7a961 964 ))
c109ef11 965 // ...and known to contain translatable string
c8b7a961 966 {
8da9d91c 967 if (!flagGettext ||
9a83f860 968 node->GetAttribute(wxT("translate"), wxT("1")) != wxT("0"))
8da9d91c 969 {
21b2dde5
VS
970 arr.push_back
971 (
972 ExtractedString
973 (
974 ConvertText(n->GetContent()),
975 filename,
976 n->GetLineNumber()
977 )
978 );
8da9d91c 979 }
c8b7a961 980 }
f80ea77b 981
c8b7a961
VS
982 // subnodes:
983 if (n->GetType() == wxXML_ELEMENT_NODE)
984 {
21b2dde5 985 ExtractedStrings a2 = FindStrings(filename, n);
c8b7a961
VS
986 WX_APPEND_ARRAY(arr, a2);
987 }
f80ea77b 988
c8b7a961
VS
989 n = n->GetNext();
990 }
991 return arr;
992}