]> git.saurik.com Git - wxWidgets.git/blob - utils/wxrc/wxrc.cpp
applied patches from Mike Oliver <oliver@math.ucla.edu> to return click position...
[wxWidgets.git] / utils / wxrc / wxrc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxrc.cpp
3 // Purpose: XML resource compiler
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
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
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWindows headers
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/cmdline.h"
30 #include "wx/xml/xml.h"
31 #include "wx/xml/xmlio.h"
32 #include "wx/ffile.h"
33 #include "wx/wfstream.h"
34
35
36
37
38
39 /*
40 #if wxUSE_GUI
41 #error "You must compile the resource compiler with wxBase!"
42 #endif
43 */
44
45 class XmlResApp : public wxApp
46 {
47 public:
48
49 #if wxUSE_GUI
50 bool OnInit();
51 #else
52 virtual int OnRun();
53 #endif
54
55 private:
56
57 void ParseParams(const wxCmdLineParser& cmdline);
58 void CompileRes();
59 wxArrayString PrepareTempFiles();
60 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
61
62 void DeleteTempFiles(const wxArrayString& flist);
63 void MakePackageZIP(const wxArrayString& flist);
64 void MakePackageCPP(const wxArrayString& flist);
65
66 bool flagVerbose, flagCPP, flagCompress;
67 wxString parOutput, parFuncname, parOutputPath;
68 wxArrayString parFiles;
69 int retCode;
70 };
71
72 IMPLEMENT_APP(XmlResApp)
73
74 #if wxUSE_GUI
75 bool XmlResApp::OnInit()
76 #else
77 int XmlResApp::OnRun()
78 #endif
79 {
80 static const wxCmdLineEntryDesc cmdLineDesc[] =
81 {
82 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
83 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
84 { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" },
85 { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
86 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" },
87 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
88
89 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
90 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
91
92 { wxCMD_LINE_NONE }
93 };
94
95 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ);
96
97 wxCmdLineParser parser(cmdLineDesc, argc, argv);
98
99 switch (parser.Parse())
100 {
101 case -1:
102 return 0;
103 break;
104
105 case 0:
106 retCode = 0;
107 ParseParams(parser);
108 CompileRes();
109 #if wxUSE_GUI
110 return FALSE;
111 #else
112 return retCode;
113 #endif
114 break;
115
116 default:
117 #if wxUSE_GUI
118 return FALSE;
119 #else
120 return 1;
121 #endif
122 break;
123 }
124 }
125
126
127
128
129 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
130 {
131 flagVerbose = cmdline.Found("v");
132 flagCPP = cmdline.Found("c");
133 flagCompress = flagCPP && !cmdline.Found("u");
134
135 if (!cmdline.Found("o", &parOutput))
136 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
137 parOutputPath = wxPathOnly(parOutput);
138 if (!parOutputPath) parOutputPath = ".";
139
140 if (!cmdline.Found("n", &parFuncname))
141 parFuncname = "InitXmlResource";
142
143 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
144 parFiles.Add(cmdline.GetParam(i));
145 }
146
147
148
149
150 void XmlResApp::CompileRes()
151 {
152 wxArrayString files = PrepareTempFiles();
153
154 wxRemoveFile(parOutput);
155
156 if (!retCode)
157 {
158 if (flagCPP)
159 MakePackageCPP(files);
160 else
161 MakePackageZIP(files);
162 }
163
164 DeleteTempFiles(files);
165 }
166
167
168
169 wxArrayString XmlResApp::PrepareTempFiles()
170 {
171 wxArrayString flist;
172
173 for (size_t i = 0; i < parFiles.Count(); i++)
174 {
175 if (flagVerbose)
176 wxPrintf("processing " + parFiles[i] + "...\n");
177
178 wxXmlDocument doc;
179
180 if (!doc.Load(parFiles[i]))
181 {
182 wxLogError("Error parsing file " + parFiles[i]);
183 retCode = 1;
184 continue;
185 }
186
187 wxString name, ext, path;
188 wxSplitPath(parFiles[i], &path, &name, &ext);
189
190 FindFilesInXML(doc.GetRoot(), flist, path);
191
192 doc.Save(parOutputPath + "/" + name + ".xrc", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
193 flist.Add(name + ".xrc");
194 }
195
196 return flist;
197 }
198
199
200
201 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
202 void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
203 {
204 wxXmlNode *n = node;
205 if (n == NULL) return;
206 n = n->GetChildren();
207
208 while (n)
209 {
210 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
211 // parent is an element, i.e. has subnodes...
212 (n->GetType() == wxXML_TEXT_NODE ||
213 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
214 // ...it is textnode...
215 (node/*not n!*/->GetName() == "bitmap"))
216 // ...and known to contain filename
217 {
218 wxString fullname;
219 wxString filename = n->GetContent();
220 if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
221 else fullname = inputPath + "/" + n->GetContent();
222
223 filename.Replace("/", "_");
224 filename.Replace("\\", "_");
225 filename.Replace("*", "_");
226 filename.Replace("?", "_");
227 n->SetContent(filename);
228
229 if (flagVerbose)
230 wxPrintf("adding " + filename + "...\n");
231
232 flist.Add(filename);
233
234 wxFileInputStream sin(fullname);
235 wxFileOutputStream sout(parOutputPath + "/" + filename);
236 sin.Read(sout); // copy the stream
237 }
238
239 // subnodes:
240 if (n->GetType() == wxXML_ELEMENT_NODE)
241 FindFilesInXML(n, flist, inputPath);
242
243 n = n->GetNext();
244 }
245 }
246
247
248
249 void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
250 {
251 for (size_t i = 0; i < flist.Count(); i++)
252 wxRemoveFile(parOutputPath + "/" + flist[i]);
253 }
254
255
256
257 void XmlResApp::MakePackageZIP(const wxArrayString& flist)
258 {
259 wxString files;
260
261 for (size_t i = 0; i < flist.Count(); i++)
262 files += flist[i] + " ";
263 files.RemoveLast();
264
265 if (flagVerbose)
266 wxPrintf("compressing " + parOutput + "...\n");
267
268 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
269 parOutput + " " + files, TRUE) == -1)
270 {
271 wxLogError("Unable to execute zip program. Make sure it is in the path.");
272 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
273 retCode = 1;
274 return;
275 }
276 }
277
278
279
280
281 static wxString FileToCppArray(wxString filename, int num)
282 {
283 wxString output;
284 wxString tmp;
285 wxString snum;
286 wxFFile file(filename, "rb");
287 size_t lng = file.Length();
288
289 snum.Printf("%i", num);
290 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
291 output += "static unsigned char xml_res_file_" + snum + "[] = {\n";
292 // we cannot use string literals because MSVC is dumb wannabe compiler
293 // with arbitrary limitation to 2048 strings :(
294
295 unsigned char *buffer = new unsigned char[lng];
296 file.Read(buffer, lng);
297
298 for (size_t i = 0, linelng = 0; i < lng; i++)
299 {
300 tmp.Printf("%i", buffer[i]);
301 if (i != 0) output << ',';
302 if (linelng > 70)
303 {
304 linelng = 0;
305 output << "\n";
306 }
307 output << tmp;
308 linelng += tmp.Length()+1;
309 }
310
311 delete[] buffer;
312
313 output += "};\n\n";
314
315 return output;
316 }
317
318
319 void XmlResApp::MakePackageCPP(const wxArrayString& flist)
320 {
321 wxFFile file(parOutput, "wt");
322 size_t i;
323
324 if (flagVerbose)
325 wxPrintf("creating C++ source file " + parOutput + "...\n");
326
327 file.Write("\
328 #include \"wx/wxprec.h\"\n\
329 \n\
330 #ifdef __BORLANDC__\n\
331 #pragma hdrstop\n\
332 #endif\n\
333 \n\
334 #ifndef WX_PRECOMP\n\
335 #include \"wx/wx.h\"\n\
336 #endif\n\
337 \
338 #include \"wx/filesys.h\"\n\
339 #include \"wx/fs_mem.h\"\n\
340 #include \"wx/xml/xmlres.h\"\n\
341 #include \"wx/xml/xh_all.h\"\n\
342 \n");
343
344 for (i = 0; i < flist.Count(); i++)
345 file.Write(FileToCppArray(flist[i], i));
346
347 file.Write("\
348 void " + parFuncname + "()\n\
349 {\n\
350 \n\
351 // Check for memory FS. If not present, load the handler:\n\
352 {\n\
353 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
354 wxFileSystem fsys;\n\
355 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
356 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
357 if (f) delete f;\n\
358 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
359 }\n\
360 \n");
361
362 for (i = 0; i < flist.Count(); i++)
363 {
364 wxString s;
365 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
366 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
367 file.Write(s);
368 }
369
370 for (i = 0; i < parFiles.Count(); i++)
371 {
372 wxString name, ext, path;
373 wxSplitPath(parFiles[i], &path, &name, &ext);
374 file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
375 name + ".xrc" + "\");\n");
376 }
377
378 file.Write("\n}\n");
379
380
381 }