]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
use SetForegroundWindow instead of BringWindowToTop
[wxWidgets.git] / utils / wxrc / wxrc.cpp
CommitLineData
56d2f750
VS
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"
a793c17b 31#include "wx/xml/xmlio.h"
56d2f750 32#include "wx/ffile.h"
f6853b4a
VS
33#include "wx/wfstream.h"
34
35
36
37
56d2f750 38
031dfec8 39/*
56d2f750
VS
40#if wxUSE_GUI
41#error "You must compile the resource compiler with wxBase!"
42#endif
031dfec8 43*/
56d2f750
VS
44
45class XmlResApp : public wxApp
46{
47public:
031dfec8
JS
48
49#if wxUSE_GUI
50 bool OnInit();
51#else
56d2f750 52 virtual int OnRun();
031dfec8 53#endif
56d2f750
VS
54
55private:
56
57 void ParseParams(const wxCmdLineParser& cmdline);
58 void CompileRes();
59 wxArrayString PrepareTempFiles();
f6853b4a
VS
60 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
61
56d2f750
VS
62 void DeleteTempFiles(const wxArrayString& flist);
63 void MakePackageZIP(const wxArrayString& flist);
64 void MakePackageCPP(const wxArrayString& flist);
c8b7a961
VS
65
66 void OutputGettext();
67 wxArrayString FindStrings();
68 wxArrayString FindStrings(wxXmlNode *node);
56d2f750 69
c8b7a961 70 bool flagVerbose, flagCPP, flagCompress, flagGettext;
56d2f750
VS
71 wxString parOutput, parFuncname, parOutputPath;
72 wxArrayString parFiles;
73 int retCode;
74};
75
76IMPLEMENT_APP(XmlResApp)
77
031dfec8
JS
78#if wxUSE_GUI
79bool XmlResApp::OnInit()
80#else
56d2f750 81int XmlResApp::OnRun()
031dfec8 82#endif
56d2f750
VS
83{
84 static const wxCmdLineEntryDesc cmdLineDesc[] =
85 {
c8b7a961 86 { wxCMD_LINE_SWITCH, "h", "help", "show help message" },
56d2f750
VS
87 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
88 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
89 { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" },
c8b7a961 90 { wxCMD_LINE_SWITCH, "g", "gettext", "output .po catalog (to stdout or file if -o used)" },
56d2f750 91 { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
e066e256 92 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" },
a793c17b 93 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
56d2f750 94
c8b7a961 95 { wxCMD_LINE_PARAM, NULL, NULL, "input file(s)",
56d2f750
VS
96 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
97
98 { wxCMD_LINE_NONE }
99 };
100
cb0b3950
VS
101#if wxUSE_GUI
102 // VS: I need reasonable output to console from wxCmdLineParser
103 wxLog::SetTimestamp(NULL);
104 delete wxLog::SetActiveTarget(new wxLogStderr);
105#endif
106
a793c17b
VS
107 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ);
108
56d2f750
VS
109 wxCmdLineParser parser(cmdLineDesc, argc, argv);
110
111 switch (parser.Parse())
112 {
113 case -1:
114 return 0;
115 break;
116
117 case 0:
118 retCode = 0;
119 ParseParams(parser);
c8b7a961
VS
120 if (flagGettext)
121 OutputGettext();
122 else
123 CompileRes();
031dfec8
JS
124#if wxUSE_GUI
125 return FALSE;
126#else
56d2f750 127 return retCode;
031dfec8 128#endif
56d2f750
VS
129 break;
130
131 default:
031dfec8
JS
132#if wxUSE_GUI
133 return FALSE;
134#else
56d2f750 135 return 1;
031dfec8 136#endif
56d2f750
VS
137 break;
138 }
139}
140
141
142
143
144void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
145{
c8b7a961 146 flagGettext = cmdline.Found("g");
56d2f750
VS
147 flagVerbose = cmdline.Found("v");
148 flagCPP = cmdline.Found("c");
149 flagCompress = flagCPP && !cmdline.Found("u");
150
151 if (!cmdline.Found("o", &parOutput))
c8b7a961
VS
152 {
153 if (flagGettext)
154 parOutput = wxEmptyString;
155 else
156 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
157 }
56d2f750
VS
158 parOutputPath = wxPathOnly(parOutput);
159 if (!parOutputPath) parOutputPath = ".";
160
161 if (!cmdline.Found("n", &parFuncname))
162 parFuncname = "InitXmlResource";
163
164 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
165 parFiles.Add(cmdline.GetParam(i));
166}
167
168
169
170
171void XmlResApp::CompileRes()
172{
173 wxArrayString files = PrepareTempFiles();
174
175 wxRemoveFile(parOutput);
176
56d2f750
VS
177 if (!retCode)
178 {
179 if (flagCPP)
180 MakePackageCPP(files);
181 else
182 MakePackageZIP(files);
183 }
184
185 DeleteTempFiles(files);
186}
187
188
189
190wxArrayString XmlResApp::PrepareTempFiles()
191{
192 wxArrayString flist;
193
194 for (size_t i = 0; i < parFiles.Count(); i++)
195 {
196 if (flagVerbose)
197 wxPrintf("processing " + parFiles[i] + "...\n");
198
199 wxXmlDocument doc;
200
201 if (!doc.Load(parFiles[i]))
202 {
203 wxLogError("Error parsing file " + parFiles[i]);
204 retCode = 1;
205 continue;
206 }
207
f6853b4a
VS
208 wxString name, ext, path;
209 wxSplitPath(parFiles[i], &path, &name, &ext);
210
211 FindFilesInXML(doc.GetRoot(), flist, path);
56d2f750 212
e066e256
VS
213 doc.Save(parOutputPath + "/" + name + ".xrc", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
214 flist.Add(name + ".xrc");
56d2f750
VS
215 }
216
217 return flist;
218}
219
220
221
f6853b4a
VS
222// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
223void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
224{
225 wxXmlNode *n = node;
226 if (n == NULL) return;
227 n = n->GetChildren();
228
229 while (n)
230 {
231 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
232 // parent is an element, i.e. has subnodes...
233 (n->GetType() == wxXML_TEXT_NODE ||
234 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
235 // ...it is textnode...
236 (node/*not n!*/->GetName() == "bitmap"))
237 // ...and known to contain filename
238 {
239 wxString fullname;
240 wxString filename = n->GetContent();
241 if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
242 else fullname = inputPath + "/" + n->GetContent();
243
244 filename.Replace("/", "_");
245 filename.Replace("\\", "_");
246 filename.Replace("*", "_");
247 filename.Replace("?", "_");
248 n->SetContent(filename);
249
250 if (flagVerbose)
251 wxPrintf("adding " + filename + "...\n");
252
253 flist.Add(filename);
254
255 wxFileInputStream sin(fullname);
256 wxFileOutputStream sout(parOutputPath + "/" + filename);
257 sin.Read(sout); // copy the stream
258 }
259
260 // subnodes:
261 if (n->GetType() == wxXML_ELEMENT_NODE)
262 FindFilesInXML(n, flist, inputPath);
263
264 n = n->GetNext();
265 }
266}
267
268
269
56d2f750
VS
270void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
271{
272 for (size_t i = 0; i < flist.Count(); i++)
273 wxRemoveFile(parOutputPath + "/" + flist[i]);
274}
275
276
277
278void XmlResApp::MakePackageZIP(const wxArrayString& flist)
279{
280 wxString files;
281
282 for (size_t i = 0; i < flist.Count(); i++)
283 files += flist[i] + " ";
284 files.RemoveLast();
285
286 if (flagVerbose)
287 wxPrintf("compressing " + parOutput + "...\n");
288
289 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
290 parOutput + " " + files, TRUE) == -1)
291 {
292 wxLogError("Unable to execute zip program. Make sure it is in the path.");
293 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
294 retCode = 1;
295 return;
296 }
297}
298
299
300
301
302static wxString FileToCppArray(wxString filename, int num)
303{
304 wxString output;
56d2f750 305 wxString tmp;
f6853b4a 306 wxString snum;
56d2f750
VS
307 wxFFile file(filename, "rb");
308 size_t lng = file.Length();
309
310 snum.Printf("%i", num);
311 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
e066e256
VS
312 output += "static unsigned char xml_res_file_" + snum + "[] = {\n";
313 // we cannot use string literals because MSVC is dumb wannabe compiler
314 // with arbitrary limitation to 2048 strings :(
56d2f750
VS
315
316 unsigned char *buffer = new unsigned char[lng];
317 file.Read(buffer, lng);
318
f6853b4a 319 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 320 {
e066e256
VS
321 tmp.Printf("%i", buffer[i]);
322 if (i != 0) output << ',';
323 if (linelng > 70)
f6853b4a
VS
324 {
325 linelng = 0;
e066e256 326 output << "\n";
f6853b4a 327 }
e066e256
VS
328 output << tmp;
329 linelng += tmp.Length()+1;
56d2f750
VS
330 }
331
332 delete[] buffer;
333
e066e256 334 output += "};\n\n";
56d2f750
VS
335
336 return output;
337}
338
339
340void XmlResApp::MakePackageCPP(const wxArrayString& flist)
341{
342 wxFFile file(parOutput, "wt");
343 size_t i;
344
345 if (flagVerbose)
346 wxPrintf("creating C++ source file " + parOutput + "...\n");
347
348 file.Write("\
349#include \"wx/wxprec.h\"\n\
350\n\
351#ifdef __BORLANDC__\n\
352 #pragma hdrstop\n\
353#endif\n\
354\n\
355#ifndef WX_PRECOMP\n\
356 #include \"wx/wx.h\"\n\
357#endif\n\
358\
359#include \"wx/filesys.h\"\n\
360#include \"wx/fs_mem.h\"\n\
361#include \"wx/xml/xmlres.h\"\n\
362#include \"wx/xml/xh_all.h\"\n\
363\n");
364
365 for (i = 0; i < flist.Count(); i++)
366 file.Write(FileToCppArray(flist[i], i));
367
368 file.Write("\
369void " + parFuncname + "()\n\
370{\n\
371\n\
372 // Check for memory FS. If not present, load the handler:\n\
373 {\n\
374 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
375 wxFileSystem fsys;\n\
a793c17b 376 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
56d2f750
VS
377 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
378 if (f) delete f;\n\
379 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
380 }\n\
381\n");
382
383 for (i = 0; i < flist.Count(); i++)
384 {
385 wxString s;
386 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
f6853b4a 387 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
56d2f750
VS
388 file.Write(s);
389 }
f6853b4a
VS
390
391 for (i = 0; i < parFiles.Count(); i++)
392 {
393 wxString name, ext, path;
394 wxSplitPath(parFiles[i], &path, &name, &ext);
395 file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
e066e256 396 name + ".xrc" + "\");\n");
f6853b4a 397 }
56d2f750
VS
398
399 file.Write("\n}\n");
400
f6853b4a 401
56d2f750 402}
c8b7a961
VS
403
404
405
406void XmlResApp::OutputGettext()
407{
408 wxArrayString str = FindStrings();
409
410 wxFFile fout;
411 if (!parOutput) fout.Attach(stdout);
412 else fout.Open(parOutput, _T("wt"));
413
414 for (size_t i = 0; i < str.GetCount(); i++)
415 fout.Write(_T("msgid \"") + str[i] + _T("\"\nmsgstr \"\"\n\n"));
416
417 if (!parOutput) fout.Detach();
418}
419
420
421
422wxArrayString XmlResApp::FindStrings()
423{
424 wxArrayString arr, a2;
425
426 for (size_t i = 0; i < parFiles.Count(); i++)
427 {
428 if (flagVerbose)
429 wxPrintf("processing " + parFiles[i] + "...\n");
430
431 wxXmlDocument doc;
432 if (!doc.Load(parFiles[i]))
433 {
434 wxLogError("Error parsing file " + parFiles[i]);
435 retCode = 1;
436 continue;
437 }
438 a2 = FindStrings(doc.GetRoot());
439 WX_APPEND_ARRAY(arr, a2);
440 }
441
442 return arr;
443}
444
445
446
447wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
448{
449 wxArrayString arr;
450
451 wxXmlNode *n = node;
452 if (n == NULL) return arr;
453 n = n->GetChildren();
454
455 while (n)
456 {
457 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
458 // parent is an element, i.e. has subnodes...
459 (n->GetType() == wxXML_TEXT_NODE ||
460 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
461 // ...it is textnode...
462 (
463 node/*not n!*/->GetName() == _T("label") ||
464 (node/*not n!*/->GetName() == _T("value") &&
465 !n->GetContent().IsNumber()) ||
466 node/*not n!*/->GetName() == _T("help") ||
467 node/*not n!*/->GetName() == _T("longhelp") ||
468 node/*not n!*/->GetName() == _T("tooltip") ||
469 node/*not n!*/->GetName() == _T("htmlcode") ||
470 node/*not n!*/->GetName() == _T("title")
471 ))
472 // ...and known to contain filename
473 {
474 arr.Add(n->GetContent());
475 }
476
477 // subnodes:
478 if (n->GetType() == wxXML_ELEMENT_NODE)
479 {
480 wxArrayString a2 = FindStrings(n);
481 WX_APPEND_ARRAY(arr, a2);
482 }
483
484 n = n->GetNext();
485 }
486 return arr;
487}