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