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