2000-07-25 18:47:21 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wxrc.cpp
|
|
|
|
// Purpose: XML resource compiler
|
|
|
|
// Author: Vaclav Slavik
|
|
|
|
// Created: 2000/03/05
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2000 Vaclav Slavik
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-09-07 12:28:46 +00:00
|
|
|
#if defined(__GNUG__) && !defined(__APPLE__)
|
2000-07-25 18:47:21 +00:00
|
|
|
#pragma implementation
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers (this file is usually all you
|
|
|
|
// need because it includes almost all "standard" wxWindows headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/cmdline.h"
|
2001-06-09 22:18:45 +00:00
|
|
|
#include "wx/xrc/xml.h"
|
2000-07-25 18:47:21 +00:00
|
|
|
#include "wx/ffile.h"
|
2002-12-25 15:23:44 +00:00
|
|
|
#include "wx/filename.h"
|
2000-08-11 19:07:43 +00:00
|
|
|
#include "wx/wfstream.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2000-07-26 12:40:49 +00:00
|
|
|
/*
|
2000-07-25 18:47:21 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
#error "You must compile the resource compiler with wxBase!"
|
|
|
|
#endif
|
2000-07-26 12:40:49 +00:00
|
|
|
*/
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
class XmlResApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2000-07-26 12:40:49 +00:00
|
|
|
|
|
|
|
#if wxUSE_GUI
|
|
|
|
bool OnInit();
|
|
|
|
#else
|
2000-07-25 18:47:21 +00:00
|
|
|
virtual int OnRun();
|
2000-07-26 12:40:49 +00:00
|
|
|
#endif
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void ParseParams(const wxCmdLineParser& cmdline);
|
|
|
|
void CompileRes();
|
|
|
|
wxArrayString PrepareTempFiles();
|
2000-08-11 19:07:43 +00:00
|
|
|
void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
|
|
|
|
|
2002-06-22 23:33:12 +00:00
|
|
|
wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
|
2000-07-25 18:47:21 +00:00
|
|
|
void DeleteTempFiles(const wxArrayString& flist);
|
|
|
|
void MakePackageZIP(const wxArrayString& flist);
|
|
|
|
void MakePackageCPP(const wxArrayString& flist);
|
2002-08-14 11:02:39 +00:00
|
|
|
void MakePackagePython(const wxArrayString& flist);
|
2000-11-24 17:20:20 +00:00
|
|
|
|
|
|
|
void OutputGettext();
|
|
|
|
wxArrayString FindStrings();
|
|
|
|
wxArrayString FindStrings(wxXmlNode *node);
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-08-14 11:02:39 +00:00
|
|
|
bool flagVerbose, flagCPP, flagPython, flagGettext;
|
2000-07-25 18:47:21 +00:00
|
|
|
wxString parOutput, parFuncname, parOutputPath;
|
|
|
|
wxArrayString parFiles;
|
|
|
|
int retCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(XmlResApp)
|
|
|
|
|
2000-07-26 12:40:49 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
bool XmlResApp::OnInit()
|
|
|
|
#else
|
2000-07-25 18:47:21 +00:00
|
|
|
int XmlResApp::OnRun()
|
2000-07-26 12:40:49 +00:00
|
|
|
#endif
|
2000-07-25 18:47:21 +00:00
|
|
|
{
|
|
|
|
static const wxCmdLineEntryDesc cmdLineDesc[] =
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"),
|
2002-07-02 22:06:00 +00:00
|
|
|
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
|
2002-12-04 14:11:26 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
|
|
|
|
{ wxCMD_LINE_OPTION, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
|
|
|
|
{ wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
|
2002-07-02 22:06:00 +00:00
|
|
|
#if 0 // not yet implemented
|
2002-12-04 14:11:26 +00:00
|
|
|
{ wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
|
2002-07-02 22:06:00 +00:00
|
|
|
#endif
|
2002-12-04 14:11:26 +00:00
|
|
|
{ wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"),
|
2002-07-02 22:06:00 +00:00
|
|
|
wxCMD_LINE_VAL_STRING,
|
|
|
|
wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
{ wxCMD_LINE_NONE }
|
|
|
|
};
|
|
|
|
|
|
|
|
wxCmdLineParser parser(cmdLineDesc, argc, argv);
|
|
|
|
|
|
|
|
switch (parser.Parse())
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
retCode = 0;
|
|
|
|
ParseParams(parser);
|
2000-11-24 17:20:20 +00:00
|
|
|
if (flagGettext)
|
|
|
|
OutputGettext();
|
|
|
|
else
|
|
|
|
CompileRes();
|
2000-07-26 12:40:49 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
return FALSE;
|
|
|
|
#else
|
2000-07-25 18:47:21 +00:00
|
|
|
return retCode;
|
2000-07-26 12:40:49 +00:00
|
|
|
#endif
|
2000-07-25 18:47:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-07-26 12:40:49 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
return FALSE;
|
|
|
|
#else
|
2000-07-25 18:47:21 +00:00
|
|
|
return 1;
|
2000-07-26 12:40:49 +00:00
|
|
|
#endif
|
2000-07-25 18:47:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
flagGettext = cmdline.Found(_T("g"));
|
|
|
|
flagVerbose = cmdline.Found(_T("v"));
|
|
|
|
flagCPP = cmdline.Found(_T("c"));
|
|
|
|
flagPython = cmdline.Found(_T("p"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
if (!cmdline.Found(_T("o"), &parOutput))
|
2000-11-24 17:20:20 +00:00
|
|
|
{
|
|
|
|
if (flagGettext)
|
|
|
|
parOutput = wxEmptyString;
|
|
|
|
else
|
2002-08-14 11:02:39 +00:00
|
|
|
{
|
|
|
|
if (flagCPP)
|
2002-12-04 14:11:26 +00:00
|
|
|
parOutput = _T("resource.cpp");
|
2002-08-14 11:02:39 +00:00
|
|
|
else if (flagPython)
|
2002-12-04 14:11:26 +00:00
|
|
|
parOutput = _T("resource.py");
|
2002-08-14 11:02:39 +00:00
|
|
|
else
|
2002-12-04 14:11:26 +00:00
|
|
|
parOutput = _T("resource.xrs");
|
2002-08-14 11:02:39 +00:00
|
|
|
}
|
2000-11-24 17:20:20 +00:00
|
|
|
}
|
2002-12-25 15:23:44 +00:00
|
|
|
wxFileName fn(parOutput);
|
|
|
|
fn.Normalize();
|
|
|
|
parOutput = fn.GetFullPath();
|
2000-07-25 18:47:21 +00:00
|
|
|
parOutputPath = wxPathOnly(parOutput);
|
2002-12-04 14:11:26 +00:00
|
|
|
if (!parOutputPath) parOutputPath = _T(".");
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
if (!cmdline.Found(_T("n"), &parFuncname))
|
|
|
|
parFuncname = _T("InitXmlResource");
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < cmdline.GetParamCount(); i++)
|
|
|
|
parFiles.Add(cmdline.GetParam(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::CompileRes()
|
|
|
|
{
|
|
|
|
wxArrayString files = PrepareTempFiles();
|
|
|
|
|
|
|
|
wxRemoveFile(parOutput);
|
|
|
|
|
|
|
|
if (!retCode)
|
|
|
|
{
|
|
|
|
if (flagCPP)
|
|
|
|
MakePackageCPP(files);
|
2002-08-14 11:02:39 +00:00
|
|
|
else if (flagPython)
|
|
|
|
MakePackagePython(files);
|
2000-07-25 18:47:21 +00:00
|
|
|
else
|
|
|
|
MakePackageZIP(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteTempFiles(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-22 23:33:12 +00:00
|
|
|
wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
|
|
|
|
{
|
|
|
|
wxString name2 = name;
|
2002-12-04 14:11:26 +00:00
|
|
|
name2.Replace(_T(":"), _T("_"));
|
|
|
|
name2.Replace(_T("/"), _T("_"));
|
|
|
|
name2.Replace(_T("\\"), _T("_"));
|
|
|
|
name2.Replace(_T("*"), _T("_"));
|
|
|
|
name2.Replace(_T("?"), _T("_"));
|
2002-06-22 23:33:12 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2;
|
2002-06-22 23:33:12 +00:00
|
|
|
|
|
|
|
if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
|
|
|
|
{
|
|
|
|
for (int i = 0;; i++)
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i);
|
2002-06-22 23:33:12 +00:00
|
|
|
if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
wxArrayString XmlResApp::PrepareTempFiles()
|
|
|
|
{
|
|
|
|
wxArrayString flist;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < parFiles.Count(); i++)
|
|
|
|
{
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
wxXmlDocument doc;
|
|
|
|
|
|
|
|
if (!doc.Load(parFiles[i]))
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxLogError(_T("Error parsing file ") + parFiles[i]);
|
2000-07-25 18:47:21 +00:00
|
|
|
retCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
wxString name, ext, path;
|
|
|
|
wxSplitPath(parFiles[i], &path, &name, &ext);
|
|
|
|
|
|
|
|
FindFilesInXML(doc.GetRoot(), flist, path);
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-06-22 23:33:12 +00:00
|
|
|
wxString internalName = GetInternalFileName(parFiles[i], flist);
|
|
|
|
|
2002-12-25 15:23:44 +00:00
|
|
|
doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
|
2002-06-22 23:33:12 +00:00
|
|
|
flist.Add(internalName);
|
2000-07-25 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return flist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-25 15:23:44 +00:00
|
|
|
// Does 'node' contain filename information at all?
|
|
|
|
static bool NodeContainsFilename(wxXmlNode *node)
|
|
|
|
{
|
|
|
|
// Any bitmaps:
|
|
|
|
if (node->GetName() == _T("bitmap"))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// URLs in wxHtmlWindow:
|
|
|
|
if (node->GetName() == _T("url"))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// wxBitmapButton:
|
|
|
|
wxXmlNode *parent = node->GetParent();
|
|
|
|
if (parent != NULL &&
|
|
|
|
parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
|
|
|
|
(node->GetName() == _T("focus") ||
|
|
|
|
node->GetName() == _T("disabled") ||
|
|
|
|
node->GetName() == _T("selected")))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// wxBitmap or wxIcon toplevel resources:
|
|
|
|
if (node->GetName() == _T("object"))
|
|
|
|
{
|
|
|
|
wxString klass = node->GetPropVal(_T("class"), wxEmptyString);
|
|
|
|
if (klass == _T("wxBitmap") || klass == _T("wxIcon"))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
|
|
|
|
void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
// Is 'node' XML node element?
|
|
|
|
if (node == NULL) return;
|
|
|
|
if (node->GetType() != wxXML_ELEMENT_NODE) return;
|
|
|
|
|
2002-12-25 15:23:44 +00:00
|
|
|
bool containsFilename = NodeContainsFilename(node);
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
wxXmlNode *n = node->GetChildren();
|
2000-08-11 19:07:43 +00:00
|
|
|
while (n)
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
if (containsFilename &&
|
2000-08-11 19:07:43 +00:00
|
|
|
(n->GetType() == wxXML_TEXT_NODE ||
|
2002-12-04 14:11:26 +00:00
|
|
|
n->GetType() == wxXML_CDATA_SECTION_NODE))
|
2000-08-11 19:07:43 +00:00
|
|
|
{
|
|
|
|
wxString fullname;
|
2002-12-04 14:11:26 +00:00
|
|
|
if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
|
|
|
|
fullname = n->GetContent();
|
|
|
|
else
|
2002-12-25 15:23:44 +00:00
|
|
|
fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
|
2002-06-22 23:33:12 +00:00
|
|
|
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("adding ") + fullname + _T("...\n"));
|
|
|
|
|
2002-06-22 23:33:12 +00:00
|
|
|
wxString filename = GetInternalFileName(n->GetContent(), flist);
|
2000-08-11 19:07:43 +00:00
|
|
|
n->SetContent(filename);
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
if (flist.Index(filename) == wxNOT_FOUND)
|
|
|
|
flist.Add(filename);
|
2000-08-11 19:07:43 +00:00
|
|
|
|
|
|
|
wxFileInputStream sin(fullname);
|
2002-12-25 15:23:44 +00:00
|
|
|
wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
|
2000-08-11 19:07:43 +00:00
|
|
|
sin.Read(sout); // copy the stream
|
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
// subnodes:
|
|
|
|
if (n->GetType() == wxXML_ELEMENT_NODE)
|
|
|
|
FindFilesInXML(n, flist, inputPath);
|
2002-12-04 14:11:26 +00:00
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
n = n->GetNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-25 18:47:21 +00:00
|
|
|
void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < flist.Count(); i++)
|
2002-12-25 15:23:44 +00:00
|
|
|
wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
|
2000-07-25 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackageZIP(const wxArrayString& flist)
|
|
|
|
{
|
|
|
|
wxString files;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < flist.Count(); i++)
|
2002-12-04 14:11:26 +00:00
|
|
|
files += flist[i] + _T(" ");
|
2000-07-25 18:47:21 +00:00
|
|
|
files.RemoveLast();
|
|
|
|
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("compressing ") + parOutput + _T("...\n"));
|
2002-12-25 15:23:44 +00:00
|
|
|
|
|
|
|
wxString cwd = wxGetCwd();
|
|
|
|
wxSetWorkingDirectory(parOutputPath);
|
|
|
|
int execres = wxExecute(_T("zip -9 -j ") +
|
|
|
|
wxString(flagVerbose ? _T("") : _T("-q ")) +
|
|
|
|
parOutput + _T(" ") + files, TRUE);
|
|
|
|
wxSetWorkingDirectory(cwd);
|
|
|
|
if (execres == -1)
|
2000-07-25 18:47:21 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
|
|
|
|
wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
|
2000-07-25 18:47:21 +00:00
|
|
|
retCode = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static wxString FileToCppArray(wxString filename, int num)
|
|
|
|
{
|
|
|
|
wxString output;
|
|
|
|
wxString tmp;
|
2000-08-11 19:07:43 +00:00
|
|
|
wxString snum;
|
2003-03-16 20:27:15 +00:00
|
|
|
wxFFile file(filename, wxT("rb"));
|
2000-07-25 18:47:21 +00:00
|
|
|
size_t lng = file.Length();
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
snum.Printf(_T("%i"), num);
|
|
|
|
output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng);
|
|
|
|
output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n");
|
2000-10-07 21:56:37 +00:00
|
|
|
// we cannot use string literals because MSVC is dumb wannabe compiler
|
|
|
|
// with arbitrary limitation to 2048 strings :(
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
unsigned char *buffer = new unsigned char[lng];
|
|
|
|
file.Read(buffer, lng);
|
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
for (size_t i = 0, linelng = 0; i < lng; i++)
|
2000-07-25 18:47:21 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
tmp.Printf(_T("%i"), buffer[i]);
|
|
|
|
if (i != 0) output << _T(',');
|
2000-10-07 21:56:37 +00:00
|
|
|
if (linelng > 70)
|
2000-08-11 19:07:43 +00:00
|
|
|
{
|
|
|
|
linelng = 0;
|
2002-12-04 14:11:26 +00:00
|
|
|
output << _T("\n");
|
2000-08-11 19:07:43 +00:00
|
|
|
}
|
2000-10-07 21:56:37 +00:00
|
|
|
output << tmp;
|
|
|
|
linelng += tmp.Length()+1;
|
2000-07-25 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
output += _T("};\n\n");
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackageCPP(const wxArrayString& flist)
|
|
|
|
{
|
2003-03-16 20:27:15 +00:00
|
|
|
wxFFile file(parOutput, wxT("wt"));
|
2000-07-25 18:47:21 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
file.Write(_T("")
|
|
|
|
_T("//\n")
|
|
|
|
_T("// This file was automatically generated by wxrc, do not edit by hand.\n")
|
|
|
|
_T("//\n\n")
|
|
|
|
_T("#include <wx/wxprec.h>\n")
|
|
|
|
_T("\n")
|
|
|
|
_T("#ifdef __BORLANDC__\n")
|
|
|
|
_T(" #pragma hdrstop\n")
|
|
|
|
_T("#endif\n")
|
|
|
|
_T("\n")
|
|
|
|
_T("#ifndef WX_PRECOMP\n")
|
|
|
|
_T(" #include <wx/wx.h>\n")
|
|
|
|
_T("#endif\n")
|
|
|
|
_T("")
|
|
|
|
_T("#include <wx/filesys.h>\n")
|
|
|
|
_T("#include <wx/fs_mem.h>\n")
|
|
|
|
_T("#include <wx/xrc/xmlres.h>\n")
|
|
|
|
_T("#include <wx/xrc/xh_all.h>\n")
|
|
|
|
_T("\n"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
for (i = 0; i < flist.Count(); i++)
|
2002-12-25 15:23:44 +00:00
|
|
|
file.Write(
|
|
|
|
FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
file.Write(_T("")
|
2003-03-16 20:27:15 +00:00
|
|
|
_T("void ") + parFuncname + wxT("()\n")
|
2002-12-04 14:11:26 +00:00
|
|
|
_T("{\n")
|
|
|
|
_T("\n")
|
|
|
|
_T(" // Check for memory FS. If not present, load the handler:\n")
|
|
|
|
_T(" {\n")
|
2003-03-16 20:27:15 +00:00
|
|
|
_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
|
2002-12-04 14:11:26 +00:00
|
|
|
_T(" wxFileSystem fsys;\n")
|
2003-03-16 20:27:15 +00:00
|
|
|
_T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
|
|
|
|
_T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
|
2002-12-04 14:11:26 +00:00
|
|
|
_T(" if (f) delete f;\n")
|
|
|
|
_T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
|
|
|
|
_T(" }\n")
|
|
|
|
_T("\n"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
|
|
|
for (i = 0; i < flist.Count(); i++)
|
|
|
|
{
|
|
|
|
wxString s;
|
2003-03-16 20:27:15 +00:00
|
|
|
s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] +
|
|
|
|
_T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i);
|
2000-07-25 18:47:21 +00:00
|
|
|
file.Write(s);
|
|
|
|
}
|
2000-08-11 19:07:43 +00:00
|
|
|
|
|
|
|
for (i = 0; i < parFiles.Count(); i++)
|
|
|
|
{
|
2003-03-16 20:27:15 +00:00
|
|
|
file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
|
|
|
|
GetInternalFileName(parFiles[i], flist) + _T("\"));\n"));
|
2000-08-11 19:07:43 +00:00
|
|
|
}
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
file.Write(_T("}\n"));
|
2000-07-25 18:47:21 +00:00
|
|
|
|
2000-08-11 19:07:43 +00:00
|
|
|
|
2000-07-25 18:47:21 +00:00
|
|
|
}
|
2000-11-24 17:20:20 +00:00
|
|
|
|
2002-08-14 11:02:39 +00:00
|
|
|
static wxString FileToPythonArray(wxString filename, int num)
|
|
|
|
{
|
|
|
|
wxString output;
|
|
|
|
wxString tmp;
|
|
|
|
wxString snum;
|
2003-03-16 20:27:15 +00:00
|
|
|
wxFFile file(filename, wxT("rb"));
|
2002-08-14 11:02:39 +00:00
|
|
|
size_t lng = file.Length();
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
snum.Printf(_T("%i"), num);
|
|
|
|
output = _T(" xml_res_file_") + snum + _T(" = \"\"\"\\\n");
|
2002-08-14 11:02:39 +00:00
|
|
|
|
|
|
|
unsigned char *buffer = new unsigned char[lng];
|
|
|
|
file.Read(buffer, lng);
|
|
|
|
|
|
|
|
for (size_t i = 0, linelng = 0; i < lng; i++)
|
|
|
|
{
|
|
|
|
unsigned char c = buffer[i];
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
tmp = (wxChar)c;
|
|
|
|
linelng = 0;
|
|
|
|
}
|
|
|
|
else if (c < 32 || c > 127)
|
2002-12-04 14:11:26 +00:00
|
|
|
tmp.Printf(_T("\\x%02x"), c);
|
2002-08-14 11:02:39 +00:00
|
|
|
else if (c == '\\')
|
2002-12-04 14:11:26 +00:00
|
|
|
tmp = _T("\\\\");
|
2002-08-14 11:02:39 +00:00
|
|
|
else
|
|
|
|
tmp = (wxChar)c;
|
|
|
|
if (linelng > 70)
|
|
|
|
{
|
|
|
|
linelng = 0;
|
2002-12-04 14:11:26 +00:00
|
|
|
output << _T("\\\n");
|
2002-08-14 11:02:39 +00:00
|
|
|
}
|
|
|
|
output << tmp;
|
|
|
|
linelng += tmp.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
output += _T("\"\"\"\n\n");
|
2002-08-14 11:02:39 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackagePython(const wxArrayString& flist)
|
|
|
|
{
|
2003-03-16 20:27:15 +00:00
|
|
|
wxFFile file(parOutput, wxT("wt"));
|
2002-08-14 11:02:39 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
|
2002-08-14 11:02:39 +00:00
|
|
|
|
|
|
|
file.Write(
|
2002-12-04 14:11:26 +00:00
|
|
|
_T("#\n")
|
|
|
|
_T("# This file was automatically generated by wxrc, do not edit by hand.\n")
|
|
|
|
_T("#\n\n")
|
|
|
|
_T("from wxPython.wx import *\n")
|
|
|
|
_T("from wxPython.xrc import *\n\n")
|
2002-08-14 11:02:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
file.Write(_T("def ") + parFuncname + _T("():\n"));
|
2002-08-14 11:02:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < flist.Count(); i++)
|
2002-12-25 15:23:44 +00:00
|
|
|
file.Write(
|
|
|
|
FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
|
2002-08-14 11:02:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < flist.Count(); i++)
|
|
|
|
{
|
|
|
|
wxString s;
|
2002-12-04 14:11:26 +00:00
|
|
|
s.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i);
|
2002-08-14 11:02:39 +00:00
|
|
|
file.Write(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-24 17:20:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::OutputGettext()
|
|
|
|
{
|
|
|
|
wxArrayString str = FindStrings();
|
|
|
|
|
|
|
|
wxFFile fout;
|
|
|
|
if (!parOutput) fout.Attach(stdout);
|
2003-03-16 20:27:15 +00:00
|
|
|
else fout.Open(parOutput, wxT("wt"));
|
2000-11-24 17:20:20 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < str.GetCount(); i++)
|
2002-08-31 15:37:20 +00:00
|
|
|
fout.Write(_T("_(\"") + str[i] + _T("\");\n"));
|
2000-11-24 17:20:20 +00:00
|
|
|
|
|
|
|
if (!parOutput) fout.Detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wxArrayString XmlResApp::FindStrings()
|
|
|
|
{
|
|
|
|
wxArrayString arr, a2;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < parFiles.Count(); i++)
|
|
|
|
{
|
|
|
|
if (flagVerbose)
|
2002-12-04 14:11:26 +00:00
|
|
|
wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
|
2000-11-24 17:20:20 +00:00
|
|
|
|
|
|
|
wxXmlDocument doc;
|
|
|
|
if (!doc.Load(parFiles[i]))
|
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxLogError(_T("Error parsing file ") + parFiles[i]);
|
2000-11-24 17:20:20 +00:00
|
|
|
retCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
a2 = FindStrings(doc.GetRoot());
|
|
|
|
WX_APPEND_ARRAY(arr, a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-03-09 16:35:00 +00:00
|
|
|
static wxString ConvertText(const wxString& str)
|
|
|
|
{
|
|
|
|
wxString str2;
|
|
|
|
const wxChar *dt;
|
|
|
|
|
|
|
|
for (dt = str.c_str(); *dt; dt++)
|
|
|
|
{
|
|
|
|
if (*dt == wxT('_'))
|
|
|
|
{
|
|
|
|
if ( *(++dt) == wxT('_') )
|
|
|
|
str2 << wxT('_');
|
|
|
|
else
|
|
|
|
str2 << wxT('&') << *dt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (*dt)
|
|
|
|
{
|
|
|
|
case wxT('\n') : str2 << wxT("\\n"); break;
|
|
|
|
case wxT('\t') : str2 << wxT("\\t"); break;
|
|
|
|
case wxT('\r') : str2 << wxT("\\r"); break;
|
2002-12-04 14:11:26 +00:00
|
|
|
case wxT('\\') : if ((*(dt+1) != 'n') &&
|
|
|
|
(*(dt+1) != 't') &&
|
|
|
|
(*(dt+1) != 'r'))
|
|
|
|
str2 << wxT("\\\\");
|
|
|
|
else
|
|
|
|
str2 << wxT("\\");
|
|
|
|
break;
|
2002-08-23 18:52:31 +00:00
|
|
|
case wxT('"') : str2 << wxT("\\\""); break;
|
2002-03-09 16:35:00 +00:00
|
|
|
default : str2 << *dt; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-24 17:20:20 +00:00
|
|
|
wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
|
|
|
|
{
|
|
|
|
wxArrayString arr;
|
|
|
|
|
|
|
|
wxXmlNode *n = node;
|
|
|
|
if (n == NULL) return arr;
|
|
|
|
n = n->GetChildren();
|
|
|
|
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
if ((node->GetType() == wxXML_ELEMENT_NODE) &&
|
|
|
|
// parent is an element, i.e. has subnodes...
|
|
|
|
(n->GetType() == wxXML_TEXT_NODE ||
|
|
|
|
n->GetType() == wxXML_CDATA_SECTION_NODE) &&
|
|
|
|
// ...it is textnode...
|
|
|
|
(
|
|
|
|
node/*not n!*/->GetName() == _T("label") ||
|
|
|
|
(node/*not n!*/->GetName() == _T("value") &&
|
|
|
|
!n->GetContent().IsNumber()) ||
|
|
|
|
node/*not n!*/->GetName() == _T("help") ||
|
|
|
|
node/*not n!*/->GetName() == _T("longhelp") ||
|
|
|
|
node/*not n!*/->GetName() == _T("tooltip") ||
|
|
|
|
node/*not n!*/->GetName() == _T("htmlcode") ||
|
2002-08-31 15:37:20 +00:00
|
|
|
node/*not n!*/->GetName() == _T("title") ||
|
|
|
|
node/*not n!*/->GetName() == _T("item")
|
2000-11-24 17:20:20 +00:00
|
|
|
))
|
2002-03-09 16:35:00 +00:00
|
|
|
// ...and known to contain translatable string
|
2000-11-24 17:20:20 +00:00
|
|
|
{
|
2002-03-09 16:35:00 +00:00
|
|
|
arr.Add(ConvertText(n->GetContent()));
|
2000-11-24 17:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// subnodes:
|
|
|
|
if (n->GetType() == wxXML_ELEMENT_NODE)
|
|
|
|
{
|
|
|
|
wxArrayString a2 = FindStrings(n);
|
|
|
|
WX_APPEND_ARRAY(arr, a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = n->GetNext();
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|