]>
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 | ||
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 |
36 | class XRCWidgetData |
37 | { | |
38 | public: | |
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; } | |
43 | private: | |
44 | wxString m_class; | |
45 | wxString m_name; | |
46 | }; | |
47 | #include "wx/arrimpl.cpp" | |
48 | WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData); | |
49 | WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData); | |
50 | ||
51 | class XRCWndClassData | |
52 | { | |
f80ea77b | 53 | private: |
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 | 78 | public: |
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") | |
118 | _T(" wxXmlResource::Get()->LoadObject(this,NULL,\"") | |
f80ea77b | 119 | + m_className |
aa063b24 | 120 | + _T("\",\"") |
f80ea77b WS |
121 | + m_parentClassName |
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 | 137 | file.Write(_T(" }\n")); |
f80ea77b | 138 | |
1dce6f09 | 139 | file.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 | }; |
150 | WX_DECLARE_OBJARRAY(XRCWndClassData,ArrayOfXRCWndClassData); | |
151 | WX_DEFINE_OBJARRAY(ArrayOfXRCWndClassData); | |
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 }, |
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 | ||
249 | void 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 | ||
302 | void 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 |
325 | wxString 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 | |
348 | wxArrayString 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? |
397 | static bool NodeContainsFilename(wxXmlNode *node) | |
398 | { | |
399 | // Any bitmaps: | |
400 | if (node->GetName() == _T("bitmap")) | |
f80ea77b | 401 | return true; |
4249ec2c VS |
402 | |
403 | // URLs in wxHtmlWindow: | |
404 | if (node->GetName() == _T("url")) | |
f80ea77b WS |
405 | return true; |
406 | ||
4249ec2c VS |
407 | // wxBitmapButton: |
408 | wxXmlNode *parent = node->GetParent(); | |
f80ea77b | 409 | if (parent != NULL && |
4249ec2c | 410 | parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") && |
f80ea77b | 411 | (node->GetName() == _T("focus") || |
4249ec2c VS |
412 | node->GetName() == _T("disabled") || |
413 | node->GetName() == _T("selected"))) | |
f80ea77b WS |
414 | return true; |
415 | ||
4249ec2c VS |
416 | // wxBitmap or wxIcon toplevel resources: |
417 | if (node->GetName() == _T("object")) | |
418 | { | |
419 | wxString klass = node->GetPropVal(_T("class"), wxEmptyString); | |
420 | if (klass == _T("wxBitmap") || klass == _T("wxIcon")) | |
f80ea77b | 421 | return true; |
4249ec2c | 422 | } |
f80ea77b WS |
423 | |
424 | return false; | |
4249ec2c | 425 | } |
56d2f750 | 426 | |
f6853b4a VS |
427 | // find all files mentioned in structure, e.g. <bitmap>filename</bitmap> |
428 | void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath) | |
429 | { | |
2b5f62a0 VZ |
430 | // Is 'node' XML node element? |
431 | if (node == NULL) return; | |
432 | if (node->GetType() != wxXML_ELEMENT_NODE) return; | |
433 | ||
4249ec2c | 434 | bool containsFilename = NodeContainsFilename(node); |
2b5f62a0 VZ |
435 | |
436 | wxXmlNode *n = node->GetChildren(); | |
f6853b4a VS |
437 | while (n) |
438 | { | |
2b5f62a0 | 439 | if (containsFilename && |
f80ea77b | 440 | (n->GetType() == wxXML_TEXT_NODE || |
2b5f62a0 | 441 | n->GetType() == wxXML_CDATA_SECTION_NODE)) |
f6853b4a VS |
442 | { |
443 | wxString fullname; | |
2b5f62a0 VZ |
444 | if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty()) |
445 | fullname = n->GetContent(); | |
446 | else | |
4249ec2c | 447 | fullname = inputPath + wxFILE_SEP_PATH + n->GetContent(); |
a7501aeb | 448 | |
f80ea77b | 449 | if (flagVerbose) |
2b5f62a0 VZ |
450 | wxPrintf(_T("adding ") + fullname + _T("...\n")); |
451 | ||
a7501aeb | 452 | wxString filename = GetInternalFileName(n->GetContent(), flist); |
f6853b4a | 453 | n->SetContent(filename); |
f6853b4a | 454 | |
2b5f62a0 VZ |
455 | if (flist.Index(filename) == wxNOT_FOUND) |
456 | flist.Add(filename); | |
f6853b4a VS |
457 | |
458 | wxFileInputStream sin(fullname); | |
4249ec2c | 459 | wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename); |
f6853b4a VS |
460 | sin.Read(sout); // copy the stream |
461 | } | |
2b5f62a0 | 462 | |
f6853b4a VS |
463 | // subnodes: |
464 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
465 | FindFilesInXML(n, flist, inputPath); | |
2b5f62a0 | 466 | |
f6853b4a VS |
467 | n = n->GetNext(); |
468 | } | |
469 | } | |
470 | ||
471 | ||
472 | ||
56d2f750 VS |
473 | void XmlResApp::DeleteTempFiles(const wxArrayString& flist) |
474 | { | |
475 | for (size_t i = 0; i < flist.Count(); i++) | |
4249ec2c | 476 | wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]); |
56d2f750 VS |
477 | } |
478 | ||
479 | ||
480 | ||
481 | void XmlResApp::MakePackageZIP(const wxArrayString& flist) | |
482 | { | |
483 | wxString files; | |
f80ea77b | 484 | |
56d2f750 | 485 | for (size_t i = 0; i < flist.Count(); i++) |
2b5f62a0 | 486 | files += flist[i] + _T(" "); |
56d2f750 | 487 | files.RemoveLast(); |
f80ea77b WS |
488 | |
489 | if (flagVerbose) | |
2b5f62a0 | 490 | wxPrintf(_T("compressing ") + parOutput + _T("...\n")); |
f80ea77b | 491 | |
4249ec2c VS |
492 | wxString cwd = wxGetCwd(); |
493 | wxSetWorkingDirectory(parOutputPath); | |
f80ea77b WS |
494 | int execres = wxExecute(_T("zip -9 -j ") + |
495 | wxString(flagVerbose ? _T("") : _T("-q ")) + | |
496 | parOutput + _T(" ") + files, true); | |
4249ec2c VS |
497 | wxSetWorkingDirectory(cwd); |
498 | if (execres == -1) | |
56d2f750 | 499 | { |
2b5f62a0 VZ |
500 | wxLogError(_T("Unable to execute zip program. Make sure it is in the path.")); |
501 | wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/")); | |
56d2f750 VS |
502 | retCode = 1; |
503 | return; | |
504 | } | |
505 | } | |
506 | ||
507 | ||
508 | ||
56d2f750 VS |
509 | static wxString FileToCppArray(wxString filename, int num) |
510 | { | |
511 | wxString output; | |
56d2f750 | 512 | wxString tmp; |
f6853b4a | 513 | wxString snum; |
5851504d | 514 | wxFFile file(filename, wxT("rb")); |
56d2f750 | 515 | size_t lng = file.Length(); |
f80ea77b | 516 | |
2b5f62a0 VZ |
517 | snum.Printf(_T("%i"), num); |
518 | output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng); | |
519 | output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n"); | |
e066e256 VS |
520 | // we cannot use string literals because MSVC is dumb wannabe compiler |
521 | // with arbitrary limitation to 2048 strings :( | |
f80ea77b | 522 | |
56d2f750 VS |
523 | unsigned char *buffer = new unsigned char[lng]; |
524 | file.Read(buffer, lng); | |
f80ea77b | 525 | |
f6853b4a | 526 | for (size_t i = 0, linelng = 0; i < lng; i++) |
56d2f750 | 527 | { |
2b5f62a0 VZ |
528 | tmp.Printf(_T("%i"), buffer[i]); |
529 | if (i != 0) output << _T(','); | |
e066e256 | 530 | if (linelng > 70) |
f6853b4a VS |
531 | { |
532 | linelng = 0; | |
2b5f62a0 | 533 | output << _T("\n"); |
f6853b4a | 534 | } |
e066e256 VS |
535 | output << tmp; |
536 | linelng += tmp.Length()+1; | |
56d2f750 | 537 | } |
f80ea77b | 538 | |
56d2f750 | 539 | delete[] buffer; |
f80ea77b | 540 | |
2b5f62a0 | 541 | output += _T("};\n\n"); |
f80ea77b | 542 | |
56d2f750 VS |
543 | return output; |
544 | } | |
545 | ||
546 | ||
547 | void XmlResApp::MakePackageCPP(const wxArrayString& flist) | |
548 | { | |
5851504d | 549 | wxFFile file(parOutput, wxT("wt")); |
56d2f750 VS |
550 | size_t i; |
551 | ||
f80ea77b | 552 | if (flagVerbose) |
2b5f62a0 | 553 | wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n")); |
f80ea77b | 554 | |
2b5f62a0 VZ |
555 | file.Write(_T("") |
556 | _T("//\n") | |
557 | _T("// This file was automatically generated by wxrc, do not edit by hand.\n") | |
558 | _T("//\n\n") | |
559 | _T("#include <wx/wxprec.h>\n") | |
560 | _T("\n") | |
561 | _T("#ifdef __BORLANDC__\n") | |
562 | _T(" #pragma hdrstop\n") | |
563 | _T("#endif\n") | |
564 | _T("\n") | |
2b5f62a0 VZ |
565 | _T("") |
566 | _T("#include <wx/filesys.h>\n") | |
567 | _T("#include <wx/fs_mem.h>\n") | |
568 | _T("#include <wx/xrc/xmlres.h>\n") | |
569 | _T("#include <wx/xrc/xh_all.h>\n") | |
570 | _T("\n")); | |
56d2f750 VS |
571 | |
572 | for (i = 0; i < flist.Count(); i++) | |
4249ec2c VS |
573 | file.Write( |
574 | FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i)); | |
f80ea77b | 575 | |
2b5f62a0 | 576 | file.Write(_T("") |
5851504d | 577 | _T("void ") + parFuncname + wxT("()\n") |
2b5f62a0 VZ |
578 | _T("{\n") |
579 | _T("\n") | |
580 | _T(" // Check for memory FS. If not present, load the handler:\n") | |
581 | _T(" {\n") | |
5851504d | 582 | _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n") |
2b5f62a0 | 583 | _T(" wxFileSystem fsys;\n") |
5851504d VS |
584 | _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n") |
585 | _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n") | |
2b5f62a0 VZ |
586 | _T(" if (f) delete f;\n") |
587 | _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n") | |
588 | _T(" }\n") | |
589 | _T("\n")); | |
56d2f750 VS |
590 | |
591 | for (i = 0; i < flist.Count(); i++) | |
592 | { | |
593 | wxString s; | |
5851504d VS |
594 | s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] + |
595 | _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i); | |
56d2f750 VS |
596 | file.Write(s); |
597 | } | |
f6853b4a VS |
598 | |
599 | for (i = 0; i < parFiles.Count(); i++) | |
600 | { | |
5851504d VS |
601 | file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") + |
602 | GetInternalFileName(parFiles[i], flist) + _T("\"));\n")); | |
f6853b4a | 603 | } |
f80ea77b | 604 | |
2b5f62a0 | 605 | file.Write(_T("}\n")); |
56d2f750 | 606 | |
f6853b4a | 607 | |
56d2f750 | 608 | } |
c8b7a961 | 609 | |
1dce6f09 VS |
610 | void XmlResApp::GenCPPHeader() |
611 | { | |
76ee0497 | 612 | wxString fileSpec = ((parOutput.BeforeLast('.')).AfterLast('/')).AfterLast('\\'); |
1dce6f09 | 613 | wxString heaFileName = fileSpec + _T(".h"); |
f80ea77b | 614 | |
1dce6f09 VS |
615 | wxFFile file(heaFileName, wxT("wt")); |
616 | file.Write( | |
617 | _T("//\n") | |
618 | _T("// This file was automatically generated by wxrc, do not edit by hand.\n") | |
619 | _T("//\n\n") | |
620 | _T("#ifndef __") + fileSpec + _T("_h__\n") | |
621 | _T("#define __") + fileSpec + _T("_h__\n") | |
f80ea77b | 622 | ); |
1dce6f09 | 623 | for(size_t i=0;i<aXRCWndClassData.Count();++i){ |
f80ea77b WS |
624 | aXRCWndClassData.Item(i).GenerateHeaderCode(file); |
625 | } | |
1dce6f09 | 626 | file.Write( |
aa063b24 RD |
627 | _T("\nvoid \n") |
628 | + parFuncname | |
629 | + _T("();\n#endif\n")); | |
1dce6f09 VS |
630 | } |
631 | ||
b8b8c49b VS |
632 | static wxString FileToPythonArray(wxString filename, int num) |
633 | { | |
634 | wxString output; | |
635 | wxString tmp; | |
636 | wxString snum; | |
5851504d | 637 | wxFFile file(filename, wxT("rb")); |
b8b8c49b | 638 | size_t lng = file.Length(); |
f80ea77b | 639 | |
2b5f62a0 | 640 | snum.Printf(_T("%i"), num); |
9a9942f7 | 641 | output = _T(" xml_res_file_") + snum + _T(" = '''\\\n"); |
f80ea77b | 642 | |
b8b8c49b VS |
643 | unsigned char *buffer = new unsigned char[lng]; |
644 | file.Read(buffer, lng); | |
f80ea77b | 645 | |
b8b8c49b VS |
646 | for (size_t i = 0, linelng = 0; i < lng; i++) |
647 | { | |
648 | unsigned char c = buffer[i]; | |
649 | if (c == '\n') | |
650 | { | |
651 | tmp = (wxChar)c; | |
652 | linelng = 0; | |
653 | } | |
9a9942f7 | 654 | else if (c < 32 || c > 127 || c == '\'') |
2b5f62a0 | 655 | tmp.Printf(_T("\\x%02x"), c); |
b8b8c49b | 656 | else if (c == '\\') |
2b5f62a0 | 657 | tmp = _T("\\\\"); |
b8b8c49b VS |
658 | else |
659 | tmp = (wxChar)c; | |
660 | if (linelng > 70) | |
661 | { | |
662 | linelng = 0; | |
2b5f62a0 | 663 | output << _T("\\\n"); |
b8b8c49b VS |
664 | } |
665 | output << tmp; | |
666 | linelng += tmp.Length(); | |
667 | } | |
f80ea77b | 668 | |
b8b8c49b | 669 | delete[] buffer; |
f80ea77b | 670 | |
9a9942f7 | 671 | output += _T("'''\n\n"); |
f80ea77b | 672 | |
b8b8c49b VS |
673 | return output; |
674 | } | |
675 | ||
676 | ||
677 | void XmlResApp::MakePackagePython(const wxArrayString& flist) | |
678 | { | |
5851504d | 679 | wxFFile file(parOutput, wxT("wt")); |
b8b8c49b VS |
680 | size_t i; |
681 | ||
f80ea77b | 682 | if (flagVerbose) |
2b5f62a0 | 683 | wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n")); |
f80ea77b | 684 | |
b8b8c49b | 685 | file.Write( |
2b5f62a0 VZ |
686 | _T("#\n") |
687 | _T("# This file was automatically generated by wxrc, do not edit by hand.\n") | |
688 | _T("#\n\n") | |
b27a4ef4 RD |
689 | _T("import wx\n") |
690 | _T("import wx.xrc\n\n") | |
b8b8c49b VS |
691 | ); |
692 | ||
f80ea77b | 693 | |
2b5f62a0 | 694 | file.Write(_T("def ") + parFuncname + _T("():\n")); |
b8b8c49b VS |
695 | |
696 | for (i = 0; i < flist.Count(); i++) | |
4249ec2c VS |
697 | file.Write( |
698 | FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i)); | |
b8b8c49b | 699 | |
b27a4ef4 RD |
700 | file.Write( |
701 | _T(" # check if the memory filesystem handler has been loaded yet, and load it if not\n") | |
702 | _T(" wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n") | |
703 | _T(" fsys = wx.FileSystem()\n") | |
704 | _T(" f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n") | |
705 | _T(" wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n") | |
706 | _T(" if f is not None:\n") | |
707 | _T(" f.Destroy()\n") | |
708 | _T(" else:\n") | |
709 | _T(" wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n") | |
710 | _T("\n") | |
711 | _T(" # load all the strings as memory files and load into XmlRes\n") | |
712 | ); | |
713 | ||
714 | ||
b8b8c49b VS |
715 | for (i = 0; i < flist.Count(); i++) |
716 | { | |
717 | wxString s; | |
b27a4ef4 RD |
718 | s.Printf(_T(" wx.MemoryFSHandler.AddFile('XRC_resource/") + flist[i] + |
719 | _T("', xml_res_file_%i)\n"), i); | |
b8b8c49b VS |
720 | file.Write(s); |
721 | } | |
b27a4ef4 RD |
722 | for (i = 0; i < parFiles.Count(); i++) |
723 | { | |
724 | file.Write(_T(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/") + | |
725 | GetInternalFileName(parFiles[i], flist) + _T("')\n")); | |
726 | } | |
727 | ||
728 | file.Write(_T("\n")); | |
b8b8c49b VS |
729 | } |
730 | ||
c8b7a961 VS |
731 | |
732 | ||
733 | void XmlResApp::OutputGettext() | |
734 | { | |
735 | wxArrayString str = FindStrings(); | |
f80ea77b | 736 | |
c8b7a961 | 737 | wxFFile fout; |
1dce6f09 VS |
738 | if (parOutput.empty()) |
739 | fout.Attach(stdout); | |
740 | else | |
741 | fout.Open(parOutput, wxT("wt")); | |
f80ea77b | 742 | |
c8b7a961 | 743 | for (size_t i = 0; i < str.GetCount(); i++) |
0653d364 | 744 | fout.Write(_T("_(\"") + str[i] + _T("\");\n")); |
f80ea77b | 745 | |
c8b7a961 VS |
746 | if (!parOutput) fout.Detach(); |
747 | } | |
748 | ||
749 | ||
750 | ||
751 | wxArrayString XmlResApp::FindStrings() | |
752 | { | |
753 | wxArrayString arr, a2; | |
754 | ||
755 | for (size_t i = 0; i < parFiles.Count(); i++) | |
756 | { | |
f80ea77b | 757 | if (flagVerbose) |
2b5f62a0 | 758 | wxPrintf(_T("processing ") + parFiles[i] + _T("...\n")); |
c8b7a961 | 759 | |
f80ea77b | 760 | wxXmlDocument doc; |
c8b7a961 VS |
761 | if (!doc.Load(parFiles[i])) |
762 | { | |
2b5f62a0 | 763 | wxLogError(_T("Error parsing file ") + parFiles[i]); |
c8b7a961 VS |
764 | retCode = 1; |
765 | continue; | |
766 | } | |
767 | a2 = FindStrings(doc.GetRoot()); | |
768 | WX_APPEND_ARRAY(arr, a2); | |
769 | } | |
f80ea77b | 770 | |
c8b7a961 VS |
771 | return arr; |
772 | } | |
773 | ||
774 | ||
775 | ||
c109ef11 VS |
776 | static wxString ConvertText(const wxString& str) |
777 | { | |
778 | wxString str2; | |
779 | const wxChar *dt; | |
780 | ||
781 | for (dt = str.c_str(); *dt; dt++) | |
782 | { | |
783 | if (*dt == wxT('_')) | |
784 | { | |
785 | if ( *(++dt) == wxT('_') ) | |
786 | str2 << wxT('_'); | |
787 | else | |
788 | str2 << wxT('&') << *dt; | |
789 | } | |
f80ea77b | 790 | else |
c109ef11 VS |
791 | { |
792 | switch (*dt) | |
793 | { | |
794 | case wxT('\n') : str2 << wxT("\\n"); break; | |
795 | case wxT('\t') : str2 << wxT("\\t"); break; | |
796 | case wxT('\r') : str2 << wxT("\\r"); break; | |
2b5f62a0 VZ |
797 | case wxT('\\') : if ((*(dt+1) != 'n') && |
798 | (*(dt+1) != 't') && | |
799 | (*(dt+1) != 'r')) | |
800 | str2 << wxT("\\\\"); | |
801 | else | |
f80ea77b | 802 | str2 << wxT("\\"); |
2b5f62a0 | 803 | break; |
904a226c | 804 | case wxT('"') : str2 << wxT("\\\""); break; |
c109ef11 VS |
805 | default : str2 << *dt; break; |
806 | } | |
807 | } | |
808 | } | |
809 | ||
810 | return str2; | |
811 | } | |
812 | ||
813 | ||
c8b7a961 VS |
814 | wxArrayString XmlResApp::FindStrings(wxXmlNode *node) |
815 | { | |
816 | wxArrayString arr; | |
817 | ||
818 | wxXmlNode *n = node; | |
819 | if (n == NULL) return arr; | |
820 | n = n->GetChildren(); | |
f80ea77b | 821 | |
c8b7a961 VS |
822 | while (n) |
823 | { | |
824 | if ((node->GetType() == wxXML_ELEMENT_NODE) && | |
825 | // parent is an element, i.e. has subnodes... | |
f80ea77b | 826 | (n->GetType() == wxXML_TEXT_NODE || |
c8b7a961 VS |
827 | n->GetType() == wxXML_CDATA_SECTION_NODE) && |
828 | // ...it is textnode... | |
829 | ( | |
830 | node/*not n!*/->GetName() == _T("label") || | |
831 | (node/*not n!*/->GetName() == _T("value") && | |
832 | !n->GetContent().IsNumber()) || | |
833 | node/*not n!*/->GetName() == _T("help") || | |
834 | node/*not n!*/->GetName() == _T("longhelp") || | |
835 | node/*not n!*/->GetName() == _T("tooltip") || | |
836 | node/*not n!*/->GetName() == _T("htmlcode") || | |
0653d364 VS |
837 | node/*not n!*/->GetName() == _T("title") || |
838 | node/*not n!*/->GetName() == _T("item") | |
c8b7a961 | 839 | )) |
c109ef11 | 840 | // ...and known to contain translatable string |
c8b7a961 | 841 | { |
8da9d91c VS |
842 | if (!flagGettext || |
843 | node->GetPropVal(_T("translate"), _T("1")) != _T("0")) | |
844 | { | |
845 | arr.Add(ConvertText(n->GetContent())); | |
846 | } | |
c8b7a961 | 847 | } |
f80ea77b | 848 | |
c8b7a961 VS |
849 | // subnodes: |
850 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
851 | { | |
852 | wxArrayString a2 = FindStrings(n); | |
853 | WX_APPEND_ARRAY(arr, a2); | |
854 | } | |
f80ea77b | 855 | |
c8b7a961 VS |
856 | n = n->GetNext(); |
857 | } | |
858 | return arr; | |
859 | } |