]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
XRC: make wxSplitterWindow's sashpos and minpanesize dimensions.
[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 5// Created: 2000/03/05
56d2f750
VS
6// Copyright: (c) 2000 Vaclav Slavik
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
56d2f750
VS
10// For compilers that support precompilation, includes "wx/wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
07ee782b 17// for all others, include the necessary headers
56d2f750 18#ifndef WX_PRECOMP
07ee782b
MB
19 #include "wx/app.h"
20 #include "wx/log.h"
6b24b421 21 #include "wx/wxcrtvararg.h"
56d2f750
VS
22#endif
23
24#include "wx/cmdline.h"
cecc483e 25#include "wx/xml/xml.h"
56d2f750 26#include "wx/ffile.h"
4249ec2c 27#include "wx/filename.h"
f6853b4a 28#include "wx/wfstream.h"
097d3ba2 29#include "wx/utils.h"
aac18ec7 30#include "wx/hashset.h"
c5d7b81e 31#include "wx/mimetype.h"
21b2dde5 32#include "wx/vector.h"
f6853b4a 33
7a7fa93b 34WX_DECLARE_HASH_SET(wxString, wxStringHash, wxStringEqual, StringSet);
f6853b4a 35
1dce6f09
VS
36class XRCWidgetData
37{
38public:
f80ea77b 39 XRCWidgetData(const wxString& vname,const wxString& vclass)
1dce6f09
VS
40 : m_class(vclass), m_name(vname) {}
41 const wxString& GetName() const { return m_name; }
42 const wxString& GetClass() const { return m_class; }
43private:
44 wxString m_class;
45 wxString m_name;
46};
2ad1ff54 47
1dce6f09
VS
48#include "wx/arrimpl.cpp"
49WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData);
17a1ebd1 50WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData)
1dce6f09
VS
51
52class XRCWndClassData
53{
f80ea77b 54private:
1dce6f09
VS
55 wxString m_className;
56 wxString m_parentClassName;
aac18ec7 57 StringSet m_ancestorClassNames;
1dce6f09 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 {
9a83f860
VZ
67 if (node->GetName() == wxT("object")
68 && node->GetAttribute(wxT("class"),&classValue)
69 && node->GetAttribute(wxT("name"),&nameValue))
1dce6f09
VS
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:
aac18ec7
VZ
81 XRCWndClassData(const wxString& className,
82 const wxString& parentClassName,
83 const wxXmlNode* node) :
2ad1ff54
WS
84 m_className(className) , m_parentClassName(parentClassName)
85 {
9a83f860 86 if ( className == wxT("wxMenu") )
aac18ec7 87 {
9a83f860
VZ
88 m_ancestorClassNames.insert(wxT("wxMenu"));
89 m_ancestorClassNames.insert(wxT("wxMenuBar"));
aac18ec7 90 }
9a83f860 91 else if ( className == wxT("wxMDIChildFrame") )
aac18ec7 92 {
9a83f860 93 m_ancestorClassNames.insert(wxT("wxMDIParentFrame"));
aac18ec7 94 }
9a83f860
VZ
95 else if( className == wxT("wxMenuBar") ||
96 className == wxT("wxStatusBar") ||
97 className == wxT("wxToolBar") )
aac18ec7 98 {
9a83f860 99 m_ancestorClassNames.insert(wxT("wxFrame"));
aac18ec7
VZ
100 }
101 else
102 {
9a83f860 103 m_ancestorClassNames.insert(wxT("wxWindow"));
aac18ec7
VZ
104 }
105
aa063b24 106 BrowseXmlNode(node->GetChildren());
aa063b24 107 }
f80ea77b 108
2ad1ff54
WS
109 const ArrayOfXRCWidgetData& GetWidgetData()
110 {
aa063b24
RD
111 return m_wdata;
112 }
76ee0497 113
216d173f 114 bool CanBeUsedWithXRCCTRL(const wxString& name)
76ee0497 115 {
9a83f860
VZ
116 if (name == wxT("tool") ||
117 name == wxT("data") ||
118 name == wxT("unknown") ||
119 name == wxT("notebookpage") ||
120 name == wxT("separator") ||
121 name == wxT("sizeritem") ||
122 name == wxT("wxMenu") ||
123 name == wxT("wxMenuBar") ||
124 name == wxT("wxMenuItem") ||
125 name.EndsWith(wxT("Sizer")) )
76ee0497
VS
126 {
127 return false;
128 }
129 return true;
130 }
2ad1ff54 131
76ee0497
VS
132 void GenerateHeaderCode(wxFFile& file)
133 {
aa063b24 134
9a83f860
VZ
135 file.Write(wxT("class ") + m_className + wxT(" : public ") + m_parentClassName
136 + wxT(" {\nprotected:\n"));
aa063b24 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;
6636ef8d 142 if( w.GetName().empty() ) continue;
aa063b24 143 file.Write(
9a83f860
VZ
144 wxT(" ") + w.GetClass() + wxT("* ") + w.GetName()
145 + wxT(";\n"));
aa063b24 146 }
9a83f860
VZ
147 file.Write(wxT("\nprivate:\n void InitWidgetsFromXRC(wxWindow *parent){\n")
148 wxT(" wxXmlResource::Get()->LoadObject(this,parent,wxT(\"")
f80ea77b 149 + m_className
9a83f860 150 + wxT("\"), wxT(\"")
f80ea77b 151 + m_parentClassName
9a83f860 152 + wxT("\"));\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;
6636ef8d 157 if( w.GetName().empty() ) continue;
9a83f860 158 file.Write( wxT(" ")
f80ea77b 159 + w.GetName()
9a83f860 160 + wxT(" = XRCCTRL(*this,\"")
f80ea77b 161 + w.GetName()
9a83f860 162 + wxT("\",")
aa063b24 163 + w.GetClass()
9a83f860 164 + wxT(");\n"));
aa063b24 165 }
9a83f860 166 file.Write(wxT(" }\n"));
2ad1ff54 167
9a83f860 168 file.Write( wxT("public:\n"));
aac18ec7
VZ
169
170 if ( m_ancestorClassNames.size() == 1 )
171 {
172 file.Write
173 (
174 m_className +
9a83f860 175 wxT("(") +
aac18ec7 176 *m_ancestorClassNames.begin() +
9a83f860
VZ
177 wxT(" *parent=NULL){\n") +
178 wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n")
179 wxT(" }\n")
180 wxT("};\n")
aac18ec7
VZ
181 );
182 }
183 else
184 {
9a83f860
VZ
185 file.Write(m_className + wxT("(){\n") +
186 wxT(" InitWidgetsFromXRC(NULL);\n")
187 wxT(" }\n")
188 wxT("};\n"));
aac18ec7
VZ
189
190 for ( StringSet::const_iterator it = m_ancestorClassNames.begin();
191 it != m_ancestorClassNames.end();
192 ++it )
193 {
9a83f860
VZ
194 file.Write(m_className + wxT("(") + *it + wxT(" *parent){\n") +
195 wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n")
196 wxT(" }\n")
197 wxT("};\n"));
aac18ec7
VZ
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
bd365871 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)
9a83f860 317 parOutput = wxT("resource.cpp");
b8b8c49b 318 else if (flagPython)
9a83f860 319 parOutput = wxT("resource.py");
b8b8c49b 320 else
9a83f860 321 parOutput = wxT("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 }
9a83f860 331 if (!parOutputPath) parOutputPath = wxT(".");
56d2f750 332
db51a209 333 if (!cmdline.Found("n", &parFuncname))
9a83f860 334 parFuncname = wxT("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
3c4845b4
VS
358 if ( wxFileExists(parOutput) )
359 wxRemoveFile(parOutput);
56d2f750 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;
9a83f860
VZ
381 name2.Replace(wxT(":"), wxT("_"));
382 name2.Replace(wxT("/"), wxT("_"));
383 name2.Replace(wxT("\\"), wxT("_"));
384 name2.Replace(wxT("*"), wxT("_"));
385 name2.Replace(wxT("?"), wxT("_"));
f80ea77b 386
9a83f860 387 wxString s = wxFileNameFromPath(parOutput) + wxT("$") + name2;
a7501aeb
VS
388
389 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
f80ea77b 390 {
a7501aeb
VS
391 for (int i = 0;; i++)
392 {
9a83f860 393 s.Printf(wxFileNameFromPath(parOutput) + wxT("$%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)
9a83f860 408 wxPrintf(wxT("processing ") + parFiles[i] + wxT("...\n"));
56d2f750
VS
409
410 wxXmlDocument doc;
f80ea77b 411
56d2f750
VS
412 if (!doc.Load(parFiles[i]))
413 {
9a83f860 414 wxLogError(wxT("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){
9a83f860
VZ
428 if(node->GetName() == wxT("object")
429 && node->GetAttribute(wxT("class"),&classValue)
430 && node->GetAttribute(wxT("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):
9a83f860 455 if ( name == wxT("bitmap") || name == wxT("bitmap2") )
f80ea77b 456 return true;
2ad1ff54 457
9a83f860 458 if ( name == wxT("icon") )
8b34993d 459 return true;
4249ec2c 460
4249ec2c
VS
461 // wxBitmapButton:
462 wxXmlNode *parent = node->GetParent();
f80ea77b 463 if (parent != NULL &&
9a83f860
VZ
464 parent->GetAttribute(wxT("class"), wxT("")) == wxT("wxBitmapButton") &&
465 (name == wxT("focus") ||
466 name == wxT("disabled") ||
467 name == wxT("hover") ||
468 name == wxT("selected")))
f80ea77b
WS
469 return true;
470
4249ec2c 471 // wxBitmap or wxIcon toplevel resources:
9a83f860 472 if ( name == wxT("object") )
4249ec2c 473 {
9a83f860
VZ
474 wxString klass = node->GetAttribute(wxT("class"), wxEmptyString);
475 if (klass == wxT("wxBitmap") ||
476 klass == wxT("wxIcon") ||
477 klass == wxT("data") )
f80ea77b 478 return true;
4249ec2c 479 }
f80ea77b 480
9ec6078f 481 // URLs in wxHtmlWindow:
9a83f860 482 if ( name == wxT("url") &&
9ec6078f 483 parent != NULL &&
9a83f860 484 parent->GetAttribute(wxT("class"), wxT("")) == wxT("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)
9a83f860 516 wxPrintf(wxT("adding ") + fullname + wxT("...\n"));
2b5f62a0 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++)
9a83f860 552 files += flist[i] + wxT(" ");
56d2f750 553 files.RemoveLast();
f80ea77b
WS
554
555 if (flagVerbose)
9a83f860 556 wxPrintf(wxT("compressing ") + parOutput + wxT("...\n"));
f80ea77b 557
4249ec2c
VS
558 wxString cwd = wxGetCwd();
559 wxSetWorkingDirectory(parOutputPath);
9a83f860
VZ
560 int execres = wxExecute(wxT("zip -9 -j ") +
561 wxString(flagVerbose ? wxT("\"") : wxT("-q \"")) +
562 parOutput + wxT("\" ") + files, true);
4249ec2c
VS
563 wxSetWorkingDirectory(cwd);
564 if (execres == -1)
56d2f750 565 {
9a83f860
VZ
566 wxLogError(wxT("Unable to execute zip program. Make sure it is in the path."));
567 wxLogError(wxT("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
9a83f860 588 snum.Printf(wxT("%i"), num);
e22fa4d7
VZ
589 output.Printf(wxT("static size_t xml_res_size_") + snum + wxT(" = %lu;\n"),
590 static_cast<unsigned long>(lng));
9a83f860 591 output += wxT("static unsigned char xml_res_file_") + snum + wxT("[] = {\n");
e066e256
VS
592 // we cannot use string literals because MSVC is dumb wannabe compiler
593 // with arbitrary limitation to 2048 strings :(
f80ea77b 594
56d2f750
VS
595 unsigned char *buffer = new unsigned char[lng];
596 file.Read(buffer, lng);
f80ea77b 597
f6853b4a 598 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 599 {
9a83f860
VZ
600 tmp.Printf(wxT("%i"), buffer[i]);
601 if (i != 0) output << wxT(',');
e066e256 602 if (linelng > 70)
f6853b4a
VS
603 {
604 linelng = 0;
9a83f860 605 output << wxT("\n");
f6853b4a 606 }
e066e256
VS
607 output << tmp;
608 linelng += tmp.Length()+1;
56d2f750 609 }
f80ea77b 610
56d2f750 611 delete[] buffer;
f80ea77b 612
9a83f860 613 output += wxT("};\n\n");
f80ea77b 614
56d2f750
VS
615 return output;
616}
617
618
619void XmlResApp::MakePackageCPP(const wxArrayString& flist)
620{
5851504d 621 wxFFile file(parOutput, wxT("wt"));
9b1aeed1 622 unsigned i;
56d2f750 623
f80ea77b 624 if (flagVerbose)
9a83f860 625 wxPrintf(wxT("creating C++ source file ") + parOutput + wxT("...\n"));
f80ea77b 626
5881d27b
VS
627 file.Write(""
628"//\n"
629"// This file was automatically generated by wxrc, do not edit by hand.\n"
630"//\n\n"
631"#include <wx/wxprec.h>\n"
632"\n"
633"#ifdef __BORLANDC__\n"
634" #pragma hdrstop\n"
635"#endif\n"
636"\n"
637""
638"#include <wx/filesys.h>\n"
639"#include <wx/fs_mem.h>\n"
640"#include <wx/xrc/xmlres.h>\n"
641"#include <wx/xrc/xh_all.h>\n"
642"\n"
643"#if wxCHECK_VERSION(2,8,5) && wxABI_VERSION >= 20805\n"
644" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
645" wxMemoryFSHandler::AddFileWithMimeType(name, data, size, mime)\n"
646"#else\n"
647" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
648" wxMemoryFSHandler::AddFile(name, data, size)\n"
649"#endif\n"
650"\n");
56d2f750 651
b4a980f4 652 for (i = 0; i < flist.GetCount(); i++)
4249ec2c
VS
653 file.Write(
654 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
f80ea77b 655
5881d27b 656 file.Write(""
09f01082 657"void " + parFuncname + "()\n"
5881d27b
VS
658"{\n"
659"\n"
660" // Check for memory FS. If not present, load the handler:\n"
661" {\n"
662" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n"
663" wxFileSystem fsys;\n"
664" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n"
665" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n"
666" if (f) delete f;\n"
667" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n"
668" }\n"
669"\n");
56d2f750 670
b4a980f4 671 for (i = 0; i < flist.GetCount(); i++)
56d2f750
VS
672 {
673 wxString s;
c5d7b81e
VS
674
675 wxString mime;
676 wxString ext = wxFileName(flist[i]).GetExt();
9a83f860
VZ
677 if ( ext.Lower() == wxT("xrc") )
678 mime = wxT("text/xml");
e74f1202 679#if wxUSE_MIMETYPE
c5d7b81e
VS
680 else
681 {
682 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
683 if ( ft )
e74f1202 684 {
c5d7b81e 685 ft->GetMimeType(&mime);
e74f1202
VZ
686 delete ft;
687 }
c5d7b81e 688 }
e74f1202 689#endif // wxUSE_MIMETYPE
c5d7b81e 690
5881d27b 691 s.Printf(" XRC_ADD_FILE(wxT(\"XRC_resource/" + flist[i] +
9b1aeed1 692 "\"), xml_res_file_%u, xml_res_size_%u, wxT(\"%s\"));\n",
c5d7b81e 693 i, i, mime.c_str());
56d2f750
VS
694 file.Write(s);
695 }
f6853b4a 696
b4a980f4 697 for (i = 0; i < parFiles.GetCount(); i++)
f6853b4a 698 {
5881d27b
VS
699 file.Write(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/" +
700 GetInternalFileName(parFiles[i], flist) + "\"));\n");
f6853b4a 701 }
f80ea77b 702
5881d27b 703 file.Write("}\n");
56d2f750 704
f6853b4a 705
56d2f750 706}
c8b7a961 707
1dce6f09
VS
708void XmlResApp::GenCPPHeader()
709{
21567b09
VZ
710 // Generate the output header in the same directory as the source file.
711 wxFileName headerName(parOutput);
712 headerName.SetExt("h");
f80ea77b 713
21567b09 714 wxFFile file(headerName.GetFullPath(), wxT("wt"));
1dce6f09 715 file.Write(
5881d27b
VS
716"//\n"
717"// This file was automatically generated by wxrc, do not edit by hand.\n"
718"//\n\n"
21567b09
VZ
719"#ifndef __" + headerName.GetName() + "_h__\n"
720"#define __" + headerName.GetName() + "_h__\n"
f80ea77b 721);
b4a980f4 722 for(size_t i=0;i<aXRCWndClassData.GetCount();++i){
f80ea77b
WS
723 aXRCWndClassData.Item(i).GenerateHeaderCode(file);
724 }
1dce6f09 725 file.Write(
5881d27b 726 "\nvoid \n"
aa063b24 727 + parFuncname
5881d27b 728 + "();\n#endif\n");
1dce6f09
VS
729}
730
b8b8c49b
VS
731static wxString FileToPythonArray(wxString filename, int num)
732{
733 wxString output;
734 wxString tmp;
735 wxString snum;
5851504d 736 wxFFile file(filename, wxT("rb"));
2ad1ff54
WS
737 wxFileOffset offset = file.Length();
738 wxASSERT_MSG( offset >= 0 , wxT("Invalid file length") );
17a1ebd1
VZ
739
740 const size_t lng = wx_truncate_cast(size_t, offset);
81d3348a 741 wxASSERT_MSG( static_cast<wxFileOffset>(lng) == offset,
563a24f0 742 wxT("Huge file not supported") );
f80ea77b 743
9a83f860 744 snum.Printf(wxT("%i"), num);
09f01082 745 output = " xml_res_file_" + snum + " = '''\\\n";
f80ea77b 746
b8b8c49b
VS
747 unsigned char *buffer = new unsigned char[lng];
748 file.Read(buffer, lng);
f80ea77b 749
b8b8c49b
VS
750 for (size_t i = 0, linelng = 0; i < lng; i++)
751 {
752 unsigned char c = buffer[i];
753 if (c == '\n')
754 {
755 tmp = (wxChar)c;
756 linelng = 0;
757 }
9a9942f7 758 else if (c < 32 || c > 127 || c == '\'')
9a83f860 759 tmp.Printf(wxT("\\x%02x"), c);
b8b8c49b 760 else if (c == '\\')
9a83f860 761 tmp = wxT("\\\\");
b8b8c49b
VS
762 else
763 tmp = (wxChar)c;
764 if (linelng > 70)
765 {
766 linelng = 0;
9a83f860 767 output << wxT("\\\n");
b8b8c49b
VS
768 }
769 output << tmp;
770 linelng += tmp.Length();
771 }
f80ea77b 772
b8b8c49b 773 delete[] buffer;
f80ea77b 774
9a83f860 775 output += wxT("'''\n\n");
f80ea77b 776
b8b8c49b
VS
777 return output;
778}
779
780
781void XmlResApp::MakePackagePython(const wxArrayString& flist)
782{
5851504d 783 wxFFile file(parOutput, wxT("wt"));
9b1aeed1 784 unsigned i;
b8b8c49b 785
f80ea77b 786 if (flagVerbose)
9a83f860 787 wxPrintf(wxT("creating Python source file ") + parOutput + wxT("...\n"));
f80ea77b 788
b8b8c49b 789 file.Write(
5881d27b
VS
790 "#\n"
791 "# This file was automatically generated by wxrc, do not edit by hand.\n"
792 "#\n\n"
793 "import wx\n"
794 "import wx.xrc\n\n"
b8b8c49b
VS
795 );
796
f80ea77b 797
09f01082 798 file.Write("def " + parFuncname + "():\n");
b8b8c49b 799
b4a980f4 800 for (i = 0; i < flist.GetCount(); i++)
4249ec2c
VS
801 file.Write(
802 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
b8b8c49b 803
2ad1ff54 804 file.Write(
5881d27b
VS
805 " # check if the memory filesystem handler has been loaded yet, and load it if not\n"
806 " wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n"
807 " fsys = wx.FileSystem()\n"
808 " f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n"
809 " wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n"
810 " if f is not None:\n"
811 " f.Destroy()\n"
812 " else:\n"
813 " wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n"
814 "\n"
815 " # load all the strings as memory files and load into XmlRes\n"
b27a4ef4
RD
816 );
817
2ad1ff54 818
b4a980f4 819 for (i = 0; i < flist.GetCount(); i++)
b8b8c49b
VS
820 {
821 wxString s;
5881d27b 822 s.Printf(" wx.MemoryFSHandler.AddFile('XRC_resource/" + flist[i] +
9b1aeed1 823 "', xml_res_file_%u)\n", i);
b8b8c49b
VS
824 file.Write(s);
825 }
b4a980f4 826 for (i = 0; i < parFiles.GetCount(); i++)
b27a4ef4 827 {
5881d27b
VS
828 file.Write(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/" +
829 GetInternalFileName(parFiles[i], flist) + "')\n");
b27a4ef4
RD
830 }
831
5881d27b 832 file.Write("\n");
b8b8c49b
VS
833}
834
c8b7a961
VS
835
836
837void XmlResApp::OutputGettext()
838{
21b2dde5 839 ExtractedStrings str = FindStrings();
f80ea77b 840
c8b7a961 841 wxFFile fout;
1dce6f09
VS
842 if (parOutput.empty())
843 fout.Attach(stdout);
844 else
845 fout.Open(parOutput, wxT("wt"));
f80ea77b 846
21b2dde5
VS
847 for (ExtractedStrings::const_iterator i = str.begin(); i != str.end(); ++i)
848 {
0d373eac
VS
849 const wxFileName filename(i->filename);
850
21b2dde5 851 wxString s;
0d373eac
VS
852 s.Printf("#line %d \"%s\"\n",
853 i->lineNo, filename.GetFullPath(wxPATH_UNIX));
21b2dde5 854
21b2dde5
VS
855 fout.Write(s);
856 fout.Write("_(\"" + i->str + "\");\n");
857 }
f80ea77b 858
c8b7a961
VS
859 if (!parOutput) fout.Detach();
860}
861
862
863
21b2dde5 864ExtractedStrings XmlResApp::FindStrings()
c8b7a961 865{
21b2dde5 866 ExtractedStrings arr, a2;
c8b7a961 867
b4a980f4 868 for (size_t i = 0; i < parFiles.GetCount(); i++)
c8b7a961 869 {
f80ea77b 870 if (flagVerbose)
9a83f860 871 wxPrintf(wxT("processing ") + parFiles[i] + wxT("...\n"));
c8b7a961 872
f80ea77b 873 wxXmlDocument doc;
c8b7a961
VS
874 if (!doc.Load(parFiles[i]))
875 {
9a83f860 876 wxLogError(wxT("Error parsing file ") + parFiles[i]);
c8b7a961
VS
877 retCode = 1;
878 continue;
879 }
21b2dde5
VS
880 a2 = FindStrings(parFiles[i], doc.GetRoot());
881
c8b7a961
VS
882 WX_APPEND_ARRAY(arr, a2);
883 }
f80ea77b 884
c8b7a961
VS
885 return arr;
886}
887
888
889
c109ef11
VS
890static wxString ConvertText(const wxString& str)
891{
892 wxString str2;
893 const wxChar *dt;
894
895 for (dt = str.c_str(); *dt; dt++)
896 {
897 if (*dt == wxT('_'))
898 {
2148394a
VS
899 if ( *(dt+1) == 0 )
900 str2 << wxT('_');
901 else if ( *(++dt) == wxT('_') )
c109ef11
VS
902 str2 << wxT('_');
903 else
904 str2 << wxT('&') << *dt;
905 }
f80ea77b 906 else
c109ef11
VS
907 {
908 switch (*dt)
909 {
910 case wxT('\n') : str2 << wxT("\\n"); break;
911 case wxT('\t') : str2 << wxT("\\t"); break;
912 case wxT('\r') : str2 << wxT("\\r"); break;
2b5f62a0
VZ
913 case wxT('\\') : if ((*(dt+1) != 'n') &&
914 (*(dt+1) != 't') &&
915 (*(dt+1) != 'r'))
916 str2 << wxT("\\\\");
917 else
f80ea77b 918 str2 << wxT("\\");
2b5f62a0 919 break;
904a226c 920 case wxT('"') : str2 << wxT("\\\""); break;
c109ef11
VS
921 default : str2 << *dt; break;
922 }
923 }
924 }
925
926 return str2;
927}
928
929
21b2dde5
VS
930ExtractedStrings
931XmlResApp::FindStrings(const wxString& filename, wxXmlNode *node)
c8b7a961 932{
21b2dde5 933 ExtractedStrings arr;
c8b7a961
VS
934
935 wxXmlNode *n = node;
936 if (n == NULL) return arr;
937 n = n->GetChildren();
f80ea77b 938
c8b7a961
VS
939 while (n)
940 {
941 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
942 // parent is an element, i.e. has subnodes...
f80ea77b 943 (n->GetType() == wxXML_TEXT_NODE ||
c8b7a961
VS
944 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
945 // ...it is textnode...
946 (
9a83f860
VZ
947 node/*not n!*/->GetName() == wxT("label") ||
948 (node/*not n!*/->GetName() == wxT("value") &&
c8b7a961 949 !n->GetContent().IsNumber()) ||
9a83f860
VZ
950 node/*not n!*/->GetName() == wxT("help") ||
951 node/*not n!*/->GetName() == wxT("longhelp") ||
952 node/*not n!*/->GetName() == wxT("tooltip") ||
953 node/*not n!*/->GetName() == wxT("htmlcode") ||
954 node/*not n!*/->GetName() == wxT("title") ||
3496ce18
VZ
955 node/*not n!*/->GetName() == wxT("item") ||
956 node/*not n!*/->GetName() == wxT("message") ||
957 node/*not n!*/->GetName() == wxT("note") ||
958 node/*not n!*/->GetName() == wxT("defaultdirectory") ||
959 node/*not n!*/->GetName() == wxT("defaultfilename") ||
960 node/*not n!*/->GetName() == wxT("defaultfolder") ||
961 node/*not n!*/->GetName() == wxT("filter") ||
962 node/*not n!*/->GetName() == wxT("caption")
c8b7a961 963 ))
c109ef11 964 // ...and known to contain translatable string
c8b7a961 965 {
8da9d91c 966 if (!flagGettext ||
9a83f860 967 node->GetAttribute(wxT("translate"), wxT("1")) != wxT("0"))
8da9d91c 968 {
21b2dde5
VS
969 arr.push_back
970 (
971 ExtractedString
972 (
973 ConvertText(n->GetContent()),
974 filename,
975 n->GetLineNumber()
976 )
977 );
8da9d91c 978 }
c8b7a961 979 }
f80ea77b 980
c8b7a961
VS
981 // subnodes:
982 if (n->GetType() == wxXML_ELEMENT_NODE)
983 {
21b2dde5 984 ExtractedStrings a2 = FindStrings(filename, n);
c8b7a961
VS
985 WX_APPEND_ARRAY(arr, a2);
986 }
f80ea77b 987
c8b7a961
VS
988 n = n->GetNext();
989 }
990 return arr;
991}