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